diff --git a/.codacy.yaml b/.codacy.yaml new file mode 100644 index 000000000..28293b226 --- /dev/null +++ b/.codacy.yaml @@ -0,0 +1,3 @@ +--- +exclude_paths: + - 'docs/**/*' diff --git a/.editorconfig b/.editorconfig index 92cbdb1d6..5b84cc53b 100644 --- a/.editorconfig +++ b/.editorconfig @@ -33,3 +33,7 @@ indent_size = 2 end_of_line = lf [*.{cmd, bat}] end_of_line = crlf + +[*.yml] +indent_style = space +indent_size = 2 diff --git a/.github/ISSUE_TEMPLATE.md b/.github/ISSUE_TEMPLATE.md deleted file mode 100644 index 5a6112fdf..000000000 --- a/.github/ISSUE_TEMPLATE.md +++ /dev/null @@ -1,25 +0,0 @@ -### Steps to reproduce -1. -2. -3. - -### Expected behavior -Tell us what should happen - -### Actual behavior -Tell us what happens instead - -### Version of SharpZipLib - -### Obtained from (place an x between the brackets for all that apply) -- [ ] Compiled from source - - branch: _______ - - commit: _______ -- [ ] Downloaded DLL from GitHub -- [ ] Downloaded DLL from SourceForge -- [ ] Downloaded DLL from _______ -- [ ] DLL included as part of -- Package installed using: - - [ ] NuGet - - [ ] MyGet - - [ ] Chocolatey diff --git a/.github/ISSUE_TEMPLATE/bug.yml b/.github/ISSUE_TEMPLATE/bug.yml new file mode 100644 index 000000000..a1620f07a --- /dev/null +++ b/.github/ISSUE_TEMPLATE/bug.yml @@ -0,0 +1,92 @@ +name: 🐛 Bug report +description: Create a report to help us improve +labels: ["bug"] + +body: + - type: textarea + id: description + attributes: + label: Describe the bug + description: A clear and concise description of what the bug is + validations: + required: true + + - type: input + id: reproduce-code + attributes: + description: | + If possible, the best way to display an issue is by making a reproducable code snippet available at jsfiddle. + Create a dotnet fiddle which reproduces your issue. You can use [this template](https://p1k.se/sharpziplib-repro) or [create a new one](https://dotnetfiddle.net/). + placeholder: https://dotnetfiddle.net/r39r0c0d3 + label: Reproduction Code + + - type: textarea + id: reproduce-steps + attributes: + label: Steps to reproduce + description: Steps to reproduce the behavior + placeholder: | + 1. Go to '...' + 2. Click on '....' + 3. Scroll down to '....' + 4. See error + validations: + required: true + + - id: expected + type: textarea + attributes: + label: Expected behavior + description: A clear and concise description of what you expected to happen. + validations: + required: true + + - id: operating-system + type: dropdown + attributes: + label: Operating System + multiple: true + options: + - Windows + - macOS + - Linux + validations: + required: false + + - id: framework + type: dropdown + attributes: + label: Framework Version + multiple: true + options: + - .NET 7 + - .NET 6 + - .NET 5 + - .NET Core v3 and earlier + - .NET Framework 4.x + - Unity + - Other + validations: + required: false + + - id: tags + type: dropdown + attributes: + label: Tags + description: What areas are your issue related to? + multiple: true + options: + - ZIP + - GZip + - Tar + - BZip2 + - Encoding + - Encryption + - Documentation + - Async + - Performance + + - type: textarea + attributes: + label: Additional context + description: Add any other context about the problem here. diff --git a/.github/ISSUE_TEMPLATE/config.yml b/.github/ISSUE_TEMPLATE/config.yml new file mode 100644 index 000000000..5a0d4a50e --- /dev/null +++ b/.github/ISSUE_TEMPLATE/config.yml @@ -0,0 +1,8 @@ +blank_issues_enabled: false +contact_links: + - name: Ask a question + url: https://github.com/icsharpcode/SharpZipLib/discussions/new?category=q-a + about: Post any questions in QA discussions instead of creating an issue + - name: Discuss + url: https://github.com/icsharpcode/SharpZipLib/discussions/new + about: Discuss with other community members diff --git a/.github/ISSUE_TEMPLATE/feature_request.yml b/.github/ISSUE_TEMPLATE/feature_request.yml new file mode 100644 index 000000000..5683f0e84 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/feature_request.yml @@ -0,0 +1,52 @@ +name: 💡 Feature request +description: Have a new idea/feature ? Please suggest! +labels: ["enhancement"] +body: + - type: textarea + id: description + attributes: + label: Is your feature request related to a problem? Please describe. + description: A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] + validations: + required: true + + - type: textarea + id: solution + attributes: + label: Describe the solution you'd like + description: A clear and concise description of what you want to happen. + validations: + required: true + + - type: textarea + id: alternatives + attributes: + label: Describe alternatives you've considered + description: A clear and concise description of any alternative solutions or features you've considered. + validations: + required: true + + - id: tags + type: dropdown + attributes: + label: Tags + description: What areas are your feature request related to? + multiple: true + options: + - ZIP + - GZip + - Tar + - BZip2 + - Encoding + - Encryption + - Documentation + - Async + - Performance + + - type: textarea + id: extrainfo + attributes: + label: Additional context + description: Add any other context or screenshots about the feature request here. + validations: + required: false diff --git a/.github/workflows/build-test.yml b/.github/workflows/build-test.yml new file mode 100644 index 000000000..a77a0e278 --- /dev/null +++ b/.github/workflows/build-test.yml @@ -0,0 +1,165 @@ +name: Build and Test + +on: + workflow_dispatch: + inputs: + tag: + description: 'Tag Ref' + required: true + pull_request: + branches: [ master ] + push: + branches: [ master ] + release: + +jobs: + Build: + runs-on: ${{ matrix.os }} + strategy: + fail-fast: false + matrix: + os: [ubuntu-latest, windows-latest, macos-latest] + target: [netstandard2.0, netstandard2.1, net6.0] + env: + LIB_PROJ: src/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib.csproj + steps: + - uses: actions/checkout@v3 + with: + ref: ${{ github.events.inputs.tag }} + fetch-depth: 0 + + - name: Setup .NET + uses: actions/setup-dotnet@v1 + with: + dotnet-version: '6.0.x' + + - name: Show .NET info + run: dotnet --info + + - name: Build library (Debug) + run: dotnet build -c debug -f ${{ matrix.target }} ${{ env.LIB_PROJ }} + + - name: Build library (Release) + run: dotnet build -c release -f ${{ matrix.target }} ${{ env.LIB_PROJ }} + + Test: + runs-on: ${{ matrix.os }}-latest + strategy: + fail-fast: false + matrix: + # Windows testing is combined with code coverage + os: [ubuntu, macos] + target: [net6.0] + steps: + - uses: actions/checkout@v2 + with: + fetch-depth: 0 + + - name: Setup .NET Core + if: matrix.target == 'net6.0' + uses: actions/setup-dotnet@v1 + with: + dotnet-version: '6.0.x' + + - name: Restore test dependencies + run: dotnet restore + + - name: Run tests (Debug) + run: dotnet test -c debug -f ${{ matrix.target }} --no-restore + + - name: Run tests (Release) + run: dotnet test -c release -f ${{ matrix.target }} --no-restore + + + CodeCov: + name: Code Coverage + runs-on: windows-2019 + env: + DOTCOVER_VER: 2021.1.2 + DOTCOVER_PKG: jetbrains.dotcover.commandlinetools + COVER_SNAPSHOT: SharpZipLib.dcvr + steps: + - uses: actions/checkout@v3 + with: + fetch-depth: 0 + + - name: Setup .NET + uses: actions/setup-dotnet@v1 + with: + dotnet-version: '6.0.x' + + # NOTE: This is the temporary fix for https://github.com/actions/virtual-environments/issues/1090 + - name: Cleanup before restore + run: dotnet clean ICSharpCode.SharpZipLib.sln && dotnet nuget locals all --clear + + - name: Install codecov + run: nuget install -o tools -version ${{env.DOTCOVER_VER}} ${{env.DOTCOVER_PKG}} + + - name: Add dotcover to path + run: echo "$(pwd)\tools\${{env.DOTCOVER_PKG}}.${{env.DOTCOVER_VER}}\tools" | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8 -Append + + - name: Run tests with code coverage + run: dotcover dotnet --output=${{env.COVER_SNAPSHOT}} --filters=-:ICSharpCode.SharpZipLib.Tests -- test -c release + + - name: Create code coverage report + run: dotcover report --source=${{env.COVER_SNAPSHOT}} --reporttype=detailedxml --output=dotcover-report.xml + + - name: Upload coverage to Codecov + uses: codecov/codecov-action@v1.2.2 + with: + files: dotcover-report.xml + + - name: Upload coverage snapshot artifact + uses: actions/upload-artifact@v2 + with: + name: Code coverage snapshot + path: ${{env.COVER_SNAPSHOT}} + + Pack: + needs: [Build, Test, CodeCov] + runs-on: windows-latest + env: + PKG_SUFFIX: '' + PKG_PROJ: src/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib.csproj + PKG_PROPS: '/p:ContinuousIntegrationBuild=true /p:EmbedUntrackedSources=true' + + steps: + - uses: actions/checkout@v2 + with: + ref: ${{ github.events.inputs.tag }} + fetch-depth: 0 + + - name: Setup .NET Core + uses: actions/setup-dotnet@v1 + with: + dotnet-version: '6.0.x' + + - name: Build library for .NET Standard 2.0 + run: dotnet build -c Release -f netstandard2.0 ${{ env.PKG_PROPS }} ${{ env.PKG_PROJ }} + - name: Build library for .NET Standard 2.1 + run: dotnet build -c Release -f netstandard2.1 ${{ env.PKG_PROPS }} ${{ env.PKG_PROJ }} + - name: Build library for .NET 6.0 + run: dotnet build -c Release -f net6.0 ${{ env.PKG_PROPS }} ${{ env.PKG_PROJ }} + + - name: Add PR suffix to package + if: ${{ github.event_name == 'pull_request' }} + run: echo "PKG_SUFFIX=-PR" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append + + - name: Set package version + continue-on-error: true + run: |- + $PKG_GIT_VERSION="$(git describe --tags --abbrev | % { $_.substring(1) })" + Write-Output "::notice::Git describe: $PKG_GIT_VERSION" + Write-Output "::notice::Package suffix: $env:PKG_SUFFIX" + $PKG_VERSION = "${PKG_GIT_VERSION}${env:PKG_SUFFIX}" + Write-Output "::notice::Package version: $PKG_VERSION" + Write-Output "PKG_VERSION=$PKG_VERSION" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append + + - name: Create nuget package + run: dotnet pack ${{ env.PKG_PROJ }} -c Release --output dist ${{ env.PKG_PROPS }} /p:Version=${{ env.PKG_VERSION }} + + - name: Upload nuget package artifact + uses: actions/upload-artifact@v2 + with: + name: Nuget package + path: dist/*.nupkg diff --git a/.github/workflows/codeql-analysis.yml b/.github/workflows/codeql-analysis.yml new file mode 100644 index 000000000..54ce5b08b --- /dev/null +++ b/.github/workflows/codeql-analysis.yml @@ -0,0 +1,71 @@ +# For most projects, this workflow file will not need changing; you simply need +# to commit it to your repository. +# +# You may wish to alter this file to override the set of languages analyzed, +# or to provide custom queries or build logic. +# +# ******** NOTE ******** +# We have attempted to detect the languages in your repository. Please check +# the `language` matrix defined below to confirm you have the correct set of +# supported CodeQL languages. +# +name: "CodeQL" + +on: + push: + branches: [ master ] + pull_request: + # The branches below must be a subset of the branches above + branches: [ master ] + schedule: + - cron: '40 7 * * 4' + +jobs: + analyze: + name: Analyze + runs-on: ubuntu-latest + permissions: + actions: read + contents: read + security-events: write + + strategy: + fail-fast: false + matrix: + language: [ 'csharp' ] + # CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python' ] + # Learn more: + # https://docs.github.com/en/free-pro-team@latest/github/finding-security-vulnerabilities-and-errors-in-your-code/configuring-code-scanning#changing-the-languages-that-are-analyzed + + steps: + - name: Checkout repository + uses: actions/checkout@v3 + + # Initializes the CodeQL tools for scanning. + - name: Initialize CodeQL + uses: github/codeql-action/init@v2 + with: + languages: ${{ matrix.language }} + # If you wish to specify custom queries, you can do so here or in a config file. + # By default, queries listed here will override any specified in a config file. + # Prefix the list here with "+" to use these queries and those in the config file. + # queries: ./path/to/local/query, your-org/your-repo/queries@main + + # Autobuild attempts to build any compiled languages (C/C++, C#, or Java). + # If this step fails, then you should remove it and run the build manually (see below) + - name: Autobuild + uses: github/codeql-action/autobuild@v2 + + # ℹ️ Command-line programs to run using the OS shell. + # 📚 https://git.io/JvXDl + + # ✏️ If the Autobuild fails above, remove it and uncomment the following three lines + # and modify them (or add more) to build your code if your project + # uses a compiled language + + #- run: | + # make bootstrap + # make release + + - name: Perform CodeQL Analysis + uses: github/codeql-action/analyze@v2 diff --git a/.github/workflows/issue.yml b/.github/workflows/issue.yml new file mode 100644 index 000000000..8f0fe77d8 --- /dev/null +++ b/.github/workflows/issue.yml @@ -0,0 +1,30 @@ +name: Apply labels from issue + +on: + issues: + types: [opened, edited] + +jobs: + Process_Issue: + runs-on: ubuntu-latest + steps: + - name: Parse Issue Forms Body + id: parse + uses: zentered/issue-forms-body-parser@v1.4.3 + - name: Apply labels from tags + uses: actions/github-script@v6 + env: + PARSED_DATA: "${{ steps.parse.outputs.data }}" + with: + script: | + const parsed = JSON.parse(process.env["PARSED_DATA"]); + const tags = parsed.tags.text; + console.log('Parsed tags:', tags); + const labels = tags.split(',').map( t => t.trim().toLowerCase() ); + console.log('Applying labels:', labels); + github.rest.issues.addLabels({ + issue_number: context.issue.number, + owner: context.repo.owner, + repo: context.repo.repo, + labels, + }) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 000000000..bb6f67445 --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,63 @@ +# Workflow to execute when a new version is released +name: Release + +on: + release: + + # Used for testing and manual execution + workflow_dispatch: + inputs: + tag: + description: 'Tag Ref' + required: true + +jobs: + build: + runs-on: windows-latest + name: Generate DocFX documentation + steps: + - uses: actions/checkout@v3 + with: + ref: ${{ github.events.inputs.tag }} + + - name: Setup .NET + uses: actions/setup-dotnet@v1 + with: + dotnet-version: '6.0.x' + + - name: Build project + run: dotnet build -f netstandard2.0 src/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib.csproj + + - name: Install docfx + run: choco install docfx + + - name: Build Documentation + run: docfx docs/help/docfx.json --warningsAsErrors + + - name: Upload documentation as artifact + uses: actions/upload-artifact@v2 + with: + name: site + path: docs/help/_site + + deploy: + needs: [build] # The second job must depend on the first one to complete before running and uses ubuntu-latest instead of windows. + runs-on: ubuntu-latest + name: Update github pages docs + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Download Artifacts # The built project is downloaded into the 'site' folder. + uses: actions/download-artifact@v1 + with: + name: site + + - name: Publish documentation to Github Pages + uses: JamesIves/github-pages-deploy-action@v4 + with: + token: ${{ secrets.GITHUB_TOKEN }} + branch: gh-pages + folder: site + target-folder: help + clean: false diff --git a/.gitignore b/.gitignore index f1e3d20e0..e5f2a21e1 100644 --- a/.gitignore +++ b/.gitignore @@ -250,3 +250,7 @@ paket-files/ # JetBrains Rider .idea/ *.sln.iml +/test/ICSharpCode.SharpZipLib.TestBootstrapper/Properties/launchSettings.json +_testRunner/ +docs/help/api/.manifest +/benchmark/ICSharpCode.SharpZipLib.Benchmark/BenchmarkDotNet.Artifacts/results diff --git a/.globalconfig b/.globalconfig new file mode 100644 index 000000000..dbb1ed562 --- /dev/null +++ b/.globalconfig @@ -0,0 +1,3 @@ +is_global = true +global_level = 1 +dotnet_diagnostic.CA2007.severity = warning diff --git a/.travis.yml b/.travis.yml index 6d42096ec..1e76533f4 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,22 +1,32 @@ language: csharp -mono: - - latest -os: - - linux - - osx solution: ICSharpCode.SharpZipLib.sln + +#matrix: +# include: +# - mono: latest +# - mono: none +# env: NETCORE=1 +# dotnet: 2.1 +mono: none +dotnet: 2.1 +os: linux install: - - nuget restore ICSharpCode.SharpZipLib.sln + - dotnet restore +# - nuget restore ICSharpCode.SharpZipLib.sln +# - nuget install NUnit.Console -Version 3.8.0 -OutputDirectory _testRunner script: - - xbuild /p:Configuration=Debug ICSharpCode.SharpZipLib.sln - - xbuild /p:Configuration=Release ICSharpCode.SharpZipLib.sln - - mono ./packages/NUnit.ConsoleRunner.3.2.1/tools/nunit3-console.exe --framework=mono-4.0 --labels=All --result=./Documentation/nunit3-test-results-travis.xml ./bin/Release/ICSharpCode.SharpZipLib.Tests.dll + - dotnet build -f netstandard2.0 src/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib.csproj + - dotnet run -c Debug -f netcoreapp2 -p test/ICSharpCode.SharpZipLib.TestBootstrapper/ICSharpCode.SharpZipLib.TestBootstrapper.csproj -- --where "class !~ WindowsNameTransformHandling & test !~ ZipEntryFactoryHandling.CreatedValues & test !~ ZipNameTransformHandling.FilenameCleaning" --result=docs/nunit3-test-results-debug.xml + - dotnet run -c Release -f netcoreapp2 -p test/ICSharpCode.SharpZipLib.TestBootstrapper/ICSharpCode.SharpZipLib.TestBootstrapper.csproj -- --where "class !~ WindowsNameTransformHandling & test !~ ZipEntryFactoryHandling.CreatedValues & test !~ ZipNameTransformHandling.FilenameCleaning" --result=docs\nunit3-test-results-release.xml +# - dotnet test test/ICSharpCode.SharpZipLib.Tests/ICSharpCode.SharpZipLib.Tests.csproj +# - xbuild /p:Configuration=Release ICSharpCode.SharpZipLib.sln +# - mono ./packages/NUnit.ConsoleRunner.3.2.1/tools/nunit3-console.exe --framework=mono-4.0 --labels=All --result=./Documentation/nunit3-test-results-travis.xml ./bin/Release/ICSharpCode.SharpZipLib.Tests.dll after_script: - - nuget pack Build/ICSharpCode.SharpZipLib.nuspec -BasePath Build -OutputDirectory bin/Release -cache: - directories: - - bin - - Documentation + - dotnet pack -f netstandard2.0 -o _dist/ src/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib.csproj +#cache: +# directories: +# - bin +# - Documentation #deploy: # provider: releases # api_key: "GITHUB OAUTH TOKEN" diff --git a/ICSharpCode.SharpZipLib.sln b/ICSharpCode.SharpZipLib.sln index 8d183664a..0c4c6c5f4 100644 --- a/ICSharpCode.SharpZipLib.sln +++ b/ICSharpCode.SharpZipLib.sln @@ -1,7 +1,7 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio 15 -VisualStudioVersion = 15.0.26228.9 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.28705.295 MinimumVisualStudioVersion = 10.0.40219.1 Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Configuration", "Solution Configuration", "{F1097E98-4DEB-4A0A-81EE-5CEC667EBDF0}" ProjectSection(SolutionItems) = preProject @@ -15,7 +15,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ICSharpCode.SharpZipLib", " EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ICSharpCode.SharpZipLib.Tests", "test\ICSharpCode.SharpZipLib.Tests\ICSharpCode.SharpZipLib.Tests.csproj", "{82211166-9C45-4603-8E3A-2CA2EFFCBC26}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ICSharpCode.SharpZipLib.TestBootstrapper", "test\ICSharpCode.SharpZipLib.TestBootstrapper\ICSharpCode.SharpZipLib.TestBootstrapper.csproj", "{535D7365-C5B1-4253-9233-D72D972CA851}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ICSharpCode.SharpZipLib.Benchmark", "benchmark\ICSharpCode.SharpZipLib.Benchmark\ICSharpCode.SharpZipLib.Benchmark.csproj", "{C51E638B-DDD0-48B6-A6BD-EBC4E6A104C7}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -31,12 +31,15 @@ Global {82211166-9C45-4603-8E3A-2CA2EFFCBC26}.Debug|Any CPU.Build.0 = Debug|Any CPU {82211166-9C45-4603-8E3A-2CA2EFFCBC26}.Release|Any CPU.ActiveCfg = Release|Any CPU {82211166-9C45-4603-8E3A-2CA2EFFCBC26}.Release|Any CPU.Build.0 = Release|Any CPU - {535D7365-C5B1-4253-9233-D72D972CA851}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {535D7365-C5B1-4253-9233-D72D972CA851}.Debug|Any CPU.Build.0 = Debug|Any CPU - {535D7365-C5B1-4253-9233-D72D972CA851}.Release|Any CPU.ActiveCfg = Release|Any CPU - {535D7365-C5B1-4253-9233-D72D972CA851}.Release|Any CPU.Build.0 = Release|Any CPU + {C51E638B-DDD0-48B6-A6BD-EBC4E6A104C7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C51E638B-DDD0-48B6-A6BD-EBC4E6A104C7}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C51E638B-DDD0-48B6-A6BD-EBC4E6A104C7}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C51E638B-DDD0-48B6-A6BD-EBC4E6A104C7}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {0A049193-65F8-49AF-82CB-75D42563DA16} + EndGlobalSection EndGlobal diff --git a/LICENSE.txt b/LICENSE.txt index 396840e00..f4597ac92 100644 --- a/LICENSE.txt +++ b/LICENSE.txt @@ -1,4 +1,4 @@ -Copyright © 2000-2016 SharpZipLib Contributors +Copyright © 2000-2018 SharpZipLib Contributors 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 @@ -14,4 +14,4 @@ INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PA 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. \ No newline at end of file +DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md index 06eb4a464..5f09f1f01 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,4 @@ -# SharpZipLib [![Build status](https://ci.appveyor.com/api/projects/status/wuf8l79mypqsbor3/branch/master?svg=true)](https://ci.appveyor.com/project/icsharpcode/sharpziplib/branch/master) [![Join the chat at https://gitter.im/icsharpcode/SharpZipLib](https://badges.gitter.im/icsharpcode/SharpZipLib.svg)](https://gitter.im/icsharpcode/SharpZipLib?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) - -The SharpZipLib project is looking for a new maintainer - please read [State of the Union August 2017](https://github.com/icsharpcode/SharpZipLib/issues/187) +# SharpZipLib [![Build Status](https://github.com/icsharpcode/SharpZipLib/actions/workflows/build-test.yml/badge.svg?branch=master)](https://github.com/icsharpcode/SharpZipLib/actions/workflows/build-test.yml) [![NuGet Version](https://img.shields.io/nuget/v/SharpZipLib.svg)](https://www.nuget.org/packages/SharpZipLib/) [![openupm](https://img.shields.io/npm/v/org.icsharpcode.sharpziplib?label=openupm®istry_uri=https://package.openupm.com)](https://openupm.com/packages/org.icsharpcode.sharpziplib/) Introduction ------------ @@ -9,7 +7,7 @@ SharpZipLib (\#ziplib, formerly NZipLib) is a compression library that supports SharpZipLib was originally ported from the [GNU Classpath](http://www.gnu.org/software/classpath/) java.util.zip library for use with [SharpDevelop](http://www.icsharpcode.net/OpenSource/SD), which needed gzip/zip compression. bzip2 compression and tar archiving were added later due to popular demand. -The [SharpZipLib homepage](http://icsharpcode.github.io/SharpZipLib/) has precompiled libraries available for download, [a link to the forum for support](http://community.sharpdevelop.net/forums/12/ShowForum.aspx), [release history](https://github.com/icsharpcode/SharpZipLib/wiki/Release-History), samples and more. +The [SharpZipLib homepage](http://icsharpcode.github.io/SharpZipLib/) has precompiled libraries available for download, [API documentation](https://icsharpcode.github.io/SharpZipLib/help/api/index.html), [release history](https://github.com/icsharpcode/SharpZipLib/wiki/Release-History), samples and more. License ------- @@ -24,7 +22,7 @@ Namespace layout | Module | Namespace | |:----------------:|:-----------------------------| |BZip2 implementation|ICSharpCode.SharpZipLib.BZip2.\*| -|Checksum implementation|ICSharpCode.SharpZipLib.Checksums.\*| +|Checksum implementation|ICSharpCode.SharpZipLib.Checksum.\*| |Core utilities / interfaces|ICSharpCode.SharpZipLib.Core.\*| |Encryption implementation|ICSharpCode.SharpZipLib.Encryption.\*| |GZip implementation|ICSharpCode.SharpZipLib.GZip.\*| diff --git a/SECURITY.md b/SECURITY.md new file mode 100644 index 000000000..1d4f9f0e4 --- /dev/null +++ b/SECURITY.md @@ -0,0 +1,12 @@ +# Security Policy + +## Supported Versions + +| Version | Supported | +| ------- | ------------------ | +| 1.3.x | :white_check_mark: | +| < 1.3 | :x: | + +## Reporting a Vulnerability + +Send an e-mail to sharpziplib@containrrr.dev diff --git a/appveyor.yml b/appveyor.yml new file mode 100644 index 000000000..df3ffb4ba --- /dev/null +++ b/appveyor.yml @@ -0,0 +1,34 @@ +version: '{build}' +image: Visual Studio 2019 +configuration: +- Debug +- Release +dotnet_csproj: + patch: true + file: '**\*.csproj' + version: $(VERSION) + package_version: $(VERSION) + assembly_version: $(BIN_VERSION) + file_version: $(BIN_VERSION) + informational_version: $(VERSION) +install: +- ps: tools/appveyor-install.ps1 +nuget: + project_feed: true + disable_publish_on_pr: true +before_build: +- ps: nuget restore ICSharpCode.SharpZipLib.sln +- ps: .\tools\appveyor-docfx-init.ps1 +build: + project: ICSharpCode.SharpZipLib.sln + publish_nuget: true + publish_nuget_symbols: true + verbosity: normal +after_build: +- ps: .\tools\appveyor-docfx-build.ps1 +test_script: +- ps: tools\appveyor-test.ps1 +artifacts: +- path: docs\help\_site + type: zip + name: Documentation diff --git a/src/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib.snk b/assets/ICSharpCode.SharpZipLib.snk similarity index 100% rename from src/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib.snk rename to assets/ICSharpCode.SharpZipLib.snk diff --git a/assets/sharpziplib-nuget-256x256.png b/assets/sharpziplib-nuget-256x256.png new file mode 100644 index 000000000..60b3a8dae Binary files /dev/null and b/assets/sharpziplib-nuget-256x256.png differ diff --git a/benchmark/.globalconfig b/benchmark/.globalconfig new file mode 100644 index 000000000..14f57bc66 --- /dev/null +++ b/benchmark/.globalconfig @@ -0,0 +1,3 @@ +is_global = true +global_level = 2 +dotnet_diagnostic.CA2007.severity = none diff --git a/benchmark/ICSharpCode.SharpZipLib.Benchmark/BZip2/BZip2InputStream.cs b/benchmark/ICSharpCode.SharpZipLib.Benchmark/BZip2/BZip2InputStream.cs new file mode 100644 index 000000000..8d5a7ccc2 --- /dev/null +++ b/benchmark/ICSharpCode.SharpZipLib.Benchmark/BZip2/BZip2InputStream.cs @@ -0,0 +1,37 @@ +using System; +using System.IO; +using BenchmarkDotNet.Attributes; + +namespace ICSharpCode.SharpZipLib.Benchmark.BZip2 +{ + [Config(typeof(MultipleRuntimes))] + public class BZip2InputStream + { + private byte[] compressedData; + + public BZip2InputStream() + { + var outputMemoryStream = new MemoryStream(); + using (var outputStream = new SharpZipLib.BZip2.BZip2OutputStream(outputMemoryStream)) + { + var random = new Random(1234); + var inputData = new byte[1024 * 1024 * 30]; + random.NextBytes(inputData); + var inputMemoryStream = new MemoryStream(inputData); + inputMemoryStream.CopyTo(outputStream); + } + + compressedData = outputMemoryStream.ToArray(); + } + + [Benchmark] + public void DecompressData() + { + var memoryStream = new MemoryStream(compressedData); + using (var inputStream = new SharpZipLib.BZip2.BZip2InputStream(memoryStream)) + { + inputStream.CopyTo(Stream.Null); + } + } + } +} diff --git a/benchmark/ICSharpCode.SharpZipLib.Benchmark/Checksum/Adler32.cs b/benchmark/ICSharpCode.SharpZipLib.Benchmark/Checksum/Adler32.cs new file mode 100644 index 000000000..0b3b906fb --- /dev/null +++ b/benchmark/ICSharpCode.SharpZipLib.Benchmark/Checksum/Adler32.cs @@ -0,0 +1,44 @@ +using System; +using BenchmarkDotNet.Attributes; + +namespace ICSharpCode.SharpZipLib.Benchmark.Checksum +{ + [Config(typeof(MultipleRuntimes))] + public class Adler32 + { + private const int ChunkCount = 256; + private const int ChunkSize = 1024 * 1024; + private const int N = ChunkCount * ChunkSize; + private readonly byte[] data; + + public Adler32() + { + data = new byte[N]; + new Random(1).NextBytes(data); + } + + [Benchmark] + public long Adler32LargeUpdate() + { + var adler32 = new ICSharpCode.SharpZipLib.Checksum.Adler32(); + adler32.Update(data); + return adler32.Value; + } + + /* + [Benchmark] + public long Adler32ChunkedUpdate() + { + var adler32 = new ICSharpCode.SharpZipLib.Checksum.Adler32(); + + for (int i = 0; i < ChunkCount; i++) + { + var segment = new ArraySegment(data, ChunkSize * i, ChunkSize); + adler32.Update(segment); + } + + return adler32.Value; + } + */ + } +} diff --git a/benchmark/ICSharpCode.SharpZipLib.Benchmark/Checksum/BZip2Crc.cs b/benchmark/ICSharpCode.SharpZipLib.Benchmark/Checksum/BZip2Crc.cs new file mode 100644 index 000000000..c7c233691 --- /dev/null +++ b/benchmark/ICSharpCode.SharpZipLib.Benchmark/Checksum/BZip2Crc.cs @@ -0,0 +1,44 @@ +using System; +using BenchmarkDotNet.Attributes; + +namespace ICSharpCode.SharpZipLib.Benchmark.Checksum +{ + [Config(typeof(MultipleRuntimes))] + public class BZip2Crc + { + private const int ChunkCount = 256; + private const int ChunkSize = 1024 * 1024; + private const int N = ChunkCount * ChunkSize; + private readonly byte[] data; + + public BZip2Crc() + { + data = new byte[N]; + new Random(1).NextBytes(data); + } + + [Benchmark] + public long BZip2CrcLargeUpdate() + { + var bzipCrc = new ICSharpCode.SharpZipLib.Checksum.BZip2Crc(); + bzipCrc.Update(data); + return bzipCrc.Value; + } + + /* + [Benchmark] + public long BZip2CrcChunkedUpdate() + { + var bzipCrc = new ICSharpCode.SharpZipLib.Checksum.BZip2Crc(); + + for (int i = 0; i < ChunkCount; i++) + { + var segment = new ArraySegment(data, ChunkSize * i, ChunkSize); + bzipCrc.Update(segment); + } + + return bzipCrc.Value; + } + */ + } +} diff --git a/benchmark/ICSharpCode.SharpZipLib.Benchmark/Checksum/Crc32.cs b/benchmark/ICSharpCode.SharpZipLib.Benchmark/Checksum/Crc32.cs new file mode 100644 index 000000000..1e355552b --- /dev/null +++ b/benchmark/ICSharpCode.SharpZipLib.Benchmark/Checksum/Crc32.cs @@ -0,0 +1,44 @@ +using System; +using BenchmarkDotNet.Attributes; + +namespace ICSharpCode.SharpZipLib.Benchmark.Checksum +{ + [Config(typeof(MultipleRuntimes))] + public class Crc32 + { + private const int ChunkCount = 256; + private const int ChunkSize = 1024 * 1024; + private const int N = ChunkCount * ChunkSize; + private readonly byte[] data; + + public Crc32() + { + data = new byte[N]; + new Random(1).NextBytes(data); + } + + [Benchmark] + public long Crc32LargeUpdate() + { + var crc32 = new ICSharpCode.SharpZipLib.Checksum.Crc32(); + crc32.Update(data); + return crc32.Value; + } + + /* + [Benchmark] + public long Crc32ChunkedUpdate() + { + var crc32 = new ICSharpCode.SharpZipLib.Checksum.Crc32(); + + for (int i = 0; i < ChunkCount; i++) + { + var segment = new ArraySegment(data, ChunkSize * i, ChunkSize); + crc32.Update(segment); + } + + return crc32.Value; + } + */ + } +} diff --git a/benchmark/ICSharpCode.SharpZipLib.Benchmark/ICSharpCode.SharpZipLib.Benchmark.csproj b/benchmark/ICSharpCode.SharpZipLib.Benchmark/ICSharpCode.SharpZipLib.Benchmark.csproj new file mode 100644 index 000000000..7fa26f80f --- /dev/null +++ b/benchmark/ICSharpCode.SharpZipLib.Benchmark/ICSharpCode.SharpZipLib.Benchmark.csproj @@ -0,0 +1,16 @@ + + + + Exe + net462;net6.0 + + + + + + + + + + + diff --git a/benchmark/ICSharpCode.SharpZipLib.Benchmark/Program.cs b/benchmark/ICSharpCode.SharpZipLib.Benchmark/Program.cs new file mode 100644 index 000000000..3a7beebbb --- /dev/null +++ b/benchmark/ICSharpCode.SharpZipLib.Benchmark/Program.cs @@ -0,0 +1,24 @@ +using BenchmarkDotNet.Configs; +using BenchmarkDotNet.Jobs; +using BenchmarkDotNet.Running; +using BenchmarkDotNet.Toolchains.CsProj; + +namespace ICSharpCode.SharpZipLib.Benchmark +{ + public class MultipleRuntimes : ManualConfig + { + public MultipleRuntimes() + { + AddJob(Job.Default.WithToolchain(CsProjClassicNetToolchain.Net462).AsBaseline()); // NET 4.6.2 + AddJob(Job.Default.WithToolchain(CsProjCoreToolchain.NetCoreApp60)); // .NET 6.0 + } + } + + class Program + { + static void Main(string[] args) + { + BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args); + } + } +} diff --git a/benchmark/ICSharpCode.SharpZipLib.Benchmark/Tar/TarInputStream.cs b/benchmark/ICSharpCode.SharpZipLib.Benchmark/Tar/TarInputStream.cs new file mode 100644 index 000000000..b59a217ab --- /dev/null +++ b/benchmark/ICSharpCode.SharpZipLib.Benchmark/Tar/TarInputStream.cs @@ -0,0 +1,82 @@ +using System; +using System.IO; +using System.Security.Cryptography; +using System.Text; +using System.Threading; +using System.Threading.Tasks; +using BenchmarkDotNet.Attributes; +using ICSharpCode.SharpZipLib.Tar; + +namespace ICSharpCode.SharpZipLib.Benchmark.Tar +{ + [MemoryDiagnoser] + [Config(typeof(MultipleRuntimes))] + public class TarInputStream + { + private readonly byte[] archivedData; + private readonly byte[] readBuffer = new byte[1024]; + + public TarInputStream() + { + using (var outputMemoryStream = new MemoryStream()) + { + using (var zipOutputStream = + new ICSharpCode.SharpZipLib.Tar.TarOutputStream(outputMemoryStream, Encoding.UTF8)) + { + var tarEntry = TarEntry.CreateTarEntry("some file"); + tarEntry.Size = 1024 * 1024; + zipOutputStream.PutNextEntry(tarEntry); + + var rng = RandomNumberGenerator.Create(); + var inputBuffer = new byte[1024]; + rng.GetBytes(inputBuffer); + + for (int i = 0; i < 1024; i++) + { + zipOutputStream.Write(inputBuffer, 0, inputBuffer.Length); + } + } + + archivedData = outputMemoryStream.ToArray(); + } + } + + [Benchmark] + public long ReadTarInputStream() + { + using (var memoryStream = new MemoryStream(archivedData)) + using (var zipInputStream = new ICSharpCode.SharpZipLib.Tar.TarInputStream(memoryStream, Encoding.UTF8)) + { + var entry = zipInputStream.GetNextEntry(); + + while (zipInputStream.Read(readBuffer, 0, readBuffer.Length) > 0) + { + } + + return entry.Size; + } + } + + [Benchmark] + public async Task ReadTarInputStreamAsync() + { + using (var memoryStream = new MemoryStream(archivedData)) + using (var zipInputStream = new ICSharpCode.SharpZipLib.Tar.TarInputStream(memoryStream, Encoding.UTF8)) + { + var entry = await zipInputStream.GetNextEntryAsync(CancellationToken.None); + +#if NETCOREAPP2_1_OR_GREATER + while (await zipInputStream.ReadAsync(readBuffer.AsMemory()) > 0) + { + } +#else + while (await zipInputStream.ReadAsync(readBuffer, 0, readBuffer.Length) > 0) + { + } +#endif + + return entry.Size; + } + } + } +} diff --git a/benchmark/ICSharpCode.SharpZipLib.Benchmark/Tar/TarOutputStream.cs b/benchmark/ICSharpCode.SharpZipLib.Benchmark/Tar/TarOutputStream.cs new file mode 100644 index 000000000..f24e83e35 --- /dev/null +++ b/benchmark/ICSharpCode.SharpZipLib.Benchmark/Tar/TarOutputStream.cs @@ -0,0 +1,64 @@ +using System.IO; +using System.Security.Cryptography; +using System.Text; +using System.Threading; +using System.Threading.Tasks; +using BenchmarkDotNet.Attributes; +using ICSharpCode.SharpZipLib.Tar; + +namespace ICSharpCode.SharpZipLib.Benchmark.Tar +{ + [MemoryDiagnoser] + [Config(typeof(MultipleRuntimes))] + public class TarOutputStream + { + private readonly byte[] backingArray = new byte[1024 * 1024 + (6 * 1024)]; + private readonly byte[] inputBuffer = new byte[1024]; + private static readonly RandomNumberGenerator _rng = RandomNumberGenerator.Create(); + + [Benchmark] + public void WriteTarOutputStream() + { + using (var outputMemoryStream = new MemoryStream(backingArray)) + { + using (var tarOutputStream = + new ICSharpCode.SharpZipLib.Tar.TarOutputStream(outputMemoryStream, Encoding.UTF8)) + { + var tarEntry = TarEntry.CreateTarEntry("some file"); + tarEntry.Size = 1024 * 1024; + tarOutputStream.PutNextEntry(tarEntry); + + _rng.GetBytes(inputBuffer); + + for (int i = 0; i < 1024; i++) + { + tarOutputStream.Write(inputBuffer, 0, inputBuffer.Length); + } + } + } + } + + [Benchmark] + public async Task WriteTarOutputStreamAsync() + { + using (var outputMemoryStream = new MemoryStream(backingArray)) + { + using (var tarOutputStream = + new ICSharpCode.SharpZipLib.Tar.TarOutputStream(outputMemoryStream, Encoding.UTF8)) + { + var tarEntry = TarEntry.CreateTarEntry("some file"); + tarEntry.Size = 1024 * 1024; + + await tarOutputStream.PutNextEntryAsync(tarEntry, CancellationToken.None); + + _rng.GetBytes(inputBuffer); + + for (int i = 0; i < 1024; i++) + { + await tarOutputStream.WriteAsync(inputBuffer, 0, inputBuffer.Length); + } + } + } + } + } +} diff --git a/benchmark/ICSharpCode.SharpZipLib.Benchmark/Zip/ZipFile.cs b/benchmark/ICSharpCode.SharpZipLib.Benchmark/Zip/ZipFile.cs new file mode 100644 index 000000000..0a84e0b88 --- /dev/null +++ b/benchmark/ICSharpCode.SharpZipLib.Benchmark/Zip/ZipFile.cs @@ -0,0 +1,63 @@ +using System; +using System.IO; +using System.Net.Http; +using System.Threading.Tasks; +using BenchmarkDotNet.Attributes; +using ICSharpCode.SharpZipLib.Zip; + +namespace ICSharpCode.SharpZipLib.Benchmark.Zip +{ + [MemoryDiagnoser] + [Config(typeof(MultipleRuntimes))] + public class ZipFile + { + private readonly byte[] readBuffer = new byte[4096]; + private string zipFileWithLargeAmountOfEntriesPath; + + [GlobalSetup] + public async Task GlobalSetup() + { + SharpZipLibOptions.InflaterPoolSize = 4; + + // large real-world test file from test262 repository + string commitSha = "2e4e0e6b8ebe3348a207144204cb6d7a5571c863"; + zipFileWithLargeAmountOfEntriesPath = Path.Combine(Path.GetTempPath(), $"{commitSha}.zip"); + if (!File.Exists(zipFileWithLargeAmountOfEntriesPath)) + { + var uri = $"https://github.com/tc39/test262/archive/{commitSha}.zip"; + + Console.WriteLine("Loading test262 repository archive from {0}", uri); + + using (var client = new HttpClient()) + { + using (var downloadStream = await client.GetStreamAsync(uri)) + { + using (var writeStream = File.OpenWrite(zipFileWithLargeAmountOfEntriesPath)) + { + await downloadStream.CopyToAsync(writeStream); + Console.WriteLine("File downloaded and saved to {0}", zipFileWithLargeAmountOfEntriesPath); + } + } + } + } + + } + + [Benchmark] + public void ReadLargeZipFile() + { + using (var file = new SharpZipLib.Zip.ZipFile(zipFileWithLargeAmountOfEntriesPath)) + { + foreach (ZipEntry entry in file) + { + using (var stream = file.GetInputStream(entry)) + { + while (stream.Read(readBuffer, 0, readBuffer.Length) > 0) + { + } + } + } + } + } + } +} diff --git a/benchmark/ICSharpCode.SharpZipLib.Benchmark/Zip/ZipInputStream.cs b/benchmark/ICSharpCode.SharpZipLib.Benchmark/Zip/ZipInputStream.cs new file mode 100644 index 000000000..2e0c057d8 --- /dev/null +++ b/benchmark/ICSharpCode.SharpZipLib.Benchmark/Zip/ZipInputStream.cs @@ -0,0 +1,57 @@ +using System.IO; +using BenchmarkDotNet.Attributes; + +namespace ICSharpCode.SharpZipLib.Benchmark.Zip +{ + [MemoryDiagnoser] + [Config(typeof(MultipleRuntimes))] + public class ZipInputStream + { + private const int ChunkCount = 64; + private const int ChunkSize = 1024 * 1024; + private const int N = ChunkCount * ChunkSize; + + byte[] zippedData; + byte[] readBuffer = new byte[4096]; + + [GlobalSetup] + public void GlobalSetup() + { + using (var memoryStream = new MemoryStream()) + { + using (var zipOutputStream = new SharpZipLib.Zip.ZipOutputStream(memoryStream)) + { + zipOutputStream.PutNextEntry(new SharpZipLib.Zip.ZipEntry("0")); + + var inputBuffer = new byte[ChunkSize]; + + for (int i = 0; i < ChunkCount; i++) + { + zipOutputStream.Write(inputBuffer, 0, inputBuffer.Length); + } + } + + zippedData = memoryStream.ToArray(); + } + } + + [Benchmark] + public long ReadZipInputStream() + { + using (var memoryStream = new MemoryStream(zippedData)) + { + using (var zipInputStream = new SharpZipLib.Zip.ZipInputStream(memoryStream)) + { + var entry = zipInputStream.GetNextEntry(); + + while (zipInputStream.Read(readBuffer, 0, readBuffer.Length) > 0) + { + + } + + return entry.Size; + } + } + } + } +} diff --git a/benchmark/ICSharpCode.SharpZipLib.Benchmark/Zip/ZipOutputStream.cs b/benchmark/ICSharpCode.SharpZipLib.Benchmark/Zip/ZipOutputStream.cs new file mode 100644 index 000000000..c4e8620e3 --- /dev/null +++ b/benchmark/ICSharpCode.SharpZipLib.Benchmark/Zip/ZipOutputStream.cs @@ -0,0 +1,62 @@ +using System.IO; +using System.Threading.Tasks; +using BenchmarkDotNet.Attributes; + +namespace ICSharpCode.SharpZipLib.Benchmark.Zip +{ + [MemoryDiagnoser] + [Config(typeof(MultipleRuntimes))] + public class ZipOutputStream + { + private const int ChunkCount = 64; + private const int ChunkSize = 1024 * 1024; + private const int N = ChunkCount * ChunkSize; + + byte[] outputBuffer; + byte[] inputBuffer; + + [GlobalSetup] + public void GlobalSetup() + { + inputBuffer = new byte[ChunkSize]; + outputBuffer = new byte[N]; + } + + [Benchmark] + public long WriteZipOutputStream() + { + using (var memoryStream = new MemoryStream(outputBuffer)) + { + var zipOutputStream = new SharpZipLib.Zip.ZipOutputStream(memoryStream); + zipOutputStream.PutNextEntry(new SharpZipLib.Zip.ZipEntry("0")); + + for (int i = 0; i < ChunkCount; i++) + { + zipOutputStream.Write(inputBuffer, 0, inputBuffer.Length); + } + + return memoryStream.Position; + } + } + + [Benchmark] + public async Task WriteZipOutputStreamAsync() + { + using (var memoryStream = new MemoryStream(outputBuffer)) + { + using (var zipOutputStream = new SharpZipLib.Zip.ZipOutputStream(memoryStream)) + { + zipOutputStream.IsStreamOwner = false; + zipOutputStream.PutNextEntry(new SharpZipLib.Zip.ZipEntry("0")); + + for (int i = 0; i < ChunkCount; i++) + { + await zipOutputStream.WriteAsync(inputBuffer, 0, inputBuffer.Length); + } + } + + return memoryStream.Position; + } + } + } +} diff --git a/docs/help/api/toc.yml b/docs/help/api/toc.yml index 2cc204bde..a56fa8ddf 100644 --- a/docs/help/api/toc.yml +++ b/docs/help/api/toc.yml @@ -4,6 +4,14 @@ items: - uid: ICSharpCode.SharpZipLib.SharpZipBaseException name: SharpZipBaseException + - uid: ICSharpCode.SharpZipLib.StreamDecodingException + name: StreamDecodingException + - uid: ICSharpCode.SharpZipLib.StreamUnsupportedException + name: StreamUnsupportedException + - uid: ICSharpCode.SharpZipLib.UnexpectedEndOfStreamException + name: UnexpectedEndOfStreamException + - uid: ICSharpCode.SharpZipLib.ValueOutOfRangeException + name: ValueOutOfRangeException - uid: ICSharpCode.SharpZipLib.BZip2 name: ICSharpCode.SharpZipLib.BZip2 items: @@ -43,6 +51,8 @@ name: FileSystemScanner - uid: ICSharpCode.SharpZipLib.Core.INameTransform name: INameTransform + - uid: ICSharpCode.SharpZipLib.Core.InvalidNameException + name: InvalidNameException - uid: ICSharpCode.SharpZipLib.Core.IScanFilter name: IScanFilter - uid: ICSharpCode.SharpZipLib.Core.NameAndSizeFilter @@ -109,6 +119,8 @@ name: TarEntry - uid: ICSharpCode.SharpZipLib.Tar.TarException name: TarException + - uid: ICSharpCode.SharpZipLib.Tar.TarExtendedHeaderReader + name: TarExtendedHeaderReader - uid: ICSharpCode.SharpZipLib.Tar.TarHeader name: TarHeader - uid: ICSharpCode.SharpZipLib.Tar.TarInputStream @@ -204,6 +216,8 @@ name: ZipNameTransform - uid: ICSharpCode.SharpZipLib.Zip.ZipOutputStream name: ZipOutputStream + - uid: ICSharpCode.SharpZipLib.Zip.ZipStrings + name: ZipStrings - uid: ICSharpCode.SharpZipLib.Zip.ZipTestResultHandler name: ZipTestResultHandler - uid: ICSharpCode.SharpZipLib.Zip.Compression diff --git a/docs/help/docfx.json b/docs/help/docfx.json index 173bb4360..1da3079e4 100644 --- a/docs/help/docfx.json +++ b/docs/help/docfx.json @@ -14,7 +14,10 @@ ] } ], - "dest": "api" + "dest": "api", + "properties": { + "TargetFramework": "netstandard2.0" + } } ], "build": { @@ -62,17 +65,22 @@ ], "globalMetadata": { "_appTitle": "SharpZipLib Help", - "_appFooter": "Copyright © 2000-2017 SharpZipLib Contributors" + "_appFooter": "Copyright © 2000-2022 SharpZipLib Contributors", + "_gitContribute": { + "repo": "https://github.com/icsharpcode/SharpZipLib", + "branch": "master" + } }, "dest": "_site", "globalMetadataFiles": [], "fileMetadataFiles": [], "template": [ - "default" + "default", + "template" ], "postProcessors": [], "noLangKeyword": false, "keepFileLink": false, "cleanupCacheHistory": false } -} \ No newline at end of file +} diff --git a/docs/help/template/logo.svg b/docs/help/template/logo.svg new file mode 100644 index 000000000..25535c631 --- /dev/null +++ b/docs/help/template/logo.svg @@ -0,0 +1,34 @@ + + + + + diff --git a/docs/help/template/partials/navbar.tmpl.partial b/docs/help/template/partials/navbar.tmpl.partial new file mode 100644 index 000000000..5f9bb9242 --- /dev/null +++ b/docs/help/template/partials/navbar.tmpl.partial @@ -0,0 +1,20 @@ + \ No newline at end of file diff --git a/docs/help/template/styles/main.css b/docs/help/template/styles/main.css new file mode 100644 index 000000000..db19bb18a --- /dev/null +++ b/docs/help/template/styles/main.css @@ -0,0 +1,35 @@ +button, a { + color: #516d86; + cursor: pointer; +} + +.affix > ul > li.active > a, .affix > ul > li.active > a:before, +.affix ul > li.active > a, .affix ul > li.active > a:before { + color: #516d86; +} + +.toc .nav > li.active > a { + color: #7391ab; +} + +.navbar-default .navbar-nav>.active>a, .navbar-default .navbar-nav>.active>a:focus, .navbar-default .navbar-nav>.active>a:hover { + color: #181c20; + background-color: #c4cdd4; +} + +svg:hover path { + fill: currentColor; +} + +.navbar-default .navbar-brand:focus, .navbar-default .navbar-brand:hover { + color: #3d4852; +} + +#toc .level1 > li { + font-weight: normal; + font-size: 14px; +} + +#sidetoc .sidetoc.shiftup { + bottom: 50px; +} diff --git a/samples/ICSharpCode.SharpZipLib.Samples/cs/Cmd_BZip2/Cmd_BZip2.csproj b/samples/ICSharpCode.SharpZipLib.Samples/cs/Cmd_BZip2/Cmd_BZip2.csproj index 351f801f9..b1536ff0f 100644 --- a/samples/ICSharpCode.SharpZipLib.Samples/cs/Cmd_BZip2/Cmd_BZip2.csproj +++ b/samples/ICSharpCode.SharpZipLib.Samples/cs/Cmd_BZip2/Cmd_BZip2.csproj @@ -80,84 +80,7 @@ copy Cmd_BZip2.exe bunzip2.exe 4096 - - ..\..\packages\SharpZipLib.1.0.0-alpha1\lib\netstandard1.3\ICSharpCode.SharpZipLib.dll - True - - - ..\..\packages\Microsoft.Win32.Primitives.4.3.0\lib\net46\Microsoft.Win32.Primitives.dll - True - - - ..\..\packages\System.AppContext.4.3.0\lib\net46\System.AppContext.dll - True - - - - ..\..\packages\System.Console.4.3.0\lib\net46\System.Console.dll - True - - - ..\..\packages\System.Diagnostics.DiagnosticSource.4.3.0\lib\net46\System.Diagnostics.DiagnosticSource.dll - True - - - ..\..\packages\System.Globalization.Calendars.4.3.0\lib\net46\System.Globalization.Calendars.dll - True - - - ..\..\packages\System.IO.Compression.4.3.0\lib\net46\System.IO.Compression.dll - True - - - - ..\..\packages\System.IO.Compression.ZipFile.4.3.0\lib\net46\System.IO.Compression.ZipFile.dll - True - - - ..\..\packages\System.IO.FileSystem.4.3.0\lib\net46\System.IO.FileSystem.dll - True - - - ..\..\packages\System.IO.FileSystem.Primitives.4.3.0\lib\net46\System.IO.FileSystem.Primitives.dll - True - - - ..\..\packages\System.Net.Http.4.1.2\lib\net46\System.Net.Http.dll - True - - - ..\..\packages\System.Net.Sockets.4.3.0\lib\net46\System.Net.Sockets.dll - True - - - - ..\..\packages\System.Runtime.InteropServices.RuntimeInformation.4.3.0\lib\net45\System.Runtime.InteropServices.RuntimeInformation.dll - True - - - ..\..\packages\System.Security.Cryptography.Algorithms.4.2.0\lib\net461\System.Security.Cryptography.Algorithms.dll - True - - - ..\..\packages\System.Security.Cryptography.Encoding.4.3.0\lib\net46\System.Security.Cryptography.Encoding.dll - True - - - ..\..\packages\System.Security.Cryptography.Primitives.4.0.0\lib\net46\System.Security.Cryptography.Primitives.dll - True - - - ..\..\packages\System.Security.Cryptography.X509Certificates.4.1.0\lib\net461\System.Security.Cryptography.X509Certificates.dll - True - - - - - ..\..\packages\System.Xml.ReaderWriter.4.3.0\lib\net46\System.Xml.ReaderWriter.dll - True - @@ -165,7 +88,6 @@ copy Cmd_BZip2.exe bunzip2.exe - @@ -177,12 +99,10 @@ copy Cmd_BZip2.exe bunzip2.exe + + + 1.3.3 + + - - - - This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. - - - - \ No newline at end of file + diff --git a/samples/ICSharpCode.SharpZipLib.Samples/cs/Cmd_BZip2/packages.config b/samples/ICSharpCode.SharpZipLib.Samples/cs/Cmd_BZip2/packages.config deleted file mode 100644 index d353d1619..000000000 --- a/samples/ICSharpCode.SharpZipLib.Samples/cs/Cmd_BZip2/packages.config +++ /dev/null @@ -1,50 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/samples/ICSharpCode.SharpZipLib.Samples/cs/Cmd_Checksum/Cmd_Checksum.cs b/samples/ICSharpCode.SharpZipLib.Samples/cs/Cmd_Checksum/Cmd_Checksum.cs index 49426af69..c40ede339 100644 --- a/samples/ICSharpCode.SharpZipLib.Samples/cs/Cmd_Checksum/Cmd_Checksum.cs +++ b/samples/ICSharpCode.SharpZipLib.Samples/cs/Cmd_Checksum/Cmd_Checksum.cs @@ -132,7 +132,7 @@ public static int Main(string[] args) case Command.Crc32: var currentCrc = new Crc32(); while ((bytesRead = checksumStream.Read(buffer, 0, buffer.Length)) > 0) { - currentCrc.Update(buffer, 0, bytesRead); + currentCrc.Update(new ArraySegment(buffer, 0, bytesRead)); } Console.WriteLine("CRC32 for {0} is 0x{1:X8}", args[0], currentCrc.Value); break; @@ -140,7 +140,7 @@ public static int Main(string[] args) case Command.BZip2: var currentBZip2Crc = new BZip2Crc(); while ((bytesRead = checksumStream.Read(buffer, 0, buffer.Length)) > 0) { - currentBZip2Crc.Update(buffer, 0, bytesRead); + currentBZip2Crc.Update(new ArraySegment(buffer, 0, bytesRead)); } Console.WriteLine("BZip2CRC32 for {0} is 0x{1:X8}", args[0], currentBZip2Crc.Value); break; @@ -148,7 +148,7 @@ public static int Main(string[] args) case Command.Adler: var currentAdler = new Adler32(); while ((bytesRead = checksumStream.Read(buffer, 0, buffer.Length)) > 0) { - currentAdler.Update(buffer, 0, bytesRead); + currentAdler.Update(new ArraySegment(buffer, 0, bytesRead)); } Console.WriteLine("Adler32 for {0} is 0x{1:X8}", args[0], currentAdler.Value); break; diff --git a/samples/ICSharpCode.SharpZipLib.Samples/cs/Cmd_Checksum/Cmd_Checksum.csproj b/samples/ICSharpCode.SharpZipLib.Samples/cs/Cmd_Checksum/Cmd_Checksum.csproj index 25d80e70b..d5d9f6cfe 100644 --- a/samples/ICSharpCode.SharpZipLib.Samples/cs/Cmd_Checksum/Cmd_Checksum.csproj +++ b/samples/ICSharpCode.SharpZipLib.Samples/cs/Cmd_Checksum/Cmd_Checksum.csproj @@ -74,84 +74,12 @@ true - - ..\..\packages\SharpZipLib.1.0.0-alpha1\lib\netstandard1.3\ICSharpCode.SharpZipLib.dll - True - - - ..\..\packages\Microsoft.Win32.Primitives.4.3.0\lib\net46\Microsoft.Win32.Primitives.dll - True - - - ..\..\packages\System.AppContext.4.3.0\lib\net46\System.AppContext.dll - True - - - ..\..\packages\System.Console.4.3.0\lib\net46\System.Console.dll - True - - - ..\..\packages\System.Diagnostics.DiagnosticSource.4.3.0\lib\net46\System.Diagnostics.DiagnosticSource.dll - True - - - ..\..\packages\System.Globalization.Calendars.4.3.0\lib\net46\System.Globalization.Calendars.dll - True - - - ..\..\packages\System.IO.Compression.4.3.0\lib\net46\System.IO.Compression.dll - True - - - ..\..\packages\System.IO.Compression.ZipFile.4.3.0\lib\net46\System.IO.Compression.ZipFile.dll - True - - - ..\..\packages\System.IO.FileSystem.4.3.0\lib\net46\System.IO.FileSystem.dll - True - - - ..\..\packages\System.IO.FileSystem.Primitives.4.3.0\lib\net46\System.IO.FileSystem.Primitives.dll - True - - - ..\..\packages\System.Net.Http.4.1.2\lib\net46\System.Net.Http.dll - True - - - ..\..\packages\System.Net.Sockets.4.3.0\lib\net46\System.Net.Sockets.dll - True - - - ..\..\packages\System.Runtime.InteropServices.RuntimeInformation.4.3.0\lib\net45\System.Runtime.InteropServices.RuntimeInformation.dll - True - - - ..\..\packages\System.Security.Cryptography.Algorithms.4.2.0\lib\net461\System.Security.Cryptography.Algorithms.dll - True - - - ..\..\packages\System.Security.Cryptography.Encoding.4.3.0\lib\net46\System.Security.Cryptography.Encoding.dll - True - - - ..\..\packages\System.Security.Cryptography.Primitives.4.0.0\lib\net46\System.Security.Cryptography.Primitives.dll - True - - - ..\..\packages\System.Security.Cryptography.X509Certificates.4.1.0\lib\net461\System.Security.Cryptography.X509Certificates.dll - True - - - ..\..\packages\System.Xml.ReaderWriter.4.3.0\lib\net46\System.Xml.ReaderWriter.dll - True - @@ -159,7 +87,6 @@ - @@ -171,12 +98,10 @@ + + + 1.3.3 + + - - - - This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. - - - - \ No newline at end of file + diff --git a/samples/ICSharpCode.SharpZipLib.Samples/cs/Cmd_Checksum/packages.config b/samples/ICSharpCode.SharpZipLib.Samples/cs/Cmd_Checksum/packages.config deleted file mode 100644 index d353d1619..000000000 --- a/samples/ICSharpCode.SharpZipLib.Samples/cs/Cmd_Checksum/packages.config +++ /dev/null @@ -1,50 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/samples/ICSharpCode.SharpZipLib.Samples/cs/Cmd_GZip/Cmd_GZip.csproj b/samples/ICSharpCode.SharpZipLib.Samples/cs/Cmd_GZip/Cmd_GZip.csproj index 825bc2cc1..9ebaa8ca6 100644 --- a/samples/ICSharpCode.SharpZipLib.Samples/cs/Cmd_GZip/Cmd_GZip.csproj +++ b/samples/ICSharpCode.SharpZipLib.Samples/cs/Cmd_GZip/Cmd_GZip.csproj @@ -74,84 +74,12 @@ copy Cmd_GZip.exe gunzip.exe true - - ..\..\packages\SharpZipLib.1.0.0-alpha1\lib\netstandard1.3\ICSharpCode.SharpZipLib.dll - True - - - ..\..\packages\Microsoft.Win32.Primitives.4.3.0\lib\net46\Microsoft.Win32.Primitives.dll - True - - - ..\..\packages\System.AppContext.4.3.0\lib\net46\System.AppContext.dll - True - - - ..\..\packages\System.Console.4.3.0\lib\net46\System.Console.dll - True - - - ..\..\packages\System.Diagnostics.DiagnosticSource.4.3.0\lib\net46\System.Diagnostics.DiagnosticSource.dll - True - - - ..\..\packages\System.Globalization.Calendars.4.3.0\lib\net46\System.Globalization.Calendars.dll - True - - - ..\..\packages\System.IO.Compression.4.3.0\lib\net46\System.IO.Compression.dll - True - - - ..\..\packages\System.IO.Compression.ZipFile.4.3.0\lib\net46\System.IO.Compression.ZipFile.dll - True - - - ..\..\packages\System.IO.FileSystem.4.3.0\lib\net46\System.IO.FileSystem.dll - True - - - ..\..\packages\System.IO.FileSystem.Primitives.4.3.0\lib\net46\System.IO.FileSystem.Primitives.dll - True - - - ..\..\packages\System.Net.Http.4.1.2\lib\net46\System.Net.Http.dll - True - - - ..\..\packages\System.Net.Sockets.4.3.0\lib\net46\System.Net.Sockets.dll - True - - - ..\..\packages\System.Runtime.InteropServices.RuntimeInformation.4.3.0\lib\net45\System.Runtime.InteropServices.RuntimeInformation.dll - True - - - ..\..\packages\System.Security.Cryptography.Algorithms.4.2.0\lib\net461\System.Security.Cryptography.Algorithms.dll - True - - - ..\..\packages\System.Security.Cryptography.Encoding.4.3.0\lib\net46\System.Security.Cryptography.Encoding.dll - True - - - ..\..\packages\System.Security.Cryptography.Primitives.4.0.0\lib\net46\System.Security.Cryptography.Primitives.dll - True - - - ..\..\packages\System.Security.Cryptography.X509Certificates.4.1.0\lib\net461\System.Security.Cryptography.X509Certificates.dll - True - - - ..\..\packages\System.Xml.ReaderWriter.4.3.0\lib\net46\System.Xml.ReaderWriter.dll - True - @@ -159,7 +87,6 @@ copy Cmd_GZip.exe gunzip.exe - @@ -171,12 +98,10 @@ copy Cmd_GZip.exe gunzip.exe + + + 1.3.3 + + - - - - This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. - - - - \ No newline at end of file + diff --git a/samples/ICSharpCode.SharpZipLib.Samples/cs/Cmd_GZip/packages.config b/samples/ICSharpCode.SharpZipLib.Samples/cs/Cmd_GZip/packages.config deleted file mode 100644 index d353d1619..000000000 --- a/samples/ICSharpCode.SharpZipLib.Samples/cs/Cmd_GZip/packages.config +++ /dev/null @@ -1,50 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/samples/ICSharpCode.SharpZipLib.Samples/cs/Cmd_Tar/Cmd_Tar.csproj b/samples/ICSharpCode.SharpZipLib.Samples/cs/Cmd_Tar/Cmd_Tar.csproj index db8910d00..d40eef52e 100644 --- a/samples/ICSharpCode.SharpZipLib.Samples/cs/Cmd_Tar/Cmd_Tar.csproj +++ b/samples/ICSharpCode.SharpZipLib.Samples/cs/Cmd_Tar/Cmd_Tar.csproj @@ -66,86 +66,14 @@ Cmd_Tar - - ..\..\packages\SharpZipLib.1.0.0-alpha1\lib\netstandard1.3\ICSharpCode.SharpZipLib.dll - True - - - ..\..\packages\Microsoft.Win32.Primitives.4.3.0\lib\net46\Microsoft.Win32.Primitives.dll - True - - - ..\..\packages\System.AppContext.4.3.0\lib\net46\System.AppContext.dll - True - - - ..\..\packages\System.Console.4.3.0\lib\net46\System.Console.dll - True - - - ..\..\packages\System.Diagnostics.DiagnosticSource.4.3.0\lib\net46\System.Diagnostics.DiagnosticSource.dll - True - - - ..\..\packages\System.Globalization.Calendars.4.3.0\lib\net46\System.Globalization.Calendars.dll - True - - - ..\..\packages\System.IO.Compression.4.3.0\lib\net46\System.IO.Compression.dll - True - - - ..\..\packages\System.IO.Compression.ZipFile.4.3.0\lib\net46\System.IO.Compression.ZipFile.dll - True - - - ..\..\packages\System.IO.FileSystem.4.3.0\lib\net46\System.IO.FileSystem.dll - True - - - ..\..\packages\System.IO.FileSystem.Primitives.4.3.0\lib\net46\System.IO.FileSystem.Primitives.dll - True - - - ..\..\packages\System.Net.Http.4.1.2\lib\net46\System.Net.Http.dll - True - - - ..\..\packages\System.Net.Sockets.4.3.0\lib\net46\System.Net.Sockets.dll - True - - - ..\..\packages\System.Runtime.InteropServices.RuntimeInformation.4.3.0\lib\net45\System.Runtime.InteropServices.RuntimeInformation.dll - True - - - ..\..\packages\System.Security.Cryptography.Algorithms.4.2.0\lib\net461\System.Security.Cryptography.Algorithms.dll - True - - - ..\..\packages\System.Security.Cryptography.Encoding.4.3.0\lib\net46\System.Security.Cryptography.Encoding.dll - True - - - ..\..\packages\System.Security.Cryptography.Primitives.4.0.0\lib\net46\System.Security.Cryptography.Primitives.dll - True - - - ..\..\packages\System.Security.Cryptography.X509Certificates.4.1.0\lib\net461\System.Security.Cryptography.X509Certificates.dll - True - - - ..\..\packages\System.Xml.ReaderWriter.4.3.0\lib\net46\System.Xml.ReaderWriter.dll - True - @@ -153,7 +81,6 @@ - @@ -162,15 +89,13 @@ true + + + 1.3.3 + + - - - - This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. - - - - \ No newline at end of file + diff --git a/samples/ICSharpCode.SharpZipLib.Samples/cs/Cmd_Tar/packages.config b/samples/ICSharpCode.SharpZipLib.Samples/cs/Cmd_Tar/packages.config deleted file mode 100644 index d353d1619..000000000 --- a/samples/ICSharpCode.SharpZipLib.Samples/cs/Cmd_Tar/packages.config +++ /dev/null @@ -1,50 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/samples/ICSharpCode.SharpZipLib.Samples/cs/Cmd_ZipInfo/Cmd_ZipInfo.csproj b/samples/ICSharpCode.SharpZipLib.Samples/cs/Cmd_ZipInfo/Cmd_ZipInfo.csproj index 12e856069..311fcb85d 100644 --- a/samples/ICSharpCode.SharpZipLib.Samples/cs/Cmd_ZipInfo/Cmd_ZipInfo.csproj +++ b/samples/ICSharpCode.SharpZipLib.Samples/cs/Cmd_ZipInfo/Cmd_ZipInfo.csproj @@ -73,84 +73,12 @@ true - - ..\..\packages\SharpZipLib.1.0.0-alpha1\lib\netstandard1.3\ICSharpCode.SharpZipLib.dll - True - - - ..\..\packages\Microsoft.Win32.Primitives.4.3.0\lib\net46\Microsoft.Win32.Primitives.dll - True - - - ..\..\packages\System.AppContext.4.3.0\lib\net46\System.AppContext.dll - True - - - ..\..\packages\System.Console.4.3.0\lib\net46\System.Console.dll - True - - - ..\..\packages\System.Diagnostics.DiagnosticSource.4.3.0\lib\net46\System.Diagnostics.DiagnosticSource.dll - True - - - ..\..\packages\System.Globalization.Calendars.4.3.0\lib\net46\System.Globalization.Calendars.dll - True - - - ..\..\packages\System.IO.Compression.4.3.0\lib\net46\System.IO.Compression.dll - True - - - ..\..\packages\System.IO.Compression.ZipFile.4.3.0\lib\net46\System.IO.Compression.ZipFile.dll - True - - - ..\..\packages\System.IO.FileSystem.4.3.0\lib\net46\System.IO.FileSystem.dll - True - - - ..\..\packages\System.IO.FileSystem.Primitives.4.3.0\lib\net46\System.IO.FileSystem.Primitives.dll - True - - - ..\..\packages\System.Net.Http.4.1.2\lib\net46\System.Net.Http.dll - True - - - ..\..\packages\System.Net.Sockets.4.3.0\lib\net46\System.Net.Sockets.dll - True - - - ..\..\packages\System.Runtime.InteropServices.RuntimeInformation.4.3.0\lib\net45\System.Runtime.InteropServices.RuntimeInformation.dll - True - - - ..\..\packages\System.Security.Cryptography.Algorithms.4.2.0\lib\net461\System.Security.Cryptography.Algorithms.dll - True - - - ..\..\packages\System.Security.Cryptography.Encoding.4.3.0\lib\net46\System.Security.Cryptography.Encoding.dll - True - - - ..\..\packages\System.Security.Cryptography.Primitives.4.0.0\lib\net46\System.Security.Cryptography.Primitives.dll - True - - - ..\..\packages\System.Security.Cryptography.X509Certificates.4.1.0\lib\net461\System.Security.Cryptography.X509Certificates.dll - True - - - ..\..\packages\System.Xml.ReaderWriter.4.3.0\lib\net46\System.Xml.ReaderWriter.dll - True - @@ -158,7 +86,6 @@ - @@ -170,12 +97,10 @@ + + + 1.3.3 + + - - - - This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. - - - - \ No newline at end of file + diff --git a/samples/ICSharpCode.SharpZipLib.Samples/cs/Cmd_ZipInfo/packages.config b/samples/ICSharpCode.SharpZipLib.Samples/cs/Cmd_ZipInfo/packages.config deleted file mode 100644 index d353d1619..000000000 --- a/samples/ICSharpCode.SharpZipLib.Samples/cs/Cmd_ZipInfo/packages.config +++ /dev/null @@ -1,50 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/samples/ICSharpCode.SharpZipLib.Samples/cs/CreateZipFile/CreateZipFile.csproj b/samples/ICSharpCode.SharpZipLib.Samples/cs/CreateZipFile/CreateZipFile.csproj index ed2046a94..efd2cd464 100644 --- a/samples/ICSharpCode.SharpZipLib.Samples/cs/CreateZipFile/CreateZipFile.csproj +++ b/samples/ICSharpCode.SharpZipLib.Samples/cs/CreateZipFile/CreateZipFile.csproj @@ -81,84 +81,12 @@ - - ..\..\packages\SharpZipLib.1.0.0-alpha1\lib\netstandard1.3\ICSharpCode.SharpZipLib.dll - True - - - ..\..\packages\Microsoft.Win32.Primitives.4.3.0\lib\net46\Microsoft.Win32.Primitives.dll - True - - - ..\..\packages\System.AppContext.4.3.0\lib\net46\System.AppContext.dll - True - - - ..\..\packages\System.Console.4.3.0\lib\net46\System.Console.dll - True - - - ..\..\packages\System.Diagnostics.DiagnosticSource.4.3.0\lib\net46\System.Diagnostics.DiagnosticSource.dll - True - - - ..\..\packages\System.Globalization.Calendars.4.3.0\lib\net46\System.Globalization.Calendars.dll - True - - - ..\..\packages\System.IO.Compression.4.3.0\lib\net46\System.IO.Compression.dll - True - - - ..\..\packages\System.IO.Compression.ZipFile.4.3.0\lib\net46\System.IO.Compression.ZipFile.dll - True - - - ..\..\packages\System.IO.FileSystem.4.3.0\lib\net46\System.IO.FileSystem.dll - True - - - ..\..\packages\System.IO.FileSystem.Primitives.4.3.0\lib\net46\System.IO.FileSystem.Primitives.dll - True - - - ..\..\packages\System.Net.Http.4.1.2\lib\net46\System.Net.Http.dll - True - - - ..\..\packages\System.Net.Sockets.4.3.0\lib\net46\System.Net.Sockets.dll - True - - - ..\..\packages\System.Runtime.InteropServices.RuntimeInformation.4.3.0\lib\net45\System.Runtime.InteropServices.RuntimeInformation.dll - True - - - ..\..\packages\System.Security.Cryptography.Algorithms.4.2.0\lib\net461\System.Security.Cryptography.Algorithms.dll - True - - - ..\..\packages\System.Security.Cryptography.Encoding.4.3.0\lib\net46\System.Security.Cryptography.Encoding.dll - True - - - ..\..\packages\System.Security.Cryptography.Primitives.4.0.0\lib\net46\System.Security.Cryptography.Primitives.dll - True - - - ..\..\packages\System.Security.Cryptography.X509Certificates.4.1.0\lib\net461\System.Security.Cryptography.X509Certificates.dll - True - - - ..\..\packages\System.Xml.ReaderWriter.4.3.0\lib\net46\System.Xml.ReaderWriter.dll - True - @@ -166,7 +94,6 @@ - @@ -178,12 +105,10 @@ + + + 1.3.3 + + - - - - This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. - - - - \ No newline at end of file + diff --git a/samples/ICSharpCode.SharpZipLib.Samples/cs/CreateZipFile/packages.config b/samples/ICSharpCode.SharpZipLib.Samples/cs/CreateZipFile/packages.config deleted file mode 100644 index d353d1619..000000000 --- a/samples/ICSharpCode.SharpZipLib.Samples/cs/CreateZipFile/packages.config +++ /dev/null @@ -1,50 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/samples/ICSharpCode.SharpZipLib.Samples/cs/FastZip/FastZip.csproj b/samples/ICSharpCode.SharpZipLib.Samples/cs/FastZip/FastZip.csproj index 7e52bb16f..efacf9ff8 100644 --- a/samples/ICSharpCode.SharpZipLib.Samples/cs/FastZip/FastZip.csproj +++ b/samples/ICSharpCode.SharpZipLib.Samples/cs/FastZip/FastZip.csproj @@ -67,84 +67,12 @@ ..\bin\FastZip.XML - - ..\..\packages\SharpZipLib.1.0.0-alpha1\lib\netstandard1.3\ICSharpCode.SharpZipLib.dll - True - - - ..\..\packages\Microsoft.Win32.Primitives.4.3.0\lib\net46\Microsoft.Win32.Primitives.dll - True - - - ..\..\packages\System.AppContext.4.3.0\lib\net46\System.AppContext.dll - True - - - ..\..\packages\System.Console.4.3.0\lib\net46\System.Console.dll - True - - - ..\..\packages\System.Diagnostics.DiagnosticSource.4.3.0\lib\net46\System.Diagnostics.DiagnosticSource.dll - True - - - ..\..\packages\System.Globalization.Calendars.4.3.0\lib\net46\System.Globalization.Calendars.dll - True - - - ..\..\packages\System.IO.Compression.4.3.0\lib\net46\System.IO.Compression.dll - True - - - ..\..\packages\System.IO.Compression.ZipFile.4.3.0\lib\net46\System.IO.Compression.ZipFile.dll - True - - - ..\..\packages\System.IO.FileSystem.4.3.0\lib\net46\System.IO.FileSystem.dll - True - - - ..\..\packages\System.IO.FileSystem.Primitives.4.3.0\lib\net46\System.IO.FileSystem.Primitives.dll - True - - - ..\..\packages\System.Net.Http.4.1.2\lib\net46\System.Net.Http.dll - True - - - ..\..\packages\System.Net.Sockets.4.3.0\lib\net46\System.Net.Sockets.dll - True - - - ..\..\packages\System.Runtime.InteropServices.RuntimeInformation.4.3.0\lib\net45\System.Runtime.InteropServices.RuntimeInformation.dll - True - - - ..\..\packages\System.Security.Cryptography.Algorithms.4.2.0\lib\net461\System.Security.Cryptography.Algorithms.dll - True - - - ..\..\packages\System.Security.Cryptography.Encoding.4.3.0\lib\net46\System.Security.Cryptography.Encoding.dll - True - - - ..\..\packages\System.Security.Cryptography.Primitives.4.0.0\lib\net46\System.Security.Cryptography.Primitives.dll - True - - - ..\..\packages\System.Security.Cryptography.X509Certificates.4.1.0\lib\net461\System.Security.Cryptography.X509Certificates.dll - True - - - ..\..\packages\System.Xml.ReaderWriter.4.3.0\lib\net46\System.Xml.ReaderWriter.dll - True - @@ -152,7 +80,6 @@ - @@ -161,12 +88,10 @@ true + + + 1.3.3 + + - - - - This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. - - - - \ No newline at end of file + diff --git a/samples/ICSharpCode.SharpZipLib.Samples/cs/FastZip/packages.config b/samples/ICSharpCode.SharpZipLib.Samples/cs/FastZip/packages.config deleted file mode 100644 index d353d1619..000000000 --- a/samples/ICSharpCode.SharpZipLib.Samples/cs/FastZip/packages.config +++ /dev/null @@ -1,50 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/samples/ICSharpCode.SharpZipLib.Samples/cs/sz/packages.config b/samples/ICSharpCode.SharpZipLib.Samples/cs/sz/packages.config deleted file mode 100644 index d353d1619..000000000 --- a/samples/ICSharpCode.SharpZipLib.Samples/cs/sz/packages.config +++ /dev/null @@ -1,50 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/samples/ICSharpCode.SharpZipLib.Samples/cs/sz/sz.cs b/samples/ICSharpCode.SharpZipLib.Samples/cs/sz/sz.cs index d928f8907..7a1d10a1b 100644 --- a/samples/ICSharpCode.SharpZipLib.Samples/cs/sz/sz.cs +++ b/samples/ICSharpCode.SharpZipLib.Samples/cs/sz/sz.cs @@ -294,7 +294,7 @@ bool SetArgs(string[] args) { try { int enc = int.Parse(optArg); if (Encoding.GetEncoding(enc) != null) { - ZipConstants.DefaultCodePage = enc; + ZipStrings.CodePage = enc; } else { result = false; Console.WriteLine("Invalid encoding " + args[argIndex]); @@ -306,7 +306,7 @@ bool SetArgs(string[] args) { } } else { try { - ZipConstants.DefaultCodePage = Encoding.GetEncoding(optArg).CodePage; + ZipStrings.CodePage = Encoding.GetEncoding(optArg).CodePage; } catch (Exception) { result = false; diff --git a/samples/ICSharpCode.SharpZipLib.Samples/cs/sz/sz.csproj b/samples/ICSharpCode.SharpZipLib.Samples/cs/sz/sz.csproj index 864be9d84..121d55859 100644 --- a/samples/ICSharpCode.SharpZipLib.Samples/cs/sz/sz.csproj +++ b/samples/ICSharpCode.SharpZipLib.Samples/cs/sz/sz.csproj @@ -62,84 +62,12 @@ false - - ..\..\packages\SharpZipLib.1.0.0-alpha1\lib\netstandard1.3\ICSharpCode.SharpZipLib.dll - True - - - ..\..\packages\Microsoft.Win32.Primitives.4.3.0\lib\net46\Microsoft.Win32.Primitives.dll - True - - - ..\..\packages\System.AppContext.4.3.0\lib\net46\System.AppContext.dll - True - - - ..\..\packages\System.Console.4.3.0\lib\net46\System.Console.dll - True - - - ..\..\packages\System.Diagnostics.DiagnosticSource.4.3.0\lib\net46\System.Diagnostics.DiagnosticSource.dll - True - - - ..\..\packages\System.Globalization.Calendars.4.3.0\lib\net46\System.Globalization.Calendars.dll - True - - - ..\..\packages\System.IO.Compression.4.3.0\lib\net46\System.IO.Compression.dll - True - - - ..\..\packages\System.IO.Compression.ZipFile.4.3.0\lib\net46\System.IO.Compression.ZipFile.dll - True - - - ..\..\packages\System.IO.FileSystem.4.3.0\lib\net46\System.IO.FileSystem.dll - True - - - ..\..\packages\System.IO.FileSystem.Primitives.4.3.0\lib\net46\System.IO.FileSystem.Primitives.dll - True - - - ..\..\packages\System.Net.Http.4.1.2\lib\net46\System.Net.Http.dll - True - - - ..\..\packages\System.Net.Sockets.4.3.0\lib\net46\System.Net.Sockets.dll - True - - - ..\..\packages\System.Runtime.InteropServices.RuntimeInformation.4.3.0\lib\net45\System.Runtime.InteropServices.RuntimeInformation.dll - True - - - ..\..\packages\System.Security.Cryptography.Algorithms.4.2.0\lib\net461\System.Security.Cryptography.Algorithms.dll - True - - - ..\..\packages\System.Security.Cryptography.Encoding.4.3.0\lib\net46\System.Security.Cryptography.Encoding.dll - True - - - ..\..\packages\System.Security.Cryptography.Primitives.4.0.0\lib\net46\System.Security.Cryptography.Primitives.dll - True - - - ..\..\packages\System.Security.Cryptography.X509Certificates.4.1.0\lib\net461\System.Security.Cryptography.X509Certificates.dll - True - - - ..\..\packages\System.Xml.ReaderWriter.4.3.0\lib\net46\System.Xml.ReaderWriter.dll - True - @@ -147,7 +75,6 @@ - @@ -156,12 +83,10 @@ true + + + 1.3.3 + + - - - - This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. - - - - \ No newline at end of file + diff --git a/samples/ICSharpCode.SharpZipLib.Samples/cs/unzipfile/packages.config b/samples/ICSharpCode.SharpZipLib.Samples/cs/unzipfile/packages.config deleted file mode 100644 index d353d1619..000000000 --- a/samples/ICSharpCode.SharpZipLib.Samples/cs/unzipfile/packages.config +++ /dev/null @@ -1,50 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/samples/ICSharpCode.SharpZipLib.Samples/cs/unzipfile/unzipfile.csproj b/samples/ICSharpCode.SharpZipLib.Samples/cs/unzipfile/unzipfile.csproj index 1566ae6f4..43403d1e4 100644 --- a/samples/ICSharpCode.SharpZipLib.Samples/cs/unzipfile/unzipfile.csproj +++ b/samples/ICSharpCode.SharpZipLib.Samples/cs/unzipfile/unzipfile.csproj @@ -38,88 +38,16 @@ UnZipFileClass - - ..\..\packages\SharpZipLib.1.0.0-alpha1\lib\netstandard1.3\ICSharpCode.SharpZipLib.dll - True - - - ..\..\packages\Microsoft.Win32.Primitives.4.3.0\lib\net46\Microsoft.Win32.Primitives.dll - True - - - ..\..\packages\System.AppContext.4.3.0\lib\net46\System.AppContext.dll - True - - - ..\..\packages\System.Console.4.3.0\lib\net46\System.Console.dll - True - - - ..\..\packages\System.Diagnostics.DiagnosticSource.4.3.0\lib\net46\System.Diagnostics.DiagnosticSource.dll - True - - - ..\..\packages\System.Globalization.Calendars.4.3.0\lib\net46\System.Globalization.Calendars.dll - True - - - ..\..\packages\System.IO.Compression.4.3.0\lib\net46\System.IO.Compression.dll - True - - - ..\..\packages\System.IO.Compression.ZipFile.4.3.0\lib\net46\System.IO.Compression.ZipFile.dll - True - - - ..\..\packages\System.IO.FileSystem.4.3.0\lib\net46\System.IO.FileSystem.dll - True - - - ..\..\packages\System.IO.FileSystem.Primitives.4.3.0\lib\net46\System.IO.FileSystem.Primitives.dll - True - - - ..\..\packages\System.Net.Http.4.1.2\lib\net46\System.Net.Http.dll - True - - - ..\..\packages\System.Net.Sockets.4.3.0\lib\net46\System.Net.Sockets.dll - True - - - ..\..\packages\System.Runtime.InteropServices.RuntimeInformation.4.3.0\lib\net45\System.Runtime.InteropServices.RuntimeInformation.dll - True - - - ..\..\packages\System.Security.Cryptography.Algorithms.4.2.0\lib\net461\System.Security.Cryptography.Algorithms.dll - True - - - ..\..\packages\System.Security.Cryptography.Encoding.4.3.0\lib\net46\System.Security.Cryptography.Encoding.dll - True - - - ..\..\packages\System.Security.Cryptography.Primitives.4.0.0\lib\net46\System.Security.Cryptography.Primitives.dll - True - - - ..\..\packages\System.Security.Cryptography.X509Certificates.4.1.0\lib\net461\System.Security.Cryptography.X509Certificates.dll - True - - - ..\..\packages\System.Xml.ReaderWriter.4.3.0\lib\net46\System.Xml.ReaderWriter.dll - True - @@ -127,19 +55,16 @@ - + + + 1.3.3 + + - - - - This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. - - - - \ No newline at end of file + diff --git a/samples/ICSharpCode.SharpZipLib.Samples/cs/viewzipfile/packages.config b/samples/ICSharpCode.SharpZipLib.Samples/cs/viewzipfile/packages.config deleted file mode 100644 index d353d1619..000000000 --- a/samples/ICSharpCode.SharpZipLib.Samples/cs/viewzipfile/packages.config +++ /dev/null @@ -1,50 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/samples/ICSharpCode.SharpZipLib.Samples/cs/viewzipfile/viewzipfile.csproj b/samples/ICSharpCode.SharpZipLib.Samples/cs/viewzipfile/viewzipfile.csproj index 0141dc7fa..4734de832 100644 --- a/samples/ICSharpCode.SharpZipLib.Samples/cs/viewzipfile/viewzipfile.csproj +++ b/samples/ICSharpCode.SharpZipLib.Samples/cs/viewzipfile/viewzipfile.csproj @@ -38,88 +38,16 @@ ViewZipFileClass - - ..\..\packages\SharpZipLib.1.0.0-alpha1\lib\netstandard1.3\ICSharpCode.SharpZipLib.dll - True - - - ..\..\packages\Microsoft.Win32.Primitives.4.3.0\lib\net46\Microsoft.Win32.Primitives.dll - True - - - ..\..\packages\System.AppContext.4.3.0\lib\net46\System.AppContext.dll - True - - - ..\..\packages\System.Console.4.3.0\lib\net46\System.Console.dll - True - - - ..\..\packages\System.Diagnostics.DiagnosticSource.4.3.0\lib\net46\System.Diagnostics.DiagnosticSource.dll - True - - - ..\..\packages\System.Globalization.Calendars.4.3.0\lib\net46\System.Globalization.Calendars.dll - True - - - ..\..\packages\System.IO.Compression.4.3.0\lib\net46\System.IO.Compression.dll - True - - - ..\..\packages\System.IO.Compression.ZipFile.4.3.0\lib\net46\System.IO.Compression.ZipFile.dll - True - - - ..\..\packages\System.IO.FileSystem.4.3.0\lib\net46\System.IO.FileSystem.dll - True - - - ..\..\packages\System.IO.FileSystem.Primitives.4.3.0\lib\net46\System.IO.FileSystem.Primitives.dll - True - - - ..\..\packages\System.Net.Http.4.1.2\lib\net46\System.Net.Http.dll - True - - - ..\..\packages\System.Net.Sockets.4.3.0\lib\net46\System.Net.Sockets.dll - True - - - ..\..\packages\System.Runtime.InteropServices.RuntimeInformation.4.3.0\lib\net45\System.Runtime.InteropServices.RuntimeInformation.dll - True - - - ..\..\packages\System.Security.Cryptography.Algorithms.4.2.0\lib\net461\System.Security.Cryptography.Algorithms.dll - True - - - ..\..\packages\System.Security.Cryptography.Encoding.4.3.0\lib\net46\System.Security.Cryptography.Encoding.dll - True - - - ..\..\packages\System.Security.Cryptography.Primitives.4.0.0\lib\net46\System.Security.Cryptography.Primitives.dll - True - - - ..\..\packages\System.Security.Cryptography.X509Certificates.4.1.0\lib\net461\System.Security.Cryptography.X509Certificates.dll - True - - - ..\..\packages\System.Xml.ReaderWriter.4.3.0\lib\net46\System.Xml.ReaderWriter.dll - True - @@ -127,19 +55,16 @@ - + + + 1.3.3 + + - - - - This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. - - - - \ No newline at end of file + diff --git a/samples/ICSharpCode.SharpZipLib.Samples/cs/zf/packages.config b/samples/ICSharpCode.SharpZipLib.Samples/cs/zf/packages.config deleted file mode 100644 index d353d1619..000000000 --- a/samples/ICSharpCode.SharpZipLib.Samples/cs/zf/packages.config +++ /dev/null @@ -1,50 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/samples/ICSharpCode.SharpZipLib.Samples/cs/zf/zf.cs b/samples/ICSharpCode.SharpZipLib.Samples/cs/zf/zf.cs index 88cd628ea..56eea376d 100644 --- a/samples/ICSharpCode.SharpZipLib.Samples/cs/zf/zf.cs +++ b/samples/ICSharpCode.SharpZipLib.Samples/cs/zf/zf.cs @@ -205,7 +205,7 @@ bool SetArgs(string[] args) #if OPTIONTEST Console.WriteLine("Encoding set to {0}", enc); #endif - ZipConstants.DefaultCodePage = enc; + ZipStrings.CodePage = enc; } else { @@ -223,7 +223,7 @@ bool SetArgs(string[] args) { try { - ZipConstants.DefaultCodePage = Encoding.GetEncoding(optArg).CodePage; + ZipStrings.CodePage = Encoding.GetEncoding(optArg).CodePage; } catch (Exception) { diff --git a/samples/ICSharpCode.SharpZipLib.Samples/cs/zf/zf.csproj b/samples/ICSharpCode.SharpZipLib.Samples/cs/zf/zf.csproj index d4802d44b..1dbf75744 100644 --- a/samples/ICSharpCode.SharpZipLib.Samples/cs/zf/zf.csproj +++ b/samples/ICSharpCode.SharpZipLib.Samples/cs/zf/zf.csproj @@ -62,84 +62,12 @@ false - - ..\..\packages\SharpZipLib.1.0.0-alpha1\lib\netstandard1.3\ICSharpCode.SharpZipLib.dll - True - - - ..\..\packages\Microsoft.Win32.Primitives.4.3.0\lib\net46\Microsoft.Win32.Primitives.dll - True - - - ..\..\packages\System.AppContext.4.3.0\lib\net46\System.AppContext.dll - True - - - ..\..\packages\System.Console.4.3.0\lib\net46\System.Console.dll - True - - - ..\..\packages\System.Diagnostics.DiagnosticSource.4.3.0\lib\net46\System.Diagnostics.DiagnosticSource.dll - True - - - ..\..\packages\System.Globalization.Calendars.4.3.0\lib\net46\System.Globalization.Calendars.dll - True - - - ..\..\packages\System.IO.Compression.4.3.0\lib\net46\System.IO.Compression.dll - True - - - ..\..\packages\System.IO.Compression.ZipFile.4.3.0\lib\net46\System.IO.Compression.ZipFile.dll - True - - - ..\..\packages\System.IO.FileSystem.4.3.0\lib\net46\System.IO.FileSystem.dll - True - - - ..\..\packages\System.IO.FileSystem.Primitives.4.3.0\lib\net46\System.IO.FileSystem.Primitives.dll - True - - - ..\..\packages\System.Net.Http.4.1.2\lib\net46\System.Net.Http.dll - True - - - ..\..\packages\System.Net.Sockets.4.3.0\lib\net46\System.Net.Sockets.dll - True - - - ..\..\packages\System.Runtime.InteropServices.RuntimeInformation.4.3.0\lib\net45\System.Runtime.InteropServices.RuntimeInformation.dll - True - - - ..\..\packages\System.Security.Cryptography.Algorithms.4.2.0\lib\net461\System.Security.Cryptography.Algorithms.dll - True - - - ..\..\packages\System.Security.Cryptography.Encoding.4.3.0\lib\net46\System.Security.Cryptography.Encoding.dll - True - - - ..\..\packages\System.Security.Cryptography.Primitives.4.0.0\lib\net46\System.Security.Cryptography.Primitives.dll - True - - - ..\..\packages\System.Security.Cryptography.X509Certificates.4.1.0\lib\net461\System.Security.Cryptography.X509Certificates.dll - True - - - ..\..\packages\System.Xml.ReaderWriter.4.3.0\lib\net46\System.Xml.ReaderWriter.dll - True - @@ -147,7 +75,6 @@ - @@ -156,12 +83,10 @@ true + + + 1.3.3 + + - - - - This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. - - - - \ No newline at end of file + diff --git a/samples/ICSharpCode.SharpZipLib.Samples/vb/CreateZipFile/CreateZipFile.vbproj b/samples/ICSharpCode.SharpZipLib.Samples/vb/CreateZipFile/CreateZipFile.vbproj index 4ed49c1cd..42db45963 100644 --- a/samples/ICSharpCode.SharpZipLib.Samples/vb/CreateZipFile/CreateZipFile.vbproj +++ b/samples/ICSharpCode.SharpZipLib.Samples/vb/CreateZipFile/CreateZipFile.vbproj @@ -66,86 +66,14 @@ false - - ..\..\packages\SharpZipLib.1.0.0-alpha1\lib\netstandard1.3\ICSharpCode.SharpZipLib.dll - True - - - ..\..\packages\Microsoft.Win32.Primitives.4.3.0\lib\net46\Microsoft.Win32.Primitives.dll - True - - - ..\..\packages\System.AppContext.4.3.0\lib\net46\System.AppContext.dll - True - - - ..\..\packages\System.Console.4.3.0\lib\net46\System.Console.dll - True - - - ..\..\packages\System.Diagnostics.DiagnosticSource.4.3.0\lib\net46\System.Diagnostics.DiagnosticSource.dll - True - - - ..\..\packages\System.Globalization.Calendars.4.3.0\lib\net46\System.Globalization.Calendars.dll - True - - - ..\..\packages\System.IO.Compression.4.3.0\lib\net46\System.IO.Compression.dll - True - - - ..\..\packages\System.IO.Compression.ZipFile.4.3.0\lib\net46\System.IO.Compression.ZipFile.dll - True - - - ..\..\packages\System.IO.FileSystem.4.3.0\lib\net46\System.IO.FileSystem.dll - True - - - ..\..\packages\System.IO.FileSystem.Primitives.4.3.0\lib\net46\System.IO.FileSystem.Primitives.dll - True - - - ..\..\packages\System.Net.Http.4.1.2\lib\net46\System.Net.Http.dll - True - - - ..\..\packages\System.Net.Sockets.4.3.0\lib\net46\System.Net.Sockets.dll - True - - - ..\..\packages\System.Runtime.InteropServices.RuntimeInformation.4.3.0\lib\net45\System.Runtime.InteropServices.RuntimeInformation.dll - True - - - ..\..\packages\System.Security.Cryptography.Algorithms.4.2.0\lib\net461\System.Security.Cryptography.Algorithms.dll - True - - - ..\..\packages\System.Security.Cryptography.Encoding.4.3.0\lib\net46\System.Security.Cryptography.Encoding.dll - True - - - ..\..\packages\System.Security.Cryptography.Primitives.4.0.0\lib\net46\System.Security.Cryptography.Primitives.dll - True - - - ..\..\packages\System.Security.Cryptography.X509Certificates.4.1.0\lib\net461\System.Security.Cryptography.X509Certificates.dll - True - - - ..\..\packages\System.Xml.ReaderWriter.4.3.0\lib\net46\System.Xml.ReaderWriter.dll - True - @@ -164,9 +92,12 @@ - - + + + 1.3.3 + + False @@ -175,11 +106,4 @@ - - - - This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. - - - \ No newline at end of file diff --git a/samples/ICSharpCode.SharpZipLib.Samples/vb/CreateZipFile/packages.config b/samples/ICSharpCode.SharpZipLib.Samples/vb/CreateZipFile/packages.config deleted file mode 100644 index d353d1619..000000000 --- a/samples/ICSharpCode.SharpZipLib.Samples/vb/CreateZipFile/packages.config +++ /dev/null @@ -1,50 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/samples/ICSharpCode.SharpZipLib.Samples/vb/WpfCreateZipFile/My Project/AssemblyInfo.vb b/samples/ICSharpCode.SharpZipLib.Samples/vb/WpfCreateZipFile/My Project/AssemblyInfo.vb index 798a0f93a..bdef02000 100644 --- a/samples/ICSharpCode.SharpZipLib.Samples/vb/WpfCreateZipFile/My Project/AssemblyInfo.vb +++ b/samples/ICSharpCode.SharpZipLib.Samples/vb/WpfCreateZipFile/My Project/AssemblyInfo.vb @@ -18,7 +18,7 @@ Imports System.Runtime.InteropServices 'NeutralResourceLanguage attribute below. Update the "en-US" in the line 'below to match the UICulture setting in the project file. - + 'The ThemeInfo attribute describes where any theme specific and generic resource dictionaries can be found. diff --git a/samples/ICSharpCode.SharpZipLib.Samples/vb/WpfCreateZipFile/WpfCreateZipFile.vbproj b/samples/ICSharpCode.SharpZipLib.Samples/vb/WpfCreateZipFile/WpfCreateZipFile.vbproj index 12a8ab1d3..d86ec9e00 100644 --- a/samples/ICSharpCode.SharpZipLib.Samples/vb/WpfCreateZipFile/WpfCreateZipFile.vbproj +++ b/samples/ICSharpCode.SharpZipLib.Samples/vb/WpfCreateZipFile/WpfCreateZipFile.vbproj @@ -73,97 +73,21 @@ My Project\app.manifest - - ..\..\packages\SharpZipLib.1.0.0-alpha1\lib\netstandard1.3\ICSharpCode.SharpZipLib.dll - True - - - ..\..\packages\Microsoft.Win32.Primitives.4.3.0\lib\net46\Microsoft.Win32.Primitives.dll - True - - - ..\..\packages\System.AppContext.4.3.0\lib\net46\System.AppContext.dll - True - - - ..\..\packages\System.Console.4.3.0\lib\net46\System.Console.dll - True - - - ..\..\packages\System.Diagnostics.DiagnosticSource.4.3.0\lib\net46\System.Diagnostics.DiagnosticSource.dll - True - - - ..\..\packages\System.Globalization.Calendars.4.3.0\lib\net46\System.Globalization.Calendars.dll - True - - - ..\..\packages\System.IO.Compression.4.3.0\lib\net46\System.IO.Compression.dll - True - - - ..\..\packages\System.IO.Compression.ZipFile.4.3.0\lib\net46\System.IO.Compression.ZipFile.dll - True - - - ..\..\packages\System.IO.FileSystem.4.3.0\lib\net46\System.IO.FileSystem.dll - True - - - ..\..\packages\System.IO.FileSystem.Primitives.4.3.0\lib\net46\System.IO.FileSystem.Primitives.dll - True - - - ..\..\packages\System.Net.Http.4.1.2\lib\net46\System.Net.Http.dll - True - - - ..\..\packages\System.Net.Sockets.4.3.0\lib\net46\System.Net.Sockets.dll - True - - - ..\..\packages\System.Runtime.InteropServices.RuntimeInformation.4.3.0\lib\net45\System.Runtime.InteropServices.RuntimeInformation.dll - True - - - ..\..\packages\System.Security.Cryptography.Algorithms.4.2.0\lib\net461\System.Security.Cryptography.Algorithms.dll - True - - - ..\..\packages\System.Security.Cryptography.Encoding.4.3.0\lib\net46\System.Security.Cryptography.Encoding.dll - True - - - ..\..\packages\System.Security.Cryptography.Primitives.4.0.0\lib\net46\System.Security.Cryptography.Primitives.dll - True - - - ..\..\packages\System.Security.Cryptography.X509Certificates.4.1.0\lib\net461\System.Security.Cryptography.X509Certificates.dll - True - 4.0 - - ..\..\packages\System.Xml.ReaderWriter.4.3.0\lib\net46\System.Xml.ReaderWriter.dll - True - - - ..\..\packages\WPFFolderBrowser.1.0.2\lib\WPFFolderBrowser.dll - True - @@ -234,7 +158,6 @@ Settings.Designer.vb - @@ -246,12 +169,13 @@ false + + + 1.3.3 + + + 1.0.2 + + - - - - This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. - - - \ No newline at end of file diff --git a/samples/ICSharpCode.SharpZipLib.Samples/vb/WpfCreateZipFile/packages.config b/samples/ICSharpCode.SharpZipLib.Samples/vb/WpfCreateZipFile/packages.config deleted file mode 100644 index 58052c86a..000000000 --- a/samples/ICSharpCode.SharpZipLib.Samples/vb/WpfCreateZipFile/packages.config +++ /dev/null @@ -1,51 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/samples/ICSharpCode.SharpZipLib.Samples/vb/minibzip2/minibzip2.vbproj b/samples/ICSharpCode.SharpZipLib.Samples/vb/minibzip2/minibzip2.vbproj index d253f603c..e15a05ec6 100644 --- a/samples/ICSharpCode.SharpZipLib.Samples/vb/minibzip2/minibzip2.vbproj +++ b/samples/ICSharpCode.SharpZipLib.Samples/vb/minibzip2/minibzip2.vbproj @@ -74,86 +74,14 @@ My Project\app.manifest - - ..\..\packages\SharpZipLib.1.0.0-alpha1\lib\netstandard1.3\ICSharpCode.SharpZipLib.dll - True - - - ..\..\packages\Microsoft.Win32.Primitives.4.3.0\lib\net46\Microsoft.Win32.Primitives.dll - True - - - ..\..\packages\System.AppContext.4.3.0\lib\net46\System.AppContext.dll - True - - - ..\..\packages\System.Console.4.3.0\lib\net46\System.Console.dll - True - - - ..\..\packages\System.Diagnostics.DiagnosticSource.4.3.0\lib\net46\System.Diagnostics.DiagnosticSource.dll - True - - - ..\..\packages\System.Globalization.Calendars.4.3.0\lib\net46\System.Globalization.Calendars.dll - True - - - ..\..\packages\System.IO.Compression.4.3.0\lib\net46\System.IO.Compression.dll - True - - - ..\..\packages\System.IO.Compression.ZipFile.4.3.0\lib\net46\System.IO.Compression.ZipFile.dll - True - - - ..\..\packages\System.IO.FileSystem.4.3.0\lib\net46\System.IO.FileSystem.dll - True - - - ..\..\packages\System.IO.FileSystem.Primitives.4.3.0\lib\net46\System.IO.FileSystem.Primitives.dll - True - - - ..\..\packages\System.Net.Http.4.1.2\lib\net46\System.Net.Http.dll - True - - - ..\..\packages\System.Net.Sockets.4.3.0\lib\net46\System.Net.Sockets.dll - True - - - ..\..\packages\System.Runtime.InteropServices.RuntimeInformation.4.3.0\lib\net45\System.Runtime.InteropServices.RuntimeInformation.dll - True - - - ..\..\packages\System.Security.Cryptography.Algorithms.4.2.0\lib\net461\System.Security.Cryptography.Algorithms.dll - True - - - ..\..\packages\System.Security.Cryptography.Encoding.4.3.0\lib\net46\System.Security.Cryptography.Encoding.dll - True - - - ..\..\packages\System.Security.Cryptography.Primitives.4.0.0\lib\net46\System.Security.Cryptography.Primitives.dll - True - - - ..\..\packages\System.Security.Cryptography.X509Certificates.4.1.0\lib\net461\System.Security.Cryptography.X509Certificates.dll - True - - - ..\..\packages\System.Xml.ReaderWriter.4.3.0\lib\net46\System.Xml.ReaderWriter.dll - True - @@ -182,7 +110,11 @@ Resources.Designer.vb - + + + 1.3.3 + + False @@ -193,7 +125,6 @@ - @@ -202,11 +133,4 @@ - - - - This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. - - - \ No newline at end of file diff --git a/samples/ICSharpCode.SharpZipLib.Samples/vb/minibzip2/packages.config b/samples/ICSharpCode.SharpZipLib.Samples/vb/minibzip2/packages.config deleted file mode 100644 index d353d1619..000000000 --- a/samples/ICSharpCode.SharpZipLib.Samples/vb/minibzip2/packages.config +++ /dev/null @@ -1,50 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/samples/ICSharpCode.SharpZipLib.Samples/vb/viewzipfile/packages.config b/samples/ICSharpCode.SharpZipLib.Samples/vb/viewzipfile/packages.config deleted file mode 100644 index d353d1619..000000000 --- a/samples/ICSharpCode.SharpZipLib.Samples/vb/viewzipfile/packages.config +++ /dev/null @@ -1,50 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/samples/ICSharpCode.SharpZipLib.Samples/vb/viewzipfile/viewzipfile.vbproj b/samples/ICSharpCode.SharpZipLib.Samples/vb/viewzipfile/viewzipfile.vbproj index ded5b3ade..c85b1082e 100644 --- a/samples/ICSharpCode.SharpZipLib.Samples/vb/viewzipfile/viewzipfile.vbproj +++ b/samples/ICSharpCode.SharpZipLib.Samples/vb/viewzipfile/viewzipfile.vbproj @@ -66,86 +66,14 @@ false - - ..\..\packages\SharpZipLib.1.0.0-alpha1\lib\netstandard1.3\ICSharpCode.SharpZipLib.dll - True - - - ..\..\packages\Microsoft.Win32.Primitives.4.3.0\lib\net46\Microsoft.Win32.Primitives.dll - True - - - ..\..\packages\System.AppContext.4.3.0\lib\net46\System.AppContext.dll - True - - - ..\..\packages\System.Console.4.3.0\lib\net46\System.Console.dll - True - - - ..\..\packages\System.Diagnostics.DiagnosticSource.4.3.0\lib\net46\System.Diagnostics.DiagnosticSource.dll - True - - - ..\..\packages\System.Globalization.Calendars.4.3.0\lib\net46\System.Globalization.Calendars.dll - True - - - ..\..\packages\System.IO.Compression.4.3.0\lib\net46\System.IO.Compression.dll - True - - - ..\..\packages\System.IO.Compression.ZipFile.4.3.0\lib\net46\System.IO.Compression.ZipFile.dll - True - - - ..\..\packages\System.IO.FileSystem.4.3.0\lib\net46\System.IO.FileSystem.dll - True - - - ..\..\packages\System.IO.FileSystem.Primitives.4.3.0\lib\net46\System.IO.FileSystem.Primitives.dll - True - - - ..\..\packages\System.Net.Http.4.1.2\lib\net46\System.Net.Http.dll - True - - - ..\..\packages\System.Net.Sockets.4.3.0\lib\net46\System.Net.Sockets.dll - True - - - ..\..\packages\System.Runtime.InteropServices.RuntimeInformation.4.3.0\lib\net45\System.Runtime.InteropServices.RuntimeInformation.dll - True - - - ..\..\packages\System.Security.Cryptography.Algorithms.4.2.0\lib\net461\System.Security.Cryptography.Algorithms.dll - True - - - ..\..\packages\System.Security.Cryptography.Encoding.4.3.0\lib\net46\System.Security.Cryptography.Encoding.dll - True - - - ..\..\packages\System.Security.Cryptography.Primitives.4.0.0\lib\net46\System.Security.Cryptography.Primitives.dll - True - - - ..\..\packages\System.Security.Cryptography.X509Certificates.4.1.0\lib\net461\System.Security.Cryptography.X509Certificates.dll - True - - - ..\..\packages\System.Xml.ReaderWriter.4.3.0\lib\net46\System.Xml.ReaderWriter.dll - True - @@ -160,7 +88,11 @@ Designer - + + + 1.3.3 + + False @@ -170,14 +102,6 @@ - - - - - This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. - - - \ No newline at end of file diff --git a/samples/ICSharpCode.SharpZipLib.Samples/vb/zipfiletest/packages.config b/samples/ICSharpCode.SharpZipLib.Samples/vb/zipfiletest/packages.config deleted file mode 100644 index d353d1619..000000000 --- a/samples/ICSharpCode.SharpZipLib.Samples/vb/zipfiletest/packages.config +++ /dev/null @@ -1,50 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/samples/ICSharpCode.SharpZipLib.Samples/vb/zipfiletest/zipfiletest.vbproj b/samples/ICSharpCode.SharpZipLib.Samples/vb/zipfiletest/zipfiletest.vbproj index a04d2231e..b8b02293e 100644 --- a/samples/ICSharpCode.SharpZipLib.Samples/vb/zipfiletest/zipfiletest.vbproj +++ b/samples/ICSharpCode.SharpZipLib.Samples/vb/zipfiletest/zipfiletest.vbproj @@ -76,86 +76,14 @@ 42353,42354,42355 - - ..\..\packages\SharpZipLib.1.0.0-alpha1\lib\netstandard1.3\ICSharpCode.SharpZipLib.dll - True - - - ..\..\packages\Microsoft.Win32.Primitives.4.3.0\lib\net46\Microsoft.Win32.Primitives.dll - True - - - ..\..\packages\System.AppContext.4.3.0\lib\net46\System.AppContext.dll - True - - - ..\..\packages\System.Console.4.3.0\lib\net46\System.Console.dll - True - - - ..\..\packages\System.Diagnostics.DiagnosticSource.4.3.0\lib\net46\System.Diagnostics.DiagnosticSource.dll - True - - - ..\..\packages\System.Globalization.Calendars.4.3.0\lib\net46\System.Globalization.Calendars.dll - True - - - ..\..\packages\System.IO.Compression.4.3.0\lib\net46\System.IO.Compression.dll - True - - - ..\..\packages\System.IO.Compression.ZipFile.4.3.0\lib\net46\System.IO.Compression.ZipFile.dll - True - - - ..\..\packages\System.IO.FileSystem.4.3.0\lib\net46\System.IO.FileSystem.dll - True - - - ..\..\packages\System.IO.FileSystem.Primitives.4.3.0\lib\net46\System.IO.FileSystem.Primitives.dll - True - - - ..\..\packages\System.Net.Http.4.1.2\lib\net46\System.Net.Http.dll - True - - - ..\..\packages\System.Net.Sockets.4.3.0\lib\net46\System.Net.Sockets.dll - True - - - ..\..\packages\System.Runtime.InteropServices.RuntimeInformation.4.3.0\lib\net45\System.Runtime.InteropServices.RuntimeInformation.dll - True - - - ..\..\packages\System.Security.Cryptography.Algorithms.4.2.0\lib\net461\System.Security.Cryptography.Algorithms.dll - True - - - ..\..\packages\System.Security.Cryptography.Encoding.4.3.0\lib\net46\System.Security.Cryptography.Encoding.dll - True - - - ..\..\packages\System.Security.Cryptography.Primitives.4.0.0\lib\net46\System.Security.Cryptography.Primitives.dll - True - - - ..\..\packages\System.Security.Cryptography.X509Certificates.4.1.0\lib\net461\System.Security.Cryptography.X509Certificates.dll - True - - - ..\..\packages\System.Xml.ReaderWriter.4.3.0\lib\net46\System.Xml.ReaderWriter.dll - True - @@ -170,7 +98,11 @@ Designer - + + + 1.3.3 + + False @@ -180,14 +112,6 @@ - - - - - This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. - - - \ No newline at end of file diff --git a/src/Directory.build.props b/src/Directory.build.props deleted file mode 100644 index 640cae627..000000000 --- a/src/Directory.build.props +++ /dev/null @@ -1,17 +0,0 @@ - - - ICSharpCode - ICSharpCode - SharpZipLib (#ziplib, formerly NZipLib) is a compression library for Zip, GZip, BZip2, and Tar written entirely in C# for .NET. It is implemented as an assembly (installable in the GAC), and thus can easily be incorporated into other projects (in any .NET language) - http://icsharpcode.github.io/SharpZipLib/#license - http://icsharpcode.github.io/SharpZipLib/ - https://github.com/PrismLibrary/Prism - Please see https://github.com/icsharpcode/SharpZipLib/wiki/Release-1.0 for more information. - Copyright © 2000-2017 SharpZipLib Contributors - Compression Library Zip GZip BZip2 LZW Tar - en-US - $(fullBuildSemanticVersion) - $(fullBuildVersion) - $(fullBuildVersion) - - \ No newline at end of file diff --git a/src/ICSharpCode.SharpZipLib/AssemblyInfo.cs b/src/ICSharpCode.SharpZipLib/AssemblyInfo.cs new file mode 100644 index 000000000..8f8e62016 --- /dev/null +++ b/src/ICSharpCode.SharpZipLib/AssemblyInfo.cs @@ -0,0 +1,3 @@ +using System.Runtime.CompilerServices; + +[assembly: InternalsVisibleTo("ICSharpCode.SharpZipLib.Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100b9a14ea8fc9d7599e0e82a1292a23103f0210e2f928a0f466963af23fffadba59dcc8c9e26ecd114d7c0b4179e4bc93b1656b7ee2d4a67dd7c1992653e0d9cc534f7914b6f583b022e0a7aa8a430f407932f9a6806f0fc64d61e78d5ae01aa8f8233196719d44da2c50a2d1cfa3f7abb7487b3567a4f0456aa6667154c6749b1")] diff --git a/src/ICSharpCode.SharpZipLib/BZip2/BZip2.cs b/src/ICSharpCode.SharpZipLib/BZip2/BZip2.cs index 896b3f6f5..4bd48b035 100644 --- a/src/ICSharpCode.SharpZipLib/BZip2/BZip2.cs +++ b/src/ICSharpCode.SharpZipLib/BZip2/BZip2.cs @@ -9,7 +9,7 @@ namespace ICSharpCode.SharpZipLib.BZip2 public static class BZip2 { /// - /// Decompress the input writing + /// Decompress the input writing /// uncompressed data to the output stream /// /// The readable stream containing data to decompress. @@ -17,17 +17,24 @@ public static class BZip2 /// Both streams are closed on completion if true. public static void Decompress(Stream inStream, Stream outStream, bool isStreamOwner) { - if (inStream == null || outStream == null) { - throw new Exception("Null Stream"); - } + if (inStream == null) + throw new ArgumentNullException(nameof(inStream)); + + if (outStream == null) + throw new ArgumentNullException(nameof(outStream)); - try { - using (BZip2InputStream bzipInput = new BZip2InputStream(inStream)) { + try + { + using (BZip2InputStream bzipInput = new BZip2InputStream(inStream)) + { bzipInput.IsStreamOwner = isStreamOwner; Core.StreamUtils.Copy(bzipInput, outStream, new byte[4096]); } - } finally { - if (isStreamOwner) { + } + finally + { + if (isStreamOwner) + { // inStream is closed by the BZip2InputStream if stream owner outStream.Dispose(); } @@ -35,32 +42,38 @@ public static void Decompress(Stream inStream, Stream outStream, bool isStreamOw } /// - /// Compress the input stream sending + /// Compress the input stream sending /// result data to output stream /// /// The readable stream to compress. /// The output stream to receive the compressed data. /// Both streams are closed on completion if true. - /// Block size acts as compression level (1 to 9) with 1 giving + /// Block size acts as compression level (1 to 9) with 1 giving /// the lowest compression and 9 the highest. public static void Compress(Stream inStream, Stream outStream, bool isStreamOwner, int level) { - if (inStream == null || outStream == null) { - throw new Exception("Null Stream"); - } + if (inStream == null) + throw new ArgumentNullException(nameof(inStream)); - try { - using (BZip2OutputStream bzipOutput = new BZip2OutputStream(outStream, level)) { + if (outStream == null) + throw new ArgumentNullException(nameof(outStream)); + + try + { + using (BZip2OutputStream bzipOutput = new BZip2OutputStream(outStream, level)) + { bzipOutput.IsStreamOwner = isStreamOwner; Core.StreamUtils.Copy(inStream, bzipOutput, new byte[4096]); } - } finally { - if (isStreamOwner) { + } + finally + { + if (isStreamOwner) + { // outStream is closed by the BZip2OutputStream if stream owner inStream.Dispose(); } } } - } } diff --git a/src/ICSharpCode.SharpZipLib/BZip2/BZip2Constants.cs b/src/ICSharpCode.SharpZipLib/BZip2/BZip2Constants.cs index 01bf81939..52fb8ad20 100644 --- a/src/ICSharpCode.SharpZipLib/BZip2/BZip2Constants.cs +++ b/src/ICSharpCode.SharpZipLib/BZip2/BZip2Constants.cs @@ -3,7 +3,7 @@ namespace ICSharpCode.SharpZipLib.BZip2 /// /// Defines internal values for both compression and decompression /// - internal sealed class BZip2Constants + internal static class BZip2Constants { /// /// Random numbers used to randomise repetitive blocks @@ -66,7 +66,7 @@ internal sealed class BZip2Constants /// /// When multiplied by compression parameter (1-9) gives the block size for compression /// 9 gives the best compression but uses the most memory. - /// + /// public const int BaseBlockSize = 100000; /// @@ -113,9 +113,5 @@ internal sealed class BZip2Constants /// Backend constant /// public const int OvershootBytes = 20; - - private BZip2Constants() - { - } } } diff --git a/src/ICSharpCode.SharpZipLib/BZip2/BZip2Exception.cs b/src/ICSharpCode.SharpZipLib/BZip2/BZip2Exception.cs index a77404dde..111d21cdc 100644 --- a/src/ICSharpCode.SharpZipLib/BZip2/BZip2Exception.cs +++ b/src/ICSharpCode.SharpZipLib/BZip2/BZip2Exception.cs @@ -1,10 +1,12 @@ using System; +using System.Runtime.Serialization; namespace ICSharpCode.SharpZipLib.BZip2 { /// /// BZip2Exception represents exceptions specific to BZip2 classes and code. /// + [Serializable] public class BZip2Exception : SharpZipBaseException { /// @@ -32,5 +34,21 @@ public BZip2Exception(string message, Exception innerException) : base(message, innerException) { } + + /// + /// Initializes a new instance of the BZip2Exception class with serialized data. + /// + /// + /// The System.Runtime.Serialization.SerializationInfo that holds the serialized + /// object data about the exception being thrown. + /// + /// + /// The System.Runtime.Serialization.StreamingContext that contains contextual information + /// about the source or destination. + /// + protected BZip2Exception(SerializationInfo info, StreamingContext context) + : base(info, context) + { + } } } diff --git a/src/ICSharpCode.SharpZipLib/BZip2/BZip2InputStream.cs b/src/ICSharpCode.SharpZipLib/BZip2/BZip2InputStream.cs index 4f141de11..3948b4e4c 100644 --- a/src/ICSharpCode.SharpZipLib/BZip2/BZip2InputStream.cs +++ b/src/ICSharpCode.SharpZipLib/BZip2/BZip2InputStream.cs @@ -1,89 +1,101 @@ +#if NETSTANDARD2_1_OR_GREATER || NETCOREAPP3_1_OR_GREATER + #define VECTORIZE_MEMORY_MOVE +#endif + +using ICSharpCode.SharpZipLib.Checksum; using System; using System.IO; -using ICSharpCode.SharpZipLib.Checksum; namespace ICSharpCode.SharpZipLib.BZip2 { /// - /// An input stream that decompresses files in the BZip2 format + /// An input stream that decompresses files in the BZip2 format /// public class BZip2InputStream : Stream { #region Constants - const int START_BLOCK_STATE = 1; - const int RAND_PART_A_STATE = 2; - const int RAND_PART_B_STATE = 3; - const int RAND_PART_C_STATE = 4; - const int NO_RAND_PART_A_STATE = 5; - const int NO_RAND_PART_B_STATE = 6; - const int NO_RAND_PART_C_STATE = 7; - #endregion + + private const int START_BLOCK_STATE = 1; + private const int RAND_PART_A_STATE = 2; + private const int RAND_PART_B_STATE = 3; + private const int RAND_PART_C_STATE = 4; + private const int NO_RAND_PART_A_STATE = 5; + private const int NO_RAND_PART_B_STATE = 6; + private const int NO_RAND_PART_C_STATE = 7; + +#if VECTORIZE_MEMORY_MOVE + private static readonly int VectorSize = System.Numerics.Vector.Count; +#endif // VECTORIZE_MEMORY_MOVE + +#endregion Constants #region Instance Fields + /*-- index of the last char in the block, so the block size == last + 1. --*/ - int last; + private int last; /*-- index in zptr[] of original string after sorting. --*/ - int origPtr; + private int origPtr; /*-- always: in the range 0 .. 9. The current block size is 100000 * this number. --*/ - int blockSize100k; + private int blockSize100k; - bool blockRandomised; + private bool blockRandomised; - int bsBuff; - int bsLive; - IChecksum mCrc = new BZip2Crc(); + private int bsBuff; + private int bsLive; + private IChecksum mCrc = new BZip2Crc(); - bool[] inUse = new bool[256]; - int nInUse; + private bool[] inUse = new bool[256]; + private int nInUse; - byte[] seqToUnseq = new byte[256]; - byte[] unseqToSeq = new byte[256]; + private byte[] seqToUnseq = new byte[256]; + private byte[] unseqToSeq = new byte[256]; - byte[] selector = new byte[BZip2Constants.MaximumSelectors]; - byte[] selectorMtf = new byte[BZip2Constants.MaximumSelectors]; + private byte[] selector = new byte[BZip2Constants.MaximumSelectors]; + private byte[] selectorMtf = new byte[BZip2Constants.MaximumSelectors]; - int[] tt; - byte[] ll8; + private int[] tt; + private byte[] ll8; /*-- freq table collected to save a pass over the data during decompression. --*/ - int[] unzftab = new int[256]; + private int[] unzftab = new int[256]; + + private int[][] limit = new int[BZip2Constants.GroupCount][]; + private int[][] baseArray = new int[BZip2Constants.GroupCount][]; + private int[][] perm = new int[BZip2Constants.GroupCount][]; + private int[] minLens = new int[BZip2Constants.GroupCount]; - int[][] limit = new int[BZip2Constants.GroupCount][]; - int[][] baseArray = new int[BZip2Constants.GroupCount][]; - int[][] perm = new int[BZip2Constants.GroupCount][]; - int[] minLens = new int[BZip2Constants.GroupCount]; + private readonly Stream baseStream; + private bool streamEnd; - readonly Stream baseStream; - bool streamEnd; + private int currentChar = -1; - int currentChar = -1; + private int currentState = START_BLOCK_STATE; - int currentState = START_BLOCK_STATE; + private int storedBlockCRC, storedCombinedCRC; + private int computedBlockCRC; + private uint computedCombinedCRC; - int storedBlockCRC, storedCombinedCRC; - int computedBlockCRC; - uint computedCombinedCRC; + private int count, chPrev, ch2; + private int tPos; + private int rNToGo; + private int rTPos; + private int i2, j2; + private byte z; - int count, chPrev, ch2; - int tPos; - int rNToGo; - int rTPos; - int i2, j2; - byte z; - #endregion + #endregion Instance Fields /// /// Construct instance for reading from stream @@ -94,7 +106,8 @@ public BZip2InputStream(Stream stream) if (stream == null) throw new ArgumentNullException(nameof(stream)); // init arrays - for (int i = 0; i < BZip2Constants.GroupCount; ++i) { + for (int i = 0; i < BZip2Constants.GroupCount; ++i) + { limit[i] = new int[BZip2Constants.MaximumAlphaSize]; baseArray[i] = new int[BZip2Constants.MaximumAlphaSize]; perm[i] = new int[BZip2Constants.MaximumAlphaSize]; @@ -115,11 +128,14 @@ public BZip2InputStream(Stream stream) public bool IsStreamOwner { get; set; } = true; #region Stream Overrides + /// /// Gets a value indicating if the stream supports reading /// - public override bool CanRead { - get { + public override bool CanRead + { + get + { return baseStream.CanRead; } } @@ -127,8 +143,10 @@ public override bool CanRead { /// /// Gets a value indicating whether the current stream supports seeking. /// - public override bool CanSeek { - get { + public override bool CanSeek + { + get + { return false; } } @@ -137,8 +155,10 @@ public override bool CanSeek { /// Gets a value indicating whether the current stream supports writing. /// This property always returns false /// - public override bool CanWrite { - get { + public override bool CanWrite + { + get + { return false; } } @@ -146,8 +166,10 @@ public override bool CanWrite { /// /// Gets the length in bytes of the stream. /// - public override long Length { - get { + public override long Length + { + get + { return baseStream.Length; } } @@ -157,11 +179,14 @@ public override long Length { /// Setting the position is not supported and will throw a NotSupportException. /// /// Any attempt to set the position. - public override long Position { - get { + public override long Position + { + get + { return baseStream.Position; } - set { + set + { throw new NotSupportedException("BZip2InputStream position cannot be set"); } } @@ -228,18 +253,21 @@ public override void WriteByte(byte value) /// Offset in array to begin storing data /// The maximum number of bytes to read /// The total number of bytes read into the buffer. This might be less - /// than the number of bytes requested if that number of bytes are not + /// than the number of bytes requested if that number of bytes are not /// currently available or zero if the end of the stream is reached. /// public override int Read(byte[] buffer, int offset, int count) { - if (buffer == null) { + if (buffer == null) + { throw new ArgumentNullException(nameof(buffer)); } - for (int i = 0; i < count; ++i) { + for (int i = 0; i < count; ++i) + { int rb = ReadByte(); - if (rb == -1) { + if (rb == -1) + { return i; } buffer[offset + i] = (byte)rb; @@ -252,34 +280,42 @@ public override int Read(byte[] buffer, int offset, int count) /// protected override void Dispose(bool disposing) { - if (disposing && IsStreamOwner) { + if (disposing && IsStreamOwner) + { baseStream.Dispose(); } } + /// /// Read a byte from stream advancing position /// /// byte read or -1 on end of stream public override int ReadByte() { - if (streamEnd) { + if (streamEnd) + { return -1; // ok } int retChar = currentChar; - switch (currentState) { + switch (currentState) + { case RAND_PART_B_STATE: SetupRandPartB(); break; + case RAND_PART_C_STATE: SetupRandPartC(); break; + case NO_RAND_PART_B_STATE: SetupNoRandPartB(); break; + case NO_RAND_PART_C_STATE: SetupNoRandPartC(); break; + case START_BLOCK_STATE: case NO_RAND_PART_A_STATE: case RAND_PART_A_STATE: @@ -288,13 +324,15 @@ public override int ReadByte() return retChar; } - #endregion + #endregion Stream Overrides - void MakeMaps() + private void MakeMaps() { nInUse = 0; - for (int i = 0; i < 256; ++i) { - if (inUse[i]) { + for (int i = 0; i < 256; ++i) + { + if (inUse[i]) + { seqToUnseq[nInUse] = (byte)i; unseqToSeq[i] = (byte)nInUse; nInUse++; @@ -302,7 +340,7 @@ void MakeMaps() } } - void Initialize() + private void Initialize() { char magic1 = BsGetUChar(); char magic2 = BsGetUChar(); @@ -310,7 +348,8 @@ void Initialize() char magic3 = BsGetUChar(); char magic4 = BsGetUChar(); - if (magic1 != 'B' || magic2 != 'Z' || magic3 != 'h' || magic4 < '1' || magic4 > '9') { + if (magic1 != 'B' || magic2 != 'Z' || magic3 != 'h' || magic4 < '1' || magic4 > '9') + { streamEnd = true; return; } @@ -319,7 +358,7 @@ void Initialize() computedCombinedCRC = 0; } - void InitBlock() + private void InitBlock() { char magic1 = BsGetUChar(); char magic2 = BsGetUChar(); @@ -328,12 +367,14 @@ void InitBlock() char magic5 = BsGetUChar(); char magic6 = BsGetUChar(); - if (magic1 == 0x17 && magic2 == 0x72 && magic3 == 0x45 && magic4 == 0x38 && magic5 == 0x50 && magic6 == 0x90) { + if (magic1 == 0x17 && magic2 == 0x72 && magic3 == 0x45 && magic4 == 0x38 && magic5 == 0x50 && magic6 == 0x90) + { Complete(); return; } - if (magic1 != 0x31 || magic2 != 0x41 || magic3 != 0x59 || magic4 != 0x26 || magic5 != 0x53 || magic6 != 0x59) { + if (magic1 != 0x31 || magic2 != 0x41 || magic3 != 0x59 || magic4 != 0x26 || magic5 != 0x53 || magic6 != 0x59) + { BadBlockHeader(); streamEnd = true; return; @@ -349,12 +390,13 @@ void InitBlock() currentState = START_BLOCK_STATE; } - void EndBlock() + private void EndBlock() { computedBlockCRC = (int)mCrc.Value; // -- A bad CRC is considered a fatal error. -- - if (storedBlockCRC != computedBlockCRC) { + if (storedBlockCRC != computedBlockCRC) + { CrcError(); } @@ -363,27 +405,32 @@ void EndBlock() computedCombinedCRC = computedCombinedCRC ^ (uint)computedBlockCRC; } - void Complete() + private void Complete() { storedCombinedCRC = BsGetInt32(); - if (storedCombinedCRC != (int)computedCombinedCRC) { + if (storedCombinedCRC != (int)computedCombinedCRC) + { CrcError(); } streamEnd = true; } - void FillBuffer() + private void FillBuffer() { int thech = 0; - try { + try + { thech = baseStream.ReadByte(); - } catch (Exception) { + } + catch (Exception) + { CompressedStreamEOF(); } - if (thech == -1) { + if (thech == -1) + { CompressedStreamEOF(); } @@ -391,9 +438,10 @@ void FillBuffer() bsLive += 8; } - int BsR(int n) + private int BsR(int n) { - while (bsLive < n) { + while (bsLive < n) + { FillBuffer(); } @@ -402,17 +450,17 @@ int BsR(int n) return v; } - char BsGetUChar() + private char BsGetUChar() { return (char)BsR(8); } - int BsGetIntVS(int numBits) + private int BsGetIntVS(int numBits) { return BsR(numBits); } - int BsGetInt32() + private int BsGetInt32() { int result = BsR(8); result = (result << 8) | BsR(8); @@ -421,27 +469,35 @@ int BsGetInt32() return result; } - void RecvDecodingTables() + private void RecvDecodingTables() { char[][] len = new char[BZip2Constants.GroupCount][]; - for (int i = 0; i < BZip2Constants.GroupCount; ++i) { + for (int i = 0; i < BZip2Constants.GroupCount; ++i) + { len[i] = new char[BZip2Constants.MaximumAlphaSize]; } bool[] inUse16 = new bool[16]; //--- Receive the mapping table --- - for (int i = 0; i < 16; i++) { + for (int i = 0; i < 16; i++) + { inUse16[i] = (BsR(1) == 1); } - for (int i = 0; i < 16; i++) { - if (inUse16[i]) { - for (int j = 0; j < 16; j++) { + for (int i = 0; i < 16; i++) + { + if (inUse16[i]) + { + for (int j = 0; j < 16; j++) + { inUse[i * 16 + j] = (BsR(1) == 1); } - } else { - for (int j = 0; j < 16; j++) { + } + else + { + for (int j = 0; j < 16; j++) + { inUse[i * 16 + j] = false; } } @@ -454,9 +510,11 @@ void RecvDecodingTables() int nGroups = BsR(3); int nSelectors = BsR(15); - for (int i = 0; i < nSelectors; i++) { + for (int i = 0; i < nSelectors; i++) + { int j = 0; - while (BsR(1) == 1) { + while (BsR(1) == 1) + { j++; } selectorMtf[i] = (byte)j; @@ -464,14 +522,17 @@ void RecvDecodingTables() //--- Undo the MTF values for the selectors. --- byte[] pos = new byte[BZip2Constants.GroupCount]; - for (int v = 0; v < nGroups; v++) { + for (int v = 0; v < nGroups; v++) + { pos[v] = (byte)v; } - for (int i = 0; i < nSelectors; i++) { + for (int i = 0; i < nSelectors; i++) + { int v = selectorMtf[i]; byte tmp = pos[v]; - while (v > 0) { + while (v > 0) + { pos[v] = pos[v - 1]; v--; } @@ -480,13 +541,19 @@ void RecvDecodingTables() } //--- Now the coding tables --- - for (int t = 0; t < nGroups; t++) { + for (int t = 0; t < nGroups; t++) + { int curr = BsR(5); - for (int i = 0; i < alphaSize; i++) { - while (BsR(1) == 1) { - if (BsR(1) == 0) { + for (int i = 0; i < alphaSize; i++) + { + while (BsR(1) == 1) + { + if (BsR(1) == 0) + { curr++; - } else { + } + else + { curr--; } } @@ -495,10 +562,12 @@ void RecvDecodingTables() } //--- Create the Huffman decoding tables --- - for (int t = 0; t < nGroups; t++) { + for (int t = 0; t < nGroups; t++) + { int minLen = 32; int maxLen = 0; - for (int i = 0; i < alphaSize; i++) { + for (int i = 0; i < alphaSize; i++) + { maxLen = Math.Max(maxLen, len[t][i]); minLen = Math.Min(minLen, len[t][i]); } @@ -507,7 +576,7 @@ void RecvDecodingTables() } } - void GetAndMoveToFrontDecode() + private void GetAndMoveToFrontDecode() { byte[] yy = new byte[256]; int nextSym; @@ -526,17 +595,20 @@ Setting up the unzftab entries here is not strictly in a separate pass, and so saves a block's worth of cache misses. --*/ - for (int i = 0; i <= 255; i++) { + for (int i = 0; i <= 255; i++) + { unzftab[i] = 0; } - for (int i = 0; i <= 255; i++) { + for (int i = 0; i <= 255; i++) + { yy[i] = (byte)i; } last = -1; - if (groupPos == 0) { + if (groupPos == 0) + { groupNo++; groupPos = BZip2Constants.GroupSize; } @@ -547,41 +619,53 @@ cache misses. int zvec = BsR(zn); int zj; - while (zvec > limit[zt][zn]) { - if (zn > 20) { // the longest code + while (zvec > limit[zt][zn]) + { + if (zn > 20) + { // the longest code throw new BZip2Exception("Bzip data error"); } zn++; - while (bsLive < 1) { + while (bsLive < 1) + { FillBuffer(); } zj = (bsBuff >> (bsLive - 1)) & 1; bsLive--; zvec = (zvec << 1) | zj; } - if (zvec - baseArray[zt][zn] < 0 || zvec - baseArray[zt][zn] >= BZip2Constants.MaximumAlphaSize) { + if (zvec - baseArray[zt][zn] < 0 || zvec - baseArray[zt][zn] >= BZip2Constants.MaximumAlphaSize) + { throw new BZip2Exception("Bzip data error"); } nextSym = perm[zt][zvec - baseArray[zt][zn]]; - while (true) { - if (nextSym == EOB) { + while (true) + { + if (nextSym == EOB) + { break; } - if (nextSym == BZip2Constants.RunA || nextSym == BZip2Constants.RunB) { + if (nextSym == BZip2Constants.RunA || nextSym == BZip2Constants.RunB) + { int s = -1; int n = 1; - do { - if (nextSym == BZip2Constants.RunA) { + do + { + if (nextSym == BZip2Constants.RunA) + { s += (0 + 1) * n; - } else if (nextSym == BZip2Constants.RunB) { + } + else if (nextSym == BZip2Constants.RunB) + { s += (1 + 1) * n; } n <<= 1; - if (groupPos == 0) { + if (groupPos == 0) + { groupNo++; groupPos = BZip2Constants.GroupSize; } @@ -592,9 +676,11 @@ cache misses. zn = minLens[zt]; zvec = BsR(zn); - while (zvec > limit[zt][zn]) { + while (zvec > limit[zt][zn]) + { zn++; - while (bsLive < 1) { + while (bsLive < 1) + { FillBuffer(); } zj = (bsBuff >> (bsLive - 1)) & 1; @@ -608,19 +694,24 @@ cache misses. byte ch = seqToUnseq[yy[0]]; unzftab[ch] += s; - while (s > 0) { + while (s > 0) + { last++; ll8[last] = ch; s--; } - if (last >= limitLast) { + if (last >= limitLast) + { BlockOverrun(); } continue; - } else { + } + else + { last++; - if (last >= limitLast) { + if (last >= limitLast) + { BlockOverrun(); } @@ -628,12 +719,31 @@ cache misses. unzftab[seqToUnseq[tmp]]++; ll8[last] = seqToUnseq[tmp]; - for (int j = nextSym - 1; j > 0; --j) { - yy[j] = yy[j - 1]; + var j = nextSym - 1; + +#if VECTORIZE_MEMORY_MOVE + // This is vectorized memory move. Going from the back, we're taking chunks of array + // and write them at the new location shifted by one. Since chunks are VectorSize long, + // at the end we have to move "tail" (or head actually) of the array using a plain loop. + // If System.Numerics.Vector API is not available, the plain loop is used to do the whole copying. + + while(j >= VectorSize) + { + var arrayPart = new System.Numerics.Vector(yy, j - VectorSize); + arrayPart.CopyTo(yy, j - VectorSize + 1); + j -= VectorSize; } +#endif // VECTORIZE_MEMORY_MOVE + + while(j > 0) + { + yy[j] = yy[--j]; + } + yy[0] = tmp; - if (groupPos == 0) { + if (groupPos == 0) + { groupNo++; groupPos = BZip2Constants.GroupSize; } @@ -642,9 +752,11 @@ cache misses. zt = selector[groupNo]; zn = minLens[zt]; zvec = BsR(zn); - while (zvec > limit[zt][zn]) { + while (zvec > limit[zt][zn]) + { zn++; - while (bsLive < 1) { + while (bsLive < 1) + { FillBuffer(); } zj = (bsBuff >> (bsLive - 1)) & 1; @@ -657,18 +769,20 @@ cache misses. } } - void SetupBlock() + private void SetupBlock() { int[] cftab = new int[257]; cftab[0] = 0; Array.Copy(unzftab, 0, cftab, 1, 256); - for (int i = 1; i <= 256; i++) { + for (int i = 1; i <= 256; i++) + { cftab[i] += cftab[i - 1]; } - for (int i = 0; i <= last; i++) { + for (int i = 0; i <= last; i++) + { byte ch = ll8[i]; tt[cftab[ch]] = i; cftab[ch]++; @@ -682,25 +796,31 @@ void SetupBlock() i2 = 0; ch2 = 256; /*-- not a char and not EOF --*/ - if (blockRandomised) { + if (blockRandomised) + { rNToGo = 0; rTPos = 0; SetupRandPartA(); - } else { + } + else + { SetupNoRandPartA(); } } - void SetupRandPartA() + private void SetupRandPartA() { - if (i2 <= last) { + if (i2 <= last) + { chPrev = ch2; ch2 = ll8[tPos]; tPos = tt[tPos]; - if (rNToGo == 0) { + if (rNToGo == 0) + { rNToGo = BZip2Constants.RandomNumbers[rTPos]; rTPos++; - if (rTPos == 512) { + if (rTPos == 512) + { rTPos = 0; } } @@ -711,16 +831,19 @@ void SetupRandPartA() currentChar = ch2; currentState = RAND_PART_B_STATE; mCrc.Update(ch2); - } else { + } + else + { EndBlock(); InitBlock(); SetupBlock(); } } - void SetupNoRandPartA() + private void SetupNoRandPartA() { - if (i2 <= last) { + if (i2 <= last) + { chPrev = ch2; ch2 = ll8[tPos]; tPos = tt[tPos]; @@ -729,28 +852,36 @@ void SetupNoRandPartA() currentChar = ch2; currentState = NO_RAND_PART_B_STATE; mCrc.Update(ch2); - } else { + } + else + { EndBlock(); InitBlock(); SetupBlock(); } } - void SetupRandPartB() + private void SetupRandPartB() { - if (ch2 != chPrev) { + if (ch2 != chPrev) + { currentState = RAND_PART_A_STATE; count = 1; SetupRandPartA(); - } else { + } + else + { count++; - if (count >= 4) { + if (count >= 4) + { z = ll8[tPos]; tPos = tt[tPos]; - if (rNToGo == 0) { + if (rNToGo == 0) + { rNToGo = BZip2Constants.RandomNumbers[rTPos]; rTPos++; - if (rTPos == 512) { + if (rTPos == 512) + { rTPos = 0; } } @@ -759,20 +890,25 @@ void SetupRandPartB() j2 = 0; currentState = RAND_PART_C_STATE; SetupRandPartC(); - } else { + } + else + { currentState = RAND_PART_A_STATE; SetupRandPartA(); } } } - void SetupRandPartC() + private void SetupRandPartC() { - if (j2 < (int)z) { + if (j2 < (int)z) + { currentChar = ch2; mCrc.Update(ch2); j2++; - } else { + } + else + { currentState = RAND_PART_A_STATE; i2++; count = 0; @@ -780,34 +916,43 @@ void SetupRandPartC() } } - void SetupNoRandPartB() + private void SetupNoRandPartB() { - if (ch2 != chPrev) { + if (ch2 != chPrev) + { currentState = NO_RAND_PART_A_STATE; count = 1; SetupNoRandPartA(); - } else { + } + else + { count++; - if (count >= 4) { + if (count >= 4) + { z = ll8[tPos]; tPos = tt[tPos]; currentState = NO_RAND_PART_C_STATE; j2 = 0; SetupNoRandPartC(); - } else { + } + else + { currentState = NO_RAND_PART_A_STATE; SetupNoRandPartA(); } } } - void SetupNoRandPartC() + private void SetupNoRandPartC() { - if (j2 < (int)z) { + if (j2 < (int)z) + { currentChar = ch2; mCrc.Update(ch2); j2++; - } else { + } + else + { currentState = NO_RAND_PART_A_STATE; i2++; count = 0; @@ -815,15 +960,17 @@ void SetupNoRandPartC() } } - void SetDecompressStructureSizes(int newSize100k) + private void SetDecompressStructureSizes(int newSize100k) { - if (!(0 <= newSize100k && newSize100k <= 9 && 0 <= blockSize100k && blockSize100k <= 9)) { + if (!(0 <= newSize100k && newSize100k <= 9 && 0 <= blockSize100k && blockSize100k <= 9)) + { throw new BZip2Exception("Invalid block size"); } blockSize100k = newSize100k; - if (newSize100k == 0) { + if (newSize100k == 0) + { return; } @@ -832,64 +979,73 @@ void SetDecompressStructureSizes(int newSize100k) tt = new int[n]; } - static void CompressedStreamEOF() + private static void CompressedStreamEOF() { throw new EndOfStreamException("BZip2 input stream end of compressed stream"); } - static void BlockOverrun() + private static void BlockOverrun() { throw new BZip2Exception("BZip2 input stream block overrun"); } - static void BadBlockHeader() + private static void BadBlockHeader() { throw new BZip2Exception("BZip2 input stream bad block header"); } - static void CrcError() + private static void CrcError() { throw new BZip2Exception("BZip2 input stream crc error"); } - static void HbCreateDecodeTables(int[] limit, int[] baseArray, int[] perm, char[] length, int minLen, int maxLen, int alphaSize) + private static void HbCreateDecodeTables(int[] limit, int[] baseArray, int[] perm, char[] length, int minLen, int maxLen, int alphaSize) { int pp = 0; - for (int i = minLen; i <= maxLen; ++i) { - for (int j = 0; j < alphaSize; ++j) { - if (length[j] == i) { + for (int i = minLen; i <= maxLen; ++i) + { + for (int j = 0; j < alphaSize; ++j) + { + if (length[j] == i) + { perm[pp] = j; ++pp; } } } - for (int i = 0; i < BZip2Constants.MaximumCodeLength; i++) { + for (int i = 0; i < BZip2Constants.MaximumCodeLength; i++) + { baseArray[i] = 0; } - for (int i = 0; i < alphaSize; i++) { + for (int i = 0; i < alphaSize; i++) + { ++baseArray[length[i] + 1]; } - for (int i = 1; i < BZip2Constants.MaximumCodeLength; i++) { + for (int i = 1; i < BZip2Constants.MaximumCodeLength; i++) + { baseArray[i] += baseArray[i - 1]; } - for (int i = 0; i < BZip2Constants.MaximumCodeLength; i++) { + for (int i = 0; i < BZip2Constants.MaximumCodeLength; i++) + { limit[i] = 0; } int vec = 0; - for (int i = minLen; i <= maxLen; i++) { + for (int i = minLen; i <= maxLen; i++) + { vec += (baseArray[i + 1] - baseArray[i]); limit[i] = vec - 1; vec <<= 1; } - for (int i = minLen + 1; i <= maxLen; i++) { + for (int i = minLen + 1; i <= maxLen; i++) + { baseArray[i] = ((limit[i - 1] + 1) << 1) - baseArray[i]; } } diff --git a/src/ICSharpCode.SharpZipLib/BZip2/BZip2OutputStream.cs b/src/ICSharpCode.SharpZipLib/BZip2/BZip2OutputStream.cs index 008015052..f331ec657 100644 --- a/src/ICSharpCode.SharpZipLib/BZip2/BZip2OutputStream.cs +++ b/src/ICSharpCode.SharpZipLib/BZip2/BZip2OutputStream.cs @@ -1,22 +1,23 @@ +using ICSharpCode.SharpZipLib.Checksum; using System; using System.IO; -using ICSharpCode.SharpZipLib.Checksum; namespace ICSharpCode.SharpZipLib.BZip2 { /// - /// An output stream that compresses into the BZip2 format + /// An output stream that compresses into the BZip2 format /// including file header chars into another stream. /// public class BZip2OutputStream : Stream { #region Constants - const int SETMASK = (1 << 21); - const int CLEARMASK = (~SETMASK); - const int GREATER_ICOST = 15; - const int LESSER_ICOST = 0; - const int SMALL_THRESH = 20; - const int DEPTH_THRESH = 10; + + private const int SETMASK = (1 << 21); + private const int CLEARMASK = (~SETMASK); + private const int GREATER_ICOST = 15; + private const int LESSER_ICOST = 0; + private const int SMALL_THRESH = 20; + private const int DEPTH_THRESH = 10; /*-- If you are ever unlucky/improbable enough @@ -26,7 +27,7 @@ again. In practice I have never seen the stack go above 27 elems, so the following limit seems very generous. --*/ - const int QSORT_STACK_SIZE = 1000; + private const int QSORT_STACK_SIZE = 1000; /*-- Knuth's increments seem to work better @@ -34,75 +35,79 @@ than Incerpi-Sedgewick here. Possibly because the number of elems to sort is usually small, typically <= 20. --*/ - readonly int[] increments = { + + private readonly int[] increments = { 1, 4, 13, 40, 121, 364, 1093, 3280, 9841, 29524, 88573, 265720, 797161, 2391484 }; - #endregion + + #endregion Constants #region Instance Fields + /*-- index of the last char in the block, so the block size == last + 1. --*/ - int last; + private int last; /*-- index in zptr[] of original string after sorting. --*/ - int origPtr; + private int origPtr; /*-- always: in the range 0 .. 9. The current block size is 100000 * this number. --*/ - int blockSize100k; + private int blockSize100k; - bool blockRandomised; + private bool blockRandomised; - int bytesOut; - int bsBuff; - int bsLive; - IChecksum mCrc = new BZip2Crc(); + private int bytesOut; + private int bsBuff; + private int bsLive; + private IChecksum mCrc = new BZip2Crc(); - bool[] inUse = new bool[256]; - int nInUse; + private bool[] inUse = new bool[256]; + private int nInUse; - char[] seqToUnseq = new char[256]; - char[] unseqToSeq = new char[256]; + private char[] seqToUnseq = new char[256]; + private char[] unseqToSeq = new char[256]; - char[] selector = new char[BZip2Constants.MaximumSelectors]; - char[] selectorMtf = new char[BZip2Constants.MaximumSelectors]; + private char[] selector = new char[BZip2Constants.MaximumSelectors]; + private char[] selectorMtf = new char[BZip2Constants.MaximumSelectors]; - byte[] block; - int[] quadrant; - int[] zptr; - short[] szptr; - int[] ftab; + private byte[] block; + private int[] quadrant; + private int[] zptr; + private short[] szptr; + private int[] ftab; - int nMTF; + private int nMTF; - int[] mtfFreq = new int[BZip2Constants.MaximumAlphaSize]; + private int[] mtfFreq = new int[BZip2Constants.MaximumAlphaSize]; /* * Used when sorting. If too many long comparisons * happen, we stop sorting, randomise the block * slightly, and try again. */ - int workFactor; - int workDone; - int workLimit; - bool firstAttempt; - int nBlocksRandomised; - - int currentChar = -1; - int runLength; - uint blockCRC, combinedCRC; - int allowableBlockSize; - readonly Stream baseStream; - bool disposed_; - #endregion + private int workFactor; + private int workDone; + private int workLimit; + private bool firstAttempt; + private int nBlocksRandomised; + + private int currentChar = -1; + private int runLength; + private uint blockCRC, combinedCRC; + private int allowableBlockSize; + private readonly Stream baseStream; + private bool disposed_; + + #endregion Instance Fields /// /// Construct a default output stream with maximum block size @@ -113,13 +118,13 @@ public BZip2OutputStream(Stream stream) : this(stream, 9) } /// - /// Initialise a new instance of the + /// Initialise a new instance of the /// for the specified stream, using the given blocksize. /// /// The stream to write compressed data to. /// The block size to use. /// - /// Valid block sizes are in the range 1..9, with 1 giving + /// Valid block sizes are in the range 1..9, with 1 giving /// the lowest compression and 9 the highest. /// public BZip2OutputStream(Stream stream, int blockSize) @@ -133,11 +138,13 @@ public BZip2OutputStream(Stream stream, int blockSize) bytesOut = 0; workFactor = 50; - if (blockSize > 9) { + if (blockSize > 9) + { blockSize = 9; } - if (blockSize < 1) { + if (blockSize < 1) + { blockSize = 1; } blockSize100k = blockSize; @@ -147,7 +154,7 @@ public BZip2OutputStream(Stream stream, int blockSize) } /// - /// Ensures that resources are freed and other cleanup operations + /// Ensures that resources are freed and other cleanup operations /// are performed when the garbage collector reclaims the BZip2OutputStream. /// ~BZip2OutputStream() @@ -257,7 +264,7 @@ public override int ReadByte() /// The offset in the buffer to start storing data at. /// The maximum number of bytes to read. /// The total number of bytes read. This might be less than the number of bytes - /// requested if that number of bytes are not currently available, or zero + /// requested if that number of bytes are not currently available, or zero /// if the end of the stream is reached. public override int Read(byte[] buffer, int offset, int count) { @@ -272,23 +279,28 @@ public override int Read(byte[] buffer, int offset, int count) /// The number of bytes to write. public override void Write(byte[] buffer, int offset, int count) { - if (buffer == null) { + if (buffer == null) + { throw new ArgumentNullException(nameof(buffer)); } - if (offset < 0) { + if (offset < 0) + { throw new ArgumentOutOfRangeException(nameof(offset)); } - if (count < 0) { + if (count < 0) + { throw new ArgumentOutOfRangeException(nameof(count)); } - if (buffer.Length - offset < count) { + if (buffer.Length - offset < count) + { throw new ArgumentException("Offset/count out of range"); } - for (int i = 0; i < count; ++i) { + for (int i = 0; i < count; ++i) + { WriteByte(buffer[offset + i]); } } @@ -300,30 +312,39 @@ public override void Write(byte[] buffer, int offset, int count) public override void WriteByte(byte value) { int b = (256 + value) % 256; - if (currentChar != -1) { - if (currentChar == b) { + if (currentChar != -1) + { + if (currentChar == b) + { runLength++; - if (runLength > 254) { + if (runLength > 254) + { WriteRun(); currentChar = -1; runLength = 0; } - } else { + } + else + { WriteRun(); runLength = 1; currentChar = b; } - } else { + } + else + { currentChar = b; runLength++; } } - void MakeMaps() + private void MakeMaps() { nInUse = 0; - for (int i = 0; i < 256; i++) { - if (inUse[i]) { + for (int i = 0; i < 256; i++) + { + if (inUse[i]) + { seqToUnseq[nInUse] = (char)i; unseqToSeq[i] = (char)nInUse; nInUse++; @@ -334,48 +355,56 @@ void MakeMaps() /// /// Get the number of bytes written to output. /// - void WriteRun() + private void WriteRun() { - if (last < allowableBlockSize) { + if (last < allowableBlockSize) + { inUse[currentChar] = true; - for (int i = 0; i < runLength; i++) { + for (int i = 0; i < runLength; i++) + { mCrc.Update(currentChar); } - switch (runLength) { - case 1: - last++; - block[last + 1] = (byte)currentChar; - break; - case 2: - last++; - block[last + 1] = (byte)currentChar; - last++; - block[last + 1] = (byte)currentChar; - break; - case 3: - last++; - block[last + 1] = (byte)currentChar; - last++; - block[last + 1] = (byte)currentChar; - last++; - block[last + 1] = (byte)currentChar; - break; - default: - inUse[runLength - 4] = true; - last++; - block[last + 1] = (byte)currentChar; - last++; - block[last + 1] = (byte)currentChar; - last++; - block[last + 1] = (byte)currentChar; - last++; - block[last + 1] = (byte)currentChar; - last++; - block[last + 1] = (byte)(runLength - 4); - break; + switch (runLength) + { + case 1: + last++; + block[last + 1] = (byte)currentChar; + break; + + case 2: + last++; + block[last + 1] = (byte)currentChar; + last++; + block[last + 1] = (byte)currentChar; + break; + + case 3: + last++; + block[last + 1] = (byte)currentChar; + last++; + block[last + 1] = (byte)currentChar; + last++; + block[last + 1] = (byte)currentChar; + break; + + default: + inUse[runLength - 4] = true; + last++; + block[last + 1] = (byte)currentChar; + last++; + block[last + 1] = (byte)currentChar; + last++; + block[last + 1] = (byte)currentChar; + last++; + block[last + 1] = (byte)currentChar; + last++; + block[last + 1] = (byte)(runLength - 4); + break; } - } else { + } + else + { EndBlock(); InitBlock(); WriteRun(); @@ -396,13 +425,17 @@ public int BytesWritten /// true to release both managed and unmanaged resources; false to release only unmanaged resources. override protected void Dispose(bool disposing) { - try { - try { + try + { + try + { base.Dispose(disposing); - if (!disposed_) { + if (!disposed_) + { disposed_ = true; - if (runLength > 0) { + if (runLength > 0) + { WriteRun(); } @@ -411,26 +444,32 @@ override protected void Dispose(bool disposing) EndCompression(); Flush(); } - } finally { - if (disposing) { - if (IsStreamOwner) { + } + finally + { + if (disposing) + { + if (IsStreamOwner) + { baseStream.Dispose(); } } } - } catch { + } + catch + { } } /// /// Flush output buffers - /// + /// public override void Flush() { baseStream.Flush(); } - void Initialize() + private void Initialize() { bytesOut = 0; nBlocksRandomised = 0; @@ -448,12 +487,13 @@ followed by a digit indicating blockSize100k. combinedCRC = 0; } - void InitBlock() + private void InitBlock() { mCrc.Reset(); last = -1; - for (int i = 0; i < 256; i++) { + for (int i = 0; i < 256; i++) + { inUse[i] = false; } @@ -461,9 +501,10 @@ void InitBlock() allowableBlockSize = BZip2Constants.BaseBlockSize * blockSize100k - 20; } - void EndBlock() + private void EndBlock() { - if (last < 0) { // dont do anything for empty files, (makes empty files compatible with original Bzip) + if (last < 0) + { // dont do anything for empty files, (makes empty files compatible with original Bzip) return; } @@ -495,15 +536,19 @@ damaged files. BsPutUChar(0x59); /*-- Now the block's CRC, so it is in a known place. --*/ - unchecked { + unchecked + { BsPutint((int)blockCRC); } /*-- Now a single bit indicating randomisation. --*/ - if (blockRandomised) { + if (blockRandomised) + { BsW(1, 1); nBlocksRandomised++; - } else { + } + else + { BsW(1, 0); } @@ -511,7 +556,7 @@ damaged files. MoveToFrontCodeAndSend(); } - void EndCompression() + private void EndCompression() { /*-- Now another magic 48-bit number, 0x177245385090, to @@ -527,16 +572,18 @@ void EndCompression() BsPutUChar(0x50); BsPutUChar(0x90); - unchecked { + unchecked + { BsPutint((int)combinedCRC); } BsFinishedWithStream(); } - void BsFinishedWithStream() + private void BsFinishedWithStream() { - while (bsLive > 0) { + while (bsLive > 0) + { int ch = (bsBuff >> 24); baseStream.WriteByte((byte)ch); // write 8-bit bsBuff <<= 8; @@ -545,9 +592,10 @@ void BsFinishedWithStream() } } - void BsW(int n, int v) + private void BsW(int n, int v) { - while (bsLive >= 8) { + while (bsLive >= 8) + { int ch = (bsBuff >> 24); unchecked { baseStream.WriteByte((byte)ch); } // write 8-bit bsBuff <<= 8; @@ -558,12 +606,12 @@ void BsW(int n, int v) bsLive += n; } - void BsPutUChar(int c) + private void BsPutUChar(int c) { BsW(8, c); } - void BsPutint(int u) + private void BsPutint(int u) { BsW(8, (u >> 24) & 0xFF); BsW(8, (u >> 16) & 0xFF); @@ -571,15 +619,16 @@ void BsPutint(int u) BsW(8, u & 0xFF); } - void BsPutIntVS(int numBits, int c) + private void BsPutIntVS(int numBits, int c) { BsW(numBits, c); } - void SendMTFValues() + private void SendMTFValues() { char[][] len = new char[BZip2Constants.GroupCount][]; - for (int i = 0; i < BZip2Constants.GroupCount; ++i) { + for (int i = 0; i < BZip2Constants.GroupCount; ++i) + { len[i] = new char[BZip2Constants.MaximumAlphaSize]; } @@ -588,26 +637,38 @@ void SendMTFValues() int nGroups; alphaSize = nInUse + 2; - for (int t = 0; t < BZip2Constants.GroupCount; t++) { - for (int v = 0; v < alphaSize; v++) { + for (int t = 0; t < BZip2Constants.GroupCount; t++) + { + for (int v = 0; v < alphaSize; v++) + { len[t][v] = (char)GREATER_ICOST; } } /*--- Decide how many coding tables to use ---*/ - if (nMTF <= 0) { + if (nMTF <= 0) + { Panic(); } - if (nMTF < 200) { + if (nMTF < 200) + { nGroups = 2; - } else if (nMTF < 600) { + } + else if (nMTF < 600) + { nGroups = 3; - } else if (nMTF < 1200) { + } + else if (nMTF < 1200) + { nGroups = 4; - } else if (nMTF < 2400) { + } + else if (nMTF < 2400) + { nGroups = 5; - } else { + } + else + { nGroups = 6; } @@ -615,24 +676,31 @@ void SendMTFValues() int nPart = nGroups; int remF = nMTF; gs = 0; - while (nPart > 0) { + while (nPart > 0) + { int tFreq = remF / nPart; int aFreq = 0; ge = gs - 1; - while (aFreq < tFreq && ge < alphaSize - 1) { + while (aFreq < tFreq && ge < alphaSize - 1) + { ge++; aFreq += mtfFreq[ge]; } - if (ge > gs && nPart != nGroups && nPart != 1 && ((nGroups - nPart) % 2 == 1)) { + if (ge > gs && nPart != nGroups && nPart != 1 && ((nGroups - nPart) % 2 == 1)) + { aFreq -= mtfFreq[ge]; ge--; } - for (int v = 0; v < alphaSize; v++) { - if (v >= gs && v <= ge) { + for (int v = 0; v < alphaSize; v++) + { + if (v >= gs && v <= ge) + { len[nPart - 1][v] = (char)LESSER_ICOST; - } else { + } + else + { len[nPart - 1][v] = (char)GREATER_ICOST; } } @@ -643,7 +711,8 @@ void SendMTFValues() } int[][] rfreq = new int[BZip2Constants.GroupCount][]; - for (int i = 0; i < BZip2Constants.GroupCount; ++i) { + for (int i = 0; i < BZip2Constants.GroupCount; ++i) + { rfreq[i] = new int[BZip2Constants.MaximumAlphaSize]; } @@ -652,13 +721,17 @@ void SendMTFValues() /*--- Iterate up to N_ITERS times to improve the tables. ---*/ - for (iter = 0; iter < BZip2Constants.NumberOfIterations; ++iter) { - for (int t = 0; t < nGroups; ++t) { + for (iter = 0; iter < BZip2Constants.NumberOfIterations; ++iter) + { + for (int t = 0; t < nGroups; ++t) + { fave[t] = 0; } - for (int t = 0; t < nGroups; ++t) { - for (int v = 0; v < alphaSize; ++v) { + for (int t = 0; t < nGroups; ++t) + { + for (int v = 0; v < alphaSize; ++v) + { rfreq[t][v] = 0; } } @@ -666,13 +739,16 @@ Iterate up to N_ITERS times to improve the tables. nSelectors = 0; totc = 0; gs = 0; - while (true) { + while (true) + { /*--- Set group start & end marks. --*/ - if (gs >= nMTF) { + if (gs >= nMTF) + { break; } ge = gs + BZip2Constants.GroupSize - 1; - if (ge >= nMTF) { + if (ge >= nMTF) + { ge = nMTF - 1; } @@ -680,14 +756,17 @@ Iterate up to N_ITERS times to improve the tables. Calculate the cost of this group as coded by each of the coding tables. --*/ - for (int t = 0; t < nGroups; t++) { + for (int t = 0; t < nGroups; t++) + { cost[t] = 0; } - if (nGroups == 6) { + if (nGroups == 6) + { short cost0, cost1, cost2, cost3, cost4, cost5; cost0 = cost1 = cost2 = cost3 = cost4 = cost5 = 0; - for (int i = gs; i <= ge; ++i) { + for (int i = gs; i <= ge; ++i) + { short icv = szptr[i]; cost0 += (short)len[0][icv]; cost1 += (short)len[1][icv]; @@ -702,10 +781,14 @@ by each of the coding tables. cost[3] = cost3; cost[4] = cost4; cost[5] = cost5; - } else { - for (int i = gs; i <= ge; ++i) { + } + else + { + for (int i = gs; i <= ge; ++i) + { short icv = szptr[i]; - for (int t = 0; t < nGroups; t++) { + for (int t = 0; t < nGroups; t++) + { cost[t] += (short)len[t][icv]; } } @@ -717,8 +800,10 @@ and record its identity in the selector table. --*/ bc = 999999999; bt = -1; - for (int t = 0; t < nGroups; ++t) { - if (cost[t] < bc) { + for (int t = 0; t < nGroups; ++t) + { + if (cost[t] < bc) + { bc = cost[t]; bt = t; } @@ -731,7 +816,8 @@ and record its identity in the selector table. /*-- Increment the symbol frequencies for the selected table. --*/ - for (int i = gs; i <= ge; ++i) { + for (int i = gs; i <= ge; ++i) + { ++rfreq[bt][szptr[i]]; } @@ -741,7 +827,8 @@ and record its identity in the selector table. /*-- Recompute the tables based on the accumulated frequencies. --*/ - for (int t = 0; t < nGroups; ++t) { + for (int t = 0; t < nGroups; ++t) + { HbMakeCodeLengths(len[t], rfreq[t], alphaSize, 20); } } @@ -750,11 +837,13 @@ Recompute the tables based on the accumulated frequencies. fave = null; cost = null; - if (!(nGroups < 8)) { + if (!(nGroups < 8)) + { Panic(); } - if (!(nSelectors < 32768 && nSelectors <= (2 + (900000 / BZip2Constants.GroupSize)))) { + if (!(nSelectors < 32768 && nSelectors <= (2 + (900000 / BZip2Constants.GroupSize)))) + { Panic(); } @@ -762,15 +851,18 @@ Recompute the tables based on the accumulated frequencies. char[] pos = new char[BZip2Constants.GroupCount]; char ll_i, tmp2, tmp; - for (int i = 0; i < nGroups; i++) { + for (int i = 0; i < nGroups; i++) + { pos[i] = (char)i; } - for (int i = 0; i < nSelectors; i++) { + for (int i = 0; i < nSelectors; i++) + { ll_i = selector[i]; int j = 0; tmp = pos[j]; - while (ll_i != tmp) { + while (ll_i != tmp) + { j++; tmp2 = tmp; tmp = pos[j]; @@ -782,26 +874,33 @@ Recompute the tables based on the accumulated frequencies. int[][] code = new int[BZip2Constants.GroupCount][]; - for (int i = 0; i < BZip2Constants.GroupCount; ++i) { + for (int i = 0; i < BZip2Constants.GroupCount; ++i) + { code[i] = new int[BZip2Constants.MaximumAlphaSize]; } /*--- Assign actual codes for the tables. --*/ - for (int t = 0; t < nGroups; t++) { + for (int t = 0; t < nGroups; t++) + { minLen = 32; maxLen = 0; - for (int i = 0; i < alphaSize; i++) { - if (len[t][i] > maxLen) { + for (int i = 0; i < alphaSize; i++) + { + if (len[t][i] > maxLen) + { maxLen = len[t][i]; } - if (len[t][i] < minLen) { + if (len[t][i] < minLen) + { minLen = len[t][i]; } } - if (maxLen > 20) { + if (maxLen > 20) + { Panic(); } - if (minLen < 1) { + if (minLen < 1) + { Panic(); } HbAssignCodes(code[t], len[t], minLen, maxLen, alphaSize); @@ -809,29 +908,42 @@ Recompute the tables based on the accumulated frequencies. /*--- Transmit the mapping table. ---*/ bool[] inUse16 = new bool[16]; - for (int i = 0; i < 16; ++i) { + for (int i = 0; i < 16; ++i) + { inUse16[i] = false; - for (int j = 0; j < 16; ++j) { - if (inUse[i * 16 + j]) { + for (int j = 0; j < 16; ++j) + { + if (inUse[i * 16 + j]) + { inUse16[i] = true; } } } - for (int i = 0; i < 16; ++i) { - if (inUse16[i]) { + for (int i = 0; i < 16; ++i) + { + if (inUse16[i]) + { BsW(1, 1); - } else { + } + else + { BsW(1, 0); } } - for (int i = 0; i < 16; ++i) { - if (inUse16[i]) { - for (int j = 0; j < 16; ++j) { - if (inUse[i * 16 + j]) { + for (int i = 0; i < 16; ++i) + { + if (inUse16[i]) + { + for (int j = 0; j < 16; ++j) + { + if (inUse[i * 16 + j]) + { BsW(1, 1); - } else { + } + else + { BsW(1, 0); } } @@ -841,23 +953,29 @@ Recompute the tables based on the accumulated frequencies. /*--- Now the selectors. ---*/ BsW(3, nGroups); BsW(15, nSelectors); - for (int i = 0; i < nSelectors; ++i) { - for (int j = 0; j < selectorMtf[i]; ++j) { + for (int i = 0; i < nSelectors; ++i) + { + for (int j = 0; j < selectorMtf[i]; ++j) + { BsW(1, 1); } BsW(1, 0); } /*--- Now the coding tables. ---*/ - for (int t = 0; t < nGroups; ++t) { + for (int t = 0; t < nGroups; ++t) + { int curr = len[t][0]; BsW(5, curr); - for (int i = 0; i < alphaSize; ++i) { - while (curr < len[t][i]) { + for (int i = 0; i < alphaSize; ++i) + { + while (curr < len[t][i]) + { BsW(2, 2); curr++; /* 10 */ } - while (curr > len[t][i]) { + while (curr > len[t][i]) + { BsW(2, 3); curr--; /* 11 */ } @@ -868,61 +986,71 @@ Recompute the tables based on the accumulated frequencies. /*--- And finally, the block data proper ---*/ selCtr = 0; gs = 0; - while (true) { - if (gs >= nMTF) { + while (true) + { + if (gs >= nMTF) + { break; } ge = gs + BZip2Constants.GroupSize - 1; - if (ge >= nMTF) { + if (ge >= nMTF) + { ge = nMTF - 1; } - for (int i = gs; i <= ge; i++) { + for (int i = gs; i <= ge; i++) + { BsW(len[selector[selCtr]][szptr[i]], code[selector[selCtr]][szptr[i]]); } gs = ge + 1; ++selCtr; } - if (!(selCtr == nSelectors)) { + if (!(selCtr == nSelectors)) + { Panic(); } } - void MoveToFrontCodeAndSend() + private void MoveToFrontCodeAndSend() { BsPutIntVS(24, origPtr); GenerateMTFValues(); SendMTFValues(); } - void SimpleSort(int lo, int hi, int d) + private void SimpleSort(int lo, int hi, int d) { int i, j, h, bigN, hp; int v; bigN = hi - lo + 1; - if (bigN < 2) { + if (bigN < 2) + { return; } hp = 0; - while (increments[hp] < bigN) { + while (increments[hp] < bigN) + { hp++; } hp--; - for (; hp >= 0; hp--) { + for (; hp >= 0; hp--) + { h = increments[hp]; i = lo + h; - while (true) { + while (true) + { /*-- copy 1 --*/ if (i > hi) break; v = zptr[i]; j = i; - while (FullGtU(zptr[j - h] + d, v + d)) { + while (FullGtU(zptr[j - h] + d, v + d)) + { zptr[j] = zptr[j - h]; j = j - h; if (j <= (lo + h - 1)) @@ -932,15 +1060,18 @@ void SimpleSort(int lo, int hi, int d) i++; /*-- copy 2 --*/ - if (i > hi) { + if (i > hi) + { break; } v = zptr[i]; j = i; - while (FullGtU(zptr[j - h] + d, v + d)) { + while (FullGtU(zptr[j - h] + d, v + d)) + { zptr[j] = zptr[j - h]; j = j - h; - if (j <= (lo + h - 1)) { + if (j <= (lo + h - 1)) + { break; } } @@ -948,32 +1079,37 @@ void SimpleSort(int lo, int hi, int d) i++; /*-- copy 3 --*/ - if (i > hi) { + if (i > hi) + { break; } v = zptr[i]; j = i; - while (FullGtU(zptr[j - h] + d, v + d)) { + while (FullGtU(zptr[j - h] + d, v + d)) + { zptr[j] = zptr[j - h]; j = j - h; - if (j <= (lo + h - 1)) { + if (j <= (lo + h - 1)) + { break; } } zptr[j] = v; i++; - if (workDone > workLimit && firstAttempt) { + if (workDone > workLimit && firstAttempt) + { return; } } } } - void Vswap(int p1, int p2, int n) + private void Vswap(int p1, int p2, int n) { int temp = 0; - while (n > 0) { + while (n > 0) + { temp = zptr[p1]; zptr[p1] = zptr[p2]; zptr[p2] = temp; @@ -983,7 +1119,7 @@ void Vswap(int p1, int p2, int n) } } - void QSort3(int loSt, int hiSt, int dSt) + private void QSort3(int loSt, int hiSt, int dSt) { int unLo, unHi, ltLo, gtHi, med, n, m; int lo, hi, d; @@ -997,8 +1133,10 @@ void QSort3(int loSt, int hiSt, int dSt) stack[sp].dd = dSt; sp++; - while (sp > 0) { - if (sp >= QSORT_STACK_SIZE) { + while (sp > 0) + { + if (sp >= QSORT_STACK_SIZE) + { Panic(); } @@ -1007,9 +1145,11 @@ void QSort3(int loSt, int hiSt, int dSt) hi = stack[sp].hh; d = stack[sp].dd; - if (hi - lo < SMALL_THRESH || d > DEPTH_THRESH) { + if (hi - lo < SMALL_THRESH || d > DEPTH_THRESH) + { SimpleSort(lo, hi, d); - if (workDone > workLimit && firstAttempt) { + if (workDone > workLimit && firstAttempt) + { return; } continue; @@ -1022,13 +1162,17 @@ void QSort3(int loSt, int hiSt, int dSt) unLo = ltLo = lo; unHi = gtHi = hi; - while (true) { - while (true) { - if (unLo > unHi) { + while (true) + { + while (true) + { + if (unLo > unHi) + { break; } n = ((int)block[zptr[unLo] + d + 1]) - med; - if (n == 0) { + if (n == 0) + { int temp = zptr[unLo]; zptr[unLo] = zptr[ltLo]; zptr[ltLo] = temp; @@ -1036,18 +1180,22 @@ void QSort3(int loSt, int hiSt, int dSt) unLo++; continue; } - if (n > 0) { + if (n > 0) + { break; } unLo++; } - while (true) { - if (unLo > unHi) { + while (true) + { + if (unLo > unHi) + { break; } n = ((int)block[zptr[unHi] + d + 1]) - med; - if (n == 0) { + if (n == 0) + { int temp = zptr[unHi]; zptr[unHi] = zptr[gtHi]; zptr[gtHi] = temp; @@ -1055,13 +1203,15 @@ void QSort3(int loSt, int hiSt, int dSt) unHi--; continue; } - if (n < 0) { + if (n < 0) + { break; } unHi--; } - if (unLo > unHi) { + if (unLo > unHi) + { break; } @@ -1074,7 +1224,8 @@ void QSort3(int loSt, int hiSt, int dSt) } } - if (gtHi < ltLo) { + if (gtHi < ltLo) + { stack[sp].ll = lo; stack[sp].hh = hi; stack[sp].dd = d + 1; @@ -1107,7 +1258,7 @@ void QSort3(int loSt, int hiSt, int dSt) } } - void MainSort() + private void MainSort() { int i, j, ss, sb; int[] runningOrder = new int[256]; @@ -1123,48 +1274,59 @@ void MainSort() --*/ // if (verbosity >= 4) fprintf ( stderr, " sort initialise ...\n" ); - for (i = 0; i < BZip2Constants.OvershootBytes; i++) { + for (i = 0; i < BZip2Constants.OvershootBytes; i++) + { block[last + i + 2] = block[(i % (last + 1)) + 1]; } - for (i = 0; i <= last + BZip2Constants.OvershootBytes; i++) { + for (i = 0; i <= last + BZip2Constants.OvershootBytes; i++) + { quadrant[i] = 0; } block[0] = (byte)(block[last + 1]); - if (last < 4000) { + if (last < 4000) + { /*-- Use simpleSort(), since the full sorting mechanism has quite a large constant overhead. --*/ - for (i = 0; i <= last; i++) { + for (i = 0; i <= last; i++) + { zptr[i] = i; } firstAttempt = false; workDone = workLimit = 0; SimpleSort(0, last, 0); - } else { + } + else + { numQSorted = 0; - for (i = 0; i <= 255; i++) { + for (i = 0; i <= 255; i++) + { bigDone[i] = false; } - for (i = 0; i <= 65536; i++) { + for (i = 0; i <= 65536; i++) + { ftab[i] = 0; } c1 = block[0]; - for (i = 0; i <= last; i++) { + for (i = 0; i <= last; i++) + { c2 = block[i + 1]; ftab[(c1 << 8) + c2]++; c1 = c2; } - for (i = 1; i <= 65536; i++) { + for (i = 1; i <= 65536; i++) + { ftab[i] += ftab[i - 1]; } c1 = block[1]; - for (i = 0; i < last; i++) { + for (i = 0; i < last; i++) + { c2 = block[i + 2]; j = (c1 << 8) + c2; c1 = c2; @@ -1182,24 +1344,30 @@ Now ftab contains the first loc of every small bucket. big bucket. --*/ - for (i = 0; i <= 255; i++) { + for (i = 0; i <= 255; i++) + { runningOrder[i] = i; } int vv; int h = 1; - do { + do + { h = 3 * h + 1; } while (h <= 256); - do { + do + { h = h / 3; - for (i = h; i <= 255; i++) { + for (i = h; i <= 255; i++) + { vv = runningOrder[i]; j = i; - while ((ftab[((runningOrder[j - h]) + 1) << 8] - ftab[(runningOrder[j - h]) << 8]) > (ftab[((vv) + 1) << 8] - ftab[(vv) << 8])) { + while ((ftab[((runningOrder[j - h]) + 1) << 8] - ftab[(runningOrder[j - h]) << 8]) > (ftab[((vv) + 1) << 8] - ftab[(vv) << 8])) + { runningOrder[j] = runningOrder[j - h]; j = j - h; - if (j <= (h - 1)) { + if (j <= (h - 1)) + { break; } } @@ -1210,8 +1378,8 @@ big bucket. /*-- The main sorting loop. --*/ - for (i = 0; i <= 255; i++) { - + for (i = 0; i <= 255; i++) + { /*-- Process big buckets, starting with the least full. --*/ @@ -1224,15 +1392,19 @@ previous pointer-scanning phases have already completed many of the small buckets [ss, j], so we don't have to sort them at all. --*/ - for (j = 0; j <= 255; j++) { + for (j = 0; j <= 255; j++) + { sb = (ss << 8) + j; - if (!((ftab[sb] & SETMASK) == SETMASK)) { + if (!((ftab[sb] & SETMASK) == SETMASK)) + { int lo = ftab[sb] & CLEARMASK; int hi = (ftab[sb + 1] & CLEARMASK) - 1; - if (hi > lo) { + if (hi > lo) + { QSort3(lo, hi, 2); numQSorted += (hi - lo + 1); - if (workDone > workLimit && firstAttempt) { + if (workDone > workLimit && firstAttempt) + { return; } } @@ -1250,25 +1422,30 @@ and update the quadrant descriptors. Remember to --*/ bigDone[ss] = true; - if (i < 255) { + if (i < 255) + { int bbStart = ftab[ss << 8] & CLEARMASK; int bbSize = (ftab[(ss + 1) << 8] & CLEARMASK) - bbStart; int shifts = 0; - while ((bbSize >> shifts) > 65534) { + while ((bbSize >> shifts) > 65534) + { shifts++; } - for (j = 0; j < bbSize; j++) { + for (j = 0; j < bbSize; j++) + { int a2update = zptr[bbStart + j]; int qVal = (j >> shifts); quadrant[a2update] = qVal; - if (a2update < BZip2Constants.OvershootBytes) { + if (a2update < BZip2Constants.OvershootBytes) + { quadrant[a2update + last + 1] = qVal; } } - if (!(((bbSize - 1) >> shifts) <= 65535)) { + if (!(((bbSize - 1) >> shifts) <= 65535)) + { Panic(); } } @@ -1277,39 +1454,47 @@ and update the quadrant descriptors. Remember to Now scan this big bucket so as to synthesise the sorted order for small buckets [t, ss] for all t != ss. --*/ - for (j = 0; j <= 255; j++) { + for (j = 0; j <= 255; j++) + { copy[j] = ftab[(j << 8) + ss] & CLEARMASK; } - for (j = ftab[ss << 8] & CLEARMASK; j < (ftab[(ss + 1) << 8] & CLEARMASK); j++) { + for (j = ftab[ss << 8] & CLEARMASK; j < (ftab[(ss + 1) << 8] & CLEARMASK); j++) + { c1 = block[zptr[j]]; - if (!bigDone[c1]) { + if (!bigDone[c1]) + { zptr[copy[c1]] = zptr[j] == 0 ? last : zptr[j] - 1; copy[c1]++; } } - for (j = 0; j <= 255; j++) { + for (j = 0; j <= 255; j++) + { ftab[(j << 8) + ss] |= SETMASK; } } } } - void RandomiseBlock() + private void RandomiseBlock() { int i; int rNToGo = 0; int rTPos = 0; - for (i = 0; i < 256; i++) { + for (i = 0; i < 256; i++) + { inUse[i] = false; } - for (i = 0; i <= last; i++) { - if (rNToGo == 0) { + for (i = 0; i <= last; i++) + { + if (rNToGo == 0) + { rNToGo = (int)BZip2Constants.RandomNumbers[rTPos]; rTPos++; - if (rTPos == 512) { + if (rTPos == 512) + { rTPos = 0; } } @@ -1322,7 +1507,7 @@ void RandomiseBlock() } } - void DoReversibleTransformation() + private void DoReversibleTransformation() { workLimit = workFactor * last; workDone = 0; @@ -1331,7 +1516,8 @@ void DoReversibleTransformation() MainSort(); - if (workDone > workLimit && firstAttempt) { + if (workDone > workLimit && firstAttempt) + { RandomiseBlock(); workLimit = workDone = 0; blockRandomised = true; @@ -1340,19 +1526,22 @@ void DoReversibleTransformation() } origPtr = -1; - for (int i = 0; i <= last; i++) { - if (zptr[i] == 0) { + for (int i = 0; i <= last; i++) + { + if (zptr[i] == 0) + { origPtr = i; break; } } - if (origPtr == -1) { + if (origPtr == -1) + { Panic(); } } - bool FullGtU(int i1, int i2) + private bool FullGtU(int i1, int i2) { int k; byte c1, c2; @@ -1360,7 +1549,8 @@ bool FullGtU(int i1, int i2) c1 = block[i1 + 1]; c2 = block[i2 + 1]; - if (c1 != c2) { + if (c1 != c2) + { return c1 > c2; } i1++; @@ -1368,7 +1558,8 @@ bool FullGtU(int i1, int i2) c1 = block[i1 + 1]; c2 = block[i2 + 1]; - if (c1 != c2) { + if (c1 != c2) + { return c1 > c2; } i1++; @@ -1376,7 +1567,8 @@ bool FullGtU(int i1, int i2) c1 = block[i1 + 1]; c2 = block[i2 + 1]; - if (c1 != c2) { + if (c1 != c2) + { return c1 > c2; } i1++; @@ -1384,7 +1576,8 @@ bool FullGtU(int i1, int i2) c1 = block[i1 + 1]; c2 = block[i2 + 1]; - if (c1 != c2) { + if (c1 != c2) + { return c1 > c2; } i1++; @@ -1392,7 +1585,8 @@ bool FullGtU(int i1, int i2) c1 = block[i1 + 1]; c2 = block[i2 + 1]; - if (c1 != c2) { + if (c1 != c2) + { return c1 > c2; } i1++; @@ -1400,7 +1594,8 @@ bool FullGtU(int i1, int i2) c1 = block[i1 + 1]; c2 = block[i2 + 1]; - if (c1 != c2) { + if (c1 != c2) + { return c1 > c2; } i1++; @@ -1408,15 +1603,18 @@ bool FullGtU(int i1, int i2) k = last + 1; - do { + do + { c1 = block[i1 + 1]; c2 = block[i2 + 1]; - if (c1 != c2) { + if (c1 != c2) + { return c1 > c2; } s1 = quadrant[i1]; s2 = quadrant[i2]; - if (s1 != s2) { + if (s1 != s2) + { return s1 > s2; } i1++; @@ -1424,12 +1622,14 @@ bool FullGtU(int i1, int i2) c1 = block[i1 + 1]; c2 = block[i2 + 1]; - if (c1 != c2) { + if (c1 != c2) + { return c1 > c2; } s1 = quadrant[i1]; s2 = quadrant[i2]; - if (s1 != s2) { + if (s1 != s2) + { return s1 > s2; } i1++; @@ -1437,12 +1637,14 @@ bool FullGtU(int i1, int i2) c1 = block[i1 + 1]; c2 = block[i2 + 1]; - if (c1 != c2) { + if (c1 != c2) + { return c1 > c2; } s1 = quadrant[i1]; s2 = quadrant[i2]; - if (s1 != s2) { + if (s1 != s2) + { return s1 > s2; } i1++; @@ -1450,22 +1652,26 @@ bool FullGtU(int i1, int i2) c1 = block[i1 + 1]; c2 = block[i2 + 1]; - if (c1 != c2) { + if (c1 != c2) + { return c1 > c2; } s1 = quadrant[i1]; s2 = quadrant[i2]; - if (s1 != s2) { + if (s1 != s2) + { return s1 > s2; } i1++; i2++; - if (i1 > last) { + if (i1 > last) + { i1 -= last; i1--; } - if (i2 > last) { + if (i2 > last) + { i2 -= last; i2--; } @@ -1477,7 +1683,7 @@ bool FullGtU(int i1, int i2) return false; } - void AllocateCompressStructures() + private void AllocateCompressStructures() { int n = BZip2Constants.BaseBlockSize * blockSize100k; block = new byte[(n + 1 + BZip2Constants.OvershootBytes)]; @@ -1485,7 +1691,8 @@ void AllocateCompressStructures() zptr = new int[n]; ftab = new int[65537]; - if (block == null || quadrant == null || zptr == null || ftab == null) { + if (block == null || quadrant == null || zptr == null || ftab == null) + { // int totalDraw = (n + 1 + NUM_OVERSHOOT_BYTES) + (n + NUM_OVERSHOOT_BYTES) + n + 65537; // compressOutOfMemory ( totalDraw, n ); } @@ -1502,11 +1709,10 @@ Seems to improve compression speed by about 1%. */ // szptr = zptr; - szptr = new short[2 * n]; } - void GenerateMTFValues() + private void GenerateMTFValues() { char[] yy = new char[256]; int i, j; @@ -1519,25 +1725,28 @@ void GenerateMTFValues() MakeMaps(); EOB = nInUse + 1; - for (i = 0; i <= EOB; i++) { + for (i = 0; i <= EOB; i++) + { mtfFreq[i] = 0; } wr = 0; zPend = 0; - for (i = 0; i < nInUse; i++) { + for (i = 0; i < nInUse; i++) + { yy[i] = (char)i; } - - for (i = 0; i <= last; i++) { + for (i = 0; i <= last; i++) + { char ll_i; ll_i = unseqToSeq[block[zptr[i]]]; j = 0; tmp = yy[j]; - while (ll_i != tmp) { + while (ll_i != tmp) + { j++; tmp2 = tmp; tmp = yy[j]; @@ -1545,25 +1754,33 @@ void GenerateMTFValues() } yy[0] = tmp; - if (j == 0) { + if (j == 0) + { zPend++; - } else { - if (zPend > 0) { + } + else + { + if (zPend > 0) + { zPend--; - while (true) { - switch (zPend % 2) { - case 0: - szptr[wr] = (short)BZip2Constants.RunA; - wr++; - mtfFreq[BZip2Constants.RunA]++; - break; - case 1: - szptr[wr] = (short)BZip2Constants.RunB; - wr++; - mtfFreq[BZip2Constants.RunB]++; - break; + while (true) + { + switch (zPend % 2) + { + case 0: + szptr[wr] = (short)BZip2Constants.RunA; + wr++; + mtfFreq[BZip2Constants.RunA]++; + break; + + case 1: + szptr[wr] = (short)BZip2Constants.RunB; + wr++; + mtfFreq[BZip2Constants.RunB]++; + break; } - if (zPend < 2) { + if (zPend < 2) + { break; } zPend = (zPend - 2) / 2; @@ -1576,22 +1793,27 @@ void GenerateMTFValues() } } - if (zPend > 0) { + if (zPend > 0) + { zPend--; - while (true) { - switch (zPend % 2) { - case 0: - szptr[wr] = (short)BZip2Constants.RunA; - wr++; - mtfFreq[BZip2Constants.RunA]++; - break; - case 1: - szptr[wr] = (short)BZip2Constants.RunB; - wr++; - mtfFreq[BZip2Constants.RunB]++; - break; + while (true) + { + switch (zPend % 2) + { + case 0: + szptr[wr] = (short)BZip2Constants.RunA; + wr++; + mtfFreq[BZip2Constants.RunA]++; + break; + + case 1: + szptr[wr] = (short)BZip2Constants.RunB; + wr++; + mtfFreq[BZip2Constants.RunB]++; + break; } - if (zPend < 2) { + if (zPend < 2) + { break; } zPend = (zPend - 2) / 2; @@ -1605,12 +1827,12 @@ void GenerateMTFValues() nMTF = wr; } - static void Panic() + private static void Panic() { throw new BZip2Exception("BZip2 output stream panic"); } - static void HbMakeCodeLengths(char[] len, int[] freq, int alphaSize, int maxLen) + private static void HbMakeCodeLengths(char[] len, int[] freq, int alphaSize, int maxLen) { /*-- Nodes and heap entries run from 1. Entry 0 @@ -1623,11 +1845,13 @@ Nodes and heap entries run from 1. Entry 0 int[] weight = new int[BZip2Constants.MaximumAlphaSize * 2]; int[] parent = new int[BZip2Constants.MaximumAlphaSize * 2]; - for (int i = 0; i < alphaSize; ++i) { + for (int i = 0; i < alphaSize; ++i) + { weight[i + 1] = (freq[i] == 0 ? 1 : freq[i]) << 8; } - while (true) { + while (true) + { nNodes = alphaSize; nHeap = 0; @@ -1635,38 +1859,46 @@ Nodes and heap entries run from 1. Entry 0 weight[0] = 0; parent[0] = -2; - for (int i = 1; i <= alphaSize; ++i) { + for (int i = 1; i <= alphaSize; ++i) + { parent[i] = -1; nHeap++; heap[nHeap] = i; int zz = nHeap; int tmp = heap[zz]; - while (weight[tmp] < weight[heap[zz >> 1]]) { + while (weight[tmp] < weight[heap[zz >> 1]]) + { heap[zz] = heap[zz >> 1]; zz >>= 1; } heap[zz] = tmp; } - if (!(nHeap < (BZip2Constants.MaximumAlphaSize + 2))) { + if (!(nHeap < (BZip2Constants.MaximumAlphaSize + 2))) + { Panic(); } - while (nHeap > 1) { + while (nHeap > 1) + { n1 = heap[1]; heap[1] = heap[nHeap]; nHeap--; int zz = 1; int yy = 0; int tmp = heap[zz]; - while (true) { + while (true) + { yy = zz << 1; - if (yy > nHeap) { + if (yy > nHeap) + { break; } - if (yy < nHeap && weight[heap[yy + 1]] < weight[heap[yy]]) { + if (yy < nHeap && weight[heap[yy + 1]] < weight[heap[yy]]) + { yy++; } - if (weight[tmp] < weight[heap[yy]]) { + if (weight[tmp] < weight[heap[yy]]) + { break; } @@ -1681,15 +1913,19 @@ Nodes and heap entries run from 1. Entry 0 zz = 1; yy = 0; tmp = heap[zz]; - while (true) { + while (true) + { yy = zz << 1; - if (yy > nHeap) { + if (yy > nHeap) + { break; } - if (yy < nHeap && weight[heap[yy + 1]] < weight[heap[yy]]) { + if (yy < nHeap && weight[heap[yy + 1]] < weight[heap[yy]]) + { yy++; } - if (weight[tmp] < weight[heap[yy]]) { + if (weight[tmp] < weight[heap[yy]]) + { break; } heap[zz] = heap[yy]; @@ -1708,21 +1944,25 @@ Nodes and heap entries run from 1. Entry 0 zz = nHeap; tmp = heap[zz]; - while (weight[tmp] < weight[heap[zz >> 1]]) { + while (weight[tmp] < weight[heap[zz >> 1]]) + { heap[zz] = heap[zz >> 1]; zz >>= 1; } heap[zz] = tmp; } - if (!(nNodes < (BZip2Constants.MaximumAlphaSize * 2))) { + if (!(nNodes < (BZip2Constants.MaximumAlphaSize * 2))) + { Panic(); } tooLong = false; - for (int i = 1; i <= alphaSize; ++i) { + for (int i = 1; i <= alphaSize; ++i) + { j = 0; k = i; - while (parent[k] >= 0) { + while (parent[k] >= 0) + { k = parent[k]; j++; } @@ -1730,11 +1970,13 @@ Nodes and heap entries run from 1. Entry 0 tooLong |= j > maxLen; } - if (!tooLong) { + if (!tooLong) + { break; } - for (int i = 1; i < alphaSize; ++i) { + for (int i = 1; i < alphaSize; ++i) + { j = weight[i] >> 8; j = 1 + (j / 2); weight[i] = j << 8; @@ -1742,12 +1984,15 @@ Nodes and heap entries run from 1. Entry 0 } } - static void HbAssignCodes(int[] code, char[] length, int minLen, int maxLen, int alphaSize) + private static void HbAssignCodes(int[] code, char[] length, int minLen, int maxLen, int alphaSize) { int vec = 0; - for (int n = minLen; n <= maxLen; ++n) { - for (int i = 0; i < alphaSize; ++i) { - if (length[i] == n) { + for (int n = minLen; n <= maxLen; ++n) + { + for (int i = 0; i < alphaSize; ++i) + { + if (length[i] == n) + { code[i] = vec; ++vec; } @@ -1756,26 +2001,29 @@ static void HbAssignCodes(int[] code, char[] length, int minLen, int maxLen, int } } - static byte Med3(byte a, byte b, byte c) + private static byte Med3(byte a, byte b, byte c) { byte t; - if (a > b) { + if (a > b) + { t = a; a = b; b = t; } - if (b > c) { + if (b > c) + { t = b; b = c; c = t; } - if (a > b) { + if (a > b) + { b = a; } return b; } - struct StackElement + private struct StackElement { public int ll; public int hh; diff --git a/src/ICSharpCode.SharpZipLib/Checksum/Adler32.cs b/src/ICSharpCode.SharpZipLib/Checksum/Adler32.cs index 9767425b1..b2a0f151a 100644 --- a/src/ICSharpCode.SharpZipLib/Checksum/Adler32.cs +++ b/src/ICSharpCode.SharpZipLib/Checksum/Adler32.cs @@ -6,31 +6,31 @@ namespace ICSharpCode.SharpZipLib.Checksum /// Computes Adler32 checksum for a stream of data. An Adler32 /// checksum is not as reliable as a CRC32 checksum, but a lot faster to /// compute. - /// + /// /// The specification for Adler32 may be found in RFC 1950. /// ZLIB Compressed Data Format Specification version 3.3) - /// - /// + /// + /// /// From that document: - /// + /// /// "ADLER32 (Adler-32 checksum) /// This contains a checksum value of the uncompressed data /// (excluding any dictionary data) computed according to Adler-32 /// algorithm. This algorithm is a 32-bit extension and improvement /// of the Fletcher algorithm, used in the ITU-T X.224 / ISO 8073 /// standard. - /// + /// /// Adler-32 is composed of two sums accumulated per byte: s1 is /// the sum of all bytes, s2 is the sum of all s1 values. Both sums /// are done modulo 65521. s1 is initialized to 1, s2 to zero. The /// Adler-32 checksum is stored as s2*65536 + s1 in most- /// significant-byte first (network) order." - /// + /// /// "8.2. The Adler-32 algorithm - /// + /// /// The Adler-32 algorithm is much faster than the CRC32 algorithm yet /// still provides an extremely low probability of undetected errors. - /// + /// /// The modulo on unsigned long accumulators can be delayed for 5552 /// bytes, so the modulo operation time is negligible. If the bytes /// are a, b, c, the second sum is 3a + 2b + c + 3, and so is position @@ -40,7 +40,7 @@ namespace ICSharpCode.SharpZipLib.Checksum /// (The Fletcher checksum uses 255, which is not prime and which also /// makes the Fletcher check insensitive to single byte changes 0 - /// 255.) - /// + /// /// The sum s1 is initialized to 1 instead of zero to make the length /// of the sequence part of s2, so that the length does not have to be /// checked separately. (Any sequence of zeroes has a Fletcher @@ -51,20 +51,22 @@ namespace ICSharpCode.SharpZipLib.Checksum public sealed class Adler32 : IChecksum { #region Instance Fields + /// /// largest prime smaller than 65536 /// - readonly static uint BASE = 65521; + private static readonly uint BASE = 65521; /// /// The CRC data checksum so far. /// - uint checkValue; - #endregion + private uint checkValue; + + #endregion Instance Fields /// /// Initialise a default instance of - /// + /// public Adler32() { Reset(); @@ -81,8 +83,10 @@ public void Reset() /// /// Returns the Adler32 data checksum computed so far. /// - public long Value { - get { + public long Value + { + get + { return checkValue; } } @@ -107,68 +111,52 @@ public void Update(int bval) } /// - /// Updates the Adler32 data checksum with the bytes taken from + /// Updates the Adler32 data checksum with the bytes taken from /// a block of data. /// /// Contains the data to update the checksum with. public void Update(byte[] buffer) { - if (buffer == null) { + if (buffer == null) + { throw new ArgumentNullException(nameof(buffer)); } - Update(buffer, 0, buffer.Length); + Update(new ArraySegment(buffer, 0, buffer.Length)); } /// /// Update Adler32 data checksum based on a portion of a block of data /// - /// Contains the data to update the CRC with. - /// The offset into the buffer where the data starts - /// The number of data bytes to update the CRC with. - public void Update(byte[] buffer, int offset, int count) + /// + /// The chunk of data to add + /// + public void Update(ArraySegment segment) { - if (buffer == null) { - throw new ArgumentNullException(nameof(buffer)); - } - - if (offset < 0) { - throw new ArgumentOutOfRangeException(nameof(offset), "cannot be less than zero"); - } - - if (offset >= buffer.Length) { - throw new ArgumentOutOfRangeException(nameof(offset), "not a valid index into buffer"); - } - - if (count < 0) { - throw new ArgumentOutOfRangeException(nameof(count), "cannot be less than zero"); - } - - if (offset + count > buffer.Length) { - throw new ArgumentOutOfRangeException(nameof(count), "exceeds buffer size"); - } - //(By Per Bothner) uint s1 = checkValue & 0xFFFF; uint s2 = checkValue >> 16; - - while (count > 0) { + var count = segment.Count; + var offset = segment.Offset; + while (count > 0) + { // We can defer the modulo operation: // s1 maximally grows from 65521 to 65521 + 255 * 3800 // s2 maximally grows by 3800 * median(s1) = 2090079800 < 2^31 int n = 3800; - if (n > count) { + if (n > count) + { n = count; } count -= n; - while (--n >= 0) { - s1 = s1 + (uint)(buffer[offset++] & 0xff); + while (--n >= 0) + { + s1 = s1 + (uint)(segment.Array[offset++] & 0xff); s2 = s2 + s1; } s1 %= BASE; s2 %= BASE; } - checkValue = (s2 << 16) | s1; } } diff --git a/src/ICSharpCode.SharpZipLib/Checksum/BZip2Crc.cs b/src/ICSharpCode.SharpZipLib/Checksum/BZip2Crc.cs index 4be8fb1cb..be76da11e 100644 --- a/src/ICSharpCode.SharpZipLib/Checksum/BZip2Crc.cs +++ b/src/ICSharpCode.SharpZipLib/Checksum/BZip2Crc.cs @@ -1,4 +1,5 @@ using System; +using System.Runtime.CompilerServices; namespace ICSharpCode.SharpZipLib.Checksum { @@ -25,92 +26,39 @@ namespace ICSharpCode.SharpZipLib.Checksum /// out is a one). We start with the highest power (least significant bit) of /// q and repeat for all eight bits of q. /// - /// The table is simply the CRC of all possible eight bit values. This is all - /// the information needed to generate CRC's on data a byte at a time for all - /// combinations of CRC register values and incoming bytes. + /// This implementation uses sixteen lookup tables stored in one linear array + /// to implement the slicing-by-16 algorithm, a variant of the slicing-by-8 + /// algorithm described in this Intel white paper: + /// + /// https://web.archive.org/web/20120722193753/http://download.intel.com/technology/comms/perfnet/download/slicing-by-8.pdf + /// + /// The first lookup table is simply the CRC of all possible eight bit values. + /// Each successive lookup table is derived from the original table generated + /// by Sarwate's algorithm. Slicing a 16-bit input and XORing the outputs + /// together will produce the same output as a byte-by-byte CRC loop with + /// fewer arithmetic and bit manipulation operations, at the cost of increased + /// memory consumed by the lookup tables. (Slicing-by-16 requires a 16KB table, + /// which is still small enough to fit in most processors' L1 cache.) /// public sealed class BZip2Crc : IChecksum { #region Instance Fields - const uint crcInit = 0xFFFFFFFF; + + private const uint crcInit = 0xFFFFFFFF; //const uint crcXor = 0x00000000; - readonly static uint[] crcTable = { - 0X00000000, 0X04C11DB7, 0X09823B6E, 0X0D4326D9, - 0X130476DC, 0X17C56B6B, 0X1A864DB2, 0X1E475005, - 0X2608EDB8, 0X22C9F00F, 0X2F8AD6D6, 0X2B4BCB61, - 0X350C9B64, 0X31CD86D3, 0X3C8EA00A, 0X384FBDBD, - 0X4C11DB70, 0X48D0C6C7, 0X4593E01E, 0X4152FDA9, - 0X5F15ADAC, 0X5BD4B01B, 0X569796C2, 0X52568B75, - 0X6A1936C8, 0X6ED82B7F, 0X639B0DA6, 0X675A1011, - 0X791D4014, 0X7DDC5DA3, 0X709F7B7A, 0X745E66CD, - 0X9823B6E0, 0X9CE2AB57, 0X91A18D8E, 0X95609039, - 0X8B27C03C, 0X8FE6DD8B, 0X82A5FB52, 0X8664E6E5, - 0XBE2B5B58, 0XBAEA46EF, 0XB7A96036, 0XB3687D81, - 0XAD2F2D84, 0XA9EE3033, 0XA4AD16EA, 0XA06C0B5D, - 0XD4326D90, 0XD0F37027, 0XDDB056FE, 0XD9714B49, - 0XC7361B4C, 0XC3F706FB, 0XCEB42022, 0XCA753D95, - 0XF23A8028, 0XF6FB9D9F, 0XFBB8BB46, 0XFF79A6F1, - 0XE13EF6F4, 0XE5FFEB43, 0XE8BCCD9A, 0XEC7DD02D, - 0X34867077, 0X30476DC0, 0X3D044B19, 0X39C556AE, - 0X278206AB, 0X23431B1C, 0X2E003DC5, 0X2AC12072, - 0X128E9DCF, 0X164F8078, 0X1B0CA6A1, 0X1FCDBB16, - 0X018AEB13, 0X054BF6A4, 0X0808D07D, 0X0CC9CDCA, - 0X7897AB07, 0X7C56B6B0, 0X71159069, 0X75D48DDE, - 0X6B93DDDB, 0X6F52C06C, 0X6211E6B5, 0X66D0FB02, - 0X5E9F46BF, 0X5A5E5B08, 0X571D7DD1, 0X53DC6066, - 0X4D9B3063, 0X495A2DD4, 0X44190B0D, 0X40D816BA, - 0XACA5C697, 0XA864DB20, 0XA527FDF9, 0XA1E6E04E, - 0XBFA1B04B, 0XBB60ADFC, 0XB6238B25, 0XB2E29692, - 0X8AAD2B2F, 0X8E6C3698, 0X832F1041, 0X87EE0DF6, - 0X99A95DF3, 0X9D684044, 0X902B669D, 0X94EA7B2A, - 0XE0B41DE7, 0XE4750050, 0XE9362689, 0XEDF73B3E, - 0XF3B06B3B, 0XF771768C, 0XFA325055, 0XFEF34DE2, - 0XC6BCF05F, 0XC27DEDE8, 0XCF3ECB31, 0XCBFFD686, - 0XD5B88683, 0XD1799B34, 0XDC3ABDED, 0XD8FBA05A, - 0X690CE0EE, 0X6DCDFD59, 0X608EDB80, 0X644FC637, - 0X7A089632, 0X7EC98B85, 0X738AAD5C, 0X774BB0EB, - 0X4F040D56, 0X4BC510E1, 0X46863638, 0X42472B8F, - 0X5C007B8A, 0X58C1663D, 0X558240E4, 0X51435D53, - 0X251D3B9E, 0X21DC2629, 0X2C9F00F0, 0X285E1D47, - 0X36194D42, 0X32D850F5, 0X3F9B762C, 0X3B5A6B9B, - 0X0315D626, 0X07D4CB91, 0X0A97ED48, 0X0E56F0FF, - 0X1011A0FA, 0X14D0BD4D, 0X19939B94, 0X1D528623, - 0XF12F560E, 0XF5EE4BB9, 0XF8AD6D60, 0XFC6C70D7, - 0XE22B20D2, 0XE6EA3D65, 0XEBA91BBC, 0XEF68060B, - 0XD727BBB6, 0XD3E6A601, 0XDEA580D8, 0XDA649D6F, - 0XC423CD6A, 0XC0E2D0DD, 0XCDA1F604, 0XC960EBB3, - 0XBD3E8D7E, 0XB9FF90C9, 0XB4BCB610, 0XB07DABA7, - 0XAE3AFBA2, 0XAAFBE615, 0XA7B8C0CC, 0XA379DD7B, - 0X9B3660C6, 0X9FF77D71, 0X92B45BA8, 0X9675461F, - 0X8832161A, 0X8CF30BAD, 0X81B02D74, 0X857130C3, - 0X5D8A9099, 0X594B8D2E, 0X5408ABF7, 0X50C9B640, - 0X4E8EE645, 0X4A4FFBF2, 0X470CDD2B, 0X43CDC09C, - 0X7B827D21, 0X7F436096, 0X7200464F, 0X76C15BF8, - 0X68860BFD, 0X6C47164A, 0X61043093, 0X65C52D24, - 0X119B4BE9, 0X155A565E, 0X18197087, 0X1CD86D30, - 0X029F3D35, 0X065E2082, 0X0B1D065B, 0X0FDC1BEC, - 0X3793A651, 0X3352BBE6, 0X3E119D3F, 0X3AD08088, - 0X2497D08D, 0X2056CD3A, 0X2D15EBE3, 0X29D4F654, - 0XC5A92679, 0XC1683BCE, 0XCC2B1D17, 0XC8EA00A0, - 0XD6AD50A5, 0XD26C4D12, 0XDF2F6BCB, 0XDBEE767C, - 0XE3A1CBC1, 0XE760D676, 0XEA23F0AF, 0XEEE2ED18, - 0XF0A5BD1D, 0XF464A0AA, 0XF9278673, 0XFDE69BC4, - 0X89B8FD09, 0X8D79E0BE, 0X803AC667, 0X84FBDBD0, - 0X9ABC8BD5, 0X9E7D9662, 0X933EB0BB, 0X97FFAD0C, - 0XAFB010B1, 0XAB710D06, 0XA6322BDF, 0XA2F33668, - 0XBCB4666D, 0XB8757BDA, 0XB5365D03, 0XB1F740B4 - }; + private static readonly uint[] crcTable = CrcUtilities.GenerateSlicingLookupTable(0x04C11DB7, isReversed: false); /// /// The CRC data checksum so far. /// - uint checkValue; - #endregion + private uint checkValue; + + #endregion Instance Fields /// /// Initialise a default instance of - /// + /// public BZip2Crc() { Reset(); @@ -128,9 +76,11 @@ public void Reset() /// Returns the CRC data checksum computed so far. /// /// Reversed Out = true - public long Value { - get { - // Tehcnically, the output should be: + public long Value + { + get + { + // Technically, the output should be: //return (long)(~checkValue ^ crcXor); // but x ^ 0 = x, so there is no point in adding // the XOR operation @@ -145,19 +95,21 @@ public long Value { /// the byte is taken as the lower 8 bits of bval /// /// Reversed Data = false + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Update(int bval) { checkValue = unchecked(crcTable[(byte)(((checkValue >> 24) & 0xFF) ^ bval)] ^ (checkValue << 8)); } /// - /// Updates the CRC data checksum with the bytes taken from + /// Updates the CRC data checksum with the bytes taken from /// a block of data. /// /// Contains the data to update the CRC with. public void Update(byte[] buffer) { - if (buffer == null) { + if (buffer == null) + { throw new ArgumentNullException(nameof(buffer)); } @@ -167,33 +119,52 @@ public void Update(byte[] buffer) /// /// Update CRC data checksum based on a portion of a block of data /// - /// Contains the data to update the CRC with. - /// The offset into the buffer where the data starts - /// The number of data bytes to update the CRC with. - public void Update(byte[] buffer, int offset, int count) + /// + /// The chunk of data to add + /// + public void Update(ArraySegment segment) { - if (buffer == null) { - throw new ArgumentNullException(nameof(buffer)); - } - - if (offset < 0) { - throw new ArgumentOutOfRangeException(nameof(offset), "cannot be less than zero"); - } + Update(segment.Array, segment.Offset, segment.Count); + } - if (offset >= buffer.Length) { - throw new ArgumentOutOfRangeException(nameof(offset), "not a valid index into buffer"); - } + /// + /// Internal helper function for updating a block of data using slicing. + /// + /// The array containing the data to add + /// Range start for (inclusive) + /// The number of bytes to checksum starting from + private void Update(byte[] data, int offset, int count) + { + int remainder = count % CrcUtilities.SlicingDegree; + int end = offset + count - remainder; - if (count < 0) { - throw new ArgumentOutOfRangeException(nameof(count), "cannot be less than zero"); + while (offset != end) + { + checkValue = CrcUtilities.UpdateDataForNormalPoly(data, offset, crcTable, checkValue); + offset += CrcUtilities.SlicingDegree; } - if (offset + count > buffer.Length) { - throw new ArgumentOutOfRangeException(nameof(count), "exceeds buffer size"); + if (remainder != 0) + { + SlowUpdateLoop(data, offset, end + remainder); } + } - for (int i = 0; i < count; ++i) { - Update(buffer[offset++]); + /// + /// A non-inlined function for updating data that doesn't fit in a 16-byte + /// block. We don't expect to enter this function most of the time, and when + /// we do we're not here for long, so disabling inlining here improves + /// performance overall. + /// + /// The array containing the data to add + /// Range start for (inclusive) + /// Range end for (exclusive) + [MethodImpl(MethodImplOptions.NoInlining)] + private void SlowUpdateLoop(byte[] data, int offset, int end) + { + while (offset != end) + { + Update(data[offset++]); } } } diff --git a/src/ICSharpCode.SharpZipLib/Checksum/Crc32.cs b/src/ICSharpCode.SharpZipLib/Checksum/Crc32.cs index 897076942..740aff566 100644 --- a/src/ICSharpCode.SharpZipLib/Checksum/Crc32.cs +++ b/src/ICSharpCode.SharpZipLib/Checksum/Crc32.cs @@ -1,4 +1,5 @@ using System; +using System.Runtime.CompilerServices; namespace ICSharpCode.SharpZipLib.Checksum { @@ -25,77 +26,37 @@ namespace ICSharpCode.SharpZipLib.Checksum /// out is a one). We start with the highest power (least significant bit) of /// q and repeat for all eight bits of q. /// - /// The table is simply the CRC of all possible eight bit values. This is all - /// the information needed to generate CRC's on data a byte at a time for all - /// combinations of CRC register values and incoming bytes. + /// This implementation uses sixteen lookup tables stored in one linear array + /// to implement the slicing-by-16 algorithm, a variant of the slicing-by-8 + /// algorithm described in this Intel white paper: + /// + /// https://web.archive.org/web/20120722193753/http://download.intel.com/technology/comms/perfnet/download/slicing-by-8.pdf + /// + /// The first lookup table is simply the CRC of all possible eight bit values. + /// Each successive lookup table is derived from the original table generated + /// by Sarwate's algorithm. Slicing a 16-bit input and XORing the outputs + /// together will produce the same output as a byte-by-byte CRC loop with + /// fewer arithmetic and bit manipulation operations, at the cost of increased + /// memory consumed by the lookup tables. (Slicing-by-16 requires a 16KB table, + /// which is still small enough to fit in most processors' L1 cache.) /// public sealed class Crc32 : IChecksum { #region Instance Fields - readonly static uint crcInit = 0xFFFFFFFF; - readonly static uint crcXor = 0xFFFFFFFF; - - readonly static uint[] crcTable = { - 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 - }; + + private static readonly uint crcInit = 0xFFFFFFFF; + private static readonly uint crcXor = 0xFFFFFFFF; + + private static readonly uint[] crcTable = CrcUtilities.GenerateSlicingLookupTable(0xEDB88320, isReversed: true); /// /// The CRC data checksum so far. /// - uint checkValue; - #endregion + private uint checkValue; + + #endregion Instance Fields + [MethodImpl(MethodImplOptions.AggressiveInlining)] internal static uint ComputeCrc32(uint oldCrc, byte bval) { return (uint)(Crc32.crcTable[(oldCrc ^ bval) & 0xFF] ^ (oldCrc >> 8)); @@ -121,8 +82,10 @@ public void Reset() /// Returns the CRC data checksum computed so far. /// /// Reversed Out = false - public long Value { - get { + public long Value + { + get + { return (long)(checkValue ^ crcXor); } } @@ -134,19 +97,21 @@ public long Value { /// the byte is taken as the lower 8 bits of bval /// /// Reversed Data = true + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Update(int bval) { checkValue = unchecked(crcTable[(checkValue ^ bval) & 0xFF] ^ (checkValue >> 8)); } /// - /// Updates the CRC data checksum with the bytes taken from + /// Updates the CRC data checksum with the bytes taken from /// a block of data. /// /// Contains the data to update the CRC with. public void Update(byte[] buffer) { - if (buffer == null) { + if (buffer == null) + { throw new ArgumentNullException(nameof(buffer)); } @@ -156,33 +121,52 @@ public void Update(byte[] buffer) /// /// Update CRC data checksum based on a portion of a block of data /// - /// Contains the data to update the CRC with. - /// The offset into the buffer where the data starts - /// The number of data bytes to update the CRC with. - public void Update(byte[] buffer, int offset, int count) + /// + /// The chunk of data to add + /// + public void Update(ArraySegment segment) { - if (buffer == null) { - throw new ArgumentNullException(nameof(buffer)); - } - - if (offset < 0) { - throw new ArgumentOutOfRangeException(nameof(offset), "cannot be less than zero"); - } + Update(segment.Array, segment.Offset, segment.Count); + } - if (offset >= buffer.Length) { - throw new ArgumentOutOfRangeException(nameof(offset), "not a valid index into buffer"); - } + /// + /// Internal helper function for updating a block of data using slicing. + /// + /// The array containing the data to add + /// Range start for (inclusive) + /// The number of bytes to checksum starting from + private void Update(byte[] data, int offset, int count) + { + int remainder = count % CrcUtilities.SlicingDegree; + int end = offset + count - remainder; - if (count < 0) { - throw new ArgumentOutOfRangeException(nameof(count), "cannot be less than zero"); + while (offset != end) + { + checkValue = CrcUtilities.UpdateDataForReversedPoly(data, offset, crcTable, checkValue); + offset += CrcUtilities.SlicingDegree; } - if (offset + count > buffer.Length) { - throw new ArgumentOutOfRangeException(nameof(count), "exceeds buffer size"); + if (remainder != 0) + { + SlowUpdateLoop(data, offset, end + remainder); } + } - for (int i = 0; i < count; ++i) { - Update(buffer[offset++]); + /// + /// A non-inlined function for updating data that doesn't fit in a 16-byte + /// block. We don't expect to enter this function most of the time, and when + /// we do we're not here for long, so disabling inlining here improves + /// performance overall. + /// + /// The array containing the data to add + /// Range start for (inclusive) + /// Range end for (exclusive) + [MethodImpl(MethodImplOptions.NoInlining)] + private void SlowUpdateLoop(byte[] data, int offset, int end) + { + while (offset != end) + { + Update(data[offset++]); } } } diff --git a/src/ICSharpCode.SharpZipLib/Checksum/CrcUtilities.cs b/src/ICSharpCode.SharpZipLib/Checksum/CrcUtilities.cs new file mode 100644 index 000000000..575abe08b --- /dev/null +++ b/src/ICSharpCode.SharpZipLib/Checksum/CrcUtilities.cs @@ -0,0 +1,158 @@ +using System.Runtime.CompilerServices; + +namespace ICSharpCode.SharpZipLib.Checksum +{ + internal static class CrcUtilities + { + /// + /// The number of slicing lookup tables to generate. + /// + internal const int SlicingDegree = 16; + + /// + /// Generates multiple CRC lookup tables for a given polynomial, stored + /// in a linear array of uints. The first block (i.e. the first 256 + /// elements) is the same as the byte-by-byte CRC lookup table. + /// + /// The generating CRC polynomial + /// Whether the polynomial is in reversed bit order + /// A linear array of 256 * elements + /// + /// This table could also be generated as a rectangular array, but the + /// JIT compiler generates slower code than if we use a linear array. + /// Known issue, see: https://github.com/dotnet/runtime/issues/30275 + /// + internal static uint[] GenerateSlicingLookupTable(uint polynomial, bool isReversed) + { + var table = new uint[256 * SlicingDegree]; + uint one = isReversed ? 1 : (1U << 31); + + for (int i = 0; i < 256; i++) + { + uint res = (uint)(isReversed ? i : i << 24); + for (int j = 0; j < SlicingDegree; j++) + { + for (int k = 0; k < 8; k++) + { + if (isReversed) + { + res = (res & one) == 1 ? polynomial ^ (res >> 1) : res >> 1; + } + else + { + res = (res & one) != 0 ? polynomial ^ (res << 1) : res << 1; + } + } + + table[(256 * j) + i] = res; + } + } + + return table; + } + + /// + /// Mixes the first four bytes of input with + /// using normal ordering before calling . + /// + /// Array of data to checksum + /// Offset to start reading from + /// The table to use for slicing-by-16 lookup + /// Checksum state before this update call + /// A new unfinalized checksum value + /// + /// + /// Assumes input[offset]..input[offset + 15] are valid array indexes. + /// For performance reasons, this must be checked by the caller. + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal static uint UpdateDataForNormalPoly(byte[] input, int offset, uint[] crcTable, uint checkValue) + { + byte x1 = (byte)((byte)(checkValue >> 24) ^ input[offset]); + byte x2 = (byte)((byte)(checkValue >> 16) ^ input[offset + 1]); + byte x3 = (byte)((byte)(checkValue >> 8) ^ input[offset + 2]); + byte x4 = (byte)((byte)checkValue ^ input[offset + 3]); + + return UpdateDataCommon(input, offset, crcTable, x1, x2, x3, x4); + } + + /// + /// Mixes the first four bytes of input with + /// using reflected ordering before calling . + /// + /// Array of data to checksum + /// Offset to start reading from + /// The table to use for slicing-by-16 lookup + /// Checksum state before this update call + /// A new unfinalized checksum value + /// + /// + /// Assumes input[offset]..input[offset + 15] are valid array indexes. + /// For performance reasons, this must be checked by the caller. + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal static uint UpdateDataForReversedPoly(byte[] input, int offset, uint[] crcTable, uint checkValue) + { + byte x1 = (byte)((byte)checkValue ^ input[offset]); + byte x2 = (byte)((byte)(checkValue >>= 8) ^ input[offset + 1]); + byte x3 = (byte)((byte)(checkValue >>= 8) ^ input[offset + 2]); + byte x4 = (byte)((byte)(checkValue >>= 8) ^ input[offset + 3]); + + return UpdateDataCommon(input, offset, crcTable, x1, x2, x3, x4); + } + + /// + /// A shared method for updating an unfinalized CRC checksum using slicing-by-16. + /// + /// Array of data to checksum + /// Offset to start reading from + /// The table to use for slicing-by-16 lookup + /// First byte of input after mixing with the old CRC + /// Second byte of input after mixing with the old CRC + /// Third byte of input after mixing with the old CRC + /// Fourth byte of input after mixing with the old CRC + /// A new unfinalized checksum value + /// + /// + /// Even though the first four bytes of input are fed in as arguments, + /// should be the same value passed to this + /// function's caller (either or + /// ). This method will get inlined + /// into both functions, so using the same offset produces faster code. + /// + /// + /// Because most processors running C# have some kind of instruction-level + /// parallelism, the order of XOR operations can affect performance. This + /// ordering assumes that the assembly code generated by the just-in-time + /// compiler will emit a bunch of arithmetic operations for checking array + /// bounds. Then it opportunistically XORs a1 and a2 to keep the processor + /// busy while those other parts of the pipeline handle the range check + /// calculations. + /// + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static uint UpdateDataCommon(byte[] input, int offset, uint[] crcTable, byte x1, byte x2, byte x3, byte x4) + { + uint result; + uint a1 = crcTable[x1 + 3840] ^ crcTable[x2 + 3584]; + uint a2 = crcTable[x3 + 3328] ^ crcTable[x4 + 3072]; + + result = crcTable[input[offset + 4] + 2816]; + result ^= crcTable[input[offset + 5] + 2560]; + a1 ^= crcTable[input[offset + 9] + 1536]; + result ^= crcTable[input[offset + 6] + 2304]; + result ^= crcTable[input[offset + 7] + 2048]; + result ^= crcTable[input[offset + 8] + 1792]; + a2 ^= crcTable[input[offset + 13] + 512]; + result ^= crcTable[input[offset + 10] + 1280]; + result ^= crcTable[input[offset + 11] + 1024]; + result ^= crcTable[input[offset + 12] + 768]; + result ^= a1; + result ^= crcTable[input[offset + 14] + 256]; + result ^= crcTable[input[offset + 15]]; + result ^= a2; + + return result; + } + } +} diff --git a/src/ICSharpCode.SharpZipLib/Checksum/IChecksum.cs b/src/ICSharpCode.SharpZipLib/Checksum/IChecksum.cs index 93dc51cfa..db74a5a5d 100644 --- a/src/ICSharpCode.SharpZipLib/Checksum/IChecksum.cs +++ b/src/ICSharpCode.SharpZipLib/Checksum/IChecksum.cs @@ -1,3 +1,5 @@ +using System; + namespace ICSharpCode.SharpZipLib.Checksum { /// @@ -17,7 +19,8 @@ public interface IChecksum /// /// Returns the data checksum computed so far. /// - long Value { + long Value + { get; } @@ -40,15 +43,9 @@ long Value { /// /// Adds the byte array to the data checksum. /// - /// - /// The buffer which contains the data - /// - /// - /// The offset in the buffer where the data starts - /// - /// - /// the number of data bytes to add. + /// + /// The chunk of data to add /// - void Update(byte[] buffer, int offset, int count); + void Update(ArraySegment segment); } } diff --git a/src/ICSharpCode.SharpZipLib/Core/ByteOrderUtils.cs b/src/ICSharpCode.SharpZipLib/Core/ByteOrderUtils.cs new file mode 100644 index 000000000..14b096207 --- /dev/null +++ b/src/ICSharpCode.SharpZipLib/Core/ByteOrderUtils.cs @@ -0,0 +1,130 @@ +using System.IO; +using System.Runtime.CompilerServices; +using System.Threading.Tasks; +using CT = System.Threading.CancellationToken; + +// ReSharper disable MemberCanBePrivate.Global +// ReSharper disable InconsistentNaming + +namespace ICSharpCode.SharpZipLib.Core +{ + internal static class ByteOrderStreamExtensions + { + internal static byte[] SwappedBytes(ushort value) => new[] {(byte)value, (byte)(value >> 8)}; + internal static byte[] SwappedBytes(short value) => new[] {(byte)value, (byte)(value >> 8)}; + internal static byte[] SwappedBytes(uint value) => new[] {(byte)value, (byte)(value >> 8), (byte)(value >> 16), (byte)(value >> 24)}; + internal static byte[] SwappedBytes(int value) => new[] {(byte)value, (byte)(value >> 8), (byte)(value >> 16), (byte)(value >> 24)}; + + internal static byte[] SwappedBytes(long value) => new[] { + (byte)value, (byte)(value >> 8), (byte)(value >> 16), (byte)(value >> 24), + (byte)(value >> 32), (byte)(value >> 40), (byte)(value >> 48), (byte)(value >> 56) + }; + + internal static byte[] SwappedBytes(ulong value) => new[] { + (byte)value, (byte)(value >> 8), (byte)(value >> 16), (byte)(value >> 24), + (byte)(value >> 32), (byte)(value >> 40), (byte)(value >> 48), (byte)(value >> 56) + }; + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal static long SwappedS64(byte[] bytes) => ( + (long)bytes[0] << 0 | (long)bytes[1] << 8 | (long)bytes[2] << 16 | (long)bytes[3] << 24 | + (long)bytes[4] << 32 | (long)bytes[5] << 40 | (long)bytes[6] << 48 | (long)bytes[7] << 56); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal static ulong SwappedU64(byte[] bytes) => ( + (ulong)bytes[0] << 0 | (ulong)bytes[1] << 8 | (ulong)bytes[2] << 16 | (ulong)bytes[3] << 24 | + (ulong)bytes[4] << 32 | (ulong)bytes[5] << 40 | (ulong)bytes[6] << 48 | (ulong)bytes[7] << 56); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal static int SwappedS32(byte[] bytes) => bytes[0] | bytes[1] << 8 | bytes[2] << 16 | bytes[3] << 24; + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal static uint SwappedU32(byte[] bytes) => (uint) SwappedS32(bytes); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal static short SwappedS16(byte[] bytes) => (short)(bytes[0] | bytes[1] << 8); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal static ushort SwappedU16(byte[] bytes) => (ushort) SwappedS16(bytes); + + internal static byte[] ReadBytes(this Stream stream, int count) + { + var bytes = new byte[count]; + var remaining = count; + while (remaining > 0) + { + var bytesRead = stream.Read(bytes, count - remaining, remaining); + if (bytesRead < 1) throw new EndOfStreamException(); + remaining -= bytesRead; + } + + return bytes; + } + + /// Read an unsigned short in little endian byte order. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static int ReadLEShort(this Stream stream) => SwappedS16(ReadBytes(stream, 2)); + + /// Read an int in little endian byte order. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static int ReadLEInt(this Stream stream) => SwappedS32(ReadBytes(stream, 4)); + + /// Read a long in little endian byte order. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static long ReadLELong(this Stream stream) => SwappedS64(ReadBytes(stream, 8)); + + /// Write an unsigned short in little endian byte order. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void WriteLEShort(this Stream stream, int value) => stream.Write(SwappedBytes(value), 0, 2); + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static async Task WriteLEShortAsync(this Stream stream, int value, CT ct) + => await stream.WriteAsync(SwappedBytes(value), 0, 2, ct).ConfigureAwait(false); + + /// Write a ushort in little endian byte order. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void WriteLEUshort(this Stream stream, ushort value) => stream.Write(SwappedBytes(value), 0, 2); + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static async Task WriteLEUshortAsync(this Stream stream, ushort value, CT ct) + => await stream.WriteAsync(SwappedBytes(value), 0, 2, ct).ConfigureAwait(false); + + /// Write an int in little endian byte order. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void WriteLEInt(this Stream stream, int value) => stream.Write(SwappedBytes(value), 0, 4); + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static async Task WriteLEIntAsync(this Stream stream, int value, CT ct) + => await stream.WriteAsync(SwappedBytes(value), 0, 4, ct).ConfigureAwait(false); + + /// Write a uint in little endian byte order. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void WriteLEUint(this Stream stream, uint value) => stream.Write(SwappedBytes(value), 0, 4); + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static async Task WriteLEUintAsync(this Stream stream, uint value, CT ct) + => await stream.WriteAsync(SwappedBytes(value), 0, 4, ct).ConfigureAwait(false); + + /// Write a long in little endian byte order. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void WriteLELong(this Stream stream, long value) => stream.Write(SwappedBytes(value), 0, 8); + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static async Task WriteLELongAsync(this Stream stream, long value, CT ct) + => await stream.WriteAsync(SwappedBytes(value), 0, 8, ct).ConfigureAwait(false); + + /// Write a ulong in little endian byte order. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void WriteLEUlong(this Stream stream, ulong value) => stream.Write(SwappedBytes(value), 0, 8); + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static async Task WriteLEUlongAsync(this Stream stream, ulong value, CT ct) + => await stream.WriteAsync(SwappedBytes(value), 0, 8, ct).ConfigureAwait(false); + } +} diff --git a/src/ICSharpCode.SharpZipLib/Core/EmptyRefs.cs b/src/ICSharpCode.SharpZipLib/Core/EmptyRefs.cs new file mode 100644 index 000000000..feb7a8e17 --- /dev/null +++ b/src/ICSharpCode.SharpZipLib/Core/EmptyRefs.cs @@ -0,0 +1,17 @@ +using System; + +namespace ICSharpCode.SharpZipLib.Core +{ + internal static class Empty + { +#if NET45 + internal static class EmptyArray + { + public static readonly T[] Value = new T[0]; + } + public static T[] Array() => EmptyArray.Value; +#else + public static T[] Array() => System.Array.Empty(); +#endif + } +} diff --git a/src/ICSharpCode.SharpZipLib/Core/ExactMemoryPool.cs b/src/ICSharpCode.SharpZipLib/Core/ExactMemoryPool.cs new file mode 100644 index 000000000..d03ca2ecf --- /dev/null +++ b/src/ICSharpCode.SharpZipLib/Core/ExactMemoryPool.cs @@ -0,0 +1,71 @@ +using System; +using System.Buffers; + +namespace ICSharpCode.SharpZipLib.Core +{ + /// + /// A MemoryPool that will return a Memory which is exactly the length asked for using the bufferSize parameter. + /// This is in contrast to the default ArrayMemoryPool which will return a Memory of equal size to the underlying + /// array which at least as long as the minBufferSize parameter. + /// Note: The underlying array may be larger than the slice of Memory + /// + /// + internal sealed class ExactMemoryPool : MemoryPool + { + public new static readonly MemoryPool Shared = new ExactMemoryPool(); + + public override IMemoryOwner Rent(int bufferSize = -1) + { + if ((uint)bufferSize > int.MaxValue || bufferSize < 0) + { + throw new ArgumentOutOfRangeException(nameof(bufferSize)); + } + + return new ExactMemoryPoolBuffer(bufferSize); + } + + protected override void Dispose(bool disposing) + { + } + + public override int MaxBufferSize => int.MaxValue; + + private sealed class ExactMemoryPoolBuffer : IMemoryOwner, IDisposable + { + private T[] array; + private readonly int size; + + public ExactMemoryPoolBuffer(int size) + { + this.size = size; + this.array = ArrayPool.Shared.Rent(size); + } + + public Memory Memory + { + get + { + T[] array = this.array; + if (array == null) + { + throw new ObjectDisposedException(nameof(ExactMemoryPoolBuffer)); + } + + return new Memory(array).Slice(0, size); + } + } + + public void Dispose() + { + T[] array = this.array; + if (array == null) + { + return; + } + + this.array = null; + ArrayPool.Shared.Return(array); + } + } + } +} diff --git a/src/ICSharpCode.SharpZipLib/SharpZipBaseException.cs b/src/ICSharpCode.SharpZipLib/Core/Exceptions/SharpZipBaseException.cs similarity index 50% rename from src/ICSharpCode.SharpZipLib/SharpZipBaseException.cs rename to src/ICSharpCode.SharpZipLib/Core/Exceptions/SharpZipBaseException.cs index 45686effe..eb14e2d49 100644 --- a/src/ICSharpCode.SharpZipLib/SharpZipBaseException.cs +++ b/src/ICSharpCode.SharpZipLib/Core/Exceptions/SharpZipBaseException.cs @@ -1,13 +1,15 @@ using System; +using System.Runtime.Serialization; namespace ICSharpCode.SharpZipLib { - /// - /// SharpZipBaseException is the base exception class for SharpZipLib. - /// All library exceptions are derived from this. - /// - /// NOTE: Not all exceptions thrown will be derived from this class. - /// A variety of other exceptions are possible for example + /// + /// SharpZipBaseException is the base exception class for SharpZipLib. + /// All library exceptions are derived from this. + /// + /// NOTE: Not all exceptions thrown will be derived from this class. + /// A variety of other exceptions are possible for example + [Serializable] public class SharpZipBaseException : Exception { /// @@ -36,5 +38,21 @@ public SharpZipBaseException(string message, Exception innerException) : base(message, innerException) { } + + /// + /// Initializes a new instance of the SharpZipBaseException class with serialized data. + /// + /// + /// The System.Runtime.Serialization.SerializationInfo that holds the serialized + /// object data about the exception being thrown. + /// + /// + /// The System.Runtime.Serialization.StreamingContext that contains contextual information + /// about the source or destination. + /// + protected SharpZipBaseException(SerializationInfo info, StreamingContext context) + : base(info, context) + { + } } } diff --git a/src/ICSharpCode.SharpZipLib/Core/Exceptions/StreamDecodingException.cs b/src/ICSharpCode.SharpZipLib/Core/Exceptions/StreamDecodingException.cs new file mode 100644 index 000000000..e22b11b0d --- /dev/null +++ b/src/ICSharpCode.SharpZipLib/Core/Exceptions/StreamDecodingException.cs @@ -0,0 +1,50 @@ +using System; +using System.Runtime.Serialization; + +namespace ICSharpCode.SharpZipLib +{ + /// + /// Indicates that an error occurred during decoding of a input stream due to corrupt + /// data or (unintentional) library incompatibility. + /// + [Serializable] + public class StreamDecodingException : SharpZipBaseException + { + private const string GenericMessage = "Input stream could not be decoded"; + + /// + /// Initializes a new instance of the StreamDecodingException with a generic message + /// + public StreamDecodingException() : base(GenericMessage) { } + + /// + /// Initializes a new instance of the StreamDecodingException class with a specified error message. + /// + /// A message describing the exception. + public StreamDecodingException(string message) : base(message) { } + + /// + /// Initializes a new instance of the StreamDecodingException class with a specified + /// error message and a reference to the inner exception that is the cause of this exception. + /// + /// A message describing the exception. + /// The inner exception + public StreamDecodingException(string message, Exception innerException) : base(message, innerException) { } + + /// + /// Initializes a new instance of the StreamDecodingException class with serialized data. + /// + /// + /// The System.Runtime.Serialization.SerializationInfo that holds the serialized + /// object data about the exception being thrown. + /// + /// + /// The System.Runtime.Serialization.StreamingContext that contains contextual information + /// about the source or destination. + /// + protected StreamDecodingException(SerializationInfo info, StreamingContext context) + : base(info, context) + { + } + } +} diff --git a/src/ICSharpCode.SharpZipLib/Core/Exceptions/StreamUnsupportedException.cs b/src/ICSharpCode.SharpZipLib/Core/Exceptions/StreamUnsupportedException.cs new file mode 100644 index 000000000..5827e559d --- /dev/null +++ b/src/ICSharpCode.SharpZipLib/Core/Exceptions/StreamUnsupportedException.cs @@ -0,0 +1,49 @@ +using System; +using System.Runtime.Serialization; + +namespace ICSharpCode.SharpZipLib +{ + /// + /// Indicates that the input stream could not decoded due to known library incompability or missing features + /// + [Serializable] + public class StreamUnsupportedException : StreamDecodingException + { + private const string GenericMessage = "Input stream is in a unsupported format"; + + /// + /// Initializes a new instance of the StreamUnsupportedException with a generic message + /// + public StreamUnsupportedException() : base(GenericMessage) { } + + /// + /// Initializes a new instance of the StreamUnsupportedException class with a specified error message. + /// + /// A message describing the exception. + public StreamUnsupportedException(string message) : base(message) { } + + /// + /// Initializes a new instance of the StreamUnsupportedException class with a specified + /// error message and a reference to the inner exception that is the cause of this exception. + /// + /// A message describing the exception. + /// The inner exception + public StreamUnsupportedException(string message, Exception innerException) : base(message, innerException) { } + + /// + /// Initializes a new instance of the StreamUnsupportedException class with serialized data. + /// + /// + /// The System.Runtime.Serialization.SerializationInfo that holds the serialized + /// object data about the exception being thrown. + /// + /// + /// The System.Runtime.Serialization.StreamingContext that contains contextual information + /// about the source or destination. + /// + protected StreamUnsupportedException(SerializationInfo info, StreamingContext context) + : base(info, context) + { + } + } +} diff --git a/src/ICSharpCode.SharpZipLib/Core/Exceptions/UnexpectedEndOfStreamException.cs b/src/ICSharpCode.SharpZipLib/Core/Exceptions/UnexpectedEndOfStreamException.cs new file mode 100644 index 000000000..a35c49f03 --- /dev/null +++ b/src/ICSharpCode.SharpZipLib/Core/Exceptions/UnexpectedEndOfStreamException.cs @@ -0,0 +1,49 @@ +using System; +using System.Runtime.Serialization; + +namespace ICSharpCode.SharpZipLib +{ + /// + /// Indicates that the input stream could not decoded due to the stream ending before enough data had been provided + /// + [Serializable] + public class UnexpectedEndOfStreamException : StreamDecodingException + { + private const string GenericMessage = "Input stream ended unexpectedly"; + + /// + /// Initializes a new instance of the UnexpectedEndOfStreamException with a generic message + /// + public UnexpectedEndOfStreamException() : base(GenericMessage) { } + + /// + /// Initializes a new instance of the UnexpectedEndOfStreamException class with a specified error message. + /// + /// A message describing the exception. + public UnexpectedEndOfStreamException(string message) : base(message) { } + + /// + /// Initializes a new instance of the UnexpectedEndOfStreamException class with a specified + /// error message and a reference to the inner exception that is the cause of this exception. + /// + /// A message describing the exception. + /// The inner exception + public UnexpectedEndOfStreamException(string message, Exception innerException) : base(message, innerException) { } + + /// + /// Initializes a new instance of the UnexpectedEndOfStreamException class with serialized data. + /// + /// + /// The System.Runtime.Serialization.SerializationInfo that holds the serialized + /// object data about the exception being thrown. + /// + /// + /// The System.Runtime.Serialization.StreamingContext that contains contextual information + /// about the source or destination. + /// + protected UnexpectedEndOfStreamException(SerializationInfo info, StreamingContext context) + : base(info, context) + { + } + } +} diff --git a/src/ICSharpCode.SharpZipLib/Core/Exceptions/ValueOutOfRangeException.cs b/src/ICSharpCode.SharpZipLib/Core/Exceptions/ValueOutOfRangeException.cs new file mode 100644 index 000000000..d41cf9882 --- /dev/null +++ b/src/ICSharpCode.SharpZipLib/Core/Exceptions/ValueOutOfRangeException.cs @@ -0,0 +1,66 @@ +using System; +using System.Runtime.Serialization; + +namespace ICSharpCode.SharpZipLib +{ + /// + /// Indicates that a value was outside of the expected range when decoding an input stream + /// + [Serializable] + public class ValueOutOfRangeException : StreamDecodingException + { + /// + /// Initializes a new instance of the ValueOutOfRangeException class naming the causing variable + /// + /// Name of the variable, use: nameof() + public ValueOutOfRangeException(string nameOfValue) + : base($"{nameOfValue} out of range") { } + + /// + /// Initializes a new instance of the ValueOutOfRangeException class naming the causing variable, + /// it's current value and expected range. + /// + /// Name of the variable, use: nameof() + /// The invalid value + /// Expected maximum value + /// Expected minimum value + public ValueOutOfRangeException(string nameOfValue, long value, long maxValue, long minValue = 0) + : this(nameOfValue, value.ToString(), maxValue.ToString(), minValue.ToString()) { } + + /// + /// Initializes a new instance of the ValueOutOfRangeException class naming the causing variable, + /// it's current value and expected range. + /// + /// Name of the variable, use: nameof() + /// The invalid value + /// Expected maximum value + /// Expected minimum value + public ValueOutOfRangeException(string nameOfValue, string value, string maxValue, string minValue = "0") : + base($"{nameOfValue} out of range: {value}, should be {minValue}..{maxValue}") + { } + + private ValueOutOfRangeException() + { + } + + private ValueOutOfRangeException(string message, Exception innerException) : base(message, innerException) + { + } + + /// + /// Initializes a new instance of the ValueOutOfRangeException class with serialized data. + /// + /// + /// The System.Runtime.Serialization.SerializationInfo that holds the serialized + /// object data about the exception being thrown. + /// + /// + /// The System.Runtime.Serialization.StreamingContext that contains contextual information + /// about the source or destination. + /// + protected ValueOutOfRangeException(SerializationInfo info, StreamingContext context) + : base(info, context) + { + } + } +} diff --git a/src/ICSharpCode.SharpZipLib/Core/FileSystemScanner.cs b/src/ICSharpCode.SharpZipLib/Core/FileSystemScanner.cs index 2a375b7d2..427e7d895 100644 --- a/src/ICSharpCode.SharpZipLib/Core/FileSystemScanner.cs +++ b/src/ICSharpCode.SharpZipLib/Core/FileSystemScanner.cs @@ -3,12 +3,14 @@ namespace ICSharpCode.SharpZipLib.Core { #region EventArgs + /// /// Event arguments for scanning. /// public class ScanEventArgs : EventArgs { #region Constructors + /// /// Initialise a new instance of /// @@ -17,27 +19,32 @@ public ScanEventArgs(string name) { name_ = name; } - #endregion + + #endregion Constructors /// /// The file or directory name for this event. /// - public string Name { + public string Name + { get { return name_; } } /// /// Get set a value indicating if scanning should continue or not. /// - public bool ContinueRunning { + public bool ContinueRunning + { get { return continueRunning_; } set { continueRunning_ = value; } } #region Instance Fields - string name_; - bool continueRunning_ = true; - #endregion + + private string name_; + private bool continueRunning_ = true; + + #endregion Instance Fields } /// @@ -46,6 +53,7 @@ public bool ContinueRunning { public class ProgressEventArgs : EventArgs { #region Constructors + /// /// Initialise a new instance of /// @@ -58,19 +66,22 @@ public ProgressEventArgs(string name, long processed, long target) processed_ = processed; target_ = target; } - #endregion + + #endregion Constructors /// /// The name for this event if known. /// - public string Name { + public string Name + { get { return name_; } } /// - /// Get set a value indicating wether scanning should continue or not. + /// Get set a value indicating whether scanning should continue or not. /// - public bool ContinueRunning { + public bool ContinueRunning + { get { return continueRunning_; } set { continueRunning_ = value; } } @@ -79,12 +90,17 @@ public bool ContinueRunning { /// Get a percentage representing how much of the has been processed /// /// 0.0 to 100.0 percent; 0 if target is not known. - public float PercentComplete { - get { + public float PercentComplete + { + get + { float result; - if (target_ <= 0) { + if (target_ <= 0) + { result = 0; - } else { + } + else + { result = ((float)processed_ / (float)target_) * 100.0f; } return result; @@ -94,7 +110,8 @@ public float PercentComplete { /// /// The number of bytes processed so far /// - public long Processed { + public long Processed + { get { return processed_; } } @@ -102,16 +119,19 @@ public long Processed { /// The number of bytes to process. /// /// Target may be 0 or negative if the value isnt known. - public long Target { + public long Target + { get { return target_; } } #region Instance Fields - string name_; - long processed_; - long target_; - bool continueRunning_ = true; - #endregion + + private string name_; + private long processed_; + private long target_; + private bool continueRunning_ = true; + + #endregion Instance Fields } /// @@ -120,6 +140,7 @@ public long Target { public class DirectoryEventArgs : ScanEventArgs { #region Constructors + /// /// Initialize an instance of . /// @@ -130,20 +151,24 @@ public DirectoryEventArgs(string name, bool hasMatchingFiles) { hasMatchingFiles_ = hasMatchingFiles; } - #endregion + + #endregion Constructors /// /// Get a value indicating if the directory contains any matching files or not. /// - public bool HasMatchingFiles { + public bool HasMatchingFiles + { get { return hasMatchingFiles_; } } - readonly + private readonly #region Instance Fields + bool hasMatchingFiles_; - #endregion + + #endregion Instance Fields } /// @@ -152,6 +177,7 @@ public bool HasMatchingFiles { public class ScanFailureEventArgs : EventArgs { #region Constructors + /// /// Initialise a new instance of /// @@ -163,40 +189,47 @@ public ScanFailureEventArgs(string name, Exception e) exception_ = e; continueRunning_ = true; } - #endregion + + #endregion Constructors /// /// The applicable name. /// - public string Name { + public string Name + { get { return name_; } } /// /// The applicable exception. /// - public Exception Exception { + public Exception Exception + { get { return exception_; } } /// - /// Get / set a value indicating wether scanning should continue. + /// Get / set a value indicating whether scanning should continue. /// - public bool ContinueRunning { + public bool ContinueRunning + { get { return continueRunning_; } set { continueRunning_ = value; } } #region Instance Fields - string name_; - Exception exception_; - bool continueRunning_; - #endregion + + private string name_; + private Exception exception_; + private bool continueRunning_; + + #endregion Instance Fields } - #endregion + #endregion EventArgs #region Delegates + /// /// Delegate invoked before starting to process a file. /// @@ -231,7 +264,8 @@ public bool ContinueRunning { /// The source of the event /// The event arguments. public delegate void FileFailureHandler(object sender, ScanFailureEventArgs e); - #endregion + + #endregion Delegates /// /// FileSystemScanner provides facilities scanning of files and directories. @@ -239,6 +273,7 @@ public bool ContinueRunning { public class FileSystemScanner { #region Constructors + /// /// Initialise a new instance of /// @@ -278,9 +313,11 @@ public FileSystemScanner(IScanFilter fileFilter, IScanFilter directoryFilter) fileFilter_ = fileFilter; directoryFilter_ = directoryFilter; } - #endregion + + #endregion Constructors #region Delegates + /// /// Delegate to invoke when a directory is processed. /// @@ -305,18 +342,20 @@ public FileSystemScanner(IScanFilter fileFilter, IScanFilter directoryFilter) /// Delegate to invoke when a file failure is detected. /// public FileFailureHandler FileFailure; - #endregion + + #endregion Delegates /// /// Raise the DirectoryFailure event. /// /// The directory name. /// The exception detected. - bool OnDirectoryFailure(string directory, Exception e) + private bool OnDirectoryFailure(string directory, Exception e) { DirectoryFailureHandler handler = DirectoryFailure; bool result = (handler != null); - if (result) { + if (result) + { var args = new ScanFailureEventArgs(directory, e); handler(this, args); alive_ = args.ContinueRunning; @@ -329,13 +368,14 @@ bool OnDirectoryFailure(string directory, Exception e) /// /// The file name. /// The exception detected. - bool OnFileFailure(string file, Exception e) + private bool OnFileFailure(string file, Exception e) { FileFailureHandler handler = FileFailure; bool result = (handler != null); - if (result) { + if (result) + { var args = new ScanFailureEventArgs(file, e); FileFailure(this, args); alive_ = args.ContinueRunning; @@ -347,11 +387,12 @@ bool OnFileFailure(string file, Exception e) /// Raise the ProcessFile event. /// /// The file name. - void OnProcessFile(string file) + private void OnProcessFile(string file) { ProcessFileHandler handler = ProcessFile; - if (handler != null) { + if (handler != null) + { var args = new ScanEventArgs(file); handler(this, args); alive_ = args.ContinueRunning; @@ -362,11 +403,12 @@ void OnProcessFile(string file) /// Raise the complete file event /// /// The file name - void OnCompleteFile(string file) + private void OnCompleteFile(string file) { CompletedFileHandler handler = CompletedFile; - if (handler != null) { + if (handler != null) + { var args = new ScanEventArgs(file); handler(this, args); alive_ = args.ContinueRunning; @@ -378,11 +420,12 @@ void OnCompleteFile(string file) /// /// The directory name. /// Flag indicating if the directory has matching files. - void OnProcessDirectory(string directory, bool hasMatchingFiles) + private void OnProcessDirectory(string directory, bool hasMatchingFiles) { EventHandler handler = ProcessDirectory; - if (handler != null) { + if (handler != null) + { var args = new DirectoryEventArgs(directory, hasMatchingFiles); handler(this, args); alive_ = args.ContinueRunning; @@ -400,57 +443,80 @@ public void Scan(string directory, bool recurse) ScanDir(directory, recurse); } - void ScanDir(string directory, bool recurse) + private void ScanDir(string directory, bool recurse) { - - try { + try + { string[] names = System.IO.Directory.GetFiles(directory); bool hasMatch = false; - for (int fileIndex = 0; fileIndex < names.Length; ++fileIndex) { - if (!fileFilter_.IsMatch(names[fileIndex])) { + for (int fileIndex = 0; fileIndex < names.Length; ++fileIndex) + { + if (!fileFilter_.IsMatch(names[fileIndex])) + { names[fileIndex] = null; - } else { + } + else + { hasMatch = true; } } OnProcessDirectory(directory, hasMatch); - if (alive_ && hasMatch) { - foreach (string fileName in names) { - try { - if (fileName != null) { + if (alive_ && hasMatch) + { + foreach (string fileName in names) + { + try + { + if (fileName != null) + { OnProcessFile(fileName); - if (!alive_) { + if (!alive_) + { break; } } - } catch (Exception e) { - if (!OnFileFailure(fileName, e)) { + } + catch (Exception e) + { + if (!OnFileFailure(fileName, e)) + { throw; } } } } - } catch (Exception e) { - if (!OnDirectoryFailure(directory, e)) { + } + catch (Exception e) + { + if (!OnDirectoryFailure(directory, e)) + { throw; } } - if (alive_ && recurse) { - try { + if (alive_ && recurse) + { + try + { string[] names = System.IO.Directory.GetDirectories(directory); - foreach (string fulldir in names) { - if ((directoryFilter_ == null) || (directoryFilter_.IsMatch(fulldir))) { + foreach (string fulldir in names) + { + if ((directoryFilter_ == null) || (directoryFilter_.IsMatch(fulldir))) + { ScanDir(fulldir, true); - if (!alive_) { + if (!alive_) + { break; } } } - } catch (Exception e) { - if (!OnDirectoryFailure(directory, e)) { + } + catch (Exception e) + { + if (!OnDirectoryFailure(directory, e)) + { throw; } } @@ -458,18 +524,22 @@ void ScanDir(string directory, bool recurse) } #region Instance Fields + /// /// The file filter currently in use. /// - IScanFilter fileFilter_; + private IScanFilter fileFilter_; + /// /// The directory filter currently in use. /// - IScanFilter directoryFilter_; + private IScanFilter directoryFilter_; + /// /// Flag indicating if scanning should continue running. /// - bool alive_; - #endregion + private bool alive_; + + #endregion Instance Fields } } diff --git a/src/ICSharpCode.SharpZipLib/Core/InflaterPool.cs b/src/ICSharpCode.SharpZipLib/Core/InflaterPool.cs new file mode 100644 index 000000000..39db32e8c --- /dev/null +++ b/src/ICSharpCode.SharpZipLib/Core/InflaterPool.cs @@ -0,0 +1,66 @@ +using System; +using System.Collections.Concurrent; +using ICSharpCode.SharpZipLib.Zip.Compression; + +namespace ICSharpCode.SharpZipLib.Core +{ + /// + /// Pool for instances as they can be costly due to byte array allocations. + /// + internal sealed class InflaterPool + { + private readonly ConcurrentQueue noHeaderPool = new ConcurrentQueue(); + private readonly ConcurrentQueue headerPool = new ConcurrentQueue(); + + internal static InflaterPool Instance { get; } = new InflaterPool(); + + private InflaterPool() + { + } + + internal Inflater Rent(bool noHeader = false) + { + if (SharpZipLibOptions.InflaterPoolSize <= 0) + { + return new Inflater(noHeader); + } + + var pool = GetPool(noHeader); + + PooledInflater inf; + if (pool.TryDequeue(out var inflater)) + { + inf = inflater; + inf.Reset(); + } + else + { + inf = new PooledInflater(noHeader); + } + + return inf; + } + + internal void Return(Inflater inflater) + { + if (SharpZipLibOptions.InflaterPoolSize <= 0) + { + return; + } + + if (!(inflater is PooledInflater pooledInflater)) + { + throw new ArgumentException("Returned inflater was not a pooled one"); + } + + var pool = GetPool(inflater.noHeader); + if (pool.Count < SharpZipLibOptions.InflaterPoolSize) + { + pooledInflater.Reset(); + pool.Enqueue(pooledInflater); + } + } + + private ConcurrentQueue GetPool(bool noHeader) => noHeader ? noHeaderPool : headerPool; + } +} diff --git a/src/ICSharpCode.SharpZipLib/Core/InvalidNameException.cs b/src/ICSharpCode.SharpZipLib/Core/InvalidNameException.cs new file mode 100644 index 000000000..6647631bd --- /dev/null +++ b/src/ICSharpCode.SharpZipLib/Core/InvalidNameException.cs @@ -0,0 +1,53 @@ +using System; +using System.Runtime.Serialization; + +namespace ICSharpCode.SharpZipLib.Core +{ + /// + /// InvalidNameException is thrown for invalid names such as directory traversal paths and names with invalid characters + /// + [Serializable] + public class InvalidNameException : SharpZipBaseException + { + /// + /// Initializes a new instance of the InvalidNameException class with a default error message. + /// + public InvalidNameException() : base("An invalid name was specified") + { + } + + /// + /// Initializes a new instance of the InvalidNameException class with a specified error message. + /// + /// A message describing the exception. + public InvalidNameException(string message) : base(message) + { + } + + /// + /// Initializes a new instance of the InvalidNameException class with a specified + /// error message and a reference to the inner exception that is the cause of this exception. + /// + /// A message describing the exception. + /// The inner exception + public InvalidNameException(string message, Exception innerException) : base(message, innerException) + { + } + + /// + /// Initializes a new instance of the InvalidNameException class with serialized data. + /// + /// + /// The System.Runtime.Serialization.SerializationInfo that holds the serialized + /// object data about the exception being thrown. + /// + /// + /// The System.Runtime.Serialization.StreamingContext that contains contextual information + /// about the source or destination. + /// + protected InvalidNameException(SerializationInfo info, StreamingContext context) + : base(info, context) + { + } + } +} diff --git a/src/ICSharpCode.SharpZipLib/Core/NameFilter.cs b/src/ICSharpCode.SharpZipLib/Core/NameFilter.cs index a2af22257..57751891c 100644 --- a/src/ICSharpCode.SharpZipLib/Core/NameFilter.cs +++ b/src/ICSharpCode.SharpZipLib/Core/NameFilter.cs @@ -1,5 +1,4 @@ using System; -using System.Collections; using System.Collections.Generic; using System.Text; using System.Text.RegularExpressions; @@ -13,7 +12,7 @@ namespace ICSharpCode.SharpZipLib.Core /// To include a semi-colon it may be quoted as in \;. Each expression can be prefixed by a plus '+' sign or /// a minus '-' sign to denote the expression is intended to include or exclude names. /// If neither a plus or minus sign is found include is the default. - /// A given name is tested for inclusion before checking exclusions. Only names matching an include spec + /// A given name is tested for inclusion before checking exclusions. Only names matching an include spec /// and not matching an exclude spec are deemed to match the filter. /// An empty filter matches any name. /// @@ -23,6 +22,7 @@ namespace ICSharpCode.SharpZipLib.Core public class NameFilter : IScanFilter { #region Constructors + /// /// Construct an instance based on the filter expression passed /// @@ -34,7 +34,8 @@ public NameFilter(string filter) exclusions_ = new List(); Compile(); } - #endregion + + #endregion Constructors /// /// Test a string to see if it is a valid regular expression. @@ -44,9 +45,12 @@ public NameFilter(string filter) public static bool IsValidExpression(string expression) { bool result = true; - try { + try + { var exp = new Regex(expression, RegexOptions.IgnoreCase | RegexOptions.Singleline); - } catch (ArgumentException) { + } + catch (ArgumentException) + { result = false; } return result; @@ -61,18 +65,27 @@ public static bool IsValidFilterExpression(string toTest) { bool result = true; - try { - if (toTest != null) { + try + { + if (toTest != null) + { string[] items = SplitQuoted(toTest); - for (int i = 0; i < items.Length; ++i) { - if ((items[i] != null) && (items[i].Length > 0)) { + for (int i = 0; i < items.Length; ++i) + { + if ((items[i] != null) && (items[i].Length > 0)) + { string toCompile; - if (items[i][0] == '+') { + if (items[i][0] == '+') + { toCompile = items[i].Substring(1, items[i].Length - 1); - } else if (items[i][0] == '-') { + } + else if (items[i][0] == '-') + { toCompile = items[i].Substring(1, items[i].Length - 1); - } else { + } + else + { toCompile = items[i]; } @@ -80,7 +93,9 @@ public static bool IsValidFilterExpression(string toTest) } } } - } catch (ArgumentException) { + } + catch (ArgumentException) + { result = false; } @@ -91,7 +106,7 @@ public static bool IsValidFilterExpression(string toTest) /// Split a string into its component pieces /// /// The original string - /// Returns an array of values containing the individual filter elements. + /// Returns an array of values containing the individual filter elements. public static string[] SplitQuoted(string original) { char escape = '\\'; @@ -99,17 +114,23 @@ public static string[] SplitQuoted(string original) var result = new List(); - if (!string.IsNullOrEmpty(original)) { + if (!string.IsNullOrEmpty(original)) + { int endIndex = -1; var b = new StringBuilder(); - while (endIndex < original.Length) { + while (endIndex < original.Length) + { endIndex += 1; - if (endIndex >= original.Length) { + if (endIndex >= original.Length) + { result.Add(b.ToString()); - } else if (original[endIndex] == escape) { + } + else if (original[endIndex] == escape) + { endIndex += 1; - if (endIndex >= original.Length) { + if (endIndex >= original.Length) + { throw new ArgumentException("Missing terminating escape character", nameof(original)); } // include escape if this is not an escaped separator @@ -117,11 +138,16 @@ public static string[] SplitQuoted(string original) b.Append(escape); b.Append(original[endIndex]); - } else { - if (Array.IndexOf(separators, original[endIndex]) >= 0) { + } + else + { + if (Array.IndexOf(separators, original[endIndex]) >= 0) + { result.Add(b.ToString()); b.Length = 0; - } else { + } + else + { b.Append(original[endIndex]); } } @@ -148,11 +174,16 @@ public override string ToString() public bool IsIncluded(string name) { bool result = false; - if (inclusions_.Count == 0) { + if (inclusions_.Count == 0) + { result = true; - } else { - foreach (Regex r in inclusions_) { - if (r.IsMatch(name)) { + } + else + { + foreach (Regex r in inclusions_) + { + if (r.IsMatch(name)) + { result = true; break; } @@ -169,8 +200,10 @@ public bool IsIncluded(string name) public bool IsExcluded(string name) { bool result = false; - foreach (Regex r in exclusions_) { - if (r.IsMatch(name)) { + foreach (Regex r in exclusions_) + { + if (r.IsMatch(name)) + { result = true; break; } @@ -179,6 +212,7 @@ public bool IsExcluded(string name) } #region IScanFilter Members + /// /// Test a value to see if it matches the filter. /// @@ -188,39 +222,51 @@ public bool IsMatch(string name) { return (IsIncluded(name) && !IsExcluded(name)); } - #endregion + + #endregion IScanFilter Members /// /// Compile this filter. /// - void Compile() + private void Compile() { // TODO: Check to see if combining RE's makes it faster/smaller. // simple scheme would be to have one RE for inclusion and one for exclusion. - if (filter_ == null) { + if (filter_ == null) + { return; } string[] items = SplitQuoted(filter_); - for (int i = 0; i < items.Length; ++i) { - if ((items[i] != null) && (items[i].Length > 0)) { + for (int i = 0; i < items.Length; ++i) + { + if ((items[i] != null) && (items[i].Length > 0)) + { bool include = (items[i][0] != '-'); string toCompile; - if (items[i][0] == '+') { + if (items[i][0] == '+') + { toCompile = items[i].Substring(1, items[i].Length - 1); - } else if (items[i][0] == '-') { + } + else if (items[i][0] == '-') + { toCompile = items[i].Substring(1, items[i].Length - 1); - } else { + } + else + { toCompile = items[i]; } // NOTE: Regular expressions can fail to compile here for a number of reasons that cause an exception // these are left unhandled here as the caller is responsible for ensuring all is valid. // several functions IsValidFilterExpression and IsValidExpression are provided for such checking - if (include) { + if (include) + { inclusions_.Add(new Regex(toCompile, RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.Singleline)); - } else { + } + else + { exclusions_.Add(new Regex(toCompile, RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.Singleline)); } } @@ -228,9 +274,11 @@ void Compile() } #region Instance Fields - string filter_; - List inclusions_; - List exclusions_; - #endregion + + private string filter_; + private List inclusions_; + private List exclusions_; + + #endregion Instance Fields } } diff --git a/src/ICSharpCode.SharpZipLib/Core/PathFilter.cs b/src/ICSharpCode.SharpZipLib/Core/PathFilter.cs index 76a40bdde..e70109c2c 100644 --- a/src/ICSharpCode.SharpZipLib/Core/PathFilter.cs +++ b/src/ICSharpCode.SharpZipLib/Core/PathFilter.cs @@ -11,6 +11,7 @@ namespace ICSharpCode.SharpZipLib.Core public class PathFilter : IScanFilter { #region Constructors + /// /// Initialise a new instance of . /// @@ -19,9 +20,11 @@ public PathFilter(string filter) { nameFilter_ = new NameFilter(filter); } - #endregion + + #endregion Constructors #region IScanFilter Members + /// /// Test a name to see if it matches the filter. /// @@ -32,19 +35,23 @@ public virtual bool IsMatch(string name) { bool result = false; - if (name != null) { + if (name != null) + { string cooked = (name.Length > 0) ? Path.GetFullPath(name) : ""; result = nameFilter_.IsMatch(cooked); } return result; } - readonly - #endregion + private readonly + + #endregion IScanFilter Members #region Instance Fields + NameFilter nameFilter_; - #endregion + + #endregion Instance Fields } /// @@ -54,6 +61,7 @@ public virtual bool IsMatch(string name) public class ExtendedPathFilter : PathFilter { #region Constructors + /// /// Initialise a new instance of ExtendedPathFilter. /// @@ -100,9 +108,11 @@ public ExtendedPathFilter(string filter, MinDate = minDate; MaxDate = maxDate; } - #endregion + + #endregion Constructors #region IScanFilter Members + /// /// Test a filename to see if it matches the filter. /// @@ -113,7 +123,8 @@ public override bool IsMatch(string name) { bool result = base.IsMatch(name); - if (result) { + if (result) + { var fileInfo = new FileInfo(name); result = (MinSize <= fileInfo.Length) && @@ -124,18 +135,23 @@ public override bool IsMatch(string name) } return result; } - #endregion + + #endregion IScanFilter Members #region Properties + /// /// Get/set the minimum size/length for a file that will match this filter. /// /// The default value is zero. /// value is less than zero; greater than - public long MinSize { + public long MinSize + { get { return minSize_; } - set { - if ((value < 0) || (maxSize_ < value)) { + set + { + if ((value < 0) || (maxSize_ < value)) + { throw new ArgumentOutOfRangeException(nameof(value)); } @@ -148,10 +164,13 @@ public long MinSize { /// /// The default value is /// value is less than zero or less than - public long MaxSize { + public long MaxSize + { get { return maxSize_; } - set { - if ((value < 0) || (minSize_ > value)) { + set + { + if ((value < 0) || (minSize_ > value)) + { throw new ArgumentOutOfRangeException(nameof(value)); } @@ -163,13 +182,17 @@ public long MaxSize { /// Get/set the minimum value that will match for this filter. /// /// Files with a LastWrite time less than this value are excluded by the filter. - public DateTime MinDate { - get { + public DateTime MinDate + { + get + { return minDate_; } - set { - if (value > maxDate_) { + set + { + if (value > maxDate_) + { throw new ArgumentOutOfRangeException(nameof(value), "Exceeds MaxDate"); } @@ -181,27 +204,34 @@ public DateTime MinDate { /// Get/set the maximum value that will match for this filter. /// /// Files with a LastWrite time greater than this value are excluded by the filter. - public DateTime MaxDate { - get { + public DateTime MaxDate + { + get + { return maxDate_; } - set { - if (minDate_ > value) { + set + { + if (minDate_ > value) + { throw new ArgumentOutOfRangeException(nameof(value), "Exceeds MinDate"); } maxDate_ = value; } } - #endregion + + #endregion Properties #region Instance Fields - long minSize_; - long maxSize_ = long.MaxValue; - DateTime minDate_ = DateTime.MinValue; - DateTime maxDate_ = DateTime.MaxValue; - #endregion + + private long minSize_; + private long maxSize_ = long.MaxValue; + private DateTime minDate_ = DateTime.MinValue; + private DateTime maxDate_ = DateTime.MaxValue; + + #endregion Instance Fields } /// @@ -211,7 +241,6 @@ public DateTime MaxDate { [Obsolete("Use ExtendedPathFilter instead")] public class NameAndSizeFilter : PathFilter { - /// /// Initialise a new instance of NameAndSizeFilter. /// @@ -234,7 +263,8 @@ public override bool IsMatch(string name) { bool result = base.IsMatch(name); - if (result) { + if (result) + { var fileInfo = new FileInfo(name); long length = fileInfo.Length; result = @@ -247,10 +277,13 @@ public override bool IsMatch(string name) /// /// Get/set the minimum size for a file that will match this filter. /// - public long MinSize { + public long MinSize + { get { return minSize_; } - set { - if ((value < 0) || (maxSize_ < value)) { + set + { + if ((value < 0) || (maxSize_ < value)) + { throw new ArgumentOutOfRangeException(nameof(value)); } @@ -261,10 +294,13 @@ public long MinSize { /// /// Get/set the maximum size for a file that will match this filter. /// - public long MaxSize { + public long MaxSize + { get { return maxSize_; } - set { - if ((value < 0) || (minSize_ > value)) { + set + { + if ((value < 0) || (minSize_ > value)) + { throw new ArgumentOutOfRangeException(nameof(value)); } @@ -273,8 +309,10 @@ public long MaxSize { } #region Instance Fields - long minSize_; - long maxSize_ = long.MaxValue; - #endregion + + private long minSize_; + private long maxSize_ = long.MaxValue; + + #endregion Instance Fields } } diff --git a/src/ICSharpCode.SharpZipLib/Core/PathUtils.cs b/src/ICSharpCode.SharpZipLib/Core/PathUtils.cs new file mode 100644 index 000000000..52f01d079 --- /dev/null +++ b/src/ICSharpCode.SharpZipLib/Core/PathUtils.cs @@ -0,0 +1,57 @@ +using System; +using System.IO; +using System.Linq; + +namespace ICSharpCode.SharpZipLib.Core +{ + /// + /// PathUtils provides simple utilities for handling paths. + /// + public static class PathUtils + { + /// + /// Remove any path root present in the path + /// + /// A containing path information. + /// The path with the root removed if it was present; path otherwise. + public static string DropPathRoot(string path) + { + // No need to drop anything + if (path == string.Empty) return path; + + var invalidChars = Path.GetInvalidPathChars(); + // If the first character after the root is a ':', .NET < 4.6.2 throws + var cleanRootSep = path.Length >= 3 && path[1] == ':' && path[2] == ':'; + + // Replace any invalid path characters with '_' to prevent Path.GetPathRoot from throwing. + // Only pass the first 258 (should be 260, but that still throws for some reason) characters + // as .NET < 4.6.2 throws on longer paths + var cleanPath = new string(path.Take(258) + .Select( (c, i) => invalidChars.Contains(c) || (i == 2 && cleanRootSep) ? '_' : c).ToArray()); + + var stripLength = Path.GetPathRoot(cleanPath)?.Length ?? 0; + while (path.Length > stripLength && (path[stripLength] == '/' || path[stripLength] == '\\')) stripLength++; + return path.Substring(stripLength); + } + + /// + /// Returns a random file name in the users temporary directory, or in directory of if specified + /// + /// If specified, used as the base file name for the temporary file + /// Returns a temporary file name + public static string GetTempFileName(string original = null) + { + string fileName; + var tempPath = Path.GetTempPath(); + + do + { + fileName = original == null + ? Path.Combine(tempPath, Path.GetRandomFileName()) + : $"{original}.{Path.GetRandomFileName()}"; + } while (File.Exists(fileName)); + + return fileName; + } + } +} diff --git a/src/ICSharpCode.SharpZipLib/Core/StreamUtils.cs b/src/ICSharpCode.SharpZipLib/Core/StreamUtils.cs index 2f150adbb..58ffa2070 100644 --- a/src/ICSharpCode.SharpZipLib/Core/StreamUtils.cs +++ b/src/ICSharpCode.SharpZipLib/Core/StreamUtils.cs @@ -1,12 +1,14 @@ using System; using System.IO; +using System.Threading; +using System.Threading.Tasks; namespace ICSharpCode.SharpZipLib.Core { /// /// Provides simple " utilities. /// - public sealed class StreamUtils + public static class StreamUtils { /// /// Read from a ensuring all the required data is read. @@ -14,7 +16,7 @@ public sealed class StreamUtils /// The stream to read. /// The buffer to fill. /// - static public void ReadFully(Stream stream, byte[] buffer) + public static void ReadFully(Stream stream, byte[] buffer) { ReadFully(stream, buffer, 0, buffer.Length); } @@ -29,28 +31,34 @@ static public void ReadFully(Stream stream, byte[] buffer) /// Required parameter is null /// and or are invalid. /// End of stream is encountered before all the data has been read. - static public void ReadFully(Stream stream, byte[] buffer, int offset, int count) + public static void ReadFully(Stream stream, byte[] buffer, int offset, int count) { - if (stream == null) { + if (stream == null) + { throw new ArgumentNullException(nameof(stream)); } - if (buffer == null) { + if (buffer == null) + { throw new ArgumentNullException(nameof(buffer)); } // Offset can equal length when buffer and count are 0. - if ((offset < 0) || (offset > buffer.Length)) { + if ((offset < 0) || (offset > buffer.Length)) + { throw new ArgumentOutOfRangeException(nameof(offset)); } - if ((count < 0) || (offset + count > buffer.Length)) { + if ((count < 0) || (offset + count > buffer.Length)) + { throw new ArgumentOutOfRangeException(nameof(count)); } - while (count > 0) { + while (count > 0) + { int readCount = stream.Read(buffer, offset, count); - if (readCount <= 0) { + if (readCount <= 0) + { throw new EndOfStreamException(); } offset += readCount; @@ -58,38 +66,94 @@ static public void ReadFully(Stream stream, byte[] buffer, int offset, int count } } + /// + /// Read as much data as possible from a ", up to the requested number of bytes + /// + /// The stream to read data from. + /// The buffer to store data in. + /// The offset at which to begin storing data. + /// The number of bytes of data to store. + /// Required parameter is null + /// and or are invalid. + public static int ReadRequestedBytes(Stream stream, byte[] buffer, int offset, int count) + { + if (stream == null) + { + throw new ArgumentNullException(nameof(stream)); + } + + if (buffer == null) + { + throw new ArgumentNullException(nameof(buffer)); + } + + // Offset can equal length when buffer and count are 0. + if ((offset < 0) || (offset > buffer.Length)) + { + throw new ArgumentOutOfRangeException(nameof(offset)); + } + + if ((count < 0) || (offset + count > buffer.Length)) + { + throw new ArgumentOutOfRangeException(nameof(count)); + } + + int totalReadCount = 0; + while (count > 0) + { + int readCount = stream.Read(buffer, offset, count); + if (readCount <= 0) + { + break; + } + offset += readCount; + count -= readCount; + totalReadCount += readCount; + } + + return totalReadCount; + } + /// /// Copy the contents of one to another. /// /// The stream to source data from. /// The stream to write data to. /// The buffer to use during copying. - static public void Copy(Stream source, Stream destination, byte[] buffer) + public static void Copy(Stream source, Stream destination, byte[] buffer) { - if (source == null) { + if (source == null) + { throw new ArgumentNullException(nameof(source)); } - if (destination == null) { + if (destination == null) + { throw new ArgumentNullException(nameof(destination)); } - if (buffer == null) { + if (buffer == null) + { throw new ArgumentNullException(nameof(buffer)); } // Ensure a reasonable size of buffer is used without being prohibitive. - if (buffer.Length < 128) { + if (buffer.Length < 128) + { throw new ArgumentException("Buffer is too small", nameof(buffer)); } bool copying = true; - while (copying) { + while (copying) + { int bytesRead = source.Read(buffer, 0, buffer.Length); - if (bytesRead > 0) { + if (bytesRead > 0) + { destination.Write(buffer, 0, bytesRead); - } else { + } + else + { destination.Flush(); copying = false; } @@ -107,7 +171,7 @@ static public void Copy(Stream source, Stream destination, byte[] buffer) /// The source for this event. /// The name to use with the event. /// This form is specialised for use within #Zip to support events during archive operations. - static public void Copy(Stream source, Stream destination, + public static void Copy(Stream source, Stream destination, byte[] buffer, ProgressHandler progressHandler, TimeSpan updateInterval, object sender, string name) { Copy(source, destination, buffer, progressHandler, updateInterval, sender, name, -1); @@ -126,29 +190,34 @@ static public void Copy(Stream source, Stream destination, /// A predetermined fixed target value to use with progress updates. /// If the value is negative the target is calculated by looking at the stream. /// This form is specialised for use within #Zip to support events during archive operations. - static public void Copy(Stream source, Stream destination, + public static void Copy(Stream source, Stream destination, byte[] buffer, ProgressHandler progressHandler, TimeSpan updateInterval, object sender, string name, long fixedTarget) { - if (source == null) { + if (source == null) + { throw new ArgumentNullException(nameof(source)); } - if (destination == null) { + if (destination == null) + { throw new ArgumentNullException(nameof(destination)); } - if (buffer == null) { + if (buffer == null) + { throw new ArgumentNullException(nameof(buffer)); } // Ensure a reasonable size of buffer is used without being prohibitive. - if (buffer.Length < 128) { + if (buffer.Length < 128) + { throw new ArgumentException("Buffer is too small", nameof(buffer)); } - if (progressHandler == null) { + if (progressHandler == null) + { throw new ArgumentNullException(nameof(progressHandler)); } @@ -158,9 +227,12 @@ static public void Copy(Stream source, Stream destination, long processed = 0; long target = 0; - if (fixedTarget >= 0) { + if (fixedTarget >= 0) + { target = fixedTarget; - } else if (source.CanSeek) { + } + else if (source.CanSeek) + { target = source.Length - source.Position; } @@ -170,18 +242,23 @@ static public void Copy(Stream source, Stream destination, bool progressFired = true; - while (copying) { + while (copying) + { int bytesRead = source.Read(buffer, 0, buffer.Length); - if (bytesRead > 0) { + if (bytesRead > 0) + { processed += bytesRead; progressFired = false; destination.Write(buffer, 0, bytesRead); - } else { + } + else + { destination.Flush(); copying = false; } - if (DateTime.Now - marker > updateInterval) { + if (DateTime.Now - marker > updateInterval) + { progressFired = true; marker = DateTime.Now; args = new ProgressEventArgs(name, processed, target); @@ -191,18 +268,28 @@ static public void Copy(Stream source, Stream destination, } } - if (!progressFired) { + if (!progressFired) + { args = new ProgressEventArgs(name, processed, target); progressHandler(sender, args); } } - - /// - /// Initialise an instance of - /// - private StreamUtils() + + internal static async Task WriteProcToStreamAsync(this Stream targetStream, MemoryStream bufferStream, Action writeProc, CancellationToken ct) + { + bufferStream.SetLength(0); + writeProc(bufferStream); + bufferStream.Position = 0; + await bufferStream.CopyToAsync(targetStream, 81920, ct).ConfigureAwait(false); + bufferStream.SetLength(0); + } + + internal static async Task WriteProcToStreamAsync(this Stream targetStream, Action writeProc, CancellationToken ct) { - // Do nothing. + using (var ms = new MemoryStream()) + { + await WriteProcToStreamAsync(targetStream, ms, writeProc, ct).ConfigureAwait(false); + } } } } diff --git a/src/ICSharpCode.SharpZipLib/Core/StringBuilderPool.cs b/src/ICSharpCode.SharpZipLib/Core/StringBuilderPool.cs new file mode 100644 index 000000000..a1121f0cc --- /dev/null +++ b/src/ICSharpCode.SharpZipLib/Core/StringBuilderPool.cs @@ -0,0 +1,22 @@ +using System.Collections.Concurrent; +using System.Text; + +namespace ICSharpCode.SharpZipLib.Core +{ + internal class StringBuilderPool + { + public static StringBuilderPool Instance { get; } = new StringBuilderPool(); + private readonly ConcurrentQueue pool = new ConcurrentQueue(); + + public StringBuilder Rent() + { + return pool.TryDequeue(out var builder) ? builder : new StringBuilder(); + } + + public void Return(StringBuilder builder) + { + builder.Clear(); + pool.Enqueue(builder); + } + } +} diff --git a/src/ICSharpCode.SharpZipLib/Core/WindowsPathUtils.cs b/src/ICSharpCode.SharpZipLib/Core/WindowsPathUtils.cs deleted file mode 100644 index d771b3731..000000000 --- a/src/ICSharpCode.SharpZipLib/Core/WindowsPathUtils.cs +++ /dev/null @@ -1,57 +0,0 @@ -namespace ICSharpCode.SharpZipLib.Core -{ - /// - /// WindowsPathUtils provides simple utilities for handling windows paths. - /// - public abstract class WindowsPathUtils - { - /// - /// Initializes a new instance of the class. - /// - internal WindowsPathUtils() - { - } - - /// - /// Remove any path root present in the path - /// - /// A containing path information. - /// The path with the root removed if it was present; path otherwise. - /// Unlike the class the path isnt otherwise checked for validity. - public static string DropPathRoot(string path) - { - string result = path; - - if (!string.IsNullOrEmpty(path)) { - if ((path[0] == '\\') || (path[0] == '/')) { - // UNC name ? - if ((path.Length > 1) && ((path[1] == '\\') || (path[1] == '/'))) { - int index = 2; - int elements = 2; - - // Scan for two separate elements \\machine\share\restofpath - while ((index <= path.Length) && - (((path[index] != '\\') && (path[index] != '/')) || (--elements > 0))) { - index++; - } - - index++; - - if (index < path.Length) { - result = path.Substring(index); - } else { - result = ""; - } - } - } else if ((path.Length > 1) && (path[1] == ':')) { - int dropCount = 2; - if ((path.Length > 2) && ((path[2] == '\\') || (path[2] == '/'))) { - dropCount = 3; - } - result = result.Remove(0, dropCount); - } - } - return result; - } - } -} diff --git a/src/ICSharpCode.SharpZipLib/Encryption/PkzipClassic.cs b/src/ICSharpCode.SharpZipLib/Encryption/PkzipClassic.cs index 8c1c7517c..1c7bd1f28 100644 --- a/src/ICSharpCode.SharpZipLib/Encryption/PkzipClassic.cs +++ b/src/ICSharpCode.SharpZipLib/Encryption/PkzipClassic.cs @@ -1,12 +1,12 @@ +using ICSharpCode.SharpZipLib.Checksum; using System; using System.Security.Cryptography; -using ICSharpCode.SharpZipLib.Checksum; namespace ICSharpCode.SharpZipLib.Encryption { /// /// PkzipClassic embodies the classic or original encryption facilities used in Pkzip archives. - /// While it has been superceded by more recent and more powerful algorithms, its still in use and + /// While it has been superseded by more recent and more powerful algorithms, its still in use and /// is viable for preventing casual snooping /// public abstract class PkzipClassic : SymmetricAlgorithm @@ -18,11 +18,13 @@ public abstract class PkzipClassic : SymmetricAlgorithm /// A new key value. static public byte[] GenerateKeys(byte[] seed) { - if (seed == null) { + if (seed == null) + { throw new ArgumentNullException(nameof(seed)); } - if (seed.Length == 0) { + if (seed.Length == 0) + { throw new ArgumentException("Length is zero", nameof(seed)); } @@ -32,7 +34,8 @@ static public byte[] GenerateKeys(byte[] seed) 0x34567890 }; - for (int i = 0; i < seed.Length; ++i) { + for (int i = 0; i < seed.Length; ++i) + { newKeys[0] = Crc32.ComputeCrc32(newKeys[0], seed[i]); newKeys[1] = newKeys[1] + (byte)newKeys[0]; newKeys[1] = newKeys[1] * 134775813 + 1; @@ -60,10 +63,10 @@ static public byte[] GenerateKeys(byte[] seed) /// PkzipClassicCryptoBase provides the low level facilities for encryption /// and decryption using the PkzipClassic algorithm. /// - class PkzipClassicCryptoBase + internal class PkzipClassicCryptoBase { /// - /// Transform a single byte + /// Transform a single byte /// /// /// The transformed value @@ -80,11 +83,13 @@ protected byte TransformByte() /// The data use to set the keys from. protected void SetKeys(byte[] keyData) { - if (keyData == null) { + if (keyData == null) + { throw new ArgumentNullException(nameof(keyData)); } - if (keyData.Length != 12) { + if (keyData.Length != 12) + { throw new InvalidOperationException("Key length is not valid"); } @@ -95,8 +100,8 @@ protected void SetKeys(byte[] keyData) } /// - /// Update encryption keys - /// + /// Update encryption keys + /// protected void UpdateKeys(byte ch) { keys[0] = Crc32.ComputeCrc32(keys[0], ch); @@ -116,14 +121,16 @@ protected void Reset() } #region Instance Fields - uint[] keys; - #endregion + + private uint[] keys; + + #endregion Instance Fields } /// /// PkzipClassic CryptoTransform for encryption. /// - class PkzipClassicEncryptCryptoTransform : PkzipClassicCryptoBase, ICryptoTransform + internal class PkzipClassicEncryptCryptoTransform : PkzipClassicCryptoBase, ICryptoTransform { /// /// Initialise a new instance of @@ -151,7 +158,7 @@ public byte[] TransformFinalBlock(byte[] inputBuffer, int inputOffset, int input } /// - /// Transforms the specified region of the input byte array and copies + /// Transforms the specified region of the input byte array and copies /// the resulting transform to the specified region of the output byte array. /// /// The input for which to compute the transform. @@ -162,7 +169,8 @@ public byte[] TransformFinalBlock(byte[] inputBuffer, int inputOffset, int input /// The number of bytes written. public int TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset) { - for (int i = inputOffset; i < inputOffset + inputCount; ++i) { + for (int i = inputOffset; i < inputOffset + inputCount; ++i) + { byte oldbyte = inputBuffer[i]; outputBuffer[outputOffset++] = (byte)(inputBuffer[i] ^ TransformByte()); UpdateKeys(oldbyte); @@ -173,8 +181,10 @@ public int TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, b /// /// Gets a value indicating whether the current transform can be reused. /// - public bool CanReuseTransform { - get { + public bool CanReuseTransform + { + get + { return true; } } @@ -182,8 +192,10 @@ public bool CanReuseTransform { /// /// Gets the size of the input data blocks in bytes. /// - public int InputBlockSize { - get { + public int InputBlockSize + { + get + { return 1; } } @@ -191,8 +203,10 @@ public int InputBlockSize { /// /// Gets the size of the output data blocks in bytes. /// - public int OutputBlockSize { - get { + public int OutputBlockSize + { + get + { return 1; } } @@ -200,13 +214,15 @@ public int OutputBlockSize { /// /// Gets a value indicating whether multiple blocks can be transformed. /// - public bool CanTransformMultipleBlocks { - get { + public bool CanTransformMultipleBlocks + { + get + { return true; } } - #endregion + #endregion ICryptoTransform Members #region IDisposable Members @@ -218,14 +234,13 @@ public void Dispose() Reset(); } - #endregion + #endregion IDisposable Members } - /// /// PkzipClassic CryptoTransform for decryption. /// - class PkzipClassicDecryptCryptoTransform : PkzipClassicCryptoBase, ICryptoTransform + internal class PkzipClassicDecryptCryptoTransform : PkzipClassicCryptoBase, ICryptoTransform { /// /// Initialise a new instance of . @@ -253,7 +268,7 @@ public byte[] TransformFinalBlock(byte[] inputBuffer, int inputOffset, int input } /// - /// Transforms the specified region of the input byte array and copies + /// Transforms the specified region of the input byte array and copies /// the resulting transform to the specified region of the output byte array. /// /// The input for which to compute the transform. @@ -264,7 +279,8 @@ public byte[] TransformFinalBlock(byte[] inputBuffer, int inputOffset, int input /// The number of bytes written. public int TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset) { - for (int i = inputOffset; i < inputOffset + inputCount; ++i) { + for (int i = inputOffset; i < inputOffset + inputCount; ++i) + { var newByte = (byte)(inputBuffer[i] ^ TransformByte()); outputBuffer[outputOffset++] = newByte; UpdateKeys(newByte); @@ -275,8 +291,10 @@ public int TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, b /// /// Gets a value indicating whether the current transform can be reused. /// - public bool CanReuseTransform { - get { + public bool CanReuseTransform + { + get + { return true; } } @@ -284,8 +302,10 @@ public bool CanReuseTransform { /// /// Gets the size of the input data blocks in bytes. /// - public int InputBlockSize { - get { + public int InputBlockSize + { + get + { return 1; } } @@ -293,8 +313,10 @@ public int InputBlockSize { /// /// Gets the size of the output data blocks in bytes. /// - public int OutputBlockSize { - get { + public int OutputBlockSize + { + get + { return 1; } } @@ -302,13 +324,15 @@ public int OutputBlockSize { /// /// Gets a value indicating whether multiple blocks can be transformed. /// - public bool CanTransformMultipleBlocks { - get { + public bool CanTransformMultipleBlocks + { + get + { return true; } } - #endregion + #endregion ICryptoTransform Members #region IDisposable Members @@ -320,11 +344,11 @@ public void Dispose() Reset(); } - #endregion + #endregion IDisposable Members } /// - /// Defines a wrapper object to access the Pkzip algorithm. + /// Defines a wrapper object to access the Pkzip algorithm. /// This class cannot be inherited. /// public sealed class PkzipClassicManaged : PkzipClassic @@ -333,13 +357,17 @@ public sealed class PkzipClassicManaged : PkzipClassic /// Get / set the applicable block size in bits. /// /// The only valid block size is 8. - public override int BlockSize { - get { + public override int BlockSize + { + get + { return 8; } - set { - if (value != 8) { + set + { + if (value != 8) + { throw new CryptographicException("Block size is invalid"); } } @@ -348,8 +376,10 @@ public override int BlockSize { /// /// Get an array of legal key sizes. /// - public override KeySizes[] LegalKeySizes { - get { + public override KeySizes[] LegalKeySizes + { + get + { KeySizes[] keySizes = new KeySizes[1]; keySizes[0] = new KeySizes(12 * 8, 12 * 8, 0); return keySizes; @@ -367,8 +397,10 @@ public override void GenerateIV() /// /// Get an array of legal block sizes. /// - public override KeySizes[] LegalBlockSizes { - get { + public override KeySizes[] LegalBlockSizes + { + get + { KeySizes[] keySizes = new KeySizes[1]; keySizes[0] = new KeySizes(1 * 8, 1 * 8, 0); return keySizes; @@ -378,21 +410,27 @@ public override KeySizes[] LegalBlockSizes { /// /// Get / set the key value applicable. /// - public override byte[] Key { - get { - if (key_ == null) { + public override byte[] Key + { + get + { + if (key_ == null) + { GenerateKey(); } return (byte[])key_.Clone(); } - set { - if (value == null) { + set + { + if (value == null) + { throw new ArgumentNullException(nameof(value)); } - if (value.Length != 12) { + if (value.Length != 12) + { throw new CryptographicException("Key size is illegal"); } @@ -406,8 +444,10 @@ public override byte[] Key { public override void GenerateKey() { key_ = new byte[12]; - var rnd = new Random(); - rnd.NextBytes(key_); + using (var rng = RandomNumberGenerator.Create()) + { + rng.GetBytes(key_); + } } /// @@ -439,7 +479,9 @@ public override ICryptoTransform CreateDecryptor( } #region Instance Fields - byte[] key_; - #endregion + + private byte[] key_; + + #endregion Instance Fields } } diff --git a/src/ICSharpCode.SharpZipLib/Encryption/ZipAESStream.cs b/src/ICSharpCode.SharpZipLib/Encryption/ZipAESStream.cs index 18e7ec6a9..346b5484b 100644 --- a/src/ICSharpCode.SharpZipLib/Encryption/ZipAESStream.cs +++ b/src/ICSharpCode.SharpZipLib/Encryption/ZipAESStream.cs @@ -1,6 +1,10 @@ using System; using System.IO; using System.Security.Cryptography; +using System.Threading; +using System.Threading.Tasks; +using ICSharpCode.SharpZipLib.Core; +using ICSharpCode.SharpZipLib.Zip; namespace ICSharpCode.SharpZipLib.Encryption { @@ -13,7 +17,6 @@ namespace ICSharpCode.SharpZipLib.Encryption /// internal class ZipAESStream : CryptoStream { - /// /// Constructor /// @@ -23,32 +26,41 @@ internal class ZipAESStream : CryptoStream public ZipAESStream(Stream stream, ZipAESTransform transform, CryptoStreamMode mode) : base(stream, transform, mode) { - _stream = stream; _transform = transform; _slideBuffer = new byte[1024]; - _blockAndAuth = CRYPTO_BLOCK_SIZE + AUTH_CODE_LENGTH; - // mode: // CryptoStreamMode.Read means we read from "stream" and pass decrypted to our Read() method. // Write bypasses this stream and uses the Transform directly. - if (mode != CryptoStreamMode.Read) { + if (mode != CryptoStreamMode.Read) + { throw new Exception("ZipAESStream only for read"); } } // The final n bytes of the AES stream contain the Auth Code. - private const int AUTH_CODE_LENGTH = 10; + public const int AUTH_CODE_LENGTH = 10; + + // Blocksize is always 16 here, even for AES-256 which has transform.InputBlockSize of 32. + private const int CRYPTO_BLOCK_SIZE = 16; + + // total length of block + auth code + private const int BLOCK_AND_AUTH = CRYPTO_BLOCK_SIZE + AUTH_CODE_LENGTH; private Stream _stream; private ZipAESTransform _transform; private byte[] _slideBuffer; private int _slideBufStartPos; private int _slideBufFreePos; - // Blocksize is always 16 here, even for AES-256 which has transform.InputBlockSize of 32. - private const int CRYPTO_BLOCK_SIZE = 16; - private int _blockAndAuth; + + // Buffer block transforms to enable partial reads + private byte[] _transformBuffer = null;// new byte[CRYPTO_BLOCK_SIZE]; + private int _transformBufferFreePos; + private int _transformBufferStartPos; + + // Do we have some buffered data available? + private bool HasBufferedData =>_transformBuffer != null && _transformBufferStartPos < _transformBufferFreePos; /// /// Reads a sequence of bytes from the current CryptoStream into buffer, @@ -56,69 +68,153 @@ public ZipAESStream(Stream stream, ZipAESTransform transform, CryptoStreamMode m /// public override int Read(byte[] buffer, int offset, int count) { + // Nothing to do + if (count == 0) + return 0; + + // If we have buffered data, read that first int nBytes = 0; - while (nBytes < count) { + if (HasBufferedData) + { + nBytes = ReadBufferedData(buffer, offset, count); + + // Read all requested data from the buffer + if (nBytes == count) + return nBytes; + + offset += nBytes; + count -= nBytes; + } + + // Read more data from the input, if available + if (_slideBuffer != null) + nBytes += ReadAndTransform(buffer, offset, count); + + return nBytes; + } + + /// + public override Task ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) + { + var readCount = Read(buffer, offset, count); + return Task.FromResult(readCount); + } + + // Read data from the underlying stream and decrypt it + private int ReadAndTransform(byte[] buffer, int offset, int count) + { + int nBytes = 0; + while (nBytes < count) + { + int bytesLeftToRead = count - nBytes; + // Calculate buffer quantities vs read-ahead size, and check for sufficient free space int byteCount = _slideBufFreePos - _slideBufStartPos; // Need to handle final block and Auth Code specially, but don't know total data length. - // Maintain a read-ahead equal to the length of (crypto block + Auth Code). + // Maintain a read-ahead equal to the length of (crypto block + Auth Code). // When that runs out we can detect these final sections. - int lengthToRead = _blockAndAuth - byteCount; - if (_slideBuffer.Length - _slideBufFreePos < lengthToRead) { + int lengthToRead = BLOCK_AND_AUTH - byteCount; + if (_slideBuffer.Length - _slideBufFreePos < lengthToRead) + { // Shift the data to the beginning of the buffer int iTo = 0; - for (int iFrom = _slideBufStartPos; iFrom < _slideBufFreePos; iFrom++, iTo++) { + for (int iFrom = _slideBufStartPos; iFrom < _slideBufFreePos; iFrom++, iTo++) + { _slideBuffer[iTo] = _slideBuffer[iFrom]; } _slideBufFreePos -= _slideBufStartPos; // Note the -= _slideBufStartPos = 0; } - int obtained = _stream.Read(_slideBuffer, _slideBufFreePos, lengthToRead); + int obtained = StreamUtils.ReadRequestedBytes(_stream, _slideBuffer, _slideBufFreePos, lengthToRead); _slideBufFreePos += obtained; // Recalculate how much data we now have byteCount = _slideBufFreePos - _slideBufStartPos; - if (byteCount >= _blockAndAuth) { - // At least a 16 byte block and an auth code remains. - _transform.TransformBlock(_slideBuffer, - _slideBufStartPos, - CRYPTO_BLOCK_SIZE, - buffer, - offset); - nBytes += CRYPTO_BLOCK_SIZE; - offset += CRYPTO_BLOCK_SIZE; - _slideBufStartPos += CRYPTO_BLOCK_SIZE; - } else { + if (byteCount >= BLOCK_AND_AUTH) + { + var read = TransformAndBufferBlock(buffer, offset, bytesLeftToRead, CRYPTO_BLOCK_SIZE); + nBytes += read; + offset += read; + } + else + { // Last round. - if (byteCount > AUTH_CODE_LENGTH) { + if (byteCount > AUTH_CODE_LENGTH) + { // At least one byte of data plus auth code int finalBlock = byteCount - AUTH_CODE_LENGTH; - _transform.TransformBlock(_slideBuffer, - _slideBufStartPos, - finalBlock, - buffer, - offset); - - nBytes += finalBlock; - _slideBufStartPos += finalBlock; - } else if (byteCount < AUTH_CODE_LENGTH) - throw new Exception("Internal error missed auth code"); // Coding bug + nBytes += TransformAndBufferBlock(buffer, offset, bytesLeftToRead, finalBlock); + } + else if (byteCount < AUTH_CODE_LENGTH) + throw new ZipException("Internal error missed auth code"); // Coding bug // Final block done. Check Auth code. byte[] calcAuthCode = _transform.GetAuthCode(); - for (int i = 0; i < AUTH_CODE_LENGTH; i++) { - if (calcAuthCode[i] != _slideBuffer[_slideBufStartPos + i]) { - throw new Exception("AES Authentication Code does not match. This is a super-CRC check on the data in the file after compression and encryption. \r\n" + for (int i = 0; i < AUTH_CODE_LENGTH; i++) + { + if (calcAuthCode[i] != _slideBuffer[_slideBufStartPos + i]) + { + throw new ZipException("AES Authentication Code does not match. This is a super-CRC check on the data in the file after compression and encryption. \r\n" + "The file may be damaged."); } } + // don't need this any more, so use it as a 'complete' flag + _slideBuffer = null; + break; // Reached the auth code } } return nBytes; } + // read some buffered data + private int ReadBufferedData(byte[] buffer, int offset, int count) + { + int copyCount = Math.Min(count, _transformBufferFreePos - _transformBufferStartPos); + + Array.Copy(_transformBuffer, _transformBufferStartPos, buffer, offset, copyCount); + _transformBufferStartPos += copyCount; + + return copyCount; + } + + // Perform the crypto transform, and buffer the data if less than one block has been requested. + private int TransformAndBufferBlock(byte[] buffer, int offset, int count, int blockSize) + { + // If the requested data is greater than one block, transform it directly into the output + // If it's smaller, do it into a temporary buffer and copy the requested part + bool bufferRequired = (blockSize > count); + + if (bufferRequired && _transformBuffer == null) + _transformBuffer = new byte[CRYPTO_BLOCK_SIZE]; + + var targetBuffer = bufferRequired ? _transformBuffer : buffer; + var targetOffset = bufferRequired ? 0 : offset; + + // Transform the data + _transform.TransformBlock(_slideBuffer, + _slideBufStartPos, + blockSize, + targetBuffer, + targetOffset); + + _slideBufStartPos += blockSize; + + if (!bufferRequired) + { + return blockSize; + } + else + { + Array.Copy(_transformBuffer, 0, buffer, offset, count); + _transformBufferStartPos = count; + _transformBufferFreePos = blockSize; + + return count; + } + } + /// /// Writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written. /// diff --git a/src/ICSharpCode.SharpZipLib/Encryption/ZipAESTransform.cs b/src/ICSharpCode.SharpZipLib/Encryption/ZipAESTransform.cs index 22bc14d4a..32c7b8156 100644 --- a/src/ICSharpCode.SharpZipLib/Encryption/ZipAESTransform.cs +++ b/src/ICSharpCode.SharpZipLib/Encryption/ZipAESTransform.cs @@ -8,32 +8,6 @@ namespace ICSharpCode.SharpZipLib.Encryption /// internal class ZipAESTransform : ICryptoTransform { - -#if NET45 - class IncrementalHash : HMACSHA1 - { - bool _finalised; - public IncrementalHash(byte[] key) : base(key) { } - public static IncrementalHash CreateHMAC(string n, byte[] key) => new IncrementalHash(key); - public void AppendData(byte[] buffer, int offset, int count) => TransformBlock(buffer, offset, count, buffer, offset); - public byte[] GetHashAndReset() - { - if (!_finalised) - { - byte[] dummy = new byte[0]; - TransformFinalBlock(dummy, 0, 0); - _finalised = true; - } - return Hash; - } - } - - static class HashAlgorithmName - { - public static string SHA1 = null; - } -#endif - private const int PWD_VER_LENGTH = 2; // WinZip use iteration count of 1000 for PBKDF2 key generation @@ -66,7 +40,6 @@ static class HashAlgorithmName /// public ZipAESTransform(string key, byte[] saltBytes, int blockSize, bool writeMode) { - if (blockSize != 16 && blockSize != 32) // 24 valid for AES but not supported by Winzip throw new Exception("Invalid blocksize " + blockSize + ". Must be 16 or 32."); if (saltBytes.Length != blockSize / 2) @@ -77,16 +50,22 @@ public ZipAESTransform(string key, byte[] saltBytes, int blockSize, bool writeMo _encrPos = ENCRYPT_BLOCK; // Performs the equivalent of derive_key in Dr Brian Gladman's pwd2key.c +#if NET472_OR_GREATER || NETSTANDARD2_1_OR_GREATER || NETCOREAPP2_0_OR_GREATER + var pdb = new Rfc2898DeriveBytes(key, saltBytes, KEY_ROUNDS, HashAlgorithmName.SHA1); +#else var pdb = new Rfc2898DeriveBytes(key, saltBytes, KEY_ROUNDS); - var rm = Aes.Create(); +#endif + var rm = Aes.Create(); rm.Mode = CipherMode.ECB; // No feedback from cipher for CTR mode _counterNonce = new byte[_blockSize]; - byte[] byteKey1 = pdb.GetBytes(_blockSize); - byte[] byteKey2 = pdb.GetBytes(_blockSize); - _encryptor = rm.CreateEncryptor(byteKey1, byteKey2); + byte[] key1bytes = pdb.GetBytes(_blockSize); + byte[] key2bytes = pdb.GetBytes(_blockSize); + + // Use empty IV for AES + _encryptor = rm.CreateEncryptor(key1bytes, new byte[16]); _pwdVerifier = pdb.GetBytes(PWD_VER_LENGTH); // - _hmacsha1 = IncrementalHash.CreateHMAC(HashAlgorithmName.SHA1, byteKey2); + _hmacsha1 = IncrementalHash.CreateHMAC(HashAlgorithmName.SHA1, key2bytes); _writeMode = writeMode; } @@ -95,19 +74,22 @@ public ZipAESTransform(string key, byte[] saltBytes, int blockSize, bool writeMo /// public int TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset) { - // Pass the data stream to the hash algorithm for generating the Auth Code. // This does not change the inputBuffer. Do this before decryption for read mode. - if (!_writeMode) { + if (!_writeMode) + { _hmacsha1.AppendData(inputBuffer, inputOffset, inputCount); } // Encrypt with AES in CTR mode. Regards to Dr Brian Gladman for this. int ix = 0; - while (ix < inputCount) { - if (_encrPos == ENCRYPT_BLOCK) { + while (ix < inputCount) + { + if (_encrPos == ENCRYPT_BLOCK) + { /* increment encryption nonce */ int j = 0; - while (++_counterNonce[j] == 0) { + while (++_counterNonce[j] == 0) + { ++j; } /* encrypt the nonce to form next xor buffer */ @@ -118,8 +100,9 @@ public int TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, b // ix++; } - if (_writeMode) { - // This does not change the buffer. + if (_writeMode) + { + // This does not change the buffer. _hmacsha1.AppendData(outputBuffer, outputOffset, inputCount); } return inputCount; @@ -128,80 +111,68 @@ public int TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, b /// /// Returns the 2 byte password verifier /// - public byte[] PwdVerifier { - get { - return _pwdVerifier; - } - } + public byte[] PwdVerifier => _pwdVerifier; /// /// Returns the 10 byte AUTH CODE to be checked or appended immediately following the AES data stream. /// - public byte[] GetAuthCode() - { - if (_authCode == null) - { - _authCode = _hmacsha1.GetHashAndReset(); - } - return _authCode; - } + public byte[] GetAuthCode() => _authCode ?? (_authCode = _hmacsha1.GetHashAndReset()); -#region ICryptoTransform Members + #region ICryptoTransform Members /// - /// Not implemented. + /// Transform final block and read auth code /// public byte[] TransformFinalBlock(byte[] inputBuffer, int inputOffset, int inputCount) { + var buffer = Array.Empty(); + + // FIXME: When used together with `ZipAESStream`, the final block handling is done inside of it instead + // This should not be necessary anymore, and the entire `ZipAESStream` class should be replaced with a plain `CryptoStream` + if (inputCount != 0) { + if (inputCount > ZipAESStream.AUTH_CODE_LENGTH) + { + // At least one byte of data is preceeding the auth code + int finalBlock = inputCount - ZipAESStream.AUTH_CODE_LENGTH; + buffer = new byte[finalBlock]; + TransformBlock(inputBuffer, inputOffset, finalBlock, buffer, 0); + } + else if (inputCount < ZipAESStream.AUTH_CODE_LENGTH) + throw new Zip.ZipException("Auth code missing from input stream"); + + // Read the authcode from the last 10 bytes + _authCode = _hmacsha1.GetHashAndReset(); + } + - throw new NotImplementedException("ZipAESTransform.TransformFinalBlock"); + return buffer; } /// /// Gets the size of the input data blocks in bytes. /// - public int InputBlockSize { - get { - return _blockSize; - } - } + public int InputBlockSize => _blockSize; /// /// Gets the size of the output data blocks in bytes. /// - public int OutputBlockSize { - get { - return _blockSize; - } - } + public int OutputBlockSize => _blockSize; /// /// Gets a value indicating whether multiple blocks can be transformed. /// - public bool CanTransformMultipleBlocks { - get { - return true; - } - } + public bool CanTransformMultipleBlocks => true; /// /// Gets a value indicating whether the current transform can be reused. /// - public bool CanReuseTransform { - get { - return true; - } - } + public bool CanReuseTransform => true; /// /// Cleanup internal state. /// - public void Dispose() - { - _encryptor.Dispose(); - } - -#endregion + public void Dispose() => _encryptor.Dispose(); + #endregion ICryptoTransform Members } } diff --git a/src/ICSharpCode.SharpZipLib/GZip/GZip.cs b/src/ICSharpCode.SharpZipLib/GZip/GZip.cs index 1354a6d30..e7e4763da 100644 --- a/src/ICSharpCode.SharpZipLib/GZip/GZip.cs +++ b/src/ICSharpCode.SharpZipLib/GZip/GZip.cs @@ -3,31 +3,41 @@ namespace ICSharpCode.SharpZipLib.GZip { + using static Zip.Compression.Deflater; + /// /// An example class to demonstrate compression and decompression of GZip streams. /// public static class GZip { /// - /// Decompress the input writing + /// Decompress the input writing /// uncompressed data to the output stream /// /// The readable stream containing data to decompress. /// The output stream to receive the decompressed data. /// Both streams are closed on completion if true. + /// Input or output stream is null public static void Decompress(Stream inStream, Stream outStream, bool isStreamOwner) { - if (inStream == null || outStream == null) { - throw new Exception("Null Stream"); - } + if (inStream == null) + throw new ArgumentNullException(nameof(inStream), "Input stream is null"); + + if (outStream == null) + throw new ArgumentNullException(nameof(outStream), "Output stream is null"); - try { - using (GZipInputStream bzipInput = new GZipInputStream(inStream)) { - bzipInput.IsStreamOwner = isStreamOwner; - Core.StreamUtils.Copy(bzipInput, outStream, new byte[4096]); + try + { + using (GZipInputStream gzipInput = new GZipInputStream(inStream)) + { + gzipInput.IsStreamOwner = isStreamOwner; + Core.StreamUtils.Copy(gzipInput, outStream, new byte[4096]); } - } finally { - if (isStreamOwner) { + } + finally + { + if (isStreamOwner) + { // inStream is closed by the GZipInputStream if stream owner outStream.Dispose(); } @@ -35,32 +45,48 @@ public static void Decompress(Stream inStream, Stream outStream, bool isStreamOw } /// - /// Compress the input stream sending + /// Compress the input stream sending /// result data to output stream /// /// The readable stream to compress. /// The output stream to receive the compressed data. /// Both streams are closed on completion if true. - /// Block size acts as compression level (1 to 9) with 1 giving - /// the lowest compression and 9 the highest. - public static void Compress(Stream inStream, Stream outStream, bool isStreamOwner, int level) + /// Deflate buffer size, minimum 512 + /// Deflate compression level, 0-9 + /// Input or output stream is null + /// Buffer Size is smaller than 512 + /// Compression level outside 0-9 + public static void Compress(Stream inStream, Stream outStream, bool isStreamOwner, int bufferSize = 512, int level = 6) { - if (inStream == null || outStream == null) { - throw new Exception("Null Stream"); - } + if (inStream == null) + throw new ArgumentNullException(nameof(inStream), "Input stream is null"); - try { - using (GZipOutputStream bzipOutput = new GZipOutputStream(outStream, level)) { - bzipOutput.IsStreamOwner = isStreamOwner; - Core.StreamUtils.Copy(inStream, bzipOutput, new byte[4096]); + if (outStream == null) + throw new ArgumentNullException(nameof(outStream), "Output stream is null"); + + if (bufferSize < 512) + throw new ArgumentOutOfRangeException(nameof(bufferSize), "Deflate buffer size must be >= 512"); + + if (level < NO_COMPRESSION || level > BEST_COMPRESSION) + throw new ArgumentOutOfRangeException(nameof(level), "Compression level must be 0-9"); + + try + { + using (GZipOutputStream gzipOutput = new GZipOutputStream(outStream, bufferSize)) + { + gzipOutput.SetLevel(level); + gzipOutput.IsStreamOwner = isStreamOwner; + Core.StreamUtils.Copy(inStream, gzipOutput, new byte[bufferSize]); } - } finally { - if (isStreamOwner) { + } + finally + { + if (isStreamOwner) + { // outStream is closed by the GZipOutputStream if stream owner inStream.Dispose(); } } } - } } diff --git a/src/ICSharpCode.SharpZipLib/GZip/GZipConstants.cs b/src/ICSharpCode.SharpZipLib/GZip/GZipConstants.cs index 747d71a54..a59799278 100644 --- a/src/ICSharpCode.SharpZipLib/GZip/GZipConstants.cs +++ b/src/ICSharpCode.SharpZipLib/GZip/GZipConstants.cs @@ -1,58 +1,78 @@ +using System; +using System.Text; + namespace ICSharpCode.SharpZipLib.GZip { /// /// This class contains constants used for gzip. /// + [System.Diagnostics.CodeAnalysis.SuppressMessage("Naming", "CA1707:Identifiers should not contain underscores", Justification = "kept for backwards compatibility")] sealed public class GZipConstants { /// - /// Magic number found at start of GZIP header + /// First GZip identification byte /// - public const int GZIP_MAGIC = 0x1F8B; + public const byte ID1 = 0x1F; - /* The flag byte is divided into individual bits as follows: - - bit 0 FTEXT - bit 1 FHCRC - bit 2 FEXTRA - bit 3 FNAME - bit 4 FCOMMENT - bit 5 reserved - bit 6 reserved - bit 7 reserved - */ + /// + /// Second GZip identification byte + /// + public const byte ID2 = 0x8B; /// - /// Flag bit mask for text + /// Deflate compression method /// - public const int FTEXT = 0x1; + public const byte CompressionMethodDeflate = 0x8; /// - /// Flag bitmask for Crc + /// Get the GZip specified encoding (CP-1252 if supported, otherwise ASCII) /// - public const int FHCRC = 0x2; + public static Encoding Encoding + { + get + { + try + { + return Encoding.GetEncoding(1252); + } + catch + { + return Encoding.ASCII; + } + } + } + + } + /// + /// GZip header flags + /// + [Flags] + public enum GZipFlags: byte + { /// - /// Flag bit mask for extra + /// Text flag hinting that the file is in ASCII /// - public const int FEXTRA = 0x4; + FTEXT = 0x1 << 0, /// - /// flag bitmask for name + /// CRC flag indicating that a CRC16 preceeds the data /// - public const int FNAME = 0x8; + FHCRC = 0x1 << 1, /// - /// flag bit mask indicating comment is present + /// Extra flag indicating that extra fields are present /// - public const int FCOMMENT = 0x10; + FEXTRA = 0x1 << 2, /// - /// Initialise default instance. + /// Filename flag indicating that the original filename is present /// - /// Constructor is private to prevent instances being created. - GZipConstants() - { - } + FNAME = 0x1 << 3, + + /// + /// Flag bit mask indicating that a comment is present + /// + FCOMMENT = 0x1 << 4, } } diff --git a/src/ICSharpCode.SharpZipLib/GZip/GZipException.cs b/src/ICSharpCode.SharpZipLib/GZip/GZipException.cs index 1a5952991..a0ec6bb51 100644 --- a/src/ICSharpCode.SharpZipLib/GZip/GZipException.cs +++ b/src/ICSharpCode.SharpZipLib/GZip/GZipException.cs @@ -1,10 +1,12 @@ using System; +using System.Runtime.Serialization; namespace ICSharpCode.SharpZipLib.GZip { /// /// GZipException represents exceptions specific to GZip classes and code. /// + [Serializable] public class GZipException : SharpZipBaseException { /// @@ -32,5 +34,21 @@ public GZipException(string message, Exception innerException) : base(message, innerException) { } + + /// + /// Initializes a new instance of the GZipException class with serialized data. + /// + /// + /// The System.Runtime.Serialization.SerializationInfo that holds the serialized + /// object data about the exception being thrown. + /// + /// + /// The System.Runtime.Serialization.StreamingContext that contains contextual information + /// about the source or destination. + /// + protected GZipException(SerializationInfo info, StreamingContext context) + : base(info, context) + { + } } } diff --git a/src/ICSharpCode.SharpZipLib/GZip/GzipInputStream.cs b/src/ICSharpCode.SharpZipLib/GZip/GzipInputStream.cs index 06d602681..feca66b3d 100644 --- a/src/ICSharpCode.SharpZipLib/GZip/GzipInputStream.cs +++ b/src/ICSharpCode.SharpZipLib/GZip/GzipInputStream.cs @@ -1,26 +1,27 @@ -using System; -using System.IO; using ICSharpCode.SharpZipLib.Checksum; using ICSharpCode.SharpZipLib.Zip.Compression; using ICSharpCode.SharpZipLib.Zip.Compression.Streams; +using System; +using System.IO; +using System.Text; +using ICSharpCode.SharpZipLib.Core; namespace ICSharpCode.SharpZipLib.GZip { - /// /// This filter stream is used to decompress a "GZIP" format stream. /// The "GZIP" format is described baseInputStream RFC 1952. - /// + /// /// author of the original java version : John Leuner /// /// This sample shows how to unzip a gzipped file /// /// using System; /// using System.IO; - /// + /// /// using ICSharpCode.SharpZipLib.Core; /// using ICSharpCode.SharpZipLib.GZip; - /// + /// /// class MainClass /// { /// public static void Main(string[] args) @@ -31,12 +32,13 @@ namespace ICSharpCode.SharpZipLib.GZip /// StreamUtils.Copy(inStream, outStream, buffer); /// } /// } - /// } + /// } /// /// public class GZipInputStream : InflaterInputStream { #region Instance Fields + /// /// CRC-32 value for uncompressed data /// @@ -46,16 +48,20 @@ public class GZipInputStream : InflaterInputStream /// Flag to indicate if we've read the GZIP header yet for the current member (block of compressed data). /// This is tracked per-block as the file is parsed. /// - bool readGZIPHeader; + private bool readGZIPHeader; + + /// + /// Flag to indicate if at least one block in a stream with concatenated blocks was read successfully. + /// This allows us to exit gracefully if downstream data is not in gzip format. + /// + private bool completedLastBlock; - /// - /// Flag to indicate if at least one block in a stream with concatenated blocks was read successfully. - /// This allows us to exit gracefully if downstream data is not in gzip format. - /// - bool completedLastBlock; - #endregion + private string fileName; + + #endregion Instance Fields #region Constructors + /// /// Creates a GZipInputStream with the default buffer size /// @@ -77,12 +83,14 @@ public GZipInputStream(Stream baseInputStream) /// Size of the buffer to use /// public GZipInputStream(Stream baseInputStream, int size) - : base(baseInputStream, new Inflater(true), size) + : base(baseInputStream, InflaterPool.Instance.Rent(true), size) { } - #endregion + + #endregion Constructors #region Stream overrides + /// /// Reads uncompressed data into an array of bytes /// @@ -101,200 +109,199 @@ public override int Read(byte[] buffer, int offset, int count) // A GZIP file can contain multiple blocks of compressed data, although this is quite rare. // A compressed block could potentially be empty, so we need to loop until we reach EOF or // we find data. - while (true) { - + while (true) + { // If we haven't read the header for this block, read it - if (!readGZIPHeader) { - - // Try to read header. If there is no header (0 bytes available), this is EOF. If there is - // an incomplete header, this will throw an exception. - try - { - if (!ReadHeader()) - { - return 0; - } - } - catch (Exception ex) when (completedLastBlock && (ex is GZipException || ex is EndOfStreamException)) - { - // if we completed the last block (i.e. we're in a stream that has multiple blocks concatenated - // we want to return gracefully from any header parsing exceptions since sometimes there may - // be trailing garbage on a stream - return 0; - } - } + if (!readGZIPHeader) + { + // Try to read header. If there is no header (0 bytes available), this is EOF. If there is + // an incomplete header, this will throw an exception. + try + { + if (!ReadHeader()) + { + return 0; + } + } + catch (Exception ex) when (completedLastBlock && (ex is GZipException || ex is EndOfStreamException)) + { + // if we completed the last block (i.e. we're in a stream that has multiple blocks concatenated + // we want to return gracefully from any header parsing exceptions since sometimes there may + // be trailing garbage on a stream + return 0; + } + } // Try to read compressed data int bytesRead = base.Read(buffer, offset, count); - if (bytesRead > 0) { - crc.Update(buffer, offset, bytesRead); + if (bytesRead > 0) + { + crc.Update(new ArraySegment(buffer, offset, bytesRead)); } // If this is the end of stream, read the footer - if (inf.IsFinished) { + if (inf.IsFinished) + { ReadFooter(); } - if (bytesRead > 0) { + // Attempting to read 0 bytes will never yield any bytesRead, so we return instead of looping forever + if (bytesRead > 0 || count == 0) + { return bytesRead; } } } - #endregion + + /// + /// Retrieves the filename header field for the block last read + /// + /// + public string GetFilename() + { + return fileName; + } + + #endregion Stream overrides #region Support routines - bool ReadHeader() + + private bool ReadHeader() { // Initialize CRC for this block crc = new Crc32(); // Make sure there is data in file. We can't rely on ReadLeByte() to fill the buffer, as this could be EOF, // which is fine, but ReadLeByte() throws an exception if it doesn't find data, so we do this part ourselves. - if (inputBuffer.Available <= 0) { + if (inputBuffer.Available <= 0) + { inputBuffer.Fill(); - if (inputBuffer.Available <= 0) { + if (inputBuffer.Available <= 0) + { // No header, EOF. return false; } } - // 1. Check the two magic bytes var headCRC = new Crc32(); - int magic = inputBuffer.ReadLeByte(); - if (magic < 0) { - throw new EndOfStreamException("EOS reading GZIP header"); - } + // 1. Check the two magic bytes + var magic = inputBuffer.ReadLeByte(); headCRC.Update(magic); - if (magic != (GZipConstants.GZIP_MAGIC >> 8)) { - throw new GZipException("Error GZIP header, first magic byte doesn't match"); + if (magic != GZipConstants.ID1) + { + throw new GZipException("Error GZIP header, first magic byte doesn't match"); } - //magic = baseInputStream.ReadByte(); magic = inputBuffer.ReadLeByte(); - - if (magic < 0) { - throw new EndOfStreamException("EOS reading GZIP header"); - } - - if (magic != (GZipConstants.GZIP_MAGIC & 0xFF)) { + if (magic != GZipConstants.ID2) + { throw new GZipException("Error GZIP header, second magic byte doesn't match"); } - headCRC.Update(magic); // 2. Check the compression type (must be 8) - int compressionType = inputBuffer.ReadLeByte(); + var compressionType = inputBuffer.ReadLeByte(); - if (compressionType < 0) { - throw new EndOfStreamException("EOS reading GZIP header"); - } - - if (compressionType != 8) { + if (compressionType != GZipConstants.CompressionMethodDeflate) + { throw new GZipException("Error GZIP header, data not in deflate format"); } headCRC.Update(compressionType); // 3. Check the flags - int flags = inputBuffer.ReadLeByte(); - if (flags < 0) { - throw new EndOfStreamException("EOS reading GZIP header"); - } - headCRC.Update(flags); + var flagsByte = inputBuffer.ReadLeByte(); - /* This flag byte is divided into individual bits as follows: - - bit 0 FTEXT - bit 1 FHCRC - bit 2 FEXTRA - bit 3 FNAME - bit 4 FCOMMENT - bit 5 reserved - bit 6 reserved - bit 7 reserved - */ + headCRC.Update(flagsByte); // 3.1 Check the reserved bits are zero - if ((flags & 0xE0) != 0) { + if ((flagsByte & 0xE0) != 0) + { throw new GZipException("Reserved flag bits in GZIP header != 0"); } + var flags = (GZipFlags)flagsByte; + // 4.-6. Skip the modification time, extra flags, and OS type - for (int i = 0; i < 6; i++) { - int readByte = inputBuffer.ReadLeByte(); - if (readByte < 0) { - throw new EndOfStreamException("EOS reading GZIP header"); - } - headCRC.Update(readByte); + for (int i = 0; i < 6; i++) + { + headCRC.Update(inputBuffer.ReadLeByte()); } // 7. Read extra field - if ((flags & GZipConstants.FEXTRA) != 0) { - + if (flags.HasFlag(GZipFlags.FEXTRA)) + { // XLEN is total length of extra subfields, we will skip them all - int len1, len2; - len1 = inputBuffer.ReadLeByte(); - len2 = inputBuffer.ReadLeByte(); - if ((len1 < 0) || (len2 < 0)) { - throw new EndOfStreamException("EOS reading GZIP header"); - } + var len1 = inputBuffer.ReadLeByte(); + var len2 = inputBuffer.ReadLeByte(); + headCRC.Update(len1); headCRC.Update(len2); int extraLen = (len2 << 8) | len1; // gzip is LSB first - for (int i = 0; i < extraLen; i++) { - int readByte = inputBuffer.ReadLeByte(); - if (readByte < 0) { - throw new EndOfStreamException("EOS reading GZIP header"); - } - headCRC.Update(readByte); + for (int i = 0; i < extraLen; i++) + { + headCRC.Update(inputBuffer.ReadLeByte()); } } // 8. Read file name - if ((flags & GZipConstants.FNAME) != 0) { + if (flags.HasFlag(GZipFlags.FNAME)) + { + var fname = new byte[1024]; + var fnamePos = 0; int readByte; - while ((readByte = inputBuffer.ReadLeByte()) > 0) { + while ((readByte = inputBuffer.ReadLeByte()) > 0) + { + if (fnamePos < 1024) + { + fname[fnamePos++] = (byte)readByte; + } headCRC.Update(readByte); } - if (readByte < 0) { - throw new EndOfStreamException("EOS reading GZIP header"); - } headCRC.Update(readByte); + + fileName = GZipConstants.Encoding.GetString(fname, 0, fnamePos); + } + else + { + fileName = null; } // 9. Read comment - if ((flags & GZipConstants.FCOMMENT) != 0) { + if (flags.HasFlag(GZipFlags.FCOMMENT)) + { int readByte; - while ((readByte = inputBuffer.ReadLeByte()) > 0) { + while ((readByte = inputBuffer.ReadLeByte()) > 0) + { headCRC.Update(readByte); } - if (readByte < 0) { - throw new EndOfStreamException("EOS reading GZIP header"); - } - headCRC.Update(readByte); } // 10. Read header CRC - if ((flags & GZipConstants.FHCRC) != 0) { + if (flags.HasFlag(GZipFlags.FHCRC)) + { int tempByte; int crcval = inputBuffer.ReadLeByte(); - if (crcval < 0) { + if (crcval < 0) + { throw new EndOfStreamException("EOS reading GZIP header"); } tempByte = inputBuffer.ReadLeByte(); - if (tempByte < 0) { + if (tempByte < 0) + { throw new EndOfStreamException("EOS reading GZIP header"); } crcval = (crcval << 8) | tempByte; - if (crcval != ((int)headCRC.Value & 0xffff)) { + if (crcval != ((int)headCRC.Value & 0xffff)) + { throw new GZipException("Header CRC value mismatch"); } } @@ -303,7 +310,7 @@ bit 7 reserved return true; } - void ReadFooter() + private void ReadFooter() { byte[] footer = new byte[8]; @@ -314,9 +321,11 @@ void ReadFooter() // Read footer from inputBuffer int needed = 8; - while (needed > 0) { + while (needed > 0) + { int count = inputBuffer.ReadClearTextBuffer(footer, 8 - needed, needed); - if (count <= 0) { + if (count <= 0) + { throw new EndOfStreamException("EOS reading GZIP footer"); } needed -= count; // Jewel Jan 16 @@ -324,8 +333,9 @@ void ReadFooter() // Calculate CRC int crcval = (footer[0] & 0xff) | ((footer[1] & 0xff) << 8) | ((footer[2] & 0xff) << 16) | (footer[3] << 24); - if (crcval != (int)crc.Value) { - throw new GZipException("GZIP crc sum mismatch, theirs \"" + crcval + "\" and ours \"" + (int)crc.Value); + if (crcval != (int)crc.Value) + { + throw new GZipException($"GZIP crc sum mismatch, theirs \"{crcval:x8}\" and ours \"{(int)crc.Value:x8}\""); } // NOTE The total here is the original total modulo 2 ^ 32. @@ -335,16 +345,18 @@ void ReadFooter() (uint)(((uint)footer[6] & 0xff) << 16) | (uint)((uint)footer[7] << 24); - if (bytesRead != total) { + if (bytesRead != total) + { throw new GZipException("Number of bytes mismatch in footer"); } // Mark header read as false so if another header exists, we'll continue reading through the file readGZIPHeader = false; - // Indicate that we succeeded on at least one block so we can exit gracefully if there is trailing garbage downstream - completedLastBlock = true; + // Indicate that we succeeded on at least one block so we can exit gracefully if there is trailing garbage downstream + completedLastBlock = true; } - #endregion + + #endregion Support routines } } diff --git a/src/ICSharpCode.SharpZipLib/GZip/GzipOutputStream.cs b/src/ICSharpCode.SharpZipLib/GZip/GzipOutputStream.cs index e2d35ceb3..d4f1aa4c3 100644 --- a/src/ICSharpCode.SharpZipLib/GZip/GzipOutputStream.cs +++ b/src/ICSharpCode.SharpZipLib/GZip/GzipOutputStream.cs @@ -1,8 +1,11 @@ -using System; -using System.IO; using ICSharpCode.SharpZipLib.Checksum; using ICSharpCode.SharpZipLib.Zip.Compression; using ICSharpCode.SharpZipLib.Zip.Compression.Streams; +using System; +using System.IO; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; namespace ICSharpCode.SharpZipLib.GZip { @@ -16,10 +19,10 @@ namespace ICSharpCode.SharpZipLib.GZip /// /// using System; /// using System.IO; - /// + /// /// using ICSharpCode.SharpZipLib.GZip; /// using ICSharpCode.SharpZipLib.Core; - /// + /// /// class MainClass /// { /// public static void Main(string[] args) @@ -31,12 +34,12 @@ namespace ICSharpCode.SharpZipLib.GZip /// } /// } /// } - /// } + /// } /// /// public class GZipOutputStream : DeflaterOutputStream { - enum OutputState + private enum OutputState { Header, Footer, @@ -45,14 +48,22 @@ enum OutputState }; #region Instance Fields + /// /// CRC-32 value for uncompressed data /// protected Crc32 crc = new Crc32(); - OutputState state_ = OutputState.Header; - #endregion + + private OutputState state_ = OutputState.Header; + + private string fileName; + + private GZipFlags flags = 0; + + #endregion Instance Fields #region Constructors + /// /// Creates a GzipOutputStream with the default buffer size /// @@ -76,11 +87,13 @@ public GZipOutputStream(Stream baseOutputStream) public GZipOutputStream(Stream baseOutputStream, int size) : base(baseOutputStream, new Deflater(Deflater.DEFAULT_COMPRESSION, true), size) { } - #endregion + + #endregion Constructors #region Public API + /// - /// Sets the active compression level (1-9). The new level will be activated + /// Sets the active compression level (0-9). The new level will be activated /// immediately. /// /// The compression level to set. @@ -90,9 +103,9 @@ public GZipOutputStream(Stream baseOutputStream) /// public void SetLevel(int level) { - if (level < Deflater.BEST_SPEED) { - throw new ArgumentOutOfRangeException(nameof(level)); - } + if (level < Deflater.NO_COMPRESSION || level > Deflater.BEST_COMPRESSION) + throw new ArgumentOutOfRangeException(nameof(level), "Compression level must be 0-9"); + deflater_.SetLevel(level); } @@ -104,9 +117,36 @@ public int GetLevel() { return deflater_.GetLevel(); } - #endregion + + /// + /// Original filename + /// + public string FileName + { + get => fileName; + set + { + fileName = CleanFilename(value); + if (string.IsNullOrEmpty(fileName)) + { + flags &= ~GZipFlags.FNAME; + } + else + { + flags |= GZipFlags.FNAME; + } + } + } + + /// + /// If defined, will use this time instead of the current for the output header + /// + public DateTime? ModifiedTime { get; set; } + + #endregion Public API #region Stream overrides + /// /// Write given buffer to output updating crc /// @@ -114,103 +154,245 @@ public int GetLevel() /// Offset of first byte in buf to write /// Number of bytes to write public override void Write(byte[] buffer, int offset, int count) + => WriteSyncOrAsync(buffer, offset, count, null).GetAwaiter().GetResult(); + + private async Task WriteSyncOrAsync(byte[] buffer, int offset, int count, CancellationToken? ct) { - if (state_ == OutputState.Header) { - WriteHeader(); + if (state_ == OutputState.Header) + { + if (ct.HasValue) + { + await WriteHeaderAsync(ct.Value).ConfigureAwait(false); + } + else + { + WriteHeader(); + } } - if (state_ != OutputState.Footer) { + if (state_ != OutputState.Footer) throw new InvalidOperationException("Write not permitted in current state"); - } + + crc.Update(new ArraySegment(buffer, offset, count)); - crc.Update(buffer, offset, count); - base.Write(buffer, offset, count); + if (ct.HasValue) + { + await base.WriteAsync(buffer, offset, count, ct.Value).ConfigureAwait(false); + } + else + { + base.Write(buffer, offset, count); + } } + /// + /// Asynchronously write given buffer to output updating crc + /// + /// Buffer to write + /// Offset of first byte in buf to write + /// Number of bytes to write + /// The token to monitor for cancellation requests + public override async Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken ct) + => await WriteSyncOrAsync(buffer, offset, count, ct).ConfigureAwait(false); + /// /// Writes remaining compressed output data to the output stream /// and closes it. /// protected override void Dispose(bool disposing) { - try { + try + { Finish(); - } finally { - if (state_ != OutputState.Closed) { + } + finally + { + if (state_ != OutputState.Closed) + { state_ = OutputState.Closed; - if (IsStreamOwner) { + if (IsStreamOwner) + { baseOutputStream_.Dispose(); } } } } - #endregion + +#if NETSTANDARD2_1_OR_GREATER || NETCOREAPP3_1_OR_GREATER + /// + public override async ValueTask DisposeAsync() + { + try + { + await FinishAsync(CancellationToken.None).ConfigureAwait(false); + } + finally + { + if (state_ != OutputState.Closed) + { + state_ = OutputState.Closed; + if (IsStreamOwner) + { + await baseOutputStream_.DisposeAsync().ConfigureAwait(false); + } + } + + await base.DisposeAsync().ConfigureAwait(false); + } + } +#endif + + /// + /// Flushes the stream by ensuring the header is written, and then calling Flush + /// on the deflater. + /// + public override void Flush() + { + if (state_ == OutputState.Header) + { + WriteHeader(); + } + + base.Flush(); + } + + /// + public override async Task FlushAsync(CancellationToken ct) + { + if (state_ == OutputState.Header) + { + await WriteHeaderAsync(ct).ConfigureAwait(false); + } + await base.FlushAsync(ct).ConfigureAwait(false); + } + + #endregion Stream overrides #region DeflaterOutputStream overrides + /// /// Finish compression and write any footer information required to stream /// public override void Finish() { // If no data has been written a header should be added. - if (state_ == OutputState.Header) { + if (state_ == OutputState.Header) + { WriteHeader(); } - if (state_ == OutputState.Footer) { + if (state_ == OutputState.Footer) + { state_ = OutputState.Finished; base.Finish(); + var gzipFooter = GetFooter(); + baseOutputStream_.Write(gzipFooter, 0, gzipFooter.Length); + } + } + + /// + public override async Task FinishAsync(CancellationToken ct) + { + // If no data has been written a header should be added. + if (state_ == OutputState.Header) + { + await WriteHeaderAsync(ct).ConfigureAwait(false); + } + + if (state_ == OutputState.Footer) + { + state_ = OutputState.Finished; + await base.FinishAsync(ct).ConfigureAwait(false); + var gzipFooter = GetFooter(); + await baseOutputStream_.WriteAsync(gzipFooter, 0, gzipFooter.Length, ct).ConfigureAwait(false); + } + } - var totalin = (uint)(deflater_.TotalIn & 0xffffffff); - var crcval = (uint)(crc.Value & 0xffffffff); + #endregion DeflaterOutputStream overrides - byte[] gzipFooter; + #region Support Routines - unchecked { - gzipFooter = new byte[] { - (byte) crcval, (byte) (crcval >> 8), - (byte) (crcval >> 16), (byte) (crcval >> 24), + private byte[] GetFooter() + { + var totalin = (uint)(deflater_.TotalIn & 0xffffffff); + var crcval = (uint)(crc.Value & 0xffffffff); - (byte) totalin, (byte) (totalin >> 8), - (byte) (totalin >> 16), (byte) (totalin >> 24) - }; - } + byte[] gzipFooter; - baseOutputStream_.Write(gzipFooter, 0, gzipFooter.Length); + unchecked + { + gzipFooter = new [] { + (byte) crcval, + (byte) (crcval >> 8), + (byte) (crcval >> 16), + (byte) (crcval >> 24), + (byte) totalin, + (byte) (totalin >> 8), + (byte) (totalin >> 16), + (byte) (totalin >> 24), + }; } + + return gzipFooter; } - #endregion - #region Support Routines - void WriteHeader() + private byte[] GetHeader() { - if (state_ == OutputState.Header) { - state_ = OutputState.Footer; + var modifiedUtc = ModifiedTime?.ToUniversalTime() ?? DateTime.UtcNow; + var modTime = (int)((modifiedUtc - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).Ticks / 10000000L); // Ticks give back 100ns intervals + byte[] gzipHeader = { + // The two magic bytes + GZipConstants.ID1, + GZipConstants.ID2, - var mod_time = (int)((DateTime.Now.Ticks - new DateTime(1970, 1, 1).Ticks) / 10000000L); // Ticks give back 100ns intervals - byte[] gzipHeader = { - // The two magic bytes - (byte) (GZipConstants.GZIP_MAGIC >> 8), (byte) (GZipConstants.GZIP_MAGIC & 0xff), + // The compression type + GZipConstants.CompressionMethodDeflate, - // The compression type - (byte) Deflater.DEFLATED, + // The flags (not set) + (byte)flags, - // The flags (not set) - 0, + // The modification time + (byte) modTime, (byte) (modTime >> 8), + (byte) (modTime >> 16), (byte) (modTime >> 24), - // The modification time - (byte) mod_time, (byte) (mod_time >> 8), - (byte) (mod_time >> 16), (byte) (mod_time >> 24), + // The extra flags + 0, - // The extra flags - 0, + // The OS type (unknown) + 255 + }; - // The OS type (unknown) - (byte) 255 - }; - baseOutputStream_.Write(gzipHeader, 0, gzipHeader.Length); + if (!flags.HasFlag(GZipFlags.FNAME)) + { + return gzipHeader; } + + + return gzipHeader + .Concat(GZipConstants.Encoding.GetBytes(fileName)) + .Concat(new byte []{0}) // End filename string with a \0 + .ToArray(); } - #endregion + + private static string CleanFilename(string path) + => path.Substring(path.LastIndexOf('/') + 1); + + private void WriteHeader() + { + if (state_ != OutputState.Header) return; + state_ = OutputState.Footer; + var gzipHeader = GetHeader(); + baseOutputStream_.Write(gzipHeader, 0, gzipHeader.Length); + } + + private async Task WriteHeaderAsync(CancellationToken ct) + { + if (state_ != OutputState.Header) return; + state_ = OutputState.Footer; + var gzipHeader = GetHeader(); + await baseOutputStream_.WriteAsync(gzipHeader, 0, gzipHeader.Length, ct).ConfigureAwait(false); + } + + #endregion Support Routines } } diff --git a/src/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib.csproj b/src/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib.csproj index 5ddb7d8b1..49a1cd5cd 100644 --- a/src/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib.csproj +++ b/src/ICSharpCode.SharpZipLib/ICSharpCode.SharpZipLib.csproj @@ -1,26 +1,51 @@  - netstandard1.3 - SharpZipLib - True - ICSharpCode.SharpZipLib.snk + netstandard2.0;netstandard2.1;net6.0 + true + true + true + ../../assets/ICSharpCode.SharpZipLib.snk true - 1.0.0-alpha2 + true + $(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb + + + + + 1.4.2 + $(Version).13 + $(FileVersion) + SharpZipLib + ICSharpCode + ICSharpCode + SharpZipLib (#ziplib, formerly NZipLib) is a compression library for Zip, GZip, BZip2, and Tar written entirely in C# for .NET. It is implemented as an assembly (installable in the GAC), and thus can easily be incorporated into other projects (in any .NET language) + MIT + http://icsharpcode.github.io/SharpZipLib/ + images/sharpziplib-nuget-256x256.png https://github.com/icsharpcode/SharpZipLib - This is a pre-release version! - -Please see https://github.com/icsharpcode/SharpZipLib/wiki/Release-1.0 for more information. - https://github.com/icsharpcode/SharpZipLib/blob/master/LICENSE.txt + Copyright © 2000-2022 SharpZipLib Contributors + Compression Library Zip GZip BZip2 LZW Tar + en-US + +Please see https://github.com/icsharpcode/SharpZipLib/wiki/Release-1.4.2 for more information. https://github.com/icsharpcode/SharpZipLib - - bin\Debug\netstandard1.3\ICSharpCode.SharpZipLib.xml - + + + - - bin\Release\netstandard1.3\ICSharpCode.SharpZipLib.xml - + + + + - \ No newline at end of file + + + True + images + + + + diff --git a/src/ICSharpCode.SharpZipLib/Lzw/LzwConstants.cs b/src/ICSharpCode.SharpZipLib/Lzw/LzwConstants.cs index d36eb362a..88934830f 100644 --- a/src/ICSharpCode.SharpZipLib/Lzw/LzwConstants.cs +++ b/src/ICSharpCode.SharpZipLib/Lzw/LzwConstants.cs @@ -3,6 +3,7 @@ namespace ICSharpCode.SharpZipLib.Lzw /// /// This class contains constants used for LZW /// + [System.Diagnostics.CodeAnalysis.SuppressMessage("Naming", "CA1707:Identifiers should not contain underscores", Justification = "kept for backwards compatibility")] sealed public class LzwConstants { /// @@ -31,6 +32,7 @@ sealed public class LzwConstants /// Indicates the presence of a fourth header byte /// public const int EXTENDED_MASK = 0x20; + //public const int FREE_MASK = 0x40; /// @@ -54,7 +56,7 @@ sealed public class LzwConstants /// public const int INIT_BITS = 9; - LzwConstants() + private LzwConstants() { } } diff --git a/src/ICSharpCode.SharpZipLib/Lzw/LzwException.cs b/src/ICSharpCode.SharpZipLib/Lzw/LzwException.cs index 3bc0cb212..1d5c44c3c 100644 --- a/src/ICSharpCode.SharpZipLib/Lzw/LzwException.cs +++ b/src/ICSharpCode.SharpZipLib/Lzw/LzwException.cs @@ -1,10 +1,12 @@ using System; +using System.Runtime.Serialization; namespace ICSharpCode.SharpZipLib.Lzw { /// /// LzwException represents exceptions specific to LZW classes and code. /// + [Serializable] public class LzwException : SharpZipBaseException { /// @@ -32,5 +34,21 @@ public LzwException(string message, Exception innerException) : base(message, innerException) { } + + /// + /// Initializes a new instance of the LzwException class with serialized data. + /// + /// + /// The System.Runtime.Serialization.SerializationInfo that holds the serialized + /// object data about the exception being thrown. + /// + /// + /// The System.Runtime.Serialization.StreamingContext that contains contextual information + /// about the source or destination. + /// + protected LzwException(SerializationInfo info, StreamingContext context) + : base(info, context) + { + } } } diff --git a/src/ICSharpCode.SharpZipLib/Lzw/LzwInputStream.cs b/src/ICSharpCode.SharpZipLib/Lzw/LzwInputStream.cs index 49374ec65..1045ef778 100644 --- a/src/ICSharpCode.SharpZipLib/Lzw/LzwInputStream.cs +++ b/src/ICSharpCode.SharpZipLib/Lzw/LzwInputStream.cs @@ -10,7 +10,7 @@ namespace ICSharpCode.SharpZipLib.Lzw /// /// See http://en.wikipedia.org/wiki/Compress /// See http://wiki.wxwidgets.org/Development:_Z_File_Format - /// + /// /// The file header consists of 3 (or optionally 4) bytes. The first two bytes /// contain the magic marker "0x1f 0x9d", followed by a byte of flags. /// @@ -21,10 +21,10 @@ namespace ICSharpCode.SharpZipLib.Lzw /// /// using System; /// using System.IO; - /// + /// /// using ICSharpCode.SharpZipLib.Core; /// using ICSharpCode.SharpZipLib.LZW; - /// + /// /// class MainClass /// { /// public static void Main(string[] args) @@ -38,7 +38,7 @@ namespace ICSharpCode.SharpZipLib.Lzw /// // now do something with the buffer /// } /// } - /// } + /// } /// /// public class LzwInputStream : Stream @@ -113,10 +113,10 @@ public override int Read(byte[] buffer, int offset, int count) byte[] lData = data; int lBitPos = bitPos; - // empty stack if stuff still left int sSize = lStack.Length - lStackP; - if (sSize > 0) { + if (sSize > 0) + { int num = (sSize >= count) ? count : sSize; Array.Copy(lStack, lStackP, buffer, offset, num); offset += num; @@ -124,26 +124,31 @@ public override int Read(byte[] buffer, int offset, int count) lStackP += num; } - if (count == 0) { + if (count == 0) + { stackP = lStackP; return offset - start; } - - // loop, filling local buffer until enough data has been decompressed - MainLoop: - do { - if (end < EXTRA) { + // loop, filling local buffer until enough data has been decompressed + MainLoop: + do + { + if (end < EXTRA) + { Fill(); } int bitIn = (got > 0) ? (end - end % lNBits) << 3 : (end << 3) - (lNBits - 1); - while (lBitPos < bitIn) { + while (lBitPos < bitIn) + { #region A + // handle 1-byte reads correctly - if (count == 0) { + if (count == 0) + { nBits = lNBits; maxCode = lMaxCode; maxMaxCode = lMaxMaxCode; @@ -158,7 +163,8 @@ public override int Read(byte[] buffer, int offset, int count) } // check for code-width expansion - if (lFreeEnt > lMaxCode) { + if (lFreeEnt > lMaxCode) + { int nBytes = lNBits << 3; lBitPos = (lBitPos - 1) + nBytes - (lBitPos - 1 + nBytes) % nBytes; @@ -171,9 +177,11 @@ public override int Read(byte[] buffer, int offset, int count) lBitPos = ResetBuf(lBitPos); goto MainLoop; } - #endregion + + #endregion A #region B + // read next code int pos = lBitPos >> 3; int code = (((lData[pos] & 0xFF) | @@ -184,7 +192,8 @@ public override int Read(byte[] buffer, int offset, int count) lBitPos += lNBits; // handle first iteration - if (lOldCode == -1) { + if (lOldCode == -1) + { if (code >= 256) throw new LzwException("corrupt input: " + code + " > 255"); @@ -195,7 +204,8 @@ public override int Read(byte[] buffer, int offset, int count) } // handle CLEAR code - if (code == TBL_CLEAR && blockMode) { + if (code == TBL_CLEAR && blockMode) + { Array.Copy(zeros, 0, lTabPrefix, 0, zeros.Length); lFreeEnt = TBL_FIRST - 1; @@ -210,16 +220,20 @@ public override int Read(byte[] buffer, int offset, int count) lBitPos = ResetBuf(lBitPos); goto MainLoop; } - #endregion + + #endregion B #region C + // setup int inCode = code; lStackP = lStack.Length; // Handle KwK case - if (code >= lFreeEnt) { - if (code > lFreeEnt) { + if (code >= lFreeEnt) + { + if (code > lFreeEnt) + { throw new LzwException("corrupt input: code=" + code + ", freeEnt=" + lFreeEnt); } @@ -229,7 +243,8 @@ public override int Read(byte[] buffer, int offset, int count) } // Generate output characters in reverse order - while (code >= 256) { + while (code >= 256) + { lStack[--lStackP] = lTabSuffix[code]; code = lTabPrefix[code]; } @@ -245,11 +260,14 @@ public override int Read(byte[] buffer, int offset, int count) offset += num; count -= num; lStackP += num; - #endregion + + #endregion C #region D + // generate new entry in table - if (lFreeEnt < lMaxMaxCode) { + if (lFreeEnt < lMaxMaxCode) + { lTabPrefix[lFreeEnt] = lOldCode; lTabSuffix[lFreeEnt] = lFinChar; lFreeEnt++; @@ -259,7 +277,8 @@ public override int Read(byte[] buffer, int offset, int count) lOldCode = inCode; // if output buffer full, then return - if (count == 0) { + if (count == 0) + { nBits = lNBits; maxCode = lMaxCode; bitMask = lBitMask; @@ -271,11 +290,11 @@ public override int Read(byte[] buffer, int offset, int count) return offset - start; } - #endregion + + #endregion D } // while lBitPos = ResetBuf(lBitPos); - } while (got > 0); // do..while nBits = lNBits; @@ -305,16 +324,15 @@ private int ResetBuf(int bitPosition) return 0; } - private void Fill() { got = baseInputStream.Read(data, end, data.Length - 1 - end); - if (got > 0) { + if (got > 0) + { end += got; } } - private void ParseHeader() { headerParsed = true; @@ -327,7 +345,8 @@ private void ParseHeader() if (result < 0) throw new LzwException("Failed to read LZW header"); - if (hdr[0] != (LzwConstants.MAGIC >> 8) || hdr[1] != (LzwConstants.MAGIC & 0xff)) { + if (hdr[0] != (LzwConstants.MAGIC >> 8) || hdr[1] != (LzwConstants.MAGIC & 0xff)) + { throw new LzwException(String.Format( "Wrong LZW header. Magic bytes don't match. 0x{0:x2} 0x{1:x2}", hdr[0], hdr[1])); @@ -337,13 +356,15 @@ private void ParseHeader() blockMode = (hdr[2] & LzwConstants.BLOCK_MODE_MASK) > 0; maxBits = hdr[2] & LzwConstants.BIT_MASK; - if (maxBits > LzwConstants.MAX_BITS) { + if (maxBits > LzwConstants.MAX_BITS) + { throw new LzwException("Stream compressed with " + maxBits + " bits, but decompression can only handle " + LzwConstants.MAX_BITS + " bits."); } - if ((hdr[2] & LzwConstants.RESERVED_MASK) > 0) { + if ((hdr[2] & LzwConstants.RESERVED_MASK) > 0) + { throw new LzwException("Unsupported bits set in the header."); } @@ -366,11 +387,14 @@ private void ParseHeader() } #region Stream Overrides + /// /// Gets a value indicating whether the current stream supports reading /// - public override bool CanRead { - get { + public override bool CanRead + { + get + { return baseInputStream.CanRead; } } @@ -378,8 +402,10 @@ public override bool CanRead { /// /// Gets a value of false indicating seeking is not supported for this stream. /// - public override bool CanSeek { - get { + public override bool CanSeek + { + get + { return false; } } @@ -387,8 +413,10 @@ public override bool CanSeek { /// /// Gets a value of false indicating that this stream is not writeable. /// - public override bool CanWrite { - get { + public override bool CanWrite + { + get + { return false; } } @@ -396,8 +424,10 @@ public override bool CanWrite { /// /// A value representing the length of the stream in bytes. /// - public override long Length { - get { + public override long Length + { + get + { return got; } } @@ -407,11 +437,14 @@ public override long Length { /// Throws a NotSupportedException when attempting to set the position /// /// Attempting to set the position - public override long Position { - get { + public override long Position + { + get + { return baseInputStream.Position; } - set { + set + { throw new NotSupportedException("InflaterInputStream Position not supported"); } } @@ -452,7 +485,7 @@ public override void SetLength(long value) /// Writes a sequence of bytes to stream and advances the current position /// This method always throws a NotSupportedException /// - /// Thew buffer containing data to write. + /// The buffer containing data to write. /// The offset of the first byte to write. /// The number of bytes to write. /// Any access @@ -478,30 +511,33 @@ public override void WriteByte(byte value) /// protected override void Dispose(bool disposing) { - if (!isClosed) { + if (!isClosed) + { isClosed = true; - if (IsStreamOwner) { + if (IsStreamOwner) + { baseInputStream.Dispose(); } } } - #endregion + #endregion Stream Overrides #region Instance Fields - Stream baseInputStream; + private Stream baseInputStream; /// /// Flag indicating wether this instance has been closed or not. /// - bool isClosed; + private bool isClosed; - readonly byte[] one = new byte[1]; - bool headerParsed; + private readonly byte[] one = new byte[1]; + private bool headerParsed; // string table stuff private const int TBL_CLEAR = 0x100; + private const int TBL_FIRST = TBL_CLEAR + 1; private int[] tabPrefix; @@ -511,6 +547,7 @@ protected override void Dispose(bool disposing) // various state private bool blockMode; + private int nBits; private int maxBits; private int maxMaxCode; @@ -523,11 +560,13 @@ protected override void Dispose(bool disposing) // input buffer private readonly byte[] data = new byte[1024 * 8]; + private int bitPos; private int end; - int got; + private int got; private bool eof; private const int EXTRA = 64; - #endregion + + #endregion Instance Fields } } diff --git a/src/ICSharpCode.SharpZipLib/SharpZipLibOptions.cs b/src/ICSharpCode.SharpZipLib/SharpZipLibOptions.cs new file mode 100644 index 000000000..a6694e71e --- /dev/null +++ b/src/ICSharpCode.SharpZipLib/SharpZipLibOptions.cs @@ -0,0 +1,15 @@ +using ICSharpCode.SharpZipLib.Zip.Compression; + +namespace ICSharpCode.SharpZipLib +{ + /// + /// Global options to alter behavior. + /// + public static class SharpZipLibOptions + { + /// + /// The max pool size allowed for reusing instances, defaults to 0 (disabled). + /// + public static int InflaterPoolSize { get; set; } = 0; + } +} diff --git a/src/ICSharpCode.SharpZipLib/Tar/InvalidHeaderException.cs b/src/ICSharpCode.SharpZipLib/Tar/InvalidHeaderException.cs index a2e114052..9f385e425 100644 --- a/src/ICSharpCode.SharpZipLib/Tar/InvalidHeaderException.cs +++ b/src/ICSharpCode.SharpZipLib/Tar/InvalidHeaderException.cs @@ -1,4 +1,5 @@ using System; +using System.Runtime.Serialization; namespace ICSharpCode.SharpZipLib.Tar { @@ -6,6 +7,7 @@ namespace ICSharpCode.SharpZipLib.Tar /// This exception is used to indicate that there is a problem /// with a TAR archive header. /// + [Serializable] public class InvalidHeaderException : TarException { /// @@ -33,5 +35,21 @@ public InvalidHeaderException(string message, Exception exception) : base(message, exception) { } + + /// + /// Initializes a new instance of the InvalidHeaderException class with serialized data. + /// + /// + /// The System.Runtime.Serialization.SerializationInfo that holds the serialized + /// object data about the exception being thrown. + /// + /// + /// The System.Runtime.Serialization.StreamingContext that contains contextual information + /// about the source or destination. + /// + protected InvalidHeaderException(SerializationInfo info, StreamingContext context) + : base(info, context) + { + } } } diff --git a/src/ICSharpCode.SharpZipLib/Tar/TarArchive.cs b/src/ICSharpCode.SharpZipLib/Tar/TarArchive.cs index 494425a65..878649017 100644 --- a/src/ICSharpCode.SharpZipLib/Tar/TarArchive.cs +++ b/src/ICSharpCode.SharpZipLib/Tar/TarArchive.cs @@ -1,6 +1,8 @@ using System; using System.IO; +using System.Numerics; using System.Text; +using ICSharpCode.SharpZipLib.Core; namespace ICSharpCode.SharpZipLib.Tar { @@ -19,12 +21,12 @@ namespace ICSharpCode.SharpZipLib.Tar /// header followed by the number of blocks needed to /// contain the file's contents. All entries are written on /// block boundaries. Blocks are 512 bytes long. - /// + /// /// TarArchives are instantiated in either read or write mode, /// based upon whether they are instantiated with an InputStream /// or an OutputStream. Once instantiated TarArchives read/write /// mode can not be changed. - /// + /// /// There is currently no support for random access to tar archives. /// However, it seems that subclassing TarArchive, and using the /// TarBuffer.CurrentRecord and TarBuffer.CurrentBlock @@ -45,12 +47,14 @@ public class TarArchive : IDisposable protected virtual void OnProgressMessageEvent(TarEntry entry, string message) { ProgressMessageHandler handler = ProgressMessageEvent; - if (handler != null) { + if (handler != null) + { handler(this, entry, message); } } #region Constructors + /// /// Constructor for a default . /// @@ -59,12 +63,13 @@ protected TarArchive() } /// - /// Initalise a TarArchive for input. + /// Initialise a TarArchive for input. /// /// The to use for input. protected TarArchive(TarInputStream stream) { - if (stream == null) { + if (stream == null) + { throw new ArgumentNullException(nameof(stream)); } @@ -74,18 +79,21 @@ protected TarArchive(TarInputStream stream) /// /// Initialise a TarArchive for output. /// - /// The to use for output. + /// The to use for output. protected TarArchive(TarOutputStream stream) { - if (stream == null) { + if (stream == null) + { throw new ArgumentNullException(nameof(stream)); } tarOut = stream; } - #endregion + + #endregion Constructors #region Static factory methods + /// /// The InputStream based constructors create a TarArchive for the /// purposes of extracting or listing a tar archive. Thus, use @@ -94,19 +102,38 @@ protected TarArchive(TarOutputStream stream) /// /// The stream to retrieve archive data from. /// Returns a new suitable for reading from. + [Obsolete("No Encoding for Name field is specified, any non-ASCII bytes will be discarded")] public static TarArchive CreateInputTarArchive(Stream inputStream) { - if (inputStream == null) { + return CreateInputTarArchive(inputStream, null); + } + + /// + /// The InputStream based constructors create a TarArchive for the + /// purposes of extracting or listing a tar archive. Thus, use + /// these constructors when you wish to extract files from or list + /// the contents of an existing tar archive. + /// + /// The stream to retrieve archive data from. + /// The used for the Name fields, or null for ASCII only + /// Returns a new suitable for reading from. + public static TarArchive CreateInputTarArchive(Stream inputStream, Encoding nameEncoding) + { + if (inputStream == null) + { throw new ArgumentNullException(nameof(inputStream)); } var tarStream = inputStream as TarInputStream; TarArchive result; - if (tarStream != null) { + if (tarStream != null) + { result = new TarArchive(tarStream); - } else { - result = CreateInputTarArchive(inputStream, TarBuffer.DefaultBlockFactor); + } + else + { + result = CreateInputTarArchive(inputStream, TarBuffer.DefaultBlockFactor, nameEncoding); } return result; } @@ -117,40 +144,68 @@ public static TarArchive CreateInputTarArchive(Stream inputStream) /// A stream containing the tar archive contents /// The blocking factor to apply /// Returns a suitable for reading. + [Obsolete("No Encoding for Name field is specified, any non-ASCII bytes will be discarded")] public static TarArchive CreateInputTarArchive(Stream inputStream, int blockFactor) { - if (inputStream == null) { + return CreateInputTarArchive(inputStream, blockFactor, null); + } + + /// + /// Create TarArchive for reading setting block factor + /// + /// A stream containing the tar archive contents + /// The blocking factor to apply + /// The used for the Name fields, or null for ASCII only + /// Returns a suitable for reading. + public static TarArchive CreateInputTarArchive(Stream inputStream, int blockFactor, Encoding nameEncoding) + { + if (inputStream == null) + { throw new ArgumentNullException(nameof(inputStream)); } - if (inputStream is TarInputStream) { + if (inputStream is TarInputStream) + { throw new ArgumentException("TarInputStream not valid"); } - return new TarArchive(new TarInputStream(inputStream, blockFactor)); + return new TarArchive(new TarInputStream(inputStream, blockFactor, nameEncoding)); } - /// /// Create a TarArchive for writing to, using the default blocking factor /// /// The to write to + /// The used for the Name fields, or null for ASCII only /// Returns a suitable for writing. - public static TarArchive CreateOutputTarArchive(Stream outputStream) + public static TarArchive CreateOutputTarArchive(Stream outputStream, Encoding nameEncoding) { - if (outputStream == null) { + if (outputStream == null) + { throw new ArgumentNullException(nameof(outputStream)); } var tarStream = outputStream as TarOutputStream; TarArchive result; - if (tarStream != null) { + if (tarStream != null) + { result = new TarArchive(tarStream); - } else { - result = CreateOutputTarArchive(outputStream, TarBuffer.DefaultBlockFactor); + } + else + { + result = CreateOutputTarArchive(outputStream, TarBuffer.DefaultBlockFactor, nameEncoding); } return result; } + /// + /// Create a TarArchive for writing to, using the default blocking factor + /// + /// The to write to + /// Returns a suitable for writing. + public static TarArchive CreateOutputTarArchive(Stream outputStream) + { + return CreateOutputTarArchive(outputStream, null); + } /// /// Create a tar archive for writing. @@ -160,17 +215,31 @@ public static TarArchive CreateOutputTarArchive(Stream outputStream) /// Returns a suitable for writing. public static TarArchive CreateOutputTarArchive(Stream outputStream, int blockFactor) { - if (outputStream == null) { + return CreateOutputTarArchive(outputStream, blockFactor, null); + } + /// + /// Create a tar archive for writing. + /// + /// The stream to write to + /// The blocking factor to use for buffering. + /// The used for the Name fields, or null for ASCII only + /// Returns a suitable for writing. + public static TarArchive CreateOutputTarArchive(Stream outputStream, int blockFactor, Encoding nameEncoding) + { + if (outputStream == null) + { throw new ArgumentNullException(nameof(outputStream)); } - if (outputStream is TarOutputStream) { + if (outputStream is TarOutputStream) + { throw new ArgumentException("TarOutputStream is not valid"); } - return new TarArchive(new TarOutputStream(outputStream, blockFactor)); + return new TarArchive(new TarOutputStream(outputStream, blockFactor, nameEncoding)); } - #endregion + + #endregion Static factory methods /// /// Set the flag that determines whether existing files are @@ -181,7 +250,8 @@ public static TarArchive CreateOutputTarArchive(Stream outputStream, int blockFa /// public void SetKeepOldFiles(bool keepExistingFiles) { - if (isDisposed) { + if (isDisposed) + { throw new ObjectDisposedException("TarArchive"); } @@ -190,30 +260,34 @@ public void SetKeepOldFiles(bool keepExistingFiles) /// /// Get/set the ascii file translation flag. If ascii file translation - /// is true, then the file is checked to see if it a binary file or not. - /// If the flag is true and the test indicates it is ascii text + /// is true, then the file is checked to see if it a binary file or not. + /// If the flag is true and the test indicates it is ascii text /// file, it will be translated. The translation converts the local /// operating system's concept of line ends into the UNIX line end, /// '\n', which is the defacto standard for a TAR archive. This makes /// text files compatible with UNIX. /// - public bool AsciiTranslate { - get { - if (isDisposed) { + public bool AsciiTranslate + { + get + { + if (isDisposed) + { throw new ObjectDisposedException("TarArchive"); } return asciiTranslate; } - set { - if (isDisposed) { + set + { + if (isDisposed) + { throw new ObjectDisposedException("TarArchive"); } asciiTranslate = value; } - } /// @@ -225,7 +299,8 @@ public bool AsciiTranslate { [Obsolete("Use the AsciiTranslate property")] public void SetAsciiTranslation(bool translateAsciiFiles) { - if (isDisposed) { + if (isDisposed) + { throw new ObjectDisposedException("TarArchive"); } @@ -234,52 +309,60 @@ public void SetAsciiTranslation(bool translateAsciiFiles) /// /// PathPrefix is added to entry names as they are written if the value is not null. - /// A slash character is appended after PathPrefix + /// A slash character is appended after PathPrefix /// - public string PathPrefix { - get { - if (isDisposed) { + public string PathPrefix + { + get + { + if (isDisposed) + { throw new ObjectDisposedException("TarArchive"); } return pathPrefix; } - set { - if (isDisposed) { + set + { + if (isDisposed) + { throw new ObjectDisposedException("TarArchive"); } pathPrefix = value; } - } /// /// RootPath is removed from entry names if it is found at the /// beginning of the name. /// - public string RootPath { - get { - if (isDisposed) { + public string RootPath + { + get + { + if (isDisposed) + { throw new ObjectDisposedException("TarArchive"); } return rootPath; } - set { - if (isDisposed) { + set + { + if (isDisposed) + { throw new ObjectDisposedException("TarArchive"); } - // Convert to forward slashes for matching. Trim trailing / for correct final path - rootPath = value.Replace('\\', '/').TrimEnd('/'); + rootPath = value.ToTarArchivePath().TrimEnd('/'); } } /// /// Set user and group information that will be used to fill in the - /// tar archive's entry headers. This information is based on that available + /// tar archive's entry headers. This information is based on that available /// for the linux operating system, which is not always available on other /// operating systems. TarArchive allows the programmer to specify values /// to be used in their place. @@ -299,7 +382,8 @@ public string RootPath { /// public void SetUserInfo(int userId, string userName, int groupId, string groupName) { - if (isDisposed) { + if (isDisposed) + { throw new ObjectDisposedException("TarArchive"); } @@ -314,17 +398,22 @@ public void SetUserInfo(int userId, string userName, int groupId, string groupNa /// Get or set a value indicating if overrides defined by SetUserInfo should be applied. /// /// If overrides are not applied then the values as set in each header will be used. - public bool ApplyUserInfoOverrides { - get { - if (isDisposed) { + public bool ApplyUserInfoOverrides + { + get + { + if (isDisposed) + { throw new ObjectDisposedException("TarArchive"); } return applyUserInfoOverrides; } - set { - if (isDisposed) { + set + { + if (isDisposed) + { throw new ObjectDisposedException("TarArchive"); } @@ -340,9 +429,12 @@ public bool ApplyUserInfoOverrides { /// /// The current user id. /// - public int UserId { - get { - if (isDisposed) { + public int UserId + { + get + { + if (isDisposed) + { throw new ObjectDisposedException("TarArchive"); } @@ -358,9 +450,12 @@ public int UserId { /// /// The current user name. /// - public string UserName { - get { - if (isDisposed) { + public string UserName + { + get + { + if (isDisposed) + { throw new ObjectDisposedException("TarArchive"); } @@ -376,9 +471,12 @@ public string UserName { /// /// The current group id. /// - public int GroupId { - get { - if (isDisposed) { + public int GroupId + { + get + { + if (isDisposed) + { throw new ObjectDisposedException("TarArchive"); } @@ -394,9 +492,12 @@ public int GroupId { /// /// The current group name. /// - public string GroupName { - get { - if (isDisposed) { + public string GroupName + { + get + { + if (isDisposed) + { throw new ObjectDisposedException("TarArchive"); } @@ -414,15 +515,21 @@ public string GroupName { /// /// The record size this archive is using. /// - public int RecordSize { - get { - if (isDisposed) { + public int RecordSize + { + get + { + if (isDisposed) + { throw new ObjectDisposedException("TarArchive"); } - if (tarIn != null) { + if (tarIn != null) + { return tarIn.RecordSize; - } else if (tarOut != null) { + } + else if (tarOut != null) + { return tarOut.RecordSize; } return TarBuffer.DefaultRecordSize; @@ -433,11 +540,16 @@ public int RecordSize { /// Sets the IsStreamOwner property on the underlying stream. /// Set this to false to prevent the Close of the TarArchive from closing the stream. /// - public bool IsStreamOwner { - set { - if (tarIn != null) { + public bool IsStreamOwner + { + set + { + if (tarIn != null) + { tarIn.IsStreamOwner = value; - } else { + } + else + { tarOut.IsStreamOwner = value; } } @@ -454,20 +566,23 @@ public void CloseArchive() /// /// Perform the "list" command for the archive contents. - /// + /// /// NOTE That this method uses the progress event to actually list /// the contents. If the progress display event is not set, nothing will be listed! /// public void ListContents() { - if (isDisposed) { + if (isDisposed) + { throw new ObjectDisposedException("TarArchive"); } - while (true) { + while (true) + { TarEntry entry = tarIn.GetNextEntry(); - if (entry == null) { + if (entry == null) + { break; } OnProgressMessageEvent(entry, null); @@ -480,23 +595,38 @@ public void ListContents() /// /// The destination directory into which to extract. /// - public void ExtractContents(string destinationDirectory) + public void ExtractContents(string destinationDirectory) + => ExtractContents(destinationDirectory, false); + + /// + /// Perform the "extract" command and extract the contents of the archive. + /// + /// + /// The destination directory into which to extract. + /// + /// Allow parent directory traversal in file paths (e.g. ../file) + public void ExtractContents(string destinationDirectory, bool allowParentTraversal) { - if (isDisposed) { + if (isDisposed) + { throw new ObjectDisposedException("TarArchive"); } - while (true) { + var fullDistDir = Path.GetFullPath(destinationDirectory).TrimEnd('/', '\\'); + + while (true) + { TarEntry entry = tarIn.GetNextEntry(); - if (entry == null) { + if (entry == null) + { break; } if (entry.TarHeader.TypeFlag == TarHeader.LF_LINK || entry.TarHeader.TypeFlag == TarHeader.LF_SYMLINK) continue; - ExtractEntry(destinationDirectory, entry); + ExtractEntry(fullDistDir, entry, allowParentTraversal); } } @@ -510,13 +640,15 @@ public void ExtractContents(string destinationDirectory) /// /// The TarEntry returned by tarIn.GetNextEntry(). /// - void ExtractEntry(string destDir, TarEntry entry) + /// Allow parent directory traversal in file paths (e.g. ../file) + private void ExtractEntry(string destDir, TarEntry entry, bool allowParentTraversal) { OnProgressMessageEvent(entry, null); string name = entry.Name; - if (Path.IsPathRooted(name)) { + if (Path.IsPathRooted(name)) + { // NOTE: // for UNC names... \\machine\share\zoom\beet.txt gives \zoom\beet.txt name = name.Substring(Path.GetPathRoot(name).Length); @@ -525,67 +657,96 @@ void ExtractEntry(string destDir, TarEntry entry) name = name.Replace('/', Path.DirectorySeparatorChar); string destFile = Path.Combine(destDir, name); + var destFileDir = Path.GetDirectoryName(Path.GetFullPath(destFile)) ?? ""; + + var isRootDir = entry.IsDirectory && entry.Name == ""; + + if (!allowParentTraversal && !isRootDir && !destFileDir.StartsWith(destDir, StringComparison.InvariantCultureIgnoreCase)) + { + throw new InvalidNameException("Parent traversal in paths is not allowed"); + } - if (entry.IsDirectory) { + if (entry.IsDirectory) + { EnsureDirectoryExists(destFile); - } else { + } + else + { string parentDirectory = Path.GetDirectoryName(destFile); EnsureDirectoryExists(parentDirectory); bool process = true; var fileInfo = new FileInfo(destFile); - if (fileInfo.Exists) { - if (keepOldFiles) { + if (fileInfo.Exists) + { + if (keepOldFiles) + { OnProgressMessageEvent(entry, "Destination file already exists"); process = false; - } else if ((fileInfo.Attributes & FileAttributes.ReadOnly) != 0) { + } + else if ((fileInfo.Attributes & FileAttributes.ReadOnly) != 0) + { OnProgressMessageEvent(entry, "Destination file already exists, and is read-only"); process = false; } } - if (process) { - bool asciiTrans = false; - - Stream outputStream = File.Create(destFile); - if (this.asciiTranslate) { - asciiTrans = !IsBinary(destFile); + if (process) + { + using (var outputStream = File.Create(destFile)) + { + if (this.asciiTranslate) + { + // May need to translate the file. + ExtractAndTranslateEntry(destFile, outputStream); + } + else + { + // If translation is disabled, just copy the entry across directly. + tarIn.CopyEntryContents(outputStream); + } } + } + } + } - StreamWriter outw = null; - if (asciiTrans) { - outw = new StreamWriter(outputStream); - } + // Extract a TAR entry, and perform an ASCII translation if required. + private void ExtractAndTranslateEntry(string destFile, Stream outputStream) + { + bool asciiTrans = !IsBinary(destFile); + if (asciiTrans) + { + using (var outw = new StreamWriter(outputStream, new UTF8Encoding(false), 1024, true)) + { byte[] rdbuf = new byte[32 * 1024]; - while (true) { + while (true) + { int numRead = tarIn.Read(rdbuf, 0, rdbuf.Length); - if (numRead <= 0) { + if (numRead <= 0) + { break; } - if (asciiTrans) { - for (int off = 0, b = 0; b < numRead; ++b) { - if (rdbuf[b] == 10) { - string s = Encoding.ASCII.GetString(rdbuf, off, (b - off)); - outw.WriteLine(s); - off = b + 1; - } + for (int off = 0, b = 0; b < numRead; ++b) + { + if (rdbuf[b] == 10) + { + string s = Encoding.ASCII.GetString(rdbuf, off, (b - off)); + outw.WriteLine(s); + off = b + 1; } - } else { - outputStream.Write(rdbuf, 0, numRead); } } - - if (asciiTrans) { - outw.Dispose(); - } else { - outputStream.Dispose(); - } } } + else + { + // No translation required. + tarIn.CopyEntryContents(outputStream); + } } /// @@ -603,22 +764,29 @@ void ExtractEntry(string destDir, TarEntry entry) /// public void WriteEntry(TarEntry sourceEntry, bool recurse) { - if (sourceEntry == null) { + if (sourceEntry == null) + { throw new ArgumentNullException(nameof(sourceEntry)); } - if (isDisposed) { + if (isDisposed) + { throw new ObjectDisposedException("TarArchive"); } - try { - if (recurse) { + try + { + if (recurse) + { TarHeader.SetValueDefaults(sourceEntry.UserId, sourceEntry.UserName, sourceEntry.GroupId, sourceEntry.GroupName); } WriteEntryCore(sourceEntry, recurse); - } finally { - if (recurse) { + } + finally + { + if (recurse) + { TarHeader.RestoreSetValues(); } } @@ -637,14 +805,15 @@ public void WriteEntry(TarEntry sourceEntry, bool recurse) /// /// If true, process the children of directory entries. /// - void WriteEntryCore(TarEntry sourceEntry, bool recurse) + private void WriteEntryCore(TarEntry sourceEntry, bool recurse) { string tempFileName = null; string entryFilename = sourceEntry.File; var entry = (TarEntry)sourceEntry.Clone(); - if (applyUserInfoOverrides) { + if (applyUserInfoOverrides) + { entry.GroupId = groupId; entry.GroupName = groupName; entry.UserId = userId; @@ -653,17 +822,21 @@ void WriteEntryCore(TarEntry sourceEntry, bool recurse) OnProgressMessageEvent(entry, null); - if (asciiTranslate && !entry.IsDirectory) { - - if (!IsBinary(entryFilename)) { - tempFileName = Path.GetTempFileName(); - - using (StreamReader inStream = File.OpenText(entryFilename)) { - using (Stream outStream = File.Create(tempFileName)) { - - while (true) { + if (asciiTranslate && !entry.IsDirectory) + { + if (!IsBinary(entryFilename)) + { + tempFileName = PathUtils.GetTempFileName(); + + using (StreamReader inStream = File.OpenText(entryFilename)) + { + using (Stream outStream = File.Create(tempFileName)) + { + while (true) + { string line = inStream.ReadLine(); - if (line == null) { + if (line == null) + { break; } byte[] data = Encoding.ASCII.GetBytes(line); @@ -682,36 +855,48 @@ void WriteEntryCore(TarEntry sourceEntry, bool recurse) string newName = null; - if (rootPath != null) { - if (entry.Name.StartsWith(rootPath, StringComparison.OrdinalIgnoreCase)) { + if (!String.IsNullOrEmpty(rootPath)) + { + if (entry.Name.StartsWith(rootPath, StringComparison.OrdinalIgnoreCase)) + { newName = entry.Name.Substring(rootPath.Length + 1); } } - if (pathPrefix != null) { + if (pathPrefix != null) + { newName = (newName == null) ? pathPrefix + "/" + entry.Name : pathPrefix + "/" + newName; } - if (newName != null) { + if (newName != null) + { entry.Name = newName; } tarOut.PutNextEntry(entry); - if (entry.IsDirectory) { - if (recurse) { + if (entry.IsDirectory) + { + if (recurse) + { TarEntry[] list = entry.GetDirectoryEntries(); - for (int i = 0; i < list.Length; ++i) { + for (int i = 0; i < list.Length; ++i) + { WriteEntryCore(list[i], recurse); } } - } else { - using (Stream inputStream = File.OpenRead(entryFilename)) { + } + else + { + using (Stream inputStream = File.OpenRead(entryFilename)) + { byte[] localBuffer = new byte[32 * 1024]; - while (true) { + while (true) + { int numRead = inputStream.Read(localBuffer, 0, localBuffer.Length); - if (numRead <= 0) { + if (numRead <= 0) + { break; } @@ -719,7 +904,8 @@ void WriteEntryCore(TarEntry sourceEntry, bool recurse) } } - if (!string.IsNullOrEmpty(tempFileName)) { + if (!string.IsNullOrEmpty(tempFileName)) + { File.Delete(tempFileName); } @@ -743,15 +929,19 @@ public void Dispose() /// false to release only unmanaged resources. protected virtual void Dispose(bool disposing) { - if (!isDisposed) { + if (!isDisposed) + { isDisposed = true; - if (disposing) { - if (tarOut != null) { + if (disposing) + { + if (tarOut != null) + { tarOut.Flush(); tarOut.Dispose(); } - if (tarIn != null) { + if (tarIn != null) + { tarIn.Dispose(); } } @@ -775,12 +965,16 @@ public virtual void Close() Dispose(false); } - static void EnsureDirectoryExists(string directoryName) + private static void EnsureDirectoryExists(string directoryName) { - if (!Directory.Exists(directoryName)) { - try { + if (!Directory.Exists(directoryName)) + { + try + { Directory.CreateDirectory(directoryName); - } catch (Exception e) { + } + catch (Exception e) + { throw new TarException("Exception creating directory '" + directoryName + "', " + e.Message, e); } } @@ -790,17 +984,20 @@ static void EnsureDirectoryExists(string directoryName) // It no longer reads entire files into memory but is still a weak test! // This assumes that byte values 0-7, 14-31 or 255 are binary // and that all non text files contain one of these values - static bool IsBinary(string filename) + private static bool IsBinary(string filename) { - using (FileStream fs = File.OpenRead(filename)) { + using (FileStream fs = File.OpenRead(filename)) + { int sampleSize = Math.Min(4096, (int)fs.Length); byte[] content = new byte[sampleSize]; int bytesRead = fs.Read(content, 0, sampleSize); - for (int i = 0; i < bytesRead; ++i) { + for (int i = 0; i < bytesRead; ++i) + { byte b = content[i]; - if ((b < 8) || ((b > 13) && (b < 32)) || (b == 255)) { + if ((b < 8) || ((b > 13) && (b < 32)) || (b == 255)) + { return true; } } @@ -809,22 +1006,24 @@ static bool IsBinary(string filename) } #region Instance Fields - bool keepOldFiles; - bool asciiTranslate; - int userId; - string userName = string.Empty; - int groupId; - string groupName = string.Empty; + private bool keepOldFiles; + private bool asciiTranslate; + + private int userId; + private string userName = string.Empty; + private int groupId; + private string groupName = string.Empty; + + private string rootPath; + private string pathPrefix; - string rootPath; - string pathPrefix; + private bool applyUserInfoOverrides; - bool applyUserInfoOverrides; + private TarInputStream tarIn; + private TarOutputStream tarOut; + private bool isDisposed; - TarInputStream tarIn; - TarOutputStream tarOut; - bool isDisposed; - #endregion + #endregion Instance Fields } } diff --git a/src/ICSharpCode.SharpZipLib/Tar/TarBuffer.cs b/src/ICSharpCode.SharpZipLib/Tar/TarBuffer.cs index 3e399a347..a0f9bab80 100644 --- a/src/ICSharpCode.SharpZipLib/Tar/TarBuffer.cs +++ b/src/ICSharpCode.SharpZipLib/Tar/TarBuffer.cs @@ -1,5 +1,8 @@ using System; +using System.Buffers; using System.IO; +using System.Threading; +using System.Threading.Tasks; namespace ICSharpCode.SharpZipLib.Tar { @@ -17,7 +20,6 @@ namespace ICSharpCode.SharpZipLib.Tar /// public class TarBuffer { - /* A quote from GNU tar man file on blocking and records A `tar' archive file contains a series of blocks. Each block contains `BLOCKSIZE' bytes. Although this format may be thought of as @@ -41,6 +43,7 @@ or which contains garbage records after a zero block. */ #region Constants + /// /// The size of a block in a tar archive in bytes. /// @@ -62,17 +65,17 @@ or which contains garbage records after a zero block. /// The default size is 10KB. /// public const int DefaultRecordSize = BlockSize * DefaultBlockFactor; - #endregion + + #endregion Constants /// /// Get the record size for this buffer /// /// The record size in bytes. /// This is equal to the multiplied by the - public int RecordSize { - get { - return recordSize; - } + public int RecordSize + { + get { return recordSize; } } /// @@ -90,10 +93,9 @@ public int GetRecordSize() /// Get the Blocking factor for the buffer /// /// This is the number of blocks in each record. - public int BlockFactor { - get { - return blockFactor; - } + public int BlockFactor + { + get { return blockFactor; } } /// @@ -120,7 +122,8 @@ protected TarBuffer() /// A new suitable for input. public static TarBuffer CreateInputTarBuffer(Stream inputStream) { - if (inputStream == null) { + if (inputStream == null) + { throw new ArgumentNullException(nameof(inputStream)); } @@ -135,11 +138,13 @@ public static TarBuffer CreateInputTarBuffer(Stream inputStream) /// A new suitable for input. public static TarBuffer CreateInputTarBuffer(Stream inputStream, int blockFactor) { - if (inputStream == null) { + if (inputStream == null) + { throw new ArgumentNullException(nameof(inputStream)); } - if (blockFactor <= 0) { + if (blockFactor <= 0) + { throw new ArgumentOutOfRangeException(nameof(blockFactor), "Factor cannot be negative"); } @@ -158,7 +163,8 @@ public static TarBuffer CreateInputTarBuffer(Stream inputStream, int blockFactor /// A new suitable for output. public static TarBuffer CreateOutputTarBuffer(Stream outputStream) { - if (outputStream == null) { + if (outputStream == null) + { throw new ArgumentNullException(nameof(outputStream)); } @@ -173,11 +179,13 @@ public static TarBuffer CreateOutputTarBuffer(Stream outputStream) /// A new suitable for output. public static TarBuffer CreateOutputTarBuffer(Stream outputStream, int blockFactor) { - if (outputStream == null) { + if (outputStream == null) + { throw new ArgumentNullException(nameof(outputStream)); } - if (blockFactor <= 0) { + if (blockFactor <= 0) + { throw new ArgumentOutOfRangeException(nameof(blockFactor), "Factor cannot be negative"); } @@ -192,16 +200,19 @@ public static TarBuffer CreateOutputTarBuffer(Stream outputStream, int blockFact /// /// Initialization common to all constructors. /// - void Initialize(int archiveBlockFactor) + private void Initialize(int archiveBlockFactor) { blockFactor = archiveBlockFactor; recordSize = archiveBlockFactor * BlockSize; - recordBuffer = new byte[RecordSize]; + recordBuffer = ArrayPool.Shared.Rent(RecordSize); - if (inputStream != null) { + if (inputStream != null) + { currentRecordIndex = -1; currentBlockIndex = BlockFactor; - } else { + } + else + { currentRecordIndex = 0; currentBlockIndex = 0; } @@ -219,16 +230,20 @@ void Initialize(int archiveBlockFactor) [Obsolete("Use IsEndOfArchiveBlock instead")] public bool IsEOFBlock(byte[] block) { - if (block == null) { + if (block == null) + { throw new ArgumentNullException(nameof(block)); } - if (block.Length != BlockSize) { + if (block.Length != BlockSize) + { throw new ArgumentException("block length is invalid"); } - for (int i = 0; i < BlockSize; ++i) { - if (block[i] != 0) { + for (int i = 0; i < BlockSize; ++i) + { + if (block[i] != 0) + { return false; } } @@ -236,7 +251,6 @@ public bool IsEOFBlock(byte[] block) return true; } - /// /// Determine if an archive block indicates the End of an Archive has been reached. /// End of archive is indicated by a block that consists entirely of null bytes. @@ -248,16 +262,20 @@ public bool IsEOFBlock(byte[] block) /// Returns true if the block is an EOF block; false otherwise. public static bool IsEndOfArchiveBlock(byte[] block) { - if (block == null) { + if (block == null) + { throw new ArgumentNullException(nameof(block)); } - if (block.Length != BlockSize) { + if (block.Length != BlockSize) + { throw new ArgumentException("block length is invalid"); } - for (int i = 0; i < BlockSize; ++i) { - if (block[i] != 0) { + for (int i = 0; i < BlockSize; ++i) + { + if (block[i] != 0) + { return false; } } @@ -268,14 +286,24 @@ public static bool IsEndOfArchiveBlock(byte[] block) /// /// Skip over a block on the input stream. /// - public void SkipBlock() + public void SkipBlock() => SkipBlockAsync(CancellationToken.None, false).GetAwaiter().GetResult(); + + /// + /// Skip over a block on the input stream. + /// + public Task SkipBlockAsync(CancellationToken ct) => SkipBlockAsync(ct, true).AsTask(); + + private async ValueTask SkipBlockAsync(CancellationToken ct, bool isAsync) { - if (inputStream == null) { + if (inputStream == null) + { throw new TarException("no input stream defined"); } - if (currentBlockIndex >= BlockFactor) { - if (!ReadRecord()) { + if (currentBlockIndex >= BlockFactor) + { + if (!await ReadRecordAsync(ct, isAsync).ConfigureAwait(false)) + { throw new TarException("Failed to read a record"); } } @@ -291,12 +319,15 @@ public void SkipBlock() /// public byte[] ReadBlock() { - if (inputStream == null) { + if (inputStream == null) + { throw new TarException("TarBuffer.ReadBlock - no input stream defined"); } - if (currentBlockIndex >= BlockFactor) { - if (!ReadRecord()) { + if (currentBlockIndex >= BlockFactor) + { + if (!ReadRecordAsync(CancellationToken.None, false).GetAwaiter().GetResult()) + { throw new TarException("Failed to read a record"); } } @@ -308,16 +339,41 @@ public byte[] ReadBlock() return result; } + internal async ValueTask ReadBlockIntAsync(byte[] buffer, CancellationToken ct, bool isAsync) + { + if (buffer.Length != BlockSize) + { + throw new ArgumentException("BUG: buffer must have length BlockSize"); + } + + if (inputStream == null) + { + throw new TarException("TarBuffer.ReadBlock - no input stream defined"); + } + + if (currentBlockIndex >= BlockFactor) + { + if (!await ReadRecordAsync(ct, isAsync).ConfigureAwait(false)) + { + throw new TarException("Failed to read a record"); + } + } + + recordBuffer.AsSpan().Slice(currentBlockIndex * BlockSize, BlockSize).CopyTo(buffer); + currentBlockIndex++; + } + /// /// Read a record from data stream. /// /// /// false if End-Of-File, else true. /// - bool ReadRecord() + private async ValueTask ReadRecordAsync(CancellationToken ct, bool isAsync) { - if (inputStream == null) { - throw new TarException("no input stream stream defined"); + if (inputStream == null) + { + throw new TarException("no input stream defined"); } currentBlockIndex = 0; @@ -325,8 +381,11 @@ bool ReadRecord() int offset = 0; int bytesNeeded = RecordSize; - while (bytesNeeded > 0) { - long numBytes = inputStream.Read(recordBuffer, offset, bytesNeeded); + while (bytesNeeded > 0) + { + long numBytes = isAsync + ? await inputStream.ReadAsync(recordBuffer, offset, bytesNeeded, ct).ConfigureAwait(false) + : inputStream.Read(recordBuffer, offset, bytesNeeded); // // NOTE @@ -341,7 +400,13 @@ bool ReadRecord() // // Thanks to 'Yohann.Roussel@alcatel.fr' for this fix. // - if (numBytes <= 0) { + if (numBytes <= 0) + { + // Fill the rest of the buffer with 0 to clear any left over data in the shared buffer + for (; offset < RecordSize; offset++) + { + recordBuffer[offset] = 0; + } break; } @@ -358,7 +423,8 @@ bool ReadRecord() /// /// Block numbers are zero based values /// - public int CurrentBlock { + public int CurrentBlock + { get { return currentBlockIndex; } } @@ -390,7 +456,8 @@ public int GetCurrentBlockNum() /// /// The current zero based record number. /// - public int CurrentRecord { + public int CurrentRecord + { get { return currentRecordIndex; } } @@ -406,6 +473,18 @@ public int GetCurrentRecordNum() return currentRecordIndex; } + /// + /// Write a block of data to the archive. + /// + /// + /// The data to write to the archive. + /// + /// + public ValueTask WriteBlockAsync(byte[] block, CancellationToken ct) + { + return WriteBlockAsync(block, 0, ct); + } + /// /// Write a block of data to the archive. /// @@ -414,26 +493,24 @@ public int GetCurrentRecordNum() /// public void WriteBlock(byte[] block) { - if (block == null) { - throw new ArgumentNullException(nameof(block)); - } - - if (outputStream == null) { - throw new TarException("TarBuffer.WriteBlock - no output stream defined"); - } - - if (block.Length != BlockSize) { - string errorText = string.Format("TarBuffer.WriteBlock - block to write has length '{0}' which is not the block size of '{1}'", - block.Length, BlockSize); - throw new TarException(errorText); - } - - if (currentBlockIndex >= BlockFactor) { - WriteRecord(); - } + WriteBlock(block, 0); + } - Array.Copy(block, 0, recordBuffer, (currentBlockIndex * BlockSize), BlockSize); - currentBlockIndex++; + /// + /// Write an archive record to the archive, where the record may be + /// inside of a larger array buffer. The buffer must be "offset plus + /// record size" long. + /// + /// + /// The buffer containing the record data to write. + /// + /// + /// The offset of the record data within buffer. + /// + /// + public ValueTask WriteBlockAsync(byte[] buffer, int offset, CancellationToken ct) + { + return WriteBlockAsync(buffer, offset, ct, true); } /// @@ -449,26 +526,37 @@ public void WriteBlock(byte[] block) /// public void WriteBlock(byte[] buffer, int offset) { - if (buffer == null) { + WriteBlockAsync(buffer, offset, CancellationToken.None, false).GetAwaiter().GetResult(); + } + + internal async ValueTask WriteBlockAsync(byte[] buffer, int offset, CancellationToken ct, bool isAsync) + { + if (buffer == null) + { throw new ArgumentNullException(nameof(buffer)); } - if (outputStream == null) { - throw new TarException("TarBuffer.WriteBlock - no output stream stream defined"); + if (outputStream == null) + { + throw new TarException("TarBuffer.WriteBlock - no output stream defined"); } - if ((offset < 0) || (offset >= buffer.Length)) { + if ((offset < 0) || (offset >= buffer.Length)) + { throw new ArgumentOutOfRangeException(nameof(offset)); } - if ((offset + BlockSize) > buffer.Length) { - string errorText = string.Format("TarBuffer.WriteBlock - record has length '{0}' with offset '{1}' which is less than the record size of '{2}'", + if ((offset + BlockSize) > buffer.Length) + { + string errorText = string.Format( + "TarBuffer.WriteBlock - record has length '{0}' with offset '{1}' which is less than the record size of '{2}'", buffer.Length, offset, recordSize); throw new TarException(errorText); } - if (currentBlockIndex >= BlockFactor) { - WriteRecord(); + if (currentBlockIndex >= BlockFactor) + { + await WriteRecordAsync(CancellationToken.None, isAsync).ConfigureAwait(false); } Array.Copy(buffer, offset, recordBuffer, (currentBlockIndex * BlockSize), BlockSize); @@ -479,14 +567,23 @@ public void WriteBlock(byte[] buffer, int offset) /// /// Write a TarBuffer record to the archive. /// - void WriteRecord() + private async ValueTask WriteRecordAsync(CancellationToken ct, bool isAsync) { - if (outputStream == null) { + if (outputStream == null) + { throw new TarException("TarBuffer.WriteRecord no output stream defined"); } - outputStream.Write(recordBuffer, 0, RecordSize); - outputStream.Flush(); + if (isAsync) + { + await outputStream.WriteAsync(recordBuffer, 0, RecordSize, ct).ConfigureAwait(false); + await outputStream.FlushAsync(ct).ConfigureAwait(false); + } + else + { + outputStream.Write(recordBuffer, 0, RecordSize); + outputStream.Flush(); + } currentBlockIndex = 0; currentRecordIndex++; @@ -497,52 +594,102 @@ void WriteRecord() /// /// Any trailing bytes are set to zero which is by definition correct behaviour /// for the end of a tar stream. - void WriteFinalRecord() + private async ValueTask WriteFinalRecordAsync(CancellationToken ct, bool isAsync) { - if (outputStream == null) { + if (outputStream == null) + { throw new TarException("TarBuffer.WriteFinalRecord no output stream defined"); } - if (currentBlockIndex > 0) { + if (currentBlockIndex > 0) + { int dataBytes = currentBlockIndex * BlockSize; Array.Clear(recordBuffer, dataBytes, RecordSize - dataBytes); - WriteRecord(); + await WriteRecordAsync(ct, isAsync).ConfigureAwait(false); } - outputStream.Flush(); + if (isAsync) + { + await outputStream.FlushAsync(ct).ConfigureAwait(false); + } + else + { + outputStream.Flush(); + } } /// /// Close the TarBuffer. If this is an output buffer, also flush the /// current block before closing. /// - public void Close() - { - if (outputStream != null) { - WriteFinalRecord(); + public void Close() => CloseAsync(CancellationToken.None, false).GetAwaiter().GetResult(); + + /// + /// Close the TarBuffer. If this is an output buffer, also flush the + /// current block before closing. + /// + public Task CloseAsync(CancellationToken ct) => CloseAsync(ct, true).AsTask(); - if (IsStreamOwner) { - outputStream.Dispose(); + private async ValueTask CloseAsync(CancellationToken ct, bool isAsync) + { + if (outputStream != null) + { + await WriteFinalRecordAsync(ct, isAsync).ConfigureAwait(false); + + if (IsStreamOwner) + { + if (isAsync) + { +#if NETSTANDARD2_1_OR_GREATER || NETCOREAPP3_1_OR_GREATER + await outputStream.DisposeAsync().ConfigureAwait(false); +#else + outputStream.Dispose(); +#endif + } + else + { + outputStream.Dispose(); + } } + outputStream = null; - } else if (inputStream != null) { - if (IsStreamOwner) { - inputStream.Dispose(); + } + else if (inputStream != null) + { + if (IsStreamOwner) + { + if (isAsync) + { +#if NETSTANDARD2_1_OR_GREATER || NETCOREAPP3_1_OR_GREATER + await inputStream.DisposeAsync().ConfigureAwait(false); +#else + inputStream.Dispose(); +#endif + } + else + { + inputStream.Dispose(); + } } + inputStream = null; } + + ArrayPool.Shared.Return(recordBuffer); } #region Instance Fields - Stream inputStream; - Stream outputStream; - byte[] recordBuffer; - int currentBlockIndex; - int currentRecordIndex; + private Stream inputStream; + private Stream outputStream; + + private byte[] recordBuffer; + private int currentBlockIndex; + private int currentRecordIndex; + + private int recordSize = DefaultRecordSize; + private int blockFactor = DefaultBlockFactor; - int recordSize = DefaultRecordSize; - int blockFactor = DefaultBlockFactor; - #endregion + #endregion Instance Fields } } diff --git a/src/ICSharpCode.SharpZipLib/Tar/TarEntry.cs b/src/ICSharpCode.SharpZipLib/Tar/TarEntry.cs index 11f13c5d1..82c813367 100644 --- a/src/ICSharpCode.SharpZipLib/Tar/TarEntry.cs +++ b/src/ICSharpCode.SharpZipLib/Tar/TarEntry.cs @@ -1,5 +1,7 @@ using System; using System.IO; +using System.Text; +using ICSharpCode.SharpZipLib.Core; namespace ICSharpCode.SharpZipLib.Tar { @@ -33,6 +35,7 @@ namespace ICSharpCode.SharpZipLib.Tar public class TarEntry { #region Constructors + /// /// Initialise a default instance of . /// @@ -48,10 +51,25 @@ private TarEntry() /// /// The header bytes from a tar archive entry. /// - public TarEntry(byte[] headerBuffer) + [Obsolete("No Encoding for Name field is specified, any non-ASCII bytes will be discarded")] + public TarEntry(byte[] headerBuffer) : this(headerBuffer, null) + { + } + + /// + /// Construct an entry from an archive's header bytes. File is set + /// to null. + /// + /// + /// The header bytes from a tar archive entry. + /// + /// + /// The used for the Name fields, or null for ASCII only + /// + public TarEntry(byte[] headerBuffer, Encoding nameEncoding) { header = new TarHeader(); - header.ParseBuffer(headerBuffer); + header.ParseBuffer(headerBuffer, nameEncoding); } /// @@ -60,15 +78,18 @@ public TarEntry(byte[] headerBuffer) /// Header details for entry public TarEntry(TarHeader header) { - if (header == null) { + if (header == null) + { throw new ArgumentNullException(nameof(header)); } this.header = (TarHeader)header.Clone(); } - #endregion + + #endregion Constructors #region ICloneable Members + /// /// Clone this tar entry. /// @@ -81,18 +102,20 @@ public object Clone() entry.Name = Name; return entry; } - #endregion + + #endregion ICloneable Members /// /// Construct an entry with only a name. - /// This allows the programmer to construct the entry's header "by hand". + /// This allows the programmer to construct the entry's header "by hand". /// /// The name to use for the entry /// Returns the newly created public static TarEntry CreateTarEntry(string name) { var entry = new TarEntry(); - TarEntry.NameTarHeader(entry.header, name); + + entry.NameTarHeader(name); return entry; } @@ -121,7 +144,8 @@ public override bool Equals(object obj) { var localEntry = obj as TarEntry; - if (localEntry != null) { + if (localEntry != null) + { return Name.Equals(localEntry.Name); } return false; @@ -149,7 +173,8 @@ public override int GetHashCode() /// public bool IsDescendent(TarEntry toTest) { - if (toTest == null) { + if (toTest == null) + { throw new ArgumentNullException(nameof(toTest)); } @@ -162,70 +187,54 @@ public bool IsDescendent(TarEntry toTest) /// /// This entry's TarHeader. /// - public TarHeader TarHeader { - get { - return header; - } + public TarHeader TarHeader + { + get { return header; } } /// /// Get/Set this entry's name. /// - public string Name { - get { - return header.Name; - } - set { - header.Name = value; - } + public string Name + { + get { return header.Name; } + set { header.Name = value; } } /// /// Get/set this entry's user id. /// - public int UserId { - get { - return header.UserId; - } - set { - header.UserId = value; - } + public int UserId + { + get { return header.UserId; } + set { header.UserId = value; } } /// /// Get/set this entry's group id. /// - public int GroupId { - get { - return header.GroupId; - } - set { - header.GroupId = value; - } + public int GroupId + { + get { return header.GroupId; } + set { header.GroupId = value; } } /// /// Get/set this entry's user name. /// - public string UserName { - get { - return header.UserName; - } - set { - header.UserName = value; - } + public string UserName + { + get { return header.UserName; } + set { header.UserName = value; } } /// /// Get/set this entry's group name. /// - public string GroupName { - get { - return header.GroupName; - } - set { - header.GroupName = value; - } + public string GroupName + { + get { return header.GroupName; } + set { header.GroupName = value; } } /// @@ -261,13 +270,10 @@ public void SetNames(string userName, string groupName) /// /// Get/Set the modification time for this entry /// - public DateTime ModTime { - get { - return header.ModTime; - } - set { - header.ModTime = value; - } + public DateTime ModTime + { + get { return header.ModTime; } + set { header.ModTime = value; } } /// @@ -276,22 +282,18 @@ public DateTime ModTime { /// /// This entry's file. /// - public string File { - get { - return file; - } + public string File + { + get { return file; } } /// /// Get/set this entry's recorded file size. /// - public long Size { - get { - return header.Size; - } - set { - header.Size = value; - } + public long Size + { + get { return header.Size; } + set { header.Size = value; } } /// @@ -300,14 +302,19 @@ public long Size { /// /// True if this entry is a directory. /// - public bool IsDirectory { - get { - if (file != null) { + public bool IsDirectory + { + get + { + if (file != null) + { return Directory.Exists(file); } - if (header != null) { - if ((header.TypeFlag == TarHeader.LF_DIR) || Name.EndsWith("/", StringComparison.Ordinal)) { + if (header != null) + { + if ((header.TypeFlag == TarHeader.LF_DIR) || Name.EndsWith("/", StringComparison.Ordinal)) + { return true; } } @@ -326,11 +333,13 @@ public bool IsDirectory { /// public void GetFileTarHeader(TarHeader header, string file) { - if (header == null) { + if (header == null) + { throw new ArgumentNullException(nameof(header)); } - if (file == null) { + if (file == null) + { throw new ArgumentNullException(nameof(file)); } @@ -340,21 +349,22 @@ public void GetFileTarHeader(TarHeader header, string file) string name = file; // 23-Jan-2004 GnuTar allows device names in path where the name is not local to the current directory - if (name.IndexOf(Directory.GetCurrentDirectory(), StringComparison.Ordinal) == 0) { + if (name.IndexOf(Directory.GetCurrentDirectory(), StringComparison.Ordinal) == 0) + { name = name.Substring(Directory.GetCurrentDirectory().Length); } /* - if (Path.DirectorySeparatorChar == '\\') + if (Path.DirectorySeparatorChar == '\\') { // check if the OS is Windows // Strip off drive letters! - if (name.Length > 2) + if (name.Length > 2) { char ch1 = name[0]; char ch2 = name[1]; - if (ch2 == ':' && Char.IsLetter(ch1)) + if (ch2 == ':' && Char.IsLetter(ch1)) { name = name.Substring(2); } @@ -362,33 +372,34 @@ public void GetFileTarHeader(TarHeader header, string file) } */ - name = name.Replace(Path.DirectorySeparatorChar, '/'); - // No absolute pathnames // Windows (and Posix?) paths can start with UNC style "\\NetworkDrive\", // so we loop on starting /'s. - while (name.StartsWith("/", StringComparison.Ordinal)) { - name = name.Substring(1); - } + name = name.ToTarArchivePath(); header.LinkName = String.Empty; header.Name = name; - if (Directory.Exists(file)) { + if (Directory.Exists(file)) + { header.Mode = 1003; // Magic number for security access for a UNIX filesystem header.TypeFlag = TarHeader.LF_DIR; - if ((header.Name.Length == 0) || header.Name[header.Name.Length - 1] != '/') { + if ((header.Name.Length == 0) || header.Name[header.Name.Length - 1] != '/') + { header.Name = header.Name + "/"; } header.Size = 0; - } else { + } + else + { header.Mode = 33216; // Magic number for security access for a UNIX filesystem header.TypeFlag = TarHeader.LF_NORMAL; header.Size = new FileInfo(file.Replace('/', Path.DirectorySeparatorChar)).Length; } - header.ModTime = System.IO.File.GetLastWriteTime(file.Replace('/', Path.DirectorySeparatorChar)).ToUniversalTime(); + header.ModTime = System.IO.File.GetLastWriteTime(file.Replace('/', Path.DirectorySeparatorChar)) + .ToUniversalTime(); header.DevMajor = 0; header.DevMinor = 0; } @@ -402,14 +413,16 @@ public void GetFileTarHeader(TarHeader header, string file) /// public TarEntry[] GetDirectoryEntries() { - if ((file == null) || !Directory.Exists(file)) { - return new TarEntry[0]; + if ((file == null) || !Directory.Exists(file)) + { + return Empty.Array(); } string[] list = Directory.GetFileSystemEntries(file); TarEntry[] result = new TarEntry[list.Length]; - for (int i = 0; i < list.Length; ++i) { + for (int i = 0; i < list.Length; ++i) + { result[i] = TarEntry.CreateEntryFromFile(list[i]); } @@ -422,9 +435,24 @@ public TarEntry[] GetDirectoryEntries() /// /// The tar entry header buffer to fill in. /// + [Obsolete("No Encoding for Name field is specified, any non-ASCII bytes will be discarded")] public void WriteEntryHeader(byte[] outBuffer) { - header.WriteHeader(outBuffer); + WriteEntryHeader(outBuffer, null); + } + + /// + /// Write an entry's header information to a header buffer. + /// + /// + /// The tar entry header buffer to fill in. + /// + /// + /// The used for the Name fields, or null for ASCII only + /// + public void WriteEntryHeader(byte[] outBuffer, Encoding nameEncoding) + { + header.WriteHeader(outBuffer, nameEncoding); } /// @@ -437,27 +465,40 @@ public void WriteEntryHeader(byte[] outBuffer) /// /// The new name to place into the header buffer. /// + [Obsolete("No Encoding for Name field is specified, any non-ASCII bytes will be discarded")] static public void AdjustEntryName(byte[] buffer, string newName) { - TarHeader.GetNameBytes(newName, buffer, 0, TarHeader.NAMELEN); + AdjustEntryName(buffer, newName, null); } /// - /// Fill in a TarHeader given only the entry's name. + /// Convenience method that will modify an entry's name directly + /// in place in an entry header buffer byte array. /// - /// - /// The TarHeader to fill in. + /// + /// The buffer containing the entry header to modify. + /// + /// + /// The new name to place into the header buffer. + /// + /// + /// The used for the Name fields, or null for ASCII only /// + static public void AdjustEntryName(byte[] buffer, string newName, Encoding nameEncoding) + { + TarHeader.GetNameBytes(newName, buffer, 0, TarHeader.NAMELEN, nameEncoding); + } + + /// + /// Fill in a TarHeader given only the entry's name. + /// /// /// The tar entry name. /// - static public void NameTarHeader(TarHeader header, string name) + public void NameTarHeader(string name) { - if (header == null) { - throw new ArgumentNullException(nameof(header)); - } - - if (name == null) { + if (name == null) + { throw new ArgumentNullException(nameof(name)); } @@ -482,15 +523,17 @@ static public void NameTarHeader(TarHeader header, string name) } #region Instance Fields + /// /// The name of the file this entry represents or null if the entry is not based on a file. /// - string file; + private string file; /// /// The entry's header information. /// - TarHeader header; - #endregion + private TarHeader header; + + #endregion Instance Fields } } diff --git a/src/ICSharpCode.SharpZipLib/Tar/TarException.cs b/src/ICSharpCode.SharpZipLib/Tar/TarException.cs index 3970c5269..9d448ca7d 100644 --- a/src/ICSharpCode.SharpZipLib/Tar/TarException.cs +++ b/src/ICSharpCode.SharpZipLib/Tar/TarException.cs @@ -1,13 +1,14 @@ using System; +using System.Runtime.Serialization; namespace ICSharpCode.SharpZipLib.Tar { /// /// TarException represents exceptions specific to Tar classes and code. /// + [Serializable] public class TarException : SharpZipBaseException { - /// /// Initialise a new instance of . /// @@ -33,5 +34,21 @@ public TarException(string message, Exception innerException) : base(message, innerException) { } + + /// + /// Initializes a new instance of the TarException class with serialized data. + /// + /// + /// The System.Runtime.Serialization.SerializationInfo that holds the serialized + /// object data about the exception being thrown. + /// + /// + /// The System.Runtime.Serialization.StreamingContext that contains contextual information + /// about the source or destination. + /// + protected TarException(SerializationInfo info, StreamingContext context) + : base(info, context) + { + } } } diff --git a/src/ICSharpCode.SharpZipLib/Tar/TarExtendedHeaderReader.cs b/src/ICSharpCode.SharpZipLib/Tar/TarExtendedHeaderReader.cs new file mode 100644 index 000000000..b711e6d54 --- /dev/null +++ b/src/ICSharpCode.SharpZipLib/Tar/TarExtendedHeaderReader.cs @@ -0,0 +1,126 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace ICSharpCode.SharpZipLib.Tar +{ + /// + /// Reads the extended header of a Tar stream + /// + public class TarExtendedHeaderReader + { + private const byte LENGTH = 0; + private const byte KEY = 1; + private const byte VALUE = 2; + private const byte END = 3; + + private readonly Dictionary headers = new Dictionary(); + + private string[] headerParts = new string[3]; + + private int bbIndex; + private byte[] byteBuffer; + private char[] charBuffer; + + private readonly StringBuilder sb = new StringBuilder(); + private readonly Decoder decoder = Encoding.UTF8.GetDecoder(); + + private int state = LENGTH; + + private int currHeaderLength; + private int currHeaderRead; + + private static readonly byte[] StateNext = { (byte)' ', (byte)'=', (byte)'\n' }; + + /// + /// Creates a new . + /// + public TarExtendedHeaderReader() + { + ResetBuffers(); + } + + /// + /// Read bytes from + /// + /// + /// + public void Read(byte[] buffer, int length) + { + for (int i = 0; i < length; i++) + { + byte next = buffer[i]; + + var foundStateEnd = state == VALUE + ? currHeaderRead == currHeaderLength -1 + : next == StateNext[state]; + + if (foundStateEnd) + { + Flush(); + headerParts[state] = sb.ToString(); + sb.Clear(); + + if (++state == END) + { + if (!headers.ContainsKey(headerParts[KEY])) + { + headers.Add(headerParts[KEY], headerParts[VALUE]); + } + + headerParts = new string[3]; + currHeaderLength = 0; + currHeaderRead = 0; + state = LENGTH; + } + else + { + currHeaderRead++; + } + + + if (state != VALUE) continue; + + if (int.TryParse(headerParts[LENGTH], out var vl)) + { + currHeaderLength = vl; + } + } + else + { + byteBuffer[bbIndex++] = next; + currHeaderRead++; + if (bbIndex == 4) + Flush(); + } + } + } + + private void Flush() + { + decoder.Convert(byteBuffer, 0, bbIndex, charBuffer, 0, 4, false, out int bytesUsed, out int charsUsed, out bool completed); + + sb.Append(charBuffer, 0, charsUsed); + ResetBuffers(); + } + + private void ResetBuffers() + { + charBuffer = new char[4]; + byteBuffer = new byte[4]; + bbIndex = 0; + } + + /// + /// Returns the parsed headers as key-value strings + /// + public Dictionary Headers + { + get + { + // TODO: Check for invalid state? -NM 2018-07-01 + return headers; + } + } + } +} diff --git a/src/ICSharpCode.SharpZipLib/Tar/TarHeader.cs b/src/ICSharpCode.SharpZipLib/Tar/TarHeader.cs index 6ce8c61f8..2ef3777aa 100644 --- a/src/ICSharpCode.SharpZipLib/Tar/TarHeader.cs +++ b/src/ICSharpCode.SharpZipLib/Tar/TarHeader.cs @@ -1,5 +1,7 @@ using System; +using System.Buffers; using System.Text; +using ICSharpCode.SharpZipLib.Core; namespace ICSharpCode.SharpZipLib.Tar { @@ -17,7 +19,7 @@ namespace ICSharpCode.SharpZipLib.Tar /// /// This is the ustar (Posix 1003.1) header. /// - /// struct header + /// struct header /// { /// char t_name[100]; // 0 Filename /// char t_mode[8]; // 100 Permissions @@ -41,6 +43,7 @@ namespace ICSharpCode.SharpZipLib.Tar public class TarHeader { #region Constants + /// /// The length of the name field in a header buffer. /// @@ -123,106 +126,106 @@ public class TarHeader /// /// Normal file type. /// - public const byte LF_NORMAL = (byte)'0'; + public const byte LF_NORMAL = (byte) '0'; /// /// Link file type. /// - public const byte LF_LINK = (byte)'1'; + public const byte LF_LINK = (byte) '1'; /// /// Symbolic link file type. /// - public const byte LF_SYMLINK = (byte)'2'; + public const byte LF_SYMLINK = (byte) '2'; /// /// Character device file type. /// - public const byte LF_CHR = (byte)'3'; + public const byte LF_CHR = (byte) '3'; /// /// Block device file type. /// - public const byte LF_BLK = (byte)'4'; + public const byte LF_BLK = (byte) '4'; /// /// Directory file type. /// - public const byte LF_DIR = (byte)'5'; + public const byte LF_DIR = (byte) '5'; /// /// FIFO (pipe) file type. /// - public const byte LF_FIFO = (byte)'6'; + public const byte LF_FIFO = (byte) '6'; /// /// Contiguous file type. /// - public const byte LF_CONTIG = (byte)'7'; + public const byte LF_CONTIG = (byte) '7'; /// /// Posix.1 2001 global extended header /// - public const byte LF_GHDR = (byte)'g'; + public const byte LF_GHDR = (byte) 'g'; /// /// Posix.1 2001 extended header /// - public const byte LF_XHDR = (byte)'x'; + public const byte LF_XHDR = (byte) 'x'; // POSIX allows for upper case ascii type as extensions /// /// Solaris access control list file type /// - public const byte LF_ACL = (byte)'A'; + public const byte LF_ACL = (byte) 'A'; /// /// GNU dir dump file type /// This is a dir entry that contains the names of files that were in the /// dir at the time the dump was made /// - public const byte LF_GNU_DUMPDIR = (byte)'D'; + public const byte LF_GNU_DUMPDIR = (byte) 'D'; /// /// Solaris Extended Attribute File /// - public const byte LF_EXTATTR = (byte)'E'; + public const byte LF_EXTATTR = (byte) 'E'; /// /// Inode (metadata only) no file content /// - public const byte LF_META = (byte)'I'; + public const byte LF_META = (byte) 'I'; /// /// Identifies the next file on the tape as having a long link name /// - public const byte LF_GNU_LONGLINK = (byte)'K'; + public const byte LF_GNU_LONGLINK = (byte) 'K'; /// /// Identifies the next file on the tape as having a long name /// - public const byte LF_GNU_LONGNAME = (byte)'L'; + public const byte LF_GNU_LONGNAME = (byte) 'L'; /// /// Continuation of a file that began on another volume /// - public const byte LF_GNU_MULTIVOL = (byte)'M'; + public const byte LF_GNU_MULTIVOL = (byte) 'M'; /// /// For storing filenames that dont fit in the main header (old GNU) /// - public const byte LF_GNU_NAMES = (byte)'N'; + public const byte LF_GNU_NAMES = (byte) 'N'; /// /// GNU Sparse file /// - public const byte LF_GNU_SPARSE = (byte)'S'; + public const byte LF_GNU_SPARSE = (byte) 'S'; /// /// GNU Tape/volume header ignore on extraction /// - public const byte LF_GNU_VOLHDR = (byte)'V'; + public const byte LF_GNU_VOLHDR = (byte) 'V'; /// /// The magic tag representing a POSIX tar archive. (would be written with a trailing NULL) @@ -234,9 +237,10 @@ public class TarHeader /// public const string GNU_TMAGIC = "ustar "; - const long timeConversionFactor = 10000000L; // 1 tick == 100 nanoseconds - readonly static DateTime dateTime1970 = new DateTime(1970, 1, 1, 0, 0, 0, 0); - #endregion + private const long timeConversionFactor = 10000000L; // 1 tick == 100 nanoseconds + private static readonly DateTime dateTime1970 = new DateTime(1970, 1, 1, 0, 0, 0, 0); + + #endregion Constants #region Constructors @@ -258,19 +262,24 @@ public TarHeader() Size = 0; } - #endregion + #endregion Constructors #region Properties + /// /// Get/set the name for this tar entry. /// /// Thrown when attempting to set the property to null. - public string Name { + public string Name + { get { return name; } - set { - if (value == null) { + set + { + if (value == null) + { throw new ArgumentNullException(nameof(value)); } + name = value; } } @@ -288,12 +297,12 @@ public string GetName() /// /// Get/set the entry's Unix style permission mode. /// - public int Mode { + public int Mode + { get { return mode; } set { mode = value; } } - /// /// The entry's user id. /// @@ -301,12 +310,12 @@ public int Mode { /// This is only directly relevant to unix systems. /// The default is zero. /// - public int UserId { + public int UserId + { get { return userId; } set { userId = value; } } - /// /// Get/set the entry's group id. /// @@ -314,27 +323,30 @@ public int UserId { /// This is only directly relevant to linux/unix systems. /// The default value is zero. /// - public int GroupId { + public int GroupId + { get { return groupId; } set { groupId = value; } } - /// /// Get/set the entry's size. /// /// Thrown when setting the size to less than zero. - public long Size { + public long Size + { get { return size; } - set { - if (value < 0) { + set + { + if (value < 0) + { throw new ArgumentOutOfRangeException(nameof(value), "Cannot be less than zero"); } + size = value; } } - /// /// Get/set the entry's modification time. /// @@ -342,147 +354,169 @@ public long Size { /// The modification time is only accurate to within a second. /// /// Thrown when setting the date time to less than 1/1/1970. - public DateTime ModTime { + public DateTime ModTime + { get { return modTime; } - set { - if (value < dateTime1970) { + set + { + if (value < dateTime1970) + { throw new ArgumentOutOfRangeException(nameof(value), "ModTime cannot be before Jan 1st 1970"); } + modTime = new DateTime(value.Year, value.Month, value.Day, value.Hour, value.Minute, value.Second); } } - /// /// Get the entry's checksum. This is only valid/updated after writing or reading an entry. /// - public int Checksum { + public int Checksum + { get { return checksum; } } - /// /// Get value of true if the header checksum is valid, false otherwise. /// - public bool IsChecksumValid { + public bool IsChecksumValid + { get { return isChecksumValid; } } - /// /// Get/set the entry's type flag. /// - public byte TypeFlag { + public byte TypeFlag + { get { return typeFlag; } set { typeFlag = value; } } - /// /// The entry's link name. /// /// Thrown when attempting to set LinkName to null. - public string LinkName { + public string LinkName + { get { return linkName; } - set { - if (value == null) { + set + { + if (value == null) + { throw new ArgumentNullException(nameof(value)); } + linkName = value; } } - /// /// Get/set the entry's magic tag. /// /// Thrown when attempting to set Magic to null. - public string Magic { + public string Magic + { get { return magic; } - set { - if (value == null) { + set + { + if (value == null) + { throw new ArgumentNullException(nameof(value)); } + magic = value; } } - /// /// The entry's version. /// /// Thrown when attempting to set Version to null. - public string Version { - get { - return version; - } + public string Version + { + get { return version; } - set { - if (value == null) { + set + { + if (value == null) + { throw new ArgumentNullException(nameof(value)); } + version = value; } } - /// /// The entry's user name. /// - public string UserName { + public string UserName + { get { return userName; } - set { - if (value != null) { + set + { + if (value != null) + { userName = value.Substring(0, Math.Min(UNAMELEN, value.Length)); - } else { + } + else + { string currentUser = "user"; - if (currentUser.Length > UNAMELEN) { + if (currentUser.Length > UNAMELEN) + { currentUser = currentUser.Substring(0, UNAMELEN); } + userName = currentUser; } } } - /// /// Get/set the entry's group name. /// /// /// This is only directly relevant to unix systems. /// - public string GroupName { + public string GroupName + { get { return groupName; } - set { - if (value == null) { + set + { + if (value == null) + { groupName = "None"; - } else { + } + else + { groupName = value; } } } - /// /// Get/set the entry's major device number. /// - public int DevMajor { + public int DevMajor + { get { return devMajor; } set { devMajor = value; } } - /// /// Get/set the entry's minor device number. /// - public int DevMinor { + public int DevMinor + { get { return devMinor; } set { devMinor = value; } } - #endregion + #endregion Properties #region ICloneable Members + /// /// Create a new that is a copy of the current instance. /// @@ -491,7 +525,8 @@ public object Clone() { return this.MemberwiseClone(); } - #endregion + + #endregion ICloneable Members /// /// Parse TarHeader information from a header buffer. @@ -499,24 +534,29 @@ public object Clone() /// /// The tar entry header buffer to get information from. /// - public void ParseBuffer(byte[] header) + /// + /// The used for the Name field, or null for ASCII only + /// + public void ParseBuffer(byte[] header, Encoding nameEncoding) { - if (header == null) { + if (header == null) + { throw new ArgumentNullException(nameof(header)); } int offset = 0; + var headerSpan = header.AsSpan(); - name = ParseName(header, offset, NAMELEN).ToString(); + name = ParseName(headerSpan.Slice(offset, NAMELEN), nameEncoding); offset += NAMELEN; - mode = (int)ParseOctal(header, offset, MODELEN); + mode = (int) ParseOctal(header, offset, MODELEN); offset += MODELEN; - UserId = (int)ParseOctal(header, offset, UIDLEN); + UserId = (int) ParseOctal(header, offset, UIDLEN); offset += UIDLEN; - GroupId = (int)ParseOctal(header, offset, GIDLEN); + GroupId = (int) ParseOctal(header, offset, GIDLEN); offset += GIDLEN; Size = ParseBinaryOrOctal(header, offset, SIZELEN); @@ -525,26 +565,26 @@ public void ParseBuffer(byte[] header) ModTime = GetDateTimeFromCTime(ParseOctal(header, offset, MODTIMELEN)); offset += MODTIMELEN; - checksum = (int)ParseOctal(header, offset, CHKSUMLEN); + checksum = (int) ParseOctal(header, offset, CHKSUMLEN); offset += CHKSUMLEN; TypeFlag = header[offset++]; - LinkName = ParseName(header, offset, NAMELEN).ToString(); + LinkName = ParseName(headerSpan.Slice(offset, NAMELEN), nameEncoding); offset += NAMELEN; - Magic = ParseName(header, offset, MAGICLEN).ToString(); + Magic = ParseName(headerSpan.Slice(offset, MAGICLEN), nameEncoding); offset += MAGICLEN; if (Magic == "ustar") { - Version = ParseName(header, offset, VERSIONLEN).ToString(); + Version = ParseName(headerSpan.Slice(offset, VERSIONLEN), nameEncoding); offset += VERSIONLEN; - UserName = ParseName(header, offset, UNAMELEN).ToString(); + UserName = ParseName(headerSpan.Slice(offset, UNAMELEN), nameEncoding); offset += UNAMELEN; - GroupName = ParseName(header, offset, GNAMELEN).ToString(); + GroupName = ParseName(headerSpan.Slice(offset, GNAMELEN), nameEncoding); offset += GNAMELEN; DevMajor = (int) ParseOctal(header, offset, DEVLEN); @@ -553,26 +593,50 @@ public void ParseBuffer(byte[] header) DevMinor = (int) ParseOctal(header, offset, DEVLEN); offset += DEVLEN; - string prefix = ParseName(header, offset, PREFIXLEN).ToString(); + string prefix = ParseName(headerSpan.Slice(offset, PREFIXLEN), nameEncoding); if (!string.IsNullOrEmpty(prefix)) Name = prefix + '/' + Name; } isChecksumValid = Checksum == TarHeader.MakeCheckSum(header); } + /// + /// Parse TarHeader information from a header buffer. + /// + /// + /// The tar entry header buffer to get information from. + /// + [Obsolete("No Encoding for Name field is specified, any non-ASCII bytes will be discarded")] + public void ParseBuffer(byte[] header) + { + ParseBuffer(header, null); + } + /// /// 'Write' header information to buffer provided, updating the check sum. /// /// output buffer for header information + [Obsolete("No Encoding for Name field is specified, any non-ASCII bytes will be discarded")] public void WriteHeader(byte[] outBuffer) { - if (outBuffer == null) { + WriteHeader(outBuffer, null); + } + + /// + /// 'Write' header information to buffer provided, updating the check sum. + /// + /// output buffer for header information + /// The used for the Name field, or null for ASCII only + public void WriteHeader(byte[] outBuffer, Encoding nameEncoding) + { + if (outBuffer == null) + { throw new ArgumentNullException(nameof(outBuffer)); } int offset = 0; - offset = GetNameBytes(Name, outBuffer, offset, NAMELEN); + offset = GetNameBytes(Name, outBuffer, offset, NAMELEN, nameEncoding); offset = GetOctalBytes(mode, outBuffer, offset, MODELEN); offset = GetOctalBytes(UserId, outBuffer, offset, UIDLEN); offset = GetOctalBytes(GroupId, outBuffer, offset, GIDLEN); @@ -581,24 +645,27 @@ public void WriteHeader(byte[] outBuffer) offset = GetOctalBytes(GetCTime(ModTime), outBuffer, offset, MODTIMELEN); int csOffset = offset; - for (int c = 0; c < CHKSUMLEN; ++c) { - outBuffer[offset++] = (byte)' '; + for (int c = 0; c < CHKSUMLEN; ++c) + { + outBuffer[offset++] = (byte) ' '; } outBuffer[offset++] = TypeFlag; - offset = GetNameBytes(LinkName, outBuffer, offset, NAMELEN); - offset = GetAsciiBytes(Magic, 0, outBuffer, offset, MAGICLEN); - offset = GetNameBytes(Version, outBuffer, offset, VERSIONLEN); - offset = GetNameBytes(UserName, outBuffer, offset, UNAMELEN); - offset = GetNameBytes(GroupName, outBuffer, offset, GNAMELEN); + offset = GetNameBytes(LinkName, outBuffer, offset, NAMELEN, nameEncoding); + offset = GetAsciiBytes(Magic, 0, outBuffer, offset, MAGICLEN, nameEncoding); + offset = GetNameBytes(Version, outBuffer, offset, VERSIONLEN, nameEncoding); + offset = GetNameBytes(UserName, outBuffer, offset, UNAMELEN, nameEncoding); + offset = GetNameBytes(GroupName, outBuffer, offset, GNAMELEN, nameEncoding); - if ((TypeFlag == LF_CHR) || (TypeFlag == LF_BLK)) { + if ((TypeFlag == LF_CHR) || (TypeFlag == LF_BLK)) + { offset = GetOctalBytes(DevMajor, outBuffer, offset, DEVLEN); offset = GetOctalBytes(DevMinor, outBuffer, offset, DEVLEN); } - for (; offset < outBuffer.Length;) { + for (; offset < outBuffer.Length;) + { outBuffer[offset++] = 0; } @@ -627,25 +694,29 @@ public override bool Equals(object obj) var localHeader = obj as TarHeader; bool result; - if (localHeader != null) { + if (localHeader != null) + { result = (name == localHeader.name) - && (mode == localHeader.mode) - && (UserId == localHeader.UserId) - && (GroupId == localHeader.GroupId) - && (Size == localHeader.Size) - && (ModTime == localHeader.ModTime) - && (Checksum == localHeader.Checksum) - && (TypeFlag == localHeader.TypeFlag) - && (LinkName == localHeader.LinkName) - && (Magic == localHeader.Magic) - && (Version == localHeader.Version) - && (UserName == localHeader.UserName) - && (GroupName == localHeader.GroupName) - && (DevMajor == localHeader.DevMajor) - && (DevMinor == localHeader.DevMinor); - } else { + && (mode == localHeader.mode) + && (UserId == localHeader.UserId) + && (GroupId == localHeader.GroupId) + && (Size == localHeader.Size) + && (ModTime == localHeader.ModTime) + && (Checksum == localHeader.Checksum) + && (TypeFlag == localHeader.TypeFlag) + && (LinkName == localHeader.LinkName) + && (Magic == localHeader.Magic) + && (Version == localHeader.Version) + && (UserName == localHeader.UserName) + && (GroupName == localHeader.GroupName) + && (DevMajor == localHeader.DevMajor) + && (DevMinor == localHeader.DevMinor); + } + else + { result = false; } + return result; } @@ -656,7 +727,7 @@ public override bool Equals(object obj) /// Value to apply as a default for userName. /// Value to apply as a default for groupId. /// Value to apply as a default for groupName. - static internal void SetValueDefaults(int userId, string userName, int groupId, string groupName) + internal static void SetValueDefaults(int userId, string userName, int groupId, string groupName) { defaultUserId = userIdAsSet = userId; defaultUser = userNameAsSet = userName; @@ -664,7 +735,7 @@ static internal void SetValueDefaults(int userId, string userName, int groupId, defaultGroupName = groupNameAsSet = groupName; } - static internal void RestoreSetValues() + internal static void RestoreSetValues() { defaultUserId = userIdAsSet; defaultUser = userNameAsSet; @@ -674,16 +745,20 @@ static internal void RestoreSetValues() // Return value that may be stored in octal or binary. Length must exceed 8. // - static private long ParseBinaryOrOctal(byte[] header, int offset, int length) + private static long ParseBinaryOrOctal(byte[] header, int offset, int length) { - if (header[offset] >= 0x80) { + if (header[offset] >= 0x80) + { // File sizes over 8GB are stored in 8 right-justified bytes of binary indicated by setting the high-order bit of the leftmost byte of a numeric field. long result = 0; - for (int pos = length - 8; pos < length; pos++) { + for (int pos = length - 8; pos < length; pos++) + { result = result << 8 | header[offset + pos]; } + return result; } + return ParseOctal(header, offset, length); } @@ -694,9 +769,10 @@ static private long ParseBinaryOrOctal(byte[] header, int offset, int length) /// The offset into the buffer from which to parse. /// The number of header bytes to parse. /// The long equivalent of the octal string. - static public long ParseOctal(byte[] header, int offset, int length) + public static long ParseOctal(byte[] header, int offset, int length) { - if (header == null) { + if (header == null) + { throw new ArgumentNullException(nameof(header)); } @@ -704,17 +780,22 @@ static public long ParseOctal(byte[] header, int offset, int length) bool stillPadding = true; int end = offset + length; - for (int i = offset; i < end; ++i) { - if (header[i] == 0) { + for (int i = offset; i < end; ++i) + { + if (header[i] == 0) + { break; } - if (header[i] == (byte)' ' || header[i] == '0') { - if (stillPadding) { + if (header[i] == (byte) ' ' || header[i] == '0') + { + if (stillPadding) + { continue; } - if (header[i] == (byte)' ') { + if (header[i] == (byte) ' ') + { break; } } @@ -742,33 +823,62 @@ static public long ParseOctal(byte[] header, int offset, int length) /// /// The name parsed. /// - static public StringBuilder ParseName(byte[] header, int offset, int length) + [Obsolete("No Encoding for Name field is specified, any non-ASCII bytes will be discarded")] + public static string ParseName(byte[] header, int offset, int length) { - if (header == null) { - throw new ArgumentNullException(nameof(header)); - } + return ParseName(header.AsSpan().Slice(offset, length), null); + } - if (offset < 0) { - throw new ArgumentOutOfRangeException(nameof(offset), "Cannot be less than zero"); - } + /// + /// Parse a name from a header buffer. + /// + /// + /// The header buffer from which to parse. + /// + /// + /// name encoding, or null for ASCII only + /// + /// + /// The name parsed. + /// + public static string ParseName(ReadOnlySpan header, Encoding encoding) + { + var builder = StringBuilderPool.Instance.Rent(); - if (length < 0) { - throw new ArgumentOutOfRangeException(nameof(length), "Cannot be less than zero"); - } + int count = 0; + if (encoding == null) + { + for (int i = 0; i < header.Length; ++i) + { + var b = header[i]; + if (b == 0) + { + break; + } - if (offset + length > header.Length) { - throw new ArgumentException("Exceeds header size", nameof(length)); + builder.Append((char) b); + } } - - var result = new StringBuilder(length); - - for (int i = offset; i < offset + length; ++i) { - if (header[i] == 0) { - break; + else + { + for (int i = 0; i < header.Length; ++i, ++count) + { + if (header[i] == 0) + { + break; + } } - result.Append((char)header[i]); + +#if NETSTANDARD2_1_OR_GREATER || NETCOREAPP3_1_OR_GREATER + var value = encoding.GetString(header.Slice(0, count)); +#else + var value = encoding.GetString(header.ToArray(), 0, count); +#endif + builder.Append(value); } + var result = builder.ToString(); + StringBuilderPool.Instance.Return(builder); return result; } @@ -783,15 +893,7 @@ static public StringBuilder ParseName(byte[] header, int offset, int length) /// The next free index in the public static int GetNameBytes(StringBuilder name, int nameOffset, byte[] buffer, int bufferOffset, int length) { - if (name == null) { - throw new ArgumentNullException(nameof(name)); - } - - if (buffer == null) { - throw new ArgumentNullException(nameof(buffer)); - } - - return GetNameBytes(name.ToString(), nameOffset, buffer, bufferOffset, length); + return GetNameBytes(name.ToString(), nameOffset, buffer, bufferOffset, length, null); } /// @@ -805,21 +907,56 @@ public static int GetNameBytes(StringBuilder name, int nameOffset, byte[] buffer /// The next free index in the public static int GetNameBytes(string name, int nameOffset, byte[] buffer, int bufferOffset, int length) { - if (name == null) { + return GetNameBytes(name, nameOffset, buffer, bufferOffset, length, null); + } + + /// + /// Add name to the buffer as a collection of bytes + /// + /// The name to add + /// The offset of the first character + /// The buffer to add to + /// The index of the first byte to add + /// The number of characters/bytes to add + /// name encoding, or null for ASCII only + /// The next free index in the + public static int GetNameBytes(string name, int nameOffset, byte[] buffer, int bufferOffset, int length, + Encoding encoding) + { + if (name == null) + { throw new ArgumentNullException(nameof(name)); } - if (buffer == null) { + if (buffer == null) + { throw new ArgumentNullException(nameof(buffer)); } int i; - - for (i = 0 ; i < length && nameOffset + i < name.Length; ++i) { - buffer[bufferOffset + i] = (byte)name[nameOffset + i]; + if (encoding != null) + { + // it can be more sufficient if using Span or unsafe + ReadOnlySpan nameArray = + name.AsSpan().Slice(nameOffset, Math.Min(name.Length - nameOffset, length)); + var charArray = ArrayPool.Shared.Rent(nameArray.Length); + nameArray.CopyTo(charArray); + + // it can be more sufficient if using Span(or unsafe?) and ArrayPool for temporary buffer + var bytesLength = encoding.GetBytes(charArray, 0, nameArray.Length, buffer, bufferOffset); + ArrayPool.Shared.Return(charArray); + i = Math.Min(bytesLength, length); + } + else + { + for (i = 0; i < length && nameOffset + i < name.Length; ++i) + { + buffer[bufferOffset + i] = (byte) name[nameOffset + i]; + } } - for (; i < length; ++i) { + for (; i < length; ++i) + { buffer[bufferOffset + i] = 0; } @@ -844,18 +981,46 @@ public static int GetNameBytes(string name, int nameOffset, byte[] buffer, int b /// /// The index of the next free byte in the buffer /// + /// TODO: what should be default behavior?(omit upper byte or UTF8?) + [Obsolete("No Encoding for Name field is specified, any non-ASCII bytes will be discarded")] public static int GetNameBytes(StringBuilder name, byte[] buffer, int offset, int length) { + return GetNameBytes(name, buffer, offset, length, null); + } - if (name == null) { + /// + /// Add an entry name to the buffer + /// + /// + /// The name to add + /// + /// + /// The buffer to add to + /// + /// + /// The offset into the buffer from which to start adding + /// + /// + /// The number of header bytes to add + /// + /// + /// + /// + /// The index of the next free byte in the buffer + /// + public static int GetNameBytes(StringBuilder name, byte[] buffer, int offset, int length, Encoding encoding) + { + if (name == null) + { throw new ArgumentNullException(nameof(name)); } - if (buffer == null) { + if (buffer == null) + { throw new ArgumentNullException(nameof(buffer)); } - return GetNameBytes(name.ToString(), 0, buffer, offset, length); + return GetNameBytes(name.ToString(), 0, buffer, offset, length, encoding); } /// @@ -866,18 +1031,35 @@ public static int GetNameBytes(StringBuilder name, byte[] buffer, int offset, in /// The offset into the buffer from which to start adding /// The number of header bytes to add /// The index of the next free byte in the buffer + /// TODO: what should be default behavior?(omit upper byte or UTF8?) + [Obsolete("No Encoding for Name field is specified, any non-ASCII bytes will be discarded")] public static int GetNameBytes(string name, byte[] buffer, int offset, int length) { + return GetNameBytes(name, buffer, offset, length, null); + } - if (name == null) { + /// + /// Add an entry name to the buffer + /// + /// The name to add + /// The buffer to add to + /// The offset into the buffer from which to start adding + /// The number of header bytes to add + /// + /// The index of the next free byte in the buffer + public static int GetNameBytes(string name, byte[] buffer, int offset, int length, Encoding encoding) + { + if (name == null) + { throw new ArgumentNullException(nameof(name)); } - if (buffer == null) { + if (buffer == null) + { throw new ArgumentNullException(nameof(buffer)); } - return GetNameBytes(name, 0, buffer, offset, length); + return GetNameBytes(name, 0, buffer, offset, length, encoding); } /// @@ -889,20 +1071,53 @@ public static int GetNameBytes(string name, byte[] buffer, int offset, int lengt /// The offset to start adding at. /// The number of ascii characters to add. /// The next free index in the buffer. + [Obsolete("No Encoding for Name field is specified, any non-ASCII bytes will be discarded")] public static int GetAsciiBytes(string toAdd, int nameOffset, byte[] buffer, int bufferOffset, int length) { - if (toAdd == null) { + return GetAsciiBytes(toAdd, nameOffset, buffer, bufferOffset, length, null); + } + + /// + /// Add a string to a buffer as a collection of ascii bytes. + /// + /// The string to add + /// The offset of the first character to add. + /// The buffer to add to. + /// The offset to start adding at. + /// The number of ascii characters to add. + /// String encoding, or null for ASCII only + /// The next free index in the buffer. + public static int GetAsciiBytes(string toAdd, int nameOffset, byte[] buffer, int bufferOffset, int length, + Encoding encoding) + { + if (toAdd == null) + { throw new ArgumentNullException(nameof(toAdd)); } - if (buffer == null) { + if (buffer == null) + { throw new ArgumentNullException(nameof(buffer)); } int i; - for (i = 0; i < length && nameOffset + i < toAdd.Length; ++i) { - buffer[bufferOffset + i] = (byte)toAdd[nameOffset + i]; + if (encoding == null) + { + for (i = 0; i < length && nameOffset + i < toAdd.Length; ++i) + { + buffer[bufferOffset + i] = (byte) toAdd[nameOffset + i]; + } + } + else + { + // It can be more sufficient if using unsafe code or Span(ToCharArray can be omitted) + var chars = toAdd.ToCharArray(); + // It can be more sufficient if using Span(or unsafe?) and ArrayPool for temporary buffer + var bytes = encoding.GetBytes(chars, nameOffset, Math.Min(toAdd.Length - nameOffset, length)); + i = Math.Min(bytes.Length, length); + Array.Copy(bytes, 0, buffer, bufferOffset, i); } + // If length is beyond the toAdd string length (which is OK by the prev loop condition), eg if a field has fixed length and the string is shorter, make sure all of the extra chars are written as NULLs, so that the reader func would ignore them and get back the original string for (; i < length; ++i) buffer[bufferOffset + i] = 0; @@ -929,7 +1144,8 @@ public static int GetAsciiBytes(string toAdd, int nameOffset, byte[] buffer, int /// public static int GetOctalBytes(long value, byte[] buffer, int offset, int length) { - if (buffer == null) { + if (buffer == null) + { throw new ArgumentNullException(nameof(buffer)); } @@ -939,15 +1155,18 @@ public static int GetOctalBytes(long value, byte[] buffer, int offset, int lengt buffer[offset + localIndex] = 0; --localIndex; - if (value > 0) { - for (long v = value; (localIndex >= 0) && (v > 0); --localIndex) { - buffer[offset + localIndex] = (byte)((byte)'0' + (byte)(v & 7)); + if (value > 0) + { + for (long v = value; (localIndex >= 0) && (v > 0); --localIndex) + { + buffer[offset + localIndex] = (byte) ((byte) '0' + (byte) (v & 7)); v >>= 3; } } - for (; localIndex >= 0; --localIndex) { - buffer[offset + localIndex] = (byte)'0'; + for (; localIndex >= 0; --localIndex) + { + buffer[offset + localIndex] = (byte) '0'; } return offset + length; @@ -963,15 +1182,20 @@ public static int GetOctalBytes(long value, byte[] buffer, int offset, int lengt /// Index of next byte private static int GetBinaryOrOctalBytes(long value, byte[] buffer, int offset, int length) { - if (value > 0x1FFFFFFFF) { // Octal 77777777777 (11 digits) - // Put value as binary, right-justified into the buffer. Set high order bit of left-most byte. - for (int pos = length - 1; pos > 0; pos--) { - buffer[offset + pos] = (byte)value; + if (value > 0x1FFFFFFFF) + { + // Octal 77777777777 (11 digits) + // Put value as binary, right-justified into the buffer. Set high order bit of left-most byte. + for (int pos = length - 1; pos > 0; pos--) + { + buffer[offset + pos] = (byte) value; value = value >> 8; } + buffer[offset] = 0x80; return offset + length; } + return GetOctalBytes(value, buffer, offset, length); } @@ -987,23 +1211,25 @@ private static int GetBinaryOrOctalBytes(long value, byte[] buffer, int offset, /// The final space is already there, from checksumming /// /// The modified buffer offset - static void GetCheckSumOctalBytes(long value, byte[] buffer, int offset, int length) + private static void GetCheckSumOctalBytes(long value, byte[] buffer, int offset, int length) { GetOctalBytes(value, buffer, offset, length - 1); } /// - /// Compute the checksum for a tar entry header. + /// Compute the checksum for a tar entry header. /// The checksum field must be all spaces prior to this happening /// /// The tar entry's header buffer. /// The computed checksum. - static int ComputeCheckSum(byte[] buffer) + private static int ComputeCheckSum(byte[] buffer) { int sum = 0; - for (int i = 0; i < buffer.Length; ++i) { + for (int i = 0; i < buffer.Length; ++i) + { sum += buffer[i]; } + return sum; } @@ -1012,70 +1238,83 @@ static int ComputeCheckSum(byte[] buffer) /// /// The tar entry's header buffer. /// The checksum for the buffer - static int MakeCheckSum(byte[] buffer) + private static int MakeCheckSum(byte[] buffer) { int sum = 0; - for (int i = 0; i < CHKSUMOFS; ++i) { + for (int i = 0; i < CHKSUMOFS; ++i) + { sum += buffer[i]; } - for (int i = 0; i < CHKSUMLEN; ++i) { - sum += (byte)' '; + for (int i = 0; i < CHKSUMLEN; ++i) + { + sum += (byte) ' '; } - for (int i = CHKSUMOFS + CHKSUMLEN; i < buffer.Length; ++i) { + for (int i = CHKSUMOFS + CHKSUMLEN; i < buffer.Length; ++i) + { sum += buffer[i]; } + return sum; } - static int GetCTime(DateTime dateTime) + private static int GetCTime(DateTime dateTime) { - return unchecked((int)((dateTime.Ticks - dateTime1970.Ticks) / timeConversionFactor)); + return unchecked((int) ((dateTime.Ticks - dateTime1970.Ticks) / timeConversionFactor)); } - static DateTime GetDateTimeFromCTime(long ticks) + private static DateTime GetDateTimeFromCTime(long ticks) { DateTime result; - try { + try + { result = new DateTime(dateTime1970.Ticks + ticks * timeConversionFactor); - } catch (ArgumentOutOfRangeException) { + } + catch (ArgumentOutOfRangeException) + { result = dateTime1970; } + return result; } #region Instance Fields - string name; - int mode; - int userId; - int groupId; - long size; - DateTime modTime; - int checksum; - bool isChecksumValid; - byte typeFlag; - string linkName; - string magic; - string version; - string userName; - string groupName; - int devMajor; - int devMinor; - #endregion + + private string name; + private int mode; + private int userId; + private int groupId; + private long size; + private DateTime modTime; + private int checksum; + private bool isChecksumValid; + private byte typeFlag; + private string linkName; + private string magic; + private string version; + private string userName; + private string groupName; + private int devMajor; + private int devMinor; + + #endregion Instance Fields #region Class Fields + // Values used during recursive operations. - static internal int userIdAsSet; - static internal int groupIdAsSet; - static internal string userNameAsSet; - static internal string groupNameAsSet = "None"; - - static internal int defaultUserId; - static internal int defaultGroupId; - static internal string defaultGroupName = "None"; - static internal string defaultUser; - #endregion + internal static int userIdAsSet; + + internal static int groupIdAsSet; + internal static string userNameAsSet; + internal static string groupNameAsSet = "None"; + + internal static int defaultUserId; + internal static int defaultGroupId; + internal static string defaultGroupName = "None"; + internal static string defaultUser; + + #endregion Class Fields } } diff --git a/src/ICSharpCode.SharpZipLib/Tar/TarInputStream.cs b/src/ICSharpCode.SharpZipLib/Tar/TarInputStream.cs index c398be0d3..2cd646ae9 100644 --- a/src/ICSharpCode.SharpZipLib/Tar/TarInputStream.cs +++ b/src/ICSharpCode.SharpZipLib/Tar/TarInputStream.cs @@ -1,6 +1,10 @@ using System; +using System.Buffers; using System.IO; using System.Text; +using System.Threading; +using System.Threading.Tasks; +using ICSharpCode.SharpZipLib.Core; namespace ICSharpCode.SharpZipLib.Tar { @@ -13,12 +17,24 @@ namespace ICSharpCode.SharpZipLib.Tar public class TarInputStream : Stream { #region Constructors + /// /// Construct a TarInputStream with default block factor /// /// stream to source data from + [Obsolete("No Encoding for Name field is specified, any non-ASCII bytes will be discarded")] public TarInputStream(Stream inputStream) - : this(inputStream, TarBuffer.DefaultBlockFactor) + : this(inputStream, TarBuffer.DefaultBlockFactor, null) + { + } + + /// + /// Construct a TarInputStream with default block factor + /// + /// stream to source data from + /// The used for the Name fields, or null for ASCII only + public TarInputStream(Stream inputStream, Encoding nameEncoding) + : this(inputStream, TarBuffer.DefaultBlockFactor, nameEncoding) { } @@ -27,75 +43,85 @@ public TarInputStream(Stream inputStream) /// /// stream to source data from /// block factor to apply to archive + [Obsolete("No Encoding for Name field is specified, any non-ASCII bytes will be discarded")] public TarInputStream(Stream inputStream, int blockFactor) { this.inputStream = inputStream; tarBuffer = TarBuffer.CreateInputTarBuffer(inputStream, blockFactor); + encoding = null; } - #endregion + /// + /// Construct a TarInputStream with user specified block factor + /// + /// stream to source data from + /// block factor to apply to archive + /// The used for the Name fields, or null for ASCII only + public TarInputStream(Stream inputStream, int blockFactor, Encoding nameEncoding) + { + this.inputStream = inputStream; + tarBuffer = TarBuffer.CreateInputTarBuffer(inputStream, blockFactor); + encoding = nameEncoding; + } + + #endregion Constructors /// /// Gets or sets a flag indicating ownership of underlying stream. /// When the flag is true will close the underlying stream also. /// /// The default value is true. - public bool IsStreamOwner { + public bool IsStreamOwner + { get { return tarBuffer.IsStreamOwner; } set { tarBuffer.IsStreamOwner = value; } } #region Stream Overrides + /// /// Gets a value indicating whether the current stream supports reading /// - public override bool CanRead { - get { - return inputStream.CanRead; - } + public override bool CanRead + { + get { return inputStream.CanRead; } } /// /// Gets a value indicating whether the current stream supports seeking /// This property always returns false. /// - public override bool CanSeek { - get { - return false; - } + public override bool CanSeek + { + get { return false; } } /// /// Gets a value indicating if the stream supports writing. /// This property always returns false. /// - public override bool CanWrite { - get { - return false; - } + public override bool CanWrite + { + get { return false; } } /// /// The length in bytes of the stream /// - public override long Length { - get { - return inputStream.Length; - } + public override long Length + { + get { return inputStream.Length; } } /// - /// Gets or sets the position within the stream. + /// Gets or sets the position within the stream. /// Setting the Position is not supported and throws a NotSupportedExceptionNotSupportedException /// /// Any attempt to set position - public override long Position { - get { - return inputStream.Position; - } - set { - throw new NotSupportedException("TarInputStream Seek not supported"); - } + public override long Position + { + get { return inputStream.Position; } + set { throw new NotSupportedException("TarInputStream Seek not supported"); } } /// @@ -106,6 +132,15 @@ public override void Flush() inputStream.Flush(); } + /// + /// Flushes the baseInputStream + /// + /// + public override async Task FlushAsync(CancellationToken cancellationToken) + { + await inputStream.FlushAsync(cancellationToken).ConfigureAwait(false); + } + /// /// Set the streams position. This operation is not supported and will throw a NotSupportedException /// @@ -152,21 +187,52 @@ public override void WriteByte(byte value) { throw new NotSupportedException("TarInputStream WriteByte not supported"); } + /// /// Reads a byte from the current tar archive entry. /// /// A byte cast to an int; -1 if the at the end of the stream. public override int ReadByte() { - byte[] oneByteBuffer = new byte[1]; - int num = Read(oneByteBuffer, 0, 1); - if (num <= 0) { + var oneByteBuffer = ArrayPool.Shared.Rent(1); + var num = Read(oneByteBuffer, 0, 1); + if (num <= 0) + { // return -1 to indicate that no byte was read. return -1; } - return oneByteBuffer[0]; + + var result = oneByteBuffer[0]; + ArrayPool.Shared.Return(oneByteBuffer); + return result; + } + + + /// + /// Reads bytes from the current tar archive entry. + /// + /// This method is aware of the boundaries of the current + /// entry in the archive and will deal with them appropriately + /// + /// + /// The buffer into which to place bytes read. + /// + /// + /// The offset at which to place bytes read. + /// + /// + /// The number of bytes to read. + /// + /// + /// + /// The number of bytes read, or 0 at end of stream/EOF. + /// + public override Task ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) + { + return ReadAsync(buffer.AsMemory().Slice(offset, count), cancellationToken, true).AsTask(); } +#if NETSTANDARD2_1_OR_GREATER || NETCOREAPP3_1_OR_GREATER /// /// Reads bytes from the current tar archive entry. /// @@ -176,6 +242,26 @@ public override int ReadByte() /// /// The buffer into which to place bytes read. /// + /// + /// + /// The number of bytes read, or 0 at end of stream/EOF. + /// + public override ValueTask ReadAsync(Memory buffer, CancellationToken cancellationToken = + new CancellationToken()) + { + return ReadAsync(buffer, cancellationToken, true); + } +#endif + + /// + /// Reads bytes from the current tar archive entry. + /// + /// This method is aware of the boundaries of the current + /// entry in the archive and will deal with them appropriately + /// + /// + /// The buffer into which to place bytes read. + /// /// /// The offset at which to place bytes read. /// @@ -187,33 +273,50 @@ public override int ReadByte() /// public override int Read(byte[] buffer, int offset, int count) { - if (buffer == null) { + if (buffer == null) + { throw new ArgumentNullException(nameof(buffer)); } + return ReadAsync(buffer.AsMemory().Slice(offset, count), CancellationToken.None, false).GetAwaiter() + .GetResult(); + } + + private async ValueTask ReadAsync(Memory buffer, CancellationToken ct, bool isAsync) + { + int offset = 0; int totalRead = 0; - if (entryOffset >= entrySize) { + if (entryOffset >= entrySize) + { return 0; } - long numToRead = count; + long numToRead = buffer.Length; - if ((numToRead + entryOffset) > entrySize) { + if ((numToRead + entryOffset) > entrySize) + { numToRead = entrySize - entryOffset; } - if (readBuffer != null) { - int sz = (numToRead > readBuffer.Length) ? readBuffer.Length : (int)numToRead; + if (readBuffer != null) + { + int sz = (numToRead > readBuffer.Memory.Length) ? readBuffer.Memory.Length : (int)numToRead; - Array.Copy(readBuffer, 0, buffer, offset, sz); + readBuffer.Memory.Slice(0, sz).CopyTo(buffer.Slice(offset, sz)); - if (sz >= readBuffer.Length) { + if (sz >= readBuffer.Memory.Length) + { + readBuffer.Dispose(); readBuffer = null; - } else { - int newLen = readBuffer.Length - sz; - byte[] newBuf = new byte[newLen]; - Array.Copy(readBuffer, sz, newBuf, 0, newLen); + } + else + { + int newLen = readBuffer.Memory.Length - sz; + var newBuf = ExactMemoryPool.Shared.Rent(newLen); + readBuffer.Memory.Slice(sz, newLen).CopyTo(newBuf.Memory); + readBuffer.Dispose(); + readBuffer = newBuf; } @@ -222,23 +325,27 @@ public override int Read(byte[] buffer, int offset, int count) offset += sz; } - while (numToRead > 0) { - byte[] rec = tarBuffer.ReadBlock(); - if (rec == null) { - // Unexpected EOF! - throw new TarException("unexpected EOF with " + numToRead + " bytes unread"); - } + var recLen = TarBuffer.BlockSize; + var recBuf = ArrayPool.Shared.Rent(recLen); + + while (numToRead > 0) + { + await tarBuffer.ReadBlockIntAsync(recBuf, ct, isAsync).ConfigureAwait(false); var sz = (int)numToRead; - int recLen = rec.Length; - if (recLen > sz) { - Array.Copy(rec, 0, buffer, offset, sz); - readBuffer = new byte[recLen - sz]; - Array.Copy(rec, sz, readBuffer, 0, recLen - sz); - } else { + if (recLen > sz) + { + recBuf.AsSpan().Slice(0, sz).CopyTo(buffer.Slice(offset, sz).Span); + readBuffer?.Dispose(); + + readBuffer = ExactMemoryPool.Shared.Rent(recLen - sz); + recBuf.AsSpan().Slice(sz, recLen - sz).CopyTo(readBuffer.Memory.Span); + } + else + { sz = recLen; - Array.Copy(rec, 0, buffer, offset, recLen); + recBuf.AsSpan().CopyTo(buffer.Slice(offset, recLen).Span); } totalRead += sz; @@ -246,6 +353,8 @@ public override int Read(byte[] buffer, int offset, int count) offset += sz; } + ArrayPool.Shared.Return(recBuf); + entryOffset += totalRead; return totalRead; @@ -263,7 +372,18 @@ protected override void Dispose(bool disposing) } } - #endregion +#if NETSTANDARD2_1_OR_GREATER || NETCOREAPP3_1_OR_GREATER + /// + /// Closes this stream. Calls the TarBuffer's close() method. + /// The underlying stream is closed by the TarBuffer. + /// + public override async ValueTask DisposeAsync() + { + await tarBuffer.CloseAsync(CancellationToken.None).ConfigureAwait(false); + } +#endif + + #endregion Stream Overrides /// /// Set the entry factory for this instance. @@ -277,7 +397,8 @@ public void SetEntryFactory(IEntryFactory factory) /// /// Get the record size being used by this stream's TarBuffer. /// - public int RecordSize { + public int RecordSize + { get { return tarBuffer.RecordSize; } } @@ -303,10 +424,9 @@ public int GetRecordSize() /// /// The number of available bytes for the current entry. /// - public long Available { - get { - return entrySize - entryOffset; - } + public long Available + { + get { return entrySize - entryOffset; } } /// @@ -318,23 +438,42 @@ public long Available { /// /// The number of bytes to skip. /// - public void Skip(long skipCount) + /// + private Task SkipAsync(long skipCount, CancellationToken ct) => SkipAsync(skipCount, ct, true).AsTask(); + + /// + /// Skip bytes in the input buffer. This skips bytes in the + /// current entry's data, not the entire archive, and will + /// stop at the end of the current entry's data if the number + /// to skip extends beyond that point. + /// + /// + /// The number of bytes to skip. + /// + private void Skip(long skipCount) => + SkipAsync(skipCount, CancellationToken.None, false).GetAwaiter().GetResult(); + + private async ValueTask SkipAsync(long skipCount, CancellationToken ct, bool isAsync) { // TODO: REVIEW efficiency of TarInputStream.Skip // This is horribly inefficient, but it ensures that we // properly skip over bytes via the TarBuffer... // - byte[] skipBuf = new byte[8 * 1024]; - - for (long num = skipCount; num > 0;) { - int toRead = num > skipBuf.Length ? skipBuf.Length : (int)num; - int numRead = Read(skipBuf, 0, toRead); + var length = 8 * 1024; + using (var skipBuf = ExactMemoryPool.Shared.Rent(length)) + { + for (long num = skipCount; num > 0;) + { + int toRead = num > length ? length : (int)num; + int numRead = await ReadAsync(skipBuf.Memory.Slice(0, toRead), ct, isAsync).ConfigureAwait(false); + + if (numRead == -1) + { + break; + } - if (numRead == -1) { - break; + num -= numRead; } - - num -= numRead; } } @@ -342,10 +481,9 @@ public void Skip(long skipCount) /// Return a value of true if marking is supported; false otherwise. /// /// Currently marking is not supported, the return value is always false. - public bool IsMarkSupported { - get { - return false; - } + public bool IsMarkSupported + { + get { return false; } } /// @@ -378,86 +516,171 @@ public void Reset() /// /// The next TarEntry in the archive, or null. /// - public TarEntry GetNextEntry() + public Task GetNextEntryAsync(CancellationToken ct) => GetNextEntryAsync(ct, true).AsTask(); + + /// + /// Get the next entry in this tar archive. This will skip + /// over any remaining data in the current entry, if there + /// is one, and place the input stream at the header of the + /// next entry, and read the header and instantiate a new + /// TarEntry from the header bytes and return that entry. + /// If there are no more entries in the archive, null will + /// be returned to indicate that the end of the archive has + /// been reached. + /// + /// + /// The next TarEntry in the archive, or null. + /// + public TarEntry GetNextEntry() => GetNextEntryAsync(CancellationToken.None, false).GetAwaiter().GetResult(); + + private async ValueTask GetNextEntryAsync(CancellationToken ct, bool isAsync) { - if (hasHitEOF) { + if (hasHitEOF) + { return null; } - if (currentEntry != null) { - SkipToNextEntry(); + if (currentEntry != null) + { + await SkipToNextEntryAsync(ct, isAsync).ConfigureAwait(false); } - byte[] headerBuf = tarBuffer.ReadBlock(); + byte[] headerBuf = ArrayPool.Shared.Rent(TarBuffer.BlockSize); + await tarBuffer.ReadBlockIntAsync(headerBuf, ct, isAsync).ConfigureAwait(false); - if (headerBuf == null) { + if (TarBuffer.IsEndOfArchiveBlock(headerBuf)) + { hasHitEOF = true; - } else - hasHitEOF |= TarBuffer.IsEndOfArchiveBlock(headerBuf); - if (hasHitEOF) { + // Read the second zero-filled block + await tarBuffer.ReadBlockIntAsync(headerBuf, ct, isAsync).ConfigureAwait(false); + } + else + { + hasHitEOF = false; + } + + if (hasHitEOF) + { currentEntry = null; - } else { - try { + readBuffer?.Dispose(); + } + else + { + try + { var header = new TarHeader(); - header.ParseBuffer(headerBuf); - if (!header.IsChecksumValid) { + header.ParseBuffer(headerBuf, encoding); + if (!header.IsChecksumValid) + { throw new TarException("Header checksum is invalid"); } + this.entryOffset = 0; this.entrySize = header.Size; - StringBuilder longName = null; + string longName = null; + + if (header.TypeFlag == TarHeader.LF_GNU_LONGNAME) + { + using (var nameBuffer = ExactMemoryPool.Shared.Rent(TarBuffer.BlockSize)) + { + long numToRead = this.entrySize; + + var longNameBuilder = StringBuilderPool.Instance.Rent(); + + while (numToRead > 0) + { + var length = (numToRead > TarBuffer.BlockSize ? TarBuffer.BlockSize : (int)numToRead); + int numRead = await ReadAsync(nameBuffer.Memory.Slice(0, length), ct, isAsync).ConfigureAwait(false); - if (header.TypeFlag == TarHeader.LF_GNU_LONGNAME) { + if (numRead == -1) + { + throw new InvalidHeaderException("Failed to read long name entry"); + } - byte[] nameBuffer = new byte[TarBuffer.BlockSize]; + longNameBuilder.Append(TarHeader.ParseName(nameBuffer.Memory.Slice(0, numRead).Span, + encoding)); + numToRead -= numRead; + } + + longName = longNameBuilder.ToString(); + StringBuilderPool.Instance.Return(longNameBuilder); + + await SkipToNextEntryAsync(ct, isAsync).ConfigureAwait(false); + await this.tarBuffer.ReadBlockIntAsync(headerBuf, ct, isAsync).ConfigureAwait(false); + } + } + else if (header.TypeFlag == TarHeader.LF_GHDR) + { + // POSIX global extended header + // Ignore things we dont understand completely for now + await SkipToNextEntryAsync(ct, isAsync).ConfigureAwait(false); + await this.tarBuffer.ReadBlockIntAsync(headerBuf, ct, isAsync).ConfigureAwait(false); + } + else if (header.TypeFlag == TarHeader.LF_XHDR) + { + // POSIX extended header + byte[] nameBuffer = ArrayPool.Shared.Rent(TarBuffer.BlockSize); long numToRead = this.entrySize; - longName = new StringBuilder(); + var xhr = new TarExtendedHeaderReader(); - while (numToRead > 0) { - int numRead = this.Read(nameBuffer, 0, (numToRead > nameBuffer.Length ? nameBuffer.Length : (int)numToRead)); + while (numToRead > 0) + { + var length = (numToRead > nameBuffer.Length ? nameBuffer.Length : (int)numToRead); + int numRead = await ReadAsync(nameBuffer.AsMemory().Slice(0, length), ct, isAsync).ConfigureAwait(false); - if (numRead == -1) { + if (numRead == -1) + { throw new InvalidHeaderException("Failed to read long name entry"); } - longName.Append(TarHeader.ParseName(nameBuffer, 0, numRead).ToString()); + xhr.Read(nameBuffer, numRead); numToRead -= numRead; } - SkipToNextEntry(); - headerBuf = this.tarBuffer.ReadBlock(); - } else if (header.TypeFlag == TarHeader.LF_GHDR) { // POSIX global extended header - // Ignore things we dont understand completely for now - SkipToNextEntry(); - headerBuf = this.tarBuffer.ReadBlock(); - } else if (header.TypeFlag == TarHeader.LF_XHDR) { // POSIX extended header - // Ignore things we dont understand completely for now - SkipToNextEntry(); - headerBuf = this.tarBuffer.ReadBlock(); - } else if (header.TypeFlag == TarHeader.LF_GNU_VOLHDR) { + ArrayPool.Shared.Return(nameBuffer); + + if (xhr.Headers.TryGetValue("path", out string name)) + { + longName = name; + } + + await SkipToNextEntryAsync(ct, isAsync).ConfigureAwait(false); + await this.tarBuffer.ReadBlockIntAsync(headerBuf, ct, isAsync).ConfigureAwait(false); + } + else if (header.TypeFlag == TarHeader.LF_GNU_VOLHDR) + { // TODO: could show volume name when verbose - SkipToNextEntry(); - headerBuf = this.tarBuffer.ReadBlock(); - } else if (header.TypeFlag != TarHeader.LF_NORMAL && - header.TypeFlag != TarHeader.LF_OLDNORM && - header.TypeFlag != TarHeader.LF_LINK && - header.TypeFlag != TarHeader.LF_SYMLINK && - header.TypeFlag != TarHeader.LF_DIR) { + await SkipToNextEntryAsync(ct, isAsync).ConfigureAwait(false); + await this.tarBuffer.ReadBlockIntAsync(headerBuf, ct, isAsync).ConfigureAwait(false); + } + else if (header.TypeFlag != TarHeader.LF_NORMAL && + header.TypeFlag != TarHeader.LF_OLDNORM && + header.TypeFlag != TarHeader.LF_LINK && + header.TypeFlag != TarHeader.LF_SYMLINK && + header.TypeFlag != TarHeader.LF_DIR) + { // Ignore things we dont understand completely for now - SkipToNextEntry(); - headerBuf = tarBuffer.ReadBlock(); + await SkipToNextEntryAsync(ct, isAsync).ConfigureAwait(false); + await tarBuffer.ReadBlockIntAsync(headerBuf, ct, isAsync).ConfigureAwait(false); } - if (entryFactory == null) { - currentEntry = new TarEntry(headerBuf); - if (longName != null) { - currentEntry.Name = longName.ToString(); + if (entryFactory == null) + { + currentEntry = new TarEntry(headerBuf, encoding); + readBuffer?.Dispose(); + + if (longName != null) + { + currentEntry.Name = longName; } - } else { + } + else + { currentEntry = entryFactory.CreateEntry(headerBuf); + readBuffer?.Dispose(); } // Magic was checked here for 'ustar' but there are multiple valid possibilities @@ -467,15 +690,22 @@ public TarEntry GetNextEntry() // TODO: Review How do we resolve this discrepancy?! entrySize = this.currentEntry.Size; - } catch (InvalidHeaderException ex) { + } + catch (InvalidHeaderException ex) + { entrySize = 0; entryOffset = 0; currentEntry = null; + readBuffer?.Dispose(); + string errorText = string.Format("Bad header in record {0} block {1} {2}", tarBuffer.CurrentRecord, tarBuffer.CurrentBlock, ex.Message); throw new InvalidHeaderException(errorText); } } + + ArrayPool.Shared.Return(headerBuf); + return currentEntry; } @@ -486,27 +716,55 @@ public TarEntry GetNextEntry() /// /// The OutputStream into which to write the entry's data. /// - public void CopyEntryContents(Stream outputStream) + /// + public Task CopyEntryContentsAsync(Stream outputStream, CancellationToken ct) => + CopyEntryContentsAsync(outputStream, ct, true).AsTask(); + + /// + /// Copies the contents of the current tar archive entry directly into + /// an output stream. + /// + /// + /// The OutputStream into which to write the entry's data. + /// + public void CopyEntryContents(Stream outputStream) => + CopyEntryContentsAsync(outputStream, CancellationToken.None, false).GetAwaiter().GetResult(); + + private async ValueTask CopyEntryContentsAsync(Stream outputStream, CancellationToken ct, bool isAsync) { - byte[] tempBuffer = new byte[32 * 1024]; + byte[] tempBuffer = ArrayPool.Shared.Rent(32 * 1024); - while (true) { - int numRead = Read(tempBuffer, 0, tempBuffer.Length); - if (numRead <= 0) { + while (true) + { + int numRead = await ReadAsync(tempBuffer, ct, isAsync).ConfigureAwait(false); + if (numRead <= 0) + { break; } - outputStream.Write(tempBuffer, 0, numRead); + + if (isAsync) + { + await outputStream.WriteAsync(tempBuffer, 0, numRead, ct).ConfigureAwait(false); + } + else + { + outputStream.Write(tempBuffer, 0, numRead); + } } + + ArrayPool.Shared.Return(tempBuffer); } - void SkipToNextEntry() + private async ValueTask SkipToNextEntryAsync(CancellationToken ct, bool isAsync) { long numToSkip = entrySize - entryOffset; - if (numToSkip > 0) { - Skip(numToSkip); + if (numToSkip > 0) + { + await SkipAsync(numToSkip, ct, isAsync).ConfigureAwait(false); } + readBuffer?.Dispose(); readBuffer = null; } @@ -517,6 +775,8 @@ void SkipToNextEntry() /// public interface IEntryFactory { + // This interface does not considering name encoding. + // How this interface should be? /// /// Create an entry based on name alone /// @@ -541,7 +801,7 @@ public interface IEntryFactory /// Create a tar entry based on the header information passed /// /// - /// Buffer containing header information to create an an entry from. + /// Buffer containing header information to create an entry from. /// /// /// Created TarEntry or descendant class @@ -554,6 +814,25 @@ public interface IEntryFactory /// public class EntryFactoryAdapter : IEntryFactory { + Encoding nameEncoding; + + /// + /// Construct standard entry factory class with ASCII name encoding + /// + [Obsolete("No Encoding for Name field is specified, any non-ASCII bytes will be discarded")] + public EntryFactoryAdapter() + { + } + + /// + /// Construct standard entry factory with name encoding + /// + /// The used for the Name fields, or null for ASCII only + public EntryFactoryAdapter(Encoding nameEncoding) + { + this.nameEncoding = nameEncoding; + } + /// /// Create a based on named /// @@ -576,16 +855,17 @@ public TarEntry CreateEntryFromFile(string fileName) /// /// Create an entry based on details in header - /// + /// /// The buffer containing entry details. /// A new public TarEntry CreateEntry(byte[] headerBuffer) { - return new TarEntry(headerBuffer); + return new TarEntry(headerBuffer, nameEncoding); } } #region Instance Fields + /// /// Flag set when last block has been read /// @@ -603,8 +883,8 @@ public TarEntry CreateEntry(byte[] headerBuffer) /// /// Buffer used with calls to Read() - /// - protected byte[] readBuffer; + /// + protected IMemoryOwner readBuffer; /// /// Working buffer @@ -614,7 +894,7 @@ public TarEntry CreateEntry(byte[] headerBuffer) /// /// Current entry being read /// - TarEntry currentEntry; + private TarEntry currentEntry; /// /// Factory used to create TarEntry or descendant class instance @@ -624,7 +904,10 @@ public TarEntry CreateEntry(byte[] headerBuffer) /// /// Stream used as the source of input data. /// - readonly Stream inputStream; - #endregion + private readonly Stream inputStream; + + private readonly Encoding encoding; + + #endregion Instance Fields } } diff --git a/src/ICSharpCode.SharpZipLib/Tar/TarOutputStream.cs b/src/ICSharpCode.SharpZipLib/Tar/TarOutputStream.cs index e47db23b0..be4f6cc79 100644 --- a/src/ICSharpCode.SharpZipLib/Tar/TarOutputStream.cs +++ b/src/ICSharpCode.SharpZipLib/Tar/TarOutputStream.cs @@ -1,5 +1,9 @@ using System; +using System.Buffers; using System.IO; +using System.Text; +using System.Threading; +using System.Threading.Tasks; namespace ICSharpCode.SharpZipLib.Tar { @@ -12,40 +16,78 @@ namespace ICSharpCode.SharpZipLib.Tar public class TarOutputStream : Stream { #region Constructors + /// /// Construct TarOutputStream using default block factor /// /// stream to write to + [Obsolete("No Encoding for Name field is specified, any non-ASCII bytes will be discarded")] public TarOutputStream(Stream outputStream) : this(outputStream, TarBuffer.DefaultBlockFactor) { } + /// + /// Construct TarOutputStream using default block factor + /// + /// stream to write to + /// The used for the Name fields, or null for ASCII only + public TarOutputStream(Stream outputStream, Encoding nameEncoding) + : this(outputStream, TarBuffer.DefaultBlockFactor, nameEncoding) + { + } + /// /// Construct TarOutputStream with user specified block factor /// /// stream to write to /// blocking factor + [Obsolete("No Encoding for Name field is specified, any non-ASCII bytes will be discarded")] public TarOutputStream(Stream outputStream, int blockFactor) { - if (outputStream == null) { + if (outputStream == null) + { + throw new ArgumentNullException(nameof(outputStream)); + } + + this.outputStream = outputStream; + buffer = TarBuffer.CreateOutputTarBuffer(outputStream, blockFactor); + + assemblyBuffer = ArrayPool.Shared.Rent(TarBuffer.BlockSize); + blockBuffer = ArrayPool.Shared.Rent(TarBuffer.BlockSize); + } + + /// + /// Construct TarOutputStream with user specified block factor + /// + /// stream to write to + /// blocking factor + /// The used for the Name fields, or null for ASCII only + public TarOutputStream(Stream outputStream, int blockFactor, Encoding nameEncoding) + { + if (outputStream == null) + { throw new ArgumentNullException(nameof(outputStream)); } this.outputStream = outputStream; buffer = TarBuffer.CreateOutputTarBuffer(outputStream, blockFactor); - assemblyBuffer = new byte[TarBuffer.BlockSize]; - blockBuffer = new byte[TarBuffer.BlockSize]; + assemblyBuffer = ArrayPool.Shared.Rent(TarBuffer.BlockSize); + blockBuffer = ArrayPool.Shared.Rent(TarBuffer.BlockSize); + + this.nameEncoding = nameEncoding; } - #endregion + + #endregion Constructors /// /// Gets or sets a flag indicating ownership of underlying stream. /// When the flag is true will close the underlying stream also. /// /// The default value is true. - public bool IsStreamOwner { + public bool IsStreamOwner + { get { return buffer.IsStreamOwner; } set { buffer.IsStreamOwner = value; } } @@ -53,49 +95,42 @@ public bool IsStreamOwner { /// /// true if the stream supports reading; otherwise, false. /// - public override bool CanRead { - get { - return outputStream.CanRead; - } + public override bool CanRead + { + get { return outputStream.CanRead; } } /// /// true if the stream supports seeking; otherwise, false. /// - public override bool CanSeek { - get { - return outputStream.CanSeek; - } + public override bool CanSeek + { + get { return outputStream.CanSeek; } } /// /// true if stream supports writing; otherwise, false. /// - public override bool CanWrite { - get { - return outputStream.CanWrite; - } + public override bool CanWrite + { + get { return outputStream.CanWrite; } } /// /// length of stream in bytes /// - public override long Length { - get { - return outputStream.Length; - } + public override long Length + { + get { return outputStream.Length; } } /// /// gets or sets the position within the current stream. /// - public override long Position { - get { - return outputStream.Position; - } - set { - outputStream.Position = value; - } + public override long Position + { + get { return outputStream.Position; } + set { outputStream.Position = value; } } /// @@ -119,7 +154,7 @@ public override void SetLength(long value) } /// - /// Read a byte from the stream and advance the position within the stream + /// Read a byte from the stream and advance the position within the stream /// by one byte or returns -1 if at the end of the stream. /// /// The byte value or -1 if at end of stream @@ -129,7 +164,7 @@ public override int ReadByte() } /// - /// read bytes from the current stream and advance the position within the + /// read bytes from the current stream and advance the position within the /// stream by the number of bytes read. /// /// The buffer to store read bytes in. @@ -137,30 +172,65 @@ public override int ReadByte() /// The desired number of bytes to read. /// The total number of bytes read, or zero if at the end of the stream. /// The number of bytes may be less than the count - /// requested if data is not avialable. + /// requested if data is not available. public override int Read(byte[] buffer, int offset, int count) { return outputStream.Read(buffer, offset, count); } + /// + /// read bytes from the current stream and advance the position within the + /// stream by the number of bytes read. + /// + /// The buffer to store read bytes in. + /// The index into the buffer to being storing bytes at. + /// The desired number of bytes to read. + /// + /// The total number of bytes read, or zero if at the end of the stream. + /// The number of bytes may be less than the count + /// requested if data is not available. + public override async Task ReadAsync(byte[] buffer, int offset, int count, + CancellationToken cancellationToken) + { + return await outputStream.ReadAsync(buffer, offset, count, cancellationToken).ConfigureAwait(false); + } + /// /// All buffered data is written to destination - /// + /// public override void Flush() { outputStream.Flush(); } + /// + /// All buffered data is written to destination + /// + public override async Task FlushAsync(CancellationToken cancellationToken) + { + await outputStream.FlushAsync(cancellationToken).ConfigureAwait(false); + } + /// /// Ends the TAR archive without closing the underlying OutputStream. /// The result is that the EOF block of nulls is written. /// - public void Finish() + public void Finish() => FinishAsync(CancellationToken.None, false).GetAwaiter().GetResult(); + + /// + /// Ends the TAR archive without closing the underlying OutputStream. + /// The result is that the EOF block of nulls is written. + /// + public Task FinishAsync(CancellationToken cancellationToken) => FinishAsync(cancellationToken, true); + + private async Task FinishAsync(CancellationToken cancellationToken, bool isAsync) { - if (IsEntryOpen) { - CloseEntry(); + if (IsEntryOpen) + { + await CloseEntryAsync(cancellationToken, isAsync).ConfigureAwait(false); } - WriteEofBlock(); + + await WriteEofBlockAsync(cancellationToken, isAsync).ConfigureAwait(false); } /// @@ -170,17 +240,22 @@ public void Finish() /// TarBuffer's Close(). protected override void Dispose(bool disposing) { - if (!isClosed) { + if (!isClosed) + { isClosed = true; Finish(); buffer.Close(); + + ArrayPool.Shared.Return(assemblyBuffer); + ArrayPool.Shared.Return(blockBuffer); } } /// /// Get the record size being used by this stream's TarBuffer. /// - public int RecordSize { + public int RecordSize + { get { return buffer.RecordSize; } } @@ -197,11 +272,11 @@ public int GetRecordSize() } /// - /// Get a value indicating wether an entry is open, requiring more data to be written. + /// Get a value indicating whether an entry is open, requiring more data to be written. /// - bool IsEntryOpen { + private bool IsEntryOpen + { get { return (currBytes < currSize); } - } /// @@ -216,39 +291,70 @@ bool IsEntryOpen { /// /// The TarEntry to be written to the archive. /// - public void PutNextEntry(TarEntry entry) + /// + public Task PutNextEntryAsync(TarEntry entry, CancellationToken cancellationToken) => + PutNextEntryAsync(entry, cancellationToken, true); + + /// + /// Put an entry on the output stream. This writes the entry's + /// header and positions the output stream for writing + /// the contents of the entry. Once this method is called, the + /// stream is ready for calls to write() to write the entry's + /// contents. Once the contents are written, closeEntry() + /// MUST be called to ensure that all buffered data + /// is completely written to the output stream. + /// + /// + /// The TarEntry to be written to the archive. + /// + public void PutNextEntry(TarEntry entry) => + PutNextEntryAsync(entry, CancellationToken.None, false).GetAwaiter().GetResult(); + + private async Task PutNextEntryAsync(TarEntry entry, CancellationToken cancellationToken, bool isAsync) { - if (entry == null) { + if (entry == null) + { throw new ArgumentNullException(nameof(entry)); } - if (entry.TarHeader.Name.Length > TarHeader.NAMELEN) { + var namelen = nameEncoding != null + ? nameEncoding.GetByteCount(entry.TarHeader.Name) + : entry.TarHeader.Name.Length; + + if (namelen > TarHeader.NAMELEN) + { var longHeader = new TarHeader(); longHeader.TypeFlag = TarHeader.LF_GNU_LONGNAME; longHeader.Name = longHeader.Name + "././@LongLink"; - longHeader.Mode = 420;//644 by default + longHeader.Mode = 420; //644 by default longHeader.UserId = entry.UserId; longHeader.GroupId = entry.GroupId; longHeader.GroupName = entry.GroupName; longHeader.UserName = entry.UserName; longHeader.LinkName = ""; - longHeader.Size = entry.TarHeader.Name.Length + 1; // Plus one to avoid dropping last char + longHeader.Size = namelen + 1; // Plus one to avoid dropping last char - longHeader.WriteHeader(blockBuffer); - buffer.WriteBlock(blockBuffer); // Add special long filename header block + longHeader.WriteHeader(blockBuffer, nameEncoding); + // Add special long filename header block + await buffer.WriteBlockAsync(blockBuffer, 0, cancellationToken, isAsync).ConfigureAwait(false); int nameCharIndex = 0; - while (nameCharIndex < entry.TarHeader.Name.Length + 1 /* we've allocated one for the null char, now we must make sure it gets written out */) { + while + (nameCharIndex < + namelen + 1 /* we've allocated one for the null char, now we must make sure it gets written out */) + { Array.Clear(blockBuffer, 0, blockBuffer.Length); - TarHeader.GetAsciiBytes(entry.TarHeader.Name, nameCharIndex, this.blockBuffer, 0, TarBuffer.BlockSize); // This func handles OK the extra char out of string length + TarHeader.GetAsciiBytes(entry.TarHeader.Name, nameCharIndex, this.blockBuffer, 0, + TarBuffer.BlockSize, nameEncoding); // This func handles OK the extra char out of string length nameCharIndex += TarBuffer.BlockSize; - buffer.WriteBlock(blockBuffer); + + await buffer.WriteBlockAsync(blockBuffer, 0, cancellationToken, isAsync).ConfigureAwait(false); } } - entry.WriteEntryHeader(blockBuffer); - buffer.WriteBlock(blockBuffer); + entry.WriteEntryHeader(blockBuffer, nameEncoding); + await buffer.WriteBlockAsync(blockBuffer, 0, cancellationToken, isAsync).ConfigureAwait(false); currBytes = 0; @@ -264,18 +370,33 @@ public void PutNextEntry(TarEntry entry) /// to the output stream before this entry is closed and the /// next entry written. /// - public void CloseEntry() + public Task CloseEntryAsync(CancellationToken cancellationToken) => CloseEntryAsync(cancellationToken, true); + + /// + /// Close an entry. This method MUST be called for all file + /// entries that contain data. The reason is that we must + /// buffer data written to the stream in order to satisfy + /// the buffer's block based writes. Thus, there may be + /// data fragments still being assembled that must be written + /// to the output stream before this entry is closed and the + /// next entry written. + /// + public void CloseEntry() => CloseEntryAsync(CancellationToken.None, false).GetAwaiter().GetResult(); + + private async Task CloseEntryAsync(CancellationToken cancellationToken, bool isAsync) { - if (assemblyBufferLength > 0) { + if (assemblyBufferLength > 0) + { Array.Clear(assemblyBuffer, assemblyBufferLength, assemblyBuffer.Length - assemblyBufferLength); - buffer.WriteBlock(assemblyBuffer); + await buffer.WriteBlockAsync(assemblyBuffer, 0, cancellationToken, isAsync).ConfigureAwait(false); currBytes += assemblyBufferLength; assemblyBufferLength = 0; } - if (currBytes < currSize) { + if (currBytes < currSize) + { string errorText = string.Format( "Entry closed at '{0}' before the '{1}' bytes specified in the header were written", currBytes, currSize); @@ -292,7 +413,10 @@ public void CloseEntry() /// public override void WriteByte(byte value) { - Write(new byte[] { value }, 0, 1); + var oneByteArray = ArrayPool.Shared.Rent(1); + oneByteArray[0] = value; + Write(oneByteArray, 0, 1); + ArrayPool.Shared.Return(oneByteArray); } /// @@ -313,25 +437,55 @@ public override void WriteByte(byte value) /// /// The number of bytes to write. /// - public override void Write(byte[] buffer, int offset, int count) + public override void Write(byte[] buffer, int offset, int count) => + WriteAsync(buffer, offset, count, CancellationToken.None, false).GetAwaiter().GetResult(); + + /// + /// Writes bytes to the current tar archive entry. This method + /// is aware of the current entry and will throw an exception if + /// you attempt to write bytes past the length specified for the + /// current entry. The method is also (painfully) aware of the + /// record buffering required by TarBuffer, and manages buffers + /// that are not a multiple of recordsize in length, including + /// assembling records from small buffers. + /// + /// + /// The buffer to write to the archive. + /// + /// + /// The offset in the buffer from which to get bytes. + /// + /// + /// The number of bytes to write. + /// + /// + public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) => + WriteAsync(buffer, offset, count, cancellationToken, true); + + private async Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken, bool isAsync) { - if (buffer == null) { + if (buffer == null) + { throw new ArgumentNullException(nameof(buffer)); } - if (offset < 0) { + if (offset < 0) + { throw new ArgumentOutOfRangeException(nameof(offset), "Cannot be negative"); } - if (buffer.Length - offset < count) { + if (buffer.Length - offset < count) + { throw new ArgumentException("offset and count combination is invalid"); } - if (count < 0) { + if (count < 0) + { throw new ArgumentOutOfRangeException(nameof(count), "Cannot be negative"); } - if ((currBytes + count) > currSize) { + if ((currBytes + count) > currSize) + { string errorText = string.Format("request to write '{0}' bytes exceeds size in header of '{1}' bytes", count, this.currSize); throw new ArgumentOutOfRangeException(nameof(count), errorText); @@ -344,14 +498,16 @@ public override void Write(byte[] buffer, int offset, int count) // TODO REVIEW Maybe this should be in TarBuffer? Could that help to // eliminate some of the buffer copying. // - if (assemblyBufferLength > 0) { - if ((assemblyBufferLength + count) >= blockBuffer.Length) { + if (assemblyBufferLength > 0) + { + if ((assemblyBufferLength + count) >= blockBuffer.Length) + { int aLen = blockBuffer.Length - assemblyBufferLength; Array.Copy(assemblyBuffer, 0, blockBuffer, 0, assemblyBufferLength); Array.Copy(buffer, offset, blockBuffer, assemblyBufferLength, aLen); - this.buffer.WriteBlock(blockBuffer); + await this.buffer.WriteBlockAsync(blockBuffer, 0, cancellationToken, isAsync).ConfigureAwait(false); currBytes += blockBuffer.Length; @@ -359,7 +515,9 @@ public override void Write(byte[] buffer, int offset, int count) count -= aLen; assemblyBufferLength = 0; - } else { + } + else + { Array.Copy(buffer, offset, assemblyBuffer, assemblyBufferLength, count); offset += count; assemblyBufferLength += count; @@ -372,14 +530,16 @@ public override void Write(byte[] buffer, int offset, int count) // o An empty "assembly" buffer. // o No bytes to write (count == 0) // - while (count > 0) { - if (count < blockBuffer.Length) { + while (count > 0) + { + if (count < blockBuffer.Length) + { Array.Copy(buffer, offset, assemblyBuffer, assemblyBufferLength, count); assemblyBufferLength += count; break; } - this.buffer.WriteBlock(buffer, offset); + await this.buffer.WriteBlockAsync(buffer, offset, cancellationToken, isAsync).ConfigureAwait(false); int bufferLength = blockBuffer.Length; currBytes += bufferLength; @@ -392,28 +552,29 @@ public override void Write(byte[] buffer, int offset, int count) /// Write an EOF (end of archive) block to the tar archive. /// The end of the archive is indicated by two blocks consisting entirely of zero bytes. /// - void WriteEofBlock() + private async Task WriteEofBlockAsync(CancellationToken cancellationToken, bool isAsync) { Array.Clear(blockBuffer, 0, blockBuffer.Length); - buffer.WriteBlock(blockBuffer); - buffer.WriteBlock(blockBuffer); + await buffer.WriteBlockAsync(blockBuffer, 0, cancellationToken, isAsync).ConfigureAwait(false); + await buffer.WriteBlockAsync(blockBuffer, 0, cancellationToken, isAsync).ConfigureAwait(false); } #region Instance Fields + /// /// bytes written for this entry so far /// - long currBytes; + private long currBytes; /// /// current 'Assembly' buffer length - /// - int assemblyBufferLength; + /// + private int assemblyBufferLength; /// - /// Flag indicating wether this instance has been closed or not. + /// Flag indicating whether this instance has been closed or not. /// - bool isClosed; + private bool isClosed; /// /// Size for the current entry @@ -421,7 +582,7 @@ void WriteEofBlock() protected long currSize; /// - /// single block working buffer + /// single block working buffer /// protected byte[] blockBuffer; @@ -439,6 +600,12 @@ void WriteEofBlock() /// the destination stream for the archive contents /// protected Stream outputStream; - #endregion + + /// + /// name encoding + /// + protected Encoding nameEncoding; + + #endregion Instance Fields } } diff --git a/src/ICSharpCode.SharpZipLib/Tar/TarStringExtension.cs b/src/ICSharpCode.SharpZipLib/Tar/TarStringExtension.cs new file mode 100644 index 000000000..433c6a424 --- /dev/null +++ b/src/ICSharpCode.SharpZipLib/Tar/TarStringExtension.cs @@ -0,0 +1,13 @@ +using System.IO; +using ICSharpCode.SharpZipLib.Core; + +namespace ICSharpCode.SharpZipLib.Tar +{ + internal static class TarStringExtension + { + public static string ToTarArchivePath(this string s) + { + return PathUtils.DropPathRoot(s).Replace(Path.DirectorySeparatorChar, '/'); + } + } +} diff --git a/src/ICSharpCode.SharpZipLib/Zip/Compression/Deflater.cs b/src/ICSharpCode.SharpZipLib/Zip/Compression/Deflater.cs index 47a45bef3..3dbe98c8d 100644 --- a/src/ICSharpCode.SharpZipLib/Zip/Compression/Deflater.cs +++ b/src/ICSharpCode.SharpZipLib/Zip/Compression/Deflater.cs @@ -9,12 +9,13 @@ namespace ICSharpCode.SharpZipLib.Zip.Compression /// /// This class is not thread safe. This is inherent in the API, due /// to the split of deflate and setInput. - /// + /// /// author of the original java version : Jochen Hoenicke /// public class Deflater { #region Deflater Documentation + /* * The Deflater can do the following state transitions: * @@ -49,8 +50,11 @@ public class Deflater * (7) At any time (7) * */ - #endregion + + #endregion Deflater Documentation + #region Public Constants + /// /// The best and slowest compression level. This tries to find very /// long and distant string repetitions. @@ -77,65 +81,73 @@ public class Deflater /// There is no need to use this constant at all. /// public const int DEFLATED = 8; - #endregion - #region Public Enum - - /// - /// Compression Level as an enum for safer use - /// - public enum CompressionLevel - { - /// - /// The best and slowest compression level. This tries to find very - /// long and distant string repetitions. - /// - BEST_COMPRESSION = Deflater.BEST_COMPRESSION, - - /// - /// The worst but fastest compression level. - /// - BEST_SPEED = Deflater.BEST_SPEED, - - /// - /// The default compression level. - /// - DEFAULT_COMPRESSION = Deflater.DEFAULT_COMPRESSION, - - /// - /// This level won't compress at all but output uncompressed blocks. - /// - NO_COMPRESSION = Deflater.NO_COMPRESSION, - - /// - /// The compression method. This is the only method supported so far. - /// There is no need to use this constant at all. - /// - DEFLATED = Deflater.DEFLATED - } - - #endregion - #region Local Constants - private const int IS_SETDICT = 0x01; + + #endregion Public Constants + + #region Public Enum + + /// + /// Compression Level as an enum for safer use + /// + public enum CompressionLevel + { + /// + /// The best and slowest compression level. This tries to find very + /// long and distant string repetitions. + /// + BEST_COMPRESSION = Deflater.BEST_COMPRESSION, + + /// + /// The worst but fastest compression level. + /// + BEST_SPEED = Deflater.BEST_SPEED, + + /// + /// The default compression level. + /// + DEFAULT_COMPRESSION = Deflater.DEFAULT_COMPRESSION, + + /// + /// This level won't compress at all but output uncompressed blocks. + /// + NO_COMPRESSION = Deflater.NO_COMPRESSION, + + /// + /// The compression method. This is the only method supported so far. + /// There is no need to use this constant at all. + /// + DEFLATED = Deflater.DEFLATED + } + + #endregion Public Enum + + #region Local Constants + + private const int IS_SETDICT = 0x01; private const int IS_FLUSHING = 0x04; private const int IS_FINISHING = 0x08; private const int INIT_STATE = 0x00; private const int SETDICT_STATE = 0x01; + // private static int INIT_FINISHING_STATE = 0x08; // private static int SETDICT_FINISHING_STATE = 0x09; private const int BUSY_STATE = 0x10; + private const int FLUSHING_STATE = 0x14; private const int FINISHING_STATE = 0x1c; private const int FINISHED_STATE = 0x1e; private const int CLOSED_STATE = 0x7f; - #endregion + + #endregion Local Constants + #region Constructors + /// /// Creates a new deflater with default compression level. /// public Deflater() : this(DEFAULT_COMPRESSION, false) { - } /// @@ -148,7 +160,6 @@ public Deflater() : this(DEFAULT_COMPRESSION, false) /// if lvl is out of range. public Deflater(int level) : this(level, false) { - } /// @@ -166,20 +177,24 @@ public Deflater(int level) : this(level, false) /// if lvl is out of range. public Deflater(int level, bool noZlibHeaderOrFooter) { - if (level == DEFAULT_COMPRESSION) { + if (level == DEFAULT_COMPRESSION) + { level = 6; - } else if (level < NO_COMPRESSION || level > BEST_COMPRESSION) { + } + else if (level < NO_COMPRESSION || level > BEST_COMPRESSION) + { throw new ArgumentOutOfRangeException(nameof(level)); } pending = new DeflaterPending(); - engine = new DeflaterEngine(pending); + engine = new DeflaterEngine(pending, noZlibHeaderOrFooter); this.noZlibHeaderOrFooter = noZlibHeaderOrFooter; SetStrategy(DeflateStrategy.Default); SetLevel(level); Reset(); } - #endregion + + #endregion Constructors /// /// Resets the deflater. The deflater acts afterwards as if it was @@ -197,8 +212,10 @@ public void Reset() /// /// Gets the current adler checksum of the data that was processed so far. /// - public int Adler { - get { + public int Adler + { + get + { return engine.Adler; } } @@ -206,8 +223,10 @@ public int Adler { /// /// Gets the number of input bytes processed so far. /// - public long TotalIn { - get { + public long TotalIn + { + get + { return engine.TotalIn; } } @@ -215,8 +234,10 @@ public long TotalIn { /// /// Gets the number of output bytes so far. /// - public long TotalOut { - get { + public long TotalOut + { + get + { return totalOut; } } @@ -247,20 +268,24 @@ public void Finish() /// Returns true if the stream was finished and no more output bytes /// are available. /// - public bool IsFinished { - get { + public bool IsFinished + { + get + { return (state == FINISHED_STATE) && pending.IsFlushed; } } /// /// Returns true, if the input buffer is empty. - /// You should then call setInput(). + /// You should then call setInput(). /// NOTE: This method can also return true when the stream /// was finished. /// - public bool IsNeedingInput { - get { + public bool IsNeedingInput + { + get + { return engine.NeedsInput(); } } @@ -305,7 +330,8 @@ public void SetInput(byte[] input) /// public void SetInput(byte[] input, int offset, int count) { - if ((state & IS_FINISHING) != 0) { + if ((state & IS_FINISHING) != 0) + { throw new InvalidOperationException("Finish() already called"); } engine.SetInput(input, offset, count); @@ -322,13 +348,17 @@ public void SetInput(byte[] input, int offset, int count) /// public void SetLevel(int level) { - if (level == DEFAULT_COMPRESSION) { + if (level == DEFAULT_COMPRESSION) + { level = 6; - } else if (level < NO_COMPRESSION || level > BEST_COMPRESSION) { + } + else if (level < NO_COMPRESSION || level > BEST_COMPRESSION) + { throw new ArgumentOutOfRangeException(nameof(level)); } - if (this.level != level) { + if (this.level != level) + { this.level = level; engine.SetLevel(level); } @@ -398,27 +428,32 @@ public int Deflate(byte[] output, int offset, int length) { int origLength = length; - if (state == CLOSED_STATE) { + if (state == CLOSED_STATE) + { throw new InvalidOperationException("Deflater closed"); } - if (state < BUSY_STATE) { + if (state < BUSY_STATE) + { // output header int header = (DEFLATED + ((DeflaterConstants.MAX_WBITS - 8) << 4)) << 8; int level_flags = (level - 1) >> 1; - if (level_flags < 0 || level_flags > 3) { + if (level_flags < 0 || level_flags > 3) + { level_flags = 3; } header |= level_flags << 6; - if ((state & IS_SETDICT) != 0) { + if ((state & IS_SETDICT) != 0) + { // Dictionary was set header |= DeflaterConstants.PRESET_DICT; } header += 31 - (header % 31); pending.WriteShortMSB(header); - if ((state & IS_SETDICT) != 0) { + if ((state & IS_SETDICT) != 0) + { int chksum = engine.Adler; engine.ResetAdler(); pending.WriteShortMSB(chksum >> 16); @@ -428,29 +463,36 @@ public int Deflate(byte[] output, int offset, int length) state = BUSY_STATE | (state & (IS_FLUSHING | IS_FINISHING)); } - for (;;) { + for (; ; ) + { int count = pending.Flush(output, offset, length); offset += count; totalOut += count; length -= count; - if (length == 0 || state == FINISHED_STATE) { + if (length == 0 || state == FINISHED_STATE) + { break; } - if (!engine.Deflate((state & IS_FLUSHING) != 0, (state & IS_FINISHING) != 0)) { - switch (state) { + if (!engine.Deflate((state & IS_FLUSHING) != 0, (state & IS_FINISHING) != 0)) + { + switch (state) + { case BUSY_STATE: // We need more input now return origLength - length; + case FLUSHING_STATE: - if (level != NO_COMPRESSION) { + if (level != NO_COMPRESSION) + { /* We have to supply some lookahead. 8 bit lookahead * is needed by the zlib inflater, and we must fill * the next byte, so that all bits are flushed. */ int neededbits = 8 + ((-pending.BitCount) & 7); - while (neededbits > 0) { + while (neededbits > 0) + { /* write a static tree block consisting solely of * an EOF: */ @@ -460,11 +502,13 @@ public int Deflate(byte[] output, int offset, int length) } state = BUSY_STATE; break; + case FINISHING_STATE: pending.AlignToByte(); // Compressed data is complete. Write footer information if required. - if (!noZlibHeaderOrFooter) { + if (!noZlibHeaderOrFooter) + { int adler = engine.Adler; pending.WriteShortMSB(adler >> 16); pending.WriteShortMSB(adler & 0xffff); @@ -514,7 +558,8 @@ public void SetDictionary(byte[] dictionary) /// public void SetDictionary(byte[] dictionary, int index, int count) { - if (state != INIT_STATE) { + if (state != INIT_STATE) + { throw new InvalidOperationException(); } @@ -523,35 +568,37 @@ public void SetDictionary(byte[] dictionary, int index, int count) } #region Instance Fields + /// /// Compression level. /// - int level; + private int level; /// /// If true no Zlib/RFC1950 headers or footers are generated /// - bool noZlibHeaderOrFooter; + private bool noZlibHeaderOrFooter; /// /// The current state. /// - int state; + private int state; /// /// The total bytes of output written. /// - long totalOut; + private long totalOut; /// /// The pending output. /// - DeflaterPending pending; + private DeflaterPending pending; /// /// The deflater engine. /// - DeflaterEngine engine; - #endregion + private DeflaterEngine engine; + + #endregion Instance Fields } } diff --git a/src/ICSharpCode.SharpZipLib/Zip/Compression/DeflaterConstants.cs b/src/ICSharpCode.SharpZipLib/Zip/Compression/DeflaterConstants.cs index b66efb06e..b7c7d2a69 100644 --- a/src/ICSharpCode.SharpZipLib/Zip/Compression/DeflaterConstants.cs +++ b/src/ICSharpCode.SharpZipLib/Zip/Compression/DeflaterConstants.cs @@ -5,6 +5,7 @@ namespace ICSharpCode.SharpZipLib.Zip.Compression /// /// This class contains constants used for deflation. /// + [System.Diagnostics.CodeAnalysis.SuppressMessage("Naming", "CA1707:Identifiers should not contain underscores", Justification = "kept for backwards compatibility")] public static class DeflaterConstants { /// @@ -39,108 +40,107 @@ public static class DeflaterConstants /// /// Internal compression engine constant - /// + /// public const int MAX_MATCH = 258; /// /// Internal compression engine constant - /// + /// public const int MIN_MATCH = 3; /// /// Internal compression engine constant - /// + /// public const int MAX_WBITS = 15; /// /// Internal compression engine constant - /// + /// public const int WSIZE = 1 << MAX_WBITS; /// /// Internal compression engine constant - /// + /// public const int WMASK = WSIZE - 1; /// /// Internal compression engine constant - /// + /// public const int HASH_BITS = DEFAULT_MEM_LEVEL + 7; /// /// Internal compression engine constant - /// + /// public const int HASH_SIZE = 1 << HASH_BITS; /// /// Internal compression engine constant - /// + /// public const int HASH_MASK = HASH_SIZE - 1; /// /// Internal compression engine constant - /// + /// public const int HASH_SHIFT = (HASH_BITS + MIN_MATCH - 1) / MIN_MATCH; /// /// Internal compression engine constant - /// + /// public const int MIN_LOOKAHEAD = MAX_MATCH + MIN_MATCH + 1; /// /// Internal compression engine constant - /// + /// public const int MAX_DIST = WSIZE - MIN_LOOKAHEAD; /// /// Internal compression engine constant - /// + /// public const int PENDING_BUF_SIZE = 1 << (DEFAULT_MEM_LEVEL + 8); /// /// Internal compression engine constant - /// + /// public static int MAX_BLOCK_SIZE = Math.Min(65535, PENDING_BUF_SIZE - 5); /// /// Internal compression engine constant - /// + /// public const int DEFLATE_STORED = 0; /// /// Internal compression engine constant - /// + /// public const int DEFLATE_FAST = 1; /// /// Internal compression engine constant - /// + /// public const int DEFLATE_SLOW = 2; /// /// Internal compression engine constant - /// + /// public static int[] GOOD_LENGTH = { 0, 4, 4, 4, 4, 8, 8, 8, 32, 32 }; /// /// Internal compression engine constant - /// + /// public static int[] MAX_LAZY = { 0, 4, 5, 6, 4, 16, 16, 32, 128, 258 }; /// /// Internal compression engine constant - /// + /// public static int[] NICE_LENGTH = { 0, 8, 16, 32, 16, 32, 128, 128, 258, 258 }; /// /// Internal compression engine constant - /// + /// public static int[] MAX_CHAIN = { 0, 4, 8, 32, 16, 32, 128, 256, 1024, 4096 }; /// /// Internal compression engine constant - /// + /// public static int[] COMPR_FUNC = { 0, 1, 1, 1, 1, 2, 2, 2, 2, 2 }; - } } diff --git a/src/ICSharpCode.SharpZipLib/Zip/Compression/DeflaterEngine.cs b/src/ICSharpCode.SharpZipLib/Zip/Compression/DeflaterEngine.cs index b8eedc3d0..556911c40 100644 --- a/src/ICSharpCode.SharpZipLib/Zip/Compression/DeflaterEngine.cs +++ b/src/ICSharpCode.SharpZipLib/Zip/Compression/DeflaterEngine.cs @@ -1,5 +1,5 @@ -using System; using ICSharpCode.SharpZipLib.Checksum; +using System; namespace ICSharpCode.SharpZipLib.Zip.Compression { @@ -19,7 +19,6 @@ public enum DeflateStrategy /// Filtered = 1, - /// /// This strategy will not look for string repetitions at all. It /// only encodes with Huffman trees (which means, that more common @@ -29,18 +28,17 @@ public enum DeflateStrategy } // DEFLATE ALGORITHM: - // + // // The uncompressed stream is inserted into the window array. When // the window array is full the first half is thrown away and the // second half is copied to the beginning. // // The head array is a hash table. Three characters build a hash value - // and they the value points to the corresponding index in window of + // and they the value points to the corresponding index in window of // the last string with this hash. The prev array implements a // linked list of matches with the same hash: prev[index & WMASK] points // to the previous index with the same hash. - // - + // /// /// Low level compression engine for deflate algorithm which uses a 32K sliding window @@ -49,21 +47,42 @@ public enum DeflateStrategy public class DeflaterEngine { #region Constants - const int TooFar = 4096; - #endregion + + private const int TooFar = 4096; + + #endregion Constants #region Constructors + /// /// Construct instance with pending buffer + /// Adler calculation will be performed /// /// /// Pending buffer to use - /// > + /// public DeflaterEngine(DeflaterPending pending) + : this (pending, false) + { + } + + + + /// + /// Construct instance with pending buffer + /// + /// + /// Pending buffer to use + /// + /// + /// If no adler calculation should be performed + /// + public DeflaterEngine(DeflaterPending pending, bool noAdlerCalculation) { this.pending = pending; huffman = new DeflaterHuffman(pending); - adler = new Adler32(); + if (!noAdlerCalculation) + adler = new Adler32(); window = new byte[2 * DeflaterConstants.WSIZE]; head = new short[DeflaterConstants.HASH_SIZE]; @@ -74,7 +93,7 @@ public DeflaterEngine(DeflaterPending pending) blockStart = strstart = 1; } - #endregion + #endregion Constructors /// /// Deflate drives actual compression of data @@ -85,7 +104,8 @@ public DeflaterEngine(DeflaterPending pending) public bool Deflate(bool flush, bool finish) { bool progress; - do { + do + { FillWindow(); bool canFlush = flush && (inputOff == inputEnd); @@ -95,16 +115,20 @@ public bool Deflate(bool flush, bool finish) + lookahead + "], " + compressionFunction + "," + canFlush); } #endif - switch (compressionFunction) { + switch (compressionFunction) + { case DeflaterConstants.DEFLATE_STORED: progress = DeflateStored(canFlush, finish); break; + case DeflaterConstants.DEFLATE_FAST: progress = DeflateFast(canFlush, finish); break; + case DeflaterConstants.DEFLATE_SLOW: progress = DeflateSlow(canFlush, finish); break; + default: throw new InvalidOperationException("unknown compressionFunction"); } @@ -121,19 +145,23 @@ public bool Deflate(bool flush, bool finish) /// The number of bytes of data to use as input. public void SetInput(byte[] buffer, int offset, int count) { - if (buffer == null) { + if (buffer == null) + { throw new ArgumentNullException(nameof(buffer)); } - if (offset < 0) { + if (offset < 0) + { throw new ArgumentOutOfRangeException(nameof(offset)); } - if (count < 0) { + if (count < 0) + { throw new ArgumentOutOfRangeException(nameof(count)); } - if (inputOff < inputEnd) { + if (inputOff < inputEnd) + { throw new InvalidOperationException("Old input was not completely processed"); } @@ -142,7 +170,8 @@ public void SetInput(byte[] buffer, int offset, int count) /* We want to throw an ArrayIndexOutOfBoundsException early. The * check is very tricky: it also handles integer wrap around. */ - if ((offset > end) || (end > buffer.Length)) { + if ((offset > end) || (end > buffer.Length)) + { throw new ArgumentOutOfRangeException(nameof(count)); } @@ -153,7 +182,7 @@ public void SetInput(byte[] buffer, int offset, int count) /// /// Determines if more input is needed. - /// + /// /// Return true if input is needed via SetInput public bool NeedsInput() { @@ -169,17 +198,19 @@ public bool NeedsInput() public void SetDictionary(byte[] buffer, int offset, int length) { #if DebugDeflation - if (DeflaterConstants.DEBUGGING && (strstart != 1) ) + if (DeflaterConstants.DEBUGGING && (strstart != 1) ) { throw new InvalidOperationException("strstart not 1"); } #endif - adler.Update(buffer, offset, length); - if (length < DeflaterConstants.MIN_MATCH) { + adler?.Update(new ArraySegment(buffer, offset, length)); + if (length < DeflaterConstants.MIN_MATCH) + { return; } - if (length > DeflaterConstants.MAX_DIST) { + if (length > DeflaterConstants.MAX_DIST) + { offset += length - DeflaterConstants.MAX_DIST; length = DeflaterConstants.MAX_DIST; } @@ -188,7 +219,8 @@ public void SetDictionary(byte[] buffer, int offset, int length) UpdateHash(); --length; - while (--length > 0) { + while (--length > 0) + { InsertString(); strstart++; } @@ -198,60 +230,69 @@ public void SetDictionary(byte[] buffer, int offset, int length) /// /// Reset internal state - /// + /// public void Reset() { huffman.Reset(); - adler.Reset(); + adler?.Reset(); blockStart = strstart = 1; lookahead = 0; totalIn = 0; prevAvailable = false; matchLen = DeflaterConstants.MIN_MATCH - 1; - for (int i = 0; i < DeflaterConstants.HASH_SIZE; i++) { + for (int i = 0; i < DeflaterConstants.HASH_SIZE; i++) + { head[i] = 0; } - for (int i = 0; i < DeflaterConstants.WSIZE; i++) { + for (int i = 0; i < DeflaterConstants.WSIZE; i++) + { prev[i] = 0; } } /// /// Reset Adler checksum - /// + /// public void ResetAdler() { - adler.Reset(); + adler?.Reset(); } /// /// Get current value of Adler checksum - /// - public int Adler { - get { - return unchecked((int)adler.Value); + /// + public int Adler + { + get + { + return (adler != null) ? unchecked((int)adler.Value) : 0; } } /// /// Total data processed - /// - public long TotalIn { - get { + /// + public long TotalIn + { + get + { return totalIn; } } /// /// Get/set the deflate strategy - /// - public DeflateStrategy Strategy { - get { + /// + public DeflateStrategy Strategy + { + get + { return strategy; } - set { + set + { strategy = value; } } @@ -262,7 +303,8 @@ public DeflateStrategy Strategy { /// The value to set the level to. public void SetLevel(int level) { - if ((level < 0) || (level > 9)) { + if ((level < 0) || (level > 9)) + { throw new ArgumentOutOfRangeException(nameof(level)); } @@ -271,17 +313,19 @@ public void SetLevel(int level) niceLength = DeflaterConstants.NICE_LENGTH[level]; max_chain = DeflaterConstants.MAX_CHAIN[level]; - if (DeflaterConstants.COMPR_FUNC[level] != compressionFunction) { - + if (DeflaterConstants.COMPR_FUNC[level] != compressionFunction) + { #if DebugDeflation if (DeflaterConstants.DEBUGGING) { Console.WriteLine("Change from " + compressionFunction + " to " + DeflaterConstants.COMPR_FUNC[level]); } #endif - switch (compressionFunction) { + switch (compressionFunction) + { case DeflaterConstants.DEFLATE_STORED: - if (strstart > blockStart) { + if (strstart > blockStart) + { huffman.FlushStoredBlock(window, blockStart, strstart - blockStart, false); blockStart = strstart; @@ -290,7 +334,8 @@ public void SetLevel(int level) break; case DeflaterConstants.DEFLATE_FAST: - if (strstart > blockStart) { + if (strstart > blockStart) + { huffman.FlushBlock(window, blockStart, strstart - blockStart, false); blockStart = strstart; @@ -298,10 +343,12 @@ public void SetLevel(int level) break; case DeflaterConstants.DEFLATE_SLOW: - if (prevAvailable) { + if (prevAvailable) + { huffman.TallyLit(window[strstart - 1] & 0xff); } - if (strstart > blockStart) { + if (strstart > blockStart) + { huffman.FlushBlock(window, blockStart, strstart - blockStart, false); blockStart = strstart; } @@ -321,34 +368,38 @@ public void FillWindow() /* 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 (strstart >= DeflaterConstants.WSIZE + DeflaterConstants.MAX_DIST) { + if (strstart >= DeflaterConstants.WSIZE + DeflaterConstants.MAX_DIST) + { SlideWindow(); } /* If there is not enough lookahead, but still some input left, * read in the input */ - if (lookahead < DeflaterConstants.MIN_LOOKAHEAD && inputOff < inputEnd) { + if (lookahead < DeflaterConstants.MIN_LOOKAHEAD && inputOff < inputEnd) + { int more = 2 * DeflaterConstants.WSIZE - lookahead - strstart; - if (more > inputEnd - inputOff) { + if (more > inputEnd - inputOff) + { more = inputEnd - inputOff; } System.Array.Copy(inputBuf, inputOff, window, strstart + lookahead, more); - adler.Update(inputBuf, inputOff, more); + adler?.Update(new ArraySegment(inputBuf, inputOff, more)); inputOff += more; totalIn += more; lookahead += more; } - if (lookahead >= DeflaterConstants.MIN_MATCH) { + if (lookahead >= DeflaterConstants.MIN_MATCH) + { UpdateHash(); } } - void UpdateHash() + private void UpdateHash() { /* if (DEBUGGING) { @@ -363,16 +414,16 @@ void UpdateHash() /// value for this hash. /// /// The previous hash value - int InsertString() + private int InsertString() { short match; int hash = ((ins_h << DeflaterConstants.HASH_SHIFT) ^ window[strstart + (DeflaterConstants.MIN_MATCH - 1)]) & DeflaterConstants.HASH_MASK; #if DebugDeflation - if (DeflaterConstants.DEBUGGING) + if (DeflaterConstants.DEBUGGING) { - if (hash != (((window[strstart] << (2*HASH_SHIFT)) ^ - (window[strstart + 1] << HASH_SHIFT) ^ + if (hash != (((window[strstart] << (2*HASH_SHIFT)) ^ + (window[strstart + 1] << HASH_SHIFT) ^ (window[strstart + 2])) & HASH_MASK)) { throw new SharpZipBaseException("hash inconsistent: " + hash + "/" +window[strstart] + "," @@ -387,7 +438,7 @@ int InsertString() return match & 0xffff; } - void SlideWindow() + private void SlideWindow() { Array.Copy(window, DeflaterConstants.WSIZE, window, 0, DeflaterConstants.WSIZE); matchStart -= DeflaterConstants.WSIZE; @@ -396,20 +447,22 @@ void SlideWindow() // Slide the hash table (could be avoided with 32 bit values // at the expense of memory usage). - for (int i = 0; i < DeflaterConstants.HASH_SIZE; ++i) { + for (int i = 0; i < DeflaterConstants.HASH_SIZE; ++i) + { int m = head[i] & 0xffff; head[i] = (short)(m >= DeflaterConstants.WSIZE ? (m - DeflaterConstants.WSIZE) : 0); } // Slide the prev table. - for (int i = 0; i < DeflaterConstants.WSIZE; i++) { + for (int i = 0; i < DeflaterConstants.WSIZE; i++) + { int m = prev[i] & 0xffff; prev[i] = (short)(m >= DeflaterConstants.WSIZE ? (m - DeflaterConstants.WSIZE) : 0); } } /// - /// Find the best (longest) string in the window matching the + /// Find the best (longest) string in the window matching the /// string starting at strstart. /// /// Preconditions: @@ -418,136 +471,150 @@ void SlideWindow() /// /// /// True if a match greater than the minimum length is found - bool FindLongestMatch( int curMatch ) + private bool FindLongestMatch(int curMatch) { - int match; - int scan = strstart; - // scanMax is the highest position that we can look at - int scanMax = scan + Math.Min( DeflaterConstants.MAX_MATCH, lookahead ) - 1; - int limit = Math.Max( scan - DeflaterConstants.MAX_DIST, 0 ); - - byte[] window = this.window; - short[] prev = this.prev; - int chainLength = this.max_chain; - int niceLength = Math.Min( this.niceLength, lookahead ); - - matchLen = Math.Max( matchLen, DeflaterConstants.MIN_MATCH - 1 ); - - if (scan + matchLen > scanMax) return false; - - byte scan_end1 = window[scan + matchLen - 1]; - byte scan_end = window[scan + matchLen]; - - // Do not waste too much time if we already have a good match: - if (matchLen >= this.goodLength) chainLength >>= 2; - - do - { - match = curMatch; - scan = strstart; - - if (window[match + matchLen] != scan_end - || window[match + matchLen - 1] != scan_end1 - || window[match] != window[scan] - || window[++match] != window[++scan]) - { - continue; - } - - // scan is set to strstart+1 and the comparison passed, so - // scanMax - scan is the maximum number of bytes we can compare. - // below we compare 8 bytes at a time, so first we compare - // (scanMax - scan) % 8 bytes, so the remainder is a multiple of 8 - - switch( (scanMax - scan) % 8 ) - { - case 1: if (window[++scan] == window[++match]) break; - break; - case 2: if (window[++scan] == window[++match] - && window[++scan] == window[++match]) break; - break; - case 3: if (window[++scan] == window[++match] - && window[++scan] == window[++match] - && window[++scan] == window[++match]) break; - break; - case 4: if (window[++scan] == window[++match] - && window[++scan] == window[++match] - && window[++scan] == window[++match] - && window[++scan] == window[++match]) break; - break; - case 5: if (window[++scan] == window[++match] - && window[++scan] == window[++match] - && window[++scan] == window[++match] - && window[++scan] == window[++match] - && window[++scan] == window[++match]) break; - break; - case 6: if (window[++scan] == window[++match] - && window[++scan] == window[++match] - && window[++scan] == window[++match] - && window[++scan] == window[++match] - && window[++scan] == window[++match] - && window[++scan] == window[++match]) break; - break; - case 7: if (window[++scan] == window[++match] - && window[++scan] == window[++match] - && window[++scan] == window[++match] - && window[++scan] == window[++match] - && window[++scan] == window[++match] - && window[++scan] == window[++match] - && window[++scan] == window[++match]) break; - break; - } - - if (window[scan] == window[match]) - { - /* We check for insufficient lookahead only every 8th comparison; - * the 256th check will be made at strstart + 258 unless lookahead is - * exhausted first. - */ - do - { - if (scan == scanMax) - { - ++scan; // advance to first position not matched - ++match; - - break; - } - } - while (window[++scan] == window[++match] - && window[++scan] == window[++match] - && window[++scan] == window[++match] - && window[++scan] == window[++match] - && window[++scan] == window[++match] - && window[++scan] == window[++match] - && window[++scan] == window[++match] - && window[++scan] == window[++match]); - } - - if (scan - strstart > matchLen) - { - #if DebugDeflation + int match; + int scan = strstart; + // scanMax is the highest position that we can look at + int scanMax = scan + Math.Min(DeflaterConstants.MAX_MATCH, lookahead) - 1; + int limit = Math.Max(scan - DeflaterConstants.MAX_DIST, 0); + + byte[] window = this.window; + short[] prev = this.prev; + int chainLength = this.max_chain; + int niceLength = Math.Min(this.niceLength, lookahead); + + matchLen = Math.Max(matchLen, DeflaterConstants.MIN_MATCH - 1); + + if (scan + matchLen > scanMax) return false; + + byte scan_end1 = window[scan + matchLen - 1]; + byte scan_end = window[scan + matchLen]; + + // Do not waste too much time if we already have a good match: + if (matchLen >= this.goodLength) chainLength >>= 2; + + do + { + match = curMatch; + scan = strstart; + + if (window[match + matchLen] != scan_end + || window[match + matchLen - 1] != scan_end1 + || window[match] != window[scan] + || window[++match] != window[++scan]) + { + continue; + } + + // scan is set to strstart+1 and the comparison passed, so + // scanMax - scan is the maximum number of bytes we can compare. + // below we compare 8 bytes at a time, so first we compare + // (scanMax - scan) % 8 bytes, so the remainder is a multiple of 8 + + switch ((scanMax - scan) % 8) + { + case 1: + if (window[++scan] == window[++match]) break; + break; + + case 2: + if (window[++scan] == window[++match] + && window[++scan] == window[++match]) break; + break; + + case 3: + if (window[++scan] == window[++match] + && window[++scan] == window[++match] + && window[++scan] == window[++match]) break; + break; + + case 4: + if (window[++scan] == window[++match] + && window[++scan] == window[++match] + && window[++scan] == window[++match] + && window[++scan] == window[++match]) break; + break; + + case 5: + if (window[++scan] == window[++match] + && window[++scan] == window[++match] + && window[++scan] == window[++match] + && window[++scan] == window[++match] + && window[++scan] == window[++match]) break; + break; + + case 6: + if (window[++scan] == window[++match] + && window[++scan] == window[++match] + && window[++scan] == window[++match] + && window[++scan] == window[++match] + && window[++scan] == window[++match] + && window[++scan] == window[++match]) break; + break; + + case 7: + if (window[++scan] == window[++match] + && window[++scan] == window[++match] + && window[++scan] == window[++match] + && window[++scan] == window[++match] + && window[++scan] == window[++match] + && window[++scan] == window[++match] + && window[++scan] == window[++match]) break; + break; + } + + if (window[scan] == window[match]) + { + /* We check for insufficient lookahead only every 8th comparison; + * the 256th check will be made at strstart + 258 unless lookahead is + * exhausted first. + */ + do + { + if (scan == scanMax) + { + ++scan; // advance to first position not matched + ++match; + + break; + } + } + while (window[++scan] == window[++match] + && window[++scan] == window[++match] + && window[++scan] == window[++match] + && window[++scan] == window[++match] + && window[++scan] == window[++match] + && window[++scan] == window[++match] + && window[++scan] == window[++match] + && window[++scan] == window[++match]); + } + + if (scan - strstart > matchLen) + { +#if DebugDeflation if (DeflaterConstants.DEBUGGING && (ins_h == 0) ) Console.Error.WriteLine("Found match: " + curMatch + "-" + (scan - strstart)); - #endif +#endif - matchStart = curMatch; - matchLen = scan - strstart; + matchStart = curMatch; + matchLen = scan - strstart; + + if (matchLen >= niceLength) + break; - if (matchLen >= niceLength) - break; - - scan_end1 = window[scan - 1]; - scan_end = window[scan]; - } - } while ((curMatch = (prev[curMatch & DeflaterConstants.WMASK] & 0xffff)) > limit && 0 != --chainLength ); + scan_end1 = window[scan - 1]; + scan_end = window[scan]; + } + } while ((curMatch = (prev[curMatch & DeflaterConstants.WMASK] & 0xffff)) > limit && 0 != --chainLength); - return matchLen >= DeflaterConstants.MIN_MATCH; - } + return matchLen >= DeflaterConstants.MIN_MATCH; + } - bool DeflateStored(bool flush, bool finish) + private bool DeflateStored(bool flush, bool finish) { - if (!flush && (lookahead == 0)) { + if (!flush && (lookahead == 0)) + { return false; } @@ -558,15 +625,17 @@ bool DeflateStored(bool flush, bool finish) if ((storedLength >= DeflaterConstants.MAX_BLOCK_SIZE) || // Block is full (blockStart < DeflaterConstants.WSIZE && storedLength >= DeflaterConstants.MAX_DIST) || // Block may move out of window - flush) { + flush) + { bool lastBlock = finish; - if (storedLength > DeflaterConstants.MAX_BLOCK_SIZE) { + if (storedLength > DeflaterConstants.MAX_BLOCK_SIZE) + { storedLength = DeflaterConstants.MAX_BLOCK_SIZE; lastBlock = false; } #if DebugDeflation - if (DeflaterConstants.DEBUGGING) + if (DeflaterConstants.DEBUGGING) { Console.WriteLine("storedBlock[" + storedLength + "," + lastBlock + "]"); } @@ -574,26 +643,30 @@ bool DeflateStored(bool flush, bool finish) huffman.FlushStoredBlock(window, blockStart, storedLength, lastBlock); blockStart += storedLength; - return !lastBlock; + return !(lastBlock || storedLength == 0); } return true; } - bool DeflateFast(bool flush, bool finish) + private bool DeflateFast(bool flush, bool finish) { - if (lookahead < DeflaterConstants.MIN_LOOKAHEAD && !flush) { + if (lookahead < DeflaterConstants.MIN_LOOKAHEAD && !flush) + { return false; } - while (lookahead >= DeflaterConstants.MIN_LOOKAHEAD || flush) { - if (lookahead == 0) { + while (lookahead >= DeflaterConstants.MIN_LOOKAHEAD || flush) + { + if (lookahead == 0) + { // We are flushing everything huffman.FlushBlock(window, blockStart, strstart - blockStart, finish); blockStart = strstart; return false; } - if (strstart > 2 * DeflaterConstants.WSIZE - DeflaterConstants.MIN_LOOKAHEAD) { + if (strstart > 2 * DeflaterConstants.WSIZE - DeflaterConstants.MIN_LOOKAHEAD) + { /* slide window, as FindLongestMatch needs this. * This should only happen when flushing and the window * is almost full. @@ -606,10 +679,11 @@ bool DeflateFast(bool flush, bool finish) (hashHead = InsertString()) != 0 && strategy != DeflateStrategy.HuffmanOnly && strstart - hashHead <= DeflaterConstants.MAX_DIST && - FindLongestMatch(hashHead)) { + FindLongestMatch(hashHead)) + { // longestMatch sets matchStart and matchLen #if DebugDeflation - if (DeflaterConstants.DEBUGGING) + if (DeflaterConstants.DEBUGGING) { for (int i = 0 ; i < matchLen; i++) { if (window[strstart + i] != window[matchStart + i]) { @@ -622,30 +696,39 @@ bool DeflateFast(bool flush, bool finish) bool full = huffman.TallyDist(strstart - matchStart, matchLen); lookahead -= matchLen; - if (matchLen <= max_lazy && lookahead >= DeflaterConstants.MIN_MATCH) { - while (--matchLen > 0) { + if (matchLen <= max_lazy && lookahead >= DeflaterConstants.MIN_MATCH) + { + while (--matchLen > 0) + { ++strstart; InsertString(); } ++strstart; - } else { + } + else + { strstart += matchLen; - if (lookahead >= DeflaterConstants.MIN_MATCH - 1) { + if (lookahead >= DeflaterConstants.MIN_MATCH - 1) + { UpdateHash(); } } matchLen = DeflaterConstants.MIN_MATCH - 1; - if (!full) { + if (!full) + { continue; } - } else { + } + else + { // No match found huffman.TallyLit(window[strstart] & 0xff); ++strstart; --lookahead; } - if (huffman.IsFull()) { + if (huffman.IsFull()) + { bool lastBlock = finish && (lookahead == 0); huffman.FlushBlock(window, blockStart, strstart - blockStart, lastBlock); blockStart = strstart; @@ -655,22 +738,26 @@ bool DeflateFast(bool flush, bool finish) return true; } - bool DeflateSlow(bool flush, bool finish) + private bool DeflateSlow(bool flush, bool finish) { - if (lookahead < DeflaterConstants.MIN_LOOKAHEAD && !flush) { + if (lookahead < DeflaterConstants.MIN_LOOKAHEAD && !flush) + { return false; } - while (lookahead >= DeflaterConstants.MIN_LOOKAHEAD || flush) { - if (lookahead == 0) { - if (prevAvailable) { + while (lookahead >= DeflaterConstants.MIN_LOOKAHEAD || flush) + { + if (lookahead == 0) + { + if (prevAvailable) + { huffman.TallyLit(window[strstart - 1] & 0xff); } prevAvailable = false; // We are flushing everything #if DebugDeflation - if (DeflaterConstants.DEBUGGING && !flush) + if (DeflaterConstants.DEBUGGING && !flush) { throw new SharpZipBaseException("Not flushing, but no lookahead"); } @@ -681,7 +768,8 @@ bool DeflateSlow(bool flush, bool finish) return false; } - if (strstart >= 2 * DeflaterConstants.WSIZE - DeflaterConstants.MIN_LOOKAHEAD) { + if (strstart >= 2 * DeflaterConstants.WSIZE - DeflaterConstants.MIN_LOOKAHEAD) + { /* slide window, as FindLongestMatch needs this. * This should only happen when flushing and the window * is almost full. @@ -691,28 +779,30 @@ bool DeflateSlow(bool flush, bool finish) int prevMatch = matchStart; int prevLen = matchLen; - if (lookahead >= DeflaterConstants.MIN_MATCH) { - + if (lookahead >= DeflaterConstants.MIN_MATCH) + { int hashHead = InsertString(); if (strategy != DeflateStrategy.HuffmanOnly && hashHead != 0 && strstart - hashHead <= DeflaterConstants.MAX_DIST && - FindLongestMatch(hashHead)) { - + FindLongestMatch(hashHead)) + { // longestMatch sets matchStart and matchLen // Discard match if too small and too far away - if (matchLen <= 5 && (strategy == DeflateStrategy.Filtered || (matchLen == DeflaterConstants.MIN_MATCH && strstart - matchStart > TooFar))) { + if (matchLen <= 5 && (strategy == DeflateStrategy.Filtered || (matchLen == DeflaterConstants.MIN_MATCH && strstart - matchStart > TooFar))) + { matchLen = DeflaterConstants.MIN_MATCH - 1; } } } // previous match was better - if ((prevLen >= DeflaterConstants.MIN_MATCH) && (matchLen <= prevLen)) { + if ((prevLen >= DeflaterConstants.MIN_MATCH) && (matchLen <= prevLen)) + { #if DebugDeflation - if (DeflaterConstants.DEBUGGING) + if (DeflaterConstants.DEBUGGING) { for (int i = 0 ; i < matchLen; i++) { if (window[strstart-1+i] != window[prevMatch + i]) @@ -722,10 +812,12 @@ bool DeflateSlow(bool flush, bool finish) #endif huffman.TallyDist(strstart - 1 - prevMatch, prevLen); prevLen -= 2; - do { + do + { strstart++; lookahead--; - if (lookahead >= DeflaterConstants.MIN_MATCH) { + if (lookahead >= DeflaterConstants.MIN_MATCH) + { InsertString(); } } while (--prevLen > 0); @@ -734,8 +826,11 @@ bool DeflateSlow(bool flush, bool finish) lookahead--; prevAvailable = false; matchLen = DeflaterConstants.MIN_MATCH - 1; - } else { - if (prevAvailable) { + } + else + { + if (prevAvailable) + { huffman.TallyLit(window[strstart - 1] & 0xff); } prevAvailable = true; @@ -743,9 +838,11 @@ bool DeflateSlow(bool flush, bool finish) lookahead--; } - if (huffman.IsFull()) { + if (huffman.IsFull()) + { int len = strstart - blockStart; - if (prevAvailable) { + if (prevAvailable) + { len--; } bool lastBlock = (finish && (lookahead == 0) && !prevAvailable); @@ -760,36 +857,39 @@ bool DeflateSlow(bool flush, bool finish) #region Instance Fields // Hash index of string to be inserted - int ins_h; + private int ins_h; /// /// Hashtable, hashing three characters to an index for window, so - /// that window[index]..window[index+2] have this hash code. + /// that window[index]..window[index+2] have this hash code. /// Note that the array should really be unsigned short, so you need /// to and the values with 0xffff. /// - short[] head; + private short[] head; /// /// prev[index & WMASK] points to the previous index that has the - /// same hash code as the string starting at index. This way + /// same hash code as the string starting at index. This way /// entries with the same hash code are in a linked list. /// Note that the array should really be unsigned short, so you need /// to and the values with 0xffff. /// - short[] prev; + private short[] prev; + + private int matchStart; - int matchStart; // Length of best match - int matchLen; + private int matchLen; + // Set if previous match exists - bool prevAvailable; - int blockStart; + private bool prevAvailable; + + private int blockStart; /// /// Points to the current character in the window. /// - int strstart; + private int strstart; /// /// lookahead is the number of characters starting at strstart in @@ -797,49 +897,50 @@ bool DeflateSlow(bool flush, bool finish) /// So window[strstart] until window[strstart+lookahead-1] are valid /// characters. /// - int lookahead; + private int lookahead; /// - /// This array contains the part of the uncompressed stream that + /// This array contains the part of the uncompressed stream that /// is of relevance. The current character is indexed by strstart. /// - byte[] window; + private byte[] window; - DeflateStrategy strategy; - int max_chain, max_lazy, niceLength, goodLength; + private DeflateStrategy strategy; + private int max_chain, max_lazy, niceLength, goodLength; /// /// The current compression function. /// - int compressionFunction; + private int compressionFunction; /// /// The input data for compression. /// - byte[] inputBuf; + private byte[] inputBuf; /// /// The total bytes of input read. /// - long totalIn; + private long totalIn; /// /// The offset into inputBuf, where input data starts. /// - int inputOff; + private int inputOff; /// /// The end offset of the input data. /// - int inputEnd; + private int inputEnd; - DeflaterPending pending; - DeflaterHuffman huffman; + private DeflaterPending pending; + private DeflaterHuffman huffman; /// /// The adler checksum /// - Adler32 adler; - #endregion + private Adler32 adler; + + #endregion Instance Fields } } diff --git a/src/ICSharpCode.SharpZipLib/Zip/Compression/DeflaterHuffman.cs b/src/ICSharpCode.SharpZipLib/Zip/Compression/DeflaterHuffman.cs index d26e79339..2f71366fc 100644 --- a/src/ICSharpCode.SharpZipLib/Zip/Compression/DeflaterHuffman.cs +++ b/src/ICSharpCode.SharpZipLib/Zip/Compression/DeflaterHuffman.cs @@ -4,36 +4,39 @@ namespace ICSharpCode.SharpZipLib.Zip.Compression { /// /// This is the DeflaterHuffman class. - /// + /// /// This class is not thread safe. This is inherent in the API, due /// to the split of Deflate and SetInput. - /// + /// /// author of the original java version : Jochen Hoenicke /// public class DeflaterHuffman { - const int BUFSIZE = 1 << (DeflaterConstants.DEFAULT_MEM_LEVEL + 6); - const int LITERAL_NUM = 286; + private const int BUFSIZE = 1 << (DeflaterConstants.DEFAULT_MEM_LEVEL + 6); + private const int LITERAL_NUM = 286; // Number of distance codes - const int DIST_NUM = 30; + private const int DIST_NUM = 30; + // Number of codes used to transfer bit lengths - const int BITLEN_NUM = 19; + private const int BITLEN_NUM = 19; // repeat previous bit length 3-6 times (2 bits of repeat count) - const int REP_3_6 = 16; + private const int REP_3_6 = 16; + // repeat a zero length 3-10 times (3 bits of repeat count) - const int REP_3_10 = 17; + private const int REP_3_10 = 17; + // repeat a zero length 11-138 times (7 bits of repeat count) - const int REP_11_138 = 18; + private const int REP_11_138 = 18; - const int EOF_SYMBOL = 256; + private const int EOF_SYMBOL = 256; // The lengths of the bit length codes are sent in order of decreasing // probability, to avoid transmitting the lengths for unused bit length codes. - static readonly int[] BL_ORDER = { 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 }; + private static readonly int[] BL_ORDER = { 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 }; - static readonly byte[] bit4Reverse = { + private static readonly byte[] bit4Reverse = { 0, 8, 4, @@ -52,14 +55,15 @@ public class DeflaterHuffman 15 }; - static short[] staticLCodes; - static byte[] staticLLength; - static short[] staticDCodes; - static byte[] staticDLength; + private static short[] staticLCodes; + private static byte[] staticLLength; + private static short[] staticDCodes; + private static byte[] staticDLength; - class Tree + private class Tree { #region Instance Fields + public short[] freqs; public byte[] length; @@ -68,13 +72,15 @@ class Tree public int numCodes; - short[] codes; - readonly int[] bl_counts; - readonly int maxLength; - DeflaterHuffman dh; - #endregion + private short[] codes; + private readonly int[] bl_counts; + private readonly int maxLength; + private DeflaterHuffman dh; + + #endregion Instance Fields #region Constructors + public Tree(DeflaterHuffman dh, int elems, int minCodes, int maxLength) { this.dh = dh; @@ -84,14 +90,15 @@ public Tree(DeflaterHuffman dh, int elems, int minCodes, int maxLength) bl_counts = new int[maxLength]; } - #endregion + #endregion Constructors /// /// Resets the internal state of the tree /// public void Reset() { - for (int i = 0; i < freqs.Length; i++) { + for (int i = 0; i < freqs.Length; i++) + { freqs[i] = 0; } codes = null; @@ -116,11 +123,13 @@ public void WriteSymbol(int code) public void CheckEmpty() { bool empty = true; - for (int i = 0; i < freqs.Length; i++) { + for (int i = 0; i < freqs.Length; i++) + { empty &= freqs[i] == 0; } - if (!empty) { + if (!empty) + { throw new SharpZipBaseException("!Empty"); } } @@ -151,7 +160,8 @@ public void BuildCodes() // //Console.WriteLine("buildCodes: "+freqs.Length); // } - for (int bits = 0; bits < maxLength; bits++) { + for (int bits = 0; bits < maxLength; bits++) + { nextCode[bits] = code; code += bl_counts[bits] << (15 - bits); @@ -162,15 +172,16 @@ public void BuildCodes() } #if DebugDeflation - if ( DeflaterConstants.DEBUGGING && (code != 65536) ) + if ( DeflaterConstants.DEBUGGING && (code != 65536) ) { throw new SharpZipBaseException("Inconsistent bl_counts!"); } #endif - for (int i = 0; i < numCodes; i++) { + for (int i = 0; i < numCodes; i++) + { int bits = length[i]; - if (bits > 0) { - + if (bits > 0) + { // if (DeflaterConstants.DEBUGGING) { // //Console.WriteLine("codes["+i+"] = rev(" + nextCode[bits-1]+"), // +bits); @@ -197,13 +208,16 @@ public void BuildTree() int[] heap = new int[numSymbols]; int heapLen = 0; int maxCode = 0; - for (int n = 0; n < numSymbols; n++) { + for (int n = 0; n < numSymbols; n++) + { int freq = freqs[n]; - if (freq != 0) { + if (freq != 0) + { // Insert n into heap int pos = heapLen++; int ppos; - while (pos > 0 && freqs[heap[ppos = (pos - 1) / 2]] > freq) { + while (pos > 0 && freqs[heap[ppos = (pos - 1) / 2]] > freq) + { heap[pos] = heap[ppos]; pos = ppos; } @@ -218,7 +232,8 @@ public void BuildTree() * literals to avoid this case. We don't care about order in * this case, both literals get a 1 bit code. */ - while (heapLen < 2) { + while (heapLen < 2) + { int node = maxCode < 2 ? ++maxCode : 0; heap[heapLen++] = node; } @@ -229,7 +244,8 @@ public void BuildTree() int[] childs = new int[4 * heapLen - 2]; int[] values = new int[2 * heapLen - 1]; int numNodes = numLeafs; - for (int i = 0; i < heapLen; i++) { + for (int i = 0; i < heapLen; i++) + { int node = heap[i]; childs[2 * i] = node; childs[2 * i + 1] = -1; @@ -240,7 +256,8 @@ public void BuildTree() /* Construct the Huffman tree by repeatedly combining the least two * frequent nodes. */ - do { + do + { int first = heap[0]; int last = heap[--heapLen]; @@ -248,8 +265,10 @@ public void BuildTree() int ppos = 0; int path = 1; - while (path < heapLen) { - if (path + 1 < heapLen && values[heap[path]] > values[heap[path + 1]]) { + while (path < heapLen) + { + if (path + 1 < heapLen && values[heap[path]] > values[heap[path + 1]]) + { path++; } @@ -262,12 +281,12 @@ public void BuildTree() * it shouldn't go too deep. */ int lastVal = values[last]; - while ((path = ppos) > 0 && values[heap[ppos = (path - 1) / 2]] > lastVal) { + while ((path = ppos) > 0 && values[heap[ppos = (path - 1) / 2]] > lastVal) + { heap[path] = heap[ppos]; } heap[path] = last; - int second = heap[0]; // Create a new node father of first and second @@ -281,8 +300,10 @@ public void BuildTree() ppos = 0; path = 1; - while (path < heapLen) { - if (path + 1 < heapLen && values[heap[path]] > values[heap[path + 1]]) { + while (path < heapLen) + { + if (path + 1 < heapLen && values[heap[path]] > values[heap[path + 1]]) + { path++; } @@ -292,13 +313,15 @@ public void BuildTree() } // Now propagate the new element down along path - while ((path = ppos) > 0 && values[heap[ppos = (path - 1) / 2]] > lastVal) { + while ((path = ppos) > 0 && values[heap[ppos = (path - 1) / 2]] > lastVal) + { heap[path] = heap[ppos]; } heap[path] = last; } while (heapLen > 1); - if (heap[0] != childs.Length / 2 - 1) { + if (heap[0] != childs.Length / 2 - 1) + { throw new SharpZipBaseException("Heap invariant violated"); } @@ -312,7 +335,8 @@ public void BuildTree() public int GetEncodedLength() { int len = 0; - for (int i = 0; i < freqs.Length; i++) { + for (int i = 0; i < freqs.Length; i++) + { len += freqs[i] * length[i]; } return len; @@ -330,16 +354,21 @@ public void CalcBLFreq(Tree blTree) int curlen = -1; /* length of current code */ int i = 0; - while (i < numCodes) { + while (i < numCodes) + { count = 1; int nextlen = length[i]; - if (nextlen == 0) { + if (nextlen == 0) + { max_count = 138; min_count = 3; - } else { + } + else + { max_count = 6; min_count = 3; - if (curlen != nextlen) { + if (curlen != nextlen) + { blTree.freqs[nextlen]++; count = 0; } @@ -347,20 +376,29 @@ public void CalcBLFreq(Tree blTree) curlen = nextlen; i++; - while (i < numCodes && curlen == length[i]) { + while (i < numCodes && curlen == length[i]) + { i++; - if (++count >= max_count) { + if (++count >= max_count) + { break; } } - if (count < min_count) { + if (count < min_count) + { blTree.freqs[curlen] += (short)count; - } else if (curlen != 0) { + } + else if (curlen != 0) + { blTree.freqs[REP_3_6]++; - } else if (count <= 10) { + } + else if (count <= 10) + { blTree.freqs[REP_3_10]++; - } else { + } + else + { blTree.freqs[REP_11_138]++; } } @@ -378,16 +416,21 @@ public void WriteTree(Tree blTree) int curlen = -1; // length of current code int i = 0; - while (i < numCodes) { + while (i < numCodes) + { count = 1; int nextlen = length[i]; - if (nextlen == 0) { + if (nextlen == 0) + { max_count = 138; min_count = 3; - } else { + } + else + { max_count = 6; min_count = 3; - if (curlen != nextlen) { + if (curlen != nextlen) + { blTree.WriteSymbol(nextlen); count = 0; } @@ -395,38 +438,49 @@ public void WriteTree(Tree blTree) curlen = nextlen; i++; - while (i < numCodes && curlen == length[i]) { + while (i < numCodes && curlen == length[i]) + { i++; - if (++count >= max_count) { + if (++count >= max_count) + { break; } } - if (count < min_count) { - while (count-- > 0) { + if (count < min_count) + { + while (count-- > 0) + { blTree.WriteSymbol(curlen); } - } else if (curlen != 0) { + } + else if (curlen != 0) + { blTree.WriteSymbol(REP_3_6); dh.pending.WriteBits(count - 3, 2); - } else if (count <= 10) { + } + else if (count <= 10) + { blTree.WriteSymbol(REP_3_10); dh.pending.WriteBits(count - 3, 3); - } else { + } + else + { blTree.WriteSymbol(REP_11_138); dh.pending.WriteBits(count - 11, 7); } } } - void BuildLength(int[] childs) + private void BuildLength(int[] childs) { this.length = new byte[freqs.Length]; int numNodes = childs.Length / 2; int numLeafs = (numNodes + 1) / 2; int overflow = 0; - for (int i = 0; i < maxLength; i++) { + for (int i = 0; i < maxLength; i++) + { bl_counts[i] = 0; } @@ -434,15 +488,20 @@ void BuildLength(int[] childs) int[] lengths = new int[numNodes]; lengths[numNodes - 1] = 0; - for (int i = numNodes - 1; i >= 0; i--) { - if (childs[2 * i + 1] != -1) { + for (int i = numNodes - 1; i >= 0; i--) + { + if (childs[2 * i + 1] != -1) + { int bitLength = lengths[i] + 1; - if (bitLength > maxLength) { + if (bitLength > maxLength) + { bitLength = maxLength; overflow++; } lengths[childs[2 * i]] = lengths[childs[2 * i + 1]] = bitLength; - } else { + } + else + { // A leaf node int bitLength = lengths[i]; bl_counts[bitLength - 1]++; @@ -458,19 +517,23 @@ void BuildLength(int[] childs) // } // } - if (overflow == 0) { + if (overflow == 0) + { return; } int incrBitLen = maxLength - 1; - do { + do + { // Find the first bit length which could increase: - while (bl_counts[--incrBitLen] == 0) { + while (bl_counts[--incrBitLen] == 0) + { } // Move this node one down and remove a corresponding // number of overflow nodes. - do { + do + { bl_counts[incrBitLen]--; bl_counts[++incrBitLen]++; overflow -= 1 << (maxLength - 1 - incrBitLen); @@ -492,11 +555,14 @@ void BuildLength(int[] childs) * array. */ int nodePtr = 2 * numLeafs; - for (int bits = maxLength; bits != 0; bits--) { + for (int bits = maxLength; bits != 0; bits--) + { int n = bl_counts[bits - 1]; - while (n > 0) { + while (n > 0) + { int childPtr = 2 * childs[nodePtr++]; - if (childs[childPtr + 1] == -1) { + if (childs[childPtr + 1] == -1) + { // We found another leaf length[childs[childPtr]] = (byte)bits; n--; @@ -511,25 +577,27 @@ void BuildLength(int[] childs) // } // } } - } #region Instance Fields + /// /// Pending buffer to use /// public DeflaterPending pending; - Tree literalTree; - Tree distTree; - Tree blTree; + private Tree literalTree; + private Tree distTree; + private Tree blTree; // Buffer for distances - short[] d_buf; - byte[] l_buf; - int last_lit; - int extra_bits; - #endregion + private short[] d_buf; + + private byte[] l_buf; + private int last_lit; + private int extra_bits; + + #endregion Instance Fields static DeflaterHuffman() { @@ -539,22 +607,26 @@ static DeflaterHuffman() staticLLength = new byte[LITERAL_NUM]; int i = 0; - while (i < 144) { + while (i < 144) + { staticLCodes[i] = BitReverse((0x030 + i) << 8); staticLLength[i++] = 8; } - while (i < 256) { + while (i < 256) + { staticLCodes[i] = BitReverse((0x190 - 144 + i) << 7); staticLLength[i++] = 9; } - while (i < 280) { + while (i < 280) + { staticLCodes[i] = BitReverse((0x000 - 256 + i) << 9); staticLLength[i++] = 7; } - while (i < LITERAL_NUM) { + while (i < LITERAL_NUM) + { staticLCodes[i] = BitReverse((0x0c0 - 280 + i) << 8); staticLLength[i++] = 8; } @@ -562,7 +634,8 @@ static DeflaterHuffman() // Distance codes staticDCodes = new short[DIST_NUM]; staticDLength = new byte[DIST_NUM]; - for (i = 0; i < DIST_NUM; i++) { + for (i = 0; i < DIST_NUM; i++) + { staticDCodes[i] = BitReverse(i << 11); staticDLength[i] = 5; } @@ -586,7 +659,7 @@ public DeflaterHuffman(DeflaterPending pending) /// /// Reset internal state - /// + /// public void Reset() { last_lit = 0; @@ -608,7 +681,8 @@ public void SendAllTrees(int blTreeCodes) pending.WriteBits(literalTree.numCodes - 257, 5); pending.WriteBits(distTree.numCodes - 1, 5); pending.WriteBits(blTreeCodes - 4, 4); - for (int rank = 0; rank < blTreeCodes; rank++) { + for (int rank = 0; rank < blTreeCodes; rank++) + { pending.WriteBits(blTree.length[BL_ORDER[rank]], 3); } literalTree.WriteTree(blTree); @@ -626,10 +700,12 @@ public void SendAllTrees(int blTreeCodes) /// public void CompressBlock() { - for (int i = 0; i < last_lit; i++) { + for (int i = 0; i < last_lit; i++) + { int litlen = l_buf[i] & 0xff; int dist = d_buf[i]; - if (dist-- != 0) { + if (dist-- != 0) + { // if (DeflaterConstants.DEBUGGING) { // Console.Write("["+(dist+1)+","+(litlen+3)+"]: "); // } @@ -638,7 +714,8 @@ public void CompressBlock() literalTree.WriteSymbol(lc); int bits = (lc - 261) / 4; - if (bits > 0 && bits <= 5) { + if (bits > 0 && bits <= 5) + { pending.WriteBits(litlen & ((1 << bits) - 1), bits); } @@ -646,10 +723,13 @@ public void CompressBlock() distTree.WriteSymbol(dc); bits = dc / 2 - 1; - if (bits > 0) { + if (bits > 0) + { pending.WriteBits(dist & ((1 << bits) - 1), bits); } - } else { + } + else + { // if (DeflaterConstants.DEBUGGING) { // if (litlen > 32 && litlen < 127) { // Console.Write("("+(char)litlen+"): "); @@ -700,7 +780,7 @@ public void FlushStoredBlock(byte[] stored, int storedOffset, int storedLength, /// /// Flush block to output with compression - /// + /// /// Data to flush /// Index of first byte to flush /// Count of bytes to flush @@ -721,8 +801,10 @@ public void FlushBlock(byte[] stored, int storedOffset, int storedLength, bool l blTree.BuildTree(); int blTreeCodes = 4; - for (int i = 18; i > blTreeCodes; i--) { - if (blTree.length[BL_ORDER[i]] > 0) { + for (int i = 18; i > blTreeCodes; i--) + { + if (blTree.length[BL_ORDER[i]] > 0) + { blTreeCodes = i + 1; } } @@ -731,18 +813,22 @@ public void FlushBlock(byte[] stored, int storedOffset, int storedLength, bool l extra_bits; int static_len = extra_bits; - for (int i = 0; i < LITERAL_NUM; i++) { + for (int i = 0; i < LITERAL_NUM; i++) + { static_len += literalTree.freqs[i] * staticLLength[i]; } - for (int i = 0; i < DIST_NUM; i++) { + for (int i = 0; i < DIST_NUM; i++) + { static_len += distTree.freqs[i] * staticDLength[i]; } - if (opt_len >= static_len) { + if (opt_len >= static_len) + { // Force static trees opt_len = static_len; } - if (storedOffset >= 0 && storedLength + 4 < opt_len >> 3) { + if (storedOffset >= 0 && storedLength + 4 < opt_len >> 3) + { // Store Block // if (DeflaterConstants.DEBUGGING) { @@ -750,14 +836,18 @@ public void FlushBlock(byte[] stored, int storedOffset, int storedLength, bool l // + " <= " + static_len); // } FlushStoredBlock(stored, storedOffset, storedLength, lastBlock); - } else if (opt_len == static_len) { + } + else if (opt_len == static_len) + { // Encode with static tree pending.WriteBits((DeflaterConstants.STATIC_TREES << 1) + (lastBlock ? 1 : 0), 3); literalTree.SetStaticCodes(staticLCodes, staticLLength); distTree.SetStaticCodes(staticDCodes, staticDLength); CompressBlock(); Reset(); - } else { + } + else + { // Encode with dynamic tree pending.WriteBits((DeflaterConstants.DYN_TREES << 1) + (lastBlock ? 1 : 0), 3); SendAllTrees(blTreeCodes); @@ -812,19 +902,20 @@ public bool TallyDist(int distance, int length) int lc = Lcode(length - 3); literalTree.freqs[lc]++; - if (lc >= 265 && lc < 285) { + if (lc >= 265 && lc < 285) + { extra_bits += (lc - 261) / 4; } int dc = Dcode(distance - 1); distTree.freqs[dc]++; - if (dc >= 4) { + if (dc >= 4) + { extra_bits += dc / 2 - 1; } return IsFull(); } - /// /// Reverse the bits of a 16 bit value. /// @@ -838,24 +929,27 @@ public static short BitReverse(int toReverse) bit4Reverse[toReverse >> 12]); } - static int Lcode(int length) + private static int Lcode(int length) { - if (length == 255) { + if (length == 255) + { return 285; } int code = 257; - while (length >= 8) { + while (length >= 8) + { code += 4; length >>= 1; } return code + length; } - static int Dcode(int distance) + private static int Dcode(int distance) { int code = 0; - while (distance >= 4) { + while (distance >= 4) + { code += 2; distance >>= 1; } diff --git a/src/ICSharpCode.SharpZipLib/Zip/Compression/DeflaterPending.cs b/src/ICSharpCode.SharpZipLib/Zip/Compression/DeflaterPending.cs index 2c6c22e1d..80d3e2142 100644 --- a/src/ICSharpCode.SharpZipLib/Zip/Compression/DeflaterPending.cs +++ b/src/ICSharpCode.SharpZipLib/Zip/Compression/DeflaterPending.cs @@ -2,7 +2,7 @@ namespace ICSharpCode.SharpZipLib.Zip.Compression { /// /// This class stores the pending output of the Deflater. - /// + /// /// author of the original java version : Jochen Hoenicke /// public class DeflaterPending : PendingBuffer diff --git a/src/ICSharpCode.SharpZipLib/Zip/Compression/Inflater.cs b/src/ICSharpCode.SharpZipLib/Zip/Compression/Inflater.cs index 8d6d7a0ef..5bf2a985e 100644 --- a/src/ICSharpCode.SharpZipLib/Zip/Compression/Inflater.cs +++ b/src/ICSharpCode.SharpZipLib/Zip/Compression/Inflater.cs @@ -1,13 +1,13 @@ -using System; using ICSharpCode.SharpZipLib.Checksum; using ICSharpCode.SharpZipLib.Zip.Compression.Streams; +using System; namespace ICSharpCode.SharpZipLib.Zip.Compression { /// /// Inflater is used to decompress data that has been compressed according /// to the "deflate" standard described in rfc1951. - /// + /// /// By default Zlib (rfc1950) headers and footers are expected in the input. /// You can use constructor public Inflater(bool noHeader) passing true /// if there is no Zlib header information @@ -32,10 +32,11 @@ namespace ICSharpCode.SharpZipLib.Zip.Compression public class Inflater { #region Constants/Readonly + /// /// Copy lengths for literal codes 257..285 /// - static readonly int[] CPLENS = { + private static readonly int[] CPLENS = { 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258 }; @@ -43,7 +44,7 @@ public class Inflater /// /// Extra bits for literal codes 257..285 /// - static readonly int[] CPLEXT = { + private static readonly int[] CPLEXT = { 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0 }; @@ -51,7 +52,7 @@ public class Inflater /// /// Copy offsets for distance codes 0..29 /// - static readonly int[] CPDIST = { + private static readonly int[] CPDIST = { 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, 8193, 12289, 16385, 24577 @@ -60,7 +61,7 @@ public class Inflater /// /// Extra bits for distance codes /// - static readonly int[] CPDEXT = { + private static readonly int[] CPDEXT = { 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13 @@ -69,78 +70,85 @@ public class Inflater /// /// These are the possible states for an inflater /// - const int DECODE_HEADER = 0; - const int DECODE_DICT = 1; - const int DECODE_BLOCKS = 2; - const int DECODE_STORED_LEN1 = 3; - const int DECODE_STORED_LEN2 = 4; - const int DECODE_STORED = 5; - const int DECODE_DYN_HEADER = 6; - const int DECODE_HUFFMAN = 7; - const int DECODE_HUFFMAN_LENBITS = 8; - const int DECODE_HUFFMAN_DIST = 9; - const int DECODE_HUFFMAN_DISTBITS = 10; - const int DECODE_CHKSUM = 11; - const int FINISHED = 12; - #endregion + private const int DECODE_HEADER = 0; + + private const int DECODE_DICT = 1; + private const int DECODE_BLOCKS = 2; + private const int DECODE_STORED_LEN1 = 3; + private const int DECODE_STORED_LEN2 = 4; + private const int DECODE_STORED = 5; + private const int DECODE_DYN_HEADER = 6; + private const int DECODE_HUFFMAN = 7; + private const int DECODE_HUFFMAN_LENBITS = 8; + private const int DECODE_HUFFMAN_DIST = 9; + private const int DECODE_HUFFMAN_DISTBITS = 10; + private const int DECODE_CHKSUM = 11; + private const int FINISHED = 12; + + #endregion Constants/Readonly #region Instance Fields + /// /// This variable contains the current state. /// - int mode; + private int mode; /// /// The adler checksum of the dictionary or of the decompressed /// stream, as it is written in the header resp. footer of the - /// compressed stream. + /// compressed stream. /// Only valid if mode is DECODE_DICT or DECODE_CHKSUM. /// - int readAdler; + private int readAdler; /// /// The number of bits needed to complete the current state. This /// is valid, if mode is DECODE_DICT, DECODE_CHKSUM, /// DECODE_HUFFMAN_LENBITS or DECODE_HUFFMAN_DISTBITS. /// - int neededBits; - int repLength; - int repDist; - int uncomprLen; + private int neededBits; + + private int repLength; + private int repDist; + private int uncomprLen; /// /// True, if the last block flag was set in the last block of the /// inflated stream. This means that the stream ends after the /// current block. /// - bool isLastBlock; + private bool isLastBlock; /// /// The total number of inflated bytes. /// - long totalOut; + private long totalOut; /// /// The total number of bytes set with setInput(). This is not the /// value returned by the TotalIn property, since this also includes the /// unprocessed input. /// - long totalIn; + private long totalIn; /// /// This variable stores the noHeader flag that was given to the constructor. - /// True means, that the inflated stream doesn't contain a Zlib header or + /// True means, that the inflated stream doesn't contain a Zlib header or /// footer. /// - bool noHeader; - readonly StreamManipulator input; - OutputWindow outputWindow; - InflaterDynHeader dynHeader; - InflaterHuffmanTree litlenTree, distTree; - Adler32 adler; - #endregion + internal bool noHeader; + + private readonly StreamManipulator input; + private OutputWindow outputWindow; + private InflaterDynHeader dynHeader; + private InflaterHuffmanTree litlenTree, distTree; + private Adler32 adler; + + #endregion Instance Fields #region Constructors + /// /// Creates a new inflater or RFC1951 decompressor /// RFC1950/Zlib headers and footers will be expected in the input data @@ -154,9 +162,9 @@ public Inflater() : this(false) /// /// /// True if no RFC1950/Zlib header and footer fields are expected in the input data - /// + /// /// This is used for GZIPed/Zipped input. - /// + /// /// For compatibility with /// Sun JDK you should provide one byte of input more than needed in /// this case. @@ -164,12 +172,14 @@ public Inflater() : this(false) public Inflater(bool noHeader) { this.noHeader = noHeader; - this.adler = new Adler32(); + if (!noHeader) + this.adler = new Adler32(); input = new StreamManipulator(); outputWindow = new OutputWindow(); mode = noHeader ? DECODE_BLOCKS : DECODE_HEADER; } - #endregion + + #endregion Constructors /// /// Resets the inflater so that a new stream can be decompressed. All @@ -186,7 +196,7 @@ public void Reset() litlenTree = null; distTree = null; isLastBlock = false; - adler.Reset(); + adler?.Reset(); } /// @@ -201,18 +211,21 @@ public void Reset() private bool DecodeHeader() { int header = input.PeekBits(16); - if (header < 0) { + if (header < 0) + { return false; } input.DropBits(16); // The header is written in "wrong" byte order header = ((header << 8) | (header >> 8)) & 0xffff; - if (header % 31 != 0) { + if (header % 31 != 0) + { throw new SharpZipBaseException("Header checksum illegal"); } - if ((header & 0x0f00) != (Deflater.DEFLATED << 8)) { + if ((header & 0x0f00) != (Deflater.DEFLATED << 8)) + { throw new SharpZipBaseException("Compression Method unknown"); } @@ -223,9 +236,12 @@ private bool DecodeHeader() int max_wbits = ((header & 0x7000) >> 12) + 8; */ - if ((header & 0x0020) == 0) { // Dictionary flag? + if ((header & 0x0020) == 0) + { // Dictionary flag? mode = DECODE_BLOCKS; - } else { + } + else + { mode = DECODE_DICT; neededBits = 32; } @@ -240,9 +256,11 @@ private bool DecodeHeader() /// private bool DecodeDict() { - while (neededBits > 0) { + while (neededBits > 0) + { int dictByte = input.PeekBits(8); - if (dictByte < 0) { + if (dictByte < 0) + { return false; } input.DropBits(8); @@ -265,22 +283,30 @@ private bool DecodeDict() private bool DecodeHuffman() { int free = outputWindow.GetFreeSpace(); - while (free >= 258) { + while (free >= 258) + { int symbol; - switch (mode) { + switch (mode) + { case DECODE_HUFFMAN: // This is the inner loop so it is optimized a bit - while (((symbol = litlenTree.GetSymbol(input)) & ~0xff) == 0) { + while (((symbol = litlenTree.GetSymbol(input)) & ~0xff) == 0) + { outputWindow.Write(symbol); - if (--free < 258) { + if (--free < 258) + { return true; } } - if (symbol < 257) { - if (symbol < 0) { + if (symbol < 257) + { + if (symbol < 0) + { return false; - } else { + } + else + { // symbol == 256: end of block distTree = null; litlenTree = null; @@ -289,19 +315,24 @@ private bool DecodeHuffman() } } - try { + try + { repLength = CPLENS[symbol - 257]; neededBits = CPLEXT[symbol - 257]; - } catch (Exception) { + } + catch (Exception) + { throw new SharpZipBaseException("Illegal rep length code"); } goto case DECODE_HUFFMAN_LENBITS; // fall through case DECODE_HUFFMAN_LENBITS: - if (neededBits > 0) { + if (neededBits > 0) + { mode = DECODE_HUFFMAN_LENBITS; int i = input.PeekBits(neededBits); - if (i < 0) { + if (i < 0) + { return false; } input.DropBits(neededBits); @@ -312,24 +343,30 @@ private bool DecodeHuffman() case DECODE_HUFFMAN_DIST: symbol = distTree.GetSymbol(input); - if (symbol < 0) { + if (symbol < 0) + { return false; } - try { + try + { repDist = CPDIST[symbol]; neededBits = CPDEXT[symbol]; - } catch (Exception) { + } + catch (Exception) + { throw new SharpZipBaseException("Illegal rep dist code"); } goto case DECODE_HUFFMAN_DISTBITS; // fall through case DECODE_HUFFMAN_DISTBITS: - if (neededBits > 0) { + if (neededBits > 0) + { mode = DECODE_HUFFMAN_DISTBITS; int i = input.PeekBits(neededBits); - if (i < 0) { + if (i < 0) + { return false; } input.DropBits(neededBits); @@ -359,9 +396,11 @@ private bool DecodeHuffman() /// private bool DecodeChksum() { - while (neededBits > 0) { + while (neededBits > 0) + { int chkByte = input.PeekBits(8); - if (chkByte < 0) { + if (chkByte < 0) + { return false; } input.DropBits(8); @@ -369,8 +408,9 @@ private bool DecodeChksum() neededBits -= 8; } - if ((int)adler.Value != readAdler) { - throw new SharpZipBaseException("Adler chksum doesn't match: " + (int)adler.Value + " vs. " + readAdler); + if ((int)adler?.Value != readAdler) + { + throw new SharpZipBaseException("Adler chksum doesn't match: " + (int)adler?.Value + " vs. " + readAdler); } mode = FINISHED; @@ -388,7 +428,8 @@ private bool DecodeChksum() /// private bool Decode() { - switch (mode) { + switch (mode) + { case DECODE_HEADER: return DecodeHeader(); @@ -399,11 +440,15 @@ private bool Decode() return DecodeChksum(); case DECODE_BLOCKS: - if (isLastBlock) { - if (noHeader) { + if (isLastBlock) + { + if (noHeader) + { mode = FINISHED; return false; - } else { + } + else + { input.SkipToByteBoundary(); neededBits = 32; mode = DECODE_CHKSUM; @@ -412,33 +457,40 @@ private bool Decode() } int type = input.PeekBits(3); - if (type < 0) { + if (type < 0) + { return false; } input.DropBits(3); isLastBlock |= (type & 1) != 0; - switch (type >> 1) { + switch (type >> 1) + { case DeflaterConstants.STORED_BLOCK: input.SkipToByteBoundary(); mode = DECODE_STORED_LEN1; break; + case DeflaterConstants.STATIC_TREES: litlenTree = InflaterHuffmanTree.defLitLenTree; distTree = InflaterHuffmanTree.defDistTree; mode = DECODE_HUFFMAN; break; + case DeflaterConstants.DYN_TREES: - dynHeader = new InflaterDynHeader(); + dynHeader = new InflaterDynHeader(input); mode = DECODE_DYN_HEADER; break; + default: throw new SharpZipBaseException("Unknown block type " + type); } return true; - case DECODE_STORED_LEN1: { - if ((uncomprLen = input.PeekBits(16)) < 0) { + case DECODE_STORED_LEN1: + { + if ((uncomprLen = input.PeekBits(16)) < 0) + { return false; } input.DropBits(16); @@ -446,23 +498,28 @@ private bool Decode() } goto case DECODE_STORED_LEN2; // fall through - case DECODE_STORED_LEN2: { + case DECODE_STORED_LEN2: + { int nlen = input.PeekBits(16); - if (nlen < 0) { + if (nlen < 0) + { return false; } input.DropBits(16); - if (nlen != (uncomprLen ^ 0xffff)) { + if (nlen != (uncomprLen ^ 0xffff)) + { throw new SharpZipBaseException("broken uncompressed block"); } mode = DECODE_STORED; } goto case DECODE_STORED; // fall through - case DECODE_STORED: { + case DECODE_STORED: + { int more = outputWindow.CopyStored(input, uncomprLen); uncomprLen -= more; - if (uncomprLen == 0) { + if (uncomprLen == 0) + { mode = DECODE_BLOCKS; return true; } @@ -470,12 +527,13 @@ private bool Decode() } case DECODE_DYN_HEADER: - if (!dynHeader.Decode(input)) { + if (!dynHeader.AttemptRead()) + { return false; } - litlenTree = dynHeader.BuildLitLenTree(); - distTree = dynHeader.BuildDistTree(); + litlenTree = dynHeader.LiteralLengthTree; + distTree = dynHeader.DistanceTree; mode = DECODE_HUFFMAN; goto case DECODE_HUFFMAN; // fall through @@ -530,28 +588,33 @@ public void SetDictionary(byte[] buffer) /// public void SetDictionary(byte[] buffer, int index, int count) { - if (buffer == null) { + if (buffer == null) + { throw new ArgumentNullException(nameof(buffer)); } - if (index < 0) { + if (index < 0) + { throw new ArgumentOutOfRangeException(nameof(index)); } - if (count < 0) { + if (count < 0) + { throw new ArgumentOutOfRangeException(nameof(count)); } - if (!IsNeedingDictionary) { + if (!IsNeedingDictionary) + { throw new InvalidOperationException("Dictionary is not needed"); } - adler.Update(buffer, index, count); + adler?.Update(new ArraySegment(buffer, index, count)); - if ((int)adler.Value != readAdler) { + if (adler != null && (int)adler.Value != readAdler) + { throw new SharpZipBaseException("Wrong adler checksum"); } - adler.Reset(); + adler?.Reset(); outputWindow.CopyDict(buffer, index, count); mode = DECODE_BLOCKS; } @@ -614,7 +677,8 @@ public void SetInput(byte[] buffer, int index, int count) /// public int Inflate(byte[] buffer) { - if (buffer == null) { + if (buffer == null) + { throw new ArgumentNullException(nameof(buffer)); } @@ -650,25 +714,31 @@ public int Inflate(byte[] buffer) /// public int Inflate(byte[] buffer, int offset, int count) { - if (buffer == null) { + if (buffer == null) + { throw new ArgumentNullException(nameof(buffer)); } - if (count < 0) { + if (count < 0) + { throw new ArgumentOutOfRangeException(nameof(count), "count cannot be negative"); } - if (offset < 0) { + if (offset < 0) + { throw new ArgumentOutOfRangeException(nameof(offset), "offset cannot be negative"); } - if (offset + count > buffer.Length) { + if (offset + count > buffer.Length) + { throw new ArgumentException("count exceeds buffer bounds"); } // Special case: count may be zero - if (count == 0) { - if (!IsFinished) { // -jr- 08-Nov-2003 INFLATE_BUG fix.. + if (count == 0) + { + if (!IsFinished) + { // -jr- 08-Nov-2003 INFLATE_BUG fix.. Decode(); } return 0; @@ -676,8 +746,10 @@ public int Inflate(byte[] buffer, int offset, int count) int bytesCopied = 0; - do { - if (mode != DECODE_CHKSUM) { + do + { + if (mode != DECODE_CHKSUM) + { /* Don't give away any output, if we are waiting for the * checksum in the input stream. * @@ -686,13 +758,15 @@ public int Inflate(byte[] buffer, int offset, int count) * implies more output can be produced. */ int more = outputWindow.CopyOutput(buffer, offset, count); - if (more > 0) { - adler.Update(buffer, offset, more); + if (more > 0) + { + adler?.Update(new ArraySegment(buffer, offset, more)); offset += more; bytesCopied += more; totalOut += (long)more; count -= more; - if (count == 0) { + if (count == 0) + { return bytesCopied; } } @@ -703,11 +777,13 @@ public int Inflate(byte[] buffer, int offset, int count) /// /// Returns true, if the input buffer is empty. - /// You should then call setInput(). + /// You should then call setInput(). /// NOTE: This method also returns true when the stream is finished. /// - public bool IsNeedingInput { - get { + public bool IsNeedingInput + { + get + { return input.IsNeedingInput; } } @@ -715,8 +791,10 @@ public bool IsNeedingInput { /// /// Returns true, if a preset dictionary is needed to inflate the input. /// - public bool IsNeedingDictionary { - get { + public bool IsNeedingDictionary + { + get + { return mode == DECODE_DICT && neededBits == 0; } } @@ -725,8 +803,10 @@ public bool IsNeedingDictionary { /// Returns true, if the inflater has finished. This means, that no /// input is needed and no output can be produced. /// - public bool IsFinished { - get { + public bool IsFinished + { + get + { return mode == FINISHED && outputWindow.GetAvailable() == 0; } } @@ -740,9 +820,22 @@ public bool IsFinished { /// /// the adler checksum. /// - public int Adler { - get { - return IsNeedingDictionary ? readAdler : (int)adler.Value; + public int Adler + { + get + { + if (IsNeedingDictionary) + { + return readAdler; + } + else if (adler != null) + { + return (int)adler.Value; + } + else + { + return 0; + } } } @@ -752,8 +845,10 @@ public int Adler { /// /// the total number of output bytes. /// - public long TotalOut { - get { + public long TotalOut + { + get + { return totalOut; } } @@ -764,8 +859,10 @@ public long TotalOut { /// /// The total number of bytes of processed input bytes. /// - public long TotalIn { - get { + public long TotalIn + { + get + { return totalIn - (long)RemainingInput; } } @@ -778,9 +875,11 @@ public long TotalIn { /// /// The number of bytes of the input which have not been processed. /// - public int RemainingInput { + public int RemainingInput + { // TODO: This should be a long? - get { + get + { return input.AvailableBytes; } } diff --git a/src/ICSharpCode.SharpZipLib/Zip/Compression/InflaterDynHeader.cs b/src/ICSharpCode.SharpZipLib/Zip/Compression/InflaterDynHeader.cs index 31a366da6..8e0196b11 100644 --- a/src/ICSharpCode.SharpZipLib/Zip/Compression/InflaterDynHeader.cs +++ b/src/ICSharpCode.SharpZipLib/Zip/Compression/InflaterDynHeader.cs @@ -1,170 +1,151 @@ -using System; using ICSharpCode.SharpZipLib.Zip.Compression.Streams; +using System; +using System.Collections.Generic; namespace ICSharpCode.SharpZipLib.Zip.Compression { - class InflaterDynHeader + internal class InflaterDynHeader { #region Constants - const int LNUM = 0; - const int DNUM = 1; - const int BLNUM = 2; - const int BLLENS = 3; - const int LENS = 4; - const int REPS = 5; - static readonly int[] repMin = { 3, 3, 11 }; - static readonly int[] repBits = { 2, 3, 7 }; + // maximum number of literal/length codes + private const int LITLEN_MAX = 286; - static readonly int[] BL_ORDER = - { 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 }; - #endregion + // maximum number of distance codes + private const int DIST_MAX = 30; - public bool Decode(StreamManipulator input) - { - decode_loop: - for (;;) { - switch (mode) { - case LNUM: - lnum = input.PeekBits(5); - if (lnum < 0) { - return false; - } - lnum += 257; - input.DropBits(5); - // System.err.println("LNUM: "+lnum); - mode = DNUM; - goto case DNUM; // fall through - case DNUM: - dnum = input.PeekBits(5); - if (dnum < 0) { - return false; - } - dnum++; - input.DropBits(5); - // System.err.println("DNUM: "+dnum); - num = lnum + dnum; - litdistLens = new byte[num]; - mode = BLNUM; - goto case BLNUM; // fall through - case BLNUM: - blnum = input.PeekBits(4); - if (blnum < 0) { - return false; - } - blnum += 4; - input.DropBits(4); - blLens = new byte[19]; - ptr = 0; - // System.err.println("BLNUM: "+blnum); - mode = BLLENS; - goto case BLLENS; // fall through - case BLLENS: - while (ptr < blnum) { - int len = input.PeekBits(3); - if (len < 0) { - return false; - } - input.DropBits(3); - // System.err.println("blLens["+BL_ORDER[ptr]+"]: "+len); - blLens[BL_ORDER[ptr]] = (byte)len; - ptr++; - } - blTree = new InflaterHuffmanTree(blLens); - blLens = null; - ptr = 0; - mode = LENS; - goto case LENS; // fall through - case LENS: { - int symbol; - while (((symbol = blTree.GetSymbol(input)) & ~15) == 0) { - /* Normal case: symbol in [0..15] */ - - // System.err.println("litdistLens["+ptr+"]: "+symbol); - litdistLens[ptr++] = lastLen = (byte)symbol; - - if (ptr == num) { - /* Finished */ - return true; - } - } - - /* need more input ? */ - if (symbol < 0) { - return false; - } - - /* otherwise repeat code */ - if (symbol >= 17) { - /* repeat zero */ - // System.err.println("repeating zero"); - lastLen = 0; - } else { - if (ptr == 0) { - throw new SharpZipBaseException(); - } - } - repSymbol = symbol - 16; - } - mode = REPS; - goto case REPS; // fall through - case REPS: { - int bits = repBits[repSymbol]; - int count = input.PeekBits(bits); - if (count < 0) { - return false; - } - input.DropBits(bits); - count += repMin[repSymbol]; - // System.err.println("litdistLens repeated: "+count); - - if (ptr + count > num) { - throw new SharpZipBaseException(); - } - while (count-- > 0) { - litdistLens[ptr++] = lastLen; - } - - if (ptr == num) { - /* Finished */ - return true; - } - } - mode = LENS; - goto decode_loop; - } - } - } + // maximum data code lengths to read + private const int CODELEN_MAX = LITLEN_MAX + DIST_MAX; + + // maximum meta code length codes to read + private const int META_MAX = 19; + + private static readonly int[] MetaCodeLengthIndex = + { 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 }; + + #endregion Constants - public InflaterHuffmanTree BuildLitLenTree() + /// + /// Continue decoding header from until more bits are needed or decoding has been completed + /// + /// Returns whether decoding could be completed + public bool AttemptRead() + => !state.MoveNext() || state.Current; + + public InflaterDynHeader(StreamManipulator input) { - byte[] litlenLens = new byte[lnum]; - Array.Copy(litdistLens, 0, litlenLens, 0, lnum); - return new InflaterHuffmanTree(litlenLens); + this.input = input; + stateMachine = CreateStateMachine(); + state = stateMachine.GetEnumerator(); } - public InflaterHuffmanTree BuildDistTree() + private IEnumerable CreateStateMachine() { - byte[] distLens = new byte[dnum]; - Array.Copy(litdistLens, lnum, distLens, 0, dnum); - return new InflaterHuffmanTree(distLens); - } + // Read initial code length counts from header + while (!input.TryGetBits(5, ref litLenCodeCount, 257)) yield return false; + while (!input.TryGetBits(5, ref distanceCodeCount, 1)) yield return false; + while (!input.TryGetBits(4, ref metaCodeCount, 4)) yield return false; + var dataCodeCount = litLenCodeCount + distanceCodeCount; + + if (litLenCodeCount > LITLEN_MAX) throw new ValueOutOfRangeException(nameof(litLenCodeCount)); + if (distanceCodeCount > DIST_MAX) throw new ValueOutOfRangeException(nameof(distanceCodeCount)); + if (metaCodeCount > META_MAX) throw new ValueOutOfRangeException(nameof(metaCodeCount)); + + // Load code lengths for the meta tree from the header bits + for (int i = 0; i < metaCodeCount; i++) + { + while (!input.TryGetBits(3, ref codeLengths, MetaCodeLengthIndex[i])) yield return false; + } - #region Instance Fields - byte[] blLens; - byte[] litdistLens; + var metaCodeTree = new InflaterHuffmanTree(codeLengths); - InflaterHuffmanTree blTree; + // Decompress the meta tree symbols into the data table code lengths + int index = 0; + while (index < dataCodeCount) + { + byte codeLength; + int symbol; + + while ((symbol = metaCodeTree.GetSymbol(input)) < 0) yield return false; + + if (symbol < 16) + { + // append literal code length + codeLengths[index++] = (byte)symbol; + } + else + { + int repeatCount = 0; + + if (symbol == 16) // Repeat last code length 3..6 times + { + if (index == 0) + throw new StreamDecodingException("Cannot repeat previous code length when no other code length has been read"); + + codeLength = codeLengths[index - 1]; + + // 2 bits + 3, [3..6] + while (!input.TryGetBits(2, ref repeatCount, 3)) yield return false; + } + else if (symbol == 17) // Repeat zero 3..10 times + { + codeLength = 0; + + // 3 bits + 3, [3..10] + while (!input.TryGetBits(3, ref repeatCount, 3)) yield return false; + } + else // (symbol == 18), Repeat zero 11..138 times + { + codeLength = 0; + + // 7 bits + 11, [11..138] + while (!input.TryGetBits(7, ref repeatCount, 11)) yield return false; + } + + if (index + repeatCount > dataCodeCount) + throw new StreamDecodingException("Cannot repeat code lengths past total number of data code lengths"); + + while (repeatCount-- > 0) + codeLengths[index++] = codeLength; + } + } + + if (codeLengths[256] == 0) + throw new StreamDecodingException("Inflater dynamic header end-of-block code missing"); + + litLenTree = new InflaterHuffmanTree(new ArraySegment(codeLengths, 0, litLenCodeCount)); + distTree = new InflaterHuffmanTree(new ArraySegment(codeLengths, litLenCodeCount, distanceCodeCount)); + + yield return true; + } + + /// + /// Get literal/length huffman tree, must not be used before has returned true + /// + /// If hader has not been successfully read by the state machine + public InflaterHuffmanTree LiteralLengthTree + => litLenTree ?? throw new StreamDecodingException("Header properties were accessed before header had been successfully read"); /// - /// The current decode mode + /// Get distance huffman tree, must not be used before has returned true /// - int mode; - int lnum, dnum, blnum, num; - int repSymbol; - byte lastLen; - int ptr; - #endregion + /// If hader has not been successfully read by the state machine + public InflaterHuffmanTree DistanceTree + => distTree ?? throw new StreamDecodingException("Header properties were accessed before header had been successfully read"); + + #region Instance Fields + + private readonly StreamManipulator input; + private readonly IEnumerator state; + private readonly IEnumerable stateMachine; + + private byte[] codeLengths = new byte[CODELEN_MAX]; + + private InflaterHuffmanTree litLenTree; + private InflaterHuffmanTree distTree; + + private int litLenCodeCount, distanceCodeCount, metaCodeCount; + #endregion Instance Fields } } diff --git a/src/ICSharpCode.SharpZipLib/Zip/Compression/InflaterHuffmanTree.cs b/src/ICSharpCode.SharpZipLib/Zip/Compression/InflaterHuffmanTree.cs index 152a4029f..ed318824f 100644 --- a/src/ICSharpCode.SharpZipLib/Zip/Compression/InflaterHuffmanTree.cs +++ b/src/ICSharpCode.SharpZipLib/Zip/Compression/InflaterHuffmanTree.cs @@ -1,5 +1,6 @@ -using System; using ICSharpCode.SharpZipLib.Zip.Compression.Streams; +using System; +using System.Collections.Generic; namespace ICSharpCode.SharpZipLib.Zip.Compression { @@ -9,12 +10,16 @@ namespace ICSharpCode.SharpZipLib.Zip.Compression public class InflaterHuffmanTree { #region Constants - const int MAX_BITLEN = 15; - #endregion + + private const int MAX_BITLEN = 15; + + #endregion Constants #region Instance Fields - short[] tree; - #endregion + + private short[] tree; + + #endregion Instance Fields /// /// Literal length tree @@ -28,65 +33,79 @@ public class InflaterHuffmanTree static InflaterHuffmanTree() { - try { + try + { byte[] codeLengths = new byte[288]; int i = 0; - while (i < 144) { + while (i < 144) + { codeLengths[i++] = 8; } - while (i < 256) { + while (i < 256) + { codeLengths[i++] = 9; } - while (i < 280) { + while (i < 280) + { codeLengths[i++] = 7; } - while (i < 288) { + while (i < 288) + { codeLengths[i++] = 8; } defLitLenTree = new InflaterHuffmanTree(codeLengths); codeLengths = new byte[32]; i = 0; - while (i < 32) { + while (i < 32) + { codeLengths[i++] = 5; } defDistTree = new InflaterHuffmanTree(codeLengths); - } catch (Exception) { + } + catch (Exception) + { throw new SharpZipBaseException("InflaterHuffmanTree: static tree length illegal"); } } #region Constructors + /// /// Constructs a Huffman tree from the array of code lengths. /// /// /// the array of code lengths /// - public InflaterHuffmanTree(byte[] codeLengths) + public InflaterHuffmanTree(IList codeLengths) { BuildTree(codeLengths); } - #endregion - void BuildTree(byte[] codeLengths) + #endregion Constructors + + private void BuildTree(IList codeLengths) { int[] blCount = new int[MAX_BITLEN + 1]; int[] nextCode = new int[MAX_BITLEN + 1]; - for (int i = 0; i < codeLengths.Length; i++) { + for (int i = 0; i < codeLengths.Count; i++) + { int bits = codeLengths[i]; - if (bits > 0) { + if (bits > 0) + { blCount[bits]++; } } int code = 0; int treeSize = 512; - for (int bits = 1; bits <= MAX_BITLEN; bits++) { + for (int bits = 1; bits <= MAX_BITLEN; bits++) + { nextCode[bits] = code; code += blCount[bits] << (16 - bits); - if (bits >= 10) { + if (bits >= 10) + { /* We need an extra table for bit lengths >= 10. */ int start = nextCode[bits] & 0x1ff80; int end = code & 0x1ff80; @@ -95,7 +114,7 @@ void BuildTree(byte[] codeLengths) } /* -jr comment this out! doesnt work for dynamic trees and pkzip 2.04g - if (code != 65536) + if (code != 65536) { throw new SharpZipBaseException("Code lengths don't add up properly."); } @@ -105,40 +124,48 @@ void BuildTree(byte[] codeLengths) */ tree = new short[treeSize]; int treePtr = 512; - for (int bits = MAX_BITLEN; bits >= 10; bits--) { + for (int bits = MAX_BITLEN; bits >= 10; bits--) + { int end = code & 0x1ff80; code -= blCount[bits] << (16 - bits); int start = code & 0x1ff80; - for (int i = start; i < end; i += 1 << 7) { + for (int i = start; i < end; i += 1 << 7) + { tree[DeflaterHuffman.BitReverse(i)] = (short)((-treePtr << 4) | bits); treePtr += 1 << (bits - 9); } } - for (int i = 0; i < codeLengths.Length; i++) { + for (int i = 0; i < codeLengths.Count; i++) + { int bits = codeLengths[i]; - if (bits == 0) { + if (bits == 0) + { continue; } code = nextCode[bits]; int revcode = DeflaterHuffman.BitReverse(code); - if (bits <= 9) { - do { + if (bits <= 9) + { + do + { tree[revcode] = (short)((i << 4) | bits); revcode += 1 << bits; } while (revcode < 512); - } else { + } + else + { int subTree = tree[revcode & 511]; int treeLen = 1 << (subTree & 15); subTree = -(subTree >> 4); - do { + do + { tree[subTree | (revcode >> 9)] = (short)((i << 4) | bits); revcode += 1 << bits; } while (revcode < treeLen); } nextCode[bits] = code + (1 << (16 - bits)); } - } /// @@ -154,40 +181,57 @@ void BuildTree(byte[] codeLengths) public int GetSymbol(StreamManipulator input) { int lookahead, symbol; - if ((lookahead = input.PeekBits(9)) >= 0) { - if ((symbol = tree[lookahead]) >= 0) { - input.DropBits(symbol & 15); + if ((lookahead = input.PeekBits(9)) >= 0) + { + symbol = tree[lookahead]; + int bitlen = symbol & 15; + + if (symbol >= 0) + { + if(bitlen == 0){ + throw new SharpZipBaseException("Encountered invalid codelength 0"); + } + input.DropBits(bitlen); return symbol >> 4; } int subtree = -(symbol >> 4); - int bitlen = symbol & 15; - if ((lookahead = input.PeekBits(bitlen)) >= 0) { + if ((lookahead = input.PeekBits(bitlen)) >= 0) + { symbol = tree[subtree | (lookahead >> 9)]; input.DropBits(symbol & 15); return symbol >> 4; - } else { + } + else + { int bits = input.AvailableBits; lookahead = input.PeekBits(bits); symbol = tree[subtree | (lookahead >> 9)]; - if ((symbol & 15) <= bits) { + if ((symbol & 15) <= bits) + { input.DropBits(symbol & 15); return symbol >> 4; - } else { + } + else + { return -1; } } - } else { + } + else // Less than 9 bits + { int bits = input.AvailableBits; lookahead = input.PeekBits(bits); symbol = tree[lookahead]; - if (symbol >= 0 && (symbol & 15) <= bits) { + if (symbol >= 0 && (symbol & 15) <= bits) + { input.DropBits(symbol & 15); return symbol >> 4; - } else { + } + else + { return -1; } } } } } - diff --git a/src/ICSharpCode.SharpZipLib/Zip/Compression/PendingBuffer.cs b/src/ICSharpCode.SharpZipLib/Zip/Compression/PendingBuffer.cs index fd6cbf0c8..6ed7e4ab8 100644 --- a/src/ICSharpCode.SharpZipLib/Zip/Compression/PendingBuffer.cs +++ b/src/ICSharpCode.SharpZipLib/Zip/Compression/PendingBuffer.cs @@ -1,31 +1,32 @@ -using System; - namespace ICSharpCode.SharpZipLib.Zip.Compression { /// /// This class is general purpose class for writing data to a buffer. - /// + /// /// It allows you to write bits as well as bytes /// Based on DeflaterPending.java - /// + /// /// author of the original java version : Jochen Hoenicke /// public class PendingBuffer { #region Instance Fields + /// /// Internal work buffer /// - readonly byte[] buffer; + private readonly byte[] buffer; + + private int start; + private int end; - int start; - int end; + private uint bits; + private int bitCount; - uint bits; - int bitCount; - #endregion + #endregion Instance Fields #region Constructors + /// /// construct instance using default buffer size of 4096 /// @@ -44,7 +45,7 @@ public PendingBuffer(int bufferSize) buffer = new byte[bufferSize]; } - #endregion + #endregion Constructors /// /// Clear internal state/buffers @@ -116,7 +117,7 @@ public void WriteInt(int value) public void WriteBlock(byte[] block, int offset, int length) { #if DebugDeflation - if (DeflaterConstants.DEBUGGING && (start != 0) ) + if (DeflaterConstants.DEBUGGING && (start != 0) ) { throw new SharpZipBaseException("Debug check: start != 0"); } @@ -128,8 +129,10 @@ public void WriteBlock(byte[] block, int offset, int length) /// /// The number of bits written to the buffer /// - public int BitCount { - get { + public int BitCount + { + get + { return bitCount; } } @@ -140,14 +143,16 @@ public int BitCount { public void AlignToByte() { #if DebugDeflation - if (DeflaterConstants.DEBUGGING && (start != 0) ) + if (DeflaterConstants.DEBUGGING && (start != 0) ) { throw new SharpZipBaseException("Debug check: start != 0"); } #endif - if (bitCount > 0) { + if (bitCount > 0) + { buffer[end++] = unchecked((byte)bits); - if (bitCount > 8) { + if (bitCount > 8) + { buffer[end++] = unchecked((byte)(bits >> 8)); } } @@ -163,7 +168,7 @@ public void AlignToByte() public void WriteBits(int b, int count) { #if DebugDeflation - if (DeflaterConstants.DEBUGGING && (start != 0) ) + if (DeflaterConstants.DEBUGGING && (start != 0) ) { throw new SharpZipBaseException("Debug check: start != 0"); } @@ -174,7 +179,8 @@ public void WriteBits(int b, int count) #endif bits |= (uint)(b << bitCount); bitCount += count; - if (bitCount >= 16) { + if (bitCount >= 16) + { buffer[end++] = unchecked((byte)bits); buffer[end++] = unchecked((byte)(bits >> 8)); bits >>= 16; @@ -189,7 +195,7 @@ public void WriteBits(int b, int count) public void WriteShortMSB(int s) { #if DebugDeflation - if (DeflaterConstants.DEBUGGING && (start != 0) ) + if (DeflaterConstants.DEBUGGING && (start != 0) ) { throw new SharpZipBaseException("Debug check: start != 0"); } @@ -201,8 +207,10 @@ public void WriteShortMSB(int s) /// /// Indicates if buffer has been flushed /// - public bool IsFlushed { - get { + public bool IsFlushed + { + get + { return end == 0; } } @@ -217,18 +225,22 @@ public bool IsFlushed { /// The number of bytes flushed. public int Flush(byte[] output, int offset, int length) { - if (bitCount >= 8) { + if (bitCount >= 8) + { buffer[end++] = unchecked((byte)bits); bits >>= 8; bitCount -= 8; } - if (length > end - start) { + if (length > end - start) + { length = end - start; System.Array.Copy(buffer, start, output, offset, length); start = 0; end = 0; - } else { + } + else + { System.Array.Copy(buffer, start, output, offset, length); start += length; } @@ -245,7 +257,7 @@ public int Flush(byte[] output, int offset, int length) public byte[] ToByteArray() { AlignToByte(); - + byte[] result = new byte[end - start]; System.Array.Copy(buffer, start, result, 0, result.Length); start = 0; diff --git a/src/ICSharpCode.SharpZipLib/Zip/Compression/PooledInflater.cs b/src/ICSharpCode.SharpZipLib/Zip/Compression/PooledInflater.cs new file mode 100644 index 000000000..0828de3ef --- /dev/null +++ b/src/ICSharpCode.SharpZipLib/Zip/Compression/PooledInflater.cs @@ -0,0 +1,14 @@ +using ICSharpCode.SharpZipLib.Core; + +namespace ICSharpCode.SharpZipLib.Zip.Compression +{ + /// + /// A marker type for pooled version of an inflator that we can return back to . + /// + internal sealed class PooledInflater : Inflater + { + public PooledInflater(bool noHeader) : base(noHeader) + { + } + } +} diff --git a/src/ICSharpCode.SharpZipLib/Zip/Compression/Streams/DeflaterOutputStream.cs b/src/ICSharpCode.SharpZipLib/Zip/Compression/Streams/DeflaterOutputStream.cs index ad49c3f63..a7e6807ca 100644 --- a/src/ICSharpCode.SharpZipLib/Zip/Compression/Streams/DeflaterOutputStream.cs +++ b/src/ICSharpCode.SharpZipLib/Zip/Compression/Streams/DeflaterOutputStream.cs @@ -1,18 +1,22 @@ +using ICSharpCode.SharpZipLib.Encryption; using System; using System.IO; using System.Security.Cryptography; -using ICSharpCode.SharpZipLib.Encryption; +using System.Text; +using System.Threading; +using System.Threading.Tasks; namespace ICSharpCode.SharpZipLib.Zip.Compression.Streams { /// /// A special stream deflating or compressing the bytes that are /// written to it. It uses a Deflater to perform actual deflating.
- /// Authors of the original java version : Tom Tromey, Jochen Hoenicke + /// Authors of the original java version : Tom Tromey, Jochen Hoenicke ///
public class DeflaterOutputStream : Stream { #region Constructors + /// /// Creates a new DeflaterOutputStream with a default Deflater and default buffer size. /// @@ -63,31 +67,32 @@ public DeflaterOutputStream(Stream baseOutputStream, Deflater deflater) /// public DeflaterOutputStream(Stream baseOutputStream, Deflater deflater, int bufferSize) { - if (baseOutputStream == null) { + if (baseOutputStream == null) + { throw new ArgumentNullException(nameof(baseOutputStream)); } - if (baseOutputStream.CanWrite == false) { + if (baseOutputStream.CanWrite == false) + { throw new ArgumentException("Must support writing", nameof(baseOutputStream)); } - if (deflater == null) { - throw new ArgumentNullException(nameof(deflater)); - } - - if (bufferSize < 512) { + if (bufferSize < 512) + { throw new ArgumentOutOfRangeException(nameof(bufferSize)); } baseOutputStream_ = baseOutputStream; buffer_ = new byte[bufferSize]; - deflater_ = deflater; + deflater_ = deflater ?? throw new ArgumentNullException(nameof(deflater)); } - #endregion + + #endregion Constructors #region Public API + /// - /// Finishes the stream by calling finish() on the deflater. + /// Finishes the stream by calling finish() on the deflater. /// /// /// Not all input is deflated @@ -95,27 +100,71 @@ public DeflaterOutputStream(Stream baseOutputStream, Deflater deflater, int buff public virtual void Finish() { deflater_.Finish(); - while (!deflater_.IsFinished) { + while (!deflater_.IsFinished) + { int len = deflater_.Deflate(buffer_, 0, buffer_.Length); - if (len <= 0) { + if (len <= 0) + { break; } - if (cryptoTransform_ != null) { - EncryptBlock(buffer_, 0, len); - } + EncryptBlock(buffer_, 0, len); baseOutputStream_.Write(buffer_, 0, len); } - if (!deflater_.IsFinished) { + if (!deflater_.IsFinished) + { throw new SharpZipBaseException("Can't deflate all input?"); } baseOutputStream_.Flush(); - if (cryptoTransform_ != null) { - if (cryptoTransform_ is ZipAESTransform) { + if (cryptoTransform_ != null) + { + if (cryptoTransform_ is ZipAESTransform) + { + AESAuthCode = ((ZipAESTransform)cryptoTransform_).GetAuthCode(); + } + cryptoTransform_.Dispose(); + cryptoTransform_ = null; + } + } + + /// + /// Finishes the stream by calling finish() on the deflater. + /// + /// The that can be used to cancel the operation. + /// + /// Not all input is deflated + /// + public virtual async Task FinishAsync(CancellationToken ct) + { + deflater_.Finish(); + while (!deflater_.IsFinished) + { + int len = deflater_.Deflate(buffer_, 0, buffer_.Length); + if (len <= 0) + { + break; + } + + EncryptBlock(buffer_, 0, len); + + await baseOutputStream_.WriteAsync(buffer_, 0, len, ct).ConfigureAwait(false); + } + + if (!deflater_.IsFinished) + { + throw new SharpZipBaseException("Can't deflate all input?"); + } + + await baseOutputStream_.FlushAsync(ct).ConfigureAwait(false); + + if (cryptoTransform_ != null) + { + if (cryptoTransform_ is ZipAESTransform) + { AESAuthCode = ((ZipAESTransform)cryptoTransform_).GetAuthCode(); } cryptoTransform_.Dispose(); @@ -133,40 +182,34 @@ public virtual void Finish() /// /// Allows client to determine if an entry can be patched after its added /// - public bool CanPatchEntries { - get { + public bool CanPatchEntries + { + get + { return baseOutputStream_.CanSeek; } } - #endregion + #endregion Public API #region Encryption - string password; - - ICryptoTransform cryptoTransform_; + /// + /// The CryptoTransform currently being used to encrypt the compressed data. + /// + protected ICryptoTransform cryptoTransform_; /// /// Returns the 10 byte AUTH CODE to be appended immediately following the AES data stream. /// protected byte[] AESAuthCode; - /// - /// Get/set the password used for encryption. - /// - /// When set to null or if the password is empty no encryption is performed - public string Password { - get { - return password; - } + /// + public Encoding ZipCryptoEncoding { + get => _stringCodec.ZipCryptoEncoding; set { - if ((value != null) && (value.Length == 0)) { - password = null; - } else { - password = value; - } - } + _stringCodec = _stringCodec.WithZipCryptoEncoding(value); + } } /// @@ -183,72 +226,62 @@ public string Password { /// protected void EncryptBlock(byte[] buffer, int offset, int length) { + if(cryptoTransform_ is null) return; cryptoTransform_.TransformBlock(buffer, 0, length, buffer, 0); } - /// - /// Initializes encryption keys based on given . - /// - /// The password. - protected void InitializePassword(string password) - { - var pkManaged = new PkzipClassicManaged(); - byte[] key = PkzipClassic.GenerateKeys(ZipConstants.ConvertToArray(password)); - cryptoTransform_ = pkManaged.CreateEncryptor(key, null); - } - - /// - /// Initializes encryption keys based on given password. - /// - protected void InitializeAESPassword(ZipEntry entry, string rawPassword, - out byte[] salt, out byte[] pwdVerifier) - { - salt = new byte[entry.AESSaltLen]; - // Salt needs to be cryptographically random, and unique per file - if (_aesRnd == null) - _aesRnd = RandomNumberGenerator.Create(); - _aesRnd.GetBytes(salt); - int blockSize = entry.AESKeySize / 8; // bits to bytes - - cryptoTransform_ = new ZipAESTransform(rawPassword, salt, blockSize, true); - pwdVerifier = ((ZipAESTransform)cryptoTransform_).PwdVerifier; - } - - #endregion + #endregion Encryption #region Deflation Support + /// /// Deflates everything in the input buffers. This will call /// def.deflate() until all bytes from the input buffers /// are processed. /// protected void Deflate() + => DeflateSyncOrAsync(false, null).GetAwaiter().GetResult(); + + private async Task DeflateSyncOrAsync(bool flushing, CancellationToken? ct) { - while (!deflater_.IsNeedingInput) { + while (flushing || !deflater_.IsNeedingInput) + { int deflateCount = deflater_.Deflate(buffer_, 0, buffer_.Length); - if (deflateCount <= 0) { + if (deflateCount <= 0) + { break; } - if (cryptoTransform_ != null) { - EncryptBlock(buffer_, 0, deflateCount); - } - baseOutputStream_.Write(buffer_, 0, deflateCount); + EncryptBlock(buffer_, 0, deflateCount); + + if (ct.HasValue) + { + await baseOutputStream_.WriteAsync(buffer_, 0, deflateCount, ct.Value).ConfigureAwait(false); + } + else + { + baseOutputStream_.Write(buffer_, 0, deflateCount); + } } - if (!deflater_.IsNeedingInput) { + if (!deflater_.IsNeedingInput) + { throw new SharpZipBaseException("DeflaterOutputStream can't deflate all input?"); } } - #endregion + + #endregion Deflation Support #region Stream Overrides + /// /// Gets value indicating stream can be read from /// - public override bool CanRead { - get { + public override bool CanRead + { + get + { return false; } } @@ -257,8 +290,10 @@ public override bool CanRead { /// Gets a value indicating if seeking is supported for this stream /// This property always returns false /// - public override bool CanSeek { - get { + public override bool CanSeek + { + get + { return false; } } @@ -266,8 +301,10 @@ public override bool CanSeek { /// /// Get value indicating if this stream supports writing /// - public override bool CanWrite { - get { + public override bool CanWrite + { + get + { return baseOutputStream_.CanWrite; } } @@ -275,8 +312,10 @@ public override bool CanWrite { /// /// Get current length of stream /// - public override long Length { - get { + public override long Length + { + get + { return baseOutputStream_.Length; } } @@ -285,11 +324,14 @@ public override long Length { /// Gets the current position within the stream. ///
/// Any attempt to set position - public override long Position { - get { + public override long Position + { + get + { return baseOutputStream_.Position; } - set { + set + { throw new NotSupportedException("Position property not supported"); } } @@ -340,43 +382,98 @@ public override int Read(byte[] buffer, int offset, int count) } /// - /// Flushes the stream by calling Flush on the deflater and then + /// Flushes the stream by calling Flush on the deflater and then /// on the underlying stream. This ensures that all bytes are flushed. /// public override void Flush() { deflater_.Flush(); - Deflate(); + DeflateSyncOrAsync(true, null).GetAwaiter().GetResult(); baseOutputStream_.Flush(); } + /// + /// Asynchronously clears all buffers for this stream, causes any buffered data to be written to the underlying device, and monitors cancellation requests. + /// + /// + /// The token to monitor for cancellation requests. The default value is . + /// + public override async Task FlushAsync(CancellationToken cancellationToken) + { + deflater_.Flush(); + await DeflateSyncOrAsync(true, cancellationToken).ConfigureAwait(false); + await baseOutputStream_.FlushAsync(cancellationToken).ConfigureAwait(false); + } + /// /// Calls and closes the underlying /// stream when is true. /// protected override void Dispose(bool disposing) { - if (!isClosed_) { + if (!isClosed_) + { isClosed_ = true; - try { + try + { Finish(); - if (cryptoTransform_ != null) { + if (cryptoTransform_ != null) + { GetAuthCodeIfAES(); cryptoTransform_.Dispose(); cryptoTransform_ = null; } - } finally { - if (IsStreamOwner) { + } + finally + { + if (IsStreamOwner) + { baseOutputStream_.Dispose(); } } } } - private void GetAuthCodeIfAES() +#if NETSTANDARD2_1 || NETCOREAPP3_0_OR_GREATER + /// + /// Calls and closes the underlying + /// stream when is true. + /// + public override async ValueTask DisposeAsync() { - if (cryptoTransform_ is ZipAESTransform) { + if (!isClosed_) + { + isClosed_ = true; + + try + { + await FinishAsync(CancellationToken.None).ConfigureAwait(false); + if (cryptoTransform_ != null) + { + GetAuthCodeIfAES(); + cryptoTransform_.Dispose(); + cryptoTransform_ = null; + } + } + finally + { + if (IsStreamOwner) + { + await baseOutputStream_.DisposeAsync().ConfigureAwait(false); + } + } + } + } +#endif + + /// + /// Get the Auth code for AES encrypted entries + /// + protected void GetAuthCodeIfAES() + { + if (cryptoTransform_ is ZipAESTransform) + { AESAuthCode = ((ZipAESTransform)cryptoTransform_).GetAuthCode(); } } @@ -411,14 +508,37 @@ public override void Write(byte[] buffer, int offset, int count) deflater_.SetInput(buffer, offset, count); Deflate(); } - #endregion + + /// + /// Asynchronously writes a sequence of bytes to the current stream, advances the current position within this stream by the number of bytes written, and monitors cancellation requests. + /// + /// + /// The byte array + /// + /// + /// The offset into the byte array where to start. + /// + /// + /// The number of bytes to write. + /// + /// + /// The token to monitor for cancellation requests. The default value is . + /// + public override async Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken ct) + { + deflater_.SetInput(buffer, offset, count); + await DeflateSyncOrAsync(false, ct).ConfigureAwait(false); + } + + #endregion Stream Overrides #region Instance Fields + /// /// This buffer is used temporarily to retrieve the bytes from the /// deflater and write them to the underlying output stream. /// - byte[] buffer_; + private byte[] buffer_; /// /// The deflater which is used to deflate the stream. @@ -430,13 +550,11 @@ public override void Write(byte[] buffer, int offset, int count) /// protected Stream baseOutputStream_; - bool isClosed_; - #endregion + private bool isClosed_; - #region Static Fields + /// + protected StringCodec _stringCodec = ZipStrings.GetStringCodec(); - // Static to help ensure that multiple files within a zip will get different random salt - private static RandomNumberGenerator _aesRnd = RandomNumberGenerator.Create(); - #endregion + #endregion Instance Fields } } diff --git a/src/ICSharpCode.SharpZipLib/Zip/Compression/Streams/InflaterInputStream.cs b/src/ICSharpCode.SharpZipLib/Zip/Compression/Streams/InflaterInputStream.cs index 4485a4ee7..980ffc701 100644 --- a/src/ICSharpCode.SharpZipLib/Zip/Compression/Streams/InflaterInputStream.cs +++ b/src/ICSharpCode.SharpZipLib/Zip/Compression/Streams/InflaterInputStream.cs @@ -1,6 +1,7 @@ using System; using System.IO; using System.Security.Cryptography; +using ICSharpCode.SharpZipLib.Core; namespace ICSharpCode.SharpZipLib.Zip.Compression.Streams { @@ -13,6 +14,7 @@ namespace ICSharpCode.SharpZipLib.Zip.Compression.Streams public class InflaterInputBuffer { #region Constructors + /// /// Initialise a new instance of with a default buffer size /// @@ -30,19 +32,23 @@ public InflaterInputBuffer(Stream stream) : this(stream, 4096) public InflaterInputBuffer(Stream stream, int bufferSize) { inputStream = stream; - if (bufferSize < 1024) { + if (bufferSize < 1024) + { bufferSize = 1024; } rawData = new byte[bufferSize]; clearText = rawData; } - #endregion + + #endregion Constructors /// - /// Get the length of bytes bytes in the + /// Get the length of bytes in the /// - public int RawLength { - get { + public int RawLength + { + get + { return rawLength; } } @@ -51,8 +57,10 @@ public int RawLength { /// Get the contents of the raw data buffer. ///
/// This may contain encrypted data. - public byte[] RawData { - get { + public byte[] RawData + { + get + { return rawData; } } @@ -60,8 +68,10 @@ public byte[] RawData { /// /// Get the number of useable bytes in /// - public int ClearTextLength { - get { + public int ClearTextLength + { + get + { return clearTextLength; } } @@ -69,8 +79,10 @@ public int ClearTextLength { /// /// Get the contents of the clear text buffer. /// - public byte[] ClearText { - get { + public byte[] ClearText + { + get + { return clearText; } } @@ -78,7 +90,8 @@ public byte[] ClearText { /// /// Get/set the number of bytes available /// - public int Available { + public int Available + { get { return available; } set { available = value; } } @@ -89,7 +102,8 @@ public int Available { /// The inflater to set input for. public void SetInflaterInput(Inflater inflater) { - if (available > 0) { + if (available > 0) + { inflater.SetInput(clearText, clearTextLength - available, available); available = 0; } @@ -103,18 +117,23 @@ public void Fill() rawLength = 0; int toRead = rawData.Length; - while (toRead > 0) { + while (toRead > 0 && inputStream.CanRead) + { int count = inputStream.Read(rawData, rawLength, toRead); - if (count <= 0) { + if (count <= 0) + { break; } rawLength += count; toRead -= count; } - if (cryptoTransform != null) { + if (cryptoTransform != null) + { clearTextLength = cryptoTransform.TransformBlock(rawData, 0, rawLength, clearText, 0); - } else { + } + else + { clearTextLength = rawLength; } @@ -140,17 +159,21 @@ public int ReadRawBuffer(byte[] buffer) /// Returns the number of bytes read. public int ReadRawBuffer(byte[] outBuffer, int offset, int length) { - if (length < 0) { + if (length < 0) + { throw new ArgumentOutOfRangeException(nameof(length)); } int currentOffset = offset; int currentLength = length; - while (currentLength > 0) { - if (available <= 0) { + while (currentLength > 0) + { + if (available <= 0) + { Fill(); - if (available <= 0) { + if (available <= 0) + { return 0; } } @@ -172,17 +195,21 @@ public int ReadRawBuffer(byte[] outBuffer, int offset, int length) /// Returns the number of bytes actually read. public int ReadClearTextBuffer(byte[] outBuffer, int offset, int length) { - if (length < 0) { + if (length < 0) + { throw new ArgumentOutOfRangeException(nameof(length)); } int currentOffset = offset; int currentLength = length; - while (currentLength > 0) { - if (available <= 0) { + while (currentLength > 0) + { + if (available <= 0) + { Fill(); - if (available <= 0) { + if (available <= 0) + { return 0; } } @@ -200,11 +227,13 @@ public int ReadClearTextBuffer(byte[] outBuffer, int offset, int length) /// Read a from the input stream. ///
/// Returns the byte read. - public int ReadLeByte() + public byte ReadLeByte() { - if (available <= 0) { + if (available <= 0) + { Fill(); - if (available <= 0) { + if (available <= 0) + { throw new ZipException("EOF in header"); } } @@ -244,21 +273,29 @@ public long ReadLeLong() /// Get/set the to apply to any data. ///
/// Set this value to null to have no transform applied. - public ICryptoTransform CryptoTransform { - set { + public ICryptoTransform CryptoTransform + { + set + { cryptoTransform = value; - if (cryptoTransform != null) { - if (rawData == clearText) { - if (internalClearText == null) { + if (cryptoTransform != null) + { + if (rawData == clearText) + { + if (internalClearText == null) + { internalClearText = new byte[rawData.Length]; } clearText = internalClearText; } clearTextLength = rawLength; - if (available > 0) { + if (available > 0) + { cryptoTransform.TransformBlock(rawData, rawLength - available, available, clearText, rawLength - available); } - } else { + } + else + { clearText = rawData; clearTextLength = rawLength; } @@ -266,18 +303,20 @@ public ICryptoTransform CryptoTransform { } #region Instance Fields - int rawLength; - byte[] rawData; - int clearTextLength; - byte[] clearText; - byte[] internalClearText; + private int rawLength; + private byte[] rawData; + + private int clearTextLength; + private byte[] clearText; + private byte[] internalClearText; - int available; + private int available; - ICryptoTransform cryptoTransform; - Stream inputStream; - #endregion + private ICryptoTransform cryptoTransform; + private Stream inputStream; + + #endregion Instance Fields } /// @@ -292,6 +331,7 @@ public ICryptoTransform CryptoTransform { public class InflaterInputStream : Stream { #region Constructors + /// /// Create an InflaterInputStream with the default decompressor /// and a default buffer size of 4KB. @@ -300,7 +340,7 @@ public class InflaterInputStream : Stream /// The InputStream to read bytes from /// public InflaterInputStream(Stream baseInputStream) - : this(baseInputStream, new Inflater(), 4096) + : this(baseInputStream, InflaterPool.Instance.Rent(), 4096) { } @@ -334,15 +374,18 @@ public InflaterInputStream(Stream baseInputStream, Inflater inf) /// public InflaterInputStream(Stream baseInputStream, Inflater inflater, int bufferSize) { - if (baseInputStream == null) { + if (baseInputStream == null) + { throw new ArgumentNullException(nameof(baseInputStream)); } - if (inflater == null) { + if (inflater == null) + { throw new ArgumentNullException(nameof(inflater)); } - if (bufferSize <= 0) { + if (bufferSize <= 0) + { throw new ArgumentOutOfRangeException(nameof(bufferSize)); } @@ -352,7 +395,7 @@ public InflaterInputStream(Stream baseInputStream, Inflater inflater, int buffer inputBuffer = new InflaterInputBuffer(baseInputStream, bufferSize); } - #endregion + #endregion Constructors /// /// Gets or sets a flag indicating ownership of underlying stream. @@ -368,7 +411,7 @@ public InflaterInputStream(Stream baseInputStream, Inflater inflater, int buffer /// Number of bytes to skip /// /// - /// The number of bytes skipped, zero if the end of + /// The number of bytes skipped, zero if the end of /// stream has been reached /// /// @@ -376,17 +419,22 @@ public InflaterInputStream(Stream baseInputStream, Inflater inflater, int buffer /// public long Skip(long count) { - if (count <= 0) { + if (count <= 0) + { throw new ArgumentOutOfRangeException(nameof(count)); } // v0.80 Skip by seeking if underlying stream supports it... - if (baseInputStream.CanSeek) { + if (baseInputStream.CanSeek) + { baseInputStream.Seek(count, SeekOrigin.Current); return count; - } else { + } + else + { int length = 2048; - if (count < length) { + if (count < length) + { length = (int)count; } @@ -394,8 +442,10 @@ public long Skip(long count) int readCount = 1; long toSkip = count; - while ((toSkip > 0) && (readCount > 0)) { - if (toSkip < length) { + while ((toSkip > 0) && (readCount > 0)) + { + if (toSkip < length) + { length = (int)toSkip; } @@ -409,7 +459,7 @@ public long Skip(long count) /// /// Clear any cryptographic state. - /// + /// protected void StopDecrypting() { inputBuffer.CryptoTransform = null; @@ -419,8 +469,10 @@ protected void StopDecrypting() /// Returns 0 once the end of the stream (EOF) has been reached. /// Otherwise returns 1. /// - public virtual int Available { - get { + public virtual int Available + { + get + { return inf.IsFinished ? 0 : 1; } } @@ -434,9 +486,11 @@ public virtual int Available { protected void Fill() { // Protect against redundant calls - if (inputBuffer.Available <= 0) { + if (inputBuffer.Available <= 0) + { inputBuffer.Fill(); - if (inputBuffer.Available <= 0) { + if (inputBuffer.Available <= 0) + { throw new SharpZipBaseException("Unexpected EOF"); } } @@ -444,11 +498,14 @@ protected void Fill() } #region Stream Overrides + /// /// Gets a value indicating whether the current stream supports reading /// - public override bool CanRead { - get { + public override bool CanRead + { + get + { return baseInputStream.CanRead; } } @@ -456,8 +513,10 @@ public override bool CanRead { /// /// Gets a value of false indicating seeking is not supported for this stream. /// - public override bool CanSeek { - get { + public override bool CanSeek + { + get + { return false; } } @@ -465,8 +524,10 @@ public override bool CanSeek { /// /// Gets a value of false indicating that this stream is not writeable. /// - public override bool CanWrite { - get { + public override bool CanWrite + { + get + { return false; } } @@ -474,10 +535,12 @@ public override bool CanWrite { /// /// A value representing the length of the stream in bytes. /// - public override long Length { - get { + public override long Length + { + get + { //return inputBuffer.RawLength; - throw new NotSupportedException("InflaterInputStream Length is not supported"); + throw new NotSupportedException("InflaterInputStream Length is not supported"); } } @@ -486,11 +549,14 @@ public override long Length { /// Throws a NotSupportedException when attempting to set the position /// /// Attempting to set the position - public override long Position { - get { + public override long Position + { + get + { return baseInputStream.Position; } - set { + set + { throw new NotSupportedException("InflaterInputStream Position not supported"); } } @@ -531,7 +597,7 @@ public override void SetLength(long value) /// Writes a sequence of bytes to stream and advances the current position /// This method always throws a NotSupportedException ///
- /// Thew buffer containing data to write. + /// The buffer containing data to write. /// The offset of the first byte to write. /// The number of bytes to write. /// Any access @@ -557,12 +623,20 @@ public override void WriteByte(byte value) /// protected override void Dispose(bool disposing) { - if (!isClosed) { + if (!isClosed) + { isClosed = true; - if (IsStreamOwner) { + if (IsStreamOwner) + { baseInputStream.Dispose(); } } + + if (inf is PooledInflater inflater) + { + InflaterPool.Instance.Return(inflater); + } + inf = null; } /// @@ -583,31 +657,39 @@ protected override void Dispose(bool disposing) /// public override int Read(byte[] buffer, int offset, int count) { - if (inf.IsNeedingDictionary) { + if (inf.IsNeedingDictionary) + { throw new SharpZipBaseException("Need a dictionary"); } int remainingBytes = count; - while (true) { + while (true) + { int bytesRead = inf.Inflate(buffer, offset, remainingBytes); offset += bytesRead; remainingBytes -= bytesRead; - if (remainingBytes == 0 || inf.IsFinished) { + if (remainingBytes == 0 || inf.IsFinished) + { break; } - if (inf.IsNeedingInput) { + if (inf.IsNeedingInput) + { Fill(); - } else if (bytesRead == 0) { - throw new ZipException("Dont know what to do"); + } + else if (bytesRead == 0) + { + throw new ZipException("Invalid input data"); } } return count - remainingBytes; } - #endregion + + #endregion Stream Overrides #region Instance Fields + /// /// Decompressor for this stream /// @@ -629,9 +711,10 @@ public override int Read(byte[] buffer, int offset, int count) protected long csize; /// - /// Flag indicating wether this instance has been closed or not. + /// Flag indicating whether this instance has been closed or not. /// - bool isClosed; - #endregion + private bool isClosed; + + #endregion Instance Fields } } diff --git a/src/ICSharpCode.SharpZipLib/Zip/Compression/Streams/OutputWindow.cs b/src/ICSharpCode.SharpZipLib/Zip/Compression/Streams/OutputWindow.cs index cd31784c0..d8241c18c 100644 --- a/src/ICSharpCode.SharpZipLib/Zip/Compression/Streams/OutputWindow.cs +++ b/src/ICSharpCode.SharpZipLib/Zip/Compression/Streams/OutputWindow.cs @@ -11,15 +11,19 @@ namespace ICSharpCode.SharpZipLib.Zip.Compression.Streams public class OutputWindow { #region Constants - const int WindowSize = 1 << 15; - const int WindowMask = WindowSize - 1; - #endregion + + private const int WindowSize = 1 << 15; + private const int WindowMask = WindowSize - 1; + + #endregion Constants #region Instance Fields - byte[] window = new byte[WindowSize]; //The window is 2^15 bytes - int windowEnd; - int windowFilled; - #endregion + + private byte[] window = new byte[WindowSize]; //The window is 2^15 bytes + private int windowEnd; + private int windowFilled; + + #endregion Instance Fields /// /// Write a byte to this output window @@ -30,17 +34,18 @@ public class OutputWindow /// public void Write(int value) { - if (windowFilled++ == WindowSize) { + if (windowFilled++ == WindowSize) + { throw new InvalidOperationException("Window full"); } window[windowEnd++] = (byte)value; windowEnd &= WindowMask; } - private void SlowRepeat(int repStart, int length, int distance) { - while (length-- > 0) { + while (length-- > 0) + { window[windowEnd++] = window[repStart++]; windowEnd &= WindowMask; repStart &= WindowMask; @@ -57,23 +62,31 @@ private void SlowRepeat(int repStart, int length, int distance) /// public void Repeat(int length, int distance) { - if ((windowFilled += length) > WindowSize) { + if ((windowFilled += length) > WindowSize) + { throw new InvalidOperationException("Window full"); } int repStart = (windowEnd - distance) & WindowMask; int border = WindowSize - length; - if ((repStart <= border) && (windowEnd < border)) { - if (length <= distance) { + if ((repStart <= border) && (windowEnd < border)) + { + if (length <= distance) + { System.Array.Copy(window, repStart, window, windowEnd, length); windowEnd += length; - } else { + } + else + { // We have to copy manually, since the repeat pattern overlaps. - while (length-- > 0) { + while (length-- > 0) + { window[windowEnd++] = window[repStart++]; } } - } else { + } + else + { SlowRepeat(repStart, length, distance); } } @@ -90,12 +103,16 @@ public int CopyStored(StreamManipulator input, int length) int copied; int tailLen = WindowSize - windowEnd; - if (length > tailLen) { + if (length > tailLen) + { copied = input.CopyBytes(window, windowEnd, tailLen); - if (copied == tailLen) { + if (copied == tailLen) + { copied += input.CopyBytes(window, 0, length - tailLen); } - } else { + } + else + { copied = input.CopyBytes(window, windowEnd, length); } @@ -115,15 +132,18 @@ public int CopyStored(StreamManipulator input, int length) /// public void CopyDict(byte[] dictionary, int offset, int length) { - if (dictionary == null) { + if (dictionary == null) + { throw new ArgumentNullException(nameof(dictionary)); } - if (windowFilled > 0) { + if (windowFilled > 0) + { throw new InvalidOperationException(); } - if (length > WindowSize) { + if (length > WindowSize) + { offset += length - WindowSize; length = WindowSize; } @@ -162,23 +182,28 @@ public int GetAvailable() public int CopyOutput(byte[] output, int offset, int len) { int copyEnd = windowEnd; - if (len > windowFilled) { + if (len > windowFilled) + { len = windowFilled; - } else { + } + else + { copyEnd = (windowEnd - windowFilled + len) & WindowMask; } int copied = len; int tailLen = len - copyEnd; - if (tailLen > 0) { + if (tailLen > 0) + { System.Array.Copy(window, WindowSize - tailLen, output, offset, tailLen); offset += tailLen; len = copyEnd; } System.Array.Copy(window, copyEnd - len, output, offset, len); windowFilled -= copied; - if (windowFilled < 0) { + if (windowFilled < 0) + { throw new InvalidOperationException(); } return copied; diff --git a/src/ICSharpCode.SharpZipLib/Zip/Compression/Streams/StreamManipulator.cs b/src/ICSharpCode.SharpZipLib/Zip/Compression/Streams/StreamManipulator.cs index e73a50c54..aff6a9c6c 100644 --- a/src/ICSharpCode.SharpZipLib/Zip/Compression/Streams/StreamManipulator.cs +++ b/src/ICSharpCode.SharpZipLib/Zip/Compression/Streams/StreamManipulator.cs @@ -30,8 +30,10 @@ public class StreamManipulator /// public int PeekBits(int bitCount) { - if (bitsInBuffer_ < bitCount) { - if (windowStart_ == windowEnd_) { + if (bitsInBuffer_ < bitCount) + { + if (windowStart_ == windowEnd_) + { return -1; // ok } buffer_ |= (uint)((window_[windowStart_++] & 0xff | @@ -41,6 +43,40 @@ public int PeekBits(int bitCount) return (int)(buffer_ & ((1 << bitCount) - 1)); } + /// + /// Tries to grab the next bits from the input and + /// sets to the value, adding . + /// + /// true if enough bits could be read, otherwise false + public bool TryGetBits(int bitCount, ref int output, int outputOffset = 0) + { + var bits = PeekBits(bitCount); + if (bits < 0) + { + return false; + } + output = bits + outputOffset; + DropBits(bitCount); + return true; + } + + /// + /// Tries to grab the next bits from the input and + /// sets of to the value. + /// + /// true if enough bits could be read, otherwise false + public bool TryGetBits(int bitCount, ref byte[] array, int index) + { + var bits = PeekBits(bitCount); + if (bits < 0) + { + return false; + } + array[index] = (byte)bits; + DropBits(bitCount); + return true; + } + /// /// Drops the next n bits from the input. You should have called PeekBits /// with a bigger or equal n before, to make sure that enough bits are in @@ -64,7 +100,8 @@ public void DropBits(int bitCount) public int GetBits(int bitCount) { int bits = PeekBits(bitCount); - if (bits >= 0) { + if (bits >= 0) + { DropBits(bitCount); } return bits; @@ -77,8 +114,10 @@ public int GetBits(int bitCount) /// /// the number of bits available. /// - public int AvailableBits { - get { + public int AvailableBits + { + get + { return bitsInBuffer_; } } @@ -89,8 +128,10 @@ public int AvailableBits { /// /// The number of bytes available. /// - public int AvailableBytes { - get { + public int AvailableBytes + { + get + { return windowEnd_ - windowStart_ + (bitsInBuffer_ >> 3); } } @@ -107,8 +148,10 @@ public void SkipToByteBoundary() /// /// Returns true when SetInput can be called /// - public bool IsNeedingInput { - get { + public bool IsNeedingInput + { + get + { return windowStart_ == windowEnd_; } } @@ -139,17 +182,20 @@ public bool IsNeedingInput { /// public int CopyBytes(byte[] output, int offset, int length) { - if (length < 0) { + if (length < 0) + { throw new ArgumentOutOfRangeException(nameof(length)); } - if ((bitsInBuffer_ & 7) != 0) { + if ((bitsInBuffer_ & 7) != 0) + { // bits_in_buffer may only be 0 or a multiple of 8 throw new InvalidOperationException("Bit buffer is not byte aligned!"); } int count = 0; - while ((bitsInBuffer_ > 0) && (length > 0)) { + while ((bitsInBuffer_ > 0) && (length > 0)) + { output[offset++] = (byte)buffer_; buffer_ >>= 8; bitsInBuffer_ -= 8; @@ -157,18 +203,21 @@ public int CopyBytes(byte[] output, int offset, int length) count++; } - if (length == 0) { + if (length == 0) + { return count; } int avail = windowEnd_ - windowStart_; - if (length > avail) { + if (length > avail) + { length = avail; } System.Array.Copy(window_, windowStart_, output, offset, length); windowStart_ += length; - if (((windowStart_ - windowEnd_) & 1) != 0) { + if (((windowStart_ - windowEnd_) & 1) != 0) + { // We always want an even number of bytes in input, see peekBits buffer_ = (uint)(window_[windowStart_++] & 0xff); bitsInBuffer_ = 8; @@ -194,19 +243,23 @@ public void Reset() /// number of bytes of input to add. public void SetInput(byte[] buffer, int offset, int count) { - if (buffer == null) { + if (buffer == null) + { throw new ArgumentNullException(nameof(buffer)); } - if (offset < 0) { + if (offset < 0) + { throw new ArgumentOutOfRangeException(nameof(offset), "Cannot be negative"); } - if (count < 0) { + if (count < 0) + { throw new ArgumentOutOfRangeException(nameof(count), "Cannot be negative"); } - if (windowStart_ < windowEnd_) { + if (windowStart_ < windowEnd_) + { throw new InvalidOperationException("Old input was not completely processed"); } @@ -214,11 +267,13 @@ public void SetInput(byte[] buffer, int offset, int count) // We want to throw an ArrayIndexOutOfBoundsException early. // Note the check also handles integer wrap around. - if ((offset > end) || (end > buffer.Length)) { + if ((offset > end) || (end > buffer.Length)) + { throw new ArgumentOutOfRangeException(nameof(count)); } - if ((count & 1) != 0) { + if ((count & 1) != 0) + { // We always want an even number of bytes in input, see PeekBits buffer_ |= (uint)((buffer[offset++] & 0xff) << bitsInBuffer_); bitsInBuffer_ += 8; @@ -230,12 +285,14 @@ public void SetInput(byte[] buffer, int offset, int count) } #region Instance Fields + private byte[] window_; private int windowStart_; private int windowEnd_; private uint buffer_; private int bitsInBuffer_; - #endregion + + #endregion Instance Fields } } diff --git a/src/ICSharpCode.SharpZipLib/Zip/FastZip.cs b/src/ICSharpCode.SharpZipLib/Zip/FastZip.cs index 14ef5443b..baa1771cb 100644 --- a/src/ICSharpCode.SharpZipLib/Zip/FastZip.cs +++ b/src/ICSharpCode.SharpZipLib/Zip/FastZip.cs @@ -1,8 +1,9 @@ -using System; -using System.IO; using ICSharpCode.SharpZipLib.Core; using ICSharpCode.SharpZipLib.Zip.Compression; +using System; +using System.IO; using static ICSharpCode.SharpZipLib.Zip.Compression.Deflater; +using static ICSharpCode.SharpZipLib.Zip.ZipEntryFactory; namespace ICSharpCode.SharpZipLib.Zip { @@ -52,7 +53,8 @@ public bool OnDirectoryFailure(string directory, Exception e) bool result = false; DirectoryFailureHandler handler = DirectoryFailure; - if (handler != null) { + if (handler != null) + { var args = new ScanFailureEventArgs(directory, e); handler(this, args); result = args.ContinueRunning; @@ -71,7 +73,8 @@ public bool OnFileFailure(string file, Exception e) FileFailureHandler handler = FileFailure; bool result = (handler != null); - if (result) { + if (result) + { var args = new ScanFailureEventArgs(file, e); handler(this, args); result = args.ContinueRunning; @@ -89,7 +92,8 @@ public bool OnProcessFile(string file) bool result = true; ProcessFileHandler handler = ProcessFile; - if (handler != null) { + if (handler != null) + { var args = new ScanEventArgs(file); handler(this, args); result = args.ContinueRunning; @@ -106,7 +110,8 @@ public bool OnCompletedFile(string file) { bool result = true; CompletedFileHandler handler = CompletedFile; - if (handler != null) { + if (handler != null) + { var args = new ScanEventArgs(file); handler(this, args); result = args.ContinueRunning; @@ -124,7 +129,8 @@ public bool OnProcessDirectory(string directory, bool hasMatchingFiles) { bool result = true; EventHandler handler = ProcessDirectory; - if (handler != null) { + if (handler != null) + { var args = new DirectoryEventArgs(directory, hasMatchingFiles); handler(this, args); result = args.ContinueRunning; @@ -138,14 +144,17 @@ public bool OnProcessDirectory(string directory, bool hasMatchingFiles) /// The minimum period of time between events. /// /// The default interval is three seconds. - public TimeSpan ProgressInterval { + public TimeSpan ProgressInterval + { get { return progressInterval_; } set { progressInterval_ = value; } } #region Instance Fields - TimeSpan progressInterval_ = TimeSpan.FromSeconds(3); - #endregion + + private TimeSpan progressInterval_ = TimeSpan.FromSeconds(3); + + #endregion Instance Fields } /// @@ -154,6 +163,7 @@ public TimeSpan ProgressInterval { public class FastZip { #region Enumerations + /// /// Defines the desired handling when overwriting files during extraction. /// @@ -163,19 +173,22 @@ public enum Overwrite /// Prompt the user to confirm overwriting /// Prompt, + /// /// Never overwrite files. /// Never, + /// /// Always overwrite files. /// Always } - #endregion + #endregion Enumerations #region Constructors + /// /// Initialise a default instance of . /// @@ -183,6 +196,29 @@ public FastZip() { } + /// + /// Initialise a new instance of using the specified + /// + /// The time setting to use when creating or extracting Zip entries. + /// Using TimeSetting.LastAccessTime[Utc] when + /// creating an archive will set the file time to the moment of reading. + /// + public FastZip(TimeSetting timeSetting) + { + entryFactory_ = new ZipEntryFactory(timeSetting); + restoreDateTimeOnExtract_ = true; + } + + /// + /// Initialise a new instance of using the specified + /// + /// The time to set all values for created or extracted Zip Entries. + public FastZip(DateTime time) + { + entryFactory_ = new ZipEntryFactory(time); + restoreDateTimeOnExtract_ = true; + } + /// /// Initialise a new instance of /// @@ -191,13 +227,16 @@ public FastZip(FastZipEvents events) { events_ = events; } - #endregion + + #endregion Constructors #region Properties + /// - /// Get/set a value indicating wether empty directories should be created. + /// Get/set a value indicating whether empty directories should be created. /// - public bool CreateEmptyDirectories { + public bool CreateEmptyDirectories + { get { return createEmptyDirectories_; } set { createEmptyDirectories_ = value; } } @@ -205,18 +244,30 @@ public bool CreateEmptyDirectories { /// /// Get / set the password value. /// - public string Password { + public string Password + { get { return password_; } set { password_ = value; } } + /// + /// Get / set the method of encrypting entries. + /// + /// + /// Only applies when is set. + /// Defaults to ZipCrypto for backwards compatibility purposes. + /// + public ZipEncryptionMethod EntryEncryptionMethod { get; set; } = ZipEncryptionMethod.ZipCrypto; + /// /// Get or set the active when creating Zip files. /// /// - public INameTransform NameTransform { + public INameTransform NameTransform + { get { return entryFactory_.NameTransform; } - set { + set + { entryFactory_.NameTransform = value; } } @@ -224,12 +275,17 @@ public INameTransform NameTransform { /// /// Get or set the active when creating Zip files. /// - public IEntryFactory EntryFactory { + public IEntryFactory EntryFactory + { get { return entryFactory_; } - set { - if (value == null) { + set + { + if (value == null) + { entryFactory_ = new ZipEntryFactory(); - } else { + } + else + { entryFactory_ = value; } } @@ -244,23 +300,27 @@ public IEntryFactory EntryFactory { /// read Zip64 archives. However it does avoid the situation were a large file /// is added and cannot be completed correctly. /// NOTE: Setting the size for entries before they are added is the best solution! - /// By default the EntryFactory used by FastZip will set fhe file size. + /// By default the EntryFactory used by FastZip will set the file size. /// - public UseZip64 UseZip64 { + public UseZip64 UseZip64 + { get { return useZip64_; } set { useZip64_ = value; } } /// - /// Get/set a value indicating wether file dates and times should + /// Get/set a value indicating whether file dates and times should /// be restored when extracting files from an archive. /// /// The default value is false. - public bool RestoreDateTimeOnExtract { - get { + public bool RestoreDateTimeOnExtract + { + get + { return restoreDateTimeOnExtract_; } - set { + set + { restoreDateTimeOnExtract_ = value; } } @@ -269,29 +329,58 @@ public bool RestoreDateTimeOnExtract { /// Get/set a value indicating whether file attributes should /// be restored during extract operations /// - public bool RestoreAttributesOnExtract { + public bool RestoreAttributesOnExtract + { get { return restoreAttributesOnExtract_; } set { restoreAttributesOnExtract_ = value; } } - /// - /// Get/set the Compression Level that will be used - /// when creating the zip - /// - public Deflater.CompressionLevel CompressionLevel{ - get { return compressionLevel_; } - set { compressionLevel_ = value; } - } - #endregion + /// + /// Get/set the Compression Level that will be used + /// when creating the zip + /// + public Deflater.CompressionLevel CompressionLevel + { + get { return compressionLevel_; } + set { compressionLevel_ = value; } + } + + /// + /// Reflects the opposite of the internal , setting it to false overrides the encoding used for reading and writing zip entries + /// + public bool UseUnicode + { + get => !_stringCodec.ForceZipLegacyEncoding; + set => _stringCodec.ForceZipLegacyEncoding = !value; + } + + /// Gets or sets the code page used for reading/writing zip file entries when unicode is disabled + public int LegacyCodePage + { + get => _stringCodec.CodePage; + set => _stringCodec = StringCodec.FromCodePage(value); + } + + /// + public StringCodec StringCodec + { + get => _stringCodec; + set => _stringCodec = value; + } + + #endregion Properties #region Delegates + /// /// Delegate called when confirming overwriting of files. /// public delegate bool ConfirmOverwriteDelegate(string fileName); - #endregion + + #endregion Delegates #region CreateZip + /// /// Create a zip file. /// @@ -328,31 +417,95 @@ public void CreateZip(string zipFileName, string sourceDirectory, bool recurse, /// The directory filter to apply. /// The is closed after creation. public void CreateZip(Stream outputStream, string sourceDirectory, bool recurse, string fileFilter, string directoryFilter) + { + CreateZip(outputStream, sourceDirectory, recurse, fileFilter, directoryFilter, false); + } + + /// + /// Create a zip archive sending output to the passed. + /// + /// The stream to write archive data to. + /// The directory to source files from. + /// True to recurse directories, false for no recursion. + /// The file filter to apply. + /// The directory filter to apply. + /// true to leave open after the zip has been created, false to dispose it. + public void CreateZip(Stream outputStream, string sourceDirectory, bool recurse, string fileFilter, string directoryFilter, bool leaveOpen) + { + var scanner = new FileSystemScanner(fileFilter, directoryFilter); + CreateZip(outputStream, sourceDirectory, recurse, scanner, leaveOpen); + } + + /// + /// Create a zip file. + /// + /// The name of the zip file to create. + /// The directory to source files from. + /// True to recurse directories, false for no recursion. + /// The file filter to apply. + /// The directory filter to apply. + public void CreateZip(string zipFileName, string sourceDirectory, + bool recurse, IScanFilter fileFilter, IScanFilter directoryFilter) + { + CreateZip(File.Create(zipFileName), sourceDirectory, recurse, fileFilter, directoryFilter, false); + } + + /// + /// Create a zip archive sending output to the passed. + /// + /// The stream to write archive data to. + /// The directory to source files from. + /// True to recurse directories, false for no recursion. + /// The file filter to apply. + /// The directory filter to apply. + /// true to leave open after the zip has been created, false to dispose it. + public void CreateZip(Stream outputStream, string sourceDirectory, bool recurse, IScanFilter fileFilter, IScanFilter directoryFilter, bool leaveOpen = false) + { + var scanner = new FileSystemScanner(fileFilter, directoryFilter); + CreateZip(outputStream, sourceDirectory, recurse, scanner, leaveOpen); + } + + /// + /// Create a zip archive sending output to the passed. + /// + /// The stream to write archive data to. + /// The directory to source files from. + /// True to recurse directories, false for no recursion. + /// For performing the actual file system scan + /// true to leave open after the zip has been created, false to dispose it. + /// The is closed after creation. + private void CreateZip(Stream outputStream, string sourceDirectory, bool recurse, FileSystemScanner scanner, bool leaveOpen) { NameTransform = new ZipNameTransform(sourceDirectory); sourceDirectory_ = sourceDirectory; - using (outputStream_ = new ZipOutputStream(outputStream)) { - - outputStream_.SetLevel((int)CompressionLevel); + using (outputStream_ = new ZipOutputStream(outputStream, _stringCodec)) + { + outputStream_.SetLevel((int)CompressionLevel); + outputStream_.IsStreamOwner = !leaveOpen; + outputStream_.NameTransform = null; // all required transforms handled by us - if (password_ != null) { + if (false == string.IsNullOrEmpty(password_) && EntryEncryptionMethod != ZipEncryptionMethod.None) + { outputStream_.Password = password_; } outputStream_.UseZip64 = UseZip64; - var scanner = new FileSystemScanner(fileFilter, directoryFilter); scanner.ProcessFile += ProcessFile; - if (this.CreateEmptyDirectories) { + if (this.CreateEmptyDirectories) + { scanner.ProcessDirectory += ProcessDirectory; } - if (events_ != null) { - if (events_.FileFailure != null) { + if (events_ != null) + { + if (events_.FileFailure != null) + { scanner.FileFailure += events_.FileFailure; } - if (events_.DirectoryFailure != null) { + if (events_.DirectoryFailure != null) + { scanner.DirectoryFailure += events_.DirectoryFailure; } } @@ -361,9 +514,10 @@ public void CreateZip(Stream outputStream, string sourceDirectory, bool recurse, } } - #endregion + #endregion CreateZip #region ExtractZip + /// /// Extract the contents of a zip file. /// @@ -385,12 +539,13 @@ public void ExtractZip(string zipFileName, string targetDirectory, string fileFi /// A filter to apply to files. /// A filter to apply to directories. /// Flag indicating whether to restore the date and time for extracted files. + /// Allow parent directory traversal in file paths (e.g. ../file) public void ExtractZip(string zipFileName, string targetDirectory, Overwrite overwrite, ConfirmOverwriteDelegate confirmDelegate, - string fileFilter, string directoryFilter, bool restoreDateTime) + string fileFilter, string directoryFilter, bool restoreDateTime, bool allowParentTraversal = false) { Stream inputStream = File.Open(zipFileName, FileMode.Open, FileAccess.Read, FileShare.Read); - ExtractZip(inputStream, targetDirectory, overwrite, confirmDelegate, fileFilter, directoryFilter, restoreDateTime, true); + ExtractZip(inputStream, targetDirectory, overwrite, confirmDelegate, fileFilter, directoryFilter, restoreDateTime, true, allowParentTraversal); } /// @@ -404,60 +559,77 @@ public void ExtractZip(string zipFileName, string targetDirectory, /// A filter to apply to directories. /// Flag indicating whether to restore the date and time for extracted files. /// Flag indicating whether the inputStream will be closed by this method. + /// Allow parent directory traversal in file paths (e.g. ../file) public void ExtractZip(Stream inputStream, string targetDirectory, Overwrite overwrite, ConfirmOverwriteDelegate confirmDelegate, string fileFilter, string directoryFilter, bool restoreDateTime, - bool isStreamOwner) + bool isStreamOwner, bool allowParentTraversal = false) { - if ((overwrite == Overwrite.Prompt) && (confirmDelegate == null)) { + if ((overwrite == Overwrite.Prompt) && (confirmDelegate == null)) + { throw new ArgumentNullException(nameof(confirmDelegate)); } continueRunning_ = true; overwrite_ = overwrite; confirmDelegate_ = confirmDelegate; - extractNameTransform_ = new WindowsNameTransform(targetDirectory); + extractNameTransform_ = new WindowsNameTransform(targetDirectory, allowParentTraversal); fileFilter_ = new NameFilter(fileFilter); directoryFilter_ = new NameFilter(directoryFilter); restoreDateTimeOnExtract_ = restoreDateTime; - using (zipFile_ = new ZipFile(inputStream)) { - - if (password_ != null) { + using (zipFile_ = new ZipFile(inputStream, !isStreamOwner, _stringCodec)) + { + if (password_ != null) + { zipFile_.Password = password_; } - zipFile_.IsStreamOwner = isStreamOwner; + System.Collections.IEnumerator enumerator = zipFile_.GetEnumerator(); - while (continueRunning_ && enumerator.MoveNext()) { + while (continueRunning_ && enumerator.MoveNext()) + { var entry = (ZipEntry)enumerator.Current; - if (entry.IsFile) { + if (entry.IsFile) + { // TODO Path.GetDirectory can fail here on invalid characters. - if (directoryFilter_.IsMatch(Path.GetDirectoryName(entry.Name)) && fileFilter_.IsMatch(entry.Name)) { + if (directoryFilter_.IsMatch(Path.GetDirectoryName(entry.Name)) && fileFilter_.IsMatch(entry.Name)) + { ExtractEntry(entry); } - } else if (entry.IsDirectory) { - if (directoryFilter_.IsMatch(entry.Name) && CreateEmptyDirectories) { + } + else if (entry.IsDirectory) + { + if (directoryFilter_.IsMatch(entry.Name) && CreateEmptyDirectories) + { ExtractEntry(entry); } - } else { + } + else + { // Do nothing for volume labels etc... } } } } - #endregion + + #endregion ExtractZip #region Internal Processing - void ProcessDirectory(object sender, DirectoryEventArgs e) + + private void ProcessDirectory(object sender, DirectoryEventArgs e) { - if (!e.HasMatchingFiles && CreateEmptyDirectories) { - if (events_ != null) { + if (!e.HasMatchingFiles && CreateEmptyDirectories) + { + if (events_ != null) + { events_.OnProcessDirectory(e.Name, e.HasMatchingFiles); } - if (e.ContinueRunning) { - if (e.Name != sourceDirectory_) { + if (e.ContinueRunning) + { + if (e.Name != sourceDirectory_) + { ZipEntry entry = entryFactory_.MakeDirectoryEntry(e.Name); outputStream_.PutNextEntry(entry); } @@ -465,26 +637,43 @@ void ProcessDirectory(object sender, DirectoryEventArgs e) } } - void ProcessFile(object sender, ScanEventArgs e) + private void ProcessFile(object sender, ScanEventArgs e) { - if ((events_ != null) && (events_.ProcessFile != null)) { + if ((events_ != null) && (events_.ProcessFile != null)) + { events_.ProcessFile(sender, e); } - if (e.ContinueRunning) { - try { - // The open below is equivalent to OpenRead which gaurantees that if opened the + if (e.ContinueRunning) + { + try + { + // The open below is equivalent to OpenRead which guarantees that if opened the // file will not be changed by subsequent openers, but precludes opening in some cases // were it could succeed. ie the open may fail as its already open for writing and the share mode should reflect that. - using (FileStream stream = File.Open(e.Name, FileMode.Open, FileAccess.Read, FileShare.Read)) { + using (FileStream stream = File.Open(e.Name, FileMode.Open, FileAccess.Read, FileShare.Read)) + { ZipEntry entry = entryFactory_.MakeFileEntry(e.Name); + if (_stringCodec.ForceZipLegacyEncoding) + { + entry.IsUnicodeText = false; + } + + // Set up AES encryption for the entry if required. + ConfigureEntryEncryption(entry); + outputStream_.PutNextEntry(entry); AddFileContents(e.Name, stream); } - } catch (Exception ex) { - if (events_ != null) { + } + catch (Exception ex) + { + if (events_ != null) + { continueRunning_ = events_.OnFileFailure(e.Name, ex); - } else { + } + else + { continueRunning_ = false; throw; } @@ -492,78 +681,162 @@ void ProcessFile(object sender, ScanEventArgs e) } } - void AddFileContents(string name, Stream stream) + // Set up the encryption method to use for the specific entry. + private void ConfigureEntryEncryption(ZipEntry entry) { - if (stream == null) { + // Only alter the entries options if AES isn't already enabled for it + // (it might have been set up by the entry factory, and if so we let that take precedence) + if (!string.IsNullOrEmpty(Password) && entry.AESEncryptionStrength == 0) + { + switch (EntryEncryptionMethod) + { + case ZipEncryptionMethod.AES128: + entry.AESKeySize = 128; + break; + + case ZipEncryptionMethod.AES256: + entry.AESKeySize = 256; + break; + } + } + } + + private void AddFileContents(string name, Stream stream) + { + if (stream == null) + { throw new ArgumentNullException(nameof(stream)); } - if (buffer_ == null) { + if (buffer_ == null) + { buffer_ = new byte[4096]; } - if ((events_ != null) && (events_.Progress != null)) { + if ((events_ != null) && (events_.Progress != null)) + { StreamUtils.Copy(stream, outputStream_, buffer_, events_.Progress, events_.ProgressInterval, this, name); - } else { + } + else + { StreamUtils.Copy(stream, outputStream_, buffer_); } - if (events_ != null) { + if (events_ != null) + { continueRunning_ = events_.OnCompletedFile(name); } } - void ExtractFileEntry(ZipEntry entry, string targetName) + private void ExtractFileEntry(ZipEntry entry, string targetName) { bool proceed = true; - if (overwrite_ != Overwrite.Always) { - if (File.Exists(targetName)) { - if ((overwrite_ == Overwrite.Prompt) && (confirmDelegate_ != null)) { + if (overwrite_ != Overwrite.Always) + { + if (File.Exists(targetName)) + { + if ((overwrite_ == Overwrite.Prompt) && (confirmDelegate_ != null)) + { proceed = confirmDelegate_(targetName); - } else { + } + else + { proceed = false; } } } - if (proceed) { - if (events_ != null) { + if (proceed) + { + if (events_ != null) + { continueRunning_ = events_.OnProcessFile(entry.Name); } - if (continueRunning_) { - try { - using (FileStream outputStream = File.Create(targetName)) { - if (buffer_ == null) { + if (continueRunning_) + { + try + { + using (FileStream outputStream = File.Create(targetName)) + { + if (buffer_ == null) + { buffer_ = new byte[4096]; } - if ((events_ != null) && (events_.Progress != null)) { - StreamUtils.Copy(zipFile_.GetInputStream(entry), outputStream, buffer_, - events_.Progress, events_.ProgressInterval, this, entry.Name, entry.Size); - } else { - StreamUtils.Copy(zipFile_.GetInputStream(entry), outputStream, buffer_); + + using (var inputStream = zipFile_.GetInputStream(entry)) + { + if ((events_ != null) && (events_.Progress != null)) + { + StreamUtils.Copy(inputStream, outputStream, buffer_, + events_.Progress, events_.ProgressInterval, this, entry.Name, entry.Size); + } + else + { + StreamUtils.Copy(inputStream, outputStream, buffer_); + } } - if (events_ != null) { + if (events_ != null) + { continueRunning_ = events_.OnCompletedFile(entry.Name); } } - if (restoreDateTimeOnExtract_) { - File.SetLastWriteTime(targetName, entry.DateTime); + if (restoreDateTimeOnExtract_) + { + switch (entryFactory_.Setting) + { + case TimeSetting.CreateTime: + File.SetCreationTime(targetName, entry.DateTime); + break; + + case TimeSetting.CreateTimeUtc: + File.SetCreationTimeUtc(targetName, entry.DateTime); + break; + + case TimeSetting.LastAccessTime: + File.SetLastAccessTime(targetName, entry.DateTime); + break; + + case TimeSetting.LastAccessTimeUtc: + File.SetLastAccessTimeUtc(targetName, entry.DateTime); + break; + + case TimeSetting.LastWriteTime: + File.SetLastWriteTime(targetName, entry.DateTime); + break; + + case TimeSetting.LastWriteTimeUtc: + File.SetLastWriteTimeUtc(targetName, entry.DateTime); + break; + + case TimeSetting.Fixed: + File.SetLastWriteTime(targetName, entryFactory_.FixedDateTime); + break; + + default: + throw new ZipException("Unhandled time setting in ExtractFileEntry"); + } } - if (RestoreAttributesOnExtract && entry.IsDOSEntry && (entry.ExternalFileAttributes != -1)) { + if (RestoreAttributesOnExtract && entry.IsDOSEntry && (entry.ExternalFileAttributes != -1)) + { var fileAttributes = (FileAttributes)entry.ExternalFileAttributes; // TODO: FastZip - Setting of other file attributes on extraction is a little trickier. fileAttributes &= (FileAttributes.Archive | FileAttributes.Normal | FileAttributes.ReadOnly | FileAttributes.Hidden); File.SetAttributes(targetName, fileAttributes); } - } catch (Exception ex) { - if (events_ != null) { + } + catch (Exception ex) + { + if (events_ != null) + { continueRunning_ = events_.OnFileFailure(targetName, ex); - } else { + } + else + { continueRunning_ = false; throw; } @@ -572,15 +845,19 @@ void ExtractFileEntry(ZipEntry entry, string targetName) } } - void ExtractEntry(ZipEntry entry) + private void ExtractEntry(ZipEntry entry) { bool doExtraction = entry.IsCompressionMethodSupported(); string targetName = entry.Name; - if (doExtraction) { - if (entry.IsFile) { + if (doExtraction) + { + if (entry.IsFile) + { targetName = extractNameTransform_.TransformFile(targetName); - } else if (entry.IsDirectory) { + } + else if (entry.IsDirectory) + { targetName = extractNameTransform_.TransformDirectory(targetName); } @@ -589,29 +866,88 @@ void ExtractEntry(ZipEntry entry) // TODO: Fire delegate/throw exception were compression method not supported, or name is invalid? - string dirName = null; + string dirName = string.Empty; - if (doExtraction) { - if (entry.IsDirectory) { + if (doExtraction) + { + if (entry.IsDirectory) + { dirName = targetName; - } else { + } + else + { dirName = Path.GetDirectoryName(Path.GetFullPath(targetName)); } } - if (doExtraction && !Directory.Exists(dirName)) { - if (!entry.IsDirectory || CreateEmptyDirectories) { - try { - Directory.CreateDirectory(dirName); - } catch (Exception ex) { + if (doExtraction && !Directory.Exists(dirName)) + { + if (!entry.IsDirectory || CreateEmptyDirectories) + { + try + { + continueRunning_ = events_?.OnProcessDirectory(dirName, true) ?? true; + if (continueRunning_) + { + Directory.CreateDirectory(dirName); + if (entry.IsDirectory && restoreDateTimeOnExtract_) + { + switch (entryFactory_.Setting) + { + case TimeSetting.CreateTime: + Directory.SetCreationTime(dirName, entry.DateTime); + break; + + case TimeSetting.CreateTimeUtc: + Directory.SetCreationTimeUtc(dirName, entry.DateTime); + break; + + case TimeSetting.LastAccessTime: + Directory.SetLastAccessTime(dirName, entry.DateTime); + break; + + case TimeSetting.LastAccessTimeUtc: + Directory.SetLastAccessTimeUtc(dirName, entry.DateTime); + break; + + case TimeSetting.LastWriteTime: + Directory.SetLastWriteTime(dirName, entry.DateTime); + break; + + case TimeSetting.LastWriteTimeUtc: + Directory.SetLastWriteTimeUtc(dirName, entry.DateTime); + break; + + case TimeSetting.Fixed: + Directory.SetLastWriteTime(dirName, entryFactory_.FixedDateTime); + break; + + default: + throw new ZipException("Unhandled time setting in ExtractEntry"); + } + } + } + else + { + doExtraction = false; + } + } + catch (Exception ex) + { doExtraction = false; - if (events_ != null) { - if (entry.IsDirectory) { + if (events_ != null) + { + if (entry.IsDirectory) + { continueRunning_ = events_.OnDirectoryFailure(targetName, ex); - } else { + } + else + { continueRunning_ = events_.OnFileFailure(targetName, ex); } - } else { + } + else + { continueRunning_ = false; throw; } @@ -619,45 +955,48 @@ void ExtractEntry(ZipEntry entry) } } - if (doExtraction && entry.IsFile) { + if (doExtraction && entry.IsFile) + { ExtractFileEntry(entry, targetName); } } - static int MakeExternalAttributes(FileInfo info) + private static int MakeExternalAttributes(FileInfo info) { return (int)info.Attributes; } - static bool NameIsValid(string name) + private static bool NameIsValid(string name) { return !string.IsNullOrEmpty(name) && (name.IndexOfAny(Path.GetInvalidPathChars()) < 0); } - #endregion + + #endregion Internal Processing #region Instance Fields - bool continueRunning_; - byte[] buffer_; - ZipOutputStream outputStream_; - ZipFile zipFile_; - string sourceDirectory_; - NameFilter fileFilter_; - NameFilter directoryFilter_; - Overwrite overwrite_; - ConfirmOverwriteDelegate confirmDelegate_; - - bool restoreDateTimeOnExtract_; - bool restoreAttributesOnExtract_; - bool createEmptyDirectories_; - FastZipEvents events_; - IEntryFactory entryFactory_ = new ZipEntryFactory(); - INameTransform extractNameTransform_; - UseZip64 useZip64_ = UseZip64.Dynamic; - CompressionLevel compressionLevel_ = CompressionLevel.DEFAULT_COMPRESSION; - - string password_; - - #endregion + + private bool continueRunning_; + private byte[] buffer_; + private ZipOutputStream outputStream_; + private ZipFile zipFile_; + private string sourceDirectory_; + private NameFilter fileFilter_; + private NameFilter directoryFilter_; + private Overwrite overwrite_; + private ConfirmOverwriteDelegate confirmDelegate_; + + private bool restoreDateTimeOnExtract_; + private bool restoreAttributesOnExtract_; + private bool createEmptyDirectories_; + private FastZipEvents events_; + private IEntryFactory entryFactory_ = new ZipEntryFactory(); + private INameTransform extractNameTransform_; + private UseZip64 useZip64_ = UseZip64.Dynamic; + private CompressionLevel compressionLevel_ = CompressionLevel.DEFAULT_COMPRESSION; + private StringCodec _stringCodec = ZipStrings.GetStringCodec(); + private string password_; + + #endregion Instance Fields } } diff --git a/src/ICSharpCode.SharpZipLib/Zip/IEntryFactory.cs b/src/ICSharpCode.SharpZipLib/Zip/IEntryFactory.cs index bbe40c4d7..d7ec18140 100644 --- a/src/ICSharpCode.SharpZipLib/Zip/IEntryFactory.cs +++ b/src/ICSharpCode.SharpZipLib/Zip/IEntryFactory.cs @@ -1,4 +1,6 @@ +using System; using ICSharpCode.SharpZipLib.Core; +using static ICSharpCode.SharpZipLib.Zip.ZipEntryFactory; namespace ICSharpCode.SharpZipLib.Zip { @@ -50,5 +52,16 @@ public interface IEntryFactory /// Get/set the applicable. /// INameTransform NameTransform { get; set; } + + /// + /// Get the in use. + /// + TimeSetting Setting { get; } + + /// + /// Get the value to use when is set to , + /// or if not specified, the value of when the class was the initialized + /// + DateTime FixedDateTime { get; } } } diff --git a/src/ICSharpCode.SharpZipLib/Zip/WindowsNameTransform.cs b/src/ICSharpCode.SharpZipLib/Zip/WindowsNameTransform.cs index 2dd32f83c..43aa61403 100644 --- a/src/ICSharpCode.SharpZipLib/Zip/WindowsNameTransform.cs +++ b/src/ICSharpCode.SharpZipLib/Zip/WindowsNameTransform.cs @@ -1,7 +1,8 @@ +using ICSharpCode.SharpZipLib.Core; using System; using System.IO; +using System.Runtime.InteropServices; using System.Text; -using ICSharpCode.SharpZipLib.Core; namespace ICSharpCode.SharpZipLib.Zip { @@ -14,17 +15,18 @@ public class WindowsNameTransform : INameTransform /// The maximum windows path name permitted. /// /// This may not valid for all windows systems - CE?, etc but I cant find the equivalent in the CLR. - const int MaxPath = 260; + private const int MaxPath = 260; - string _baseDirectory; - bool _trimIncomingPaths; - char _replacementChar = '_'; + private string _baseDirectory; + private bool _trimIncomingPaths; + private char _replacementChar = '_'; + private bool _allowParentTraversal; /// /// In this case we need Windows' invalid path characters. /// Path.GetInvalidPathChars() only returns a subset invalid on all platforms. /// - static readonly char[] InvalidEntryChars = new char[] { + private static readonly char[] InvalidEntryChars = new char[] { '"', '<', '>', '|', '\0', '\u0001', '\u0002', '\u0003', '\u0004', '\u0005', '\u0006', '\a', '\b', '\t', '\n', '\v', '\f', '\r', '\u000e', '\u000f', '\u0010', '\u0011', '\u0012', '\u0013', '\u0014', '\u0015', '\u0016', @@ -38,13 +40,11 @@ public class WindowsNameTransform : INameTransform /// Initialises a new instance of /// /// - public WindowsNameTransform(string baseDirectory) + /// Allow parent directory traversal in file paths (e.g. ../file) + public WindowsNameTransform(string baseDirectory, bool allowParentTraversal = false) { - if (baseDirectory == null) { - throw new ArgumentNullException(nameof(baseDirectory), "Directory name is invalid"); - } - - BaseDirectory = baseDirectory; + BaseDirectory = baseDirectory ?? throw new ArgumentNullException(nameof(baseDirectory), "Directory name is invalid"); + AllowParentTraversal = allowParentTraversal; } /// @@ -58,10 +58,13 @@ public WindowsNameTransform() /// /// Gets or sets a value containing the target directory to prefix values with. /// - public string BaseDirectory { + public string BaseDirectory + { get { return _baseDirectory; } - set { - if (value == null) { + set + { + if (value == null) + { throw new ArgumentNullException(nameof(value)); } @@ -70,9 +73,19 @@ public string BaseDirectory { } /// - /// Gets or sets a value indicating wether paths on incoming values should be removed. + /// Allow parent directory traversal in file paths (e.g. ../file) + /// + public bool AllowParentTraversal + { + get => _allowParentTraversal; + set => _allowParentTraversal = value; + } + + /// + /// Gets or sets a value indicating whether paths on incoming values should be removed. /// - public bool TrimIncomingPaths { + public bool TrimIncomingPaths + { get { return _trimIncomingPaths; } set { _trimIncomingPaths = value; } } @@ -85,12 +98,16 @@ public bool TrimIncomingPaths { public string TransformDirectory(string name) { name = TransformFile(name); - if (name.Length > 0) { - while (name.EndsWith(Path.DirectorySeparatorChar.ToString(), StringComparison.Ordinal)) { + if (name.Length > 0) + { + while (name.EndsWith(Path.DirectorySeparatorChar.ToString(), StringComparison.Ordinal)) + { name = name.Remove(name.Length - 1, 1); } - } else { - throw new ZipException("Cannot have an empty directory name"); + } + else + { + throw new InvalidNameException("Cannot have an empty directory name"); } return name; } @@ -102,19 +119,36 @@ public string TransformDirectory(string name) /// The transformed name. public string TransformFile(string name) { - if (name != null) { + if (name != null) + { name = MakeValidName(name, _replacementChar); - if (_trimIncomingPaths) { + if (_trimIncomingPaths) + { name = Path.GetFileName(name); } // This may exceed windows length restrictions. // Combine will throw a PathTooLongException in that case. - if (_baseDirectory != null) { + if (_baseDirectory != null) + { name = Path.Combine(_baseDirectory, name); + + // Ensure base directory ends with directory separator ('/' or '\' depending on OS) + var pathBase = Path.GetFullPath(_baseDirectory); + if (pathBase[pathBase.Length - 1] != Path.DirectorySeparatorChar) + { + pathBase += Path.DirectorySeparatorChar; + } + + if (!_allowParentTraversal && !Path.GetFullPath(name).StartsWith(pathBase, StringComparison.InvariantCultureIgnoreCase)) + { + throw new InvalidNameException("Parent traversal in paths is not allowed"); + } } - } else { + } + else + { name = string.Empty; } return name; @@ -145,40 +179,49 @@ public static bool IsValidName(string name) /// Returns a valid name public static string MakeValidName(string name, char replacement) { - if (name == null) { + if (name == null) + { throw new ArgumentNullException(nameof(name)); } - name = WindowsPathUtils.DropPathRoot(name.Replace("/", Path.DirectorySeparatorChar.ToString())); + name = PathUtils.DropPathRoot(name.Replace("/", Path.DirectorySeparatorChar.ToString())); // Drop any leading slashes. - while ((name.Length > 0) && (name[0] == Path.DirectorySeparatorChar)) { + while ((name.Length > 0) && (name[0] == Path.DirectorySeparatorChar)) + { name = name.Remove(0, 1); } // Drop any trailing slashes. - while ((name.Length > 0) && (name[name.Length - 1] == Path.DirectorySeparatorChar)) { + while ((name.Length > 0) && (name[name.Length - 1] == Path.DirectorySeparatorChar)) + { name = name.Remove(name.Length - 1, 1); } // Convert consecutive \\ characters to \ int index = name.IndexOf(string.Format("{0}{0}", Path.DirectorySeparatorChar), StringComparison.Ordinal); - while (index >= 0) { + while (index >= 0) + { name = name.Remove(index, 1); index = name.IndexOf(string.Format("{0}{0}", Path.DirectorySeparatorChar), StringComparison.Ordinal); } // Convert any invalid characters using the replacement one. index = name.IndexOfAny(InvalidEntryChars); - if (index >= 0) { + if (index >= 0) + { var builder = new StringBuilder(name); - while (index >= 0) { + while (index >= 0) + { builder[index] = replacement; - if (index >= name.Length) { + if (index >= name.Length) + { index = -1; - } else { + } + else + { index = name.IndexOfAny(InvalidEntryChars, index + 1); } } @@ -187,7 +230,8 @@ public static string MakeValidName(string name, char replacement) // Check for names greater than MaxPath characters. // TODO: Were is CLR version of MaxPath defined? Can't find it in Environment. - if (name.Length > MaxPath) { + if (name.Length > MaxPath) + { throw new PathTooLongException(); } @@ -197,16 +241,21 @@ public static string MakeValidName(string name, char replacement) /// /// Gets or set the character to replace invalid characters during transformations. /// - public char Replacement { + public char Replacement + { get { return _replacementChar; } - set { - for (int i = 0; i < InvalidEntryChars.Length; ++i) { - if (InvalidEntryChars[i] == value) { + set + { + for (int i = 0; i < InvalidEntryChars.Length; ++i) + { + if (InvalidEntryChars[i] == value) + { throw new ArgumentException("invalid path character"); } } - if ((value == Path.DirectorySeparatorChar) || (value == Path.AltDirectorySeparatorChar)) { + if ((value == Path.DirectorySeparatorChar) || (value == Path.AltDirectorySeparatorChar)) + { throw new ArgumentException("invalid replacement character"); } diff --git a/src/ICSharpCode.SharpZipLib/Zip/ZipConstants.cs b/src/ICSharpCode.SharpZipLib/Zip/ZipConstants.cs index 988e4eb10..b16fdefdf 100644 --- a/src/ICSharpCode.SharpZipLib/Zip/ZipConstants.cs +++ b/src/ICSharpCode.SharpZipLib/Zip/ZipConstants.cs @@ -1,7 +1,4 @@ using System; -using System.Globalization; -using System.Text; -using System.Threading; namespace ICSharpCode.SharpZipLib.Zip { @@ -17,10 +14,12 @@ public enum UseZip64 /// /// An entry can have this overridden if required Off, + /// /// Zip64 should always be used. /// On, + /// /// #ZipLib will determine use based on entry values when added to archive. /// @@ -38,7 +37,7 @@ public enum CompressionMethod Stored = 0, /// - /// Common Zip compression method using a sliding dictionary + /// Common Zip compression method using a sliding dictionary /// of up to 32KB and secondary compression from Huffman/Shannon-Fano trees /// Deflated = 8, @@ -51,13 +50,22 @@ public enum CompressionMethod /// /// BZip2 compression. Not supported by #Zip. /// - BZip2 = 11, + BZip2 = 12, + + /// + /// LZMA compression. Not supported by #Zip. + /// + LZMA = 14, + + /// + /// PPMd compression. Not supported by #Zip. + /// + PPMd = 98, /// /// WinZip special for AES encryption, Now supported by #Zip. /// WinZipAES = 99, - } /// @@ -69,54 +77,67 @@ public enum EncryptionAlgorithm /// No encryption has been used. /// None = 0, + /// /// Encrypted using PKZIP 2.0 or 'classic' encryption. /// PkzipClassic = 1, + /// /// DES encryption has been used. /// Des = 0x6601, + /// /// RC2 encryption has been used for encryption. /// RC2 = 0x6602, + /// /// Triple DES encryption with 168 bit keys has been used for this entry. /// TripleDes168 = 0x6603, + /// /// Triple DES with 112 bit keys has been used for this entry. /// TripleDes112 = 0x6609, + /// /// AES 128 has been used for encryption. /// Aes128 = 0x660e, + /// /// AES 192 has been used for encryption. /// Aes192 = 0x660f, + /// /// AES 256 has been used for encryption. /// Aes256 = 0x6610, + /// /// RC2 corrected has been used for encryption. /// RC2Corrected = 0x6702, + /// /// Blowfish has been used for encryption. /// Blowfish = 0x6720, + /// /// Twofish has been used for encryption. /// Twofish = 0x6721, + /// /// RC4 has been used for encryption. /// RC4 = 0x6801, + /// /// An unknown algorithm has been used for encryption. /// @@ -133,52 +154,64 @@ public enum GeneralBitFlags /// Bit 0 if set indicates that the file is encrypted /// Encrypted = 0x0001, + /// /// Bits 1 and 2 - Two bits defining the compression method (only for Method 6 Imploding and 8,9 Deflating) /// Method = 0x0006, + /// - /// Bit 3 if set indicates a trailing data desciptor is appended to the entry data + /// Bit 3 if set indicates a trailing data descriptor is appended to the entry data /// Descriptor = 0x0008, + /// /// Bit 4 is reserved for use with method 8 for enhanced deflation /// ReservedPKware4 = 0x0010, + /// /// Bit 5 if set indicates the file contains Pkzip compressed patched data. /// Requires version 2.7 or greater. /// Patched = 0x0020, + /// /// Bit 6 if set indicates strong encryption has been used for this entry. /// StrongEncryption = 0x0040, + /// /// Bit 7 is currently unused /// Unused7 = 0x0080, + /// /// Bit 8 is currently unused /// Unused8 = 0x0100, + /// /// Bit 9 is currently unused /// Unused9 = 0x0200, + /// /// Bit 10 is currently unused /// Unused10 = 0x0400, + /// - /// Bit 11 if set indicates the filename and + /// Bit 11 if set indicates the filename and /// comment fields for this file must be encoded using UTF-8. /// UnicodeText = 0x0800, + /// /// Bit 12 is documented as being reserved by PKware for enhanced compression. /// EnhancedCompress = 0x1000, + /// /// Bit 13 if set indicates that values in the local header are masked to hide /// their actual values, and the central directory is encrypted. @@ -187,24 +220,43 @@ public enum GeneralBitFlags /// Used when encrypting the central directory contents. /// HeaderMasked = 0x2000, + /// /// Bit 14 is documented as being reserved for use by PKware /// ReservedPkware14 = 0x4000, + /// /// Bit 15 is documented as being reserved for use by PKware /// ReservedPkware15 = 0x8000 } + + /// + /// Helpers for + /// + public static class GeneralBitFlagsExtensions + { + /// + /// This is equivalent of in .NET Core, but since the .NET FW + /// version is really slow (due to un-/boxing and reflection) we use this wrapper. + /// + /// + /// + /// + public static bool Includes(this GeneralBitFlags flagData, GeneralBitFlags flag) => (flag & flagData) != 0; + } - #endregion + #endregion Enumerations /// /// This class contains constants used for Zip format files /// - public sealed class ZipConstants + [System.Diagnostics.CodeAnalysis.SuppressMessage("Naming", "CA1707:Identifiers should not contain underscores", Justification = "kept for backwards compatibility")] + public static class ZipConstants { #region Versions + /// /// The version made by field for entries in the central header when created by this library /// @@ -244,9 +296,16 @@ public sealed class ZipConstants /// The version required for Zip64 extensions (4.5 or higher) /// public const int VersionZip64 = 45; - #endregion + + /// + /// The version required for BZip2 compression (4.6 or higher) + /// + public const int VersionBZip2 = 46; + + #endregion Versions #region Header Sizes + /// /// Size of local entry header (excluding variable length fields at end) /// @@ -306,7 +365,13 @@ public sealed class ZipConstants /// [Obsolete("Use CryptoHeaderSize instead")] public const int CRYPTO_HEADER_SIZE = 12; - #endregion + + /// + /// The size of the Zip64 central directory locator. + /// + public const int Zip64EndOfCentralDirectoryLocatorSize = 20; + + #endregion Header Sizes #region Header Signatures @@ -399,12 +464,12 @@ public sealed class ZipConstants public const int ArchiveExtraDataSignature = 'P' | ('K' << 8) | (6 << 16) | (7 << 24); /// - /// Central header digitial signature + /// Central header digital signature /// public const int CentralHeaderDigitalSignature = 'P' | ('K' << 8) | (5 << 16) | (5 << 24); /// - /// Central header digitial signature + /// Central header digital signature /// [Obsolete("Use CentralHeaderDigitalSignaure instead")] public const int CENDIGITALSIG = 'P' | ('K' << 8) | (5 << 16) | (5 << 24); @@ -419,168 +484,31 @@ public sealed class ZipConstants /// [Obsolete("Use EndOfCentralDirectorySignature instead")] public const int ENDSIG = 'P' | ('K' << 8) | (5 << 16) | (6 << 24); - #endregion - /// - /// The original Zip specification (https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT) states - /// that file names should only be encoded with IBM Code Page 437 or UTF-8. - /// In practice, most zip apps use OEM or system encoding (typically cp437 on Windows). - /// Let's be good citizens and default to UTF-8 http://utf8everywhere.org/ - /// - static int defaultCodePage = Encoding.UTF8.CodePage; - - /// - /// Default encoding used for string conversion. 0 gives the default system OEM code page. - /// Using the default code page isnt the full solution neccessarily - /// there are many variable factors, codepage 850 is often a good choice for - /// European users, however be careful about compatability. - /// - public static int DefaultCodePage { - get { - return defaultCodePage; - } - set { - if ((value < 0) || (value > 65535) || - (value == 1) || (value == 2) || (value == 3) || (value == 42)) { - throw new ArgumentOutOfRangeException(nameof(value)); - } - - defaultCodePage = value; - } - } - - /// - /// Convert a portion of a byte array to a string. - /// - /// - /// Data to convert to string - /// - /// - /// Number of bytes to convert starting from index 0 - /// - /// - /// data[0]..data[count - 1] converted to a string - /// - public static string ConvertToString(byte[] data, int count) - { - if (data == null) { - return string.Empty; - } - - return Encoding.GetEncoding(DefaultCodePage).GetString(data, 0, count); - } - - /// - /// Convert a byte array to string - /// - /// - /// Byte array to convert - /// - /// - /// dataconverted to a string - /// - public static string ConvertToString(byte[] data) - { - if (data == null) { - return string.Empty; - } - return ConvertToString(data, data.Length); - } - - /// - /// Convert a byte array to string - /// - /// The applicable general purpose bits flags - /// - /// Byte array to convert - /// - /// The number of bytes to convert. - /// - /// dataconverted to a string - /// - public static string ConvertToStringExt(int flags, byte[] data, int count) - { - if (data == null) { - return string.Empty; - } - - if ((flags & (int)GeneralBitFlags.UnicodeText) != 0) { - return Encoding.UTF8.GetString(data, 0, count); - } else { - return ConvertToString(data, count); - } - } - - /// - /// Convert a byte array to string - /// - /// - /// Byte array to convert - /// - /// The applicable general purpose bits flags - /// - /// dataconverted to a string - /// - public static string ConvertToStringExt(int flags, byte[] data) - { - if (data == null) { - return string.Empty; - } - - if ((flags & (int)GeneralBitFlags.UnicodeText) != 0) { - return Encoding.UTF8.GetString(data, 0, data.Length); - } else { - return ConvertToString(data, data.Length); - } - } - - /// - /// Convert a string to a byte array - /// - /// - /// String to convert to an array - /// - /// Converted array - public static byte[] ConvertToArray(string str) - { - if (str == null) { - return new byte[0]; - } - - return Encoding.GetEncoding(DefaultCodePage).GetBytes(str); - } - - /// - /// Convert a string to a byte array - /// - /// The applicable general purpose bits flags - /// - /// String to convert to an array - /// - /// Converted array - public static byte[] ConvertToArray(int flags, string str) - { - if (str == null) { - return new byte[0]; - } - - if ((flags & (int)GeneralBitFlags.UnicodeText) != 0) { - return Encoding.UTF8.GetBytes(str); - } else { - return ConvertToArray(str); - } - } - - - /// - /// Initialise default instance of ZipConstants + #endregion Header Signatures + } + + /// + /// GeneralBitFlags helper extensions + /// + public static class GenericBitFlagsExtensions + { + /// + /// Efficiently check if any of the flags are set without enum un-/boxing /// - /// - /// Private to prevent instances being created. - /// - ZipConstants() - { - // Do nothing - } + /// + /// + /// Returns whether any of flags are set + public static bool HasAny(this GeneralBitFlags target, GeneralBitFlags flags) + => ((int)target & (int)flags) != 0; + + /// + /// Efficiently check if all the flags are set without enum un-/boxing + /// + /// + /// + /// Returns whether the flags are all set + public static bool HasAll(this GeneralBitFlags target, GeneralBitFlags flags) + => ((int)target & (int)flags) == (int)flags; } } diff --git a/src/ICSharpCode.SharpZipLib/Zip/ZipEncryptionMethod.cs b/src/ICSharpCode.SharpZipLib/Zip/ZipEncryptionMethod.cs new file mode 100644 index 000000000..ed51559cd --- /dev/null +++ b/src/ICSharpCode.SharpZipLib/Zip/ZipEncryptionMethod.cs @@ -0,0 +1,28 @@ +namespace ICSharpCode.SharpZipLib.Zip +{ + /// + /// The method of encrypting entries when creating zip archives. + /// + public enum ZipEncryptionMethod + { + /// + /// No encryption will be used. + /// + None, + + /// + /// Encrypt entries with ZipCrypto. + /// + ZipCrypto, + + /// + /// Encrypt entries with AES 128. + /// + AES128, + + /// + /// Encrypt entries with AES 256. + /// + AES256 + } +} diff --git a/src/ICSharpCode.SharpZipLib/Zip/ZipEntry.cs b/src/ICSharpCode.SharpZipLib/Zip/ZipEntry.cs index b0f1ecb3d..b0bf15821 100644 --- a/src/ICSharpCode.SharpZipLib/Zip/ZipEntry.cs +++ b/src/ICSharpCode.SharpZipLib/Zip/ZipEntry.cs @@ -1,5 +1,6 @@ using System; using System.IO; +using System.Text; namespace ICSharpCode.SharpZipLib.Zip { @@ -12,82 +13,102 @@ public enum HostSystemID /// Host system = MSDOS /// Msdos = 0, + /// /// Host system = Amiga /// Amiga = 1, + /// /// Host system = Open VMS /// OpenVms = 2, + /// /// Host system = Unix /// Unix = 3, + /// /// Host system = VMCms /// VMCms = 4, + /// /// Host system = Atari ST /// AtariST = 5, + /// /// Host system = OS2 /// OS2 = 6, + /// /// Host system = Macintosh /// Macintosh = 7, + /// /// Host system = ZSystem /// ZSystem = 8, + /// /// Host system = Cpm /// Cpm = 9, + /// /// Host system = Windows NT /// WindowsNT = 10, + /// /// Host system = MVS /// MVS = 11, + /// /// Host system = VSE /// Vse = 12, + /// /// Host system = Acorn RISC /// AcornRisc = 13, + /// /// Host system = VFAT /// Vfat = 14, + /// /// Host system = Alternate MVS /// AlternateMvs = 15, + /// /// Host system = BEOS /// BeOS = 16, + /// /// Host system = Tandem /// Tandem = 17, + /// /// Host system = OS400 /// OS400 = 18, + /// /// Host system = OSX /// OSX = 19, + /// /// Host system = WinZIP AES /// @@ -97,7 +118,7 @@ public enum HostSystemID /// /// This class represents an entry in a zip archive. This can be a file /// or a directory - /// ZipFile and ZipInputStream will give you instances of this class as + /// ZipFile and ZipInputStream will give you instances of this class as /// information about the members in an archive. ZipOutputStream /// uses an instance of this class when creating an entry in a Zip file. ///
@@ -106,7 +127,7 @@ public enum HostSystemID public class ZipEntry { [Flags] - enum Known : byte + private enum Known : byte { None = 0, Size = 0x01, @@ -117,6 +138,7 @@ enum Known : byte } #region Constructors + /// /// Creates a zip entry with the given name. /// @@ -129,7 +151,7 @@ enum Known : byte /// The name passed is null /// public ZipEntry(string name) - : this(name, 0, ZipConstants.VersionMadeBy, CompressionMethod.Deflated) + : this(name, 0, ZipConstants.VersionMadeBy, CompressionMethod.Deflated, true) { } @@ -138,7 +160,7 @@ public ZipEntry(string name) ///
/// /// The name for this entry. Can include directory components. - /// The convention for names is 'unix' style paths with no device names and + /// The convention for names is 'unix' style paths with no device names and /// path elements separated by '/' characters. This is not enforced see CleanName /// on how to ensure names are valid if this is desired. /// @@ -150,7 +172,7 @@ public ZipEntry(string name) /// internal ZipEntry(string name, int versionRequiredToExtract) : this(name, versionRequiredToExtract, ZipConstants.VersionMadeBy, - CompressionMethod.Deflated) + CompressionMethod.Deflated, true) { } @@ -161,6 +183,7 @@ internal ZipEntry(string name, int versionRequiredToExtract) /// Version and HostSystem Information /// Minimum required zip feature version required to extract this entry /// Compression method for this entry. + /// Whether the entry uses unicode for name and comment /// /// The name passed is null /// @@ -172,25 +195,30 @@ internal ZipEntry(string name, int versionRequiredToExtract) /// It is not generally useful, use the constructor specifying the name only. /// internal ZipEntry(string name, int versionRequiredToExtract, int madeByInfo, - CompressionMethod method) + CompressionMethod method, bool unicode) { - if (name == null) { + if (name == null) + { throw new ArgumentNullException(nameof(name)); } - if (name.Length > 0xffff) { + if (name.Length > 0xffff) + { throw new ArgumentException("Name is too long", nameof(name)); } - if ((versionRequiredToExtract != 0) && (versionRequiredToExtract < 10)) { + if ((versionRequiredToExtract != 0) && (versionRequiredToExtract < 10)) + { throw new ArgumentOutOfRangeException(nameof(versionRequiredToExtract)); } this.DateTime = DateTime.Now; - this.name = CleanName(name); + this.name = name; this.versionMadeBy = (ushort)madeByInfo; this.versionToExtract = (ushort)versionRequiredToExtract; this.method = method; + + IsUnicodeText = unicode; } /// @@ -202,7 +230,8 @@ internal ZipEntry(string name, int versionRequiredToExtract, int madeByInfo, [Obsolete("Use Clone instead")] public ZipEntry(ZipEntry entry) { - if (entry == null) { + if (entry == null) + { throw new ArgumentNullException(nameof(entry)); } @@ -211,7 +240,7 @@ public ZipEntry(ZipEntry entry) size = entry.size; compressedSize = entry.compressedSize; crc = entry.crc; - dosTime = entry.dosTime; + dateTime = entry.DateTime; method = entry.method; comment = entry.comment; versionToExtract = entry.versionToExtract; @@ -224,70 +253,49 @@ public ZipEntry(ZipEntry entry) forceZip64_ = entry.forceZip64_; - if (entry.extra != null) { + if (entry.extra != null) + { extra = new byte[entry.extra.Length]; Array.Copy(entry.extra, 0, extra, 0, entry.extra.Length); } } - #endregion + #endregion Constructors /// - /// Get a value indicating wether the entry has a CRC value available. + /// Get a value indicating whether the entry has a CRC value available. /// - public bool HasCrc { - get { - return (known & Known.Crc) != 0; - } - } + public bool HasCrc => (known & Known.Crc) != 0; /// /// Get/Set flag indicating if entry is encrypted. /// A simple helper routine to aid interpretation of flags /// /// This is an assistant that interprets the flags property. - public bool IsCrypted { - get { - return (flags & 1) != 0; - } - set { - if (value) { - flags |= 1; - } else { - flags &= ~1; - } - } + public bool IsCrypted + { + get => this.HasFlag(GeneralBitFlags.Encrypted); + set => this.SetFlag(GeneralBitFlags.Encrypted, value); } /// - /// Get / set a flag indicating wether entry name and comment text are + /// Get / set a flag indicating whether entry name and comment text are /// encoded in unicode UTF8. /// /// This is an assistant that interprets the flags property. - public bool IsUnicodeText { - get { - return (flags & (int)GeneralBitFlags.UnicodeText) != 0; - } - set { - if (value) { - flags |= (int)GeneralBitFlags.UnicodeText; - } else { - flags &= ~(int)GeneralBitFlags.UnicodeText; - } - } + public bool IsUnicodeText + { + get => this.HasFlag(GeneralBitFlags.UnicodeText); + set => this.SetFlag(GeneralBitFlags.UnicodeText, value); } /// /// Value used during password checking for PKZIP 2.0 / 'classic' encryption. /// - internal byte CryptoCheckValue { - get { - return cryptoCheckValue_; - } - - set { - cryptoCheckValue_ = value; - } + internal byte CryptoCheckValue + { + get => cryptoCheckValue_; + set => cryptoCheckValue_ = value; } /// @@ -321,55 +329,42 @@ internal byte CryptoCheckValue { /// /// /// - public int Flags { - get { - return flags; - } - set { - flags = value; - } + public int Flags + { + get => flags; + set => flags = value; } /// /// Get/Set index of this entry in Zip file /// /// This is only valid when the entry is part of a - public long ZipFileIndex { - get { - return zipFileIndex; - } - set { - zipFileIndex = value; - } + public long ZipFileIndex + { + get => zipFileIndex; + set => zipFileIndex = value; } /// /// Get/set offset for use in central header /// - public long Offset { - get { - return offset; - } - set { - offset = value; - } + public long Offset + { + get => offset; + set => offset = value; } /// /// Get/Set external file attributes as an integer. - /// The values of this are operating system dependant see + /// The values of this are operating system dependent see /// HostSystem for details /// - public int ExternalFileAttributes { - get { - if ((known & Known.ExternalAttributes) == 0) { - return -1; - } else { - return externalFileAttributes; - } - } + public int ExternalFileAttributes + { + get => (known & Known.ExternalAttributes) == 0 ? -1 : externalFileAttributes; - set { + set + { externalFileAttributes = value; known |= Known.ExternalAttributes; } @@ -377,24 +372,17 @@ public int ExternalFileAttributes { /// /// Get the version made by for this entry or zero if unknown. - /// The value / 10 indicates the major version number, and + /// The value / 10 indicates the major version number, and /// the value mod 10 is the minor version number /// - public int VersionMadeBy { - get { - return (versionMadeBy & 0xff); - } - } + public int VersionMadeBy => versionMadeBy & 0xff; /// /// Get a value indicating this entry is for a DOS/Windows system. /// - public bool IsDOSEntry { - get { - return ((HostSystem == (int)HostSystemID.Msdos) || - (HostSystem == (int)HostSystemID.WindowsNT)); - } - } + public bool IsDOSEntry + => (HostSystem == (int)HostSystemID.Msdos) + || (HostSystem == (int)HostSystemID.WindowsNT); /// /// Test the external attributes for this to @@ -402,12 +390,13 @@ public bool IsDOSEntry { /// and match the values /// /// The attributes to test. - /// Returns true if the external attributes are known to be DOS/Windows + /// Returns true if the external attributes are known to be DOS/Windows /// based and have the same attributes set as the value passed. - bool HasDosAttributes(int attributes) + private bool HasDosAttributes(int attributes) { bool result = false; - if ((known & Known.ExternalAttributes) != 0) { + if ((known & Known.ExternalAttributes) != 0) + { result |= (((HostSystem == (int)HostSystemID.Msdos) || (HostSystem == (int)HostSystemID.WindowsNT)) && (ExternalFileAttributes & attributes) == attributes); @@ -416,12 +405,12 @@ bool HasDosAttributes(int attributes) } /// - /// Gets the compatability information for the external file attribute + /// Gets the compatibility information for the external file attribute /// If the external file attributes are compatible with MS-DOS and can be read /// by PKZIP for DOS version 2.04g then this value will be zero. Otherwise the value /// will be non-zero and identify the host system on which the attributes are compatible. /// - /// + /// /// /// The values for this as defined in the Zip File format and by others are shown below. The values are somewhat /// misleading in some cases as they are not all used as shown. You should consult the relevant documentation @@ -452,20 +441,20 @@ bool HasDosAttributes(int attributes) /// remainder - unused /// /// - public int HostSystem { - get { - return (versionMadeBy >> 8) & 0xff; - } + public int HostSystem + { + get => (versionMadeBy >> 8) & 0xff; - set { - versionMadeBy &= 0xff; + set + { + versionMadeBy &= 0x00ff; versionMadeBy |= (ushort)((value & 0xff) << 8); } } /// /// Get minimum Zip feature version required to extract this entry - /// + /// /// /// Minimum features are defined as:
/// 1.0 - Default value
@@ -493,79 +482,74 @@ public int HostSystem { /// 6.3 - File is encrypted using Twofish
///
/// - public int Version { - get { + public int Version + { + get + { // Return recorded version if known. - if (versionToExtract != 0) { - return versionToExtract & 0x00ff; // Only lower order byte. High order is O/S file system. - } else { - int result = 10; - if (AESKeySize > 0) { - result = ZipConstants.VERSION_AES; // Ver 5.1 = AES - } else if (CentralHeaderRequiresZip64) { - result = ZipConstants.VersionZip64; - } else if (CompressionMethod.Deflated == method) { - result = 20; - } else if (IsDirectory == true) { - result = 20; - } else if (IsCrypted == true) { - result = 20; - } else if (HasDosAttributes(0x08)) { - result = 11; - } - return result; - } + if (versionToExtract != 0) + // Only lower order byte. High order is O/S file system. + return versionToExtract & 0x00ff; + + if (AESKeySize > 0) + // Ver 5.1 = AES + return ZipConstants.VERSION_AES; + + if (CompressionMethod.BZip2 == method) + return ZipConstants.VersionBZip2; + + if (CentralHeaderRequiresZip64) + return ZipConstants.VersionZip64; + + if (CompressionMethod.Deflated == method || IsDirectory || IsCrypted) + return 20; + + if (HasDosAttributes(0x08)) + return 11; + + return 10; } } /// /// Get a value indicating whether this entry can be decompressed by the library. /// - /// This is based on the and - /// wether the compression method is supported. - public bool CanDecompress { - get { - return (Version <= ZipConstants.VersionMadeBy) && - ((Version == 10) || - (Version == 11) || - (Version == 20) || - (Version == 45) || - (Version == 51)) && - IsCompressionMethodSupported(); - } - } + /// This is based on the and + /// whether the compression method is supported. + public bool CanDecompress + => Version <= ZipConstants.VersionMadeBy + && (Version == 10 || Version == 11 || Version == 20 || Version == 45 || Version == 46 || Version == 51) + && IsCompressionMethodSupported(); /// /// Force this entry to be recorded using Zip64 extensions. /// - public void ForceZip64() - { - forceZip64_ = true; - } + public void ForceZip64() => forceZip64_ = true; /// - /// Get a value indicating wether Zip64 extensions were forced. + /// Get a value indicating whether Zip64 extensions were forced. /// /// A value of true if Zip64 extensions have been forced on; false if not. - public bool IsZip64Forced() - { - return forceZip64_; - } + public bool IsZip64Forced() => forceZip64_; /// - /// Gets a value indicating if the entry requires Zip64 extensions + /// Gets a value indicating if the entry requires Zip64 extensions /// to store the full entry values. /// /// A value of true if a local header requires Zip64 extensions; false if not. - public bool LocalHeaderRequiresZip64 { - get { + public bool LocalHeaderRequiresZip64 + { + get + { bool result = forceZip64_; - if (!result) { + if (!result) + { ulong trueCompressedSize = compressedSize; - if ((versionToExtract == 0) && IsCrypted) { - trueCompressedSize += ZipConstants.CryptoHeaderSize; + if ((versionToExtract == 0) && IsCrypted) + { + trueCompressedSize += (ulong)this.EncryptionOverheadSize; } // TODO: A better estimation of the true limit based on compression overhead should be used @@ -580,13 +564,10 @@ public bool LocalHeaderRequiresZip64 { } /// - /// Get a value indicating wether the central directory entry requires Zip64 extensions to be stored. + /// Get a value indicating whether the central directory entry requires Zip64 extensions to be stored. /// - public bool CentralHeaderRequiresZip64 { - get { - return LocalHeaderRequiresZip64 || (offset >= uint.MaxValue); - } - } + public bool CentralHeaderRequiresZip64 + => LocalHeaderRequiresZip64 || (offset >= uint.MaxValue); /// /// Get/Set DosTime value. @@ -594,59 +575,33 @@ public bool CentralHeaderRequiresZip64 { /// /// The MS-DOS date format can only represent dates between 1/1/1980 and 12/31/2107. /// - public long DosTime { - get { - if ((known & Known.Time) == 0) { - return 0; - } else { - return dosTime; - } - } - - set { - unchecked { - dosTime = (uint)value; - } - - known |= Known.Time; - } - } - - /// - /// Gets/Sets the time of last modification of the entry. - /// - /// - /// The property is updated to match this as far as possible. - /// - public DateTime DateTime + public long DosTime { get { - uint sec = Math.Min(59, 2 * (dosTime & 0x1f)); - uint min = Math.Min(59, (dosTime >> 5) & 0x3f); - uint hrs = Math.Min(23, (dosTime >> 11) & 0x1f); - uint mon = Math.Max(1, Math.Min(12, ((dosTime >> 21) & 0xf))); - uint year = ((dosTime >> 25) & 0x7f) + 1980; - int day = Math.Max(1, Math.Min(DateTime.DaysInMonth((int)year, (int)mon), (int)((dosTime >> 16) & 0x1f))); - return new System.DateTime((int)year, (int)mon, day, (int)hrs, (int)min, (int)sec); - } + if ((known & Known.Time) == 0) + { + return 0; + } - set { - var year = (uint)value.Year; - var month = (uint)value.Month; - var day = (uint)value.Day; - var hour = (uint)value.Hour; - var minute = (uint)value.Minute; - var second = (uint)value.Second; + var year = (uint)DateTime.Year; + var month = (uint)DateTime.Month; + var day = (uint)DateTime.Day; + var hour = (uint)DateTime.Hour; + var minute = (uint)DateTime.Minute; + var second = (uint)DateTime.Second; - if (year < 1980) { + if (year < 1980) + { year = 1980; month = 1; day = 1; hour = 0; minute = 0; second = 0; - } else if (year > 2107) { + } + else if (year > 2107) + { year = 2107; month = 12; day = 31; @@ -655,12 +610,44 @@ public DateTime DateTime second = 59; } - DosTime = ((year - 1980) & 0x7f) << 25 | - (month << 21) | - (day << 16) | - (hour << 11) | - (minute << 5) | - (second >> 1); + return ((year - 1980) & 0x7f) << 25 | + (month << 21) | + (day << 16) | + (hour << 11) | + (minute << 5) | + (second >> 1); + } + + set + { + unchecked + { + var dosTime = (uint)value; + uint sec = Math.Min(59, 2 * (dosTime & 0x1f)); + uint min = Math.Min(59, (dosTime >> 5) & 0x3f); + uint hrs = Math.Min(23, (dosTime >> 11) & 0x1f); + uint mon = Math.Max(1, Math.Min(12, ((uint)(value >> 21) & 0xf))); + uint year = ((dosTime >> 25) & 0x7f) + 1980; + int day = Math.Max(1, Math.Min(DateTime.DaysInMonth((int)year, (int)mon), (int)((value >> 16) & 0x1f))); + DateTime = new DateTime((int)year, (int)mon, day, (int)hrs, (int)min, (int)sec, DateTimeKind.Unspecified); + } + } + } + + /// + /// Gets/Sets the time of last modification of the entry. + /// + /// + /// The property is updated to match this as far as possible. + /// + public DateTime DateTime + { + get => dateTime; + + set + { + dateTime = value; + known |= Known.Time; } } @@ -673,10 +660,10 @@ public DateTime DateTime /// Dos device names like C: should also be removed. /// See the class, or /// - public string Name { - get { - return name; - } + public string Name + { + get => name; + internal set => name = value; } /// @@ -686,14 +673,14 @@ public string Name { /// The size or -1 if unknown. /// /// Setting the size before adding an entry to an archive can help - /// avoid compatability problems with some archivers which dont understand Zip64 extensions. - public long Size { - get { - return (known & Known.Size) != 0 ? (long)size : -1L; - } - set { - this.size = (ulong)value; - this.known |= Known.Size; + /// avoid compatibility problems with some archivers which don't understand Zip64 extensions. + public long Size + { + get => (known & Known.Size) != 0 ? (long)size : -1L; + set + { + size = (ulong)value; + known |= Known.Size; } } @@ -703,13 +690,13 @@ public long Size { /// /// The compressed entry size or -1 if unknown. /// - public long CompressedSize { - get { - return (known & Known.CompressedSize) != 0 ? (long)compressedSize : -1L; - } - set { - this.compressedSize = (ulong)value; - this.known |= Known.CompressedSize; + public long CompressedSize + { + get => (known & Known.CompressedSize) != 0 ? (long)compressedSize : -1L; + set + { + compressedSize = (ulong)value; + known |= Known.CompressedSize; } } @@ -722,12 +709,13 @@ public long CompressedSize { /// /// The crc value or -1 if unknown. /// - public long Crc { - get { - return (known & Known.Crc) != 0 ? crc & 0xffffffffL : -1L; - } - set { - if (((ulong)crc & 0xffffffff00000000L) != 0) { + public long Crc + { + get => (known & Known.Crc) != 0 ? crc & 0xffffffffL : -1L; + set + { + if ((crc & 0xffffffff00000000L) != 0) + { throw new ArgumentOutOfRangeException(nameof(value)); } this.crc = (uint)value; @@ -736,24 +724,15 @@ public long Crc { } /// - /// Gets/Sets the compression method. Only Deflated and Stored are supported. + /// Gets/Sets the compression method. /// /// /// The compression method for this entry /// - /// - /// - public CompressionMethod CompressionMethod { - get { - return method; - } - - set { - if (!IsCompressionMethodSupported(value)) { - throw new NotSupportedException("Compression method not supported"); - } - this.method = value; - } + public CompressionMethod CompressionMethod + { + get => method; + set => method = value; } /// @@ -761,11 +740,8 @@ public CompressionMethod CompressionMethod { /// Returns same value as CompressionMethod except when AES encrypting, which /// places 99 in the method and places the real method in the extra data. /// - internal CompressionMethod CompressionMethodForHeader { - get { - return (AESKeySize > 0) ? CompressionMethod.WinZipAES : method; - } - } + internal CompressionMethod CompressionMethodForHeader + => (AESKeySize > 0) ? CompressionMethod.WinZipAES : method; /// /// Gets/Sets the extra data. @@ -776,19 +752,22 @@ internal CompressionMethod CompressionMethodForHeader { /// /// Extra data or null if not set. /// - public byte[] ExtraData { - - get { - // TODO: This is slightly safer but less efficient. Think about wether it should change. - // return (byte[]) extra.Clone(); - return extra; - } + public byte[] ExtraData + { + // TODO: This is slightly safer but less efficient. Think about whether it should change. + // return (byte[]) extra.Clone(); + get => extra; - set { - if (value == null) { + set + { + if (value == null) + { extra = null; - } else { - if (value.Length > 0xffff) { + } + else + { + if (value.Length > 0xffff) + { throw new System.ArgumentOutOfRangeException(nameof(value)); } @@ -798,38 +777,47 @@ public byte[] ExtraData { } } - /// /// For AES encrypted files returns or sets the number of bits of encryption (128, 192 or 256). /// When setting, only 0 (off), 128 or 256 is supported. /// - public int AESKeySize { - get { + public int AESKeySize + { + get + { // the strength (1 or 3) is in the entry header - switch (_aesEncryptionStrength) { + switch (_aesEncryptionStrength) + { case 0: return 0; // Not AES case 1: return 128; + case 2: return 192; // Not used by WinZip case 3: return 256; + default: throw new ZipException("Invalid AESEncryptionStrength " + _aesEncryptionStrength); } } - set { - switch (value) { + set + { + switch (value) + { case 0: _aesEncryptionStrength = 0; break; + case 128: _aesEncryptionStrength = 1; break; + case 256: _aesEncryptionStrength = 3; break; + default: throw new ZipException("AESKeySize must be 0, 128 or 256: " + value); } @@ -840,36 +828,38 @@ public int AESKeySize { /// AES Encryption strength for storage in extra data in entry header. /// 1 is 128 bit, 2 is 192 bit, 3 is 256 bit. /// - internal byte AESEncryptionStrength { - get { - return (byte)_aesEncryptionStrength; - } - } + internal byte AESEncryptionStrength => (byte)_aesEncryptionStrength; /// - /// Returns the length of the salt, in bytes + /// Returns the length of the salt, in bytes /// - internal int AESSaltLen { - get { - // Key size -> Salt length: 128 bits = 8 bytes, 192 bits = 12 bytes, 256 bits = 16 bytes. - return AESKeySize / 16; - } - } + /// Key size -> Salt length: 128 bits = 8 bytes, 192 bits = 12 bytes, 256 bits = 16 bytes. + internal int AESSaltLen => AESKeySize / 16; /// /// Number of extra bytes required to hold the AES Header fields (Salt, Pwd verify, AuthCode) /// - internal int AESOverheadSize { - get { - // File format: - // Bytes Content - // Variable Salt value - // 2 Password verification value - // Variable Encrypted file data - // 10 Authentication code - return 12 + AESSaltLen; - } - } + /// File format: + /// Bytes | Content + /// ---------+--------------------------- + /// Variable | Salt value + /// 2 | Password verification value + /// Variable | Encrypted file data + /// 10 | Authentication code + internal int AESOverheadSize => 12 + AESSaltLen; + + /// + /// Number of extra bytes required to hold the encryption header fields. + /// + internal int EncryptionOverheadSize => + !IsCrypted + // Entry is not encrypted - no overhead + ? 0 + : _aesEncryptionStrength == 0 + // Entry is encrypted using ZipCrypto + ? ZipConstants.CryptoHeaderSize + // Entry is encrypted using AES + : AESOverheadSize; /// /// Process extra data fields updating the entry based on the contents. @@ -881,72 +871,82 @@ internal void ProcessExtraData(bool localHeader) { var extraData = new ZipExtraData(this.extra); - if (extraData.Find(0x0001)) { + if (extraData.Find(0x0001)) + { // Version required to extract is ignored here as some archivers dont set it correctly // in theory it should be version 45 or higher // The recorded size will change but remember that this is zip64. forceZip64_ = true; - if (extraData.ValueLength < 4) { + if (extraData.ValueLength < 4) + { throw new ZipException("Extra data extended Zip64 information length is invalid"); } - // (localHeader ||) was deleted, because actually there is no specific difference with reading sizes between local header & central directory - // https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT + // (localHeader ||) was deleted, because actually there is no specific difference with reading sizes between local header & central directory + // https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT // ... // 4.4 Explanation of fields // ... // 4.4.8 compressed size: (4 bytes) // 4.4.9 uncompressed size: (4 bytes) - // + // // The size of the file compressed (4.4.8) and uncompressed, - // (4.4.9) respectively. When a decryption header is present it + // (4.4.9) respectively. When a decryption header is present it // will be placed in front of the file data and the value of the // compressed file size will include the bytes of the decryption - // header. If bit 3 of the general purpose bit flag is set, - // these fields are set to zero in the local header and the + // header. If bit 3 of the general purpose bit flag is set, + // these fields are set to zero in the local header and the // correct values are put in the data descriptor and // in the central directory. If an archive is in ZIP64 format // and the value in this field is 0xFFFFFFFF, the size will be - // in the corresponding 8 byte ZIP64 extended information + // in the corresponding 8 byte ZIP64 extended information // extra field. When encrypting the central directory, if the - // local header is not in ZIP64 format and general purpose bit - // flag 13 is set indicating masking, the value stored for the - // uncompressed size in the Local Header will be zero. - // - // Othewise there is problem with minizip implementation - if (size == uint.MaxValue) { + // local header is not in ZIP64 format and general purpose bit + // flag 13 is set indicating masking, the value stored for the + // uncompressed size in the Local Header will be zero. + // + // Otherwise there is problem with minizip implementation + if (size == uint.MaxValue) + { size = (ulong)extraData.ReadLong(); } - if (compressedSize == uint.MaxValue) { + if (compressedSize == uint.MaxValue) + { compressedSize = (ulong)extraData.ReadLong(); } - if (!localHeader && (offset == uint.MaxValue)) { + if (!localHeader && (offset == uint.MaxValue)) + { offset = extraData.ReadLong(); } // Disk number on which file starts is ignored - } else { + } + else + { if ( ((versionToExtract & 0xff) >= ZipConstants.VersionZip64) && ((size == uint.MaxValue) || (compressedSize == uint.MaxValue)) - ) { + ) + { throw new ZipException("Zip64 Extended information required but is missing."); } } - DateTime = GetDateTime(extraData); - if (method == CompressionMethod.WinZipAES) { + DateTime = GetDateTime(extraData) ?? DateTime; + if (method == CompressionMethod.WinZipAES) + { ProcessAESExtraData(extraData); } } - private DateTime GetDateTime(ZipExtraData extraData) { + private static DateTime? GetDateTime(ZipExtraData extraData) + { // Check for NT timestamp - // NOTE: Disable by default to match behavior of InfoZIP + // NOTE: Disable by default to match behavior of InfoZIP #if RESPECT_NT_TIMESTAMP NTTaggedData ntData = extraData.GetData(); if (ntData != null) @@ -955,34 +955,20 @@ private DateTime GetDateTime(ZipExtraData extraData) { // Check for Unix timestamp ExtendedUnixData unixData = extraData.GetData(); - if (unixData != null && - // Only apply modification time, but require all other values to be present - // This is done to match InfoZIP's behaviour - ((unixData.Include & ExtendedUnixData.Flags.ModificationTime) != 0) && - ((unixData.Include & ExtendedUnixData.Flags.AccessTime) != 0) && - ((unixData.Include & ExtendedUnixData.Flags.CreateTime) != 0)) + if (unixData != null && unixData.Include.HasFlag(ExtendedUnixData.Flags.ModificationTime)) return unixData.ModificationTime; - // Fall back to DOS time - uint sec = Math.Min(59, 2 * (dosTime & 0x1f)); - uint min = Math.Min(59, (dosTime >> 5) & 0x3f); - uint hrs = Math.Min(23, (dosTime >> 11) & 0x1f); - uint mon = Math.Max(1, Math.Min(12, ((dosTime >> 21) & 0xf))); - uint year = ((dosTime >> 25) & 0x7f) + 1980; - int day = Math.Max(1, Math.Min(DateTime.DaysInMonth((int)year, (int)mon), (int)((dosTime >> 16) & 0x1f))); - return new DateTime((int)year, (int)mon, day, (int)hrs, (int)min, (int)sec, DateTimeKind.Utc); + return null; } // For AES the method in the entry is 99, and the real compression method is in the extradata - // private void ProcessAESExtraData(ZipExtraData extraData) { - - if (extraData.Find(0x9901)) { - // Set version and flag for Zipfile.CreateAndInitDecryptionStream + if (extraData.Find(0x9901)) + { + // Set version for Zipfile.CreateAndInitDecryptionStream versionToExtract = ZipConstants.VERSION_AES; // Ver 5.1 = AES see "Version" getter - // Set StrongEncryption flag for ZipFile.CreateAndInitDecryptionStream - Flags = Flags | (int)GeneralBitFlags.StrongEncryption; + // // Unpack AES extra data field see http://www.winzip.com/aes_info.htm int length = extraData.ValueLength; // Data size currently 7 @@ -995,14 +981,15 @@ private void ProcessAESExtraData(ZipExtraData extraData) _aesVer = ver; _aesEncryptionStrength = encrStrength; method = (CompressionMethod)actualCompress; - } else + } + else throw new ZipException("AES Extra Data missing"); } /// /// Gets/Sets the entry comment. /// - /// + /// /// If comment is longer than 0xffff. /// /// @@ -1010,21 +997,22 @@ private void ProcessAESExtraData(ZipExtraData extraData) /// /// /// A comment is only available for entries when read via the class. - /// The class doesnt have the comment data available. + /// The class doesn't have the comment data available. /// - public string Comment { - get { - return comment; - } - set { + public string Comment + { + get => comment; + set + { // This test is strictly incorrect as the length is in characters // while the storage limit is in bytes. - // While the test is partially correct in that a comment of this length or greater + // While the test is partially correct in that a comment of this length or greater // is definitely invalid, shorter comments may also have an invalid length // where there are multi-byte characters // The full test is not possible here however as the code page to apply conversions with - // isnt available. - if ((value != null) && (value.Length > 0xffff)) { + // isn't available. + if ((value != null) && (value.Length > 0xffff)) + { throw new ArgumentOutOfRangeException(nameof(value), "cannot exceed 65535"); } @@ -1042,17 +1030,9 @@ public string Comment { /// Currently only dos/windows attributes are tested in this manner. /// The trailing slash convention should always be followed. /// - public bool IsDirectory { - get { - int nameLength = name.Length; - bool result = - ((nameLength > 0) && - ((name[nameLength - 1] == '/') || (name[nameLength - 1] == '\\'))) || - HasDosAttributes(16) - ; - return result; - } - } + public bool IsDirectory + => name.Length > 0 + && (name[name.Length - 1] == '/' || name[name.Length - 1] == '\\') || HasDosAttributes(16); /// /// Get a value of true if the entry appears to be a file; false otherwise @@ -1061,22 +1041,16 @@ public bool IsDirectory { /// This only takes account of DOS/Windows attributes. Other operating systems are ignored. /// For linux and others the result may be incorrect. /// - public bool IsFile { - get { - return !IsDirectory && !HasDosAttributes(8); - } - } + public bool IsFile => !IsDirectory && !HasDosAttributes(8); /// /// Test entry to see if data can be extracted. /// /// Returns true if data can be extracted for this entry; false otherwise. - public bool IsCompressionMethodSupported() - { - return IsCompressionMethodSupported(CompressionMethod); - } + public bool IsCompressionMethodSupported() => IsCompressionMethodSupported(CompressionMethod); #region ICloneable Members + /// /// Creates a copy of this zip entry. /// @@ -1086,7 +1060,8 @@ public object Clone() var result = (ZipEntry)this.MemberwiseClone(); // Ensure extra data is unique if it exists. - if (extra != null) { + if (extra != null) + { result.extra = new byte[extra.Length]; Array.Copy(extra, 0, result.extra, 0, extra.Length); } @@ -1094,16 +1069,13 @@ public object Clone() return result; } - #endregion + #endregion ICloneable Members /// /// Gets a string representation of this ZipEntry. /// /// A readable textual representation of this - public override string ToString() - { - return name; - } + public override string ToString() => name; /// /// Test a compression method to see if this library @@ -1111,17 +1083,15 @@ public override string ToString() /// /// The compression method to test. /// Returns true if the compression method is supported; false otherwise - public static bool IsCompressionMethodSupported(CompressionMethod method) - { - return - (method == CompressionMethod.Deflated) || - (method == CompressionMethod.Stored); - } + public static bool IsCompressionMethodSupported(CompressionMethod method) + => method == CompressionMethod.Deflated + || method == CompressionMethod.Stored + || method == CompressionMethod.BZip2; /// /// Cleans a name making it conform to Zip file conventions. /// Devices names ('c:\') and UNC share names ('\\server\share') are removed - /// and forward slashes ('\') are converted to back slashes ('/'). + /// and back slashes ('\') are converted to forward slashes ('/'). /// Names are made relative by trimming leading slashes which is compatible /// with the ZIP naming convention. /// @@ -1132,11 +1102,13 @@ public static bool IsCompressionMethodSupported(CompressionMethod method) /// public static string CleanName(string name) { - if (name == null) { + if (name == null) + { return string.Empty; } - if (Path.IsPathRooted(name)) { + if (Path.IsPathRooted(name)) + { // NOTE: // for UNC names... \\machine\share\zoom\beet.txt gives \zoom\beet.txt name = name.Substring(Path.GetPathRoot(name).Length); @@ -1144,39 +1116,42 @@ public static string CleanName(string name) name = name.Replace(@"\", "/"); - while ((name.Length > 0) && (name[0] == '/')) { + while ((name.Length > 0) && (name[0] == '/')) + { name = name.Remove(0, 1); } return name; } #region Instance Fields - Known known; - int externalFileAttributes = -1; // contains external attributes (O/S dependant) - ushort versionMadeBy; // Contains host system and version information - // only relevant for central header entries + private Known known; + private int externalFileAttributes = -1; // contains external attributes (O/S dependant) + + private ushort versionMadeBy; // Contains host system and version information + // only relevant for central header entries + + private string name; + private ulong size; + private ulong compressedSize; + private ushort versionToExtract; // Version required to extract (library handles <= 2.0) + private uint crc; + private DateTime dateTime; - string name; - ulong size; - ulong compressedSize; - ushort versionToExtract; // Version required to extract (library handles <= 2.0) - uint crc; - uint dosTime; + private CompressionMethod method = CompressionMethod.Deflated; + private byte[] extra; + private string comment; - CompressionMethod method = CompressionMethod.Deflated; - byte[] extra; - string comment; + private int flags; // general purpose bit flags - int flags; // general purpose bit flags + private long zipFileIndex = -1; // used by ZipFile + private long offset; // used by ZipFile and ZipOutputStream - long zipFileIndex = -1; // used by ZipFile - long offset; // used by ZipFile and ZipOutputStream + private bool forceZip64_; + private byte cryptoCheckValue_; + private int _aesVer; // Version number (2 = AE-2 ?). Assigned but not used. + private int _aesEncryptionStrength; // Encryption strength 1 = 128 2 = 192 3 = 256 - bool forceZip64_; - byte cryptoCheckValue_; - int _aesVer; // Version number (2 = AE-2 ?). Assigned but not used. - int _aesEncryptionStrength; // Encryption strength 1 = 128 2 = 192 3 = 256 - #endregion + #endregion Instance Fields } } diff --git a/src/ICSharpCode.SharpZipLib/Zip/ZipEntryExtensions.cs b/src/ICSharpCode.SharpZipLib/Zip/ZipEntryExtensions.cs new file mode 100644 index 000000000..927e94cfe --- /dev/null +++ b/src/ICSharpCode.SharpZipLib/Zip/ZipEntryExtensions.cs @@ -0,0 +1,32 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace ICSharpCode.SharpZipLib.Zip +{ + /// + /// General ZipEntry helper extensions + /// + public static class ZipEntryExtensions + { + /// + /// Efficiently check if a flag is set without enum un-/boxing + /// + /// + /// + /// Returns whether the flag was set + public static bool HasFlag(this ZipEntry entry, GeneralBitFlags flag) + => (entry.Flags & (int) flag) != 0; + + /// + /// Efficiently set a flag without enum un-/boxing + /// + /// + /// + /// Whether the passed flag should be set (1) or cleared (0) + public static void SetFlag(this ZipEntry entry, GeneralBitFlags flag, bool enabled = true) + => entry.Flags = enabled + ? entry.Flags | (int) flag + : entry.Flags & ~(int) flag; + } +} diff --git a/src/ICSharpCode.SharpZipLib/Zip/ZipEntryFactory.cs b/src/ICSharpCode.SharpZipLib/Zip/ZipEntryFactory.cs index 4fdbff8c4..ccbb26968 100644 --- a/src/ICSharpCode.SharpZipLib/Zip/ZipEntryFactory.cs +++ b/src/ICSharpCode.SharpZipLib/Zip/ZipEntryFactory.cs @@ -1,6 +1,6 @@ +using ICSharpCode.SharpZipLib.Core; using System; using System.IO; -using ICSharpCode.SharpZipLib.Core; namespace ICSharpCode.SharpZipLib.Zip { @@ -10,6 +10,7 @@ namespace ICSharpCode.SharpZipLib.Zip public class ZipEntryFactory : IEntryFactory { #region Enumerations + /// /// Defines the possible values to be used for the . /// @@ -19,39 +20,47 @@ public enum TimeSetting /// Use the recorded LastWriteTime value for the file. /// LastWriteTime, + /// /// Use the recorded LastWriteTimeUtc value for the file /// LastWriteTimeUtc, + /// /// Use the recorded CreateTime value for the file. /// CreateTime, + /// /// Use the recorded CreateTimeUtc value for the file. /// CreateTimeUtc, + /// /// Use the recorded LastAccessTime value for the file. /// LastAccessTime, + /// /// Use the recorded LastAccessTimeUtc value for the file. /// LastAccessTimeUtc, + /// /// Use a fixed value. /// /// The actual value used can be - /// specified via the constructor or + /// specified via the constructor or /// using the with the setting set /// to which will use the when this class was constructed. /// The property can also be used to set this value. Fixed, } - #endregion + + #endregion Enumerations #region Constructors + /// /// Initialise a new instance of the class. /// @@ -59,44 +68,49 @@ public enum TimeSetting public ZipEntryFactory() { nameTransform_ = new ZipNameTransform(); + isUnicodeText_ = true; } /// /// Initialise a new instance of using the specified /// /// The time setting to use when creating Zip entries. - public ZipEntryFactory(TimeSetting timeSetting) + public ZipEntryFactory(TimeSetting timeSetting) : this() { timeSetting_ = timeSetting; - nameTransform_ = new ZipNameTransform(); } /// /// Initialise a new instance of using the specified /// /// The time to set all values to. - public ZipEntryFactory(DateTime time) + public ZipEntryFactory(DateTime time) : this() { timeSetting_ = TimeSetting.Fixed; FixedDateTime = time; - nameTransform_ = new ZipNameTransform(); } - #endregion + #endregion Constructors #region Properties + /// /// Get / set the to be used when creating new values. /// /// /// Setting this property to null will cause a default name transform to be used. /// - public INameTransform NameTransform { + public INameTransform NameTransform + { get { return nameTransform_; } - set { - if (value == null) { + set + { + if (value == null) + { nameTransform_ = new ZipNameTransform(); - } else { + } + else + { nameTransform_ = value; } } @@ -105,7 +119,8 @@ public INameTransform NameTransform { /// /// Get / set the in use. /// - public TimeSetting Setting { + public TimeSetting Setting + { get { return timeSetting_; } set { timeSetting_ = value; } } @@ -113,10 +128,13 @@ public TimeSetting Setting { /// /// Get / set the value to use when is set to /// - public DateTime FixedDateTime { + public DateTime FixedDateTime + { get { return fixedDateTime_; } - set { - if (value.Year < 1970) { + set + { + if (value.Year < 1970) + { throw new ArgumentException("Value is too old to be valid", nameof(value)); } fixedDateTime_ = value; @@ -127,7 +145,8 @@ public DateTime FixedDateTime { /// A bitmask defining the attributes to be retrieved from the actual file. /// /// The default is to get all possible attributes from the actual file. - public int GetAttributes { + public int GetAttributes + { get { return getAttributes_; } set { getAttributes_ = value; } } @@ -136,20 +155,22 @@ public int GetAttributes { /// A bitmask defining which attributes are to be set on. /// /// By default no attributes are set on. - public int SetAttributes { + public int SetAttributes + { get { return setAttributes_; } set { setAttributes_ = value; } } /// - /// Get set a value indicating wether unidoce text should be set on. + /// Get set a value indicating whether unicode text should be set on. /// - public bool IsUnicodeText { + public bool IsUnicodeText + { get { return isUnicodeText_; } set { isUnicodeText_ = value; } } - #endregion + #endregion Properties #region IEntryFactory Members @@ -190,12 +211,15 @@ public ZipEntry MakeFileEntry(string fileName, string entryName, bool useFileSys bool useAttributes = (setAttributes_ != 0); FileInfo fi = null; - if (useFileSystem) { + if (useFileSystem) + { fi = new FileInfo(fileName); } - if ((fi != null) && fi.Exists) { - switch (timeSetting_) { + if ((fi != null) && fi.Exists) + { + switch (timeSetting_) + { case TimeSetting.CreateTime: result.DateTime = fi.CreationTime; break; @@ -232,13 +256,17 @@ public ZipEntry MakeFileEntry(string fileName, string entryName, bool useFileSys useAttributes = true; externalAttributes = ((int)fi.Attributes & getAttributes_); - } else { - if (timeSetting_ == TimeSetting.Fixed) { + } + else + { + if (timeSetting_ == TimeSetting.Fixed) + { result.DateTime = fixedDateTime_; } } - if (useAttributes) { + if (useAttributes) + { externalAttributes |= setAttributes_; result.ExternalFileAttributes = externalAttributes; } @@ -264,7 +292,6 @@ public ZipEntry MakeDirectoryEntry(string directoryName) /// Returns a new representing a directory. public ZipEntry MakeDirectoryEntry(string directoryName, bool useFileSystem) { - var result = new ZipEntry(nameTransform_.TransformDirectory(directoryName)); result.IsUnicodeText = isUnicodeText_; result.Size = 0; @@ -273,13 +300,15 @@ public ZipEntry MakeDirectoryEntry(string directoryName, bool useFileSystem) DirectoryInfo di = null; - if (useFileSystem) { + if (useFileSystem) + { di = new DirectoryInfo(directoryName); } - - if ((di != null) && di.Exists) { - switch (timeSetting_) { + if ((di != null) && di.Exists) + { + switch (timeSetting_) + { case TimeSetting.CreateTime: result.DateTime = di.CreationTime; break; @@ -313,8 +342,11 @@ public ZipEntry MakeDirectoryEntry(string directoryName, bool useFileSystem) } externalAttributes = ((int)di.Attributes & getAttributes_); - } else { - if (timeSetting_ == TimeSetting.Fixed) { + } + else + { + if (timeSetting_ == TimeSetting.Fixed) + { result.DateTime = fixedDateTime_; } } @@ -326,16 +358,18 @@ public ZipEntry MakeDirectoryEntry(string directoryName, bool useFileSystem) return result; } - #endregion + #endregion IEntryFactory Members #region Instance Fields - INameTransform nameTransform_; - DateTime fixedDateTime_ = DateTime.Now; - TimeSetting timeSetting_; - bool isUnicodeText_; - - int getAttributes_ = -1; - int setAttributes_; - #endregion + + private INameTransform nameTransform_; + private DateTime fixedDateTime_ = DateTime.Now; + private TimeSetting timeSetting_ = TimeSetting.LastWriteTime; + private bool isUnicodeText_; + + private int getAttributes_ = -1; + private int setAttributes_; + + #endregion Instance Fields } } diff --git a/src/ICSharpCode.SharpZipLib/Zip/ZipException.cs b/src/ICSharpCode.SharpZipLib/Zip/ZipException.cs index cd18e7016..ef8142b65 100644 --- a/src/ICSharpCode.SharpZipLib/Zip/ZipException.cs +++ b/src/ICSharpCode.SharpZipLib/Zip/ZipException.cs @@ -1,13 +1,14 @@ using System; +using System.Runtime.Serialization; namespace ICSharpCode.SharpZipLib.Zip { /// /// ZipException represents exceptions specific to Zip classes and code. /// + [Serializable] public class ZipException : SharpZipBaseException { - /// /// Initialise a new instance of . /// @@ -33,5 +34,21 @@ public ZipException(string message, Exception innerException) : base(message, innerException) { } + + /// + /// Initializes a new instance of the ZipException class with serialized data. + /// + /// + /// The System.Runtime.Serialization.SerializationInfo that holds the serialized + /// object data about the exception being thrown. + /// + /// + /// The System.Runtime.Serialization.StreamingContext that contains contextual information + /// about the source or destination. + /// + protected ZipException(SerializationInfo info, StreamingContext context) + : base(info, context) + { + } } } diff --git a/src/ICSharpCode.SharpZipLib/Zip/ZipExtraData.cs b/src/ICSharpCode.SharpZipLib/Zip/ZipExtraData.cs index 4582d9afd..cc2e74490 100644 --- a/src/ICSharpCode.SharpZipLib/Zip/ZipExtraData.cs +++ b/src/ICSharpCode.SharpZipLib/Zip/ZipExtraData.cs @@ -1,9 +1,10 @@ using System; using System.IO; +using ICSharpCode.SharpZipLib.Core; namespace ICSharpCode.SharpZipLib.Zip { - // TODO: Sort out wether tagged data is useful and what a good implementation might look like. + // TODO: Sort out whether tagged data is useful and what a good implementation might look like. // Its just a sketch of an idea at the moment. /// @@ -14,7 +15,7 @@ public interface ITaggedData /// /// Get the ID for this tagged data value. /// - short TagID { get; } + ushort TagID { get; } /// /// Set the contents of this instance from the data passed. @@ -40,7 +41,7 @@ public class RawTaggedData : ITaggedData /// Initialise a new instance. /// /// The tag ID. - public RawTaggedData(short tag) + public RawTaggedData(ushort tag) { _tag = tag; } @@ -50,7 +51,8 @@ public RawTaggedData(short tag) /// /// Get the ID for this tagged data value. /// - public short TagID { + public ushort TagID + { get { return _tag; } set { _tag = value; } } @@ -63,7 +65,8 @@ public short TagID { /// The number of bytes available. public void SetData(byte[] data, int offset, int count) { - if (data == null) { + if (data == null) + { throw new ArgumentNullException(nameof(data)); } @@ -80,25 +83,28 @@ public byte[] GetData() return _data; } - #endregion + #endregion ITaggedData Members /// /// Get /set the binary data representing this instance. /// /// The raw binary data representing this instance. - public byte[] Data { + public byte[] Data + { get { return _data; } set { _data = value; } } #region Instance Fields + /// /// The tag ID for this instance. /// - short _tag; + private ushort _tag; + + private byte[] _data; - byte[] _data; - #endregion + #endregion Instance Fields } /// @@ -133,7 +139,8 @@ public enum Flags : byte /// /// Get the ID /// - public short TagID { + public ushort TagID + { get { return 0x5455; } } @@ -146,15 +153,15 @@ public short TagID { public void SetData(byte[] data, int index, int count) { using (MemoryStream ms = new MemoryStream(data, index, count, false)) - using (ZipHelperStream helperStream = new ZipHelperStream(ms)) { + { // bit 0 if set, modification time is present // bit 1 if set, access time is present // bit 2 if set, creation time is present - _flags = (Flags)helperStream.ReadByte(); + _flags = (Flags)ms.ReadByte(); if (((_flags & Flags.ModificationTime) != 0)) { - int iTime = helperStream.ReadLEInt(); + int iTime = ms.ReadLEInt(); _modificationTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc) + new TimeSpan(0, 0, 0, iTime, 0); @@ -163,15 +170,17 @@ public void SetData(byte[] data, int index, int count) if (count <= 5) return; } - if ((_flags & Flags.AccessTime) != 0) { - int iTime = helperStream.ReadLEInt(); + if ((_flags & Flags.AccessTime) != 0) + { + int iTime = ms.ReadLEInt(); _lastAccessTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc) + new TimeSpan(0, 0, 0, iTime, 0); } - if ((_flags & Flags.CreateTime) != 0) { - int iTime = helperStream.ReadLEInt(); + if ((_flags & Flags.CreateTime) != 0) + { + int iTime = ms.ReadLEInt(); _createTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc) + new TimeSpan(0, 0, 0, iTime, 0); @@ -186,29 +195,31 @@ public void SetData(byte[] data, int index, int count) public byte[] GetData() { using (MemoryStream ms = new MemoryStream()) - using (ZipHelperStream helperStream = new ZipHelperStream(ms)) { - helperStream.IsStreamOwner = false; - helperStream.WriteByte((byte)_flags); // Flags - if ((_flags & Flags.ModificationTime) != 0) { + { + ms.WriteByte((byte)_flags); // Flags + if ((_flags & Flags.ModificationTime) != 0) + { TimeSpan span = _modificationTime - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc); var seconds = (int)span.TotalSeconds; - helperStream.WriteLEInt(seconds); + ms.WriteLEInt(seconds); } - if ((_flags & Flags.AccessTime) != 0) { + if ((_flags & Flags.AccessTime) != 0) + { TimeSpan span = _lastAccessTime - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc); var seconds = (int)span.TotalSeconds; - helperStream.WriteLEInt(seconds); + ms.WriteLEInt(seconds); } - if ((_flags & Flags.CreateTime) != 0) { + if ((_flags & Flags.CreateTime) != 0) + { TimeSpan span = _createTime - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc); var seconds = (int)span.TotalSeconds; - helperStream.WriteLEInt(seconds); + ms.WriteLEInt(seconds); } return ms.ToArray(); } } - #endregion + #endregion ITaggedData Members /// /// Test a value to see if is valid and can be represented here. @@ -232,10 +243,13 @@ public static bool IsValidValue(DateTime value) /// /// /// - public DateTime ModificationTime { + public DateTime ModificationTime + { get { return _modificationTime; } - set { - if (!IsValidValue(value)) { + set + { + if (!IsValidValue(value)) + { throw new ArgumentOutOfRangeException(nameof(value)); } @@ -249,10 +263,13 @@ public DateTime ModificationTime { /// /// /// - public DateTime AccessTime { + public DateTime AccessTime + { get { return _lastAccessTime; } - set { - if (!IsValidValue(value)) { + set + { + if (!IsValidValue(value)) + { throw new ArgumentOutOfRangeException(nameof(value)); } @@ -266,10 +283,13 @@ public DateTime AccessTime { /// /// /// - public DateTime CreateTime { + public DateTime CreateTime + { get { return _createTime; } - set { - if (!IsValidValue(value)) { + set + { + if (!IsValidValue(value)) + { throw new ArgumentOutOfRangeException(nameof(value)); } @@ -288,11 +308,13 @@ public Flags Include } #region Instance Fields - Flags _flags; - DateTime _modificationTime = new DateTime(1970, 1, 1); - DateTime _lastAccessTime = new DateTime(1970, 1, 1); - DateTime _createTime = new DateTime(1970, 1, 1); - #endregion + + private Flags _flags; + private DateTime _modificationTime = new DateTime(1970, 1, 1); + private DateTime _lastAccessTime = new DateTime(1970, 1, 1); + private DateTime _createTime = new DateTime(1970, 1, 1); + + #endregion Instance Fields } /// @@ -303,7 +325,8 @@ public class NTTaggedData : ITaggedData /// /// Get the ID for this tagged data value. /// - public short TagID { + public ushort TagID + { get { return 10; } } @@ -316,26 +339,31 @@ public short TagID { public void SetData(byte[] data, int index, int count) { using (MemoryStream ms = new MemoryStream(data, index, count, false)) - using (ZipHelperStream helperStream = new ZipHelperStream(ms)) { - helperStream.ReadLEInt(); // Reserved - while (helperStream.Position < helperStream.Length) { - int ntfsTag = helperStream.ReadLEShort(); - int ntfsLength = helperStream.ReadLEShort(); - if (ntfsTag == 1) { - if (ntfsLength >= 24) { - long lastModificationTicks = helperStream.ReadLELong(); + { + ms.ReadLEInt(); // Reserved + while (ms.Position < ms.Length) + { + int ntfsTag = ms.ReadLEShort(); + int ntfsLength = ms.ReadLEShort(); + if (ntfsTag == 1) + { + if (ntfsLength >= 24) + { + long lastModificationTicks = ms.ReadLELong(); _lastModificationTime = DateTime.FromFileTimeUtc(lastModificationTicks); - long lastAccessTicks = helperStream.ReadLELong(); + long lastAccessTicks = ms.ReadLELong(); _lastAccessTime = DateTime.FromFileTimeUtc(lastAccessTicks); - long createTimeTicks = helperStream.ReadLELong(); + long createTimeTicks = ms.ReadLELong(); _createTime = DateTime.FromFileTimeUtc(createTimeTicks); } break; - } else { + } + else + { // An unknown NTFS tag so simply skip it. - helperStream.Seek(ntfsLength, SeekOrigin.Current); + ms.Seek(ntfsLength, SeekOrigin.Current); } } } @@ -348,14 +376,13 @@ public void SetData(byte[] data, int index, int count) public byte[] GetData() { using (MemoryStream ms = new MemoryStream()) - using (ZipHelperStream helperStream = new ZipHelperStream(ms)) { - helperStream.IsStreamOwner = false; - helperStream.WriteLEInt(0); // Reserved - helperStream.WriteLEShort(1); // Tag - helperStream.WriteLEShort(24); // Length = 3 x 8. - helperStream.WriteLELong(_lastModificationTime.ToFileTimeUtc()); - helperStream.WriteLELong(_lastAccessTime.ToFileTimeUtc()); - helperStream.WriteLELong(_createTime.ToFileTimeUtc()); + { + ms.WriteLEInt(0); // Reserved + ms.WriteLEShort(1); // Tag + ms.WriteLEShort(24); // Length = 3 x 8. + ms.WriteLELong(_lastModificationTime.ToFileTimeUtc()); + ms.WriteLELong(_lastAccessTime.ToFileTimeUtc()); + ms.WriteLELong(_createTime.ToFileTimeUtc()); return ms.ToArray(); } } @@ -374,9 +401,12 @@ public byte[] GetData() public static bool IsValidValue(DateTime value) { bool result = true; - try { + try + { value.ToFileTimeUtc(); - } catch { + } + catch + { result = false; } return result; @@ -385,10 +415,13 @@ public static bool IsValidValue(DateTime value) /// /// Get/set the last modification time. /// - public DateTime LastModificationTime { + public DateTime LastModificationTime + { get { return _lastModificationTime; } - set { - if (!IsValidValue(value)) { + set + { + if (!IsValidValue(value)) + { throw new ArgumentOutOfRangeException(nameof(value)); } _lastModificationTime = value; @@ -398,10 +431,13 @@ public DateTime LastModificationTime { /// /// Get /set the create time /// - public DateTime CreateTime { + public DateTime CreateTime + { get { return _createTime; } - set { - if (!IsValidValue(value)) { + set + { + if (!IsValidValue(value)) + { throw new ArgumentOutOfRangeException(nameof(value)); } _createTime = value; @@ -411,10 +447,13 @@ public DateTime CreateTime { /// /// Get /set the last access time. /// - public DateTime LastAccessTime { + public DateTime LastAccessTime + { get { return _lastAccessTime; } - set { - if (!IsValidValue(value)) { + set + { + if (!IsValidValue(value)) + { throw new ArgumentOutOfRangeException(nameof(value)); } _lastAccessTime = value; @@ -422,16 +461,18 @@ public DateTime LastAccessTime { } #region Instance Fields - DateTime _lastAccessTime = DateTime.FromFileTimeUtc(0); - DateTime _lastModificationTime = DateTime.FromFileTimeUtc(0); - DateTime _createTime = DateTime.FromFileTimeUtc(0); - #endregion + + private DateTime _lastAccessTime = DateTime.FromFileTimeUtc(0); + private DateTime _lastModificationTime = DateTime.FromFileTimeUtc(0); + private DateTime _createTime = DateTime.FromFileTimeUtc(0); + + #endregion Instance Fields } /// /// A factory that creates tagged data instances. /// - interface ITaggedDataFactory + internal interface ITaggedDataFactory { /// /// Get data for a specific tag value. @@ -444,7 +485,7 @@ interface ITaggedDataFactory ITaggedData Create(short tag, byte[] data, int offset, int count); } - /// + /// /// /// A class to handle the extra data field for Zip entries /// @@ -458,6 +499,7 @@ interface ITaggedDataFactory sealed public class ZipExtraData : IDisposable { #region Constructors + /// /// Initialise a default instance. /// @@ -472,13 +514,17 @@ public ZipExtraData() /// The extra data. public ZipExtraData(byte[] data) { - if (data == null) { - _data = new byte[0]; - } else { + if (data == null) + { + _data = Empty.Array(); + } + else + { _data = data; } } - #endregion + + #endregion Constructors /// /// Get the raw extra data value @@ -486,7 +532,8 @@ public ZipExtraData(byte[] data) /// Returns the raw byte[] extra data this instance represents. public byte[] GetEntryData() { - if (Length > ushort.MaxValue) { + if (Length > ushort.MaxValue) + { throw new ZipException("Data exceeds maximum length"); } @@ -498,15 +545,17 @@ public byte[] GetEntryData() /// public void Clear() { - if ((_data == null) || (_data.Length != 0)) { - _data = new byte[0]; + if ((_data == null) || (_data.Length != 0)) + { + _data = Empty.Array(); } } /// /// Gets the current extra data length. /// - public int Length { + public int Length + { get { return _data.Length; } } @@ -518,7 +567,8 @@ public int Length { public Stream GetStreamForTag(int tag) { Stream result = null; - if (Find(tag)) { + if (Find(tag)) + { result = new MemoryStream(_data, _index, _readValueLength, false); } return result; @@ -545,7 +595,8 @@ public T GetData() /// Get the length of the last value found by /// /// This is only valid if has previously returned true. - public int ValueLength { + public int ValueLength + { get { return _readValueLength; } } @@ -555,17 +606,21 @@ public int ValueLength { /// This is only valid if has previously returned true. /// Initially the result will be the index of the first byte of actual data. The value is updated after calls to /// , and . - public int CurrentReadIndex { + public int CurrentReadIndex + { get { return _index; } } /// /// Get the number of bytes remaining to be read for the current value; /// - public int UnreadCount { - get { + public int UnreadCount + { + get + { if ((_readValueStart > _data.Length) || - (_readValueStart < 4)) { + (_readValueStart < 4)) + { throw new ZipException("Find must be called before calling a Read method"); } @@ -589,17 +644,20 @@ public bool Find(int headerID) // Trailing bytes that cant make up an entry (as there arent enough // bytes for a tag and length) are ignored! - while ((localTag != headerID) && (_index < _data.Length - 3)) { + while ((localTag != headerID) && (_index < _data.Length - 3)) + { localTag = ReadShortInternal(); localLength = ReadShortInternal(); - if (localTag != headerID) { + if (localTag != headerID) + { _index += localLength; } } bool result = (localTag == headerID) && ((_index + localLength) <= _data.Length); - if (result) { + if (result) + { _readValueStart = _index; _readValueLength = localLength; } @@ -613,7 +671,8 @@ public bool Find(int headerID) /// The value to add. public void AddEntry(ITaggedData taggedData) { - if (taggedData == null) { + if (taggedData == null) + { throw new ArgumentNullException(nameof(taggedData)); } AddEntry(taggedData.TagID, taggedData.GetData()); @@ -627,24 +686,28 @@ public void AddEntry(ITaggedData taggedData) /// If the ID already exists its contents are replaced. public void AddEntry(int headerID, byte[] fieldData) { - if ((headerID > ushort.MaxValue) || (headerID < 0)) { + if ((headerID > ushort.MaxValue) || (headerID < 0)) + { throw new ArgumentOutOfRangeException(nameof(headerID)); } int addLength = (fieldData == null) ? 0 : fieldData.Length; - if (addLength > ushort.MaxValue) { + if (addLength > ushort.MaxValue) + { throw new ArgumentOutOfRangeException(nameof(fieldData), "exceeds maximum length"); } // Test for new length before adjusting data. int newLength = _data.Length + addLength + 4; - if (Find(headerID)) { + if (Find(headerID)) + { newLength -= (ValueLength + 4); } - if (newLength > ushort.MaxValue) { + if (newLength > ushort.MaxValue) + { throw new ZipException("Data exceeds maximum length"); } @@ -656,7 +719,8 @@ public void AddEntry(int headerID, byte[] fieldData) _data = newData; SetShort(ref index, headerID); SetShort(ref index, addLength); - if (fieldData != null) { + if (fieldData != null) + { fieldData.CopyTo(newData, index); } } @@ -700,7 +764,8 @@ public void AddData(byte data) /// public void AddData(byte[] data) { - if (data == null) { + if (data == null) + { throw new ArgumentNullException(nameof(data)); } @@ -714,7 +779,8 @@ public void AddData(byte[] data) /// public void AddLeShort(int toAdd) { - unchecked { + unchecked + { _newEntry.WriteByte((byte)toAdd); _newEntry.WriteByte((byte)(toAdd >> 8)); } @@ -727,7 +793,8 @@ public void AddLeShort(int toAdd) /// public void AddLeInt(int toAdd) { - unchecked { + unchecked + { AddLeShort((short)toAdd); AddLeShort((short)(toAdd >> 16)); } @@ -740,7 +807,8 @@ public void AddLeInt(int toAdd) /// public void AddLeLong(long toAdd) { - unchecked { + unchecked + { AddLeInt((int)(toAdd & 0xffffffff)); AddLeInt((int)(toAdd >> 32)); } @@ -755,7 +823,8 @@ public bool Delete(int headerID) { bool result = false; - if (Find(headerID)) { + if (Find(headerID)) + { result = true; int trueStart = _readValueStart - 4; @@ -770,6 +839,7 @@ public bool Delete(int headerID) } #region Reading Support + /// /// Read a long in little endian form from the last found data value /// @@ -813,7 +883,8 @@ public int ReadShort() public int ReadByte() { int result = -1; - if ((_index < _data.Length) && (_readValueStart + _readValueLength > _index)) { + if ((_index < _data.Length) && (_readValueStart + _readValueLength > _index)) + { result = _data[_index]; _index += 1; } @@ -830,18 +901,21 @@ public void Skip(int amount) _index += amount; } - void ReadCheck(int length) + private void ReadCheck(int length) { if ((_readValueStart > _data.Length) || - (_readValueStart < 4)) { + (_readValueStart < 4)) + { throw new ZipException("Find must be called before calling a Read method"); } - if (_index > _readValueStart + _readValueLength - length) { + if (_index > _readValueStart + _readValueLength - length) + { throw new ZipException("End of extra data"); } - if (_index + length < 4) { + if (_index + length < 4) + { throw new ZipException("Cannot read before start of tag"); } } @@ -850,9 +924,10 @@ void ReadCheck(int length) /// Internal form of that reads data at any location. /// /// Returns the short value read. - int ReadShortInternal() + private int ReadShortInternal() { - if (_index > _data.Length - 2) { + if (_index > _data.Length - 2) + { throw new ZipException("End of extra data"); } @@ -861,14 +936,14 @@ int ReadShortInternal() return result; } - void SetShort(ref int index, int source) + private void SetShort(ref int index, int source) { _data[index] = (byte)source; _data[index + 1] = (byte)(source >> 8); index += 2; } - #endregion + #endregion Reading Support #region IDisposable Members @@ -877,20 +952,23 @@ void SetShort(ref int index, int source) /// public void Dispose() { - if (_newEntry != null) { + if (_newEntry != null) + { _newEntry.Dispose(); } } - #endregion + #endregion IDisposable Members #region Instance Fields - int _index; - int _readValueStart; - int _readValueLength; - MemoryStream _newEntry; - byte[] _data; - #endregion + private int _index; + private int _readValueStart; + private int _readValueLength; + + private MemoryStream _newEntry; + private byte[] _data; + + #endregion Instance Fields } } diff --git a/src/ICSharpCode.SharpZipLib/Zip/ZipFile.cs b/src/ICSharpCode.SharpZipLib/Zip/ZipFile.cs index b65895adc..7fc1c5592 100644 --- a/src/ICSharpCode.SharpZipLib/Zip/ZipFile.cs +++ b/src/ICSharpCode.SharpZipLib/Zip/ZipFile.cs @@ -1,25 +1,27 @@ +using ICSharpCode.SharpZipLib.Checksum; +using ICSharpCode.SharpZipLib.Core; +using ICSharpCode.SharpZipLib.Encryption; +using ICSharpCode.SharpZipLib.Zip.Compression; +using ICSharpCode.SharpZipLib.Zip.Compression.Streams; using System; using System.Collections; +using System.Collections.Generic; using System.IO; -using System.Text; -using System.Globalization; +using System.Linq; using System.Security.Cryptography; -using ICSharpCode.SharpZipLib.Encryption; -using ICSharpCode.SharpZipLib.Core; -using ICSharpCode.SharpZipLib.Checksum; -using ICSharpCode.SharpZipLib.Zip.Compression.Streams; -using ICSharpCode.SharpZipLib.Zip.Compression; -using System.Collections.Generic; +using System.Text; namespace ICSharpCode.SharpZipLib.Zip { #region Keys Required Event Args + /// /// Arguments used with KeysRequiredEvent /// public class KeysRequiredEventArgs : EventArgs { #region Constructors + /// /// Initialise a new instance of /// @@ -40,32 +42,41 @@ public KeysRequiredEventArgs(string name, byte[] keyValue) key = keyValue; } - #endregion + #endregion Constructors + #region Properties + /// /// Gets the name of the file for which keys are required. /// - public string FileName { + public string FileName + { get { return fileName; } } /// /// Gets or sets the key value /// - public byte[] Key { + public byte[] Key + { get { return key; } set { key = value; } } - #endregion + + #endregion Properties #region Instance Fields - string fileName; - byte[] key; - #endregion + + private readonly string fileName; + private byte[] key; + + #endregion Instance Fields } - #endregion + + #endregion Keys Required Event Args #region Test Definitions + /// /// The strategy to apply to testing. /// @@ -75,6 +86,7 @@ public enum TestStrategy /// Find the first error only. ///
FindFirstError, + /// /// Find all possible errors. /// @@ -119,12 +131,13 @@ public enum TestOperation } /// - /// Status returned returned by during testing. + /// Status returned by during testing. /// /// TestArchive public class TestStatus { #region Constructors + /// /// Initialise a new instance of /// @@ -133,54 +146,63 @@ public TestStatus(ZipFile file) { file_ = file; } - #endregion + + #endregion Constructors #region Properties /// /// Get the current in progress. /// - public TestOperation Operation { + public TestOperation Operation + { get { return operation_; } } /// /// Get the this status is applicable to. /// - public ZipFile File { + public ZipFile File + { get { return file_; } } /// /// Get the current/last entry tested. /// - public ZipEntry Entry { + public ZipEntry Entry + { get { return entry_; } } /// /// Get the number of errors detected so far. /// - public int ErrorCount { + public int ErrorCount + { get { return errorCount_; } } /// /// Get the number of bytes tested so far for the current entry. /// - public long BytesTested { + public long BytesTested + { get { return bytesTested_; } } /// - /// Get a value indicating wether the last entry test was valid. + /// Get a value indicating whether the last entry test was valid. /// - public bool EntryValid { + public bool EntryValid + { get { return entryValid_; } } - #endregion + + #endregion Properties #region Internal API + internal void AddError() { errorCount_++; @@ -203,16 +225,19 @@ internal void SetBytesTested(long value) { bytesTested_ = value; } - #endregion + + #endregion Internal API #region Instance Fields - ZipFile file_; - ZipEntry entry_; - bool entryValid_; - int errorCount_; - long bytesTested_; - TestOperation operation_; - #endregion + + private readonly ZipFile file_; + private ZipEntry entry_; + private bool entryValid_; + private int errorCount_; + private long bytesTested_; + private TestOperation operation_; + + #endregion Instance Fields } /// @@ -221,9 +246,11 @@ internal void SetBytesTested(long value) /// If the message is non-null an error has occured. If the message is null /// the operation as found in status has started. public delegate void ZipTestResultHandler(TestStatus status, string message); - #endregion + + #endregion Test Definitions #region Update Definitions + /// /// The possible ways of applying updates to an archive. /// @@ -233,21 +260,24 @@ public enum FileUpdateMode /// Perform all updates on temporary files ensuring that the original file is saved. /// Safe, + /// /// Update the archive directly, which is faster but less safe. /// Direct, } - #endregion + + #endregion Update Definitions #region ZipFile Class + /// /// This class represents a Zip archive. You can ask for the contained /// entries, or get an input stream for a file entry. The entry is /// automatically decompressed. - /// + /// /// You can also update the archive adding or deleting entries. - /// + /// /// This class is thread safe for input: You can open input streams for arbitrary /// entries in different threads. ///
@@ -259,9 +289,9 @@ public enum FileUpdateMode /// using System.Text; /// using System.Collections; /// using System.IO; - /// + /// /// using ICSharpCode.SharpZipLib.Zip; - /// + /// /// class MainClass /// { /// static public void Main(string[] args) @@ -284,12 +314,12 @@ public enum FileUpdateMode /// } /// /// - public class ZipFile : IEnumerable, IDisposable + public class ZipFile : IEnumerable, IDisposable { #region KeyHandling /// - /// Delegate for handling keys/password setting during compresion/decompression. + /// Delegate for handling keys/password setting during compression/decompression. /// public delegate void KeysRequiredEventHandler( object sender, @@ -305,9 +335,10 @@ KeysRequiredEventArgs e /// Handles getting of encryption keys when required. ///
/// The file for which encryption keys are required. - void OnKeysRequired(string fileName) + private void OnKeysRequired(string fileName) { - if (KeysRequired != null) { + if (KeysRequired != null) + { var krea = new KeysRequiredEventArgs(fileName, key); KeysRequired(this, krea); key = krea.Key; @@ -317,7 +348,8 @@ void OnKeysRequired(string fileName) /// /// Get/set the encryption key value. /// - byte[] Key { + private byte[] Key + { get { return key; } set { key = value; } } @@ -326,26 +358,35 @@ byte[] Key { /// Password to be used for encrypting/decrypting files. /// /// Set to null if no password is required. - public string Password { - set { - if (string.IsNullOrEmpty(value)) { + public string Password + { + set + { + if (string.IsNullOrEmpty(value)) + { key = null; - } else { - rawPassword_ = value; - key = PkzipClassic.GenerateKeys(ZipConstants.ConvertToArray(value)); } + else + { + key = PkzipClassic.GenerateKeys(ZipCryptoEncoding.GetBytes(value)); + } + + rawPassword_ = value; } } /// - /// Get a value indicating wether encryption keys are currently available. + /// Get a value indicating whether encryption keys are currently available. /// - bool HaveKeys { + private bool HaveKeys + { get { return key != null; } } - #endregion + + #endregion KeyHandling #region Constructors + /// /// Opens a Zip file with the given name for reading. /// @@ -357,20 +398,42 @@ bool HaveKeys { /// /// The file doesn't contain a valid zip archive. /// - public ZipFile(string name) - { - if (name == null) { - throw new ArgumentNullException(nameof(name)); - } + public ZipFile(string name) : + this(name, null) + { - name_ = name; + } + + /// + /// Opens a Zip file with the given name for reading. + /// + /// The name of the file to open. + /// + /// The argument supplied is null. + /// + /// An i/o error occurs + /// + /// + /// The file doesn't contain a valid zip archive. + /// + public ZipFile(string name, StringCodec stringCodec) + { + name_ = name ?? throw new ArgumentNullException(nameof(name)); baseStream_ = File.Open(name, FileMode.Open, FileAccess.Read, FileShare.Read); isStreamOwner = true; - try { + if (stringCodec != null) + { + _stringCodec = stringCodec; + } + + try + { ReadEntries(); - } catch { + } + catch + { DisposeInternal(true); throw; } @@ -387,23 +450,46 @@ public ZipFile(string name) /// /// The file doesn't contain a valid zip archive. /// - public ZipFile(FileStream file) + public ZipFile(FileStream file) : + this(file, false) + { + + } + + /// + /// Opens a Zip file reading the given . + /// + /// The to read archive data from. + /// true to leave the file open when the ZipFile is disposed, false to dispose of it + /// The supplied argument is null. + /// + /// An i/o error occurs. + /// + /// + /// The file doesn't contain a valid zip archive. + /// + public ZipFile(FileStream file, bool leaveOpen) { - if (file == null) { + if (file == null) + { throw new ArgumentNullException(nameof(file)); } - if (!file.CanSeek) { + if (!file.CanSeek) + { throw new ArgumentException("Stream is not seekable", nameof(file)); } baseStream_ = file; name_ = file.Name; - isStreamOwner = true; + isStreamOwner = !leaveOpen; - try { + try + { ReadEntries(); - } catch { + } + catch + { DisposeInternal(true); throw; } @@ -425,28 +511,88 @@ public ZipFile(FileStream file) /// /// The stream argument is null. /// - public ZipFile(Stream stream) + public ZipFile(Stream stream) : + this(stream, false) + { + + } + + /// + /// Opens a Zip file reading the given . + /// + /// The to read archive data from. + /// true to leave the stream open when the ZipFile is disposed, false to dispose of it + /// + /// An i/o error occurs + /// + /// + /// The stream doesn't contain a valid zip archive.
+ ///
+ /// + /// The stream doesnt support seeking. + /// + /// + /// The stream argument is null. + /// + public ZipFile(Stream stream, bool leaveOpen) : + this(stream, leaveOpen, null) + { + + } + + /// + /// Opens a Zip file reading the given . + /// + /// The to read archive data from. + /// true to leave the stream open when the ZipFile is disposed, false to dispose of it + /// + /// + /// An i/o error occurs + /// + /// + /// The stream doesn't contain a valid zip archive.
+ ///
+ /// + /// The stream doesnt support seeking. + /// + /// + /// The stream argument is null. + /// + public ZipFile(Stream stream, bool leaveOpen, StringCodec stringCodec) { - if (stream == null) { + if (stream == null) + { throw new ArgumentNullException(nameof(stream)); } - if (!stream.CanSeek) { + if (!stream.CanSeek) + { throw new ArgumentException("Stream is not seekable", nameof(stream)); } baseStream_ = stream; - isStreamOwner = true; + isStreamOwner = !leaveOpen; - if (baseStream_.Length > 0) { - try { + if (stringCodec != null) + { + _stringCodec = stringCodec; + } + + if (baseStream_.Length > 0) + { + try + { ReadEntries(); - } catch { + } + catch + { DisposeInternal(true); throw; } - } else { - entries_ = new ZipEntry[0]; + } + else + { + entries_ = Empty.Array(); isNewArchive_ = true; } } @@ -456,13 +602,14 @@ public ZipFile(Stream stream) /// internal ZipFile() { - entries_ = new ZipEntry[0]; + entries_ = Empty.Array(); isNewArchive_ = true; } - #endregion + #endregion Constructors #region Destructors and Closing + /// /// Finalize this instance. /// @@ -484,9 +631,10 @@ public void Close() GC.SuppressFinalize(this); } - #endregion + #endregion Destructors and Closing #region Creators + /// /// Create a new whose data will be stored in a file. /// @@ -495,17 +643,19 @@ public void Close() /// is null public static ZipFile Create(string fileName) { - if (fileName == null) { + if (fileName == null) + { throw new ArgumentNullException(nameof(fileName)); } FileStream fs = File.Create(fileName); - var result = new ZipFile(); - result.name_ = fileName; - result.baseStream_ = fs; - result.isStreamOwner = true; - return result; + return new ZipFile + { + name_ = fileName, + baseStream_ = fs, + isStreamOwner = true + }; } /// @@ -517,26 +667,32 @@ public static ZipFile Create(string fileName) /// doesnt support writing. public static ZipFile Create(Stream outStream) { - if (outStream == null) { + if (outStream == null) + { throw new ArgumentNullException(nameof(outStream)); } - if (!outStream.CanWrite) { + if (!outStream.CanWrite) + { throw new ArgumentException("Stream is not writeable", nameof(outStream)); } - if (!outStream.CanSeek) { + if (!outStream.CanSeek) + { throw new ArgumentException("Stream is not seekable", nameof(outStream)); } - var result = new ZipFile(); - result.baseStream_ = outStream; + var result = new ZipFile + { + baseStream_ = outStream + }; return result; } - #endregion + #endregion Creators #region Properties + /// /// Get/set a flag indicating if the underlying stream is owned by the ZipFile instance. /// If the flag is true then the stream will be closed when Close is called. @@ -544,16 +700,18 @@ public static ZipFile Create(Stream outStream) /// /// The default value is true in all cases. /// - public bool IsStreamOwner { + public bool IsStreamOwner + { get { return isStreamOwner; } set { isStreamOwner = value; } } /// - /// Get a value indicating wether + /// Get a value indicating whether /// this archive is embedded in another file or not. /// - public bool IsEmbeddedArchive { + public bool IsEmbeddedArchive + { // Not strictly correct in all circumstances currently get { return offsetOfFirstEntry > 0; } } @@ -561,21 +719,24 @@ public bool IsEmbeddedArchive { /// /// Get a value indicating that this archive is a new one. /// - public bool IsNewArchive { + public bool IsNewArchive + { get { return isNewArchive_; } } /// /// Gets the comment for the zip file. /// - public string ZipFileComment { + public string ZipFileComment + { get { return comment_; } } /// /// Gets the name of this zip file. /// - public string Name { + public string Name + { get { return name_; } } @@ -586,8 +747,10 @@ public string Name { /// The Zip file has been closed. /// [Obsolete("Use the Count property instead")] - public int Size { - get { + public int Size + { + get + { return entries_.Length; } } @@ -595,8 +758,10 @@ public int Size { /// /// Get the number of entries contained in this . /// - public long Count { - get { + public long Count + { + get + { return entries_.Length; } } @@ -605,15 +770,51 @@ public long Count { /// Indexer property for ZipEntries /// [System.Runtime.CompilerServices.IndexerNameAttribute("EntryByIndex")] - public ZipEntry this[int index] { - get { + public ZipEntry this[int index] + { + get + { return (ZipEntry)entries_[index].Clone(); } } - #endregion + + /// + public Encoding ZipCryptoEncoding + { + get => _stringCodec.ZipCryptoEncoding; + set => _stringCodec = _stringCodec.WithZipCryptoEncoding(value); + } + + /// + public StringCodec StringCodec + { + set { + _stringCodec = value; + if (!isNewArchive_) + { + // Since the string codec was changed + ReadEntries(); + } + } + } + + #endregion Properties #region Input Handling + + /// + /// Gets an enumerator for the Zip entries in this Zip file. + /// + /// Returns an for this archive. + /// + /// The Zip file has been closed. + /// + IEnumerator IEnumerable.GetEnumerator() + { + return GetEnumerator(); + } + /// /// Gets an enumerator for the Zip entries in this Zip file. /// @@ -621,9 +822,22 @@ public ZipEntry this[int index] { /// /// The Zip file has been closed. /// - public IEnumerator GetEnumerator() + IEnumerator IEnumerable.GetEnumerator() { - if (isDisposed_) { + return GetEnumerator(); + } + + /// + /// Gets an enumerator for the Zip entries in this Zip file. + /// + /// Returns an for this archive. + /// + /// The Zip file has been closed. + /// + public ZipEntryEnumerator GetEnumerator() + { + if (isDisposed_) + { throw new ObjectDisposedException("ZipFile"); } @@ -641,13 +855,16 @@ public IEnumerator GetEnumerator() /// public int FindEntry(string name, bool ignoreCase) { - if (isDisposed_) { + if (isDisposed_) + { throw new ObjectDisposedException("ZipFile"); } // TODO: This will be slow as the next ice age for huge archives! - for (int i = 0; i < entries_.Length; i++) { - if (string.Compare(name, entries_[i].Name, ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal) == 0) { + for (int i = 0; i < entries_.Length; i++) + { + if (string.Compare(name, entries_[i].Name, ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal) == 0) + { return i; } } @@ -669,7 +886,8 @@ public int FindEntry(string name, bool ignoreCase) /// public ZipEntry GetEntry(string name) { - if (isDisposed_) { + if (isDisposed_) + { throw new ObjectDisposedException("ZipFile"); } @@ -694,18 +912,22 @@ public ZipEntry GetEntry(string name) /// public Stream GetInputStream(ZipEntry entry) { - if (entry == null) { + if (entry == null) + { throw new ArgumentNullException(nameof(entry)); } - if (isDisposed_) { + if (isDisposed_) + { throw new ObjectDisposedException("ZipFile"); } long index = entry.ZipFileIndex; - if ((index < 0) || (index >= entries_.Length) || (entries_[index].Name != entry.Name)) { + if ((index < 0) || (index >= entries_.Length) || (entries_[index].Name != entry.Name)) + { index = FindEntry(entry.Name, true); - if (index < 0) { + if (index < 0) + { throw new ZipException("Entry cannot be found"); } } @@ -730,7 +952,8 @@ public Stream GetInputStream(ZipEntry entry) /// public Stream GetInputStream(long entryIndex) { - if (isDisposed_) { + if (isDisposed_) + { throw new ObjectDisposedException("ZipFile"); } @@ -738,21 +961,28 @@ public Stream GetInputStream(long entryIndex) CompressionMethod method = entries_[entryIndex].CompressionMethod; Stream result = new PartialInputStream(this, start, entries_[entryIndex].CompressedSize); - if (entries_[entryIndex].IsCrypted == true) { + if (entries_[entryIndex].IsCrypted == true) + { result = CreateAndInitDecryptionStream(result, entries_[entryIndex]); - if (result == null) { + if (result == null) + { throw new ZipException("Unable to decrypt this entry"); } } - switch (method) { + switch (method) + { case CompressionMethod.Stored: // read as is. break; case CompressionMethod.Deflated: // No need to worry about ownership and closing as underlying stream close does nothing. - result = new InflaterInputStream(result, new Inflater(true)); + result = new InflaterInputStream(result, InflaterPool.Instance.Rent(true)); + break; + + case CompressionMethod.BZip2: + result = new BZip2.BZip2InputStream(result); break; default: @@ -762,9 +992,10 @@ public Stream GetInputStream(long entryIndex) return result; } - #endregion + #endregion Input Handling #region Archive Testing + /// /// Test an archive for integrity/validity /// @@ -786,60 +1017,72 @@ public bool TestArchive(bool testData) /// The object has already been closed. public bool TestArchive(bool testData, TestStrategy strategy, ZipTestResultHandler resultHandler) { - if (isDisposed_) { + if (isDisposed_) + { throw new ObjectDisposedException("ZipFile"); } var status = new TestStatus(this); - if (resultHandler != null) { - resultHandler(status, null); - } + resultHandler?.Invoke(status, null); HeaderTest test = testData ? (HeaderTest.Header | HeaderTest.Extract) : HeaderTest.Header; bool testing = true; - try { + try + { int entryIndex = 0; - while (testing && (entryIndex < Count)) { - if (resultHandler != null) { + while (testing && (entryIndex < Count)) + { + if (resultHandler != null) + { status.SetEntry(this[entryIndex]); status.SetOperation(TestOperation.EntryHeader); resultHandler(status, null); } - try { + try + { TestLocalHeader(this[entryIndex], test); - } catch (ZipException ex) { + } + catch (ZipException ex) + { status.AddError(); - if (resultHandler != null) { - resultHandler(status, - string.Format("Exception during test - '{0}'", ex.Message)); - } + resultHandler?.Invoke(status, $"Exception during test - '{ex.Message}'"); testing &= strategy != TestStrategy.FindFirstError; } - if (testing && testData && this[entryIndex].IsFile) { - if (resultHandler != null) { + if (testing && testData && this[entryIndex].IsFile) + { + // Don't check CRC for AES encrypted archives + var checkCRC = this[entryIndex].AESKeySize == 0; + + if (resultHandler != null) + { status.SetOperation(TestOperation.EntryData); resultHandler(status, null); } var crc = new Crc32(); - using (Stream entryStream = this.GetInputStream(this[entryIndex])) { - + using (Stream entryStream = this.GetInputStream(this[entryIndex])) + { byte[] buffer = new byte[4096]; long totalBytes = 0; int bytesRead; - while ((bytesRead = entryStream.Read(buffer, 0, buffer.Length)) > 0) { - crc.Update(buffer, 0, bytesRead); + while ((bytesRead = entryStream.Read(buffer, 0, buffer.Length)) > 0) + { + if (checkCRC) + { + crc.Update(new ArraySegment(buffer, 0, bytesRead)); + } - if (resultHandler != null) { + if (resultHandler != null) + { totalBytes += bytesRead; status.SetBytesTested(totalBytes); resultHandler(status, null); @@ -847,35 +1090,41 @@ public bool TestArchive(bool testData, TestStrategy strategy, ZipTestResultHandl } } - if (this[entryIndex].Crc != crc.Value) { + if (checkCRC && this[entryIndex].Crc != crc.Value) + { status.AddError(); - if (resultHandler != null) { - resultHandler(status, "CRC mismatch"); - } + resultHandler?.Invoke(status, "CRC mismatch"); testing &= strategy != TestStrategy.FindFirstError; } - if ((this[entryIndex].Flags & (int)GeneralBitFlags.Descriptor) != 0) { - var helper = new ZipHelperStream(baseStream_); + if ((this[entryIndex].Flags & (int)GeneralBitFlags.Descriptor) != 0) + { var data = new DescriptorData(); - helper.ReadDataDescriptor(this[entryIndex].LocalHeaderRequiresZip64, data); - if (this[entryIndex].Crc != data.Crc) { + ZipFormat.ReadDataDescriptor(baseStream_, this[entryIndex].LocalHeaderRequiresZip64, data); + if (checkCRC && this[entryIndex].Crc != data.Crc) + { status.AddError(); + resultHandler?.Invoke(status, "Descriptor CRC mismatch"); } - if (this[entryIndex].CompressedSize != data.CompressedSize) { + if (this[entryIndex].CompressedSize != data.CompressedSize) + { status.AddError(); + resultHandler?.Invoke(status, "Descriptor compressed size mismatch"); } - if (this[entryIndex].Size != data.Size) { + if (this[entryIndex].Size != data.Size) + { status.AddError(); + resultHandler?.Invoke(status, "Descriptor size mismatch"); } } } - if (resultHandler != null) { + if (resultHandler != null) + { status.SetOperation(TestOperation.EntryComplete); resultHandler(status, null); } @@ -883,22 +1132,24 @@ public bool TestArchive(bool testData, TestStrategy strategy, ZipTestResultHandl entryIndex += 1; } - if (resultHandler != null) { + if (resultHandler != null) + { status.SetOperation(TestOperation.MiscellaneousTests); resultHandler(status, null); } // TODO: the 'Corrina Johns' test where local headers are missing from // the central directory. They are therefore invisible to many archivers. - } catch (Exception ex) { + } + catch (Exception ex) + { status.AddError(); - if (resultHandler != null) { - resultHandler(status, string.Format("Exception during test - '{0}'", ex.Message)); - } + resultHandler?.Invoke(status, $"Exception during test - '{ex.Message}'"); } - if (resultHandler != null) { + if (resultHandler != null) + { status.SetOperation(TestOperation.Complete); status.SetEntry(null); resultHandler(status, null); @@ -908,8 +1159,9 @@ public bool TestArchive(bool testData, TestStrategy strategy, ZipTestResultHandl } [Flags] - enum HeaderTest + private enum HeaderTest { + None = 0x0, Extract = 0x01, // Check that this header represents an entry whose data can be extracted Header = 0x02, // Check that this header contents are valid } @@ -922,20 +1174,26 @@ enum HeaderTest /// /// The type of tests to carry out. /// The offset of the entries data in the file - long TestLocalHeader(ZipEntry entry, HeaderTest tests) + private long TestLocalHeader(ZipEntry entry, HeaderTest tests) { - lock (baseStream_) { + lock (baseStream_) + { bool testHeader = (tests & HeaderTest.Header) != 0; bool testData = (tests & HeaderTest.Extract) != 0; - baseStream_.Seek(offsetOfFirstEntry + entry.Offset, SeekOrigin.Begin); - if ((int)ReadLEUint() != ZipConstants.LocalHeaderSignature) { - throw new ZipException(string.Format("Wrong local header signature @{0:X}", offsetOfFirstEntry + entry.Offset)); + var entryAbsOffset = offsetOfFirstEntry + entry.Offset; + + baseStream_.Seek(entryAbsOffset, SeekOrigin.Begin); + var signature = (int)ReadLEUint(); + + if (signature != ZipConstants.LocalHeaderSignature) + { + throw new ZipException($"Wrong local header signature at 0x{entryAbsOffset:x}, expected 0x{ZipConstants.LocalHeaderSignature:x8}, actual 0x{signature:x8}"); } var extractVersion = (short)(ReadLEUshort() & 0x00ff); - var localFlags = (short)ReadLEUshort(); - var compressionMethod = (short)ReadLEUshort(); + var localFlags = (GeneralBitFlags)ReadLEUshort(); + var compressionMethod = (CompressionMethod)ReadLEUshort(); var fileTime = (short)ReadLEUshort(); var fileDate = (short)ReadLEUshort(); uint crcValue = ReadLEUint(); @@ -953,49 +1211,66 @@ long TestLocalHeader(ZipEntry entry, HeaderTest tests) var localExtraData = new ZipExtraData(extraData); // Extra data / zip64 checks - if (localExtraData.Find(1)) { + if (localExtraData.Find(headerID: 1)) + { // 2010-03-04 Forum 10512: removed checks for version >= ZipConstants.VersionZip64 // and size or compressedSize = MaxValue, due to rogue creators. size = localExtraData.ReadLong(); compressedSize = localExtraData.ReadLong(); - if ((localFlags & (int)GeneralBitFlags.Descriptor) != 0) { + if (localFlags.HasAny(GeneralBitFlags.Descriptor)) + { // These may be valid if patched later - if ((size != -1) && (size != entry.Size)) { + if ((size != 0) && (size != entry.Size)) + { throw new ZipException("Size invalid for descriptor"); } - if ((compressedSize != -1) && (compressedSize != entry.CompressedSize)) { + if ((compressedSize != 0) && (compressedSize != entry.CompressedSize)) + { throw new ZipException("Compressed size invalid for descriptor"); } } - } else { + } + else + { // No zip64 extra data but entry requires it. if ((extractVersion >= ZipConstants.VersionZip64) && - (((uint)size == uint.MaxValue) || ((uint)compressedSize == uint.MaxValue))) { + (((uint)size == uint.MaxValue) || ((uint)compressedSize == uint.MaxValue))) + { throw new ZipException("Required Zip64 extended information missing"); } } - if (testData) { - if (entry.IsFile) { - if (!entry.IsCompressionMethodSupported()) { + if (testData) + { + if (entry.IsFile) + { + if (!entry.IsCompressionMethodSupported()) + { throw new ZipException("Compression method not supported"); } - if ((extractVersion > ZipConstants.VersionMadeBy) - || ((extractVersion > 20) && (extractVersion < ZipConstants.VersionZip64))) { - throw new ZipException(string.Format("Version required to extract this entry not supported ({0})", extractVersion)); + if (extractVersion > ZipConstants.VersionMadeBy + || (extractVersion > 20 && extractVersion < ZipConstants.VersionZip64)) + { + throw new ZipException($"Version required to extract this entry not supported ({extractVersion})"); } - if ((localFlags & (int)(GeneralBitFlags.Patched | GeneralBitFlags.StrongEncryption | GeneralBitFlags.EnhancedCompress | GeneralBitFlags.HeaderMasked)) != 0) { - throw new ZipException("The library does not support the zip version required to extract this entry"); + const GeneralBitFlags notSupportedFlags = GeneralBitFlags.Patched + | GeneralBitFlags.StrongEncryption + | GeneralBitFlags.EnhancedCompress + | GeneralBitFlags.HeaderMasked; + if (localFlags.HasAny(notSupportedFlags)) + { + throw new ZipException($"The library does not support the zip features required to extract this entry ({localFlags & notSupportedFlags:F})"); } } } - if (testHeader) { + if (testHeader) + { if ((extractVersion <= 63) && // Ignore later versions as we dont know about them.. (extractVersion != 10) && (extractVersion != 11) && @@ -1011,112 +1286,141 @@ long TestLocalHeader(ZipEntry entry, HeaderTest tests) (extractVersion != 61) && (extractVersion != 62) && (extractVersion != 63) - ) { - throw new ZipException(string.Format("Version required to extract this entry is invalid ({0})", extractVersion)); + ) + { + throw new ZipException($"Version required to extract this entry is invalid ({extractVersion})"); } + var localEncoding = _stringCodec.ZipInputEncoding(localFlags); + // Local entry flags dont have reserved bit set on. - if ((localFlags & (int)(GeneralBitFlags.ReservedPKware4 | GeneralBitFlags.ReservedPkware14 | GeneralBitFlags.ReservedPkware15)) != 0) { + if (localFlags.HasAny(GeneralBitFlags.ReservedPKware4 | GeneralBitFlags.ReservedPkware14 | GeneralBitFlags.ReservedPkware15)) + { throw new ZipException("Reserved bit flags cannot be set."); } // Encryption requires extract version >= 20 - if (((localFlags & (int)GeneralBitFlags.Encrypted) != 0) && (extractVersion < 20)) { - throw new ZipException(string.Format("Version required to extract this entry is too low for encryption ({0})", extractVersion)); + if (localFlags.HasAny(GeneralBitFlags.Encrypted) && extractVersion < 20) + { + throw new ZipException($"Version required to extract this entry is too low for encryption ({extractVersion})"); } // Strong encryption requires encryption flag to be set and extract version >= 50. - if ((localFlags & (int)GeneralBitFlags.StrongEncryption) != 0) { - if ((localFlags & (int)GeneralBitFlags.Encrypted) == 0) { + if (localFlags.HasAny(GeneralBitFlags.StrongEncryption)) + { + if (!localFlags.HasAny(GeneralBitFlags.Encrypted)) + { throw new ZipException("Strong encryption flag set but encryption flag is not set"); } - if (extractVersion < 50) { - throw new ZipException(string.Format("Version required to extract this entry is too low for encryption ({0})", extractVersion)); + if (extractVersion < 50) + { + throw new ZipException($"Version required to extract this entry is too low for encryption ({extractVersion})"); } } // Patched entries require extract version >= 27 - if (((localFlags & (int)GeneralBitFlags.Patched) != 0) && (extractVersion < 27)) { - throw new ZipException(string.Format("Patched data requires higher version than ({0})", extractVersion)); + if (localFlags.HasAny(GeneralBitFlags.Patched) && extractVersion < 27) + { + throw new ZipException($"Patched data requires higher version than ({extractVersion})"); } // Central header flags match local entry flags. - if (localFlags != entry.Flags) { - throw new ZipException("Central header/local header flags mismatch"); + if ((int)localFlags != entry.Flags) + { + throw new ZipException($"Central header/local header flags mismatch ({(GeneralBitFlags)entry.Flags:F} vs {localFlags:F})"); } // Central header compression method matches local entry - if (entry.CompressionMethod != (CompressionMethod)compressionMethod) { - throw new ZipException("Central header/local header compression method mismatch"); + if (entry.CompressionMethodForHeader != compressionMethod) + { + throw new ZipException($"Central header/local header compression method mismatch ({entry.CompressionMethodForHeader:G} vs {compressionMethod:G})"); } - if (entry.Version != extractVersion) { + if (entry.Version != extractVersion) + { throw new ZipException("Extract version mismatch"); } // Strong encryption and extract version match - if ((localFlags & (int)GeneralBitFlags.StrongEncryption) != 0) { - if (extractVersion < 62) { + if (localFlags.HasAny(GeneralBitFlags.StrongEncryption)) + { + if (extractVersion < 62) + { throw new ZipException("Strong encryption flag set but version not high enough"); } } - if ((localFlags & (int)GeneralBitFlags.HeaderMasked) != 0) { - if ((fileTime != 0) || (fileDate != 0)) { + if (localFlags.HasAny(GeneralBitFlags.HeaderMasked)) + { + if (fileTime != 0 || fileDate != 0) + { throw new ZipException("Header masked set but date/time values non-zero"); } } - if ((localFlags & (int)GeneralBitFlags.Descriptor) == 0) { - if (crcValue != (uint)entry.Crc) { + if (!localFlags.HasAny(GeneralBitFlags.Descriptor)) + { + if (crcValue != (uint)entry.Crc) + { throw new ZipException("Central header/local header crc mismatch"); } } // Crc valid for empty entry. - // This will also apply to streamed entries where size isnt known and the header cant be patched - if ((size == 0) && (compressedSize == 0)) { - if (crcValue != 0) { + // This will also apply to streamed entries where size isn't known and the header cant be patched + if (size == 0 && compressedSize == 0) + { + if (crcValue != 0) + { throw new ZipException("Invalid CRC for empty entry"); } } // TODO: make test more correct... can't compare lengths as was done originally as this can fail for MBCS strings // Assuming a code page at this point is not valid? Best is to store the name length in the ZipEntry probably - if (entry.Name.Length > storedNameLength) { + if (entry.Name.Length > storedNameLength) + { throw new ZipException("File name length mismatch"); } // Name data has already been read convert it and compare. - string localName = ZipConstants.ConvertToStringExt(localFlags, nameData); + string localName = localEncoding.GetString(nameData); // Central directory and local entry name match - if (localName != entry.Name) { + if (localName != entry.Name) + { throw new ZipException("Central header and local header file name mismatch"); } // Directories have zero actual size but can have compressed size - if (entry.IsDirectory) { - if (size > 0) { + if (entry.IsDirectory) + { + if (size > 0) + { throw new ZipException("Directory cannot have size"); } // There may be other cases where the compressed size can be greater than this? // If so until details are known we will be strict. - if (entry.IsCrypted) { - if (compressedSize > ZipConstants.CryptoHeaderSize + 2) { + if (entry.IsCrypted) + { + if (compressedSize > entry.EncryptionOverheadSize + 2) + { throw new ZipException("Directory compressed size invalid"); } - } else if (compressedSize > 2) { + } + else if (compressedSize > 2) + { // When not compressed the directory size can validly be 2 bytes - // if the true size wasnt known when data was originally being written. + // if the true size wasn't known when data was originally being written. // NOTE: Versions of the library 0.85.4 and earlier always added 2 bytes throw new ZipException("Directory compressed size invalid"); } } - if (!ZipNameTransform.IsValidName(localName, true)) { + if (!ZipNameTransform.IsValidName(localName, true)) + { throw new ZipException("Name is invalid"); } } @@ -1125,21 +1429,18 @@ long TestLocalHeader(ZipEntry entry, HeaderTest tests) // Size can be verified only if it is known in the local header. // it will always be known in the central header. - if (((localFlags & (int)GeneralBitFlags.Descriptor) == 0) || - ((size > 0 || compressedSize > 0) && entry.Size > 0)) { - - if ((size != 0) - && (size != entry.Size)) { - throw new ZipException( - string.Format("Size mismatch between central header({0}) and local header({1})", - entry.Size, size)); + if (!localFlags.HasAny(GeneralBitFlags.Descriptor) || + ((size > 0 || compressedSize > 0) && entry.Size > 0)) + { + if (size != 0 && size != entry.Size) + { + throw new ZipException($"Size mismatch between central header ({entry.Size}) and local header ({size})"); } - if ((compressedSize != 0) - && (compressedSize != entry.CompressedSize && compressedSize != 0xFFFFFFFF && compressedSize != -1)) { - throw new ZipException( - string.Format("Compressed size mismatch between central header({0}) and local header({1})", - entry.CompressedSize, compressedSize)); + if (compressedSize != 0 + && (compressedSize != entry.CompressedSize && compressedSize != 0xFFFFFFFF && compressedSize != -1)) + { + throw new ZipException($"Compressed size mismatch between central header({entry.CompressedSize}) and local header({compressedSize})"); } } @@ -1148,16 +1449,16 @@ long TestLocalHeader(ZipEntry entry, HeaderTest tests) } } - #endregion + #endregion Archive Testing #region Updating - const int DefaultBufferSize = 4096; + private const int DefaultBufferSize = 4096; /// /// The kind of update to apply. /// - enum UpdateCommand + private enum UpdateCommand { Copy, // Copy original file contents. Modify, // Change encryption, compression, attributes, name, time etc, of an existing file. @@ -1165,15 +1466,19 @@ enum UpdateCommand } #region Properties + /// /// Get / set the to apply to names when updating. /// - public INameTransform NameTransform { - get { + public INameTransform NameTransform + { + get + { return updateEntryFactory_.NameTransform; } - set { + set + { updateEntryFactory_.NameTransform = value; } } @@ -1182,15 +1487,21 @@ public INameTransform NameTransform { /// Get/set the used to generate values /// during updates. /// - public IEntryFactory EntryFactory { - get { + public IEntryFactory EntryFactory + { + get + { return updateEntryFactory_; } - set { - if (value == null) { + set + { + if (value == null) + { updateEntryFactory_ = new ZipEntryFactory(); - } else { + } + else + { updateEntryFactory_ = value; } } @@ -1199,14 +1510,18 @@ public IEntryFactory EntryFactory { /// /// Get /set the buffer size to be used when updating this zip file. /// - public int BufferSize { + public int BufferSize + { get { return bufferSize_; } - set { - if (value < 1024) { + set + { + if (value < 1024) + { throw new ArgumentOutOfRangeException(nameof(value), "cannot be below 1024"); } - if (bufferSize_ != value) { + if (bufferSize_ != value) + { bufferSize_ = value; copyBuffer_ = null; } @@ -1216,23 +1531,26 @@ public int BufferSize { /// /// Get a value indicating an update has been started. /// - public bool IsUpdating { + public bool IsUpdating + { get { return updates_ != null; } } /// /// Get / set a value indicating how Zip64 Extension usage is determined when adding entries. /// - public UseZip64 UseZip64 { + public UseZip64 UseZip64 + { get { return useZip64_; } set { useZip64_ = value; } } - #endregion + #endregion Properties #region Immediate updating + // TBD: Direct form of updating - // + // // public void Update(IEntryMatcher deleteMatcher) // { // } @@ -1240,9 +1558,11 @@ public UseZip64 UseZip64 { // public void Update(IScanner addScanner) // { // } - #endregion + + #endregion Immediate updating #region Deferred Updating + /// /// Begin updating this archive. /// @@ -1253,31 +1573,26 @@ public UseZip64 UseZip64 { /// ZipFile has been closed. public void BeginUpdate(IArchiveStorage archiveStorage, IDynamicDataSource dataSource) { - if (archiveStorage == null) { - throw new ArgumentNullException(nameof(archiveStorage)); - } - - if (dataSource == null) { - throw new ArgumentNullException(nameof(dataSource)); - } - - if (isDisposed_) { + if (isDisposed_) + { throw new ObjectDisposedException("ZipFile"); } - if (IsEmbeddedArchive) { + if (IsEmbeddedArchive) + { throw new ZipException("Cannot update embedded/SFX archives"); } - archiveStorage_ = archiveStorage; - updateDataSource_ = dataSource; + archiveStorage_ = archiveStorage ?? throw new ArgumentNullException(nameof(archiveStorage)); + updateDataSource_ = dataSource ?? throw new ArgumentNullException(nameof(dataSource)); // NOTE: the baseStream_ may not currently support writing or seeking. updateIndex_ = new Dictionary(); updates_ = new List(entries_.Length); - foreach (ZipEntry entry in entries_) { + foreach (ZipEntry entry in entries_) + { int index = updates_.Count; updates_.Add(new ZipUpdate(entry)); updateIndex_.Add(entry.Name, index); @@ -1287,7 +1602,8 @@ public void BeginUpdate(IArchiveStorage archiveStorage, IDynamicDataSource dataS updates_.Sort(new UpdateComparer()); int idx = 0; - foreach (ZipUpdate update in updates_) { + foreach (ZipUpdate update in updates_) + { //If last entry, there is no next entry offset to use if (idx == updates_.Count - 1) break; @@ -1319,9 +1635,12 @@ public void BeginUpdate(IArchiveStorage archiveStorage) /// public void BeginUpdate() { - if (Name == null) { + if (Name == null) + { BeginUpdate(new MemoryArchiveStorage(), new DynamicDiskDataSource()); - } else { + } + else + { BeginUpdate(new DiskArchiveStorage(this), new DynamicDiskDataSource()); } } @@ -1334,31 +1653,38 @@ public void BeginUpdate() /// ZipFile has been closed. public void CommitUpdate() { - if (isDisposed_) { + if (isDisposed_) + { throw new ObjectDisposedException("ZipFile"); } CheckUpdating(); - try { + try + { updateIndex_.Clear(); updateIndex_ = null; - if (contentsEdited_) { + if (contentsEdited_) + { RunUpdates(); - } else if (commentEdited_) { + } + else if (commentEdited_ && !isNewArchive_) + { UpdateCommentOnly(); - } else { + } + else + { // Create an empty archive if none existed originally. - if (entries_.Length == 0) { - byte[] theComment = (newComment_ != null) ? newComment_.RawComment : ZipConstants.ConvertToArray(comment_); - using (ZipHelperStream zhs = new ZipHelperStream(baseStream_)) { - zhs.WriteEndOfCentralDirectory(0, 0, 0, theComment); - } - } + if (entries_.Length != 0) return; + byte[] theComment = (newComment_ != null) + ? newComment_.RawComment + : _stringCodec.ZipArchiveCommentEncoding.GetBytes(comment_); + ZipFormat.WriteEndOfCentralDirectory(baseStream_, 0, 0, 0, theComment); } - - } finally { + } + finally + { PostUpdateCleanup(); } } @@ -1380,15 +1706,17 @@ public void AbortUpdate() /// ZipFile has been closed. public void SetComment(string comment) { - if (isDisposed_) { + if (isDisposed_) + { throw new ObjectDisposedException("ZipFile"); } CheckUpdating(); - newComment_ = new ZipString(comment); + newComment_ = new ZipString(comment, _stringCodec.ZipArchiveCommentEncoding); - if (newComment_.RawLength > 0xffff) { + if (newComment_.RawLength > 0xffff) + { newComment_ = null; throw new ZipException("Comment length exceeds maximum - 65535"); } @@ -1398,24 +1726,28 @@ public void SetComment(string comment) commentEdited_ = true; } - #endregion + #endregion Deferred Updating #region Adding Entries - void AddUpdate(ZipUpdate update) + private void AddUpdate(ZipUpdate update) { contentsEdited_ = true; - int index = FindExistingUpdate(update.Entry.Name); + int index = FindExistingUpdate(update.Entry.Name, isEntryName: true); - if (index >= 0) { - if (updates_[index] == null) { + if (index >= 0) + { + if (updates_[index] == null) + { updateCount_ += 1; } // Direct replacement is faster than delete and add. updates_[index] = update; - } else { + } + else + { index = updates_.Count; updates_.Add(update); updateCount_ += 1; @@ -1431,21 +1763,20 @@ void AddUpdate(ZipUpdate update) /// Ensure Unicode text is used for name and comment for this entry. /// Argument supplied is null. /// ZipFile has been closed. - /// Compression method is not supported. + /// Compression method is not supported for creating entries. public void Add(string fileName, CompressionMethod compressionMethod, bool useUnicodeText) { - if (fileName == null) { + if (fileName == null) + { throw new ArgumentNullException(nameof(fileName)); } - if (isDisposed_) { + if (isDisposed_) + { throw new ObjectDisposedException("ZipFile"); } - if (!ZipEntry.IsCompressionMethodSupported(compressionMethod)) { - throw new ArgumentOutOfRangeException(nameof(compressionMethod)); - } - + CheckSupportedCompressionMethod(compressionMethod); CheckUpdating(); contentsEdited_ = true; @@ -1462,17 +1793,15 @@ public void Add(string fileName, CompressionMethod compressionMethod, bool useUn /// The name of the file to add. /// The compression method to use. /// ZipFile has been closed. - /// The compression method is not supported. + /// Compression method is not supported for creating entries. public void Add(string fileName, CompressionMethod compressionMethod) { - if (fileName == null) { + if (fileName == null) + { throw new ArgumentNullException(nameof(fileName)); } - if (!ZipEntry.IsCompressionMethodSupported(compressionMethod)) { - throw new ArgumentOutOfRangeException(nameof(compressionMethod)); - } - + CheckSupportedCompressionMethod(compressionMethod); CheckUpdating(); contentsEdited_ = true; @@ -1488,7 +1817,8 @@ public void Add(string fileName, CompressionMethod compressionMethod) /// Argument supplied is null. public void Add(string fileName) { - if (fileName == null) { + if (fileName == null) + { throw new ArgumentNullException(nameof(fileName)); } @@ -1504,11 +1834,13 @@ public void Add(string fileName) /// Argument supplied is null. public void Add(string fileName, string entryName) { - if (fileName == null) { + if (fileName == null) + { throw new ArgumentNullException(nameof(fileName)); } - if (entryName == null) { + if (entryName == null) + { throw new ArgumentNullException(nameof(entryName)); } @@ -1516,7 +1848,6 @@ public void Add(string fileName, string entryName) AddUpdate(new ZipUpdate(fileName, EntryFactory.MakeFileEntry(fileName, entryName, true))); } - /// /// Add a file entry with data. /// @@ -1524,11 +1855,13 @@ public void Add(string fileName, string entryName) /// The name to give to the entry. public void Add(IStaticDataSource dataSource, string entryName) { - if (dataSource == null) { + if (dataSource == null) + { throw new ArgumentNullException(nameof(dataSource)); } - if (entryName == null) { + if (entryName == null) + { throw new ArgumentNullException(nameof(entryName)); } @@ -1542,16 +1875,20 @@ public void Add(IStaticDataSource dataSource, string entryName) /// The source of the data for this entry. /// The name to give to the entry. /// The compression method to use. + /// Compression method is not supported for creating entries. public void Add(IStaticDataSource dataSource, string entryName, CompressionMethod compressionMethod) { - if (dataSource == null) { + if (dataSource == null) + { throw new ArgumentNullException(nameof(dataSource)); } - if (entryName == null) { + if (entryName == null) + { throw new ArgumentNullException(nameof(entryName)); } + CheckSupportedCompressionMethod(compressionMethod); CheckUpdating(); ZipEntry entry = EntryFactory.MakeFileEntry(entryName, false); @@ -1567,16 +1904,20 @@ public void Add(IStaticDataSource dataSource, string entryName, CompressionMetho /// The name to give to the entry. /// The compression method to use. /// Ensure Unicode text is used for name and comments for this entry. + /// Compression method is not supported for creating entries. public void Add(IStaticDataSource dataSource, string entryName, CompressionMethod compressionMethod, bool useUnicodeText) { - if (dataSource == null) { + if (dataSource == null) + { throw new ArgumentNullException(nameof(dataSource)); } - if (entryName == null) { + if (entryName == null) + { throw new ArgumentNullException(nameof(entryName)); } + CheckSupportedCompressionMethod(compressionMethod); CheckUpdating(); ZipEntry entry = EntryFactory.MakeFileEntry(entryName, false); @@ -1593,26 +1934,64 @@ public void Add(IStaticDataSource dataSource, string entryName, CompressionMetho /// This can be used to add directories, volume labels, or empty file entries. public void Add(ZipEntry entry) { - if (entry == null) { + if (entry == null) + { throw new ArgumentNullException(nameof(entry)); } CheckUpdating(); - if ((entry.Size != 0) || (entry.CompressedSize != 0)) { + if ((entry.Size != 0) || (entry.CompressedSize != 0)) + { throw new ZipException("Entry cannot have any data"); } AddUpdate(new ZipUpdate(UpdateCommand.Add, entry)); } + /// + /// Add a with data. + /// + /// The source of the data for this entry. + /// The entry to add. + /// This can be used to add file entries with a custom data source. + /// + /// The encryption method specified in is unsupported. + /// + /// Compression method is not supported for creating entries. + public void Add(IStaticDataSource dataSource, ZipEntry entry) + { + if (entry == null) + { + throw new ArgumentNullException(nameof(entry)); + } + + if (dataSource == null) + { + throw new ArgumentNullException(nameof(dataSource)); + } + + // We don't currently support adding entries with AES encryption, so throw + // up front instead of failing or falling back to ZipCrypto later on + if (entry.AESKeySize > 0) + { + throw new NotSupportedException("Creation of AES encrypted entries is not supported"); + } + + CheckSupportedCompressionMethod(entry.CompressionMethod); + CheckUpdating(); + + AddUpdate(new ZipUpdate(dataSource, entry)); + } + /// /// Add a directory entry to the archive. /// /// The directory to add. public void AddDirectory(string directoryName) { - if (directoryName == null) { + if (directoryName == null) + { throw new ArgumentNullException(nameof(directoryName)); } @@ -1622,9 +2001,22 @@ public void AddDirectory(string directoryName) AddUpdate(new ZipUpdate(UpdateCommand.Add, dirEntry)); } - #endregion + /// + /// Check if the specified compression method is supported for adding a new entry. + /// + /// The compression method for the new entry. + private static void CheckSupportedCompressionMethod(CompressionMethod compressionMethod) + { + if (compressionMethod != CompressionMethod.Deflated && compressionMethod != CompressionMethod.Stored && compressionMethod != CompressionMethod.BZip2) + { + throw new NotImplementedException("Compression method not supported"); + } + } + + #endregion Adding Entries #region Modifying Entries + /* Modify not yet ready for public consumption. Direct modification of an entry should not overwrite original data before its read. Safe mode is trivial in this sense. @@ -1633,19 +2025,19 @@ public void Modify(ZipEntry original, ZipEntry updated) if ( original == null ) { throw new ArgumentNullException("original"); } - if ( updated == null ) { throw new ArgumentNullException("updated"); } - CheckUpdating(); contentsEdited_ = true; updates_.Add(new ZipUpdate(original, updated)); } */ - #endregion + + #endregion Modifying Entries #region Deleting Entries + /// /// Delete an entry by name /// @@ -1653,7 +2045,8 @@ public void Modify(ZipEntry original, ZipEntry updated) /// True if the entry was found and deleted; false otherwise. public bool Delete(string fileName) { - if (fileName == null) { + if (fileName == null) + { throw new ArgumentNullException(nameof(fileName)); } @@ -1661,12 +2054,15 @@ public bool Delete(string fileName) bool result = false; int index = FindExistingUpdate(fileName); - if ((index >= 0) && (updates_[index] != null)) { + if ((index >= 0) && (updates_[index] != null)) + { result = true; contentsEdited_ = true; updates_[index] = null; updateCount_ -= 1; - } else { + } + else + { throw new ZipException("Cannot find entry to delete"); } return result; @@ -1678,28 +2074,33 @@ public bool Delete(string fileName) /// The entry to delete. public void Delete(ZipEntry entry) { - if (entry == null) { + if (entry == null) + { throw new ArgumentNullException(nameof(entry)); } CheckUpdating(); int index = FindExistingUpdate(entry); - if (index >= 0) { + if (index >= 0) + { contentsEdited_ = true; updates_[index] = null; updateCount_ -= 1; - } else { + } + else + { throw new ZipException("Cannot find entry to delete"); } } - #endregion + #endregion Deleting Entries #region Update Support #region Writing Values/Headers - void WriteLEShort(int value) + + private void WriteLEShort(int value) { baseStream_.WriteByte((byte)(value & 0xff)); baseStream_.WriteByte((byte)((value >> 8) & 0xff)); @@ -1708,7 +2109,7 @@ void WriteLEShort(int value) /// /// Write an unsigned short in little endian byte order. /// - void WriteLEUshort(ushort value) + private void WriteLEUshort(ushort value) { baseStream_.WriteByte((byte)(value & 0xff)); baseStream_.WriteByte((byte)(value >> 8)); @@ -1717,7 +2118,7 @@ void WriteLEUshort(ushort value) /// /// Write an int in little endian byte order. /// - void WriteLEInt(int value) + private void WriteLEInt(int value) { WriteLEShort(value & 0xffff); WriteLEShort(value >> 16); @@ -1726,7 +2127,7 @@ void WriteLEInt(int value) /// /// Write an unsigned int in little endian byte order. /// - void WriteLEUint(uint value) + private void WriteLEUint(uint value) { WriteLEUshort((ushort)(value & 0xffff)); WriteLEUshort((ushort)(value >> 16)); @@ -1735,19 +2136,19 @@ void WriteLEUint(uint value) /// /// Write a long in little endian byte order. /// - void WriteLeLong(long value) + private void WriteLeLong(long value) { WriteLEInt((int)(value & 0xffffffff)); WriteLEInt((int)(value >> 32)); } - void WriteLEUlong(ulong value) + private void WriteLEUlong(ulong value) { WriteLEUint((uint)(value & 0xffffffff)); WriteLEUint((uint)(value >> 32)); } - void WriteLocalEntryHeader(ZipUpdate update) + private void WriteLocalEntryHeader(ZipUpdate update) { ZipEntry entry = update.OutEntry; @@ -1755,30 +2156,41 @@ void WriteLocalEntryHeader(ZipUpdate update) entry.Offset = baseStream_.Position; // TODO: Need to clear any entry flags that dont make sense or throw an exception here. - if (update.Command != UpdateCommand.Copy) { - if (entry.CompressionMethod == CompressionMethod.Deflated) { - if (entry.Size == 0) { + if (update.Command != UpdateCommand.Copy) + { + if (entry.CompressionMethod == CompressionMethod.Deflated) + { + if (entry.Size == 0) + { // No need to compress - no data. entry.CompressedSize = entry.Size; entry.Crc = 0; entry.CompressionMethod = CompressionMethod.Stored; } - } else if (entry.CompressionMethod == CompressionMethod.Stored) { + } + else if (entry.CompressionMethod == CompressionMethod.Stored) + { entry.Flags &= ~(int)GeneralBitFlags.Descriptor; } - if (HaveKeys) { + if (HaveKeys) + { entry.IsCrypted = true; - if (entry.Crc < 0) { + if (entry.Crc < 0) + { entry.Flags |= (int)GeneralBitFlags.Descriptor; } - } else { + } + else + { entry.IsCrypted = false; } - switch (useZip64_) { + switch (useZip64_) + { case UseZip64.Dynamic: - if (entry.Size < 0) { + if (entry.Size < 0) + { entry.ForceZip64(); } break; @@ -1788,7 +2200,7 @@ void WriteLocalEntryHeader(ZipUpdate update) break; case UseZip64.Off: - // Do nothing. The entry itself may be using Zip64 independantly. + // Do nothing. The entry itself may be using Zip64 independently. break; } } @@ -1799,22 +2211,29 @@ void WriteLocalEntryHeader(ZipUpdate update) WriteLEShort(entry.Version); WriteLEShort(entry.Flags); - WriteLEShort((byte)entry.CompressionMethod); + WriteLEShort((byte)entry.CompressionMethodForHeader); WriteLEInt((int)entry.DosTime); - if (!entry.HasCrc) { + if (!entry.HasCrc) + { // Note patch address for updating CRC later. update.CrcPatchOffset = baseStream_.Position; WriteLEInt((int)0); - } else { + } + else + { WriteLEInt(unchecked((int)entry.Crc)); } - if (entry.LocalHeaderRequiresZip64) { + if (entry.LocalHeaderRequiresZip64) + { WriteLEInt(-1); WriteLEInt(-1); - } else { - if ((entry.CompressedSize < 0) || (entry.Size < 0)) { + } + else + { + if ((entry.CompressedSize < 0) || (entry.Size < 0)) + { update.SizePatchOffset = baseStream_.Position; } @@ -1822,15 +2241,18 @@ void WriteLocalEntryHeader(ZipUpdate update) WriteLEInt((int)entry.Size); } - byte[] name = ZipConstants.ConvertToArray(entry.Flags, entry.Name); + var entryEncoding = _stringCodec.ZipInputEncoding(entry.Flags); + byte[] name = entryEncoding.GetBytes(entry.Name); - if (name.Length > 0xFFFF) { + if (name.Length > 0xFFFF) + { throw new ZipException("Entry name too long."); } var ed = new ZipExtraData(entry.ExtraData); - if (entry.LocalHeaderRequiresZip64) { + if (entry.LocalHeaderRequiresZip64) + { ed.StartNewEntry(); // Local entry header always includes size and compressed size. @@ -1838,7 +2260,9 @@ void WriteLocalEntryHeader(ZipUpdate update) ed.AddLeLong(entry.Size); ed.AddLeLong(entry.CompressedSize); ed.AddNewEntry(1); - } else { + } + else + { ed.Delete(1); } @@ -1847,34 +2271,41 @@ void WriteLocalEntryHeader(ZipUpdate update) WriteLEShort(name.Length); WriteLEShort(entry.ExtraData.Length); - if (name.Length > 0) { + if (name.Length > 0) + { baseStream_.Write(name, 0, name.Length); } - if (entry.LocalHeaderRequiresZip64) { - if (!ed.Find(1)) { + if (entry.LocalHeaderRequiresZip64) + { + if (!ed.Find(1)) + { throw new ZipException("Internal error cannot find extra data"); } update.SizePatchOffset = baseStream_.Position + ed.CurrentReadIndex; } - if (entry.ExtraData.Length > 0) { + if (entry.ExtraData.Length > 0) + { baseStream_.Write(entry.ExtraData, 0, entry.ExtraData.Length); } } - int WriteCentralDirectoryHeader(ZipEntry entry) + private int WriteCentralDirectoryHeader(ZipEntry entry) { - if (entry.CompressedSize < 0) { + if (entry.CompressedSize < 0) + { throw new ZipException("Attempt to write central directory entry with unknown csize"); } - if (entry.Size < 0) { + if (entry.Size < 0) + { throw new ZipException("Attempt to write central directory entry with unknown size"); } - if (entry.Crc < 0) { + if (entry.Crc < 0) + { throw new ZipException("Attempt to write central directory entry with unknown crc"); } @@ -1882,34 +2313,47 @@ int WriteCentralDirectoryHeader(ZipEntry entry) WriteLEInt(ZipConstants.CentralHeaderSignature); // Version made by - WriteLEShort(ZipConstants.VersionMadeBy); + WriteLEShort((entry.HostSystem << 8) | entry.VersionMadeBy); // Version required to extract WriteLEShort(entry.Version); WriteLEShort(entry.Flags); - unchecked { - WriteLEShort((byte)entry.CompressionMethod); + unchecked + { + WriteLEShort((byte)entry.CompressionMethodForHeader); WriteLEInt((int)entry.DosTime); WriteLEInt((int)entry.Crc); } - if ((entry.IsZip64Forced()) || (entry.CompressedSize >= 0xffffffff)) { + bool useExtraCompressedSize = false; //Do we want to store the compressed size in the extra data? + if ((entry.IsZip64Forced()) || (entry.CompressedSize >= 0xffffffff)) + { + useExtraCompressedSize = true; WriteLEInt(-1); - } else { + } + else + { WriteLEInt((int)(entry.CompressedSize & 0xffffffff)); } - if ((entry.IsZip64Forced()) || (entry.Size >= 0xffffffff)) { + bool useExtraUncompressedSize = false; //Do we want to store the uncompressed size in the extra data? + if ((entry.IsZip64Forced()) || (entry.Size >= 0xffffffff)) + { + useExtraUncompressedSize = true; WriteLEInt(-1); - } else { + } + else + { WriteLEInt((int)entry.Size); } - byte[] name = ZipConstants.ConvertToArray(entry.Flags, entry.Name); + var entryEncoding = _stringCodec.ZipInputEncoding(entry.Flags); + byte[] name = entryEncoding.GetBytes(entry.Name); - if (name.Length > 0xFFFF) { + if (name.Length > 0xFFFF) + { throw new ZipException("Entry name is too long."); } @@ -1918,24 +2362,30 @@ int WriteCentralDirectoryHeader(ZipEntry entry) // Central header extra data is different to local header version so regenerate. var ed = new ZipExtraData(entry.ExtraData); - if (entry.CentralHeaderRequiresZip64) { + if (entry.CentralHeaderRequiresZip64) + { ed.StartNewEntry(); - if ((entry.Size >= 0xffffffff) || (useZip64_ == UseZip64.On)) { + if (useExtraUncompressedSize) + { ed.AddLeLong(entry.Size); } - if ((entry.CompressedSize >= 0xffffffff) || (useZip64_ == UseZip64.On)) { + if (useExtraCompressedSize) + { ed.AddLeLong(entry.CompressedSize); } - if (entry.Offset >= 0xffffffff) { + if (entry.Offset >= 0xffffffff) + { ed.AddLeLong(entry.Offset); } // Number of disk on which this file starts isnt supported and is never written here. ed.AddNewEntry(1); - } else { + } + else + { // Should have already be done when local header was added. ed.Delete(1); } @@ -1949,53 +2399,67 @@ int WriteCentralDirectoryHeader(ZipEntry entry) WriteLEShort(0); // internal file attributes // External file attributes... - if (entry.ExternalFileAttributes != -1) { + if (entry.ExternalFileAttributes != -1) + { WriteLEInt(entry.ExternalFileAttributes); - } else { - if (entry.IsDirectory) { + } + else + { + if (entry.IsDirectory) + { WriteLEUint(16); - } else { + } + else + { WriteLEUint(0); } } - if (entry.Offset >= 0xffffffff) { + if (entry.Offset >= 0xffffffff) + { WriteLEUint(0xffffffff); - } else { + } + else + { WriteLEUint((uint)(int)entry.Offset); } - if (name.Length > 0) { + if (name.Length > 0) + { baseStream_.Write(name, 0, name.Length); } - if (centralExtraData.Length > 0) { + if (centralExtraData.Length > 0) + { baseStream_.Write(centralExtraData, 0, centralExtraData.Length); } - byte[] rawComment = (entry.Comment != null) ? Encoding.ASCII.GetBytes(entry.Comment) : new byte[0]; + byte[] rawComment = (entry.Comment != null) ? Encoding.ASCII.GetBytes(entry.Comment) : Empty.Array(); - if (rawComment.Length > 0) { + if (rawComment.Length > 0) + { baseStream_.Write(rawComment, 0, rawComment.Length); } return ZipConstants.CentralHeaderBaseSize + name.Length + centralExtraData.Length + rawComment.Length; } - #endregion - void PostUpdateCleanup() + #endregion Writing Values/Headers + + private void PostUpdateCleanup() { updateDataSource_ = null; updates_ = null; updateIndex_ = null; - if (archiveStorage_ != null) { + if (archiveStorage_ != null) + { archiveStorage_.Dispose(); archiveStorage_ = null; } } - string GetTransformedFileName(string name) + private string GetTransformedFileName(string name) { INameTransform transform = NameTransform; return (transform != null) ? @@ -2003,7 +2467,7 @@ string GetTransformedFileName(string name) name; } - string GetTransformedDirectoryName(string name) + private string GetTransformedDirectoryName(string name) { INameTransform transform = NameTransform; return (transform != null) ? @@ -2015,39 +2479,57 @@ string GetTransformedDirectoryName(string name) /// Get a raw memory buffer. /// /// Returns a raw memory buffer. - byte[] GetBuffer() + private byte[] GetBuffer() { - if (copyBuffer_ == null) { + if (copyBuffer_ == null) + { copyBuffer_ = new byte[bufferSize_]; } return copyBuffer_; } - void CopyDescriptorBytes(ZipUpdate update, Stream dest, Stream source) + private void CopyDescriptorBytes(ZipUpdate update, Stream dest, Stream source) { - int bytesToCopy = GetDescriptorSize(update); + // Don't include the signature size to allow copy without seeking + var bytesToCopy = GetDescriptorSize(update, false); - if (bytesToCopy > 0) { - byte[] buffer = GetBuffer(); + // Don't touch the source stream if no descriptor is present + if (bytesToCopy == 0) return; - while (bytesToCopy > 0) { - int readSize = Math.Min(buffer.Length, bytesToCopy); + var buffer = GetBuffer(); - int bytesRead = source.Read(buffer, 0, readSize); - if (bytesRead > 0) { - dest.Write(buffer, 0, bytesRead); - bytesToCopy -= bytesRead; - } else { - throw new ZipException("Unxpected end of stream"); - } + // Copy the first 4 bytes of the descriptor + source.Read(buffer, 0, sizeof(int)); + dest.Write(buffer, 0, sizeof(int)); + + if (BitConverter.ToUInt32(buffer, 0) != ZipConstants.DataDescriptorSignature) + { + // The initial bytes wasn't the descriptor, reduce the pending byte count + bytesToCopy -= buffer.Length; + } + + while (bytesToCopy > 0) + { + int readSize = Math.Min(buffer.Length, bytesToCopy); + + int bytesRead = source.Read(buffer, 0, readSize); + if (bytesRead > 0) + { + dest.Write(buffer, 0, bytesRead); + bytesToCopy -= bytesRead; + } + else + { + throw new ZipException("Unxpected end of stream"); } } } - void CopyBytes(ZipUpdate update, Stream destination, Stream source, + private void CopyBytes(ZipUpdate update, Stream destination, Stream source, long bytesToCopy, bool updateCrc) { - if (destination == source) { + if (destination == source) + { throw new InvalidOperationException("Destination and source are the same"); } @@ -2059,17 +2541,21 @@ void CopyBytes(ZipUpdate update, Stream destination, Stream source, long totalBytesRead = 0; int bytesRead; - do { + do + { int readSize = buffer.Length; - if (bytesToCopy < readSize) { + if (bytesToCopy < readSize) + { readSize = (int)bytesToCopy; } bytesRead = source.Read(buffer, 0, readSize); - if (bytesRead > 0) { - if (updateCrc) { - crc.Update(buffer, 0, bytesRead); + if (bytesRead > 0) + { + if (updateCrc) + { + crc.Update(new ArraySegment(buffer, 0, bytesRead)); } destination.Write(buffer, 0, bytesRead); bytesToCopy -= bytesRead; @@ -2078,11 +2564,13 @@ void CopyBytes(ZipUpdate update, Stream destination, Stream source, } while ((bytesRead > 0) && (bytesToCopy > 0)); - if (totalBytesRead != targetBytes) { + if (totalBytesRead != targetBytes) + { throw new ZipException(string.Format("Failed to copy bytes expected {0} read {1}", targetBytes, totalBytesRead)); } - if (updateCrc) { + if (updateCrc) + { update.OutEntry.Crc = crc.Value; } } @@ -2091,42 +2579,53 @@ void CopyBytes(ZipUpdate update, Stream destination, Stream source, /// Get the size of the source descriptor for a . /// /// The update to get the size for. - /// The descriptor size, zero if there isnt one. - int GetDescriptorSize(ZipUpdate update) - { - int result = 0; - if ((update.Entry.Flags & (int)GeneralBitFlags.Descriptor) != 0) { - result = ZipConstants.DataDescriptorSize - 4; - if (update.Entry.LocalHeaderRequiresZip64) { - result = ZipConstants.Zip64DataDescriptorSize - 4; - } - } - return result; + /// Whether to include the signature size + /// The descriptor size, zero if there isn't one. + private static int GetDescriptorSize(ZipUpdate update, bool includingSignature) + { + if (!((GeneralBitFlags)update.Entry.Flags).HasAny(GeneralBitFlags.Descriptor)) + return 0; + + var descriptorWithSignature = update.Entry.LocalHeaderRequiresZip64 + ? ZipConstants.Zip64DataDescriptorSize + : ZipConstants.DataDescriptorSize; + + return includingSignature + ? descriptorWithSignature + : descriptorWithSignature - sizeof(int); } - void CopyDescriptorBytesDirect(ZipUpdate update, Stream stream, ref long destinationPosition, long sourcePosition) + private void CopyDescriptorBytesDirect(ZipUpdate update, Stream stream, ref long destinationPosition, long sourcePosition) { - int bytesToCopy = GetDescriptorSize(update); + var buffer = GetBuffer(); ; + + stream.Position = sourcePosition; + stream.Read(buffer, 0, sizeof(int)); + var sourceHasSignature = BitConverter.ToUInt32(buffer, 0) == ZipConstants.DataDescriptorSignature; - while (bytesToCopy > 0) { - var readSize = (int)bytesToCopy; - byte[] buffer = GetBuffer(); + var bytesToCopy = GetDescriptorSize(update, sourceHasSignature); + while (bytesToCopy > 0) + { stream.Position = sourcePosition; - int bytesRead = stream.Read(buffer, 0, readSize); - if (bytesRead > 0) { + + var bytesRead = stream.Read(buffer, 0, bytesToCopy); + if (bytesRead > 0) + { stream.Position = destinationPosition; stream.Write(buffer, 0, bytesRead); bytesToCopy -= bytesRead; destinationPosition += bytesRead; sourcePosition += bytesRead; - } else { - throw new ZipException("Unxpected end of stream"); + } + else + { + throw new ZipException("Unexpected end of stream"); } } } - void CopyEntryDataDirect(ZipUpdate update, Stream stream, bool updateCrc, ref long destinationPosition, ref long sourcePosition) + private void CopyEntryDataDirect(ZipUpdate update, Stream stream, bool updateCrc, ref long destinationPosition, ref long sourcePosition) { long bytesToCopy = update.Entry.CompressedSize; @@ -2138,18 +2637,22 @@ void CopyEntryDataDirect(ZipUpdate update, Stream stream, bool updateCrc, ref lo long totalBytesRead = 0; int bytesRead; - do { + do + { int readSize = buffer.Length; - if (bytesToCopy < readSize) { + if (bytesToCopy < readSize) + { readSize = (int)bytesToCopy; } stream.Position = sourcePosition; bytesRead = stream.Read(buffer, 0, readSize); - if (bytesRead > 0) { - if (updateCrc) { - crc.Update(buffer, 0, bytesRead); + if (bytesRead > 0) + { + if (updateCrc) + { + crc.Update(new ArraySegment(buffer, 0, bytesRead)); } stream.Position = destinationPosition; stream.Write(buffer, 0, bytesRead); @@ -2162,22 +2665,23 @@ void CopyEntryDataDirect(ZipUpdate update, Stream stream, bool updateCrc, ref lo } while ((bytesRead > 0) && (bytesToCopy > 0)); - if (totalBytesRead != targetBytes) { + if (totalBytesRead != targetBytes) + { throw new ZipException(string.Format("Failed to copy bytes expected {0} read {1}", targetBytes, totalBytesRead)); } - if (updateCrc) { + if (updateCrc) + { update.OutEntry.Crc = crc.Value; } } - int FindExistingUpdate(ZipEntry entry) + private int FindExistingUpdate(ZipEntry entry) { int result = -1; - string convertedName = GetTransformedFileName(entry.Name); - - if (updateIndex_.ContainsKey(convertedName)) { - result = (int)updateIndex_[convertedName]; + if (updateIndex_.ContainsKey(entry.Name)) + { + result = (int)updateIndex_[entry.Name]; } /* // This is slow like the coming of the next ice age but takes less storage and may be useful @@ -2195,13 +2699,14 @@ int FindExistingUpdate(ZipEntry entry) return result; } - int FindExistingUpdate(string fileName) + private int FindExistingUpdate(string fileName, bool isEntryName = false) { int result = -1; - string convertedName = GetTransformedFileName(fileName); + string convertedName = !isEntryName ? GetTransformedFileName(fileName) : fileName; - if (updateIndex_.ContainsKey(convertedName)) { + if (updateIndex_.ContainsKey(convertedName)) + { result = (int)updateIndex_[convertedName]; } @@ -2225,51 +2730,82 @@ int FindExistingUpdate(string fileName) /// /// The entry to get an output stream for. /// The output stream obtained for the entry. - Stream GetOutputStream(ZipEntry entry) + private Stream GetOutputStream(ZipEntry entry) { Stream result = baseStream_; - if (entry.IsCrypted == true) { + if (entry.IsCrypted == true) + { result = CreateAndInitEncryptionStream(result, entry); } - switch (entry.CompressionMethod) { + switch (entry.CompressionMethod) + { case CompressionMethod.Stored: - result = new UncompressedStream(result); + if (!entry.IsCrypted) + { + // If there is an encryption stream in use, that can be returned directly + // otherwise, wrap the base stream in an UncompressedStream instead of returning it directly + result = new UncompressedStream(result); + } break; case CompressionMethod.Deflated: - var dos = new DeflaterOutputStream(result, new Deflater(9, true)); - dos.IsStreamOwner = false; + var dos = new DeflaterOutputStream(result, new Deflater(9, true)) + { + // If there is an encryption stream in use, then we want that to be disposed when the deflator stream is disposed + // If not, then we don't want it to dispose the base stream + IsStreamOwner = entry.IsCrypted + }; result = dos; break; + case CompressionMethod.BZip2: + var bzos = new BZip2.BZip2OutputStream(result) + { + // If there is an encryption stream in use, then we want that to be disposed when the BZip2OutputStream stream is disposed + // If not, then we don't want it to dispose the base stream + IsStreamOwner = entry.IsCrypted + }; + result = bzos; + break; + default: throw new ZipException("Unknown compression method " + entry.CompressionMethod); } return result; } - void AddEntry(ZipFile workFile, ZipUpdate update) + private void AddEntry(ZipFile workFile, ZipUpdate update) { Stream source = null; - if (update.Entry.IsFile) { + if (update.Entry.IsFile) + { source = update.GetSource(); - if (source == null) { + if (source == null) + { source = updateDataSource_.GetSource(update.Entry, update.Filename); } } - if (source != null) { - using (source) { + var useCrc = update.Entry.AESKeySize == 0; + + if (source != null) + { + using (source) + { long sourceStreamLength = source.Length; - if (update.OutEntry.Size < 0) { + if (update.OutEntry.Size < 0) + { update.OutEntry.Size = sourceStreamLength; - } else { + } + else + { // Check for errant entries. - if (update.OutEntry.Size != sourceStreamLength) { + if (update.OutEntry.Size != sourceStreamLength) + { throw new ZipException("Entry size/stream size mismatch"); } } @@ -2278,34 +2814,39 @@ void AddEntry(ZipFile workFile, ZipUpdate update) long dataStart = workFile.baseStream_.Position; - using (Stream output = workFile.GetOutputStream(update.OutEntry)) { - CopyBytes(update, output, source, sourceStreamLength, true); + using (Stream output = workFile.GetOutputStream(update.OutEntry)) + { + CopyBytes(update, output, source, sourceStreamLength, useCrc); } long dataEnd = workFile.baseStream_.Position; update.OutEntry.CompressedSize = dataEnd - dataStart; - if ((update.OutEntry.Flags & (int)GeneralBitFlags.Descriptor) == (int)GeneralBitFlags.Descriptor) { - var helper = new ZipHelperStream(workFile.baseStream_); - helper.WriteDataDescriptor(update.OutEntry); + if ((update.OutEntry.Flags & (int)GeneralBitFlags.Descriptor) == (int)GeneralBitFlags.Descriptor) + { + ZipFormat.WriteDataDescriptor(workFile.baseStream_, update.OutEntry); } } - } else { + } + else + { workFile.WriteLocalEntryHeader(update); update.OutEntry.CompressedSize = 0; } - } - void ModifyEntry(ZipFile workFile, ZipUpdate update) + private void ModifyEntry(ZipFile workFile, ZipUpdate update) { workFile.WriteLocalEntryHeader(update); long dataStart = workFile.baseStream_.Position; // TODO: This is slow if the changes don't effect the data!! - if (update.Entry.IsFile && (update.Filename != null)) { - using (Stream output = workFile.GetOutputStream(update.OutEntry)) { - using (Stream source = this.GetInputStream(update.Entry)) { + if (update.Entry.IsFile && (update.Filename != null)) + { + using (Stream output = workFile.GetOutputStream(update.OutEntry)) + { + using (Stream source = this.GetInputStream(update.Entry)) + { CopyBytes(update, output, source, source.Length, true); } } @@ -2315,11 +2856,12 @@ void ModifyEntry(ZipFile workFile, ZipUpdate update) update.Entry.CompressedSize = dataEnd - dataStart; } - void CopyEntryDirect(ZipFile workFile, ZipUpdate update, ref long destinationPosition) + private void CopyEntryDirect(ZipFile workFile, ZipUpdate update, ref long destinationPosition) { bool skipOver = false || update.Entry.Offset == destinationPosition; - if (!skipOver) { + if (!skipOver) + { baseStream_.Position = destinationPosition; workFile.WriteLocalEntryHeader(update); destinationPosition = baseStream_.Position; @@ -2336,34 +2878,51 @@ void CopyEntryDirect(ZipFile workFile, ZipUpdate update, ref long destinationPos // Clumsy way of handling retrieving the original name and extra data length for now. // TODO: Stop re-reading name and data length in CopyEntryDirect. + uint nameLength = ReadLEUshort(); uint extraLength = ReadLEUshort(); sourcePosition = baseStream_.Position + nameLength + extraLength; - if (skipOver) { + if (skipOver) + { if (update.OffsetBasedSize != -1) + { destinationPosition += update.OffsetBasedSize; + } else - // TODO: Find out why this calculation comes up 4 bytes short on some entries in ODT (Office Document Text) archives. - // WinZip produces a warning on these entries: - // "caution: value of lrec.csize (compressed size) changed from ..." - destinationPosition += - (sourcePosition - entryDataOffset) + NameLengthOffset + // Header size - update.Entry.CompressedSize + GetDescriptorSize(update); - } else { - if (update.Entry.CompressedSize > 0) { + { + // Skip entry header + destinationPosition += (sourcePosition - entryDataOffset) + NameLengthOffset; + + // Skip entry compressed data + destinationPosition += update.Entry.CompressedSize; + + // Seek to end of entry to check for descriptor signature + baseStream_.Seek(destinationPosition, SeekOrigin.Begin); + + var descriptorHasSignature = ReadLEUint() == ZipConstants.DataDescriptorSignature; + + // Skip descriptor and it's signature (if present) + destinationPosition += GetDescriptorSize(update, descriptorHasSignature); + } + } + else + { + if (update.Entry.CompressedSize > 0) + { CopyEntryDataDirect(update, baseStream_, false, ref destinationPosition, ref sourcePosition); } CopyDescriptorBytesDirect(update, baseStream_, ref destinationPosition, sourcePosition); } } - void CopyEntry(ZipFile workFile, ZipUpdate update) + private void CopyEntry(ZipFile workFile, ZipUpdate update) { workFile.WriteLocalEntryHeader(update); - if (update.Entry.CompressedSize > 0) { + if (update.Entry.CompressedSize > 0) + { const int NameLengthOffset = 26; long entryDataOffset = update.Entry.Offset + NameLengthOffset; @@ -2381,62 +2940,65 @@ void CopyEntry(ZipFile workFile, ZipUpdate update) CopyDescriptorBytes(update, workFile.baseStream_, baseStream_); } - void Reopen(Stream source) + private void Reopen(Stream source) { - if (source == null) { - throw new ZipException("Failed to reopen archive - no source"); - } - isNewArchive_ = false; - baseStream_ = source; + baseStream_ = source ?? throw new ZipException("Failed to reopen archive - no source"); ReadEntries(); } - void Reopen() + private void Reopen() { - if (Name == null) { + if (Name == null) + { throw new InvalidOperationException("Name is not known cannot Reopen"); } Reopen(File.Open(Name, FileMode.Open, FileAccess.Read, FileShare.Read)); } - void UpdateCommentOnly() + private void UpdateCommentOnly() { long baseLength = baseStream_.Length; - ZipHelperStream updateFile = null; + Stream updateFile; - if (archiveStorage_.UpdateMode == FileUpdateMode.Safe) { - Stream copyStream = archiveStorage_.MakeTemporaryCopy(baseStream_); - updateFile = new ZipHelperStream(copyStream); - updateFile.IsStreamOwner = true; + if (archiveStorage_.UpdateMode == FileUpdateMode.Safe) + { + updateFile = archiveStorage_.MakeTemporaryCopy(baseStream_); baseStream_.Dispose(); baseStream_ = null; - } else { - if (archiveStorage_.UpdateMode == FileUpdateMode.Direct) { + } + else + { + if (archiveStorage_.UpdateMode == FileUpdateMode.Direct) + { // TODO: archiveStorage wasnt originally intended for this use. - // Need to revisit this to tidy up handling as archive storage currently doesnt + // Need to revisit this to tidy up handling as archive storage currently doesnt // handle the original stream well. // The problem is when using an existing zip archive with an in memory archive storage. // The open stream wont support writing but the memory storage should open the same file not an in memory one. // Need to tidy up the archive storage interface and contract basically. baseStream_ = archiveStorage_.OpenForDirectUpdate(baseStream_); - updateFile = new ZipHelperStream(baseStream_); - } else { + updateFile = baseStream_; + } + else + { baseStream_.Dispose(); baseStream_ = null; - updateFile = new ZipHelperStream(Name); + updateFile = new FileStream(Name, FileMode.Open, FileAccess.ReadWrite); } } - using (updateFile) { + try + { long locatedCentralDirOffset = - updateFile.LocateBlockWithSignature(ZipConstants.EndOfCentralDirectorySignature, - baseLength, ZipConstants.EndOfCentralRecordBaseSize, 0xffff); - if (locatedCentralDirOffset < 0) { + ZipFormat.LocateBlockWithSignature(updateFile, ZipConstants.EndOfCentralDirectorySignature, + baseLength, ZipConstants.EndOfCentralRecordBaseSize, 0xffff); + if (locatedCentralDirOffset < 0) + { throw new ZipException("Cannot find central directory"); } @@ -2449,10 +3011,18 @@ void UpdateCommentOnly() updateFile.Write(rawComment, 0, rawComment.Length); updateFile.SetLength(updateFile.Position); } + finally + { + if(updateFile != baseStream_) + updateFile.Dispose(); + } - if (archiveStorage_.UpdateMode == FileUpdateMode.Safe) { + if (archiveStorage_.UpdateMode == FileUpdateMode.Safe) + { Reopen(archiveStorage_.ConvertTemporaryToFinal()); - } else { + } + else + { ReadEntries(); } } @@ -2460,10 +3030,10 @@ void UpdateCommentOnly() /// /// Class used to sort updates. /// - class UpdateComparer : IComparer + private class UpdateComparer : IComparer { /// - /// Compares two objects and returns a value indicating whether one is + /// Compares two objects and returns a value indicating whether one is /// less than, equal to or greater than the other. /// /// First object to compare @@ -2473,26 +3043,40 @@ public int Compare(ZipUpdate x, ZipUpdate y) { int result; - if (x == null) { - if (y == null) { + if (x == null) + { + if (y == null) + { result = 0; - } else { + } + else + { result = -1; } - } else if (y == null) { + } + else if (y == null) + { result = 1; - } else { + } + else + { int xCmdValue = ((x.Command == UpdateCommand.Copy) || (x.Command == UpdateCommand.Modify)) ? 0 : 1; int yCmdValue = ((y.Command == UpdateCommand.Copy) || (y.Command == UpdateCommand.Modify)) ? 0 : 1; result = xCmdValue - yCmdValue; - if (result == 0) { + if (result == 0) + { long offsetDiff = x.Entry.Offset - y.Entry.Offset; - if (offsetDiff < 0) { + if (offsetDiff < 0) + { result = -1; - } else if (offsetDiff == 0) { + } + else if (offsetDiff == 0) + { result = 0; - } else { + } + else + { result = 1; } } @@ -2501,7 +3085,7 @@ public int Compare(ZipUpdate x, ZipUpdate y) } } - void RunUpdates() + private void RunUpdates() { long sizeEntries = 0; long endOfStream = 0; @@ -2510,11 +3094,14 @@ void RunUpdates() ZipFile workFile; - if (IsNewArchive) { + if (IsNewArchive) + { workFile = this; workFile.baseStream_.Position = 0; directUpdate = true; - } else if (archiveStorage_.UpdateMode == FileUpdateMode.Direct) { + } + else if (archiveStorage_.UpdateMode == FileUpdateMode.Direct) + { workFile = this; workFile.baseStream_.Position = 0; directUpdate = true; @@ -2522,23 +3109,33 @@ void RunUpdates() // Sort the updates by offset within copies/modifies, then adds. // This ensures that data required by copies will not be overwritten. updates_.Sort(new UpdateComparer()); - } else { + } + else + { workFile = ZipFile.Create(archiveStorage_.GetTemporaryOutput()); workFile.UseZip64 = UseZip64; - if (key != null) { + if (key != null) + { workFile.key = (byte[])key.Clone(); } } - try { - foreach (ZipUpdate update in updates_) { - if (update != null) { - switch (update.Command) { + try + { + foreach (ZipUpdate update in updates_) + { + if (update != null) + { + switch (update.Command) + { case UpdateCommand.Copy: - if (directUpdate) { + if (directUpdate) + { CopyEntryDirect(workFile, update, ref destinationPosition); - } else { + } + else + { CopyEntry(workFile, update); } break; @@ -2549,13 +3146,15 @@ void RunUpdates() break; case UpdateCommand.Add: - if (!IsNewArchive && directUpdate) { + if (!IsNewArchive && directUpdate) + { workFile.baseStream_.Position = destinationPosition; } AddEntry(workFile, update); - if (directUpdate) { + if (directUpdate) + { destinationPosition = workFile.baseStream_.Position; } break; @@ -2563,82 +3162,100 @@ void RunUpdates() } } - if (!IsNewArchive && directUpdate) { + if (!IsNewArchive && directUpdate) + { workFile.baseStream_.Position = destinationPosition; } long centralDirOffset = workFile.baseStream_.Position; - foreach (ZipUpdate update in updates_) { - if (update != null) { + foreach (ZipUpdate update in updates_) + { + if (update != null) + { sizeEntries += workFile.WriteCentralDirectoryHeader(update.OutEntry); } } - byte[] theComment = (newComment_ != null) ? newComment_.RawComment : ZipConstants.ConvertToArray(comment_); - using (ZipHelperStream zhs = new ZipHelperStream(workFile.baseStream_)) { - zhs.WriteEndOfCentralDirectory(updateCount_, sizeEntries, centralDirOffset, theComment); - } + byte[] theComment = newComment_?.RawComment ?? _stringCodec.ZipArchiveCommentEncoding.GetBytes(comment_); + ZipFormat.WriteEndOfCentralDirectory(workFile.baseStream_, updateCount_, + sizeEntries, centralDirOffset, theComment); endOfStream = workFile.baseStream_.Position; // And now patch entries... - foreach (ZipUpdate update in updates_) { - if (update != null) { + foreach (ZipUpdate update in updates_) + { + if (update != null) + { // If the size of the entry is zero leave the crc as 0 as well. // The calculated crc will be all bits on... - if ((update.CrcPatchOffset > 0) && (update.OutEntry.CompressedSize > 0)) { + if ((update.CrcPatchOffset > 0) && (update.OutEntry.CompressedSize > 0)) + { workFile.baseStream_.Position = update.CrcPatchOffset; workFile.WriteLEInt((int)update.OutEntry.Crc); } - if (update.SizePatchOffset > 0) { + if (update.SizePatchOffset > 0) + { workFile.baseStream_.Position = update.SizePatchOffset; - if (update.OutEntry.LocalHeaderRequiresZip64) { + if (update.OutEntry.LocalHeaderRequiresZip64) + { workFile.WriteLeLong(update.OutEntry.Size); workFile.WriteLeLong(update.OutEntry.CompressedSize); - } else { + } + else + { workFile.WriteLEInt((int)update.OutEntry.CompressedSize); workFile.WriteLEInt((int)update.OutEntry.Size); } } } } - } catch { + } + catch + { workFile.Close(); - if (!directUpdate && (workFile.Name != null)) { + if (!directUpdate && (workFile.Name != null)) + { File.Delete(workFile.Name); } throw; } - if (directUpdate) { + if (directUpdate) + { workFile.baseStream_.SetLength(endOfStream); workFile.baseStream_.Flush(); isNewArchive_ = false; ReadEntries(); - } else { + } + else + { baseStream_.Dispose(); Reopen(archiveStorage_.ConvertTemporaryToFinal()); } } - void CheckUpdating() + private void CheckUpdating() { - if (updates_ == null) { + if (updates_ == null) + { throw new InvalidOperationException("BeginUpdate has not been called"); } } - #endregion + #endregion Update Support #region ZipUpdate class + /// /// Represents a pending update to a Zip file. /// - class ZipUpdate + private class ZipUpdate { #region Constructors + public ZipUpdate(string fileName, ZipEntry entry) { command_ = UpdateCommand.Add; @@ -2650,8 +3267,10 @@ public ZipUpdate(string fileName, ZipEntry entry) public ZipUpdate(string fileName, string entryName, CompressionMethod compressionMethod) { command_ = UpdateCommand.Add; - entry_ = new ZipEntry(entryName); - entry_.CompressionMethod = compressionMethod; + entry_ = new ZipEntry(entryName) + { + CompressionMethod = compressionMethod + }; filename_ = fileName; } @@ -2666,8 +3285,10 @@ public ZipUpdate(string fileName, string entryName) public ZipUpdate(IStaticDataSource dataSource, string entryName, CompressionMethod compressionMethod) { command_ = UpdateCommand.Add; - entry_ = new ZipEntry(entryName); - entry_.CompressionMethod = compressionMethod; + entry_ = new ZipEntry(entryName) + { + CompressionMethod = compressionMethod + }; dataSource_ = dataSource; } @@ -2694,7 +3315,6 @@ public ZipUpdate(UpdateCommand command, ZipEntry entry) entry_ = (ZipEntry)entry.Clone(); } - /// /// Copy an existing entry. /// @@ -2704,22 +3324,27 @@ public ZipUpdate(ZipEntry entry) { // Do nothing. } - #endregion + + #endregion Constructors /// /// Get the for this update. /// /// This is the source or original entry. - public ZipEntry Entry { + public ZipEntry Entry + { get { return entry_; } } /// /// Get the that will be written to the updated/new file. /// - public ZipEntry OutEntry { - get { - if (outEntry_ == null) { + public ZipEntry OutEntry + { + get + { + if (outEntry_ == null) + { outEntry_ = (ZipEntry)entry_.Clone(); } @@ -2730,21 +3355,24 @@ public ZipEntry OutEntry { /// /// Get the command for this update. /// - public UpdateCommand Command { + public UpdateCommand Command + { get { return command_; } } /// /// Get the filename if any for this update. Null if none exists. /// - public string Filename { + public string Filename + { get { return filename_; } } /// /// Get/set the location of the size patch for this update. /// - public long SizePatchOffset { + public long SizePatchOffset + { get { return sizePatchOffset_; } set { sizePatchOffset_ = value; } } @@ -2752,7 +3380,8 @@ public long SizePatchOffset { /// /// Get /set the location of the crc patch for this update. /// - public long CrcPatchOffset { + public long CrcPatchOffset + { get { return crcPatchOffset_; } set { crcPatchOffset_ = value; } } @@ -2761,7 +3390,8 @@ public long CrcPatchOffset { /// Get/set the size calculated by offset. /// Specifically, the difference between this and next entry's starting offset. /// - public long OffsetBasedSize { + public long OffsetBasedSize + { get { return _offsetBasedSize; } set { _offsetBasedSize = value; } } @@ -2769,7 +3399,8 @@ public long OffsetBasedSize { public Stream GetSource() { Stream result = null; - if (dataSource_ != null) { + if (dataSource_ != null) + { result = dataSource_.GetSource(); } @@ -2777,37 +3408,45 @@ public Stream GetSource() } #region Instance Fields - ZipEntry entry_; - ZipEntry outEntry_; - UpdateCommand command_; - IStaticDataSource dataSource_; - string filename_; - long sizePatchOffset_ = -1; - long crcPatchOffset_ = -1; - long _offsetBasedSize = -1; - #endregion + + private ZipEntry entry_; + private ZipEntry outEntry_; + private readonly UpdateCommand command_; + private IStaticDataSource dataSource_; + private readonly string filename_; + private long sizePatchOffset_ = -1; + private long crcPatchOffset_ = -1; + private long _offsetBasedSize = -1; + + #endregion Instance Fields } - #endregion - #endregion + #endregion ZipUpdate class + + #endregion Updating #region Disposing #region IDisposable Members + void IDisposable.Dispose() { Close(); } - #endregion - void DisposeInternal(bool disposing) + #endregion IDisposable Members + + private void DisposeInternal(bool disposing) { - if (!isDisposed_) { + if (!isDisposed_) + { isDisposed_ = true; - entries_ = new ZipEntry[0]; + entries_ = Empty.Array(); - if (IsStreamOwner && (baseStream_ != null)) { - lock (baseStream_) { + if (IsStreamOwner && (baseStream_ != null)) + { + lock (baseStream_) + { baseStream_.Dispose(); } } @@ -2826,10 +3465,12 @@ protected virtual void Dispose(bool disposing) DisposeInternal(disposing); } - #endregion + #endregion Disposing #region Internal routines + #region Reading + /// /// Read an unsigned short in little endian byte order. /// @@ -2837,21 +3478,22 @@ protected virtual void Dispose(bool disposing) /// /// The stream ends prematurely /// - ushort ReadLEUshort() + private ushort ReadLEUshort() { int data1 = baseStream_.ReadByte(); - if (data1 < 0) { + if (data1 < 0) + { throw new EndOfStreamException("End of stream"); } int data2 = baseStream_.ReadByte(); - if (data2 < 0) { + if (data2 < 0) + { throw new EndOfStreamException("End of stream"); } - return unchecked((ushort)((ushort)data1 | (ushort)(data2 << 8))); } @@ -2865,24 +3507,21 @@ ushort ReadLEUshort() /// /// The file ends prematurely /// - uint ReadLEUint() + private uint ReadLEUint() { return (uint)(ReadLEUshort() | (ReadLEUshort() << 16)); } - ulong ReadLEUlong() + private ulong ReadLEUlong() { return ReadLEUint() | ((ulong)ReadLEUint() << 32); } - #endregion + #endregion Reading + // NOTE this returns the offset of the first byte after the signature. - long LocateBlockWithSignature(int signature, long endLocation, int minimumBlockSize, int maximumVariableData) - { - using (ZipHelperStream les = new ZipHelperStream(baseStream_)) { - return les.LocateBlockWithSignature(signature, endLocation, minimumBlockSize, maximumVariableData); - } - } + private long LocateBlockWithSignature(int signature, long endLocation, int minimumBlockSize, int maximumVariableData) + => ZipFormat.LocateBlockWithSignature(baseStream_, signature, endLocation, minimumBlockSize, maximumVariableData); /// /// Search for and read the central directory of a zip file filling the entries array. @@ -2893,25 +3532,27 @@ long LocateBlockWithSignature(int signature, long endLocation, int minimumBlockS /// /// The central directory is malformed or cannot be found /// - void ReadEntries() + private void ReadEntries() { // Search for the End Of Central Directory. When a zip comment is // present the directory will start earlier - // + // // The search is limited to 64K which is the maximum size of a trailing comment field to aid speed. // This should be compatible with both SFX and ZIP files but has only been tested for Zip files - // If a SFX file has the Zip data attached as a resource and there are other resources occuring later then + // If a SFX file has the Zip data attached as a resource and there are other resources occurring later then // this could be invalid. - // Could also speed this up by reading memory in larger blocks. + // Could also speed this up by reading memory in larger blocks. - if (baseStream_.CanSeek == false) { + if (baseStream_.CanSeek == false) + { throw new ZipException("ZipFile stream must be seekable"); } long locatedEndOfCentralDir = LocateBlockWithSignature(ZipConstants.EndOfCentralDirectorySignature, baseStream_.Length, ZipConstants.EndOfCentralRecordBaseSize, 0xffff); - if (locatedEndOfCentralDir < 0) { + if (locatedEndOfCentralDir < 0) + { throw new ZipException("Cannot find central directory"); } @@ -2924,34 +3565,53 @@ void ReadEntries() long offsetOfCentralDir = ReadLEUint(); uint commentSize = ReadLEUshort(); - if (commentSize > 0) { + if (commentSize > 0) + { byte[] comment = new byte[commentSize]; StreamUtils.ReadFully(baseStream_, comment); - comment_ = ZipConstants.ConvertToString(comment); - } else { + comment_ = _stringCodec.ZipArchiveCommentEncoding.GetString(comment); + } + else + { comment_ = string.Empty; } bool isZip64 = false; - + // Check if zip64 header information is required. - if ((thisDiskNumber == 0xffff) || - (startCentralDirDisk == 0xffff) || - (entriesForThisDisk == 0xffff) || - (entriesForWholeCentralDir == 0xffff) || - (centralDirSize == 0xffffffff) || - (offsetOfCentralDir == 0xffffffff)) { - isZip64 = true; - - long offset = LocateBlockWithSignature(ZipConstants.Zip64CentralDirLocatorSignature, locatedEndOfCentralDir, 0, 0x1000); - if (offset < 0) { + bool requireZip64 = thisDiskNumber == 0xffff || + startCentralDirDisk == 0xffff || + entriesForThisDisk == 0xffff || + entriesForWholeCentralDir == 0xffff || + centralDirSize == 0xffffffff || + offsetOfCentralDir == 0xffffffff; + + // #357 - always check for the existence of the Zip64 central directory. + // #403 - Take account of the fixed size of the locator when searching. + // Subtract from locatedEndOfCentralDir so that the endLocation is the location of EndOfCentralDirectorySignature, + // rather than the data following the signature. + long locatedZip64EndOfCentralDirLocator = LocateBlockWithSignature( + ZipConstants.Zip64CentralDirLocatorSignature, + locatedEndOfCentralDir - 4, + ZipConstants.Zip64EndOfCentralDirectoryLocatorSize, + 0); + + if (locatedZip64EndOfCentralDirLocator < 0) + { + if (requireZip64) + { + // This is only an error in cases where the Zip64 directory is required. throw new ZipException("Cannot find Zip64 locator"); } + } + else + { + isZip64 = true; - // number of the disk with the start of the zip64 end of central directory 4 bytes - // relative offset of the zip64 end of central directory record 8 bytes - // total number of disks 4 bytes + // number of the disk with the start of the zip64 end of central directory 4 bytes + // relative offset of the zip64 end of central directory record 8 bytes + // total number of disks 4 bytes ReadLEUint(); // startDisk64 is not currently used ulong offset64 = ReadLEUlong(); uint totalDisks = ReadLEUint(); @@ -2959,8 +3619,9 @@ void ReadEntries() baseStream_.Position = (long)offset64; long sig64 = ReadLEUint(); - if (sig64 != ZipConstants.Zip64CentralFileHeaderSignature) { - throw new ZipException(string.Format("Invalid Zip64 Central directory signature at {0:X}", offset64)); + if (sig64 != ZipConstants.Zip64CentralFileHeaderSignature) + { + throw new ZipException($"Invalid Zip64 Central directory signature at {offset64:X}"); } // NOTE: Record size = SizeOfFixedFields + SizeOfVariableData - 12. @@ -2985,17 +3646,21 @@ void ReadEntries() // Zip files created by some archivers have the offsets altered to reflect the true offsets // and so dont require any adjustment here... // TODO: Difficulty with Zip64 and SFX offset handling needs resolution - maths? - if (!isZip64 && (offsetOfCentralDir < locatedEndOfCentralDir - (4 + (long)centralDirSize))) { + if (!isZip64 && (offsetOfCentralDir < locatedEndOfCentralDir - (4 + (long)centralDirSize))) + { offsetOfFirstEntry = locatedEndOfCentralDir - (4 + (long)centralDirSize + offsetOfCentralDir); - if (offsetOfFirstEntry <= 0) { + if (offsetOfFirstEntry <= 0) + { throw new ZipException("Invalid embedded zip archive"); } } baseStream_.Seek(offsetOfFirstEntry + offsetOfCentralDir, SeekOrigin.Begin); - for (ulong i = 0; i < entriesForThisDisk; i++) { - if (ReadLEUint() != ZipConstants.CentralHeaderSignature) { + for (ulong i = 0; i < entriesForThisDisk; i++) + { + if (ReadLEUint() != ZipConstants.CentralHeaderSignature) + { throw new ZipException("Wrong Central Directory signature"); } @@ -3011,34 +3676,45 @@ void ReadEntries() int extraLen = ReadLEUshort(); int commentLen = ReadLEUshort(); - int diskStartNo = ReadLEUshort(); // Not currently used - int internalAttributes = ReadLEUshort(); // Not currently used + + // ReSharper disable once UnusedVariable, Currently unused but needs to be read to offset the stream + int diskStartNo = ReadLEUshort(); + // ReSharper disable once UnusedVariable, Currently unused but needs to be read to offset the stream + int internalAttributes = ReadLEUshort(); uint externalAttributes = ReadLEUint(); long offset = ReadLEUint(); byte[] buffer = new byte[Math.Max(nameLen, commentLen)]; + var entryEncoding = _stringCodec.ZipInputEncoding(bitFlags); StreamUtils.ReadFully(baseStream_, buffer, 0, nameLen); - string name = ZipConstants.ConvertToStringExt(bitFlags, buffer, nameLen); - - var entry = new ZipEntry(name, versionToExtract, versionMadeBy, (CompressionMethod)method); - entry.Crc = crc & 0xffffffffL; - entry.Size = size & 0xffffffffL; - entry.CompressedSize = csize & 0xffffffffL; - entry.Flags = bitFlags; - entry.DosTime = (uint)dostime; - entry.ZipFileIndex = (long)i; - entry.Offset = offset; - entry.ExternalFileAttributes = (int)externalAttributes; - - if ((bitFlags & 8) == 0) { + string name = entryEncoding.GetString(buffer, 0, nameLen); + var unicode = entryEncoding.IsZipUnicode(); + + var entry = new ZipEntry(name, versionToExtract, versionMadeBy, (CompressionMethod)method, unicode) + { + Crc = crc & 0xffffffffL, + Size = size & 0xffffffffL, + CompressedSize = csize & 0xffffffffL, + Flags = bitFlags, + DosTime = dostime, + ZipFileIndex = (long)i, + Offset = offset, + ExternalFileAttributes = (int)externalAttributes + }; + + if (!entry.HasFlag(GeneralBitFlags.Descriptor)) + { entry.CryptoCheckValue = (byte)(crc >> 24); - } else { + } + else + { entry.CryptoCheckValue = (byte)((dostime >> 8) & 0xff); } - if (extraLen > 0) { + if (extraLen > 0) + { byte[] extra = new byte[extraLen]; StreamUtils.ReadFully(baseStream_, extra); entry.ExtraData = extra; @@ -3046,9 +3722,10 @@ void ReadEntries() entry.ProcessExtraData(false); - if (commentLen > 0) { + if (commentLen > 0) + { StreamUtils.ReadFully(baseStream_, buffer, 0, commentLen); - entry.Comment = ZipConstants.ConvertToStringExt(bitFlags, buffer, commentLen); + entry.Comment = entryEncoding.GetString(buffer, 0, commentLen); } entries_[i] = entry; @@ -3068,49 +3745,70 @@ void ReadEntries() /// The local header signature is invalid, the entry and central header file name lengths are different /// or the local and entry compression methods dont match /// - long LocateEntry(ZipEntry entry) + private long LocateEntry(ZipEntry entry) { - return TestLocalHeader(entry, HeaderTest.Extract); + return TestLocalHeader(entry, SkipLocalEntryTestsOnLocate ? HeaderTest.None : HeaderTest.Extract); } - Stream CreateAndInitDecryptionStream(Stream baseStream, ZipEntry entry) + /// + /// Skip the verification of the local header when reading an archive entry. Set this to attempt to read the + /// entries even if the headers should indicate that doing so would fail or produce an unexpected output. + /// + public bool SkipLocalEntryTestsOnLocate { get; set; } = false; + + private Stream CreateAndInitDecryptionStream(Stream baseStream, ZipEntry entry) { CryptoStream result = null; - if ((entry.Version < ZipConstants.VersionStrongEncryption) - || (entry.Flags & (int)GeneralBitFlags.StrongEncryption) == 0) { - var classicManaged = new PkzipClassicManaged(); - - OnKeysRequired(entry.Name); - if (HaveKeys == false) { - throw new ZipException("No password available for encrypted stream"); - } - - result = new CryptoStream(baseStream, classicManaged.CreateDecryptor(key, null), CryptoStreamMode.Read); - CheckClassicPassword(result, entry); - } else { - if (entry.Version == ZipConstants.VERSION_AES) { - // + if (entry.CompressionMethodForHeader == CompressionMethod.WinZipAES) + { + if (entry.Version >= ZipConstants.VERSION_AES) + { + // Issue #471 - accept an empty string as a password, but reject null. OnKeysRequired(entry.Name); - if (HaveKeys == false) { + if (rawPassword_ == null) + { throw new ZipException("No password available for AES encrypted stream"); } int saltLen = entry.AESSaltLen; byte[] saltBytes = new byte[saltLen]; - int saltIn = baseStream.Read(saltBytes, 0, saltLen); - if (saltIn != saltLen) - throw new ZipException("AES Salt expected " + saltLen + " got " + saltIn); - // + int saltIn = StreamUtils.ReadRequestedBytes(baseStream, saltBytes, offset: 0, saltLen); + + if (saltIn != saltLen) throw new ZipException($"AES Salt expected {saltLen} git {saltIn}"); + byte[] pwdVerifyRead = new byte[2]; - baseStream.Read(pwdVerifyRead, 0, 2); + StreamUtils.ReadFully(baseStream, pwdVerifyRead); int blockSize = entry.AESKeySize / 8; // bits to bytes - var decryptor = new ZipAESTransform(rawPassword_, saltBytes, blockSize, false); + var decryptor = new ZipAESTransform(rawPassword_, saltBytes, blockSize, writeMode: false); byte[] pwdVerifyCalc = decryptor.PwdVerifier; if (pwdVerifyCalc[0] != pwdVerifyRead[0] || pwdVerifyCalc[1] != pwdVerifyRead[1]) throw new ZipException("Invalid password for AES"); result = new ZipAESStream(baseStream, decryptor, CryptoStreamMode.Read); - } else { + } + else + { + throw new ZipException("Decryption method not supported"); + } + } + else + { + if (entry.Version < ZipConstants.VersionStrongEncryption || !entry.HasFlag(GeneralBitFlags.StrongEncryption)) + { + var classicManaged = new PkzipClassicManaged(); + + OnKeysRequired(entry.Name); + if (HaveKeys == false) + { + throw new ZipException("No password available for encrypted stream"); + } + + result = new CryptoStream(baseStream, classicManaged.CreateDecryptor(key, null), CryptoStreamMode.Read); + CheckClassicPassword(result, entry); + } + else + { + // We don't support PKWare strong encryption throw new ZipException("Decryption method not supported"); } } @@ -3118,125 +3816,143 @@ Stream CreateAndInitDecryptionStream(Stream baseStream, ZipEntry entry) return result; } - Stream CreateAndInitEncryptionStream(Stream baseStream, ZipEntry entry) + private Stream CreateAndInitEncryptionStream(Stream baseStream, ZipEntry entry) { - CryptoStream result = null; - if ((entry.Version < ZipConstants.VersionStrongEncryption) - || (entry.Flags & (int)GeneralBitFlags.StrongEncryption) == 0) { - var classicManaged = new PkzipClassicManaged(); + if (entry.Version >= ZipConstants.VersionStrongEncryption && + entry.HasFlag(GeneralBitFlags.StrongEncryption)) return null; - OnKeysRequired(entry.Name); - if (HaveKeys == false) { - throw new ZipException("No password available for encrypted stream"); - } + var classicManaged = new PkzipClassicManaged(); - // Closing a CryptoStream will close the base stream as well so wrap it in an UncompressedStream - // which doesnt do this. - result = new CryptoStream(new UncompressedStream(baseStream), - classicManaged.CreateEncryptor(key, null), CryptoStreamMode.Write); + OnKeysRequired(entry.Name); + if (HaveKeys == false) + { + throw new ZipException("No password available for encrypted stream"); + } - if ((entry.Crc < 0) || (entry.Flags & 8) != 0) { - WriteEncryptionHeader(result, entry.DosTime << 16); - } else { - WriteEncryptionHeader(result, entry.Crc); - } + // Closing a CryptoStream will close the base stream as well so wrap it in an UncompressedStream + // which doesnt do this. + var result = new CryptoStream(new UncompressedStream(baseStream), + classicManaged.CreateEncryptor(key, null), CryptoStreamMode.Write); + + if (entry.Crc < 0 || entry.HasFlag(GeneralBitFlags.Descriptor)) + { + WriteEncryptionHeader(result, entry.DosTime << 16); + } + else + { + WriteEncryptionHeader(result, entry.Crc); } return result; } - static void CheckClassicPassword(CryptoStream classicCryptoStream, ZipEntry entry) + private static void CheckClassicPassword(CryptoStream classicCryptoStream, ZipEntry entry) { byte[] cryptbuffer = new byte[ZipConstants.CryptoHeaderSize]; StreamUtils.ReadFully(classicCryptoStream, cryptbuffer); - if (cryptbuffer[ZipConstants.CryptoHeaderSize - 1] != entry.CryptoCheckValue) { + if (cryptbuffer[ZipConstants.CryptoHeaderSize - 1] != entry.CryptoCheckValue) + { throw new ZipException("Invalid password"); } } - static void WriteEncryptionHeader(Stream stream, long crcValue) + private static void WriteEncryptionHeader(Stream stream, long crcValue) { byte[] cryptBuffer = new byte[ZipConstants.CryptoHeaderSize]; - var rnd = new Random(); - rnd.NextBytes(cryptBuffer); + using (var rng = RandomNumberGenerator.Create()) + { + rng.GetBytes(cryptBuffer); + } cryptBuffer[11] = (byte)(crcValue >> 24); - stream.Write(cryptBuffer, 0, cryptBuffer.Length); + stream.Write(cryptBuffer, offset: 0, cryptBuffer.Length); } - #endregion + #endregion Internal routines #region Instance Fields - bool isDisposed_; - string name_; - string comment_; - string rawPassword_; - Stream baseStream_; - bool isStreamOwner; - long offsetOfFirstEntry; - ZipEntry[] entries_; - byte[] key; - bool isNewArchive_; + + private bool isDisposed_; + private string name_; + private string comment_ = string.Empty; + private string rawPassword_; + private Stream baseStream_; + private bool isStreamOwner; + private long offsetOfFirstEntry; + private ZipEntry[] entries_; + private byte[] key; + private bool isNewArchive_; + private StringCodec _stringCodec = ZipStrings.GetStringCodec(); // Default is dynamic which is not backwards compatible and can cause problems // with XP's built in compression which cant read Zip64 archives. // However it does avoid the situation were a large file is added and cannot be completed correctly. // Hint: Set always ZipEntry size before they are added to an archive and this setting isnt needed. - UseZip64 useZip64_ = UseZip64.Dynamic; + private UseZip64 useZip64_ = UseZip64.Dynamic; #region Zip Update Instance Fields - List updates_; - long updateCount_; // Count is managed manually as updates_ can contain nulls! - Dictionary updateIndex_; - IArchiveStorage archiveStorage_; - IDynamicDataSource updateDataSource_; - bool contentsEdited_; - int bufferSize_ = DefaultBufferSize; - byte[] copyBuffer_; - ZipString newComment_; - bool commentEdited_; - IEntryFactory updateEntryFactory_ = new ZipEntryFactory(); - #endregion - #endregion + + private List updates_; + private long updateCount_; // Count is managed manually as updates_ can contain nulls! + private Dictionary updateIndex_; + private IArchiveStorage archiveStorage_; + private IDynamicDataSource updateDataSource_; + private bool contentsEdited_; + private int bufferSize_ = DefaultBufferSize; + private byte[] copyBuffer_; + private ZipString newComment_; + private bool commentEdited_; + private IEntryFactory updateEntryFactory_ = new ZipEntryFactory(); + + #endregion Zip Update Instance Fields + + #endregion Instance Fields #region Support Classes + /// /// Represents a string from a which is stored as an array of bytes. /// - class ZipString + private class ZipString { #region Constructors + /// /// Initialise a with a string. /// /// The textual string form. - public ZipString(string comment) + /// + public ZipString(string comment, Encoding encoding) { comment_ = comment; isSourceString_ = true; + _encoding = encoding; } /// /// Initialise a using a string in its binary 'raw' form. /// /// - public ZipString(byte[] rawString) + /// + public ZipString(byte[] rawString, Encoding encoding) { rawComment_ = rawString; + _encoding = encoding; } - #endregion + + #endregion Constructors /// /// Get a value indicating the original source of data for this instance. /// True if the source was a string; false if the source was binary data. /// - public bool IsSourceString { - get { return isSourceString_; } - } + public bool IsSourceString => isSourceString_; /// /// Get the length of the comment when represented as raw bytes. /// - public int RawLength { - get { + public int RawLength + { + get + { MakeBytesAvailable(); return rawComment_.Length; } @@ -3245,8 +3961,10 @@ public int RawLength { /// /// Get the comment in its 'raw' form as plain bytes. /// - public byte[] RawComment { - get { + public byte[] RawComment + { + get + { MakeBytesAvailable(); return (byte[])rawComment_.Clone(); } @@ -3257,24 +3975,29 @@ public byte[] RawComment { /// public void Reset() { - if (isSourceString_) { + if (isSourceString_) + { rawComment_ = null; - } else { + } + else + { comment_ = null; } } - void MakeTextAvailable() + private void MakeTextAvailable() { - if (comment_ == null) { - comment_ = ZipConstants.ConvertToString(rawComment_); + if (comment_ == null) + { + comment_ = _encoding.GetString(rawComment_); } } - void MakeBytesAvailable() + private void MakeBytesAvailable() { - if (rawComment_ == null) { - rawComment_ = ZipConstants.ConvertToArray(comment_); + if (rawComment_ == null) + { + rawComment_ = _encoding.GetBytes(comment_); } } @@ -3283,74 +4006,104 @@ void MakeBytesAvailable() /// /// The to convert to a string. /// The textual equivalent for the input value. - static public implicit operator string(ZipString zipString) + public static implicit operator string(ZipString zipString) { zipString.MakeTextAvailable(); return zipString.comment_; } #region Instance Fields - string comment_; - byte[] rawComment_; - bool isSourceString_; - #endregion + + private string comment_; + private byte[] rawComment_; + private readonly bool isSourceString_; + private readonly Encoding _encoding; + + #endregion Instance Fields } /// /// An enumerator for Zip entries /// - class ZipEntryEnumerator : IEnumerator + public struct ZipEntryEnumerator : IEnumerator { #region Constructors + + /// + /// Constructs a new instance of . + /// + /// Entries to iterate. public ZipEntryEnumerator(ZipEntry[] entries) { array = entries; + index = -1; } - #endregion + #endregion Constructors + #region IEnumerator Members - public object Current { - get { + + /// + public ZipEntry Current + { + get + { return array[index]; } } + /// + object IEnumerator.Current => Current; + + /// public void Reset() { index = -1; } + /// public bool MoveNext() { return (++index < array.Length); } - #endregion + + /// + public void Dispose() + { + } + + #endregion IEnumerator Members + #region Instance Fields - ZipEntry[] array; - int index = -1; - #endregion + + private ZipEntry[] array; + private int index; + + #endregion Instance Fields } /// /// An is a stream that you can write uncompressed data /// to and flush, but cannot read, seek or do anything else to. /// - class UncompressedStream : Stream + private class UncompressedStream : Stream { #region Constructors + public UncompressedStream(Stream baseStream) { baseStream_ = baseStream; } - #endregion - + #endregion Constructors /// /// Gets a value indicating whether the current stream supports reading. /// - public override bool CanRead { - get { + public override bool CanRead + { + get + { return false; } } @@ -3366,8 +4119,10 @@ public override void Flush() /// /// Gets a value indicating whether the current stream supports writing. /// - public override bool CanWrite { - get { + public override bool CanWrite + { + get + { return baseStream_.CanWrite; } } @@ -3375,8 +4130,10 @@ public override bool CanWrite { /// /// Gets a value indicating whether the current stream supports seeking. /// - public override bool CanSeek { - get { + public override bool CanSeek + { + get + { return false; } } @@ -3384,8 +4141,10 @@ public override bool CanSeek { /// /// Get the length in bytes of the stream. /// - public override long Length { - get { + public override long Length + { + get + { return 0; } } @@ -3393,11 +4152,14 @@ public override long Length { /// /// Gets or sets the position within the current stream. /// - public override long Position { - get { + public override long Position + { + get + { return baseStream_.Position; } - set { + set + { throw new NotImplementedException(); } } @@ -3411,12 +4173,12 @@ public override long Position { /// /// The total number of bytes read into the buffer. This can be less than the number of bytes requested if that many bytes are not currently available, or zero (0) if the end of the stream has been reached. /// - /// The sum of offset and count is larger than the buffer length. - /// Methods were called after the stream was closed. - /// The stream does not support reading. - /// buffer is null. - /// An I/O error occurs. - /// offset or count is negative. + /// The sum of offset and count is larger than the buffer length. + /// Methods were called after the stream was closed. + /// The stream does not support reading. + /// buffer is null. + /// An I/O error occurs. + /// offset or count is negative. public override int Read(byte[] buffer, int offset, int count) { return 0; @@ -3426,13 +4188,13 @@ public override int Read(byte[] buffer, int offset, int count) /// Sets the position within the current stream. /// /// A byte offset relative to the origin parameter. - /// A value of type indicating the reference point used to obtain the new position. + /// A value of type indicating the reference point used to obtain the new position. /// /// The new position within the current stream. /// - /// An I/O error occurs. - /// The stream does not support seeking, such as if the stream is constructed from a pipe or console output. - /// Methods were called after the stream was closed. + /// An I/O error occurs. + /// The stream does not support seeking, such as if the stream is constructed from a pipe or console output. + /// Methods were called after the stream was closed. public override long Seek(long offset, SeekOrigin origin) { return 0; @@ -3442,9 +4204,9 @@ public override long Seek(long offset, SeekOrigin origin) /// Sets the length of the current stream. /// /// The desired length of the current stream in bytes. - /// The stream does not support both writing and seeking, such as if the stream is constructed from a pipe or console output. - /// An I/O error occurs. - /// Methods were called after the stream was closed. + /// The stream does not support both writing and seeking, such as if the stream is constructed from a pipe or console output. + /// An I/O error occurs. + /// Methods were called after the stream was closed. public override void SetLength(long value) { } @@ -3455,31 +4217,34 @@ public override void SetLength(long value) /// An array of bytes. This method copies count bytes from buffer to the current stream. /// The zero-based byte offset in buffer at which to begin copying bytes to the current stream. /// The number of bytes to be written to the current stream. - /// An I/O error occurs. - /// The stream does not support writing. - /// Methods were called after the stream was closed. - /// buffer is null. - /// The sum of offset and count is greater than the buffer length. - /// offset or count is negative. + /// An I/O error occurs. + /// The stream does not support writing. + /// Methods were called after the stream was closed. + /// buffer is null. + /// The sum of offset and count is greater than the buffer length. + /// offset or count is negative. public override void Write(byte[] buffer, int offset, int count) { baseStream_.Write(buffer, offset, count); } - readonly + private readonly #region Instance Fields + Stream baseStream_; - #endregion + + #endregion Instance Fields } /// /// A is an /// whose data is only a part or subsection of a file. /// - class PartialInputStream : Stream + private class PartialInputStream : Stream { #region Constructors + /// /// Initialise a new instance of the class. /// @@ -3506,7 +4271,8 @@ public PartialInputStream(ZipFile zipFile, long start, long length) readPos_ = start; end_ = start + length; } - #endregion + + #endregion Constructors /// /// Read a byte from this stream. @@ -3514,12 +4280,14 @@ public PartialInputStream(ZipFile zipFile, long start, long length) /// Returns the byte read or -1 on end of stream. public override int ReadByte() { - if (readPos_ >= end_) { + if (readPos_ >= end_) + { // -1 is the correct value at end of stream. return -1; } - lock (baseStream_) { + lock (baseStream_) + { baseStream_.Seek(readPos_++, SeekOrigin.Begin); return baseStream_.ReadByte(); } @@ -3534,28 +4302,33 @@ public override int ReadByte() /// /// The total number of bytes read into the buffer. This can be less than the number of bytes requested if that many bytes are not currently available, or zero (0) if the end of the stream has been reached. /// - /// The sum of offset and count is larger than the buffer length. - /// Methods were called after the stream was closed. - /// The stream does not support reading. - /// buffer is null. - /// An I/O error occurs. - /// offset or count is negative. + /// The sum of offset and count is larger than the buffer length. + /// Methods were called after the stream was closed. + /// The stream does not support reading. + /// buffer is null. + /// An I/O error occurs. + /// offset or count is negative. public override int Read(byte[] buffer, int offset, int count) { - lock (baseStream_) { - if (count > end_ - readPos_) { + lock (baseStream_) + { + if (count > end_ - readPos_) + { count = (int)(end_ - readPos_); - if (count == 0) { + if (count == 0) + { return 0; } } // Protect against Stream implementations that throw away their buffer on every Seek // (for example, Mono FileStream) - if (baseStream_.Position != readPos_) { + if (baseStream_.Position != readPos_) + { baseStream_.Seek(readPos_, SeekOrigin.Begin); } int readCount = baseStream_.Read(buffer, offset, count); - if (readCount > 0) { + if (readCount > 0) + { readPos_ += readCount; } return readCount; @@ -3568,12 +4341,12 @@ public override int Read(byte[] buffer, int offset, int count) /// An array of bytes. This method copies count bytes from buffer to the current stream. /// The zero-based byte offset in buffer at which to begin copying bytes to the current stream. /// The number of bytes to be written to the current stream. - /// An I/O error occurs. - /// The stream does not support writing. - /// Methods were called after the stream was closed. - /// buffer is null. - /// The sum of offset and count is greater than the buffer length. - /// offset or count is negative. + /// An I/O error occurs. + /// The stream does not support writing. + /// Methods were called after the stream was closed. + /// buffer is null. + /// The sum of offset and count is greater than the buffer length. + /// offset or count is negative. public override void Write(byte[] buffer, int offset, int count) { throw new NotSupportedException(); @@ -3583,9 +4356,9 @@ public override void Write(byte[] buffer, int offset, int count) /// When overridden in a derived class, sets the length of the current stream. /// /// The desired length of the current stream in bytes. - /// The stream does not support both writing and seeking, such as if the stream is constructed from a pipe or console output. - /// An I/O error occurs. - /// Methods were called after the stream was closed. + /// The stream does not support both writing and seeking, such as if the stream is constructed from a pipe or console output. + /// An I/O error occurs. + /// Methods were called after the stream was closed. public override void SetLength(long value) { throw new NotSupportedException(); @@ -3595,18 +4368,19 @@ public override void SetLength(long value) /// When overridden in a derived class, sets the position within the current stream. /// /// A byte offset relative to the origin parameter. - /// A value of type indicating the reference point used to obtain the new position. + /// A value of type indicating the reference point used to obtain the new position. /// /// The new position within the current stream. /// - /// An I/O error occurs. - /// The stream does not support seeking, such as if the stream is constructed from a pipe or console output. - /// Methods were called after the stream was closed. + /// An I/O error occurs. + /// The stream does not support seeking, such as if the stream is constructed from a pipe or console output. + /// Methods were called after the stream was closed. public override long Seek(long offset, SeekOrigin origin) { long newPos = readPos_; - switch (origin) { + switch (origin) + { case SeekOrigin.Begin: newPos = start_ + offset; break; @@ -3620,11 +4394,13 @@ public override long Seek(long offset, SeekOrigin origin) break; } - if (newPos < start_) { + if (newPos < start_) + { throw new ArgumentException("Negative position is invalid"); } - if (newPos >= end_) { + if (newPos > end_) + { throw new IOException("Cannot seek past end"); } readPos_ = newPos; @@ -3634,7 +4410,7 @@ public override long Seek(long offset, SeekOrigin origin) /// /// Clears all buffers for this stream and causes any buffered data to be written to the underlying device. /// - /// An I/O error occurs. + /// An I/O error occurs. public override void Flush() { // Nothing to do. @@ -3645,19 +4421,23 @@ public override void Flush() /// /// /// The current position within the stream. - /// An I/O error occurs. - /// The stream does not support seeking. - /// Methods were called after the stream was closed. - public override long Position { + /// An I/O error occurs. + /// The stream does not support seeking. + /// Methods were called after the stream was closed. + public override long Position + { get { return readPos_ - start_; } - set { + set + { long newPos = start_ + value; - if (newPos < start_) { + if (newPos < start_) + { throw new ArgumentException("Negative position is invalid"); } - if (newPos >= end_) { + if (newPos > end_) + { throw new InvalidOperationException("Cannot seek past end"); } readPos_ = newPos; @@ -3669,9 +4449,10 @@ public override long Position { /// /// /// A long value representing the length of the stream in bytes. - /// A class derived from Stream does not support seeking. - /// Methods were called after the stream was closed. - public override long Length { + /// A class derived from Stream does not support seeking. + /// Methods were called after the stream was closed. + public override long Length + { get { return length_; } } @@ -3680,7 +4461,8 @@ public override long Length { /// /// false /// true if the stream supports writing; otherwise, false. - public override bool CanWrite { + public override bool CanWrite + { get { return false; } } @@ -3689,7 +4471,8 @@ public override bool CanWrite { /// /// true /// true if the stream supports seeking; otherwise, false. - public override bool CanSeek { + public override bool CanSeek + { get { return true; } } @@ -3698,7 +4481,8 @@ public override bool CanSeek { /// /// true. /// true if the stream supports reading; otherwise, false. - public override bool CanRead { + public override bool CanRead + { get { return true; } } @@ -3707,24 +4491,30 @@ public override bool CanRead { /// /// /// A value that determines whether the current stream can time out. - public override bool CanTimeout { + public override bool CanTimeout + { get { return baseStream_.CanTimeout; } } + #region Instance Fields - ZipFile zipFile_; - Stream baseStream_; - long start_; - long length_; - long readPos_; - long end_; - #endregion + + private ZipFile zipFile_; + private Stream baseStream_; + private readonly long start_; + private readonly long length_; + private long readPos_; + private readonly long end_; + + #endregion Instance Fields } - #endregion + + #endregion Support Classes } - #endregion + #endregion ZipFile Class #region DataSources + /// /// Provides a static way to obtain a source of data for an entry. /// @@ -3760,7 +4550,7 @@ public interface IDynamicDataSource public class StaticDiskDataSource : IStaticDataSource { /// - /// Initialise a new instnace of + /// Initialise a new instance of /// /// The name of the file to obtain data from. public StaticDiskDataSource(string fileName) @@ -3773,28 +4563,30 @@ public StaticDiskDataSource(string fileName) /// /// Get a providing data. /// - /// Returns a provising data. + /// Returns a providing data. public Stream GetSource() { return File.Open(fileName_, FileMode.Open, FileAccess.Read, FileShare.Read); } - readonly + private readonly + + #endregion IDataSource Members - #endregion #region Instance Fields + string fileName_; - #endregion - } + #endregion Instance Fields + } /// /// Default implementation of for files stored on disk. /// public class DynamicDiskDataSource : IDynamicDataSource { - #region IDataSource Members + /// /// Get a providing data for an entry. /// @@ -3805,19 +4597,21 @@ public Stream GetSource(ZipEntry entry, string name) { Stream result = null; - if (name != null) { + if (name != null) + { result = File.Open(name, FileMode.Open, FileAccess.Read, FileShare.Read); } return result; } - #endregion + #endregion IDataSource Members } - #endregion + #endregion DataSources #region Archive Storage + /// /// Defines facilities for data storage when updating Zip Archives. /// @@ -3869,6 +4663,7 @@ public interface IArchiveStorage abstract public class BaseArchiveStorage : IArchiveStorage { #region Constructors + /// /// Initializes a new instance of the class. /// @@ -3877,7 +4672,8 @@ protected BaseArchiveStorage(FileUpdateMode updateMode) { updateMode_ = updateMode; } - #endregion + + #endregion Constructors #region IArchiveStorage Members @@ -3919,17 +4715,21 @@ protected BaseArchiveStorage(FileUpdateMode updateMode) /// Gets the update mode applicable. /// /// The update mode. - public FileUpdateMode UpdateMode { - get { + public FileUpdateMode UpdateMode + { + get + { return updateMode_; } } - #endregion + #endregion IArchiveStorage Members #region Instance Fields - FileUpdateMode updateMode_; - #endregion + + private readonly FileUpdateMode updateMode_; + + #endregion Instance Fields } /// @@ -3938,6 +4738,7 @@ public FileUpdateMode UpdateMode { public class DiskArchiveStorage : BaseArchiveStorage { #region Constructors + /// /// Initializes a new instance of the class. /// @@ -3946,7 +4747,8 @@ public class DiskArchiveStorage : BaseArchiveStorage public DiskArchiveStorage(ZipFile file, FileUpdateMode updateMode) : base(updateMode) { - if (file.Name == null) { + if (file.Name == null) + { throw new ZipException("Cant handle non file archives"); } @@ -3961,7 +4763,8 @@ public DiskArchiveStorage(ZipFile file) : this(file, FileUpdateMode.Safe) { } - #endregion + + #endregion Constructors #region IArchiveStorage Members @@ -3971,15 +4774,8 @@ public DiskArchiveStorage(ZipFile file) /// Returns the temporary output stream. public override Stream GetTemporaryOutput() { - if (temporaryName_ != null) { - temporaryName_ = GetTempFileName(temporaryName_, true); - temporaryStream_ = File.Open(temporaryName_, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None); - } else { - // Determine where to place files based on internal strategy. - // Currently this is always done in system temp directory. - temporaryName_ = Path.GetTempFileName(); - temporaryStream_ = File.Open(temporaryName_, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None); - } + temporaryName_ = PathUtils.GetTempFileName(temporaryName_); + temporaryStream_ = File.Open(temporaryName_, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None); return temporaryStream_; } @@ -3991,16 +4787,18 @@ public override Stream GetTemporaryOutput() /// the final storage for the archive. public override Stream ConvertTemporaryToFinal() { - if (temporaryStream_ == null) { + if (temporaryStream_ == null) + { throw new ZipException("No temporary stream has been created"); } Stream result = null; - string moveTempName = GetTempFileName(fileName_, false); + string moveTempName = PathUtils.GetTempFileName(fileName_); bool newFileCreated = false; - try { + try + { temporaryStream_.Dispose(); File.Move(fileName_, moveTempName); File.Move(temporaryName_, fileName_); @@ -4008,11 +4806,14 @@ public override Stream ConvertTemporaryToFinal() File.Delete(moveTempName); result = File.Open(fileName_, FileMode.Open, FileAccess.Read, FileShare.Read); - } catch (Exception) { + } + catch (Exception) + { result = null; // Try to roll back changes... - if (!newFileCreated) { + if (!newFileCreated) + { File.Move(moveTempName, fileName_); File.Delete(temporaryName_); } @@ -4032,7 +4833,7 @@ public override Stream MakeTemporaryCopy(Stream stream) { stream.Dispose(); - temporaryName_ = GetTempFileName(fileName_, true); + temporaryName_ = PathUtils.GetTempFileName(fileName_); File.Copy(fileName_, temporaryName_, true); temporaryStream_ = new FileStream(temporaryName_, @@ -4050,15 +4851,19 @@ public override Stream MakeTemporaryCopy(Stream stream) public override Stream OpenForDirectUpdate(Stream stream) { Stream result; - if ((stream == null) || !stream.CanWrite) { - if (stream != null) { + if ((stream == null) || !stream.CanWrite) + { + if (stream != null) + { stream.Dispose(); } result = new FileStream(fileName_, FileMode.Open, FileAccess.ReadWrite); - } else { + } + else + { result = stream; } @@ -4070,52 +4875,21 @@ public override Stream OpenForDirectUpdate(Stream stream) /// public override void Dispose() { - if (temporaryStream_ != null) { + if (temporaryStream_ != null) + { temporaryStream_.Dispose(); } } - #endregion - - #region Internal routines - static string GetTempFileName(string original, bool makeTempFile) - { - string result = null; - - if (original == null) { - result = Path.GetTempFileName(); - } else { - int counter = 0; - int suffixSeed = DateTime.Now.Second; - - while (result == null) { - counter += 1; - string newName = string.Format("{0}.{1}{2}.tmp", original, suffixSeed, counter); - if (!File.Exists(newName)) { - if (makeTempFile) { - try { - // Try and create the file. - using (FileStream stream = File.Create(newName)) { - } - result = newName; - } catch { - suffixSeed = DateTime.Now.Second; - } - } else { - result = newName; - } - } - } - } - return result; - } - #endregion + #endregion IArchiveStorage Members #region Instance Fields - Stream temporaryStream_; - string fileName_; - string temporaryName_; - #endregion + + private Stream temporaryStream_; + private readonly string fileName_; + private string temporaryName_; + + #endregion Instance Fields } /// @@ -4124,6 +4898,7 @@ static string GetTempFileName(string original, bool makeTempFile) public class MemoryArchiveStorage : BaseArchiveStorage { #region Constructors + /// /// Initializes a new instance of the class. /// @@ -4142,17 +4917,19 @@ public MemoryArchiveStorage(FileUpdateMode updateMode) { } - #endregion + #endregion Constructors #region Properties + /// /// Get the stream returned by if this was in fact called. /// - public MemoryStream FinalStream { + public MemoryStream FinalStream + { get { return finalStream_; } } - #endregion + #endregion Properties #region IArchiveStorage Members @@ -4173,7 +4950,8 @@ public override Stream GetTemporaryOutput() /// the final storage for the archive. public override Stream ConvertTemporaryToFinal() { - if (temporaryStream_ == null) { + if (temporaryStream_ == null) + { throw new ZipException("No temporary stream has been created"); } @@ -4204,17 +4982,20 @@ public override Stream MakeTemporaryCopy(Stream stream) public override Stream OpenForDirectUpdate(Stream stream) { Stream result; - if ((stream == null) || !stream.CanWrite) { - + if ((stream == null) || !stream.CanWrite) + { result = new MemoryStream(); - if (stream != null) { + if (stream != null) + { stream.Position = 0; StreamUtils.Copy(stream, result, new byte[4096]); stream.Dispose(); } - } else { + } + else + { result = stream; } @@ -4226,18 +5007,21 @@ public override Stream OpenForDirectUpdate(Stream stream) /// public override void Dispose() { - if (temporaryStream_ != null) { + if (temporaryStream_ != null) + { temporaryStream_.Dispose(); } } - #endregion + #endregion IArchiveStorage Members #region Instance Fields - MemoryStream temporaryStream_; - MemoryStream finalStream_; - #endregion + + private MemoryStream temporaryStream_; + private MemoryStream finalStream_; + + #endregion Instance Fields } - #endregion + #endregion Archive Storage } diff --git a/src/ICSharpCode.SharpZipLib/Zip/ZipFormat.cs b/src/ICSharpCode.SharpZipLib/Zip/ZipFormat.cs new file mode 100644 index 000000000..ec63d7943 --- /dev/null +++ b/src/ICSharpCode.SharpZipLib/Zip/ZipFormat.cs @@ -0,0 +1,598 @@ +using System; +using System.IO; +using System.Threading; +using System.Threading.Tasks; +using ICSharpCode.SharpZipLib.Core; + +namespace ICSharpCode.SharpZipLib.Zip +{ + /// + /// Holds data pertinent to a data descriptor. + /// + public class DescriptorData + { + private long _crc; + + /// + /// Get /set the compressed size of data. + /// + public long CompressedSize { get; set; } + + /// + /// Get / set the uncompressed size of data + /// + public long Size { get; set; } + + /// + /// Get /set the crc value. + /// + public long Crc + { + get => _crc; + set => _crc = (value & 0xffffffff); + } + } + + internal struct EntryPatchData + { + public long SizePatchOffset { get; set; } + + public long CrcPatchOffset { get; set; } + } + + /// + /// This class assists with writing/reading from Zip files. + /// + internal static class ZipFormat + { + // Write the local file header + // TODO: ZipFormat.WriteLocalHeader is not yet used and needs checking for ZipFile and ZipOuptutStream usage + internal static int WriteLocalHeader(Stream stream, ZipEntry entry, out EntryPatchData patchData, + bool headerInfoAvailable, bool patchEntryHeader, long streamOffset, StringCodec stringCodec) + { + patchData = new EntryPatchData(); + + stream.WriteLEInt(ZipConstants.LocalHeaderSignature); + stream.WriteLEShort(entry.Version); + stream.WriteLEShort(entry.Flags); + stream.WriteLEShort((byte)entry.CompressionMethodForHeader); + stream.WriteLEInt((int)entry.DosTime); + + if (headerInfoAvailable) + { + stream.WriteLEInt((int)entry.Crc); + if (entry.LocalHeaderRequiresZip64) + { + stream.WriteLEInt(-1); + stream.WriteLEInt(-1); + } + else + { + stream.WriteLEInt((int)entry.CompressedSize + entry.EncryptionOverheadSize); + stream.WriteLEInt((int)entry.Size); + } + } + else + { + if (patchEntryHeader) + patchData.CrcPatchOffset = streamOffset + stream.Position; + + stream.WriteLEInt(0); // Crc + + if (patchEntryHeader) + patchData.SizePatchOffset = streamOffset + stream.Position; + + // For local header both sizes appear in Zip64 Extended Information + if (entry.LocalHeaderRequiresZip64 && patchEntryHeader) + { + stream.WriteLEInt(-1); + stream.WriteLEInt(-1); + } + else + { + stream.WriteLEInt(0); // Compressed size + stream.WriteLEInt(0); // Uncompressed size + } + } + + byte[] name = stringCodec.ZipEncoding(entry.IsUnicodeText).GetBytes(entry.Name); + + if (name.Length > 0xFFFF) + { + throw new ZipException("Entry name too long."); + } + + var ed = new ZipExtraData(entry.ExtraData); + + if (entry.LocalHeaderRequiresZip64) + { + ed.StartNewEntry(); + if (headerInfoAvailable) + { + ed.AddLeLong(entry.Size); + ed.AddLeLong(entry.CompressedSize + entry.EncryptionOverheadSize); + } + else + { + // If the sizes are stored in the descriptor, the local Zip64 sizes should be 0 + ed.AddLeLong(0); + ed.AddLeLong(0); + } + ed.AddNewEntry(1); + + if (!ed.Find(1)) + { + throw new ZipException("Internal error cant find extra data"); + } + + patchData.SizePatchOffset = ed.CurrentReadIndex; + } + else + { + ed.Delete(1); + } + + if (entry.AESKeySize > 0) + { + AddExtraDataAES(entry, ed); + } + byte[] extra = ed.GetEntryData(); + + stream.WriteLEShort(name.Length); + stream.WriteLEShort(extra.Length); + + if (name.Length > 0) + { + stream.Write(name, 0, name.Length); + } + + if (entry.LocalHeaderRequiresZip64 && patchEntryHeader) + { + patchData.SizePatchOffset += streamOffset + stream.Position; + } + + if (extra.Length > 0) + { + stream.Write(extra, 0, extra.Length); + } + + return ZipConstants.LocalHeaderBaseSize + name.Length + extra.Length; + } + + /// + /// Locates a block with the desired . + /// + /// + /// The signature to find. + /// Location, marking the end of block. + /// Minimum size of the block. + /// The maximum variable data. + /// Returns the offset of the first byte after the signature; -1 if not found + internal static long LocateBlockWithSignature(Stream stream, int signature, long endLocation, int minimumBlockSize, int maximumVariableData) + { + long pos = endLocation - minimumBlockSize; + if (pos < 0) + { + return -1; + } + + long giveUpMarker = Math.Max(pos - maximumVariableData, 0); + + // TODO: This loop could be optimized for speed. + do + { + if (pos < giveUpMarker) + { + return -1; + } + stream.Seek(pos--, SeekOrigin.Begin); + } while (stream.ReadLEInt() != signature); + + return stream.Position; + } + + /// + public static async Task WriteZip64EndOfCentralDirectoryAsync(Stream stream, long noOfEntries, + long sizeEntries, long centralDirOffset, CancellationToken cancellationToken) + { + await stream.WriteProcToStreamAsync(s => WriteZip64EndOfCentralDirectory(s, noOfEntries, sizeEntries, centralDirOffset), cancellationToken).ConfigureAwait(false); + } + + /// + /// Write Zip64 end of central directory records (File header and locator). + /// + /// + /// The number of entries in the central directory. + /// The size of entries in the central directory. + /// The offset of the central directory. + internal static void WriteZip64EndOfCentralDirectory(Stream stream, long noOfEntries, long sizeEntries, long centralDirOffset) + { + long centralSignatureOffset = centralDirOffset + sizeEntries; + stream.WriteLEInt(ZipConstants.Zip64CentralFileHeaderSignature); + stream.WriteLELong(44); // Size of this record (total size of remaining fields in header or full size - 12) + stream.WriteLEShort(ZipConstants.VersionMadeBy); // Version made by + stream.WriteLEShort(ZipConstants.VersionZip64); // Version to extract + stream.WriteLEInt(0); // Number of this disk + stream.WriteLEInt(0); // number of the disk with the start of the central directory + stream.WriteLELong(noOfEntries); // No of entries on this disk + stream.WriteLELong(noOfEntries); // Total No of entries in central directory + stream.WriteLELong(sizeEntries); // Size of the central directory + stream.WriteLELong(centralDirOffset); // offset of start of central directory + // zip64 extensible data sector not catered for here (variable size) + + // Write the Zip64 end of central directory locator + stream.WriteLEInt(ZipConstants.Zip64CentralDirLocatorSignature); + + // no of the disk with the start of the zip64 end of central directory + stream.WriteLEInt(0); + + // relative offset of the zip64 end of central directory record + stream.WriteLELong(centralSignatureOffset); + + // total number of disks + stream.WriteLEInt(1); + } + + /// + public static async Task WriteEndOfCentralDirectoryAsync(Stream stream, long noOfEntries, long sizeEntries, + long start, byte[] comment, CancellationToken cancellationToken) + => await stream.WriteProcToStreamAsync(s + => WriteEndOfCentralDirectory(s, noOfEntries, sizeEntries, start, comment), cancellationToken).ConfigureAwait(false); + + /// + /// Write the required records to end the central directory. + /// + /// + /// The number of entries in the directory. + /// The size of the entries in the directory. + /// The start of the central directory. + /// The archive comment. (This can be null). + + internal static void WriteEndOfCentralDirectory(Stream stream, long noOfEntries, long sizeEntries, long start, byte[] comment) + { + if (noOfEntries >= 0xffff || + start >= 0xffffffff || + sizeEntries >= 0xffffffff) + { + WriteZip64EndOfCentralDirectory(stream, noOfEntries, sizeEntries, start); + } + + stream.WriteLEInt(ZipConstants.EndOfCentralDirectorySignature); + + // TODO: ZipFile Multi disk handling not done + stream.WriteLEShort(0); // number of this disk + stream.WriteLEShort(0); // no of disk with start of central dir + + // Number of entries + if (noOfEntries >= 0xffff) + { + stream.WriteLEUshort(0xffff); // Zip64 marker + stream.WriteLEUshort(0xffff); + } + else + { + stream.WriteLEShort((short)noOfEntries); // entries in central dir for this disk + stream.WriteLEShort((short)noOfEntries); // total entries in central directory + } + + // Size of the central directory + if (sizeEntries >= 0xffffffff) + { + stream.WriteLEUint(0xffffffff); // Zip64 marker + } + else + { + stream.WriteLEInt((int)sizeEntries); + } + + // offset of start of central directory + if (start >= 0xffffffff) + { + stream.WriteLEUint(0xffffffff); // Zip64 marker + } + else + { + stream.WriteLEInt((int)start); + } + + var commentLength = comment?.Length ?? 0; + + if (commentLength > 0xffff) + { + throw new ZipException($"Comment length ({commentLength}) is larger than 64K"); + } + + stream.WriteLEShort(commentLength); + + if (commentLength > 0) + { + stream.Write(comment, 0, commentLength); + } + } + + + + /// + /// Write a data descriptor. + /// + /// + /// The entry to write a descriptor for. + /// Returns the number of descriptor bytes written. + internal static int WriteDataDescriptor(Stream stream, ZipEntry entry) + { + if (entry == null) + { + throw new ArgumentNullException(nameof(entry)); + } + + int result = 0; + + // Add data descriptor if flagged as required + if ((entry.Flags & (int)GeneralBitFlags.Descriptor) != 0) + { + // The signature is not PKZIP originally but is now described as optional + // in the PKZIP Appnote documenting the format. + stream.WriteLEInt(ZipConstants.DataDescriptorSignature); + stream.WriteLEInt(unchecked((int)(entry.Crc))); + + result += 8; + + if (entry.LocalHeaderRequiresZip64) + { + stream.WriteLELong(entry.CompressedSize); + stream.WriteLELong(entry.Size); + result += 16; + } + else + { + stream.WriteLEInt((int)entry.CompressedSize); + stream.WriteLEInt((int)entry.Size); + result += 8; + } + } + + return result; + } + + /// + /// Read data descriptor at the end of compressed data. + /// + /// + /// if set to true [zip64]. + /// The data to fill in. + /// Returns the number of bytes read in the descriptor. + internal static void ReadDataDescriptor(Stream stream, bool zip64, DescriptorData data) + { + int intValue = stream.ReadLEInt(); + + // In theory this may not be a descriptor according to PKZIP appnote. + // In practice its always there. + if (intValue != ZipConstants.DataDescriptorSignature) + { + throw new ZipException("Data descriptor signature not found"); + } + + data.Crc = stream.ReadLEInt(); + + if (zip64) + { + data.CompressedSize = stream.ReadLELong(); + data.Size = stream.ReadLELong(); + } + else + { + data.CompressedSize = stream.ReadLEInt(); + data.Size = stream.ReadLEInt(); + } + } + + internal static int WriteEndEntry(Stream stream, ZipEntry entry, StringCodec stringCodec) + { + stream.WriteLEInt(ZipConstants.CentralHeaderSignature); + stream.WriteLEShort((entry.HostSystem << 8) | entry.VersionMadeBy); + stream.WriteLEShort(entry.Version); + stream.WriteLEShort(entry.Flags); + stream.WriteLEShort((short)entry.CompressionMethodForHeader); + stream.WriteLEInt((int)entry.DosTime); + stream.WriteLEInt((int)entry.Crc); + + if (entry.IsZip64Forced() || + (entry.CompressedSize >= uint.MaxValue)) + { + stream.WriteLEInt(-1); + } + else + { + stream.WriteLEInt((int)entry.CompressedSize); + } + + if (entry.IsZip64Forced() || + (entry.Size >= uint.MaxValue)) + { + stream.WriteLEInt(-1); + } + else + { + stream.WriteLEInt((int)entry.Size); + } + + byte[] name = stringCodec.ZipOutputEncoding.GetBytes(entry.Name); + + if (name.Length > 0xffff) + { + throw new ZipException("Name too long."); + } + + var ed = new ZipExtraData(entry.ExtraData); + + if (entry.CentralHeaderRequiresZip64) + { + ed.StartNewEntry(); + if (entry.IsZip64Forced() || + (entry.Size >= 0xffffffff)) + { + ed.AddLeLong(entry.Size); + } + + if (entry.IsZip64Forced() || + (entry.CompressedSize >= 0xffffffff)) + { + ed.AddLeLong(entry.CompressedSize); + } + + if (entry.Offset >= 0xffffffff) + { + ed.AddLeLong(entry.Offset); + } + + ed.AddNewEntry(1); + } + else + { + ed.Delete(1); + } + + if (entry.AESKeySize > 0) + { + AddExtraDataAES(entry, ed); + } + byte[] extra = ed.GetEntryData(); + + byte[] entryComment = !(entry.Comment is null) + ? stringCodec.ZipOutputEncoding.GetBytes(entry.Comment) + : Empty.Array(); + + if (entryComment.Length > 0xffff) + { + throw new ZipException("Comment too long."); + } + + stream.WriteLEShort(name.Length); + stream.WriteLEShort(extra.Length); + stream.WriteLEShort(entryComment.Length); + stream.WriteLEShort(0); // disk number + stream.WriteLEShort(0); // internal file attributes + // external file attributes + + if (entry.ExternalFileAttributes != -1) + { + stream.WriteLEInt(entry.ExternalFileAttributes); + } + else + { + if (entry.IsDirectory) + { // mark entry as directory (from nikolam.AT.perfectinfo.com) + stream.WriteLEInt(16); + } + else + { + stream.WriteLEInt(0); + } + } + + if (entry.Offset >= uint.MaxValue) + { + stream.WriteLEInt(-1); + } + else + { + stream.WriteLEInt((int)entry.Offset); + } + + if (name.Length > 0) + { + stream.Write(name, 0, name.Length); + } + + if (extra.Length > 0) + { + stream.Write(extra, 0, extra.Length); + } + + if (entryComment.Length > 0) + { + stream.Write(entryComment, 0, entryComment.Length); + } + + return ZipConstants.CentralHeaderBaseSize + name.Length + extra.Length + entryComment.Length; + } + + internal static void AddExtraDataAES(ZipEntry entry, ZipExtraData extraData) + { + // Vendor Version: AE-1 IS 1. AE-2 is 2. With AE-2 no CRC is required and 0 is stored. + const int VENDOR_VERSION = 2; + // Vendor ID is the two ASCII characters "AE". + const int VENDOR_ID = 0x4541; //not 6965; + extraData.StartNewEntry(); + // Pack AES extra data field see http://www.winzip.com/aes_info.htm + //extraData.AddLeShort(7); // Data size (currently 7) + extraData.AddLeShort(VENDOR_VERSION); // 2 = AE-2 + extraData.AddLeShort(VENDOR_ID); // "AE" + extraData.AddData(entry.AESEncryptionStrength); // 1 = 128, 2 = 192, 3 = 256 + extraData.AddLeShort((int)entry.CompressionMethod); // The actual compression method used to compress the file + extraData.AddNewEntry(0x9901); + } + + internal static async Task PatchLocalHeaderAsync(Stream stream, ZipEntry entry, + EntryPatchData patchData, CancellationToken ct) + { + var initialPos = stream.Position; + + // Update CRC + stream.Seek(patchData.CrcPatchOffset, SeekOrigin.Begin); + await stream.WriteLEIntAsync((int)entry.Crc, ct).ConfigureAwait(false); + + // Update Sizes + if (entry.LocalHeaderRequiresZip64) + { + if (patchData.SizePatchOffset == -1) + { + throw new ZipException("Entry requires zip64 but this has been turned off"); + } + // Seek to the Zip64 Extra Data + stream.Seek(patchData.SizePatchOffset, SeekOrigin.Begin); + + // Note: The order of the size fields is reversed when compared to the local header! + await stream.WriteLELongAsync(entry.Size, ct).ConfigureAwait(false); + await stream.WriteLELongAsync(entry.CompressedSize, ct).ConfigureAwait(false); + } + else + { + await stream.WriteLEIntAsync((int)entry.CompressedSize, ct).ConfigureAwait(false); + await stream.WriteLEIntAsync((int)entry.Size, ct).ConfigureAwait(false); + } + + stream.Seek(initialPos, SeekOrigin.Begin); + } + + internal static void PatchLocalHeaderSync(Stream stream, ZipEntry entry, + EntryPatchData patchData) + { + var initialPos = stream.Position; + stream.Seek(patchData.CrcPatchOffset, SeekOrigin.Begin); + stream.WriteLEInt((int)entry.Crc); + + if (entry.LocalHeaderRequiresZip64) + { + if (patchData.SizePatchOffset == -1) + { + throw new ZipException("Entry requires zip64 but this has been turned off"); + } + + // Seek to the Zip64 Extra Data + stream.Seek(patchData.SizePatchOffset, SeekOrigin.Begin); + + // Note: The order of the size fields is reversed when compared to the local header! + stream.WriteLELong(entry.Size); + stream.WriteLELong(entry.CompressedSize); + } + else + { + stream.WriteLEInt((int)entry.CompressedSize); + stream.WriteLEInt((int)entry.Size); + } + + stream.Seek(initialPos, SeekOrigin.Begin); + } + } +} diff --git a/src/ICSharpCode.SharpZipLib/Zip/ZipHelperStream.cs b/src/ICSharpCode.SharpZipLib/Zip/ZipHelperStream.cs index 8901dbf76..e69de29bb 100644 --- a/src/ICSharpCode.SharpZipLib/Zip/ZipHelperStream.cs +++ b/src/ICSharpCode.SharpZipLib/Zip/ZipHelperStream.cs @@ -1,561 +0,0 @@ -using System; -using System.IO; -using System.Text; - -namespace ICSharpCode.SharpZipLib.Zip -{ - /// - /// Holds data pertinent to a data descriptor. - /// - public class DescriptorData - { - /// - /// Get /set the compressed size of data. - /// - public long CompressedSize { - get { return compressedSize; } - set { compressedSize = value; } - } - - /// - /// Get / set the uncompressed size of data - /// - public long Size { - get { return size; } - set { size = value; } - } - - /// - /// Get /set the crc value. - /// - public long Crc { - get { return crc; } - set { crc = (value & 0xffffffff); } - } - - #region Instance Fields - long size; - long compressedSize; - long crc; - #endregion - } - - class EntryPatchData - { - public long SizePatchOffset { - get { return sizePatchOffset_; } - set { sizePatchOffset_ = value; } - } - - public long CrcPatchOffset { - get { return crcPatchOffset_; } - set { crcPatchOffset_ = value; } - } - - #region Instance Fields - long sizePatchOffset_; - long crcPatchOffset_; - #endregion - } - - /// - /// This class assists with writing/reading from Zip files. - /// - internal class ZipHelperStream : Stream - { - #region Constructors - /// - /// Initialise an instance of this class. - /// - /// The name of the file to open. - public ZipHelperStream(string name) - { - stream_ = new FileStream(name, FileMode.Open, FileAccess.ReadWrite); - isOwner_ = true; - } - - /// - /// Initialise a new instance of . - /// - /// The stream to use. - public ZipHelperStream(Stream stream) - { - stream_ = stream; - } - #endregion - - /// - /// Get / set a value indicating wether the the underlying stream is owned or not. - /// - /// If the stream is owned it is closed when this instance is closed. - public bool IsStreamOwner { - get { return isOwner_; } - set { isOwner_ = value; } - } - - #region Base Stream Methods - public override bool CanRead { - get { return stream_.CanRead; } - } - - public override bool CanSeek { - get { return stream_.CanSeek; } - } - - public override bool CanTimeout { - get { return stream_.CanTimeout; } - } - - public override long Length { - get { return stream_.Length; } - } - - public override long Position { - get { return stream_.Position; } - set { stream_.Position = value; } - } - - public override bool CanWrite { - get { return stream_.CanWrite; } - } - - public override void Flush() - { - stream_.Flush(); - } - - public override long Seek(long offset, SeekOrigin origin) - { - return stream_.Seek(offset, origin); - } - - public override void SetLength(long value) - { - stream_.SetLength(value); - } - - public override int Read(byte[] buffer, int offset, int count) - { - return stream_.Read(buffer, offset, count); - } - - public override void Write(byte[] buffer, int offset, int count) - { - stream_.Write(buffer, offset, count); - } - - /// - /// Close the stream. - /// - /// - /// The underlying stream is closed only if is true. - /// - protected override void Dispose(bool disposing) - { - Stream toClose = stream_; - stream_ = null; - if (isOwner_ && (toClose != null)) { - isOwner_ = false; - toClose.Dispose(); - } - } - - #endregion - - // Write the local file header - // TODO: ZipHelperStream.WriteLocalHeader is not yet used and needs checking for ZipFile and ZipOuptutStream usage - void WriteLocalHeader(ZipEntry entry, EntryPatchData patchData) - { - CompressionMethod method = entry.CompressionMethod; - bool headerInfoAvailable = true; // How to get this? - bool patchEntryHeader = false; - - WriteLEInt(ZipConstants.LocalHeaderSignature); - - WriteLEShort(entry.Version); - WriteLEShort(entry.Flags); - WriteLEShort((byte)method); - WriteLEInt((int)entry.DosTime); - - if (headerInfoAvailable == true) { - WriteLEInt((int)entry.Crc); - if (entry.LocalHeaderRequiresZip64) { - WriteLEInt(-1); - WriteLEInt(-1); - } else { - WriteLEInt(entry.IsCrypted ? (int)entry.CompressedSize + ZipConstants.CryptoHeaderSize : (int)entry.CompressedSize); - WriteLEInt((int)entry.Size); - } - } else { - if (patchData != null) { - patchData.CrcPatchOffset = stream_.Position; - } - WriteLEInt(0); // Crc - - if (patchData != null) { - patchData.SizePatchOffset = stream_.Position; - } - - // For local header both sizes appear in Zip64 Extended Information - if (entry.LocalHeaderRequiresZip64 && patchEntryHeader) { - WriteLEInt(-1); - WriteLEInt(-1); - } else { - WriteLEInt(0); // Compressed size - WriteLEInt(0); // Uncompressed size - } - } - - byte[] name = ZipConstants.ConvertToArray(entry.Flags, entry.Name); - - if (name.Length > 0xFFFF) { - throw new ZipException("Entry name too long."); - } - - var ed = new ZipExtraData(entry.ExtraData); - - if (entry.LocalHeaderRequiresZip64 && (headerInfoAvailable || patchEntryHeader)) { - ed.StartNewEntry(); - if (headerInfoAvailable) { - ed.AddLeLong(entry.Size); - ed.AddLeLong(entry.CompressedSize); - } else { - ed.AddLeLong(-1); - ed.AddLeLong(-1); - } - ed.AddNewEntry(1); - - if (!ed.Find(1)) { - throw new ZipException("Internal error cant find extra data"); - } - - if (patchData != null) { - patchData.SizePatchOffset = ed.CurrentReadIndex; - } - } else { - ed.Delete(1); - } - - byte[] extra = ed.GetEntryData(); - - WriteLEShort(name.Length); - WriteLEShort(extra.Length); - - if (name.Length > 0) { - stream_.Write(name, 0, name.Length); - } - - if (entry.LocalHeaderRequiresZip64 && patchEntryHeader) { - patchData.SizePatchOffset += stream_.Position; - } - - if (extra.Length > 0) { - stream_.Write(extra, 0, extra.Length); - } - } - - /// - /// Locates a block with the desired . - /// - /// The signature to find. - /// Location, marking the end of block. - /// Minimum size of the block. - /// The maximum variable data. - /// Eeturns the offset of the first byte after the signature; -1 if not found - public long LocateBlockWithSignature(int signature, long endLocation, int minimumBlockSize, int maximumVariableData) - { - long pos = endLocation - minimumBlockSize; - if (pos < 0) { - return -1; - } - - long giveUpMarker = Math.Max(pos - maximumVariableData, 0); - - // TODO: This loop could be optimised for speed. - do { - if (pos < giveUpMarker) { - return -1; - } - Seek(pos--, SeekOrigin.Begin); - } while (ReadLEInt() != signature); - - return Position; - } - - /// - /// Write Zip64 end of central directory records (File header and locator). - /// - /// The number of entries in the central directory. - /// The size of entries in the central directory. - /// The offset of the dentral directory. - public void WriteZip64EndOfCentralDirectory(long noOfEntries, long sizeEntries, long centralDirOffset) - { - long centralSignatureOffset = stream_.Position; - WriteLEInt(ZipConstants.Zip64CentralFileHeaderSignature); - WriteLELong(44); // Size of this record (total size of remaining fields in header or full size - 12) - WriteLEShort(ZipConstants.VersionMadeBy); // Version made by - WriteLEShort(ZipConstants.VersionZip64); // Version to extract - WriteLEInt(0); // Number of this disk - WriteLEInt(0); // number of the disk with the start of the central directory - WriteLELong(noOfEntries); // No of entries on this disk - WriteLELong(noOfEntries); // Total No of entries in central directory - WriteLELong(sizeEntries); // Size of the central directory - WriteLELong(centralDirOffset); // offset of start of central directory - // zip64 extensible data sector not catered for here (variable size) - - // Write the Zip64 end of central directory locator - WriteLEInt(ZipConstants.Zip64CentralDirLocatorSignature); - - // no of the disk with the start of the zip64 end of central directory - WriteLEInt(0); - - // relative offset of the zip64 end of central directory record - WriteLELong(centralSignatureOffset); - - // total number of disks - WriteLEInt(1); - } - - /// - /// Write the required records to end the central directory. - /// - /// The number of entries in the directory. - /// The size of the entries in the directory. - /// The start of the central directory. - /// The archive comment. (This can be null). - public void WriteEndOfCentralDirectory(long noOfEntries, long sizeEntries, - long startOfCentralDirectory, byte[] comment) - { - - if ((noOfEntries >= 0xffff) || - (startOfCentralDirectory >= 0xffffffff) || - (sizeEntries >= 0xffffffff)) { - WriteZip64EndOfCentralDirectory(noOfEntries, sizeEntries, startOfCentralDirectory); - } - - WriteLEInt(ZipConstants.EndOfCentralDirectorySignature); - - // TODO: ZipFile Multi disk handling not done - WriteLEShort(0); // number of this disk - WriteLEShort(0); // no of disk with start of central dir - - - // Number of entries - if (noOfEntries >= 0xffff) { - WriteLEUshort(0xffff); // Zip64 marker - WriteLEUshort(0xffff); - } else { - WriteLEShort((short)noOfEntries); // entries in central dir for this disk - WriteLEShort((short)noOfEntries); // total entries in central directory - } - - // Size of the central directory - if (sizeEntries >= 0xffffffff) { - WriteLEUint(0xffffffff); // Zip64 marker - } else { - WriteLEInt((int)sizeEntries); - } - - - // offset of start of central directory - if (startOfCentralDirectory >= 0xffffffff) { - WriteLEUint(0xffffffff); // Zip64 marker - } else { - WriteLEInt((int)startOfCentralDirectory); - } - - int commentLength = (comment != null) ? comment.Length : 0; - - if (commentLength > 0xffff) { - throw new ZipException(string.Format("Comment length({0}) is too long can only be 64K", commentLength)); - } - - WriteLEShort(commentLength); - - if (commentLength > 0) { - Write(comment, 0, comment.Length); - } - } - - #region LE value reading/writing - /// - /// Read an unsigned short in little endian byte order. - /// - /// Returns the value read. - /// - /// An i/o error occurs. - /// - /// - /// The file ends prematurely - /// - public int ReadLEShort() - { - int byteValue1 = stream_.ReadByte(); - - if (byteValue1 < 0) { - throw new EndOfStreamException(); - } - - int byteValue2 = stream_.ReadByte(); - if (byteValue2 < 0) { - throw new EndOfStreamException(); - } - - return byteValue1 | (byteValue2 << 8); - } - - /// - /// Read an int in little endian byte order. - /// - /// Returns the value read. - /// - /// An i/o error occurs. - /// - /// - /// The file ends prematurely - /// - public int ReadLEInt() - { - return ReadLEShort() | (ReadLEShort() << 16); - } - - /// - /// Read a long in little endian byte order. - /// - /// The value read. - public long ReadLELong() - { - return (uint)ReadLEInt() | ((long)ReadLEInt() << 32); - } - - /// - /// Write an unsigned short in little endian byte order. - /// - /// The value to write. - public void WriteLEShort(int value) - { - stream_.WriteByte((byte)(value & 0xff)); - stream_.WriteByte((byte)((value >> 8) & 0xff)); - } - - /// - /// Write a ushort in little endian byte order. - /// - /// The value to write. - public void WriteLEUshort(ushort value) - { - stream_.WriteByte((byte)(value & 0xff)); - stream_.WriteByte((byte)(value >> 8)); - } - - /// - /// Write an int in little endian byte order. - /// - /// The value to write. - public void WriteLEInt(int value) - { - WriteLEShort(value); - WriteLEShort(value >> 16); - } - - /// - /// Write a uint in little endian byte order. - /// - /// The value to write. - public void WriteLEUint(uint value) - { - WriteLEUshort((ushort)(value & 0xffff)); - WriteLEUshort((ushort)(value >> 16)); - } - - /// - /// Write a long in little endian byte order. - /// - /// The value to write. - public void WriteLELong(long value) - { - WriteLEInt((int)value); - WriteLEInt((int)(value >> 32)); - } - - /// - /// Write a ulong in little endian byte order. - /// - /// The value to write. - public void WriteLEUlong(ulong value) - { - WriteLEUint((uint)(value & 0xffffffff)); - WriteLEUint((uint)(value >> 32)); - } - - #endregion - - /// - /// Write a data descriptor. - /// - /// The entry to write a descriptor for. - /// Returns the number of descriptor bytes written. - public int WriteDataDescriptor(ZipEntry entry) - { - if (entry == null) { - throw new ArgumentNullException(nameof(entry)); - } - - int result = 0; - - // Add data descriptor if flagged as required - if ((entry.Flags & (int)GeneralBitFlags.Descriptor) != 0) { - // The signature is not PKZIP originally but is now described as optional - // in the PKZIP Appnote documenting trhe format. - WriteLEInt(ZipConstants.DataDescriptorSignature); - WriteLEInt(unchecked((int)(entry.Crc))); - - result += 8; - - if (entry.LocalHeaderRequiresZip64) { - WriteLELong(entry.CompressedSize); - WriteLELong(entry.Size); - result += 16; - } else { - WriteLEInt((int)entry.CompressedSize); - WriteLEInt((int)entry.Size); - result += 8; - } - } - - return result; - } - - /// - /// Read data descriptor at the end of compressed data. - /// - /// if set to true [zip64]. - /// The data to fill in. - /// Returns the number of bytes read in the descriptor. - public void ReadDataDescriptor(bool zip64, DescriptorData data) - { - int intValue = ReadLEInt(); - - // In theory this may not be a descriptor according to PKZIP appnote. - // In practise its always there. - if (intValue != ZipConstants.DataDescriptorSignature) { - throw new ZipException("Data descriptor signature not found"); - } - - data.Crc = ReadLEInt(); - - if (zip64) { - data.CompressedSize = ReadLELong(); - data.Size = ReadLELong(); - } else { - data.CompressedSize = ReadLEInt(); - data.Size = ReadLEInt(); - } - } - - #region Instance Fields - bool isOwner_; - Stream stream_; - #endregion - } -} diff --git a/src/ICSharpCode.SharpZipLib/Zip/ZipInputStream.cs b/src/ICSharpCode.SharpZipLib/Zip/ZipInputStream.cs index df513e085..37e9e8ba8 100644 --- a/src/ICSharpCode.SharpZipLib/Zip/ZipInputStream.cs +++ b/src/ICSharpCode.SharpZipLib/Zip/ZipInputStream.cs @@ -1,9 +1,11 @@ -using System; -using System.IO; using ICSharpCode.SharpZipLib.Checksum; using ICSharpCode.SharpZipLib.Encryption; -using ICSharpCode.SharpZipLib.Zip.Compression; using ICSharpCode.SharpZipLib.Zip.Compression.Streams; +using System; +using System.Diagnostics; +using System.IO; +using ICSharpCode.SharpZipLib.Core; +using ICSharpCode.SharpZipLib.Zip.Compression; namespace ICSharpCode.SharpZipLib.Zip { @@ -16,15 +18,15 @@ namespace ICSharpCode.SharpZipLib.Zip ///
///
Author of the original java version : Jochen Hoenicke /// - /// + /// /// This sample shows how to read a zip file /// /// using System; /// using System.Text; /// using System.IO; - /// + /// /// using ICSharpCode.SharpZipLib.Zip; - /// + /// /// class MainClass /// { /// public static void Main(string[] args) @@ -34,7 +36,7 @@ namespace ICSharpCode.SharpZipLib.Zip /// ZipEntry theEntry; /// const int size = 2048; /// byte[] data = new byte[2048]; - /// + /// /// while ((theEntry = s.GetNextEntry()) != null) { /// if ( entry.IsFile ) { /// Console.Write("Show contents (y/n) ?"); @@ -60,31 +62,34 @@ public class ZipInputStream : InflaterInputStream #region Instance Fields /// - /// Delegate for reading bytes from a stream. + /// Delegate for reading bytes from a stream. /// - delegate int ReadDataHandler(byte[] b, int offset, int length); + private delegate int ReadDataHandler(byte[] b, int offset, int length); /// /// The current reader this instance. /// - ReadDataHandler internalReader; + private ReadDataHandler internalReader; + + private Crc32 crc = new Crc32(); + private ZipEntry entry; - Crc32 crc = new Crc32(); - ZipEntry entry; + private long size; + private CompressionMethod method; + private int flags; + private string password; + private readonly StringCodec _stringCodec = ZipStrings.GetStringCodec(); - long size; - int method; - int flags; - string password; - #endregion + #endregion Instance Fields #region Constructors + /// /// Creates a new Zip input stream, for reading a zip archive. /// /// The underlying providing data. public ZipInputStream(Stream baseInputStream) - : base(baseInputStream, new Inflater(true)) + : base(baseInputStream, InflaterPool.Instance.Rent(true)) { internalReader = new ReadDataHandler(ReadingNotAvailable); } @@ -95,37 +100,74 @@ public ZipInputStream(Stream baseInputStream) /// The underlying providing data. /// Size of the buffer. public ZipInputStream(Stream baseInputStream, int bufferSize) - : base(baseInputStream, new Inflater(true), bufferSize) + : base(baseInputStream, InflaterPool.Instance.Rent(true), bufferSize) + { + internalReader = new ReadDataHandler(ReadingNotAvailable); + } + + /// + /// Creates a new Zip input stream, for reading a zip archive. + /// + /// The underlying providing data. + /// + public ZipInputStream(Stream baseInputStream, StringCodec stringCodec) + : base(baseInputStream, new Inflater(true)) { internalReader = new ReadDataHandler(ReadingNotAvailable); + if (stringCodec != null) + { + _stringCodec = stringCodec; + } } - #endregion + + #endregion Constructors /// /// Optional password used for encryption when non-null /// /// A password for all encrypted entries in this - public string Password { - get { + public string Password + { + get + { return password; } - set { + set + { password = value; } } - /// /// Gets a value indicating if there is a current entry and it can be decompressed /// /// /// The entry can only be decompressed if the library supports the zip features required to extract it. /// See the ZipEntry Version property for more details. + /// + /// Since uses the local headers for extraction, entries with no compression combined with the + /// flag set, cannot be extracted as the end of the entry data cannot be deduced. /// - public bool CanDecompressEntry { - get { - return (entry != null) && entry.CanDecompress; - } + public bool CanDecompressEntry + => entry != null + && IsEntryCompressionMethodSupported(entry) + && entry.CanDecompress + && (!entry.HasFlag(GeneralBitFlags.Descriptor) || entry.CompressionMethod != CompressionMethod.Stored || entry.IsCrypted); + + /// + /// Is the compression method for the specified entry supported? + /// + /// + /// Uses entry.CompressionMethodForHeader so that entries of type WinZipAES will be rejected. + /// + /// the entry to check. + /// true if the compression method is supported, false if not. + private static bool IsEntryCompressionMethodSupported(ZipEntry entry) + { + var entryCompressionMethod = entry.CompressionMethodForHeader; + + return entryCompressionMethod == CompressionMethod.Deflated || + entryCompressionMethod == CompressionMethod.Stored; } /// @@ -146,40 +188,26 @@ public bool CanDecompressEntry { /// public ZipEntry GetNextEntry() { - if (crc == null) { + if (crc == null) + { throw new InvalidOperationException("Closed."); } - if (entry != null) { + if (entry != null) + { CloseEntry(); } - int header = inputBuffer.ReadLeInt(); - - if (header == ZipConstants.CentralHeaderSignature || - header == ZipConstants.EndOfCentralDirectorySignature || - header == ZipConstants.CentralHeaderDigitalSignature || - header == ZipConstants.ArchiveExtraDataSignature || - header == ZipConstants.Zip64CentralFileHeaderSignature) { - // No more individual entries exist + if (!SkipUntilNextEntry()) + { Dispose(); return null; } - // -jr- 07-Dec-2003 Ignore spanning temporary signatures if found - // Spanning signature is same as descriptor signature and is untested as yet. - if ((header == ZipConstants.SpanningTempSignature) || (header == ZipConstants.SpanningSignature)) { - header = inputBuffer.ReadLeInt(); - } - - if (header != ZipConstants.LocalHeaderSignature) { - throw new ZipException("Wrong Local header signature: 0x" + String.Format("{0:X}", header)); - } - var versionRequiredToExtract = (short)inputBuffer.ReadLeShort(); flags = inputBuffer.ReadLeShort(); - method = inputBuffer.ReadLeShort(); + method = (CompressionMethod)inputBuffer.ReadLeShort(); var dostime = (uint)inputBuffer.ReadLeInt(); int crc2 = inputBuffer.ReadLeInt(); csize = inputBuffer.ReadLeInt(); @@ -192,33 +220,39 @@ public ZipEntry GetNextEntry() byte[] buffer = new byte[nameLen]; inputBuffer.ReadRawBuffer(buffer); - string name = ZipConstants.ConvertToStringExt(flags, buffer); + var entryEncoding = _stringCodec.ZipInputEncoding(flags); + string name = entryEncoding.GetString(buffer); + var unicode = entryEncoding.IsZipUnicode(); - entry = new ZipEntry(name, versionRequiredToExtract); - entry.Flags = flags; + entry = new ZipEntry(name, versionRequiredToExtract, ZipConstants.VersionMadeBy, method, unicode) + { + Flags = flags, + }; - entry.CompressionMethod = (CompressionMethod)method; - - if ((flags & 8) == 0) { + if ((flags & 8) == 0) + { entry.Crc = crc2 & 0xFFFFFFFFL; entry.Size = size & 0xFFFFFFFFL; entry.CompressedSize = csize & 0xFFFFFFFFL; entry.CryptoCheckValue = (byte)((crc2 >> 24) & 0xff); - - } else { - + } + else + { // This allows for GNU, WinZip and possibly other archives, the PKZIP spec // says these values are zero under these circumstances. - if (crc2 != 0) { + if (crc2 != 0) + { entry.Crc = crc2 & 0xFFFFFFFFL; } - if (size != 0) { + if (size != 0) + { entry.Size = size & 0xFFFFFFFFL; } - if (csize != 0) { + if (csize != 0) + { entry.CompressedSize = csize & 0xFFFFFFFFL; } @@ -231,29 +265,36 @@ public ZipEntry GetNextEntry() // both values. // Handle extra data if present. This can set/alter some fields of the entry. - if (extraLen > 0) { + if (extraLen > 0) + { byte[] extra = new byte[extraLen]; inputBuffer.ReadRawBuffer(extra); entry.ExtraData = extra; } entry.ProcessExtraData(true); - if (entry.CompressedSize >= 0) { + if (entry.CompressedSize >= 0) + { csize = entry.CompressedSize; } - if (entry.Size >= 0) { + if (entry.Size >= 0) + { size = entry.Size; } - if (method == (int)CompressionMethod.Stored && (!isCrypted && csize != size || (isCrypted && csize - ZipConstants.CryptoHeaderSize != size))) { + if (method == CompressionMethod.Stored && (!isCrypted && csize != size || (isCrypted && csize - ZipConstants.CryptoHeaderSize != size))) + { throw new ZipException("Stored, but compressed != uncompressed"); } // Determine how to handle reading of data if this is attempted. - if (entry.IsCompressionMethodSupported()) { + if (IsEntryCompressionMethodSupported(entry)) + { internalReader = new ReadDataHandler(InitialRead); - } else { + } + else + { internalReader = new ReadDataHandler(ReadingNotSupported); } @@ -261,20 +302,72 @@ public ZipEntry GetNextEntry() } /// - /// Read data descriptor at the end of compressed data. + /// Reads bytes from the input stream until either a local file header signature, or another signature + /// indicating that no more entries should be present, is found. + /// + /// Thrown if the end of the input stream is reached without any signatures found + /// Returns whether the found signature is for a local entry header + private bool SkipUntilNextEntry() + { + // First let's skip all null bytes since it's the sane padding to add when updating an entry with smaller size + var paddingSkipped = 0; + while(inputBuffer.ReadLeByte() == 0) { + paddingSkipped++; + } + + // Last byte read was not actually consumed, restore the offset + inputBuffer.Available += 1; + if(paddingSkipped > 0) { + Debug.WriteLine("Skipped {0} null byte(s) before reading signature", paddingSkipped); + } + + var offset = 0; + // Read initial header quad directly after the last entry + var header = (uint)inputBuffer.ReadLeInt(); + do + { + switch (header) + { + case ZipConstants.CentralHeaderSignature: + case ZipConstants.EndOfCentralDirectorySignature: + case ZipConstants.CentralHeaderDigitalSignature: + case ZipConstants.ArchiveExtraDataSignature: + case ZipConstants.Zip64CentralFileHeaderSignature: + Debug.WriteLine("Non-entry signature found at offset {0,2}: 0x{1:x8}", offset, header); + // No more individual entries exist + return false; + + case ZipConstants.LocalHeaderSignature: + Debug.WriteLine("Entry local header signature found at offset {0,2}: 0x{1:x8}", offset, header); + return true; + default: + // Current header quad did not match any signature, shift in another byte + header = (uint) (inputBuffer.ReadLeByte() << 24) | (header >> 8); + offset++; + break; + } + } while (true); // Loop until we either get an EOF exception or we find the next signature + } + + /// + /// Read data descriptor at the end of compressed data. /// - void ReadDataDescriptor() + private void ReadDataDescriptor() { - if (inputBuffer.ReadLeInt() != ZipConstants.DataDescriptorSignature) { + if (inputBuffer.ReadLeInt() != ZipConstants.DataDescriptorSignature) + { throw new ZipException("Data descriptor signature not found"); } entry.Crc = inputBuffer.ReadLeInt() & 0xFFFFFFFFL; - if (entry.LocalHeaderRequiresZip64) { + if (entry.LocalHeaderRequiresZip64) + { csize = inputBuffer.ReadLeLong(); size = inputBuffer.ReadLeLong(); - } else { + } + else + { csize = inputBuffer.ReadLeInt(); size = inputBuffer.ReadLeInt(); } @@ -286,24 +379,27 @@ void ReadDataDescriptor() /// Complete cleanup as the final part of closing. /// /// True if the crc value should be tested - void CompleteCloseEntry(bool testCrc) + private void CompleteCloseEntry(bool testCrc) { StopDecrypting(); - if ((flags & 8) != 0) { + if ((flags & 8) != 0) + { ReadDataDescriptor(); } size = 0; if (testCrc && - ((crc.Value & 0xFFFFFFFFL) != entry.Crc) && (entry.Crc != -1)) { + ((crc.Value & 0xFFFFFFFFL) != entry.Crc) && (entry.Crc != -1)) + { throw new ZipException("CRC mismatch"); } crc.Reset(); - if (method == (int)CompressionMethod.Deflated) { + if (method == CompressionMethod.Deflated) + { inf.Reset(); } entry = null; @@ -320,21 +416,26 @@ void CompleteCloseEntry(bool testCrc) /// public void CloseEntry() { - if (crc == null) { + if (crc == null) + { throw new InvalidOperationException("Closed"); } - if (entry == null) { + if (entry == null) + { return; } - if (method == (int)CompressionMethod.Deflated) { - if ((flags & 8) != 0) { + if (method == CompressionMethod.Deflated) + { + if ((flags & 8) != 0) + { // We don't know how much we must skip, read until end. byte[] tmp = new byte[4096]; // Read will close this entry - while (Read(tmp, 0, tmp.Length) > 0) { + while (Read(tmp, 0, tmp.Length) > 0) + { } return; } @@ -343,15 +444,21 @@ public void CloseEntry() inputBuffer.Available += inf.RemainingInput; } - if ((inputBuffer.Available > csize) && (csize >= 0)) { + if ((inputBuffer.Available > csize) && (csize >= 0)) + { + // Buffer can contain entire entry data. Internally offsetting position inside buffer inputBuffer.Available = (int)((long)inputBuffer.Available - csize); - } else { + } + else + { csize -= inputBuffer.Available; inputBuffer.Available = 0; - while (csize != 0) { + while (csize != 0) + { long skipped = Skip(csize); - if (skipped <= 0) { + if (skipped <= 0) + { throw new ZipException("Zip archive ends early."); } @@ -366,8 +473,10 @@ public void CloseEntry() /// Returns 1 if there is an entry available /// Otherwise returns 0. /// - public override int Available { - get { + public override int Available + { + get + { return entry != null ? 1 : 0; } } @@ -377,19 +486,26 @@ public override int Available { /// /// Thrown if the entry size is not known. /// Thrown if no entry is currently available. - public override long Length { - get { - if (entry != null) { - if (entry.Size >= 0) { + public override long Length + { + get + { + if (entry != null) + { + if (entry.Size >= 0) + { return entry.Size; - } else { + } + else + { throw new ZipException("Length not available for the current entry"); } - } else { + } + else + { throw new InvalidOperationException("No current entry"); } } - } /// @@ -401,7 +517,8 @@ public override long Length { public override int ReadByte() { byte[] b = new byte[1]; - if (Read(b, 0, 1) <= 0) { + if (Read(b, 0, 1) <= 0) + { return -1; } return b[0] & 0xff; @@ -414,7 +531,7 @@ public override int ReadByte() /// The offset at which data read should be stored. /// The maximum number of bytes to read. /// Returns the number of bytes actually read. - int ReadingNotAvailable(byte[] destination, int offset, int count) + private int ReadingNotAvailable(byte[] destination, int offset, int count) { throw new InvalidOperationException("Unable to read from this stream"); } @@ -422,64 +539,94 @@ int ReadingNotAvailable(byte[] destination, int offset, int count) /// /// Handle attempts to read from this entry by throwing an exception /// - int ReadingNotSupported(byte[] destination, int offset, int count) + private int ReadingNotSupported(byte[] destination, int offset, int count) { throw new ZipException("The compression method for this entry is not supported"); } /// - /// Perform the initial read on an entry which may include + /// Handle attempts to read from this entry by throwing an exception + /// + private int StoredDescriptorEntry(byte[] destination, int offset, int count) => + throw new StreamUnsupportedException( + "The combination of Stored compression method and Descriptor flag is not possible to read using ZipInputStream"); + + + /// + /// Perform the initial read on an entry which may include /// reading encryption headers and setting up inflation. /// /// The destination to fill with data read. /// The offset to start reading at. /// The maximum number of bytes to read. /// The actual number of bytes read. - int InitialRead(byte[] destination, int offset, int count) + private int InitialRead(byte[] destination, int offset, int count) { - if (!CanDecompressEntry) { - throw new ZipException("Library cannot extract this entry. Version required is (" + entry.Version + ")"); - } + var usesDescriptor = (entry.Flags & (int)GeneralBitFlags.Descriptor) != 0; // Handle encryption if required. - if (entry.IsCrypted) { - if (password == null) { + if (entry.IsCrypted) + { + if (password == null) + { throw new ZipException("No password set."); } // Generate and set crypto transform... var managed = new PkzipClassicManaged(); - byte[] key = PkzipClassic.GenerateKeys(ZipConstants.ConvertToArray(password)); + byte[] key = PkzipClassic.GenerateKeys(_stringCodec.ZipCryptoEncoding.GetBytes(password)); inputBuffer.CryptoTransform = managed.CreateDecryptor(key, null); byte[] cryptbuffer = new byte[ZipConstants.CryptoHeaderSize]; inputBuffer.ReadClearTextBuffer(cryptbuffer, 0, ZipConstants.CryptoHeaderSize); - if (cryptbuffer[ZipConstants.CryptoHeaderSize - 1] != entry.CryptoCheckValue) { + if (cryptbuffer[ZipConstants.CryptoHeaderSize - 1] != entry.CryptoCheckValue) + { throw new ZipException("Invalid password"); } - if (csize >= ZipConstants.CryptoHeaderSize) { + if (csize >= ZipConstants.CryptoHeaderSize) + { csize -= ZipConstants.CryptoHeaderSize; - } else if ((entry.Flags & (int)GeneralBitFlags.Descriptor) == 0) { - throw new ZipException(string.Format("Entry compressed size {0} too small for encryption", csize)); } - } else { + else if (!usesDescriptor) + { + throw new ZipException($"Entry compressed size {csize} too small for encryption"); + } + } + else + { inputBuffer.CryptoTransform = null; } - if ((csize > 0) || ((flags & (int)GeneralBitFlags.Descriptor) != 0)) { - if ((method == (int)CompressionMethod.Deflated) && (inputBuffer.Available > 0)) { + if (csize > 0 || usesDescriptor) + { + if (method == CompressionMethod.Deflated && inputBuffer.Available > 0) + { inputBuffer.SetInflaterInput(inf); } - internalReader = new ReadDataHandler(BodyRead); + // It's not possible to know how many bytes to read when using "Stored" compression (unless using encryption) + if (!entry.IsCrypted && method == CompressionMethod.Stored && usesDescriptor) + { + internalReader = StoredDescriptorEntry; + return StoredDescriptorEntry(destination, offset, count); + } + + if (!CanDecompressEntry) + { + internalReader = ReadingNotSupported; + return ReadingNotSupported(destination, offset, count); + } + + internalReader = BodyRead; return BodyRead(destination, offset, count); - } else { - internalReader = new ReadDataHandler(ReadingNotAvailable); - return 0; } + + + internalReader = ReadingNotAvailable; + return 0; } /// @@ -492,19 +639,23 @@ int InitialRead(byte[] destination, int offset, int count) /// Zero bytes read means end of stream. public override int Read(byte[] buffer, int offset, int count) { - if (buffer == null) { + if (buffer == null) + { throw new ArgumentNullException(nameof(buffer)); } - if (offset < 0) { + if (offset < 0) + { throw new ArgumentOutOfRangeException(nameof(offset), "Cannot be negative"); } - if (count < 0) { + if (count < 0) + { throw new ArgumentOutOfRangeException(nameof(count), "Cannot be negative"); } - if ((buffer.Length - offset) < count) { + if ((buffer.Length - offset) < count) + { throw new ArgumentException("Invalid offset/count combination"); } @@ -517,8 +668,8 @@ public override int Read(byte[] buffer, int offset, int count) /// /// The number of bytes read (this may be less than the length requested, even before the end of stream), or 0 on end of stream. /// - /// - /// An i/o error occured. + /// + /// An i/o error occurred. /// /// /// The deflated stream is corrupted. @@ -526,34 +677,41 @@ public override int Read(byte[] buffer, int offset, int count) /// /// The stream is not open. /// - int BodyRead(byte[] buffer, int offset, int count) + private int BodyRead(byte[] buffer, int offset, int count) { - if (crc == null) { + if (crc == null) + { throw new InvalidOperationException("Closed"); } - if ((entry == null) || (count <= 0)) { + if ((entry == null) || (count <= 0)) + { return 0; } - if (offset + count > buffer.Length) { + if (offset + count > buffer.Length) + { throw new ArgumentException("Offset + count exceeds buffer size"); } bool finished = false; - switch (method) { - case (int)CompressionMethod.Deflated: + switch (method) + { + case CompressionMethod.Deflated: count = base.Read(buffer, offset, count); - if (count <= 0) { - if (!inf.IsFinished) { + if (count <= 0) + { + if (!inf.IsFinished) + { throw new ZipException("Inflater not finished!"); } inputBuffer.Available = inf.RemainingInput; // A csize of -1 is from an unpatched local header if ((flags & 8) == 0 && - (inf.TotalIn != csize && csize != 0xFFFFFFFF && csize != -1 || inf.TotalOut != size)) { + (inf.TotalIn != csize && csize != 0xFFFFFFFF && csize != -1 || inf.TotalOut != size)) + { throw new ZipException("Size mismatch: " + csize + ";" + size + " <-> " + inf.TotalIn + ";" + inf.TotalOut); } inf.Reset(); @@ -561,34 +719,43 @@ int BodyRead(byte[] buffer, int offset, int count) } break; - case (int)CompressionMethod.Stored: - if ((count > csize) && (csize >= 0)) { + case CompressionMethod.Stored: + if ((count > csize) && (csize >= 0)) + { count = (int)csize; } - if (count > 0) { + if (count > 0) + { count = inputBuffer.ReadClearTextBuffer(buffer, offset, count); - if (count > 0) { + if (count > 0) + { csize -= count; size -= count; } } - if (csize == 0) { + if (csize == 0) + { finished = true; - } else { - if (count < 0) { + } + else + { + if (count < 0) + { throw new ZipException("EOF in stored block"); } } break; } - if (count > 0) { - crc.Update(buffer, offset, count); + if (count > 0) + { + crc.Update(new ArraySegment(buffer, offset, count)); } - if (finished) { + if (finished) + { CompleteCloseEntry(true); } diff --git a/src/ICSharpCode.SharpZipLib/Zip/ZipNameTransform.cs b/src/ICSharpCode.SharpZipLib/Zip/ZipNameTransform.cs index e2315a298..f91b20c3d 100644 --- a/src/ICSharpCode.SharpZipLib/Zip/ZipNameTransform.cs +++ b/src/ICSharpCode.SharpZipLib/Zip/ZipNameTransform.cs @@ -1,18 +1,19 @@ +using ICSharpCode.SharpZipLib.Core; using System; using System.IO; using System.Text; -using ICSharpCode.SharpZipLib.Core; namespace ICSharpCode.SharpZipLib.Zip { /// /// ZipNameTransform transforms names as per the Zip file naming convention. /// - /// The use of absolute names is supported although its use is not valid + /// The use of absolute names is supported although its use is not valid /// according to Zip naming conventions, and should not be used if maximum compatability is desired. public class ZipNameTransform : INameTransform { #region Constructors + /// /// Initialize a new instance of /// @@ -28,7 +29,8 @@ public ZipNameTransform(string trimPrefix) { TrimPrefix = trimPrefix; } - #endregion + + #endregion Constructors /// /// Static constructor. @@ -61,11 +63,15 @@ static ZipNameTransform() public string TransformDirectory(string name) { name = TransformFile(name); - if (name.Length > 0) { - if (!name.EndsWith("/", StringComparison.Ordinal)) { + if (name.Length > 0) + { + if (!name.EndsWith("/", StringComparison.Ordinal)) + { name += "/"; } - } else { + } + else + { throw new ZipException("Cannot have an empty directory name"); } return name; @@ -78,34 +84,32 @@ public string TransformDirectory(string name) /// The transformed name. public string TransformFile(string name) { - if (name != null) { + if (name != null) + { string lowerName = name.ToLower(); - if ((trimPrefix_ != null) && (lowerName.IndexOf(trimPrefix_, StringComparison.Ordinal) == 0)) { + if ((trimPrefix_ != null) && (lowerName.IndexOf(trimPrefix_, StringComparison.Ordinal) == 0)) + { name = name.Substring(trimPrefix_.Length); } name = name.Replace(@"\", "/"); - name = WindowsPathUtils.DropPathRoot(name); + name = PathUtils.DropPathRoot(name); - // Drop any leading slashes. - while ((name.Length > 0) && (name[0] == '/')) { - name = name.Remove(0, 1); - } - - // Drop any trailing slashes. - while ((name.Length > 0) && (name[name.Length - 1] == '/')) { - name = name.Remove(name.Length - 1, 1); - } + // Drop any leading and trailing slashes. + name = name.Trim('/'); // Convert consecutive // characters to / int index = name.IndexOf("//", StringComparison.Ordinal); - while (index >= 0) { + while (index >= 0) + { name = name.Remove(index, 1); index = name.IndexOf("//", StringComparison.Ordinal); } name = MakeValidName(name, '_'); - } else { + } + else + { name = string.Empty; } return name; @@ -116,11 +120,14 @@ public string TransformFile(string name) /// /// The prefix is trimmed before any conversion from /// a windows path is done. - public string TrimPrefix { + public string TrimPrefix + { get { return trimPrefix_; } - set { + set + { trimPrefix_ = value; - if (trimPrefix_ != null) { + if (trimPrefix_ != null) + { trimPrefix_ = trimPrefix_.ToLower(); } } @@ -132,25 +139,31 @@ public string TrimPrefix { /// The name to force valid /// The replacement character to use. /// Returns a valid name - static string MakeValidName(string name, char replacement) + private static string MakeValidName(string name, char replacement) { int index = name.IndexOfAny(InvalidEntryChars); - if (index >= 0) { + if (index >= 0) + { var builder = new StringBuilder(name); - while (index >= 0) { + while (index >= 0) + { builder[index] = replacement; - if (index >= name.Length) { + if (index >= name.Length) + { index = -1; - } else { + } + else + { index = name.IndexOfAny(InvalidEntryChars, index + 1); } } name = builder.ToString(); } - if (name.Length > 0xffff) { + if (name.Length > 0xffff) + { throw new PathTooLongException(); } @@ -173,10 +186,14 @@ public static bool IsValidName(string name, bool relaxed) { bool result = (name != null); - if (result) { - if (relaxed) { + if (result) + { + if (relaxed) + { result = name.IndexOfAny(InvalidEntryCharsRelaxed) < 0; - } else { + } + else + { result = (name.IndexOfAny(InvalidEntryChars) < 0) && (name.IndexOf('/') != 0); @@ -209,12 +226,88 @@ public static bool IsValidName(string name) } #region Instance Fields - string trimPrefix_; - #endregion + + private string trimPrefix_; + + #endregion Instance Fields #region Class Fields - static readonly char[] InvalidEntryChars; - static readonly char[] InvalidEntryCharsRelaxed; - #endregion + + private static readonly char[] InvalidEntryChars; + private static readonly char[] InvalidEntryCharsRelaxed; + + #endregion Class Fields + } + + /// + /// An implementation of INameTransform that transforms entry paths as per the Zip file naming convention. + /// Strips path roots and puts directory separators in the correct format ('/') + /// + public class PathTransformer : INameTransform + { + /// + /// Initialize a new instance of + /// + public PathTransformer() + { + } + + /// + /// Transform a windows directory name according to the Zip file naming conventions. + /// + /// The directory name to transform. + /// The transformed name. + public string TransformDirectory(string name) + { + name = TransformFile(name); + + if (name.Length > 0) + { + if (!name.EndsWith("/", StringComparison.Ordinal)) + { + name += "/"; + } + } + else + { + throw new ZipException("Cannot have an empty directory name"); + } + + return name; + } + + /// + /// Transform a windows file name according to the Zip file naming conventions. + /// + /// The file name to transform. + /// The transformed name. + public string TransformFile(string name) + { + if (name != null) + { + // Put separators in the expected format. + name = name.Replace(@"\", "/"); + + // Remove the path root. + name = PathUtils.DropPathRoot(name); + + // Drop any leading and trailing slashes. + name = name.Trim('/'); + + // Convert consecutive // characters to / + int index = name.IndexOf("//", StringComparison.Ordinal); + while (index >= 0) + { + name = name.Remove(index, 1); + index = name.IndexOf("//", StringComparison.Ordinal); + } + } + else + { + name = string.Empty; + } + + return name; + } } } diff --git a/src/ICSharpCode.SharpZipLib/Zip/ZipOutputStream.cs b/src/ICSharpCode.SharpZipLib/Zip/ZipOutputStream.cs index f872074dd..2cc36df22 100644 --- a/src/ICSharpCode.SharpZipLib/Zip/ZipOutputStream.cs +++ b/src/ICSharpCode.SharpZipLib/Zip/ZipOutputStream.cs @@ -1,10 +1,15 @@ -using System; -using System.IO; -using System.Collections; using ICSharpCode.SharpZipLib.Checksum; +using ICSharpCode.SharpZipLib.Core; +using ICSharpCode.SharpZipLib.Encryption; using ICSharpCode.SharpZipLib.Zip.Compression; using ICSharpCode.SharpZipLib.Zip.Compression.Streams; +using System; using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Security.Cryptography; +using System.Threading; +using System.Threading.Tasks; namespace ICSharpCode.SharpZipLib.Zip { @@ -13,7 +18,7 @@ namespace ICSharpCode.SharpZipLib.Zip /// archive one after another. It has a special method to start a new /// zip entry. The zip entries contains information about the file name /// size, compressed size, CRC, etc. - /// + /// /// It includes support for Stored and Deflated entries. /// This class is not thread safe. ///
@@ -23,21 +28,21 @@ namespace ICSharpCode.SharpZipLib.Zip /// /// using System; /// using System.IO; - /// + /// /// using ICSharpCode.SharpZipLib.Core; /// using ICSharpCode.SharpZipLib.Zip; - /// + /// /// class MainClass /// { /// public static void Main(string[] args) /// { /// string[] filenames = Directory.GetFiles(args[0]); /// byte[] buffer = new byte[4096]; - /// + /// /// using ( ZipOutputStream s = new ZipOutputStream(File.Create(args[1])) ) { - /// + /// /// s.SetLevel(9); // 0 - store only to 9 - means best compression - /// + /// /// foreach (string file in filenames) { /// ZipEntry entry = new ZipEntry(file); /// s.PutNextEntry(entry); @@ -48,12 +53,13 @@ namespace ICSharpCode.SharpZipLib.Zip /// } /// } /// } - /// } + /// } /// /// public class ZipOutputStream : DeflaterOutputStream { #region Constructors + /// /// Creates a new Zip output stream, writing a zip archive. /// @@ -74,14 +80,27 @@ public ZipOutputStream(Stream baseOutputStream, int bufferSize) : base(baseOutputStream, new Deflater(Deflater.DEFAULT_COMPRESSION, true), bufferSize) { } - #endregion + + /// + /// Creates a new Zip output stream, writing a zip archive. + /// + /// The output stream to which the archive contents are written. + /// + public ZipOutputStream(Stream baseOutputStream, StringCodec stringCodec) : this(baseOutputStream) + { + _stringCodec = stringCodec; + } + + #endregion Constructors /// /// Gets a flag value of true if the central header has been added for this archive; false if it has not been added. /// /// No further entries can be added once this has been done. - public bool IsFinished { - get { + public bool IsFinished + { + get + { return entries == null; } } @@ -92,14 +111,14 @@ public bool IsFinished { /// /// The comment text for the entire archive. /// - /// + /// /// The converted comment is longer than 0xffff bytes. /// public void SetComment(string comment) { - // TODO: Its not yet clear how to handle unicode comments here. - byte[] commentBytes = ZipConstants.ConvertToArray(comment); - if (commentBytes.Length > 0xffff) { + byte[] commentBytes = _stringCodec.ZipArchiveCommentEncoding.GetBytes(comment); + if (commentBytes.Length > 0xffff) + { throw new ArgumentOutOfRangeException(nameof(comment)); } zipComment = commentBytes; @@ -136,17 +155,48 @@ public int GetLevel() /// If backwards compatability is an issue be careful when adding entries to an archive. /// Setting this property to off is workable but less desirable as in those circumstances adding a file /// larger then 4GB will fail.
- public UseZip64 UseZip64 { + public UseZip64 UseZip64 + { get { return useZip64_; } set { useZip64_ = value; } } + /// + /// Used for transforming the names of entries added by . + /// Defaults to , set to null to disable transforms and use names as supplied. + /// + public INameTransform NameTransform { get; set; } = new PathTransformer(); + + /// + /// Get/set the password used for encryption. + /// + /// When set to null or if the password is empty no encryption is performed + public string Password + { + get + { + return password; + } + set + { + if ((value != null) && (value.Length == 0)) + { + password = null; + } + else + { + password = value; + } + } + } + /// /// Write an unsigned short in little endian byte order. /// private void WriteLeShort(int value) { - unchecked { + unchecked + { baseOutputStream_.WriteByte((byte)(value & 0xff)); baseOutputStream_.WriteByte((byte)((value >> 8) & 0xff)); } @@ -157,7 +207,8 @@ private void WriteLeShort(int value) ///
private void WriteLeInt(int value) { - unchecked { + unchecked + { WriteLeShort(value); WriteLeShort(value >> 16); } @@ -168,12 +219,22 @@ private void WriteLeInt(int value) ///
private void WriteLeLong(long value) { - unchecked { + unchecked + { WriteLeInt((int)value); WriteLeInt((int)(value >> 32)); } } + // Apply any configured transforms/cleaning to the name of the supplied entry. + private void TransformEntryName(ZipEntry entry) + { + if (NameTransform == null) return; + entry.Name = entry.IsDirectory + ? NameTransform.TransformDirectory(entry.Name) + : NameTransform.TransformFile(entry.Name); + } + /// /// Starts a new Zip entry. It automatically closes the previous /// entry if present. @@ -188,7 +249,7 @@ private void WriteLeLong(long value) /// if entry passed is null. /// /// - /// if an I/O error occured. + /// if an I/O error occurred. /// /// /// if stream was finished @@ -198,25 +259,137 @@ private void WriteLeLong(long value) /// Entry name is too long
/// Finish has already been called
///
+ /// + /// The Compression method specified for the entry is unsupported. + /// public void PutNextEntry(ZipEntry entry) { - if (entry == null) { - throw new ArgumentNullException(nameof(entry)); + if (curEntry != null) + { + CloseEntry(); } - if (entries == null) { - throw new InvalidOperationException("ZipOutputStream was finished"); + PutNextEntry(baseOutputStream_, entry); + + if (entry.IsCrypted) + { + WriteOutput(GetEntryEncryptionHeader(entry)); } + } - if (curEntry != null) { + /// + /// Starts a new passthrough Zip entry. It automatically closes the previous + /// entry if present. + /// Passthrough entry is an entry that is created from compressed data. + /// It is useful to avoid recompression to save CPU resources if compressed data is already disposable. + /// All entry elements bar name, crc, size and compressed size are optional, but must be correct if present. + /// Compression should be set to Deflated. + /// + /// + /// the entry. + /// + /// + /// if entry passed is null. + /// + /// + /// if an I/O error occurred. + /// + /// + /// if stream was finished. + /// + /// + /// Crc is not set
+ /// Size is not set
+ /// CompressedSize is not set
+ /// CompressionMethod is not Deflate
+ /// Too many entries in the Zip file
+ /// Entry name is too long
+ /// Finish has already been called
+ ///
+ /// + /// The Compression method specified for the entry is unsupported
+ /// Entry is encrypted
+ ///
+ public void PutNextPassthroughEntry(ZipEntry entry) + { + if(curEntry != null) + { CloseEntry(); } - if (entries.Count == int.MaxValue) { + if(entry.Crc < 0) + { + throw new ZipException("Crc must be set for passthrough entry"); + } + + if(entry.Size < 0) + { + throw new ZipException("Size must be set for passthrough entry"); + } + + if(entry.CompressedSize < 0) + { + throw new ZipException("CompressedSize must be set for passthrough entry"); + } + + if(entry.CompressionMethod != CompressionMethod.Deflated) + { + throw new NotImplementedException("Only Deflated entries are supported for passthrough"); + } + + if(!string.IsNullOrEmpty(Password)) + { + throw new NotImplementedException("Encrypted passthrough entries are not supported"); + } + + PutNextEntry(baseOutputStream_, entry, 0, true); + } + + + private void WriteOutput(byte[] bytes) + => baseOutputStream_.Write(bytes, 0, bytes.Length); + + private Task WriteOutputAsync(byte[] bytes) + => baseOutputStream_.WriteAsync(bytes, 0, bytes.Length); + + private byte[] GetEntryEncryptionHeader(ZipEntry entry) => + entry.AESKeySize > 0 + ? InitializeAESPassword(entry, Password) + : CreateZipCryptoHeader(entry.Crc < 0 ? entry.DosTime << 16 : entry.Crc); + + internal void PutNextEntry(Stream stream, ZipEntry entry, long streamOffset = 0, bool passthroughEntry = false) + { + if (entry == null) + { + throw new ArgumentNullException(nameof(entry)); + } + + if (entries == null) + { + throw new InvalidOperationException("ZipOutputStream was finished"); + } + + if (entries.Count == int.MaxValue) + { throw new ZipException("Too many entries for Zip file"); } CompressionMethod method = entry.CompressionMethod; + + // Check that the compression is one that we support + if (method != CompressionMethod.Deflated && method != CompressionMethod.Stored) + { + throw new NotImplementedException("Compression method not supported"); + } + + // A password must have been set in order to add AES encrypted entries + if (entry.AESKeySize > 0 && string.IsNullOrEmpty(this.Password)) + { + throw new InvalidOperationException("The Password property must be set before AES encrypted entries can be added"); + } + + entryIsPassthrough = passthroughEntry; + int compressionLevel = defaultCompressionLevel; // Clear flags that the library manages internally @@ -226,45 +399,58 @@ public void PutNextEntry(ZipEntry entry) bool headerInfoAvailable; // No need to compress - definitely no data. - if (entry.Size == 0) { + if (entry.Size == 0 && !entryIsPassthrough) + { entry.CompressedSize = entry.Size; entry.Crc = 0; method = CompressionMethod.Stored; headerInfoAvailable = true; - } else { + } + else + { headerInfoAvailable = (entry.Size >= 0) && entry.HasCrc && entry.CompressedSize >= 0; // Switch to deflation if storing isnt possible. - if (method == CompressionMethod.Stored) { - if (!headerInfoAvailable) { - if (!CanPatchEntries) { + if (method == CompressionMethod.Stored) + { + if (!headerInfoAvailable) + { + if (!CanPatchEntries) + { // Can't patch entries so storing is not possible. method = CompressionMethod.Deflated; compressionLevel = 0; } - } else // entry.size must be > 0 - { + } + else // entry.size must be > 0 + { entry.CompressedSize = entry.Size; headerInfoAvailable = entry.HasCrc; } } } - if (headerInfoAvailable == false) { - if (CanPatchEntries == false) { + if (headerInfoAvailable == false) + { + if (CanPatchEntries == false) + { // Only way to record size and compressed size is to append a data descriptor // after compressed data. // Stored entries of this form have already been converted to deflating. entry.Flags |= 8; - } else { + } + else + { patchEntryHeader = true; } } - if (Password != null) { + if (Password != null) + { entry.IsCrypted = true; - if (entry.Crc < 0) { + if (entry.Crc < 0) + { // Need to append a data descriptor as the crc isnt available for use // with encryption, the date is used instead. Setting the flag // indicates this to the decompressor. @@ -276,130 +462,87 @@ public void PutNextEntry(ZipEntry entry) entry.CompressionMethod = (CompressionMethod)method; curMethod = method; - sizePatchPos = -1; - if ((useZip64_ == UseZip64.On) || ((entry.Size < 0) && (useZip64_ == UseZip64.Dynamic))) { + if ((useZip64_ == UseZip64.On) || ((entry.Size < 0) && (useZip64_ == UseZip64.Dynamic))) + { entry.ForceZip64(); } - // Write the local file header - WriteLeInt(ZipConstants.LocalHeaderSignature); - - WriteLeShort(entry.Version); - WriteLeShort(entry.Flags); - WriteLeShort((byte)entry.CompressionMethodForHeader); - WriteLeInt((int)entry.DosTime); - - // TODO: Refactor header writing. Its done in several places. - if (headerInfoAvailable) { - WriteLeInt((int)entry.Crc); - if (entry.LocalHeaderRequiresZip64) { - WriteLeInt(-1); - WriteLeInt(-1); - } else { - WriteLeInt(entry.IsCrypted ? (int)entry.CompressedSize + ZipConstants.CryptoHeaderSize : (int)entry.CompressedSize); - WriteLeInt((int)entry.Size); - } - } else { - if (patchEntryHeader) { - crcPatchPos = baseOutputStream_.Position; - } - WriteLeInt(0); // Crc - - if (patchEntryHeader) { - sizePatchPos = baseOutputStream_.Position; - } - - // For local header both sizes appear in Zip64 Extended Information - if (entry.LocalHeaderRequiresZip64 || patchEntryHeader) { - WriteLeInt(-1); - WriteLeInt(-1); - } else { - WriteLeInt(0); // Compressed size - WriteLeInt(0); // Uncompressed size - } - } - - byte[] name = ZipConstants.ConvertToArray(entry.Flags, entry.Name); - - if (name.Length > 0xFFFF) { - throw new ZipException("Entry name too long."); - } - - var ed = new ZipExtraData(entry.ExtraData); - - if (entry.LocalHeaderRequiresZip64) { - ed.StartNewEntry(); - if (headerInfoAvailable) { - ed.AddLeLong(entry.Size); - ed.AddLeLong(entry.CompressedSize); - } else { - ed.AddLeLong(-1); - ed.AddLeLong(-1); - } - ed.AddNewEntry(1); - - if (!ed.Find(1)) { - throw new ZipException("Internal error cant find extra data"); - } - - if (patchEntryHeader) { - sizePatchPos = ed.CurrentReadIndex; - } - } else { - ed.Delete(1); - } - - if (entry.AESKeySize > 0) { - AddExtraDataAES(entry, ed); - } - byte[] extra = ed.GetEntryData(); - - WriteLeShort(name.Length); - WriteLeShort(extra.Length); - - if (name.Length > 0) { - baseOutputStream_.Write(name, 0, name.Length); - } + // Apply any required transforms to the entry name + TransformEntryName(entry); - if (entry.LocalHeaderRequiresZip64 && patchEntryHeader) { - sizePatchPos += baseOutputStream_.Position; - } + // Write the local file header + offset += ZipFormat.WriteLocalHeader(stream, entry, out var entryPatchData, + headerInfoAvailable, patchEntryHeader, streamOffset, _stringCodec); - if (extra.Length > 0) { - baseOutputStream_.Write(extra, 0, extra.Length); - } + patchData = entryPatchData; - offset += ZipConstants.LocalHeaderBaseSize + name.Length + extra.Length; // Fix offsetOfCentraldir for AES if (entry.AESKeySize > 0) offset += entry.AESOverheadSize; // Activate the entry. curEntry = entry; + size = 0; + + if(entryIsPassthrough) + return; + crc.Reset(); - if (method == CompressionMethod.Deflated) { + if (method == CompressionMethod.Deflated) + { deflater_.Reset(); deflater_.SetLevel(compressionLevel); } - size = 0; + } - if (entry.IsCrypted) { - if (entry.AESKeySize > 0) { - WriteAESHeader(entry); - } else { - if (entry.Crc < 0) { // so testing Zip will says its ok - WriteEncryptionHeader(entry.DosTime << 16); - } else { - WriteEncryptionHeader(entry.Crc); - } - } - } + /// + /// Starts a new Zip entry. It automatically closes the previous + /// entry if present. + /// All entry elements bar name are optional, but must be correct if present. + /// If the compression method is stored and the output is not patchable + /// the compression for that entry is automatically changed to deflate level 0 + /// + /// + /// the entry. + /// + /// The that can be used to cancel the operation. + /// + /// if entry passed is null. + /// + /// + /// if an I/O error occured. + /// + /// + /// if stream was finished + /// + /// + /// Too many entries in the Zip file
+ /// Entry name is too long
+ /// Finish has already been called
+ ///
+ /// + /// The Compression method specified for the entry is unsupported. + /// + public async Task PutNextEntryAsync(ZipEntry entry, CancellationToken ct = default) + { + if (curEntry != null) await CloseEntryAsync(ct).ConfigureAwait(false); + var position = CanPatchEntries ? baseOutputStream_.Position : -1; + await baseOutputStream_.WriteProcToStreamAsync(s => + { + PutNextEntry(s, entry, position); + }, ct).ConfigureAwait(false); + + if (!entry.IsCrypted) return; + await WriteOutputAsync(GetEntryEncryptionHeader(entry)).ConfigureAwait(false); } /// /// Closes the current entry, updating header and footer information as required /// + /// + /// Invalid entry field values. + /// /// /// An I/O error occurs. /// @@ -408,154 +551,226 @@ public void PutNextEntry(ZipEntry entry) /// public void CloseEntry() { - if (curEntry == null) { - throw new InvalidOperationException("No open entry"); + // Note: This method will run synchronously + FinishCompressionSyncOrAsync(null).GetAwaiter().GetResult(); + WriteEntryFooter(baseOutputStream_); + + // Patch the header if possible + if (patchEntryHeader) + { + patchEntryHeader = false; + ZipFormat.PatchLocalHeaderSync(baseOutputStream_, curEntry, patchData); } - long csize = size; + entries.Add(curEntry); + curEntry = null; + } + + private async Task FinishCompressionSyncOrAsync(CancellationToken? ct) + { + // Compression handled externally + if (entryIsPassthrough) return; // First finish the deflater, if appropriate - if (curMethod == CompressionMethod.Deflated) { - if (size >= 0) { - base.Finish(); - csize = deflater_.TotalOut; - } else { + if (curMethod == CompressionMethod.Deflated) + { + if (size >= 0) + { + if (ct.HasValue) { + await base.FinishAsync(ct.Value).ConfigureAwait(false); + } else { + base.Finish(); + } + } + else + { deflater_.Reset(); } } - - // Write the AES Authentication Code (a hash of the compressed and encrypted data) - if (curEntry.AESKeySize > 0) { - baseOutputStream_.Write(AESAuthCode, 0, 10); + if (curMethod == CompressionMethod.Stored) + { + // This is done by Finish() for Deflated entries, but we need to do it + // ourselves for Stored ones + base.GetAuthCodeIfAES(); } - if (curEntry.Size < 0) { - curEntry.Size = size; - } else if (curEntry.Size != size) { - throw new ZipException("size was " + size + ", but I expected " + curEntry.Size); + return; + } + + /// + public async Task CloseEntryAsync(CancellationToken ct) + { + await FinishCompressionSyncOrAsync(ct).ConfigureAwait(false); + await baseOutputStream_.WriteProcToStreamAsync(WriteEntryFooter, ct).ConfigureAwait(false); + + // Patch the header if possible + if (patchEntryHeader) + { + patchEntryHeader = false; + await ZipFormat.PatchLocalHeaderAsync(baseOutputStream_, curEntry, patchData, ct).ConfigureAwait(false); } - if (curEntry.CompressedSize < 0) { - curEntry.CompressedSize = csize; - } else if (curEntry.CompressedSize != csize) { - throw new ZipException("compressed size was " + csize + ", but I expected " + curEntry.CompressedSize); + entries.Add(curEntry); + curEntry = null; + } + + internal void WriteEntryFooter(Stream stream) + { + if (curEntry == null) + { + throw new InvalidOperationException("No open entry"); } - if (curEntry.Crc < 0) { - curEntry.Crc = crc.Value; - } else if (curEntry.Crc != crc.Value) { - throw new ZipException("crc was " + crc.Value + ", but I expected " + curEntry.Crc); + if(entryIsPassthrough) + { + if(curEntry.CompressedSize != size) + { + throw new ZipException($"compressed size was {size}, but {curEntry.CompressedSize} expected"); + } + + offset += size; + return; } - offset += csize; + long csize = size; - if (curEntry.IsCrypted) { - if (curEntry.AESKeySize > 0) { - curEntry.CompressedSize += curEntry.AESOverheadSize; + if (curMethod == CompressionMethod.Deflated && size >= 0) + { + csize = deflater_.TotalOut; + } - } else { - curEntry.CompressedSize += ZipConstants.CryptoHeaderSize; + // Write the AES Authentication Code (a hash of the compressed and encrypted data) + if (curEntry.AESKeySize > 0) + { + stream.Write(AESAuthCode, 0, 10); + // Always use 0 as CRC for AE-2 format + curEntry.Crc = 0; + } + else + { + if (curEntry.Crc < 0) + { + curEntry.Crc = crc.Value; + } + else if (curEntry.Crc != crc.Value) + { + throw new ZipException($"crc was {crc.Value}, but {curEntry.Crc} was expected"); } } - // Patch the header if possible - if (patchEntryHeader) { - patchEntryHeader = false; - - long curPos = baseOutputStream_.Position; - baseOutputStream_.Seek(crcPatchPos, SeekOrigin.Begin); - WriteLeInt((int)curEntry.Crc); + if (curEntry.Size < 0) + { + curEntry.Size = size; + } + else if (curEntry.Size != size) + { + throw new ZipException($"size was {size}, but {curEntry.Size} was expected"); + } - if (curEntry.LocalHeaderRequiresZip64) { + if (curEntry.CompressedSize < 0) + { + curEntry.CompressedSize = csize; + } + else if (curEntry.CompressedSize != csize) + { + throw new ZipException($"compressed size was {csize}, but {curEntry.CompressedSize} expected"); + } - if (sizePatchPos == -1) { - throw new ZipException("Entry requires zip64 but this has been turned off"); - } + offset += csize; - baseOutputStream_.Seek(sizePatchPos, SeekOrigin.Begin); - WriteLeLong(curEntry.Size); - WriteLeLong(curEntry.CompressedSize); - } else { - WriteLeInt((int)curEntry.CompressedSize); - WriteLeInt((int)curEntry.Size); - } - baseOutputStream_.Seek(curPos, SeekOrigin.Begin); + if (curEntry.IsCrypted) + { + curEntry.CompressedSize += curEntry.EncryptionOverheadSize; } // Add data descriptor if flagged as required - if ((curEntry.Flags & 8) != 0) { - WriteLeInt(ZipConstants.DataDescriptorSignature); - WriteLeInt(unchecked((int)curEntry.Crc)); - - if (curEntry.LocalHeaderRequiresZip64) { - WriteLeLong(curEntry.CompressedSize); - WriteLeLong(curEntry.Size); + if ((curEntry.Flags & 8) != 0) + { + stream.WriteLEInt(ZipConstants.DataDescriptorSignature); + stream.WriteLEInt(unchecked((int)curEntry.Crc)); + + if (curEntry.LocalHeaderRequiresZip64) + { + stream.WriteLELong(curEntry.CompressedSize); + stream.WriteLELong(curEntry.Size); offset += ZipConstants.Zip64DataDescriptorSize; - } else { - WriteLeInt((int)curEntry.CompressedSize); - WriteLeInt((int)curEntry.Size); + } + else + { + stream.WriteLEInt((int)curEntry.CompressedSize); + stream.WriteLEInt((int)curEntry.Size); offset += ZipConstants.DataDescriptorSize; } } - - entries.Add(curEntry); - curEntry = null; } - void WriteEncryptionHeader(long crcValue) + + + // File format for AES: + // Size (bytes) Content + // ------------ ------- + // Variable Salt value + // 2 Password verification value + // Variable Encrypted file data + // 10 Authentication code + // + // Value in the "compressed size" fields of the local file header and the central directory entry + // is the total size of all the items listed above. In other words, it is the total size of the + // salt value, password verification value, encrypted data, and authentication code. + + /// + /// Initializes encryption keys based on given password. + /// + protected byte[] InitializeAESPassword(ZipEntry entry, string rawPassword) + { + var salt = new byte[entry.AESSaltLen]; + // Salt needs to be cryptographically random, and unique per file + if (_aesRnd == null) + _aesRnd = RandomNumberGenerator.Create(); + _aesRnd.GetBytes(salt); + int blockSize = entry.AESKeySize / 8; // bits to bytes + + cryptoTransform_ = new ZipAESTransform(rawPassword, salt, blockSize, true); + + var headBytes = new byte[salt.Length + 2]; + + Array.Copy(salt, headBytes, salt.Length); + Array.Copy(((ZipAESTransform)cryptoTransform_).PwdVerifier, 0, + headBytes, headBytes.Length - 2, 2); + + return headBytes; + } + + private byte[] CreateZipCryptoHeader(long crcValue) { offset += ZipConstants.CryptoHeaderSize; - InitializePassword(Password); + InitializeZipCryptoPassword(Password); byte[] cryptBuffer = new byte[ZipConstants.CryptoHeaderSize]; - var rnd = new Random(); - rnd.NextBytes(cryptBuffer); + using (var rng = RandomNumberGenerator.Create()) + { + rng.GetBytes(cryptBuffer); + } + cryptBuffer[11] = (byte)(crcValue >> 24); EncryptBlock(cryptBuffer, 0, cryptBuffer.Length); - baseOutputStream_.Write(cryptBuffer, 0, cryptBuffer.Length); - } - - private static void AddExtraDataAES(ZipEntry entry, ZipExtraData extraData) - { - // Vendor Version: AE-1 IS 1. AE-2 is 2. With AE-2 no CRC is required and 0 is stored. - const int VENDOR_VERSION = 2; - // Vendor ID is the two ASCII characters "AE". - const int VENDOR_ID = 0x4541; //not 6965; - extraData.StartNewEntry(); - // Pack AES extra data field see http://www.winzip.com/aes_info.htm - //extraData.AddLeShort(7); // Data size (currently 7) - extraData.AddLeShort(VENDOR_VERSION); // 2 = AE-2 - extraData.AddLeShort(VENDOR_ID); // "AE" - extraData.AddData(entry.AESEncryptionStrength); // 1 = 128, 2 = 192, 3 = 256 - extraData.AddLeShort((int)entry.CompressionMethod); // The actual compression method used to compress the file - extraData.AddNewEntry(0x9901); + return cryptBuffer; } - - // Replaces WriteEncryptionHeader for AES - // - private void WriteAESHeader(ZipEntry entry) + + /// + /// Initializes encryption keys based on given . + /// + /// The password. + private void InitializeZipCryptoPassword(string password) { - byte[] salt; - byte[] pwdVerifier; - InitializeAESPassword(entry, Password, out salt, out pwdVerifier); - // File format for AES: - // Size (bytes) Content - // ------------ ------- - // Variable Salt value - // 2 Password verification value - // Variable Encrypted file data - // 10 Authentication code - // - // Value in the "compressed size" fields of the local file header and the central directory entry - // is the total size of all the items listed above. In other words, it is the total size of the - // salt value, password verification value, encrypted data, and authentication code. - baseOutputStream_.Write(salt, 0, salt.Length); - baseOutputStream_.Write(pwdVerifier, 0, pwdVerifier.Length); + var pkManaged = new PkzipClassicManaged(); + byte[] key = PkzipClassic.GenerateKeys(ZipCryptoEncoding.GetBytes(password)); + cryptoTransform_ = pkManaged.CreateEncryptor(key, null); } - + /// /// Writes the given buffer to the current entry. /// @@ -565,51 +780,85 @@ private void WriteAESHeader(ZipEntry entry) /// Archive size is invalid /// No entry is active. public override void Write(byte[] buffer, int offset, int count) + => WriteSyncOrAsync(buffer, offset, count, null).GetAwaiter().GetResult(); + + /// + public override async Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken ct) + => await WriteSyncOrAsync(buffer, offset, count, ct).ConfigureAwait(false); + + private async Task WriteSyncOrAsync(byte[] buffer, int offset, int count, CancellationToken? ct) { - if (curEntry == null) { + if (curEntry == null) + { throw new InvalidOperationException("No open entry."); } - if (buffer == null) { + if (buffer == null) + { throw new ArgumentNullException(nameof(buffer)); } - if (offset < 0) { + if (offset < 0) + { throw new ArgumentOutOfRangeException(nameof(offset), "Cannot be negative"); } - if (count < 0) { + if (count < 0) + { throw new ArgumentOutOfRangeException(nameof(count), "Cannot be negative"); } - if ((buffer.Length - offset) < count) { + if ((buffer.Length - offset) < count) + { throw new ArgumentException("Invalid offset/count combination"); } - crc.Update(buffer, offset, count); - size += count; + if (curEntry.AESKeySize == 0 && !entryIsPassthrough) + { + // Only update CRC if AES is not enabled and entry is not a passthrough one + crc.Update(new ArraySegment(buffer, offset, count)); + } - switch (curMethod) { - case CompressionMethod.Deflated: - base.Write(buffer, offset, count); - break; + size += count; - case CompressionMethod.Stored: - if (Password != null) { - CopyAndEncrypt(buffer, offset, count); - } else { + if (curMethod == CompressionMethod.Stored || entryIsPassthrough) + { + if (Password != null) + { + CopyAndEncrypt(buffer, offset, count); + } + else + { + if (ct.HasValue) + { + await baseOutputStream_.WriteAsync(buffer, offset, count, ct.Value).ConfigureAwait(false); + } + else + { baseOutputStream_.Write(buffer, offset, count); } - break; + } + } + else + { + if (ct.HasValue) + { + await base.WriteAsync(buffer, offset, count, ct.Value).ConfigureAwait(false); + } + else + { + base.Write(buffer, offset, count); + } } } - void CopyAndEncrypt(byte[] buffer, int offset, int count) + private void CopyAndEncrypt(byte[] buffer, int offset, int count) { - const int CopyBufferSize = 4096; - byte[] localBuffer = new byte[CopyBufferSize]; - while (count > 0) { - int bufferCount = (count < CopyBufferSize) ? count : CopyBufferSize; + const int copyBufferSize = 4096; + byte[] localBuffer = new byte[copyBufferSize]; + while (count > 0) + { + int bufferCount = (count < copyBufferSize) ? count : copyBufferSize; Array.Copy(buffer, offset, localBuffer, 0, bufferCount); EncryptBlock(localBuffer, 0, bufferCount); @@ -635,183 +884,145 @@ void CopyAndEncrypt(byte[] buffer, int offset, int count) /// public override void Finish() { - if (entries == null) { + if (entries == null) + { return; } - if (curEntry != null) { + if (curEntry != null) + { CloseEntry(); } long numEntries = entries.Count; long sizeEntries = 0; - foreach (ZipEntry entry in entries) { - WriteLeInt(ZipConstants.CentralHeaderSignature); - WriteLeShort(ZipConstants.VersionMadeBy); - WriteLeShort(entry.Version); - WriteLeShort(entry.Flags); - WriteLeShort((short)entry.CompressionMethodForHeader); - WriteLeInt((int)entry.DosTime); - WriteLeInt((int)entry.Crc); - - if (entry.IsZip64Forced() || - (entry.CompressedSize >= uint.MaxValue)) { - WriteLeInt(-1); - } else { - WriteLeInt((int)entry.CompressedSize); - } - - if (entry.IsZip64Forced() || - (entry.Size >= uint.MaxValue)) { - WriteLeInt(-1); - } else { - WriteLeInt((int)entry.Size); - } - - byte[] name = ZipConstants.ConvertToArray(entry.Flags, entry.Name); - - if (name.Length > 0xffff) { - throw new ZipException("Name too long."); - } - - var ed = new ZipExtraData(entry.ExtraData); - - if (entry.CentralHeaderRequiresZip64) { - ed.StartNewEntry(); - if (entry.IsZip64Forced() || - (entry.Size >= 0xffffffff)) { - ed.AddLeLong(entry.Size); - } - - if (entry.IsZip64Forced() || - (entry.CompressedSize >= 0xffffffff)) { - ed.AddLeLong(entry.CompressedSize); - } - - if (entry.Offset >= 0xffffffff) { - ed.AddLeLong(entry.Offset); - } - - ed.AddNewEntry(1); - } else { - ed.Delete(1); - } - - if (entry.AESKeySize > 0) { - AddExtraDataAES(entry, ed); - } - byte[] extra = ed.GetEntryData(); + foreach (var entry in entries) + { + sizeEntries += ZipFormat.WriteEndEntry(baseOutputStream_, entry, _stringCodec); + } - byte[] entryComment = - (entry.Comment != null) ? - ZipConstants.ConvertToArray(entry.Flags, entry.Comment) : - new byte[0]; + ZipFormat.WriteEndOfCentralDirectory(baseOutputStream_, numEntries, sizeEntries, offset, zipComment); - if (entryComment.Length > 0xffff) { - throw new ZipException("Comment too long."); - } + entries = null; + } - WriteLeShort(name.Length); - WriteLeShort(extra.Length); - WriteLeShort(entryComment.Length); - WriteLeShort(0); // disk number - WriteLeShort(0); // internal file attributes - // external file attributes - - if (entry.ExternalFileAttributes != -1) { - WriteLeInt(entry.ExternalFileAttributes); - } else { - if (entry.IsDirectory) { // mark entry as directory (from nikolam.AT.perfectinfo.com) - WriteLeInt(16); - } else { - WriteLeInt(0); - } + /// > + public override async Task FinishAsync(CancellationToken ct) + { + using (var ms = new MemoryStream()) + { + if (entries == null) + { + return; } - if (entry.Offset >= uint.MaxValue) { - WriteLeInt(-1); - } else { - WriteLeInt((int)entry.Offset); + if (curEntry != null) + { + await CloseEntryAsync(ct).ConfigureAwait(false); } - if (name.Length > 0) { - baseOutputStream_.Write(name, 0, name.Length); - } + long numEntries = entries.Count; + long sizeEntries = 0; - if (extra.Length > 0) { - baseOutputStream_.Write(extra, 0, extra.Length); + foreach (var entry in entries) + { + await baseOutputStream_.WriteProcToStreamAsync(ms, s => + { + sizeEntries += ZipFormat.WriteEndEntry(s, entry, _stringCodec); + }, ct).ConfigureAwait(false); } - if (entryComment.Length > 0) { - baseOutputStream_.Write(entryComment, 0, entryComment.Length); - } + await baseOutputStream_.WriteProcToStreamAsync(ms, s + => ZipFormat.WriteEndOfCentralDirectory(s, numEntries, sizeEntries, offset, zipComment), + ct).ConfigureAwait(false); - sizeEntries += ZipConstants.CentralHeaderBaseSize + name.Length + extra.Length + entryComment.Length; + entries = null; } + } - using (ZipHelperStream zhs = new ZipHelperStream(baseOutputStream_)) { - zhs.WriteEndOfCentralDirectory(numEntries, sizeEntries, offset, zipComment); + /// + /// Flushes the stream by calling Flush on the deflater stream unless + /// the current compression method is . Then it flushes the underlying output stream. + /// + public override void Flush() + { + if(curMethod == CompressionMethod.Stored) + { + baseOutputStream_.Flush(); + } + else + { + base.Flush(); } - - entries = null; } #region Instance Fields + /// /// The entries for the archive. /// - List entries = new List(); + private List entries = new List(); /// /// Used to track the crc of data added to entries. /// - Crc32 crc = new Crc32(); + private Crc32 crc = new Crc32(); /// /// The current entry being added. /// - ZipEntry curEntry; + private ZipEntry curEntry; - int defaultCompressionLevel = Deflater.DEFAULT_COMPRESSION; + private bool entryIsPassthrough; - CompressionMethod curMethod = CompressionMethod.Deflated; + private int defaultCompressionLevel = Deflater.DEFAULT_COMPRESSION; + + private CompressionMethod curMethod = CompressionMethod.Deflated; /// /// Used to track the size of data for an entry during writing. /// - long size; + private long size; /// /// Offset to be recorded for each entry in the central header. /// - long offset; + private long offset; /// /// Comment for the entire archive recorded in central header. /// - byte[] zipComment = new byte[0]; + private byte[] zipComment = Empty.Array(); /// /// Flag indicating that header patching is required for the current entry. /// - bool patchEntryHeader; - - /// - /// Position to patch crc - /// - long crcPatchPos = -1; + private bool patchEntryHeader; /// - /// Position to patch size. + /// The values to patch in the entry local header /// - long sizePatchPos = -1; + private EntryPatchData patchData; // Default is dynamic which is not backwards compatible and can cause problems // with XP's built in compression which cant read Zip64 archives. // However it does avoid the situation were a large file is added and cannot be completed correctly. // NOTE: Setting the size for entries before they are added is the best solution! - UseZip64 useZip64_ = UseZip64.Dynamic; - #endregion + private UseZip64 useZip64_ = UseZip64.Dynamic; + + /// + /// The password to use when encrypting archive entries. + /// + private string password; + + #endregion Instance Fields + + #region Static Fields + + // Static to help ensure that multiple files within a zip will get different random salt + private static RandomNumberGenerator _aesRnd = RandomNumberGenerator.Create(); + + #endregion Static Fields } } diff --git a/src/ICSharpCode.SharpZipLib/Zip/ZipStrings.cs b/src/ICSharpCode.SharpZipLib/Zip/ZipStrings.cs new file mode 100644 index 000000000..3eab416ef --- /dev/null +++ b/src/ICSharpCode.SharpZipLib/Zip/ZipStrings.cs @@ -0,0 +1,260 @@ +using System; +using System.Text; +using ICSharpCode.SharpZipLib.Core; + +namespace ICSharpCode.SharpZipLib.Zip +{ + internal static class EncodingExtensions + { + public static bool IsZipUnicode(this Encoding e) + => e.Equals(StringCodec.UnicodeZipEncoding); + } + + /// + /// Deprecated way of setting zip encoding provided for backwards compability. + /// Use when possible. + /// + /// + /// If any ZipStrings properties are being modified, it will enter a backwards compatibility mode, mimicking the + /// old behaviour where a single instance was shared between all Zip* instances. + /// + public static class ZipStrings + { + static StringCodec CompatCodec = StringCodec.Default; + + private static bool compatibilityMode; + + /// + /// Returns a new instance or the shared backwards compatible instance. + /// + /// + public static StringCodec GetStringCodec() + => compatibilityMode ? CompatCodec : StringCodec.Default; + + /// + [Obsolete("Use ZipFile/Zip*Stream StringCodec instead")] + public static int CodePage + { + get => CompatCodec.CodePage; + set + { + CompatCodec = new StringCodec(CompatCodec.ForceZipLegacyEncoding, Encoding.GetEncoding(value)) + { + ZipArchiveCommentEncoding = CompatCodec.ZipArchiveCommentEncoding, + ZipCryptoEncoding = CompatCodec.ZipCryptoEncoding, + }; + compatibilityMode = true; + } + } + + /// + [Obsolete("Use ZipFile/Zip*Stream StringCodec instead")] + public static int SystemDefaultCodePage => StringCodec.SystemDefaultCodePage; + + /// + [Obsolete("Use ZipFile/Zip*Stream StringCodec instead")] + public static bool UseUnicode + { + get => !CompatCodec.ForceZipLegacyEncoding; + set + { + CompatCodec = new StringCodec(!value, CompatCodec.LegacyEncoding) + { + ZipArchiveCommentEncoding = CompatCodec.ZipArchiveCommentEncoding, + ZipCryptoEncoding = CompatCodec.ZipCryptoEncoding, + }; + compatibilityMode = true; + } + } + + /// + [Obsolete("Use ZipFile/Zip*Stream StringCodec instead")] + private static bool HasUnicodeFlag(int flags) + => ((GeneralBitFlags)flags).HasFlag(GeneralBitFlags.UnicodeText); + + /// + [Obsolete("Use ZipFile/Zip*Stream StringCodec instead")] + public static string ConvertToString(byte[] data, int count) + => CompatCodec.ZipOutputEncoding.GetString(data, 0, count); + + /// + [Obsolete("Use ZipFile/Zip*Stream StringCodec instead")] + public static string ConvertToString(byte[] data) + => CompatCodec.ZipOutputEncoding.GetString(data); + + /// + [Obsolete("Use ZipFile/Zip*Stream StringCodec instead")] + public static string ConvertToStringExt(int flags, byte[] data, int count) + => CompatCodec.ZipEncoding(HasUnicodeFlag(flags)).GetString(data, 0, count); + + /// + [Obsolete("Use ZipFile/Zip*Stream StringCodec instead")] + public static string ConvertToStringExt(int flags, byte[] data) + => CompatCodec.ZipEncoding(HasUnicodeFlag(flags)).GetString(data); + + /// + [Obsolete("Use ZipFile/Zip*Stream StringCodec instead")] + public static byte[] ConvertToArray(string str) + => ConvertToArray(0, str); + + /// + [Obsolete("Use ZipFile/Zip*Stream StringCodec instead")] + public static byte[] ConvertToArray(int flags, string str) + => (string.IsNullOrEmpty(str)) + ? Empty.Array() + : CompatCodec.ZipEncoding(HasUnicodeFlag(flags)).GetBytes(str); + } + + /// + /// Utility class for resolving the encoding used for reading and writing strings + /// + public class StringCodec + { + internal StringCodec(bool forceLegacyEncoding, Encoding legacyEncoding) + { + LegacyEncoding = legacyEncoding; + ForceZipLegacyEncoding = forceLegacyEncoding; + ZipArchiveCommentEncoding = legacyEncoding; + ZipCryptoEncoding = legacyEncoding; + } + + /// + /// Creates a StringCodec that uses the system default encoder or UTF-8 depending on whether the zip entry Unicode flag is set + /// + public static StringCodec Default + => new StringCodec(false, SystemDefaultEncoding); + + /// + /// Creates a StringCodec that uses an encoding from the specified code page except for zip entries with the Unicode flag + /// + public static StringCodec FromCodePage(int codePage) + => new StringCodec(false, Encoding.GetEncoding(codePage)); + + /// + /// Creates a StringCodec that uses an the specified encoding, except for zip entries with the Unicode flag + /// + public static StringCodec FromEncoding(Encoding encoding) + => new StringCodec(false, encoding); + + /// + /// Creates a StringCodec that uses the zip specification encoder or UTF-8 depending on whether the zip entry Unicode flag is set + /// + public static StringCodec WithStrictSpecEncoding() + => new StringCodec(false, Encoding.GetEncoding(ZipSpecCodePage)); + + /// + /// If set, use the encoding set by for zip entries instead of the defaults + /// + public bool ForceZipLegacyEncoding { get; internal set; } + + /// + /// The default encoding used for ZipCrypto passwords in zip files, set to + /// for greatest compability. + /// + public static Encoding DefaultZipCryptoEncoding => SystemDefaultEncoding; + + /// + /// Returns the encoding for an output . + /// Unless overriden by it returns . + /// + public Encoding ZipOutputEncoding => ZipEncoding(!ForceZipLegacyEncoding); + + /// + /// Returns if is set, otherwise it returns the encoding indicated by + /// + public Encoding ZipEncoding(bool unicode) + => unicode ? UnicodeZipEncoding : LegacyEncoding; + + /// + /// Returns the appropriate encoding for an input according to . + /// If overridden by , it always returns the encoding indicated by . + /// + /// + /// + public Encoding ZipInputEncoding(GeneralBitFlags flags) + => ZipEncoding(!ForceZipLegacyEncoding && flags.HasAny(GeneralBitFlags.UnicodeText)); + + /// + public Encoding ZipInputEncoding(int flags) => ZipInputEncoding((GeneralBitFlags)flags); + + /// Code page encoding, used for non-unicode strings + /// + /// The original Zip specification (https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT) states + /// that file names should only be encoded with IBM Code Page 437 or UTF-8. + /// In practice, most zip apps use OEM or system encoding (typically cp437 on Windows). + /// + public Encoding LegacyEncoding { get; internal set; } + + /// + /// Returns the UTF-8 code page (65001) used for zip entries with unicode flag set + /// + public static readonly Encoding UnicodeZipEncoding = Encoding.UTF8; + + /// + /// Code page used for non-unicode strings and legacy zip encoding (if is set). + /// Default value is + /// + public int CodePage => LegacyEncoding.CodePage; + + /// + /// The non-unicode code page that should be used according to the zip specification + /// + public const int ZipSpecCodePage = 437; + + /// + /// Operating system default codepage. + /// + public static int SystemDefaultCodePage => SystemDefaultEncoding.CodePage; + + /// + /// The system default encoding. + /// + public static Encoding SystemDefaultEncoding => Encoding.GetEncoding(0); + + /// + /// The encoding used for the zip archive comment. Defaults to the encoding for , since + /// no unicode flag can be set for it in the files. + /// + public Encoding ZipArchiveCommentEncoding { get; internal set; } + + /// + /// The encoding used for the ZipCrypto passwords. Defaults to . + /// + public Encoding ZipCryptoEncoding { get; internal set; } + + /// + /// Create a copy of this StringCodec with the specified zip archive comment encoding + /// + /// + /// + public StringCodec WithZipArchiveCommentEncoding(Encoding commentEncoding) + => new StringCodec(ForceZipLegacyEncoding, LegacyEncoding) + { + ZipArchiveCommentEncoding = commentEncoding, + ZipCryptoEncoding = ZipCryptoEncoding + }; + + /// + /// Create a copy of this StringCodec with the specified zip crypto password encoding + /// + /// + /// + public StringCodec WithZipCryptoEncoding(Encoding cryptoEncoding) + => new StringCodec(ForceZipLegacyEncoding, LegacyEncoding) + { + ZipArchiveCommentEncoding = ZipArchiveCommentEncoding, + ZipCryptoEncoding = cryptoEncoding + }; + + /// + /// Create a copy of this StringCodec that ignores the Unicode flag when reading entries + /// + /// + public StringCodec WithForcedLegacyEncoding() + => new StringCodec(true, LegacyEncoding) + { + ZipArchiveCommentEncoding = ZipArchiveCommentEncoding, + ZipCryptoEncoding = ZipCryptoEncoding + }; + } +} diff --git a/test/.globalconfig b/test/.globalconfig new file mode 100644 index 000000000..14f57bc66 --- /dev/null +++ b/test/.globalconfig @@ -0,0 +1,3 @@ +is_global = true +global_level = 2 +dotnet_diagnostic.CA2007.severity = none diff --git a/test/ICSharpCode.SharpZipLib.TestBootstrapper/ICSharpCode.SharpZipLib.TestBootstrapper.csproj b/test/ICSharpCode.SharpZipLib.TestBootstrapper/ICSharpCode.SharpZipLib.TestBootstrapper.csproj deleted file mode 100644 index 44325d267..000000000 --- a/test/ICSharpCode.SharpZipLib.TestBootstrapper/ICSharpCode.SharpZipLib.TestBootstrapper.csproj +++ /dev/null @@ -1,21 +0,0 @@ - - - - Exe - netcoreapp2.0 - - - - - - - - - - - - - - - - diff --git a/test/ICSharpCode.SharpZipLib.TestBootstrapper/NuGet.config b/test/ICSharpCode.SharpZipLib.TestBootstrapper/NuGet.config deleted file mode 100644 index 7be9c71ec..000000000 --- a/test/ICSharpCode.SharpZipLib.TestBootstrapper/NuGet.config +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - diff --git a/test/ICSharpCode.SharpZipLib.TestBootstrapper/Program.cs b/test/ICSharpCode.SharpZipLib.TestBootstrapper/Program.cs deleted file mode 100644 index 3f138610e..000000000 --- a/test/ICSharpCode.SharpZipLib.TestBootstrapper/Program.cs +++ /dev/null @@ -1,17 +0,0 @@ -using System; -using NUnit.Common; -using NUnitLite; -using System.Reflection; - -namespace ICSharpCode.SharpZipLib.TestBootstrapper -{ - public class Program - { - static void Main(string[] args) - { - new AutoRun(typeof(ICSharpCode.SharpZipLib.Tests.Base.InflaterDeflaterTestSuite).GetTypeInfo().Assembly) - .Execute(args, new ExtendedTextWrapper(Console.Out), Console.In); - } - - } -} diff --git a/test/ICSharpCode.SharpZipLib.Tests/BZip2/Bzip2Tests.cs b/test/ICSharpCode.SharpZipLib.Tests/BZip2/Bzip2Tests.cs index a807b0ed9..62d5a7874 100644 --- a/test/ICSharpCode.SharpZipLib.Tests/BZip2/Bzip2Tests.cs +++ b/test/ICSharpCode.SharpZipLib.Tests/BZip2/Bzip2Tests.cs @@ -1,9 +1,7 @@ -using System; -using System.IO; using ICSharpCode.SharpZipLib.BZip2; using ICSharpCode.SharpZipLib.Tests.TestSupport; using NUnit.Framework; -using System.Threading; +using System.IO; namespace ICSharpCode.SharpZipLib.Tests.BZip2 { @@ -13,6 +11,9 @@ namespace ICSharpCode.SharpZipLib.Tests.BZip2 [TestFixture] public class BZip2Suite { + // Use the same random seed to guarantee all the code paths are followed + const int RandomSeed = 4; + /// /// Basic compress/decompress test BZip2 /// @@ -22,30 +23,30 @@ public void BasicRoundTrip() { var ms = new MemoryStream(); var outStream = new BZip2OutputStream(ms); + + var buf = Utils.GetDummyBytes(size: 10000, RandomSeed); - byte[] buf = new byte[10000]; - var rnd = new Random(); - rnd.NextBytes(buf); - - outStream.Write(buf, 0, buf.Length); + outStream.Write(buf, offset: 0, buf.Length); outStream.Close(); ms = new MemoryStream(ms.GetBuffer()); - ms.Seek(0, SeekOrigin.Begin); - - using (BZip2InputStream inStream = new BZip2InputStream(ms)) { - byte[] buf2 = new byte[buf.Length]; - int pos = 0; - while (true) { - int numRead = inStream.Read(buf2, pos, 4096); - if (numRead <= 0) { - break; - } - pos += numRead; + ms.Seek(offset: 0, SeekOrigin.Begin); + + using BZip2InputStream inStream = new BZip2InputStream(ms); + var buf2 = new byte[buf.Length]; + var pos = 0; + while (true) + { + var numRead = inStream.Read(buf2, pos, count: 4096); + if (numRead <= 0) + { + break; } + pos += numRead; + } - for (int i = 0; i < buf.Length; ++i) { - Assert.AreEqual(buf2[i], buf[i]); - } + for (var i = 0; i < buf.Length; ++i) + { + Assert.AreEqual(buf2[i], buf[i]); } } @@ -63,113 +64,47 @@ public void CreateEmptyArchive() ms.Seek(0, SeekOrigin.Begin); - using (BZip2InputStream inStream = new BZip2InputStream(ms)) { + using (BZip2InputStream inStream = new BZip2InputStream(ms)) + { byte[] buffer = new byte[1024]; int pos = 0; - while (true) { + while (true) + { int numRead = inStream.Read(buffer, 0, buffer.Length); - if (numRead <= 0) { + if (numRead <= 0) + { break; } pos += numRead; } - Assert.AreEqual(pos, 0); + Assert.Zero(pos); } } - BZip2OutputStream outStream_; - BZip2InputStream inStream_; - WindowedStream window_; - long readTarget_; - long writeTarget_; - [Test] [Category("BZip2")] - [Ignore("TODO : Fix this")] - public void Performance() + [Category("Performance")] + [Explicit("Long-running")] + public void WriteThroughput() { - window_ = new WindowedStream(0x150000); - - outStream_ = new BZip2OutputStream(window_, 1); - - const long Target = 0x10000000; - readTarget_ = writeTarget_ = Target; - - Thread reader = new Thread(Reader); - reader.Name = "Reader"; - - Thread writer = new Thread(Writer); - writer.Name = "Writer"; - - DateTime startTime = DateTime.Now; - writer.Start(); - - inStream_ = new BZip2InputStream(window_); - - reader.Start(); - - Assert.IsTrue(writer.Join(TimeSpan.FromMinutes(5.0D))); - Assert.IsTrue(reader.Join(TimeSpan.FromMinutes(5.0D))); - - DateTime endTime = DateTime.Now; - TimeSpan span = endTime - startTime; - Console.WriteLine("Time {0} throughput {1} KB/Sec", span, (Target / 1024) / span.TotalSeconds); + PerformanceTesting.TestWrite( + size: TestDataSize.Small, + output: w => new BZip2OutputStream(w) + ); } - void Reader() - { - const int Size = 8192; - int readBytes = 1; - byte[] buffer = new byte[Size]; - - long passifierLevel = readTarget_ - 0x10000000; - - while ((readTarget_ > 0) && (readBytes > 0)) { - int count = Size; - if (count > readTarget_) { - count = (int)readTarget_; - } - - readBytes = inStream_.Read(buffer, 0, count); - readTarget_ -= readBytes; - - if (readTarget_ <= passifierLevel) { - Console.WriteLine("Reader {0} bytes remaining", readTarget_); - passifierLevel = readTarget_ - 0x10000000; - } - } - - Assert.IsTrue(window_.IsClosed, "Window should be closed"); - - // This shouldnt read any data but should read the footer - readBytes = inStream_.Read(buffer, 0, 1); - Assert.AreEqual(0, readBytes, "Stream should be empty"); - Assert.AreEqual(0, window_.Length, "Window should be closed"); - inStream_.Close(); - } - - void WriteTargetBytes() - { - const int Size = 8192; - - byte[] buffer = new byte[Size]; - - while (writeTarget_ > 0) { - int thisTime = Size; - if (thisTime > writeTarget_) { - thisTime = (int)writeTarget_; - } - - outStream_.Write(buffer, 0, thisTime); - writeTarget_ -= thisTime; - } - } - - void Writer() + [Test] + [Category("BZip2")] + [Category("Performance")] + [Explicit("Long-running")] + public void ReadWriteThroughput() { - WriteTargetBytes(); - outStream_.Close(); + PerformanceTesting.TestReadWrite( + size: TestDataSize.Small, + input: w => new BZip2InputStream(w), + output: w => new BZip2OutputStream(w) + ); } } } diff --git a/test/ICSharpCode.SharpZipLib.Tests/Base/InflaterDeflaterTests.cs b/test/ICSharpCode.SharpZipLib.Tests/Base/InflaterDeflaterTests.cs index 9dd7679b9..e9ba0ad77 100644 --- a/test/ICSharpCode.SharpZipLib.Tests/Base/InflaterDeflaterTests.cs +++ b/test/ICSharpCode.SharpZipLib.Tests/Base/InflaterDeflaterTests.cs @@ -1,11 +1,13 @@ -using System; -using System.IO; -using System.Security; -using System.Text; using ICSharpCode.SharpZipLib.Tests.TestSupport; using ICSharpCode.SharpZipLib.Zip.Compression; using ICSharpCode.SharpZipLib.Zip.Compression.Streams; using NUnit.Framework; +using System; +using System.IO; +using System.Security; +using System.Text; +using System.Threading; +using System.Threading.Tasks; namespace ICSharpCode.SharpZipLib.Tests.Base { @@ -15,59 +17,42 @@ namespace ICSharpCode.SharpZipLib.Tests.Base [TestFixture] public class InflaterDeflaterTestSuite { - void Inflate(MemoryStream ms, byte[] original, int level, bool zlib) + // Use the same random seed to guarantee all the code paths are followed + const int RandomSeed = 5; + + private void Inflate(MemoryStream ms, byte[] original, int level, bool zlib) { - ms.Seek(0, SeekOrigin.Begin); - - var inflater = new Inflater(!zlib); - var inStream = new InflaterInputStream(ms, inflater); byte[] buf2 = new byte[original.Length]; - int currentIndex = 0; - int count = buf2.Length; + using (var inStream = GetInflaterInputStream(ms, zlib)) + { + int currentIndex = 0; + int count = buf2.Length; - try { - while (true) { + while (true) + { int numRead = inStream.Read(buf2, currentIndex, count); - if (numRead <= 0) { + if (numRead <= 0) + { break; } currentIndex += numRead; count -= numRead; } - } catch (Exception ex) { - Console.WriteLine("Unexpected exception - '{0}'", ex.Message); - throw; - } - if (currentIndex != original.Length) { - Console.WriteLine("Original {0}, new {1}", original.Length, currentIndex); - Assert.Fail("Lengths different"); + Assert.That(currentIndex, Is.EqualTo(original.Length), "Decompressed data must have the same length as the original data"); } - for (int i = 0; i < original.Length; ++i) { - if (buf2[i] != original[i]) { - string description = string.Format("Difference at {0} level {1} zlib {2} ", i, level, zlib); - if (original.Length < 2048) { - var builder = new StringBuilder(description); - for (int d = 0; d < original.Length; ++d) { - builder.AppendFormat("{0} ", original[d]); - } - - Assert.Fail(builder.ToString()); - } else { - Assert.Fail(description); - } - } - } + VerifyInflatedData(original, buf2, level, zlib); } - MemoryStream Deflate(byte[] data, int level, bool zlib) + private MemoryStream Deflate(byte[] data, int level, bool zlib) { var memoryStream = new MemoryStream(); var deflater = new Deflater(level, !zlib); - using (DeflaterOutputStream outStream = new DeflaterOutputStream(memoryStream, deflater)) { + using (DeflaterOutputStream outStream = new DeflaterOutputStream(memoryStream, deflater)) + { outStream.IsStreamOwner = false; outStream.Write(data, 0, data.Length); outStream.Flush(); @@ -76,94 +61,198 @@ MemoryStream Deflate(byte[] data, int level, bool zlib) return memoryStream; } - void RandomDeflateInflate(int size, int level, bool zlib) + private void RandomDeflateInflate(int size, int level, bool zlib) { - byte[] buffer = new byte[size]; - var rnd = new Random(); - rnd.NextBytes(buffer); - - MemoryStream ms = Deflate(buffer, level, zlib); + var buffer = Utils.GetDummyBytes(size, RandomSeed); + var ms = Deflate(buffer, level, zlib); Inflate(ms, buffer, level, zlib); } + + private static InflaterInputStream GetInflaterInputStream(Stream compressedStream, bool zlib) + { + compressedStream.Seek(0, SeekOrigin.Begin); + + var inflater = new Inflater(!zlib); + var inStream = new InflaterInputStream(compressedStream, inflater); + + return inStream; + } + + private async Task InflateAsync(MemoryStream ms, byte[] original, int level, bool zlib) + { + byte[] buf2 = new byte[original.Length]; + + using (var inStream = GetInflaterInputStream(ms, zlib)) + { + int currentIndex = 0; + int count = buf2.Length; + + while (true) + { + int numRead = await inStream.ReadAsync(buf2, currentIndex, count); + if (numRead <= 0) + { + break; + } + currentIndex += numRead; + count -= numRead; + } + + Assert.That(currentIndex, Is.EqualTo(original.Length), "Decompressed data must have the same length as the original data"); + } + + VerifyInflatedData(original, buf2, level, zlib); + } + + private async Task DeflateAsync(byte[] data, int level, bool zlib) + { + var memoryStream = new MemoryStream(); + + var deflater = new Deflater(level, !zlib); + using (DeflaterOutputStream outStream = new DeflaterOutputStream(memoryStream, deflater)) + { + outStream.IsStreamOwner = false; + await outStream.WriteAsync(data, 0, data.Length); + await outStream.FlushAsync(); + await outStream.FinishAsync(CancellationToken.None); + } + return memoryStream; + } + + private async Task RandomDeflateInflateAsync(int size, int level, bool zlib) + { + var buffer = Utils.GetDummyBytes(size, RandomSeed); + var ms = await DeflateAsync(buffer, level, zlib); + await InflateAsync(ms, buffer, level, zlib); + } + + private void VerifyInflatedData(byte[] original, byte[] buf2, int level, bool zlib) + { + for (int i = 0; i < original.Length; ++i) + { + if (buf2[i] != original[i]) + { + string description = string.Format("Difference at {0} level {1} zlib {2} ", i, level, zlib); + if (original.Length < 2048) + { + var builder = new StringBuilder(description); + for (int d = 0; d < original.Length; ++d) + { + builder.AppendFormat("{0} ", original[d]); + } + + Assert.Fail(builder.ToString()); + } + else + { + Assert.Fail(description); + } + } + } + } /// /// Basic inflate/deflate test /// [Test] [Category("Base")] - public void InflateDeflateZlib() + public void InflateDeflateZlib([Range(0, 9)] int level) { - for (int level = 0; level < 10; ++level) { - RandomDeflateInflate(100000, level, true); - } + RandomDeflateInflate(100000, level, true); } - delegate void RunCompress(byte[] buffer); + /// + /// Basic async inflate/deflate test + /// + [Test] + [Category("Base")] + [Category("Async")] + public async Task InflateDeflateZlibAsync([Range(0, 9)] int level) + { + await RandomDeflateInflateAsync(size: 100000, level, zlib: true); + } + + private delegate void RunCompress(byte[] buffer); - int runLevel; - bool runZlib; - long runCount; - Random runRandom = new Random(5); + private int _runLevel; + private bool _runZlib; - void DeflateAndInflate(byte[] buffer) + private void DeflateAndInflate(byte[] buffer) { - ++runCount; - MemoryStream ms = Deflate(buffer, runLevel, runZlib); - Inflate(ms, buffer, runLevel, runZlib); + var ms = Deflate(buffer, _runLevel, _runZlib); + Inflate(ms, buffer, _runLevel, _runZlib); } - void TryVariants(RunCompress test, byte[] buffer, int index) + private void TryVariants(RunCompress test, byte[] buffer, Random random, int index) { int worker = 0; - while (worker <= 255) { + while (worker <= 255) + { buffer[index] = (byte)worker; - if (index < buffer.Length - 1) { - TryVariants(test, buffer, index + 1); - } else { + if (index < buffer.Length - 1) + { + TryVariants(test, buffer, random, index + 1); + } + else + { test(buffer); } - worker += runRandom.Next(256); + worker += random.Next(maxValue: 256); } } - void TryManyVariants(int level, bool zlib, RunCompress test, byte[] buffer) + private void TryManyVariants(int level, bool zlib, RunCompress test, byte[] buffer) { - runLevel = level; - runZlib = zlib; - TryVariants(test, buffer, 0); + var random = new Random(RandomSeed); + _runLevel = level; + _runZlib = zlib; + TryVariants(test, buffer, random, 0); } // TODO: Fix this - //[Test] - //[Category("Base")] - //public void SmallBlocks() - //{ - // byte[] buffer = new byte[10]; - // Array.Clear(buffer, 0, buffer.Length); - // TryManyVariants(0, false, new RunCompress(DeflateAndInflate), buffer); - //} + [Test] + [Category("Base")] + [Explicit("Long-running")] + public void SmallBlocks() + { + var buffer = new byte[10]; + TryManyVariants(level: 0, zlib: false, DeflateAndInflate, buffer); + } /// /// Basic inflate/deflate test /// [Test] [Category("Base")] - public void InflateDeflateNonZlib() + public void InflateDeflateNonZlib([Range(0, 9)] int level) { - for (int level = 0; level < 10; ++level) { - RandomDeflateInflate(100000, level, false); - } + RandomDeflateInflate(100000, level, false); + } + + /// + /// Basic async inflate/deflate test + /// + [Test] + [Category("Base")] + [Category("Async")] + public async Task InflateDeflateNonZlibAsync([Range(0, 9)] int level) + { + await RandomDeflateInflateAsync(100000, level, false); } + [Test] [Category("Base")] public void CloseDeflatorWithNestedUsing() { string tempFile = null; - try { + try + { tempFile = Path.GetTempPath(); - } catch (SecurityException) { + } + catch (SecurityException) + { } Assert.IsNotNull(tempFile, "No permission to execute this test?"); @@ -171,7 +260,8 @@ public void CloseDeflatorWithNestedUsing() tempFile = Path.Combine(tempFile, "SharpZipTest.Zip"); using (FileStream diskFile = File.Create(tempFile)) using (DeflaterOutputStream deflator = new DeflaterOutputStream(diskFile)) - using (StreamWriter txtFile = new StreamWriter(deflator)) { + using (StreamWriter txtFile = new StreamWriter(deflator)) + { txtFile.Write("Hello"); txtFile.Flush(); } @@ -205,7 +295,6 @@ public void DeflatorStreamOwnership() Assert.IsFalse(memStream.IsClosed, "Should not be closed after parent owner close"); Assert.IsFalse(memStream.IsDisposed, "Should not be disposed after parent owner close"); - } [Test] @@ -234,7 +323,6 @@ public void InflatorStreamOwnership() Assert.IsFalse(memStream.IsClosed, "Should not be closed after parent owner close"); Assert.IsFalse(memStream.IsDisposed, "Should not be disposed after parent owner close"); - } [Test] @@ -242,9 +330,12 @@ public void InflatorStreamOwnership() public void CloseInflatorWithNestedUsing() { string tempFile = null; - try { + try + { tempFile = Path.GetTempPath(); - } catch (SecurityException) { + } + catch (SecurityException) + { } Assert.IsNotNull(tempFile, "No permission to execute this test?"); @@ -252,14 +343,16 @@ public void CloseInflatorWithNestedUsing() tempFile = Path.Combine(tempFile, "SharpZipTest.Zip"); using (FileStream diskFile = File.Create(tempFile)) using (DeflaterOutputStream deflator = new DeflaterOutputStream(diskFile)) - using (StreamWriter textWriter = new StreamWriter(deflator)) { + using (StreamWriter textWriter = new StreamWriter(deflator)) + { textWriter.Write("Hello"); textWriter.Flush(); } using (FileStream diskFile = File.OpenRead(tempFile)) using (InflaterInputStream deflator = new InflaterInputStream(diskFile)) - using (StreamReader textReader = new StreamReader(deflator)) { + using (StreamReader textReader = new StreamReader(deflator)) + { char[] buffer = new char[5]; int readCount = textReader.Read(buffer, 0, 5); Assert.AreEqual(5, readCount); @@ -267,7 +360,6 @@ public void CloseInflatorWithNestedUsing() var b = new StringBuilder(); b.Append(buffer); Assert.AreEqual("Hello", b.ToString()); - } File.Delete(tempFile); diff --git a/test/ICSharpCode.SharpZipLib.Tests/Checksum/ChecksumTests.cs b/test/ICSharpCode.SharpZipLib.Tests/Checksum/ChecksumTests.cs index 82e78e6ec..56c6bb836 100644 --- a/test/ICSharpCode.SharpZipLib.Tests/Checksum/ChecksumTests.cs +++ b/test/ICSharpCode.SharpZipLib.Tests/Checksum/ChecksumTests.cs @@ -1,6 +1,7 @@ -using NUnit.Framework; -using ICSharpCode.SharpZipLib.Checksum; +using ICSharpCode.SharpZipLib.Checksum; +using NUnit.Framework; using System; +using System.Diagnostics; namespace ICSharpCode.SharpZipLib.Tests.Checksum { @@ -8,10 +9,18 @@ namespace ICSharpCode.SharpZipLib.Tests.Checksum [Category("Checksum")] public class ChecksumTests { - readonly + private readonly // Represents ASCII string of "123456789" byte[] check = { 49, 50, 51, 52, 53, 54, 55, 56, 57 }; + // Represents string "123456789123456789123456789123456789" + private readonly byte[] longCheck = { + 49, 50, 51, 52, 53, 54, 55, 56, 57, + 49, 50, 51, 52, 53, 54, 55, 56, 57, + 49, 50, 51, 52, 53, 54, 55, 56, 57, + 49, 50, 51, 52, 53, 54, 55, 56, 57 + }; + [Test] public void Adler_32() { @@ -27,6 +36,33 @@ public void Adler_32() exceptionTesting(underTestAdler32); } + const long BufferSize = 256 * 1024 * 1024; + + [Test] + public void Adler_32_Performance() + { + var rand = new Random(1); + + var buffer = new byte[BufferSize]; + rand.NextBytes(buffer); + + var adler = new Adler32(); + Assert.AreEqual(0x00000001, adler.Value); + + var sw = new Stopwatch(); + sw.Start(); + + adler.Update(buffer); + + sw.Stop(); + Console.WriteLine($"Adler32 Hashing of 256 MiB: {sw.Elapsed.TotalSeconds:f4} second(s)"); + + adler.Update(check); + Assert.AreEqual(0xD4897DA3, adler.Value); + + exceptionTesting(adler); + } + [Test] public void CRC_32_BZip2() { @@ -42,6 +78,32 @@ public void CRC_32_BZip2() exceptionTesting(underTestBZip2Crc); } + [Test] + public void CRC_32_BZip2_Long() + { + var underTestCrc32 = new BZip2Crc(); + underTestCrc32.Update(longCheck); + Assert.AreEqual(0xEE53D2B2, underTestCrc32.Value); + } + + [Test] + public void CRC_32_BZip2_Unaligned() + { + // Extract "456" and CRC + var underTestCrc32 = new BZip2Crc(); + underTestCrc32.Update(new ArraySegment(check, 3, 3)); + Assert.AreEqual(0x001D0511, underTestCrc32.Value); + } + + [Test] + public void CRC_32_BZip2_Long_Unaligned() + { + // Extract "789123456789123456" and CRC + var underTestCrc32 = new BZip2Crc(); + underTestCrc32.Update(new ArraySegment(longCheck, 15, 18)); + Assert.AreEqual(0x025846E0, underTestCrc32.Value); + } + [Test] public void CRC_32() { @@ -57,62 +119,105 @@ public void CRC_32() exceptionTesting(underTestCrc32); } - private void exceptionTesting(IChecksum crcUnderTest) + [Test] + public void CRC_32_Long() + { + var underTestCrc32 = new Crc32(); + underTestCrc32.Update(longCheck); + Assert.AreEqual(0x3E29169C, underTestCrc32.Value); + } + + [Test] + public void CRC_32_Unaligned() { + // Extract "456" and CRC + var underTestCrc32 = new Crc32(); + underTestCrc32.Update(new ArraySegment(check, 3, 3)); + Assert.AreEqual(0xB1A8C371, underTestCrc32.Value); + } + [Test] + public void CRC_32_Long_Unaligned() + { + // Extract "789123456789123456" and CRC + var underTestCrc32 = new Crc32(); + underTestCrc32.Update(new ArraySegment(longCheck, 15, 18)); + Assert.AreEqual(0x31CA9A2E, underTestCrc32.Value); + } + + private void exceptionTesting(IChecksum crcUnderTest) + { bool exception = false; - try { + try + { crcUnderTest.Update(null); - } catch (ArgumentNullException) { + } + catch (ArgumentNullException) + { exception = true; } Assert.IsTrue(exception, "Passing a null buffer should cause an ArgumentNullException"); // reset exception exception = false; - try { - crcUnderTest.Update(null, 0, 0); - } catch (ArgumentNullException) { + try + { + crcUnderTest.Update(new ArraySegment(null, 0, 0)); + } + catch (ArgumentNullException) + { exception = true; } Assert.IsTrue(exception, "Passing a null buffer should cause an ArgumentNullException"); // reset exception exception = false; - try { - crcUnderTest.Update(check, -1, 9); - } catch (ArgumentOutOfRangeException) { + try + { + crcUnderTest.Update(new ArraySegment(check, -1, 9)); + } + catch (ArgumentOutOfRangeException) + { exception = true; } Assert.IsTrue(exception, "Passing a negative offset should cause an ArgumentOutOfRangeException"); // reset exception exception = false; - try { - crcUnderTest.Update(check, 9, 0); - } catch (ArgumentOutOfRangeException) { + try + { + crcUnderTest.Update(new ArraySegment(check, 10, 0)); + } + catch (ArgumentException) + { exception = true; } - Assert.IsTrue(exception, "Passing an offset greater than or equal to buffer.Length should cause an ArgumentOutOfRangeException"); + Assert.IsTrue(exception, "Passing an offset greater than buffer.Length should cause an ArgumentException"); // reset exception exception = false; - try { - crcUnderTest.Update(check, 0, -1); - } catch (ArgumentOutOfRangeException) { + try + { + crcUnderTest.Update(new ArraySegment(check, 0, -1)); + } + catch (ArgumentOutOfRangeException) + { exception = true; } Assert.IsTrue(exception, "Passing a negative count should cause an ArgumentOutOfRangeException"); // reset exception exception = false; - try { - crcUnderTest.Update(check, 0, 10); - } catch (ArgumentOutOfRangeException) { + try + { + crcUnderTest.Update(new ArraySegment(check, 0, 10)); + } + catch (ArgumentException) + { exception = true; } - Assert.IsTrue(exception, "Passing a count + offset greater than buffer.Length should cause an ArgumentOutOfRangeException"); + Assert.IsTrue(exception, "Passing a count + offset greater than buffer.Length should cause an ArgumentException"); } } } diff --git a/test/ICSharpCode.SharpZipLib.Tests/Core/ByteOrderUtilsTests.cs b/test/ICSharpCode.SharpZipLib.Tests/Core/ByteOrderUtilsTests.cs new file mode 100644 index 000000000..1a5d271ff --- /dev/null +++ b/test/ICSharpCode.SharpZipLib.Tests/Core/ByteOrderUtilsTests.cs @@ -0,0 +1,137 @@ +using System; +using System.IO; +using System.Threading; +using System.Threading.Tasks; +using NUnit.Framework; +using BO = ICSharpCode.SharpZipLib.Core.ByteOrderStreamExtensions; +using ICSharpCode.SharpZipLib.Core; + +// ReSharper disable InconsistentNaming + +namespace ICSharpCode.SharpZipLib.Tests.Core +{ + [TestFixture] + [Category("Core")] + public class ByteOrderUtilsTests + { + private const short native16 = 0x1234; + private static readonly byte[] swapped16 = { 0x34, 0x12 }; + + private const int native32 = 0x12345678; + private static readonly byte[] swapped32 = { 0x78, 0x56, 0x34, 0x12 }; + + private const long native64 = 0x123456789abcdef0; + private static readonly byte[] swapped64 = { 0xf0, 0xde, 0xbc, 0x9a, 0x78, 0x56, 0x34, 0x12 }; + + [Test] + public void ToSwappedBytes() + { + Assert.AreEqual(swapped16, BO.SwappedBytes(native16)); + Assert.AreEqual(swapped16, BO.SwappedBytes((ushort)native16)); + + Assert.AreEqual(swapped32, BO.SwappedBytes(native32)); + Assert.AreEqual(swapped32, BO.SwappedBytes((uint)native32)); + + Assert.AreEqual(swapped64, BO.SwappedBytes(native64)); + Assert.AreEqual(swapped64, BO.SwappedBytes((ulong)native64)); + } + + [Test] + public void FromSwappedBytes() + { + Assert.AreEqual(native16, BO.SwappedS16(swapped16)); + Assert.AreEqual(native16, BO.SwappedU16(swapped16)); + + Assert.AreEqual(native32, BO.SwappedS32(swapped32)); + Assert.AreEqual(native32, BO.SwappedU32(swapped32)); + + Assert.AreEqual(native64, BO.SwappedS64(swapped64)); + Assert.AreEqual(native64, BO.SwappedU64(swapped64)); + } + + [Test] + public void ReadLESigned16() + => TestReadLE(native16, 2, BO.ReadLEShort); + + [Test] + public void ReadLESigned32() + => TestReadLE(native32,4, BO.ReadLEInt); + + [Test] + public void ReadLESigned64() + => TestReadLE(native64,8, BO.ReadLELong); + + [Test] + public void WriteLESigned16() + => TestWriteLE(swapped16, s => s.WriteLEShort(native16)); + + [Test] + public void WriteLESigned32() + => TestWriteLE(swapped32, s => s.WriteLEInt(native32)); + + [Test] + public void WriteLESigned64() + => TestWriteLE(swapped64, s => s.WriteLELong(native64)); + + [Test] + public void WriteLEUnsigned16() + => TestWriteLE(swapped16, s => s.WriteLEUshort((ushort)native16)); + + [Test] + public void WriteLEUnsigned32() + => TestWriteLE(swapped32, s => s.WriteLEUint(native32)); + + [Test] + public void WriteLEUnsigned64() + => TestWriteLE(swapped64, s => s.WriteLEUlong(native64)); + + [Test] + public async Task WriteLEAsyncSigned16() + => await TestWriteLEAsync(swapped16, (int)native16, BO.WriteLEShortAsync); + + [Test] + public async Task WriteLEAsyncUnsigned16() + => await TestWriteLEAsync(swapped16, (ushort)native16, BO.WriteLEUshortAsync); + + [Test] + public async Task WriteLEAsyncSigned32() + => await TestWriteLEAsync(swapped32, native32, BO.WriteLEIntAsync); + [Test] + public async Task WriteLEAsyncUnsigned32() + => await TestWriteLEAsync(swapped32, (uint)native32, BO.WriteLEUintAsync); + + [Test] + public async Task WriteLEAsyncSigned64() + => await TestWriteLEAsync(swapped64, native64, BO.WriteLELongAsync); + [Test] + public async Task WriteLEAsyncUnsigned64() + => await TestWriteLEAsync(swapped64, (ulong)native64, BO.WriteLEUlongAsync); + + + private static void TestReadLE(T expected, int bytes, Func read) + { + using (var ms = new MemoryStream(swapped64, 8 - bytes, bytes)) + { + Assert.AreEqual(expected, read(ms)); + } + } + + private static void TestWriteLE(byte[] expected, Action write) + { + using (var ms = new MemoryStream()) + { + write(ms); + Assert.AreEqual(expected, ms.ToArray()); + } + } + + private static async Task TestWriteLEAsync(byte[] expected, T input, Func write) + { + using (var ms = new MemoryStream()) + { + await write(ms, input, CancellationToken.None); + Assert.AreEqual(expected, ms.ToArray()); + } + } + } +} diff --git a/test/ICSharpCode.SharpZipLib.Tests/Core/CoreTests.cs b/test/ICSharpCode.SharpZipLib.Tests/Core/CoreTests.cs index 4d49862c5..1b43c79ae 100644 --- a/test/ICSharpCode.SharpZipLib.Tests/Core/CoreTests.cs +++ b/test/ICSharpCode.SharpZipLib.Tests/Core/CoreTests.cs @@ -1,3 +1,4 @@ +using System; using ICSharpCode.SharpZipLib.Core; using NUnit.Framework; @@ -15,19 +16,22 @@ public void FilterQuoting() filters = NameFilter.SplitQuoted(";;;"); Assert.AreEqual(4, filters.Length); - foreach (string filter in filters) { + foreach (string filter in filters) + { Assert.AreEqual("", filter); } filters = NameFilter.SplitQuoted("a;a;a;a;a"); Assert.AreEqual(5, filters.Length); - foreach (string filter in filters) { + foreach (string filter in filters) + { Assert.AreEqual("a", filter); } filters = NameFilter.SplitQuoted(@"a\;;a\;;a\;;a\;;a\;"); Assert.AreEqual(5, filters.Length); - foreach (string filter in filters) { + foreach (string filter in filters) + { Assert.AreEqual("a;", filter); } } @@ -51,5 +55,51 @@ public void ValidFilter() Assert.IsFalse(NameFilter.IsValidFilterExpression(@"\,)")); Assert.IsFalse(NameFilter.IsValidFilterExpression(@"[]")); } + + // Use a shorter name wrapper to make tests more legible + private static string DropRoot(string s) => PathUtils.DropPathRoot(s); + + [Test] + [Category("Core")] + [Platform("Win")] + public void DropPathRoot_Windows() + { + Assert.AreEqual("file.txt", DropRoot(@"\\server\share\file.txt")); + Assert.AreEqual("file.txt", DropRoot(@"c:\file.txt")); + Assert.AreEqual(@"subdir with spaces\file.txt", DropRoot(@"z:\subdir with spaces\file.txt")); + Assert.AreEqual("", DropRoot(@"\\server\share\")); + Assert.AreEqual(@"server\share\file.txt", DropRoot(@"\server\share\file.txt")); + Assert.AreEqual(@"path\file.txt", DropRoot(@"\\server\share\\path\file.txt")); + } + + [Test] + [Category("Core")] + [Platform(Exclude="Win")] + public void DropPathRoot_Posix() + { + Assert.AreEqual("file.txt", DropRoot("/file.txt")); + Assert.AreEqual(@"tmp/file.txt", DropRoot(@"/tmp/file.txt")); + Assert.AreEqual(@"tmp\file.txt", DropRoot(@"\tmp\file.txt")); + Assert.AreEqual(@"tmp/file.txt", DropRoot(@"\tmp/file.txt")); + Assert.AreEqual(@"tmp\file.txt", DropRoot(@"/tmp\file.txt")); + Assert.AreEqual("", DropRoot("/")); + + } + + [Test] + [TestCase(@"c:\file:+/")] + [TestCase(@"c:\file*?")] + [TestCase("c:\\file|\"")] + [TestCase(@"c:\file<>")] + [TestCase(@"c:file")] + [TestCase(@"c::file")] + [TestCase(@"c:?file")] + [TestCase(@"c:+file")] + [TestCase(@"cc:file")] + [Category("Core")] + public void DropPathRoot_DoesNotThrowForInvalidPath(string path) + { + Assert.DoesNotThrow(() => Console.WriteLine(PathUtils.DropPathRoot(path))); + } } } diff --git a/test/ICSharpCode.SharpZipLib.Tests/Core/StringBuilderPoolTests.cs b/test/ICSharpCode.SharpZipLib.Tests/Core/StringBuilderPoolTests.cs new file mode 100644 index 000000000..85d8c65a9 --- /dev/null +++ b/test/ICSharpCode.SharpZipLib.Tests/Core/StringBuilderPoolTests.cs @@ -0,0 +1,77 @@ +using System.Threading; +using System.Threading.Tasks; +using ICSharpCode.SharpZipLib.Core; +using NUnit.Framework; + +namespace ICSharpCode.SharpZipLib.Tests.Core +{ + [TestFixture] + public class StringBuilderPoolTests + { + [Test] + [Category("Core")] + public void RoundTrip() + { + var pool = new StringBuilderPool(); + var builder1 = pool.Rent(); + pool.Return(builder1); + var builder2 = pool.Rent(); + Assert.AreEqual(builder1, builder2); + } + + [Test] + [Category("Core")] + public void ReturnsClears() + { + var pool = new StringBuilderPool(); + var builder1 = pool.Rent(); + builder1.Append("Hello"); + pool.Return(builder1); + Assert.AreEqual(0, builder1.Length); + } + + [Test] + [Category("Core")] + public async Task ThreadSafeAsync() + { + // use a lot of threads to increase the likelihood of errors + var concurrency = 100; + + var pool = new StringBuilderPool(); + var gate = new TaskCompletionSource(); + var startedTasks = new Task[concurrency]; + var completedTasks = new Task[concurrency]; + for (int i = 0; i < concurrency; i++) + { + var started = new TaskCompletionSource(); + startedTasks[i] = started.Task; + var captured = i; + completedTasks[i] = Task.Run(async () => + { + started.SetResult(true); + await gate.Task; + var builder = pool.Rent(); + builder.Append("Hello "); + builder.Append(captured); + var str = builder.ToString(); + pool.Return(builder); + return str; + }); + } + + // make sure all the threads have started + await Task.WhenAll(startedTasks); + + // let them all loose at the same time + gate.SetResult(true); + + // make sure every thread produces the expected string and hence had its own StringBuilder + var results = await Task.WhenAll(completedTasks); + for (int i = 0; i < concurrency; i++) + { + var result = results[i]; + Assert.AreEqual($"Hello {i}", result); + } + } + } +} diff --git a/test/ICSharpCode.SharpZipLib.Tests/GZip/GZipAsyncTests.cs b/test/ICSharpCode.SharpZipLib.Tests/GZip/GZipAsyncTests.cs new file mode 100644 index 000000000..b259af8cf --- /dev/null +++ b/test/ICSharpCode.SharpZipLib.Tests/GZip/GZipAsyncTests.cs @@ -0,0 +1,186 @@ +using System; +using System.IO; +using System.Text; +using System.Threading.Tasks; +using ICSharpCode.SharpZipLib.GZip; +using ICSharpCode.SharpZipLib.Tests.TestSupport; +using NUnit.Framework; + +namespace ICSharpCode.SharpZipLib.Tests.GZip +{ + [TestFixture] + public class GZipAsyncTests + { + [Test] + [Category("GZip")] + [Category("Async")] + public async Task SmallBufferDecompressionAsync([Values(0, 1, 3)] int seed) + { + var outputBufferSize = 100000; + var outputBuffer = new byte[outputBufferSize]; + var inputBuffer = Utils.GetDummyBytes(outputBufferSize * 4, seed); + +#if NETCOREAPP3_1_OR_GREATER + await using var msGzip = new MemoryStream(); + await using (var gzos = new GZipOutputStream(msGzip){IsStreamOwner = false}) + { + await gzos.WriteAsync(inputBuffer, 0, inputBuffer.Length); + } + + msGzip.Seek(0, SeekOrigin.Begin); + + using (var gzis = new GZipInputStream(msGzip)) + await using (var msRaw = new MemoryStream()) + { + int readOut; + while ((readOut = gzis.Read(outputBuffer, 0, outputBuffer.Length)) > 0) + { + await msRaw.WriteAsync(outputBuffer, 0, readOut); + } + + var resultBuffer = msRaw.ToArray(); + for (var i = 0; i < resultBuffer.Length; i++) + { + Assert.AreEqual(inputBuffer[i], resultBuffer[i]); + } + } +#else + using var msGzip = new MemoryStream(); + using (var gzos = new GZipOutputStream(msGzip){IsStreamOwner = false}) + { + await gzos.WriteAsync(inputBuffer, 0, inputBuffer.Length); + } + + msGzip.Seek(0, SeekOrigin.Begin); + + using (var gzis = new GZipInputStream(msGzip)) + using (var msRaw = new MemoryStream()) + { + int readOut; + while ((readOut = gzis.Read(outputBuffer, 0, outputBuffer.Length)) > 0) + { + await msRaw.WriteAsync(outputBuffer, 0, readOut); + } + + var resultBuffer = msRaw.ToArray(); + for (var i = 0; i < resultBuffer.Length; i++) + { + Assert.AreEqual(inputBuffer[i], resultBuffer[i]); + } + } +#endif + } + + /// + /// Basic compress/decompress test + /// + [Test] + [Category("GZip")] + [Category("Async")] + public async Task OriginalFilenameAsync() + { + var content = "FileContents"; + +#if NETCOREAPP3_1_OR_GREATER + await using var ms = new MemoryStream(); + await using (var outStream = new GZipOutputStream(ms) { IsStreamOwner = false }) + { + outStream.FileName = "/path/to/file.ext"; + outStream.Write(Encoding.ASCII.GetBytes(content)); + } +#else + var ms = new MemoryStream(); + var outStream = new GZipOutputStream(ms){ IsStreamOwner = false }; + outStream.FileName = "/path/to/file.ext"; + var bytes = Encoding.ASCII.GetBytes(content); + outStream.Write(bytes, 0, bytes.Length); + await outStream.FinishAsync(System.Threading.CancellationToken.None); + outStream.Dispose(); + +#endif + ms.Seek(0, SeekOrigin.Begin); + + using (var inStream = new GZipInputStream(ms)) + { + var readBuffer = new byte[content.Length]; + inStream.Read(readBuffer, 0, readBuffer.Length); + Assert.AreEqual(content, Encoding.ASCII.GetString(readBuffer)); + Assert.AreEqual("file.ext", inStream.GetFilename()); + } + } + + /// + /// Test creating an empty gzip stream using async + /// + [Test] + [Category("GZip")] + [Category("Async")] + public async Task EmptyGZipStreamAsync() + { +#if NETCOREAPP3_1_OR_GREATER + await using var ms = new MemoryStream(); + await using (var outStream = new GZipOutputStream(ms) { IsStreamOwner = false }) + { + // No content + } +#else + var ms = new MemoryStream(); + var outStream = new GZipOutputStream(ms){ IsStreamOwner = false }; + await outStream.FinishAsync(System.Threading.CancellationToken.None); + outStream.Dispose(); + +#endif + ms.Seek(0, SeekOrigin.Begin); + + using (var inStream = new GZipInputStream(ms)) + using (var reader = new StreamReader(inStream)) + { + var content = await reader.ReadToEndAsync(); + Assert.IsEmpty(content); + } + } + + [Test] + [Category("GZip")] + [Category("Async")] + public async Task WriteGZipStreamToAsyncOnlyStream() + { +#if NETSTANDARD2_1 || NETCOREAPP3_0_OR_GREATER + var content = Encoding.ASCII.GetBytes("a"); + var modTime = DateTime.UtcNow; + + await using (var msAsync = new MemoryStreamWithoutSync()) + { + await using (var outStream = new GZipOutputStream(msAsync) { IsStreamOwner = false }) + { + outStream.ModifiedTime = modTime; + await outStream.WriteAsync(content); + } + + using var msSync = new MemoryStream(); + using (var outStream = new GZipOutputStream(msSync) { IsStreamOwner = false }) + { + outStream.ModifiedTime = modTime; + outStream.Write(content); + } + + var syncBytes = string.Join(' ', msSync.ToArray()); + var asyncBytes = string.Join(' ', msAsync.ToArray()); + + Assert.AreEqual(syncBytes, asyncBytes, "Sync and Async compressed streams are not equal"); + + // Since GZipInputStream isn't async yet we need to read from it from a regular MemoryStream + using (var readStream = new MemoryStream(msAsync.ToArray())) + using (var inStream = new GZipInputStream(readStream)) + using (var reader = new StreamReader(inStream)) + { + Assert.AreEqual(content, await reader.ReadToEndAsync()); + } + } +#else + await Task.CompletedTask; + Assert.Ignore("AsyncDispose is not supported"); +#endif + } + } +} diff --git a/test/ICSharpCode.SharpZipLib.Tests/GZip/GZipTests.cs b/test/ICSharpCode.SharpZipLib.Tests/GZip/GZipTests.cs index c5c97fc6f..3241fd134 100644 --- a/test/ICSharpCode.SharpZipLib.Tests/GZip/GZipTests.cs +++ b/test/ICSharpCode.SharpZipLib.Tests/GZip/GZipTests.cs @@ -1,9 +1,11 @@ -using System; -using System.IO; using ICSharpCode.SharpZipLib.GZip; using ICSharpCode.SharpZipLib.Tests.TestSupport; using NUnit.Framework; +using System; +using System.IO; +using System.Text; using System.Threading; +using System.Threading.Tasks; namespace ICSharpCode.SharpZipLib.Tests.GZip { @@ -23,9 +25,7 @@ public void TestGZip() var ms = new MemoryStream(); var outStream = new GZipOutputStream(ms); - byte[] buf = new byte[100000]; - var rnd = new Random(); - rnd.NextBytes(buf); + var buf = Utils.GetDummyBytes(size: 100000); outStream.Write(buf, 0, buf.Length); outStream.Flush(); @@ -38,9 +38,11 @@ public void TestGZip() int currentIndex = 0; int count = buf2.Length; - while (true) { + while (true) + { int numRead = inStream.Read(buf2, currentIndex, count); - if (numRead <= 0) { + if (numRead <= 0) + { break; } currentIndex += numRead; @@ -49,7 +51,8 @@ public void TestGZip() Assert.AreEqual(0, count); - for (int i = 0; i < buf.Length; ++i) { + for (int i = 0; i < buf.Length; ++i) + { Assert.AreEqual(buf2[i], buf[i]); } } @@ -60,17 +63,47 @@ public void TestGZip() [Test] [Category("GZip")] public void DelayedHeaderWriteNoData() + { + using var ms = new MemoryStream(); + Assert.Zero(ms.Length); + + using (new GZipOutputStream(ms)) + { + Assert.Zero(ms.Length); + } + + Assert.NotZero(ms.ToArray().Length); + } + + + /// + /// Variant of DelayedHeaderWriteNoData testing flushing for https://github.com/icsharpcode/SharpZipLib/issues/382 + /// + [Test] + [Category("GZip")] + public void DelayedHeaderWriteFlushNoData() { var ms = new MemoryStream(); Assert.AreEqual(0, ms.Length); - using (GZipOutputStream outStream = new GZipOutputStream(ms)) { - Assert.AreEqual(0, ms.Length); + using (GZipOutputStream outStream = new GZipOutputStream(ms) { IsStreamOwner = false }) + { + // #382 - test flushing the stream before writing to it. + outStream.Flush(); } - byte[] data = ms.ToArray(); + ms.Seek(0, SeekOrigin.Begin); - Assert.IsTrue(data.Length > 0); + // Test that the gzip stream can be read + var readStream = new MemoryStream(); + using (GZipInputStream inStream = new GZipInputStream(ms)) + { + inStream.CopyTo(readStream); + } + + byte[] data = readStream.ToArray(); + + Assert.That(data, Is.Empty, "Should not have any decompressed data"); } /// @@ -82,7 +115,8 @@ public void DelayedHeaderWriteWithData() { var ms = new MemoryStream(); Assert.AreEqual(0, ms.Length); - using (GZipOutputStream outStream = new GZipOutputStream(ms)) { + using (GZipOutputStream outStream = new GZipOutputStream(ms)) + { Assert.AreEqual(0, ms.Length); outStream.WriteByte(45); @@ -95,6 +129,38 @@ public void DelayedHeaderWriteWithData() Assert.IsTrue(data.Length > 0); } + /// + /// variant of DelayedHeaderWriteWithData to test https://github.com/icsharpcode/SharpZipLib/issues/382 + /// + [Test] + [Category("GZip")] + public void DelayedHeaderWriteFlushWithData() + { + var ms = new MemoryStream(); + Assert.AreEqual(0, ms.Length); + using (GZipOutputStream outStream = new GZipOutputStream(ms) { IsStreamOwner = false }) + { + Assert.AreEqual(0, ms.Length); + + // #382 - test flushing the stream before writing to it. + outStream.Flush(); + outStream.WriteByte(45); + } + + ms.Seek(0, SeekOrigin.Begin); + + // Test that the gzip stream can be read + var readStream = new MemoryStream(); + using (GZipInputStream inStream = new GZipInputStream(ms)) + { + inStream.CopyTo(readStream); + } + + // Check that the data was read + byte[] data = readStream.ToArray(); + CollectionAssert.AreEqual(new byte[] { 45 }, data, "Decompressed data should match initial data"); + } + [Test] [Category("GZip")] public void ZeroLengthInputStream() @@ -169,7 +235,6 @@ public void InputStreamOwnership() Assert.IsFalse(memStream.IsClosed, "Should not be closed after parent owner close"); Assert.IsFalse(memStream.IsDisposed, "Should not be disposed after parent owner close"); - } [Test] @@ -193,7 +258,8 @@ public void DoubleClose() s.Close(); memStream = new TrackedMemoryStream(); - using (GZipOutputStream no2 = new GZipOutputStream(memStream)) { + using (new GZipOutputStream(memStream)) + { s.Close(); } } @@ -205,11 +271,7 @@ public void WriteAfterFinish() var s = new GZipOutputStream(memStream); s.Finish(); - try { - s.WriteByte(7); - Assert.Fail("Write should fail"); - } catch { - } + Assert.Throws(() => s.WriteByte(value: 7), "Write should fail"); } [Test] @@ -219,151 +281,227 @@ public void WriteAfterClose() var s = new GZipOutputStream(memStream); s.Close(); - try { - s.WriteByte(7); - Assert.Fail("Write should fail"); - } catch { - } + Assert.Throws(() => s.WriteByte(value: 7), "Write should fail"); } - /// - /// Verify that if a decompression was successful for at least one block we're exiting gracefully. - /// - [Test] - public void TrailingGarbage() - { - /* ARRANGE */ + /// + /// Verify that if a decompression was successful for at least one block we're exiting gracefully. + /// + [Test] + public void TrailingGarbage() + { + /* ARRANGE */ var ms = new MemoryStream(); var outStream = new GZipOutputStream(ms); - // input buffer to be compressed - byte[] buf = new byte[100000]; - var rnd = new Random(); - rnd.NextBytes(buf); + // input buffer to be compressed + var buf = Utils.GetDummyBytes(size: 100000, seed: 3); - // compress input buffer + // compress input buffer outStream.Write(buf, 0, buf.Length); outStream.Flush(); outStream.Finish(); - // generate random trailing garbage and add to the compressed stream - byte[] garbage = new byte[4096]; - rnd.NextBytes(garbage); - ms.Write(garbage, 0, garbage.Length); + // generate random trailing garbage and add to the compressed stream + Utils.WriteDummyData(ms, size: 4096, seed: 4); - // rewind the concatenated stream + // rewind the concatenated stream ms.Seek(0, SeekOrigin.Begin); - - /* ACT */ - // decompress concatenated stream + /* ACT */ + // decompress concatenated stream var inStream = new GZipInputStream(ms); byte[] buf2 = new byte[buf.Length]; int currentIndex = 0; int count = buf2.Length; - while (true) { - int numRead = inStream.Read(buf2, currentIndex, count); - if (numRead <= 0) { - break; - } - currentIndex += numRead; - count -= numRead; - } - + while (true) + { + var numRead = inStream.Read(buf2, currentIndex, count); + if (numRead <= 0) + { + break; + } + currentIndex += numRead; + count -= numRead; + } - /* ASSERT */ - Assert.AreEqual(0, count); - for (int i = 0; i < buf.Length; ++i) { + /* ASSERT */ + Assert.Zero(count); + for (int i = 0; i < buf.Length; ++i) + { Assert.AreEqual(buf2[i], buf[i]); } - } + } + /// + /// Test that if we flush a GZip output stream then all data that has been written + /// is flushed through to the underlying stream and can be successfully read back + /// even if the stream is not yet finished. + /// [Test] [Category("GZip")] - [Category("Long Running")] - [Ignore("TODO : Fix this")] - public void BigStream() + public void FlushToUnderlyingStream() { - window_ = new WindowedStream(0x3ffff); - outStream_ = new GZipOutputStream(window_); - inStream_ = new GZipInputStream(window_); + var ms = new MemoryStream(); + var outStream = new GZipOutputStream(ms); - long target = 0x10000000; - readTarget_ = writeTarget_ = target; + byte[] buf = Utils.GetDummyBytes(size: 100000); - Thread reader = new Thread(Reader); - reader.Name = "Reader"; - reader.Start(); + outStream.Write(buf, 0, buf.Length); + // Flush output stream but don't finish it yet + outStream.Flush(); - Thread writer = new Thread(Writer); - writer.Name = "Writer"; + ms.Seek(0, SeekOrigin.Begin); - DateTime startTime = DateTime.Now; - writer.Start(); + var inStream = new GZipInputStream(ms); + byte[] buf2 = new byte[buf.Length]; + int currentIndex = 0; + int count = buf2.Length; - writer.Join(); - reader.Join(); + while (true) + { + try + { + int numRead = inStream.Read(buf2, currentIndex, count); + if (numRead <= 0) + { + break; + } + currentIndex += numRead; + count -= numRead; + } + catch (GZipException) + { + // We should get an unexpected EOF exception once we've read all + // data as the stream isn't yet finished. + break; + } + } - DateTime endTime = DateTime.Now; + Assert.AreEqual(0, count); - TimeSpan span = endTime - startTime; - Console.WriteLine("Time {0} processes {1} KB/Sec", span, (target / 1024) / span.TotalSeconds); + for (int i = 0; i < buf.Length; ++i) + { + Assert.AreEqual(buf2[i], buf[i]); + } } - void Reader() + [Test] + [Category("GZip")] + public void SmallBufferDecompression([Values(0, 1, 3)] int seed) { - const int Size = 8192; - int readBytes = 1; - byte[] buffer = new byte[Size]; + var outputBufferSize = 100000; + var outputBuffer = new byte[outputBufferSize]; + var inputBuffer = Utils.GetDummyBytes(outputBufferSize * 4, seed); - long passifierLevel = readTarget_ - 0x10000000; + using var msGzip = new MemoryStream(); + using (var gzos = new GZipOutputStream(msGzip){IsStreamOwner = false}) + { + gzos.Write(inputBuffer, 0, inputBuffer.Length); + } - while ((readTarget_ > 0) && (readBytes > 0)) { - int count = Size; - if (count > readTarget_) { - count = (int)readTarget_; + msGzip.Seek(0, SeekOrigin.Begin); + + using (var gzis = new GZipInputStream(msGzip)) + using (var msRaw = new MemoryStream()) + { + int readOut; + while ((readOut = gzis.Read(outputBuffer, 0, outputBuffer.Length)) > 0) + { + msRaw.Write(outputBuffer, 0, readOut); } - readBytes = inStream_.Read(buffer, 0, count); - readTarget_ -= readBytes; - - if (readTarget_ <= passifierLevel) { - Console.WriteLine("Reader {0} bytes remaining", readTarget_); - passifierLevel = readTarget_ - 0x10000000; + var resultBuffer = msRaw.ToArray(); + for (var i = 0; i < resultBuffer.Length; i++) + { + Assert.AreEqual(inputBuffer[i], resultBuffer[i]); } } + } + + /// + /// Should gracefully handle reading from a stream that becomes unreadable after + /// all of the data has been read. + /// + /// + /// Test for https://github.com/icsharpcode/SharpZipLib/issues/379 + /// + [Test] + [Category("Zip")] + public void ShouldGracefullyHandleReadingANonReadableStream() + { + MemoryStream ms = new SelfClosingStream(); + using (var gzos = new GZipOutputStream(ms)) + { + gzos.IsStreamOwner = false; + Utils.WriteDummyData(gzos, size: 100000); + } + + ms.Seek(0, SeekOrigin.Begin); + + using (var gzis = new GZipInputStream(ms)) + using (var msRaw = new MemoryStream()) + { + gzis.CopyTo(msRaw); + } + } - Assert.IsTrue(window_.IsClosed, "Window should be closed"); + [Test] + [Category("GZip")] + [Category("Performance")] + [Category("Long Running")] + [Explicit("Long Running")] + public void WriteThroughput() + { + PerformanceTesting.TestWrite( + size: TestDataSize.Large, + output: w => new GZipOutputStream(w) + ); + } - // This shouldnt read any data but should read the footer - readBytes = inStream_.Read(buffer, 0, 1); - Assert.AreEqual(0, readBytes, "Stream should be empty"); - Assert.AreEqual(0, window_.Length, "Window should be closed"); - inStream_.Close(); + [Test] + [Category("GZip")] + [Category("Performance")] + [Explicit("Long Running")] + public void ReadWriteThroughput() + { + PerformanceTesting.TestReadWrite( + size: TestDataSize.Large, + input: w => new GZipInputStream(w), + output: w => new GZipOutputStream(w) + ); } - void Writer() + /// + /// Basic compress/decompress test + /// + [Test] + [Category("GZip")] + public void OriginalFilename() { - const int Size = 8192; + var content = "FileContents"; - byte[] buffer = new byte[Size]; - while (writeTarget_ > 0) { - int thisTime = Size; - if (thisTime > writeTarget_) { - thisTime = (int)writeTarget_; - } + using var ms = new MemoryStream(); + using (var outStream = new GZipOutputStream(ms) { IsStreamOwner = false }) + { + outStream.FileName = "/path/to/file.ext"; - outStream_.Write(buffer, 0, thisTime); - writeTarget_ -= thisTime; + var writeBuffer = Encoding.ASCII.GetBytes(content); + outStream.Write(writeBuffer, 0, writeBuffer.Length); + outStream.Flush(); + outStream.Finish(); } - outStream_.Close(); - } - WindowedStream window_; - GZipOutputStream outStream_; - GZipInputStream inStream_; - long readTarget_; - long writeTarget_; + ms.Seek(0, SeekOrigin.Begin); + + using (var inStream = new GZipInputStream(ms)) + { + var readBuffer = new byte[content.Length]; + inStream.Read(readBuffer, 0, readBuffer.Length); + Assert.AreEqual(content, Encoding.ASCII.GetString(readBuffer)); + Assert.AreEqual("file.ext", inStream.GetFilename()); + } + } } } diff --git a/test/ICSharpCode.SharpZipLib.Tests/ICSharpCode.SharpZipLib.Tests.csproj b/test/ICSharpCode.SharpZipLib.Tests/ICSharpCode.SharpZipLib.Tests.csproj index 6a25b0ca5..8e9745e96 100644 --- a/test/ICSharpCode.SharpZipLib.Tests/ICSharpCode.SharpZipLib.Tests.csproj +++ b/test/ICSharpCode.SharpZipLib.Tests/ICSharpCode.SharpZipLib.Tests.csproj @@ -2,22 +2,22 @@ Library - netcoreapp2.0 + net6.0;net462 + true + ..\..\assets\ICSharpCode.SharpZipLib.snk + true + 8.0 - - - - - - - - - - + + + + + + @@ -28,4 +28,10 @@ + + + ICSharpCode.SharpZipLib.snk + + + diff --git a/test/ICSharpCode.SharpZipLib.Tests/ICSharpCode.SharpZipLib.snk b/test/ICSharpCode.SharpZipLib.Tests/ICSharpCode.SharpZipLib.snk deleted file mode 100644 index 58cf194df..000000000 Binary files a/test/ICSharpCode.SharpZipLib.Tests/ICSharpCode.SharpZipLib.snk and /dev/null differ diff --git a/test/ICSharpCode.SharpZipLib.Tests/Lzw/LzwTests.cs b/test/ICSharpCode.SharpZipLib.Tests/Lzw/LzwTests.cs index f72521d45..eef5e4898 100644 --- a/test/ICSharpCode.SharpZipLib.Tests/Lzw/LzwTests.cs +++ b/test/ICSharpCode.SharpZipLib.Tests/Lzw/LzwTests.cs @@ -1,11 +1,10 @@ -using System.IO; -using ICSharpCode.SharpZipLib.Lzw; +using ICSharpCode.SharpZipLib.Lzw; using ICSharpCode.SharpZipLib.Tests.TestSupport; using NUnit.Framework; +using System.IO; namespace ICSharpCode.SharpZipLib.Tests.Lzw { - [TestFixture] public class LzwTestSuite { @@ -44,16 +43,18 @@ public void ZeroLengthInputStream() { var lis = new LzwInputStream(new MemoryStream()); bool exception = false; - try { + try + { lis.ReadByte(); - } catch { + } + catch + { exception = true; } Assert.IsTrue(exception, "reading from an empty stream should cause an exception"); } - [Test] [Category("LZW")] public void InputStreamOwnership() @@ -80,7 +81,6 @@ public void InputStreamOwnership() Assert.IsFalse(memStream.IsClosed, "Should not be closed after parent owner close"); Assert.IsFalse(memStream.IsDisposed, "Should not be disposed after parent owner close"); - } } } diff --git a/test/ICSharpCode.SharpZipLib.Tests/NuGet.config b/test/ICSharpCode.SharpZipLib.Tests/NuGet.config deleted file mode 100644 index 7be9c71ec..000000000 --- a/test/ICSharpCode.SharpZipLib.Tests/NuGet.config +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - diff --git a/test/ICSharpCode.SharpZipLib.Tests/Serialization/SerializationTests.cs b/test/ICSharpCode.SharpZipLib.Tests/Serialization/SerializationTests.cs new file mode 100644 index 000000000..cb3344ae4 --- /dev/null +++ b/test/ICSharpCode.SharpZipLib.Tests/Serialization/SerializationTests.cs @@ -0,0 +1,79 @@ +using System; +using System.IO; +using System.Runtime.Serialization; + +using NUnit.Framework; +using ICSharpCode.SharpZipLib.BZip2; +using ICSharpCode.SharpZipLib.Core; +using ICSharpCode.SharpZipLib.GZip; +using ICSharpCode.SharpZipLib.Lzw; +using ICSharpCode.SharpZipLib.Tar; +using ICSharpCode.SharpZipLib.Zip; + +namespace ICSharpCode.SharpZipLib.Tests.Serialization +{ + [TestFixture] + public class SerializationTests + { + /// + /// Test that SharpZipLib Custom Exceptions can be serialized. + /// + [Test] + [Category("Core")] + [Category("Serialization")] + [TestCase(typeof(BZip2Exception))] + [TestCase(typeof(GZipException))] + [TestCase(typeof(InvalidHeaderException))] + [TestCase(typeof(InvalidNameException))] + [TestCase(typeof(LzwException))] + [TestCase(typeof(SharpZipBaseException))] + [TestCase(typeof(StreamDecodingException))] + [TestCase(typeof(StreamUnsupportedException))] + [TestCase(typeof(TarException))] + [TestCase(typeof(UnexpectedEndOfStreamException))] + [TestCase(typeof(ZipException))] + public void SerializeException(Type exceptionType) + { + string message = $"Serialized {exceptionType.Name}"; + var exception = Activator.CreateInstance(exceptionType, message); + + var deserializedException = ExceptionSerialiseHelper(exception, exceptionType) as Exception; + Assert.That(deserializedException, Is.InstanceOf(exceptionType), "deserialized object should have the correct type"); + Assert.That(deserializedException.Message, Is.EqualTo(message), "deserialized message should match original message"); + } + + /// + /// Test that ValueOutOfRangeException can be serialized. + /// + [Test] + [Category("Core")] + [Category("Serialization")] + public void SerializeValueOutOfRangeException() + { + string message = "Serialized ValueOutOfRangeException"; + var exception = new ValueOutOfRangeException(message); + + var deserializedException = ExceptionSerialiseHelper(exception, typeof(ValueOutOfRangeException)) as ValueOutOfRangeException; + + // ValueOutOfRangeException appends 'out of range' to the end of the message + Assert.That(deserializedException.Message, Is.EqualTo($"{message} out of range"), "should have expected message"); + } + + // Shared serialization helper + // round trips the specified exception using DataContractSerializer + private static object ExceptionSerialiseHelper(object exception, Type exceptionType) + { + DataContractSerializer ser = new DataContractSerializer(exceptionType); + + using (var memoryStream = new MemoryStream()) + { + ser.WriteObject(memoryStream, exception); + + memoryStream.Seek(0, loc: SeekOrigin.Begin); + + return ser.ReadObject(memoryStream); + } + } + } +} + diff --git a/test/ICSharpCode.SharpZipLib.Tests/Tar/TarArchiveTests.cs b/test/ICSharpCode.SharpZipLib.Tests/Tar/TarArchiveTests.cs new file mode 100644 index 000000000..d9e32194a --- /dev/null +++ b/test/ICSharpCode.SharpZipLib.Tests/Tar/TarArchiveTests.cs @@ -0,0 +1,91 @@ +using System.IO; +using System.Text; +using ICSharpCode.SharpZipLib.Core; +using ICSharpCode.SharpZipLib.Tar; +using ICSharpCode.SharpZipLib.Tests.TestSupport; +using static ICSharpCode.SharpZipLib.Tests.TestSupport.Utils; +using NUnit.Framework; + +namespace ICSharpCode.SharpZipLib.Tests.Tar +{ + [TestFixture] + public class TarArchiveTests + { + [Test] + [Category("Tar")] + [Category("CreatesTempFile")] + [TestCase("output")] + [TestCase("output/")] + [TestCase(@"output\", IncludePlatform = "Win")] + public void ExtractingContentsWithNonTraversalPathSucceeds(string outputDir) + { + Assert.DoesNotThrow(() => ExtractTarOK(outputDir, "file", allowTraverse: false)); + } + + [Test] + [Category("Tar")] + [Category("CreatesTempFile")] + public void ExtractingContentsWithExplicitlyAllowedTraversalPathSucceeds() + { + Assert.DoesNotThrow(() => ExtractTarOK("output", "../file", allowTraverse: true)); + } + + [Test] + [Category("Tar")] + [Category("CreatesTempFile")] + [TestCase("output", "../file")] + [TestCase("output/", "../file")] + [TestCase("output", "../output.txt")] + public void ExtractingContentsWithDisallowedPathsFails(string outputDir, string fileName) + { + Assert.Throws(() => ExtractTarOK(outputDir, fileName, allowTraverse: false)); + } + + [Test] + [Category("Tar")] + [Category("CreatesTempFile")] + [Platform(Include = "Win", Reason = "Backslashes are only treated as path separators on windows")] + [TestCase(@"output\", @"..\file")] + [TestCase(@"output/", @"..\file")] + [TestCase("output", @"..\output.txt")] + [TestCase(@"output\", @"..\output.txt")] + public void ExtractingContentsOnWindowsWithDisallowedPathsFails(string outputDir, string fileName) + { + Assert.Throws(() => ExtractTarOK(outputDir, fileName, allowTraverse: false)); + } + + public void ExtractTarOK(string outputDir, string fileName, bool allowTraverse) + { + var fileContent = Encoding.UTF8.GetBytes("file content"); + using var tempDir = GetTempDir(); + + var tempPath = tempDir.FullName; + var extractPath = Path.Combine(tempPath, outputDir); + var expectedOutputFile = Path.Combine(extractPath, fileName); + + using var archiveStream = new MemoryStream(); + + Directory.CreateDirectory(extractPath); + + using (var tos = new TarOutputStream(archiveStream, Encoding.UTF8){IsStreamOwner = false}) + { + var entry = TarEntry.CreateTarEntry(fileName); + entry.Size = fileContent.Length; + tos.PutNextEntry(entry); + tos.Write(fileContent, 0, fileContent.Length); + tos.CloseEntry(); + } + + archiveStream.Position = 0; + + using (var ta = TarArchive.CreateInputTarArchive(archiveStream, Encoding.UTF8)) + { + ta.ProgressMessageEvent += (archive, entry, message) + => TestContext.WriteLine($"{entry.Name} {entry.Size} {message}"); + ta.ExtractContents(extractPath, allowTraverse); + } + + Assert.That(File.Exists(expectedOutputFile)); + } + } +} diff --git a/test/ICSharpCode.SharpZipLib.Tests/Tar/TarBufferTests.cs b/test/ICSharpCode.SharpZipLib.Tests/Tar/TarBufferTests.cs new file mode 100644 index 000000000..6f7ffedca --- /dev/null +++ b/test/ICSharpCode.SharpZipLib.Tests/Tar/TarBufferTests.cs @@ -0,0 +1,116 @@ +using System; +using System.IO; +using System.Threading; +using System.Threading.Tasks; +using ICSharpCode.SharpZipLib.Tar; +using ICSharpCode.SharpZipLib.Tests.TestSupport; +using NUnit.Framework; + +namespace ICSharpCode.SharpZipLib.Tests.Tar +{ + [TestFixture] + public class TarBufferTests + { + [Test] + public void TestSimpleReadWrite() + { + var ms = new MemoryStream(); + var reader = TarBuffer.CreateInputTarBuffer(ms, 1); + var writer = TarBuffer.CreateOutputTarBuffer(ms, 1); + writer.IsStreamOwner = false; + + var block = Utils.GetDummyBytes(TarBuffer.BlockSize); + + writer.WriteBlock(block); + writer.WriteBlock(block); + writer.WriteBlock(block); + writer.Close(); + + ms.Seek(0, SeekOrigin.Begin); + + var block0 = reader.ReadBlock(); + var block1 = reader.ReadBlock(); + var block2 = reader.ReadBlock(); + Assert.AreEqual(block, block0); + Assert.AreEqual(block, block1); + Assert.AreEqual(block, block2); + writer.Close(); + } + + [Test] + public void TestSkipBlock() + { + var ms = new MemoryStream(); + var reader = TarBuffer.CreateInputTarBuffer(ms, 1); + var writer = TarBuffer.CreateOutputTarBuffer(ms, 1); + writer.IsStreamOwner = false; + + var block0 = Utils.GetDummyBytes(TarBuffer.BlockSize); + var block1 = Utils.GetDummyBytes(TarBuffer.BlockSize); + + writer.WriteBlock(block0); + writer.WriteBlock(block1); + writer.Close(); + + ms.Seek(0, SeekOrigin.Begin); + + reader.SkipBlock(); + var block = reader.ReadBlock(); + Assert.AreEqual(block, block1); + writer.Close(); + } + + [Test] + public async Task TestSimpleReadWriteAsync() + { + var ms = new MemoryStream(); + var reader = TarBuffer.CreateInputTarBuffer(ms, 1); + var writer = TarBuffer.CreateOutputTarBuffer(ms, 1); + writer.IsStreamOwner = false; + + var block = Utils.GetDummyBytes(TarBuffer.BlockSize); + + await writer.WriteBlockAsync(block, CancellationToken.None); + await writer.WriteBlockAsync(block, CancellationToken.None); + await writer.WriteBlockAsync(block, CancellationToken.None); + await writer.CloseAsync(CancellationToken.None); + + ms.Seek(0, SeekOrigin.Begin); + + var block0 = new byte[TarBuffer.BlockSize]; + await reader.ReadBlockIntAsync(block0, CancellationToken.None, true); + var block1 = new byte[TarBuffer.BlockSize]; + await reader.ReadBlockIntAsync(block1, CancellationToken.None, true); + var block2 = new byte[TarBuffer.BlockSize]; + await reader.ReadBlockIntAsync(block2, CancellationToken.None, true); + Assert.AreEqual(block, block0); + Assert.AreEqual(block, block1); + Assert.AreEqual(block, block2); + await writer.CloseAsync(CancellationToken.None); + } + + [Test] + public async Task TestSkipBlockAsync() + { + var ms = new MemoryStream(); + var reader = TarBuffer.CreateInputTarBuffer(ms, 1); + var writer = TarBuffer.CreateOutputTarBuffer(ms, 1); + writer.IsStreamOwner = false; + + var block0 = Utils.GetDummyBytes(TarBuffer.BlockSize); + var block1 = Utils.GetDummyBytes(TarBuffer.BlockSize); + + await writer.WriteBlockAsync(block0, CancellationToken.None); + await writer.WriteBlockAsync(block1, CancellationToken.None); + await writer.CloseAsync(CancellationToken.None); + + ms.Seek(0, SeekOrigin.Begin); + + await reader.SkipBlockAsync(CancellationToken.None); + var block = new byte[TarBuffer.BlockSize]; + await reader.ReadBlockIntAsync(block, CancellationToken.None, true); + Assert.AreEqual(block, block1); + await writer.CloseAsync(CancellationToken.None); + } + } +} diff --git a/test/ICSharpCode.SharpZipLib.Tests/Tar/TarInputStreamTests.cs b/test/ICSharpCode.SharpZipLib.Tests/Tar/TarInputStreamTests.cs new file mode 100644 index 000000000..a69cfdf6d --- /dev/null +++ b/test/ICSharpCode.SharpZipLib.Tests/Tar/TarInputStreamTests.cs @@ -0,0 +1,111 @@ +using System; +using System.Buffers; +using System.IO; +using System.Text; +using System.Threading; +using System.Threading.Tasks; +using ICSharpCode.SharpZipLib.Tar; +using ICSharpCode.SharpZipLib.Tests.TestSupport; +using NUnit.Framework; + +namespace ICSharpCode.SharpZipLib.Tests.Tar +{ + public class TarInputStreamTests + { + [Test] + public void TestRead() + { + var entryBytes = Utils.GetDummyBytes(2000); + using var ms = new MemoryStream(); + using (var tos = new TarOutputStream(ms, Encoding.UTF8) { IsStreamOwner = false }) + { + var e = TarEntry.CreateTarEntry("some entry"); + e.Size = entryBytes.Length; + tos.PutNextEntry(e); + tos.Write(entryBytes, 0, entryBytes.Length); + tos.CloseEntry(); + } + + ms.Seek(0, SeekOrigin.Begin); + + using var tis = new TarInputStream(ms, Encoding.UTF8); + var entry = tis.GetNextEntry(); + Assert.AreEqual("some entry", entry.Name); + var buffer = new byte[1000]; // smaller than 2 blocks + var read0 = tis.Read(buffer, 0, buffer.Length); + Assert.AreEqual(1000, read0); + Assert.AreEqual(entryBytes.AsSpan(0, 1000).ToArray(), buffer); + + var read1 = tis.Read(buffer, 0, 5); + Assert.AreEqual(5, read1); + Assert.AreEqual(entryBytes.AsSpan(1000, 5).ToArray(), buffer.AsSpan().Slice(0, 5).ToArray()); + + var read2 = tis.Read(buffer, 0, 20); + Assert.AreEqual(20, read2); + Assert.AreEqual(entryBytes.AsSpan(1005, 20).ToArray(), buffer.AsSpan().Slice(0, 20).ToArray()); + + var read3 = tis.Read(buffer, 0, 975); + Assert.AreEqual(975, read3); + Assert.AreEqual(entryBytes.AsSpan(1025, 975).ToArray(), buffer.AsSpan().Slice(0, 975).ToArray()); + } + + [Test] + public async Task TestReadAsync() + { + var entryBytes = Utils.GetDummyBytes(2000); + using var ms = new MemoryStream(); + using (var tos = new TarOutputStream(ms, Encoding.UTF8) { IsStreamOwner = false }) + { + var e = TarEntry.CreateTarEntry("some entry"); + e.Size = entryBytes.Length; + await tos.PutNextEntryAsync(e, CancellationToken.None); + await tos.WriteAsync(entryBytes, 0, entryBytes.Length); + await tos.CloseEntryAsync(CancellationToken.None); + } + + ms.Seek(0, SeekOrigin.Begin); + + using var tis = new TarInputStream(ms, Encoding.UTF8); + var entry = await tis.GetNextEntryAsync(CancellationToken.None); + Assert.AreEqual("some entry", entry.Name); + var buffer = new byte[1000]; // smaller than 2 blocks + var read0 = await tis.ReadAsync(buffer, 0, buffer.Length); + Assert.AreEqual(1000, read0); + Assert.AreEqual(entryBytes.AsSpan(0, 1000).ToArray(), buffer); + + var read1 = await tis.ReadAsync(buffer, 0, 5); + Assert.AreEqual(5, read1); + Assert.AreEqual(entryBytes.AsSpan(1000, 5).ToArray(), buffer.AsSpan().Slice(0, 5).ToArray()); + + var read2 = await tis.ReadAsync(buffer, 0, 20); + Assert.AreEqual(20, read2); + Assert.AreEqual(entryBytes.AsSpan(1005, 20).ToArray(), buffer.AsSpan().Slice(0, 20).ToArray()); + + var read3 = await tis.ReadAsync(buffer, 0, 975); + Assert.AreEqual(975, read3); + Assert.AreEqual(entryBytes.AsSpan(1025, 975).ToArray(), buffer.AsSpan().Slice(0, 975).ToArray()); + } + + [Test] + public void ReadEmptyStreamWhenArrayPoolIsDirty() + { + // Rent an array with the same size as the tar buffer from the array pool + var buffer = ArrayPool.Shared.Rent(TarBuffer.DefaultRecordSize); + + // Fill the array with anything but 0 + Utils.FillArray(buffer, 0x8b); + + // Return the now dirty buffer to the array pool + ArrayPool.Shared.Return(buffer); + + Assert.DoesNotThrow(() => + { + using var emptyStream = new MemoryStream(Array.Empty()); + using var tarInputStream = new TarInputStream(emptyStream, Encoding.UTF8); + while (tarInputStream.GetNextEntry() is { } tarEntry) + { + } + }, "reading from an empty input stream should not cause an error"); + } + } +} diff --git a/test/ICSharpCode.SharpZipLib.Tests/Tar/TarTests.cs b/test/ICSharpCode.SharpZipLib.Tests/Tar/TarTests.cs index f5082a601..e49035afa 100644 --- a/test/ICSharpCode.SharpZipLib.Tests/Tar/TarTests.cs +++ b/test/ICSharpCode.SharpZipLib.Tests/Tar/TarTests.cs @@ -1,25 +1,35 @@ -using System; -using System.IO; using ICSharpCode.SharpZipLib.Tar; +using ICSharpCode.SharpZipLib.GZip; using ICSharpCode.SharpZipLib.Tests.TestSupport; using NUnit.Framework; +using System; +using System.IO; +using System.Text; +using System.Threading; +using System.Threading.Tasks; +using NUnit.Framework.Internal; namespace ICSharpCode.SharpZipLib.Tests.Tar { - /// /// This class contains test cases for Tar archive handling. /// [TestFixture] public class TarTestSuite { - int entryCount; + private int entryCount; - void EntryCounter(TarArchive archive, TarEntry entry, string message) + private void EntryCounter(TarArchive archive, TarEntry entry, string message) { entryCount++; } + [SetUp] + public void Setup() + { + Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); + } + /// /// Test that an empty archive can be created and when read has 0 entries in it /// @@ -28,23 +38,25 @@ void EntryCounter(TarArchive archive, TarEntry entry, string message) public void EmptyTar() { var ms = new MemoryStream(); - int recordSize = 0; - using (TarArchive tarOut = TarArchive.CreateOutputTarArchive(ms)) { + int recordSize; + using (var tarOut = TarArchive.CreateOutputTarArchive(ms)) + { recordSize = tarOut.RecordSize; } Assert.IsTrue(ms.GetBuffer().Length > 0, "Archive size must be > zero"); - Assert.AreEqual(ms.GetBuffer().Length % recordSize, 0, "Archive size must be a multiple of record size"); + Assert.Zero(ms.GetBuffer().Length % recordSize, "Archive size must be a multiple of record size"); var ms2 = new MemoryStream(); ms2.Write(ms.GetBuffer(), 0, ms.GetBuffer().Length); ms2.Seek(0, SeekOrigin.Begin); - using (TarArchive tarIn = TarArchive.CreateInputTarArchive(ms2)) { + using (var tarIn = TarArchive.CreateInputTarArchive(ms2, nameEncoding: null)) + { entryCount = 0; tarIn.ProgressMessageEvent += EntryCounter; tarIn.ListContents(); - Assert.AreEqual(0, entryCount, "Expected 0 tar entries"); + Assert.Zero(entryCount, "Expected 0 tar entries"); } } @@ -55,25 +67,25 @@ public void EmptyTar() [Category("Tar")] public void BlockFactorHandling() { - const int MinimumBlockFactor = 1; - const int MaximumBlockFactor = 64; - const int FillFactor = 2; + const int minimumBlockFactor = 1; + const int maximumBlockFactor = 64; + const int fillFactor = 2; - for (int factor = MinimumBlockFactor; factor < MaximumBlockFactor; ++factor) { + for (var factor = minimumBlockFactor; factor < maximumBlockFactor; ++factor) + { var ms = new MemoryStream(); - using (TarOutputStream tarOut = new TarOutputStream(ms, factor)) { - TarEntry entry = TarEntry.CreateTarEntry("TestEntry"); - entry.Size = (TarBuffer.BlockSize * factor * FillFactor); + using (var tarOut = new TarOutputStream(ms, factor, nameEncoding: null)) + { + var entry = TarEntry.CreateTarEntry("TestEntry"); + entry.Size = TarBuffer.BlockSize * factor * fillFactor; tarOut.PutNextEntry(entry); - byte[] buffer = new byte[TarBuffer.BlockSize]; - - var r = new Random(); - r.NextBytes(buffer); + var buffer = Utils.GetDummyBytes(TarBuffer.BlockSize); // Last block is a partial one - for (int i = 0; i < factor * FillFactor; ++i) { + for (var i = 0; i < factor * fillFactor; ++i) + { tarOut.Write(buffer, 0, buffer.Length); } } @@ -82,7 +94,7 @@ public void BlockFactorHandling() Assert.IsNotNull(tarData, "Data written is null"); // Blocks = Header + Data Blocks + Zero block + Record trailer - int usedBlocks = 1 + (factor * FillFactor) + 2; + int usedBlocks = 1 + (factor * fillFactor) + 2; int totalBlocks = usedBlocks + (factor - 1); totalBlocks /= factor; totalBlocks *= factor; @@ -90,22 +102,22 @@ public void BlockFactorHandling() Assert.AreEqual(TarBuffer.BlockSize * totalBlocks, tarData.Length, "Tar file should contain {0} blocks in length", totalBlocks); - if (usedBlocks < totalBlocks) { - // Start at first byte after header. - int byteIndex = TarBuffer.BlockSize * ((factor * FillFactor) + 1); - while (byteIndex < tarData.Length) { - int blockNumber = byteIndex / TarBuffer.BlockSize; - int offset = blockNumber % TarBuffer.BlockSize; - Assert.AreEqual(0, tarData[byteIndex], - string.Format("Trailing block data should be null iteration {0} block {1} offset {2} index {3}", - factor, - blockNumber, offset, byteIndex)); - byteIndex += 1; - } + if (usedBlocks >= totalBlocks) continue; + + // Start at first byte after header. + var byteIndex = TarBuffer.BlockSize * ((factor * fillFactor) + 1); + while (byteIndex < tarData.Length) + { + var blockNumber = byteIndex / TarBuffer.BlockSize; + var offset = blockNumber % TarBuffer.BlockSize; + Assert.AreEqual(0, tarData[byteIndex], + "Trailing block data should be null iteration {0} block {1} offset {2} index {3}", + factor, blockNumber, offset, byteIndex); + byteIndex += 1; } } } - + /// /// Check that the tar trailer only contains nulls. /// @@ -113,30 +125,33 @@ public void BlockFactorHandling() [Category("Tar")] public void TrailerContainsNulls() { - const int TestBlockFactor = 3; + const int testBlockFactor = 3; - for (int iteration = 0; iteration < TestBlockFactor * 2; ++iteration) { + for (int iteration = 0; iteration < testBlockFactor * 2; ++iteration) + { var ms = new MemoryStream(); - using (TarOutputStream tarOut = new TarOutputStream(ms, TestBlockFactor)) { + using (TarOutputStream tarOut = new TarOutputStream(ms, testBlockFactor, null)) + { TarEntry entry = TarEntry.CreateTarEntry("TestEntry"); - if (iteration > 0) { + if (iteration > 0) + { entry.Size = (TarBuffer.BlockSize * (iteration - 1)) + 9; } tarOut.PutNextEntry(entry); - byte[] buffer = new byte[TarBuffer.BlockSize]; + byte[] buffer = Utils.GetDummyBytes(TarBuffer.BlockSize); - var r = new Random(); - r.NextBytes(buffer); - - if (iteration > 0) { - for (int i = 0; i < iteration - 1; ++i) { + if (iteration > 0) + { + for (int i = 0; i < iteration - 1; ++i) + { tarOut.Write(buffer, 0, buffer.Length); } // Last block is a partial one - for (int i = 1; i < 10; ++i) { + for (int i = 1; i < 10; ++i) + { tarOut.WriteByte((byte)i); } } @@ -147,17 +162,19 @@ public void TrailerContainsNulls() // Blocks = Header + Data Blocks + Zero block + Record trailer int usedBlocks = 1 + iteration + 2; - int totalBlocks = usedBlocks + (TestBlockFactor - 1); - totalBlocks /= TestBlockFactor; - totalBlocks *= TestBlockFactor; + int totalBlocks = usedBlocks + (testBlockFactor - 1); + totalBlocks /= testBlockFactor; + totalBlocks *= testBlockFactor; Assert.AreEqual(TarBuffer.BlockSize * totalBlocks, tarData.Length, string.Format("Tar file should be {0} blocks in length", totalBlocks)); - if (usedBlocks < totalBlocks) { + if (usedBlocks < totalBlocks) + { // Start at first byte after header. int byteIndex = TarBuffer.BlockSize * (iteration + 1); - while (byteIndex < tarData.Length) { + while (byteIndex < tarData.Length) + { int blockNumber = byteIndex / TarBuffer.BlockSize; int offset = blockNumber % TarBuffer.BlockSize; Assert.AreEqual(0, tarData[byteIndex], @@ -170,10 +187,11 @@ public void TrailerContainsNulls() } } - void TryLongName(string name) + private void TryLongName(string name) { var ms = new MemoryStream(); - using (TarOutputStream tarOut = new TarOutputStream(ms)) { + using (TarOutputStream tarOut = new TarOutputStream(ms, nameEncoding: null)) + { DateTime modTime = DateTime.Now; TarEntry entry = TarEntry.CreateTarEntry(name); @@ -184,7 +202,8 @@ void TryLongName(string name) ms2.Write(ms.GetBuffer(), 0, ms.GetBuffer().Length); ms2.Seek(0, SeekOrigin.Begin); - using (TarInputStream tarIn = new TarInputStream(ms2)) { + using (TarInputStream tarIn = new TarInputStream(ms2, nameEncoding: null)) + { TarEntry nextEntry = tarIn.GetNextEntry(); Assert.AreEqual(nextEntry.Name, name, "Name match failure"); @@ -224,13 +243,59 @@ public void LongNames() "11111111112222222222333333333344444444445555555555" + "66666666667777777777888888888899999999990000000000"); - for (int n = 1; n < 1024; ++n) { + for (int n = 1; n < 1024; ++n) + { string format = "{0," + n + "}"; string formatted = string.Format(format, "A"); TryLongName(formatted); } } + [Test] + [Category("Tar")] + public void ExtendedHeaderLongName() + { + string expectedName = "lftest-0000000000111111111122222222223333333333444444444455555555556666666666777777777788888888889999999999"; + + var input64 = @"Li9QYXhIZWFkZXJzLjExOTY5L2xmdGVzdC0wMDAwMDAwMDAwMTExMTExMTExMTIyMjIyMjIyMjIz + MzMzMzMzMzMzNDQ0NDQ0NDQ0NDU1NTU1NTU1NTU2NjY2NjY2NjY2Nzc3NzAwMDA2NDQAMDAwMDAw + MAAwMDAwMDAwADAwMDAwMDAwMzE3ADEzMzE2MTYyMzMzADAyMTYwNgAgeAAAAAAAAAAAAAAAAAAA + AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA + AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAB1c3RhcgAwMAAAAAAAAAAAAAAAAAAAAAAAAAAA + AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA + AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA + AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA + AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAx + MTcgcGF0aD1sZnRlc3QtMDAwMDAwMDAwMDExMTExMTExMTEyMjIyMjIyMjIyMzMzMzMzMzMzMzQ0 + NDQ0NDQ0NDQ1NTU1NTU1NTU1NjY2NjY2NjY2Njc3Nzc3Nzc3Nzc4ODg4ODg4ODg4OTk5OTk5OTk5 + OQozMCBtdGltZT0xNTMwNDU1MjU5LjcwNjU0ODg4OAozMCBhdGltZT0xNTMwNDU1MjU5LjcwNjU0 + ODg4OAozMCBjdGltZT0xNTMwNDU1MjU5LjcwNjU0ODg4OAoAAAAAAAAAAAAAAAAAAAAAAAAAAAAA + AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA + AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA + AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA + AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA + AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAGxm + dGVzdC0wMDAwMDAwMDAwMTExMTExMTExMTIyMjIyMjIyMjIzMzMzMzMzMzMzNDQ0NDQ0NDQ0NDU1 + NTU1NTU1NTU2NjY2NjY2NjY2Nzc3Nzc3Nzc3Nzg4ODg4ODg4ODg5OTkwMDAwNjY0ADAwMDE3NTAA + MDAwMTc1MAAwMDAwMDAwMDAwMAAxMzMxNjE2MjMzMwAwMjM3MjcAIDAAAAAAAAAAAAAAAAAAAAAA + AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA + AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAdXN0YXIAMDBuaWxzAAAAAAAAAAAAAAAAAAAAAAAA + AAAAAAAAAAAAAG5pbHMAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMDAwMDAwMAAwMDAwMDAw"; + + var buffer = new byte[2560]; + var truncated = Convert.FromBase64String(input64); + Array.Copy(truncated, buffer, truncated.Length); + + using var ms = new MemoryStream(buffer); + using var tis = new TarInputStream(ms, nameEncoding: null); + var entry = tis.GetNextEntry(); + + Assert.IsNotNull(entry, "Entry is null"); + Assert.IsNotNull(entry.Name, "Entry name is null"); + Assert.AreEqual(expectedName.Length, entry.Name.Length, $"Entry name is truncated to {entry.Name.Length} bytes."); + Assert.AreEqual(expectedName, entry.Name, "Entry name does not match expected value"); + } + /// /// Test equals function for tar headers. /// @@ -303,7 +368,6 @@ public void HeaderEquality() h2.GroupName = h1.GroupName; Assert.IsTrue(h1.Equals(h2)); - h1.DevMajor = 165; Assert.IsFalse(h1.Equals(h2)); h2.DevMajor = h1.DevMajor; @@ -313,7 +377,6 @@ public void HeaderEquality() Assert.IsFalse(h1.Equals(h2)); h2.DevMinor = h1.DevMinor; Assert.IsTrue(h1.Equals(h2)); - } [Test] @@ -321,10 +384,9 @@ public void HeaderEquality() public void Checksum() { var ms = new MemoryStream(); - using (TarOutputStream tarOut = new TarOutputStream(ms)) { - DateTime modTime = DateTime.Now; - - TarEntry entry = TarEntry.CreateTarEntry("TestEntry"); + using (var tarOut = new TarOutputStream(ms, nameEncoding: null)) + { + var entry = TarEntry.CreateTarEntry("TestEntry"); entry.TarHeader.Mode = 12345; tarOut.PutNextEntry(entry); @@ -335,7 +397,8 @@ public void Checksum() ms2.Seek(0, SeekOrigin.Begin); TarEntry nextEntry; - using (TarInputStream tarIn = new TarInputStream(ms2)) { + using (var tarIn = new TarInputStream(ms2, nameEncoding: null)) + { nextEntry = tarIn.GetNextEntry(); Assert.IsTrue(nextEntry.TarHeader.IsChecksumValid, "Checksum should be valid"); } @@ -346,16 +409,9 @@ public void Checksum() ms3.Write(new byte[] { 34 }, 0, 1); ms3.Seek(0, SeekOrigin.Begin); - using (TarInputStream tarIn = new TarInputStream(ms3)) { - bool trapped = false; - - try { - nextEntry = tarIn.GetNextEntry(); - } catch (TarException) { - trapped = true; - } - - Assert.IsTrue(trapped, "Checksum should be invalid"); + using (var tarIn = new TarInputStream(ms3, nameEncoding: null)) + { + Assert.Throws(() => tarIn.GetNextEntry(), "Checksum should be invalid"); } } @@ -370,7 +426,8 @@ public void ValuesPreserved() TarEntry entry; DateTime modTime = DateTime.Now; - using (TarOutputStream tarOut = new TarOutputStream(ms)) { + using (TarOutputStream tarOut = new TarOutputStream(ms, null)) + { entry = TarEntry.CreateTarEntry("TestEntry"); entry.GroupId = 12; entry.UserId = 14; @@ -386,20 +443,22 @@ public void ValuesPreserved() ms2.Write(ms.GetBuffer(), 0, ms.GetBuffer().Length); ms2.Seek(0, SeekOrigin.Begin); - using (TarInputStream tarIn = new TarInputStream(ms2)) { + using (TarInputStream tarIn = new TarInputStream(ms2, null)) + { TarEntry nextEntry = tarIn.GetNextEntry(); Assert.AreEqual(entry.TarHeader.Checksum, nextEntry.TarHeader.Checksum, "Checksum"); Assert.IsTrue(nextEntry.Equals(entry), "Entries should be equal"); Assert.IsTrue(nextEntry.TarHeader.Equals(entry.TarHeader), "Headers should match"); - // Tar only stores seconds + // Tar only stores seconds var truncatedTime = new DateTime(modTime.Year, modTime.Month, modTime.Day, modTime.Hour, modTime.Minute, modTime.Second); Assert.AreEqual(truncatedTime, nextEntry.ModTime, "Modtimes should match"); entryCount = 0; - while (nextEntry != null) { + while (nextEntry != null) + { ++entryCount; nextEntry = tarIn.GetNextEntry(); } @@ -562,7 +621,7 @@ public void CloningAndUniqueness() public void OutputStreamOwnership() { var memStream = new TrackedMemoryStream(); - var s = new TarOutputStream(memStream); + var s = new TarOutputStream(memStream, null); Assert.IsFalse(memStream.IsClosed, "Shouldnt be closed initially"); Assert.IsFalse(memStream.IsDisposed, "Shouldnt be disposed initially"); @@ -573,7 +632,7 @@ public void OutputStreamOwnership() Assert.IsTrue(memStream.IsDisposed, "Should be disposed after parent owner close"); memStream = new TrackedMemoryStream(); - s = new TarOutputStream(memStream); + s = new TarOutputStream(memStream, null); Assert.IsFalse(memStream.IsClosed, "Shouldnt be closed initially"); Assert.IsFalse(memStream.IsDisposed, "Shouldnt be disposed initially"); @@ -590,7 +649,7 @@ public void OutputStreamOwnership() public void InputStreamOwnership() { var memStream = new TrackedMemoryStream(); - var s = new TarInputStream(memStream); + var s = new TarInputStream(memStream, null); Assert.IsFalse(memStream.IsClosed, "Shouldnt be closed initially"); Assert.IsFalse(memStream.IsDisposed, "Shouldnt be disposed initially"); @@ -601,7 +660,7 @@ public void InputStreamOwnership() Assert.IsTrue(memStream.IsDisposed, "Should be disposed after parent owner close"); memStream = new TrackedMemoryStream(); - s = new TarInputStream(memStream); + s = new TarInputStream(memStream, null); Assert.IsFalse(memStream.IsClosed, "Shouldnt be closed initially"); Assert.IsFalse(memStream.IsDisposed, "Shouldnt be disposed initially"); @@ -612,5 +671,247 @@ public void InputStreamOwnership() Assert.IsFalse(memStream.IsClosed, "Should not be closed after parent owner close"); Assert.IsFalse(memStream.IsDisposed, "Should not be disposed after parent owner close"); } + + [Test] + [Category("Tar")] + public void EndBlockHandling() + { + int dummySize = 70145; + + long outCount, inCount; + + using var ms = new MemoryStream(); + using (var tarOut = TarArchive.CreateOutputTarArchive(ms)) + using (var dummyFile = Utils.GetDummyFile(dummySize)) + { + tarOut.IsStreamOwner = false; + tarOut.WriteEntry(TarEntry.CreateEntryFromFile(dummyFile), recurse: false); + } + + outCount = ms.Position; + ms.Seek(0, SeekOrigin.Begin); + + using (var tarIn = TarArchive.CreateInputTarArchive(ms, nameEncoding: null)) + using (var tempDir = Utils.GetTempDir()) + { + tarIn.IsStreamOwner = false; + tarIn.ExtractContents(tempDir); + + foreach (var file in Directory.GetFiles(tempDir, "*", SearchOption.AllDirectories)) + { + Console.WriteLine($"Extracted \"{file}\""); + } + } + + inCount = ms.Position; + + Console.WriteLine($"Output count: {outCount}"); + Console.WriteLine($"Input count: {inCount}"); + + Assert.AreEqual(inCount, outCount, "Bytes read and bytes written should be equal"); + } + + [Test] + [Category("Tar")] + [Category("Performance")] + [Explicit("Long Running")] + public void WriteThroughput() + { + const string entryName = "LargeTarEntry"; + + PerformanceTesting.TestWrite(TestDataSize.Large, bs => + { + var tos = new TarOutputStream(bs, nameEncoding: null); + tos.PutNextEntry(new TarEntry(new TarHeader() + { + Name = entryName, + Size = (int)TestDataSize.Large, + })); + return tos; + }, + stream => + { + ((TarOutputStream)stream).CloseEntry(); + }); + } + + [Test] + [Category("Tar")] + [Category("Performance")] + [Explicit("Long Running")] + public void SingleLargeEntry() + { + const string entryName = "LargeTarEntry"; + const TestDataSize dataSize = TestDataSize.Large; + + PerformanceTesting.TestReadWrite( + size: dataSize, + input: bs => + { + var tis = new TarInputStream(bs, null); + var entry = tis.GetNextEntry(); + + Assert.AreEqual(entryName, entry.Name); + return tis; + }, + output: bs => + { + var tos = new TarOutputStream(bs, null); + tos.PutNextEntry(new TarEntry(new TarHeader() + { + Name = entryName, + Size = (int)dataSize, + })); + return tos; + }, + outputClose: stream => + { + ((TarOutputStream)stream).CloseEntry(); + } + ); + } + + // Test for corruption issue described @ https://github.com/icsharpcode/SharpZipLib/issues/321 + [Test] + [Category("Tar")] + public void ExtractingCorruptTarShouldntLeakFiles() + { + using var memoryStream = new MemoryStream(); + //Create a tar.gz in the output stream + using (var gzipStream = new GZipOutputStream(memoryStream)) + { + gzipStream.IsStreamOwner = false; + + using (var tarOut = TarArchive.CreateOutputTarArchive(gzipStream)) + using (var dummyFile = Utils.GetDummyFile(size: 32000)) + { + tarOut.IsStreamOwner = false; + tarOut.WriteEntry(TarEntry.CreateEntryFromFile(dummyFile), recurse: false); + } + } + + // corrupt archive - make sure the file still has more than one block + memoryStream.SetLength(16000); + memoryStream.Seek(0, SeekOrigin.Begin); + + // try to extract + using (var gzipStream = new GZipInputStream(memoryStream)) + { + gzipStream.IsStreamOwner = false; + + using var tempDir = Utils.GetTempDir(); + using (var tarIn = TarArchive.CreateInputTarArchive(gzipStream, nameEncoding: null)) + { + tarIn.IsStreamOwner = false; + Assert.Throws(() => tarIn.ExtractContents(tempDir)); + } + + // Try to remove the output directory to check if any file handles are still being held + Assert.DoesNotThrow(() => tempDir.Delete()); + + Assert.That(tempDir.Exists, Is.False, "Temporary folder should have been removed"); + } + } + [TestCase(10, "utf-8")] + [TestCase(10, "shift-jis")] + [Category("Tar")] + public void ParseHeaderWithEncoding(int length, string encodingName) + { + // U+3042 is Japanese Hiragana + // https://unicode.org/charts/PDF/U3040.pdf + var name = new string((char)0x3042, length); + var header = new TarHeader(); + var enc = Encoding.GetEncoding(encodingName); + byte[] headerbytes = new byte[1024]; + var encodedName = enc.GetBytes(name); + header.Name = name; + header.WriteHeader(headerbytes, enc); + var reparseHeader = new TarHeader(); + reparseHeader.ParseBuffer(headerbytes, enc); + Assert.AreEqual(name, reparseHeader.Name); + // top 100 bytes are name field in tar header + for (int i = 0; i < encodedName.Length; i++) + { + Assert.AreEqual(encodedName[i], headerbytes[i]); + } + } + [TestCase(1, "utf-8")] + [TestCase(100, "utf-8")] + [TestCase(128, "utf-8")] + [TestCase(1, "shift-jis")] + [TestCase(100, "shift-jis")] + [TestCase(128, "shift-jis")] + [Category("Tar")] + public async Task StreamWithJapaneseNameAsync(int length, string encodingName) + { + // U+3042 is Japanese Hiragana + // https://unicode.org/charts/PDF/U3040.pdf + var entryName = new string((char)0x3042, length); + var data = new byte[32]; + var encoding = Encoding.GetEncoding(encodingName); + using (var memoryStream = new MemoryStream()) + { + using (var tarOutput = new TarOutputStream(memoryStream, encoding)) + { + var entry = TarEntry.CreateTarEntry(entryName); + entry.Size = 32; + tarOutput.PutNextEntry(entry); + tarOutput.Write(data, 0, data.Length); + } + + using(var memInput = new MemoryStream(memoryStream.ToArray())) + using(var inputStream = new TarInputStream(memInput, encoding)) + { + var buf = new byte[64]; + var entry = await inputStream.GetNextEntryAsync(CancellationToken.None); + Assert.AreEqual(entryName, entry.Name); + var bytesread = await inputStream.ReadAsync(buf, 0, buf.Length, CancellationToken.None); + Assert.AreEqual(data.Length, bytesread); + } + File.WriteAllBytes(Path.Combine(Path.GetTempPath(), $"jpnametest_{length}_{encodingName}.tar"), memoryStream.ToArray()); + } + } + /// + /// This test could be considered integration test. it creates a tar archive with the root directory specified + /// Then extracts it and compares the two folders. This used to fail on unix due to issues with root folder handling + /// in the tar archive. + /// + [Test] + [Category("Tar")] + public void RootPathIsRespected() + { + using (var extractDirectory = new TempDir()) + using (var tarFileName = new TempFile()) + using (var tempDirectory = new TempDir()) + { + tempDirectory.CreateDummyFile(); + + using (var tarFile = File.Open(tarFileName.FullName, FileMode.Create)) + { + using (var tarOutputStream = TarArchive.CreateOutputTarArchive(tarFile)) + { + tarOutputStream.RootPath = tempDirectory.FullName; + var entry = TarEntry.CreateEntryFromFile(tempDirectory.FullName); + tarOutputStream.WriteEntry(entry, true); + } + } + + using (var file = File.OpenRead(tarFileName.FullName)) + { + using (var archive = TarArchive.CreateInputTarArchive(file, Encoding.UTF8)) + { + archive.ExtractContents(extractDirectory.FullName); + } + } + + var expectationDirectory = new DirectoryInfo(tempDirectory.FullName); + foreach (var checkFile in expectationDirectory.GetFiles("", SearchOption.AllDirectories)) + { + var relativePath = checkFile.FullName.Substring(expectationDirectory.FullName.Length + 1); + FileAssert.Exists(Path.Combine(extractDirectory.FullName, relativePath)); + FileAssert.AreEqual(checkFile.FullName, Path.Combine(extractDirectory.FullName, relativePath)); + } + } + } } } diff --git a/test/ICSharpCode.SharpZipLib.Tests/TestSupport/PerformanceTesting.cs b/test/ICSharpCode.SharpZipLib.Tests/TestSupport/PerformanceTesting.cs new file mode 100644 index 000000000..73e581ef2 --- /dev/null +++ b/test/ICSharpCode.SharpZipLib.Tests/TestSupport/PerformanceTesting.cs @@ -0,0 +1,227 @@ +using NUnit.Framework; +using System; +using System.Diagnostics; +using System.IO; +using System.Runtime.ExceptionServices; +using System.Threading; + +namespace ICSharpCode.SharpZipLib.Tests.TestSupport +{ + internal static class PerformanceTesting + { + private const double ByteToMB = 1000000; + private const int PacifierOffset = 0x100000; + + public static void TestReadWrite(TestDataSize size, Func input, Func output, Action outputClose = null) + => TestReadWrite((int)size, input, output); + + public static void TestWrite(TestDataSize size, Func output, Action outputClose = null) + => TestWrite((int)size, output, outputClose); + + public static void TestReadWrite(int size, Func input, Func output, Action outputClose = null) + { + var cts = new CancellationTokenSource(TimeSpan.FromMinutes(1)); + var window = new WindowedStream(size, cts.Token); + + var readerState = new PerfWorkerState() + { + bytesLeft = size, + token = cts.Token, + baseStream = window, + streamCtr = input, + }; + + var writerState = new PerfWorkerState() + { + bytesLeft = size, + token = cts.Token, + baseStream = window, + streamCtr = output, + streamCls = outputClose + }; + + var reader = new Thread(stateObject => + { + var state = (PerfWorkerState)stateObject; + try + { + // Run output stream constructor + state.InitStream(); + + // Main read loop + ReadTargetBytes(ref state); + + if (!state.token.IsCancellationRequested) + { + Assert.IsFalse(state.baseStream.CanRead, "Base Stream should be closed"); + + // This shouldnt read any data but should read the footer + var buffer = new byte[1]; + int readBytes = state.stream.Read(buffer, 0, 1); + Assert.LessOrEqual(readBytes, 0, "Stream should be empty"); + } + + // Dispose of the input stream + state.stream.Close(); + } + catch (Exception x) + { + state.exception = x; + } + }); + + Thread writer = new Thread(stateObject => + { + var state = (PerfWorkerState)stateObject; + try + { + // Run input stream constructor + state.InitStream(); + + // Main write loop + WriteTargetBytes(ref state); + + state.DeinitStream(); + + // Dispose of the input stream + state.stream.Close(); + } + catch (Exception x) + { + state.exception = x; + } + }); + + var sw = Stopwatch.StartNew(); + + writer.Name = "Writer"; + writer.Start(writerState); + + // Give the writer thread a couple of seconds to write headers + Thread.Sleep(TimeSpan.FromSeconds(3)); + + reader.Name = "Reader"; + reader.Start(readerState); + + bool writerJoined = false, readerJoined = false; + const int timeout = 100; + + while (!writerJoined && !readerJoined) + { + writerJoined = writer.Join(timeout); + if (writerJoined && writerState.exception != null) + ExceptionDispatchInfo.Capture(writerState.exception).Throw(); + + readerJoined = reader.Join(timeout); + if (readerJoined && readerState.exception != null) + ExceptionDispatchInfo.Capture(readerState.exception).Throw(); + + if (cts.IsCancellationRequested) break; + } + + //Assert.IsTrue(writerJoined, "Timed out waiting for reader thread to join"); + //Assert.IsTrue(readerJoined, "Timed out waiting for writer thread to join"); + + Assert.IsFalse(cts.IsCancellationRequested, "Threads were cancelled before completing execution"); + + var elapsed = sw.Elapsed; + var testSize = size / ByteToMB; + Console.WriteLine($"Time {elapsed:mm\\:ss\\.fff} throughput {testSize / elapsed.TotalSeconds:f2} MB/s (using test size: {testSize:f2} MB)"); + } + + public static void TestWrite(int size, Func output, Action outputClose = null) + { + var cts = new CancellationTokenSource(TimeSpan.FromMinutes(1)); + + var sw = Stopwatch.StartNew(); + var writerState = new PerfWorkerState() + { + bytesLeft = size, + token = cts.Token, + baseStream = new NullStream(), + streamCtr = output, + }; + + writerState.InitStream(); + WriteTargetBytes(ref writerState); + + writerState.DeinitStream(); + + writerState.stream.Close(); + + var elapsed = sw.Elapsed; + var testSize = size / ByteToMB; + Console.WriteLine($"Time {elapsed:mm\\:ss\\.fff} throughput {testSize / elapsed.TotalSeconds:f2} MB/s (using test size: {testSize:f2} MB)"); + } + + internal static void WriteTargetBytes(ref PerfWorkerState state) + { + const int bufferSize = 8192; + byte[] buffer = new byte[bufferSize]; + int bytesToWrite = bufferSize; + + while (state.bytesLeft > 0 && !state.token.IsCancellationRequested) + { + if (state.bytesLeft < bufferSize) + bytesToWrite = bufferSize; + + state.stream.Write(buffer, 0, bytesToWrite); + state.bytesLeft -= bytesToWrite; + } + } + + internal static void ReadTargetBytes(ref PerfWorkerState state) + { + const int bufferSize = 8192; + byte[] buffer = new byte[bufferSize]; + int bytesRead, bytesToRead = bufferSize; + + int pacifierLevel = state.bytesLeft - PacifierOffset; + + while ((state.bytesLeft > 0) && !state.token.IsCancellationRequested) + { + if (state.bytesLeft < bufferSize) + bytesToRead = bufferSize; + + bytesRead = state.stream.Read(buffer, 0, bytesToRead); + state.bytesLeft -= bytesRead; + + if (state.bytesLeft <= pacifierLevel) + { + Debug.WriteLine($"Reader {state.bytesLeft} bytes remaining"); + pacifierLevel = state.bytesLeft - PacifierOffset; + } + + if (bytesRead == 0) break; + } + } + } + + internal class PerfWorkerState + { + public Stream stream; + public Stream baseStream; + public int bytesLeft; + public Exception exception; + public CancellationToken token; + public Func streamCtr; + public Action streamCls; + + public void InitStream() + { + stream = streamCtr(baseStream); + } + + public void DeinitStream() + { + streamCls?.Invoke(stream); + } + } + + public enum TestDataSize : int + { + Large = 0x10000000, + Medium = 0x5000000, + Small = 0x1400000, + } +} diff --git a/test/ICSharpCode.SharpZipLib.Tests/TestSupport/RingBuffer.cs b/test/ICSharpCode.SharpZipLib.Tests/TestSupport/RingBuffer.cs index 19ae1f130..c8ee11881 100644 --- a/test/ICSharpCode.SharpZipLib.Tests/TestSupport/RingBuffer.cs +++ b/test/ICSharpCode.SharpZipLib.Tests/TestSupport/RingBuffer.cs @@ -2,10 +2,9 @@ // They are about the same in terms of speed. #define SimpleSynch +using NUnit.Framework; using System; using System.Threading; -using NUnit.Framework; - namespace ICSharpCode.SharpZipLib.Tests.TestSupport { @@ -17,18 +16,21 @@ namespace ICSharpCode.SharpZipLib.Tests.TestSupport public class ReadWriteRingBuffer { #region Constructors + /// /// Create a new RingBuffer with a specified size. /// /// The size of the ring buffer to create. - public ReadWriteRingBuffer(int size) + public ReadWriteRingBuffer(int size, CancellationToken? token = null) { - if (size <= 0) { + if (size <= 0) + { throw new ArgumentOutOfRangeException(nameof(size)); } array_ = new byte[size]; lockObject_ = new object(); + token_ = token; #if SimpleSynch waitSpan_ = TimeSpan.FromMilliseconds(1); @@ -37,7 +39,8 @@ public ReadWriteRingBuffer(int size) notFullEvent_ = new ManualResetEvent(true); #endif } - #endregion + + #endregion Constructors /// /// Clear the buffer contents. @@ -74,19 +77,23 @@ public void Close() /// The value to add. public void WriteByte(byte value) { - if (isClosed_) { + if (isClosed_) + { throw new ApplicationException("Buffer is closed"); } #if SimpleSynch - while (IsFull) { + while (IsFull) + { Thread.Sleep(waitSpan_); + token_?.ThrowIfCancellationRequested(); } #else notFullEvent_.WaitOne(); #endif - lock (lockObject_) { + lock (lockObject_) + { array_[head_] = value; head_ = (head_ + 1) % array_.Length; @@ -114,14 +121,18 @@ public void WriteByte(byte value) public void Write(byte[] buffer, int index, int count) { - if (isClosed_) { + if (isClosed_) + { throw new ApplicationException("Buffer is closed"); } - while (count > 0) { + while (count > 0) + { #if SimpleSynch - while (IsFull) { + while (IsFull) + { Thread.Sleep(waitSpan_); + token_?.ThrowIfCancellationRequested(); } #else notFullEvent_.WaitOne(); @@ -129,17 +140,20 @@ public void Write(byte[] buffer, int index, int count) // Gauranteed to not be full at this point, however readers may sill read // from the buffer first. - lock (lockObject_) { + lock (lockObject_) + { int bytesToWrite = Length - Count; - if (count < bytesToWrite) { + if (count < bytesToWrite) + { bytesToWrite = count; } #if !SimpleSynch bool setEmpty = (count_ == 0); #endif - while (bytesToWrite > 0) { + while (bytesToWrite > 0) + { array_[head_] = buffer[index]; index++; @@ -175,15 +189,19 @@ public int ReadByte() int result = -1; #if SimpleSynch - while (!isClosed_ && IsEmpty) { + while (!isClosed_ && IsEmpty) + { Thread.Sleep(waitSpan_); + token_?.ThrowIfCancellationRequested(); } #else notEmptyEvent_.WaitOne(); #endif - if (!IsEmpty) { - lock (lockObject_) { + if (!IsEmpty) + { + lock (lockObject_) + { result = array_[tail_]; tail_ = (tail_ + 1) % array_.Length; #if !SimpleSynch @@ -213,22 +231,30 @@ public int Read(byte[] buffer, int index, int count) { int result = 0; - while (count > 0) { + while (count > 0) + { #if SimpleSynch - while (!isClosed_ && IsEmpty) { + while (!isClosed_ && IsEmpty) + { Thread.Sleep(waitSpan_); + token_?.ThrowIfCancellationRequested(); } #else notEmptyEvent_.WaitOne(); #endif - if (IsEmpty) { + if (IsEmpty) + { count = 0; - } else { - lock (lockObject_) { + } + else + { + lock (lockObject_) + { int toRead = Count; - if (toRead > count) { + if (toRead > count) + { toRead = count; } @@ -238,7 +264,8 @@ public int Read(byte[] buffer, int index, int count) bool setFull = IsFull; #endif - while (toRead > 0) { + while (toRead > 0) + { buffer[index] = array_[tail_]; index++; @@ -271,48 +298,59 @@ public int Read(byte[] buffer, int index, int count) /// /// Gets a value indicating wether the buffer is empty or not. /// - public bool IsEmpty { + public bool IsEmpty + { get { return count_ == 0; } } - public bool IsFull { - get { + public bool IsFull + { + get + { return (count_ == array_.Length); } } - public bool IsClosed { + public bool IsClosed + { get { return isClosed_; } } /// /// Gets the number of elements in the buffer. /// - public int Count { - get { + public int Count + { + get + { return count_; } } - - public int Length { + public int Length + { get { return array_.Length; } } - public long BytesWritten { + public long BytesWritten + { get { return bytesWritten_; } } - public long BytesRead { + public long BytesRead + { get { return bytesRead_; } } /// /// Indexer - Get an element from the tail of the RingBuffer. /// - public byte this[int index] { - get { - if ((index < 0) || (index >= array_.Length)) { + public byte this[int index] + { + get + { + if ((index < 0) || (index >= array_.Length)) + { throw new ArgumentOutOfRangeException(nameof(index)); } @@ -320,51 +358,54 @@ public byte this[int index] { } } - #endregion + #endregion Properties #region Instance Variables + /// /// Flag indicating the buffer is closed. /// - bool isClosed_; + private bool isClosed_; /// /// Index for the head of the buffer. /// /// Its the index of the next byte to be written. - int head_; + private int head_; /// /// Index for the tail of the buffer. /// /// Its the index of the next byte to be written. - int tail_; + private int tail_; /// /// The total number of elements added to the buffer. /// - int count_; + private int count_; /// /// Storage for the ring buffer contents. /// - byte[] array_; + private byte[] array_; - long bytesWritten_; - long bytesRead_; + private long bytesWritten_; + private long bytesRead_; - object lockObject_; - - TimeSpan waitSpan_; + private object lockObject_; + private CancellationToken? token_; + private TimeSpan waitSpan_; #if !SimpleSynch ManualResetEvent notEmptyEvent_; ManualResetEvent notFullEvent_; #endif - #endregion + + #endregion Instance Variables } [TestFixture] + [Explicit("Meta tests (for ringbuffer)")] public class ExerciseBuffer { [Test] @@ -388,7 +429,8 @@ public void Basic() Assert.IsFalse(buffer_.IsFull); Assert.IsTrue(buffer_.IsEmpty); - for (int i = 0; i < buffer_.Length; ++i) { + for (int i = 0; i < buffer_.Length; ++i) + { buffer_.WriteByte(unchecked((byte)(i & 0xff))); } @@ -400,9 +442,12 @@ public void Basic() Assert.IsTrue(buffer_.IsClosed); bool caught = false; - try { + try + { buffer_.WriteByte(1); - } catch { + } + catch + { caught = true; } @@ -411,7 +456,8 @@ public void Basic() int count = Size; int expected = 0; - while (count != 0) { + while (count != 0) + { Assert.AreEqual(count, buffer_.Count); Assert.AreEqual(expected, buffer_.ReadByte()); count--; @@ -425,13 +471,13 @@ public void Basic() [Test] public void Buffered() { - const int Size = 64; buffer_ = new ReadWriteRingBuffer(Size); byte[] writeBuffer = new byte[16]; - for (int i = 0; i < 16; ++i) { + for (int i = 0; i < 16; ++i) + { writeBuffer[i] = (byte)i; } @@ -440,10 +486,10 @@ public void Buffered() byte[] readBuffer = new byte[16]; Assert.AreEqual(3, buffer_.Read(readBuffer, 0, 3)); - for (int i = 0; i < 3; ++i) { + for (int i = 0; i < 3; ++i) + { Assert.AreEqual(i, readBuffer[i]); } - } [Test] @@ -462,18 +508,21 @@ public void Threaded() reader.Join(); } - void Reader() + private void Reader() { - var r = new Random(); + var r = new Random(Utils.DefaultSeed); byte nextValue = 0; - while (readTarget_ > 0) { + while (readTarget_ > 0) + { int thisTime = r.Next(16); - if (thisTime > readTarget_) { + if (thisTime > readTarget_) + { thisTime = readTarget_; } - while (thisTime > 0) { + while (thisTime > 0) + { int readValue = buffer_.ReadByte(); Assert.AreEqual(nextValue, readValue); nextValue = (byte)((nextValue + 1) & 0xff); @@ -482,7 +531,6 @@ void Reader() } Thread.Sleep(r.Next(10)); - } int last = buffer_.ReadByte(); @@ -491,18 +539,21 @@ void Reader() Assert.IsTrue(buffer_.IsClosed); } - void Writer() + private void Writer() { - var r = new Random(); + var r = new Random(Utils.DefaultSeed); byte nextValue = 0; - while (writeTarget_ > 0) { + while (writeTarget_ > 0) + { int thisTime = r.Next(16); - if (thisTime > writeTarget_) { + if (thisTime > writeTarget_) + { thisTime = writeTarget_; } - while (thisTime > 0) { + while (thisTime > 0) + { buffer_.WriteByte(nextValue); nextValue = (byte)((nextValue + 1) & 0xff); thisTime--; @@ -513,10 +564,9 @@ void Writer() buffer_.Close(); } - int readTarget_; - int writeTarget_; - - ReadWriteRingBuffer buffer_; + private int readTarget_; + private int writeTarget_; + private ReadWriteRingBuffer buffer_; } } diff --git a/test/ICSharpCode.SharpZipLib.Tests/TestSupport/SevenZip.cs b/test/ICSharpCode.SharpZipLib.Tests/TestSupport/SevenZip.cs new file mode 100644 index 000000000..e9887172a --- /dev/null +++ b/test/ICSharpCode.SharpZipLib.Tests/TestSupport/SevenZip.cs @@ -0,0 +1,112 @@ +using System; +using System.Diagnostics; +using System.IO; +using NUnit.Framework; + +namespace ICSharpCode.SharpZipLib.Tests.TestSupport +{ + // Helper class for verifying zips with 7-zip + internal static class SevenZipHelper + { + private static readonly string[] possible7zPaths = new[] { + // Check in PATH + "7z", "7za", + + // Check in default install location + Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), "7-Zip", "7z.exe"), + Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86), "7-Zip", "7z.exe"), + }; + + private static bool TryGet7zBinPath(out string path7z) + { + var runTimeLimit = TimeSpan.FromSeconds(3); + + foreach (var testPath in possible7zPaths) + { + try + { + var p = Process.Start(new ProcessStartInfo(testPath, "i") + { + RedirectStandardOutput = true, + UseShellExecute = false + }); + while (!p.StandardOutput.EndOfStream && (DateTime.Now - p.StartTime) < runTimeLimit) + { + p.StandardOutput.DiscardBufferedData(); + } + if (!p.HasExited) + { + p.Close(); + Assert.Warn($"Timed out checking for 7z binary in \"{testPath}\"!"); + continue; + } + + if (p.ExitCode == 0) + { + path7z = testPath; + return true; + } + } + catch (Exception) + { + continue; + } + } + path7z = null; + return false; + } + + /// + /// Helper function to verify the provided zip stream with 7Zip. + /// + /// A stream containing the zip archive to test. + /// The password for the archive. + internal static void VerifyZipWith7Zip(Stream zipStream, string password) + { + if (TryGet7zBinPath(out string path7z)) + { + Console.WriteLine($"Using 7z path: \"{path7z}\""); + + var fileName = Path.GetTempFileName(); + + try + { + using (var fs = File.OpenWrite(fileName)) + { + zipStream.Seek(0, SeekOrigin.Begin); + zipStream.CopyTo(fs); + } + + var p = Process.Start(new ProcessStartInfo(path7z, $"t -p{password} \"{fileName}\"") + { + RedirectStandardOutput = true, + RedirectStandardError = true, + UseShellExecute = false, + }); + + if (p == null) + { + Assert.Inconclusive("Failed to start 7z process. Skipping!"); + } + if (!p.WaitForExit(2000)) + { + Assert.Warn("Timed out verifying zip file!"); + } + + TestContext.Out.Write(p.StandardOutput.ReadToEnd()); + var errors = p.StandardError.ReadToEnd(); + Assert.IsEmpty(errors, "7z reported errors"); + Assert.AreEqual(0, p.ExitCode, "Archive verification failed"); + } + finally + { + File.Delete(fileName); + } + } + else + { + Assert.Warn("Skipping file verification since 7za is not in path"); + } + } + } +} diff --git a/test/ICSharpCode.SharpZipLib.Tests/TestSupport/Streams.cs b/test/ICSharpCode.SharpZipLib.Tests/TestSupport/Streams.cs index 93aeed7ef..2d1b00fb8 100644 --- a/test/ICSharpCode.SharpZipLib.Tests/TestSupport/Streams.cs +++ b/test/ICSharpCode.SharpZipLib.Tests/TestSupport/Streams.cs @@ -1,5 +1,7 @@ using System; using System.IO; +using System.Threading; +using System.Threading.Tasks; namespace ICSharpCode.SharpZipLib.Tests.TestSupport { @@ -60,7 +62,8 @@ protected override void Dispose(bool disposing) /// public override void Close() { - if (isClosed_) { + if (isClosed_) + { throw new InvalidOperationException("Already closed"); } @@ -72,7 +75,76 @@ public override void Close() /// Gets a value indicating whether this instance is closed. /// /// true if this instance is closed; otherwise, false. - public bool IsClosed { + public bool IsClosed + { + get { return isClosed_; } + } + + /// + /// Gets a value indicating whether this instance is disposed. + /// + /// + /// true if this instance is disposed; otherwise, false. + /// + public bool IsDisposed + { + get { return isDisposed_; } + } + + #region Instance Fields + + private bool isDisposed_; + + private bool isClosed_; + + #endregion Instance Fields + } + + /// + /// An extended file stream + /// that tracks closing and disposing + /// + public class TrackedFileStream : FileStream + { + /// + /// Initializes a new instance of the class. + /// + /// The buffer. + public TrackedFileStream(string path, FileMode mode = FileMode.Open, FileAccess access = FileAccess.Read) + : base(path, mode, access) + { + } + + /// + /// Releases the unmanaged resources used by the class and optionally releases the managed resources. + /// + /// true to release both managed and unmanaged resources; false to release only unmanaged resources. + protected override void Dispose(bool disposing) + { + isDisposed_ = true; + base.Dispose(disposing); + } + + /// + /// Closes the current stream and releases any resources (such as sockets and file handles) associated with the current stream. + /// + public override void Close() + { + if (isClosed_) + { + throw new InvalidOperationException("Already closed"); + } + + isClosed_ = true; + base.Close(); + } + + /// + /// Gets a value indicating whether this instance is closed. + /// + /// true if this instance is closed; otherwise, false. + public bool IsClosed + { get { return isClosed_; } } @@ -82,15 +154,18 @@ public bool IsClosed { /// /// true if this instance is disposed; otherwise, false. /// - public bool IsDisposed { + public bool IsDisposed + { get { return isDisposed_; } } #region Instance Fields - bool isDisposed_; - bool isClosed_; - #endregion + private bool isDisposed_; + + private bool isClosed_; + + #endregion Instance Fields } /// @@ -103,12 +178,87 @@ public class MemoryStreamWithoutSeek : TrackedMemoryStream /// /// /// true if the stream is open. - public override bool CanSeek { - get { - return false; + public override bool CanSeek => false; + + /// + public override long Position + { + get => throw new NotSupportedException("Getting position is not supported"); + set => throw new NotSupportedException("Setting position is not supported"); + } + + } + +#if NETSTANDARD2_1 || NETCOREAPP3_0_OR_GREATER + /// + /// A that does not support non-async operations. + /// + /// + /// This could not be done by extending MemoryStream itself, since other instances of MemoryStream tries to us a faster (non-async) method of copying + /// if it detects that it's a (subclass of) MemoryStream. + /// + public class MemoryStreamWithoutSync : Stream + { + MemoryStream _inner = new MemoryStream(); + + public override bool CanRead => _inner.CanRead; + public override bool CanSeek => _inner.CanSeek; + public override bool CanWrite => _inner.CanWrite; + public override long Length => _inner.Length; + public override long Position { get => _inner.Position; set => _inner.Position = value; } + + public byte[] ToArray() => _inner.ToArray(); + + public override void Flush() => throw new NotSupportedException($"Non-async call to {nameof(Flush)}"); + + + public override void CopyTo(Stream destination, int bufferSize) => throw new NotSupportedException($"Non-async call to {nameof(CopyTo)}"); + public override void Write(ReadOnlySpan buffer) => throw new NotSupportedException($"Non-async call to {nameof(Write)}"); + public override int Read(Span buffer) => throw new NotSupportedException($"Non-async call to {nameof(Read)}"); + + public override void Write(byte[] buffer, int offset, int count) => throw new NotSupportedException($"Non-async call to {nameof(Write)}"); + public override void WriteByte(byte value) => throw new NotSupportedException($"Non-async call to {nameof(Write)}"); + + public override int Read(byte[] buffer, int offset, int count) => throw new NotSupportedException($"Non-async call to {nameof(Read)}"); + public override int ReadByte() => throw new NotSupportedException($"Non-async call to {nameof(ReadByte)}"); + + // Even though our mock stream is writing synchronously, this should not fail the tests + public override async Task CopyToAsync(Stream destination, int bufferSize, CancellationToken cancellationToken) + { + var buf = new byte[bufferSize]; + while(_inner.Read(buf, 0, bufferSize) > 0) { + await destination.WriteAsync(buf, cancellationToken); } } + public override Task FlushAsync(CancellationToken cancellationToken) => TaskFromBlocking(() => _inner.Flush()); + public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) => TaskFromBlocking(() => _inner.Write(buffer, offset, count)); + public override Task ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) => Task.FromResult(_inner.Read(buffer, offset, count)); + public override ValueTask WriteAsync(ReadOnlyMemory buffer, CancellationToken cancellationToken = default) => ValueTaskFromBlocking(() => _inner.Write(buffer.Span)); + public override ValueTask ReadAsync(Memory buffer, CancellationToken cancellationToken = default) => ValueTask.FromResult(_inner.Read(buffer.Span)); + + static Task TaskFromBlocking(Action action) + { + action(); + return Task.CompletedTask; + } + + static ValueTask ValueTaskFromBlocking(Action action) + { + action(); + return ValueTask.CompletedTask; + } + + public override long Seek(long offset, SeekOrigin origin) + { + return _inner.Seek(offset, origin); + } + + public override void SetLength(long value) + { + _inner.SetLength(value); + } } +#endif /// /// A that cannot be read but supports infinite writes. @@ -120,7 +270,8 @@ public class NullStream : Stream /// /// /// true if the stream supports reading; otherwise, false. - public override bool CanRead { + public override bool CanRead + { get { return false; } } @@ -129,7 +280,8 @@ public override bool CanRead { ///
/// /// true if the stream supports seeking; otherwise, false. - public override bool CanSeek { + public override bool CanSeek + { get { return false; } } @@ -138,7 +290,8 @@ public override bool CanSeek { /// /// /// true if the stream supports writing; otherwise, false. - public override bool CanWrite { + public override bool CanWrite + { get { return true; } } @@ -158,7 +311,8 @@ public override void Flush() /// A long value representing the length of the stream in bytes. /// A class derived from Stream does not support seeking. /// Methods were called after the stream was closed. - public override long Length { + public override long Length + { get { throw new Exception("The method or operation is not implemented."); } } @@ -170,11 +324,14 @@ public override long Length { /// An I/O error occurs. /// The stream does not support seeking. /// Methods were called after the stream was closed. - public override long Position { - get { + public override long Position + { + get + { throw new Exception("The method or operation is not implemented."); } - set { + set + { throw new Exception("The method or operation is not implemented."); } } @@ -251,7 +408,7 @@ public override void Write(byte[] buffer, int offset, int count) /// /// A that supports reading and writing from a fixed size memory buffer. - /// This provides the ability to test writing and reading from very large streams + /// This provides the ability to test writing and reading from very large streams /// without using any disk storeage /// public class WindowedStream : Stream @@ -260,37 +417,33 @@ public class WindowedStream : Stream /// Initializes a new instance of the class. /// /// The size. - public WindowedStream(int size) + public WindowedStream(int size, CancellationToken? token = null) { - ringBuffer_ = new ReadWriteRingBuffer(size); + ringBuffer = new ReadWriteRingBuffer(size, token); } /// /// When overridden in a derived class, gets a value indicating whether the current stream supports reading. /// /// - /// true if the stream supports reading; otherwise, false. - public override bool CanRead { - get { return true; } - } + /// true if the stream is not closed. + /// If the stream is closed, this property returns false. + public override bool CanRead => !ringBuffer.IsClosed; /// - /// When overridden in a derived class, gets a value indicating whether the current stream supports seeking. + /// Gets a value indicating whether the current stream supports seeking. /// /// - /// true if the stream supports seeking; otherwise, false. - public override bool CanSeek { - get { return false; } - } + /// false + public override bool CanSeek => false; /// /// When overridden in a derived class, gets a value indicating whether the current stream supports writing. /// /// - /// true if the stream supports writing; otherwise, false. - public override bool CanWrite { - get { return true; } - } + /// true if the stream is not closed. + /// If the stream is closed, this property returns false. + public override bool CanWrite => !ringBuffer.IsClosed; /// /// When overridden in a derived class, clears all buffers for this stream and causes any buffered data to be written to the underlying device. @@ -308,9 +461,9 @@ public override void Flush() /// A long value representing the length of the stream in bytes. /// A class derived from Stream does not support seeking. /// Methods were called after the stream was closed. - public override long Length { - // A bit of a HAK as its not true in the stream sense. - get { return ringBuffer_.Count; } + public override long Length + { + get => throw new NotSupportedException(); } /// @@ -321,13 +474,10 @@ public override long Length { /// An I/O error occurs. /// The stream does not support seeking. /// Methods were called after the stream was closed. - public override long Position { - get { - throw new Exception("The method or operation is not implemented."); - } - set { - throw new Exception("The method or operation is not implemented."); - } + public override long Position + { + get => throw new NotSupportedException(); + set => throw new NotSupportedException(); } /// @@ -350,14 +500,18 @@ public override long Position { public override int Read(byte[] buffer, int offset, int count) { int bytesRead = 0; - while (count > 0) { - int value = ringBuffer_.ReadByte(); - if (value >= 0) { + while (count > 0) + { + int value = ringBuffer.ReadByte(); + if (value >= 0) + { buffer[offset] = (byte)(value & 0xff); offset++; bytesRead++; count--; - } else { + } + else + { break; } } @@ -366,35 +520,23 @@ public override int Read(byte[] buffer, int offset, int count) } /// - /// When overridden in a derived class, sets the position within the current stream. + /// Not supported, throws . /// /// A byte offset relative to the parameter. /// A value of type indicating the reference point used to obtain the new position. - /// - /// The new position within the current stream. - /// - /// An I/O error occurs. + /// /// The stream does not support seeking, such as if the stream is constructed from a pipe or console output. - /// Methods were called after the stream was closed. - public override long Seek(long offset, SeekOrigin origin) - { - throw new Exception("The method or operation is not implemented."); - } + public override long Seek(long offset, SeekOrigin origin) => throw new NotSupportedException(); /// - /// When overridden in a derived class, sets the length of the current stream. + /// Not supported, throws . /// /// The desired length of the current stream in bytes. - /// An I/O error occurs. /// The stream does not support both writing and seeking, such as if the stream is constructed from a pipe or console output. - /// Methods were called after the stream was closed. - public override void SetLength(long value) - { - throw new Exception("The method or operation is not implemented."); - } + public override void SetLength(long value) => throw new NotSupportedException(); /// - /// When overridden in a derived class, writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written. + /// Writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written. /// /// An array of bytes. This method copies bytes from to the current stream. /// The zero-based byte offset in at which to begin copying bytes to the current stream. @@ -409,8 +551,9 @@ public override void SetLength(long value) /// Methods were called after the stream was closed. public override void Write(byte[] buffer, int offset, int count) { - for (int i = 0; i < count; ++i) { - ringBuffer_.WriteByte(buffer[offset + i]); + for (int i = 0; i < count; ++i) + { + ringBuffer.WriteByte(buffer[offset + i]); } } @@ -418,39 +561,104 @@ public override void Write(byte[] buffer, int offset, int count) /// Gets a value indicating whether this instance is closed. /// /// true if this instance is closed; otherwise, false. - public bool IsClosed { - get { return ringBuffer_.IsClosed; } + public bool IsClosed + { + get { return ringBuffer.IsClosed; } } - /// - /// Closes the current stream and releases any resources (such as sockets and file handles) associated with the current stream. - /// - public override void Close() + /// Releases the unmanaged resources used by the and optionally releases the managed resources. + /// true to release both managed and unmanaged resources; false to release only unmanaged resources. + protected override void Dispose(bool disposing) { - ringBuffer_.Close(); + if (disposing && !ringBuffer.IsClosed) + { + ringBuffer.Close(); + } + base.Dispose(disposing); } /// /// Gets the bytes written. /// /// The bytes written. - public long BytesWritten { - get { return ringBuffer_.BytesWritten; } - } + public long BytesWritten => ringBuffer.BytesWritten; /// /// Gets the bytes read. /// /// The bytes read. - public long BytesRead { - get { return ringBuffer_.BytesRead; } + public long BytesRead => ringBuffer.BytesRead; + + #region Instance Fields + + private readonly ReadWriteRingBuffer ringBuffer; + + #endregion Instance Fields + } + + internal class SingleByteReadingStream : MemoryStream + { + /// + /// Initializes a new instance of the class. + /// + public SingleByteReadingStream() + { } - readonly + public override int Read(byte[] buffer, int offset, int count) + { + if (count > 0) + count = 1; - #region Instance Fields - ReadWriteRingBuffer ringBuffer_; + return base.Read(buffer, offset, count); + } + } - #endregion + /// + /// A stream that closes itself when all of its data is read. + /// + /// + /// Useful for testing issues such as https://github.com/icsharpcode/SharpZipLib/issues/379 + /// + internal class SelfClosingStream : MemoryStream + { + private bool isFullyRead = false; + + /// + /// Initializes a new instance of the class. + /// + public SelfClosingStream() + { + } + + // + public override int Read(byte[] buffer, int offset, int count) + { + var read = base.Read(buffer, offset, count); + + if (read == 0) + { + isFullyRead = true; + Close(); + } + + return read; + } + + /// + /// CanRead is false if we're closed, or base.CanRead otherwise. + /// + public override bool CanRead + { + get + { + if (isFullyRead) + { + return false; + } + + return base.CanRead; + } + } } } diff --git a/test/ICSharpCode.SharpZipLib.Tests/TestSupport/StringTesting.cs b/test/ICSharpCode.SharpZipLib.Tests/TestSupport/StringTesting.cs new file mode 100644 index 000000000..e1d7a1fb0 --- /dev/null +++ b/test/ICSharpCode.SharpZipLib.Tests/TestSupport/StringTesting.cs @@ -0,0 +1,26 @@ +using System.Collections.Generic; +using System.Linq; + +namespace ICSharpCode.SharpZipLib.Tests.TestSupport +{ + public static class StringTesting + { + static StringTesting() + { + TestSamples = new [] + { + ("Chinese", "測試.txt", "big5"), + ("Greek", "Ϗΰ.txt", "windows-1253"), + ("Nordic", "Åæ.txt", "windows-1252"), + ("Arabic", "ڀڅ.txt", "windows-1256"), + ("Russian", "Прйвёт.txt", "windows-1251"), + }; + } + + public static (string language, string filename, string encoding)[] TestSamples { get; } + + public static IEnumerable Languages => TestSamples.Select(s => s.language); + public static IEnumerable Filenames => TestSamples.Select(s => s.filename); + public static IEnumerable Encodings => TestSamples.Select(s => s.encoding); + } +} diff --git a/test/ICSharpCode.SharpZipLib.Tests/TestSupport/ThreadEx.cs b/test/ICSharpCode.SharpZipLib.Tests/TestSupport/ThreadEx.cs new file mode 100644 index 000000000..f57a094be --- /dev/null +++ b/test/ICSharpCode.SharpZipLib.Tests/TestSupport/ThreadEx.cs @@ -0,0 +1,20 @@ +using System; +using System.Threading; +using System.Reflection; + +public class ThreadEx +{ + public static void Abort(Thread thread) + { + MethodInfo abort = null; + foreach(MethodInfo m in thread.GetType().GetMethods(BindingFlags.NonPublic | BindingFlags.Instance)) + { + if (m.Name.Equals("AbortInternal") && m.GetParameters().Length == 0) abort = m; + } + if (abort == null) + { + throw new Exception("Failed to get Thread.Abort method"); + } + abort.Invoke(thread, new object[0]); + } +} \ No newline at end of file diff --git a/test/ICSharpCode.SharpZipLib.Tests/TestSupport/Utils.cs b/test/ICSharpCode.SharpZipLib.Tests/TestSupport/Utils.cs index c8cb5d28d..ca1838500 100644 --- a/test/ICSharpCode.SharpZipLib.Tests/TestSupport/Utils.cs +++ b/test/ICSharpCode.SharpZipLib.Tests/TestSupport/Utils.cs @@ -1,5 +1,11 @@ -using System; using NUnit.Framework; +using System; +using System.Diagnostics; +using System.IO; +using System.Text; +using ICSharpCode.SharpZipLib.Tests.Zip; +using System.Linq; +using System.Threading.Tasks; namespace ICSharpCode.SharpZipLib.Tests.TestSupport { @@ -8,22 +14,273 @@ namespace ICSharpCode.SharpZipLib.Tests.TestSupport /// public static class Utils { + public static int DummyContentLength = 16; - static void Compare(byte[] a, byte[] b) + internal const int DefaultSeed = 5; + private static Random random = new Random(DefaultSeed); + + /// + /// Returns the system root for the current platform (usually c:\ for windows and / for others) + /// + public static string SystemRoot { get; } = + Path.GetPathRoot(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData)); + + private static void Compare(byte[] a, byte[] b) { - if (a == null) { + + if (a == null) + { throw new ArgumentNullException(nameof(a)); } - if (b == null) { + if (b == null) + { throw new ArgumentNullException(nameof(b)); } Assert.AreEqual(a.Length, b.Length); - for (int i = 0; i < a.Length; ++i) { + for (int i = 0; i < a.Length; ++i) + { Assert.AreEqual(a[i], b[i]); } } + /// + /// Write pseudo-random data to , + /// creating it if it does not exist or truncating it otherwise + /// + /// + /// + /// + public static void WriteDummyData(string fileName, int size, int seed = DefaultSeed) + { + using var fs = File.Create(fileName); + WriteDummyData(fs, size, seed); + } + + /// + /// Write pseudo-random data to + /// + /// + /// + /// + public static void WriteDummyData(Stream stream, int size, int seed = DefaultSeed) + { + var bytes = GetDummyBytes(size, seed); + stream.Write(bytes, offset: 0, bytes.Length); + } + + /// + /// Creates a buffer of pseudo-random bytes + /// + /// + /// + /// + public static byte[] GetDummyBytes(int size, int seed = DefaultSeed) + { + var random = new Random(seed); + var bytes = new byte[size]; + random.NextBytes(bytes); + return bytes; + } + + public static async Task WriteDummyDataAsync(Stream stream, int size = -1) + { + var bytes = GetDummyBytes(size); + await stream.WriteAsync(bytes, 0, bytes.Length); + } + + /// + /// Returns a file reference with bytes of dummy data written to it + /// + /// + /// + public static TempFile GetDummyFile(int size = 16) + { + var tempFile = new TempFile(); + using var fs = tempFile.Create(); + WriteDummyData(fs, size); + return tempFile; + } + + /// + /// Returns a randomized file/directory name (without any path) using a generated GUID + /// + /// + public static string GetDummyFileName() + => string.Concat(Guid.NewGuid().ToByteArray().Select(b => $"{b:x2}")); + + /// + /// Returns a reference to a temporary directory that deletes it's contents when disposed + /// + /// + public static TempDir GetTempDir() => new TempDir(); + + /// + /// Returns a reference to a temporary file that deletes it's referred file when disposed + /// + /// + public static TempFile GetTempFile() => new TempFile(); + + public static void PatchFirstEntrySize(Stream stream, int newSize) + { + using(stream) + { + var sizeBytes = BitConverter.GetBytes(newSize); + + stream.Seek(18, SeekOrigin.Begin); + stream.Write(sizeBytes, 0, 4); + stream.Write(sizeBytes, 0, 4); + } + } + + public static void FillArray(byte[] buffer, byte value) + { +#if NET6_0_OR_GREATER + Array.Fill(buffer, value); +#else + for(var i = 0; i < buffer.Length; i++) buffer[i] = value; +#endif + } } + + public class TestTraceListener : TraceListener + { + private readonly TextWriter _writer; + public TestTraceListener(TextWriter writer) + { + _writer = writer; + } + + public override void WriteLine(string message) => _writer.WriteLine(message); + public override void Write(string message) => _writer.Write(message); + } + + public class TempFile : FileSystemInfo, IDisposable + { + private FileInfo _fileInfo; + + public override string Name => _fileInfo.Name; + public override bool Exists => _fileInfo.Exists; + public string DirectoryName => _fileInfo.DirectoryName; + + public override string FullName => _fileInfo.FullName; + + public byte[] ReadAllBytes() => File.ReadAllBytes(_fileInfo.FullName); + + public static implicit operator string(TempFile tf) => tf._fileInfo.FullName; + + public override void Delete() + { + if(!Exists) return; + _fileInfo.Delete(); + } + + public FileStream Open(FileMode mode, FileAccess access) => _fileInfo.Open(mode, access); + public FileStream Open(FileMode mode) => _fileInfo.Open(mode); + public FileStream Create() => _fileInfo.Create(); + + public static TempFile WithDummyData(int size, string dirPath = null, string filename = null, int seed = Utils.DefaultSeed) + { + var tempFile = new TempFile(dirPath, filename); + Utils.WriteDummyData(tempFile.FullName, size, seed); + return tempFile; + } + + internal TempFile(string dirPath = null, string filename = null) + { + dirPath ??= Path.GetTempPath(); + filename ??= Utils.GetDummyFileName(); + _fileInfo = new FileInfo(Path.Combine(dirPath, filename)); + } + +#region IDisposable Support + + private bool _disposed; // To detect redundant calls + + protected virtual void Dispose(bool disposing) + { + if (_disposed) return; + if (disposing) + { + try + { + Delete(); + } + catch + { + // ignored + } + } + + _disposed = true; + } + + public void Dispose() + { + Dispose(disposing: true); + GC.SuppressFinalize(this); + } + +#endregion IDisposable Support + } + + + + public class TempDir : FileSystemInfo, IDisposable + { + public override string Name => Path.GetFileName(FullName); + public override bool Exists => Directory.Exists(FullName); + + public static implicit operator string(TempDir td) => td.FullName; + + public override void Delete() + { + if(!Exists) return; + Directory.Delete(FullPath, recursive: true); + } + + public TempDir() + { + FullPath = Path.Combine(Path.GetTempPath(), Utils.GetDummyFileName()); + Directory.CreateDirectory(FullPath); + } + + public TempFile CreateDummyFile(int size = 16, int seed = Utils.DefaultSeed) + => CreateDummyFile(null, size); + + public TempFile CreateDummyFile(string name, int size = 16, int seed = Utils.DefaultSeed) + => TempFile.WithDummyData(size, FullPath, name, seed); + + public TempFile GetFile(string fileName) => new TempFile(FullPath, fileName); + +#region IDisposable Support + + private bool _disposed; // To detect redundant calls + + protected virtual void Dispose(bool disposing) + { + if (_disposed) return; + if (disposing) + { + try + { + Delete(); + } + catch + { + // ignored + } + } + _disposed = true; + } + + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + +#endregion IDisposable Support + } } diff --git a/test/ICSharpCode.SharpZipLib.Tests/TestSupport/ZipTesting.cs b/test/ICSharpCode.SharpZipLib.Tests/TestSupport/ZipTesting.cs index 8bc44777a..7311da7a2 100644 --- a/test/ICSharpCode.SharpZipLib.Tests/TestSupport/ZipTesting.cs +++ b/test/ICSharpCode.SharpZipLib.Tests/TestSupport/ZipTesting.cs @@ -1,36 +1,123 @@ -using System.IO; using ICSharpCode.SharpZipLib.Zip; +using NUnit.Framework.Constraints; +using NUnit.Framework; +using System.Collections.Generic; +using System.IO; +using System.Linq; namespace ICSharpCode.SharpZipLib.Tests.TestSupport { /// /// Provides support for testing in memory zip archives. /// - static class ZipTesting + internal static class ZipTesting + { + public static void AssertValidZip(Stream stream, string password = null, bool usesAes = true) + { + using var zipFile = new ZipFile(stream) + { + IsStreamOwner = false, + Password = password, + }; + + Assert.That(zipFile, Does.PassTestArchive()); + + if (!string.IsNullOrEmpty(password) && usesAes) + { + Assert.Ignore("ZipInputStream does not support AES"); + } + + stream.Seek(0, SeekOrigin.Begin); + + Assert.DoesNotThrow(() => + { + using var zis = new ZipInputStream(stream){Password = password}; + while (zis.GetNextEntry() != null) + { + new StreamReader(zis).ReadToEnd(); + } + }, "Archive could not be read by ZipInputStream"); + } + } + + public class TestArchiveReport { - /// - /// Tests the archive. - /// - /// The data. - /// - public static bool TestArchive(byte[] data) + internal const string PassingArchive = "Passing Archive"; + + readonly List _messages = new List(); + public void HandleTestResults(TestStatus status, string message) { - return TestArchive(data, null); + if (string.IsNullOrWhiteSpace(message)) return; + _messages.Add(message); } - /// - /// Tests the archive. - /// - /// The data. - /// The password. - /// true if archive tests ok; false otherwise. - public static bool TestArchive(byte[] data, string password) + public override string ToString() => _messages.Any() ? string.Join(", ", _messages) : PassingArchive; + } + + public class PassesTestArchiveConstraint : Constraint + { + private readonly string _password; + private readonly bool _testData; + + public PassesTestArchiveConstraint(string password = null, bool testData = true) { - using (MemoryStream ms = new MemoryStream(data)) - using (ZipFile zipFile = new ZipFile(ms)) { - zipFile.Password = password; - return zipFile.TestArchive(true); + _password = password; + _testData = testData; + } + + public override string Description => TestArchiveReport.PassingArchive; + + public override ConstraintResult ApplyTo(TActual actual) + { + MemoryStream ms = null; + try + { + if (!(actual is ZipFile zipFile)) + { + if (!(actual is byte[] rawArchive)) + { + return new ConstraintResult(this, actual, ConstraintStatus.Failure); + } + + ms = new MemoryStream(rawArchive); + zipFile = new ZipFile(ms){Password = _password}; + } + + var report = new TestArchiveReport(); + + return new ConstraintResult( + this, report, zipFile.TestArchive( + _testData, + TestStrategy.FindAllErrors, + report.HandleTestResults + ) + ? ConstraintStatus.Success + : ConstraintStatus.Failure); + } + finally + { + ms?.Dispose(); } } } + + public static class ZipTestingConstraintExtensions + { + public static IResolveConstraint PassTestArchive(this ConstraintExpression expression, string password = null, bool testData = true) + { + var constraint = new PassesTestArchiveConstraint(password, testData); + expression.Append(constraint); + return constraint; + } + } + + /// + public class Does: NUnit.Framework.Does + { + public static IResolveConstraint PassTestArchive(string password = null, bool testData = true) + => new PassesTestArchiveConstraint(password, testData); + + public static IResolveConstraint PassTestArchive(bool testData) + => new PassesTestArchiveConstraint(password: null, testData); + } } diff --git a/test/ICSharpCode.SharpZipLib.Tests/Zip/FastZipHandling.cs b/test/ICSharpCode.SharpZipLib.Tests/Zip/FastZipHandling.cs index 8044b6dd2..3858f38f8 100644 --- a/test/ICSharpCode.SharpZipLib.Tests/Zip/FastZipHandling.cs +++ b/test/ICSharpCode.SharpZipLib.Tests/Zip/FastZipHandling.cs @@ -1,7 +1,13 @@ -using System.IO; -using System.Text.RegularExpressions; +using ICSharpCode.SharpZipLib.Tests.TestSupport; using ICSharpCode.SharpZipLib.Zip; using NUnit.Framework; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using Does = ICSharpCode.SharpZipLib.Tests.TestSupport.Does; +using TimeSetting = ICSharpCode.SharpZipLib.Zip.ZipEntryFactory.TimeSetting; namespace ICSharpCode.SharpZipLib.Tests.Zip { @@ -23,32 +29,37 @@ public void Basics() string addFile = Path.Combine(tempFilePath, tempName1); MakeTempFile(addFile, 1); - try { + try + { var fastZip = new FastZip(); fastZip.CreateZip(target, tempFilePath, false, @"a\(1\)\.dat", null); var archive = new MemoryStream(target.ToArray()); - using (ZipFile zf = new ZipFile(archive)) { + using (ZipFile zf = new ZipFile(archive)) + { Assert.AreEqual(1, zf.Count); ZipEntry entry = zf[0]; Assert.AreEqual(tempName1, entry.Name); Assert.AreEqual(1, entry.Size); - Assert.IsTrue(zf.TestArchive(true)); + Assert.That(zf, Does.PassTestArchive()); zf.Close(); } - } finally { + } + finally + { File.Delete(tempName1); } } - const string ZipTempDir = "SharpZipLibTest"; + private const string ZipTempDir = "SharpZipLibTest"; - void EnsureTestDirectoryIsEmpty(string baseDir) + private void EnsureTestDirectoryIsEmpty(string baseDir) { string name = Path.Combine(baseDir, ZipTempDir); - if (Directory.Exists(name)) { + if (Directory.Exists(name)) + { Directory.Delete(name, true); } @@ -68,8 +79,10 @@ public void ExtractEmptyDirectories() EnsureTestDirectoryIsEmpty(tempFilePath); string targetDir = Path.Combine(tempFilePath, ZipTempDir + @"\floyd"); - using (FileStream fs = File.Create(name)) { - using (ZipOutputStream zOut = new ZipOutputStream(fs)) { + using (FileStream fs = File.Create(name)) + { + using (ZipOutputStream zOut = new ZipOutputStream(fs)) + { zOut.PutNextEntry(new ZipEntry("floyd/")); } } @@ -82,11 +95,79 @@ public void ExtractEmptyDirectories() Assert.IsTrue(Directory.Exists(targetDir), "Empty directory should be created"); } + /// + /// Test that FastZip can create empty directory entries in archives. + /// + [TestCase(null)] + [TestCase("password")] + [Category("Zip")] + [Category("CreatesTempFile")] + public void CreateEmptyDirectories(string password) + { + using (var tempFilePath = Utils.GetTempDir()) + { + string name = Path.Combine(tempFilePath.FullName, "x.zip"); + + // Create empty test folders (The folder that we'll zip, and the test sub folder). + string archiveRootDir = Path.Combine(tempFilePath.FullName, ZipTempDir); + string targetDir = Path.Combine(archiveRootDir, "floyd"); + Directory.CreateDirectory(targetDir); + + // Create the archive with FastZip + var fastZip = new FastZip + { + CreateEmptyDirectories = true, + Password = password, + }; + fastZip.CreateZip(name, archiveRootDir, recurse: true, fileFilter: null); + + // Test that the archive contains the empty folder entry + using (var zipFile = new ZipFile(name)) + { + Assert.That(zipFile.Count, Is.EqualTo(1), "Should only be one entry in the file"); + + var folderEntry = zipFile.GetEntry("floyd/"); + Assert.That(folderEntry.IsDirectory, Is.True, "The entry must be a folder"); + + Assert.That(zipFile, Does.PassTestArchive()); + } + } + } + + [Test] + [Category("Zip")] + [Category("CreatesTempFile")] + public void ContentEqualAfterAfterArchived([Values(0, 1, 64)]int contentSize) + { + using var sourceDir = Utils.GetTempDir(); + using var targetDir = Utils.GetTempDir(); + using var zipFile = Utils.GetTempFile(); + + var sourceFile = sourceDir.CreateDummyFile(contentSize); + var sourceContent = sourceFile.ReadAllBytes(); + new FastZip().CreateZip(zipFile.FullName, sourceDir.FullName, recurse: true, fileFilter: null); + + Assert.DoesNotThrow(() => + { + new FastZip().ExtractZip(zipFile, targetDir, fileFilter: null); + }, "Exception during extraction of test archive"); + + var targetFile = Path.Combine(targetDir, Path.GetFileName(sourceFile)); + var targetContent = File.ReadAllBytes(targetFile); + + Assert.AreEqual(sourceContent.Length, targetContent.Length, "Extracted file size does not match source file size"); + Assert.AreEqual(sourceContent, targetContent, "Extracted content does not match source content"); + } + [Test] + [TestCase(ZipEncryptionMethod.ZipCrypto)] + [TestCase(ZipEncryptionMethod.AES128)] + [TestCase(ZipEncryptionMethod.AES256)] [Category("Zip")] - public void Encryption() + public void Encryption(ZipEncryptionMethod encryptionMethod) { const string tempName1 = "a.dat"; + const int tempSize = 1; var target = new MemoryStream(); @@ -94,25 +175,48 @@ public void Encryption() Assert.IsNotNull(tempFilePath, "No permission to execute this test?"); string addFile = Path.Combine(tempFilePath, tempName1); - MakeTempFile(addFile, 1); + MakeTempFile(addFile, tempSize); - try { - var fastZip = new FastZip(); - fastZip.Password = "Ahoy"; + try + { + var fastZip = new FastZip + { + Password = "Ahoy", + EntryEncryptionMethod = encryptionMethod + }; fastZip.CreateZip(target, tempFilePath, false, @"a\.dat", null); var archive = new MemoryStream(target.ToArray()); - using (ZipFile zf = new ZipFile(archive)) { + using (ZipFile zf = new ZipFile(archive)) + { zf.Password = "Ahoy"; - Assert.AreEqual(1, zf.Count); - ZipEntry entry = zf[0]; - Assert.AreEqual(tempName1, entry.Name); - Assert.AreEqual(1, entry.Size); - Assert.IsTrue(zf.TestArchive(true)); - Assert.IsTrue(entry.IsCrypted); + Assert.That(zf.Count, Is.EqualTo(1)); + var entry = zf[0]; + Assert.That(entry.Name, Is.EqualTo(tempName1)); + Assert.That(entry.Size, Is.EqualTo(tempSize)); + Assert.That(entry.IsCrypted); + + Assert.That(zf, Does.PassTestArchive()); + + switch (encryptionMethod) + { + case ZipEncryptionMethod.ZipCrypto: + Assert.That(entry.AESKeySize, Is.Zero, "AES key size should be 0 for ZipCrypto encrypted entries"); + break; + + case ZipEncryptionMethod.AES128: + Assert.That(entry.AESKeySize, Is.EqualTo(128), "AES key size should be 128 for AES128 encrypted entries"); + break; + + case ZipEncryptionMethod.AES256: + Assert.That(entry.AESKeySize, Is.EqualTo(256), "AES key size should be 256 for AES256 encrypted entries"); + break; + } } - } finally { + } + finally + { File.Delete(tempName1); } } @@ -121,49 +225,100 @@ public void Encryption() [Category("Zip")] public void CreateExceptions() { - var fastZip = new FastZip(); - string tempFilePath = GetTempFilePath(); - Assert.IsNotNull(tempFilePath, "No permission to execute this test?"); - - Assert.Throws(() => + Assert.Throws(() => { - string addFile = Path.Combine(tempFilePath, "test.zip"); - try { - fastZip.CreateZip(addFile, @"z:\doesnt exist", false, null); - } finally { - File.Delete(addFile); - } + using var tempDir = Utils.GetTempDir(); + var fastZip = new FastZip(); + var badPath = Path.Combine(Path.GetTempPath(), Utils.GetDummyFileName()); + var addFile = tempDir.GetFile("test.zip"); + fastZip.CreateZip(addFile, badPath, recurse: false, fileFilter: null); }); } - [Test] - [Category("Zip")] - public void UnicodeText() + #region String testing helper + + private void TestFileNames(int codePage, IReadOnlyList names) { var zippy = new FastZip(); - var factory = new ZipEntryFactory(); - factory.IsUnicodeText = true; - zippy.EntryFactory = factory; + if (codePage > 0) + { + zippy.UseUnicode = false; + zippy.LegacyCodePage = codePage; + } - string tempFilePath = GetTempFilePath(); - Assert.IsNotNull(tempFilePath, "No permission to execute this test?"); + using var tempDir = Utils.GetTempDir(); + using var tempZip = Utils.GetTempFile(); + int nameCount = 0; + foreach (var name in names) + { + tempDir.CreateDummyFile(name); + nameCount++; + } - const string tempName1 = "a.dat"; - string addFile = Path.Combine(tempFilePath, tempName1); - MakeTempFile(addFile, 1); + zippy.CreateZip(tempZip, tempDir, recurse: true, fileFilter: null); - try { - var target = new MemoryStream(); - zippy.CreateZip(target, tempFilePath, false, Regex.Escape(tempName1), null); + using var zf = new ZipFile(tempZip, zippy.StringCodec); + Assert.AreEqual(nameCount, zf.Count); + foreach (var name in names) + { + var index = zf.FindEntry(name, ignoreCase: true); - var archive = new MemoryStream(target.ToArray()); + Assert.AreNotEqual(expected: -1, index, "Zip entry \"{0}\" not found", name); - using (ZipFile z = new ZipFile(archive)) { - Assert.AreEqual(1, z.Count); - Assert.IsTrue(z[0].IsUnicodeText); + var entry = zf[index]; + + if (zippy.UseUnicode) + { + Assert.IsTrue(entry.IsUnicodeText, "Zip entry #{0} not marked as unicode", index); } - } finally { - File.Delete(addFile); + else + { + Assert.IsFalse(entry.IsUnicodeText, "Zip entry #{0} marked as unicode", index); + } + + Assert.AreEqual(name, entry.Name); + + var nameBytes = string.Join(" ", Encoding.BigEndianUnicode.GetBytes(entry.Name).Select(b => b.ToString("x2"))); + + Console.WriteLine($" - Zip entry: {entry.Name} ({nameBytes})"); + } + } + + #endregion String testing helper + + [Test] + [Category("Zip")] + [Category("Unicode")] + public void UnicodeText() + { + TestFileNames(0, StringTesting.Filenames.ToArray()); + } + + [Test] + [Category("Zip")] + [Category("Unicode")] + public void NonUnicodeText() + { + Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); + + foreach (var (language, filename, encoding) in StringTesting.TestSamples) + { + Console.WriteLine($"{language} filename \"{filename}\" using \"{encoding}\":"); + + // TODO: samples of this test must be reversible + // Some samples can't be restored back with their encoding. + // test wasn't failing only because SystemDefaultCodepage is 65001 on Net.Core and + // old behaviour actually was using Unicode instead of user's passed codepage + var encoder = Encoding.GetEncoding(encoding); + var bytes = encoder.GetBytes(filename); + var restoredString = encoder.GetString(bytes); + if(string.CompareOrdinal(filename, restoredString) != 0) + { + Console.WriteLine($"Sample for language {language} with value of {filename} is skipped, because it's irreversable"); + continue; + } + + TestFileNames(Encoding.GetEncoding(encoding).CodePage, new [] { filename }); } } @@ -176,9 +331,12 @@ public void ExtractExceptions() Assert.IsNotNull(tempFilePath, "No permission to execute this test?"); string addFile = Path.Combine(tempFilePath, "test.zip"); - try { + try + { Assert.Throws(() => fastZip.ExtractZip(addFile, @"z:\doesnt exist", null)); - } finally { + } + finally + { File.Delete(addFile); } } @@ -197,12 +355,13 @@ public void ExtractExceptions() * but doing so would make FastZip work with locked files (that are potentially written to by others) * and silently ignoring any locks. HOWEVER: This can lead to corrupt/incomplete files, which is why it * should not be the default behavior. - * + * * Therefore I would remove this test. **/ public void ReadingOfLockedDataFiles() { const string tempName1 = "a.dat"; + const int tempSize = 1; var target = new MemoryStream(); @@ -210,26 +369,31 @@ public void ReadingOfLockedDataFiles() Assert.IsNotNull(tempFilePath, "No permission to execute this test?"); string addFile = Path.Combine(tempFilePath, tempName1); - MakeTempFile(addFile, 1); + MakeTempFile(addFile, tempSize); - try { + try + { var fastZip = new FastZip(); - using (File.Open(addFile, FileMode.Open, FileAccess.Write, FileShare.ReadWrite)) { + using (File.Open(addFile, FileMode.Open, FileAccess.Write, FileShare.ReadWrite)) + { fastZip.CreateZip(target, tempFilePath, false, @"a\.dat", null); var archive = new MemoryStream(target.ToArray()); - using (ZipFile zf = new ZipFile(archive)) { - Assert.AreEqual(1, zf.Count); - ZipEntry entry = zf[0]; - Assert.AreEqual(tempName1, entry.Name); - Assert.AreEqual(1, entry.Size); - Assert.IsTrue(zf.TestArchive(true)); + using (ZipFile zf = new ZipFile(archive)) + { + Assert.That(zf.Count, Is.EqualTo(1)); + var entry = zf[0]; + Assert.That(entry.Name, Is.EqualTo(tempName1)); + Assert.That(entry.Size, Is.EqualTo(tempSize)); + Assert.That(zf, Does.PassTestArchive()); zf.Close(); } } - } finally { + } + finally + { File.Delete(tempName1); } } @@ -239,6 +403,7 @@ public void ReadingOfLockedDataFiles() public void NonAsciiPasswords() { const string tempName1 = "a.dat"; + const int tempSize = 1; var target = new MemoryStream(); @@ -246,28 +411,425 @@ public void NonAsciiPasswords() Assert.IsNotNull(tempFilePath, "No permission to execute this test?"); string addFile = Path.Combine(tempFilePath, tempName1); - MakeTempFile(addFile, 1); + MakeTempFile(addFile, tempSize); string password = "abc\u0066\u0393"; - try { + try + { var fastZip = new FastZip(); fastZip.Password = password; fastZip.CreateZip(target, tempFilePath, false, @"a\.dat", null); var archive = new MemoryStream(target.ToArray()); - using (ZipFile zf = new ZipFile(archive)) { + using (ZipFile zf = new ZipFile(archive)) + { zf.Password = password; - Assert.AreEqual(1, zf.Count); - ZipEntry entry = zf[0]; - Assert.AreEqual(tempName1, entry.Name); - Assert.AreEqual(1, entry.Size); - Assert.IsTrue(zf.TestArchive(true)); - Assert.IsTrue(entry.IsCrypted); + Assert.That(zf.Count, Is.EqualTo(1)); + var entry = zf[0]; + Assert.That(entry.Name, Is.EqualTo(tempName1)); + Assert.That(entry.Size, Is.EqualTo(tempSize)); + Assert.That(zf, Does.PassTestArchive()); + Assert.That(entry.IsCrypted); } - } finally { + } + finally + { File.Delete(tempName1); } } + + [Test] + [Category("Zip")] + [Category("CreatesTempFile")] + public void LimitExtractPath() + { + string tempPath = GetTempFilePath(); + Assert.IsNotNull(tempPath, "No permission to execute this test?"); + + var uniqueName = "SharpZipLib.Test_" + DateTime.Now.Ticks.ToString("x"); + + tempPath = Path.Combine(tempPath, uniqueName); + var extractPath = Path.Combine(tempPath, "output"); + + const string contentFile = "output.txt"; + + var contentFilePathBad = Path.Combine("..", contentFile); + var extractFilePathBad = Path.Combine(tempPath, contentFile); + var archiveFileBad = Path.Combine(tempPath, "test-good.zip"); + + var contentFilePathGood = Path.Combine("childDir", contentFile); + var extractFilePathGood = Path.Combine(extractPath, contentFilePathGood); + var archiveFileGood = Path.Combine(tempPath, "test-bad.zip"); + + try + { + Directory.CreateDirectory(extractPath); + + // Create test input + void CreateTestFile(string archiveFile, string contentPath) + { + using (var zf = ZipFile.Create(archiveFile)) + { + zf.BeginUpdate(); + zf.Add(new StringMemoryDataSource($"Content of {archiveFile}"), contentPath); + zf.CommitUpdate(); + } + } + + CreateTestFile(archiveFileGood, contentFilePathGood); + CreateTestFile(archiveFileBad, contentFilePathBad); + + Assert.IsTrue(File.Exists(archiveFileGood), "Good test archive was not created"); + Assert.IsTrue(File.Exists(archiveFileBad), "Bad test archive was not created"); + + var fastZip = new FastZip(); + + Assert.DoesNotThrow(() => + { + fastZip.ExtractZip(archiveFileGood, extractPath, ""); + }, "Threw exception on good file name"); + + Assert.IsTrue(File.Exists(extractFilePathGood), "Good output file not created"); + + Assert.Throws(() => + { + fastZip.ExtractZip(archiveFileBad, extractPath, ""); + }, "No exception was thrown for bad file name"); + + Assert.IsFalse(File.Exists(extractFilePathBad), "Bad output file created"); + + Assert.DoesNotThrow(() => + { + fastZip.ExtractZip(archiveFileBad, extractPath, FastZip.Overwrite.Never, null, "", "", true, true); + }, "Threw exception on bad file name when traversal explicitly allowed"); + + Assert.IsTrue(File.Exists(extractFilePathBad), "Bad output file not created when traversal explicitly allowed"); + } + finally + { + Directory.Delete(tempPath, true); + } + } + + /// + /// Check that the input stream is not closed on error when isStreamOwner is false + /// + [Test] + public void StreamNotClosedOnError() + { + // test paths + string tempFilePath = GetTempFilePath(); + Assert.IsNotNull(tempFilePath, "No permission to execute this test?"); + + var tempFolderPath = Path.Combine(tempFilePath, Path.GetRandomFileName()); + Assert.That(Directory.Exists(tempFolderPath), Is.False, "Temp folder path should not exist"); + + // memory that isn't a valid zip + var ms = new TrackedMemoryStream(new byte[32]); + Assert.IsFalse(ms.IsClosed, "Underlying stream should NOT be closed initially"); + + // Try to extract + var fastZip = new FastZip(); + fastZip.CreateEmptyDirectories = true; + + Assert.Throws(() => fastZip.ExtractZip(ms, tempFolderPath, FastZip.Overwrite.Always, null, "a", "b", false, false), "Should throw when extracting an invalid file"); + Assert.IsFalse(ms.IsClosed, "inputStream stream should NOT be closed when isStreamOwner is false"); + + // test folder should not have been created on error + Assert.That(Directory.Exists(tempFolderPath), Is.False, "Temp folder path should still not exist"); + } + + /// + /// Check that the input stream is closed on error when isStreamOwner is true + /// + [Test] + public void StreamClosedOnError() + { + // test paths + string tempFilePath = GetTempFilePath(); + Assert.IsNotNull(tempFilePath, "No permission to execute this test?"); + + var tempFolderPath = Path.Combine(tempFilePath, Path.GetRandomFileName()); + Assert.That(Directory.Exists(tempFolderPath), Is.False, "Temp folder path should not exist"); + + // memory that isn't a valid zip + var ms = new TrackedMemoryStream(new byte[32]); + Assert.IsFalse(ms.IsClosed, "Underlying stream should NOT be closed initially"); + + // Try to extract + var fastZip = new FastZip(); + fastZip.CreateEmptyDirectories = true; + + Assert.Throws(() => fastZip.ExtractZip(ms, tempFolderPath, FastZip.Overwrite.Always, null, "a", "b", false, true), "Should throw when extracting an invalid file"); + Assert.IsTrue(ms.IsClosed, "inputStream stream should be closed when isStreamOwner is true"); + + // test folder should not have been created on error + Assert.That(Directory.Exists(tempFolderPath), Is.False, "Temp folder path should still not exist"); + } + + /// + /// #426 - set the modified date for created directory entries if the RestoreDateTimeOnExtract option is enabled + /// + [Test] + [Category("Zip")] + [Category("CreatesTempFile")] + public void SetDirectoryModifiedDate() + { + string tempFilePath = GetTempFilePath(); + Assert.IsNotNull(tempFilePath, "No permission to execute this test?"); + + string zipName = Path.Combine(tempFilePath, $"{nameof(SetDirectoryModifiedDate)}.zip"); + + EnsureTestDirectoryIsEmpty(tempFilePath); + + var modifiedTime = new DateTime(2001, 1, 2); + string targetDir = Path.Combine(tempFilePath, ZipTempDir, nameof(SetDirectoryModifiedDate)); + using (FileStream fs = File.Create(zipName)) + { + using (ZipOutputStream zOut = new ZipOutputStream(fs)) + { + // Add an empty directory entry, with a specified time field + var entry = new ZipEntry("emptyFolder/") + { + DateTime = modifiedTime + }; + zOut.PutNextEntry(entry); + } + } + + try + { + // extract the zip + var fastZip = new FastZip + { + CreateEmptyDirectories = true, + RestoreDateTimeOnExtract = true + }; + fastZip.ExtractZip(zipName, targetDir, "zz"); + + File.Delete(zipName); + + // Check that the empty sub folder exists and has the expected modlfied date + string emptyTargetDir = Path.Combine(targetDir, "emptyFolder"); + + Assert.That(Directory.Exists(emptyTargetDir), Is.True, "Empty directory should be created"); + + var extractedFolderTime = Directory.GetLastWriteTime(emptyTargetDir); + Assert.That(extractedFolderTime, Is.EqualTo(modifiedTime)); + } + finally + { + // Tidy up + Directory.Delete(targetDir, true); + } + } + + /// + /// Test for https://github.com/icsharpcode/SharpZipLib/issues/78 + /// + /// if true, the stream given to CreateZip should be left open, if false it should be disposed. + [TestCase(true)] + [TestCase(false)] + [Category("Zip")] + [Category("CreatesTempFile")] + public void CreateZipShouldLeaveOutputStreamOpenIfRequested(bool leaveOpen) + { + const string tempFileName = "a(2).dat"; + const int tempSize = 16; + + using var tempFolder = Utils.GetTempDir(); + // Create test input file + tempFolder.CreateDummyFile(tempFileName, tempSize); + + // Create the zip with fast zip + var target = new TrackedMemoryStream(); + var fastZip = new FastZip(); + + fastZip.CreateZip(target, tempFolder, recurse: false, @"a\(2\)\.dat", directoryFilter: null, leaveOpen); + + // Check that the output stream was disposed (or not) as expected + Assert.That(target.IsDisposed, Is.Not.EqualTo(leaveOpen), "IsDisposed should be the opposite of leaveOpen"); + + // Check that the file contents are correct in both cases + var archive = new MemoryStream(target.ToArray()); + using var zf = new ZipFile(archive); + Assert.That(zf.Count, Is.EqualTo(1)); + var entry = zf[0]; + Assert.That(entry.Name, Is.EqualTo(tempFileName)); + Assert.That(entry.Size, Is.EqualTo(tempSize)); + Assert.That(zf, Does.PassTestArchive()); + } + + [Category("Zip")] + [Category("CreatesTempFile")] + [Test] + public void CreateZipShouldSetTimeOnEntriesFromConstructorDateTime() + { + var targetTime = TestTargetTime(TimeSetting.Fixed); + var fastZip = new FastZip(targetTime); + var target = CreateFastZipTestArchiveWithAnEntry(fastZip); + var archive = new MemoryStream(target.ToArray()); + using (var zf = new ZipFile(archive)) + { + Assert.AreEqual(targetTime, zf[0].DateTime); + } + } + + [Category("Zip")] + [Category("CreatesTempFile")] + [TestCase(TimeSetting.CreateTimeUtc), TestCase(TimeSetting.LastWriteTimeUtc), TestCase(TimeSetting.LastAccessTimeUtc)] + [TestCase(TimeSetting.CreateTime), TestCase(TimeSetting.LastWriteTime), TestCase(TimeSetting.LastAccessTime)] + public void CreateZipShouldSetTimeOnEntriesFromConstructorTimeSetting(TimeSetting timeSetting) + { + var targetTime = TestTargetTime(timeSetting); + var fastZip = new FastZip(timeSetting); + + var alterTime = (Action) null; + switch(timeSetting) + { + case TimeSetting.LastWriteTime: alterTime = fi => fi.LastWriteTime = targetTime; break; + case TimeSetting.LastWriteTimeUtc: alterTime = fi => fi.LastWriteTimeUtc = targetTime; break; + case TimeSetting.CreateTime: alterTime = fi => fi.CreationTime = targetTime; break; + case TimeSetting.CreateTimeUtc: alterTime = fi => fi.CreationTimeUtc = targetTime; break; + } + + var target = CreateFastZipTestArchiveWithAnEntry(fastZip, alterTime); + // Check that the file contents are correct in both cases + var archive = new MemoryStream(target.ToArray()); + using (var zf = new ZipFile(archive)) + { + var expectedTime = TestTargetTime(timeSetting); + var actualTime = zf[0].DateTime; + // Assert that the time is within +/- 2s of the target time to allow for timing/rounding discrepancies + Assert.LessOrEqual(Math.Abs((expectedTime - actualTime).TotalSeconds), 2); + } + } + + [Category("Zip")] + [Category("CreatesTempFile")] + [TestCase(TimeSetting.CreateTimeUtc), TestCase(TimeSetting.LastWriteTimeUtc), TestCase(TimeSetting.LastAccessTimeUtc)] + [TestCase(TimeSetting.CreateTime), TestCase(TimeSetting.LastWriteTime), TestCase(TimeSetting.LastAccessTime)] + [TestCase(TimeSetting.Fixed)] + public void ExtractZipShouldSetTimeOnFilesFromConstructorTimeSetting(TimeSetting timeSetting) + { + var targetTime = ExpectedFixedTime(); + var archiveStream = CreateFastZipTestArchiveWithAnEntry(new FastZip(targetTime)); + + if (timeSetting == TimeSetting.Fixed) + { + Assert.Ignore("Fixed time without specifying a time is undefined"); + } + + var fastZip = new FastZip(timeSetting); + using var extractDir = Utils.GetTempDir(); + fastZip.ExtractZip(archiveStream, extractDir.FullName, FastZip.Overwrite.Always, + _ => true, "", "", restoreDateTime: true, isStreamOwner: true, allowParentTraversal: false); + var fi = new FileInfo(Path.Combine(extractDir.FullName, SingleEntryFileName)); + var actualTime = FileTimeFromTimeSetting(fi, timeSetting); + // Assert that the time is within +/- 2s of the target time to allow for timing/rounding discrepancies + Assert.LessOrEqual(Math.Abs((targetTime - actualTime).TotalSeconds), 2); + } + + [Category("Zip")] + [Category("CreatesTempFile")] + [TestCase(DateTimeKind.Local), TestCase(DateTimeKind.Utc)] + public void ExtractZipShouldSetTimeOnFilesFromConstructorDateTime(DateTimeKind dtk) + { + // Create the archive with a fixed "bad" datetime + var target = CreateFastZipTestArchiveWithAnEntry(new FastZip(UnexpectedFixedTime(dtk))); + + // Extract the archive with a fixed time override + var targetTime = ExpectedFixedTime(dtk); + var fastZip = new FastZip(targetTime); + using var extractDir = Utils.GetTempDir(); + fastZip.ExtractZip(target, extractDir.FullName, FastZip.Overwrite.Always, + _ => true, "", "", restoreDateTime: true, isStreamOwner: true, allowParentTraversal: false); + var fi = new FileInfo(Path.Combine(extractDir.FullName, SingleEntryFileName)); + var fileTime = FileTimeFromTimeSetting(fi, TimeSetting.Fixed); + if (fileTime.Kind != dtk) fileTime = fileTime.ToUniversalTime(); + Assert.AreEqual(targetTime, fileTime); + } + + [Category("Zip")] + [Category("CreatesTempFile")] + [TestCase(DateTimeKind.Local), TestCase(DateTimeKind.Utc)] + public void ExtractZipShouldSetTimeOnFilesWithEmptyConstructor(DateTimeKind dtk) + { + // Create the archive with a fixed datetime + var targetTime = ExpectedFixedTime(dtk); + var target = CreateFastZipTestArchiveWithAnEntry(new FastZip(targetTime)); + + // Extract the archive with an empty constructor + var fastZip = new FastZip(); + using var extractDir = Utils.GetTempDir(); + fastZip.ExtractZip(target, extractDir.FullName, FastZip.Overwrite.Always, + _ => true, "", "", restoreDateTime: true, isStreamOwner: true, allowParentTraversal: false); + var fi = new FileInfo(Path.Combine(extractDir.FullName, SingleEntryFileName)); + Assert.AreEqual(targetTime, FileTimeFromTimeSetting(fi, TimeSetting.Fixed)); + } + + private static bool IsLastAccessTime(TimeSetting ts) + => ts == TimeSetting.LastAccessTime || ts == TimeSetting.LastAccessTimeUtc; + + private static DateTime FileTimeFromTimeSetting(FileInfo fi, TimeSetting timeSetting) + { + switch (timeSetting) + { + case TimeSetting.LastWriteTime: return fi.LastWriteTime; + case TimeSetting.LastWriteTimeUtc: return fi.LastWriteTimeUtc; + case TimeSetting.CreateTime: return fi.CreationTime; + case TimeSetting.CreateTimeUtc: return fi.CreationTimeUtc; + case TimeSetting.LastAccessTime: return fi.LastAccessTime; + case TimeSetting.LastAccessTimeUtc: return fi.LastAccessTimeUtc; + case TimeSetting.Fixed: return fi.LastWriteTime; + } + + throw new ArgumentException("Invalid TimeSetting", nameof(timeSetting)); + } + + private static DateTime TestTargetTime(TimeSetting ts) + { + var dtk = ts == TimeSetting.CreateTimeUtc + || ts == TimeSetting.LastWriteTimeUtc + || ts == TimeSetting.LastAccessTimeUtc + ? DateTimeKind.Utc + : DateTimeKind.Local; + + return IsLastAccessTime(ts) + // AccessTime will be altered by reading/writing the file entry + ? CurrentTime(dtk) + : ExpectedFixedTime(dtk); + } + + private static DateTime CurrentTime(DateTimeKind kind) + { + var now = kind == DateTimeKind.Utc ? DateTime.UtcNow : DateTime.Now; + return new DateTime(now.Year, now.Month, now.Day, now.Hour, now.Minute, (now.Second / 2) * 2, kind); + } + + private static DateTime ExpectedFixedTime(DateTimeKind dtk = DateTimeKind.Unspecified) + => new DateTime(2010, 5, 30, 16, 22, 50, dtk); + private static DateTime UnexpectedFixedTime(DateTimeKind dtk = DateTimeKind.Unspecified) + => new DateTime(1980, 10, 11, 22, 39, 30, dtk); + + private const string SingleEntryFileName = "testEntry.dat"; + + private static TrackedMemoryStream CreateFastZipTestArchiveWithAnEntry(FastZip fastZip, Action alterFile = null) + { + var target = new TrackedMemoryStream(); + + using var tempFolder = Utils.GetTempDir(); + // Create test input file + var addFile = Path.Combine(tempFolder.FullName, SingleEntryFileName); + MakeTempFile(addFile, 16); + var fi = new FileInfo(addFile); + alterFile?.Invoke(fi); + + fastZip.CreateZip(target, tempFolder.FullName, recurse: false, + SingleEntryFileName, directoryFilter: null, leaveOpen: true); + + return target; + } } } diff --git a/test/ICSharpCode.SharpZipLib.Tests/Zip/GeneralHandling.cs b/test/ICSharpCode.SharpZipLib.Tests/Zip/GeneralHandling.cs index e4f7da1c7..f3bf9a995 100644 --- a/test/ICSharpCode.SharpZipLib.Tests/Zip/GeneralHandling.cs +++ b/test/ICSharpCode.SharpZipLib.Tests/Zip/GeneralHandling.cs @@ -1,12 +1,15 @@ -using System; +using ICSharpCode.SharpZipLib.Checksum; +using ICSharpCode.SharpZipLib.Tests.TestSupport; +using ICSharpCode.SharpZipLib.Zip; +using ICSharpCode.SharpZipLib.Zip.Compression.Streams; +using NUnit.Framework; +using System; using System.IO; using System.Reflection; using System.Runtime.Serialization.Formatters.Binary; using System.Security; using System.Text; -using ICSharpCode.SharpZipLib.Tests.TestSupport; -using ICSharpCode.SharpZipLib.Zip; -using NUnit.Framework; +using Does = ICSharpCode.SharpZipLib.Tests.TestSupport.Does; namespace ICSharpCode.SharpZipLib.Tests.Zip { @@ -16,18 +19,7 @@ namespace ICSharpCode.SharpZipLib.Tests.Zip [TestFixture] public class GeneralHandling : ZipBase { - void AddRandomDataToEntry(ZipOutputStream zipStream, int size) - { - if (size > 0) { - byte[] data = new byte[size]; - var rnd = new Random(); - rnd.NextBytes(data); - - zipStream.Write(data, 0, data.Length); - } - } - - void ExerciseZip(CompressionMethod method, int compressionLevel, + private void ExerciseZip(CompressionMethod method, int compressionLevel, int size, string password, bool canSeek) { byte[] originalData = null; @@ -36,26 +28,32 @@ void ExerciseZip(CompressionMethod method, int compressionLevel, var ms = new MemoryStream(compressedData); ms.Seek(0, SeekOrigin.Begin); - using (ZipInputStream inStream = new ZipInputStream(ms)) { + using (ZipInputStream inStream = new ZipInputStream(ms)) + { byte[] decompressedData = new byte[size]; - if (password != null) { + if (password != null) + { inStream.Password = password; } ZipEntry entry2 = inStream.GetNextEntry(); - if ((entry2.Flags & 8) == 0) { + if ((entry2.Flags & 8) == 0) + { Assert.AreEqual(size, entry2.Size, "Entry size invalid"); } int currentIndex = 0; - if (size > 0) { + if (size > 0) + { int count = decompressedData.Length; - while (true) { + while (true) + { int numRead = inStream.Read(decompressedData, currentIndex, count); - if (numRead <= 0) { + if (numRead <= 0) + { break; } currentIndex += numRead; @@ -65,73 +63,23 @@ void ExerciseZip(CompressionMethod method, int compressionLevel, Assert.AreEqual(currentIndex, size, "Original and decompressed data different sizes"); - if (originalData != null) { - for (int i = 0; i < originalData.Length; ++i) { + if (originalData != null) + { + for (int i = 0; i < originalData.Length; ++i) + { Assert.AreEqual(decompressedData[i], originalData[i], "Decompressed data doesnt match original, compression level: " + compressionLevel); } } } } - string DescribeAttributes(FieldAttributes attributes) - { - string att = string.Empty; - if ((FieldAttributes.Public & attributes) != 0) { - att = att + "Public,"; - } - - if ((FieldAttributes.Static & attributes) != 0) { - att = att + "Static,"; - } - - if ((FieldAttributes.Literal & attributes) != 0) { - att = att + "Literal,"; - } - - if ((FieldAttributes.HasDefault & attributes) != 0) { - att = att + "HasDefault,"; - } - - if ((FieldAttributes.InitOnly & attributes) != 0) { - att = att + "InitOnly,"; - } - - if ((FieldAttributes.Assembly & attributes) != 0) { - att = att + "Assembly,"; - } - - if ((FieldAttributes.FamANDAssem & attributes) != 0) { - att = att + "FamANDAssembly,"; - } - - if ((FieldAttributes.FamORAssem & attributes) != 0) { - att = att + "FamORAssembly,"; - } - - if ((FieldAttributes.HasFieldMarshal & attributes) != 0) { - att = att + "HasFieldMarshal,"; - } - - return att; - } - - [Test] - [Category("Zip")] - //[ExpectedException(typeof(NotSupportedException))] - public void UnsupportedCompressionMethod() - { - var ze = new ZipEntry("HumblePie"); - //ze.CompressionMethod = CompressionMethod.BZip2; - - Assert.That(() => ze.CompressionMethod = CompressionMethod.BZip2, - Throws.TypeOf()); - } - /// /// Invalid passwords should be detected early if possible, seekable stream + /// Note: Have a 1/255 chance of failing due to CRC collision (hence retried once) /// [Test] [Category("Zip")] + [Retry(2)] public void InvalidPasswordSeekable() { byte[] originalData = null; @@ -148,11 +96,13 @@ public void InvalidPasswordSeekable() ZipEntry entry2 = inStream.GetNextEntry(); - Assert.Throws(() => + Assert.Throws(() => { - while (true) { + while (true) + { int numRead = inStream.Read(buf2, pos, buf2.Length); - if (numRead <= 0) { + if (numRead <= 0) + { break; } pos += numRead; @@ -182,10 +132,12 @@ public void ExerciseGetNextEntry() var ms = new MemoryStream(compressedData); ms.Seek(0, SeekOrigin.Begin); - using (ZipInputStream inStream = new ZipInputStream(ms)) { + using (ZipInputStream inStream = new ZipInputStream(ms)) + { byte[] buffer = new byte[10]; - while (inStream.GetNextEntry() != null) { + while (inStream.GetNextEntry() != null) + { // Read a portion of the data, so GetNextEntry has some work to do. inStream.Read(buffer, 0, 10); } @@ -194,9 +146,11 @@ public void ExerciseGetNextEntry() /// /// Invalid passwords should be detected early if possible, non seekable stream + /// Note: Have a 1/255 chance of failing due to CRC collision (hence retried once) /// [Test] [Category("Zip")] + [Retry(2)] public void InvalidPasswordNonSeekable() { byte[] originalData = null; @@ -213,11 +167,13 @@ public void InvalidPasswordNonSeekable() ZipEntry entry2 = inStream.GetNextEntry(); - Assert.Throws(() => + Assert.Throws(() => { - while (true) { + while (true) + { int numRead = inStream.Read(buf2, pos, buf2.Length); - if (numRead <= 0) { + if (numRead <= 0) + { break; } pos += numRead; @@ -281,7 +237,8 @@ public void CloseOnlyHandled() [Category("Zip")] public void BasicDeflated() { - for (int i = 0; i <= 9; ++i) { + for (int i = 0; i <= 9; ++i) + { ExerciseZip(CompressionMethod.Deflated, i, 50000, null, true); } } @@ -294,7 +251,8 @@ public void BasicDeflated() [Category("Zip")] public void BasicDeflatedNonSeekable() { - for (int i = 0; i <= 9; ++i) { + for (int i = 0; i <= 9; ++i) + { ExerciseZip(CompressionMethod.Deflated, i, 50000, null, false); } } @@ -322,90 +280,44 @@ public void BasicStoredNonSeekable() [Test] [Category("Zip")] - public void StoredNonSeekableKnownSizeNoCrc() + [TestCase(21348, null)] + [TestCase(24692, "Mabutu")] + public void StoredNonSeekableKnownSizeNoCrc(int targetSize, string password) { - // This cannot be stored directly as the crc is not be known. - const int TargetSize = 21348; - const string Password = null; + // This cannot be stored directly as the crc is not known. MemoryStream ms = new MemoryStreamWithoutSeek(); - using (ZipOutputStream outStream = new ZipOutputStream(ms)) { - outStream.Password = Password; + using (ZipOutputStream outStream = new ZipOutputStream(ms)) + { + outStream.Password = password; outStream.IsStreamOwner = false; var entry = new ZipEntry("dummyfile.tst"); entry.CompressionMethod = CompressionMethod.Stored; // The bit thats in question is setting the size before its added to the archive. - entry.Size = TargetSize; + entry.Size = targetSize; outStream.PutNextEntry(entry); Assert.AreEqual(CompressionMethod.Deflated, entry.CompressionMethod, "Entry should be deflated"); Assert.AreEqual(-1, entry.CompressedSize, "Compressed size should be known"); - var rnd = new Random(); - - int size = TargetSize; - byte[] original = new byte[size]; - rnd.NextBytes(original); - - // Although this could be written in one chunk doing it in lumps - // throws up buffering problems including with encryption the original - // source for this change. - int index = 0; - while (size > 0) { - int count = (size > 0x200) ? 0x200 : size; - outStream.Write(original, index, count); - size -= 0x200; - index += count; - } - } - Assert.IsTrue(ZipTesting.TestArchive(ms.ToArray())); - } - - [Test] - [Category("Zip")] - public void StoredNonSeekableKnownSizeNoCrcEncrypted() - { - // This cant be stored directly as the crc is not known - const int TargetSize = 24692; - const string Password = "Mabutu"; - - MemoryStream ms = new MemoryStreamWithoutSeek(); - - using (ZipOutputStream outStream = new ZipOutputStream(ms)) { - outStream.Password = Password; - outStream.IsStreamOwner = false; - var entry = new ZipEntry("dummyfile.tst"); - entry.CompressionMethod = CompressionMethod.Stored; - - // The bit thats in question is setting the size before its added to the archive. - entry.Size = TargetSize; - - outStream.PutNextEntry(entry); - - Assert.AreEqual(CompressionMethod.Deflated, entry.CompressionMethod, "Entry should be stored"); - Assert.AreEqual(-1, entry.CompressedSize, "Compressed size should be known"); - - var rnd = new Random(); - - int size = TargetSize; - byte[] original = new byte[size]; - rnd.NextBytes(original); + byte[] original = Utils.GetDummyBytes(targetSize); // Although this could be written in one chunk doing it in lumps // throws up buffering problems including with encryption the original // source for this change. - int index = 0; - while (size > 0) { + int index = 0, size = targetSize; + while (size > 0) + { int count = (size > 0x200) ? 0x200 : size; outStream.Write(original, index, count); size -= 0x200; index += count; } } - Assert.IsTrue(ZipTesting.TestArchive(ms.ToArray(), Password)); + Assert.That(ms.ToArray(), Does.PassTestArchive(password)); } /// @@ -416,7 +328,8 @@ public void StoredNonSeekableKnownSizeNoCrcEncrypted() [Category("Zip")] public void BasicDeflatedEncrypted() { - for (int i = 0; i <= 9; ++i) { + for (int i = 0; i <= 9; ++i) + { ExerciseZip(CompressionMethod.Deflated, i, 50157, "Rosebud", true); } } @@ -429,7 +342,8 @@ public void BasicDeflatedEncrypted() [Category("Zip")] public void BasicDeflatedEncryptedNonSeekable() { - for (int i = 0; i <= 9; ++i) { + for (int i = 0; i <= 9; ++i) + { ExerciseZip(CompressionMethod.Deflated, i, 50000, "Rosebud", false); } } @@ -451,7 +365,8 @@ public void SkipEncryptedEntriesWithoutSettingPassword() var ms = new MemoryStream(compressedData); var inStream = new ZipInputStream(ms); - while (inStream.GetNextEntry() != null) { + while (inStream.GetNextEntry() != null) + { } inStream.Close(); @@ -469,20 +384,24 @@ public void MixedEncryptedAndPlain() ); var ms = new MemoryStream(compressedData); - using (ZipInputStream inStream = new ZipInputStream(ms)) { + using (ZipInputStream inStream = new ZipInputStream(ms)) + { inStream.Password = "1234"; int extractCount = 0; int extractIndex = 0; - ZipEntry entry; + byte[] decompressedData = new byte[100]; - while ((entry = inStream.GetNextEntry()) != null) { + while (inStream.GetNextEntry() != null) + { extractCount = decompressedData.Length; extractIndex = 0; - while (true) { + while (true) + { int numRead = inStream.Read(decompressedData, extractIndex, extractCount); - if (numRead <= 0) { + if (numRead <= 0) + { break; } extractIndex += numRead; @@ -500,7 +419,7 @@ public void MixedEncryptedAndPlain() [Category("Zip")] public void BasicStoredEncrypted() { - ExerciseZip(CompressionMethod.Stored, 0, 50000, "Rosebud", true); + ExerciseZip(CompressionMethod.Stored, compressionLevel: 0, size: 50000, "Rosebud", canSeek: true); } /// @@ -511,7 +430,7 @@ public void BasicStoredEncrypted() [Category("Zip")] public void BasicStoredEncryptedNonSeekable() { - ExerciseZip(CompressionMethod.Stored, 0, 50000, "Rosebud", false); + ExerciseZip(CompressionMethod.Stored, compressionLevel: 0, size: 50000, "Rosebud", canSeek: false); } /// @@ -533,12 +452,12 @@ public void StoredNonSeekableConvertToDeflate() outStream.PutNextEntry(entry); Assert.AreEqual(0, outStream.GetLevel(), "Compression level invalid"); - AddRandomDataToEntry(outStream, 100); + Utils.WriteDummyData(outStream, 100); entry = new ZipEntry("2.tst"); entry.CompressionMethod = CompressionMethod.Deflated; outStream.PutNextEntry(entry); Assert.AreEqual(8, outStream.GetLevel(), "Compression level invalid"); - AddRandomDataToEntry(outStream, 100); + Utils.WriteDummyData(outStream, 100); outStream.Close(); } @@ -554,7 +473,6 @@ public void StoredNonSeekableConvertToDeflate() // const int target = 65537; // MemoryStream ms = new MemoryStream(); // using (ZipOutputStream s = new ZipOutputStream(ms)) { - // for (int i = 0; i < target; ++i) { // s.PutNextEntry(new ZipEntry("dummyfile.tst")); // } @@ -575,7 +493,8 @@ public void StoredNonSeekableConvertToDeflate() public void Stream_UnicodeEntries() { var ms = new MemoryStream(); - using (ZipOutputStream s = new ZipOutputStream(ms)) { + using (ZipOutputStream s = new ZipOutputStream(ms)) + { s.IsStreamOwner = false; string sampleName = "\u03A5\u03d5\u03a3"; @@ -586,7 +505,8 @@ public void Stream_UnicodeEntries() s.Finish(); ms.Seek(0, SeekOrigin.Begin); - using (ZipInputStream zis = new ZipInputStream(ms)) { + using (ZipInputStream zis = new ZipInputStream(ms)) + { ZipEntry ze = zis.GetNextEntry(); Assert.AreEqual(sampleName, ze.Name, "Expected name to match original"); Assert.IsTrue(ze.IsUnicodeText, "Expected IsUnicodeText flag to be set"); @@ -602,11 +522,13 @@ public void PartialStreamClosing() string tempFile = GetTempFilePath(); Assert.IsNotNull(tempFile, "No permission to execute this test?"); - if (tempFile != null) { + if (tempFile != null) + { tempFile = Path.Combine(tempFile, "SharpZipTest.Zip"); MakeZipFile(tempFile, new String[] { "Farriera", "Champagne", "Urban myth" }, 10, "Aha"); - using (ZipFile zipFile = new ZipFile(tempFile)) { + using (ZipFile zipFile = new ZipFile(tempFile)) + { Stream stream = zipFile.GetInputStream(0); stream.Close(); @@ -617,29 +539,34 @@ public void PartialStreamClosing() } } - void TestLargeZip(string tempFile, int targetFiles) + private void TestLargeZip(string tempFile, int targetFiles) { const int BlockSize = 4096; byte[] data = new byte[BlockSize]; byte nextValue = 0; - for (int i = 0; i < BlockSize; ++i) { + for (int i = 0; i < BlockSize; ++i) + { nextValue = ScatterValue(nextValue); data[i] = nextValue; } - using (ZipFile zFile = new ZipFile(tempFile)) { + using (ZipFile zFile = new ZipFile(tempFile)) + { Assert.AreEqual(targetFiles, zFile.Count); byte[] readData = new byte[BlockSize]; int readIndex; - foreach (ZipEntry ze in zFile) { + foreach (ZipEntry ze in zFile) + { Stream s = zFile.GetInputStream(ze); readIndex = 0; - while (readIndex < readData.Length) { + while (readIndex < readData.Length) + { readIndex += s.Read(readData, readIndex, data.Length - readIndex); } - for (int ii = 0; ii < BlockSize; ++ii) { + for (int ii = 0; ii < BlockSize; ++ii) + { Assert.AreEqual(data[ii], readData[ii]); } } @@ -663,10 +590,13 @@ public void TestLargeZipFile() public void MakeLargeZipFile() { string tempFile = null; - try { + try + { // tempFile = Path.GetTempPath(); tempFile = @"g:\\tmp"; - } catch (SecurityException) { + } + catch (SecurityException) + { } Assert.IsNotNull(tempFile, "No permission to execute this test?"); @@ -675,25 +605,30 @@ public void MakeLargeZipFile() byte[] data = new byte[blockSize]; byte nextValue = 0; - for (int i = 0; i < blockSize; ++i) { + for (int i = 0; i < blockSize; ++i) + { nextValue = ScatterValue(nextValue); data[i] = nextValue; } tempFile = Path.Combine(tempFile, "SharpZipTest.Zip"); Console.WriteLine("Starting at {0}", DateTime.Now); - try { + try + { // MakeZipFile(tempFile, new String[] {"1", "2" }, int.MaxValue, "C1"); - using (FileStream fs = File.Create(tempFile)) { + using (FileStream fs = File.Create(tempFile)) + { var zOut = new ZipOutputStream(fs); zOut.SetLevel(4); const int TargetFiles = 8100; - for (int i = 0; i < TargetFiles; ++i) { + for (int i = 0; i < TargetFiles; ++i) + { var e = new ZipEntry(i.ToString()); e.CompressionMethod = CompressionMethod.Stored; zOut.PutNextEntry(e); - for (int block = 0; block < 128; ++block) { + for (int block = 0; block < 128; ++block) + { zOut.Write(data, 0, blockSize); } } @@ -702,7 +637,9 @@ public void MakeLargeZipFile() TestLargeZip(tempFile, TargetFiles); } - } finally { + } + finally + { Console.WriteLine("Starting at {0}", DateTime.Now); // File.Delete(tempFile); } @@ -714,13 +651,15 @@ public void MakeLargeZipFile() /// [Test] [Category("Zip")] + [Ignore("With ArraySegment for crc checking, this test doesn't throw an exception. Not sure if it's needed.")] public void SerializedObjectZeroLength() { bool exception = false; object data = new byte[0]; // Thisa wont be zero length here due to serialisation. - try { + try + { byte[] zipped = ZipZeroLength(data); object o = UnZipZeroLength(zipped); @@ -729,8 +668,9 @@ public void SerializedObjectZeroLength() Assert.IsNotNull(returned, "Expected a byte[]"); Assert.AreEqual(0, returned.Length); - - } catch (ArgumentOutOfRangeException) { + } + catch (ArgumentOutOfRangeException) + { exception = true; } @@ -763,12 +703,13 @@ public void SerializedObject() Assert.AreEqual(sampleString, returnedString); } - byte[] ZipZeroLength(object data) + private byte[] ZipZeroLength(object data) { var formatter = new BinaryFormatter(); var memStream = new MemoryStream(); - using (ZipOutputStream zipStream = new ZipOutputStream(memStream)) { + using (ZipOutputStream zipStream = new ZipOutputStream(memStream)) + { zipStream.PutNextEntry(new ZipEntry("data")); formatter.Serialize(zipStream, data); zipStream.CloseEntry(); @@ -781,18 +722,21 @@ byte[] ZipZeroLength(object data) return result; } - object UnZipZeroLength(byte[] zipped) + private object UnZipZeroLength(byte[] zipped) { - if (zipped == null) { + if (zipped == null) + { return null; } object result = null; var formatter = new BinaryFormatter(); var memStream = new MemoryStream(zipped); - using (ZipInputStream zipStream = new ZipInputStream(memStream)) { + using (ZipInputStream zipStream = new ZipInputStream(memStream)) + { ZipEntry zipEntry = zipStream.GetNextEntry(); - if (zipEntry != null) { + if (zipEntry != null) + { result = formatter.Deserialize(zipStream); } zipStream.Close(); @@ -802,20 +746,17 @@ object UnZipZeroLength(byte[] zipped) return result; } - void CheckNameConversion(string toCheck) - { - byte[] intermediate = ZipConstants.ConvertToArray(toCheck); - string final = ZipConstants.ConvertToString(intermediate); - - Assert.AreEqual(toCheck, final, "Expected identical result"); - } - [Test] [Category("Zip")] - public void NameConversion() + [TestCase("Hello")] + [TestCase("a/b/c/d/e/f/g/h/SomethingLikeAnArchiveName.txt")] + public void LegacyNameConversion(string name) { - CheckNameConversion("Hello"); - CheckNameConversion("a/b/c/d/e/f/g/h/SomethingLikeAnArchiveName.txt"); + var encoding = StringCodec.Default.ZipEncoding(false); + byte[] intermediate = encoding.GetBytes(name); + string final = encoding.GetString(intermediate); + + Assert.AreEqual(name, final, "Expected identical result"); } [Test] @@ -824,22 +765,22 @@ public void UnicodeNameConversion() { Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); - ZipConstants.DefaultCodePage = 850; + var codec = StringCodec.FromCodePage(850); string sample = "Hello world"; byte[] rawData = Encoding.ASCII.GetBytes(sample); - string converted = ZipConstants.ConvertToStringExt(0, rawData); + var converted = codec.LegacyEncoding.GetString(rawData); Assert.AreEqual(sample, converted); - converted = ZipConstants.ConvertToStringExt((int)GeneralBitFlags.UnicodeText, rawData); + converted = codec.ZipInputEncoding(GeneralBitFlags.UnicodeText).GetString(rawData); Assert.AreEqual(sample, converted); // This time use some greek characters sample = "\u03A5\u03d5\u03a3"; rawData = Encoding.UTF8.GetBytes(sample); - converted = ZipConstants.ConvertToStringExt((int)GeneralBitFlags.UnicodeText, rawData); + converted = codec.ZipInputEncoding(GeneralBitFlags.UnicodeText).GetString(rawData); Assert.AreEqual(sample, converted); } @@ -857,7 +798,8 @@ public void PasswordCheckingWithDateInExtraData() var ms = new MemoryStream(); var checkTime = new DateTimeOffset(2010, 10, 16, 0, 3, 28, new TimeSpan(1, 0, 0)); - using (ZipOutputStream zos = new ZipOutputStream(ms)) { + using (ZipOutputStream zos = new ZipOutputStream(ms)) + { zos.IsStreamOwner = false; zos.Password = "secret"; var ze = new ZipEntry("uno"); @@ -880,7 +822,8 @@ public void PasswordCheckingWithDateInExtraData() } ms.Position = 0; - using (ZipInputStream zis = new ZipInputStream(ms)) { + using (ZipInputStream zis = new ZipInputStream(ms)) + { zis.Password = "secret"; ZipEntry uno = zis.GetNextEntry(); var theByte = (byte)zis.ReadByte(); diff --git a/test/ICSharpCode.SharpZipLib.Tests/Zip/PassthroughTests.cs b/test/ICSharpCode.SharpZipLib.Tests/Zip/PassthroughTests.cs new file mode 100644 index 000000000..954e339b1 --- /dev/null +++ b/test/ICSharpCode.SharpZipLib.Tests/Zip/PassthroughTests.cs @@ -0,0 +1,141 @@ +using System; +using System.IO; +using System.IO.Compression; +using System.Text; +using ICSharpCode.SharpZipLib.Checksum; +using ICSharpCode.SharpZipLib.Tests.TestSupport; +using ICSharpCode.SharpZipLib.Zip; +using NUnit.Framework; +using Does = ICSharpCode.SharpZipLib.Tests.TestSupport.Does; + +namespace ICSharpCode.SharpZipLib.Tests.Zip +{ + [TestFixture] + public class PassthroughTests + { + [Test] + [Category("Zip")] + public void AddingValidPrecompressedEntryToZipOutputStream() + { + using var ms = new MemoryStream(); + + using (var outStream = new ZipOutputStream(ms){IsStreamOwner = false}) + { + var (compressedData, crc, size) = CreateDeflatedData(); + var entry = new ZipEntry("dummyfile.tst") + { + CompressionMethod = CompressionMethod.Deflated, + Size = size, + Crc = (uint)crc.Value, + CompressedSize = compressedData.Length, + }; + + outStream.PutNextPassthroughEntry(entry); + + compressedData.CopyTo(outStream); + } + + Assert.That(ms.ToArray(), Does.PassTestArchive()); + } + + private static (MemoryStream, Crc32, int) CreateDeflatedData() + { + var data = Encoding.UTF8.GetBytes("Hello, world"); + + var crc = new Crc32(); + crc.Update(data); + + var compressedData = new MemoryStream(); + using(var gz = new DeflateStream(compressedData, CompressionMode.Compress, leaveOpen: true)) + { + gz.Write(data, 0, data.Length); + } + compressedData.Position = 0; + + return (compressedData, crc, data.Length); + } + + [Test] + [Category("Zip")] + public void AddingPrecompressedEntryToZipOutputStreamWithInvalidSize() + { + using var outStream = new ZipOutputStream(new MemoryStream()); + var (compressedData, crc, size) = CreateDeflatedData(); + outStream.Password = "mockpassword"; + var entry = new ZipEntry("dummyfile.tst") + { + CompressionMethod = CompressionMethod.Stored, + Crc = (uint)crc.Value, + CompressedSize = compressedData.Length, + }; + + Assert.Throws(() => + { + outStream.PutNextPassthroughEntry(entry); + }); + } + + + [Test] + [Category("Zip")] + public void AddingPrecompressedEntryToZipOutputStreamWithInvalidCompressedSize() + { + using var outStream = new ZipOutputStream(new MemoryStream()); + var (compressedData, crc, size) = CreateDeflatedData(); + outStream.Password = "mockpassword"; + var entry = new ZipEntry("dummyfile.tst") + { + CompressionMethod = CompressionMethod.Stored, + Size = size, + Crc = (uint)crc.Value, + }; + + Assert.Throws(() => + { + outStream.PutNextPassthroughEntry(entry); + }); + } + + [Test] + [Category("Zip")] + public void AddingPrecompressedEntryToZipOutputStreamWithNonSupportedMethod() + { + using var outStream = new ZipOutputStream(new MemoryStream()); + var (compressedData, crc, size) = CreateDeflatedData(); + outStream.Password = "mockpassword"; + var entry = new ZipEntry("dummyfile.tst") + { + CompressionMethod = CompressionMethod.LZMA, + Size = size, + Crc = (uint)crc.Value, + CompressedSize = compressedData.Length, + }; + + Assert.Throws(() => + { + outStream.PutNextPassthroughEntry(entry); + }); + } + + [Test] + [Category("Zip")] + public void AddingPrecompressedEntryToZipOutputStreamWithEncryption() + { + using var outStream = new ZipOutputStream(new MemoryStream()); + var (compressedData, crc, size) = CreateDeflatedData(); + outStream.Password = "mockpassword"; + var entry = new ZipEntry("dummyfile.tst") + { + CompressionMethod = CompressionMethod.Deflated, + Size = size, + Crc = (uint)crc.Value, + CompressedSize = compressedData.Length, + }; + + Assert.Throws(() => + { + outStream.PutNextPassthroughEntry(entry); + }); + } + } +} diff --git a/test/ICSharpCode.SharpZipLib.Tests/Zip/StreamHandling.cs b/test/ICSharpCode.SharpZipLib.Tests/Zip/StreamHandling.cs index a362617b9..3e8ab732c 100644 --- a/test/ICSharpCode.SharpZipLib.Tests/Zip/StreamHandling.cs +++ b/test/ICSharpCode.SharpZipLib.Tests/Zip/StreamHandling.cs @@ -1,9 +1,13 @@ -using System; -using System.IO; +using ICSharpCode.SharpZipLib.Core; using ICSharpCode.SharpZipLib.Tests.TestSupport; using ICSharpCode.SharpZipLib.Zip; using NUnit.Framework; -using System.Threading; +using System; +using System.Diagnostics; +using System.IO; +using System.Linq; +using System.Text; +using Does = ICSharpCode.SharpZipLib.Tests.TestSupport.Does; namespace ICSharpCode.SharpZipLib.Tests.Zip { @@ -13,12 +17,21 @@ namespace ICSharpCode.SharpZipLib.Tests.Zip [TestFixture] public class StreamHandling : ZipBase { - void MustFailRead(Stream s, byte[] buffer, int offset, int count) + private TestTraceListener Listener; + [SetUp] + public void Init() => Trace.Listeners.Add(Listener = new TestTraceListener(TestContext.Out)); + [TearDown] + public void Deinit() => Trace.Listeners.Remove(Listener); + + private void MustFailRead(Stream s, byte[] buffer, int offset, int count) { bool exception = false; - try { + try + { s.Read(buffer, offset, count); - } catch { + } + catch + { exception = true; } Assert.IsTrue(exception, "Read should fail"); @@ -41,9 +54,9 @@ public void ParameterHandling() ms.Seek(0, SeekOrigin.Begin); var inStream = new ZipInputStream(ms); - ZipEntry e = inStream.GetNextEntry(); + inStream.GetNextEntry(); - MustFailRead(inStream, null, 0, 0); + MustFailRead(inStream, buffer: null, 0, 0); MustFailRead(inStream, buffer, -1, 1); MustFailRead(inStream, buffer, 0, 11); MustFailRead(inStream, buffer, 7, 5); @@ -74,7 +87,7 @@ public void Zip64Descriptor() outStream.WriteByte(89); outStream.Close(); - Assert.IsTrue(ZipTesting.TestArchive(msw.ToArray())); + Assert.That(msw.ToArray(), Does.PassTestArchive()); msw = new MemoryStreamWithoutSeek(); outStream = new ZipOutputStream(msw); @@ -85,7 +98,7 @@ public void Zip64Descriptor() outStream.WriteByte(89); outStream.Close(); - Assert.IsTrue(ZipTesting.TestArchive(msw.ToArray())); + Assert.That(msw.ToArray(), Does.PassTestArchive()); } [Test] @@ -93,7 +106,8 @@ public void Zip64Descriptor() public void ReadAndWriteZip64NonSeekable() { MemoryStream msw = new MemoryStreamWithoutSeek(); - using (ZipOutputStream outStream = new ZipOutputStream(msw)) { + using (ZipOutputStream outStream = new ZipOutputStream(msw)) + { outStream.UseZip64 = UseZip64.On; outStream.IsStreamOwner = false; @@ -106,16 +120,17 @@ public void ReadAndWriteZip64NonSeekable() outStream.Close(); } - Assert.IsTrue(ZipTesting.TestArchive(msw.ToArray())); - - msw.Position = 0; - - using (ZipInputStream zis = new ZipInputStream(msw)) { - while (zis.GetNextEntry() != null) { - int len = 0; - int bufferSize = 1024; - byte[] buffer = new byte[bufferSize]; - while ((len = zis.Read(buffer, 0, bufferSize)) > 0) { + var msBytes = msw.ToArray(); + Assert.That(msBytes, Does.PassTestArchive()); + + using (var zis = new ZipInputStream(new MemoryStream(msBytes))) + { + while (zis.GetNextEntry() != null) + { + const int bufferSize = 1024; + var buffer = new byte[bufferSize]; + while (zis.Read(buffer, 0, bufferSize) > 0) + { // Reading the data is enough } } @@ -141,8 +156,9 @@ public void EntryWithNoDataAndZip64() outStream.Finish(); outStream.Close(); - Assert.IsTrue(ZipTesting.TestArchive(msw.ToArray())); + Assert.That(msw.ToArray(), Does.PassTestArchive()); } + /// /// Empty zip entries can be created and read? /// @@ -154,7 +170,8 @@ public void EmptyZipEntries() var ms = new MemoryStream(); var outStream = new ZipOutputStream(ms); - for (int i = 0; i < 10; ++i) { + for (int i = 0; i < 10; ++i) + { outStream.PutNextEntry(new ZipEntry(i.ToString())); } @@ -167,17 +184,132 @@ public void EmptyZipEntries() int extractCount = 0; byte[] decompressedData = new byte[100]; - while ((inStream.GetNextEntry()) != null) { - while (true) { + while ((inStream.GetNextEntry()) != null) + { + while (true) + { int numRead = inStream.Read(decompressedData, extractCount, decompressedData.Length); - if (numRead <= 0) { + if (numRead <= 0) + { break; } extractCount += numRead; } } inStream.Close(); - Assert.AreEqual(extractCount, 0, "No data should be read from empty entries"); + Assert.Zero(extractCount, "No data should be read from empty entries"); + } + + /// + /// Test that calling Write with 0 bytes behaves. + /// See issue @ https://github.com/icsharpcode/SharpZipLib/issues/123. + /// + [Test] + [Category("Zip")] + public void TestZeroByteWrite() + { + using (var ms = new MemoryStreamWithoutSeek()) + { + using (var outStream = new ZipOutputStream(ms) { IsStreamOwner = false }) + { + var ze = new ZipEntry("Striped Marlin"); + outStream.PutNextEntry(ze); + + var buffer = Array.Empty(); + outStream.Write(buffer, 0, 0); + } + + ms.Seek(0, SeekOrigin.Begin); + + using (var inStream = new ZipInputStream(ms) { IsStreamOwner = false }) + { + int extractCount = 0; + byte[] decompressedData = new byte[100]; + + while (inStream.GetNextEntry() != null) + { + while (true) + { + int numRead = inStream.Read(decompressedData, extractCount, decompressedData.Length); + if (numRead <= 0) + { + break; + } + extractCount += numRead; + } + } + Assert.Zero(extractCount, "No data should be read from empty entries"); + } + } + } + + [Test] + [Category("Zip")] + public void WriteZipStreamWithNoCompression([Values(0, 1, 256)] int contentLength) + { + var buffer = new byte[255]; + + using var dummyZip = Utils.GetTempFile(); + using var inputFile = Utils.GetDummyFile(contentLength); + // Filename is manually cleaned here to prevent this test from failing while ZipEntry doesn't automatically clean it + var inputFileName = ZipEntry.CleanName(inputFile); + + using (var zipFileStream = File.OpenWrite(dummyZip)) + using (var zipOutputStream = new ZipOutputStream(zipFileStream)) + using (var inputFileStream = File.OpenRead(inputFile)) + { + zipOutputStream.PutNextEntry(new ZipEntry(inputFileName) + { + CompressionMethod = CompressionMethod.Stored, + }); + + StreamUtils.Copy(inputFileStream, zipOutputStream, buffer); + } + + using (var zf = new ZipFile(dummyZip)) + { + var inputBytes = File.ReadAllBytes(inputFile); + + var entry = zf.GetEntry(inputFileName); + Assert.IsNotNull(entry, "No entry matching source file \"{0}\" found in archive, found \"{1}\"", inputFileName, zf[0].Name); + + Assert.DoesNotThrow(() => + { + using var entryStream = zf.GetInputStream(entry); + var outputBytes = new byte[entryStream.Length]; + entryStream.Read(outputBytes, 0, outputBytes.Length); + + Assert.AreEqual(inputBytes, outputBytes, "Archive content does not match the source content"); + }, "Failed to locate entry stream in archive"); + + Assert.That(zf, Does.PassTestArchive()); + } + } + + [Test] + [Category("Zip")] + public void ZipEntryFileNameAutoClean() + { + using var dummyZip = Utils.GetDummyFile(0); + using var inputFile = Utils.GetDummyFile(); + using (var zipFileStream = File.OpenWrite(dummyZip)) + using (var zipOutputStream = new ZipOutputStream(zipFileStream)) + using (var inputFileStream = File.OpenRead(inputFile)) + { + // New ZipEntry created with a full file name path as it's name + zipOutputStream.PutNextEntry(new ZipEntry(inputFile) + { + CompressionMethod = CompressionMethod.Stored, + }); + + inputFileStream.CopyTo(zipOutputStream); + } + + using (var zf = new ZipFile(dummyZip)) + { + // The ZipEntry name should have been automatically cleaned + Assert.AreEqual(ZipEntry.CleanName(inputFile), zf[0].Name); + } } /// @@ -194,7 +326,8 @@ public void CreateAndReadEmptyZip() ms.Seek(0, SeekOrigin.Begin); var inStream = new ZipInputStream(ms); - while ((inStream.GetNextEntry()) != null) { + while ((inStream.GetNextEntry()) != null) + { Assert.Fail("No entries should be found in empty zip"); } } @@ -209,7 +342,8 @@ public void BaseClosedWhenOwner() Assert.IsFalse(ms.IsClosed, "Underlying stream should NOT be closed"); - using (ZipOutputStream stream = new ZipOutputStream(ms)) { + using (ZipOutputStream stream = new ZipOutputStream(ms)) + { Assert.IsTrue(stream.IsStreamOwner, "Should be stream owner by default"); } @@ -226,7 +360,8 @@ public void BaseNotClosedWhenNotOwner() Assert.IsFalse(ms.IsClosed, "Underlying stream should NOT be closed"); - using (ZipOutputStream stream = new ZipOutputStream(ms)) { + using (ZipOutputStream stream = new ZipOutputStream(ms)) + { Assert.IsTrue(stream.IsStreamOwner, "Should be stream owner by default"); stream.IsStreamOwner = false; } @@ -243,19 +378,26 @@ public void BaseClosedAfterFailure() Assert.IsFalse(ms.IsClosed, "Underlying stream should NOT be closed initially"); bool blewUp = false; - try { - using (ZipOutputStream stream = new ZipOutputStream(ms)) { + try + { + using (ZipOutputStream stream = new ZipOutputStream(ms)) + { Assert.IsTrue(stream.IsStreamOwner, "Should be stream owner by default"); - try { + try + { stream.PutNextEntry(new ZipEntry("Tiny")); stream.Write(new byte[32], 0, 32); - } finally { + } + finally + { Assert.IsFalse(ms.IsClosed, "Stream should still not be closed."); stream.Close(); Assert.Fail("Exception not thrown"); } } - } catch { + } + catch + { blewUp = true; } @@ -265,122 +407,184 @@ public void BaseClosedAfterFailure() [Test] [Category("Zip")] - [Ignore("TODO : Fix this")] + [Category("Performance")] + [Explicit("Long Running")] public void WriteThroughput() { - outStream_ = new ZipOutputStream(new NullStream()); - - DateTime startTime = DateTime.Now; - - long target = 0x10000000; - - writeTarget_ = target; - outStream_.PutNextEntry(new ZipEntry("0")); - WriteTargetBytes(); - - outStream_.Close(); - - DateTime endTime = DateTime.Now; - TimeSpan span = endTime - startTime; - Console.WriteLine("Time {0} throughput {1} KB/Sec", span, (target / 1024.0) / span.TotalSeconds); + PerformanceTesting.TestWrite(0x10000000, bs => + { + var zos = new ZipOutputStream(bs); + zos.PutNextEntry(new ZipEntry("0")); + return zos; + }); } - [Test] + [Test] [Category("Zip")] - [Category("Long Running")] - [Ignore("TODO : Fix this")] + [Category("Performance")] + [Explicit("Long Running")] public void SingleLargeEntry() { - window_ = new WindowedStream(0x10000); - outStream_ = new ZipOutputStream(window_); - inStream_ = new ZipInputStream(window_); - - long target = 0x10000000; - readTarget_ = writeTarget_ = target; + const string entryName = "CantSeek"; + + PerformanceTesting.TestReadWrite( + size: TestDataSize.Large, + input: bs => + { + var zis = new ZipInputStream(bs); + var entry = zis.GetNextEntry(); + + Assert.AreEqual(entryName, entry.Name); + Assert.IsTrue((entry.Flags & (int)GeneralBitFlags.Descriptor) != 0); + return zis; + }, + output: bs => + { + var zos = new ZipOutputStream(bs); + zos.PutNextEntry(new ZipEntry(entryName)); + return zos; + } + ); + } - Thread reader = new Thread(Reader); - reader.Name = "Reader"; + const string BZip2CompressedZip = + "UEsDBC4AAAAMAEyxgU5p3ou9JwAAAAcAAAAFAAAAYS5kYXRCWmg5MUFZJlNZ0buMcAAAAkgACABA" + + "ACAAIQCCCxdyRThQkNG7jHBQSwECMwAuAAAADABMsYFOad6LvScAAAAHAAAABQAAAAAAAAAAAAAA" + + "AAAAAAAAYS5kYXRQSwUGAAAAAAEAAQAzAAAASgAAAAAA"; - Thread writer = new Thread(Writer); - writer.Name = "Writer"; + /// + /// Should fail to read a zip with BZip2 compression + /// + [Test] + [Category("Zip")] + public void ShouldReadBZip2EntryButNotDecompress() + { + var fileBytes = Convert.FromBase64String(BZip2CompressedZip); - DateTime startTime = DateTime.Now; - reader.Start(); - writer.Start(); + using var input = new MemoryStream(fileBytes, writable: false); + var zis = new ZipInputStream(input); + var entry = zis.GetNextEntry(); - writer.Join(); - reader.Join(); + Assert.That(entry.Name, Is.EqualTo("a.dat"), "Should be able to get entry name"); + Assert.That(entry.CompressionMethod, Is.EqualTo(CompressionMethod.BZip2), "Entry should be BZip2 compressed"); + Assert.That(zis.CanDecompressEntry, Is.False, "Should not be able to decompress BZip2 entry"); - DateTime endTime = DateTime.Now; - TimeSpan span = endTime - startTime; - Console.WriteLine("Time {0} throughput {1} KB/Sec", span, (target / 1024.0) / span.TotalSeconds); + var buffer = new byte[1]; + Assert.Throws(() => zis.Read(buffer, 0, 1), "Trying to read the stream should throw"); } - void Reader() + /// + /// Test for https://github.com/icsharpcode/SharpZipLib/issues/341 + /// Should be able to read entries whose names contain invalid filesystem + /// characters + /// + [Test] + [Category("Zip")] + public void ShouldBeAbleToReadEntriesWithInvalidFileNames() { - const int Size = 8192; - int readBytes = 1; - byte[] buffer = new byte[Size]; - - long passifierLevel = readTarget_ - 0x10000000; - ZipEntry single = inStream_.GetNextEntry(); - - Assert.AreEqual(single.Name, "CantSeek"); - Assert.IsTrue((single.Flags & (int)GeneralBitFlags.Descriptor) != 0); - - while ((readTarget_ > 0) && (readBytes > 0)) { - int count = Size; - if (count > readTarget_) { - count = (int)readTarget_; + var testFileName = ".txt"; + + using (var memoryStream = new MemoryStream()) + { + using (var outStream = new ZipOutputStream(memoryStream)) + { + outStream.IsStreamOwner = false; + outStream.PutNextEntry(new ZipEntry(testFileName)); } - readBytes = inStream_.Read(buffer, 0, count); - readTarget_ -= readBytes; + memoryStream.Seek(0, SeekOrigin.Begin); - if (readTarget_ <= passifierLevel) { - Console.WriteLine("Reader {0} bytes remaining", readTarget_); - passifierLevel = readTarget_ - 0x10000000; + using (var inStream = new ZipInputStream(memoryStream)) + { + var entry = inStream.GetNextEntry(); + Assert.That(entry.Name, Is.EqualTo(testFileName), "output name must match original name"); } } + } - Assert.IsTrue(window_.IsClosed, "Window should be closed"); + /// + /// Test for https://github.com/icsharpcode/SharpZipLib/issues/507 + /// + [Test] + [Category("Zip")] + public void AddingAnAESEntryWithNoPasswordShouldThrow() + { + using var memoryStream = new MemoryStream(); + using var outStream = new ZipOutputStream(memoryStream); + var newEntry = new ZipEntry("test") { AESKeySize = 256 }; - // This shouldnt read any data but should read the footer - readBytes = inStream_.Read(buffer, 0, 1); - Assert.AreEqual(0, readBytes, "Stream should be empty"); - Assert.AreEqual(0, window_.Length, "Window should be closed"); - inStream_.Close(); + Assert.Throws(() => outStream.PutNextEntry(newEntry)); } - void WriteTargetBytes() + [Test] + [Category("Zip")] + public void ShouldThrowDescriptiveExceptionOnUncompressedDescriptorEntry() { - const int Size = 8192; + using var ms = new MemoryStreamWithoutSeek(); + using (var zos = new ZipOutputStream(ms)) + { + zos.IsStreamOwner = false; + var entry = new ZipEntry("testentry"); + entry.CompressionMethod = CompressionMethod.Stored; + entry.Flags |= (int)GeneralBitFlags.Descriptor; + zos.PutNextEntry(entry); + zos.Write(new byte[1], 0, 1); + zos.CloseEntry(); + } - byte[] buffer = new byte[Size]; + // Patch the Compression Method, since ZipOutputStream automatically changes it to Deflate when descriptors are used + ms.Seek(8, SeekOrigin.Begin); + ms.WriteByte((byte)CompressionMethod.Stored); + ms.Seek(0, SeekOrigin.Begin); - while (writeTarget_ > 0) { - int thisTime = Size; - if (thisTime > writeTarget_) { - thisTime = (int)writeTarget_; - } + using (var zis = new ZipInputStream(ms)) + { + zis.IsStreamOwner = false; + var buf = new byte[32]; + zis.GetNextEntry(); - outStream_.Write(buffer, 0, thisTime); - writeTarget_ -= thisTime; + Assert.Throws(typeof(StreamUnsupportedException), () => + { + zis.Read(buf, 0, buf.Length); + }); } } - - void Writer() + + [Test] + [Category("Zip")] + public void IteratingOverEntriesInDirectUpdatedArchive([Values(0x0, 0x80)] byte padding) { - outStream_.PutNextEntry(new ZipEntry("CantSeek")); - WriteTargetBytes(); - outStream_.Close(); - } - - WindowedStream window_; - ZipOutputStream outStream_; - ZipInputStream inStream_; - long readTarget_; - long writeTarget_; + using (var tempFile = new TempFile()) + { + using (var zf = ZipFile.Create(tempFile)) + { + zf.BeginUpdate(); + // Add a "large" file, where the bottom 1023 bytes will become padding + var contentsAndPadding = Enumerable.Repeat(padding, count: 1024).ToArray(); + zf.Add(new MemoryDataSource(contentsAndPadding), "FirstFile", CompressionMethod.Stored); + // Add a second file after the first one + zf.Add(new StringMemoryDataSource("fileContents"), "SecondFile", CompressionMethod.Stored); + zf.CommitUpdate(); + } + // Since ZipFile doesn't support UpdateCommand.Modify yet we'll have to simulate it by patching the header + Utils.PatchFirstEntrySize(tempFile.Open(FileMode.Open), 1); + + // Iterate updated entries + using (var fs = File.OpenRead(tempFile)) + using (var zis = new ZipInputStream(fs)) + { + var firstEntry = zis.GetNextEntry(); + Assert.NotNull(firstEntry); + Assert.AreEqual(1, firstEntry.CompressedSize); + Assert.AreEqual(1, firstEntry.Size); + + var secondEntry = zis.GetNextEntry(); + Assert.NotNull(secondEntry, "Zip entry following padding not found"); + var contents = new StreamReader(zis, Encoding.UTF8, false, 128, true).ReadToEnd(); + Assert.AreEqual("fileContents", contents); + } + } + } } } diff --git a/test/ICSharpCode.SharpZipLib.Tests/Zip/WindowsNameTransformHandling.cs b/test/ICSharpCode.SharpZipLib.Tests/Zip/WindowsNameTransformHandling.cs index 6eccd9d95..8e6941251 100644 --- a/test/ICSharpCode.SharpZipLib.Tests/Zip/WindowsNameTransformHandling.cs +++ b/test/ICSharpCode.SharpZipLib.Tests/Zip/WindowsNameTransformHandling.cs @@ -1,13 +1,21 @@ -using System; -using System.IO; -using ICSharpCode.SharpZipLib.Zip; +using ICSharpCode.SharpZipLib.Zip; using NUnit.Framework; +using System; +using System.IO; +using System.Runtime.InteropServices; namespace ICSharpCode.SharpZipLib.Tests.Zip { [TestFixture] public class WindowsNameTransformHandling : TransformBase { + [OneTimeSetUp] + public void TestInit() { + if (Path.DirectorySeparatorChar != '\\') { + Assert.Inconclusive("WindowsNameTransform will not work on platforms not using '\\' directory separators"); + } + } + [Test] public void BasicFiles() { @@ -36,10 +44,13 @@ public void NameTooLong() { var wnt = new WindowsNameTransform(); var veryLong = new string('x', 261); - try { + try + { wnt.TransformDirectory(veryLong); Assert.Fail("Expected an exception"); - } catch (PathTooLongException) { + } + catch (PathTooLongException) + { } } @@ -48,9 +59,12 @@ public void LengthBoundaryOk() { var wnt = new WindowsNameTransform(); string veryLong = "c:\\" + new string('x', 260); - try { + try + { string transformed = wnt.TransformDirectory(veryLong); - } catch { + } + catch + { Assert.Fail("Expected no exception"); } } @@ -59,34 +73,49 @@ public void LengthBoundaryOk() public void ReplacementChecking() { var wnt = new WindowsNameTransform(); - try { + try + { wnt.Replacement = '*'; Assert.Fail("Expected an exception"); - } catch (ArgumentException) { + } + catch (ArgumentException) + { } - try { + try + { wnt.Replacement = '?'; Assert.Fail("Expected an exception"); - } catch (ArgumentException) { + } + catch (ArgumentException) + { } - try { + try + { wnt.Replacement = ':'; Assert.Fail("Expected an exception"); - } catch (ArgumentException) { + } + catch (ArgumentException) + { } - try { + try + { wnt.Replacement = '/'; Assert.Fail("Expected an exception"); - } catch (ArgumentException) { + } + catch (ArgumentException) + { } - try { + try + { wnt.Replacement = '\\'; Assert.Fail("Expected an exception"); - } catch (ArgumentException) { + } + catch (ArgumentException) + { } } diff --git a/test/ICSharpCode.SharpZipLib.Tests/Zip/ZipCorruptionHandling.cs b/test/ICSharpCode.SharpZipLib.Tests/Zip/ZipCorruptionHandling.cs new file mode 100644 index 000000000..07d192fb0 --- /dev/null +++ b/test/ICSharpCode.SharpZipLib.Tests/Zip/ZipCorruptionHandling.cs @@ -0,0 +1,71 @@ +using System; +using System.IO; +using System.Threading; +using ICSharpCode.SharpZipLib.Zip; +using NUnit.Framework; + +namespace ICSharpCode.SharpZipLib.Tests.Zip +{ + public class ZipCorruptionHandling + { + + const string TestFileZeroCodeLength = "UEsDBBQA+AAIANwyZ0U5T8HwjQAAAL8AAAAIABUAbGltZXJpY2t" + + "VVAkAAzBXXFR6LmBUVXgEAPQB9AEFjTEOwjAQBHu/YkVDg3gHoUaivjgHtmKfI5+D5d9zbndHM6/AldFJQTIJ" + + "PrVkPOkgce9QlJFi5hr9rhD+cUUvZ9qgnuRuBAtId97Qw0AL1Kbw5h6MykeKdlyWdlWs7OlUdgsodRqKVo0v8" + + "JWyGWZ6mLpuiii2t2Bl0mZ54QksOIpqXNPATF/eH1BLAQIXAxQAAgAIANxQZ0U5T8HwjQAAAL8AAAAIAA0AAA" + + "AAAAEAAACggQAAAABsaW1lcgEAQwAAAMgAAAAAAA=="; + + [Test] + [Category("Zip")] + public void ZeroCodeLengthZipFile() + { + Assert.Throws(() => { + Exception threadException = null; + var testThread = new Thread(() => { + try { + var fileBytes = Convert.FromBase64String(TestFileZeroCodeLength); + using (var ms = new MemoryStream(fileBytes)) + using (var zip = new ZipInputStream(ms)) + { + while (zip.GetNextEntry() != null) { } + } + } + catch (Exception x) { + threadException = x; + } + }); + + testThread.Start(); + + if(!testThread.Join(5000)){ + // Aborting threads is deprecated in .NET Core, but if we don't do this, + // the poor CI will hang for 2 hours upon running this test + ThreadEx.Abort(testThread); + throw new TimeoutException("Timed out waiting for GetNextEntry() to return"); + } + else if(threadException != null){ + throw threadException; + } + }); + } + + const string TestFileBadCDGoodCD64 = @"UEsDBC0AAAAIANhy+k4cj+r8//////////8IABQAdGVzdGZpbGUBABAAAAA + AAAAAAAAUAAAAAAAAACtJLS5Jy8xJVUjOzytJzSsp5gIAUEsBAjMALQAAAAgA2HL6ThyP6vz//////////wgAFAAAAAAAA + AAAAAAAAAAAAHRlc3RmaWxlAQAQABIAAAAAAAAAFAAAAAAAAABQSwUGAAAAAAEAAQBKAAAATgAAAAAA"; + + [Test] + [Category("Zip")] + public void CorruptCentralDirWithCorrectZip64CD() + { + var fileBytes = Convert.FromBase64String(TestFileBadCDGoodCD64); + using (var ms = new MemoryStream(fileBytes)) + using (var zip = new ZipFile(ms)) + { + Assert.AreEqual(1, zip.Count); + Assert.AreNotEqual(0, zip[0].Size, "Uncompressed file size read from corrupt CentralDir instead of CD64"); + } + } + + } + +} \ No newline at end of file diff --git a/test/ICSharpCode.SharpZipLib.Tests/Zip/ZipEncryptionHandling.cs b/test/ICSharpCode.SharpZipLib.Tests/Zip/ZipEncryptionHandling.cs new file mode 100644 index 000000000..0cf7395cb --- /dev/null +++ b/test/ICSharpCode.SharpZipLib.Tests/Zip/ZipEncryptionHandling.cs @@ -0,0 +1,582 @@ +using ICSharpCode.SharpZipLib.Zip; +using NUnit.Framework; +using System; +using System.IO; +using System.Text; +using ICSharpCode.SharpZipLib.Tests.TestSupport; +using System.Threading.Tasks; +using Does = ICSharpCode.SharpZipLib.Tests.TestSupport.Does; + +namespace ICSharpCode.SharpZipLib.Tests.Zip +{ + [TestFixture] + public class ZipEncryptionHandling + { + [Test] + [Category("Encryption")] + [Category("Zip")] + [TestCase(CompressionMethod.Stored)] + [TestCase(CompressionMethod.Deflated)] + public void Aes128Encryption(CompressionMethod compressionMethod) + { + CreateZipWithEncryptedEntries("foo", 128, compressionMethod); + } + + [Test] + [Category("Encryption")] + [Category("Zip")] + [TestCase(CompressionMethod.Stored)] + [TestCase(CompressionMethod.Deflated)] + public void Aes256Encryption(CompressionMethod compressionMethod) + { + CreateZipWithEncryptedEntries("foo", 256, compressionMethod); + } + + [Test] + [Category("Encryption")] + [Category("Zip")] + [TestCase(CompressionMethod.Stored)] + [TestCase(CompressionMethod.Deflated)] + public void ZipCryptoEncryption(CompressionMethod compressionMethod) + { + CreateZipWithEncryptedEntries("foo", 0, compressionMethod); + } + + /// + /// Test Known zero length encrypted entries with ZipOutputStream. + /// These are entries where the entry size is set to 0 ahead of time, so that PutNextEntry will fill in the header and there will be no patching. + /// Test with Zip64 on and off, as the logic is different for the two. + /// + [Test] + public void ZipOutputStreamEncryptEmptyEntries( + [Values] UseZip64 useZip64, + [Values(0, 128, 256)] int keySize, + [Values(CompressionMethod.Stored, CompressionMethod.Deflated)] CompressionMethod compressionMethod) + { + using (var ms = new MemoryStream()) + { + using (var zipOutputStream = new ZipOutputStream(ms)) + { + zipOutputStream.IsStreamOwner = false; + zipOutputStream.Password = "password"; + zipOutputStream.UseZip64 = useZip64; + + ZipEntry zipEntry = new ZipEntry("emptyEntry") + { + AESKeySize = keySize, + CompressionMethod = compressionMethod, + CompressedSize = 0, + Crc = 0, + Size = 0, + }; + + zipOutputStream.PutNextEntry(zipEntry); + zipOutputStream.CloseEntry(); + } + + SevenZipHelper.VerifyZipWith7Zip(ms, "password"); + } + } + + [Test] + [Category("Encryption")] + [Category("Zip")] + public void ZipFileAesDecryption() + { + var password = "password"; + + using (var ms = new MemoryStream()) + { + WriteEncryptedZipToStream(ms, password, 256); + + var zipFile = new ZipFile(ms) + { + Password = password + }; + + foreach (ZipEntry entry in zipFile) + { + if (!entry.IsFile) continue; + + using (var zis = zipFile.GetInputStream(entry)) + using (var sr = new StreamReader(zis, Encoding.UTF8)) + { + var content = sr.ReadToEnd(); + Assert.AreEqual(DummyDataString, content, "Decompressed content does not match input data"); + } + } + + Assert.That(zipFile, Does.PassTestArchive(testData: false), "Encrypted archive should pass validation."); + } + } + + [Test] + [Category("Encryption")] + [Category("Zip")] + public void ZipFileAesRead() + { + var password = "password"; + + using (var ms = new SingleByteReadingStream()) + { + WriteEncryptedZipToStream(ms, password, 256); + ms.Seek(0, SeekOrigin.Begin); + + var zipFile = new ZipFile(ms) + { + Password = password + }; + + foreach (ZipEntry entry in zipFile) + { + if (!entry.IsFile) continue; + + using (var zis = zipFile.GetInputStream(entry)) + using (var sr = new StreamReader(zis, Encoding.UTF8)) + { + var content = sr.ReadToEnd(); + Assert.AreEqual(DummyDataString, content, "Decompressed content does not match input data"); + } + } + } + } + + /// + /// Test using AES encryption on a file whose contents are Stored rather than deflated + /// + [Test] + [Category("Encryption")] + [Category("Zip")] + public void ZipFileStoreAes() + { + string password = "password"; + + // Make an encrypted zip file + using (var memoryStream = MakeAESEncryptedZipStream(password)) + { + // try to read it + var zipFile = new ZipFile(memoryStream, leaveOpen: true) + { + Password = password + }; + + foreach (ZipEntry entry in zipFile) + { + if (!entry.IsFile) continue; + + // Should be stored rather than deflated + Assert.That(entry.CompressionMethod, Is.EqualTo(CompressionMethod.Stored), "Entry should be stored"); + + using (var zis = zipFile.GetInputStream(entry)) + using (var sr = new StreamReader(zis, Encoding.UTF8)) + { + var content = sr.ReadToEnd(); + Assert.That(content, Is.EqualTo(DummyDataString), "Decompressed content does not match input data"); + } + } + } + } + + /// + /// As , but with Async reads + /// + [Test] + [Category("Encryption")] + [Category("Zip")] + public async Task ZipFileStoreAesAsync() + { + string password = "password"; + + // Make an encrypted zip file + using (var memoryStream = MakeAESEncryptedZipStream(password)) + { + // try to read it + var zipFile = new ZipFile(memoryStream, leaveOpen: true) + { + Password = password + }; + + foreach (ZipEntry entry in zipFile) + { + // Should be stored rather than deflated + Assert.That(entry.CompressionMethod, Is.EqualTo(CompressionMethod.Stored), "Entry should be stored"); + + using (var zis = zipFile.GetInputStream(entry)) + { + using (var inputStream = zipFile.GetInputStream(entry)) + using (var sr = new StreamReader(zis, Encoding.UTF8)) + { + var content = await sr.ReadToEndAsync(); + Assert.That(content, Is.EqualTo(DummyDataString), "Decompressed content does not match input data"); + } + } + } + } + } + + // Shared helper for the ZipFileStoreAes tests + private static Stream MakeAESEncryptedZipStream(string password) + { + var memoryStream = new MemoryStream(); + + // Try to create a zip stream + WriteEncryptedZipToStream(memoryStream, password, 256, CompressionMethod.Stored); + + // reset + memoryStream.Seek(0, SeekOrigin.Begin); + + return memoryStream; + } + + /// + /// Test using AES encryption on a file whose contents are Stored rather than deflated + /// + [Test] + [Category("Encryption")] + [Category("Zip")] + public void ZipFileStoreAesPartialRead([Values(1, 7, 17)] int readSize) + { + string password = "password"; + + using (var memoryStream = new MemoryStream()) + { + // Try to create a zip stream + WriteEncryptedZipToStream(memoryStream, password, 256, CompressionMethod.Stored); + + // reset + memoryStream.Seek(0, SeekOrigin.Begin); + + // try to read it + var zipFile = new ZipFile(memoryStream, leaveOpen: true) + { + Password = password + }; + + foreach (ZipEntry entry in zipFile) + { + if (!entry.IsFile) continue; + + // Should be stored rather than deflated + Assert.That(entry.CompressionMethod, Is.EqualTo(CompressionMethod.Stored), "Entry should be stored"); + + using (var ms = new MemoryStream()) + { + using (var zis = zipFile.GetInputStream(entry)) + { + byte[] buffer = new byte[readSize]; + + while (true) + { + int read = zis.Read(buffer, 0, readSize); + + if (read == 0) + break; + + ms.Write(buffer, 0, read); + } + } + + ms.Seek(0, SeekOrigin.Begin); + + using (var sr = new StreamReader(ms, Encoding.UTF8)) + { + var content = sr.ReadToEnd(); + Assert.That(content, Is.EqualTo(DummyDataString), "Decompressed content does not match input data"); + } + } + } + } + } + + /// + /// Test adding files to an encrypted zip + /// + [Test] + [Category("Encryption")] + [Category("Zip")] + public void ZipFileAesAdd() + { + string password = "password"; + string testData = "AdditionalData"; + int keySize = 256; + + using (var memoryStream = new MemoryStream()) + { + // Try to create a zip stream + WriteEncryptedZipToStream(memoryStream, password, keySize, CompressionMethod.Deflated); + + // reset + memoryStream.Seek(0, SeekOrigin.Begin); + + // Update the archive with ZipFile + { + using (var zipFile = new ZipFile(memoryStream, leaveOpen: true) { Password = password }) + { + zipFile.BeginUpdate(); + zipFile.Add(new StringMemoryDataSource(testData), "AdditionalEntry", CompressionMethod.Deflated); + zipFile.CommitUpdate(); + } + } + + // Test the updated archive + { + memoryStream.Seek(0, SeekOrigin.Begin); + + using (var zipFile = new ZipFile(memoryStream, leaveOpen: true) { Password = password }) + { + Assert.That(zipFile.Count, Is.EqualTo(2), "Incorrect entry count in updated archive"); + + // Disabled because of bug #317 + // Assert.That(zipFile.TestArchive(true), Is.True); + + // Check the original entry + { + var originalEntry = zipFile.GetEntry("test"); + Assert.That(originalEntry.IsCrypted, Is.True); + Assert.That(originalEntry.AESKeySize, Is.EqualTo(keySize)); + + + using (var zis = zipFile.GetInputStream(originalEntry)) + using (var sr = new StreamReader(zis, Encoding.UTF8)) + { + var content = sr.ReadToEnd(); + Assert.That(content, Is.EqualTo(DummyDataString), "Decompressed content does not match input data"); + } + } + + // Check the additional entry + // This should be encrypted, though currently only with ZipCrypto + { + var additionalEntry = zipFile.GetEntry("AdditionalEntry"); + Assert.That(additionalEntry.IsCrypted, Is.True); + + using (var zis = zipFile.GetInputStream(additionalEntry)) + using (var sr = new StreamReader(zis, Encoding.UTF8)) + { + var content = sr.ReadToEnd(); + Assert.That(content, Is.EqualTo(testData), "Decompressed content does not match input data"); + } + } + } + } + + // As an extra test, verify the file with 7-zip + SevenZipHelper.VerifyZipWith7Zip(memoryStream, password); + } + } + + /// + /// Test deleting files from an encrypted zip + /// + [Test] + [Category("Encryption")] + [Category("Zip")] + public void ZipFileAesDelete() + { + string password = "password"; + int keySize = 256; + + using (var memoryStream = new MemoryStream()) + { + // Try to create a zip stream + WriteEncryptedZipToStream(memoryStream, 3, password, keySize, CompressionMethod.Deflated); + + // reset + memoryStream.Seek(0, SeekOrigin.Begin); + + // delete one of the entries from the file + { + using (var zipFile = new ZipFile(memoryStream, leaveOpen: true) { Password = password }) + { + // Must have 3 entries to start with + Assert.That(zipFile.Count, Is.EqualTo(3), "Must have 3 entries to start with"); + + var entryToDelete = zipFile.GetEntry("test-1"); + Assert.That(entryToDelete, Is.Not.Null, "the entry that we want to delete must exist"); + + zipFile.BeginUpdate(); + zipFile.Delete(entryToDelete); + zipFile.CommitUpdate(); + } + } + + // Test the updated archive + { + memoryStream.Seek(0, SeekOrigin.Begin); + + using (var zipFile = new ZipFile(memoryStream, leaveOpen: true) { Password = password }) + { + // We should now only have 2 files + Assert.That(zipFile.Count, Is.EqualTo(2), "Incorrect entry count in updated archive"); + + // Disabled because of bug #317 + // Assert.That(zipFile.TestArchive(true), Is.True); + + // Check the first entry + { + var originalEntry = zipFile.GetEntry("test-0"); + Assert.That(originalEntry.IsCrypted, Is.True); + Assert.That(originalEntry.AESKeySize, Is.EqualTo(keySize)); + + + using (var zis = zipFile.GetInputStream(originalEntry)) + using (var sr = new StreamReader(zis, Encoding.UTF8)) + { + var content = sr.ReadToEnd(); + Assert.That(content, Is.EqualTo(DummyDataString), "Decompressed content does not match input data"); + } + } + + // Check the second entry + { + var originalEntry = zipFile.GetEntry("test-2"); + Assert.That(originalEntry.IsCrypted, Is.True); + Assert.That(originalEntry.AESKeySize, Is.EqualTo(keySize)); + + + using (var zis = zipFile.GetInputStream(originalEntry)) + using (var sr = new StreamReader(zis, Encoding.UTF8)) + { + var content = sr.ReadToEnd(); + Assert.That(content, Is.EqualTo(DummyDataString), "Decompressed content does not match input data"); + } + } + } + } + + // As an extra test, verify the file with 7-zip + SevenZipHelper.VerifyZipWith7Zip(memoryStream, password); + } + } + + // This is a zip file with one AES encrypted entry, whose password in an empty string. + const string TestFileWithEmptyPassword = @"UEsDBDMACQBjACaj0FAyKbop//////////8EAB8AdGVzdAEAEAA4AAAA + AAAAAFIAAAAAAAAAAZkHAAIAQUUDCABADvo3YqmCtIE+lhw26kjbqkGsLEOk6bVA+FnSpVD4yGP4Mr66Hs14aTtsPUaANX2 + Z6qZczEmwoaNQpNBnKl7p9YOG8GSHDfTCUU/AZvT4yGFhUEsHCDIpuilSAAAAAAAAADgAAAAAAAAAUEsBAjMAMwAJAGMAJq + PQUDIpuin//////////wQAHwAAAAAAAAAAAAAAAAAAAHRlc3QBABAAOAAAAAAAAABSAAAAAAAAAAGZBwACAEFFAwgAUEsFBgAAAAABAAEAUQAAAKsAAAAAAA=="; + + /// + /// Test reading an AES encrypted entry whose password is an empty string. + /// + /// + /// Test added for https://github.com/icsharpcode/SharpZipLib/issues/471. + /// + [Test] + [Category("Zip")] + public void ZipFileAESReadWithEmptyPassword() + { + var fileBytes = Convert.FromBase64String(TestFileWithEmptyPassword); + + using (var ms = new MemoryStream(fileBytes)) + using (var zipFile = new ZipFile(ms, leaveOpen: true)) + { + zipFile.Password = string.Empty; + + var entry = zipFile.FindEntry("test", true); + + using (var inputStream = zipFile.GetInputStream(entry)) + using (var sr = new StreamReader(inputStream, Encoding.UTF8)) + { + var content = sr.ReadToEnd(); + Assert.That(content, Is.EqualTo("Lorem ipsum dolor sit amet, consectetur adipiscing elit."), "Decompressed content does not match expected data"); + } + } + } + + /// + /// ZipInputStream can't decrypt AES encrypted entries, but it should report that to the caller + /// rather than just failing. + /// + [Test] + [Category("Zip")] + public void ZipinputStreamShouldGracefullyFailWithAESStreams() + { + string password = "password"; + + using (var memoryStream = new MemoryStream()) + { + // Try to create a zip stream + WriteEncryptedZipToStream(memoryStream, password, 256); + + // reset + memoryStream.Seek(0, SeekOrigin.Begin); + + // Try to read + using (var inputStream = new ZipInputStream(memoryStream)) + { + inputStream.Password = password; + var entry = inputStream.GetNextEntry(); + Assert.That(entry.AESKeySize, Is.EqualTo(256), "Test entry should be AES256 encrypted."); + + // CanDecompressEntry should be false. + Assert.That(inputStream.CanDecompressEntry, Is.False, "CanDecompressEntry should be false for AES encrypted entries"); + + // Should throw on read. + Assert.Throws(() => inputStream.ReadByte()); + } + } + } + + public static void WriteEncryptedZipToStream(Stream stream, string password, int keySize, CompressionMethod compressionMethod = CompressionMethod.Deflated) + { + using (var zs = new ZipOutputStream(stream)) + { + zs.IsStreamOwner = false; + zs.SetLevel(9); // 0-9, 9 being the highest level of compression + zs.Password = password; // optional. Null is the same as not setting. Required if using AES. + + AddEncrypedEntryToStream(zs, $"test", keySize, compressionMethod); + } + } + + public void WriteEncryptedZipToStream(Stream stream, int entryCount, string password, int keySize, CompressionMethod compressionMethod) + { + using (var zs = new ZipOutputStream(stream)) + { + zs.IsStreamOwner = false; + zs.SetLevel(9); // 0-9, 9 being the highest level of compression + zs.Password = password; // optional. Null is the same as not setting. Required if using AES. + + for (int i = 0; i < entryCount; i++) + { + AddEncrypedEntryToStream(zs, $"test-{i}", keySize, compressionMethod); + } + } + } + + private static void AddEncrypedEntryToStream(ZipOutputStream zipOutputStream, string entryName, int keySize, CompressionMethod compressionMethod) + { + ZipEntry zipEntry = new ZipEntry(entryName) + { + AESKeySize = keySize, + DateTime = DateTime.Now, + CompressionMethod = compressionMethod + }; + + zipOutputStream.PutNextEntry(zipEntry); + + byte[] dummyData = Encoding.UTF8.GetBytes(DummyDataString); + + using (var dummyStream = new MemoryStream(dummyData)) + { + dummyStream.CopyTo(zipOutputStream); + } + + zipOutputStream.CloseEntry(); + } + + public void CreateZipWithEncryptedEntries(string password, int keySize, CompressionMethod compressionMethod = CompressionMethod.Deflated) + { + using (var ms = new MemoryStream()) + { + WriteEncryptedZipToStream(ms, password, keySize, compressionMethod); + SevenZipHelper.VerifyZipWith7Zip(ms, password); + } + } + + private const string DummyDataString = @"Lorem ipsum dolor sit amet, consectetur adipiscing elit. +Fusce bibendum diam ac nunc rutrum ornare. Maecenas blandit elit ligula, eget suscipit lectus rutrum eu. +Maecenas aliquam, purus mattis pulvinar pharetra, nunc orci maximus justo, sed facilisis massa dui sed lorem. +Vestibulum id iaculis leo. Duis porta ante lorem. Duis condimentum enim nec lorem tristique interdum. Fusce in faucibus libero."; + } +} diff --git a/test/ICSharpCode.SharpZipLib.Tests/Zip/ZipEntryFactoryHandling.cs b/test/ICSharpCode.SharpZipLib.Tests/Zip/ZipEntryFactoryHandling.cs index 8066a1934..5a892abde 100644 --- a/test/ICSharpCode.SharpZipLib.Tests/Zip/ZipEntryFactoryHandling.cs +++ b/test/ICSharpCode.SharpZipLib.Tests/Zip/ZipEntryFactoryHandling.cs @@ -1,7 +1,7 @@ -using System; -using System.IO; -using ICSharpCode.SharpZipLib.Zip; +using ICSharpCode.SharpZipLib.Zip; using NUnit.Framework; +using System; +using System.IO; namespace ICSharpCode.SharpZipLib.Tests.Zip { @@ -87,91 +87,181 @@ public void CreateInMemoryValues() [Test] [Category("Zip")] [Category("CreatesTempFile")] - public void CreatedValues() + [Platform("Win32NT")] + public void CreatedFileEntriesUsesExpectedAttributes() + { + string tempDir = GetTempFilePath(); + if (tempDir == null) Assert.Inconclusive("No permission to execute this test?"); + + tempDir = Path.Combine(tempDir, "SharpZipTest"); + Directory.CreateDirectory(tempDir); + + try + { + string tempFile = Path.Combine(tempDir, "SharpZipTest.Zip"); + + using (FileStream f = File.Create(tempFile, 1024)) + { + f.WriteByte(0); + } + + FileAttributes attributes = FileAttributes.Hidden; + + File.SetAttributes(tempFile, attributes); + ZipEntryFactory factory = null; + ZipEntry entry; + int combinedAttributes = 0; + + try + { + factory = new ZipEntryFactory(); + + factory.GetAttributes = ~((int)FileAttributes.ReadOnly); + factory.SetAttributes = (int)FileAttributes.ReadOnly; + combinedAttributes = (int)(FileAttributes.ReadOnly | FileAttributes.Hidden); + + entry = factory.MakeFileEntry(tempFile); + Assert.AreEqual(entry.ExternalFileAttributes, combinedAttributes); + Assert.AreEqual(1, entry.Size); + } + finally + { + File.Delete(tempFile); + } + } + finally + { + Directory.Delete(tempDir, true); + } + + } + + [Test] + [Category("Zip")] + [Category("CreatesTempFile")] + [TestCase(ZipEntryFactory.TimeSetting.CreateTime)] + [TestCase(ZipEntryFactory.TimeSetting.LastAccessTime)] + [TestCase(ZipEntryFactory.TimeSetting.LastWriteTime)] + public void CreatedFileEntriesUsesExpectedTime(ZipEntryFactory.TimeSetting timeSetting) + { + string tempDir = GetTempFilePath(); + if (tempDir == null) Assert.Inconclusive("No permission to execute this test?"); + + tempDir = Path.Combine(tempDir, "SharpZipTest"); + + // Note the seconds returned will be even! + var expectedTime = new DateTime(2100, 2, 27, 11, 07, 56); + + Directory.CreateDirectory(tempDir); + + try + { + + string tempFile = Path.Combine(tempDir, "SharpZipTest.Zip"); + + using (FileStream f = File.Create(tempFile, 1024)) + { + f.WriteByte(0); + } + + DateTime fileTime = DateTime.MinValue; + + if (timeSetting == ZipEntryFactory.TimeSetting.CreateTime) { + File.SetCreationTime(tempFile, expectedTime); + fileTime = File.GetCreationTime(tempFile); + } + + if (timeSetting == ZipEntryFactory.TimeSetting.LastAccessTime){ + File.SetLastAccessTime(tempFile, expectedTime); + fileTime = File.GetLastAccessTime(tempFile); + } + + if (timeSetting == ZipEntryFactory.TimeSetting.LastWriteTime) { + File.SetLastWriteTime(tempFile, expectedTime); + fileTime = File.GetLastWriteTime(tempFile); + } + + if(fileTime != expectedTime) { + Assert.Inconclusive("File time could not be altered"); + } + + var factory = new ZipEntryFactory(); + + factory.Setting = timeSetting; + + var entry = factory.MakeFileEntry(tempFile); + Assert.AreEqual(expectedTime, entry.DateTime); + Assert.AreEqual(1, entry.Size); + + } + finally + { + Directory.Delete(tempDir, true); + } + + } + + [Test] + [Category("Zip")] + [Category("CreatesTempFile")] + [TestCase(ZipEntryFactory.TimeSetting.CreateTime)] + [TestCase(ZipEntryFactory.TimeSetting.LastAccessTime)] + [TestCase(ZipEntryFactory.TimeSetting.LastWriteTime)] + public void CreatedDirectoryEntriesUsesExpectedTime(ZipEntryFactory.TimeSetting timeSetting) { string tempDir = GetTempFilePath(); - Assert.IsNotNull(tempDir, "No permission to execute this test?"); + if (tempDir == null) Assert.Inconclusive("No permission to execute this test?"); tempDir = Path.Combine(tempDir, "SharpZipTest"); - if (tempDir != null) { - - Directory.CreateDirectory(tempDir); - - try { - // Note the seconds returned will be even! - var createTime = new DateTime(2100, 2, 27, 11, 07, 56); - var lastWriteTime = new DateTime(2050, 11, 3, 7, 23, 32); - var lastAccessTime = new DateTime(2050, 11, 3, 0, 42, 12); - - string tempFile = Path.Combine(tempDir, "SharpZipTest.Zip"); - using (FileStream f = File.Create(tempFile, 1024)) { - f.WriteByte(0); - } - - File.SetCreationTime(tempFile, createTime); - File.SetLastWriteTime(tempFile, lastWriteTime); - File.SetLastAccessTime(tempFile, lastAccessTime); - - FileAttributes attributes = FileAttributes.Hidden; - - File.SetAttributes(tempFile, attributes); - ZipEntryFactory factory = null; - ZipEntry entry; - int combinedAttributes = 0; - - try { - factory = new ZipEntryFactory(); - - factory.Setting = ZipEntryFactory.TimeSetting.CreateTime; - factory.GetAttributes = ~((int)FileAttributes.ReadOnly); - factory.SetAttributes = (int)FileAttributes.ReadOnly; - combinedAttributes = (int)(FileAttributes.ReadOnly | FileAttributes.Hidden); - - entry = factory.MakeFileEntry(tempFile); - Assert.AreEqual(createTime, entry.DateTime, "Create time failure"); - Assert.AreEqual(entry.ExternalFileAttributes, combinedAttributes); - Assert.AreEqual(1, entry.Size); - - factory.Setting = ZipEntryFactory.TimeSetting.LastAccessTime; - entry = factory.MakeFileEntry(tempFile); - Assert.AreEqual(lastAccessTime, entry.DateTime, "Access time failure"); - Assert.AreEqual(1, entry.Size); - - factory.Setting = ZipEntryFactory.TimeSetting.LastWriteTime; - entry = factory.MakeFileEntry(tempFile); - Assert.AreEqual(lastWriteTime, entry.DateTime, "Write time failure"); - Assert.AreEqual(1, entry.Size); - } finally { - File.Delete(tempFile); - } - - // Do the same for directories - // Note the seconds returned will be even! - createTime = new DateTime(2090, 2, 27, 11, 7, 56); - lastWriteTime = new DateTime(2107, 12, 31, 23, 59, 58); - lastAccessTime = new DateTime(1980, 1, 1, 1, 0, 0); - - Directory.SetCreationTime(tempDir, createTime); - Directory.SetLastWriteTime(tempDir, lastWriteTime); - Directory.SetLastAccessTime(tempDir, lastAccessTime); - - factory.Setting = ZipEntryFactory.TimeSetting.CreateTime; - entry = factory.MakeDirectoryEntry(tempDir); - Assert.AreEqual(createTime, entry.DateTime, "Directory create time failure"); - Assert.IsTrue((entry.ExternalFileAttributes & (int)FileAttributes.Directory) == (int)FileAttributes.Directory); - - factory.Setting = ZipEntryFactory.TimeSetting.LastAccessTime; - entry = factory.MakeDirectoryEntry(tempDir); - Assert.AreEqual(lastAccessTime, entry.DateTime, "Directory access time failure"); - - factory.Setting = ZipEntryFactory.TimeSetting.LastWriteTime; - entry = factory.MakeDirectoryEntry(tempDir); - Assert.AreEqual(lastWriteTime, entry.DateTime, "Directory write time failure"); - } finally { - Directory.Delete(tempDir, true); + // Note the seconds returned will be even! + var expectedTime = new DateTime(2100, 2, 27, 11, 07, 56); + + Directory.CreateDirectory(tempDir); + + try + { + + string tempFile = Path.Combine(tempDir, "SharpZipTest.Zip"); + + using (FileStream f = File.Create(tempFile, 1024)) + { + f.WriteByte(0); + } + + DateTime dirTime = DateTime.MinValue; + + if (timeSetting == ZipEntryFactory.TimeSetting.CreateTime) { + Directory.SetCreationTime(tempFile, expectedTime); + dirTime = Directory.GetCreationTime(tempDir); + } + + if (timeSetting == ZipEntryFactory.TimeSetting.LastAccessTime){ + Directory.SetLastAccessTime(tempDir, expectedTime); + dirTime = Directory.GetLastAccessTime(tempDir); } + + if (timeSetting == ZipEntryFactory.TimeSetting.LastWriteTime) { + Directory.SetLastWriteTime(tempDir, expectedTime); + dirTime = Directory.GetLastWriteTime(tempDir); + } + + if(dirTime != expectedTime) { + Assert.Inconclusive("Directory time could not be altered"); + } + + var factory = new ZipEntryFactory(); + + factory.Setting = timeSetting; + + var entry = factory.MakeDirectoryEntry(tempDir); + Assert.AreEqual(expectedTime, entry.DateTime); + } + finally + { + Directory.Delete(tempDir, true); } + } } } diff --git a/test/ICSharpCode.SharpZipLib.Tests/Zip/ZipEntryHandling.cs b/test/ICSharpCode.SharpZipLib.Tests/Zip/ZipEntryHandling.cs index 9e126e3df..4b08f7519 100644 --- a/test/ICSharpCode.SharpZipLib.Tests/Zip/ZipEntryHandling.cs +++ b/test/ICSharpCode.SharpZipLib.Tests/Zip/ZipEntryHandling.cs @@ -1,20 +1,21 @@ -using System; +using ICSharpCode.SharpZipLib.Tests.TestSupport; +using ICSharpCode.SharpZipLib.Zip; +using NUnit.Framework; +using System; using System.IO; using System.Reflection; using System.Text; -using ICSharpCode.SharpZipLib.Tests.TestSupport; -using ICSharpCode.SharpZipLib.Zip; -using NUnit.Framework; namespace ICSharpCode.SharpZipLib.Tests.Zip { [TestFixture] public class ZipEntryHandling : ZipBase { - byte[] MakeLocalHeader(string asciiName, short versionToExtract, short flags, short method, + private byte[] MakeLocalHeader(string asciiName, short versionToExtract, short flags, short method, int dostime, int crc, int compressedSize, int size) { - using (TrackedMemoryStream ms = new TrackedMemoryStream()) { + using (TrackedMemoryStream ms = new TrackedMemoryStream()) + { ms.WriteByte((byte)'P'); ms.WriteByte((byte)'K'); ms.WriteByte(3); @@ -36,7 +37,7 @@ byte[] MakeLocalHeader(string asciiName, short versionToExtract, short flags, sh } } - ZipEntry MakeEntry(string asciiName, short versionToExtract, short flags, short method, + private ZipEntry MakeEntry(string asciiName, short versionToExtract, short flags, short method, int dostime, int crc, int compressedSize, int size) { byte[] data = MakeLocalHeader(asciiName, versionToExtract, flags, method, @@ -48,7 +49,7 @@ ZipEntry MakeEntry(string asciiName, short versionToExtract, short flags, short return ze; } - void PiecewiseCompare(ZipEntry lhs, ZipEntry rhs) + private void PiecewiseCompare(ZipEntry lhs, ZipEntry rhs) { Type entryType = typeof(ZipEntry); BindingFlags binding = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance; @@ -57,7 +58,8 @@ void PiecewiseCompare(ZipEntry lhs, ZipEntry rhs) Assert.Greater(fields.Length, 8, "Failed to find fields"); - foreach (FieldInfo info in fields) { + foreach (FieldInfo info in fields) + { object lValue = info.GetValue(lhs); object rValue = info.GetValue(rhs); @@ -193,10 +195,12 @@ public void DateAndTime() // Over the limit are set to max. ze.DateTime = new DateTime(2108, 1, 1); + ze.DosTime = ze.DosTime; Assert.AreEqual(new DateTime(2107, 12, 31, 23, 59, 58), ze.DateTime); // Under the limit are set to min. ze.DateTime = new DateTime(1906, 12, 4); + ze.DosTime = ze.DosTime; Assert.AreEqual(new DateTime(1980, 1, 1, 0, 0, 0), ze.DateTime); } diff --git a/test/ICSharpCode.SharpZipLib.Tests/Zip/ZipExtraDataHandling.cs b/test/ICSharpCode.SharpZipLib.Tests/Zip/ZipExtraDataHandling.cs index 6a0416a11..870050507 100644 --- a/test/ICSharpCode.SharpZipLib.Tests/Zip/ZipExtraDataHandling.cs +++ b/test/ICSharpCode.SharpZipLib.Tests/Zip/ZipExtraDataHandling.cs @@ -1,7 +1,7 @@ -using System; -using System.IO; -using ICSharpCode.SharpZipLib.Zip; +using ICSharpCode.SharpZipLib.Zip; using NUnit.Framework; +using System; +using System.IO; namespace ICSharpCode.SharpZipLib.Tests.Zip { @@ -41,9 +41,12 @@ public void ExceedSize() Assert.AreEqual(65535, zed.Length); bool caught = false; - try { + try + { zed.AddEntry(3, null); - } catch { + } + catch + { caught = true; } @@ -54,9 +57,12 @@ public void ExceedSize() Assert.AreEqual(65510, zed.Length); caught = false; - try { + try + { zed.AddEntry(2, new byte[22]); - } catch { + } + catch + { caught = true; } Assert.IsTrue(caught, "Expected an exception when max size exceeded"); @@ -115,7 +121,8 @@ public void BasicOperations() zed2.AddEntry(1, new byte[] { }); byte[] data = zed.GetEntryData(); - for (int i = 0; i < data.Length; ++i) { + for (int i = 0; i < data.Length; ++i) + { Assert.AreEqual(zed2.GetEntryData()[i], data[i]); } @@ -136,14 +143,16 @@ public void BasicOperations() findResult = zed.Find(1); Assert.IsTrue(findResult, "B - Should find tag 1"); Assert.AreEqual(3, zed.ValueLength, "B - Length of entry should be 3"); - for (int i = 1; i <= 3; ++i) { + for (int i = 1; i <= 3; ++i) + { Assert.AreEqual(i, zed.ReadByte()); } Assert.AreEqual(-1, zed.ReadByte()); Stream s = zed.GetStreamForTag(1); Assert.AreEqual(3, s.Length, "B.1 Stream length should be 3"); - for (int i = 1; i <= 3; ++i) { + for (int i = 1; i <= 3; ++i) + { Assert.AreEqual(i, s.ReadByte()); } Assert.AreEqual(-1, s.ReadByte()); @@ -154,7 +163,8 @@ public void BasicOperations() findResult = zed.Find(1); Assert.IsTrue(findResult, "C.1 - Should find tag 1"); Assert.AreEqual(3, zed.ValueLength, "C.1 - Length of entry should be 3"); - for (int i = 1; i <= 3; ++i) { + for (int i = 1; i <= 3; ++i) + { Assert.AreEqual(i, zed.ReadByte()); } Assert.AreEqual(-1, zed.ReadByte()); @@ -249,7 +259,8 @@ public void UnreadCountValid() zed = new ZipExtraData(new byte[] { 1, 0, 7, 0, 1, 2, 3, 4, 5, 6, 7 }); Assert.IsTrue(zed.Find(1), "Should find tag 1"); - for (int i = 0; i < 7; ++i) { + for (int i = 0; i < 7; ++i) + { Assert.AreEqual(7 - i, zed.UnreadCount); zed.ReadByte(); } @@ -287,9 +298,12 @@ public void Skipping() bool exceptionCaught = false; - try { + try + { zed.Skip(1); - } catch (ZipException) { + } + catch (ZipException) + { exceptionCaught = true; } Assert.IsTrue(exceptionCaught, "Should fail to skip past end"); @@ -302,9 +316,12 @@ public void Skipping() Assert.AreEqual(4, zed.CurrentReadIndex); exceptionCaught = false; - try { + try + { zed.Skip(-1); - } catch (ZipException) { + } + catch (ZipException) + { exceptionCaught = true; } Assert.IsTrue(exceptionCaught, "Should fail to skip before beginning"); @@ -320,9 +337,12 @@ public void ReadOverrunLong() // Empty Tag bool exceptionCaught = false; - try { + try + { zed.ReadLong(); - } catch (ZipException) { + } + catch (ZipException) + { exceptionCaught = true; } Assert.IsTrue(exceptionCaught, "Expected EOS exception"); @@ -332,9 +352,12 @@ public void ReadOverrunLong() Assert.IsTrue(zed.Find(1), "Should find tag 1"); exceptionCaught = false; - try { + try + { zed.ReadLong(); - } catch (ZipException) { + } + catch (ZipException) + { exceptionCaught = true; } Assert.IsTrue(exceptionCaught, "Expected EOS exception"); @@ -345,9 +368,12 @@ public void ReadOverrunLong() zed.ReadLong(); exceptionCaught = false; - try { + try + { zed.ReadLong(); - } catch (ZipException) { + } + catch (ZipException) + { exceptionCaught = true; } Assert.IsTrue(exceptionCaught, "Expected EOS exception"); @@ -363,9 +389,12 @@ public void ReadOverrunInt() // Empty Tag bool exceptionCaught = false; - try { + try + { zed.ReadInt(); - } catch (ZipException) { + } + catch (ZipException) + { exceptionCaught = true; } Assert.IsTrue(exceptionCaught, "Expected EOS exception"); @@ -375,9 +404,12 @@ public void ReadOverrunInt() Assert.IsTrue(zed.Find(1), "Should find tag 1"); exceptionCaught = false; - try { + try + { zed.ReadInt(); - } catch (ZipException) { + } + catch (ZipException) + { exceptionCaught = true; } Assert.IsTrue(exceptionCaught, "Expected EOS exception"); @@ -388,9 +420,12 @@ public void ReadOverrunInt() zed.ReadInt(); exceptionCaught = false; - try { + try + { zed.ReadInt(); - } catch (ZipException) { + } + catch (ZipException) + { exceptionCaught = true; } Assert.IsTrue(exceptionCaught, "Expected EOS exception"); @@ -406,9 +441,12 @@ public void ReadOverrunShort() // Empty Tag bool exceptionCaught = false; - try { + try + { zed.ReadShort(); - } catch (ZipException) { + } + catch (ZipException) + { exceptionCaught = true; } Assert.IsTrue(exceptionCaught, "Expected EOS exception"); @@ -418,9 +456,12 @@ public void ReadOverrunShort() Assert.IsTrue(zed.Find(1), "Should find tag 1"); exceptionCaught = false; - try { + try + { zed.ReadShort(); - } catch (ZipException) { + } + catch (ZipException) + { exceptionCaught = true; } Assert.IsTrue(exceptionCaught, "Expected EOS exception"); @@ -431,9 +472,12 @@ public void ReadOverrunShort() zed.ReadShort(); exceptionCaught = false; - try { + try + { zed.ReadShort(); - } catch (ZipException) { + } + catch (ZipException) + { exceptionCaught = true; } Assert.IsTrue(exceptionCaught, "Expected EOS exception"); diff --git a/test/ICSharpCode.SharpZipLib.Tests/Zip/ZipFileHandling.cs b/test/ICSharpCode.SharpZipLib.Tests/Zip/ZipFileHandling.cs index 17c94c89e..c25059da4 100644 --- a/test/ICSharpCode.SharpZipLib.Tests/Zip/ZipFileHandling.cs +++ b/test/ICSharpCode.SharpZipLib.Tests/Zip/ZipFileHandling.cs @@ -1,10 +1,12 @@ -using System; -using System.IO; -using System.Text; -using ICSharpCode.SharpZipLib.Core; +using ICSharpCode.SharpZipLib.Core; using ICSharpCode.SharpZipLib.Tests.TestSupport; using ICSharpCode.SharpZipLib.Zip; using NUnit.Framework; +using System; +using System.IO; +using System.Text; +using System.Threading.Tasks; +using Does = ICSharpCode.SharpZipLib.Tests.TestSupport.Does; namespace ICSharpCode.SharpZipLib.Tests.Zip { @@ -20,9 +22,13 @@ public void NullStreamDetected() bool nullStreamDetected = false; - try { + try + { + // ReSharper disable once ExpressionIsAlwaysNull bad = new ZipFile(nullStream); - } catch { + } + catch + { nullStreamDetected = true; } @@ -43,10 +49,12 @@ public void Zip64Entries() const int target = 65537; - using (ZipFile zipFile = ZipFile.Create(Path.GetTempFileName())) { + using (ZipFile zipFile = ZipFile.Create(Path.GetTempFileName())) + { zipFile.BeginUpdate(); - for (int i = 0; i < target; ++i) { + for (int i = 0; i < target; ++i) + { var ze = new ZipEntry(i.ToString()); ze.CompressedSize = 0; ze.Size = 0; @@ -54,7 +62,7 @@ public void Zip64Entries() } zipFile.CommitUpdate(); - Assert.IsTrue(zipFile.TestArchive(true)); + Assert.That(zipFile, Does.PassTestArchive()); Assert.AreEqual(target, zipFile.Count, "Incorrect number of entries stored"); } } @@ -64,7 +72,8 @@ public void Zip64Entries() public void EmbeddedArchive() { var memStream = new MemoryStream(); - using (ZipFile f = new ZipFile(memStream)) { + using (ZipFile f = new ZipFile(memStream)) + { f.IsStreamOwner = false; var m = new StringMemoryDataSource("0000000"); @@ -72,7 +81,7 @@ public void EmbeddedArchive() f.Add(m, "a.dat"); f.Add(m, "b.dat"); f.CommitUpdate(); - Assert.IsTrue(f.TestArchive(true)); + Assert.That(f, Does.PassTestArchive()); } byte[] rawArchive = memStream.ToArray(); @@ -80,8 +89,10 @@ public void EmbeddedArchive() Array.Copy(rawArchive, 0, pseudoSfx, 1049, rawArchive.Length); memStream = new MemoryStream(pseudoSfx); - using (ZipFile f = new ZipFile(memStream)) { - for (int index = 0; index < f.Count; ++index) { + using (ZipFile f = new ZipFile(memStream)) + { + for (int index = 0; index < f.Count; ++index) + { Stream entryStream = f.GetInputStream(index); var data = new MemoryStream(); StreamUtils.Copy(entryStream, data, new byte[128]); @@ -96,7 +107,8 @@ public void EmbeddedArchive() public void Zip64Useage() { var memStream = new MemoryStream(); - using (ZipFile f = new ZipFile(memStream)) { + using (ZipFile f = new ZipFile(memStream)) + { f.IsStreamOwner = false; f.UseZip64 = UseZip64.On; @@ -105,7 +117,7 @@ public void Zip64Useage() f.Add(m, "a.dat"); f.Add(m, "b.dat"); f.CommitUpdate(); - Assert.IsTrue(f.TestArchive(true)); + Assert.That(f, Does.PassTestArchive()); } byte[] rawArchive = memStream.ToArray(); @@ -114,8 +126,10 @@ public void Zip64Useage() Array.Copy(rawArchive, 0, pseudoSfx, 1049, rawArchive.Length); memStream = new MemoryStream(pseudoSfx); - using (ZipFile f = new ZipFile(memStream)) { - for (int index = 0; index < f.Count; ++index) { + using (ZipFile f = new ZipFile(memStream)) + { + for (int index = 0; index < f.Count; ++index) + { Stream entryStream = f.GetInputStream(index); var data = new MemoryStream(); StreamUtils.Copy(entryStream, data, new byte[128]); @@ -125,6 +139,92 @@ public void Zip64Useage() } } + /// + /// Test that entries can be removed from a Zip64 file + /// + [Test] + [Category("Zip")] + public void Zip64Update() + { + using (var memStream = new MemoryStream()) + { + using (ZipFile f = new ZipFile(memStream, leaveOpen: true)) + { + f.UseZip64 = UseZip64.On; + + var m = new StringMemoryDataSource("0000000"); + f.BeginUpdate(new MemoryArchiveStorage()); + f.Add(m, "a.dat"); + f.Add(m, "b.dat"); + f.CommitUpdate(); + Assert.That(f.TestArchive(true), Is.True, "initial archive should be valid"); + } + + memStream.Seek(0, SeekOrigin.Begin); + + using (ZipFile f = new ZipFile(memStream, leaveOpen: true)) + { + Assert.That(f.Count, Is.EqualTo(2), "Archive should have 2 entries"); + + f.BeginUpdate(new MemoryArchiveStorage()); + f.Delete("b.dat"); + f.CommitUpdate(); + Assert.That(f.TestArchive(true), Is.True, "modified archive should be valid"); + } + + memStream.Seek(0, SeekOrigin.Begin); + + using (ZipFile f = new ZipFile(memStream, leaveOpen: true)) + { + Assert.That(f.Count, Is.EqualTo(1), "Archive should have 1 entry"); + + for (int index = 0; index < f.Count; ++index) + { + Stream entryStream = f.GetInputStream(index); + var data = new MemoryStream(); + StreamUtils.Copy(entryStream, data, new byte[128]); + string contents = Encoding.ASCII.GetString(data.ToArray()); + Assert.That(contents, Is.EqualTo("0000000"), "archive member data should be correct"); + } + } + } + } + + /// + /// Test for issue #403 - zip64 locator signature bytes being present in a contained file, + /// when the outer zip file isn't using zip64 + /// + [Test] + [Category("Zip")] + public void FakeZip64Locator() + { + using (var memStream = new MemoryStream()) + { + // set the file contents to the zip 64 directory locator signature + var locatorValue = ZipConstants.Zip64CentralDirLocatorSignature; + var locatorBytes = new byte[] { (byte)(locatorValue & 0xff), (byte)((locatorValue >> 8) & 0xff), (byte)((locatorValue >> 16) & 0xff), (byte)((locatorValue >> 24) & 0xff) }; + + using (ZipFile f = new ZipFile(memStream, leaveOpen: true)) + { + var m = new MemoryDataSource(locatorBytes); + + // Add the entry - set compression method to stored so the signature bytes remain as expected + f.BeginUpdate(new MemoryArchiveStorage()); + f.Add(m, "a.dat", CompressionMethod.Stored); + f.CommitUpdate(); + Assert.That(f, Does.PassTestArchive()); + } + + memStream.Seek(0, SeekOrigin.Begin); + + // Check that the archive is readable. + using (ZipFile f = new ZipFile(memStream, leaveOpen: true)) + { + Assert.That(f.Count, Is.EqualTo(1), "Archive should have 1 entry"); + } + } + } + [Test] [Category("Zip")] [Explicit] @@ -139,27 +239,30 @@ public void Zip64Offset() [Category("Zip")] public void BasicEncryption() { - const string TestValue = "0001000"; + const string testValue = "0001000"; var memStream = new MemoryStream(); - using (ZipFile f = new ZipFile(memStream)) { - f.IsStreamOwner = false; - f.Password = "Hello"; - - var m = new StringMemoryDataSource(TestValue); - f.BeginUpdate(new MemoryArchiveStorage()); - f.Add(m, "a.dat"); - f.CommitUpdate(); - Assert.IsTrue(f.TestArchive(true), "Archive test should pass"); + using (var zf = new ZipFile(memStream)) + { + zf.IsStreamOwner = false; + zf.Password = "Hello"; + + var m = new StringMemoryDataSource(testValue); + zf.BeginUpdate(new MemoryArchiveStorage()); + zf.Add(m, "a.dat"); + zf.CommitUpdate(); + Assert.IsTrue(zf.TestArchive(testData: true), "Archive test should pass"); } - using (ZipFile g = new ZipFile(memStream)) { - g.Password = "Hello"; - ZipEntry ze = g[0]; + using (var zf = new ZipFile(memStream)) + { + zf.Password = "Hello"; + var ze = zf[0]; Assert.IsTrue(ze.IsCrypted, "Entry should be encrypted"); - using (StreamReader r = new StreamReader(g.GetInputStream(0))) { - string data = r.ReadToEnd(); - Assert.AreEqual(TestValue, data); + using (var r = new StreamReader(zf.GetInputStream(entryIndex: 0))) + { + var data = r.ReadToEnd(); + Assert.AreEqual(testValue, data); } } } @@ -169,34 +272,38 @@ public void BasicEncryption() [Category("CreatesTempFile")] public void BasicEncryptionToDisk() { - const string TestValue = "0001000"; - string tempFile = GetTempFilePath(); + const string testValue = "0001000"; + var tempFile = GetTempFilePath(); Assert.IsNotNull(tempFile, "No permission to execute this test?"); tempFile = Path.Combine(tempFile, "SharpZipTest.Zip"); - using (ZipFile f = ZipFile.Create(tempFile)) { - f.Password = "Hello"; + using (var zf = ZipFile.Create(tempFile)) + { + zf.Password = "Hello"; - var m = new StringMemoryDataSource(TestValue); - f.BeginUpdate(); - f.Add(m, "a.dat"); - f.CommitUpdate(); + var m = new StringMemoryDataSource(testValue); + zf.BeginUpdate(); + zf.Add(m, "a.dat"); + zf.CommitUpdate(); } - using (ZipFile f = new ZipFile(tempFile)) { - f.Password = "Hello"; - Assert.IsTrue(f.TestArchive(true), "Archive test should pass"); + using (var zf = new ZipFile(tempFile)) + { + zf.Password = "Hello"; + Assert.IsTrue(zf.TestArchive(testData: true), "Archive test should pass"); } - using (ZipFile g = new ZipFile(tempFile)) { - g.Password = "Hello"; - ZipEntry ze = g[0]; + using (var zf = new ZipFile(tempFile)) + { + zf.Password = "Hello"; + ZipEntry ze = zf[0]; Assert.IsTrue(ze.IsCrypted, "Entry should be encrypted"); - using (StreamReader r = new StreamReader(g.GetInputStream(0))) { - string data = r.ReadToEnd(); - Assert.AreEqual(TestValue, data); + using (var r = new StreamReader(zf.GetInputStream(entryIndex: 0))) + { + var data = r.ReadToEnd(); + Assert.AreEqual(testValue, data); } } @@ -207,29 +314,32 @@ public void BasicEncryptionToDisk() [Category("Zip")] public void AddEncryptedEntriesToExistingArchive() { - const string TestValue = "0001000"; + const string testValue = "0001000"; var memStream = new MemoryStream(); - using (ZipFile f = new ZipFile(memStream)) { + using (ZipFile f = new ZipFile(memStream)) + { f.IsStreamOwner = false; f.UseZip64 = UseZip64.Off; - var m = new StringMemoryDataSource(TestValue); + var m = new StringMemoryDataSource(testValue); f.BeginUpdate(new MemoryArchiveStorage()); f.Add(m, "a.dat"); f.CommitUpdate(); Assert.IsTrue(f.TestArchive(true), "Archive test should pass"); } - using (ZipFile g = new ZipFile(memStream)) { + using (ZipFile g = new ZipFile(memStream)) + { ZipEntry ze = g[0]; Assert.IsFalse(ze.IsCrypted, "Entry should NOT be encrypted"); - using (StreamReader r = new StreamReader(g.GetInputStream(0))) { + using (StreamReader r = new StreamReader(g.GetInputStream(0))) + { string data = r.ReadToEnd(); - Assert.AreEqual(TestValue, data); + Assert.AreEqual(testValue, data); } - var n = new StringMemoryDataSource(TestValue); + var n = new StringMemoryDataSource(testValue); g.Password = "Axolotyl"; g.UseZip64 = UseZip64.Off; @@ -241,30 +351,34 @@ public void AddEncryptedEntriesToExistingArchive() ze = g[1]; Assert.IsTrue(ze.IsCrypted, "New entry should be encrypted"); - using (StreamReader r = new StreamReader(g.GetInputStream(0))) { + using (StreamReader r = new StreamReader(g.GetInputStream(0))) + { string data = r.ReadToEnd(); - Assert.AreEqual(TestValue, data); + Assert.AreEqual(testValue, data); } } } - void TryDeleting(byte[] master, int totalEntries, int additions, params string[] toDelete) + private void TryDeleting(byte[] master, int totalEntries, int additions, params string[] toDelete) { var ms = new MemoryStream(); ms.Write(master, 0, master.Length); - using (ZipFile f = new ZipFile(ms)) { + using (ZipFile f = new ZipFile(ms)) + { f.IsStreamOwner = false; Assert.AreEqual(totalEntries, f.Count); - Assert.IsTrue(f.TestArchive(true)); + Assert.That(f, Does.PassTestArchive()); f.BeginUpdate(new MemoryArchiveStorage()); - for (int i = 0; i < additions; ++i) { + for (int i = 0; i < additions; ++i) + { f.Add(new StringMemoryDataSource("Another great file"), string.Format("Add{0}.dat", i + 1)); } - foreach (string name in toDelete) { + foreach (string name in toDelete) + { f.Delete(name); } f.CommitUpdate(); @@ -279,23 +393,26 @@ void TryDeleting(byte[] master, int totalEntries, int additions, params string[] } } - void TryDeleting(byte[] master, int totalEntries, int additions, params int[] toDelete) + private void TryDeleting(byte[] master, int totalEntries, int additions, params int[] toDelete) { var ms = new MemoryStream(); ms.Write(master, 0, master.Length); - using (ZipFile f = new ZipFile(ms)) { + using (ZipFile f = new ZipFile(ms)) + { f.IsStreamOwner = false; Assert.AreEqual(totalEntries, f.Count); - Assert.IsTrue(f.TestArchive(true)); + Assert.That(f, Does.PassTestArchive()); f.BeginUpdate(new MemoryArchiveStorage()); - for (int i = 0; i < additions; ++i) { + for (int i = 0; i < additions; ++i) + { f.Add(new StringMemoryDataSource("Another great file"), string.Format("Add{0}.dat", i + 1)); } - foreach (int i in toDelete) { + foreach (int i in toDelete) + { f.Delete(f[i]); } f.CommitUpdate(); @@ -319,21 +436,26 @@ public void AddAndDeleteEntriesMemory() { var memStream = new MemoryStream(); - using (ZipFile f = new ZipFile(memStream)) { + using (ZipFile f = new ZipFile(memStream)) + { f.IsStreamOwner = false; f.BeginUpdate(new MemoryArchiveStorage()); - f.Add(new StringMemoryDataSource("Hello world"), @"z:\a\a.dat"); + f.Add(new StringMemoryDataSource("Hello world"), Utils.SystemRoot + @"a\a.dat"); f.Add(new StringMemoryDataSource("Another"), @"\b\b.dat"); f.Add(new StringMemoryDataSource("Mr C"), @"c\c.dat"); f.Add(new StringMemoryDataSource("Mrs D was a star"), @"d\d.dat"); f.CommitUpdate(); - Assert.IsTrue(f.TestArchive(true)); + Assert.That(f, Does.PassTestArchive()); + foreach (ZipEntry entry in f) + { + Console.WriteLine($" - {entry.Name}"); + } } byte[] master = memStream.ToArray(); - TryDeleting(master, 4, 1, @"z:\a\a.dat"); + TryDeleting(master, 4, 1, Utils.SystemRoot + @"a\a.dat"); TryDeleting(master, 4, 1, @"\a\a.dat"); TryDeleting(master, 4, 1, @"a/a.dat"); @@ -374,24 +496,38 @@ public void AddAndDeleteEntries() string addFile2 = Path.Combine(tempFile, "b.dat"); MakeTempFile(addFile2, 259); + string addDirectory = Path.Combine(tempFile, "dir"); + tempFile = Path.Combine(tempFile, "SharpZipTest.Zip"); - using (ZipFile f = ZipFile.Create(tempFile)) { + using (ZipFile f = ZipFile.Create(tempFile)) + { f.BeginUpdate(); f.Add(addFile); f.Add(addFile2); + f.AddDirectory(addDirectory); f.CommitUpdate(); - Assert.IsTrue(f.TestArchive(true)); + Assert.That(f, Does.PassTestArchive()); } - using (ZipFile f = new ZipFile(tempFile)) { - Assert.AreEqual(2, f.Count); - Assert.IsTrue(f.TestArchive(true)); + using (ZipFile f = new ZipFile(tempFile)) + { + Assert.AreEqual(3, f.Count); + Assert.That(f, Does.PassTestArchive()); + + // Delete file f.BeginUpdate(); f.Delete(f[0]); f.CommitUpdate(); + Assert.AreEqual(2, f.Count); + Assert.That(f, Does.PassTestArchive()); + + // Delete directory + f.BeginUpdate(); + f.Delete(f[1]); + f.CommitUpdate(); Assert.AreEqual(1, f.Count); - Assert.IsTrue(f.TestArchive(true)); + Assert.That(f, Does.PassTestArchive()); } File.Delete(addFile); @@ -412,17 +548,22 @@ public void RoundTrip() tempFile = Path.Combine(tempFile, "SharpZipTest.Zip"); - try { + try + { MakeZipFile(tempFile, "", 10, 1024, ""); - using (ZipFile zipFile = new ZipFile(tempFile)) { - foreach (ZipEntry e in zipFile) { + using (ZipFile zipFile = new ZipFile(tempFile)) + { + foreach (ZipEntry e in zipFile) + { Stream instream = zipFile.GetInputStream(e); CheckKnownEntry(instream, 1024); } zipFile.Close(); } - } finally { + } + finally + { File.Delete(tempFile); } } @@ -437,8 +578,10 @@ public void RoundTripInMemory() var storage = new MemoryStream(); MakeZipFile(storage, false, "", 10, 1024, ""); - using (ZipFile zipFile = new ZipFile(storage)) { - foreach (ZipEntry e in zipFile) { + using (ZipFile zipFile = new ZipFile(storage)) + { + foreach (ZipEntry e in zipFile) + { Stream instream = zipFile.GetInputStream(e); CheckKnownEntry(instream, 1024); } @@ -446,6 +589,29 @@ public void RoundTripInMemory() } } + /// + /// Simple async round trip test for ZipFile class + /// + [TestCase(CompressionMethod.Stored)] + [TestCase(CompressionMethod.Deflated)] + [TestCase(CompressionMethod.BZip2)] + [Category("Zip")] + [Category("Async")] + public async Task RoundTripInMemoryAsync(CompressionMethod compressionMethod) + { + var storage = new MemoryStream(); + MakeZipFile(storage, compressionMethod, false, "", 10, 1024, ""); + + using (ZipFile zipFile = new ZipFile(storage)) + { + foreach (ZipEntry e in zipFile) + { + Stream instream = zipFile.GetInputStream(e); + await CheckKnownEntryAsync(instream, 1024); + } + } + } + [Test] [Category("Zip")] public void AddToEmptyArchive() @@ -457,29 +623,34 @@ public void AddToEmptyArchive() MakeTempFile(addFile, 1); - try { + try + { tempFile = Path.Combine(tempFile, "SharpZipTest.Zip"); - using (ZipFile f = ZipFile.Create(tempFile)) { + using (ZipFile f = ZipFile.Create(tempFile)) + { f.BeginUpdate(); f.Add(addFile); f.CommitUpdate(); Assert.AreEqual(1, f.Count); - Assert.IsTrue(f.TestArchive(true)); + Assert.That(f, Does.PassTestArchive()); } - using (ZipFile f = new ZipFile(tempFile)) { + using (ZipFile f = new ZipFile(tempFile)) + { Assert.AreEqual(1, f.Count); f.BeginUpdate(); f.Delete(f[0]); f.CommitUpdate(); Assert.AreEqual(0, f.Count); - Assert.IsTrue(f.TestArchive(true)); + Assert.That(f, Does.PassTestArchive()); f.Close(); } File.Delete(tempFile); - } finally { + } + finally + { File.Delete(addFile); } } @@ -493,20 +664,50 @@ public void CreateEmptyArchive() tempFile = Path.Combine(tempFile, "SharpZipTest.Zip"); - using (ZipFile f = ZipFile.Create(tempFile)) { + using (ZipFile f = ZipFile.Create(tempFile)) + { f.BeginUpdate(); f.CommitUpdate(); - Assert.IsTrue(f.TestArchive(true)); + Assert.That(f, Does.PassTestArchive()); f.Close(); } - using (ZipFile f = new ZipFile(tempFile)) { + using (ZipFile f = new ZipFile(tempFile)) + { Assert.AreEqual(0, f.Count); } File.Delete(tempFile); } + [Test] + [Category("Zip")] + [Category("CreatesTempFile")] + public void CreateArchiveWithNoCompression() + { + using var sourceFile = Utils.GetDummyFile(); + using var zipFile = Utils.GetDummyFile(0); + var inputContent = File.ReadAllText(sourceFile); + using (var zf = ZipFile.Create(zipFile)) + { + zf.BeginUpdate(); + zf.Add(sourceFile, CompressionMethod.Stored); + zf.CommitUpdate(); + Assert.That(zf, Does.PassTestArchive()); + zf.Close(); + } + + using (var zf = new ZipFile(zipFile)) + { + Assert.AreEqual(1, zf.Count); + using (var sr = new StreamReader(zf.GetInputStream(zf[0]))) + { + var outputContent = sr.ReadToEnd(); + Assert.AreEqual(inputContent, outputContent, "extracted content does not match source content"); + } + } + } + /// /// Check that ZipFile finds entries when its got a long comment /// @@ -522,15 +723,20 @@ public void FindEntriesInArchiveWithLongComment() var longComment = new String('A', 65535); MakeZipFile(tempFile, "", 1, 1, longComment); - try { - using (ZipFile zipFile = new ZipFile(tempFile)) { - foreach (ZipEntry e in zipFile) { + try + { + using (ZipFile zipFile = new ZipFile(tempFile)) + { + foreach (ZipEntry e in zipFile) + { Stream instream = zipFile.GetInputStream(e); CheckKnownEntry(instream, 1); } zipFile.Close(); } - } finally { + } + finally + { File.Delete(tempFile); } } @@ -558,15 +764,20 @@ public void FindEntriesInArchiveExtraData() tempStream.Close(); bool fails = false; - try { - using (ZipFile zipFile = new ZipFile(tempFile)) { - foreach (ZipEntry e in zipFile) { + try + { + using (ZipFile zipFile = new ZipFile(tempFile)) + { + foreach (ZipEntry e in zipFile) + { Stream instream = zipFile.GetInputStream(e); CheckKnownEntry(instream, 1); } zipFile.Close(); } - } catch { + } + catch + { fails = true; } @@ -588,7 +799,8 @@ public void FindEntry() tempFile = Path.Combine(tempFile, "SharpZipTest.Zip"); MakeZipFile(tempFile, new string[] { "Farriera", "Champagne", "Urban myth" }, 10, "Aha"); - using (ZipFile zipFile = new ZipFile(tempFile)) { + using (ZipFile zipFile = new ZipFile(tempFile)) + { Assert.AreEqual(3, zipFile.Count, "Expected 1 entry"); int testIndex = zipFile.FindEntry("Farriera", false); @@ -631,7 +843,8 @@ public void HandlesNoEntries() tempFile = Path.Combine(tempFile, "SharpZipTest.Zip"); MakeZipFile(tempFile, "", 0, 1, "Aha"); - using (ZipFile zipFile = new ZipFile(tempFile)) { + using (ZipFile zipFile = new ZipFile(tempFile)) + { Assert.AreEqual(0, zipFile.Count); zipFile.Close(); } @@ -650,9 +863,9 @@ public void ArchiveTesting() var ms = new MemoryStream(compressedData); ms.Seek(0, SeekOrigin.Begin); - using (ZipFile testFile = new ZipFile(ms)) { - - Assert.IsTrue(testFile.TestArchive(true), "Unexpected error in archive detected"); + using (ZipFile testFile = new ZipFile(ms)) + { + Assert.That(testFile, Does.PassTestArchive(), "Unexpected error in archive detected"); byte[] corrupted = new byte[compressedData.Length]; Array.Copy(compressedData, corrupted, compressedData.Length); @@ -661,12 +874,13 @@ public void ArchiveTesting() ms = new MemoryStream(corrupted); } - using (ZipFile testFile = new ZipFile(ms)) { - Assert.IsFalse(testFile.TestArchive(true), "Error in archive not detected"); + using (ZipFile testFile = new ZipFile(ms)) + { + Assert.That(testFile, Does.Not.PassTestArchive(), "Error in archive not detected"); } } - void TestDirectoryEntry(MemoryStream s) + private void TestDirectoryEntry(MemoryStream s) { var outStream = new ZipOutputStream(s); outStream.IsStreamOwner = false; @@ -674,8 +888,9 @@ void TestDirectoryEntry(MemoryStream s) outStream.Close(); var ms2 = new MemoryStream(s.ToArray()); - using (ZipFile zf = new ZipFile(ms2)) { - Assert.IsTrue(zf.TestArchive(true)); + using (ZipFile zf = new ZipFile(ms2)) + { + Assert.That(zf, Does.PassTestArchive()); } } @@ -687,27 +902,28 @@ public void TestDirectoryEntry() TestDirectoryEntry(new MemoryStreamWithoutSeek()); } - void TestEncryptedDirectoryEntry(MemoryStream s) + private void TestEncryptedDirectoryEntry(MemoryStream s, int aesKeySize) { var outStream = new ZipOutputStream(s); outStream.Password = "Tonto hand me a beer"; outStream.IsStreamOwner = false; - outStream.PutNextEntry(new ZipEntry("YeUnreadableDirectory/")); + outStream.PutNextEntry(new ZipEntry("YeUnreadableDirectory/") { AESKeySize = aesKeySize } ); outStream.Close(); var ms2 = new MemoryStream(s.ToArray()); - using (ZipFile zf = new ZipFile(ms2)) { - Assert.IsTrue(zf.TestArchive(true)); + using (ZipFile zf = new ZipFile(ms2)) + { + Assert.That(zf, Does.PassTestArchive()); } } [Test] [Category("Zip")] - public void TestEncryptedDirectoryEntry() + public void TestEncryptedDirectoryEntry([Values(0, 128, 256)]int aesKeySize) { - TestEncryptedDirectoryEntry(new MemoryStream()); - TestEncryptedDirectoryEntry(new MemoryStreamWithoutSeek()); + TestEncryptedDirectoryEntry(new MemoryStream(), aesKeySize); + TestEncryptedDirectoryEntry(new MemoryStreamWithoutSeek(), aesKeySize); } [Test] @@ -718,7 +934,8 @@ public void Crypto_AddEncryptedEntryToExistingArchiveSafe() byte[] rawData; - using (ZipFile testFile = new ZipFile(ms)) { + using (ZipFile testFile = new ZipFile(ms)) + { testFile.IsStreamOwner = false; testFile.BeginUpdate(); testFile.Add(new StringMemoryDataSource("Aha"), "No1", CompressionMethod.Stored); @@ -726,21 +943,22 @@ public void Crypto_AddEncryptedEntryToExistingArchiveSafe() testFile.Add(new StringMemoryDataSource("No3"), "No3", CompressionMethod.Stored); testFile.CommitUpdate(); - Assert.IsTrue(testFile.TestArchive(true)); + Assert.That(testFile, Does.PassTestArchive()); rawData = ms.ToArray(); } ms = new MemoryStream(rawData); - using (ZipFile testFile = new ZipFile(ms)) { - Assert.IsTrue(testFile.TestArchive(true)); + using (ZipFile testFile = new ZipFile(ms)) + { + Assert.That(testFile, Does.PassTestArchive()); testFile.BeginUpdate(new MemoryArchiveStorage(FileUpdateMode.Safe)); testFile.Password = "pwd"; testFile.Add(new StringMemoryDataSource("Zapata!"), "encrypttest.xml"); testFile.CommitUpdate(); - Assert.IsTrue(testFile.TestArchive(true)); + Assert.That(testFile, Does.PassTestArchive()); int entryIndex = testFile.FindEntry("encrypttest.xml", true); Assert.IsNotNull(entryIndex >= 0); @@ -754,7 +972,8 @@ public void Crypto_AddEncryptedEntryToExistingArchiveDirect() { var ms = new MemoryStream(); - using (ZipFile testFile = new ZipFile(ms)) { + using (ZipFile testFile = new ZipFile(ms)) + { testFile.IsStreamOwner = false; testFile.BeginUpdate(); testFile.Add(new StringMemoryDataSource("Aha"), "No1", CompressionMethod.Stored); @@ -762,11 +981,12 @@ public void Crypto_AddEncryptedEntryToExistingArchiveDirect() testFile.Add(new StringMemoryDataSource("No3"), "No3", CompressionMethod.Stored); testFile.CommitUpdate(); - Assert.IsTrue(testFile.TestArchive(true)); + Assert.That(testFile, Does.PassTestArchive()); } - using (ZipFile testFile = new ZipFile(ms)) { - Assert.IsTrue(testFile.TestArchive(true)); + using (ZipFile testFile = new ZipFile(ms)) + { + Assert.That(testFile, Does.PassTestArchive()); testFile.IsStreamOwner = true; testFile.BeginUpdate(); @@ -774,7 +994,7 @@ public void Crypto_AddEncryptedEntryToExistingArchiveDirect() testFile.Add(new StringMemoryDataSource("Zapata!"), "encrypttest.xml"); testFile.CommitUpdate(); - Assert.IsTrue(testFile.TestArchive(true)); + Assert.That(testFile, Does.PassTestArchive()); int entryIndex = testFile.FindEntry("encrypttest.xml", true); Assert.IsNotNull(entryIndex >= 0); @@ -784,33 +1004,43 @@ public void Crypto_AddEncryptedEntryToExistingArchiveDirect() [Test] [Category("Zip")] + [Category("Unicode")] public void UnicodeNames() { - var memStream = new MemoryStream(); - using (ZipFile f = new ZipFile(memStream)) { + using var memStream = new MemoryStream(); + using (var f = new ZipFile(memStream)) + { f.IsStreamOwner = false; f.BeginUpdate(new MemoryArchiveStorage()); - - var names = new string[] + foreach (var (language, name, _) in StringTesting.TestSamples) { - "\u030A\u03B0", // Greek - "\u0680\u0685" // Arabic - }; - - foreach (string name in names) { - f.Add(new StringMemoryDataSource("Hello world"), name, - CompressionMethod.Deflated, true); + f.Add(new StringMemoryDataSource(language), name, + CompressionMethod.Deflated, useUnicodeText: true); } f.CommitUpdate(); - Assert.IsTrue(f.TestArchive(true)); - foreach (string name in names) { - int index = f.FindEntry(name, true); + Assert.That(f, Does.PassTestArchive()); + } + memStream.Seek(0, SeekOrigin.Begin); + using (var zf = new ZipFile(memStream)) + { + foreach (var name in StringTesting.Filenames) + { + string content; + var index = zf.FindEntry(name, ignoreCase: true); + var entry = zf[index]; + + using (var entryStream = zf.GetInputStream(entry)) + using (var sr = new StreamReader(entryStream)) + { + content = sr.ReadToEnd(); + } + + TestContext.WriteLine($"Entry #{index}: {name}, Content: {content}"); Assert.IsTrue(index >= 0); - ZipEntry found = f[index]; - Assert.AreEqual(name, found.Name); + Assert.AreEqual(name, entry.Name); } } } @@ -821,7 +1051,8 @@ public void UpdateCommentOnlyInMemory() { var ms = new MemoryStream(); - using (ZipFile testFile = new ZipFile(ms)) { + using (ZipFile testFile = new ZipFile(ms)) + { testFile.IsStreamOwner = false; testFile.BeginUpdate(); testFile.Add(new StringMemoryDataSource("Aha"), "No1", CompressionMethod.Stored); @@ -829,11 +1060,12 @@ public void UpdateCommentOnlyInMemory() testFile.Add(new StringMemoryDataSource("No3"), "No3", CompressionMethod.Stored); testFile.CommitUpdate(); - Assert.IsTrue(testFile.TestArchive(true)); + Assert.That(testFile, Does.PassTestArchive()); } - using (ZipFile testFile = new ZipFile(ms)) { - Assert.IsTrue(testFile.TestArchive(true)); + using (ZipFile testFile = new ZipFile(ms)) + { + Assert.That(testFile, Does.PassTestArchive()); Assert.AreEqual("", testFile.ZipFileComment); testFile.IsStreamOwner = false; @@ -841,11 +1073,12 @@ public void UpdateCommentOnlyInMemory() testFile.SetComment("Here is my comment"); testFile.CommitUpdate(); - Assert.IsTrue(testFile.TestArchive(true)); + Assert.That(testFile, Does.PassTestArchive()); } - using (ZipFile testFile = new ZipFile(ms)) { - Assert.IsTrue(testFile.TestArchive(true)); + using (ZipFile testFile = new ZipFile(ms)) + { + Assert.That(testFile, Does.PassTestArchive()); Assert.AreEqual("Here is my comment", testFile.ZipFileComment); } } @@ -859,61 +1092,68 @@ public void UpdateCommentOnlyOnDisk() Assert.IsNotNull(tempFile, "No permission to execute this test?"); tempFile = Path.Combine(tempFile, "SharpZipTest.Zip"); - if (File.Exists(tempFile)) { + if (File.Exists(tempFile)) + { File.Delete(tempFile); } - using (ZipFile testFile = ZipFile.Create(tempFile)) { + using (ZipFile testFile = ZipFile.Create(tempFile)) + { testFile.BeginUpdate(); testFile.Add(new StringMemoryDataSource("Aha"), "No1", CompressionMethod.Stored); testFile.Add(new StringMemoryDataSource("And so it goes"), "No2", CompressionMethod.Stored); testFile.Add(new StringMemoryDataSource("No3"), "No3", CompressionMethod.Stored); testFile.CommitUpdate(); - Assert.IsTrue(testFile.TestArchive(true)); + Assert.That(testFile, Does.PassTestArchive()); } - using (ZipFile testFile = new ZipFile(tempFile)) { - Assert.IsTrue(testFile.TestArchive(true)); + using (ZipFile testFile = new ZipFile(tempFile)) + { + Assert.That(testFile, Does.PassTestArchive()); Assert.AreEqual("", testFile.ZipFileComment); testFile.BeginUpdate(new DiskArchiveStorage(testFile, FileUpdateMode.Direct)); testFile.SetComment("Here is my comment"); testFile.CommitUpdate(); - Assert.IsTrue(testFile.TestArchive(true)); + Assert.That(testFile, Does.PassTestArchive()); } - using (ZipFile testFile = new ZipFile(tempFile)) { - Assert.IsTrue(testFile.TestArchive(true)); + using (ZipFile testFile = new ZipFile(tempFile)) + { + Assert.That(testFile, Does.PassTestArchive()); Assert.AreEqual("Here is my comment", testFile.ZipFileComment); } File.Delete(tempFile); // Variant using indirect updating. - using (ZipFile testFile = ZipFile.Create(tempFile)) { + using (ZipFile testFile = ZipFile.Create(tempFile)) + { testFile.BeginUpdate(); testFile.Add(new StringMemoryDataSource("Aha"), "No1", CompressionMethod.Stored); testFile.Add(new StringMemoryDataSource("And so it goes"), "No2", CompressionMethod.Stored); testFile.Add(new StringMemoryDataSource("No3"), "No3", CompressionMethod.Stored); testFile.CommitUpdate(); - Assert.IsTrue(testFile.TestArchive(true)); + Assert.That(testFile, Does.PassTestArchive()); } - using (ZipFile testFile = new ZipFile(tempFile)) { - Assert.IsTrue(testFile.TestArchive(true)); + using (ZipFile testFile = new ZipFile(tempFile)) + { + Assert.That(testFile, Does.PassTestArchive()); Assert.AreEqual("", testFile.ZipFileComment); testFile.BeginUpdate(); testFile.SetComment("Here is my comment"); testFile.CommitUpdate(); - Assert.IsTrue(testFile.TestArchive(true)); + Assert.That(testFile, Does.PassTestArchive()); } - using (ZipFile testFile = new ZipFile(tempFile)) { - Assert.IsTrue(testFile.TestArchive(true)); + using (ZipFile testFile = new ZipFile(tempFile)) + { + Assert.That(testFile, Does.PassTestArchive()); Assert.AreEqual("Here is my comment", testFile.ZipFileComment); } File.Delete(tempFile); @@ -925,7 +1165,8 @@ public void NameFactory() { var memStream = new MemoryStream(); var fixedTime = new DateTime(1981, 4, 3); - using (ZipFile f = new ZipFile(memStream)) { + using (ZipFile f = new ZipFile(memStream)) + { f.IsStreamOwner = false; ((ZipEntryFactory)f.EntryFactory).IsUnicodeText = true; ((ZipEntryFactory)f.EntryFactory).Setting = ZipEntryFactory.TimeSetting.Fixed; @@ -936,17 +1177,19 @@ public void NameFactory() var names = new string[] { "\u030A\u03B0", // Greek - "\u0680\u0685" // Arabic - }; + "\u0680\u0685" // Arabic + }; - foreach (string name in names) { + foreach (string name in names) + { f.Add(new StringMemoryDataSource("Hello world"), name, CompressionMethod.Deflated, true); } f.CommitUpdate(); - Assert.IsTrue(f.TestArchive(true)); + Assert.That(f, Does.PassTestArchive()); - foreach (string name in names) { + foreach (string name in names) + { int index = f.FindEntry(name, true); Assert.IsTrue(index >= 0); @@ -964,7 +1207,8 @@ public void NameFactory() public void NestedArchive() { var ms = new MemoryStream(); - using (ZipOutputStream zos = new ZipOutputStream(ms)) { + using (ZipOutputStream zos = new ZipOutputStream(ms)) + { zos.IsStreamOwner = false; var ze = new ZipEntry("Nest1"); @@ -976,7 +1220,8 @@ public void NestedArchive() byte[] data = ms.ToArray(); ms = new MemoryStream(); - using (ZipOutputStream zos = new ZipOutputStream(ms)) { + using (ZipOutputStream zos = new ZipOutputStream(ms)) + { zos.IsStreamOwner = false; var ze = new ZipEntry("Container"); ze.CompressionMethod = CompressionMethod.Stored; @@ -984,12 +1229,14 @@ public void NestedArchive() zos.Write(data, 0, data.Length); } - using (ZipFile zipFile = new ZipFile(ms)) { + using (ZipFile zipFile = new ZipFile(ms)) + { ZipEntry e = zipFile[0]; Assert.AreEqual("Container", e.Name); - using (ZipFile nested = new ZipFile(zipFile.GetInputStream(0))) { - Assert.IsTrue(nested.TestArchive(true)); + using (ZipFile nested = new ZipFile(zipFile.GetInputStream(0))) + { + Assert.That(nested, Does.PassTestArchive()); Assert.AreEqual(1, nested.Count); Stream nestedStream = nested.GetInputStream(0); @@ -1003,10 +1250,11 @@ public void NestedArchive() } } - Stream GetPartialStream() + private Stream GetPartialStream() { var ms = new MemoryStream(); - using (ZipOutputStream zos = new ZipOutputStream(ms)) { + using (ZipOutputStream zos = new ZipOutputStream(ms)) + { zos.IsStreamOwner = false; var ze = new ZipEntry("E1"); @@ -1029,5 +1277,565 @@ public void UnreferencedZipFileClosingPartialStream() s.ReadByte(); } + + /// + /// Check that input stream is closed when IsStreamOwner is true (default), or leaveOpen is false + /// + [Test] + [Category("Zip")] + public void StreamClosedWhenOwner() + { + var ms = new MemoryStream(); + MakeZipFile(ms, false, "StreamClosedWhenOwner", 1, 10, "test"); + ms.Seek(0, SeekOrigin.Begin); + var zipData = ms.ToArray(); + + // Stream should be closed when leaveOpen is unspecified + { + var inMemoryZip = new TrackedMemoryStream(zipData); + Assert.IsFalse(inMemoryZip.IsClosed, "Input stream should NOT be closed"); + + using (var zipFile = new ZipFile(inMemoryZip)) + { + Assert.IsTrue(zipFile.IsStreamOwner, "Should be stream owner by default"); + } + + Assert.IsTrue(inMemoryZip.IsClosed, "Input stream should be closed by default"); + } + + // Stream should be closed when leaveOpen is false + { + var inMemoryZip = new TrackedMemoryStream(zipData); + Assert.IsFalse(inMemoryZip.IsClosed, "Input stream should NOT be closed"); + + using (var zipFile = new ZipFile(inMemoryZip, false)) + { + Assert.IsTrue(zipFile.IsStreamOwner, "Should be stream owner when leaveOpen is false"); + } + + Assert.IsTrue(inMemoryZip.IsClosed, "Input stream should be closed when leaveOpen is false"); + } + } + + /// + /// Check that input stream is not closed when IsStreamOwner is false; + /// + [Test] + [Category("Zip")] + public void StreamNotClosedWhenNotOwner() + { + var ms = new TrackedMemoryStream(); + MakeZipFile(ms, false, "StreamNotClosedWhenNotOwner", 1, 10, "test"); + ms.Seek(0, SeekOrigin.Begin); + + Assert.IsFalse(ms.IsClosed, "Input stream should NOT be closed"); + + // Stream should not be closed when leaveOpen is true + { + using (var zipFile = new ZipFile(ms, true)) + { + Assert.IsFalse(zipFile.IsStreamOwner, "Should NOT be stream owner when leaveOpen is true"); + } + + Assert.IsFalse(ms.IsClosed, "Input stream should NOT be closed when leaveOpen is true"); + } + + ms.Seek(0, SeekOrigin.Begin); + + // Stream should not be closed when IsStreamOwner is set to false after opening + { + using (var zipFile = new ZipFile(ms, false)) + { + Assert.IsTrue(zipFile.IsStreamOwner, "Should be stream owner when leaveOpen is false"); + zipFile.IsStreamOwner = false; + Assert.IsFalse(zipFile.IsStreamOwner, "Should be able to set IsStreamOwner to false"); + } + + Assert.IsFalse(ms.IsClosed, "Input stream should NOT be closed when IsStreamOwner is false"); + } + } + + /// + /// Check that input file is closed when IsStreamOwner is true (default), or leaveOpen is false + /// + [Test] + [Category("Zip")] + public void FileStreamClosedWhenOwner() + { + string tempFile = GetTempFilePath(); + Assert.IsNotNull(tempFile, "No permission to execute this test?"); + + tempFile = Path.Combine(tempFile, "SharpZipFileStreamClosedWhenOwnerTest.Zip"); + if (File.Exists(tempFile)) + { + File.Delete(tempFile); + } + + MakeZipFile(tempFile, "FileStreamClosedWhenOwner", 2, 10, "test"); + + // Stream should be closed when leaveOpen is unspecified + { + var fileStream = new TrackedFileStream(tempFile); + Assert.IsFalse(fileStream.IsClosed, "Input file should NOT be closed"); + + using (var zipFile = new ZipFile(fileStream)) + { + Assert.IsTrue(zipFile.IsStreamOwner, "Should be stream owner by default"); + } + + Assert.IsTrue(fileStream.IsClosed, "Input stream should be closed by default"); + } + + // Stream should be closed when leaveOpen is false + { + var fileStream = new TrackedFileStream(tempFile); + Assert.IsFalse(fileStream.IsClosed, "Input stream should NOT be closed"); + + using (var zipFile = new ZipFile(fileStream, false)) + { + Assert.IsTrue(zipFile.IsStreamOwner, "Should be stream owner when leaveOpen is false"); + } + + Assert.IsTrue(fileStream.IsClosed, "Input stream should be closed when leaveOpen is false"); + } + + File.Delete(tempFile); + } + + /// + /// Check that input file is not closed when IsStreamOwner is false; + /// + [Test] + [Category("Zip")] + public void FileStreamNotClosedWhenNotOwner() + { + string tempFile = GetTempFilePath(); + Assert.IsNotNull(tempFile, "No permission to execute this test?"); + + tempFile = Path.Combine(tempFile, "SharpZipFileStreamNotClosedWhenNotOwner.Zip"); + if (File.Exists(tempFile)) + { + File.Delete(tempFile); + } + + MakeZipFile(tempFile, "FileStreamClosedWhenOwner", 2, 10, "test"); + + // Stream should not be closed when leaveOpen is true + { + using (var fileStream = new TrackedFileStream(tempFile)) + { + Assert.IsFalse(fileStream.IsClosed, "Input file should NOT be closed"); + + using (var zipFile = new ZipFile(fileStream, true)) + { + Assert.IsFalse(zipFile.IsStreamOwner, "Should NOT be stream owner when leaveOpen is true"); + } + + Assert.IsFalse(fileStream.IsClosed, "Input stream should NOT be closed when leaveOpen is true"); + } + } + + // Stream should not be closed when IsStreamOwner is set to false after opening + { + using (var fileStream = new TrackedFileStream(tempFile)) + { + Assert.IsFalse(fileStream.IsClosed, "Input file should NOT be closed"); + + using (var zipFile = new ZipFile(fileStream, false)) + { + Assert.IsTrue(zipFile.IsStreamOwner, "Should be stream owner when leaveOpen is false"); + zipFile.IsStreamOwner = false; + Assert.IsFalse(zipFile.IsStreamOwner, "Should be able to set IsStreamOwner to false"); + } + + Assert.IsFalse(fileStream.IsClosed, "Input stream should NOT be closed when leaveOpen is true"); + } + } + + File.Delete(tempFile); + } + + /// + /// Check that input stream is only closed when construction fails and leaveOpen is false + /// + [Test] + [Category("Zip")] + public void StreamClosedOnError([Values(true, false)] bool leaveOpen) + { + var ms = new TrackedMemoryStream(new byte[32]); + + Assert.IsFalse(ms.IsClosed, "Underlying stream should NOT be closed initially"); + Assert.Throws(() => + { + using var zf = new ZipFile(ms, leaveOpen); + }, "Should have failed to load the file"); + + if (leaveOpen) + { + Assert.IsFalse(ms.IsClosed, "Underlying stream should NOT be closed"); + } + else + { + Assert.IsTrue(ms.IsClosed, "Underlying stream should be closed"); + } + } + + [Test] + [Category("Zip")] + public void HostSystemPersistedFromOutputStream() + { + using (var ms = new MemoryStream()) + { + var fileName = "testfile"; + + using (var zos = new ZipOutputStream(ms) { IsStreamOwner = false }) + { + var source = new StringMemoryDataSource("foo"); + zos.PutNextEntry(new ZipEntry(fileName) { HostSystem = (int)HostSystemID.Unix }); + source.GetSource().CopyTo(zos); + zos.CloseEntry(); + zos.Finish(); + } + + ms.Seek(0, SeekOrigin.Begin); + + using (var zis = new ZipFile(ms)) + { + var ze = zis.GetEntry(fileName); + Assert.NotNull(ze); + + Assert.AreEqual((int)HostSystemID.Unix, ze.HostSystem); + Assert.AreEqual(ZipConstants.VersionMadeBy, ze.VersionMadeBy); + } + } + } + + [Test] + [Category("Zip")] + public void HostSystemPersistedFromZipFile() + { + using (var ms = new MemoryStream()) + { + var fileName = "testfile"; + + using (var zof = new ZipFile(ms, true)) + { + var ze = zof.EntryFactory.MakeFileEntry(fileName, false); + ze.HostSystem = (int)HostSystemID.Unix; + + zof.BeginUpdate(); + zof.Add(new StringMemoryDataSource("foo"), ze); + zof.CommitUpdate(); + } + + ms.Seek(0, SeekOrigin.Begin); + + using (var zis = new ZipFile(ms)) + { + var ze = zis.GetEntry(fileName); + Assert.NotNull(ze); + + Assert.AreEqual((int)HostSystemID.Unix, ze.HostSystem); + Assert.AreEqual(ZipConstants.VersionMadeBy, ze.VersionMadeBy); + } + } + } + + /// + /// Refs https://github.com/icsharpcode/SharpZipLib/issues/385 + /// Trying to add an AES Encrypted entry to ZipFile should throw as it isn't supported + /// + [Test] + [Category("Zip")] + public void AddingAnAESEncryptedEntryShouldThrow() + { + var memStream = new MemoryStream(); + using var zof = new ZipFile(memStream); + var entry = new ZipEntry("test") + { + AESKeySize = 256, + }; + + zof.BeginUpdate(); + var exception = Assert.Throws(() => zof.Add(new StringMemoryDataSource("foo"), entry)); + Assert.That(exception?.Message, Is.EqualTo("Creation of AES encrypted entries is not supported")); + } + + /// + /// Test that we can add a file entry and set the name to sometihng other than the name of the file. + /// + [Test] + [Category("Zip")] + [Category("CreatesTempFile")] + public void AddFileWithAlternateName() + { + // Create a unique name that will be different from the file name + var fileName = Utils.GetDummyFileName(); + + using var sourceFile = Utils.GetDummyFile(size: 16); + using var outputFile = Utils.GetTempFile(); + var inputContent = File.ReadAllText(sourceFile); + using (var zf = ZipFile.Create(outputFile)) + { + zf.BeginUpdate(); + + // Add a file with the unique display name + zf.Add(sourceFile, fileName); + + zf.CommitUpdate(); + zf.Close(); + } + + using (var zipFile = new ZipFile(outputFile)) + { + Assert.That(zipFile.Count, Is.EqualTo(1)); + + var fileEntry = zipFile.GetEntry(fileName); + Assert.That(fileEntry, Is.Not.Null); + + using (var sr = new StreamReader(zipFile.GetInputStream(fileEntry))) + { + var outputContent = sr.ReadToEnd(); + Assert.AreEqual(inputContent, outputContent, "extracted content does not match source content"); + } + } + } + + /// + /// Test a zip file using BZip2 compression. + /// + [TestCase(true)] + [TestCase(false)] + [Category("Zip")] + public void ZipWithBZip2Compression(bool encryptEntries) + { + string password = "pwd"; + + using (var memStream = new MemoryStream()) + { + using (ZipFile f = new ZipFile(memStream, leaveOpen: true)) + { + if (encryptEntries) + f.Password = password; + + f.BeginUpdate(new MemoryArchiveStorage()); + + var m = new StringMemoryDataSource("BZip2Compressed"); + f.Add(m, "a.dat", CompressionMethod.BZip2); + + var m2 = new StringMemoryDataSource("DeflateCompressed"); + f.Add(m2, "b.dat", CompressionMethod.Deflated); + f.CommitUpdate(); + Assert.That(f, Does.PassTestArchive()); + } + + memStream.Seek(0, SeekOrigin.Begin); + + using (ZipFile f = new ZipFile(memStream)) + { + if (encryptEntries) + f.Password = password; + + { + var entry = f.GetEntry("a.dat"); + Assert.That(entry.CompressionMethod, Is.EqualTo(CompressionMethod.BZip2), "Compression method should be BZip2"); + Assert.That(entry.Version, Is.EqualTo(ZipConstants.VersionBZip2), "Entry version should be 46"); + Assert.That(entry.IsCrypted, Is.EqualTo(encryptEntries)); + + using (var reader = new StreamReader(f.GetInputStream(entry))) + { + string contents = reader.ReadToEnd(); + Assert.That(contents, Is.EqualTo("BZip2Compressed"), "extract string must match original string"); + } + } + + { + var entry = f.GetEntry("b.dat"); + Assert.That(entry.CompressionMethod, Is.EqualTo(CompressionMethod.Deflated), "Compression method should be Deflated"); + Assert.That(entry.IsCrypted, Is.EqualTo(encryptEntries)); + + using (var reader = new StreamReader(f.GetInputStream(entry))) + { + string contents = reader.ReadToEnd(); + Assert.That(contents, Is.EqualTo("DeflateCompressed"), "extract string must match original string"); + } + } + } + + // @@TODO@@ verify the archive with 7-zip? + } + } + + /// + /// We should be able to read a bzip2 compressed zip file created by 7-zip. + /// + [Test] + [Category("Zip")] + public void ShouldReadBZip2ZipCreatedBy7Zip() + { + const string bZip2CompressedZipCreatedBy7Zip = + "UEsDBC4AAAAMAIa50U4/rHf5qwAAAK8AAAAJAAAASGVsbG8udHh0QlpoOTFBWSZTWTL8pwYAA" + + "BWfgEhlUAAiLUgQP+feMCAAiCKaeiaBobU9JiaAMGmoak9GmRNqPUDQ9T1PQsz/t9B6YvEdvF" + + "5dhwXzGE1ooO41A6TtATBEFxFUq6trGtUcSJDyWWWj/S2VwY15fy3IqHi3hHUS+K76zdoDzQa" + + "VGE/4YkYZe3JAtv1EsIqIsiTnnktIbBo1R4xY3JZEOm2BvwLuSKcKEgZflODAUEsBAj8ALgAA" + + "AAwAhrnRTj+sd/mrAAAArwAAAAkAJAAAAAAAAAAgAAAAAAAAAEhlbGxvLnR4dAoAIAAAAAAAA" + + "QAYAO97MLZZJdUB73swtlkl1QEK0UTFWCXVAVBLBQYAAAAAAQABAFsAAADSAAAAAAA="; + + const string originalText = + "SharpZipLib (#ziplib, formerly NZipLib) is a compression library that supports Zip files using both stored and deflate compression methods, PKZIP 2.0 style and AES encryption."; + + var fileBytes = Convert.FromBase64String(bZip2CompressedZipCreatedBy7Zip); + + using var input = new MemoryStream(fileBytes, writable: false); + using var zf = new ZipFile(input); + var entry = zf.GetEntry("Hello.txt"); + Assert.That(entry.CompressionMethod, Is.EqualTo(CompressionMethod.BZip2), "Compression method should be BZip2"); + Assert.That(entry.Version, Is.EqualTo(ZipConstants.VersionBZip2), "Entry version should be 46"); + + using var reader = new StreamReader(zf.GetInputStream(entry)); + var contents = reader.ReadToEnd(); + Assert.That(contents, Is.EqualTo(originalText), "extract string must match original string"); + } + + /// + /// We should be able to read a bzip2 compressed / AES encrypted zip file created by 7-zip. + /// + [Test] + [Category("Zip")] + public void ShouldReadAESBZip2ZipCreatedBy7Zip() + { + const string bZip2CompressedZipCreatedBy7Zip = + "UEsDBDMAAQBjAIa50U4AAAAAxwAAAK8AAAAJAAsASGVsbG8udHh0AZkHAAIAQUUDDAAYg6jqf" + + "kvZClVMOtgmqKT0/8I9fMPgo96myxw9hLQUhKj1Qczi3fT7QIhAnAKU+u03nA8rCKGWmDI5Qz" + + "qPREy95boQVDPwmwEsWksv3GAWzMfzZUhmB/TgIJlA34a4yP0f2ucy3/QCQYo8QcHjBtjWX5b" + + "dZn0+fwY9Ci7q8JSI8zNSbgQ0Ert/lIJ9MxQ4lzBxMl4LySkd104cDPh/FslTAcPtHoy8Mf1c" + + "vnI1uICMgjWVeTqYrvSvt2uuHnqr4AiehArFiXTnUEsBAj8AMwABAGMAhrnRTgAAAADHAAAAr" + + "wAAAAkALwAAAAAAAAAgAAAAAAAAAEhlbGxvLnR4dAoAIAAAAAAAAQAYAO97MLZZJdUBYdnjul" + + "kl1QEK0UTFWCXVAQGZBwACAEFFAwwAUEsFBgAAAAABAAEAZgAAAPkAAAAAAA=="; + + const string originalText = + "SharpZipLib (#ziplib, formerly NZipLib) is a compression library that supports Zip files using both stored and deflate compression methods, PKZIP 2.0 style and AES encryption."; + + var fileBytes = Convert.FromBase64String(bZip2CompressedZipCreatedBy7Zip); + + using var input = new MemoryStream(fileBytes, writable: false); + using var zf = new ZipFile(input); + zf.Password = "password"; + + var entry = zf.GetEntry("Hello.txt"); + Assert.That(entry.CompressionMethod, Is.EqualTo(CompressionMethod.BZip2), "Compression method should be BZip2"); + Assert.That(entry.Version, Is.EqualTo(ZipConstants.VERSION_AES), "Entry version should be 51"); + Assert.That(entry.IsCrypted, Is.True, "Entry should be encrypted"); + Assert.That(entry.AESKeySize, Is.EqualTo(256), "AES Keysize should be 256"); + + using var reader = new StreamReader(zf.GetInputStream(entry)); + var contents = reader.ReadToEnd(); + Assert.That(contents, Is.EqualTo(originalText), "extract string must match original string"); + } + + /// + /// Test for https://github.com/icsharpcode/SharpZipLib/issues/147, when deleting items in a zip + /// + /// Whether Zip64 should be used in the test archive + [TestCase(UseZip64.On)] + [TestCase(UseZip64.Off)] + [Category("Zip")] + public void TestDescriptorUpdateOnDelete(UseZip64 useZip64) + { + MemoryStream msw = new MemoryStreamWithoutSeek(); + using (ZipOutputStream outStream = new ZipOutputStream(msw)) + { + outStream.UseZip64 = useZip64; + outStream.IsStreamOwner = false; + outStream.PutNextEntry(new ZipEntry("StripedMarlin")); + outStream.WriteByte(89); + + outStream.PutNextEntry(new ZipEntry("StripedMarlin2")); + outStream.WriteByte(91); + } + + var zipData = msw.ToArray(); + Assert.That(zipData, Does.PassTestArchive()); + + using (var memoryStream = new MemoryStream(zipData)) + { + using (var zipFile = new ZipFile(memoryStream, leaveOpen: true)) + { + zipFile.BeginUpdate(); + zipFile.Delete("StripedMarlin"); + zipFile.CommitUpdate(); + } + + memoryStream.Position = 0; + + using (var zipFile = new ZipFile(memoryStream, leaveOpen: true)) + { + Assert.That(zipFile, Does.PassTestArchive()); + } + } + } + + /// + /// Test for https://github.com/icsharpcode/SharpZipLib/issues/147, when adding items to a zip + /// + /// Whether Zip64 should be used in the test archive + [TestCase(UseZip64.On)] + [TestCase(UseZip64.Off)] + [Category("Zip")] + public void TestDescriptorUpdateOnAdd(UseZip64 useZip64) + { + MemoryStream msw = new MemoryStreamWithoutSeek(); + using (ZipOutputStream outStream = new ZipOutputStream(msw)) + { + outStream.UseZip64 = useZip64; + outStream.IsStreamOwner = false; + outStream.PutNextEntry(new ZipEntry("StripedMarlin")); + outStream.WriteByte(89); + } + + var zipData = msw.ToArray(); + Assert.That(zipData, Does.PassTestArchive()); + + using (var memoryStream = new MemoryStream()) + { + memoryStream.Write(zipData, 0, zipData.Length); + + using (var zipFile = new ZipFile(memoryStream, leaveOpen: true)) + { + zipFile.BeginUpdate(); + zipFile.Add(new StringMemoryDataSource("stripey"), "Zebra"); + zipFile.CommitUpdate(); + } + + memoryStream.Position = 0; + + using (var zipFile = new ZipFile(memoryStream, leaveOpen: true)) + { + Assert.That(zipFile, Does.PassTestArchive()); + } + } + } + + /// + /// Check that Zip files can be created with an empty file name + /// + [Test] + [Category("Zip")] + public void HandlesEmptyFileName() + { + using var ms = new MemoryStream(); + using (var zos = new ZipOutputStream(ms){IsStreamOwner = false}) + { + zos.PutNextEntry(new ZipEntry(String.Empty)); + Utils.WriteDummyData(zos, 64); + } + ms.Seek(0, SeekOrigin.Begin); + using (var zis = new ZipInputStream(ms){IsStreamOwner = false}) + { + var entry = zis.GetNextEntry(); + Assert.That(entry.Name, Is.Empty); + Assert.That(zis.ReadBytes(64).Length, Is.EqualTo(64)); + } + } } } diff --git a/test/ICSharpCode.SharpZipLib.Tests/Zip/ZipNameTransformHandling.cs b/test/ICSharpCode.SharpZipLib.Tests/Zip/ZipNameTransformHandling.cs index 2536494cd..5ec8a07e8 100644 --- a/test/ICSharpCode.SharpZipLib.Tests/Zip/ZipNameTransformHandling.cs +++ b/test/ICSharpCode.SharpZipLib.Tests/Zip/ZipNameTransformHandling.cs @@ -1,8 +1,9 @@ -using System; -using System.IO; -using ICSharpCode.SharpZipLib.Core; +using ICSharpCode.SharpZipLib.Core; using ICSharpCode.SharpZipLib.Zip; using NUnit.Framework; +using System; +using System.IO; +using ICSharpCode.SharpZipLib.Tests.TestSupport; namespace ICSharpCode.SharpZipLib.Tests.Zip { @@ -16,8 +17,6 @@ public void Basic() var t = new ZipNameTransform(); TestFile(t, "abcdef", "abcdef"); - TestFile(t, @"\\uncpath\d1\file1", "file1"); - TestFile(t, @"C:\absolute\file2", "absolute/file2"); // This is ignored but could be converted to 'file3' TestFile(t, @"./file3", "./file3"); @@ -28,44 +27,73 @@ public void Basic() // Trick filenames. TestFile(t, @".....file3", ".....file3"); + } + + [Test] + [Category("Zip")] + [Platform("Win")] + public void Basic_Windows() + { + var t = new ZipNameTransform(); + TestFile(t, @"\\uncpath\d1\file1", "file1"); + TestFile(t, @"C:\absolute\file2", "absolute/file2"); + TestFile(t, @"c::file", "_file"); } + + [Test] + [Category("Zip")] + [Platform(Exclude="Win")] + public void Basic_Posix() + { + var t = new ZipNameTransform(); + TestFile(t, @"backslash_path\file1", "backslash_path/file1"); + TestFile(t, "/absolute/file2", "absolute/file2"); + + TestFile(t, @"////////:file", "_file"); + } [Test] public void TooLong() { var zt = new ZipNameTransform(); - var veryLong = new string('x', 65536); - try { - zt.TransformDirectory(veryLong); - Assert.Fail("Expected an exception"); - } catch (PathTooLongException) { - } + var tooLong = new string('x', 65536); + Assert.Throws(() => zt.TransformDirectory(tooLong)); } [Test] public void LengthBoundaryOk() { var zt = new ZipNameTransform(); - string veryLong = "c:\\" + new string('x', 65535); - try { - zt.TransformDirectory(veryLong); - } catch { - Assert.Fail("Expected no exception"); - } + var tooLongWithRoot = Utils.SystemRoot + new string('x', 65535); + Assert.DoesNotThrow(() => zt.TransformDirectory(tooLongWithRoot)); } [Test] [Category("Zip")] - public void NameTransforms() + [Platform("Win")] + public void NameTransforms_Windows() { INameTransform t = new ZipNameTransform(@"C:\Slippery"); Assert.AreEqual("Pongo/Directory/", t.TransformDirectory(@"C:\Slippery\Pongo\Directory"), "Value should be trimmed and converted"); Assert.AreEqual("PoNgo/Directory/", t.TransformDirectory(@"c:\slipperY\PoNgo\Directory"), "Trimming should be case insensitive"); - Assert.AreEqual("slippery/Pongo/Directory/", t.TransformDirectory(@"d:\slippery\Pongo\Directory"), "Trimming should be case insensitive"); + Assert.AreEqual("slippery/Pongo/Directory/", t.TransformDirectory(@"d:\slippery\Pongo\Directory"), "Trimming should account for root"); Assert.AreEqual("Pongo/File", t.TransformFile(@"C:\Slippery\Pongo\File"), "Value should be trimmed and converted"); } + + [Test] + [Category("Zip")] + [Platform(Exclude="Win")] + public void NameTransforms_Posix() + { + INameTransform t = new ZipNameTransform(@"/Slippery"); + Assert.AreEqual("Pongo/Directory/", t.TransformDirectory(@"/Slippery\Pongo\Directory"), "Value should be trimmed and converted"); + Assert.AreEqual("PoNgo/Directory/", t.TransformDirectory(@"/slipperY\PoNgo\Directory"), "Trimming should be case insensitive"); + Assert.AreEqual("slippery/Pongo/Directory/", t.TransformDirectory(@"/slippery/slippery/Pongo/Directory"), "Trimming should account for root"); + + Assert.AreEqual("Pongo/File", t.TransformFile(@"/Slippery/Pongo/File"), "Value should be trimmed and converted"); + } /// /// Test ZipEntry static file name cleaning methods @@ -74,10 +102,16 @@ public void NameTransforms() [Category("Zip")] public void FilenameCleaning() { - Assert.AreEqual(0, string.Compare(ZipEntry.CleanName("hello"), "hello", StringComparison.Ordinal)); - Assert.AreEqual(0, string.Compare(ZipEntry.CleanName(@"z:\eccles"), "eccles", StringComparison.Ordinal)); - Assert.AreEqual(0, string.Compare(ZipEntry.CleanName(@"\\server\share\eccles"), "eccles", StringComparison.Ordinal)); - Assert.AreEqual(0, string.Compare(ZipEntry.CleanName(@"\\server\share\dir\eccles"), "dir/eccles", StringComparison.Ordinal)); + Assert.AreEqual("hello", ZipEntry.CleanName("hello")); + if(Environment.OSVersion.Platform == PlatformID.Win32NT) + { + Assert.AreEqual("eccles", ZipEntry.CleanName(@"z:\eccles")); + Assert.AreEqual("eccles", ZipEntry.CleanName(@"\\server\share\eccles")); + Assert.AreEqual("dir/eccles", ZipEntry.CleanName(@"\\server\share\dir\eccles")); + } + else { + Assert.AreEqual("eccles", ZipEntry.CleanName(@"/eccles")); + } } [Test] diff --git a/test/ICSharpCode.SharpZipLib.Tests/Zip/ZipStreamAsyncTests.cs b/test/ICSharpCode.SharpZipLib.Tests/Zip/ZipStreamAsyncTests.cs new file mode 100644 index 000000000..d228e5ee4 --- /dev/null +++ b/test/ICSharpCode.SharpZipLib.Tests/Zip/ZipStreamAsyncTests.cs @@ -0,0 +1,154 @@ +using System.IO; +using System.Threading; +using System.Threading.Tasks; +using ICSharpCode.SharpZipLib.Zip; +using ICSharpCode.SharpZipLib.Tests.TestSupport; +using NUnit.Framework; + +namespace ICSharpCode.SharpZipLib.Tests.Zip +{ + [TestFixture] + public class ZipStreamAsyncTests + { + [Test] + [Category("Zip")] + [Category("Async")] + public async Task WriteZipStreamUsingAsync() + { +#if NETSTANDARD2_1 || NETCOREAPP3_0_OR_GREATER + await using var ms = new MemoryStream(); + + await using (var outStream = new ZipOutputStream(ms){IsStreamOwner = false}) + { + await outStream.PutNextEntryAsync(new ZipEntry("FirstFile")); + await Utils.WriteDummyDataAsync(outStream, 12); + + await outStream.PutNextEntryAsync(new ZipEntry("SecondFile")); + await Utils.WriteDummyDataAsync(outStream, 12); + } + + ZipTesting.AssertValidZip(ms); +#else + await Task.CompletedTask; + Assert.Ignore("Async Using is not supported"); +#endif + } + + [Test] + [Category("Zip")] + [Category("Async")] + public async Task WriteZipStreamAsync () + { + using var ms = new MemoryStream(); + + using(var outStream = new ZipOutputStream(ms) { IsStreamOwner = false }) + { + await outStream.PutNextEntryAsync(new ZipEntry("FirstFile")); + await Utils.WriteDummyDataAsync(outStream, 12); + + await outStream.PutNextEntryAsync(new ZipEntry("SecondFile")); + await Utils.WriteDummyDataAsync(outStream, 12); + + await outStream.FinishAsync(CancellationToken.None); + } + + ZipTesting.AssertValidZip(ms); + } + + + [Test] + [Category("Zip")] + [Category("Async")] + public async Task WriteZipStreamWithAesAsync() + { + using var ms = new MemoryStream(); + var password = "f4ls3p0s1t1v3"; + + using (var outStream = new ZipOutputStream(ms){IsStreamOwner = false, Password = password}) + { + await outStream.PutNextEntryAsync(new ZipEntry("FirstFile"){AESKeySize = 256}); + await Utils.WriteDummyDataAsync(outStream, 12); + + await outStream.PutNextEntryAsync(new ZipEntry("SecondFile"){AESKeySize = 256}); + await Utils.WriteDummyDataAsync(outStream, 12); + + await outStream.FinishAsync(CancellationToken.None); + } + + ZipTesting.AssertValidZip(ms, password); + } + + [Test] + [Category("Zip")] + [Category("Async")] + public async Task WriteZipStreamWithZipCryptoAsync() + { + using var ms = new MemoryStream(); + var password = "f4ls3p0s1t1v3"; + + using (var outStream = new ZipOutputStream(ms){IsStreamOwner = false, Password = password}) + { + await outStream.PutNextEntryAsync(new ZipEntry("FirstFile"){AESKeySize = 0}); + await Utils.WriteDummyDataAsync(outStream, 12); + + await outStream.PutNextEntryAsync(new ZipEntry("SecondFile"){AESKeySize = 0}); + await Utils.WriteDummyDataAsync(outStream, 12); + + await outStream.FinishAsync(CancellationToken.None); + } + + ZipTesting.AssertValidZip(ms, password, false); + } + + [Test] + [Category("Zip")] + [Category("Async")] + public async Task WriteReadOnlyZipStreamAsync () + { + using var ms = new MemoryStreamWithoutSeek(); + + using(var outStream = new ZipOutputStream(ms) { IsStreamOwner = false }) + { + await outStream.PutNextEntryAsync(new ZipEntry("FirstFile")); + await Utils.WriteDummyDataAsync(outStream, 12); + + await outStream.PutNextEntryAsync(new ZipEntry("SecondFile")); + await Utils.WriteDummyDataAsync(outStream, 12); + + await outStream.FinishAsync(CancellationToken.None); + } + + ZipTesting.AssertValidZip(new MemoryStream(ms.ToArray())); + } + + [Test] + [Category("Zip")] + [Category("Async")] + [TestCase(12, Description = "Small files")] + [TestCase(12000, Description = "Large files")] + public async Task WriteZipStreamToAsyncOnlyStream (int fileSize) + { +#if NETSTANDARD2_1 || NETCOREAPP3_0_OR_GREATER + await using(var ms = new MemoryStreamWithoutSync()){ + await using(var outStream = new ZipOutputStream(ms) { IsStreamOwner = false }) + { + await outStream.PutNextEntryAsync(new ZipEntry("FirstFile")); + await Utils.WriteDummyDataAsync(outStream, fileSize); + + await outStream.PutNextEntryAsync(new ZipEntry("SecondFile")); + await Utils.WriteDummyDataAsync(outStream, fileSize); + + await outStream.FinishAsync(CancellationToken.None); + await outStream.DisposeAsync(); + } + + ZipTesting.AssertValidZip(new MemoryStream(ms.ToArray())); + } +#else + await Task.CompletedTask; + Assert.Ignore("AsyncDispose is not supported"); +#endif + } + + } +} \ No newline at end of file diff --git a/test/ICSharpCode.SharpZipLib.Tests/Zip/ZipStringsTests.cs b/test/ICSharpCode.SharpZipLib.Tests/Zip/ZipStringsTests.cs new file mode 100644 index 000000000..cd213df6e --- /dev/null +++ b/test/ICSharpCode.SharpZipLib.Tests/Zip/ZipStringsTests.cs @@ -0,0 +1,239 @@ +using ICSharpCode.SharpZipLib.Tests.TestSupport; +using ICSharpCode.SharpZipLib.Tests.Zip; +using ICSharpCode.SharpZipLib.Zip; +using NUnit.Framework; +using System; +using System.Collections.Generic; +using System.IO; +using System.Text; +using Does = ICSharpCode.SharpZipLib.Tests.TestSupport.Does; + +// As there is no way to order the test namespace execution order we use a name that should be alphabetically sorted before any other namespace +// This is because we have one test that only works when no encoding provider has been loaded which is not reversable once done. +namespace ICSharpCode.SharpZipLib.Tests._Zip +{ + [TestFixture] + [Order(1)] + public class ZipStringsTests + { + [Test] + [Order(1)] + // NOTE: This test needs to be run before any test registering CodePagesEncodingProvider.Instance + public void TestSystemDefaultEncoding() + { + Console.WriteLine($"Default encoding before registering provider: {Encoding.GetEncoding(0).EncodingName}"); + Encoding.RegisterProvider(new TestEncodingProvider()); + Console.WriteLine($"Default encoding after registering provider: {Encoding.GetEncoding(0).EncodingName}"); + + // Initialize a default StringCodec + var sc = StringCodec.Default; + + var legacyEncoding = sc.ZipEncoding(false); + Assert.That(legacyEncoding.EncodingName, Is.EqualTo(TestEncodingProvider.DefaultEncodingName)); + Assert.That(legacyEncoding.CodePage, Is.EqualTo(TestEncodingProvider.DefaultEncodingCodePage)); + } + + [Test] + [Order(2)] + public void TestFastZipRoundTripWithCodePage() + { + Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); + + using var ms = new MemoryStream(); + using var zipFile = new TempFile(); + using var srcDir = new TempDir(); + using var dstDir = new TempDir(); + + srcDir.CreateDummyFile("file1"); + srcDir.CreateDummyFile("слово"); + + foreach(var f in Directory.EnumerateFiles(srcDir.FullName)) + { + Console.WriteLine(f); + } + + var fzCreate = new FastZip() { StringCodec = StringCodec.FromCodePage(866), UseUnicode = false }; + fzCreate.CreateZip(zipFile, srcDir.FullName, true, null); + + var fzExtract = new FastZip() { StringCodec = StringCodec.FromCodePage(866) }; + fzExtract.ExtractZip(zipFile, dstDir.FullName, null); + + foreach (var f in Directory.EnumerateFiles(dstDir.FullName)) + { + Console.WriteLine(f); + } + + Assert.That(dstDir.GetFile("file1").FullName, Does.Exist); + Assert.That(dstDir.GetFile("слово").FullName, Does.Exist); + } + + + [Test] + [Order(2)] + public void TestZipFileRoundTripWithCodePage() + { + Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); + + using var ms = new MemoryStream(); + using (var zf = ZipFile.Create(ms)) + { + zf.StringCodec = StringCodec.FromCodePage(866); + zf.BeginUpdate(); + zf.Add(MemoryDataSource.Empty, "file1", CompressionMethod.Stored, useUnicodeText: false); + zf.Add(MemoryDataSource.Empty, "слово", CompressionMethod.Stored, useUnicodeText: false); + zf.CommitUpdate(); + } + + ms.Seek(0, SeekOrigin.Begin); + + using (var zf = new ZipFile(ms, false, StringCodec.FromCodePage(866)) { IsStreamOwner = false }) + { + Assert.That(zf.GetEntry("file1"), Is.Not.Null); + Assert.That(zf.GetEntry("слово"), Is.Not.Null); + } + + } + + [Test] + [Order(2)] + public void TestZipStreamRoundTripWithCodePage() + { + Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); + + using var ms = new MemoryStream(); + using (var zos = new ZipOutputStream(ms, StringCodec.FromCodePage(866)) { IsStreamOwner = false }) + { + zos.PutNextEntry(new ZipEntry("file1") { IsUnicodeText = false }); + zos.PutNextEntry(new ZipEntry("слово") { IsUnicodeText = false }); + } + + ms.Seek(0, SeekOrigin.Begin); + + using (var zis = new ZipInputStream(ms, StringCodec.FromCodePage(866)) { IsStreamOwner = false }) + { + Assert.That(zis.GetNextEntry().Name, Is.EqualTo("file1")); + Assert.That(zis.GetNextEntry().Name, Is.EqualTo("слово")); + } + + } + + [Test] + [Order(2)] + public void TestZipCryptoPasswordEncodingRoundtrip() + { + Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); + + var content = Utils.GetDummyBytes(32); + + using var ms = new MemoryStream(); + using (var zos = new ZipOutputStream(ms, StringCodec.FromCodePage(866)) { IsStreamOwner = false }) + { + zos.Password = "слово"; + zos.PutNextEntry(new ZipEntry("file1")); + zos.Write(content, 0, content.Length); + } + + ms.Seek(0, SeekOrigin.Begin); + + using (var zis = new ZipInputStream(ms, StringCodec.FromCodePage(866)) { IsStreamOwner = false }) + { + zis.Password = "слово"; + var entry = zis.GetNextEntry(); + var output = new byte[32]; + Assert.That(zis.Read(output, 0, 32), Is.EqualTo(32)); + Assert.That(output, Is.EqualTo(content)); + } + + } + + [Test] + [Order(2)] + public void TestZipStreamCommentEncodingRoundtrip() + { + Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); + + var content = Utils.GetDummyBytes(32); + + using var ms = new MemoryStream(); + using (var zos = new ZipOutputStream(ms, StringCodec.FromCodePage(866)) { IsStreamOwner = false }) + { + zos.SetComment("слово"); + } + + ms.Seek(0, SeekOrigin.Begin); + + using var zf = new ZipFile(ms, false, StringCodec.FromCodePage(866)); + Assert.That(zf.ZipFileComment, Is.EqualTo("слово")); + } + + + [Test] + [Order(2)] + public void TestZipFileCommentEncodingRoundtrip() + { + Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); + + var content = Utils.GetDummyBytes(32); + + using var ms = new MemoryStream(); + using (var zf = ZipFile.Create(ms)) + { + zf.StringCodec = StringCodec.FromCodePage(866); + zf.BeginUpdate(); + zf.SetComment("слово"); + zf.CommitUpdate(); + } + + ms.Seek(0, SeekOrigin.Begin); + + using (var zf = new ZipFile(ms, false, StringCodec.FromCodePage(866))) + { + Assert.That(zf.ZipFileComment, Is.EqualTo("слово")); + } + } + } + + + internal class TestEncodingProvider : EncodingProvider + { + internal static string DefaultEncodingName = "TestDefaultEncoding"; + internal static int DefaultEncodingCodePage = -37; + + class TestDefaultEncoding : Encoding + { + public override string EncodingName => DefaultEncodingName; + public override int CodePage => DefaultEncodingCodePage; + + public override int GetByteCount(char[] chars, int index, int count) + => UTF8.GetByteCount(chars, index, count); + + public override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex) + => UTF8.GetBytes(chars, charIndex, charCount, bytes, byteIndex); + + public override int GetCharCount(byte[] bytes, int index, int count) + => UTF8.GetCharCount(bytes, index, count); + + public override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex) + => UTF8.GetChars(bytes, byteIndex, byteCount, chars, charIndex); + + public override int GetMaxByteCount(int charCount) => UTF8.GetMaxByteCount(charCount); + + public override int GetMaxCharCount(int byteCount) => UTF8.GetMaxCharCount(byteCount); + } + + TestDefaultEncoding testDefaultEncoding = new TestDefaultEncoding(); + + public override Encoding GetEncoding(int codepage) + => (codepage == 0 || codepage == DefaultEncodingCodePage) ? testDefaultEncoding : null; + + public override Encoding GetEncoding(string name) + => DefaultEncodingName == name ? testDefaultEncoding : null; + +#if NET6_0_OR_GREATER + public override IEnumerable GetEncodings() + { + yield return new EncodingInfo(this, DefaultEncodingCodePage, DefaultEncodingName, DefaultEncodingName); + } +#endif + } +} diff --git a/test/ICSharpCode.SharpZipLib.Tests/Zip/ZipTests.cs b/test/ICSharpCode.SharpZipLib.Tests/Zip/ZipTests.cs index 712e0c60b..885c976f3 100644 --- a/test/ICSharpCode.SharpZipLib.Tests/Zip/ZipTests.cs +++ b/test/ICSharpCode.SharpZipLib.Tests/Zip/ZipTests.cs @@ -1,16 +1,17 @@ -using System; -using System.IO; -using System.Security; -using System.Text; using ICSharpCode.SharpZipLib.Checksum; using ICSharpCode.SharpZipLib.Core; using ICSharpCode.SharpZipLib.Tests.TestSupport; using ICSharpCode.SharpZipLib.Zip; using NUnit.Framework; +using System; +using System.IO; +using System.Security; +using System.Text; +using System.Threading.Tasks; namespace ICSharpCode.SharpZipLib.Tests.Zip { - class RuntimeInfo + internal class RuntimeInfo { public RuntimeInfo(CompressionMethod method, int compressionLevel, int size, string password, bool getCrc) @@ -22,23 +23,26 @@ public RuntimeInfo(CompressionMethod method, int compressionLevel, this.random = false; original = new byte[Size]; - if (random) { - var rnd = new Random(); - rnd.NextBytes(original); - } else { - for (int i = 0; i < size; ++i) { + if (random) + { + original = Utils.GetDummyBytes(Size); + } + else + { + for (int i = 0; i < size; ++i) + { original[i] = (byte)'A'; } } - if (getCrc) { + if (getCrc) + { var crc32 = new Crc32(); - crc32.Update(original, 0, size); + crc32.Update(new ArraySegment(original, 0, size)); crc = crc32.Value; } } - public RuntimeInfo(string password, bool isDirectory) { this.method = CompressionMethod.Stored; @@ -50,53 +54,64 @@ public RuntimeInfo(string password, bool isDirectory) original = new byte[0]; } - public byte[] Original { + public byte[] Original + { get { return original; } } - public CompressionMethod Method { + public CompressionMethod Method + { get { return method; } } - public int CompressionLevel { + public int CompressionLevel + { get { return compressionLevel; } } - public int Size { + public int Size + { get { return size; } } - public string Password { + public string Password + { get { return password; } } - bool Random { + private bool Random + { get { return random; } } - public long Crc { + public long Crc + { get { return crc; } } - public bool IsDirectory { + public bool IsDirectory + { get { return isDirectory_; } } #region Instance Fields - readonly byte[] original; - readonly CompressionMethod method; - int compressionLevel; - int size; - string password; - bool random; - bool isDirectory_; - long crc = -1; - #endregion + + private readonly byte[] original; + private readonly CompressionMethod method; + private int compressionLevel; + private int size; + private string password; + private bool random; + private bool isDirectory_; + private long crc = -1; + + #endregion Instance Fields } - class MemoryDataSource : IStaticDataSource + internal class MemoryDataSource : IStaticDataSource { #region Constructors + /// /// Initialise a new instance. /// @@ -105,7 +120,10 @@ public MemoryDataSource(byte[] data) { data_ = data; } - #endregion + + public static MemoryDataSource Empty => new MemoryDataSource(Array.Empty()); + + #endregion Constructors #region IDataSource Members @@ -117,14 +135,17 @@ public Stream GetSource() { return new MemoryStream(data_); } - #endregion + + #endregion IDataSource Members #region Instance Fields - readonly byte[] data_; - #endregion + + private readonly byte[] data_; + + #endregion Instance Fields } - class StringMemoryDataSource : MemoryDataSource + internal class StringMemoryDataSource : MemoryDataSource { public StringMemoryDataSource(string data) : base(Encoding.ASCII.GetBytes(data)) @@ -137,9 +158,12 @@ public class ZipBase static protected string GetTempFilePath() { string result = null; - try { + try + { result = Path.GetTempPath(); - } catch (SecurityException) { + } + catch (SecurityException) + { } return result; } @@ -148,38 +172,49 @@ protected byte[] MakeInMemoryZip(bool withSeek, params object[] createSpecs) { MemoryStream ms; - if (withSeek) { + if (withSeek) + { ms = new MemoryStream(); - } else { + } + else + { ms = new MemoryStreamWithoutSeek(); } - using (ZipOutputStream outStream = new ZipOutputStream(ms)) { - for (int counter = 0; counter < createSpecs.Length; ++counter) { + using (ZipOutputStream outStream = new ZipOutputStream(ms)) + { + for (int counter = 0; counter < createSpecs.Length; ++counter) + { var info = createSpecs[counter] as RuntimeInfo; outStream.Password = info.Password; - if (info.Method != CompressionMethod.Stored) { + if (info.Method != CompressionMethod.Stored) + { outStream.SetLevel(info.CompressionLevel); // 0 - store only to 9 - means best compression } string entryName; - if (info.IsDirectory) { + if (info.IsDirectory) + { entryName = "dir" + counter + "/"; - } else { + } + else + { entryName = "entry" + counter + ".tst"; } var entry = new ZipEntry(entryName); entry.CompressionMethod = info.Method; - if (info.Crc >= 0) { + if (info.Crc >= 0) + { entry.Crc = info.Crc; } outStream.PutNextEntry(entry); - if (info.Size > 0) { + if (info.Size > 0) + { outStream.Write(info.Original, 0, info.Original.Length); } } @@ -192,16 +227,21 @@ protected byte[] MakeInMemoryZip(ref byte[] original, CompressionMethod method, { MemoryStream ms; - if (withSeek) { + if (withSeek) + { ms = new MemoryStream(); - } else { + } + else + { ms = new MemoryStreamWithoutSeek(); } - using (ZipOutputStream outStream = new ZipOutputStream(ms)) { + using (ZipOutputStream outStream = new ZipOutputStream(ms)) + { outStream.Password = password; - if (method != CompressionMethod.Stored) { + if (method != CompressionMethod.Stored) + { outStream.SetLevel(compressionLevel); // 0 - store only to 9 - means best compression } @@ -210,16 +250,16 @@ protected byte[] MakeInMemoryZip(ref byte[] original, CompressionMethod method, outStream.PutNextEntry(entry); - if (size > 0) { - var rnd = new Random(); - original = new byte[size]; - rnd.NextBytes(original); + if (size > 0) + { + original = Utils.GetDummyBytes(size); // Although this could be written in one chunk doing it in lumps // throws up buffering problems including with encryption the original // source for this change. int index = 0; - while (size > 0) { + while (size > 0) + { int count = (size > 0x200) ? 0x200 : size; outStream.Write(original, index, count); size -= 0x200; @@ -232,9 +272,11 @@ protected byte[] MakeInMemoryZip(ref byte[] original, CompressionMethod method, protected static void MakeTempFile(string name, int size) { - using (FileStream fs = File.Create(name)) { + using (FileStream fs = File.Create(name)) + { byte[] buffer = new byte[4096]; - while (size > 0) { + while (size > 0) + { fs.Write(buffer, 0, Math.Min(size, buffer.Length)); size -= buffer.Length; } @@ -246,19 +288,22 @@ protected static byte ScatterValue(byte rhs) return (byte)((rhs * 253 + 7) & 0xff); } - static void AddKnownDataToEntry(ZipOutputStream zipStream, int size) + private static void AddKnownDataToEntry(Stream zipStream, int size) { - if (size > 0) { + if (size > 0) + { byte nextValue = 0; int bufferSize = Math.Min(size, 65536); byte[] data = new byte[bufferSize]; int currentIndex = 0; - for (int i = 0; i < size; ++i) { + for (int i = 0; i < size; ++i) + { data[currentIndex] = nextValue; nextValue = ScatterValue(nextValue); currentIndex += 1; - if ((currentIndex >= data.Length) || (i + 1 == size)) { + if ((currentIndex >= data.Length) || (i + 1 == size)) + { zipStream.Write(data, 0, currentIndex); currentIndex = 0; } @@ -268,18 +313,22 @@ static void AddKnownDataToEntry(ZipOutputStream zipStream, int size) public void WriteToFile(string fileName, byte[] data) { - using (FileStream fs = File.Open(fileName, FileMode.Create, FileAccess.ReadWrite, FileShare.Read)) { + using (FileStream fs = File.Open(fileName, FileMode.Create, FileAccess.ReadWrite, FileShare.Read)) + { fs.Write(data, 0, data.Length); } } #region MakeZipFile + protected void MakeZipFile(Stream storage, bool isOwner, string[] names, int size, string comment) { - using (ZipOutputStream zOut = new ZipOutputStream(storage)) { + using (ZipOutputStream zOut = new ZipOutputStream(storage)) + { zOut.IsStreamOwner = isOwner; zOut.SetComment(comment); - for (int i = 0; i < names.Length; ++i) { + for (int i = 0; i < names.Length; ++i) + { zOut.PutNextEntry(new ZipEntry(names[i])); AddKnownDataToEntry(zOut, size); } @@ -289,10 +338,13 @@ protected void MakeZipFile(Stream storage, bool isOwner, string[] names, int siz protected void MakeZipFile(string name, string[] names, int size, string comment) { - using (FileStream fs = File.Create(name)) { - using (ZipOutputStream zOut = new ZipOutputStream(fs)) { + using (FileStream fs = File.Create(name)) + { + using (ZipOutputStream zOut = new ZipOutputStream(fs)) + { zOut.SetComment(comment); - for (int i = 0; i < names.Length; ++i) { + for (int i = 0; i < names.Length; ++i) + { zOut.PutNextEntry(new ZipEntry(names[i])); AddKnownDataToEntry(zOut, size); } @@ -301,15 +353,19 @@ protected void MakeZipFile(string name, string[] names, int size, string comment fs.Close(); } } - #endregion + + #endregion MakeZipFile #region MakeZipFile Entries + protected void MakeZipFile(string name, string entryNamePrefix, int entries, int size, string comment) { using (FileStream fs = File.Create(name)) - using (ZipOutputStream zOut = new ZipOutputStream(fs)) { + using (ZipOutputStream zOut = new ZipOutputStream(fs)) + { zOut.SetComment(comment); - for (int i = 0; i < entries; ++i) { + for (int i = 0; i < entries; ++i) + { zOut.PutNextEntry(new ZipEntry(entryNamePrefix + (i + 1))); AddKnownDataToEntry(zOut, size); } @@ -319,17 +375,40 @@ protected void MakeZipFile(string name, string entryNamePrefix, int entries, int protected void MakeZipFile(Stream storage, bool isOwner, string entryNamePrefix, int entries, int size, string comment) { - using (ZipOutputStream zOut = new ZipOutputStream(storage)) { + using (ZipOutputStream zOut = new ZipOutputStream(storage)) + { zOut.IsStreamOwner = isOwner; zOut.SetComment(comment); - for (int i = 0; i < entries; ++i) { + for (int i = 0; i < entries; ++i) + { zOut.PutNextEntry(new ZipEntry(entryNamePrefix + (i + 1))); AddKnownDataToEntry(zOut, size); } } } - #endregion + protected void MakeZipFile(Stream storage, CompressionMethod compressionMethod, bool isOwner, + string entryNamePrefix, int entries, int size, string comment) + { + using (ZipFile f = new ZipFile(storage, leaveOpen: !isOwner)) + { + f.BeginUpdate(); + f.SetComment(comment); + + for (int i = 0; i < entries; ++i) + { + var data = new MemoryStream(); + AddKnownDataToEntry(data, size); + + var m = new MemoryDataSource(data.ToArray()); + f.Add(m, entryNamePrefix + (i + 1), compressionMethod); + } + + f.CommitUpdate(); + } + } + + #endregion MakeZipFile Entries protected static void CheckKnownEntry(Stream inStream, int expectedCount) { @@ -338,9 +417,30 @@ protected static void CheckKnownEntry(Stream inStream, int expectedCount) int bytesRead; int total = 0; byte nextValue = 0; - while ((bytesRead = inStream.Read(buffer, 0, buffer.Length)) > 0) { + while ((bytesRead = inStream.Read(buffer, 0, buffer.Length)) > 0) + { + total += bytesRead; + for (int i = 0; i < bytesRead; ++i) + { + Assert.AreEqual(nextValue, buffer[i], "Wrong value read from entry"); + nextValue = ScatterValue(nextValue); + } + } + Assert.AreEqual(expectedCount, total, "Wrong number of bytes read from entry"); + } + + protected static async Task CheckKnownEntryAsync(Stream inStream, int expectedCount) + { + byte[] buffer = new byte[1024]; + + int bytesRead; + int total = 0; + byte nextValue = 0; + while ((bytesRead = await inStream.ReadAsync(buffer, 0, buffer.Length)) > 0) + { total += bytesRead; - for (int i = 0; i < bytesRead; ++i) { + for (int i = 0; i < bytesRead; ++i) + { Assert.AreEqual(nextValue, buffer[i], "Wrong value read from entry"); nextValue = ScatterValue(nextValue); } @@ -368,15 +468,15 @@ protected long ReadLong(Stream stream) long result = ReadInt(stream) & 0xffffffff; return result | (((long)ReadInt(stream)) << 32); } - } - class TestHelper + internal class TestHelper { static public void SaveMemoryStream(MemoryStream ms, string fileName) { byte[] data = ms.ToArray(); - using (FileStream fs = File.Open(fileName, FileMode.Create, FileAccess.ReadWrite, FileShare.Read)) { + using (FileStream fs = File.Open(fileName, FileMode.Create, FileAccess.ReadWrite, FileShare.Read)) + { fs.Write(data, 0, data.Length); } } @@ -386,15 +486,20 @@ static public int CompareDosDateTimes(DateTime l, DateTime r) // Compare dates to dos accuracy... // Ticks can be different yet all these values are still the same! int result = l.Year - r.Year; - if (result == 0) { + if (result == 0) + { result = l.Month - r.Month; - if (result == 0) { + if (result == 0) + { result = l.Day - r.Day; - if (result == 0) { + if (result == 0) + { result = l.Hour - r.Hour; - if (result == 0) { + if (result == 0) + { result = l.Minute - r.Minute; - if (result == 0) { + if (result == 0) + { result = (l.Second / 2) - (r.Second / 2); } } diff --git a/tools/appveyor-docfx-build.ps1 b/tools/appveyor-docfx-build.ps1 new file mode 100644 index 000000000..d5db36dfa --- /dev/null +++ b/tools/appveyor-docfx-build.ps1 @@ -0,0 +1,24 @@ +if(-Not $env:APPVEYOR_PULL_REQUEST_TITLE -and $env:CONFIGURATION -eq "Release") +{ + pushd docs\help + # & docfx metadata + & docfx docfx.json + if ($lastexitcode -ne 0){ + throw [System.Exception] "docfx build failed with exit code $lastexitcode." + } + popd +<# + ## Useful for automatically updating gh pages: + + git config --global credential.helper store + Add-Content "$env:USERPROFILE\.git-credentials" "https://$($env:access_token):x-oauth-basic@github.com`n" + git config --global user.email $env:op_build_user_email + git config --global user.name $env:op_build_user + git clone https://github.com/ICSharpCode/SharpZipLib.git -b gh-pages origin_site -q + Copy-Item origin_site/.git _site -recurse + CD _site + git add -A 2>&1 + git commit -m "CI Updates" -q + git push origin gh-pages -q +#> +} \ No newline at end of file diff --git a/tools/appveyor-docfx-init.ps1 b/tools/appveyor-docfx-init.ps1 new file mode 100644 index 000000000..a5a40d6fa --- /dev/null +++ b/tools/appveyor-docfx-init.ps1 @@ -0,0 +1,5 @@ +if(-Not $env:APPVEYOR_PULL_REQUEST_TITLE -and $env:CONFIGURATION -eq "Release") +{ + git checkout $env:APPVEYOR_REPO_BRANCH -q + choco install docfx -y +} \ No newline at end of file diff --git a/tools/appveyor-install.ps1 b/tools/appveyor-install.ps1 new file mode 100644 index 000000000..443988dac --- /dev/null +++ b/tools/appveyor-install.ps1 @@ -0,0 +1,53 @@ +# Describe from the lastest tag matching 'vX.Y.Z', removing the initial 'v' +$description = $(git describe --long --tags --match 'v[0-9]*.[0-9]*.[0-9]*').substring(1); + +# Description is in the format of: TAG-COMMITS_SINCE_TAG-COMMIT_HASH +$dparts = $description -split('-'); +$short_version = $dparts[0]; +$commits_since_tag = $dparts[1]; +$commit_hash = $dparts[2]; + +$masterBranches = @("master"); + +$is_tag_build = ($env:APPVEYOR_REPO_TAG -eq "true"); + +# If not in master branch, set branch variable +$av_branch = $env:APPVEYOR_REPO_BRANCH; +$branch = $(if ($is_tag_build -or $masterBranches -contains $av_branch) { "" } else { "-$av_branch" }); + +# If this is a PR, add the PR suffix +$suffix = $(if ($env:APPVEYOR_PULL_REQUEST_NUMBER) { "-pr$env:APPVEYOR_PULL_REQUEST_NUMBER" } else { "" }); + +# Main build is when we're in the master branch and not a PR +$is_main_build = ($branch -eq "" -and $suffix -eq "") + +# Use appveyor build number as the last version digit (x.x.x.B) +$build = ${env:APPVEYOR_BUILD_NUMBER} + +$is_release_build = ($commits_since_tag -eq 0 -and $is_main_build) + +$version = $(if ($is_release_build) { $short_version } else { "$short_version-$commit_hash" }) +$bin_version = "$short_version.$build" + +write-host -n "Branch: "; +write-host -f cyan $av_branch; + +write-host -n "Release type: "; +if ($is_release_build) {write-host -f green 'release'} else { write-host -f yellow 'pre-release'} + +write-host -n "NuGet Package Version: "; +write-host -f cyan $version; + +write-host -n "Assembly Version: "; +write-host -f cyan $bin_version; + +$av_version = "$bin_version$branch$suffix"; + +$env:APPVEYOR_BUILD_VERSION=$av_version; +$env:BIN_VERSION=$bin_version; +$env:VERSION=$version; + +write-host -n "AppVeyor Build Version: "; +write-host -f green $av_version; + +appveyor UpdateBuild -Version $av_version \ No newline at end of file diff --git a/tools/appveyor-test.ps1 b/tools/appveyor-test.ps1 new file mode 100644 index 000000000..0005b5c3d --- /dev/null +++ b/tools/appveyor-test.ps1 @@ -0,0 +1,18 @@ +$proj = ".\test\ICSharpCode.SharpZipLib.TestBootstrapper\ICSharpCode.SharpZipLib.TestBootstrapper.csproj"; +$resxml = ".\docs\nunit3-test-results-debug.xml"; + +# Nuget 3 Console runner: +#$tester = "nunit3-console .\test\ICSharpCode.SharpZipLib.Tests\bin\$($env:CONFIGURATION)\netcoreapp2.0\ICSharpCode.SharpZipLib.Tests.dll" + +# Bootstrapper: +$tester = "dotnet run -f netcoreapp3.1 -p $proj -c $env:CONFIGURATION"; +iex "$tester --explore=tests.xml"; + +[xml]$xml = Get-Content("tests.xml"); +$assembly = select-xml "/test-suite[@type='Assembly']" $xml | select -f 1 -exp Node; +$testcases = select-xml "//test-case" $xml | % { Add-AppveyorTest -Name $_.Node.fullname -Framework NUnit -Filename $assembly.name }; + +iex "$tester --result=$resxml"; + +$wc = New-Object 'System.Net.WebClient'; +$wc.UploadFile("https://ci.appveyor.com/api/testresults/nunit3/$($env:APPVEYOR_JOB_ID)", (Resolve-Path $resxml)); \ No newline at end of file diff --git a/tools/ICSharpCode.SharpZipLib.nuspec b/tools/old/ICSharpCode.SharpZipLib.nuspec similarity index 100% rename from tools/ICSharpCode.SharpZipLib.nuspec rename to tools/old/ICSharpCode.SharpZipLib.nuspec diff --git a/tools/ICSharpCode.SharpZipLib.version b/tools/old/ICSharpCode.SharpZipLib.version similarity index 100% rename from tools/ICSharpCode.SharpZipLib.version rename to tools/old/ICSharpCode.SharpZipLib.version diff --git a/tools/install-fxmicroframework-43-44.ps1 b/tools/old/install-fxmicroframework-43-44.ps1 similarity index 100% rename from tools/install-fxmicroframework-43-44.ps1 rename to tools/old/install-fxmicroframework-43-44.ps1 diff --git a/tools/installGAC.bat b/tools/old/installGAC.bat similarity index 100% rename from tools/installGAC.bat rename to tools/old/installGAC.bat diff --git a/tools/run-appveyor-build.ps1 b/tools/old/run-appveyor-build.ps1 similarity index 100% rename from tools/run-appveyor-build.ps1 rename to tools/old/run-appveyor-build.ps1 diff --git a/tools/run-nunit3-tests-debug.cmd b/tools/old/run-nunit3-tests-debug.cmd similarity index 100% rename from tools/run-nunit3-tests-debug.cmd rename to tools/old/run-nunit3-tests-debug.cmd diff --git a/tools/run-nunit3-tests-release.cmd b/tools/old/run-nunit3-tests-release.cmd similarity index 100% rename from tools/run-nunit3-tests-release.cmd rename to tools/old/run-nunit3-tests-release.cmd diff --git a/tools/run-opencover.cmd b/tools/old/run-opencover.cmd similarity index 100% rename from tools/run-opencover.cmd rename to tools/old/run-opencover.cmd diff --git a/tools/run-reportgenerator.cmd b/tools/old/run-reportgenerator.cmd similarity index 100% rename from tools/run-reportgenerator.cmd rename to tools/old/run-reportgenerator.cmd diff --git a/tools/sharpziplib.pc b/tools/old/sharpziplib.pc similarity index 100% rename from tools/sharpziplib.pc rename to tools/old/sharpziplib.pc diff --git a/tools/uninstallGAC.bat b/tools/old/uninstallGAC.bat similarity index 100% rename from tools/uninstallGAC.bat rename to tools/old/uninstallGAC.bat diff --git a/tools/release-notes/README.md b/tools/release-notes/README.md new file mode 100644 index 000000000..90eb018a9 --- /dev/null +++ b/tools/release-notes/README.md @@ -0,0 +1,10 @@ +## Requirements +``` +npm install -g git-release-notes +``` + +## Usage +``` +git release-notes -f release-notes.json PREVIOUS..CURRENT release-notes-md.ejs +``` +Where PREVIOUS is the previous release tag, and CURRENT is the current release tag diff --git a/tools/release-notes/release-notes-md.ejs b/tools/release-notes/release-notes-md.ejs new file mode 100644 index 000000000..afde8c298 --- /dev/null +++ b/tools/release-notes/release-notes-md.ejs @@ -0,0 +1,31 @@ +<% +const typeGroups = { + feats: { title: 'Features:', types: ['feat'] }, + fixes: { title: 'Fixes:', types: ['fix'] }, + etc: { + title: 'Other changes (not related to library code):', + types: ['docs','style','refactor','perf','test','build','ci','chore'] + }, + unknown: { title: 'Unknown:', types: ['?'] }, +} + +const commitTypes = { + feat: '✨', fix: '🐛', docs: '📚', style: '💎', + refactor: '🔨', perf: '🚀', test: '🚨', build: '📦', + ci: '⚙️', chore: '🔧', ['?']: '❓', +} + +for(const group of Object.values(typeGroups)){ + const groupCommits = commits.filter(c => group.types.includes(c.type)); + if (groupCommits.length < 1) continue; +%> +## <%=group.title%> +<% for (const {issue, title, authorName, authorUser, scope, type} of groupCommits) { %> +* <%=commitTypes[type]%> +<%=issue ? ` [[#${issue}](https://github.com/icsharpcode/SharpZipLib/pull/${issue})]\n` : ''-%> +<%=scope ? ` \`${scope}\`\n` : ''-%> + __<%=title-%>__ + by <%=authorUser ? `[_${authorName}_](https://github.com/${authorUser})` : `_${authorName}_`%> +<% } %> + +<% } %> diff --git a/tools/release-notes/release-notes.js b/tools/release-notes/release-notes.js new file mode 100644 index 000000000..ce18ccac5 --- /dev/null +++ b/tools/release-notes/release-notes.js @@ -0,0 +1,76 @@ +const https = require('https') + +const authorUsers = {} + +/** + * @param {string} email + * @param {string} prId + * @returns {Promise} User login if found */ +const getAuthorUser = async (email, prId) => { + const lookupUser = authorUsers[email]; + if (lookupUser) return lookupUser; + + const match = /[0-9]+\+([^@]+)@users\.noreply\.github\.com/.exec(email); + if (match) { + return match[1]; + } + + const pr = await new Promise((resolve, reject) => { + console.warn(`Looking up GitHub user for PR #${prId} (${email})...`) + https.get(`https://api.github.com/repos/icsharpcode/sharpziplib/pulls/${prId}`, { + headers: {Accept: 'application/vnd.github.v3+json', 'User-Agent': 'release-notes-script/0.3.1'} + }, (res) => { + res.setEncoding('utf8'); + let chunks = ''; + res.on('data', (chunk) => chunks += chunk); + res.on('end', () => resolve(JSON.parse(chunks))); + res.on('error', reject); + }).on('error', reject); + }).catch(e => { + console.error(`Could not get GitHub user (${email}): ${e}}`) + return null; + }); + + if (!pr) { + console.error(`Could not get GitHub user (${email})}`) + return null; + } else { + const user = pr.user.login; + console.warn(`Resolved email ${email} to user ${user}`) + authorUsers[email] = user; + return user; + } +} + +/** + * @typedef {{issue?: string, sha1: string, authorEmail: string, title: string, type: string}} Commit + * @param {{commits: Commit[], range: string, dateFnsFormat: ()=>any, debug: (...p[]) => void}} data + * @param {(data: {commits: Commit[], extra: {[key: string]: any}}) => void} callback + * */ +module.exports = (data, callback) => { + // Migrates commits in the old format to conventional commit style, omitting any commits in neither format + const normalizedCommits = data.commits.flatMap(c => { + if (c.type) return [c] + const match = /^(?:Merge )?(?:PR ?)?#(\d+):? (.*)/.exec(c.title) + if (match != null) { + const [, issue, title] = match + return [{...c, title, issue, type: '?'}] + } else { + console.warn(`Skipping commit [${c.sha1.substr(0, 7)}] "${c.title}"!`); + return []; + } + }); + + const commitAuthoredBy = email => commit => commit.authorEmail === email && commit.issue ? [commit.issue] : [] + const authorEmails = new Set(normalizedCommits.map(c => c.authorEmail)); + Promise.all( + Array + .from(authorEmails.values(), e => [e, normalizedCommits.flatMap(commitAuthoredBy(e))]) + .map(async ([email, prs]) => [email, await getAuthorUser(email, ...prs)]) + ) + .then(Object.fromEntries) + .then(authorUsers => callback({ + commits: normalizedCommits.map(c => ({...c, authorUser: authorUsers[c.authorEmail]})), + extra: {} + })) +}; diff --git a/tools/release-notes/release-notes.json b/tools/release-notes/release-notes.json new file mode 100644 index 000000000..7ad7733d1 --- /dev/null +++ b/tools/release-notes/release-notes.json @@ -0,0 +1,5 @@ +{ + "title" : "^([a-z]+)(?:\\(([\\w\\$\\.]*)\\))?\\: (.*?)(?: \\(#(\\d+)\\))?$", + "meaning": ["type", "scope", "title", "issue"], + "script": "release-notes.js" +} diff --git a/tools/test-scripts.ps1 b/tools/test-scripts.ps1 new file mode 100644 index 000000000..5c6f08c60 --- /dev/null +++ b/tools/test-scripts.ps1 @@ -0,0 +1,24 @@ +$env:APPVEYOR_BUILD_NUMBER = 123; + +function Test-Install { + param( [string]$PR, [string]$Branch ) + Write-Host -n "-- Testing with PR " + if($PR -eq "") { Write-Host -n -f red "No" } else { Write-Host -n -f cyan "#$PR" } + Write-Host -n " and branch " + if($Branch -eq "master") { Write-Host -f green "master" } else { Write-Host -f cyan "$Branch" } + $env:APPVEYOR_PULL_REQUEST_NUMBER = $PR; + $env:APPVEYOR_REPO_BRANCH = $Branch; + .\appveyor-install.ps1 + Write-Host "---------------------------------------------------------------------"; + Write-Host; +} + +function appveyor { + # Dummy function for compability with AV +} + +Write-Host; +Test-Install "" "master" +Test-Install 42 "master" +Test-Install "" "misc-feature" +Test-Install 36 "other-branch" \ No newline at end of file