on master help sign in
286 files changed+21186−12206
since splitsnapshots
unifiedsplit
.azure/azure-linux-template.yml+93 .azure/azure-osx-template.yml+30 .azure/azure-pipelines.yml+30 .azure/azure-windows-template.yml+23 .github/ISSUE_TEMPLATE.md+28 .github/PULL_REQUEST_TEMPLATE.md+14 .gitignore+11−1 CONTRIBUTING.md+94 LICENSE+17−22 Network/Wai/Handler/CGI.hs−140 Network/Wai/Handler/Helper.hs−25 Network/Wai/Handler/SimpleServer.hs−181 Network/Wai/Middleware/CleanPath.hs−76 Network/Wai/Middleware/Gzip.hs−63 Network/Wai/Middleware/Jsonp.hs−85 Network/Wai/Parse.hs−397 Network/Wai/Zlib.hs−106 README−1 README.md+13 auto-update/ChangeLog.md+34 auto-update/Control/AutoUpdate.hs+201 auto-update/Control/AutoUpdate/Util.hs+23 auto-update/Control/Debounce.hs+60 auto-update/Control/Debounce/Internal.hs+98 auto-update/Control/Reaper.hs+280 auto-update/LICENSE+20 auto-update/README.md+13 auto-update/Setup.hs+2 auto-update/auto-update.cabal+38 auto-update/test/Control/AutoUpdateSpec.hs+35 auto-update/test/Control/DebounceSpec.hs+120 auto-update/test/Control/ReaperSpec.hs+39 auto-update/test/Spec.hs+1 c/helper.c−56 cbits/adler32.c−149 cbits/compress.c−79 cbits/crc32.c−423 cbits/crc32.h−441 cbits/deflate.c−1736 cbits/deflate.h−331 cbits/gzio.c−1026 cbits/infback.c−623 cbits/inffast.c−318 cbits/inffast.h−11 cbits/inffixed.h−94 cbits/inflate.c−1368 cbits/inflate.h−115 cbits/inftrees.c−329 cbits/inftrees.h−55 cbits/trees.c−1219 cbits/trees.h−128 cbits/uncompr.c−61 cbits/zconf.h−332 cbits/zlib.h−1357 cbits/zutil.c−318 cbits/zutil.h−269 mime-types/ChangeLog.md+15 mime-types/LICENSE+20 mime-types/Network/Mime.hs+1096 mime-types/README.md+3 Setup.lhs → mime-types/Setup.lhs mime-types/mime-types.cabal+24 runtests.hs−196 stack-lts-11.yaml+27 stack-lts-12.yaml+26 stack-nightly.yaml+20 stack.yaml+25 test/sample.hs−25 time-manager/LICENSE+20 time-manager/System/TimeManager.hs+152 time-manager/time-manager.cabal+19 wai-app-static/.ghci+1 wai-app-static/.gitignore+2 wai-app-static/ChangeLog.md+76 wai-app-static/LICENSE+20 wai-app-static/Network/Wai/Application/Static.hs+289 wai-app-static/README wai-app-static/README.md+5 wai-app-static/Setup.lhs+7 wai-app-static/Util.hs+45 wai-app-static/WaiAppStatic/CmdLine.hs+118 wai-app-static/WaiAppStatic/Listing.hs+150 wai-app-static/WaiAppStatic/Storage/Embedded.hs+12 wai-app-static/WaiAppStatic/Storage/Embedded/Runtime.hs+97 wai-app-static/WaiAppStatic/Storage/Embedded/TH.hs+277 wai-app-static/WaiAppStatic/Storage/Filesystem.hs+170 wai-app-static/WaiAppStatic/Types.hs+153 wai-app-static/app/warp-static.hs+7 wai-app-static/embedded-sample.hs+13 wai-app-static/folder.svg+424 wai-app-static/images/folder.png wai-app-static/images/haskell.png wai-app-static/sample.hs+17 wai-app-static/test/EmbeddedTestEntries.hs+55 wai-app-static/test/WaiAppEmbeddedTest.hs+94 wai-app-static/test/WaiAppStaticTest.hs+184 wai-app-static/test/a/b wai-app-static/tests.hs+6 wai-app-static/wai-app-static.cabal+109 wai-app-static/קרררר.html+1 wai-conduit/ChangeLog.md+7 wai-conduit/LICENSE+20 wai-conduit/Network/Wai/Conduit.hs+66 wai-conduit/README.md+4 wai-conduit/Setup.hs+2 wai-conduit/example/Main.hs+19 wai-conduit/wai-conduit.cabal+23 wai-extra.cabal−50 wai-extra/.ghci+1 wai-extra/.gitignore+2 wai-extra/ChangeLog.md+209 wai-extra/LICENSE+20 wai-extra/Network/Wai/EventSource.hs+59 wai-extra/Network/Wai/EventSource/EventStream.hs+76 wai-extra/Network/Wai/Handler/CGI.hs+201 wai-extra/Network/Wai/Handler/SCGI.hs+107 wai-extra/Network/Wai/Header.hs+18 wai-extra/Network/Wai/Middleware/AcceptOverride.hs+24 wai-extra/Network/Wai/Middleware/AddHeaders.hs+24 wai-extra/Network/Wai/Middleware/Approot.hs+121 wai-extra/Network/Wai/Middleware/Autohead.hs+17 wai-extra/Network/Wai/Middleware/CleanPath.hs+32 wai-extra/Network/Wai/Middleware/ForceDomain.hs+38 wai-extra/Network/Wai/Middleware/ForceSSL.hs+37 wai-extra/Network/Wai/Middleware/Gzip.hs+230 wai-extra/Network/Wai/Middleware/HttpAuth.hs+132 wai-extra/Network/Wai/Middleware/Jsonp.hs+94 wai-extra/Network/Wai/Middleware/Local.hs+26 wai-extra/Network/Wai/Middleware/MethodOverride.hs+20 wai-extra/Network/Wai/Middleware/MethodOverridePost.hs+44 wai-extra/Network/Wai/Middleware/RequestLogger.hs+371 wai-extra/Network/Wai/Middleware/RequestLogger/Internal.hs+33 wai-extra/Network/Wai/Middleware/RequestLogger/JSON.hs+137 wai-extra/Network/Wai/Middleware/Rewrite.hs+339 wai-extra/Network/Wai/Middleware/Routed.hs+39 wai-extra/Network/Wai/Middleware/StreamFile.hs+43 wai-extra/Network/Wai/Middleware/StripHeaders.hs+45 wai-extra/Network/Wai/Middleware/Timeout.hs+33 wai-extra/Network/Wai/Middleware/Vhost.hs+37 wai-extra/Network/Wai/Parse.hs+715 wai-extra/Network/Wai/Request.hs+101 wai-extra/Network/Wai/Test.hs+333 wai-extra/Network/Wai/Test/Internal.hs+35 wai-extra/Network/Wai/UrlMap.hs+104 wai-extra/README.md+16 wai-extra/Setup.lhs+7 wai-extra/example/Main.hs+62 wai-extra/example/index.html+20 wai-extra/proxy.hs+25 wai-extra/test/Network/Wai/Middleware/ApprootSpec.hs+37 wai-extra/test/Network/Wai/Middleware/ForceSSLSpec.hs+60 wai-extra/test/Network/Wai/Middleware/RoutedSpec.hs+53 wai-extra/test/Network/Wai/Middleware/StripHeadersSpec.hs+58 wai-extra/test/Network/Wai/Middleware/TimeoutSpec.hs+57 wai-extra/test/Network/Wai/ParseSpec.hs+294 wai-extra/test/Network/Wai/RequestSpec.hs+93 wai-extra/test/Network/Wai/TestSpec.hs+227 wai-extra/test/Spec.hs+1 wai-extra/test/WaiExtraSpec.hs+451 test/json → wai-extra/test/json wai-extra/test/requests/dalvik-request wai-extra/test/sample.hs+26 test/test.html → wai-extra/test/test.html wai-extra/wai-extra.cabal+212 wai-frontend-monadcgi/.gitignore+2 wai-frontend-monadcgi/ChangeLog.md+7 wai-frontend-monadcgi/LICENSE+20 wai-frontend-monadcgi/Network/Wai/Frontend/MonadCGI.hs+71 wai-frontend-monadcgi/README.md+5 wai-frontend-monadcgi/Setup.lhs+7 wai-frontend-monadcgi/samples/.htaccess+5 wai-frontend-monadcgi/samples/plain_cgi.hs+5 wai-frontend-monadcgi/samples/wai_cgi.hs+8 wai-frontend-monadcgi/samples/wai_cgi_generic.hs+19 wai-frontend-monadcgi/samples/wai_fastcgi.hs+8 wai-frontend-monadcgi/wai-frontend-monadcgi.cabal+29 wai-http2-extra/LICENSE+20 wai-http2-extra/Network/Wai/Middleware/Push/Referer.hs+236 wai-http2-extra/Network/Wai/Middleware/Push/Referer/LimitMultiMap.hs+40 wai-http2-extra/test/doctests.hs+4 wai-http2-extra/wai-http2-extra.cabal+44 wai-websockets/ChangeLog.md+27 wai-websockets/LICENSE+20 wai-websockets/Network/Wai/Handler/WebSockets.hs+118 wai-websockets/README.md+3 wai-websockets/Setup.lhs+7 wai-websockets/server.lhs+168 wai-websockets/static/client.html+41 wai-websockets/static/client.js+80 wai-websockets/static/screen.css+86 wai-websockets/wai-websockets.cabal+57 wai/.ghci+2 wai/ChangeLog.md+56 wai/LICENSE+20 wai/Network/Wai.hs+348 wai/Network/Wai/Internal.hs+161 wai/README.lhs+1 wai/README.md+116 wai/Setup.lhs+7 wai/test/Network/WaiSpec.hs+82 wai/test/Spec.hs+1 wai/wai.cabal+48 wai/webkit-sample/index.html+17 wai/webkit-sample/webkit-sample.hs+30 warp-tls/ChangeLog.md+117 warp-tls/LICENSE+20 warp-tls/Network/Wai/Handler/WarpTLS.hs+531 warp-tls/README.md+9 warp-tls/Setup.lhs+7 warp-tls/certificate.pem+15 warp-tls/graceful-pong.hs+125 warp-tls/key.pem+15 warp-tls/pong.hs+80 warp-tls/pong.txt+1 warp-tls/warp-tls.cabal+42 warp/.ghci+1 warp/ChangeLog.md+404 warp/LICENSE+20 warp/Network/Wai/Handler/Warp.hs+509 warp/Network/Wai/Handler/Warp/Buffer.hs+109 warp/Network/Wai/Handler/Warp/Conduit.hs+169 warp/Network/Wai/Handler/Warp/Counter.hs+30 warp/Network/Wai/Handler/Warp/Date.hs+41 warp/Network/Wai/Handler/Warp/FdCache.hs+142 warp/Network/Wai/Handler/Warp/File.hs+152 warp/Network/Wai/Handler/Warp/FileInfoCache.hs+108 warp/Network/Wai/Handler/Warp/HTTP2.hs+85 warp/Network/Wai/Handler/Warp/HTTP2/File.hs+30 warp/Network/Wai/Handler/Warp/HTTP2/PushPromise.hs+36 warp/Network/Wai/Handler/Warp/HTTP2/Request.hs+126 warp/Network/Wai/Handler/Warp/HTTP2/Response.hs+127 warp/Network/Wai/Handler/Warp/HTTP2/Types.hs+81 warp/Network/Wai/Handler/Warp/HashMap.hs+42 warp/Network/Wai/Handler/Warp/Header.hs+101 warp/Network/Wai/Handler/Warp/IO.hs+30 warp/Network/Wai/Handler/Warp/Imports.hs+27 warp/Network/Wai/Handler/Warp/Internal.hs+86 warp/Network/Wai/Handler/Warp/MultiMap.hs+111 warp/Network/Wai/Handler/Warp/PackInt.hs+56 warp/Network/Wai/Handler/Warp/ReadInt.hs+67 warp/Network/Wai/Handler/Warp/Recv.hs+160 warp/Network/Wai/Handler/Warp/Request.hs+303 warp/Network/Wai/Handler/Warp/RequestHeader.hs+133 warp/Network/Wai/Handler/Warp/Response.hs+427 warp/Network/Wai/Handler/Warp/ResponseHeader.hs+84 warp/Network/Wai/Handler/Warp/Run.hs+599 warp/Network/Wai/Handler/Warp/SendFile.hs+149 warp/Network/Wai/Handler/Warp/Settings.hs+231 warp/Network/Wai/Handler/Warp/Types.hs+188 warp/Network/Wai/Handler/Warp/Windows.hs+27 warp/Network/Wai/Handler/Warp/WithApplication.hs+104 warp/README.md+3 warp/Setup.lhs+7 warp/attic/bigtable-single.hs+22 warp/attic/bigtable-stream.hs+24 warp/attic/file-nolen.hs+6 warp/attic/file.hs+6 warp/attic/hex+1 warp/attic/pong.hs+72 warp/attic/pong.txt+1 warp/attic/print-post.hs+18 warp/attic/readInt.hs+162 warp/attic/runtests.hs+43 warp/attic/server-no-keepalive.hs+34 warp/attic/statuses.hs+18 warp/attic/test.txt+1 warp/attic/undrained.hs+9 warp/bench/Parser.hs+220 warp/test/BufferPoolSpec.hs+47 warp/test/ConduitSpec.hs+67 warp/test/ExceptionSpec.hs+66 warp/test/FdCacheSpec.hs+27 warp/test/FileSpec.hs+45 warp/test/HTTP.hs+41 warp/test/ReadIntSpec.hs+26 warp/test/RequestSpec.hs+149 warp/test/ResponseHeaderSpec.hs+48 warp/test/ResponseSpec.hs+107 warp/test/RunSpec.hs+463 warp/test/SendFileSpec.hs+116 warp/test/Spec.hs+1 warp/test/WithApplicationSpec.hs+45 warp/test/doctests.hs+4 warp/test/head-response+1 warp/test/inputFile+100 warp/warp.cabal+270
.azure/azure-linux-template.ymlnew file+93
@@ -0,0 +1,93 @@
1+jobs:
2+- job: ${{ parameters.name }}
3+ timeoutInMinutes: 180
4+ pool:
5+ vmImage: ${{ parameters.vmImage }}
6+ strategy:
7+ matrix:
8+ stack-lts-11:
9+ BUILD: stack
10+ STACK_YAML: stack-lts-11.yaml
11+ ARGS: "--resolver lts-11"
12+ stack-lts-12:
13+ BUILD: stack
14+ STACK_YAML: stack-lts-12.yaml
15+ ARGS: "--resolver lts-12"
16+ stack-lts-14:
17+ BUILD: stack
18+ ARGS: "--resolver lts-14"
19+ stack-nightly:
20+ BUILD: stack
21+ STACK_YAML: stack-nightly.yaml
22+ ARGS: "--resolver nightly"
23+ cabal-8.2.2:
24+ BUILD: cabal
25+ GHCVER: 8.4.4
26+ CABALVER: 2.0
27+ cabal-8.4.4:
28+ BUILD: cabal
29+ GHCVER: 8.4.4
30+ CABALVER: 2.2
31+ cabal-8.6.5:
32+ BUILD: cabal
33+ GHCVER: 8.6.5
34+ CABALVER: 2.4
35+ maxParallel: 6
36+ steps:
37+ - script: |
38+ export STACK_ROOT="$(Build.SourcesDirectory)"/.stack-root;
39+ mkdir -p ~/.local/bin
40+ curl -L https://get.haskellstack.org/stable/linux-x86_64.tar.gz | tar xz --wildcards --strip-components=1 -C ~/.local/bin '*/stack'
41+ case "$BUILD" in
42+ cabal)
43+ sudo add-apt-repository -y ppa:hvr/ghc
44+ sudo apt-get update
45+ sudo apt-get install cabal-install-$CABALVER ghc-$GHCVER
46+ # See note here: https://github.com/haskell-CI/haskell-ci#alex--happy-with-ghc--78
47+ if [ "$GHCVER" = "head" ] || [ "${GHCVER%.*}" = "7.8" ] || [ "${GHCVER%.*}" = "7.10" ]; then
48+ sudo apt-get install happy-1.19.4 alex-3.1.3
49+ export PATH=/opt/alex/3.1.3/bin:/opt/happy/1.19.4/bin:$PATH
50+ else
51+ sudo apt-get install happy alex
52+ fi
53+ export PATH=$HOME/.local/bin:/opt/ghc/$GHCVER/bin:/opt/cabal/$CABALVER/bin:$PATH
54+ cabal --version
55+ cabal update
56+ PACKAGES=$(stack --install-ghc query locals | grep '^ *path' | sed 's@^ *path:@@')
57+ cabal install --only-dependencies --enable-tests --enable-benchmarks --force-reinstalls --ghc-options=-O0 --reorder-goals --max-backjumps=-1 $CABALARGS $PACKAGES
58+ ;;
59+ *)
60+ export PATH=$HOME/.local/bin:$PATH
61+ stack --install-ghc $ARGS test --bench --only-dependencies
62+ ;;
63+ esac
64+ set -ex
65+ case "$BUILD" in
66+ cabal)
67+ cabal install --enable-tests --enable-benchmarks --force-reinstalls --ghc-options=-O0 --reorder-goals --max-backjumps=-1 $CABALARGS $PACKAGES
68+
69+ ORIGDIR=$(pwd)
70+ for dir in $PACKAGES
71+ do
72+ cd $dir
73+ cabal check || [ "$CABALVER" == "1.22" ] # https://github.com/yesodweb/wai/pull/754#issuecomment-504866279
74+ cabal sdist
75+ PKGVER=$(cabal info . | awk '{print $2;exit}')
76+ SRC_TGZ=$PKGVER.tar.gz
77+ cd dist
78+ tar zxfv "$SRC_TGZ"
79+ cd "$PKGVER"
80+ cabal configure --enable-tests --ghc-options -O0
81+ cabal build
82+ cabal test --show-details=streaming
83+ cd $ORIGDIR
84+ done
85+ ;;
86+ *)
87+ stack $ARGS test --bench --no-run-benchmarks --haddock --no-haddock-deps
88+ ;;
89+ esac
90+ set +ex
91+ env:
92+ OS_NAME: ${{ parameters.os }}
93+ displayName: 'Installation ${{parameters.os}} & Test'
.azure/azure-osx-template.ymlnew file+30
@@ -0,0 +1,30 @@
1+jobs:
2+- job: ${{ parameters.name }}
3+ timeoutInMinutes: 180
4+ pool:
5+ vmImage: ${{ parameters.vmImage }}
6+ strategy:
7+ matrix:
8+ stack-lts-11:
9+ BUILD: stack
10+ STACK_YAML: stack-lts-11.yaml
11+ ARGS: "--resolver lts-11"
12+ stack-lts-12:
13+ BUILD: stack
14+ STACK_YAML: stack-lts-12.yaml
15+ ARGS: "--resolver lts-12"
16+ stack-lts-14:
17+ BUILD: stack
18+ ARGS: "--resolver lts-14"
19+ maxParallel: 3
20+ steps:
21+ - script: |
22+ export STACK_ROOT="$(Build.SourcesDirectory)"/.stack-root;
23+ mkdir -p ~/.local/bin
24+ curl -skL https://get.haskellstack.org/stable/osx-x86_64.tar.gz | tar xz --strip-components=1 --include '*/stack' -C ~/.local/bin;
25+ export PATH=$HOME/.local/bin:$PATH
26+ stack --install-ghc $ARGS test --bench --only-dependencies
27+ stack $ARGS test --bench --no-run-benchmarks --haddock --no-haddock-deps
28+ env:
29+ OS_NAME: ${{ parameters.os }}
30+ displayName: 'Installation ${{parameters.os}} & Test'
.azure/azure-pipelines.ymlnew file+30
@@ -0,0 +1,30 @@
1+# This is the complex Azure configuration, which is intended for use
2+# on open source libraries which need compatibility across multiple GHC
3+# versions, must work with cabal-install, and should be
4+# cross-platform. For more information and other options, see:
5+#
6+# https://docs.haskellstack.org/en/stable/azure_ci/
7+#
8+# Copy these contents into the root directory of your Github project in a file
9+# named azure-pipelines.yml
10+#
11+# For better organization, you split various jobs into seprate parts
12+# and each of them are controlled via individual file.
13+jobs:
14+- template: azure-linux-template.yml
15+ parameters:
16+ name: Linux
17+ vmImage: ubuntu-16.04
18+ os: linux
19+
20+- template: azure-osx-template.yml
21+ parameters:
22+ name: macOS
23+ vmImage: macOS-10.13
24+ os: osx
25+
26+- template: azure-windows-template.yml
27+ parameters:
28+ name: Windows
29+ vmImage: vs2017-win2016
30+ os: windows
.azure/azure-windows-template.ymlnew file+23
@@ -0,0 +1,23 @@
1+jobs:
2+- job: ${{ parameters.name }}
3+ timeoutInMinutes: 180
4+ pool:
5+ vmImage: ${{ parameters.vmImage }}
6+ strategy:
7+ matrix:
8+ stack-lts-14:
9+ BUILD: stack
10+ ARGS: "--resolver lts-14"
11+ maxParallel: 3
12+ steps:
13+ - bash: |
14+ export STACK_ROOT="$(Build.SourcesDirectory)"/.stack-root;
15+ curl -sSkL http://www.stackage.org/stack/windows-x86_64 -o /usr/bin/stack.zip
16+ unzip -o /usr/bin/stack.zip -d /usr/bin/
17+ stack setup $ARGS > /dev/null
18+ stack --install-ghc $ARGS test --bench --only-dependencies
19+ stack $ARGS test --bench --no-run-benchmarks --no-run-tests
20+ stack $ARGS test --bench --no-run-benchmarks --jobs 1 # non-parallel test execution
21+ env:
22+ OS_NAME: ${{ parameters.os }}
23+ displayName: 'Installation ${{parameters.os}} & Test'
.github/ISSUE_TEMPLATE.mdnew file+28
@@ -0,0 +1,28 @@
1+<!---
2+
3+### Bug Reports
4+
5+Reporting a bug? If relevant, we recommend including:
6+
7+* Your OS name and version
8+* The versions of tools you're using (e.g. `stack`, `ghc`).
9+* The versions of dependencies you're using
10+
11+For your convenience, we recommend pasting this script into bash and uploading the output [as a gist](https://gist.github.com/).
12+
13+```
14+command -v sw_vers && sw_vers # OS X only
15+command -v uname && uname -a # Kernel version
16+command -v stack && stack --version
17+command -v stack && stack ghc -- --version
18+command -v stack && stack list-dependencies
19+```
20+
21+* Also, is there anything custom or unusual about your setup? i.e. new or prerelease versions of GHC, Stack, etc.
22+
23+* Finally, if possible, please reproduce the error in a small script, or if necessary create a new Github repo with the smallest possible reproducing case. [Stack's scripting support](https://docs.haskellstack.org/en/stable/GUIDE/#script-interpreter) might be useful for creating your reproduction example.
24+
25+### Support
26+
27+Please direct support questions to [Stack Overflow](https://stackoverflow.com/questions/tagged/yesod+haskell) or the [Yesod Google Group](https://groups.google.com/forum/#!forum/yesodweb). If you don't get a response there, or you suspect there may be a bug in Wai causing your problem, you're welcome to ask here.
28+-->
.github/PULL_REQUEST_TEMPLATE.mdnew file+14
@@ -0,0 +1,14 @@
1+Before submitting your PR, check that you've:
2+
3+- [ ] Bumped the version number
4+- [ ] Documented new APIs with [Haddock markup](https://www.haskell.org/haddock/doc/html/index.html)
5+- [ ] Added [`@since` declarations](http://haskell-haddock.readthedocs.io/en/latest/markup.html#since) to the Haddock
6+
7+After submitting your PR:
8+
9+- [ ] Update the Changelog.md file with a link to your PR
10+- [ ] Check that CI passes (or if it fails, for reasons unrelated to your change, like CI timeouts)
11+
12+<!---Thanks so much for contributing! :)
13+
14+_If these checkboxes don't apply to your PR, you can delete them_-->
.gitignore+11−1
@@ -1,2 +1,12 @@
1+dist/
12*.swp
2−dist
3+*.hi
4+*.o
5+tarballs/
6+/.hsenv
7+/.shelly
8+.cabal-sandbox/
9+cabal.sandbox.config
10+.stack-work/
11+*~
12+stack*.yaml.lock
CONTRIBUTING.mdnew file+94
@@ -0,0 +1,94 @@
1+# Contributing
2+
3+Thanks for your interest in contributing to Wai! This file has some tips for developing Wai and getting a pull request accepted.
4+
5+## Development
6+
7+Wai is a mega-repo that contains many Haskell packages, each in a different directory. All the subprojects can be developed with Stack, using `stack <command> <subproject>`, e.g.
8+
9+* `stack build mime-types`
10+* `stack test auto-update`
11+* `stack haddock warp`
12+
13+If you'd like to test your changes in a full-fledged Wai app, you can use Stack to build against it, e.g.:
14+
15+```
16+packages:
17+- '/path/to/this/repo/wai'
18+```
19+
20+## Coding Guidelines
21+
22+### Safety
23+
24+Avoid partial functions. Even if you know the partial function is safe in your instance, partial functions require more reasoning from the programmer and are not resilient to refactoring. For the rare cases where a partial function is appropriate, a custom `error` should be used.
25+
26+### Style
27+
28+Keep coding style consistent with the rest of the file, but don't worry about style too much otherwise. PRs changing code style are viewed skeptically.
29+
30+### Dependencies
31+
32+Avoid adding unnecessary dependencies. If a dependency provides only a minor convenience for your implementation, it's probably better to skip it.
33+
34+If you do add a new dependency, try to support a wide range of versions of it.
35+
36+### Backwards Compatibility
37+
38+Backwards incompatible changes are viewed skeptically—best to ask in an issue to see if a particular backwards incompatible change would be approved. If possible keep backwards compatibility by adding new APIs and deprecating old ones.
39+
40+Keep backwards compatibility with old versions of dependencies when possible.
41+
42+## PR Guidelines
43+
44+### PR Scope
45+
46+As much as possible, keep separate changes in separate PRs.
47+
48+### Testing
49+
50+Tests are recommended, but not required.
51+
52+### Documentation
53+
54+All public APIs must be documented. Documenting private functions is optional, but may be nice depending on their complexity. Example documentation:
55+
56+```
57+-- | Generate an action which will either read from an automatically
58+-- updated value, or run the update action in the current thread.
59+--
60+-- ==== __Examples__
61+--
62+-- > getTime <- mkAutoUpdate defaultUpdateSettings
63+--
64+-- @since 0.1.0
65+mkAutoUpdate :: UpdateSettings a -> IO (IO a)
66+```
67+
68+Examples are recommended, but not required, in documentation. Marking new APIs with `@since <version number>` is required.
69+
70+### Versioning
71+
72+Wai packages roughly follow the Haskell Package Versioning Policy style of A.B.C.[D] (MAJOR.MAJOR.MINOR.[PATCH])
73+
74+* A - Used for massive changes in the library. (Example: 1.2.3.4 becomes 2.0.0)
75+* B - Used for smaller breaking changes, like removing, renaming, or changing behavior of existing public API. (Example: 1.2.3.4 becomes 1.3.0)
76+* C - Used for new public APIs (Example: 1.2.3.4 becomes 1.2.4)
77+* D - Used for bug fixes (Example: 1.2.3.4 becomes 1.2.3.5).
78+ * D is optional in the version number, so 2.0.0 is a valid version.
79+
80+Documentation changes don't require a new version.
81+
82+If you feel there is ambiguity to a change (e.g. fixing a bug in a function, when people may be relying on the old broken behavior), you can ask in an issue or pull request.
83+
84+Unlike in the Package Versioning Policy, deprecations are not counted as MAJOR changes.
85+
86+In some cases, dropping compatibility with a major version of a dependency (e.g. changing from transformers >= 0.3 to transformers >= 0.4), is considered a breaking change.
87+
88+### Changelog
89+
90+After you submit a PR, update the subproject's Changelog.md file with the new version number and a link to your PR. If your PR does not need to bump the version number, include the change in an "Unreleased" section at the top.
91+
92+### Releases
93+
94+Releases should be done as soon as possible after a pull request is merged—don't be shy about reminding us to make a release if we forget.
LICENSE+17−22
@@ -1,25 +1,20 @@
1−The following license covers this documentation, and the source code, except
2−where otherwise indicated.
1+Copyright (c) 2012-2017 Michael Snoyman, http://www.yesodweb.com/
32
4−Copyright 2010, Michael Snoyman. All rights reserved.
3+Permission is hereby granted, free of charge, to any person obtaining
4+a copy of this software and associated documentation files (the
5+"Software"), to deal in the Software without restriction, including
6+without limitation the rights to use, copy, modify, merge, publish,
7+distribute, sublicense, and/or sell copies of the Software, and to
8+permit persons to whom the Software is furnished to do so, subject to
9+the following conditions:
510
6−Redistribution and use in source and binary forms, with or without
7−modification, are permitted provided that the following conditions are met:
11+The above copyright notice and this permission notice shall be
12+included in all copies or substantial portions of the Software.
813
9−* Redistributions of source code must retain the above copyright notice, this
10− list of conditions and the following disclaimer.
11−
12−* Redistributions in binary form must reproduce the above copyright notice,
13− this list of conditions and the following disclaimer in the documentation
14− and/or other materials provided with the distribution.
15−
16−THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS "AS IS" AND ANY EXPRESS OR
17−IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18−MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19−EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY DIRECT, INDIRECT,
20−INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21−NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
22−OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23−LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
24−OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25−ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Network/Wai/Handler/CGI.hsdeleted−140
@@ -1,140 +0,0 @@
1−{-# LANGUAGE OverloadedStrings #-}
2−module Network.Wai.Handler.CGI
3− ( run
4− , run'
5− , run''
6− , runSendfile
7− ) where
8−
9−import Network.Wai
10−import Network.Wai.Enumerator (fromResponseBody)
11−import Network.Wai.Handler.Helper
12−import System.Environment (getEnvironment)
13−import Data.Maybe (fromMaybe)
14−import qualified Data.ByteString.Char8 as B
15−import Control.Arrow ((***))
16−import Data.Char (toLower)
17−import qualified System.IO
18−import Data.String (fromString)
19−
20−safeRead :: Read a => a -> String -> a
21−safeRead d s =
22− case reads s of
23− ((x, _):_) -> x
24− [] -> d
25−
26−lookup' :: String -> [(String, String)] -> String
27−lookup' key pairs = fromMaybe "" $ lookup key pairs
28−
29−run :: Application -> IO ()
30−run app = do
31− vars <- getEnvironment
32− let input = requestBodyHandle System.IO.stdin
33− output = B.hPut System.IO.stdout
34− run'' vars input output Nothing app
35−
36−runSendfile :: String -- ^ sendfile header
37− -> Application -> IO ()
38−runSendfile sf app = do
39− vars <- getEnvironment
40− let input = requestBodyHandle System.IO.stdin
41− output = B.hPut System.IO.stdout
42− run'' vars input output (Just sf) app
43−
44−run' :: [(String, String)] -- ^ all variables
45− -> System.IO.Handle -- ^ responseBody of input
46− -> System.IO.Handle -- ^ destination for output
47− -> Application
48− -> IO ()
49−run' vars inputH outputH app = do
50− let input = requestBodyHandle inputH
51− output = B.hPut outputH
52− run'' vars input output Nothing app
53−
54−run'' :: [(String, String)] -- ^ all variables
55− -> (Int -> Source) -- ^ responseBody of input
56− -> (B.ByteString -> IO ()) -- ^ destination for output
57− -> Maybe String -- ^ does the server support the X-Sendfile header?
58− -> Application
59− -> IO ()
60−run'' vars inputH outputH xsendfile app = do
61− let rmethod = B.pack $ lookup' "REQUEST_METHOD" vars
62− pinfo = lookup' "PATH_INFO" vars
63− qstring = lookup' "QUERY_STRING" vars
64− servername = lookup' "SERVER_NAME" vars
65− serverport = safeRead 80 $ lookup' "SERVER_PORT" vars
66− contentLength = safeRead 0 $ lookup' "CONTENT_LENGTH" vars
67− remoteHost' =
68− case lookup "REMOTE_HOST" vars of
69− Just x -> x
70− Nothing ->
71− case lookup "REMOTE_ADDR" vars of
72− Just x -> x
73− Nothing -> ""
74− isSecure' =
75− case map toLower $ lookup' "SERVER_PROTOCOL" vars of
76− "https" -> True
77− _ -> False
78− let env = Request
79− { requestMethod = rmethod
80− , pathInfo = B.pack pinfo
81− , queryString = B.pack qstring
82− , serverName = B.pack servername
83− , serverPort = serverport
84− , requestHeaders = map (cleanupVarName *** B.pack) vars
85− , isSecure = isSecure'
86− , requestBody = inputH contentLength
87− , errorHandler = System.IO.hPutStr System.IO.stderr
88− , remoteHost = B.pack remoteHost'
89− , httpVersion = "1.1" -- FIXME
90− }
91− res <- app env
92− let h = responseHeaders res
93− let h' = case lookup "Content-Type" h of
94− Nothing -> ("Content-Type", "text/html; charset=utf-8")
95− : h
96− Just _ -> h
97− let hPut = outputH
98− hPut $ B.pack $ "Status: " ++ (show $ statusCode $ status res) ++ " "
99− hPut $ statusMessage $ status res
100− hPut $ B.singleton '\n'
101− mapM_ (printHeader hPut) h'
102− case (xsendfile, responseBody res) of
103− (Just sf, ResponseFile fp) ->
104− hPut $ B.pack $ concat
105− [ sf
106− , ": "
107− , fp
108− , "\n\n"
109− , sf
110− , " not supported"
111− ]
112− _ -> do
113− hPut $ B.singleton '\n'
114− _ <- runEnumerator (fromResponseBody (responseBody res))
115− (myPut outputH) ()
116− return ()
117−
118−myPut :: (B.ByteString -> IO ()) -> () -> B.ByteString -> IO (Either () ())
119−myPut output _ bs = output bs >> return (Right ())
120−
121−printHeader :: (B.ByteString -> IO ())
122− -> (ResponseHeader, B.ByteString)
123− -> IO ()
124−printHeader f (x, y) = do
125− f $ ciOriginal x
126− f $ B.pack ": "
127− f y
128− f $ B.singleton '\n'
129−
130−cleanupVarName :: String -> RequestHeader
131−cleanupVarName ('H':'T':'T':'P':'_':a:as) =
132− fromString $ a : helper' as
133− where
134− helper' ('_':x:rest) = '-' : x : helper' rest
135− helper' (x:rest) = toLower x : helper' rest
136− helper' [] = []
137−cleanupVarName "CONTENT_TYPE" = "Content-Type"
138−cleanupVarName "CONTENT_LENGTH" = "Content-Length"
139−cleanupVarName "SCRIPT_NAME" = "CGI-Script-Name"
140−cleanupVarName x = fromString x -- FIXME remove?
Network/Wai/Handler/Helper.hsdeleted−25
@@ -1,25 +0,0 @@
1−module Network.Wai.Handler.Helper
2− ( requestBodyHandle
3− , requestBodyFunc
4− ) where
5−
6−import System.IO (Handle)
7−import qualified Data.ByteString as B
8−import Data.ByteString.Lazy.Internal (defaultChunkSize)
9−import Network.Wai (Source (..))
10−
11−requestBodyHandle :: Handle -> Int -> Source
12−requestBodyHandle h =
13− requestBodyFunc go
14− where
15− go i = Just `fmap` B.hGet h (min i defaultChunkSize)
16−
17−requestBodyFunc :: (Int -> IO (Maybe B.ByteString)) -> Int -> Source
18−requestBodyFunc _ 0 = Source $ return Nothing
19−requestBodyFunc h len = Source $ do
20− mbs <- h len
21− case mbs of
22− Nothing -> return Nothing
23− Just bs -> do
24− let newLen = len - B.length bs
25− return $ Just (bs, requestBodyFunc h $ max 0 newLen)
Network/Wai/Handler/SimpleServer.hsdeleted−181
@@ -1,181 +0,0 @@
1−{-# LANGUAGE DeriveDataTypeable #-}
2−{-# LANGUAGE FlexibleContexts #-}
3−{-# LANGUAGE OverloadedStrings #-}
4−---------------------------------------------------------
5−-- |
6−-- Module : Network.Wai.Handler.SimpleServer
7−-- Copyright : Michael Snoyman
8−-- License : BSD3
9−--
10−-- Maintainer : Michael Snoyman <michael@snoyman.com>
11−-- Stability : Stable
12−-- Portability : portable
13−--
14−-- A simplistic HTTP server handler for Wai.
15−--
16−---------------------------------------------------------
17−module Network.Wai.Handler.SimpleServer
18− ( run
19− ) where
20−
21−import Network.Wai
22−import Network.Wai.Handler.Helper
23−import qualified System.IO
24−
25−import Data.ByteString (ByteString)
26−import qualified Data.ByteString.Char8 as B
27−import qualified Data.ByteString.Lazy as L
28−import Network
29− ( listenOn, accept, sClose, PortID(PortNumber), Socket
30− , withSocketsDo)
31−import Control.Exception (bracket, finally, Exception, throwIO)
32−import System.IO (Handle, hClose)
33−import Control.Concurrent (forkIO)
34−import Control.Monad (unless)
35−import Data.Maybe (isJust, fromJust, fromMaybe)
36−
37−import Data.Typeable (Typeable)
38−
39−import Network.Socket.SendFile
40−import Control.Arrow (first)
41−
42−run :: Port -> Application -> IO ()
43−run port = withSocketsDo .
44− bracket
45− (listenOn $ PortNumber $ fromIntegral port)
46− sClose .
47− serveConnections port
48−type Port = Int
49−
50−serveConnections :: Port -> Application -> Socket -> IO ()
51−serveConnections port app socket = do
52− (conn, remoteHost', _) <- accept socket
53− _ <- forkIO $ serveConnection port app conn remoteHost'
54− serveConnections port app socket
55−
56−serveConnection :: Port -> Application -> Handle -> String -> IO ()
57−serveConnection port app conn remoteHost' =
58− finally
59− serveConnection'
60− (hClose conn)
61− where
62− serveConnection' = do
63− env <- hParseRequest port conn remoteHost'
64− res <- app env
65− sendResponse (httpVersion env) conn res
66−
67−hParseRequest :: Port -> Handle -> String -> IO Request
68−hParseRequest port conn remoteHost' = do
69− headers' <- takeUntilBlank conn id
70− parseRequest port headers' conn remoteHost'
71−
72−takeUntilBlank :: Handle
73− -> ([ByteString] -> [ByteString])
74− -> IO [ByteString]
75−takeUntilBlank h front = do
76− l <- stripCR `fmap` B.hGetLine h
77− if B.null l
78− then return $ front []
79− else takeUntilBlank h $ front . (:) l
80−
81−stripCR :: ByteString -> ByteString
82−stripCR bs
83− | B.null bs = bs
84− | B.last bs == '\r' = B.init bs
85− | otherwise = bs
86−
87−data InvalidRequest =
88− NotEnoughLines [String]
89− | HostNotIncluded
90− | BadFirstLine String
91− | NonHttp
92− deriving (Show, Typeable)
93−instance Exception InvalidRequest
94−
95−-- | Parse a set of header lines and body into a 'Request'.
96−parseRequest :: Port
97− -> [ByteString]
98− -> Handle
99− -> String
100− -> IO Request
101−parseRequest port lines' handle remoteHost' = do
102− case lines' of
103− (_:_:_) -> return ()
104− _ -> throwIO $ NotEnoughLines $ map B.unpack lines'
105− (method, rpath', gets, httpversion) <- parseFirst $ head lines'
106− let rpath = '/' : case B.unpack rpath' of
107− ('/':x) -> x
108− _ -> B.unpack rpath'
109− let heads = map (first mkCIByteString . parseHeaderNoAttr) $ tail lines'
110− let host' = lookup "Host" heads
111− unless (isJust host') $ throwIO HostNotIncluded
112− let host = fromJust host'
113− let len = fromMaybe 0 $ do
114− bs <- lookup "Content-Length" heads
115− let str = B.unpack bs
116− case reads str of
117− (x, _):_ -> Just x
118− _ -> Nothing
119− let (serverName', _) = B.break (== ':') host
120− return $ Request
121− { requestMethod = method
122− , httpVersion = httpversion
123− , pathInfo = B.pack rpath
124− , queryString = gets
125− , serverName = serverName'
126− , serverPort = port
127− , requestHeaders = heads
128− , isSecure = False
129− , requestBody = requestBodyHandle handle len
130− , errorHandler = System.IO.hPutStr System.IO.stderr
131− , remoteHost = B.pack remoteHost'
132− }
133−
134−parseFirst :: ByteString
135− -> IO (ByteString, ByteString, ByteString, HttpVersion)
136−parseFirst s = do
137− let pieces = B.words s
138− (method, query, http') <-
139− case pieces of
140− [x, y, z] -> return (x, y, z)
141− _ -> throwIO $ BadFirstLine $ B.unpack s
142− let (hfirst, hsecond) = B.splitAt 5 http'
143− unless (hfirst == B.pack "HTTP/") $ throwIO NonHttp
144− let (rpath, qstring) = B.break (== '?') query
145− return (method, rpath, qstring, hsecond)
146−
147−sendResponse :: HttpVersion -> Handle -> Response -> IO ()
148−sendResponse httpversion h res = do
149− B.hPut h $ B.pack "HTTP/"
150− B.hPut h $ httpversion
151− B.hPut h $ B.pack " "
152− B.hPut h $ B.pack $ show $ statusCode $ status res
153− B.hPut h $ B.pack " "
154− B.hPut h $ statusMessage $ status res
155− B.hPut h $ B.pack "\r\n"
156− mapM_ putHeader $ responseHeaders res
157− B.hPut h $ B.pack "\r\n"
158− case responseBody res of
159− ResponseFile fp -> unsafeSendFile h fp
160− ResponseEnumerator (Enumerator enum) -> enum myPut h >> return ()
161− ResponseLBS lbs -> L.hPut h lbs
162− where
163− myPut _ bs = do
164− B.hPut h bs
165− return (Right h)
166− putHeader (x, y) = do
167− B.hPut h $ ciOriginal x
168− B.hPut h $ B.pack ": "
169− B.hPut h y
170− B.hPut h $ B.pack "\r\n"
171−
172−parseHeaderNoAttr :: ByteString -> (ByteString, ByteString)
173−parseHeaderNoAttr s =
174− let (k, rest) = B.span (/= ':') s
175− rest' = if not (B.null rest) &&
176− B.head rest == ':' &&
177− not (B.null $ B.tail rest) &&
178− B.head (B.tail rest) == ' '
179− then B.drop 2 rest
180− else rest
181− in (k, rest')
Network/Wai/Middleware/CleanPath.hsdeleted−76
@@ -1,76 +0,0 @@
1−{-# LANGUAGE OverloadedStrings #-}
2−module Network.Wai.Middleware.CleanPath (cleanPath, cleanPathRel, splitPath) where
3−
4−import Network.Wai
5−import qualified Data.ByteString.Char8 as B
6−import qualified Data.ByteString.Lazy as L
7−import Network.URI (unEscapeString)
8−
9−-- | Performs redirects as per 'splitPath'.
10−cleanPathRel :: B.ByteString -> ([String] -> Request -> IO Response) -> Request -> IO Response
11−cleanPathRel prefix app env =
12− case splitPath $ pathInfo env of
13− Right pieces -> app pieces env
14− Left p -> return
15− . Response status301
16− [("Location", B.concat [prefix, p, suffix])]
17− $ ResponseLBS L.empty
18− where
19− -- include the query string if present
20− suffix =
21− case B.uncons $ queryString env of
22− Nothing -> B.empty
23− Just ('?', _) -> queryString env
24− _ -> B.cons '?' $ queryString env
25−
26−cleanPath :: ([String] -> Request -> IO Response) -> Request -> IO Response
27−cleanPath = cleanPathRel B.empty
28−
29−-- | Given a certain requested path, return either a corrected path
30−-- to redirect to or the tokenized path.
31−--
32−-- This code corrects for the following issues:
33−--
34−-- * It is missing a trailing slash, and there is no period after the
35−-- last slash.
36−--
37−-- * There are any doubled slashes.
38−splitPath :: B.ByteString -> Either B.ByteString [String]
39−splitPath s =
40− let corrected = B.pack $ rts $ ats $ rds $ B.unpack s
41− in if corrected == s
42− then Right $ map (unEscapeString . B.unpack)
43− $ filter (not . B.null)
44− $ B.split '/' s
45− else Left corrected
46−
47−-- | Remove double slashes
48−rds :: String -> String
49−rds [] = []
50−rds [x] = [x]
51−rds (a:b:c)
52− | a == '/' && b == '/' = rds (b:c)
53− | otherwise = a : rds (b:c)
54−
55−-- | Add a trailing slash if it is missing. Empty string is left alone.
56−ats :: String -> String
57−ats [] = []
58−ats s =
59− if last s == '/' || dbs (reverse s)
60− then s
61− else s ++ "/"
62−
63−-- | Remove a trailing slash if the last piece has a period.
64−rts :: String -> String
65−rts [] = []
66−rts s =
67− if last s == '/' && dbs (tail $ reverse s)
68− then init s
69− else s
70−
71−-- | Is there a period before a slash here?
72−dbs :: String -> Bool
73−dbs ('/':_) = False
74−dbs ('.':_) = True
75−dbs (_:x) = dbs x
76−dbs [] = False
Network/Wai/Middleware/Gzip.hsdeleted−63
@@ -1,63 +0,0 @@
1−{-# LANGUAGE Rank2Types #-}
2−{-# LANGUAGE ScopedTypeVariables #-}
3−{-# LANGUAGE OverloadedStrings #-}
4−---------------------------------------------------------
5−-- |
6−-- Module : Network.Wai.Middleware.Gzip
7−-- Copyright : Michael Snoyman
8−-- License : BSD3
9−--
10−-- Maintainer : Michael Snoyman <michael@snoyman.com>
11−-- Stability : Unstable
12−-- Portability : portable
13−--
14−-- Automatic gzip compression of responses.
15−--
16−---------------------------------------------------------
17−module Network.Wai.Middleware.Gzip (gzip) where
18−
19−import Network.Wai
20−import Network.Wai.Enumerator (fromResponseBody)
21−import Network.Wai.Zlib
22−import Data.Maybe (fromMaybe)
23−import qualified Data.ByteString.Char8 as B
24−
25−-- | Use gzip to compress the body of the response.
26−--
27−-- Analyzes the \"Accept-Encoding\" header from the client to determine
28−-- if gzip is supported.
29−--
30−-- Possible future enhancements:
31−--
32−-- * Only compress if the response is above a certain size.
33−--
34−-- * Add Content-Length.
35−--
36−-- * I read somewhere that \"the beast\" (MSIE) can\'t support compression
37−-- for Javascript files..
38−gzip :: Middleware
39−gzip app env = do
40− res <- app env
41− case responseBody res of
42− ResponseFile _ -> return res
43− _ -> do
44− let enc = fromMaybe []
45− $ (splitCommas . B.unpack)
46− `fmap` lookup "Accept-Encoding"
47− (requestHeaders env)
48− if "gzip" `elem` enc
49− then return res
50− { responseBody = compressE $ responseBody res
51− , responseHeaders = ("Content-Encoding", "gzip")
52− : responseHeaders res
53− }
54− else return res
55−
56−compressE :: ResponseBody -> ResponseBody
57−compressE = ResponseEnumerator . compress . fromResponseBody
58−
59−splitCommas :: String -> [String]
60−splitCommas [] = []
61−splitCommas x =
62− let (y, z) = break (== ',') x
63− in y : splitCommas (drop 1 z)
Network/Wai/Middleware/Jsonp.hsdeleted−85
@@ -1,85 +0,0 @@
1−{-# LANGUAGE OverloadedStrings #-}
2−---------------------------------------------------------
3−-- |
4−-- Module : Network.Wai.Middleware.Jsonp
5−-- Copyright : Michael Snoyman
6−-- License : BSD3
7−--
8−-- Maintainer : Michael Snoyman <michael@snoyman.com>
9−-- Stability : Unstable
10−-- Portability : portable
11−--
12−-- Automatic wrapping of JSON responses to convert into JSONP.
13−--
14−---------------------------------------------------------
15−module Network.Wai.Middleware.Jsonp (jsonp) where
16−
17−import Network.Wai
18−import Network.Wai.Enumerator (fromResponseBody)
19−import qualified Data.ByteString.Char8 as B8
20−import Data.Maybe (fromMaybe)
21−
22−takeCallback :: B8.ByteString -> Maybe B8.ByteString
23−takeCallback bs | B8.null bs = Nothing
24−takeCallback bs =
25− let (x, y) = B8.break (== '=') bs
26− (y', z) = B8.break (== '&') $ B8.drop 1 y
27− in if x == B8.pack "callback"
28− then Just y'
29− else takeCallback $ B8.drop 1 z
30−
31−dropQM :: B8.ByteString -> B8.ByteString
32−dropQM bs
33− | B8.null bs = bs
34− | B8.head bs == '?' = B8.tail bs
35− | otherwise = bs
36−
37−-- | Wrap json responses in a jsonp callback.
38−--
39−-- Basically, if the user requested a \"text\/javascript\" and supplied a
40−-- \"callback\" GET parameter, ask the application for an
41−-- \"application/json\" response, then convern that into a JSONP response,
42−-- having a content type of \"text\/javascript\" and calling the specified
43−-- callback function.
44−jsonp :: Middleware
45−jsonp app env = do
46− let accept = fromMaybe B8.empty $ lookup "Accept" $ requestHeaders env
47− let callback :: Maybe B8.ByteString
48− callback =
49− if B8.pack "text/javascript" `B8.isInfixOf` accept
50− then takeCallback $ dropQM $ queryString env
51− else Nothing
52− let env' =
53− case callback of
54− Nothing -> env
55− Just _ -> env
56− { requestHeaders = changeVal "Accept"
57− "application/json"
58− $ requestHeaders env
59− }
60− res <- app env'
61− case (fmap B8.unpack $ lookup "Content-Type" $ responseHeaders res, callback) of
62− (Just "application/json", Just c) -> return $ res
63− { responseHeaders = changeVal "Content-Type" "text/javascript" $ responseHeaders res
64− , responseBody = ResponseEnumerator $ addCallback c $ fromResponseBody $ responseBody res
65− }
66− _ -> return res
67−
68−addCallback :: B8.ByteString -> Enumerator -> Enumerator
69−addCallback cb (Enumerator e) = Enumerator $ \iter a -> do
70− ea' <- iter a $ B8.snoc cb '('
71− case ea' of
72− Left a' -> return $ Left a'
73− Right a' -> do
74− ea'' <- e iter a'
75− case ea'' of
76− Left a'' -> return $ Left a''
77− Right a'' -> iter a'' $ B8.singleton ')'
78−
79−changeVal :: Eq a
80− => a
81− -> String
82− -> [(a, B8.ByteString)]
83− -> [(a, B8.ByteString)]
84−changeVal key val old = (key, B8.pack val)
85− : filter (\(k, _) -> k /= key) old
Network/Wai/Parse.hsdeleted−397
@@ -1,397 +0,0 @@
1−{-# LANGUAGE CPP #-}
2−{-# LANGUAGE OverloadedStrings #-}
3−-- | Some helpers for parsing data out of a raw WAI 'Request'.
4−
5−module Network.Wai.Parse
6− ( parseQueryString
7− , parseCookies
8− , parseHttpAccept
9− , parseRequestBody
10− , Sink (..)
11− , lbsSink
12− , tempFileSink
13− , FileInfo (..)
14−#if TEST
15− , Bound (..)
16− , findBound
17− , sinkTillBound
18−#endif
19− ) where
20−
21−import qualified Data.ByteString as S
22−import qualified Data.ByteString.Lazy as L
23−import qualified Data.ByteString.Char8 as S8
24−import Data.Word (Word8)
25−import Data.Bits
26−import Data.Maybe (fromMaybe)
27−import Data.List (sortBy)
28−import Data.Function (on)
29−import System.Directory (removeFile, getTemporaryDirectory)
30−import System.IO (hClose, openBinaryTempFile, Handle)
31−import Network.Wai
32−
33−uncons :: S.ByteString -> Maybe (Word8, S.ByteString)
34−uncons s
35− | S.null s = Nothing
36− | otherwise = Just (S.head s, S.tail s)
37−
38−breakDiscard :: Word8 -> S.ByteString -> (S.ByteString, S.ByteString)
39−breakDiscard w s =
40− let (x, y) = S.break (== w) s
41− in (x, S.drop 1 y)
42−
43−-- | Split out the query string into a list of keys and values. A few
44−-- importants points:
45−--
46−-- * There is no way to distinguish between a parameter with no value and a
47−-- parameter with an empty value. Eg, "foo=" and "foo" are the same.
48−--
49−-- * The result returned is still bytestrings, since we perform no character
50−-- decoding here. Most likely, you will want to use UTF-8 decoding, but this is
51−-- left to the user of the library.
52−--
53−-- * Percent decoding errors are ignored. In particular, "%Q" will be output as
54−-- "%Q".
55−parseQueryString :: S.ByteString -> [(S.ByteString, S.ByteString)]
56−parseQueryString = parseQueryString' . dropQuestion
57− where
58− dropQuestion q | S.null q || S.head q /= 63 = q
59− dropQuestion q | otherwise = S.tail q
60− parseQueryString' q | S.null q = []
61− parseQueryString' q =
62− let (x, xs) = breakDiscard 38 q -- ampersand
63− in parsePair x : parseQueryString' xs
64− where
65− parsePair x =
66− let (k, v) = breakDiscard 61 x -- equal sign
67− in (qsDecode k, qsDecode v)
68−
69−
70−qsDecode :: S.ByteString -> S.ByteString
71−qsDecode z = fst $ S.unfoldrN (S.length z) go z
72− where
73− go bs =
74− case uncons bs of
75− Nothing -> Nothing
76− Just (43, ws) -> Just (32, ws) -- plus to space
77− Just (37, ws) -> Just $ fromMaybe (37, ws) $ do -- percent
78− (x, xs) <- uncons ws
79− x' <- hexVal x
80− (y, ys) <- uncons xs
81− y' <- hexVal y
82− Just $ (combine x' y', ys)
83− Just (w, ws) -> Just (w, ws)
84− hexVal w
85− | 48 <= w && w <= 57 = Just $ w - 48 -- 0 - 9
86− | 65 <= w && w <= 70 = Just $ w - 55 -- A - F
87− | 97 <= w && w <= 102 = Just $ w - 87 -- a - f
88− | otherwise = Nothing
89− combine :: Word8 -> Word8 -> Word8
90− combine a b = shiftL a 4 .|. b
91−
92−-- | Decode the value of an HTTP_COOKIE header into key/value pairs.
93−parseCookies :: S.ByteString -> [(S.ByteString, S.ByteString)]
94−parseCookies s
95− | S.null s = []
96− | otherwise =
97− let (first, rest) = breakDiscard 59 s -- semicolon
98− in parseCookie first : parseCookies rest
99−
100−parseCookie :: S.ByteString -> (S.ByteString, S.ByteString)
101−parseCookie s =
102− let (key, value) = breakDiscard 61 s -- equals sign
103− key' = S.dropWhile (== 32) key -- space
104− in (key', value)
105−
106−-- | Parse the HTTP accept string to determine supported content types.
107−parseHttpAccept :: S.ByteString -> [S.ByteString]
108−parseHttpAccept = map fst
109− . sortBy (rcompare `on` snd)
110− . map grabQ
111− . S.split 44 -- comma
112− where
113− rcompare :: Double -> Double -> Ordering
114− rcompare = flip compare
115− grabQ s =
116− let (s', q) = breakDiscard 59 s -- semicolon
117− (_, q') = breakDiscard 61 q -- equals sign
118− in (trimWhite s', readQ $ trimWhite q')
119− readQ s = case reads $ S8.unpack s of
120− (x, _):_ -> x
121− _ -> 1.0
122− trimWhite = S.dropWhile (== 32) -- space
123−
124−-- | A destination for data, the opposite of a 'Source'.
125−data Sink x y = Sink
126− { sinkInit :: IO x
127− , sinkAppend :: x -> S.ByteString -> IO x
128− , sinkClose :: x -> IO y
129− , sinkFinalize :: y -> IO ()
130− }
131−
132−lbsSink :: Sink ([S.ByteString] -> [S.ByteString]) L.ByteString
133−lbsSink = Sink
134− { sinkInit = return id
135− , sinkAppend = \front bs -> return $ front . (:) bs
136− , sinkClose = \front -> return $ L.fromChunks $ front []
137− , sinkFinalize = \_ -> return ()
138− }
139−
140−tempFileSink :: Sink (FilePath, Handle) FilePath
141−tempFileSink = Sink
142− { sinkInit = do
143− tempDir <- getTemporaryDirectory
144− openBinaryTempFile tempDir "webenc.buf"
145− , sinkAppend = \(fp, h) bs -> S.hPut h bs >> return (fp, h)
146− , sinkClose = \(fp, h) -> do
147− hClose h
148− return fp
149− , sinkFinalize = \fp -> removeFile fp
150− }
151−
152−-- | Information on an uploaded file.
153−data FileInfo c = FileInfo
154− { fileName :: S.ByteString
155− , fileContentType :: S.ByteString
156− , fileContent :: c
157− }
158− deriving (Eq, Show)
159−
160−type Param = (S.ByteString, S.ByteString)
161−type File y = (S.ByteString, FileInfo y)
162−
163−parseRequestBody :: Sink x y
164− -> Request
165− -> IO ([Param], [File y])
166−parseRequestBody sink req = do
167− let ctype = do
168− ctype' <- lookup "Content-Type" $ requestHeaders req
169− if urlenc `S.isPrefixOf` ctype'
170− then Just Nothing
171− else if formBound `S.isPrefixOf` ctype'
172− then Just $ Just $ S.drop (S.length formBound) ctype'
173− else Nothing
174− case ctype of
175− Nothing -> return ([], [])
176− Just Nothing -> do -- url-encoded
177− -- NOTE: in general, url-encoded data will be in a single chunk.
178− -- Therefore, I'm optimizing for the usual case by sticking with
179− -- strict byte strings here.
180− bs <- sourceToBs $ requestBody req
181− return (parseQueryString bs, [])
182− Just (Just bound) -> -- multi-part
183− let bound' = S8.pack "--" `S.append` bound
184− in parsePieces sink bound' (S.empty, Just $ requestBody req)
185− where
186− urlenc = S8.pack "application/x-www-form-urlencoded"
187− formBound = S8.pack "multipart/form-data; boundary="
188−
189−sourceToBs :: Source -> IO S.ByteString
190−sourceToBs = fmap S.concat . go id
191− where
192− go front (Source src) = do
193− res <- src
194− case res of
195− Nothing -> return $ front []
196− Just (bs, src') -> go (front . (:) bs) src'
197−
198−type Source' = (S.ByteString, Maybe Source)
199−
200−takeLine :: Source' -> IO (Maybe (S.ByteString, Source'))
201−takeLine (s, msrc)
202− | S.null s = case msrc of
203− Nothing -> return Nothing
204− Just (Source src) -> do
205− res <- src
206− case res of
207− Nothing -> return Nothing
208− Just (x, y) -> takeLine (x, Just y)
209−takeLine (s, msrc) =
210− let (x, y) = S.break (== 10) s -- newline
211− in if S.null y
212− then do
213− case msrc of
214− Nothing -> return $ Just (x, (y, msrc))
215− Just (Source src) -> do
216− res <- src
217− case res of
218− Nothing -> return $ Just (x, (y, Nothing))
219− Just (s', src') ->
220− takeLine (s `S.append` s', Just src')
221− else return $ Just (killCarriage x, (S.drop 1 y, msrc))
222− where
223− killCarriage bs
224− | S.null bs = bs
225− | S.last bs == 13 = S.init bs -- carriage return
226− | otherwise = bs
227−
228−takeLines :: Source' -> IO (Maybe ([S.ByteString], Source'))
229−takeLines src = do
230− res <- takeLine src
231− case res of
232− Nothing -> return Nothing
233− Just (l, src') ->
234− if S.null l
235− then return $ Just ([], src')
236− else do
237− res' <- takeLines src'
238− case res' of
239− Nothing -> return $ Just ([l], src')
240− Just (ls, src'') -> return $ Just (l : ls, src'')
241−
242−parsePieces :: Sink x y -> S.ByteString -> Source'
243− -> IO ([Param], [File y])
244−parsePieces sink bound src = do
245− res <- takeLine src
246− src' <- case res of
247− Nothing -> return (S.empty, Nothing)
248− -- The _bs here should only contain boundary information
249− Just (_bs, src') -> return src'
250− res' <- takeLines src'
251− case res' of
252− Nothing -> return ([], [])
253− Just (ls, src'') -> do
254− let ls' = map parsePair ls
255− let x = do
256− cd <- lookup contDisp ls'
257− let ct = lookup contType ls'
258− let attrs = parseAttrs cd
259− let nameBS = S8.pack "name"
260− name <- lookup nameBS attrs
261− let fnBS = S8.pack "filename"
262− return (ct, name, lookup fnBS attrs)
263− case x of
264− Just (Just ct, name, Just filename) -> do
265− seed <- sinkInit sink
266− (seed', wasFound, msrc''') <-
267− sinkTillBound bound src'' (sinkAppend sink) seed
268− y <- sinkClose sink seed'
269− let fi = FileInfo filename ct y
270− let y' = (name, fi)
271− (xs, ys) <-
272− if wasFound
273− then parsePieces sink bound msrc'''
274− else return ([], [])
275− return (xs, y' : ys)
276− Just (_ct, name, Nothing) -> do
277− let seed = id
278− let iter front bs = return $ front . (:) bs
279− (front, wasFound, msrc''') <-
280− sinkTillBound bound src'' iter seed
281− let bs = S.concat $ front []
282− let x' = (name, qsDecode bs)
283− (xs, ys) <-
284− if wasFound
285− then parsePieces sink bound msrc'''
286− else return ([], [])
287− return (x' : xs, ys)
288− _ -> do
289− -- ignore this part
290− let seed = ()
291− iter () _ = return ()
292− ((), wasFound, msrc''') <-
293− sinkTillBound bound src'' iter seed
294− if wasFound
295− then parsePieces sink bound msrc'''
296− else return ([], [])
297− where
298− contDisp = S8.pack "Content-Disposition"
299− contType = S8.pack "Content-Type"
300− parsePair s =
301− let (x, y) = breakDiscard 58 s -- colon
302− in (x, S.dropWhile (== 32) y) -- space
303−
304−data Bound = FoundBound S.ByteString S.ByteString
305− | NoBound
306− | PartialBound
307− deriving (Eq, Show)
308−
309−findBound :: S.ByteString -> S.ByteString -> Bound
310−findBound b bs = go [0..S.length bs - 1]
311− where
312− go [] = NoBound
313− go (i:is)
314− | mismatch [0..S.length b - 1] [i..S.length bs - 1] = go is
315− | otherwise =
316− let endI = i + S.length b
317− in if endI > S.length bs
318− then PartialBound
319− else FoundBound (S.take i bs) (S.drop endI bs)
320− mismatch [] _ = False
321− mismatch _ [] = False
322− mismatch (x:xs) (y:ys)
323− | S.index b x == S.index bs y = mismatch xs ys
324− | otherwise = True
325−
326−sinkTillBound :: S.ByteString
327− -> Source'
328− -> (x -> S.ByteString -> IO x)
329− -> x
330− -> IO (x, Bool, Source')
331−sinkTillBound bound (bs, msrc) iter seed = do
332− case findBound bound bs of
333− NoBound -> do
334− case msrc of
335− Nothing -> do
336− seed' <- iter seed bs
337− return (seed', False, (S.empty, Nothing))
338− Just (Source src) -> do
339− res <- src
340− case res of
341− Nothing -> do
342− seed' <- iter seed bs
343− return (seed', False, (S.empty, Nothing))
344− Just (bs', src') -> do
345− -- this funny bit is to catch when there's a
346− -- newline at the end of the previous chunk
347− (seed', bs'') <-
348− if not (S8.null bs) && S8.last bs `elem` "\n\r"
349− then do
350− let (front, back) =
351− S.splitAt (S.length bs - 2) bs
352− seed' <- iter seed front
353− return (seed', back `S.append` bs')
354− else do
355− seed' <- iter seed bs
356− return (seed', bs')
357− sinkTillBound bound (bs'', Just src') iter seed'
358− FoundBound before after -> do
359− let before' = killCRLF before
360− seed' <- iter seed before'
361− return (seed', True, (after, msrc))
362− PartialBound -> do
363− -- not so efficient, but hopefully the unusual case
364− case msrc of
365− Nothing -> do
366− seed' <- iter seed bs
367− return (seed', False, (S.empty, Nothing))
368− Just (Source src) -> do
369− res <- src
370− case res of
371− Nothing -> do
372− seed' <- iter seed bs
373− return (seed', False, (S.empty, Nothing))
374− Just (bs', src') -> do
375− let bs'' = bs `S.append` bs'
376− sinkTillBound bound (bs'', Just src') iter seed
377−
378−parseAttrs :: S.ByteString -> [(S.ByteString, S.ByteString)]
379−parseAttrs = map go . S.split 59 -- semicolon
380− where
381− tw = S.dropWhile (== 32) -- space
382− dq s = if S.length s > 2 && S.head s == 34 && S.last s == 34 -- quote
383− then S.tail $ S.init s
384− else s
385− go s =
386− let (x, y) = breakDiscard 61 s -- equals sign
387− in (tw x, dq $ tw y)
388−
389−killCRLF :: S.ByteString -> S.ByteString
390−killCRLF bs
391− | S.null bs || S8.last bs /= '\n' = bs
392− | otherwise = killCR $ S.init bs
393−
394−killCR :: S.ByteString -> S.ByteString
395−killCR bs
396− | S.null bs || S8.last bs /= '\r' = bs
397− | otherwise = S.init bs
Network/Wai/Zlib.hsdeleted−106
@@ -1,106 +0,0 @@
1−{-# LANGUAGE CPP, ForeignFunctionInterface #-}
2−{-# LANGUAGE EmptyDataDecls #-}
3−module Network.Wai.Zlib (compress) where
4−
5−import Prelude hiding (length)
6−import Network.Wai hiding (status)
7−
8−import Data.ByteString.Unsafe
9−import Data.ByteString.Lazy.Internal (defaultChunkSize)
10−
11−import Foreign.C
12−import Foreign.Ptr
13−import Foreign.Marshal.Alloc
14−import Control.Exception
15−import Control.Monad
16−
17−import Data.ByteString.Char8 hiding (putStrLn)
18−
19−chunkSize :: Int
20−chunkSize = defaultChunkSize
21−
22−compress :: Enumerator -> Enumerator
23−compress enum =
24− Enumerator $ \iter acc ->
25− bracket
26− c_create_z_stream
27− c_free_z_stream
28− (compressInner iter acc enum)
29−
30−compressInner :: (acc -> ByteString -> IO (Either acc acc))
31− -> acc
32− -> Enumerator
33− -> ZStream
34− -> IO (Either acc acc)
35−compressInner iter acc enum stream = allocaBytes chunkSize $ \outbuff -> do
36− when (stream == nullPtr) $ error "Error from zlib library"
37− c_set_avail_out stream outbuff $ fromIntegral chunkSize
38− eacc <- runEnumerator enum (compressIter iter stream outbuff) acc
39− case eacc of
40− Left acc' -> return $ Left acc'
41− Right acc' -> finishStream iter stream outbuff acc'
42−
43−drainOutput :: Bool -- ^ are we finishing?
44− -> ZStream
45− -> Ptr CChar -- ^ output buffer
46− -> (acc -> ByteString -> IO (Either acc acc))
47− -> acc
48− -> IO (Either acc acc)
49−drainOutput isFinish stream outbuff iter acc = do
50− if isFinish
51− then c_call_deflate_finish stream >> return ()
52− else c_call_deflate_noflush stream
53− newAvail <- fromIntegral `fmap` c_get_avail_out stream
54− if newAvail == 0 || (isFinish && newAvail /= chunkSize)
55− then do
56− bs <- unsafePackCStringLen (outbuff, chunkSize - newAvail)
57− eacc <- iter acc bs
58− case eacc of
59− Left acc' -> return $ Left acc'
60− Right acc' -> do
61− c_set_avail_out stream outbuff $ fromIntegral chunkSize
62− drainOutput isFinish stream outbuff iter acc'
63− else return $ Right acc
64−
65−finishStream :: (acc -> ByteString -> IO (Either acc acc))
66− -> ZStream
67− -> Ptr CChar
68− -> acc
69− -> IO (Either acc acc)
70−finishStream iter stream outbuff acc =
71− drainOutput True stream outbuff iter acc
72−
73−compressIter :: (acc -> ByteString -> IO (Either acc acc))
74− -> ZStream
75− -> Ptr CChar
76− -> acc
77− -> ByteString
78− -> IO (Either acc acc)
79−compressIter iter stream outbuff acc bsInput = do
80− unsafeUseAsCStringLen bsInput $ \(cstr, len) ->
81− c_set_avail_in stream cstr $ fromIntegral len
82− drainOutput False stream outbuff iter acc
83−
84−data ZStreamStruct
85−type ZStream = Ptr ZStreamStruct
86−
87−foreign import ccall unsafe "create_z_stream"
88− c_create_z_stream :: IO ZStream
89−
90−foreign import ccall unsafe "free_z_stream"
91− c_free_z_stream :: ZStream -> IO ()
92−
93−foreign import ccall unsafe "set_avail_in"
94− c_set_avail_in :: ZStream -> Ptr CChar -> CUInt -> IO ()
95−
96−foreign import ccall unsafe "set_avail_out"
97− c_set_avail_out :: ZStream -> Ptr CChar -> CUInt -> IO ()
98−
99−foreign import ccall unsafe "call_deflate_noflush"
100− c_call_deflate_noflush :: ZStream -> IO ()
101−
102−foreign import ccall unsafe "call_deflate_finish"
103− c_call_deflate_finish :: ZStream -> IO CInt
104−
105−foreign import ccall unsafe "get_avail_out"
106− c_get_avail_out :: ZStream -> IO CUInt
READMEdeleted−1
@@ -1 +0,0 @@
1−Provides some basic WAI handlers and middleware.
README.mdnew file+13
@@ -0,0 +1,13 @@
1+An interface between Haskell web frameworks and web servers.
2+
3+A Haskell web application targets WAI and then can be deployed to any backend with a WAI adaptor.
4+The premier WAI backend is the Warp web server.
5+
6+WAI also adds modularity and code-sharing through middleware and WAI applications.
7+Code can be written that works with any web framework targeting WAI.
8+
9+[Documentation](http://www.yesodweb.com/book/web-application-interface)
10+
11+[Haddock Documentation](http://hackage.haskell.org/package/wai)
12+
13+[![Build Status](https://dev.azure.com/yesodweb/wai/_apis/build/status/yesodweb.wai?branchName=master)](https://dev.azure.com/yesodweb/wai/_build/latest?definitionId=1&branchName=master)
auto-update/ChangeLog.mdnew file+34
@@ -0,0 +1,34 @@
1+# ChangeLog for auto-update
2+
3+## 0.1.6
4+
5+* Add control of activation on leading vs. trailing edges for Control.Debounce
6+ [#756](https://github.com/yesodweb/wai/pull/756)
7+
8+## 0.1.5
9+
10+* Using the Strict and StrictData language extensions for GHC >8.
11+ [#752](https://github.com/yesodweb/wai/pull/752)
12+
13+## 0.1.4.1
14+
15+* [#693](https://github.com/yesodweb/wai/pull/693):
16+ Improve documentation for `reaperAction` function.
17+* [#732](https://github.com/yesodweb/wai/pull/732):
18+ Fixed memory leak in `reaperAdd` function.
19+
20+## 0.1.4
21+
22+* Provide updateActionModify API in AutoUpdate [#547](https://github.com/yesodweb/wai/pull/547)
23+
24+## 0.1.3.1
25+
26+* Doc improvements
27+
28+## 0.1.3
29+
30+* Adding a new AIP - reaperKill
31+
32+## 0.1.2
33+
34+* Added Control.Debounce
auto-update/Control/AutoUpdate.hsnew file+201
@@ -0,0 +1,201 @@
1+{-# LANGUAGE CPP #-}
2+-- | In a multithreaded environment, running actions on a regularly scheduled
3+-- background thread can dramatically improve performance.
4+-- For example, web servers need to return the current time with each HTTP response.
5+-- For a high-volume server, it's much faster for a dedicated thread to run every
6+-- second, and write the current time to a shared 'IORef', than it is for each
7+-- request to make its own call to 'getCurrentTime'.
8+--
9+-- But for a low-volume server, whose request frequency is less than once per
10+-- second, that approach will result in /more/ calls to 'getCurrentTime' than
11+-- necessary, and worse, kills idle GC.
12+--
13+-- This library solves that problem by allowing you to define actions which will
14+-- either be performed by a dedicated thread, or, in times of low volume, will
15+-- be executed by the calling thread.
16+--
17+-- Example usage:
18+--
19+-- @
20+-- import "Data.Time"
21+-- import "Control.AutoUpdate"
22+--
23+-- getTime <- 'mkAutoUpdate' 'defaultUpdateSettings'
24+-- { 'updateAction' = 'Data.Time.Clock.getCurrentTime'
25+-- , 'updateFreq' = 1000000 -- The default frequency, once per second
26+-- }
27+-- currentTime <- getTime
28+-- @
29+--
30+-- For more examples, <http://www.yesodweb.com/blog/2014/08/announcing-auto-update see the blog post introducing this library>.
31+module Control.AutoUpdate (
32+ -- * Type
33+ UpdateSettings
34+ , defaultUpdateSettings
35+ -- * Accessors
36+ , updateAction
37+ , updateFreq
38+ , updateSpawnThreshold
39+ -- * Creation
40+ , mkAutoUpdate
41+ , mkAutoUpdateWithModify
42+ ) where
43+
44+#if __GLASGOW_HASKELL__ < 709
45+import Control.Applicative ((<*>))
46+#endif
47+import Control.Concurrent (forkIO, threadDelay)
48+import Control.Concurrent.MVar (newEmptyMVar, putMVar, readMVar,
49+ takeMVar, tryPutMVar)
50+import Control.Exception (SomeException, catch, mask_, throw,
51+ try)
52+import Control.Monad (void)
53+import Data.IORef (newIORef, readIORef, writeIORef)
54+
55+-- | Default value for creating an 'UpdateSettings'.
56+--
57+-- @since 0.1.0
58+defaultUpdateSettings :: UpdateSettings ()
59+defaultUpdateSettings = UpdateSettings
60+ { updateFreq = 1000000
61+ , updateSpawnThreshold = 3
62+ , updateAction = return ()
63+ }
64+
65+-- | Settings to control how values are updated.
66+--
67+-- This should be constructed using 'defaultUpdateSettings' and record
68+-- update syntax, e.g.:
69+--
70+-- @
71+-- let settings = 'defaultUpdateSettings' { 'updateAction' = 'Data.Time.Clock.getCurrentTime' }
72+-- @
73+--
74+-- @since 0.1.0
75+data UpdateSettings a = UpdateSettings
76+ { updateFreq :: Int
77+ -- ^ Microseconds between update calls. Same considerations as
78+ -- 'threadDelay' apply.
79+ --
80+ -- Default: 1 second (1000000)
81+ --
82+ -- @since 0.1.0
83+ , updateSpawnThreshold :: Int
84+ -- ^ NOTE: This value no longer has any effect, since worker threads are
85+ -- dedicated instead of spawned on demand.
86+ --
87+ -- Previously, this determined how many times the data must be requested
88+ -- before we decide to spawn a dedicated thread.
89+ --
90+ -- Default: 3
91+ --
92+ -- @since 0.1.0
93+ , updateAction :: IO a
94+ -- ^ Action to be performed to get the current value.
95+ --
96+ -- Default: does nothing.
97+ --
98+ -- @since 0.1.0
99+ }
100+
101+-- | Generate an action which will either read from an automatically
102+-- updated value, or run the update action in the current thread.
103+--
104+-- @since 0.1.0
105+mkAutoUpdate :: UpdateSettings a -> IO (IO a)
106+mkAutoUpdate us = mkAutoUpdateHelper us Nothing
107+
108+-- | Generate an action which will either read from an automatically
109+-- updated value, or run the update action in the current thread if
110+-- the first time or the provided modify action after that.
111+--
112+-- @since 0.1.4
113+mkAutoUpdateWithModify :: UpdateSettings a -> (a -> IO a) -> IO (IO a)
114+mkAutoUpdateWithModify us f = mkAutoUpdateHelper us (Just f)
115+
116+mkAutoUpdateHelper :: UpdateSettings a -> Maybe (a -> IO a) -> IO (IO a)
117+mkAutoUpdateHelper us updateActionModify = do
118+ -- A baton to tell the worker thread to generate a new value.
119+ needsRunning <- newEmptyMVar
120+
121+ -- The initial response variable. Response variables allow the requesting
122+ -- thread to block until a value is generated by the worker thread.
123+ responseVar0 <- newEmptyMVar
124+
125+ -- The current value, if available. We start off with a Left value
126+ -- indicating no value is available, and the above-created responseVar0 to
127+ -- give a variable to block on.
128+ currRef <- newIORef $ Left responseVar0
129+
130+ -- This is used to set a value in the currRef variable when the worker
131+ -- thread exits. In reality, that value should never be used, since the
132+ -- worker thread exiting only occurs if an async exception is thrown, which
133+ -- should only occur if there are no references to needsRunning left.
134+ -- However, this handler will make error messages much clearer if there's a
135+ -- bug in the implementation.
136+ let fillRefOnExit f = do
137+ eres <- try f
138+ case eres of
139+ Left e -> writeIORef currRef $ error $
140+ "Control.AutoUpdate.mkAutoUpdate: worker thread exited with exception: "
141+ ++ show (e :: SomeException)
142+ Right () -> writeIORef currRef $ error $
143+ "Control.AutoUpdate.mkAutoUpdate: worker thread exited normally, "
144+ ++ "which should be impossible due to usage of infinite loop"
145+
146+ -- fork the worker thread immediately. Note that we mask async exceptions,
147+ -- but *not* in an uninterruptible manner. This will allow a
148+ -- BlockedIndefinitelyOnMVar exception to still be thrown, which will take
149+ -- down this thread when all references to the returned function are
150+ -- garbage collected, and therefore there is no thread that can fill the
151+ -- needsRunning MVar.
152+ --
153+ -- Note that since we throw away the ThreadId of this new thread and never
154+ -- calls myThreadId, normal async exceptions can never be thrown to it,
155+ -- only RTS exceptions.
156+ mask_ $ void $ forkIO $ fillRefOnExit $ do
157+ -- This infinite loop makes up out worker thread. It takes an a
158+ -- responseVar value where the next value should be putMVar'ed to for
159+ -- the benefit of any requesters currently blocked on it.
160+ let loop responseVar maybea = do
161+ -- block until a value is actually needed
162+ takeMVar needsRunning
163+
164+ -- new value requested, so run the updateAction
165+ a <- catchSome $ maybe (updateAction us) id (updateActionModify <*> maybea)
166+
167+ -- we got a new value, update currRef and lastValue
168+ writeIORef currRef $ Right a
169+ putMVar responseVar a
170+
171+ -- delay until we're needed again
172+ threadDelay $ updateFreq us
173+
174+ -- delay's over. create a new response variable and set currRef
175+ -- to use it, so that the next requester will block on that
176+ -- variable. Then loop again with the updated response
177+ -- variable.
178+ responseVar' <- newEmptyMVar
179+ writeIORef currRef $ Left responseVar'
180+ loop responseVar' (Just a)
181+
182+ -- Kick off the loop, with the initial responseVar0 variable.
183+ loop responseVar0 Nothing
184+
185+ return $ do
186+ mval <- readIORef currRef
187+ case mval of
188+ Left responseVar -> do
189+ -- no current value, force the worker thread to run...
190+ void $ tryPutMVar needsRunning ()
191+
192+ -- and block for the result from the worker
193+ readMVar responseVar
194+ -- we have a current value, use it
195+ Right val -> return val
196+
197+-- | Turn a runtime exception into an impure exception, so that all 'IO'
198+-- actions will complete successfully. This simply defers the exception until
199+-- the value is forced.
200+catchSome :: IO a -> IO a
201+catchSome act = Control.Exception.catch act $ \e -> return $ throw (e :: SomeException)
auto-update/Control/AutoUpdate/Util.hsnew file+23
@@ -0,0 +1,23 @@
1+{-# LANGUAGE CPP #-}
2+module Control.AutoUpdate.Util
3+ ( atomicModifyIORef'
4+ ) where
5+
6+#ifndef MIN_VERSION_base
7+#define MIN_VERSION_base(x,y,z) 1
8+#endif
9+
10+#if MIN_VERSION_base(4,6,0)
11+import Data.IORef (atomicModifyIORef')
12+#else
13+import Data.IORef (IORef, atomicModifyIORef)
14+-- | Strict version of 'atomicModifyIORef'. This forces both the value stored
15+-- in the 'IORef' as well as the value returned.
16+atomicModifyIORef' :: IORef a -> (a -> (a,b)) -> IO b
17+atomicModifyIORef' ref f = do
18+ c <- atomicModifyIORef ref
19+ (\x -> let (a, b) = f x -- Lazy application of "f"
20+ in (a, a `seq` b)) -- Lazy application of "seq"
21+ -- The following forces "a `seq` b", so it also forces "f x".
22+ c `seq` return c
23+#endif
auto-update/Control/Debounce.hsnew file+60
@@ -0,0 +1,60 @@
1+{-# LANGUAGE ScopedTypeVariables #-}
2+-- | Debounce an action, ensuring it doesn't occur more than once for a given
3+-- period of time.
4+--
5+-- This is useful as an optimization, for example to ensure that logs are only
6+-- flushed to disk at most once per second.
7+--
8+-- Example usage:
9+--
10+-- @
11+-- printString <- 'mkDebounce' 'defaultDebounceSettings'
12+-- { 'debounceAction' = putStrLn "Running action"
13+-- , 'debounceFreq' = 5000000 -- 5 seconds
14+-- , 'debounceEdge' = 'DI.trailingEdge' -- Trigger on the trailing edge
15+-- }
16+-- @
17+--
18+-- >>> printString
19+-- Running action
20+-- >>> printString
21+-- <Wait five seconds>
22+-- Running action
23+--
24+-- See the fast-logger package ("System.Log.FastLogger") for real-world usage.
25+--
26+-- @since 0.1.2
27+module Control.Debounce
28+ ( -- * Type
29+ DI.DebounceSettings
30+ , defaultDebounceSettings
31+ -- * Accessors
32+ , DI.debounceFreq
33+ , DI.debounceAction
34+ , DI.debounceEdge
35+ , DI.leadingEdge
36+ , DI.trailingEdge
37+ -- * Creation
38+ , mkDebounce
39+ ) where
40+
41+import Control.Concurrent (newEmptyMVar, threadDelay)
42+import qualified Control.Debounce.Internal as DI
43+
44+-- | Default value for creating a 'DebounceSettings'.
45+--
46+-- @since 0.1.2
47+defaultDebounceSettings :: DI.DebounceSettings
48+defaultDebounceSettings = DI.DebounceSettings
49+ { DI.debounceFreq = 1000000
50+ , DI.debounceAction = return ()
51+ , DI.debounceEdge = DI.leadingEdge
52+ }
53+
54+-- | Generate an action which will trigger the debounced action to be performed.
55+--
56+-- @since 0.1.2
57+mkDebounce :: DI.DebounceSettings -> IO (IO ())
58+mkDebounce settings = do
59+ baton <- newEmptyMVar
60+ DI.mkDebounceInternal baton threadDelay settings
auto-update/Control/Debounce/Internal.hsnew file+98
@@ -0,0 +1,98 @@
1+{-# LANGUAGE ScopedTypeVariables #-}
2+
3+-- | Unstable API which exposes internals for testing.
4+module Control.Debounce.Internal (
5+ DebounceSettings(..)
6+ , DebounceEdge(..)
7+ , leadingEdge
8+ , trailingEdge
9+ , mkDebounceInternal
10+ ) where
11+
12+import Control.Concurrent (forkIO)
13+import Control.Concurrent.MVar (takeMVar, tryPutMVar, tryTakeMVar, MVar)
14+import Control.Exception (SomeException, handle, mask_)
15+import Control.Monad (forever, void)
16+
17+-- | Settings to control how debouncing should work.
18+--
19+-- This should be constructed using 'defaultDebounceSettings' and record
20+-- update syntax, e.g.:
21+--
22+-- @
23+-- let settings = 'defaultDebounceSettings' { 'debounceAction' = flushLog }
24+-- @
25+--
26+-- @since 0.1.2
27+data DebounceSettings = DebounceSettings
28+ { debounceFreq :: Int
29+ -- ^ Length of the debounce timeout period in microseconds.
30+ --
31+ -- Default: 1 second (1000000)
32+ --
33+ -- @since 0.1.2
34+ , debounceAction :: IO ()
35+ -- ^ Action to be performed.
36+ --
37+ -- Note: all exceptions thrown by this action will be silently discarded.
38+ --
39+ -- Default: does nothing.
40+ --
41+ -- @since 0.1.2
42+ , debounceEdge :: DebounceEdge
43+ -- ^ Whether to perform the action on the leading edge or trailing edge of
44+ -- the timeout.
45+ --
46+ -- Default: 'trailingEdge'.
47+ --
48+ -- @since 0.1.6
49+ }
50+
51+-- | Setting to control whether the action happens at the leading and/or trailing
52+-- edge of the timeout.
53+--
54+-- @since 0.1.6
55+data DebounceEdge =
56+ Leading
57+ -- ^ Perform the action immediately, and then begin a cooldown period.
58+ -- If the trigger happens again during the cooldown, wait until the end of the cooldown
59+ -- and then perform the action again, then enter a new cooldown period.
60+ | Trailing
61+ -- ^ Start a cooldown period and perform the action when the period ends. If another trigger
62+ -- happens during the cooldown, it has no effect.
63+ deriving (Show, Eq)
64+
65+
66+-- | Perform the action immediately, and then begin a cooldown period.
67+-- If the trigger happens again during the cooldown, wait until the end of the cooldown
68+-- and then perform the action again, then enter a new cooldown period.
69+--
70+-- @since 0.1.6
71+leadingEdge :: DebounceEdge
72+leadingEdge = Leading
73+
74+-- | Start a cooldown period and perform the action when the period ends. If another trigger
75+-- happens during the cooldown, it has no effect.
76+--
77+-- @since 0.1.6
78+trailingEdge :: DebounceEdge
79+trailingEdge = Trailing
80+
81+mkDebounceInternal :: MVar () -> (Int -> IO ()) -> DebounceSettings -> IO (IO ())
82+mkDebounceInternal baton delayFn (DebounceSettings freq action edge) = do
83+ mask_ $ void $ forkIO $ forever $ do
84+ takeMVar baton
85+ case edge of
86+ Leading -> do
87+ ignoreExc action
88+ delayFn freq
89+ Trailing -> do
90+ delayFn freq
91+ -- Empty the baton of any other activations during the interval
92+ void $ tryTakeMVar baton
93+ ignoreExc action
94+
95+ return $ void $ tryPutMVar baton ()
96+
97+ignoreExc :: IO () -> IO ()
98+ignoreExc = handle $ \(_ :: SomeException) -> return ()
auto-update/Control/Reaper.hsnew file+280
@@ -0,0 +1,280 @@
1+{-# LANGUAGE RecordWildCards #-}
2+{-# LANGUAGE BangPatterns #-}
3+
4+-- | This module provides the ability to create reapers: dedicated cleanup
5+-- threads. These threads will automatically spawn and die based on the
6+-- presence of a workload to process on. Example uses include:
7+--
8+-- * Killing long-running jobs
9+-- * Closing unused connections in a connection pool
10+-- * Pruning a cache of old items (see example below)
11+--
12+-- For real-world usage, search the <https://github.com/yesodweb/wai WAI family of packages>
13+-- for imports of "Control.Reaper".
14+module Control.Reaper (
15+ -- * Example: Regularly cleaning a cache
16+ -- $example1
17+
18+ -- * Settings
19+ ReaperSettings
20+ , defaultReaperSettings
21+ -- * Accessors
22+ , reaperAction
23+ , reaperDelay
24+ , reaperCons
25+ , reaperNull
26+ , reaperEmpty
27+ -- * Type
28+ , Reaper(..)
29+ -- * Creation
30+ , mkReaper
31+ -- * Helper
32+ , mkListAction
33+ ) where
34+
35+import Control.AutoUpdate.Util (atomicModifyIORef')
36+import Control.Concurrent (forkIO, threadDelay, killThread, ThreadId)
37+import Control.Exception (mask_)
38+import Data.IORef (IORef, newIORef, readIORef, writeIORef)
39+
40+-- | Settings for creating a reaper. This type has two parameters:
41+-- @workload@ gives the entire workload, whereas @item@ gives an
42+-- individual piece of the queue. A common approach is to have @workload@
43+-- be a list of @item@s. This is encouraged by 'defaultReaperSettings' and
44+-- 'mkListAction'.
45+--
46+-- @since 0.1.1
47+data ReaperSettings workload item = ReaperSettings
48+ { reaperAction :: workload -> IO (workload -> workload)
49+ -- ^ The action to perform on a workload. The result of this is a
50+ -- \"workload modifying\" function. In the common case of using lists,
51+ -- the result should be a difference list that prepends the remaining
52+ -- workload to the temporary workload. The temporary workload here
53+ -- refers to items added to the workload while the reaper action is
54+ -- running. For help with setting up such an action, see 'mkListAction'.
55+ --
56+ -- Default: do nothing with the workload, and then prepend it to the
57+ -- temporary workload. This is incredibly useless; you should
58+ -- definitely override this default.
59+ --
60+ -- @since 0.1.1
61+ , reaperDelay :: {-# UNPACK #-} !Int
62+ -- ^ Number of microseconds to delay between calls of 'reaperAction'.
63+ --
64+ -- Default: 30 seconds.
65+ --
66+ -- @since 0.1.1
67+ , reaperCons :: item -> workload -> workload
68+ -- ^ Add an item onto a workload.
69+ --
70+ -- Default: list consing.
71+ --
72+ -- @since 0.1.1
73+ , reaperNull :: workload -> Bool
74+ -- ^ Check if a workload is empty, in which case the worker thread
75+ -- will shut down.
76+ --
77+ -- Default: 'null'.
78+ --
79+ -- @since 0.1.1
80+ , reaperEmpty :: workload
81+ -- ^ An empty workload.
82+ --
83+ -- Default: empty list.
84+ --
85+ -- @since 0.1.1
86+ }
87+
88+-- | Default @ReaperSettings@ value, biased towards having a list of work
89+-- items.
90+--
91+-- @since 0.1.1
92+defaultReaperSettings :: ReaperSettings [item] item
93+defaultReaperSettings = ReaperSettings
94+ { reaperAction = \wl -> return (wl ++)
95+ , reaperDelay = 30000000
96+ , reaperCons = (:)
97+ , reaperNull = null
98+ , reaperEmpty = []
99+ }
100+
101+-- | A data structure to hold reaper APIs.
102+data Reaper workload item = Reaper {
103+ -- | Adding an item to the workload
104+ reaperAdd :: item -> IO ()
105+ -- | Reading workload.
106+ , reaperRead :: IO workload
107+ -- | Stopping the reaper thread if exists.
108+ -- The current workload is returned.
109+ , reaperStop :: IO workload
110+ -- | Killing the reaper thread immediately if exists.
111+ , reaperKill :: IO ()
112+ }
113+
114+-- | State of reaper.
115+data State workload = NoReaper -- ^ No reaper thread
116+ | Workload !workload -- ^ The current jobs
117+
118+-- | Create a reaper addition function. This function can be used to add
119+-- new items to the workload. Spawning of reaper threads will be handled
120+-- for you automatically.
121+--
122+-- @since 0.1.1
123+mkReaper :: ReaperSettings workload item -> IO (Reaper workload item)
124+mkReaper settings@ReaperSettings{..} = do
125+ stateRef <- newIORef NoReaper
126+ tidRef <- newIORef Nothing
127+ return Reaper {
128+ reaperAdd = add settings stateRef tidRef
129+ , reaperRead = readRef stateRef
130+ , reaperStop = stop stateRef
131+ , reaperKill = kill tidRef
132+ }
133+ where
134+ readRef stateRef = do
135+ mx <- readIORef stateRef
136+ case mx of
137+ NoReaper -> return reaperEmpty
138+ Workload wl -> return wl
139+ stop stateRef = atomicModifyIORef' stateRef $ \mx ->
140+ case mx of
141+ NoReaper -> (NoReaper, reaperEmpty)
142+ Workload x -> (Workload reaperEmpty, x)
143+ kill tidRef = do
144+ mtid <- readIORef tidRef
145+ case mtid of
146+ Nothing -> return ()
147+ Just tid -> killThread tid
148+
149+add :: ReaperSettings workload item
150+ -> IORef (State workload) -> IORef (Maybe ThreadId)
151+ -> item -> IO ()
152+add settings@ReaperSettings{..} stateRef tidRef item =
153+ mask_ $ do
154+ next <- atomicModifyIORef' stateRef cons
155+ next
156+ where
157+ cons NoReaper = let wl = reaperCons item reaperEmpty
158+ in (Workload wl, spawn settings stateRef tidRef)
159+ cons (Workload wl) = let wl' = reaperCons item wl
160+ in (Workload wl', return ())
161+
162+spawn :: ReaperSettings workload item
163+ -> IORef (State workload) -> IORef (Maybe ThreadId)
164+ -> IO ()
165+spawn settings stateRef tidRef = do
166+ tid <- forkIO $ reaper settings stateRef tidRef
167+ writeIORef tidRef $ Just tid
168+
169+reaper :: ReaperSettings workload item
170+ -> IORef (State workload) -> IORef (Maybe ThreadId)
171+ -> IO ()
172+reaper settings@ReaperSettings{..} stateRef tidRef = do
173+ threadDelay reaperDelay
174+ -- Getting the current jobs. Push an empty job to the reference.
175+ wl <- atomicModifyIORef' stateRef swapWithEmpty
176+ -- Do the jobs. A function to merge the left jobs and
177+ -- new jobs is returned.
178+ !merge <- reaperAction wl
179+ -- Merging the left jobs and new jobs.
180+ -- If there is no jobs, this thread finishes.
181+ next <- atomicModifyIORef' stateRef (check merge)
182+ next
183+ where
184+ swapWithEmpty NoReaper = error "Control.Reaper.reaper: unexpected NoReaper (1)"
185+ swapWithEmpty (Workload wl) = (Workload reaperEmpty, wl)
186+
187+ check _ NoReaper = error "Control.Reaper.reaper: unexpected NoReaper (2)"
188+ check merge (Workload wl)
189+ -- If there is no job, reaper is terminated.
190+ | reaperNull wl' = (NoReaper, writeIORef tidRef Nothing)
191+ -- If there are jobs, carry them out.
192+ | otherwise = (Workload wl', reaper settings stateRef tidRef)
193+ where
194+ wl' = merge wl
195+
196+-- | A helper function for creating 'reaperAction' functions. You would
197+-- provide this function with a function to process a single work item and
198+-- return either a new work item, or @Nothing@ if the work item is
199+-- expired.
200+--
201+-- @since 0.1.1
202+mkListAction :: (item -> IO (Maybe item'))
203+ -> [item]
204+ -> IO ([item'] -> [item'])
205+mkListAction f =
206+ go id
207+ where
208+ go !front [] = return front
209+ go !front (x:xs) = do
210+ my <- f x
211+ let front' =
212+ case my of
213+ Nothing -> front
214+ Just y -> front . (y:)
215+ go front' xs
216+
217+-- $example1
218+-- In this example code, we use a 'Data.Map.Strict.Map' to cache fibonacci numbers, and a 'Reaper' to prune the cache.
219+--
220+-- The @main@ function first creates a 'Reaper', with fields to initialize the
221+-- cache ('reaperEmpty'), add items to it ('reaperCons'), and prune it ('reaperAction').
222+-- The reaper will run every two seconds ('reaperDelay'), but will stop running while
223+-- 'reaperNull' is true.
224+--
225+-- @main@ then loops infinitely ('Control.Monad.forever'). Each second it calculates the fibonacci number
226+-- for a value between 30 and 34, first trying the cache ('reaperRead' and 'Data.Map.Strict.lookup'),
227+-- then falling back to manually calculating it (@fib@)
228+-- and updating the cache with the result ('reaperAdd')
229+--
230+-- @clean@ simply removes items cached for more than 10 seconds.
231+-- This function is where you would perform IO-related cleanup,
232+-- like killing threads or closing connections, if that was the purpose of your reaper.
233+--
234+-- @
235+-- module Main where
236+--
237+-- import "Data.Time" (UTCTime, getCurrentTime, diffUTCTime)
238+-- import "Control.Reaper"
239+-- import "Control.Concurrent" (threadDelay)
240+-- import "Data.Map.Strict" (Map)
241+-- import qualified "Data.Map.Strict" as Map
242+-- import "Control.Monad" (forever)
243+-- import "System.Random" (getStdRandom, randomR)
244+--
245+-- fib :: 'Int' -> 'Int'
246+-- fib 0 = 0
247+-- fib 1 = 1
248+-- fib n = fib (n-1) + fib (n-2)
249+--
250+-- type Cache = 'Data.Map.Strict.Map' 'Int' ('Int', 'Data.Time.Clock.UTCTime')
251+--
252+-- main :: IO ()
253+-- main = do
254+-- reaper <- 'mkReaper' 'defaultReaperSettings'
255+-- { 'reaperEmpty' = Map.'Data.Map.Strict.empty'
256+-- , 'reaperCons' = \\(k, v, time) workload -> Map.'Data.Map.Strict.insert' k (v, time) workload
257+-- , 'reaperAction' = clean
258+-- , 'reaperDelay' = 1000000 * 2 -- Clean every 2 seconds
259+-- , 'reaperNull' = Map.'Data.Map.Strict.null'
260+-- }
261+-- forever $ do
262+-- fibArg <- 'System.Random.getStdRandom' ('System.Random.randomR' (30,34))
263+-- cache <- 'reaperRead' reaper
264+-- let cachedResult = Map.'Data.Map.Strict.lookup' fibArg cache
265+-- case cachedResult of
266+-- 'Just' (fibResult, _createdAt) -> 'putStrLn' $ "Found in cache: `fib " ++ 'show' fibArg ++ "` " ++ 'show' fibResult
267+-- 'Nothing' -> do
268+-- let fibResult = fib fibArg
269+-- 'putStrLn' $ "Calculating `fib " ++ 'show' fibArg ++ "` " ++ 'show' fibResult
270+-- time <- 'Data.Time.Clock.getCurrentTime'
271+-- ('reaperAdd' reaper) (fibArg, fibResult, time)
272+-- 'threadDelay' 1000000 -- 1 second
273+--
274+-- -- Remove items > 10 seconds old
275+-- clean :: Cache -> IO (Cache -> Cache)
276+-- clean oldMap = do
277+-- currentTime <- 'Data.Time.Clock.getCurrentTime'
278+-- let pruned = Map.'Data.Map.Strict.filter' (\\(_, createdAt) -> currentTime \`diffUTCTime\` createdAt < 10.0) oldMap
279+-- return (\\newData -> Map.'Data.Map.Strict.union' pruned newData)
280+-- @
auto-update/LICENSEnew file+20
@@ -0,0 +1,20 @@
1+Copyright (c) 2014 Michael Snoyman
2+
3+Permission is hereby granted, free of charge, to any person obtaining
4+a copy of this software and associated documentation files (the
5+"Software"), to deal in the Software without restriction, including
6+without limitation the rights to use, copy, modify, merge, publish,
7+distribute, sublicense, and/or sell copies of the Software, and to
8+permit persons to whom the Software is furnished to do so, subject to
9+the following conditions:
10+
11+The above copyright notice and this permission notice shall be included
12+in all copies or substantial portions of the Software.
13+
14+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
auto-update/README.mdnew file+13
@@ -0,0 +1,13 @@
1+## auto-update
2+
3+A common problem is the desire to have an action run at a scheduled interval,
4+but only if it is needed. For example, instead of having every web request
5+result in a new `getCurrentTime` call, we'd like to have a single worker thread
6+run every second, updating an `IORef`. However, if the request frequency is
7+less than once per second, this is a pessimization, and worse, kills idle GC.
8+
9+This library allows you to define actions which will either be performed by a
10+dedicated thread or, in times of low volume, will be executed by the calling
11+thread.
12+
13+For original use case, see [yesod-scaffold issue #15](https://github.com/yesodweb/yesod-scaffold/pull/15).
auto-update/Setup.hsnew file+2
@@ -0,0 +1,2 @@
1+import Distribution.Simple
2+main = defaultMain
auto-update/auto-update.cabalnew file+38
@@ -0,0 +1,38 @@
1+name: auto-update
2+version: 0.1.6
3+synopsis: Efficiently run periodic, on-demand actions
4+description: API docs and the README are available at <http://www.stackage.org/package/auto-update>.
5+homepage: https://github.com/yesodweb/wai
6+license: MIT
7+license-file: LICENSE
8+author: Michael Snoyman
9+maintainer: michael@snoyman.com
10+category: Control
11+build-type: Simple
12+extra-source-files: README.md
13+ ChangeLog.md
14+cabal-version: >=1.10
15+
16+library
17+ ghc-options: -Wall
18+ exposed-modules: Control.AutoUpdate
19+ Control.Debounce
20+ Control.Debounce.Internal
21+ Control.Reaper
22+ other-modules: Control.AutoUpdate.Util
23+ build-depends: base >= 4 && < 5
24+ default-language: Haskell2010
25+ if impl(ghc >= 8)
26+ default-extensions: Strict StrictData
27+
28+-- Test suite is currently not robust enough, gives too many false negatives.
29+
30+test-suite spec
31+ main-is: Spec.hs
32+ other-modules: Control.AutoUpdateSpec
33+ Control.DebounceSpec
34+ Control.ReaperSpec
35+ hs-source-dirs: test
36+ type: exitcode-stdio-1.0
37+ build-depends: base, auto-update, exceptions, hspec, retry, HUnit
38+ default-language: Haskell2010
auto-update/test/Control/AutoUpdateSpec.hsnew file+35
@@ -0,0 +1,35 @@
1+module Control.AutoUpdateSpec (spec) where
2+
3+import Control.AutoUpdate
4+import Control.Concurrent (threadDelay)
5+import Control.Monad (replicateM_, forM_)
6+import Data.IORef
7+import Test.Hspec
8+import Test.Hspec.QuickCheck
9+
10+spec :: Spec
11+spec = return ()
12+ -- do
13+ -- prop "incrementer" $ \st' -> do
14+ -- let st = abs st' `mod` 10000
15+ -- ref <- newIORef 0
16+ -- next <- mkAutoUpdate defaultUpdateSettings
17+ -- { updateAction = atomicModifyIORef ref $ \i ->
18+ -- let i' = succ i in i' `seq` (i', i')
19+ -- , updateSpawnThreshold = st
20+ -- , updateFreq = 10000
21+ -- }
22+
23+ -- forM_ [1..st + 1] $ \i -> do
24+ -- j <- next
25+ -- j `shouldBe` i
26+
27+ -- replicateM_ 50 $ do
28+ -- i <- next
29+ -- i `shouldBe` st + 2
30+
31+ -- threadDelay 60000
32+ -- last1 <- readIORef ref
33+ -- threadDelay 20000
34+ -- last2 <- readIORef ref
35+ -- last2 `shouldBe` last1
auto-update/test/Control/DebounceSpec.hsnew file+120
@@ -0,0 +1,120 @@
1+{-# LANGUAGE ScopedTypeVariables #-}
2+module Control.DebounceSpec (spec) where
3+
4+import Control.Concurrent
5+import Control.Debounce
6+import qualified Control.Debounce.Internal as DI
7+import Control.Monad
8+import Control.Monad.Catch
9+import Control.Retry
10+import Data.IORef
11+import Test.HUnit.Lang
12+import Test.Hspec
13+
14+spec :: Spec
15+spec = describe "mkDebounce" $ do
16+ describe "Leading edge" $ do
17+ it "works for a single event" $ do
18+ (ref, debounced, baton, returnFromWait) <- getDebounce leadingEdge
19+
20+ debounced
21+ waitUntil 5 $ readIORef ref >>= (`shouldBe` 1)
22+
23+ returnFromWait
24+ pause
25+ readIORef ref >>= (`shouldBe` 1)
26+
27+ -- Try another round
28+ debounced
29+ waitUntil 5 $ readIORef ref >>= (`shouldBe` 2)
30+
31+ returnFromWait
32+ pause
33+ readIORef ref >>= (`shouldBe` 2)
34+
35+ it "works for multiple events" $ do
36+ (ref, debounced, baton, returnFromWait) <- getDebounce leadingEdge
37+
38+ debounced
39+ waitForBatonToBeTaken baton
40+ debounced
41+ pause
42+ waitUntil 5 $ readIORef ref >>= (`shouldBe` 1)
43+
44+ returnFromWait
45+ pause
46+ readIORef ref >>= (`shouldBe` 2)
47+
48+ describe "Trailing edge" $ do
49+ it "works for a single event" $ do
50+ (ref, debounced, baton, returnFromWait) <- getDebounce trailingEdge
51+
52+ debounced
53+ pause
54+ waitUntil 5 $ readIORef ref >>= (`shouldBe` 0)
55+
56+ returnFromWait
57+ waitUntil 5 $ readIORef ref >>= (`shouldBe` 1)
58+
59+ -- Try another round
60+ debounced
61+ pause
62+ waitUntil 5 $ readIORef ref >>= (`shouldBe` 1)
63+
64+ returnFromWait
65+ waitUntil 5 $ readIORef ref >>= (`shouldBe` 2)
66+
67+ it "works for multiple events" $ do
68+ (ref, debounced, baton, returnFromWait) <- getDebounce trailingEdge
69+
70+ debounced
71+ waitForBatonToBeTaken baton
72+ debounced
73+ pause
74+ waitUntil 5 $ readIORef ref >>= (`shouldBe` 0)
75+
76+ returnFromWait
77+ waitUntil 5 $ readIORef ref >>= (`shouldBe` 1)
78+
79+
80+-- | Make a controllable delay function
81+getWaitAction :: IO (p -> IO (), IO ())
82+getWaitAction = do
83+ waitVar <- newEmptyMVar
84+ let waitAction _ = takeMVar waitVar
85+ let returnFromWait = putMVar waitVar ()
86+ return (waitAction, returnFromWait)
87+
88+-- | Get a debounce system with access to the internals for testing
89+getDebounce :: DI.DebounceEdge -> IO (IORef Int, IO (), MVar (), IO ())
90+getDebounce edge = do
91+ ref :: IORef Int <- newIORef 0
92+ let action = modifyIORef ref (+ 1)
93+
94+ (waitAction, returnFromWait) <- getWaitAction
95+
96+ baton <- newEmptyMVar
97+
98+ debounced <- DI.mkDebounceInternal baton waitAction defaultDebounceSettings {
99+ debounceFreq = 5000000 -- unused
100+ , debounceAction = action
101+ , debounceEdge = edge
102+ }
103+
104+ return (ref, debounced, baton, returnFromWait)
105+
106+-- | Pause briefly (100ms)
107+pause :: IO ()
108+pause = threadDelay 100000
109+
110+waitForBatonToBeTaken :: MVar () -> IO ()
111+waitForBatonToBeTaken baton = waitUntil 5 $ tryReadMVar baton >>= (`shouldBe` Nothing)
112+
113+-- | Wait up to n seconds for an action to complete without throwing an HUnitFailure
114+waitUntil :: Int -> IO a -> IO ()
115+waitUntil n action = recovering policy [handler] (\_status -> void action)
116+ where policy = constantDelay 1000 `mappend` limitRetries (n * 1000) -- 1ms * n * 1000 tries = n seconds
117+ handler _status = Handler (\(HUnitFailure {}) -> return True)
118+
119+main :: IO ()
120+main = hspec spec
auto-update/test/Control/ReaperSpec.hsnew file+39
@@ -0,0 +1,39 @@
1+module Control.ReaperSpec (spec) where
2+
3+import Control.Concurrent
4+import Control.Reaper
5+import Data.IORef
6+import Test.Hspec
7+import Test.Hspec.QuickCheck
8+
9+
10+spec :: Spec
11+spec = return ()
12+-- prop "works" $ \is -> do
13+-- reaper <- mkReaper defaultReaperSettings
14+-- { reaperAction = action
15+-- , reaperDelay = 1000
16+-- }
17+
18+-- let mkTestCase i = do
19+-- ref <- newIORef 0
20+-- let expected = (abs i `mod` 10) + 1
21+-- reaperAdd reaper (expected, ref)
22+-- return (expected, ref)
23+-- testCases <- mapM mkTestCase is
24+
25+-- let test (expected, ref) = do
26+-- actual <- readIORef ref
27+-- actual `shouldBe` (expected :: Int)
28+-- threadDelay 100000
29+-- mapM_ test testCases
30+-- [] <- reaperRead reaper
31+-- return ()
32+
33+-- type Item = (Int, IORef Int)
34+
35+-- action = mkListAction $ \(i, ref) -> do
36+-- modifyIORef ref succ
37+-- return $ if i > 1
38+-- then Just (pred i, ref)
39+-- else Nothing
auto-update/test/Spec.hsnew file+1
@@ -0,0 +1 @@
1+{-# OPTIONS_GHC -F -pgmF hspec-discover #-}
c/helper.cdeleted−56
@@ -1,56 +0,0 @@
1−#include <zlib.h>
2−#include <stdlib.h>
3−#include <assert.h>
4−#include <stdio.h>
5−
6−z_stream * create_z_stream (void)
7−{
8− z_stream *ret = malloc(sizeof(z_stream));
9− ret->zalloc = Z_NULL;
10− ret->zfree = Z_NULL;
11− ret->opaque = Z_NULL;
12− ret->next_in = NULL;
13− ret->avail_in = 0;
14−
15− if (deflateInit2(ret, 7, Z_DEFLATED, 31, 8, Z_DEFAULT_STRATEGY)
16− != Z_OK) return NULL;
17− else return ret;
18−}
19−
20−void free_z_stream (z_stream *stream)
21−{
22− deflateEnd(stream);
23− free(stream);
24−}
25−
26−void set_avail_in (z_stream *stream, char *buff, unsigned int avail)
27−{
28− stream->next_in = buff;
29− stream->avail_in = avail;
30−}
31−
32−void set_avail_out (z_stream *stream, char *buff, unsigned int avail)
33−{
34− stream->next_out = buff;
35− stream->avail_out = avail;
36−}
37−
38−void call_deflate_noflush (z_stream *stream)
39−{
40− int ret;
41− ret = deflate(stream, Z_NO_FLUSH);
42− assert (ret == Z_OK);
43−}
44−
45−int call_deflate_finish (z_stream *stream)
46−{
47− int ret;
48− ret = deflate(stream, Z_FINISH);
49− assert (ret == Z_OK || ret == Z_STREAM_END);
50− return ret;
51−}
52−
53−unsigned int get_avail_out (z_stream *stream)
54−{
55− return stream->avail_out;
56−}
cbits/adler32.cdeleted−149
@@ -1,149 +0,0 @@
1−/* adler32.c -- compute the Adler-32 checksum of a data stream
2− * Copyright (C) 1995-2004 Mark Adler
3− * For conditions of distribution and use, see copyright notice in zlib.h
4− */
5−
6−/* @(#) $Id$ */
7−
8−#define ZLIB_INTERNAL
9−#include "zlib.h"
10−
11−#define BASE 65521UL /* largest prime smaller than 65536 */
12−#define NMAX 5552
13−/* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */
14−
15−#define DO1(buf,i) {adler += (buf)[i]; sum2 += adler;}
16−#define DO2(buf,i) DO1(buf,i); DO1(buf,i+1);
17−#define DO4(buf,i) DO2(buf,i); DO2(buf,i+2);
18−#define DO8(buf,i) DO4(buf,i); DO4(buf,i+4);
19−#define DO16(buf) DO8(buf,0); DO8(buf,8);
20−
21−/* use NO_DIVIDE if your processor does not do division in hardware */
22−#ifdef NO_DIVIDE
23−# define MOD(a) \
24− do { \
25− if (a >= (BASE << 16)) a -= (BASE << 16); \
26− if (a >= (BASE << 15)) a -= (BASE << 15); \
27− if (a >= (BASE << 14)) a -= (BASE << 14); \
28− if (a >= (BASE << 13)) a -= (BASE << 13); \
29− if (a >= (BASE << 12)) a -= (BASE << 12); \
30− if (a >= (BASE << 11)) a -= (BASE << 11); \
31− if (a >= (BASE << 10)) a -= (BASE << 10); \
32− if (a >= (BASE << 9)) a -= (BASE << 9); \
33− if (a >= (BASE << 8)) a -= (BASE << 8); \
34− if (a >= (BASE << 7)) a -= (BASE << 7); \
35− if (a >= (BASE << 6)) a -= (BASE << 6); \
36− if (a >= (BASE << 5)) a -= (BASE << 5); \
37− if (a >= (BASE << 4)) a -= (BASE << 4); \
38− if (a >= (BASE << 3)) a -= (BASE << 3); \
39− if (a >= (BASE << 2)) a -= (BASE << 2); \
40− if (a >= (BASE << 1)) a -= (BASE << 1); \
41− if (a >= BASE) a -= BASE; \
42− } while (0)
43−# define MOD4(a) \
44− do { \
45− if (a >= (BASE << 4)) a -= (BASE << 4); \
46− if (a >= (BASE << 3)) a -= (BASE << 3); \
47− if (a >= (BASE << 2)) a -= (BASE << 2); \
48− if (a >= (BASE << 1)) a -= (BASE << 1); \
49− if (a >= BASE) a -= BASE; \
50− } while (0)
51−#else
52−# define MOD(a) a %= BASE
53−# define MOD4(a) a %= BASE
54−#endif
55−
56−/* ========================================================================= */
57−uLong ZEXPORT adler32(adler, buf, len)
58− uLong adler;
59− const Bytef *buf;
60− uInt len;
61−{
62− unsigned long sum2;
63− unsigned n;
64−
65− /* split Adler-32 into component sums */
66− sum2 = (adler >> 16) & 0xffff;
67− adler &= 0xffff;
68−
69− /* in case user likes doing a byte at a time, keep it fast */
70− if (len == 1) {
71− adler += buf[0];
72− if (adler >= BASE)
73− adler -= BASE;
74− sum2 += adler;
75− if (sum2 >= BASE)
76− sum2 -= BASE;
77− return adler | (sum2 << 16);
78− }
79−
80− /* initial Adler-32 value (deferred check for len == 1 speed) */
81− if (buf == Z_NULL)
82− return 1L;
83−
84− /* in case short lengths are provided, keep it somewhat fast */
85− if (len < 16) {
86− while (len--) {
87− adler += *buf++;
88− sum2 += adler;
89− }
90− if (adler >= BASE)
91− adler -= BASE;
92− MOD4(sum2); /* only added so many BASE's */
93− return adler | (sum2 << 16);
94− }
95−
96− /* do length NMAX blocks -- requires just one modulo operation */
97− while (len >= NMAX) {
98− len -= NMAX;
99− n = NMAX / 16; /* NMAX is divisible by 16 */
100− do {
101− DO16(buf); /* 16 sums unrolled */
102− buf += 16;
103− } while (--n);
104− MOD(adler);
105− MOD(sum2);
106− }
107−
108− /* do remaining bytes (less than NMAX, still just one modulo) */
109− if (len) { /* avoid modulos if none remaining */
110− while (len >= 16) {
111− len -= 16;
112− DO16(buf);
113− buf += 16;
114− }
115− while (len--) {
116− adler += *buf++;
117− sum2 += adler;
118− }
119− MOD(adler);
120− MOD(sum2);
121− }
122−
123− /* return recombined sums */
124− return adler | (sum2 << 16);
125−}
126−
127−/* ========================================================================= */
128−uLong ZEXPORT adler32_combine(adler1, adler2, len2)
129− uLong adler1;
130− uLong adler2;
131− z_off_t len2;
132−{
133− unsigned long sum1;
134− unsigned long sum2;
135− unsigned rem;
136−
137− /* the derivation of this formula is left as an exercise for the reader */
138− rem = (unsigned)(len2 % BASE);
139− sum1 = adler1 & 0xffff;
140− sum2 = rem * sum1;
141− MOD(sum2);
142− sum1 += (adler2 & 0xffff) + BASE - 1;
143− sum2 += ((adler1 >> 16) & 0xffff) + ((adler2 >> 16) & 0xffff) + BASE - rem;
144− if (sum1 > BASE) sum1 -= BASE;
145− if (sum1 > BASE) sum1 -= BASE;
146− if (sum2 > (BASE << 1)) sum2 -= (BASE << 1);
147− if (sum2 > BASE) sum2 -= BASE;
148− return sum1 | (sum2 << 16);
149−}
cbits/compress.cdeleted−79
@@ -1,79 +0,0 @@
1−/* compress.c -- compress a memory buffer
2− * Copyright (C) 1995-2003 Jean-loup Gailly.
3− * For conditions of distribution and use, see copyright notice in zlib.h
4− */
5−
6−/* @(#) $Id$ */
7−
8−#define ZLIB_INTERNAL
9−#include "zlib.h"
10−
11−/* ===========================================================================
12− Compresses the source buffer into the destination buffer. The level
13− parameter has the same meaning as in deflateInit. sourceLen is the byte
14− length of the source buffer. Upon entry, destLen is the total size of the
15− destination buffer, which must be at least 0.1% larger than sourceLen plus
16− 12 bytes. Upon exit, destLen is the actual size of the compressed buffer.
17−
18− compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
19− memory, Z_BUF_ERROR if there was not enough room in the output buffer,
20− Z_STREAM_ERROR if the level parameter is invalid.
21−*/
22−int ZEXPORT compress2 (dest, destLen, source, sourceLen, level)
23− Bytef *dest;
24− uLongf *destLen;
25− const Bytef *source;
26− uLong sourceLen;
27− int level;
28−{
29− z_stream stream;
30− int err;
31−
32− stream.next_in = (Bytef*)source;
33− stream.avail_in = (uInt)sourceLen;
34−#ifdef MAXSEG_64K
35− /* Check for source > 64K on 16-bit machine: */
36− if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR;
37−#endif
38− stream.next_out = dest;
39− stream.avail_out = (uInt)*destLen;
40− if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR;
41−
42− stream.zalloc = (alloc_func)0;
43− stream.zfree = (free_func)0;
44− stream.opaque = (voidpf)0;
45−
46− err = deflateInit(&stream, level);
47− if (err != Z_OK) return err;
48−
49− err = deflate(&stream, Z_FINISH);
50− if (err != Z_STREAM_END) {
51− deflateEnd(&stream);
52− return err == Z_OK ? Z_BUF_ERROR : err;
53− }
54− *destLen = stream.total_out;
55−
56− err = deflateEnd(&stream);
57− return err;
58−}
59−
60−/* ===========================================================================
61− */
62−int ZEXPORT compress (dest, destLen, source, sourceLen)
63− Bytef *dest;
64− uLongf *destLen;
65− const Bytef *source;
66− uLong sourceLen;
67−{
68− return compress2(dest, destLen, source, sourceLen, Z_DEFAULT_COMPRESSION);
69−}
70−
71−/* ===========================================================================
72− If the default memLevel or windowBits for deflateInit() is changed, then
73− this function needs to be updated.
74− */
75−uLong ZEXPORT compressBound (sourceLen)
76− uLong sourceLen;
77−{
78− return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) + 11;
79−}
cbits/crc32.cdeleted−423
@@ -1,423 +0,0 @@
1−/* crc32.c -- compute the CRC-32 of a data stream
2− * Copyright (C) 1995-2005 Mark Adler
3− * For conditions of distribution and use, see copyright notice in zlib.h
4− *
5− * Thanks to Rodney Brown <rbrown64@csc.com.au> for his contribution of faster
6− * CRC methods: exclusive-oring 32 bits of data at a time, and pre-computing
7− * tables for updating the shift register in one step with three exclusive-ors
8− * instead of four steps with four exclusive-ors. This results in about a
9− * factor of two increase in speed on a Power PC G4 (PPC7455) using gcc -O3.
10− */
11−
12−/* @(#) $Id$ */
13−
14−/*
15− Note on the use of DYNAMIC_CRC_TABLE: there is no mutex or semaphore
16− protection on the static variables used to control the first-use generation
17− of the crc tables. Therefore, if you #define DYNAMIC_CRC_TABLE, you should
18− first call get_crc_table() to initialize the tables before allowing more than
19− one thread to use crc32().
20− */
21−
22−#ifdef MAKECRCH
23−# include <stdio.h>
24−# ifndef DYNAMIC_CRC_TABLE
25−# define DYNAMIC_CRC_TABLE
26−# endif /* !DYNAMIC_CRC_TABLE */
27−#endif /* MAKECRCH */
28−
29−#include "zutil.h" /* for STDC and FAR definitions */
30−
31−#define local static
32−
33−/* Find a four-byte integer type for crc32_little() and crc32_big(). */
34−#ifndef NOBYFOUR
35−# ifdef STDC /* need ANSI C limits.h to determine sizes */
36−# include <limits.h>
37−# define BYFOUR
38−# if (UINT_MAX == 0xffffffffUL)
39− typedef unsigned int u4;
40−# else
41−# if (ULONG_MAX == 0xffffffffUL)
42− typedef unsigned long u4;
43−# else
44−# if (USHRT_MAX == 0xffffffffUL)
45− typedef unsigned short u4;
46−# else
47−# undef BYFOUR /* can't find a four-byte integer type! */
48−# endif
49−# endif
50−# endif
51−# endif /* STDC */
52−#endif /* !NOBYFOUR */
53−
54−/* Definitions for doing the crc four data bytes at a time. */
55−#ifdef BYFOUR
56−# define REV(w) (((w)>>24)+(((w)>>8)&0xff00)+ \
57− (((w)&0xff00)<<8)+(((w)&0xff)<<24))
58− local unsigned long crc32_little OF((unsigned long,
59− const unsigned char FAR *, unsigned));
60− local unsigned long crc32_big OF((unsigned long,
61− const unsigned char FAR *, unsigned));
62−# define TBLS 8
63−#else
64−# define TBLS 1
65−#endif /* BYFOUR */
66−
67−/* Local functions for crc concatenation */
68−local unsigned long gf2_matrix_times OF((unsigned long *mat,
69− unsigned long vec));
70−local void gf2_matrix_square OF((unsigned long *square, unsigned long *mat));
71−
72−#ifdef DYNAMIC_CRC_TABLE
73−
74−local volatile int crc_table_empty = 1;
75−local unsigned long FAR crc_table[TBLS][256];
76−local void make_crc_table OF((void));
77−#ifdef MAKECRCH
78− local void write_table OF((FILE *, const unsigned long FAR *));
79−#endif /* MAKECRCH */
80−/*
81− Generate tables for a byte-wise 32-bit CRC calculation on the polynomial:
82− x^32+x^26+x^23+x^22+x^16+x^12+x^11+x^10+x^8+x^7+x^5+x^4+x^2+x+1.
83−
84− Polynomials over GF(2) are represented in binary, one bit per coefficient,
85− with the lowest powers in the most significant bit. Then adding polynomials
86− is just exclusive-or, and multiplying a polynomial by x is a right shift by
87− one. If we call the above polynomial p, and represent a byte as the
88− polynomial q, also with the lowest power in the most significant bit (so the
89− byte 0xb1 is the polynomial x^7+x^3+x+1), then the CRC is (q*x^32) mod p,
90− where a mod b means the remainder after dividing a by b.
91−
92− This calculation is done using the shift-register method of multiplying and
93− taking the remainder. The register is initialized to zero, and for each
94− incoming bit, x^32 is added mod p to the register if the bit is a one (where
95− x^32 mod p is p+x^32 = x^26+...+1), and the register is multiplied mod p by
96− x (which is shifting right by one and adding x^32 mod p if the bit shifted
97− out is a one). We start with the highest power (least significant bit) of
98− q and repeat for all eight bits of q.
99−
100− The first table is simply the CRC of all possible eight bit values. This is
101− all the information needed to generate CRCs on data a byte at a time for all
102− combinations of CRC register values and incoming bytes. The remaining tables
103− allow for word-at-a-time CRC calculation for both big-endian and little-
104− endian machines, where a word is four bytes.
105−*/
106−local void make_crc_table()
107−{
108− unsigned long c;
109− int n, k;
110− unsigned long poly; /* polynomial exclusive-or pattern */
111− /* terms of polynomial defining this crc (except x^32): */
112− static volatile int first = 1; /* flag to limit concurrent making */
113− static const unsigned char p[] = {0,1,2,4,5,7,8,10,11,12,16,22,23,26};
114−
115− /* See if another task is already doing this (not thread-safe, but better
116− than nothing -- significantly reduces duration of vulnerability in
117− case the advice about DYNAMIC_CRC_TABLE is ignored) */
118− if (first) {
119− first = 0;
120−
121− /* make exclusive-or pattern from polynomial (0xedb88320UL) */
122− poly = 0UL;
123− for (n = 0; n < sizeof(p)/sizeof(unsigned char); n++)
124− poly |= 1UL << (31 - p[n]);
125−
126− /* generate a crc for every 8-bit value */
127− for (n = 0; n < 256; n++) {
128− c = (unsigned long)n;
129− for (k = 0; k < 8; k++)
130− c = c & 1 ? poly ^ (c >> 1) : c >> 1;
131− crc_table[0][n] = c;
132− }
133−
134−#ifdef BYFOUR
135− /* generate crc for each value followed by one, two, and three zeros,
136− and then the byte reversal of those as well as the first table */
137− for (n = 0; n < 256; n++) {
138− c = crc_table[0][n];
139− crc_table[4][n] = REV(c);
140− for (k = 1; k < 4; k++) {
141− c = crc_table[0][c & 0xff] ^ (c >> 8);
142− crc_table[k][n] = c;
143− crc_table[k + 4][n] = REV(c);
144− }
145− }
146−#endif /* BYFOUR */
147−
148− crc_table_empty = 0;
149− }
150− else { /* not first */
151− /* wait for the other guy to finish (not efficient, but rare) */
152− while (crc_table_empty)
153− ;
154− }
155−
156−#ifdef MAKECRCH
157− /* write out CRC tables to crc32.h */
158− {
159− FILE *out;
160−
161− out = fopen("crc32.h", "w");
162− if (out == NULL) return;
163− fprintf(out, "/* crc32.h -- tables for rapid CRC calculation\n");
164− fprintf(out, " * Generated automatically by crc32.c\n */\n\n");
165− fprintf(out, "local const unsigned long FAR ");
166− fprintf(out, "crc_table[TBLS][256] =\n{\n {\n");
167− write_table(out, crc_table[0]);
168−# ifdef BYFOUR
169− fprintf(out, "#ifdef BYFOUR\n");
170− for (k = 1; k < 8; k++) {
171− fprintf(out, " },\n {\n");
172− write_table(out, crc_table[k]);
173− }
174− fprintf(out, "#endif\n");
175−# endif /* BYFOUR */
176− fprintf(out, " }\n};\n");
177− fclose(out);
178− }
179−#endif /* MAKECRCH */
180−}
181−
182−#ifdef MAKECRCH
183−local void write_table(out, table)
184− FILE *out;
185− const unsigned long FAR *table;
186−{
187− int n;
188−
189− for (n = 0; n < 256; n++)
190− fprintf(out, "%s0x%08lxUL%s", n % 5 ? "" : " ", table[n],
191− n == 255 ? "\n" : (n % 5 == 4 ? ",\n" : ", "));
192−}
193−#endif /* MAKECRCH */
194−
195−#else /* !DYNAMIC_CRC_TABLE */
196−/* ========================================================================
197− * Tables of CRC-32s of all single-byte values, made by make_crc_table().
198− */
199−#include "crc32.h"
200−#endif /* DYNAMIC_CRC_TABLE */
201−
202−/* =========================================================================
203− * This function can be used by asm versions of crc32()
204− */
205−const unsigned long FAR * ZEXPORT get_crc_table()
206−{
207−#ifdef DYNAMIC_CRC_TABLE
208− if (crc_table_empty)
209− make_crc_table();
210−#endif /* DYNAMIC_CRC_TABLE */
211− return (const unsigned long FAR *)crc_table;
212−}
213−
214−/* ========================================================================= */
215−#define DO1 crc = crc_table[0][((int)crc ^ (*buf++)) & 0xff] ^ (crc >> 8)
216−#define DO8 DO1; DO1; DO1; DO1; DO1; DO1; DO1; DO1
217−
218−/* ========================================================================= */
219−unsigned long ZEXPORT crc32(crc, buf, len)
220− unsigned long crc;
221− const unsigned char FAR *buf;
222− unsigned len;
223−{
224− if (buf == Z_NULL) return 0UL;
225−
226−#ifdef DYNAMIC_CRC_TABLE
227− if (crc_table_empty)
228− make_crc_table();
229−#endif /* DYNAMIC_CRC_TABLE */
230−
231−#ifdef BYFOUR
232− if (sizeof(void *) == sizeof(ptrdiff_t)) {
233− u4 endian;
234−
235− endian = 1;
236− if (*((unsigned char *)(&endian)))
237− return crc32_little(crc, buf, len);
238− else
239− return crc32_big(crc, buf, len);
240− }
241−#endif /* BYFOUR */
242− crc = crc ^ 0xffffffffUL;
243− while (len >= 8) {
244− DO8;
245− len -= 8;
246− }
247− if (len) do {
248− DO1;
249− } while (--len);
250− return crc ^ 0xffffffffUL;
251−}
252−
253−#ifdef BYFOUR
254−
255−/* ========================================================================= */
256−#define DOLIT4 c ^= *buf4++; \
257− c = crc_table[3][c & 0xff] ^ crc_table[2][(c >> 8) & 0xff] ^ \
258− crc_table[1][(c >> 16) & 0xff] ^ crc_table[0][c >> 24]
259−#define DOLIT32 DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4
260−
261−/* ========================================================================= */
262−local unsigned long crc32_little(crc, buf, len)
263− unsigned long crc;
264− const unsigned char FAR *buf;
265− unsigned len;
266−{
267− register u4 c;
268− register const u4 FAR *buf4;
269−
270− c = (u4)crc;
271− c = ~c;
272− while (len && ((ptrdiff_t)buf & 3)) {
273− c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8);
274− len--;
275− }
276−
277− buf4 = (const u4 FAR *)(const void FAR *)buf;
278− while (len >= 32) {
279− DOLIT32;
280− len -= 32;
281− }
282− while (len >= 4) {
283− DOLIT4;
284− len -= 4;
285− }
286− buf = (const unsigned char FAR *)buf4;
287−
288− if (len) do {
289− c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8);
290− } while (--len);
291− c = ~c;
292− return (unsigned long)c;
293−}
294−
295−/* ========================================================================= */
296−#define DOBIG4 c ^= *++buf4; \
297− c = crc_table[4][c & 0xff] ^ crc_table[5][(c >> 8) & 0xff] ^ \
298− crc_table[6][(c >> 16) & 0xff] ^ crc_table[7][c >> 24]
299−#define DOBIG32 DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4
300−
301−/* ========================================================================= */
302−local unsigned long crc32_big(crc, buf, len)
303− unsigned long crc;
304− const unsigned char FAR *buf;
305− unsigned len;
306−{
307− register u4 c;
308− register const u4 FAR *buf4;
309−
310− c = REV((u4)crc);
311− c = ~c;
312− while (len && ((ptrdiff_t)buf & 3)) {
313− c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8);
314− len--;
315− }
316−
317− buf4 = (const u4 FAR *)(const void FAR *)buf;
318− buf4--;
319− while (len >= 32) {
320− DOBIG32;
321− len -= 32;
322− }
323− while (len >= 4) {
324− DOBIG4;
325− len -= 4;
326− }
327− buf4++;
328− buf = (const unsigned char FAR *)buf4;
329−
330− if (len) do {
331− c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8);
332− } while (--len);
333− c = ~c;
334− return (unsigned long)(REV(c));
335−}
336−
337−#endif /* BYFOUR */
338−
339−#define GF2_DIM 32 /* dimension of GF(2) vectors (length of CRC) */
340−
341−/* ========================================================================= */
342−local unsigned long gf2_matrix_times(mat, vec)
343− unsigned long *mat;
344− unsigned long vec;
345−{
346− unsigned long sum;
347−
348− sum = 0;
349− while (vec) {
350− if (vec & 1)
351− sum ^= *mat;
352− vec >>= 1;
353− mat++;
354− }
355− return sum;
356−}
357−
358−/* ========================================================================= */
359−local void gf2_matrix_square(square, mat)
360− unsigned long *square;
361− unsigned long *mat;
362−{
363− int n;
364−
365− for (n = 0; n < GF2_DIM; n++)
366− square[n] = gf2_matrix_times(mat, mat[n]);
367−}
368−
369−/* ========================================================================= */
370−uLong ZEXPORT crc32_combine(crc1, crc2, len2)
371− uLong crc1;
372− uLong crc2;
373− z_off_t len2;
374−{
375− int n;
376− unsigned long row;
377− unsigned long even[GF2_DIM]; /* even-power-of-two zeros operator */
378− unsigned long odd[GF2_DIM]; /* odd-power-of-two zeros operator */
379−
380− /* degenerate case */
381− if (len2 == 0)
382− return crc1;
383−
384− /* put operator for one zero bit in odd */
385− odd[0] = 0xedb88320L; /* CRC-32 polynomial */
386− row = 1;
387− for (n = 1; n < GF2_DIM; n++) {
388− odd[n] = row;
389− row <<= 1;
390− }
391−
392− /* put operator for two zero bits in even */
393− gf2_matrix_square(even, odd);
394−
395− /* put operator for four zero bits in odd */
396− gf2_matrix_square(odd, even);
397−
398− /* apply len2 zeros to crc1 (first square will put the operator for one
399− zero byte, eight zero bits, in even) */
400− do {
401− /* apply zeros operator for this bit of len2 */
402− gf2_matrix_square(even, odd);
403− if (len2 & 1)
404− crc1 = gf2_matrix_times(even, crc1);
405− len2 >>= 1;
406−
407− /* if no more bits set, then done */
408− if (len2 == 0)
409− break;
410−
411− /* another iteration of the loop with odd and even swapped */
412− gf2_matrix_square(odd, even);
413− if (len2 & 1)
414− crc1 = gf2_matrix_times(odd, crc1);
415− len2 >>= 1;
416−
417− /* if no more bits set, then done */
418− } while (len2 != 0);
419−
420− /* return combined crc */
421− crc1 ^= crc2;
422− return crc1;
423−}
cbits/crc32.hdeleted−441
@@ -1,441 +0,0 @@
1−/* crc32.h -- tables for rapid CRC calculation
2− * Generated automatically by crc32.c
3− */
4−
5−local const unsigned long FAR crc_table[TBLS][256] =
6−{
7− {
8− 0x00000000UL, 0x77073096UL, 0xee0e612cUL, 0x990951baUL, 0x076dc419UL,
9− 0x706af48fUL, 0xe963a535UL, 0x9e6495a3UL, 0x0edb8832UL, 0x79dcb8a4UL,
10− 0xe0d5e91eUL, 0x97d2d988UL, 0x09b64c2bUL, 0x7eb17cbdUL, 0xe7b82d07UL,
11− 0x90bf1d91UL, 0x1db71064UL, 0x6ab020f2UL, 0xf3b97148UL, 0x84be41deUL,
12− 0x1adad47dUL, 0x6ddde4ebUL, 0xf4d4b551UL, 0x83d385c7UL, 0x136c9856UL,
13− 0x646ba8c0UL, 0xfd62f97aUL, 0x8a65c9ecUL, 0x14015c4fUL, 0x63066cd9UL,
14− 0xfa0f3d63UL, 0x8d080df5UL, 0x3b6e20c8UL, 0x4c69105eUL, 0xd56041e4UL,
15− 0xa2677172UL, 0x3c03e4d1UL, 0x4b04d447UL, 0xd20d85fdUL, 0xa50ab56bUL,
16− 0x35b5a8faUL, 0x42b2986cUL, 0xdbbbc9d6UL, 0xacbcf940UL, 0x32d86ce3UL,
17− 0x45df5c75UL, 0xdcd60dcfUL, 0xabd13d59UL, 0x26d930acUL, 0x51de003aUL,
18− 0xc8d75180UL, 0xbfd06116UL, 0x21b4f4b5UL, 0x56b3c423UL, 0xcfba9599UL,
19− 0xb8bda50fUL, 0x2802b89eUL, 0x5f058808UL, 0xc60cd9b2UL, 0xb10be924UL,
20− 0x2f6f7c87UL, 0x58684c11UL, 0xc1611dabUL, 0xb6662d3dUL, 0x76dc4190UL,
21− 0x01db7106UL, 0x98d220bcUL, 0xefd5102aUL, 0x71b18589UL, 0x06b6b51fUL,
22− 0x9fbfe4a5UL, 0xe8b8d433UL, 0x7807c9a2UL, 0x0f00f934UL, 0x9609a88eUL,
23− 0xe10e9818UL, 0x7f6a0dbbUL, 0x086d3d2dUL, 0x91646c97UL, 0xe6635c01UL,
24− 0x6b6b51f4UL, 0x1c6c6162UL, 0x856530d8UL, 0xf262004eUL, 0x6c0695edUL,
25− 0x1b01a57bUL, 0x8208f4c1UL, 0xf50fc457UL, 0x65b0d9c6UL, 0x12b7e950UL,
26− 0x8bbeb8eaUL, 0xfcb9887cUL, 0x62dd1ddfUL, 0x15da2d49UL, 0x8cd37cf3UL,
27− 0xfbd44c65UL, 0x4db26158UL, 0x3ab551ceUL, 0xa3bc0074UL, 0xd4bb30e2UL,
28− 0x4adfa541UL, 0x3dd895d7UL, 0xa4d1c46dUL, 0xd3d6f4fbUL, 0x4369e96aUL,
29− 0x346ed9fcUL, 0xad678846UL, 0xda60b8d0UL, 0x44042d73UL, 0x33031de5UL,
30− 0xaa0a4c5fUL, 0xdd0d7cc9UL, 0x5005713cUL, 0x270241aaUL, 0xbe0b1010UL,
31− 0xc90c2086UL, 0x5768b525UL, 0x206f85b3UL, 0xb966d409UL, 0xce61e49fUL,
32− 0x5edef90eUL, 0x29d9c998UL, 0xb0d09822UL, 0xc7d7a8b4UL, 0x59b33d17UL,
33− 0x2eb40d81UL, 0xb7bd5c3bUL, 0xc0ba6cadUL, 0xedb88320UL, 0x9abfb3b6UL,
34− 0x03b6e20cUL, 0x74b1d29aUL, 0xead54739UL, 0x9dd277afUL, 0x04db2615UL,
35− 0x73dc1683UL, 0xe3630b12UL, 0x94643b84UL, 0x0d6d6a3eUL, 0x7a6a5aa8UL,
36− 0xe40ecf0bUL, 0x9309ff9dUL, 0x0a00ae27UL, 0x7d079eb1UL, 0xf00f9344UL,
37− 0x8708a3d2UL, 0x1e01f268UL, 0x6906c2feUL, 0xf762575dUL, 0x806567cbUL,
38− 0x196c3671UL, 0x6e6b06e7UL, 0xfed41b76UL, 0x89d32be0UL, 0x10da7a5aUL,
39− 0x67dd4accUL, 0xf9b9df6fUL, 0x8ebeeff9UL, 0x17b7be43UL, 0x60b08ed5UL,
40− 0xd6d6a3e8UL, 0xa1d1937eUL, 0x38d8c2c4UL, 0x4fdff252UL, 0xd1bb67f1UL,
41− 0xa6bc5767UL, 0x3fb506ddUL, 0x48b2364bUL, 0xd80d2bdaUL, 0xaf0a1b4cUL,
42− 0x36034af6UL, 0x41047a60UL, 0xdf60efc3UL, 0xa867df55UL, 0x316e8eefUL,
43− 0x4669be79UL, 0xcb61b38cUL, 0xbc66831aUL, 0x256fd2a0UL, 0x5268e236UL,
44− 0xcc0c7795UL, 0xbb0b4703UL, 0x220216b9UL, 0x5505262fUL, 0xc5ba3bbeUL,
45− 0xb2bd0b28UL, 0x2bb45a92UL, 0x5cb36a04UL, 0xc2d7ffa7UL, 0xb5d0cf31UL,
46− 0x2cd99e8bUL, 0x5bdeae1dUL, 0x9b64c2b0UL, 0xec63f226UL, 0x756aa39cUL,
47− 0x026d930aUL, 0x9c0906a9UL, 0xeb0e363fUL, 0x72076785UL, 0x05005713UL,
48− 0x95bf4a82UL, 0xe2b87a14UL, 0x7bb12baeUL, 0x0cb61b38UL, 0x92d28e9bUL,
49− 0xe5d5be0dUL, 0x7cdcefb7UL, 0x0bdbdf21UL, 0x86d3d2d4UL, 0xf1d4e242UL,
50− 0x68ddb3f8UL, 0x1fda836eUL, 0x81be16cdUL, 0xf6b9265bUL, 0x6fb077e1UL,
51− 0x18b74777UL, 0x88085ae6UL, 0xff0f6a70UL, 0x66063bcaUL, 0x11010b5cUL,
52− 0x8f659effUL, 0xf862ae69UL, 0x616bffd3UL, 0x166ccf45UL, 0xa00ae278UL,
53− 0xd70dd2eeUL, 0x4e048354UL, 0x3903b3c2UL, 0xa7672661UL, 0xd06016f7UL,
54− 0x4969474dUL, 0x3e6e77dbUL, 0xaed16a4aUL, 0xd9d65adcUL, 0x40df0b66UL,
55− 0x37d83bf0UL, 0xa9bcae53UL, 0xdebb9ec5UL, 0x47b2cf7fUL, 0x30b5ffe9UL,
56− 0xbdbdf21cUL, 0xcabac28aUL, 0x53b39330UL, 0x24b4a3a6UL, 0xbad03605UL,
57− 0xcdd70693UL, 0x54de5729UL, 0x23d967bfUL, 0xb3667a2eUL, 0xc4614ab8UL,
58− 0x5d681b02UL, 0x2a6f2b94UL, 0xb40bbe37UL, 0xc30c8ea1UL, 0x5a05df1bUL,
59− 0x2d02ef8dUL
60−#ifdef BYFOUR
61− },
62− {
63− 0x00000000UL, 0x191b3141UL, 0x32366282UL, 0x2b2d53c3UL, 0x646cc504UL,
64− 0x7d77f445UL, 0x565aa786UL, 0x4f4196c7UL, 0xc8d98a08UL, 0xd1c2bb49UL,
65− 0xfaefe88aUL, 0xe3f4d9cbUL, 0xacb54f0cUL, 0xb5ae7e4dUL, 0x9e832d8eUL,
66− 0x87981ccfUL, 0x4ac21251UL, 0x53d92310UL, 0x78f470d3UL, 0x61ef4192UL,
67− 0x2eaed755UL, 0x37b5e614UL, 0x1c98b5d7UL, 0x05838496UL, 0x821b9859UL,
68− 0x9b00a918UL, 0xb02dfadbUL, 0xa936cb9aUL, 0xe6775d5dUL, 0xff6c6c1cUL,
69− 0xd4413fdfUL, 0xcd5a0e9eUL, 0x958424a2UL, 0x8c9f15e3UL, 0xa7b24620UL,
70− 0xbea97761UL, 0xf1e8e1a6UL, 0xe8f3d0e7UL, 0xc3de8324UL, 0xdac5b265UL,
71− 0x5d5daeaaUL, 0x44469febUL, 0x6f6bcc28UL, 0x7670fd69UL, 0x39316baeUL,
72− 0x202a5aefUL, 0x0b07092cUL, 0x121c386dUL, 0xdf4636f3UL, 0xc65d07b2UL,
73− 0xed705471UL, 0xf46b6530UL, 0xbb2af3f7UL, 0xa231c2b6UL, 0x891c9175UL,
74− 0x9007a034UL, 0x179fbcfbUL, 0x0e848dbaUL, 0x25a9de79UL, 0x3cb2ef38UL,
75− 0x73f379ffUL, 0x6ae848beUL, 0x41c51b7dUL, 0x58de2a3cUL, 0xf0794f05UL,
76− 0xe9627e44UL, 0xc24f2d87UL, 0xdb541cc6UL, 0x94158a01UL, 0x8d0ebb40UL,
77− 0xa623e883UL, 0xbf38d9c2UL, 0x38a0c50dUL, 0x21bbf44cUL, 0x0a96a78fUL,
78− 0x138d96ceUL, 0x5ccc0009UL, 0x45d73148UL, 0x6efa628bUL, 0x77e153caUL,
79− 0xbabb5d54UL, 0xa3a06c15UL, 0x888d3fd6UL, 0x91960e97UL, 0xded79850UL,
80− 0xc7cca911UL, 0xece1fad2UL, 0xf5facb93UL, 0x7262d75cUL, 0x6b79e61dUL,
81− 0x4054b5deUL, 0x594f849fUL, 0x160e1258UL, 0x0f152319UL, 0x243870daUL,
82− 0x3d23419bUL, 0x65fd6ba7UL, 0x7ce65ae6UL, 0x57cb0925UL, 0x4ed03864UL,
83− 0x0191aea3UL, 0x188a9fe2UL, 0x33a7cc21UL, 0x2abcfd60UL, 0xad24e1afUL,
84− 0xb43fd0eeUL, 0x9f12832dUL, 0x8609b26cUL, 0xc94824abUL, 0xd05315eaUL,
85− 0xfb7e4629UL, 0xe2657768UL, 0x2f3f79f6UL, 0x362448b7UL, 0x1d091b74UL,
86− 0x04122a35UL, 0x4b53bcf2UL, 0x52488db3UL, 0x7965de70UL, 0x607eef31UL,
87− 0xe7e6f3feUL, 0xfefdc2bfUL, 0xd5d0917cUL, 0xcccba03dUL, 0x838a36faUL,
88− 0x9a9107bbUL, 0xb1bc5478UL, 0xa8a76539UL, 0x3b83984bUL, 0x2298a90aUL,
89− 0x09b5fac9UL, 0x10aecb88UL, 0x5fef5d4fUL, 0x46f46c0eUL, 0x6dd93fcdUL,
90− 0x74c20e8cUL, 0xf35a1243UL, 0xea412302UL, 0xc16c70c1UL, 0xd8774180UL,
91− 0x9736d747UL, 0x8e2de606UL, 0xa500b5c5UL, 0xbc1b8484UL, 0x71418a1aUL,
92− 0x685abb5bUL, 0x4377e898UL, 0x5a6cd9d9UL, 0x152d4f1eUL, 0x0c367e5fUL,
93− 0x271b2d9cUL, 0x3e001cddUL, 0xb9980012UL, 0xa0833153UL, 0x8bae6290UL,
94− 0x92b553d1UL, 0xddf4c516UL, 0xc4eff457UL, 0xefc2a794UL, 0xf6d996d5UL,
95− 0xae07bce9UL, 0xb71c8da8UL, 0x9c31de6bUL, 0x852aef2aUL, 0xca6b79edUL,
96− 0xd37048acUL, 0xf85d1b6fUL, 0xe1462a2eUL, 0x66de36e1UL, 0x7fc507a0UL,
97− 0x54e85463UL, 0x4df36522UL, 0x02b2f3e5UL, 0x1ba9c2a4UL, 0x30849167UL,
98− 0x299fa026UL, 0xe4c5aeb8UL, 0xfdde9ff9UL, 0xd6f3cc3aUL, 0xcfe8fd7bUL,
99− 0x80a96bbcUL, 0x99b25afdUL, 0xb29f093eUL, 0xab84387fUL, 0x2c1c24b0UL,
100− 0x350715f1UL, 0x1e2a4632UL, 0x07317773UL, 0x4870e1b4UL, 0x516bd0f5UL,
101− 0x7a468336UL, 0x635db277UL, 0xcbfad74eUL, 0xd2e1e60fUL, 0xf9ccb5ccUL,
102− 0xe0d7848dUL, 0xaf96124aUL, 0xb68d230bUL, 0x9da070c8UL, 0x84bb4189UL,
103− 0x03235d46UL, 0x1a386c07UL, 0x31153fc4UL, 0x280e0e85UL, 0x674f9842UL,
104− 0x7e54a903UL, 0x5579fac0UL, 0x4c62cb81UL, 0x8138c51fUL, 0x9823f45eUL,
105− 0xb30ea79dUL, 0xaa1596dcUL, 0xe554001bUL, 0xfc4f315aUL, 0xd7626299UL,
106− 0xce7953d8UL, 0x49e14f17UL, 0x50fa7e56UL, 0x7bd72d95UL, 0x62cc1cd4UL,
107− 0x2d8d8a13UL, 0x3496bb52UL, 0x1fbbe891UL, 0x06a0d9d0UL, 0x5e7ef3ecUL,
108− 0x4765c2adUL, 0x6c48916eUL, 0x7553a02fUL, 0x3a1236e8UL, 0x230907a9UL,
109− 0x0824546aUL, 0x113f652bUL, 0x96a779e4UL, 0x8fbc48a5UL, 0xa4911b66UL,
110− 0xbd8a2a27UL, 0xf2cbbce0UL, 0xebd08da1UL, 0xc0fdde62UL, 0xd9e6ef23UL,
111− 0x14bce1bdUL, 0x0da7d0fcUL, 0x268a833fUL, 0x3f91b27eUL, 0x70d024b9UL,
112− 0x69cb15f8UL, 0x42e6463bUL, 0x5bfd777aUL, 0xdc656bb5UL, 0xc57e5af4UL,
113− 0xee530937UL, 0xf7483876UL, 0xb809aeb1UL, 0xa1129ff0UL, 0x8a3fcc33UL,
114− 0x9324fd72UL
115− },
116− {
117− 0x00000000UL, 0x01c26a37UL, 0x0384d46eUL, 0x0246be59UL, 0x0709a8dcUL,
118− 0x06cbc2ebUL, 0x048d7cb2UL, 0x054f1685UL, 0x0e1351b8UL, 0x0fd13b8fUL,
119− 0x0d9785d6UL, 0x0c55efe1UL, 0x091af964UL, 0x08d89353UL, 0x0a9e2d0aUL,
120− 0x0b5c473dUL, 0x1c26a370UL, 0x1de4c947UL, 0x1fa2771eUL, 0x1e601d29UL,
121− 0x1b2f0bacUL, 0x1aed619bUL, 0x18abdfc2UL, 0x1969b5f5UL, 0x1235f2c8UL,
122− 0x13f798ffUL, 0x11b126a6UL, 0x10734c91UL, 0x153c5a14UL, 0x14fe3023UL,
123− 0x16b88e7aUL, 0x177ae44dUL, 0x384d46e0UL, 0x398f2cd7UL, 0x3bc9928eUL,
124− 0x3a0bf8b9UL, 0x3f44ee3cUL, 0x3e86840bUL, 0x3cc03a52UL, 0x3d025065UL,
125− 0x365e1758UL, 0x379c7d6fUL, 0x35dac336UL, 0x3418a901UL, 0x3157bf84UL,
126− 0x3095d5b3UL, 0x32d36beaUL, 0x331101ddUL, 0x246be590UL, 0x25a98fa7UL,
127− 0x27ef31feUL, 0x262d5bc9UL, 0x23624d4cUL, 0x22a0277bUL, 0x20e69922UL,
128− 0x2124f315UL, 0x2a78b428UL, 0x2bbade1fUL, 0x29fc6046UL, 0x283e0a71UL,
129− 0x2d711cf4UL, 0x2cb376c3UL, 0x2ef5c89aUL, 0x2f37a2adUL, 0x709a8dc0UL,
130− 0x7158e7f7UL, 0x731e59aeUL, 0x72dc3399UL, 0x7793251cUL, 0x76514f2bUL,
131− 0x7417f172UL, 0x75d59b45UL, 0x7e89dc78UL, 0x7f4bb64fUL, 0x7d0d0816UL,
132− 0x7ccf6221UL, 0x798074a4UL, 0x78421e93UL, 0x7a04a0caUL, 0x7bc6cafdUL,
133− 0x6cbc2eb0UL, 0x6d7e4487UL, 0x6f38fadeUL, 0x6efa90e9UL, 0x6bb5866cUL,
134− 0x6a77ec5bUL, 0x68315202UL, 0x69f33835UL, 0x62af7f08UL, 0x636d153fUL,
135− 0x612bab66UL, 0x60e9c151UL, 0x65a6d7d4UL, 0x6464bde3UL, 0x662203baUL,
136− 0x67e0698dUL, 0x48d7cb20UL, 0x4915a117UL, 0x4b531f4eUL, 0x4a917579UL,
137− 0x4fde63fcUL, 0x4e1c09cbUL, 0x4c5ab792UL, 0x4d98dda5UL, 0x46c49a98UL,
138− 0x4706f0afUL, 0x45404ef6UL, 0x448224c1UL, 0x41cd3244UL, 0x400f5873UL,
139− 0x4249e62aUL, 0x438b8c1dUL, 0x54f16850UL, 0x55330267UL, 0x5775bc3eUL,
140− 0x56b7d609UL, 0x53f8c08cUL, 0x523aaabbUL, 0x507c14e2UL, 0x51be7ed5UL,
141− 0x5ae239e8UL, 0x5b2053dfUL, 0x5966ed86UL, 0x58a487b1UL, 0x5deb9134UL,
142− 0x5c29fb03UL, 0x5e6f455aUL, 0x5fad2f6dUL, 0xe1351b80UL, 0xe0f771b7UL,
143− 0xe2b1cfeeUL, 0xe373a5d9UL, 0xe63cb35cUL, 0xe7fed96bUL, 0xe5b86732UL,
144− 0xe47a0d05UL, 0xef264a38UL, 0xeee4200fUL, 0xeca29e56UL, 0xed60f461UL,
145− 0xe82fe2e4UL, 0xe9ed88d3UL, 0xebab368aUL, 0xea695cbdUL, 0xfd13b8f0UL,
146− 0xfcd1d2c7UL, 0xfe976c9eUL, 0xff5506a9UL, 0xfa1a102cUL, 0xfbd87a1bUL,
147− 0xf99ec442UL, 0xf85cae75UL, 0xf300e948UL, 0xf2c2837fUL, 0xf0843d26UL,
148− 0xf1465711UL, 0xf4094194UL, 0xf5cb2ba3UL, 0xf78d95faUL, 0xf64fffcdUL,
149− 0xd9785d60UL, 0xd8ba3757UL, 0xdafc890eUL, 0xdb3ee339UL, 0xde71f5bcUL,
150− 0xdfb39f8bUL, 0xddf521d2UL, 0xdc374be5UL, 0xd76b0cd8UL, 0xd6a966efUL,
151− 0xd4efd8b6UL, 0xd52db281UL, 0xd062a404UL, 0xd1a0ce33UL, 0xd3e6706aUL,
152− 0xd2241a5dUL, 0xc55efe10UL, 0xc49c9427UL, 0xc6da2a7eUL, 0xc7184049UL,
153− 0xc25756ccUL, 0xc3953cfbUL, 0xc1d382a2UL, 0xc011e895UL, 0xcb4dafa8UL,
154− 0xca8fc59fUL, 0xc8c97bc6UL, 0xc90b11f1UL, 0xcc440774UL, 0xcd866d43UL,
155− 0xcfc0d31aUL, 0xce02b92dUL, 0x91af9640UL, 0x906dfc77UL, 0x922b422eUL,
156− 0x93e92819UL, 0x96a63e9cUL, 0x976454abUL, 0x9522eaf2UL, 0x94e080c5UL,
157− 0x9fbcc7f8UL, 0x9e7eadcfUL, 0x9c381396UL, 0x9dfa79a1UL, 0x98b56f24UL,
158− 0x99770513UL, 0x9b31bb4aUL, 0x9af3d17dUL, 0x8d893530UL, 0x8c4b5f07UL,
159− 0x8e0de15eUL, 0x8fcf8b69UL, 0x8a809decUL, 0x8b42f7dbUL, 0x89044982UL,
160− 0x88c623b5UL, 0x839a6488UL, 0x82580ebfUL, 0x801eb0e6UL, 0x81dcdad1UL,
161− 0x8493cc54UL, 0x8551a663UL, 0x8717183aUL, 0x86d5720dUL, 0xa9e2d0a0UL,
162− 0xa820ba97UL, 0xaa6604ceUL, 0xaba46ef9UL, 0xaeeb787cUL, 0xaf29124bUL,
163− 0xad6fac12UL, 0xacadc625UL, 0xa7f18118UL, 0xa633eb2fUL, 0xa4755576UL,
164− 0xa5b73f41UL, 0xa0f829c4UL, 0xa13a43f3UL, 0xa37cfdaaUL, 0xa2be979dUL,
165− 0xb5c473d0UL, 0xb40619e7UL, 0xb640a7beUL, 0xb782cd89UL, 0xb2cddb0cUL,
166− 0xb30fb13bUL, 0xb1490f62UL, 0xb08b6555UL, 0xbbd72268UL, 0xba15485fUL,
167− 0xb853f606UL, 0xb9919c31UL, 0xbcde8ab4UL, 0xbd1ce083UL, 0xbf5a5edaUL,
168− 0xbe9834edUL
169− },
170− {
171− 0x00000000UL, 0xb8bc6765UL, 0xaa09c88bUL, 0x12b5afeeUL, 0x8f629757UL,
172− 0x37def032UL, 0x256b5fdcUL, 0x9dd738b9UL, 0xc5b428efUL, 0x7d084f8aUL,
173− 0x6fbde064UL, 0xd7018701UL, 0x4ad6bfb8UL, 0xf26ad8ddUL, 0xe0df7733UL,
174− 0x58631056UL, 0x5019579fUL, 0xe8a530faUL, 0xfa109f14UL, 0x42acf871UL,
175− 0xdf7bc0c8UL, 0x67c7a7adUL, 0x75720843UL, 0xcdce6f26UL, 0x95ad7f70UL,
176− 0x2d111815UL, 0x3fa4b7fbUL, 0x8718d09eUL, 0x1acfe827UL, 0xa2738f42UL,
177− 0xb0c620acUL, 0x087a47c9UL, 0xa032af3eUL, 0x188ec85bUL, 0x0a3b67b5UL,
178− 0xb28700d0UL, 0x2f503869UL, 0x97ec5f0cUL, 0x8559f0e2UL, 0x3de59787UL,
179− 0x658687d1UL, 0xdd3ae0b4UL, 0xcf8f4f5aUL, 0x7733283fUL, 0xeae41086UL,
180− 0x525877e3UL, 0x40edd80dUL, 0xf851bf68UL, 0xf02bf8a1UL, 0x48979fc4UL,
181− 0x5a22302aUL, 0xe29e574fUL, 0x7f496ff6UL, 0xc7f50893UL, 0xd540a77dUL,
182− 0x6dfcc018UL, 0x359fd04eUL, 0x8d23b72bUL, 0x9f9618c5UL, 0x272a7fa0UL,
183− 0xbafd4719UL, 0x0241207cUL, 0x10f48f92UL, 0xa848e8f7UL, 0x9b14583dUL,
184− 0x23a83f58UL, 0x311d90b6UL, 0x89a1f7d3UL, 0x1476cf6aUL, 0xaccaa80fUL,
185− 0xbe7f07e1UL, 0x06c36084UL, 0x5ea070d2UL, 0xe61c17b7UL, 0xf4a9b859UL,
186− 0x4c15df3cUL, 0xd1c2e785UL, 0x697e80e0UL, 0x7bcb2f0eUL, 0xc377486bUL,
187− 0xcb0d0fa2UL, 0x73b168c7UL, 0x6104c729UL, 0xd9b8a04cUL, 0x446f98f5UL,
188− 0xfcd3ff90UL, 0xee66507eUL, 0x56da371bUL, 0x0eb9274dUL, 0xb6054028UL,
189− 0xa4b0efc6UL, 0x1c0c88a3UL, 0x81dbb01aUL, 0x3967d77fUL, 0x2bd27891UL,
190− 0x936e1ff4UL, 0x3b26f703UL, 0x839a9066UL, 0x912f3f88UL, 0x299358edUL,
191− 0xb4446054UL, 0x0cf80731UL, 0x1e4da8dfUL, 0xa6f1cfbaUL, 0xfe92dfecUL,
192− 0x462eb889UL, 0x549b1767UL, 0xec277002UL, 0x71f048bbUL, 0xc94c2fdeUL,
193− 0xdbf98030UL, 0x6345e755UL, 0x6b3fa09cUL, 0xd383c7f9UL, 0xc1366817UL,
194− 0x798a0f72UL, 0xe45d37cbUL, 0x5ce150aeUL, 0x4e54ff40UL, 0xf6e89825UL,
195− 0xae8b8873UL, 0x1637ef16UL, 0x048240f8UL, 0xbc3e279dUL, 0x21e91f24UL,
196− 0x99557841UL, 0x8be0d7afUL, 0x335cb0caUL, 0xed59b63bUL, 0x55e5d15eUL,
197− 0x47507eb0UL, 0xffec19d5UL, 0x623b216cUL, 0xda874609UL, 0xc832e9e7UL,
198− 0x708e8e82UL, 0x28ed9ed4UL, 0x9051f9b1UL, 0x82e4565fUL, 0x3a58313aUL,
199− 0xa78f0983UL, 0x1f336ee6UL, 0x0d86c108UL, 0xb53aa66dUL, 0xbd40e1a4UL,
200− 0x05fc86c1UL, 0x1749292fUL, 0xaff54e4aUL, 0x322276f3UL, 0x8a9e1196UL,
201− 0x982bbe78UL, 0x2097d91dUL, 0x78f4c94bUL, 0xc048ae2eUL, 0xd2fd01c0UL,
202− 0x6a4166a5UL, 0xf7965e1cUL, 0x4f2a3979UL, 0x5d9f9697UL, 0xe523f1f2UL,
203− 0x4d6b1905UL, 0xf5d77e60UL, 0xe762d18eUL, 0x5fdeb6ebUL, 0xc2098e52UL,
204− 0x7ab5e937UL, 0x680046d9UL, 0xd0bc21bcUL, 0x88df31eaUL, 0x3063568fUL,
205− 0x22d6f961UL, 0x9a6a9e04UL, 0x07bda6bdUL, 0xbf01c1d8UL, 0xadb46e36UL,
206− 0x15080953UL, 0x1d724e9aUL, 0xa5ce29ffUL, 0xb77b8611UL, 0x0fc7e174UL,
207− 0x9210d9cdUL, 0x2aacbea8UL, 0x38191146UL, 0x80a57623UL, 0xd8c66675UL,
208− 0x607a0110UL, 0x72cfaefeUL, 0xca73c99bUL, 0x57a4f122UL, 0xef189647UL,
209− 0xfdad39a9UL, 0x45115eccUL, 0x764dee06UL, 0xcef18963UL, 0xdc44268dUL,
210− 0x64f841e8UL, 0xf92f7951UL, 0x41931e34UL, 0x5326b1daUL, 0xeb9ad6bfUL,
211− 0xb3f9c6e9UL, 0x0b45a18cUL, 0x19f00e62UL, 0xa14c6907UL, 0x3c9b51beUL,
212− 0x842736dbUL, 0x96929935UL, 0x2e2efe50UL, 0x2654b999UL, 0x9ee8defcUL,
213− 0x8c5d7112UL, 0x34e11677UL, 0xa9362eceUL, 0x118a49abUL, 0x033fe645UL,
214− 0xbb838120UL, 0xe3e09176UL, 0x5b5cf613UL, 0x49e959fdUL, 0xf1553e98UL,
215− 0x6c820621UL, 0xd43e6144UL, 0xc68bceaaUL, 0x7e37a9cfUL, 0xd67f4138UL,
216− 0x6ec3265dUL, 0x7c7689b3UL, 0xc4caeed6UL, 0x591dd66fUL, 0xe1a1b10aUL,
217− 0xf3141ee4UL, 0x4ba87981UL, 0x13cb69d7UL, 0xab770eb2UL, 0xb9c2a15cUL,
218− 0x017ec639UL, 0x9ca9fe80UL, 0x241599e5UL, 0x36a0360bUL, 0x8e1c516eUL,
219− 0x866616a7UL, 0x3eda71c2UL, 0x2c6fde2cUL, 0x94d3b949UL, 0x090481f0UL,
220− 0xb1b8e695UL, 0xa30d497bUL, 0x1bb12e1eUL, 0x43d23e48UL, 0xfb6e592dUL,
221− 0xe9dbf6c3UL, 0x516791a6UL, 0xccb0a91fUL, 0x740cce7aUL, 0x66b96194UL,
222− 0xde0506f1UL
223− },
224− {
225− 0x00000000UL, 0x96300777UL, 0x2c610eeeUL, 0xba510999UL, 0x19c46d07UL,
226− 0x8ff46a70UL, 0x35a563e9UL, 0xa395649eUL, 0x3288db0eUL, 0xa4b8dc79UL,
227− 0x1ee9d5e0UL, 0x88d9d297UL, 0x2b4cb609UL, 0xbd7cb17eUL, 0x072db8e7UL,
228− 0x911dbf90UL, 0x6410b71dUL, 0xf220b06aUL, 0x4871b9f3UL, 0xde41be84UL,
229− 0x7dd4da1aUL, 0xebe4dd6dUL, 0x51b5d4f4UL, 0xc785d383UL, 0x56986c13UL,
230− 0xc0a86b64UL, 0x7af962fdUL, 0xecc9658aUL, 0x4f5c0114UL, 0xd96c0663UL,
231− 0x633d0ffaUL, 0xf50d088dUL, 0xc8206e3bUL, 0x5e10694cUL, 0xe44160d5UL,
232− 0x727167a2UL, 0xd1e4033cUL, 0x47d4044bUL, 0xfd850dd2UL, 0x6bb50aa5UL,
233− 0xfaa8b535UL, 0x6c98b242UL, 0xd6c9bbdbUL, 0x40f9bcacUL, 0xe36cd832UL,
234− 0x755cdf45UL, 0xcf0dd6dcUL, 0x593dd1abUL, 0xac30d926UL, 0x3a00de51UL,
235− 0x8051d7c8UL, 0x1661d0bfUL, 0xb5f4b421UL, 0x23c4b356UL, 0x9995bacfUL,
236− 0x0fa5bdb8UL, 0x9eb80228UL, 0x0888055fUL, 0xb2d90cc6UL, 0x24e90bb1UL,
237− 0x877c6f2fUL, 0x114c6858UL, 0xab1d61c1UL, 0x3d2d66b6UL, 0x9041dc76UL,
238− 0x0671db01UL, 0xbc20d298UL, 0x2a10d5efUL, 0x8985b171UL, 0x1fb5b606UL,
239− 0xa5e4bf9fUL, 0x33d4b8e8UL, 0xa2c90778UL, 0x34f9000fUL, 0x8ea80996UL,
240− 0x18980ee1UL, 0xbb0d6a7fUL, 0x2d3d6d08UL, 0x976c6491UL, 0x015c63e6UL,
241− 0xf4516b6bUL, 0x62616c1cUL, 0xd8306585UL, 0x4e0062f2UL, 0xed95066cUL,
242− 0x7ba5011bUL, 0xc1f40882UL, 0x57c40ff5UL, 0xc6d9b065UL, 0x50e9b712UL,
243− 0xeab8be8bUL, 0x7c88b9fcUL, 0xdf1ddd62UL, 0x492dda15UL, 0xf37cd38cUL,
244− 0x654cd4fbUL, 0x5861b24dUL, 0xce51b53aUL, 0x7400bca3UL, 0xe230bbd4UL,
245− 0x41a5df4aUL, 0xd795d83dUL, 0x6dc4d1a4UL, 0xfbf4d6d3UL, 0x6ae96943UL,
246− 0xfcd96e34UL, 0x468867adUL, 0xd0b860daUL, 0x732d0444UL, 0xe51d0333UL,
247− 0x5f4c0aaaUL, 0xc97c0dddUL, 0x3c710550UL, 0xaa410227UL, 0x10100bbeUL,
248− 0x86200cc9UL, 0x25b56857UL, 0xb3856f20UL, 0x09d466b9UL, 0x9fe461ceUL,
249− 0x0ef9de5eUL, 0x98c9d929UL, 0x2298d0b0UL, 0xb4a8d7c7UL, 0x173db359UL,
250− 0x810db42eUL, 0x3b5cbdb7UL, 0xad6cbac0UL, 0x2083b8edUL, 0xb6b3bf9aUL,
251− 0x0ce2b603UL, 0x9ad2b174UL, 0x3947d5eaUL, 0xaf77d29dUL, 0x1526db04UL,
252− 0x8316dc73UL, 0x120b63e3UL, 0x843b6494UL, 0x3e6a6d0dUL, 0xa85a6a7aUL,
253− 0x0bcf0ee4UL, 0x9dff0993UL, 0x27ae000aUL, 0xb19e077dUL, 0x44930ff0UL,
254− 0xd2a30887UL, 0x68f2011eUL, 0xfec20669UL, 0x5d5762f7UL, 0xcb676580UL,
255− 0x71366c19UL, 0xe7066b6eUL, 0x761bd4feUL, 0xe02bd389UL, 0x5a7ada10UL,
256− 0xcc4add67UL, 0x6fdfb9f9UL, 0xf9efbe8eUL, 0x43beb717UL, 0xd58eb060UL,
257− 0xe8a3d6d6UL, 0x7e93d1a1UL, 0xc4c2d838UL, 0x52f2df4fUL, 0xf167bbd1UL,
258− 0x6757bca6UL, 0xdd06b53fUL, 0x4b36b248UL, 0xda2b0dd8UL, 0x4c1b0aafUL,
259− 0xf64a0336UL, 0x607a0441UL, 0xc3ef60dfUL, 0x55df67a8UL, 0xef8e6e31UL,
260− 0x79be6946UL, 0x8cb361cbUL, 0x1a8366bcUL, 0xa0d26f25UL, 0x36e26852UL,
261− 0x95770cccUL, 0x03470bbbUL, 0xb9160222UL, 0x2f260555UL, 0xbe3bbac5UL,
262− 0x280bbdb2UL, 0x925ab42bUL, 0x046ab35cUL, 0xa7ffd7c2UL, 0x31cfd0b5UL,
263− 0x8b9ed92cUL, 0x1daede5bUL, 0xb0c2649bUL, 0x26f263ecUL, 0x9ca36a75UL,
264− 0x0a936d02UL, 0xa906099cUL, 0x3f360eebUL, 0x85670772UL, 0x13570005UL,
265− 0x824abf95UL, 0x147ab8e2UL, 0xae2bb17bUL, 0x381bb60cUL, 0x9b8ed292UL,
266− 0x0dbed5e5UL, 0xb7efdc7cUL, 0x21dfdb0bUL, 0xd4d2d386UL, 0x42e2d4f1UL,
267− 0xf8b3dd68UL, 0x6e83da1fUL, 0xcd16be81UL, 0x5b26b9f6UL, 0xe177b06fUL,
268− 0x7747b718UL, 0xe65a0888UL, 0x706a0fffUL, 0xca3b0666UL, 0x5c0b0111UL,
269− 0xff9e658fUL, 0x69ae62f8UL, 0xd3ff6b61UL, 0x45cf6c16UL, 0x78e20aa0UL,
270− 0xeed20dd7UL, 0x5483044eUL, 0xc2b30339UL, 0x612667a7UL, 0xf71660d0UL,
271− 0x4d476949UL, 0xdb776e3eUL, 0x4a6ad1aeUL, 0xdc5ad6d9UL, 0x660bdf40UL,
272− 0xf03bd837UL, 0x53aebca9UL, 0xc59ebbdeUL, 0x7fcfb247UL, 0xe9ffb530UL,
273− 0x1cf2bdbdUL, 0x8ac2bacaUL, 0x3093b353UL, 0xa6a3b424UL, 0x0536d0baUL,
274− 0x9306d7cdUL, 0x2957de54UL, 0xbf67d923UL, 0x2e7a66b3UL, 0xb84a61c4UL,
275− 0x021b685dUL, 0x942b6f2aUL, 0x37be0bb4UL, 0xa18e0cc3UL, 0x1bdf055aUL,
276− 0x8def022dUL
277− },
278− {
279− 0x00000000UL, 0x41311b19UL, 0x82623632UL, 0xc3532d2bUL, 0x04c56c64UL,
280− 0x45f4777dUL, 0x86a75a56UL, 0xc796414fUL, 0x088ad9c8UL, 0x49bbc2d1UL,
281− 0x8ae8effaUL, 0xcbd9f4e3UL, 0x0c4fb5acUL, 0x4d7eaeb5UL, 0x8e2d839eUL,
282− 0xcf1c9887UL, 0x5112c24aUL, 0x1023d953UL, 0xd370f478UL, 0x9241ef61UL,
283− 0x55d7ae2eUL, 0x14e6b537UL, 0xd7b5981cUL, 0x96848305UL, 0x59981b82UL,
284− 0x18a9009bUL, 0xdbfa2db0UL, 0x9acb36a9UL, 0x5d5d77e6UL, 0x1c6c6cffUL,
285− 0xdf3f41d4UL, 0x9e0e5acdUL, 0xa2248495UL, 0xe3159f8cUL, 0x2046b2a7UL,
286− 0x6177a9beUL, 0xa6e1e8f1UL, 0xe7d0f3e8UL, 0x2483dec3UL, 0x65b2c5daUL,
287− 0xaaae5d5dUL, 0xeb9f4644UL, 0x28cc6b6fUL, 0x69fd7076UL, 0xae6b3139UL,
288− 0xef5a2a20UL, 0x2c09070bUL, 0x6d381c12UL, 0xf33646dfUL, 0xb2075dc6UL,
289− 0x715470edUL, 0x30656bf4UL, 0xf7f32abbUL, 0xb6c231a2UL, 0x75911c89UL,
290− 0x34a00790UL, 0xfbbc9f17UL, 0xba8d840eUL, 0x79dea925UL, 0x38efb23cUL,
291− 0xff79f373UL, 0xbe48e86aUL, 0x7d1bc541UL, 0x3c2ade58UL, 0x054f79f0UL,
292− 0x447e62e9UL, 0x872d4fc2UL, 0xc61c54dbUL, 0x018a1594UL, 0x40bb0e8dUL,
293− 0x83e823a6UL, 0xc2d938bfUL, 0x0dc5a038UL, 0x4cf4bb21UL, 0x8fa7960aUL,
294− 0xce968d13UL, 0x0900cc5cUL, 0x4831d745UL, 0x8b62fa6eUL, 0xca53e177UL,
295− 0x545dbbbaUL, 0x156ca0a3UL, 0xd63f8d88UL, 0x970e9691UL, 0x5098d7deUL,
296− 0x11a9ccc7UL, 0xd2fae1ecUL, 0x93cbfaf5UL, 0x5cd76272UL, 0x1de6796bUL,
297− 0xdeb55440UL, 0x9f844f59UL, 0x58120e16UL, 0x1923150fUL, 0xda703824UL,
298− 0x9b41233dUL, 0xa76bfd65UL, 0xe65ae67cUL, 0x2509cb57UL, 0x6438d04eUL,
299− 0xa3ae9101UL, 0xe29f8a18UL, 0x21cca733UL, 0x60fdbc2aUL, 0xafe124adUL,
300− 0xeed03fb4UL, 0x2d83129fUL, 0x6cb20986UL, 0xab2448c9UL, 0xea1553d0UL,
301− 0x29467efbUL, 0x687765e2UL, 0xf6793f2fUL, 0xb7482436UL, 0x741b091dUL,
302− 0x352a1204UL, 0xf2bc534bUL, 0xb38d4852UL, 0x70de6579UL, 0x31ef7e60UL,
303− 0xfef3e6e7UL, 0xbfc2fdfeUL, 0x7c91d0d5UL, 0x3da0cbccUL, 0xfa368a83UL,
304− 0xbb07919aUL, 0x7854bcb1UL, 0x3965a7a8UL, 0x4b98833bUL, 0x0aa99822UL,
305− 0xc9fab509UL, 0x88cbae10UL, 0x4f5def5fUL, 0x0e6cf446UL, 0xcd3fd96dUL,
306− 0x8c0ec274UL, 0x43125af3UL, 0x022341eaUL, 0xc1706cc1UL, 0x804177d8UL,
307− 0x47d73697UL, 0x06e62d8eUL, 0xc5b500a5UL, 0x84841bbcUL, 0x1a8a4171UL,
308− 0x5bbb5a68UL, 0x98e87743UL, 0xd9d96c5aUL, 0x1e4f2d15UL, 0x5f7e360cUL,
309− 0x9c2d1b27UL, 0xdd1c003eUL, 0x120098b9UL, 0x533183a0UL, 0x9062ae8bUL,
310− 0xd153b592UL, 0x16c5f4ddUL, 0x57f4efc4UL, 0x94a7c2efUL, 0xd596d9f6UL,
311− 0xe9bc07aeUL, 0xa88d1cb7UL, 0x6bde319cUL, 0x2aef2a85UL, 0xed796bcaUL,
312− 0xac4870d3UL, 0x6f1b5df8UL, 0x2e2a46e1UL, 0xe136de66UL, 0xa007c57fUL,
313− 0x6354e854UL, 0x2265f34dUL, 0xe5f3b202UL, 0xa4c2a91bUL, 0x67918430UL,
314− 0x26a09f29UL, 0xb8aec5e4UL, 0xf99fdefdUL, 0x3accf3d6UL, 0x7bfde8cfUL,
315− 0xbc6ba980UL, 0xfd5ab299UL, 0x3e099fb2UL, 0x7f3884abUL, 0xb0241c2cUL,
316− 0xf1150735UL, 0x32462a1eUL, 0x73773107UL, 0xb4e17048UL, 0xf5d06b51UL,
317− 0x3683467aUL, 0x77b25d63UL, 0x4ed7facbUL, 0x0fe6e1d2UL, 0xccb5ccf9UL,
318− 0x8d84d7e0UL, 0x4a1296afUL, 0x0b238db6UL, 0xc870a09dUL, 0x8941bb84UL,
319− 0x465d2303UL, 0x076c381aUL, 0xc43f1531UL, 0x850e0e28UL, 0x42984f67UL,
320− 0x03a9547eUL, 0xc0fa7955UL, 0x81cb624cUL, 0x1fc53881UL, 0x5ef42398UL,
321− 0x9da70eb3UL, 0xdc9615aaUL, 0x1b0054e5UL, 0x5a314ffcUL, 0x996262d7UL,
322− 0xd85379ceUL, 0x174fe149UL, 0x567efa50UL, 0x952dd77bUL, 0xd41ccc62UL,
323− 0x138a8d2dUL, 0x52bb9634UL, 0x91e8bb1fUL, 0xd0d9a006UL, 0xecf37e5eUL,
324− 0xadc26547UL, 0x6e91486cUL, 0x2fa05375UL, 0xe836123aUL, 0xa9070923UL,
325− 0x6a542408UL, 0x2b653f11UL, 0xe479a796UL, 0xa548bc8fUL, 0x661b91a4UL,
326− 0x272a8abdUL, 0xe0bccbf2UL, 0xa18dd0ebUL, 0x62defdc0UL, 0x23efe6d9UL,
327− 0xbde1bc14UL, 0xfcd0a70dUL, 0x3f838a26UL, 0x7eb2913fUL, 0xb924d070UL,
328− 0xf815cb69UL, 0x3b46e642UL, 0x7a77fd5bUL, 0xb56b65dcUL, 0xf45a7ec5UL,
329− 0x370953eeUL, 0x763848f7UL, 0xb1ae09b8UL, 0xf09f12a1UL, 0x33cc3f8aUL,
330− 0x72fd2493UL
331− },
332− {
333− 0x00000000UL, 0x376ac201UL, 0x6ed48403UL, 0x59be4602UL, 0xdca80907UL,
334− 0xebc2cb06UL, 0xb27c8d04UL, 0x85164f05UL, 0xb851130eUL, 0x8f3bd10fUL,
335− 0xd685970dUL, 0xe1ef550cUL, 0x64f91a09UL, 0x5393d808UL, 0x0a2d9e0aUL,
336− 0x3d475c0bUL, 0x70a3261cUL, 0x47c9e41dUL, 0x1e77a21fUL, 0x291d601eUL,
337− 0xac0b2f1bUL, 0x9b61ed1aUL, 0xc2dfab18UL, 0xf5b56919UL, 0xc8f23512UL,
338− 0xff98f713UL, 0xa626b111UL, 0x914c7310UL, 0x145a3c15UL, 0x2330fe14UL,
339− 0x7a8eb816UL, 0x4de47a17UL, 0xe0464d38UL, 0xd72c8f39UL, 0x8e92c93bUL,
340− 0xb9f80b3aUL, 0x3cee443fUL, 0x0b84863eUL, 0x523ac03cUL, 0x6550023dUL,
341− 0x58175e36UL, 0x6f7d9c37UL, 0x36c3da35UL, 0x01a91834UL, 0x84bf5731UL,
342− 0xb3d59530UL, 0xea6bd332UL, 0xdd011133UL, 0x90e56b24UL, 0xa78fa925UL,
343− 0xfe31ef27UL, 0xc95b2d26UL, 0x4c4d6223UL, 0x7b27a022UL, 0x2299e620UL,
344− 0x15f32421UL, 0x28b4782aUL, 0x1fdeba2bUL, 0x4660fc29UL, 0x710a3e28UL,
345− 0xf41c712dUL, 0xc376b32cUL, 0x9ac8f52eUL, 0xada2372fUL, 0xc08d9a70UL,
346− 0xf7e75871UL, 0xae591e73UL, 0x9933dc72UL, 0x1c259377UL, 0x2b4f5176UL,
347− 0x72f11774UL, 0x459bd575UL, 0x78dc897eUL, 0x4fb64b7fUL, 0x16080d7dUL,
348− 0x2162cf7cUL, 0xa4748079UL, 0x931e4278UL, 0xcaa0047aUL, 0xfdcac67bUL,
349− 0xb02ebc6cUL, 0x87447e6dUL, 0xdefa386fUL, 0xe990fa6eUL, 0x6c86b56bUL,
350− 0x5bec776aUL, 0x02523168UL, 0x3538f369UL, 0x087faf62UL, 0x3f156d63UL,
351− 0x66ab2b61UL, 0x51c1e960UL, 0xd4d7a665UL, 0xe3bd6464UL, 0xba032266UL,
352− 0x8d69e067UL, 0x20cbd748UL, 0x17a11549UL, 0x4e1f534bUL, 0x7975914aUL,
353− 0xfc63de4fUL, 0xcb091c4eUL, 0x92b75a4cUL, 0xa5dd984dUL, 0x989ac446UL,
354− 0xaff00647UL, 0xf64e4045UL, 0xc1248244UL, 0x4432cd41UL, 0x73580f40UL,
355− 0x2ae64942UL, 0x1d8c8b43UL, 0x5068f154UL, 0x67023355UL, 0x3ebc7557UL,
356− 0x09d6b756UL, 0x8cc0f853UL, 0xbbaa3a52UL, 0xe2147c50UL, 0xd57ebe51UL,
357− 0xe839e25aUL, 0xdf53205bUL, 0x86ed6659UL, 0xb187a458UL, 0x3491eb5dUL,
358− 0x03fb295cUL, 0x5a456f5eUL, 0x6d2fad5fUL, 0x801b35e1UL, 0xb771f7e0UL,
359− 0xeecfb1e2UL, 0xd9a573e3UL, 0x5cb33ce6UL, 0x6bd9fee7UL, 0x3267b8e5UL,
360− 0x050d7ae4UL, 0x384a26efUL, 0x0f20e4eeUL, 0x569ea2ecUL, 0x61f460edUL,
361− 0xe4e22fe8UL, 0xd388ede9UL, 0x8a36abebUL, 0xbd5c69eaUL, 0xf0b813fdUL,
362− 0xc7d2d1fcUL, 0x9e6c97feUL, 0xa90655ffUL, 0x2c101afaUL, 0x1b7ad8fbUL,
363− 0x42c49ef9UL, 0x75ae5cf8UL, 0x48e900f3UL, 0x7f83c2f2UL, 0x263d84f0UL,
364− 0x115746f1UL, 0x944109f4UL, 0xa32bcbf5UL, 0xfa958df7UL, 0xcdff4ff6UL,
365− 0x605d78d9UL, 0x5737bad8UL, 0x0e89fcdaUL, 0x39e33edbUL, 0xbcf571deUL,
366− 0x8b9fb3dfUL, 0xd221f5ddUL, 0xe54b37dcUL, 0xd80c6bd7UL, 0xef66a9d6UL,
367− 0xb6d8efd4UL, 0x81b22dd5UL, 0x04a462d0UL, 0x33cea0d1UL, 0x6a70e6d3UL,
368− 0x5d1a24d2UL, 0x10fe5ec5UL, 0x27949cc4UL, 0x7e2adac6UL, 0x494018c7UL,
369− 0xcc5657c2UL, 0xfb3c95c3UL, 0xa282d3c1UL, 0x95e811c0UL, 0xa8af4dcbUL,
370− 0x9fc58fcaUL, 0xc67bc9c8UL, 0xf1110bc9UL, 0x740744ccUL, 0x436d86cdUL,
371− 0x1ad3c0cfUL, 0x2db902ceUL, 0x4096af91UL, 0x77fc6d90UL, 0x2e422b92UL,
372− 0x1928e993UL, 0x9c3ea696UL, 0xab546497UL, 0xf2ea2295UL, 0xc580e094UL,
373− 0xf8c7bc9fUL, 0xcfad7e9eUL, 0x9613389cUL, 0xa179fa9dUL, 0x246fb598UL,
374− 0x13057799UL, 0x4abb319bUL, 0x7dd1f39aUL, 0x3035898dUL, 0x075f4b8cUL,
375− 0x5ee10d8eUL, 0x698bcf8fUL, 0xec9d808aUL, 0xdbf7428bUL, 0x82490489UL,
376− 0xb523c688UL, 0x88649a83UL, 0xbf0e5882UL, 0xe6b01e80UL, 0xd1dadc81UL,
377− 0x54cc9384UL, 0x63a65185UL, 0x3a181787UL, 0x0d72d586UL, 0xa0d0e2a9UL,
378− 0x97ba20a8UL, 0xce0466aaUL, 0xf96ea4abUL, 0x7c78ebaeUL, 0x4b1229afUL,
379− 0x12ac6fadUL, 0x25c6adacUL, 0x1881f1a7UL, 0x2feb33a6UL, 0x765575a4UL,
380− 0x413fb7a5UL, 0xc429f8a0UL, 0xf3433aa1UL, 0xaafd7ca3UL, 0x9d97bea2UL,
381− 0xd073c4b5UL, 0xe71906b4UL, 0xbea740b6UL, 0x89cd82b7UL, 0x0cdbcdb2UL,
382− 0x3bb10fb3UL, 0x620f49b1UL, 0x55658bb0UL, 0x6822d7bbUL, 0x5f4815baUL,
383− 0x06f653b8UL, 0x319c91b9UL, 0xb48adebcUL, 0x83e01cbdUL, 0xda5e5abfUL,
384− 0xed3498beUL
385− },
386− {
387− 0x00000000UL, 0x6567bcb8UL, 0x8bc809aaUL, 0xeeafb512UL, 0x5797628fUL,
388− 0x32f0de37UL, 0xdc5f6b25UL, 0xb938d79dUL, 0xef28b4c5UL, 0x8a4f087dUL,
389− 0x64e0bd6fUL, 0x018701d7UL, 0xb8bfd64aUL, 0xddd86af2UL, 0x3377dfe0UL,
390− 0x56106358UL, 0x9f571950UL, 0xfa30a5e8UL, 0x149f10faUL, 0x71f8ac42UL,
391− 0xc8c07bdfUL, 0xada7c767UL, 0x43087275UL, 0x266fcecdUL, 0x707fad95UL,
392− 0x1518112dUL, 0xfbb7a43fUL, 0x9ed01887UL, 0x27e8cf1aUL, 0x428f73a2UL,
393− 0xac20c6b0UL, 0xc9477a08UL, 0x3eaf32a0UL, 0x5bc88e18UL, 0xb5673b0aUL,
394− 0xd00087b2UL, 0x6938502fUL, 0x0c5fec97UL, 0xe2f05985UL, 0x8797e53dUL,
395− 0xd1878665UL, 0xb4e03addUL, 0x5a4f8fcfUL, 0x3f283377UL, 0x8610e4eaUL,
396− 0xe3775852UL, 0x0dd8ed40UL, 0x68bf51f8UL, 0xa1f82bf0UL, 0xc49f9748UL,
397− 0x2a30225aUL, 0x4f579ee2UL, 0xf66f497fUL, 0x9308f5c7UL, 0x7da740d5UL,
398− 0x18c0fc6dUL, 0x4ed09f35UL, 0x2bb7238dUL, 0xc518969fUL, 0xa07f2a27UL,
399− 0x1947fdbaUL, 0x7c204102UL, 0x928ff410UL, 0xf7e848a8UL, 0x3d58149bUL,
400− 0x583fa823UL, 0xb6901d31UL, 0xd3f7a189UL, 0x6acf7614UL, 0x0fa8caacUL,
401− 0xe1077fbeUL, 0x8460c306UL, 0xd270a05eUL, 0xb7171ce6UL, 0x59b8a9f4UL,
402− 0x3cdf154cUL, 0x85e7c2d1UL, 0xe0807e69UL, 0x0e2fcb7bUL, 0x6b4877c3UL,
403− 0xa20f0dcbUL, 0xc768b173UL, 0x29c70461UL, 0x4ca0b8d9UL, 0xf5986f44UL,
404− 0x90ffd3fcUL, 0x7e5066eeUL, 0x1b37da56UL, 0x4d27b90eUL, 0x284005b6UL,
405− 0xc6efb0a4UL, 0xa3880c1cUL, 0x1ab0db81UL, 0x7fd76739UL, 0x9178d22bUL,
406− 0xf41f6e93UL, 0x03f7263bUL, 0x66909a83UL, 0x883f2f91UL, 0xed589329UL,
407− 0x546044b4UL, 0x3107f80cUL, 0xdfa84d1eUL, 0xbacff1a6UL, 0xecdf92feUL,
408− 0x89b82e46UL, 0x67179b54UL, 0x027027ecUL, 0xbb48f071UL, 0xde2f4cc9UL,
409− 0x3080f9dbUL, 0x55e74563UL, 0x9ca03f6bUL, 0xf9c783d3UL, 0x176836c1UL,
410− 0x720f8a79UL, 0xcb375de4UL, 0xae50e15cUL, 0x40ff544eUL, 0x2598e8f6UL,
411− 0x73888baeUL, 0x16ef3716UL, 0xf8408204UL, 0x9d273ebcUL, 0x241fe921UL,
412− 0x41785599UL, 0xafd7e08bUL, 0xcab05c33UL, 0x3bb659edUL, 0x5ed1e555UL,
413− 0xb07e5047UL, 0xd519ecffUL, 0x6c213b62UL, 0x094687daUL, 0xe7e932c8UL,
414− 0x828e8e70UL, 0xd49eed28UL, 0xb1f95190UL, 0x5f56e482UL, 0x3a31583aUL,
415− 0x83098fa7UL, 0xe66e331fUL, 0x08c1860dUL, 0x6da63ab5UL, 0xa4e140bdUL,
416− 0xc186fc05UL, 0x2f294917UL, 0x4a4ef5afUL, 0xf3762232UL, 0x96119e8aUL,
417− 0x78be2b98UL, 0x1dd99720UL, 0x4bc9f478UL, 0x2eae48c0UL, 0xc001fdd2UL,
418− 0xa566416aUL, 0x1c5e96f7UL, 0x79392a4fUL, 0x97969f5dUL, 0xf2f123e5UL,
419− 0x05196b4dUL, 0x607ed7f5UL, 0x8ed162e7UL, 0xebb6de5fUL, 0x528e09c2UL,
420− 0x37e9b57aUL, 0xd9460068UL, 0xbc21bcd0UL, 0xea31df88UL, 0x8f566330UL,
421− 0x61f9d622UL, 0x049e6a9aUL, 0xbda6bd07UL, 0xd8c101bfUL, 0x366eb4adUL,
422− 0x53090815UL, 0x9a4e721dUL, 0xff29cea5UL, 0x11867bb7UL, 0x74e1c70fUL,
423− 0xcdd91092UL, 0xa8beac2aUL, 0x46111938UL, 0x2376a580UL, 0x7566c6d8UL,
424− 0x10017a60UL, 0xfeaecf72UL, 0x9bc973caUL, 0x22f1a457UL, 0x479618efUL,
425− 0xa939adfdUL, 0xcc5e1145UL, 0x06ee4d76UL, 0x6389f1ceUL, 0x8d2644dcUL,
426− 0xe841f864UL, 0x51792ff9UL, 0x341e9341UL, 0xdab12653UL, 0xbfd69aebUL,
427− 0xe9c6f9b3UL, 0x8ca1450bUL, 0x620ef019UL, 0x07694ca1UL, 0xbe519b3cUL,
428− 0xdb362784UL, 0x35999296UL, 0x50fe2e2eUL, 0x99b95426UL, 0xfcdee89eUL,
429− 0x12715d8cUL, 0x7716e134UL, 0xce2e36a9UL, 0xab498a11UL, 0x45e63f03UL,
430− 0x208183bbUL, 0x7691e0e3UL, 0x13f65c5bUL, 0xfd59e949UL, 0x983e55f1UL,
431− 0x2106826cUL, 0x44613ed4UL, 0xaace8bc6UL, 0xcfa9377eUL, 0x38417fd6UL,
432− 0x5d26c36eUL, 0xb389767cUL, 0xd6eecac4UL, 0x6fd61d59UL, 0x0ab1a1e1UL,
433− 0xe41e14f3UL, 0x8179a84bUL, 0xd769cb13UL, 0xb20e77abUL, 0x5ca1c2b9UL,
434− 0x39c67e01UL, 0x80fea99cUL, 0xe5991524UL, 0x0b36a036UL, 0x6e511c8eUL,
435− 0xa7166686UL, 0xc271da3eUL, 0x2cde6f2cUL, 0x49b9d394UL, 0xf0810409UL,
436− 0x95e6b8b1UL, 0x7b490da3UL, 0x1e2eb11bUL, 0x483ed243UL, 0x2d596efbUL,
437− 0xc3f6dbe9UL, 0xa6916751UL, 0x1fa9b0ccUL, 0x7ace0c74UL, 0x9461b966UL,
438− 0xf10605deUL
439−#endif
440− }
441−};
cbits/deflate.cdeleted−1736
@@ -1,1736 +0,0 @@
1−/* deflate.c -- compress data using the deflation algorithm
2− * Copyright (C) 1995-2005 Jean-loup Gailly.
3− * For conditions of distribution and use, see copyright notice in zlib.h
4− */
5−
6−/*
7− * ALGORITHM
8− *
9− * The "deflation" process depends on being able to identify portions
10− * of the input text which are identical to earlier input (within a
11− * sliding window trailing behind the input currently being processed).
12− *
13− * The most straightforward technique turns out to be the fastest for
14− * most input files: try all possible matches and select the longest.
15− * The key feature of this algorithm is that insertions into the string
16− * dictionary are very simple and thus fast, and deletions are avoided
17− * completely. Insertions are performed at each input character, whereas
18− * string matches are performed only when the previous match ends. So it
19− * is preferable to spend more time in matches to allow very fast string
20− * insertions and avoid deletions. The matching algorithm for small
21− * strings is inspired from that of Rabin & Karp. A brute force approach
22− * is used to find longer strings when a small match has been found.
23− * A similar algorithm is used in comic (by Jan-Mark Wams) and freeze
24− * (by Leonid Broukhis).
25− * A previous version of this file used a more sophisticated algorithm
26− * (by Fiala and Greene) which is guaranteed to run in linear amortized
27− * time, but has a larger average cost, uses more memory and is patented.
28− * However the F&G algorithm may be faster for some highly redundant
29− * files if the parameter max_chain_length (described below) is too large.
30− *
31− * ACKNOWLEDGEMENTS
32− *
33− * The idea of lazy evaluation of matches is due to Jan-Mark Wams, and
34− * I found it in 'freeze' written by Leonid Broukhis.
35− * Thanks to many people for bug reports and testing.
36− *
37− * REFERENCES
38− *
39− * Deutsch, L.P.,"DEFLATE Compressed Data Format Specification".
40− * Available in http://www.ietf.org/rfc/rfc1951.txt
41− *
42− * A description of the Rabin and Karp algorithm is given in the book
43− * "Algorithms" by R. Sedgewick, Addison-Wesley, p252.
44− *
45− * Fiala,E.R., and Greene,D.H.
46− * Data Compression with Finite Windows, Comm.ACM, 32,4 (1989) 490-595
47− *
48− */
49−
50−/* @(#) $Id$ */
51−
52−#include "deflate.h"
53−
54−const char deflate_copyright[] =
55− " deflate 1.2.3 Copyright 1995-2005 Jean-loup Gailly ";
56−/*
57− If you use the zlib library in a product, an acknowledgment is welcome
58− in the documentation of your product. If for some reason you cannot
59− include such an acknowledgment, I would appreciate that you keep this
60− copyright string in the executable of your product.
61− */
62−
63−/* ===========================================================================
64− * Function prototypes.
65− */
66−typedef enum {
67− need_more, /* block not completed, need more input or more output */
68− block_done, /* block flush performed */
69− finish_started, /* finish started, need only more output at next deflate */
70− finish_done /* finish done, accept no more input or output */
71−} block_state;
72−
73−typedef block_state (*compress_func) OF((deflate_state *s, int flush));
74−/* Compression function. Returns the block state after the call. */
75−
76−local void fill_window OF((deflate_state *s));
77−local block_state deflate_stored OF((deflate_state *s, int flush));
78−local block_state deflate_fast OF((deflate_state *s, int flush));
79−#ifndef FASTEST
80−local block_state deflate_slow OF((deflate_state *s, int flush));
81−#endif
82−local void lm_init OF((deflate_state *s));
83−local void putShortMSB OF((deflate_state *s, uInt b));
84−local void flush_pending OF((z_streamp strm));
85−local int read_buf OF((z_streamp strm, Bytef *buf, unsigned size));
86−#ifndef FASTEST
87−#ifdef ASMV
88− void match_init OF((void)); /* asm code initialization */
89− uInt longest_match OF((deflate_state *s, IPos cur_match));
90−#else
91−local uInt longest_match OF((deflate_state *s, IPos cur_match));
92−#endif
93−#endif
94−local uInt longest_match_fast OF((deflate_state *s, IPos cur_match));
95−
96−#ifdef DEBUG
97−local void check_match OF((deflate_state *s, IPos start, IPos match,
98− int length));
99−#endif
100−
101−/* ===========================================================================
102− * Local data
103− */
104−
105−#define NIL 0
106−/* Tail of hash chains */
107−
108−#ifndef TOO_FAR
109−# define TOO_FAR 4096
110−#endif
111−/* Matches of length 3 are discarded if their distance exceeds TOO_FAR */
112−
113−#define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1)
114−/* Minimum amount of lookahead, except at the end of the input file.
115− * See deflate.c for comments about the MIN_MATCH+1.
116− */
117−
118−/* Values for max_lazy_match, good_match and max_chain_length, depending on
119− * the desired pack level (0..9). The values given below have been tuned to
120− * exclude worst case performance for pathological files. Better values may be
121− * found for specific files.
122− */
123−typedef struct config_s {
124− ush good_length; /* reduce lazy search above this match length */
125− ush max_lazy; /* do not perform lazy search above this match length */
126− ush nice_length; /* quit search above this match length */
127− ush max_chain;
128− compress_func func;
129−} config;
130−
131−#ifdef FASTEST
132−local const config configuration_table[2] = {
133−/* good lazy nice chain */
134−/* 0 */ {0, 0, 0, 0, deflate_stored}, /* store only */
135−/* 1 */ {4, 4, 8, 4, deflate_fast}}; /* max speed, no lazy matches */
136−#else
137−local const config configuration_table[10] = {
138−/* good lazy nice chain */
139−/* 0 */ {0, 0, 0, 0, deflate_stored}, /* store only */
140−/* 1 */ {4, 4, 8, 4, deflate_fast}, /* max speed, no lazy matches */
141−/* 2 */ {4, 5, 16, 8, deflate_fast},
142−/* 3 */ {4, 6, 32, 32, deflate_fast},
143−
144−/* 4 */ {4, 4, 16, 16, deflate_slow}, /* lazy matches */
145−/* 5 */ {8, 16, 32, 32, deflate_slow},
146−/* 6 */ {8, 16, 128, 128, deflate_slow},
147−/* 7 */ {8, 32, 128, 256, deflate_slow},
148−/* 8 */ {32, 128, 258, 1024, deflate_slow},
149−/* 9 */ {32, 258, 258, 4096, deflate_slow}}; /* max compression */
150−#endif
151−
152−/* Note: the deflate() code requires max_lazy >= MIN_MATCH and max_chain >= 4
153− * For deflate_fast() (levels <= 3) good is ignored and lazy has a different
154− * meaning.
155− */
156−
157−#define EQUAL 0
158−/* result of memcmp for equal strings */
159−
160−#ifndef NO_DUMMY_DECL
161−struct static_tree_desc_s {int dummy;}; /* for buggy compilers */
162−#endif
163−
164−/* ===========================================================================
165− * Update a hash value with the given input byte
166− * IN assertion: all calls to to UPDATE_HASH are made with consecutive
167− * input characters, so that a running hash key can be computed from the
168− * previous key instead of complete recalculation each time.
169− */
170−#define UPDATE_HASH(s,h,c) (h = (((h)<<s->hash_shift) ^ (c)) & s->hash_mask)
171−
172−
173−/* ===========================================================================
174− * Insert string str in the dictionary and set match_head to the previous head
175− * of the hash chain (the most recent string with same hash key). Return
176− * the previous length of the hash chain.
177− * If this file is compiled with -DFASTEST, the compression level is forced
178− * to 1, and no hash chains are maintained.
179− * IN assertion: all calls to to INSERT_STRING are made with consecutive
180− * input characters and the first MIN_MATCH bytes of str are valid
181− * (except for the last MIN_MATCH-1 bytes of the input file).
182− */
183−#ifdef FASTEST
184−#define INSERT_STRING(s, str, match_head) \
185− (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \
186− match_head = s->head[s->ins_h], \
187− s->head[s->ins_h] = (Pos)(str))
188−#else
189−#define INSERT_STRING(s, str, match_head) \
190− (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \
191− match_head = s->prev[(str) & s->w_mask] = s->head[s->ins_h], \
192− s->head[s->ins_h] = (Pos)(str))
193−#endif
194−
195−/* ===========================================================================
196− * Initialize the hash table (avoiding 64K overflow for 16 bit systems).
197− * prev[] will be initialized on the fly.
198− */
199−#define CLEAR_HASH(s) \
200− s->head[s->hash_size-1] = NIL; \
201− zmemzero((Bytef *)s->head, (unsigned)(s->hash_size-1)*sizeof(*s->head));
202−
203−/* ========================================================================= */
204−int ZEXPORT deflateInit_(strm, level, version, stream_size)
205− z_streamp strm;
206− int level;
207− const char *version;
208− int stream_size;
209−{
210− return deflateInit2_(strm, level, Z_DEFLATED, MAX_WBITS, DEF_MEM_LEVEL,
211− Z_DEFAULT_STRATEGY, version, stream_size);
212− /* To do: ignore strm->next_in if we use it as window */
213−}
214−
215−/* ========================================================================= */
216−int ZEXPORT deflateInit2_(strm, level, method, windowBits, memLevel, strategy,
217− version, stream_size)
218− z_streamp strm;
219− int level;
220− int method;
221− int windowBits;
222− int memLevel;
223− int strategy;
224− const char *version;
225− int stream_size;
226−{
227− deflate_state *s;
228− int wrap = 1;
229− static const char my_version[] = ZLIB_VERSION;
230−
231− ushf *overlay;
232− /* We overlay pending_buf and d_buf+l_buf. This works since the average
233− * output size for (length,distance) codes is <= 24 bits.
234− */
235−
236− if (version == Z_NULL || version[0] != my_version[0] ||
237− stream_size != sizeof(z_stream)) {
238− return Z_VERSION_ERROR;
239− }
240− if (strm == Z_NULL) return Z_STREAM_ERROR;
241−
242− strm->msg = Z_NULL;
243− if (strm->zalloc == (alloc_func)0) {
244− strm->zalloc = zcalloc;
245− strm->opaque = (voidpf)0;
246− }
247− if (strm->zfree == (free_func)0) strm->zfree = zcfree;
248−
249−#ifdef FASTEST
250− if (level != 0) level = 1;
251−#else
252− if (level == Z_DEFAULT_COMPRESSION) level = 6;
253−#endif
254−
255− if (windowBits < 0) { /* suppress zlib wrapper */
256− wrap = 0;
257− windowBits = -windowBits;
258− }
259−#ifdef GZIP
260− else if (windowBits > 15) {
261− wrap = 2; /* write gzip wrapper instead */
262− windowBits -= 16;
263− }
264−#endif
265− if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method != Z_DEFLATED ||
266− windowBits < 8 || windowBits > 15 || level < 0 || level > 9 ||
267− strategy < 0 || strategy > Z_FIXED) {
268− return Z_STREAM_ERROR;
269− }
270− if (windowBits == 8) windowBits = 9; /* until 256-byte window bug fixed */
271− s = (deflate_state *) ZALLOC(strm, 1, sizeof(deflate_state));
272− if (s == Z_NULL) return Z_MEM_ERROR;
273− strm->state = (struct internal_state FAR *)s;
274− s->strm = strm;
275−
276− s->wrap = wrap;
277− s->gzhead = Z_NULL;
278− s->w_bits = windowBits;
279− s->w_size = 1 << s->w_bits;
280− s->w_mask = s->w_size - 1;
281−
282− s->hash_bits = memLevel + 7;
283− s->hash_size = 1 << s->hash_bits;
284− s->hash_mask = s->hash_size - 1;
285− s->hash_shift = ((s->hash_bits+MIN_MATCH-1)/MIN_MATCH);
286−
287− s->window = (Bytef *) ZALLOC(strm, s->w_size, 2*sizeof(Byte));
288− s->prev = (Posf *) ZALLOC(strm, s->w_size, sizeof(Pos));
289− s->head = (Posf *) ZALLOC(strm, s->hash_size, sizeof(Pos));
290−
291− s->lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */
292−
293− overlay = (ushf *) ZALLOC(strm, s->lit_bufsize, sizeof(ush)+2);
294− s->pending_buf = (uchf *) overlay;
295− s->pending_buf_size = (ulg)s->lit_bufsize * (sizeof(ush)+2L);
296−
297− if (s->window == Z_NULL || s->prev == Z_NULL || s->head == Z_NULL ||
298− s->pending_buf == Z_NULL) {
299− s->status = FINISH_STATE;
300− strm->msg = (char*)ERR_MSG(Z_MEM_ERROR);
301− deflateEnd (strm);
302− return Z_MEM_ERROR;
303− }
304− s->d_buf = overlay + s->lit_bufsize/sizeof(ush);
305− s->l_buf = s->pending_buf + (1+sizeof(ush))*s->lit_bufsize;
306−
307− s->level = level;
308− s->strategy = strategy;
309− s->method = (Byte)method;
310−
311− return deflateReset(strm);
312−}
313−
314−/* ========================================================================= */
315−int ZEXPORT deflateSetDictionary (strm, dictionary, dictLength)
316− z_streamp strm;
317− const Bytef *dictionary;
318− uInt dictLength;
319−{
320− deflate_state *s;
321− uInt length = dictLength;
322− uInt n;
323− IPos hash_head = 0;
324−
325− if (strm == Z_NULL || strm->state == Z_NULL || dictionary == Z_NULL ||
326− strm->state->wrap == 2 ||
327− (strm->state->wrap == 1 && strm->state->status != INIT_STATE))
328− return Z_STREAM_ERROR;
329−
330− s = strm->state;
331− if (s->wrap)
332− strm->adler = adler32(strm->adler, dictionary, dictLength);
333−
334− if (length < MIN_MATCH) return Z_OK;
335− if (length > MAX_DIST(s)) {
336− length = MAX_DIST(s);
337− dictionary += dictLength - length; /* use the tail of the dictionary */
338− }
339− zmemcpy(s->window, dictionary, length);
340− s->strstart = length;
341− s->block_start = (long)length;
342−
343− /* Insert all strings in the hash table (except for the last two bytes).
344− * s->lookahead stays null, so s->ins_h will be recomputed at the next
345− * call of fill_window.
346− */
347− s->ins_h = s->window[0];
348− UPDATE_HASH(s, s->ins_h, s->window[1]);
349− for (n = 0; n <= length - MIN_MATCH; n++) {
350− INSERT_STRING(s, n, hash_head);
351− }
352− if (hash_head) hash_head = 0; /* to make compiler happy */
353− return Z_OK;
354−}
355−
356−/* ========================================================================= */
357−int ZEXPORT deflateReset (strm)
358− z_streamp strm;
359−{
360− deflate_state *s;
361−
362− if (strm == Z_NULL || strm->state == Z_NULL ||
363− strm->zalloc == (alloc_func)0 || strm->zfree == (free_func)0) {
364− return Z_STREAM_ERROR;
365− }
366−
367− strm->total_in = strm->total_out = 0;
368− strm->msg = Z_NULL; /* use zfree if we ever allocate msg dynamically */
369− strm->data_type = Z_UNKNOWN;
370−
371− s = (deflate_state *)strm->state;
372− s->pending = 0;
373− s->pending_out = s->pending_buf;
374−
375− if (s->wrap < 0) {
376− s->wrap = -s->wrap; /* was made negative by deflate(..., Z_FINISH); */
377− }
378− s->status = s->wrap ? INIT_STATE : BUSY_STATE;
379− strm->adler =
380−#ifdef GZIP
381− s->wrap == 2 ? crc32(0L, Z_NULL, 0) :
382−#endif
383− adler32(0L, Z_NULL, 0);
384− s->last_flush = Z_NO_FLUSH;
385−
386− _tr_init(s);
387− lm_init(s);
388−
389− return Z_OK;
390−}
391−
392−/* ========================================================================= */
393−int ZEXPORT deflateSetHeader (strm, head)
394− z_streamp strm;
395− gz_headerp head;
396−{
397− if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
398− if (strm->state->wrap != 2) return Z_STREAM_ERROR;
399− strm->state->gzhead = head;
400− return Z_OK;
401−}
402−
403−/* ========================================================================= */
404−int ZEXPORT deflatePrime (strm, bits, value)
405− z_streamp strm;
406− int bits;
407− int value;
408−{
409− if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
410− strm->state->bi_valid = bits;
411− strm->state->bi_buf = (ush)(value & ((1 << bits) - 1));
412− return Z_OK;
413−}
414−
415−/* ========================================================================= */
416−int ZEXPORT deflateParams(strm, level, strategy)
417− z_streamp strm;
418− int level;
419− int strategy;
420−{
421− deflate_state *s;
422− compress_func func;
423− int err = Z_OK;
424−
425− if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
426− s = strm->state;
427−
428−#ifdef FASTEST
429− if (level != 0) level = 1;
430−#else
431− if (level == Z_DEFAULT_COMPRESSION) level = 6;
432−#endif
433− if (level < 0 || level > 9 || strategy < 0 || strategy > Z_FIXED) {
434− return Z_STREAM_ERROR;
435− }
436− func = configuration_table[s->level].func;
437−
438− if (func != configuration_table[level].func && strm->total_in != 0) {
439− /* Flush the last buffer: */
440− err = deflate(strm, Z_PARTIAL_FLUSH);
441− }
442− if (s->level != level) {
443− s->level = level;
444− s->max_lazy_match = configuration_table[level].max_lazy;
445− s->good_match = configuration_table[level].good_length;
446− s->nice_match = configuration_table[level].nice_length;
447− s->max_chain_length = configuration_table[level].max_chain;
448− }
449− s->strategy = strategy;
450− return err;
451−}
452−
453−/* ========================================================================= */
454−int ZEXPORT deflateTune(strm, good_length, max_lazy, nice_length, max_chain)
455− z_streamp strm;
456− int good_length;
457− int max_lazy;
458− int nice_length;
459− int max_chain;
460−{
461− deflate_state *s;
462−
463− if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
464− s = strm->state;
465− s->good_match = good_length;
466− s->max_lazy_match = max_lazy;
467− s->nice_match = nice_length;
468− s->max_chain_length = max_chain;
469− return Z_OK;
470−}
471−
472−/* =========================================================================
473− * For the default windowBits of 15 and memLevel of 8, this function returns
474− * a close to exact, as well as small, upper bound on the compressed size.
475− * They are coded as constants here for a reason--if the #define's are
476− * changed, then this function needs to be changed as well. The return
477− * value for 15 and 8 only works for those exact settings.
478− *
479− * For any setting other than those defaults for windowBits and memLevel,
480− * the value returned is a conservative worst case for the maximum expansion
481− * resulting from using fixed blocks instead of stored blocks, which deflate
482− * can emit on compressed data for some combinations of the parameters.
483− *
484− * This function could be more sophisticated to provide closer upper bounds
485− * for every combination of windowBits and memLevel, as well as wrap.
486− * But even the conservative upper bound of about 14% expansion does not
487− * seem onerous for output buffer allocation.
488− */
489−uLong ZEXPORT deflateBound(strm, sourceLen)
490− z_streamp strm;
491− uLong sourceLen;
492−{
493− deflate_state *s;
494− uLong destLen;
495−
496− /* conservative upper bound */
497− destLen = sourceLen +
498− ((sourceLen + 7) >> 3) + ((sourceLen + 63) >> 6) + 11;
499−
500− /* if can't get parameters, return conservative bound */
501− if (strm == Z_NULL || strm->state == Z_NULL)
502− return destLen;
503−
504− /* if not default parameters, return conservative bound */
505− s = strm->state;
506− if (s->w_bits != 15 || s->hash_bits != 8 + 7)
507− return destLen;
508−
509− /* default settings: return tight bound for that case */
510− return compressBound(sourceLen);
511−}
512−
513−/* =========================================================================
514− * Put a short in the pending buffer. The 16-bit value is put in MSB order.
515− * IN assertion: the stream state is correct and there is enough room in
516− * pending_buf.
517− */
518−local void putShortMSB (s, b)
519− deflate_state *s;
520− uInt b;
521−{
522− put_byte(s, (Byte)(b >> 8));
523− put_byte(s, (Byte)(b & 0xff));
524−}
525−
526−/* =========================================================================
527− * Flush as much pending output as possible. All deflate() output goes
528− * through this function so some applications may wish to modify it
529− * to avoid allocating a large strm->next_out buffer and copying into it.
530− * (See also read_buf()).
531− */
532−local void flush_pending(strm)
533− z_streamp strm;
534−{
535− unsigned len = strm->state->pending;
536−
537− if (len > strm->avail_out) len = strm->avail_out;
538− if (len == 0) return;
539−
540− zmemcpy(strm->next_out, strm->state->pending_out, len);
541− strm->next_out += len;
542− strm->state->pending_out += len;
543− strm->total_out += len;
544− strm->avail_out -= len;
545− strm->state->pending -= len;
546− if (strm->state->pending == 0) {
547− strm->state->pending_out = strm->state->pending_buf;
548− }
549−}
550−
551−/* ========================================================================= */
552−int ZEXPORT deflate (strm, flush)
553− z_streamp strm;
554− int flush;
555−{
556− int old_flush; /* value of flush param for previous deflate call */
557− deflate_state *s;
558−
559− if (strm == Z_NULL || strm->state == Z_NULL ||
560− flush > Z_FINISH || flush < 0) {
561− return Z_STREAM_ERROR;
562− }
563− s = strm->state;
564−
565− if (strm->next_out == Z_NULL ||
566− (strm->next_in == Z_NULL && strm->avail_in != 0) ||
567− (s->status == FINISH_STATE && flush != Z_FINISH)) {
568− ERR_RETURN(strm, Z_STREAM_ERROR);
569− }
570− if (strm->avail_out == 0) ERR_RETURN(strm, Z_BUF_ERROR);
571−
572− s->strm = strm; /* just in case */
573− old_flush = s->last_flush;
574− s->last_flush = flush;
575−
576− /* Write the header */
577− if (s->status == INIT_STATE) {
578−#ifdef GZIP
579− if (s->wrap == 2) {
580− strm->adler = crc32(0L, Z_NULL, 0);
581− put_byte(s, 31);
582− put_byte(s, 139);
583− put_byte(s, 8);
584− if (s->gzhead == NULL) {
585− put_byte(s, 0);
586− put_byte(s, 0);
587− put_byte(s, 0);
588− put_byte(s, 0);
589− put_byte(s, 0);
590− put_byte(s, s->level == 9 ? 2 :
591− (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ?
592− 4 : 0));
593− put_byte(s, OS_CODE);
594− s->status = BUSY_STATE;
595− }
596− else {
597− put_byte(s, (s->gzhead->text ? 1 : 0) +
598− (s->gzhead->hcrc ? 2 : 0) +
599− (s->gzhead->extra == Z_NULL ? 0 : 4) +
600− (s->gzhead->name == Z_NULL ? 0 : 8) +
601− (s->gzhead->comment == Z_NULL ? 0 : 16)
602− );
603− put_byte(s, (Byte)(s->gzhead->time & 0xff));
604− put_byte(s, (Byte)((s->gzhead->time >> 8) & 0xff));
605− put_byte(s, (Byte)((s->gzhead->time >> 16) & 0xff));
606− put_byte(s, (Byte)((s->gzhead->time >> 24) & 0xff));
607− put_byte(s, s->level == 9 ? 2 :
608− (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ?
609− 4 : 0));
610− put_byte(s, s->gzhead->os & 0xff);
611− if (s->gzhead->extra != NULL) {
612− put_byte(s, s->gzhead->extra_len & 0xff);
613− put_byte(s, (s->gzhead->extra_len >> 8) & 0xff);
614− }
615− if (s->gzhead->hcrc)
616− strm->adler = crc32(strm->adler, s->pending_buf,
617− s->pending);
618− s->gzindex = 0;
619− s->status = EXTRA_STATE;
620− }
621− }
622− else
623−#endif
624− {
625− uInt header = (Z_DEFLATED + ((s->w_bits-8)<<4)) << 8;
626− uInt level_flags;
627−
628− if (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2)
629− level_flags = 0;
630− else if (s->level < 6)
631− level_flags = 1;
632− else if (s->level == 6)
633− level_flags = 2;
634− else
635− level_flags = 3;
636− header |= (level_flags << 6);
637− if (s->strstart != 0) header |= PRESET_DICT;
638− header += 31 - (header % 31);
639−
640− s->status = BUSY_STATE;
641− putShortMSB(s, header);
642−
643− /* Save the adler32 of the preset dictionary: */
644− if (s->strstart != 0) {
645− putShortMSB(s, (uInt)(strm->adler >> 16));
646− putShortMSB(s, (uInt)(strm->adler & 0xffff));
647− }
648− strm->adler = adler32(0L, Z_NULL, 0);
649− }
650− }
651−#ifdef GZIP
652− if (s->status == EXTRA_STATE) {
653− if (s->gzhead->extra != NULL) {
654− uInt beg = s->pending; /* start of bytes to update crc */
655−
656− while (s->gzindex < (s->gzhead->extra_len & 0xffff)) {
657− if (s->pending == s->pending_buf_size) {
658− if (s->gzhead->hcrc && s->pending > beg)
659− strm->adler = crc32(strm->adler, s->pending_buf + beg,
660− s->pending - beg);
661− flush_pending(strm);
662− beg = s->pending;
663− if (s->pending == s->pending_buf_size)
664− break;
665− }
666− put_byte(s, s->gzhead->extra[s->gzindex]);
667− s->gzindex++;
668− }
669− if (s->gzhead->hcrc && s->pending > beg)
670− strm->adler = crc32(strm->adler, s->pending_buf + beg,
671− s->pending - beg);
672− if (s->gzindex == s->gzhead->extra_len) {
673− s->gzindex = 0;
674− s->status = NAME_STATE;
675− }
676− }
677− else
678− s->status = NAME_STATE;
679− }
680− if (s->status == NAME_STATE) {
681− if (s->gzhead->name != NULL) {
682− uInt beg = s->pending; /* start of bytes to update crc */
683− int val;
684−
685− do {
686− if (s->pending == s->pending_buf_size) {
687− if (s->gzhead->hcrc && s->pending > beg)
688− strm->adler = crc32(strm->adler, s->pending_buf + beg,
689− s->pending - beg);
690− flush_pending(strm);
691− beg = s->pending;
692− if (s->pending == s->pending_buf_size) {
693− val = 1;
694− break;
695− }
696− }
697− val = s->gzhead->name[s->gzindex++];
698− put_byte(s, val);
699− } while (val != 0);
700− if (s->gzhead->hcrc && s->pending > beg)
701− strm->adler = crc32(strm->adler, s->pending_buf + beg,
702− s->pending - beg);
703− if (val == 0) {
704− s->gzindex = 0;
705− s->status = COMMENT_STATE;
706− }
707− }
708− else
709− s->status = COMMENT_STATE;
710− }
711− if (s->status == COMMENT_STATE) {
712− if (s->gzhead->comment != NULL) {
713− uInt beg = s->pending; /* start of bytes to update crc */
714− int val;
715−
716− do {
717− if (s->pending == s->pending_buf_size) {
718− if (s->gzhead->hcrc && s->pending > beg)
719− strm->adler = crc32(strm->adler, s->pending_buf + beg,
720− s->pending - beg);
721− flush_pending(strm);
722− beg = s->pending;
723− if (s->pending == s->pending_buf_size) {
724− val = 1;
725− break;
726− }
727− }
728− val = s->gzhead->comment[s->gzindex++];
729− put_byte(s, val);
730− } while (val != 0);
731− if (s->gzhead->hcrc && s->pending > beg)
732− strm->adler = crc32(strm->adler, s->pending_buf + beg,
733− s->pending - beg);
734− if (val == 0)
735− s->status = HCRC_STATE;
736− }
737− else
738− s->status = HCRC_STATE;
739− }
740− if (s->status == HCRC_STATE) {
741− if (s->gzhead->hcrc) {
742− if (s->pending + 2 > s->pending_buf_size)
743− flush_pending(strm);
744− if (s->pending + 2 <= s->pending_buf_size) {
745− put_byte(s, (Byte)(strm->adler & 0xff));
746− put_byte(s, (Byte)((strm->adler >> 8) & 0xff));
747− strm->adler = crc32(0L, Z_NULL, 0);
748− s->status = BUSY_STATE;
749− }
750− }
751− else
752− s->status = BUSY_STATE;
753− }
754−#endif
755−
756− /* Flush as much pending output as possible */
757− if (s->pending != 0) {
758− flush_pending(strm);
759− if (strm->avail_out == 0) {
760− /* Since avail_out is 0, deflate will be called again with
761− * more output space, but possibly with both pending and
762− * avail_in equal to zero. There won't be anything to do,
763− * but this is not an error situation so make sure we
764− * return OK instead of BUF_ERROR at next call of deflate:
765− */
766− s->last_flush = -1;
767− return Z_OK;
768− }
769−
770− /* Make sure there is something to do and avoid duplicate consecutive
771− * flushes. For repeated and useless calls with Z_FINISH, we keep
772− * returning Z_STREAM_END instead of Z_BUF_ERROR.
773− */
774− } else if (strm->avail_in == 0 && flush <= old_flush &&
775− flush != Z_FINISH) {
776− ERR_RETURN(strm, Z_BUF_ERROR);
777− }
778−
779− /* User must not provide more input after the first FINISH: */
780− if (s->status == FINISH_STATE && strm->avail_in != 0) {
781− ERR_RETURN(strm, Z_BUF_ERROR);
782− }
783−
784− /* Start a new block or continue the current one.
785− */
786− if (strm->avail_in != 0 || s->lookahead != 0 ||
787− (flush != Z_NO_FLUSH && s->status != FINISH_STATE)) {
788− block_state bstate;
789−
790− bstate = (*(configuration_table[s->level].func))(s, flush);
791−
792− if (bstate == finish_started || bstate == finish_done) {
793− s->status = FINISH_STATE;
794− }
795− if (bstate == need_more || bstate == finish_started) {
796− if (strm->avail_out == 0) {
797− s->last_flush = -1; /* avoid BUF_ERROR next call, see above */
798− }
799− return Z_OK;
800− /* If flush != Z_NO_FLUSH && avail_out == 0, the next call
801− * of deflate should use the same flush parameter to make sure
802− * that the flush is complete. So we don't have to output an
803− * empty block here, this will be done at next call. This also
804− * ensures that for a very small output buffer, we emit at most
805− * one empty block.
806− */
807− }
808− if (bstate == block_done) {
809− if (flush == Z_PARTIAL_FLUSH) {
810− _tr_align(s);
811− } else { /* FULL_FLUSH or SYNC_FLUSH */
812− _tr_stored_block(s, (char*)0, 0L, 0);
813− /* For a full flush, this empty block will be recognized
814− * as a special marker by inflate_sync().
815− */
816− if (flush == Z_FULL_FLUSH) {
817− CLEAR_HASH(s); /* forget history */
818− }
819− }
820− flush_pending(strm);
821− if (strm->avail_out == 0) {
822− s->last_flush = -1; /* avoid BUF_ERROR at next call, see above */
823− return Z_OK;
824− }
825− }
826− }
827− Assert(strm->avail_out > 0, "bug2");
828−
829− if (flush != Z_FINISH) return Z_OK;
830− if (s->wrap <= 0) return Z_STREAM_END;
831−
832− /* Write the trailer */
833−#ifdef GZIP
834− if (s->wrap == 2) {
835− put_byte(s, (Byte)(strm->adler & 0xff));
836− put_byte(s, (Byte)((strm->adler >> 8) & 0xff));
837− put_byte(s, (Byte)((strm->adler >> 16) & 0xff));
838− put_byte(s, (Byte)((strm->adler >> 24) & 0xff));
839− put_byte(s, (Byte)(strm->total_in & 0xff));
840− put_byte(s, (Byte)((strm->total_in >> 8) & 0xff));
841− put_byte(s, (Byte)((strm->total_in >> 16) & 0xff));
842− put_byte(s, (Byte)((strm->total_in >> 24) & 0xff));
843− }
844− else
845−#endif
846− {
847− putShortMSB(s, (uInt)(strm->adler >> 16));
848− putShortMSB(s, (uInt)(strm->adler & 0xffff));
849− }
850− flush_pending(strm);
851− /* If avail_out is zero, the application will call deflate again
852− * to flush the rest.
853− */
854− if (s->wrap > 0) s->wrap = -s->wrap; /* write the trailer only once! */
855− return s->pending != 0 ? Z_OK : Z_STREAM_END;
856−}
857−
858−/* ========================================================================= */
859−int ZEXPORT deflateEnd (strm)
860− z_streamp strm;
861−{
862− int status;
863−
864− if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
865−
866− status = strm->state->status;
867− if (status != INIT_STATE &&
868− status != EXTRA_STATE &&
869− status != NAME_STATE &&
870− status != COMMENT_STATE &&
871− status != HCRC_STATE &&
872− status != BUSY_STATE &&
873− status != FINISH_STATE) {
874− return Z_STREAM_ERROR;
875− }
876−
877− /* Deallocate in reverse order of allocations: */
878− TRY_FREE(strm, strm->state->pending_buf);
879− TRY_FREE(strm, strm->state->head);
880− TRY_FREE(strm, strm->state->prev);
881− TRY_FREE(strm, strm->state->window);
882−
883− ZFREE(strm, strm->state);
884− strm->state = Z_NULL;
885−
886− return status == BUSY_STATE ? Z_DATA_ERROR : Z_OK;
887−}
888−
889−/* =========================================================================
890− * Copy the source state to the destination state.
891− * To simplify the source, this is not supported for 16-bit MSDOS (which
892− * doesn't have enough memory anyway to duplicate compression states).
893− */
894−int ZEXPORT deflateCopy (dest, source)
895− z_streamp dest;
896− z_streamp source;
897−{
898−#ifdef MAXSEG_64K
899− return Z_STREAM_ERROR;
900−#else
901− deflate_state *ds;
902− deflate_state *ss;
903− ushf *overlay;
904−
905−
906− if (source == Z_NULL || dest == Z_NULL || source->state == Z_NULL) {
907− return Z_STREAM_ERROR;
908− }
909−
910− ss = source->state;
911−
912− zmemcpy(dest, source, sizeof(z_stream));
913−
914− ds = (deflate_state *) ZALLOC(dest, 1, sizeof(deflate_state));
915− if (ds == Z_NULL) return Z_MEM_ERROR;
916− dest->state = (struct internal_state FAR *) ds;
917− zmemcpy(ds, ss, sizeof(deflate_state));
918− ds->strm = dest;
919−
920− ds->window = (Bytef *) ZALLOC(dest, ds->w_size, 2*sizeof(Byte));
921− ds->prev = (Posf *) ZALLOC(dest, ds->w_size, sizeof(Pos));
922− ds->head = (Posf *) ZALLOC(dest, ds->hash_size, sizeof(Pos));
923− overlay = (ushf *) ZALLOC(dest, ds->lit_bufsize, sizeof(ush)+2);
924− ds->pending_buf = (uchf *) overlay;
925−
926− if (ds->window == Z_NULL || ds->prev == Z_NULL || ds->head == Z_NULL ||
927− ds->pending_buf == Z_NULL) {
928− deflateEnd (dest);
929− return Z_MEM_ERROR;
930− }
931− /* following zmemcpy do not work for 16-bit MSDOS */
932− zmemcpy(ds->window, ss->window, ds->w_size * 2 * sizeof(Byte));
933− zmemcpy(ds->prev, ss->prev, ds->w_size * sizeof(Pos));
934− zmemcpy(ds->head, ss->head, ds->hash_size * sizeof(Pos));
935− zmemcpy(ds->pending_buf, ss->pending_buf, (uInt)ds->pending_buf_size);
936−
937− ds->pending_out = ds->pending_buf + (ss->pending_out - ss->pending_buf);
938− ds->d_buf = overlay + ds->lit_bufsize/sizeof(ush);
939− ds->l_buf = ds->pending_buf + (1+sizeof(ush))*ds->lit_bufsize;
940−
941− ds->l_desc.dyn_tree = ds->dyn_ltree;
942− ds->d_desc.dyn_tree = ds->dyn_dtree;
943− ds->bl_desc.dyn_tree = ds->bl_tree;
944−
945− return Z_OK;
946−#endif /* MAXSEG_64K */
947−}
948−
949−/* ===========================================================================
950− * Read a new buffer from the current input stream, update the adler32
951− * and total number of bytes read. All deflate() input goes through
952− * this function so some applications may wish to modify it to avoid
953− * allocating a large strm->next_in buffer and copying from it.
954− * (See also flush_pending()).
955− */
956−local int read_buf(strm, buf, size)
957− z_streamp strm;
958− Bytef *buf;
959− unsigned size;
960−{
961− unsigned len = strm->avail_in;
962−
963− if (len > size) len = size;
964− if (len == 0) return 0;
965−
966− strm->avail_in -= len;
967−
968− if (strm->state->wrap == 1) {
969− strm->adler = adler32(strm->adler, strm->next_in, len);
970− }
971−#ifdef GZIP
972− else if (strm->state->wrap == 2) {
973− strm->adler = crc32(strm->adler, strm->next_in, len);
974− }
975−#endif
976− zmemcpy(buf, strm->next_in, len);
977− strm->next_in += len;
978− strm->total_in += len;
979−
980− return (int)len;
981−}
982−
983−/* ===========================================================================
984− * Initialize the "longest match" routines for a new zlib stream
985− */
986−local void lm_init (s)
987− deflate_state *s;
988−{
989− s->window_size = (ulg)2L*s->w_size;
990−
991− CLEAR_HASH(s);
992−
993− /* Set the default configuration parameters:
994− */
995− s->max_lazy_match = configuration_table[s->level].max_lazy;
996− s->good_match = configuration_table[s->level].good_length;
997− s->nice_match = configuration_table[s->level].nice_length;
998− s->max_chain_length = configuration_table[s->level].max_chain;
999−
1000− s->strstart = 0;
1001− s->block_start = 0L;
1002− s->lookahead = 0;
1003− s->match_length = s->prev_length = MIN_MATCH-1;
1004− s->match_available = 0;
1005− s->ins_h = 0;
1006−#ifndef FASTEST
1007−#ifdef ASMV
1008− match_init(); /* initialize the asm code */
1009−#endif
1010−#endif
1011−}
1012−
1013−#ifndef FASTEST
1014−/* ===========================================================================
1015− * Set match_start to the longest match starting at the given string and
1016− * return its length. Matches shorter or equal to prev_length are discarded,
1017− * in which case the result is equal to prev_length and match_start is
1018− * garbage.
1019− * IN assertions: cur_match is the head of the hash chain for the current
1020− * string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1
1021− * OUT assertion: the match length is not greater than s->lookahead.
1022− */
1023−#ifndef ASMV
1024−/* For 80x86 and 680x0, an optimized version will be provided in match.asm or
1025− * match.S. The code will be functionally equivalent.
1026− */
1027−local uInt longest_match(s, cur_match)
1028− deflate_state *s;
1029− IPos cur_match; /* current match */
1030−{
1031− unsigned chain_length = s->max_chain_length;/* max hash chain length */
1032− register Bytef *scan = s->window + s->strstart; /* current string */
1033− register Bytef *match; /* matched string */
1034− register int len; /* length of current match */
1035− int best_len = s->prev_length; /* best match length so far */
1036− int nice_match = s->nice_match; /* stop if match long enough */
1037− IPos limit = s->strstart > (IPos)MAX_DIST(s) ?
1038− s->strstart - (IPos)MAX_DIST(s) : NIL;
1039− /* Stop when cur_match becomes <= limit. To simplify the code,
1040− * we prevent matches with the string of window index 0.
1041− */
1042− Posf *prev = s->prev;
1043− uInt wmask = s->w_mask;
1044−
1045−#ifdef UNALIGNED_OK
1046− /* Compare two bytes at a time. Note: this is not always beneficial.
1047− * Try with and without -DUNALIGNED_OK to check.
1048− */
1049− register Bytef *strend = s->window + s->strstart + MAX_MATCH - 1;
1050− register ush scan_start = *(ushf*)scan;
1051− register ush scan_end = *(ushf*)(scan+best_len-1);
1052−#else
1053− register Bytef *strend = s->window + s->strstart + MAX_MATCH;
1054− register Byte scan_end1 = scan[best_len-1];
1055− register Byte scan_end = scan[best_len];
1056−#endif
1057−
1058− /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.
1059− * It is easy to get rid of this optimization if necessary.
1060− */
1061− Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever");
1062−
1063− /* Do not waste too much time if we already have a good match: */
1064− if (s->prev_length >= s->good_match) {
1065− chain_length >>= 2;
1066− }
1067− /* Do not look for matches beyond the end of the input. This is necessary
1068− * to make deflate deterministic.
1069− */
1070− if ((uInt)nice_match > s->lookahead) nice_match = s->lookahead;
1071−
1072− Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead");
1073−
1074− do {
1075− Assert(cur_match < s->strstart, "no future");
1076− match = s->window + cur_match;
1077−
1078− /* Skip to next match if the match length cannot increase
1079− * or if the match length is less than 2. Note that the checks below
1080− * for insufficient lookahead only occur occasionally for performance
1081− * reasons. Therefore uninitialized memory will be accessed, and
1082− * conditional jumps will be made that depend on those values.
1083− * However the length of the match is limited to the lookahead, so
1084− * the output of deflate is not affected by the uninitialized values.
1085− */
1086−#if (defined(UNALIGNED_OK) && MAX_MATCH == 258)
1087− /* This code assumes sizeof(unsigned short) == 2. Do not use
1088− * UNALIGNED_OK if your compiler uses a different size.
1089− */
1090− if (*(ushf*)(match+best_len-1) != scan_end ||
1091− *(ushf*)match != scan_start) continue;
1092−
1093− /* It is not necessary to compare scan[2] and match[2] since they are
1094− * always equal when the other bytes match, given that the hash keys
1095− * are equal and that HASH_BITS >= 8. Compare 2 bytes at a time at
1096− * strstart+3, +5, ... up to strstart+257. We check for insufficient
1097− * lookahead only every 4th comparison; the 128th check will be made
1098− * at strstart+257. If MAX_MATCH-2 is not a multiple of 8, it is
1099− * necessary to put more guard bytes at the end of the window, or
1100− * to check more often for insufficient lookahead.
1101− */
1102− Assert(scan[2] == match[2], "scan[2]?");
1103− scan++, match++;
1104− do {
1105− } while (*(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
1106− *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
1107− *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
1108− *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
1109− scan < strend);
1110− /* The funny "do {}" generates better code on most compilers */
1111−
1112− /* Here, scan <= window+strstart+257 */
1113− Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
1114− if (*scan == *match) scan++;
1115−
1116− len = (MAX_MATCH - 1) - (int)(strend-scan);
1117− scan = strend - (MAX_MATCH-1);
1118−
1119−#else /* UNALIGNED_OK */
1120−
1121− if (match[best_len] != scan_end ||
1122− match[best_len-1] != scan_end1 ||
1123− *match != *scan ||
1124− *++match != scan[1]) continue;
1125−
1126− /* The check at best_len-1 can be removed because it will be made
1127− * again later. (This heuristic is not always a win.)
1128− * It is not necessary to compare scan[2] and match[2] since they
1129− * are always equal when the other bytes match, given that
1130− * the hash keys are equal and that HASH_BITS >= 8.
1131− */
1132− scan += 2, match++;
1133− Assert(*scan == *match, "match[2]?");
1134−
1135− /* We check for insufficient lookahead only every 8th comparison;
1136− * the 256th check will be made at strstart+258.
1137− */
1138− do {
1139− } while (*++scan == *++match && *++scan == *++match &&
1140− *++scan == *++match && *++scan == *++match &&
1141− *++scan == *++match && *++scan == *++match &&
1142− *++scan == *++match && *++scan == *++match &&
1143− scan < strend);
1144−
1145− Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
1146−
1147− len = MAX_MATCH - (int)(strend - scan);
1148− scan = strend - MAX_MATCH;
1149−
1150−#endif /* UNALIGNED_OK */
1151−
1152− if (len > best_len) {
1153− s->match_start = cur_match;
1154− best_len = len;
1155− if (len >= nice_match) break;
1156−#ifdef UNALIGNED_OK
1157− scan_end = *(ushf*)(scan+best_len-1);
1158−#else
1159− scan_end1 = scan[best_len-1];
1160− scan_end = scan[best_len];
1161−#endif
1162− }
1163− } while ((cur_match = prev[cur_match & wmask]) > limit
1164− && --chain_length != 0);
1165−
1166− if ((uInt)best_len <= s->lookahead) return (uInt)best_len;
1167− return s->lookahead;
1168−}
1169−#endif /* ASMV */
1170−#endif /* FASTEST */
1171−
1172−/* ---------------------------------------------------------------------------
1173− * Optimized version for level == 1 or strategy == Z_RLE only
1174− */
1175−local uInt longest_match_fast(s, cur_match)
1176− deflate_state *s;
1177− IPos cur_match; /* current match */
1178−{
1179− register Bytef *scan = s->window + s->strstart; /* current string */
1180− register Bytef *match; /* matched string */
1181− register int len; /* length of current match */
1182− register Bytef *strend = s->window + s->strstart + MAX_MATCH;
1183−
1184− /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.
1185− * It is easy to get rid of this optimization if necessary.
1186− */
1187− Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever");
1188−
1189− Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead");
1190−
1191− Assert(cur_match < s->strstart, "no future");
1192−
1193− match = s->window + cur_match;
1194−
1195− /* Return failure if the match length is less than 2:
1196− */
1197− if (match[0] != scan[0] || match[1] != scan[1]) return MIN_MATCH-1;
1198−
1199− /* The check at best_len-1 can be removed because it will be made
1200− * again later. (This heuristic is not always a win.)
1201− * It is not necessary to compare scan[2] and match[2] since they
1202− * are always equal when the other bytes match, given that
1203− * the hash keys are equal and that HASH_BITS >= 8.
1204− */
1205− scan += 2, match += 2;
1206− Assert(*scan == *match, "match[2]?");
1207−
1208− /* We check for insufficient lookahead only every 8th comparison;
1209− * the 256th check will be made at strstart+258.
1210− */
1211− do {
1212− } while (*++scan == *++match && *++scan == *++match &&
1213− *++scan == *++match && *++scan == *++match &&
1214− *++scan == *++match && *++scan == *++match &&
1215− *++scan == *++match && *++scan == *++match &&
1216− scan < strend);
1217−
1218− Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
1219−
1220− len = MAX_MATCH - (int)(strend - scan);
1221−
1222− if (len < MIN_MATCH) return MIN_MATCH - 1;
1223−
1224− s->match_start = cur_match;
1225− return (uInt)len <= s->lookahead ? (uInt)len : s->lookahead;
1226−}
1227−
1228−#ifdef DEBUG
1229−/* ===========================================================================
1230− * Check that the match at match_start is indeed a match.
1231− */
1232−local void check_match(s, start, match, length)
1233− deflate_state *s;
1234− IPos start, match;
1235− int length;
1236−{
1237− /* check that the match is indeed a match */
1238− if (zmemcmp(s->window + match,
1239− s->window + start, length) != EQUAL) {
1240− fprintf(stderr, " start %u, match %u, length %d\n",
1241− start, match, length);
1242− do {
1243− fprintf(stderr, "%c%c", s->window[match++], s->window[start++]);
1244− } while (--length != 0);
1245− z_error("invalid match");
1246− }
1247− if (z_verbose > 1) {
1248− fprintf(stderr,"\\[%d,%d]", start-match, length);
1249− do { putc(s->window[start++], stderr); } while (--length != 0);
1250− }
1251−}
1252−#else
1253−# define check_match(s, start, match, length)
1254−#endif /* DEBUG */
1255−
1256−/* ===========================================================================
1257− * Fill the window when the lookahead becomes insufficient.
1258− * Updates strstart and lookahead.
1259− *
1260− * IN assertion: lookahead < MIN_LOOKAHEAD
1261− * OUT assertions: strstart <= window_size-MIN_LOOKAHEAD
1262− * At least one byte has been read, or avail_in == 0; reads are
1263− * performed for at least two bytes (required for the zip translate_eol
1264− * option -- not supported here).
1265− */
1266−local void fill_window(s)
1267− deflate_state *s;
1268−{
1269− register unsigned n, m;
1270− register Posf *p;
1271− unsigned more; /* Amount of free space at the end of the window. */
1272− uInt wsize = s->w_size;
1273−
1274− do {
1275− more = (unsigned)(s->window_size -(ulg)s->lookahead -(ulg)s->strstart);
1276−
1277− /* Deal with !@#$% 64K limit: */
1278− if (sizeof(int) <= 2) {
1279− if (more == 0 && s->strstart == 0 && s->lookahead == 0) {
1280− more = wsize;
1281−
1282− } else if (more == (unsigned)(-1)) {
1283− /* Very unlikely, but possible on 16 bit machine if
1284− * strstart == 0 && lookahead == 1 (input done a byte at time)
1285− */
1286− more--;
1287− }
1288− }
1289−
1290− /* If the window is almost full and there is insufficient lookahead,
1291− * move the upper half to the lower one to make room in the upper half.
1292− */
1293− if (s->strstart >= wsize+MAX_DIST(s)) {
1294−
1295− zmemcpy(s->window, s->window+wsize, (unsigned)wsize);
1296− s->match_start -= wsize;
1297− s->strstart -= wsize; /* we now have strstart >= MAX_DIST */
1298− s->block_start -= (long) wsize;
1299−
1300− /* Slide the hash table (could be avoided with 32 bit values
1301− at the expense of memory usage). We slide even when level == 0
1302− to keep the hash table consistent if we switch back to level > 0
1303− later. (Using level 0 permanently is not an optimal usage of
1304− zlib, so we don't care about this pathological case.)
1305− */
1306− /* %%% avoid this when Z_RLE */
1307− n = s->hash_size;
1308− p = &s->head[n];
1309− do {
1310− m = *--p;
1311− *p = (Pos)(m >= wsize ? m-wsize : NIL);
1312− } while (--n);
1313−
1314− n = wsize;
1315−#ifndef FASTEST
1316− p = &s->prev[n];
1317− do {
1318− m = *--p;
1319− *p = (Pos)(m >= wsize ? m-wsize : NIL);
1320− /* If n is not on any hash chain, prev[n] is garbage but
1321− * its value will never be used.
1322− */
1323− } while (--n);
1324−#endif
1325− more += wsize;
1326− }
1327− if (s->strm->avail_in == 0) return;
1328−
1329− /* If there was no sliding:
1330− * strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 &&
1331− * more == window_size - lookahead - strstart
1332− * => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1)
1333− * => more >= window_size - 2*WSIZE + 2
1334− * In the BIG_MEM or MMAP case (not yet supported),
1335− * window_size == input_size + MIN_LOOKAHEAD &&
1336− * strstart + s->lookahead <= input_size => more >= MIN_LOOKAHEAD.
1337− * Otherwise, window_size == 2*WSIZE so more >= 2.
1338− * If there was sliding, more >= WSIZE. So in all cases, more >= 2.
1339− */
1340− Assert(more >= 2, "more < 2");
1341−
1342− n = read_buf(s->strm, s->window + s->strstart + s->lookahead, more);
1343− s->lookahead += n;
1344−
1345− /* Initialize the hash value now that we have some input: */
1346− if (s->lookahead >= MIN_MATCH) {
1347− s->ins_h = s->window[s->strstart];
1348− UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]);
1349−#if MIN_MATCH != 3
1350− Call UPDATE_HASH() MIN_MATCH-3 more times
1351−#endif
1352− }
1353− /* If the whole input has less than MIN_MATCH bytes, ins_h is garbage,
1354− * but this is not important since only literal bytes will be emitted.
1355− */
1356−
1357− } while (s->lookahead < MIN_LOOKAHEAD && s->strm->avail_in != 0);
1358−}
1359−
1360−/* ===========================================================================
1361− * Flush the current block, with given end-of-file flag.
1362− * IN assertion: strstart is set to the end of the current match.
1363− */
1364−#define FLUSH_BLOCK_ONLY(s, eof) { \
1365− _tr_flush_block(s, (s->block_start >= 0L ? \
1366− (charf *)&s->window[(unsigned)s->block_start] : \
1367− (charf *)Z_NULL), \
1368− (ulg)((long)s->strstart - s->block_start), \
1369− (eof)); \
1370− s->block_start = s->strstart; \
1371− flush_pending(s->strm); \
1372− Tracev((stderr,"[FLUSH]")); \
1373−}
1374−
1375−/* Same but force premature exit if necessary. */
1376−#define FLUSH_BLOCK(s, eof) { \
1377− FLUSH_BLOCK_ONLY(s, eof); \
1378− if (s->strm->avail_out == 0) return (eof) ? finish_started : need_more; \
1379−}
1380−
1381−/* ===========================================================================
1382− * Copy without compression as much as possible from the input stream, return
1383− * the current block state.
1384− * This function does not insert new strings in the dictionary since
1385− * uncompressible data is probably not useful. This function is used
1386− * only for the level=0 compression option.
1387− * NOTE: this function should be optimized to avoid extra copying from
1388− * window to pending_buf.
1389− */
1390−local block_state deflate_stored(s, flush)
1391− deflate_state *s;
1392− int flush;
1393−{
1394− /* Stored blocks are limited to 0xffff bytes, pending_buf is limited
1395− * to pending_buf_size, and each stored block has a 5 byte header:
1396− */
1397− ulg max_block_size = 0xffff;
1398− ulg max_start;
1399−
1400− if (max_block_size > s->pending_buf_size - 5) {
1401− max_block_size = s->pending_buf_size - 5;
1402− }
1403−
1404− /* Copy as much as possible from input to output: */
1405− for (;;) {
1406− /* Fill the window as much as possible: */
1407− if (s->lookahead <= 1) {
1408−
1409− Assert(s->strstart < s->w_size+MAX_DIST(s) ||
1410− s->block_start >= (long)s->w_size, "slide too late");
1411−
1412− fill_window(s);
1413− if (s->lookahead == 0 && flush == Z_NO_FLUSH) return need_more;
1414−
1415− if (s->lookahead == 0) break; /* flush the current block */
1416− }
1417− Assert(s->block_start >= 0L, "block gone");
1418−
1419− s->strstart += s->lookahead;
1420− s->lookahead = 0;
1421−
1422− /* Emit a stored block if pending_buf will be full: */
1423− max_start = s->block_start + max_block_size;
1424− if (s->strstart == 0 || (ulg)s->strstart >= max_start) {
1425− /* strstart == 0 is possible when wraparound on 16-bit machine */
1426− s->lookahead = (uInt)(s->strstart - max_start);
1427− s->strstart = (uInt)max_start;
1428− FLUSH_BLOCK(s, 0);
1429− }
1430− /* Flush if we may have to slide, otherwise block_start may become
1431− * negative and the data will be gone:
1432− */
1433− if (s->strstart - (uInt)s->block_start >= MAX_DIST(s)) {
1434− FLUSH_BLOCK(s, 0);
1435− }
1436− }
1437− FLUSH_BLOCK(s, flush == Z_FINISH);
1438− return flush == Z_FINISH ? finish_done : block_done;
1439−}
1440−
1441−/* ===========================================================================
1442− * Compress as much as possible from the input stream, return the current
1443− * block state.
1444− * This function does not perform lazy evaluation of matches and inserts
1445− * new strings in the dictionary only for unmatched strings or for short
1446− * matches. It is used only for the fast compression options.
1447− */
1448−local block_state deflate_fast(s, flush)
1449− deflate_state *s;
1450− int flush;
1451−{
1452− IPos hash_head = NIL; /* head of the hash chain */
1453− int bflush; /* set if current block must be flushed */
1454−
1455− for (;;) {
1456− /* Make sure that we always have enough lookahead, except
1457− * at the end of the input file. We need MAX_MATCH bytes
1458− * for the next match, plus MIN_MATCH bytes to insert the
1459− * string following the next match.
1460− */
1461− if (s->lookahead < MIN_LOOKAHEAD) {
1462− fill_window(s);
1463− if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
1464− return need_more;
1465− }
1466− if (s->lookahead == 0) break; /* flush the current block */
1467− }
1468−
1469− /* Insert the string window[strstart .. strstart+2] in the
1470− * dictionary, and set hash_head to the head of the hash chain:
1471− */
1472− if (s->lookahead >= MIN_MATCH) {
1473− INSERT_STRING(s, s->strstart, hash_head);
1474− }
1475−
1476− /* Find the longest match, discarding those <= prev_length.
1477− * At this point we have always match_length < MIN_MATCH
1478− */
1479− if (hash_head != NIL && s->strstart - hash_head <= MAX_DIST(s)) {
1480− /* To simplify the code, we prevent matches with the string
1481− * of window index 0 (in particular we have to avoid a match
1482− * of the string with itself at the start of the input file).
1483− */
1484−#ifdef FASTEST
1485− if ((s->strategy != Z_HUFFMAN_ONLY && s->strategy != Z_RLE) ||
1486− (s->strategy == Z_RLE && s->strstart - hash_head == 1)) {
1487− s->match_length = longest_match_fast (s, hash_head);
1488− }
1489−#else
1490− if (s->strategy != Z_HUFFMAN_ONLY && s->strategy != Z_RLE) {
1491− s->match_length = longest_match (s, hash_head);
1492− } else if (s->strategy == Z_RLE && s->strstart - hash_head == 1) {
1493− s->match_length = longest_match_fast (s, hash_head);
1494− }
1495−#endif
1496− /* longest_match() or longest_match_fast() sets match_start */
1497− }
1498− if (s->match_length >= MIN_MATCH) {
1499− check_match(s, s->strstart, s->match_start, s->match_length);
1500−
1501− _tr_tally_dist(s, s->strstart - s->match_start,
1502− s->match_length - MIN_MATCH, bflush);
1503−
1504− s->lookahead -= s->match_length;
1505−
1506− /* Insert new strings in the hash table only if the match length
1507− * is not too large. This saves time but degrades compression.
1508− */
1509−#ifndef FASTEST
1510− if (s->match_length <= s->max_insert_length &&
1511− s->lookahead >= MIN_MATCH) {
1512− s->match_length--; /* string at strstart already in table */
1513− do {
1514− s->strstart++;
1515− INSERT_STRING(s, s->strstart, hash_head);
1516− /* strstart never exceeds WSIZE-MAX_MATCH, so there are
1517− * always MIN_MATCH bytes ahead.
1518− */
1519− } while (--s->match_length != 0);
1520− s->strstart++;
1521− } else
1522−#endif
1523− {
1524− s->strstart += s->match_length;
1525− s->match_length = 0;
1526− s->ins_h = s->window[s->strstart];
1527− UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]);
1528−#if MIN_MATCH != 3
1529− Call UPDATE_HASH() MIN_MATCH-3 more times
1530−#endif
1531− /* If lookahead < MIN_MATCH, ins_h is garbage, but it does not
1532− * matter since it will be recomputed at next deflate call.
1533− */
1534− }
1535− } else {
1536− /* No match, output a literal byte */
1537− Tracevv((stderr,"%c", s->window[s->strstart]));
1538− _tr_tally_lit (s, s->window[s->strstart], bflush);
1539− s->lookahead--;
1540− s->strstart++;
1541− }
1542− if (bflush) FLUSH_BLOCK(s, 0);
1543− }
1544− FLUSH_BLOCK(s, flush == Z_FINISH);
1545− return flush == Z_FINISH ? finish_done : block_done;
1546−}
1547−
1548−#ifndef FASTEST
1549−/* ===========================================================================
1550− * Same as above, but achieves better compression. We use a lazy
1551− * evaluation for matches: a match is finally adopted only if there is
1552− * no better match at the next window position.
1553− */
1554−local block_state deflate_slow(s, flush)
1555− deflate_state *s;
1556− int flush;
1557−{
1558− IPos hash_head = NIL; /* head of hash chain */
1559− int bflush; /* set if current block must be flushed */
1560−
1561− /* Process the input block. */
1562− for (;;) {
1563− /* Make sure that we always have enough lookahead, except
1564− * at the end of the input file. We need MAX_MATCH bytes
1565− * for the next match, plus MIN_MATCH bytes to insert the
1566− * string following the next match.
1567− */
1568− if (s->lookahead < MIN_LOOKAHEAD) {
1569− fill_window(s);
1570− if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
1571− return need_more;
1572− }
1573− if (s->lookahead == 0) break; /* flush the current block */
1574− }
1575−
1576− /* Insert the string window[strstart .. strstart+2] in the
1577− * dictionary, and set hash_head to the head of the hash chain:
1578− */
1579− if (s->lookahead >= MIN_MATCH) {
1580− INSERT_STRING(s, s->strstart, hash_head);
1581− }
1582−
1583− /* Find the longest match, discarding those <= prev_length.
1584− */
1585− s->prev_length = s->match_length, s->prev_match = s->match_start;
1586− s->match_length = MIN_MATCH-1;
1587−
1588− if (hash_head != NIL && s->prev_length < s->max_lazy_match &&
1589− s->strstart - hash_head <= MAX_DIST(s)) {
1590− /* To simplify the code, we prevent matches with the string
1591− * of window index 0 (in particular we have to avoid a match
1592− * of the string with itself at the start of the input file).
1593− */
1594− if (s->strategy != Z_HUFFMAN_ONLY && s->strategy != Z_RLE) {
1595− s->match_length = longest_match (s, hash_head);
1596− } else if (s->strategy == Z_RLE && s->strstart - hash_head == 1) {
1597− s->match_length = longest_match_fast (s, hash_head);
1598− }
1599− /* longest_match() or longest_match_fast() sets match_start */
1600−
1601− if (s->match_length <= 5 && (s->strategy == Z_FILTERED
1602−#if TOO_FAR <= 32767
1603− || (s->match_length == MIN_MATCH &&
1604− s->strstart - s->match_start > TOO_FAR)
1605−#endif
1606− )) {
1607−
1608− /* If prev_match is also MIN_MATCH, match_start is garbage
1609− * but we will ignore the current match anyway.
1610− */
1611− s->match_length = MIN_MATCH-1;
1612− }
1613− }
1614− /* If there was a match at the previous step and the current
1615− * match is not better, output the previous match:
1616− */
1617− if (s->prev_length >= MIN_MATCH && s->match_length <= s->prev_length) {
1618− uInt max_insert = s->strstart + s->lookahead - MIN_MATCH;
1619− /* Do not insert strings in hash table beyond this. */
1620−
1621− check_match(s, s->strstart-1, s->prev_match, s->prev_length);
1622−
1623− _tr_tally_dist(s, s->strstart -1 - s->prev_match,
1624− s->prev_length - MIN_MATCH, bflush);
1625−
1626− /* Insert in hash table all strings up to the end of the match.
1627− * strstart-1 and strstart are already inserted. If there is not
1628− * enough lookahead, the last two strings are not inserted in
1629− * the hash table.
1630− */
1631− s->lookahead -= s->prev_length-1;
1632− s->prev_length -= 2;
1633− do {
1634− if (++s->strstart <= max_insert) {
1635− INSERT_STRING(s, s->strstart, hash_head);
1636− }
1637− } while (--s->prev_length != 0);
1638− s->match_available = 0;
1639− s->match_length = MIN_MATCH-1;
1640− s->strstart++;
1641−
1642− if (bflush) FLUSH_BLOCK(s, 0);
1643−
1644− } else if (s->match_available) {
1645− /* If there was no match at the previous position, output a
1646− * single literal. If there was a match but the current match
1647− * is longer, truncate the previous match to a single literal.
1648− */
1649− Tracevv((stderr,"%c", s->window[s->strstart-1]));
1650− _tr_tally_lit(s, s->window[s->strstart-1], bflush);
1651− if (bflush) {
1652− FLUSH_BLOCK_ONLY(s, 0);
1653− }
1654− s->strstart++;
1655− s->lookahead--;
1656− if (s->strm->avail_out == 0) return need_more;
1657− } else {
1658− /* There is no previous match to compare with, wait for
1659− * the next step to decide.
1660− */
1661− s->match_available = 1;
1662− s->strstart++;
1663− s->lookahead--;
1664− }
1665− }
1666− Assert (flush != Z_NO_FLUSH, "no flush?");
1667− if (s->match_available) {
1668− Tracevv((stderr,"%c", s->window[s->strstart-1]));
1669− _tr_tally_lit(s, s->window[s->strstart-1], bflush);
1670− s->match_available = 0;
1671− }
1672− FLUSH_BLOCK(s, flush == Z_FINISH);
1673− return flush == Z_FINISH ? finish_done : block_done;
1674−}
1675−#endif /* FASTEST */
1676−
1677−#if 0
1678−/* ===========================================================================
1679− * For Z_RLE, simply look for runs of bytes, generate matches only of distance
1680− * one. Do not maintain a hash table. (It will be regenerated if this run of
1681− * deflate switches away from Z_RLE.)
1682− */
1683−local block_state deflate_rle(s, flush)
1684− deflate_state *s;
1685− int flush;
1686−{
1687− int bflush; /* set if current block must be flushed */
1688− uInt run; /* length of run */
1689− uInt max; /* maximum length of run */
1690− uInt prev; /* byte at distance one to match */
1691− Bytef *scan; /* scan for end of run */
1692−
1693− for (;;) {
1694− /* Make sure that we always have enough lookahead, except
1695− * at the end of the input file. We need MAX_MATCH bytes
1696− * for the longest encodable run.
1697− */
1698− if (s->lookahead < MAX_MATCH) {
1699− fill_window(s);
1700− if (s->lookahead < MAX_MATCH && flush == Z_NO_FLUSH) {
1701− return need_more;
1702− }
1703− if (s->lookahead == 0) break; /* flush the current block */
1704− }
1705−
1706− /* See how many times the previous byte repeats */
1707− run = 0;
1708− if (s->strstart > 0) { /* if there is a previous byte, that is */
1709− max = s->lookahead < MAX_MATCH ? s->lookahead : MAX_MATCH;
1710− scan = s->window + s->strstart - 1;
1711− prev = *scan++;
1712− do {
1713− if (*scan++ != prev)
1714− break;
1715− } while (++run < max);
1716− }
1717−
1718− /* Emit match if have run of MIN_MATCH or longer, else emit literal */
1719− if (run >= MIN_MATCH) {
1720− check_match(s, s->strstart, s->strstart - 1, run);
1721− _tr_tally_dist(s, 1, run - MIN_MATCH, bflush);
1722− s->lookahead -= run;
1723− s->strstart += run;
1724− } else {
1725− /* No match, output a literal byte */
1726− Tracevv((stderr,"%c", s->window[s->strstart]));
1727− _tr_tally_lit (s, s->window[s->strstart], bflush);
1728− s->lookahead--;
1729− s->strstart++;
1730− }
1731− if (bflush) FLUSH_BLOCK(s, 0);
1732− }
1733− FLUSH_BLOCK(s, flush == Z_FINISH);
1734− return flush == Z_FINISH ? finish_done : block_done;
1735−}
1736−#endif
cbits/deflate.hdeleted−331
@@ -1,331 +0,0 @@
1−/* deflate.h -- internal compression state
2− * Copyright (C) 1995-2004 Jean-loup Gailly
3− * For conditions of distribution and use, see copyright notice in zlib.h
4− */
5−
6−/* WARNING: this file should *not* be used by applications. It is
7− part of the implementation of the compression library and is
8− subject to change. Applications should only use zlib.h.
9− */
10−
11−/* @(#) $Id$ */
12−
13−#ifndef DEFLATE_H
14−#define DEFLATE_H
15−
16−#include "zutil.h"
17−
18−/* define NO_GZIP when compiling if you want to disable gzip header and
19− trailer creation by deflate(). NO_GZIP would be used to avoid linking in
20− the crc code when it is not needed. For shared libraries, gzip encoding
21− should be left enabled. */
22−#ifndef NO_GZIP
23−# define GZIP
24−#endif
25−
26−/* ===========================================================================
27− * Internal compression state.
28− */
29−
30−#define LENGTH_CODES 29
31−/* number of length codes, not counting the special END_BLOCK code */
32−
33−#define LITERALS 256
34−/* number of literal bytes 0..255 */
35−
36−#define L_CODES (LITERALS+1+LENGTH_CODES)
37−/* number of Literal or Length codes, including the END_BLOCK code */
38−
39−#define D_CODES 30
40−/* number of distance codes */
41−
42−#define BL_CODES 19
43−/* number of codes used to transfer the bit lengths */
44−
45−#define HEAP_SIZE (2*L_CODES+1)
46−/* maximum heap size */
47−
48−#define MAX_BITS 15
49−/* All codes must not exceed MAX_BITS bits */
50−
51−#define INIT_STATE 42
52−#define EXTRA_STATE 69
53−#define NAME_STATE 73
54−#define COMMENT_STATE 91
55−#define HCRC_STATE 103
56−#define BUSY_STATE 113
57−#define FINISH_STATE 666
58−/* Stream status */
59−
60−
61−/* Data structure describing a single value and its code string. */
62−typedef struct ct_data_s {
63− union {
64− ush freq; /* frequency count */
65− ush code; /* bit string */
66− } fc;
67− union {
68− ush dad; /* father node in Huffman tree */
69− ush len; /* length of bit string */
70− } dl;
71−} FAR ct_data;
72−
73−#define Freq fc.freq
74−#define Code fc.code
75−#define Dad dl.dad
76−#define Len dl.len
77−
78−typedef struct static_tree_desc_s static_tree_desc;
79−
80−typedef struct tree_desc_s {
81− ct_data *dyn_tree; /* the dynamic tree */
82− int max_code; /* largest code with non zero frequency */
83− static_tree_desc *stat_desc; /* the corresponding static tree */
84−} FAR tree_desc;
85−
86−typedef ush Pos;
87−typedef Pos FAR Posf;
88−typedef unsigned IPos;
89−
90−/* A Pos is an index in the character window. We use short instead of int to
91− * save space in the various tables. IPos is used only for parameter passing.
92− */
93−
94−typedef struct internal_state {
95− z_streamp strm; /* pointer back to this zlib stream */
96− int status; /* as the name implies */
97− Bytef *pending_buf; /* output still pending */
98− ulg pending_buf_size; /* size of pending_buf */
99− Bytef *pending_out; /* next pending byte to output to the stream */
100− uInt pending; /* nb of bytes in the pending buffer */
101− int wrap; /* bit 0 true for zlib, bit 1 true for gzip */
102− gz_headerp gzhead; /* gzip header information to write */
103− uInt gzindex; /* where in extra, name, or comment */
104− Byte method; /* STORED (for zip only) or DEFLATED */
105− int last_flush; /* value of flush param for previous deflate call */
106−
107− /* used by deflate.c: */
108−
109− uInt w_size; /* LZ77 window size (32K by default) */
110− uInt w_bits; /* log2(w_size) (8..16) */
111− uInt w_mask; /* w_size - 1 */
112−
113− Bytef *window;
114− /* Sliding window. Input bytes are read into the second half of the window,
115− * and move to the first half later to keep a dictionary of at least wSize
116− * bytes. With this organization, matches are limited to a distance of
117− * wSize-MAX_MATCH bytes, but this ensures that IO is always
118− * performed with a length multiple of the block size. Also, it limits
119− * the window size to 64K, which is quite useful on MSDOS.
120− * To do: use the user input buffer as sliding window.
121− */
122−
123− ulg window_size;
124− /* Actual size of window: 2*wSize, except when the user input buffer
125− * is directly used as sliding window.
126− */
127−
128− Posf *prev;
129− /* Link to older string with same hash index. To limit the size of this
130− * array to 64K, this link is maintained only for the last 32K strings.
131− * An index in this array is thus a window index modulo 32K.
132− */
133−
134− Posf *head; /* Heads of the hash chains or NIL. */
135−
136− uInt ins_h; /* hash index of string to be inserted */
137− uInt hash_size; /* number of elements in hash table */
138− uInt hash_bits; /* log2(hash_size) */
139− uInt hash_mask; /* hash_size-1 */
140−
141− uInt hash_shift;
142− /* Number of bits by which ins_h must be shifted at each input
143− * step. It must be such that after MIN_MATCH steps, the oldest
144− * byte no longer takes part in the hash key, that is:
145− * hash_shift * MIN_MATCH >= hash_bits
146− */
147−
148− long block_start;
149− /* Window position at the beginning of the current output block. Gets
150− * negative when the window is moved backwards.
151− */
152−
153− uInt match_length; /* length of best match */
154− IPos prev_match; /* previous match */
155− int match_available; /* set if previous match exists */
156− uInt strstart; /* start of string to insert */
157− uInt match_start; /* start of matching string */
158− uInt lookahead; /* number of valid bytes ahead in window */
159−
160− uInt prev_length;
161− /* Length of the best match at previous step. Matches not greater than this
162− * are discarded. This is used in the lazy match evaluation.
163− */
164−
165− uInt max_chain_length;
166− /* To speed up deflation, hash chains are never searched beyond this
167− * length. A higher limit improves compression ratio but degrades the
168− * speed.
169− */
170−
171− uInt max_lazy_match;
172− /* Attempt to find a better match only when the current match is strictly
173− * smaller than this value. This mechanism is used only for compression
174− * levels >= 4.
175− */
176−# define max_insert_length max_lazy_match
177− /* Insert new strings in the hash table only if the match length is not
178− * greater than this length. This saves time but degrades compression.
179− * max_insert_length is used only for compression levels <= 3.
180− */
181−
182− int level; /* compression level (1..9) */
183− int strategy; /* favor or force Huffman coding*/
184−
185− uInt good_match;
186− /* Use a faster search when the previous match is longer than this */
187−
188− int nice_match; /* Stop searching when current match exceeds this */
189−
190− /* used by trees.c: */
191− /* Didn't use ct_data typedef below to supress compiler warning */
192− struct ct_data_s dyn_ltree[HEAP_SIZE]; /* literal and length tree */
193− struct ct_data_s dyn_dtree[2*D_CODES+1]; /* distance tree */
194− struct ct_data_s bl_tree[2*BL_CODES+1]; /* Huffman tree for bit lengths */
195−
196− struct tree_desc_s l_desc; /* desc. for literal tree */
197− struct tree_desc_s d_desc; /* desc. for distance tree */
198− struct tree_desc_s bl_desc; /* desc. for bit length tree */
199−
200− ush bl_count[MAX_BITS+1];
201− /* number of codes at each bit length for an optimal tree */
202−
203− int heap[2*L_CODES+1]; /* heap used to build the Huffman trees */
204− int heap_len; /* number of elements in the heap */
205− int heap_max; /* element of largest frequency */
206− /* The sons of heap[n] are heap[2*n] and heap[2*n+1]. heap[0] is not used.
207− * The same heap array is used to build all trees.
208− */
209−
210− uch depth[2*L_CODES+1];
211− /* Depth of each subtree used as tie breaker for trees of equal frequency
212− */
213−
214− uchf *l_buf; /* buffer for literals or lengths */
215−
216− uInt lit_bufsize;
217− /* Size of match buffer for literals/lengths. There are 4 reasons for
218− * limiting lit_bufsize to 64K:
219− * - frequencies can be kept in 16 bit counters
220− * - if compression is not successful for the first block, all input
221− * data is still in the window so we can still emit a stored block even
222− * when input comes from standard input. (This can also be done for
223− * all blocks if lit_bufsize is not greater than 32K.)
224− * - if compression is not successful for a file smaller than 64K, we can
225− * even emit a stored file instead of a stored block (saving 5 bytes).
226− * This is applicable only for zip (not gzip or zlib).
227− * - creating new Huffman trees less frequently may not provide fast
228− * adaptation to changes in the input data statistics. (Take for
229− * example a binary file with poorly compressible code followed by
230− * a highly compressible string table.) Smaller buffer sizes give
231− * fast adaptation but have of course the overhead of transmitting
232− * trees more frequently.
233− * - I can't count above 4
234− */
235−
236− uInt last_lit; /* running index in l_buf */
237−
238− ushf *d_buf;
239− /* Buffer for distances. To simplify the code, d_buf and l_buf have
240− * the same number of elements. To use different lengths, an extra flag
241− * array would be necessary.
242− */
243−
244− ulg opt_len; /* bit length of current block with optimal trees */
245− ulg static_len; /* bit length of current block with static trees */
246− uInt matches; /* number of string matches in current block */
247− int last_eob_len; /* bit length of EOB code for last block */
248−
249−#ifdef DEBUG
250− ulg compressed_len; /* total bit length of compressed file mod 2^32 */
251− ulg bits_sent; /* bit length of compressed data sent mod 2^32 */
252−#endif
253−
254− ush bi_buf;
255− /* Output buffer. bits are inserted starting at the bottom (least
256− * significant bits).
257− */
258− int bi_valid;
259− /* Number of valid bits in bi_buf. All bits above the last valid bit
260− * are always zero.
261− */
262−
263−} FAR deflate_state;
264−
265−/* Output a byte on the stream.
266− * IN assertion: there is enough room in pending_buf.
267− */
268−#define put_byte(s, c) {s->pending_buf[s->pending++] = (c);}
269−
270−
271−#define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1)
272−/* Minimum amount of lookahead, except at the end of the input file.
273− * See deflate.c for comments about the MIN_MATCH+1.
274− */
275−
276−#define MAX_DIST(s) ((s)->w_size-MIN_LOOKAHEAD)
277−/* In order to simplify the code, particularly on 16 bit machines, match
278− * distances are limited to MAX_DIST instead of WSIZE.
279− */
280−
281− /* in trees.c */
282−void _tr_init OF((deflate_state *s));
283−int _tr_tally OF((deflate_state *s, unsigned dist, unsigned lc));
284−void _tr_flush_block OF((deflate_state *s, charf *buf, ulg stored_len,
285− int eof));
286−void _tr_align OF((deflate_state *s));
287−void _tr_stored_block OF((deflate_state *s, charf *buf, ulg stored_len,
288− int eof));
289−
290−#define d_code(dist) \
291− ((dist) < 256 ? _dist_code[dist] : _dist_code[256+((dist)>>7)])
292−/* Mapping from a distance to a distance code. dist is the distance - 1 and
293− * must not have side effects. _dist_code[256] and _dist_code[257] are never
294− * used.
295− */
296−
297−#ifndef DEBUG
298−/* Inline versions of _tr_tally for speed: */
299−
300−#if defined(GEN_TREES_H) || !defined(STDC)
301− extern uch _length_code[];
302− extern uch _dist_code[];
303−#else
304− extern const uch _length_code[];
305− extern const uch _dist_code[];
306−#endif
307−
308−# define _tr_tally_lit(s, c, flush) \
309− { uch cc = (c); \
310− s->d_buf[s->last_lit] = 0; \
311− s->l_buf[s->last_lit++] = cc; \
312− s->dyn_ltree[cc].Freq++; \
313− flush = (s->last_lit == s->lit_bufsize-1); \
314− }
315−# define _tr_tally_dist(s, distance, length, flush) \
316− { uch len = (length); \
317− ush dist = (distance); \
318− s->d_buf[s->last_lit] = dist; \
319− s->l_buf[s->last_lit++] = len; \
320− dist--; \
321− s->dyn_ltree[_length_code[len]+LITERALS+1].Freq++; \
322− s->dyn_dtree[d_code(dist)].Freq++; \
323− flush = (s->last_lit == s->lit_bufsize-1); \
324− }
325−#else
326−# define _tr_tally_lit(s, c, flush) flush = _tr_tally(s, 0, c)
327−# define _tr_tally_dist(s, distance, length, flush) \
328− flush = _tr_tally(s, distance, length)
329−#endif
330−
331−#endif /* DEFLATE_H */
cbits/gzio.cdeleted−1026
@@ -1,1026 +0,0 @@
1−/* gzio.c -- IO on .gz files
2− * Copyright (C) 1995-2005 Jean-loup Gailly.
3− * For conditions of distribution and use, see copyright notice in zlib.h
4− *
5− * Compile this file with -DNO_GZCOMPRESS to avoid the compression code.
6− */
7−
8−/* @(#) $Id$ */
9−
10−#include <stdio.h>
11−
12−#include "zutil.h"
13−
14−#ifdef NO_DEFLATE /* for compatibility with old definition */
15−# define NO_GZCOMPRESS
16−#endif
17−
18−#ifndef NO_DUMMY_DECL
19−struct internal_state {int dummy;}; /* for buggy compilers */
20−#endif
21−
22−#ifndef Z_BUFSIZE
23−# ifdef MAXSEG_64K
24−# define Z_BUFSIZE 4096 /* minimize memory usage for 16-bit DOS */
25−# else
26−# define Z_BUFSIZE 16384
27−# endif
28−#endif
29−#ifndef Z_PRINTF_BUFSIZE
30−# define Z_PRINTF_BUFSIZE 4096
31−#endif
32−
33−#ifdef __MVS__
34−# pragma map (fdopen , "\174\174FDOPEN")
35− FILE *fdopen(int, const char *);
36−#endif
37−
38−#ifndef STDC
39−extern voidp malloc OF((uInt size));
40−extern void free OF((voidpf ptr));
41−#endif
42−
43−#define ALLOC(size) malloc(size)
44−#define TRYFREE(p) {if (p) free(p);}
45−
46−static int const gz_magic[2] = {0x1f, 0x8b}; /* gzip magic header */
47−
48−/* gzip flag byte */
49−#define ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */
50−#define HEAD_CRC 0x02 /* bit 1 set: header CRC present */
51−#define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
52−#define ORIG_NAME 0x08 /* bit 3 set: original file name present */
53−#define COMMENT 0x10 /* bit 4 set: file comment present */
54−#define RESERVED 0xE0 /* bits 5..7: reserved */
55−
56−typedef struct gz_stream {
57− z_stream stream;
58− int z_err; /* error code for last stream operation */
59− int z_eof; /* set if end of input file */
60− FILE *file; /* .gz file */
61− Byte *inbuf; /* input buffer */
62− Byte *outbuf; /* output buffer */
63− uLong crc; /* crc32 of uncompressed data */
64− char *msg; /* error message */
65− char *path; /* path name for debugging only */
66− int transparent; /* 1 if input file is not a .gz file */
67− char mode; /* 'w' or 'r' */
68− z_off_t start; /* start of compressed data in file (header skipped) */
69− z_off_t in; /* bytes into deflate or inflate */
70− z_off_t out; /* bytes out of deflate or inflate */
71− int back; /* one character push-back */
72− int last; /* true if push-back is last character */
73−} gz_stream;
74−
75−
76−local gzFile gz_open OF((const char *path, const char *mode, int fd));
77−local int do_flush OF((gzFile file, int flush));
78−local int get_byte OF((gz_stream *s));
79−local void check_header OF((gz_stream *s));
80−local int destroy OF((gz_stream *s));
81−local void putLong OF((FILE *file, uLong x));
82−local uLong getLong OF((gz_stream *s));
83−
84−/* ===========================================================================
85− Opens a gzip (.gz) file for reading or writing. The mode parameter
86− is as in fopen ("rb" or "wb"). The file is given either by file descriptor
87− or path name (if fd == -1).
88− gz_open returns NULL if the file could not be opened or if there was
89− insufficient memory to allocate the (de)compression state; errno
90− can be checked to distinguish the two cases (if errno is zero, the
91− zlib error is Z_MEM_ERROR).
92−*/
93−local gzFile gz_open (path, mode, fd)
94− const char *path;
95− const char *mode;
96− int fd;
97−{
98− int err;
99− int level = Z_DEFAULT_COMPRESSION; /* compression level */
100− int strategy = Z_DEFAULT_STRATEGY; /* compression strategy */
101− char *p = (char*)mode;
102− gz_stream *s;
103− char fmode[80]; /* copy of mode, without the compression level */
104− char *m = fmode;
105−
106− if (!path || !mode) return Z_NULL;
107−
108− s = (gz_stream *)ALLOC(sizeof(gz_stream));
109− if (!s) return Z_NULL;
110−
111− s->stream.zalloc = (alloc_func)0;
112− s->stream.zfree = (free_func)0;
113− s->stream.opaque = (voidpf)0;
114− s->stream.next_in = s->inbuf = Z_NULL;
115− s->stream.next_out = s->outbuf = Z_NULL;
116− s->stream.avail_in = s->stream.avail_out = 0;
117− s->file = NULL;
118− s->z_err = Z_OK;
119− s->z_eof = 0;
120− s->in = 0;
121− s->out = 0;
122− s->back = EOF;
123− s->crc = crc32(0L, Z_NULL, 0);
124− s->msg = NULL;
125− s->transparent = 0;
126−
127− s->path = (char*)ALLOC(strlen(path)+1);
128− if (s->path == NULL) {
129− return destroy(s), (gzFile)Z_NULL;
130− }
131− strcpy(s->path, path); /* do this early for debugging */
132−
133− s->mode = '\0';
134− do {
135− if (*p == 'r') s->mode = 'r';
136− if (*p == 'w' || *p == 'a') s->mode = 'w';
137− if (*p >= '0' && *p <= '9') {
138− level = *p - '0';
139− } else if (*p == 'f') {
140− strategy = Z_FILTERED;
141− } else if (*p == 'h') {
142− strategy = Z_HUFFMAN_ONLY;
143− } else if (*p == 'R') {
144− strategy = Z_RLE;
145− } else {
146− *m++ = *p; /* copy the mode */
147− }
148− } while (*p++ && m != fmode + sizeof(fmode));
149− if (s->mode == '\0') return destroy(s), (gzFile)Z_NULL;
150−
151− if (s->mode == 'w') {
152−#ifdef NO_GZCOMPRESS
153− err = Z_STREAM_ERROR;
154−#else
155− err = deflateInit2(&(s->stream), level,
156− Z_DEFLATED, -MAX_WBITS, DEF_MEM_LEVEL, strategy);
157− /* windowBits is passed < 0 to suppress zlib header */
158−
159− s->stream.next_out = s->outbuf = (Byte*)ALLOC(Z_BUFSIZE);
160−#endif
161− if (err != Z_OK || s->outbuf == Z_NULL) {
162− return destroy(s), (gzFile)Z_NULL;
163− }
164− } else {
165− s->stream.next_in = s->inbuf = (Byte*)ALLOC(Z_BUFSIZE);
166−
167− err = inflateInit2(&(s->stream), -MAX_WBITS);
168− /* windowBits is passed < 0 to tell that there is no zlib header.
169− * Note that in this case inflate *requires* an extra "dummy" byte
170− * after the compressed stream in order to complete decompression and
171− * return Z_STREAM_END. Here the gzip CRC32 ensures that 4 bytes are
172− * present after the compressed stream.
173− */
174− if (err != Z_OK || s->inbuf == Z_NULL) {
175− return destroy(s), (gzFile)Z_NULL;
176− }
177− }
178− s->stream.avail_out = Z_BUFSIZE;
179−
180− errno = 0;
181− s->file = fd < 0 ? F_OPEN(path, fmode) : (FILE*)fdopen(fd, fmode);
182−
183− if (s->file == NULL) {
184− return destroy(s), (gzFile)Z_NULL;
185− }
186− if (s->mode == 'w') {
187− /* Write a very simple .gz header:
188− */
189− fprintf(s->file, "%c%c%c%c%c%c%c%c%c%c", gz_magic[0], gz_magic[1],
190− Z_DEFLATED, 0 /*flags*/, 0,0,0,0 /*time*/, 0 /*xflags*/, OS_CODE);
191− s->start = 10L;
192− /* We use 10L instead of ftell(s->file) to because ftell causes an
193− * fflush on some systems. This version of the library doesn't use
194− * start anyway in write mode, so this initialization is not
195− * necessary.
196− */
197− } else {
198− check_header(s); /* skip the .gz header */
199− s->start = ftell(s->file) - s->stream.avail_in;
200− }
201−
202− return (gzFile)s;
203−}
204−
205−/* ===========================================================================
206− Opens a gzip (.gz) file for reading or writing.
207−*/
208−gzFile ZEXPORT gzopen (path, mode)
209− const char *path;
210− const char *mode;
211−{
212− return gz_open (path, mode, -1);
213−}
214−
215−/* ===========================================================================
216− Associate a gzFile with the file descriptor fd. fd is not dup'ed here
217− to mimic the behavio(u)r of fdopen.
218−*/
219−gzFile ZEXPORT gzdopen (fd, mode)
220− int fd;
221− const char *mode;
222−{
223− char name[46]; /* allow for up to 128-bit integers */
224−
225− if (fd < 0) return (gzFile)Z_NULL;
226− sprintf(name, "<fd:%d>", fd); /* for debugging */
227−
228− return gz_open (name, mode, fd);
229−}
230−
231−/* ===========================================================================
232− * Update the compression level and strategy
233− */
234−int ZEXPORT gzsetparams (file, level, strategy)
235− gzFile file;
236− int level;
237− int strategy;
238−{
239− gz_stream *s = (gz_stream*)file;
240−
241− if (s == NULL || s->mode != 'w') return Z_STREAM_ERROR;
242−
243− /* Make room to allow flushing */
244− if (s->stream.avail_out == 0) {
245−
246− s->stream.next_out = s->outbuf;
247− if (fwrite(s->outbuf, 1, Z_BUFSIZE, s->file) != Z_BUFSIZE) {
248− s->z_err = Z_ERRNO;
249− }
250− s->stream.avail_out = Z_BUFSIZE;
251− }
252−
253− return deflateParams (&(s->stream), level, strategy);
254−}
255−
256−/* ===========================================================================
257− Read a byte from a gz_stream; update next_in and avail_in. Return EOF
258− for end of file.
259− IN assertion: the stream s has been sucessfully opened for reading.
260−*/
261−local int get_byte(s)
262− gz_stream *s;
263−{
264− if (s->z_eof) return EOF;
265− if (s->stream.avail_in == 0) {
266− errno = 0;
267− s->stream.avail_in = (uInt)fread(s->inbuf, 1, Z_BUFSIZE, s->file);
268− if (s->stream.avail_in == 0) {
269− s->z_eof = 1;
270− if (ferror(s->file)) s->z_err = Z_ERRNO;
271− return EOF;
272− }
273− s->stream.next_in = s->inbuf;
274− }
275− s->stream.avail_in--;
276− return *(s->stream.next_in)++;
277−}
278−
279−/* ===========================================================================
280− Check the gzip header of a gz_stream opened for reading. Set the stream
281− mode to transparent if the gzip magic header is not present; set s->err
282− to Z_DATA_ERROR if the magic header is present but the rest of the header
283− is incorrect.
284− IN assertion: the stream s has already been created sucessfully;
285− s->stream.avail_in is zero for the first time, but may be non-zero
286− for concatenated .gz files.
287−*/
288−local void check_header(s)
289− gz_stream *s;
290−{
291− int method; /* method byte */
292− int flags; /* flags byte */
293− uInt len;
294− int c;
295−
296− /* Assure two bytes in the buffer so we can peek ahead -- handle case
297− where first byte of header is at the end of the buffer after the last
298− gzip segment */
299− len = s->stream.avail_in;
300− if (len < 2) {
301− if (len) s->inbuf[0] = s->stream.next_in[0];
302− errno = 0;
303− len = (uInt)fread(s->inbuf + len, 1, Z_BUFSIZE >> len, s->file);
304− if (len == 0 && ferror(s->file)) s->z_err = Z_ERRNO;
305− s->stream.avail_in += len;
306− s->stream.next_in = s->inbuf;
307− if (s->stream.avail_in < 2) {
308− s->transparent = s->stream.avail_in;
309− return;
310− }
311− }
312−
313− /* Peek ahead to check the gzip magic header */
314− if (s->stream.next_in[0] != gz_magic[0] ||
315− s->stream.next_in[1] != gz_magic[1]) {
316− s->transparent = 1;
317− return;
318− }
319− s->stream.avail_in -= 2;
320− s->stream.next_in += 2;
321−
322− /* Check the rest of the gzip header */
323− method = get_byte(s);
324− flags = get_byte(s);
325− if (method != Z_DEFLATED || (flags & RESERVED) != 0) {
326− s->z_err = Z_DATA_ERROR;
327− return;
328− }
329−
330− /* Discard time, xflags and OS code: */
331− for (len = 0; len < 6; len++) (void)get_byte(s);
332−
333− if ((flags & EXTRA_FIELD) != 0) { /* skip the extra field */
334− len = (uInt)get_byte(s);
335− len += ((uInt)get_byte(s))<<8;
336− /* len is garbage if EOF but the loop below will quit anyway */
337− while (len-- != 0 && get_byte(s) != EOF) ;
338− }
339− if ((flags & ORIG_NAME) != 0) { /* skip the original file name */
340− while ((c = get_byte(s)) != 0 && c != EOF) ;
341− }
342− if ((flags & COMMENT) != 0) { /* skip the .gz file comment */
343− while ((c = get_byte(s)) != 0 && c != EOF) ;
344− }
345− if ((flags & HEAD_CRC) != 0) { /* skip the header crc */
346− for (len = 0; len < 2; len++) (void)get_byte(s);
347− }
348− s->z_err = s->z_eof ? Z_DATA_ERROR : Z_OK;
349−}
350−
351− /* ===========================================================================
352− * Cleanup then free the given gz_stream. Return a zlib error code.
353− Try freeing in the reverse order of allocations.
354− */
355−local int destroy (s)
356− gz_stream *s;
357−{
358− int err = Z_OK;
359−
360− if (!s) return Z_STREAM_ERROR;
361−
362− TRYFREE(s->msg);
363−
364− if (s->stream.state != NULL) {
365− if (s->mode == 'w') {
366−#ifdef NO_GZCOMPRESS
367− err = Z_STREAM_ERROR;
368−#else
369− err = deflateEnd(&(s->stream));
370−#endif
371− } else if (s->mode == 'r') {
372− err = inflateEnd(&(s->stream));
373− }
374− }
375− if (s->file != NULL && fclose(s->file)) {
376−#ifdef ESPIPE
377− if (errno != ESPIPE) /* fclose is broken for pipes in HP/UX */
378−#endif
379− err = Z_ERRNO;
380− }
381− if (s->z_err < 0) err = s->z_err;
382−
383− TRYFREE(s->inbuf);
384− TRYFREE(s->outbuf);
385− TRYFREE(s->path);
386− TRYFREE(s);
387− return err;
388−}
389−
390−/* ===========================================================================
391− Reads the given number of uncompressed bytes from the compressed file.
392− gzread returns the number of bytes actually read (0 for end of file).
393−*/
394−int ZEXPORT gzread (file, buf, len)
395− gzFile file;
396− voidp buf;
397− unsigned len;
398−{
399− gz_stream *s = (gz_stream*)file;
400− Bytef *start = (Bytef*)buf; /* starting point for crc computation */
401− Byte *next_out; /* == stream.next_out but not forced far (for MSDOS) */
402−
403− if (s == NULL || s->mode != 'r') return Z_STREAM_ERROR;
404−
405− if (s->z_err == Z_DATA_ERROR || s->z_err == Z_ERRNO) return -1;
406− if (s->z_err == Z_STREAM_END) return 0; /* EOF */
407−
408− next_out = (Byte*)buf;
409− s->stream.next_out = (Bytef*)buf;
410− s->stream.avail_out = len;
411−
412− if (s->stream.avail_out && s->back != EOF) {
413− *next_out++ = s->back;
414− s->stream.next_out++;
415− s->stream.avail_out--;
416− s->back = EOF;
417− s->out++;
418− start++;
419− if (s->last) {
420− s->z_err = Z_STREAM_END;
421− return 1;
422− }
423− }
424−
425− while (s->stream.avail_out != 0) {
426−
427− if (s->transparent) {
428− /* Copy first the lookahead bytes: */
429− uInt n = s->stream.avail_in;
430− if (n > s->stream.avail_out) n = s->stream.avail_out;
431− if (n > 0) {
432− zmemcpy(s->stream.next_out, s->stream.next_in, n);
433− next_out += n;
434− s->stream.next_out = next_out;
435− s->stream.next_in += n;
436− s->stream.avail_out -= n;
437− s->stream.avail_in -= n;
438− }
439− if (s->stream.avail_out > 0) {
440− s->stream.avail_out -=
441− (uInt)fread(next_out, 1, s->stream.avail_out, s->file);
442− }
443− len -= s->stream.avail_out;
444− s->in += len;
445− s->out += len;
446− if (len == 0) s->z_eof = 1;
447− return (int)len;
448− }
449− if (s->stream.avail_in == 0 && !s->z_eof) {
450−
451− errno = 0;
452− s->stream.avail_in = (uInt)fread(s->inbuf, 1, Z_BUFSIZE, s->file);
453− if (s->stream.avail_in == 0) {
454− s->z_eof = 1;
455− if (ferror(s->file)) {
456− s->z_err = Z_ERRNO;
457− break;
458− }
459− }
460− s->stream.next_in = s->inbuf;
461− }
462− s->in += s->stream.avail_in;
463− s->out += s->stream.avail_out;
464− s->z_err = inflate(&(s->stream), Z_NO_FLUSH);
465− s->in -= s->stream.avail_in;
466− s->out -= s->stream.avail_out;
467−
468− if (s->z_err == Z_STREAM_END) {
469− /* Check CRC and original size */
470− s->crc = crc32(s->crc, start, (uInt)(s->stream.next_out - start));
471− start = s->stream.next_out;
472−
473− if (getLong(s) != s->crc) {
474− s->z_err = Z_DATA_ERROR;
475− } else {
476− (void)getLong(s);
477− /* The uncompressed length returned by above getlong() may be
478− * different from s->out in case of concatenated .gz files.
479− * Check for such files:
480− */
481− check_header(s);
482− if (s->z_err == Z_OK) {
483− inflateReset(&(s->stream));
484− s->crc = crc32(0L, Z_NULL, 0);
485− }
486− }
487− }
488− if (s->z_err != Z_OK || s->z_eof) break;
489− }
490− s->crc = crc32(s->crc, start, (uInt)(s->stream.next_out - start));
491−
492− if (len == s->stream.avail_out &&
493− (s->z_err == Z_DATA_ERROR || s->z_err == Z_ERRNO))
494− return -1;
495− return (int)(len - s->stream.avail_out);
496−}
497−
498−
499−/* ===========================================================================
500− Reads one byte from the compressed file. gzgetc returns this byte
501− or -1 in case of end of file or error.
502−*/
503−int ZEXPORT gzgetc(file)
504− gzFile file;
505−{
506− unsigned char c;
507−
508− return gzread(file, &c, 1) == 1 ? c : -1;
509−}
510−
511−
512−/* ===========================================================================
513− Push one byte back onto the stream.
514−*/
515−int ZEXPORT gzungetc(c, file)
516− int c;
517− gzFile file;
518−{
519− gz_stream *s = (gz_stream*)file;
520−
521− if (s == NULL || s->mode != 'r' || c == EOF || s->back != EOF) return EOF;
522− s->back = c;
523− s->out--;
524− s->last = (s->z_err == Z_STREAM_END);
525− if (s->last) s->z_err = Z_OK;
526− s->z_eof = 0;
527− return c;
528−}
529−
530−
531−/* ===========================================================================
532− Reads bytes from the compressed file until len-1 characters are
533− read, or a newline character is read and transferred to buf, or an
534− end-of-file condition is encountered. The string is then terminated
535− with a null character.
536− gzgets returns buf, or Z_NULL in case of error.
537−
538− The current implementation is not optimized at all.
539−*/
540−char * ZEXPORT gzgets(file, buf, len)
541− gzFile file;
542− char *buf;
543− int len;
544−{
545− char *b = buf;
546− if (buf == Z_NULL || len <= 0) return Z_NULL;
547−
548− while (--len > 0 && gzread(file, buf, 1) == 1 && *buf++ != '\n') ;
549− *buf = '\0';
550− return b == buf && len > 0 ? Z_NULL : b;
551−}
552−
553−
554−#ifndef NO_GZCOMPRESS
555−/* ===========================================================================
556− Writes the given number of uncompressed bytes into the compressed file.
557− gzwrite returns the number of bytes actually written (0 in case of error).
558−*/
559−int ZEXPORT gzwrite (file, buf, len)
560− gzFile file;
561− voidpc buf;
562− unsigned len;
563−{
564− gz_stream *s = (gz_stream*)file;
565−
566− if (s == NULL || s->mode != 'w') return Z_STREAM_ERROR;
567−
568− s->stream.next_in = (Bytef*)buf;
569− s->stream.avail_in = len;
570−
571− while (s->stream.avail_in != 0) {
572−
573− if (s->stream.avail_out == 0) {
574−
575− s->stream.next_out = s->outbuf;
576− if (fwrite(s->outbuf, 1, Z_BUFSIZE, s->file) != Z_BUFSIZE) {
577− s->z_err = Z_ERRNO;
578− break;
579− }
580− s->stream.avail_out = Z_BUFSIZE;
581− }
582− s->in += s->stream.avail_in;
583− s->out += s->stream.avail_out;
584− s->z_err = deflate(&(s->stream), Z_NO_FLUSH);
585− s->in -= s->stream.avail_in;
586− s->out -= s->stream.avail_out;
587− if (s->z_err != Z_OK) break;
588− }
589− s->crc = crc32(s->crc, (const Bytef *)buf, len);
590−
591− return (int)(len - s->stream.avail_in);
592−}
593−
594−
595−/* ===========================================================================
596− Converts, formats, and writes the args to the compressed file under
597− control of the format string, as in fprintf. gzprintf returns the number of
598− uncompressed bytes actually written (0 in case of error).
599−*/
600−#ifdef STDC
601−#include <stdarg.h>
602−
603−int ZEXPORTVA gzprintf (gzFile file, const char *format, /* args */ ...)
604−{
605− char buf[Z_PRINTF_BUFSIZE];
606− va_list va;
607− int len;
608−
609− buf[sizeof(buf) - 1] = 0;
610− va_start(va, format);
611−#ifdef NO_vsnprintf
612−# ifdef HAS_vsprintf_void
613− (void)vsprintf(buf, format, va);
614− va_end(va);
615− for (len = 0; len < sizeof(buf); len++)
616− if (buf[len] == 0) break;
617−# else
618− len = vsprintf(buf, format, va);
619− va_end(va);
620−# endif
621−#else
622−# ifdef HAS_vsnprintf_void
623− (void)vsnprintf(buf, sizeof(buf), format, va);
624− va_end(va);
625− len = strlen(buf);
626−# else
627− len = vsnprintf(buf, sizeof(buf), format, va);
628− va_end(va);
629−# endif
630−#endif
631− if (len <= 0 || len >= (int)sizeof(buf) || buf[sizeof(buf) - 1] != 0)
632− return 0;
633− return gzwrite(file, buf, (unsigned)len);
634−}
635−#else /* not ANSI C */
636−
637−int ZEXPORTVA gzprintf (file, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10,
638− a11, a12, a13, a14, a15, a16, a17, a18, a19, a20)
639− gzFile file;
640− const char *format;
641− int a1, a2, a3, a4, a5, a6, a7, a8, a9, a10,
642− a11, a12, a13, a14, a15, a16, a17, a18, a19, a20;
643−{
644− char buf[Z_PRINTF_BUFSIZE];
645− int len;
646−
647− buf[sizeof(buf) - 1] = 0;
648−#ifdef NO_snprintf
649−# ifdef HAS_sprintf_void
650− sprintf(buf, format, a1, a2, a3, a4, a5, a6, a7, a8,
651− a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
652− for (len = 0; len < sizeof(buf); len++)
653− if (buf[len] == 0) break;
654−# else
655− len = sprintf(buf, format, a1, a2, a3, a4, a5, a6, a7, a8,
656− a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
657−# endif
658−#else
659−# ifdef HAS_snprintf_void
660− snprintf(buf, sizeof(buf), format, a1, a2, a3, a4, a5, a6, a7, a8,
661− a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
662− len = strlen(buf);
663−# else
664− len = snprintf(buf, sizeof(buf), format, a1, a2, a3, a4, a5, a6, a7, a8,
665− a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
666−# endif
667−#endif
668− if (len <= 0 || len >= sizeof(buf) || buf[sizeof(buf) - 1] != 0)
669− return 0;
670− return gzwrite(file, buf, len);
671−}
672−#endif
673−
674−/* ===========================================================================
675− Writes c, converted to an unsigned char, into the compressed file.
676− gzputc returns the value that was written, or -1 in case of error.
677−*/
678−int ZEXPORT gzputc(file, c)
679− gzFile file;
680− int c;
681−{
682− unsigned char cc = (unsigned char) c; /* required for big endian systems */
683−
684− return gzwrite(file, &cc, 1) == 1 ? (int)cc : -1;
685−}
686−
687−
688−/* ===========================================================================
689− Writes the given null-terminated string to the compressed file, excluding
690− the terminating null character.
691− gzputs returns the number of characters written, or -1 in case of error.
692−*/
693−int ZEXPORT gzputs(file, s)
694− gzFile file;
695− const char *s;
696−{
697− return gzwrite(file, (char*)s, (unsigned)strlen(s));
698−}
699−
700−
701−/* ===========================================================================
702− Flushes all pending output into the compressed file. The parameter
703− flush is as in the deflate() function.
704−*/
705−local int do_flush (file, flush)
706− gzFile file;
707− int flush;
708−{
709− uInt len;
710− int done = 0;
711− gz_stream *s = (gz_stream*)file;
712−
713− if (s == NULL || s->mode != 'w') return Z_STREAM_ERROR;
714−
715− s->stream.avail_in = 0; /* should be zero already anyway */
716−
717− for (;;) {
718− len = Z_BUFSIZE - s->stream.avail_out;
719−
720− if (len != 0) {
721− if ((uInt)fwrite(s->outbuf, 1, len, s->file) != len) {
722− s->z_err = Z_ERRNO;
723− return Z_ERRNO;
724− }
725− s->stream.next_out = s->outbuf;
726− s->stream.avail_out = Z_BUFSIZE;
727− }
728− if (done) break;
729− s->out += s->stream.avail_out;
730− s->z_err = deflate(&(s->stream), flush);
731− s->out -= s->stream.avail_out;
732−
733− /* Ignore the second of two consecutive flushes: */
734− if (len == 0 && s->z_err == Z_BUF_ERROR) s->z_err = Z_OK;
735−
736− /* deflate has finished flushing only when it hasn't used up
737− * all the available space in the output buffer:
738− */
739− done = (s->stream.avail_out != 0 || s->z_err == Z_STREAM_END);
740−
741− if (s->z_err != Z_OK && s->z_err != Z_STREAM_END) break;
742− }
743− return s->z_err == Z_STREAM_END ? Z_OK : s->z_err;
744−}
745−
746−int ZEXPORT gzflush (file, flush)
747− gzFile file;
748− int flush;
749−{
750− gz_stream *s = (gz_stream*)file;
751− int err = do_flush (file, flush);
752−
753− if (err) return err;
754− fflush(s->file);
755− return s->z_err == Z_STREAM_END ? Z_OK : s->z_err;
756−}
757−#endif /* NO_GZCOMPRESS */
758−
759−/* ===========================================================================
760− Sets the starting position for the next gzread or gzwrite on the given
761− compressed file. The offset represents a number of bytes in the
762− gzseek returns the resulting offset location as measured in bytes from
763− the beginning of the uncompressed stream, or -1 in case of error.
764− SEEK_END is not implemented, returns error.
765− In this version of the library, gzseek can be extremely slow.
766−*/
767−z_off_t ZEXPORT gzseek (file, offset, whence)
768− gzFile file;
769− z_off_t offset;
770− int whence;
771−{
772− gz_stream *s = (gz_stream*)file;
773−
774− if (s == NULL || whence == SEEK_END ||
775− s->z_err == Z_ERRNO || s->z_err == Z_DATA_ERROR) {
776− return -1L;
777− }
778−
779− if (s->mode == 'w') {
780−#ifdef NO_GZCOMPRESS
781− return -1L;
782−#else
783− if (whence == SEEK_SET) {
784− offset -= s->in;
785− }
786− if (offset < 0) return -1L;
787−
788− /* At this point, offset is the number of zero bytes to write. */
789− if (s->inbuf == Z_NULL) {
790− s->inbuf = (Byte*)ALLOC(Z_BUFSIZE); /* for seeking */
791− if (s->inbuf == Z_NULL) return -1L;
792− zmemzero(s->inbuf, Z_BUFSIZE);
793− }
794− while (offset > 0) {
795− uInt size = Z_BUFSIZE;
796− if (offset < Z_BUFSIZE) size = (uInt)offset;
797−
798− size = gzwrite(file, s->inbuf, size);
799− if (size == 0) return -1L;
800−
801− offset -= size;
802− }
803− return s->in;
804−#endif
805− }
806− /* Rest of function is for reading only */
807−
808− /* compute absolute position */
809− if (whence == SEEK_CUR) {
810− offset += s->out;
811− }
812− if (offset < 0) return -1L;
813−
814− if (s->transparent) {
815− /* map to fseek */
816− s->back = EOF;
817− s->stream.avail_in = 0;
818− s->stream.next_in = s->inbuf;
819− if (fseek(s->file, offset, SEEK_SET) < 0) return -1L;
820−
821− s->in = s->out = offset;
822− return offset;
823− }
824−
825− /* For a negative seek, rewind and use positive seek */
826− if (offset >= s->out) {
827− offset -= s->out;
828− } else if (gzrewind(file) < 0) {
829− return -1L;
830− }
831− /* offset is now the number of bytes to skip. */
832−
833− if (offset != 0 && s->outbuf == Z_NULL) {
834− s->outbuf = (Byte*)ALLOC(Z_BUFSIZE);
835− if (s->outbuf == Z_NULL) return -1L;
836− }
837− if (offset && s->back != EOF) {
838− s->back = EOF;
839− s->out++;
840− offset--;
841− if (s->last) s->z_err = Z_STREAM_END;
842− }
843− while (offset > 0) {
844− int size = Z_BUFSIZE;
845− if (offset < Z_BUFSIZE) size = (int)offset;
846−
847− size = gzread(file, s->outbuf, (uInt)size);
848− if (size <= 0) return -1L;
849− offset -= size;
850− }
851− return s->out;
852−}
853−
854−/* ===========================================================================
855− Rewinds input file.
856−*/
857−int ZEXPORT gzrewind (file)
858− gzFile file;
859−{
860− gz_stream *s = (gz_stream*)file;
861−
862− if (s == NULL || s->mode != 'r') return -1;
863−
864− s->z_err = Z_OK;
865− s->z_eof = 0;
866− s->back = EOF;
867− s->stream.avail_in = 0;
868− s->stream.next_in = s->inbuf;
869− s->crc = crc32(0L, Z_NULL, 0);
870− if (!s->transparent) (void)inflateReset(&s->stream);
871− s->in = 0;
872− s->out = 0;
873− return fseek(s->file, s->start, SEEK_SET);
874−}
875−
876−/* ===========================================================================
877− Returns the starting position for the next gzread or gzwrite on the
878− given compressed file. This position represents a number of bytes in the
879− uncompressed data stream.
880−*/
881−z_off_t ZEXPORT gztell (file)
882− gzFile file;
883−{
884− return gzseek(file, 0L, SEEK_CUR);
885−}
886−
887−/* ===========================================================================
888− Returns 1 when EOF has previously been detected reading the given
889− input stream, otherwise zero.
890−*/
891−int ZEXPORT gzeof (file)
892− gzFile file;
893−{
894− gz_stream *s = (gz_stream*)file;
895−
896− /* With concatenated compressed files that can have embedded
897− * crc trailers, z_eof is no longer the only/best indicator of EOF
898− * on a gz_stream. Handle end-of-stream error explicitly here.
899− */
900− if (s == NULL || s->mode != 'r') return 0;
901− if (s->z_eof) return 1;
902− return s->z_err == Z_STREAM_END;
903−}
904−
905−/* ===========================================================================
906− Returns 1 if reading and doing so transparently, otherwise zero.
907−*/
908−int ZEXPORT gzdirect (file)
909− gzFile file;
910−{
911− gz_stream *s = (gz_stream*)file;
912−
913− if (s == NULL || s->mode != 'r') return 0;
914− return s->transparent;
915−}
916−
917−/* ===========================================================================
918− Outputs a long in LSB order to the given file
919−*/
920−local void putLong (file, x)
921− FILE *file;
922− uLong x;
923−{
924− int n;
925− for (n = 0; n < 4; n++) {
926− fputc((int)(x & 0xff), file);
927− x >>= 8;
928− }
929−}
930−
931−/* ===========================================================================
932− Reads a long in LSB order from the given gz_stream. Sets z_err in case
933− of error.
934−*/
935−local uLong getLong (s)
936− gz_stream *s;
937−{
938− uLong x = (uLong)get_byte(s);
939− int c;
940−
941− x += ((uLong)get_byte(s))<<8;
942− x += ((uLong)get_byte(s))<<16;
943− c = get_byte(s);
944− if (c == EOF) s->z_err = Z_DATA_ERROR;
945− x += ((uLong)c)<<24;
946− return x;
947−}
948−
949−/* ===========================================================================
950− Flushes all pending output if necessary, closes the compressed file
951− and deallocates all the (de)compression state.
952−*/
953−int ZEXPORT gzclose (file)
954− gzFile file;
955−{
956− gz_stream *s = (gz_stream*)file;
957−
958− if (s == NULL) return Z_STREAM_ERROR;
959−
960− if (s->mode == 'w') {
961−#ifdef NO_GZCOMPRESS
962− return Z_STREAM_ERROR;
963−#else
964− if (do_flush (file, Z_FINISH) != Z_OK)
965− return destroy((gz_stream*)file);
966−
967− putLong (s->file, s->crc);
968− putLong (s->file, (uLong)(s->in & 0xffffffff));
969−#endif
970− }
971− return destroy((gz_stream*)file);
972−}
973−
974−#ifdef STDC
975−# define zstrerror(errnum) strerror(errnum)
976−#else
977−# define zstrerror(errnum) ""
978−#endif
979−
980−/* ===========================================================================
981− Returns the error message for the last error which occurred on the
982− given compressed file. errnum is set to zlib error number. If an
983− error occurred in the file system and not in the compression library,
984− errnum is set to Z_ERRNO and the application may consult errno
985− to get the exact error code.
986−*/
987−const char * ZEXPORT gzerror (file, errnum)
988− gzFile file;
989− int *errnum;
990−{
991− char *m;
992− gz_stream *s = (gz_stream*)file;
993−
994− if (s == NULL) {
995− *errnum = Z_STREAM_ERROR;
996− return (const char*)ERR_MSG(Z_STREAM_ERROR);
997− }
998− *errnum = s->z_err;
999− if (*errnum == Z_OK) return (const char*)"";
1000−
1001− m = (char*)(*errnum == Z_ERRNO ? zstrerror(errno) : s->stream.msg);
1002−
1003− if (m == NULL || *m == '\0') m = (char*)ERR_MSG(s->z_err);
1004−
1005− TRYFREE(s->msg);
1006− s->msg = (char*)ALLOC(strlen(s->path) + strlen(m) + 3);
1007− if (s->msg == Z_NULL) return (const char*)ERR_MSG(Z_MEM_ERROR);
1008− strcpy(s->msg, s->path);
1009− strcat(s->msg, ": ");
1010− strcat(s->msg, m);
1011− return (const char*)s->msg;
1012−}
1013−
1014−/* ===========================================================================
1015− Clear the error and end-of-file flags, and do the same for the real file.
1016−*/
1017−void ZEXPORT gzclearerr (file)
1018− gzFile file;
1019−{
1020− gz_stream *s = (gz_stream*)file;
1021−
1022− if (s == NULL) return;
1023− if (s->z_err != Z_STREAM_END) s->z_err = Z_OK;
1024− s->z_eof = 0;
1025− clearerr(s->file);
1026−}
cbits/infback.cdeleted−623
@@ -1,623 +0,0 @@
1−/* infback.c -- inflate using a call-back interface
2− * Copyright (C) 1995-2005 Mark Adler
3− * For conditions of distribution and use, see copyright notice in zlib.h
4− */
5−
6−/*
7− This code is largely copied from inflate.c. Normally either infback.o or
8− inflate.o would be linked into an application--not both. The interface
9− with inffast.c is retained so that optimized assembler-coded versions of
10− inflate_fast() can be used with either inflate.c or infback.c.
11− */
12−
13−#include "zutil.h"
14−#include "inftrees.h"
15−#include "inflate.h"
16−#include "inffast.h"
17−
18−/* function prototypes */
19−local void fixedtables OF((struct inflate_state FAR *state));
20−
21−/*
22− strm provides memory allocation functions in zalloc and zfree, or
23− Z_NULL to use the library memory allocation functions.
24−
25− windowBits is in the range 8..15, and window is a user-supplied
26− window and output buffer that is 2**windowBits bytes.
27− */
28−int ZEXPORT inflateBackInit_(strm, windowBits, window, version, stream_size)
29−z_streamp strm;
30−int windowBits;
31−unsigned char FAR *window;
32−const char *version;
33−int stream_size;
34−{
35− struct inflate_state FAR *state;
36−
37− if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
38− stream_size != (int)(sizeof(z_stream)))
39− return Z_VERSION_ERROR;
40− if (strm == Z_NULL || window == Z_NULL ||
41− windowBits < 8 || windowBits > 15)
42− return Z_STREAM_ERROR;
43− strm->msg = Z_NULL; /* in case we return an error */
44− if (strm->zalloc == (alloc_func)0) {
45− strm->zalloc = zcalloc;
46− strm->opaque = (voidpf)0;
47− }
48− if (strm->zfree == (free_func)0) strm->zfree = zcfree;
49− state = (struct inflate_state FAR *)ZALLOC(strm, 1,
50− sizeof(struct inflate_state));
51− if (state == Z_NULL) return Z_MEM_ERROR;
52− Tracev((stderr, "inflate: allocated\n"));
53− strm->state = (struct internal_state FAR *)state;
54− state->dmax = 32768U;
55− state->wbits = windowBits;
56− state->wsize = 1U << windowBits;
57− state->window = window;
58− state->write = 0;
59− state->whave = 0;
60− return Z_OK;
61−}
62−
63−/*
64− Return state with length and distance decoding tables and index sizes set to
65− fixed code decoding. Normally this returns fixed tables from inffixed.h.
66− If BUILDFIXED is defined, then instead this routine builds the tables the
67− first time it's called, and returns those tables the first time and
68− thereafter. This reduces the size of the code by about 2K bytes, in
69− exchange for a little execution time. However, BUILDFIXED should not be
70− used for threaded applications, since the rewriting of the tables and virgin
71− may not be thread-safe.
72− */
73−local void fixedtables(state)
74−struct inflate_state FAR *state;
75−{
76−#ifdef BUILDFIXED
77− static int virgin = 1;
78− static code *lenfix, *distfix;
79− static code fixed[544];
80−
81− /* build fixed huffman tables if first call (may not be thread safe) */
82− if (virgin) {
83− unsigned sym, bits;
84− static code *next;
85−
86− /* literal/length table */
87− sym = 0;
88− while (sym < 144) state->lens[sym++] = 8;
89− while (sym < 256) state->lens[sym++] = 9;
90− while (sym < 280) state->lens[sym++] = 7;
91− while (sym < 288) state->lens[sym++] = 8;
92− next = fixed;
93− lenfix = next;
94− bits = 9;
95− inflate_table(LENS, state->lens, 288, &(next), &(bits), state->work);
96−
97− /* distance table */
98− sym = 0;
99− while (sym < 32) state->lens[sym++] = 5;
100− distfix = next;
101− bits = 5;
102− inflate_table(DISTS, state->lens, 32, &(next), &(bits), state->work);
103−
104− /* do this just once */
105− virgin = 0;
106− }
107−#else /* !BUILDFIXED */
108−# include "inffixed.h"
109−#endif /* BUILDFIXED */
110− state->lencode = lenfix;
111− state->lenbits = 9;
112− state->distcode = distfix;
113− state->distbits = 5;
114−}
115−
116−/* Macros for inflateBack(): */
117−
118−/* Load returned state from inflate_fast() */
119−#define LOAD() \
120− do { \
121− put = strm->next_out; \
122− left = strm->avail_out; \
123− next = strm->next_in; \
124− have = strm->avail_in; \
125− hold = state->hold; \
126− bits = state->bits; \
127− } while (0)
128−
129−/* Set state from registers for inflate_fast() */
130−#define RESTORE() \
131− do { \
132− strm->next_out = put; \
133− strm->avail_out = left; \
134− strm->next_in = next; \
135− strm->avail_in = have; \
136− state->hold = hold; \
137− state->bits = bits; \
138− } while (0)
139−
140−/* Clear the input bit accumulator */
141−#define INITBITS() \
142− do { \
143− hold = 0; \
144− bits = 0; \
145− } while (0)
146−
147−/* Assure that some input is available. If input is requested, but denied,
148− then return a Z_BUF_ERROR from inflateBack(). */
149−#define PULL() \
150− do { \
151− if (have == 0) { \
152− have = in(in_desc, &next); \
153− if (have == 0) { \
154− next = Z_NULL; \
155− ret = Z_BUF_ERROR; \
156− goto inf_leave; \
157− } \
158− } \
159− } while (0)
160−
161−/* Get a byte of input into the bit accumulator, or return from inflateBack()
162− with an error if there is no input available. */
163−#define PULLBYTE() \
164− do { \
165− PULL(); \
166− have--; \
167− hold += (unsigned long)(*next++) << bits; \
168− bits += 8; \
169− } while (0)
170−
171−/* Assure that there are at least n bits in the bit accumulator. If there is
172− not enough available input to do that, then return from inflateBack() with
173− an error. */
174−#define NEEDBITS(n) \
175− do { \
176− while (bits < (unsigned)(n)) \
177− PULLBYTE(); \
178− } while (0)
179−
180−/* Return the low n bits of the bit accumulator (n < 16) */
181−#define BITS(n) \
182− ((unsigned)hold & ((1U << (n)) - 1))
183−
184−/* Remove n bits from the bit accumulator */
185−#define DROPBITS(n) \
186− do { \
187− hold >>= (n); \
188− bits -= (unsigned)(n); \
189− } while (0)
190−
191−/* Remove zero to seven bits as needed to go to a byte boundary */
192−#define BYTEBITS() \
193− do { \
194− hold >>= bits & 7; \
195− bits -= bits & 7; \
196− } while (0)
197−
198−/* Assure that some output space is available, by writing out the window
199− if it's full. If the write fails, return from inflateBack() with a
200− Z_BUF_ERROR. */
201−#define ROOM() \
202− do { \
203− if (left == 0) { \
204− put = state->window; \
205− left = state->wsize; \
206− state->whave = left; \
207− if (out(out_desc, put, left)) { \
208− ret = Z_BUF_ERROR; \
209− goto inf_leave; \
210− } \
211− } \
212− } while (0)
213−
214−/*
215− strm provides the memory allocation functions and window buffer on input,
216− and provides information on the unused input on return. For Z_DATA_ERROR
217− returns, strm will also provide an error message.
218−
219− in() and out() are the call-back input and output functions. When
220− inflateBack() needs more input, it calls in(). When inflateBack() has
221− filled the window with output, or when it completes with data in the
222− window, it calls out() to write out the data. The application must not
223− change the provided input until in() is called again or inflateBack()
224− returns. The application must not change the window/output buffer until
225− inflateBack() returns.
226−
227− in() and out() are called with a descriptor parameter provided in the
228− inflateBack() call. This parameter can be a structure that provides the
229− information required to do the read or write, as well as accumulated
230− information on the input and output such as totals and check values.
231−
232− in() should return zero on failure. out() should return non-zero on
233− failure. If either in() or out() fails, than inflateBack() returns a
234− Z_BUF_ERROR. strm->next_in can be checked for Z_NULL to see whether it
235− was in() or out() that caused in the error. Otherwise, inflateBack()
236− returns Z_STREAM_END on success, Z_DATA_ERROR for an deflate format
237− error, or Z_MEM_ERROR if it could not allocate memory for the state.
238− inflateBack() can also return Z_STREAM_ERROR if the input parameters
239− are not correct, i.e. strm is Z_NULL or the state was not initialized.
240− */
241−int ZEXPORT inflateBack(strm, in, in_desc, out, out_desc)
242−z_streamp strm;
243−in_func in;
244−void FAR *in_desc;
245−out_func out;
246−void FAR *out_desc;
247−{
248− struct inflate_state FAR *state;
249− unsigned char FAR *next; /* next input */
250− unsigned char FAR *put; /* next output */
251− unsigned have, left; /* available input and output */
252− unsigned long hold; /* bit buffer */
253− unsigned bits; /* bits in bit buffer */
254− unsigned copy; /* number of stored or match bytes to copy */
255− unsigned char FAR *from; /* where to copy match bytes from */
256− code this; /* current decoding table entry */
257− code last; /* parent table entry */
258− unsigned len; /* length to copy for repeats, bits to drop */
259− int ret; /* return code */
260− static const unsigned short order[19] = /* permutation of code lengths */
261− {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
262−
263− /* Check that the strm exists and that the state was initialized */
264− if (strm == Z_NULL || strm->state == Z_NULL)
265− return Z_STREAM_ERROR;
266− state = (struct inflate_state FAR *)strm->state;
267−
268− /* Reset the state */
269− strm->msg = Z_NULL;
270− state->mode = TYPE;
271− state->last = 0;
272− state->whave = 0;
273− next = strm->next_in;
274− have = next != Z_NULL ? strm->avail_in : 0;
275− hold = 0;
276− bits = 0;
277− put = state->window;
278− left = state->wsize;
279−
280− /* Inflate until end of block marked as last */
281− for (;;)
282− switch (state->mode) {
283− case TYPE:
284− /* determine and dispatch block type */
285− if (state->last) {
286− BYTEBITS();
287− state->mode = DONE;
288− break;
289− }
290− NEEDBITS(3);
291− state->last = BITS(1);
292− DROPBITS(1);
293− switch (BITS(2)) {
294− case 0: /* stored block */
295− Tracev((stderr, "inflate: stored block%s\n",
296− state->last ? " (last)" : ""));
297− state->mode = STORED;
298− break;
299− case 1: /* fixed block */
300− fixedtables(state);
301− Tracev((stderr, "inflate: fixed codes block%s\n",
302− state->last ? " (last)" : ""));
303− state->mode = LEN; /* decode codes */
304− break;
305− case 2: /* dynamic block */
306− Tracev((stderr, "inflate: dynamic codes block%s\n",
307− state->last ? " (last)" : ""));
308− state->mode = TABLE;
309− break;
310− case 3:
311− strm->msg = (char *)"invalid block type";
312− state->mode = BAD;
313− }
314− DROPBITS(2);
315− break;
316−
317− case STORED:
318− /* get and verify stored block length */
319− BYTEBITS(); /* go to byte boundary */
320− NEEDBITS(32);
321− if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) {
322− strm->msg = (char *)"invalid stored block lengths";
323− state->mode = BAD;
324− break;
325− }
326− state->length = (unsigned)hold & 0xffff;
327− Tracev((stderr, "inflate: stored length %u\n",
328− state->length));
329− INITBITS();
330−
331− /* copy stored block from input to output */
332− while (state->length != 0) {
333− copy = state->length;
334− PULL();
335− ROOM();
336− if (copy > have) copy = have;
337− if (copy > left) copy = left;
338− zmemcpy(put, next, copy);
339− have -= copy;
340− next += copy;
341− left -= copy;
342− put += copy;
343− state->length -= copy;
344− }
345− Tracev((stderr, "inflate: stored end\n"));
346− state->mode = TYPE;
347− break;
348−
349− case TABLE:
350− /* get dynamic table entries descriptor */
351− NEEDBITS(14);
352− state->nlen = BITS(5) + 257;
353− DROPBITS(5);
354− state->ndist = BITS(5) + 1;
355− DROPBITS(5);
356− state->ncode = BITS(4) + 4;
357− DROPBITS(4);
358−#ifndef PKZIP_BUG_WORKAROUND
359− if (state->nlen > 286 || state->ndist > 30) {
360− strm->msg = (char *)"too many length or distance symbols";
361− state->mode = BAD;
362− break;
363− }
364−#endif
365− Tracev((stderr, "inflate: table sizes ok\n"));
366−
367− /* get code length code lengths (not a typo) */
368− state->have = 0;
369− while (state->have < state->ncode) {
370− NEEDBITS(3);
371− state->lens[order[state->have++]] = (unsigned short)BITS(3);
372− DROPBITS(3);
373− }
374− while (state->have < 19)
375− state->lens[order[state->have++]] = 0;
376− state->next = state->codes;
377− state->lencode = (code const FAR *)(state->next);
378− state->lenbits = 7;
379− ret = inflate_table(CODES, state->lens, 19, &(state->next),
380− &(state->lenbits), state->work);
381− if (ret) {
382− strm->msg = (char *)"invalid code lengths set";
383− state->mode = BAD;
384− break;
385− }
386− Tracev((stderr, "inflate: code lengths ok\n"));
387−
388− /* get length and distance code code lengths */
389− state->have = 0;
390− while (state->have < state->nlen + state->ndist) {
391− for (;;) {
392− this = state->lencode[BITS(state->lenbits)];
393− if ((unsigned)(this.bits) <= bits) break;
394− PULLBYTE();
395− }
396− if (this.val < 16) {
397− NEEDBITS(this.bits);
398− DROPBITS(this.bits);
399− state->lens[state->have++] = this.val;
400− }
401− else {
402− if (this.val == 16) {
403− NEEDBITS(this.bits + 2);
404− DROPBITS(this.bits);
405− if (state->have == 0) {
406− strm->msg = (char *)"invalid bit length repeat";
407− state->mode = BAD;
408− break;
409− }
410− len = (unsigned)(state->lens[state->have - 1]);
411− copy = 3 + BITS(2);
412− DROPBITS(2);
413− }
414− else if (this.val == 17) {
415− NEEDBITS(this.bits + 3);
416− DROPBITS(this.bits);
417− len = 0;
418− copy = 3 + BITS(3);
419− DROPBITS(3);
420− }
421− else {
422− NEEDBITS(this.bits + 7);
423− DROPBITS(this.bits);
424− len = 0;
425− copy = 11 + BITS(7);
426− DROPBITS(7);
427− }
428− if (state->have + copy > state->nlen + state->ndist) {
429− strm->msg = (char *)"invalid bit length repeat";
430− state->mode = BAD;
431− break;
432− }
433− while (copy--)
434− state->lens[state->have++] = (unsigned short)len;
435− }
436− }
437−
438− /* handle error breaks in while */
439− if (state->mode == BAD) break;
440−
441− /* build code tables */
442− state->next = state->codes;
443− state->lencode = (code const FAR *)(state->next);
444− state->lenbits = 9;
445− ret = inflate_table(LENS, state->lens, state->nlen, &(state->next),
446− &(state->lenbits), state->work);
447− if (ret) {
448− strm->msg = (char *)"invalid literal/lengths set";
449− state->mode = BAD;
450− break;
451− }
452− state->distcode = (code const FAR *)(state->next);
453− state->distbits = 6;
454− ret = inflate_table(DISTS, state->lens + state->nlen, state->ndist,
455− &(state->next), &(state->distbits), state->work);
456− if (ret) {
457− strm->msg = (char *)"invalid distances set";
458− state->mode = BAD;
459− break;
460− }
461− Tracev((stderr, "inflate: codes ok\n"));
462− state->mode = LEN;
463−
464− case LEN:
465− /* use inflate_fast() if we have enough input and output */
466− if (have >= 6 && left >= 258) {
467− RESTORE();
468− if (state->whave < state->wsize)
469− state->whave = state->wsize - left;
470− inflate_fast(strm, state->wsize);
471− LOAD();
472− break;
473− }
474−
475− /* get a literal, length, or end-of-block code */
476− for (;;) {
477− this = state->lencode[BITS(state->lenbits)];
478− if ((unsigned)(this.bits) <= bits) break;
479− PULLBYTE();
480− }
481− if (this.op && (this.op & 0xf0) == 0) {
482− last = this;
483− for (;;) {
484− this = state->lencode[last.val +
485− (BITS(last.bits + last.op) >> last.bits)];
486− if ((unsigned)(last.bits + this.bits) <= bits) break;
487− PULLBYTE();
488− }
489− DROPBITS(last.bits);
490− }
491− DROPBITS(this.bits);
492− state->length = (unsigned)this.val;
493−
494− /* process literal */
495− if (this.op == 0) {
496− Tracevv((stderr, this.val >= 0x20 && this.val < 0x7f ?
497− "inflate: literal '%c'\n" :
498− "inflate: literal 0x%02x\n", this.val));
499− ROOM();
500− *put++ = (unsigned char)(state->length);
501− left--;
502− state->mode = LEN;
503− break;
504− }
505−
506− /* process end of block */
507− if (this.op & 32) {
508− Tracevv((stderr, "inflate: end of block\n"));
509− state->mode = TYPE;
510− break;
511− }
512−
513− /* invalid code */
514− if (this.op & 64) {
515− strm->msg = (char *)"invalid literal/length code";
516− state->mode = BAD;
517− break;
518− }
519−
520− /* length code -- get extra bits, if any */
521− state->extra = (unsigned)(this.op) & 15;
522− if (state->extra != 0) {
523− NEEDBITS(state->extra);
524− state->length += BITS(state->extra);
525− DROPBITS(state->extra);
526− }
527− Tracevv((stderr, "inflate: length %u\n", state->length));
528−
529− /* get distance code */
530− for (;;) {
531− this = state->distcode[BITS(state->distbits)];
532− if ((unsigned)(this.bits) <= bits) break;
533− PULLBYTE();
534− }
535− if ((this.op & 0xf0) == 0) {
536− last = this;
537− for (;;) {
538− this = state->distcode[last.val +
539− (BITS(last.bits + last.op) >> last.bits)];
540− if ((unsigned)(last.bits + this.bits) <= bits) break;
541− PULLBYTE();
542− }
543− DROPBITS(last.bits);
544− }
545− DROPBITS(this.bits);
546− if (this.op & 64) {
547− strm->msg = (char *)"invalid distance code";
548− state->mode = BAD;
549− break;
550− }
551− state->offset = (unsigned)this.val;
552−
553− /* get distance extra bits, if any */
554− state->extra = (unsigned)(this.op) & 15;
555− if (state->extra != 0) {
556− NEEDBITS(state->extra);
557− state->offset += BITS(state->extra);
558− DROPBITS(state->extra);
559− }
560− if (state->offset > state->wsize - (state->whave < state->wsize ?
561− left : 0)) {
562− strm->msg = (char *)"invalid distance too far back";
563− state->mode = BAD;
564− break;
565− }
566− Tracevv((stderr, "inflate: distance %u\n", state->offset));
567−
568− /* copy match from window to output */
569− do {
570− ROOM();
571− copy = state->wsize - state->offset;
572− if (copy < left) {
573− from = put + copy;
574− copy = left - copy;
575− }
576− else {
577− from = put - state->offset;
578− copy = left;
579− }
580− if (copy > state->length) copy = state->length;
581− state->length -= copy;
582− left -= copy;
583− do {
584− *put++ = *from++;
585− } while (--copy);
586− } while (state->length != 0);
587− break;
588−
589− case DONE:
590− /* inflate stream terminated properly -- write leftover output */
591− ret = Z_STREAM_END;
592− if (left < state->wsize) {
593− if (out(out_desc, state->window, state->wsize - left))
594− ret = Z_BUF_ERROR;
595− }
596− goto inf_leave;
597−
598− case BAD:
599− ret = Z_DATA_ERROR;
600− goto inf_leave;
601−
602− default: /* can't happen, but makes compilers happy */
603− ret = Z_STREAM_ERROR;
604− goto inf_leave;
605− }
606−
607− /* Return unused input */
608− inf_leave:
609− strm->next_in = next;
610− strm->avail_in = have;
611− return ret;
612−}
613−
614−int ZEXPORT inflateBackEnd(strm)
615−z_streamp strm;
616−{
617− if (strm == Z_NULL || strm->state == Z_NULL || strm->zfree == (free_func)0)
618− return Z_STREAM_ERROR;
619− ZFREE(strm, strm->state);
620− strm->state = Z_NULL;
621− Tracev((stderr, "inflate: end\n"));
622− return Z_OK;
623−}
cbits/inffast.cdeleted−318
@@ -1,318 +0,0 @@
1−/* inffast.c -- fast decoding
2− * Copyright (C) 1995-2004 Mark Adler
3− * For conditions of distribution and use, see copyright notice in zlib.h
4− */
5−
6−#include "zutil.h"
7−#include "inftrees.h"
8−#include "inflate.h"
9−#include "inffast.h"
10−
11−#ifndef ASMINF
12−
13−/* Allow machine dependent optimization for post-increment or pre-increment.
14− Based on testing to date,
15− Pre-increment preferred for:
16− - PowerPC G3 (Adler)
17− - MIPS R5000 (Randers-Pehrson)
18− Post-increment preferred for:
19− - none
20− No measurable difference:
21− - Pentium III (Anderson)
22− - M68060 (Nikl)
23− */
24−#ifdef POSTINC
25−# define OFF 0
26−# define PUP(a) *(a)++
27−#else
28−# define OFF 1
29−# define PUP(a) *++(a)
30−#endif
31−
32−/*
33− Decode literal, length, and distance codes and write out the resulting
34− literal and match bytes until either not enough input or output is
35− available, an end-of-block is encountered, or a data error is encountered.
36− When large enough input and output buffers are supplied to inflate(), for
37− example, a 16K input buffer and a 64K output buffer, more than 95% of the
38− inflate execution time is spent in this routine.
39−
40− Entry assumptions:
41−
42− state->mode == LEN
43− strm->avail_in >= 6
44− strm->avail_out >= 258
45− start >= strm->avail_out
46− state->bits < 8
47−
48− On return, state->mode is one of:
49−
50− LEN -- ran out of enough output space or enough available input
51− TYPE -- reached end of block code, inflate() to interpret next block
52− BAD -- error in block data
53−
54− Notes:
55−
56− - The maximum input bits used by a length/distance pair is 15 bits for the
57− length code, 5 bits for the length extra, 15 bits for the distance code,
58− and 13 bits for the distance extra. This totals 48 bits, or six bytes.
59− Therefore if strm->avail_in >= 6, then there is enough input to avoid
60− checking for available input while decoding.
61−
62− - The maximum bytes that a single length/distance pair can output is 258
63− bytes, which is the maximum length that can be coded. inflate_fast()
64− requires strm->avail_out >= 258 for each loop to avoid checking for
65− output space.
66− */
67−void inflate_fast(strm, start)
68−z_streamp strm;
69−unsigned start; /* inflate()'s starting value for strm->avail_out */
70−{
71− struct inflate_state FAR *state;
72− unsigned char FAR *in; /* local strm->next_in */
73− unsigned char FAR *last; /* while in < last, enough input available */
74− unsigned char FAR *out; /* local strm->next_out */
75− unsigned char FAR *beg; /* inflate()'s initial strm->next_out */
76− unsigned char FAR *end; /* while out < end, enough space available */
77−#ifdef INFLATE_STRICT
78− unsigned dmax; /* maximum distance from zlib header */
79−#endif
80− unsigned wsize; /* window size or zero if not using window */
81− unsigned whave; /* valid bytes in the window */
82− unsigned write; /* window write index */
83− unsigned char FAR *window; /* allocated sliding window, if wsize != 0 */
84− unsigned long hold; /* local strm->hold */
85− unsigned bits; /* local strm->bits */
86− code const FAR *lcode; /* local strm->lencode */
87− code const FAR *dcode; /* local strm->distcode */
88− unsigned lmask; /* mask for first level of length codes */
89− unsigned dmask; /* mask for first level of distance codes */
90− code this; /* retrieved table entry */
91− unsigned op; /* code bits, operation, extra bits, or */
92− /* window position, window bytes to copy */
93− unsigned len; /* match length, unused bytes */
94− unsigned dist; /* match distance */
95− unsigned char FAR *from; /* where to copy match from */
96−
97− /* copy state to local variables */
98− state = (struct inflate_state FAR *)strm->state;
99− in = strm->next_in - OFF;
100− last = in + (strm->avail_in - 5);
101− out = strm->next_out - OFF;
102− beg = out - (start - strm->avail_out);
103− end = out + (strm->avail_out - 257);
104−#ifdef INFLATE_STRICT
105− dmax = state->dmax;
106−#endif
107− wsize = state->wsize;
108− whave = state->whave;
109− write = state->write;
110− window = state->window;
111− hold = state->hold;
112− bits = state->bits;
113− lcode = state->lencode;
114− dcode = state->distcode;
115− lmask = (1U << state->lenbits) - 1;
116− dmask = (1U << state->distbits) - 1;
117−
118− /* decode literals and length/distances until end-of-block or not enough
119− input data or output space */
120− do {
121− if (bits < 15) {
122− hold += (unsigned long)(PUP(in)) << bits;
123− bits += 8;
124− hold += (unsigned long)(PUP(in)) << bits;
125− bits += 8;
126− }
127− this = lcode[hold & lmask];
128− dolen:
129− op = (unsigned)(this.bits);
130− hold >>= op;
131− bits -= op;
132− op = (unsigned)(this.op);
133− if (op == 0) { /* literal */
134− Tracevv((stderr, this.val >= 0x20 && this.val < 0x7f ?
135− "inflate: literal '%c'\n" :
136− "inflate: literal 0x%02x\n", this.val));
137− PUP(out) = (unsigned char)(this.val);
138− }
139− else if (op & 16) { /* length base */
140− len = (unsigned)(this.val);
141− op &= 15; /* number of extra bits */
142− if (op) {
143− if (bits < op) {
144− hold += (unsigned long)(PUP(in)) << bits;
145− bits += 8;
146− }
147− len += (unsigned)hold & ((1U << op) - 1);
148− hold >>= op;
149− bits -= op;
150− }
151− Tracevv((stderr, "inflate: length %u\n", len));
152− if (bits < 15) {
153− hold += (unsigned long)(PUP(in)) << bits;
154− bits += 8;
155− hold += (unsigned long)(PUP(in)) << bits;
156− bits += 8;
157− }
158− this = dcode[hold & dmask];
159− dodist:
160− op = (unsigned)(this.bits);
161− hold >>= op;
162− bits -= op;
163− op = (unsigned)(this.op);
164− if (op & 16) { /* distance base */
165− dist = (unsigned)(this.val);
166− op &= 15; /* number of extra bits */
167− if (bits < op) {
168− hold += (unsigned long)(PUP(in)) << bits;
169− bits += 8;
170− if (bits < op) {
171− hold += (unsigned long)(PUP(in)) << bits;
172− bits += 8;
173− }
174− }
175− dist += (unsigned)hold & ((1U << op) - 1);
176−#ifdef INFLATE_STRICT
177− if (dist > dmax) {
178− strm->msg = (char *)"invalid distance too far back";
179− state->mode = BAD;
180− break;
181− }
182−#endif
183− hold >>= op;
184− bits -= op;
185− Tracevv((stderr, "inflate: distance %u\n", dist));
186− op = (unsigned)(out - beg); /* max distance in output */
187− if (dist > op) { /* see if copy from window */
188− op = dist - op; /* distance back in window */
189− if (op > whave) {
190− strm->msg = (char *)"invalid distance too far back";
191− state->mode = BAD;
192− break;
193− }
194− from = window - OFF;
195− if (write == 0) { /* very common case */
196− from += wsize - op;
197− if (op < len) { /* some from window */
198− len -= op;
199− do {
200− PUP(out) = PUP(from);
201− } while (--op);
202− from = out - dist; /* rest from output */
203− }
204− }
205− else if (write < op) { /* wrap around window */
206− from += wsize + write - op;
207− op -= write;
208− if (op < len) { /* some from end of window */
209− len -= op;
210− do {
211− PUP(out) = PUP(from);
212− } while (--op);
213− from = window - OFF;
214− if (write < len) { /* some from start of window */
215− op = write;
216− len -= op;
217− do {
218− PUP(out) = PUP(from);
219− } while (--op);
220− from = out - dist; /* rest from output */
221− }
222− }
223− }
224− else { /* contiguous in window */
225− from += write - op;
226− if (op < len) { /* some from window */
227− len -= op;
228− do {
229− PUP(out) = PUP(from);
230− } while (--op);
231− from = out - dist; /* rest from output */
232− }
233− }
234− while (len > 2) {
235− PUP(out) = PUP(from);
236− PUP(out) = PUP(from);
237− PUP(out) = PUP(from);
238− len -= 3;
239− }
240− if (len) {
241− PUP(out) = PUP(from);
242− if (len > 1)
243− PUP(out) = PUP(from);
244− }
245− }
246− else {
247− from = out - dist; /* copy direct from output */
248− do { /* minimum length is three */
249− PUP(out) = PUP(from);
250− PUP(out) = PUP(from);
251− PUP(out) = PUP(from);
252− len -= 3;
253− } while (len > 2);
254− if (len) {
255− PUP(out) = PUP(from);
256− if (len > 1)
257− PUP(out) = PUP(from);
258− }
259− }
260− }
261− else if ((op & 64) == 0) { /* 2nd level distance code */
262− this = dcode[this.val + (hold & ((1U << op) - 1))];
263− goto dodist;
264− }
265− else {
266− strm->msg = (char *)"invalid distance code";
267− state->mode = BAD;
268− break;
269− }
270− }
271− else if ((op & 64) == 0) { /* 2nd level length code */
272− this = lcode[this.val + (hold & ((1U << op) - 1))];
273− goto dolen;
274− }
275− else if (op & 32) { /* end-of-block */
276− Tracevv((stderr, "inflate: end of block\n"));
277− state->mode = TYPE;
278− break;
279− }
280− else {
281− strm->msg = (char *)"invalid literal/length code";
282− state->mode = BAD;
283− break;
284− }
285− } while (in < last && out < end);
286−
287− /* return unused bytes (on entry, bits < 8, so in won't go too far back) */
288− len = bits >> 3;
289− in -= len;
290− bits -= len << 3;
291− hold &= (1U << bits) - 1;
292−
293− /* update state and return */
294− strm->next_in = in + OFF;
295− strm->next_out = out + OFF;
296− strm->avail_in = (unsigned)(in < last ? 5 + (last - in) : 5 - (in - last));
297− strm->avail_out = (unsigned)(out < end ?
298− 257 + (end - out) : 257 - (out - end));
299− state->hold = hold;
300− state->bits = bits;
301− return;
302−}
303−
304−/*
305− inflate_fast() speedups that turned out slower (on a PowerPC G3 750CXe):
306− - Using bit fields for code structure
307− - Different op definition to avoid & for extra bits (do & for table bits)
308− - Three separate decoding do-loops for direct, window, and write == 0
309− - Special case for distance > 1 copies to do overlapped load and store copy
310− - Explicit branch predictions (based on measured branch probabilities)
311− - Deferring match copy and interspersed it with decoding subsequent codes
312− - Swapping literal/length else
313− - Swapping window/direct else
314− - Larger unrolled copy loops (three is about right)
315− - Moving len -= 3 statement into middle of loop
316− */
317−
318−#endif /* !ASMINF */
cbits/inffast.hdeleted−11
@@ -1,11 +0,0 @@
1−/* inffast.h -- header to use inffast.c
2− * Copyright (C) 1995-2003 Mark Adler
3− * For conditions of distribution and use, see copyright notice in zlib.h
4− */
5−
6−/* WARNING: this file should *not* be used by applications. It is
7− part of the implementation of the compression library and is
8− subject to change. Applications should only use zlib.h.
9− */
10−
11−void inflate_fast OF((z_streamp strm, unsigned start));
cbits/inffixed.hdeleted−94
@@ -1,94 +0,0 @@
1− /* inffixed.h -- table for decoding fixed codes
2− * Generated automatically by makefixed().
3− */
4−
5− /* WARNING: this file should *not* be used by applications. It
6− is part of the implementation of the compression library and
7− is subject to change. Applications should only use zlib.h.
8− */
9−
10− static const code lenfix[512] = {
11− {96,7,0},{0,8,80},{0,8,16},{20,8,115},{18,7,31},{0,8,112},{0,8,48},
12− {0,9,192},{16,7,10},{0,8,96},{0,8,32},{0,9,160},{0,8,0},{0,8,128},
13− {0,8,64},{0,9,224},{16,7,6},{0,8,88},{0,8,24},{0,9,144},{19,7,59},
14− {0,8,120},{0,8,56},{0,9,208},{17,7,17},{0,8,104},{0,8,40},{0,9,176},
15− {0,8,8},{0,8,136},{0,8,72},{0,9,240},{16,7,4},{0,8,84},{0,8,20},
16− {21,8,227},{19,7,43},{0,8,116},{0,8,52},{0,9,200},{17,7,13},{0,8,100},
17− {0,8,36},{0,9,168},{0,8,4},{0,8,132},{0,8,68},{0,9,232},{16,7,8},
18− {0,8,92},{0,8,28},{0,9,152},{20,7,83},{0,8,124},{0,8,60},{0,9,216},
19− {18,7,23},{0,8,108},{0,8,44},{0,9,184},{0,8,12},{0,8,140},{0,8,76},
20− {0,9,248},{16,7,3},{0,8,82},{0,8,18},{21,8,163},{19,7,35},{0,8,114},
21− {0,8,50},{0,9,196},{17,7,11},{0,8,98},{0,8,34},{0,9,164},{0,8,2},
22− {0,8,130},{0,8,66},{0,9,228},{16,7,7},{0,8,90},{0,8,26},{0,9,148},
23− {20,7,67},{0,8,122},{0,8,58},{0,9,212},{18,7,19},{0,8,106},{0,8,42},
24− {0,9,180},{0,8,10},{0,8,138},{0,8,74},{0,9,244},{16,7,5},{0,8,86},
25− {0,8,22},{64,8,0},{19,7,51},{0,8,118},{0,8,54},{0,9,204},{17,7,15},
26− {0,8,102},{0,8,38},{0,9,172},{0,8,6},{0,8,134},{0,8,70},{0,9,236},
27− {16,7,9},{0,8,94},{0,8,30},{0,9,156},{20,7,99},{0,8,126},{0,8,62},
28− {0,9,220},{18,7,27},{0,8,110},{0,8,46},{0,9,188},{0,8,14},{0,8,142},
29− {0,8,78},{0,9,252},{96,7,0},{0,8,81},{0,8,17},{21,8,131},{18,7,31},
30− {0,8,113},{0,8,49},{0,9,194},{16,7,10},{0,8,97},{0,8,33},{0,9,162},
31− {0,8,1},{0,8,129},{0,8,65},{0,9,226},{16,7,6},{0,8,89},{0,8,25},
32− {0,9,146},{19,7,59},{0,8,121},{0,8,57},{0,9,210},{17,7,17},{0,8,105},
33− {0,8,41},{0,9,178},{0,8,9},{0,8,137},{0,8,73},{0,9,242},{16,7,4},
34− {0,8,85},{0,8,21},{16,8,258},{19,7,43},{0,8,117},{0,8,53},{0,9,202},
35− {17,7,13},{0,8,101},{0,8,37},{0,9,170},{0,8,5},{0,8,133},{0,8,69},
36− {0,9,234},{16,7,8},{0,8,93},{0,8,29},{0,9,154},{20,7,83},{0,8,125},
37− {0,8,61},{0,9,218},{18,7,23},{0,8,109},{0,8,45},{0,9,186},{0,8,13},
38− {0,8,141},{0,8,77},{0,9,250},{16,7,3},{0,8,83},{0,8,19},{21,8,195},
39− {19,7,35},{0,8,115},{0,8,51},{0,9,198},{17,7,11},{0,8,99},{0,8,35},
40− {0,9,166},{0,8,3},{0,8,131},{0,8,67},{0,9,230},{16,7,7},{0,8,91},
41− {0,8,27},{0,9,150},{20,7,67},{0,8,123},{0,8,59},{0,9,214},{18,7,19},
42− {0,8,107},{0,8,43},{0,9,182},{0,8,11},{0,8,139},{0,8,75},{0,9,246},
43− {16,7,5},{0,8,87},{0,8,23},{64,8,0},{19,7,51},{0,8,119},{0,8,55},
44− {0,9,206},{17,7,15},{0,8,103},{0,8,39},{0,9,174},{0,8,7},{0,8,135},
45− {0,8,71},{0,9,238},{16,7,9},{0,8,95},{0,8,31},{0,9,158},{20,7,99},
46− {0,8,127},{0,8,63},{0,9,222},{18,7,27},{0,8,111},{0,8,47},{0,9,190},
47− {0,8,15},{0,8,143},{0,8,79},{0,9,254},{96,7,0},{0,8,80},{0,8,16},
48− {20,8,115},{18,7,31},{0,8,112},{0,8,48},{0,9,193},{16,7,10},{0,8,96},
49− {0,8,32},{0,9,161},{0,8,0},{0,8,128},{0,8,64},{0,9,225},{16,7,6},
50− {0,8,88},{0,8,24},{0,9,145},{19,7,59},{0,8,120},{0,8,56},{0,9,209},
51− {17,7,17},{0,8,104},{0,8,40},{0,9,177},{0,8,8},{0,8,136},{0,8,72},
52− {0,9,241},{16,7,4},{0,8,84},{0,8,20},{21,8,227},{19,7,43},{0,8,116},
53− {0,8,52},{0,9,201},{17,7,13},{0,8,100},{0,8,36},{0,9,169},{0,8,4},
54− {0,8,132},{0,8,68},{0,9,233},{16,7,8},{0,8,92},{0,8,28},{0,9,153},
55− {20,7,83},{0,8,124},{0,8,60},{0,9,217},{18,7,23},{0,8,108},{0,8,44},
56− {0,9,185},{0,8,12},{0,8,140},{0,8,76},{0,9,249},{16,7,3},{0,8,82},
57− {0,8,18},{21,8,163},{19,7,35},{0,8,114},{0,8,50},{0,9,197},{17,7,11},
58− {0,8,98},{0,8,34},{0,9,165},{0,8,2},{0,8,130},{0,8,66},{0,9,229},
59− {16,7,7},{0,8,90},{0,8,26},{0,9,149},{20,7,67},{0,8,122},{0,8,58},
60− {0,9,213},{18,7,19},{0,8,106},{0,8,42},{0,9,181},{0,8,10},{0,8,138},
61− {0,8,74},{0,9,245},{16,7,5},{0,8,86},{0,8,22},{64,8,0},{19,7,51},
62− {0,8,118},{0,8,54},{0,9,205},{17,7,15},{0,8,102},{0,8,38},{0,9,173},
63− {0,8,6},{0,8,134},{0,8,70},{0,9,237},{16,7,9},{0,8,94},{0,8,30},
64− {0,9,157},{20,7,99},{0,8,126},{0,8,62},{0,9,221},{18,7,27},{0,8,110},
65− {0,8,46},{0,9,189},{0,8,14},{0,8,142},{0,8,78},{0,9,253},{96,7,0},
66− {0,8,81},{0,8,17},{21,8,131},{18,7,31},{0,8,113},{0,8,49},{0,9,195},
67− {16,7,10},{0,8,97},{0,8,33},{0,9,163},{0,8,1},{0,8,129},{0,8,65},
68− {0,9,227},{16,7,6},{0,8,89},{0,8,25},{0,9,147},{19,7,59},{0,8,121},
69− {0,8,57},{0,9,211},{17,7,17},{0,8,105},{0,8,41},{0,9,179},{0,8,9},
70− {0,8,137},{0,8,73},{0,9,243},{16,7,4},{0,8,85},{0,8,21},{16,8,258},
71− {19,7,43},{0,8,117},{0,8,53},{0,9,203},{17,7,13},{0,8,101},{0,8,37},
72− {0,9,171},{0,8,5},{0,8,133},{0,8,69},{0,9,235},{16,7,8},{0,8,93},
73− {0,8,29},{0,9,155},{20,7,83},{0,8,125},{0,8,61},{0,9,219},{18,7,23},
74− {0,8,109},{0,8,45},{0,9,187},{0,8,13},{0,8,141},{0,8,77},{0,9,251},
75− {16,7,3},{0,8,83},{0,8,19},{21,8,195},{19,7,35},{0,8,115},{0,8,51},
76− {0,9,199},{17,7,11},{0,8,99},{0,8,35},{0,9,167},{0,8,3},{0,8,131},
77− {0,8,67},{0,9,231},{16,7,7},{0,8,91},{0,8,27},{0,9,151},{20,7,67},
78− {0,8,123},{0,8,59},{0,9,215},{18,7,19},{0,8,107},{0,8,43},{0,9,183},
79− {0,8,11},{0,8,139},{0,8,75},{0,9,247},{16,7,5},{0,8,87},{0,8,23},
80− {64,8,0},{19,7,51},{0,8,119},{0,8,55},{0,9,207},{17,7,15},{0,8,103},
81− {0,8,39},{0,9,175},{0,8,7},{0,8,135},{0,8,71},{0,9,239},{16,7,9},
82− {0,8,95},{0,8,31},{0,9,159},{20,7,99},{0,8,127},{0,8,63},{0,9,223},
83− {18,7,27},{0,8,111},{0,8,47},{0,9,191},{0,8,15},{0,8,143},{0,8,79},
84− {0,9,255}
85− };
86−
87− static const code distfix[32] = {
88− {16,5,1},{23,5,257},{19,5,17},{27,5,4097},{17,5,5},{25,5,1025},
89− {21,5,65},{29,5,16385},{16,5,3},{24,5,513},{20,5,33},{28,5,8193},
90− {18,5,9},{26,5,2049},{22,5,129},{64,5,0},{16,5,2},{23,5,385},
91− {19,5,25},{27,5,6145},{17,5,7},{25,5,1537},{21,5,97},{29,5,24577},
92− {16,5,4},{24,5,769},{20,5,49},{28,5,12289},{18,5,13},{26,5,3073},
93− {22,5,193},{64,5,0}
94− };
cbits/inflate.cdeleted−1368
@@ -1,1368 +0,0 @@
1−/* inflate.c -- zlib decompression
2− * Copyright (C) 1995-2005 Mark Adler
3− * For conditions of distribution and use, see copyright notice in zlib.h
4− */
5−
6−/*
7− * Change history:
8− *
9− * 1.2.beta0 24 Nov 2002
10− * - First version -- complete rewrite of inflate to simplify code, avoid
11− * creation of window when not needed, minimize use of window when it is
12− * needed, make inffast.c even faster, implement gzip decoding, and to
13− * improve code readability and style over the previous zlib inflate code
14− *
15− * 1.2.beta1 25 Nov 2002
16− * - Use pointers for available input and output checking in inffast.c
17− * - Remove input and output counters in inffast.c
18− * - Change inffast.c entry and loop from avail_in >= 7 to >= 6
19− * - Remove unnecessary second byte pull from length extra in inffast.c
20− * - Unroll direct copy to three copies per loop in inffast.c
21− *
22− * 1.2.beta2 4 Dec 2002
23− * - Change external routine names to reduce potential conflicts
24− * - Correct filename to inffixed.h for fixed tables in inflate.c
25− * - Make hbuf[] unsigned char to match parameter type in inflate.c
26− * - Change strm->next_out[-state->offset] to *(strm->next_out - state->offset)
27− * to avoid negation problem on Alphas (64 bit) in inflate.c
28− *
29− * 1.2.beta3 22 Dec 2002
30− * - Add comments on state->bits assertion in inffast.c
31− * - Add comments on op field in inftrees.h
32− * - Fix bug in reuse of allocated window after inflateReset()
33− * - Remove bit fields--back to byte structure for speed
34− * - Remove distance extra == 0 check in inflate_fast()--only helps for lengths
35− * - Change post-increments to pre-increments in inflate_fast(), PPC biased?
36− * - Add compile time option, POSTINC, to use post-increments instead (Intel?)
37− * - Make MATCH copy in inflate() much faster for when inflate_fast() not used
38− * - Use local copies of stream next and avail values, as well as local bit
39− * buffer and bit count in inflate()--for speed when inflate_fast() not used
40− *
41− * 1.2.beta4 1 Jan 2003
42− * - Split ptr - 257 statements in inflate_table() to avoid compiler warnings
43− * - Move a comment on output buffer sizes from inffast.c to inflate.c
44− * - Add comments in inffast.c to introduce the inflate_fast() routine
45− * - Rearrange window copies in inflate_fast() for speed and simplification
46− * - Unroll last copy for window match in inflate_fast()
47− * - Use local copies of window variables in inflate_fast() for speed
48− * - Pull out common write == 0 case for speed in inflate_fast()
49− * - Make op and len in inflate_fast() unsigned for consistency
50− * - Add FAR to lcode and dcode declarations in inflate_fast()
51− * - Simplified bad distance check in inflate_fast()
52− * - Added inflateBackInit(), inflateBack(), and inflateBackEnd() in new
53− * source file infback.c to provide a call-back interface to inflate for
54− * programs like gzip and unzip -- uses window as output buffer to avoid
55− * window copying
56− *
57− * 1.2.beta5 1 Jan 2003
58− * - Improved inflateBack() interface to allow the caller to provide initial
59− * input in strm.
60− * - Fixed stored blocks bug in inflateBack()
61− *
62− * 1.2.beta6 4 Jan 2003
63− * - Added comments in inffast.c on effectiveness of POSTINC
64− * - Typecasting all around to reduce compiler warnings
65− * - Changed loops from while (1) or do {} while (1) to for (;;), again to
66− * make compilers happy
67− * - Changed type of window in inflateBackInit() to unsigned char *
68− *
69− * 1.2.beta7 27 Jan 2003
70− * - Changed many types to unsigned or unsigned short to avoid warnings
71− * - Added inflateCopy() function
72− *
73− * 1.2.0 9 Mar 2003
74− * - Changed inflateBack() interface to provide separate opaque descriptors
75− * for the in() and out() functions
76− * - Changed inflateBack() argument and in_func typedef to swap the length
77− * and buffer address return values for the input function
78− * - Check next_in and next_out for Z_NULL on entry to inflate()
79− *
80− * The history for versions after 1.2.0 are in ChangeLog in zlib distribution.
81− */
82−
83−#include "zutil.h"
84−#include "inftrees.h"
85−#include "inflate.h"
86−#include "inffast.h"
87−
88−#ifdef MAKEFIXED
89−# ifndef BUILDFIXED
90−# define BUILDFIXED
91−# endif
92−#endif
93−
94−/* function prototypes */
95−local void fixedtables OF((struct inflate_state FAR *state));
96−local int updatewindow OF((z_streamp strm, unsigned out));
97−#ifdef BUILDFIXED
98− void makefixed OF((void));
99−#endif
100−local unsigned syncsearch OF((unsigned FAR *have, unsigned char FAR *buf,
101− unsigned len));
102−
103−int ZEXPORT inflateReset(strm)
104−z_streamp strm;
105−{
106− struct inflate_state FAR *state;
107−
108− if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
109− state = (struct inflate_state FAR *)strm->state;
110− strm->total_in = strm->total_out = state->total = 0;
111− strm->msg = Z_NULL;
112− strm->adler = 1; /* to support ill-conceived Java test suite */
113− state->mode = HEAD;
114− state->last = 0;
115− state->havedict = 0;
116− state->dmax = 32768U;
117− state->head = Z_NULL;
118− state->wsize = 0;
119− state->whave = 0;
120− state->write = 0;
121− state->hold = 0;
122− state->bits = 0;
123− state->lencode = state->distcode = state->next = state->codes;
124− Tracev((stderr, "inflate: reset\n"));
125− return Z_OK;
126−}
127−
128−int ZEXPORT inflatePrime(strm, bits, value)
129−z_streamp strm;
130−int bits;
131−int value;
132−{
133− struct inflate_state FAR *state;
134−
135− if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
136− state = (struct inflate_state FAR *)strm->state;
137− if (bits > 16 || state->bits + bits > 32) return Z_STREAM_ERROR;
138− value &= (1L << bits) - 1;
139− state->hold += value << state->bits;
140− state->bits += bits;
141− return Z_OK;
142−}
143−
144−int ZEXPORT inflateInit2_(strm, windowBits, version, stream_size)
145−z_streamp strm;
146−int windowBits;
147−const char *version;
148−int stream_size;
149−{
150− struct inflate_state FAR *state;
151−
152− if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
153− stream_size != (int)(sizeof(z_stream)))
154− return Z_VERSION_ERROR;
155− if (strm == Z_NULL) return Z_STREAM_ERROR;
156− strm->msg = Z_NULL; /* in case we return an error */
157− if (strm->zalloc == (alloc_func)0) {
158− strm->zalloc = zcalloc;
159− strm->opaque = (voidpf)0;
160− }
161− if (strm->zfree == (free_func)0) strm->zfree = zcfree;
162− state = (struct inflate_state FAR *)
163− ZALLOC(strm, 1, sizeof(struct inflate_state));
164− if (state == Z_NULL) return Z_MEM_ERROR;
165− Tracev((stderr, "inflate: allocated\n"));
166− strm->state = (struct internal_state FAR *)state;
167− if (windowBits < 0) {
168− state->wrap = 0;
169− windowBits = -windowBits;
170− }
171− else {
172− state->wrap = (windowBits >> 4) + 1;
173−#ifdef GUNZIP
174− if (windowBits < 48) windowBits &= 15;
175−#endif
176− }
177− if (windowBits < 8 || windowBits > 15) {
178− ZFREE(strm, state);
179− strm->state = Z_NULL;
180− return Z_STREAM_ERROR;
181− }
182− state->wbits = (unsigned)windowBits;
183− state->window = Z_NULL;
184− return inflateReset(strm);
185−}
186−
187−int ZEXPORT inflateInit_(strm, version, stream_size)
188−z_streamp strm;
189−const char *version;
190−int stream_size;
191−{
192− return inflateInit2_(strm, DEF_WBITS, version, stream_size);
193−}
194−
195−/*
196− Return state with length and distance decoding tables and index sizes set to
197− fixed code decoding. Normally this returns fixed tables from inffixed.h.
198− If BUILDFIXED is defined, then instead this routine builds the tables the
199− first time it's called, and returns those tables the first time and
200− thereafter. This reduces the size of the code by about 2K bytes, in
201− exchange for a little execution time. However, BUILDFIXED should not be
202− used for threaded applications, since the rewriting of the tables and virgin
203− may not be thread-safe.
204− */
205−local void fixedtables(state)
206−struct inflate_state FAR *state;
207−{
208−#ifdef BUILDFIXED
209− static int virgin = 1;
210− static code *lenfix, *distfix;
211− static code fixed[544];
212−
213− /* build fixed huffman tables if first call (may not be thread safe) */
214− if (virgin) {
215− unsigned sym, bits;
216− static code *next;
217−
218− /* literal/length table */
219− sym = 0;
220− while (sym < 144) state->lens[sym++] = 8;
221− while (sym < 256) state->lens[sym++] = 9;
222− while (sym < 280) state->lens[sym++] = 7;
223− while (sym < 288) state->lens[sym++] = 8;
224− next = fixed;
225− lenfix = next;
226− bits = 9;
227− inflate_table(LENS, state->lens, 288, &(next), &(bits), state->work);
228−
229− /* distance table */
230− sym = 0;
231− while (sym < 32) state->lens[sym++] = 5;
232− distfix = next;
233− bits = 5;
234− inflate_table(DISTS, state->lens, 32, &(next), &(bits), state->work);
235−
236− /* do this just once */
237− virgin = 0;
238− }
239−#else /* !BUILDFIXED */
240−# include "inffixed.h"
241−#endif /* BUILDFIXED */
242− state->lencode = lenfix;
243− state->lenbits = 9;
244− state->distcode = distfix;
245− state->distbits = 5;
246−}
247−
248−#ifdef MAKEFIXED
249−#include <stdio.h>
250−
251−/*
252− Write out the inffixed.h that is #include'd above. Defining MAKEFIXED also
253− defines BUILDFIXED, so the tables are built on the fly. makefixed() writes
254− those tables to stdout, which would be piped to inffixed.h. A small program
255− can simply call makefixed to do this:
256−
257− void makefixed(void);
258−
259− int main(void)
260− {
261− makefixed();
262− return 0;
263− }
264−
265− Then that can be linked with zlib built with MAKEFIXED defined and run:
266−
267− a.out > inffixed.h
268− */
269−void makefixed()
270−{
271− unsigned low, size;
272− struct inflate_state state;
273−
274− fixedtables(&state);
275− puts(" /* inffixed.h -- table for decoding fixed codes");
276− puts(" * Generated automatically by makefixed().");
277− puts(" */");
278− puts("");
279− puts(" /* WARNING: this file should *not* be used by applications.");
280− puts(" It is part of the implementation of this library and is");
281− puts(" subject to change. Applications should only use zlib.h.");
282− puts(" */");
283− puts("");
284− size = 1U << 9;
285− printf(" static const code lenfix[%u] = {", size);
286− low = 0;
287− for (;;) {
288− if ((low % 7) == 0) printf("\n ");
289− printf("{%u,%u,%d}", state.lencode[low].op, state.lencode[low].bits,
290− state.lencode[low].val);
291− if (++low == size) break;
292− putchar(',');
293− }
294− puts("\n };");
295− size = 1U << 5;
296− printf("\n static const code distfix[%u] = {", size);
297− low = 0;
298− for (;;) {
299− if ((low % 6) == 0) printf("\n ");
300− printf("{%u,%u,%d}", state.distcode[low].op, state.distcode[low].bits,
301− state.distcode[low].val);
302− if (++low == size) break;
303− putchar(',');
304− }
305− puts("\n };");
306−}
307−#endif /* MAKEFIXED */
308−
309−/*
310− Update the window with the last wsize (normally 32K) bytes written before
311− returning. If window does not exist yet, create it. This is only called
312− when a window is already in use, or when output has been written during this
313− inflate call, but the end of the deflate stream has not been reached yet.
314− It is also called to create a window for dictionary data when a dictionary
315− is loaded.
316−
317− Providing output buffers larger than 32K to inflate() should provide a speed
318− advantage, since only the last 32K of output is copied to the sliding window
319− upon return from inflate(), and since all distances after the first 32K of
320− output will fall in the output data, making match copies simpler and faster.
321− The advantage may be dependent on the size of the processor's data caches.
322− */
323−local int updatewindow(strm, out)
324−z_streamp strm;
325−unsigned out;
326−{
327− struct inflate_state FAR *state;
328− unsigned copy, dist;
329−
330− state = (struct inflate_state FAR *)strm->state;
331−
332− /* if it hasn't been done already, allocate space for the window */
333− if (state->window == Z_NULL) {
334− state->window = (unsigned char FAR *)
335− ZALLOC(strm, 1U << state->wbits,
336− sizeof(unsigned char));
337− if (state->window == Z_NULL) return 1;
338− }
339−
340− /* if window not in use yet, initialize */
341− if (state->wsize == 0) {
342− state->wsize = 1U << state->wbits;
343− state->write = 0;
344− state->whave = 0;
345− }
346−
347− /* copy state->wsize or less output bytes into the circular window */
348− copy = out - strm->avail_out;
349− if (copy >= state->wsize) {
350− zmemcpy(state->window, strm->next_out - state->wsize, state->wsize);
351− state->write = 0;
352− state->whave = state->wsize;
353− }
354− else {
355− dist = state->wsize - state->write;
356− if (dist > copy) dist = copy;
357− zmemcpy(state->window + state->write, strm->next_out - copy, dist);
358− copy -= dist;
359− if (copy) {
360− zmemcpy(state->window, strm->next_out - copy, copy);
361− state->write = copy;
362− state->whave = state->wsize;
363− }
364− else {
365− state->write += dist;
366− if (state->write == state->wsize) state->write = 0;
367− if (state->whave < state->wsize) state->whave += dist;
368− }
369− }
370− return 0;
371−}
372−
373−/* Macros for inflate(): */
374−
375−/* check function to use adler32() for zlib or crc32() for gzip */
376−#ifdef GUNZIP
377−# define UPDATE(check, buf, len) \
378− (state->flags ? crc32(check, buf, len) : adler32(check, buf, len))
379−#else
380−# define UPDATE(check, buf, len) adler32(check, buf, len)
381−#endif
382−
383−/* check macros for header crc */
384−#ifdef GUNZIP
385−# define CRC2(check, word) \
386− do { \
387− hbuf[0] = (unsigned char)(word); \
388− hbuf[1] = (unsigned char)((word) >> 8); \
389− check = crc32(check, hbuf, 2); \
390− } while (0)
391−
392−# define CRC4(check, word) \
393− do { \
394− hbuf[0] = (unsigned char)(word); \
395− hbuf[1] = (unsigned char)((word) >> 8); \
396− hbuf[2] = (unsigned char)((word) >> 16); \
397− hbuf[3] = (unsigned char)((word) >> 24); \
398− check = crc32(check, hbuf, 4); \
399− } while (0)
400−#endif
401−
402−/* Load registers with state in inflate() for speed */
403−#define LOAD() \
404− do { \
405− put = strm->next_out; \
406− left = strm->avail_out; \
407− next = strm->next_in; \
408− have = strm->avail_in; \
409− hold = state->hold; \
410− bits = state->bits; \
411− } while (0)
412−
413−/* Restore state from registers in inflate() */
414−#define RESTORE() \
415− do { \
416− strm->next_out = put; \
417− strm->avail_out = left; \
418− strm->next_in = next; \
419− strm->avail_in = have; \
420− state->hold = hold; \
421− state->bits = bits; \
422− } while (0)
423−
424−/* Clear the input bit accumulator */
425−#define INITBITS() \
426− do { \
427− hold = 0; \
428− bits = 0; \
429− } while (0)
430−
431−/* Get a byte of input into the bit accumulator, or return from inflate()
432− if there is no input available. */
433−#define PULLBYTE() \
434− do { \
435− if (have == 0) goto inf_leave; \
436− have--; \
437− hold += (unsigned long)(*next++) << bits; \
438− bits += 8; \
439− } while (0)
440−
441−/* Assure that there are at least n bits in the bit accumulator. If there is
442− not enough available input to do that, then return from inflate(). */
443−#define NEEDBITS(n) \
444− do { \
445− while (bits < (unsigned)(n)) \
446− PULLBYTE(); \
447− } while (0)
448−
449−/* Return the low n bits of the bit accumulator (n < 16) */
450−#define BITS(n) \
451− ((unsigned)hold & ((1U << (n)) - 1))
452−
453−/* Remove n bits from the bit accumulator */
454−#define DROPBITS(n) \
455− do { \
456− hold >>= (n); \
457− bits -= (unsigned)(n); \
458− } while (0)
459−
460−/* Remove zero to seven bits as needed to go to a byte boundary */
461−#define BYTEBITS() \
462− do { \
463− hold >>= bits & 7; \
464− bits -= bits & 7; \
465− } while (0)
466−
467−/* Reverse the bytes in a 32-bit value */
468−#define REVERSE(q) \
469− ((((q) >> 24) & 0xff) + (((q) >> 8) & 0xff00) + \
470− (((q) & 0xff00) << 8) + (((q) & 0xff) << 24))
471−
472−/*
473− inflate() uses a state machine to process as much input data and generate as
474− much output data as possible before returning. The state machine is
475− structured roughly as follows:
476−
477− for (;;) switch (state) {
478− ...
479− case STATEn:
480− if (not enough input data or output space to make progress)
481− return;
482− ... make progress ...
483− state = STATEm;
484− break;
485− ...
486− }
487−
488− so when inflate() is called again, the same case is attempted again, and
489− if the appropriate resources are provided, the machine proceeds to the
490− next state. The NEEDBITS() macro is usually the way the state evaluates
491− whether it can proceed or should return. NEEDBITS() does the return if
492− the requested bits are not available. The typical use of the BITS macros
493− is:
494−
495− NEEDBITS(n);
496− ... do something with BITS(n) ...
497− DROPBITS(n);
498−
499− where NEEDBITS(n) either returns from inflate() if there isn't enough
500− input left to load n bits into the accumulator, or it continues. BITS(n)
501− gives the low n bits in the accumulator. When done, DROPBITS(n) drops
502− the low n bits off the accumulator. INITBITS() clears the accumulator
503− and sets the number of available bits to zero. BYTEBITS() discards just
504− enough bits to put the accumulator on a byte boundary. After BYTEBITS()
505− and a NEEDBITS(8), then BITS(8) would return the next byte in the stream.
506−
507− NEEDBITS(n) uses PULLBYTE() to get an available byte of input, or to return
508− if there is no input available. The decoding of variable length codes uses
509− PULLBYTE() directly in order to pull just enough bytes to decode the next
510− code, and no more.
511−
512− Some states loop until they get enough input, making sure that enough
513− state information is maintained to continue the loop where it left off
514− if NEEDBITS() returns in the loop. For example, want, need, and keep
515− would all have to actually be part of the saved state in case NEEDBITS()
516− returns:
517−
518− case STATEw:
519− while (want < need) {
520− NEEDBITS(n);
521− keep[want++] = BITS(n);
522− DROPBITS(n);
523− }
524− state = STATEx;
525− case STATEx:
526−
527− As shown above, if the next state is also the next case, then the break
528− is omitted.
529−
530− A state may also return if there is not enough output space available to
531− complete that state. Those states are copying stored data, writing a
532− literal byte, and copying a matching string.
533−
534− When returning, a "goto inf_leave" is used to update the total counters,
535− update the check value, and determine whether any progress has been made
536− during that inflate() call in order to return the proper return code.
537− Progress is defined as a change in either strm->avail_in or strm->avail_out.
538− When there is a window, goto inf_leave will update the window with the last
539− output written. If a goto inf_leave occurs in the middle of decompression
540− and there is no window currently, goto inf_leave will create one and copy
541− output to the window for the next call of inflate().
542−
543− In this implementation, the flush parameter of inflate() only affects the
544− return code (per zlib.h). inflate() always writes as much as possible to
545− strm->next_out, given the space available and the provided input--the effect
546− documented in zlib.h of Z_SYNC_FLUSH. Furthermore, inflate() always defers
547− the allocation of and copying into a sliding window until necessary, which
548− provides the effect documented in zlib.h for Z_FINISH when the entire input
549− stream available. So the only thing the flush parameter actually does is:
550− when flush is set to Z_FINISH, inflate() cannot return Z_OK. Instead it
551− will return Z_BUF_ERROR if it has not reached the end of the stream.
552− */
553−
554−int ZEXPORT inflate(strm, flush)
555−z_streamp strm;
556−int flush;
557−{
558− struct inflate_state FAR *state;
559− unsigned char FAR *next; /* next input */
560− unsigned char FAR *put; /* next output */
561− unsigned have, left; /* available input and output */
562− unsigned long hold; /* bit buffer */
563− unsigned bits; /* bits in bit buffer */
564− unsigned in, out; /* save starting available input and output */
565− unsigned copy; /* number of stored or match bytes to copy */
566− unsigned char FAR *from; /* where to copy match bytes from */
567− code this; /* current decoding table entry */
568− code last; /* parent table entry */
569− unsigned len; /* length to copy for repeats, bits to drop */
570− int ret; /* return code */
571−#ifdef GUNZIP
572− unsigned char hbuf[4]; /* buffer for gzip header crc calculation */
573−#endif
574− static const unsigned short order[19] = /* permutation of code lengths */
575− {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
576−
577− if (strm == Z_NULL || strm->state == Z_NULL || strm->next_out == Z_NULL ||
578− (strm->next_in == Z_NULL && strm->avail_in != 0))
579− return Z_STREAM_ERROR;
580−
581− state = (struct inflate_state FAR *)strm->state;
582− if (state->mode == TYPE) state->mode = TYPEDO; /* skip check */
583− LOAD();
584− in = have;
585− out = left;
586− ret = Z_OK;
587− for (;;)
588− switch (state->mode) {
589− case HEAD:
590− if (state->wrap == 0) {
591− state->mode = TYPEDO;
592− break;
593− }
594− NEEDBITS(16);
595−#ifdef GUNZIP
596− if ((state->wrap & 2) && hold == 0x8b1f) { /* gzip header */
597− state->check = crc32(0L, Z_NULL, 0);
598− CRC2(state->check, hold);
599− INITBITS();
600− state->mode = FLAGS;
601− break;
602− }
603− state->flags = 0; /* expect zlib header */
604− if (state->head != Z_NULL)
605− state->head->done = -1;
606− if (!(state->wrap & 1) || /* check if zlib header allowed */
607−#else
608− if (
609−#endif
610− ((BITS(8) << 8) + (hold >> 8)) % 31) {
611− strm->msg = (char *)"incorrect header check";
612− state->mode = BAD;
613− break;
614− }
615− if (BITS(4) != Z_DEFLATED) {
616− strm->msg = (char *)"unknown compression method";
617− state->mode = BAD;
618− break;
619− }
620− DROPBITS(4);
621− len = BITS(4) + 8;
622− if (len > state->wbits) {
623− strm->msg = (char *)"invalid window size";
624− state->mode = BAD;
625− break;
626− }
627− state->dmax = 1U << len;
628− Tracev((stderr, "inflate: zlib header ok\n"));
629− strm->adler = state->check = adler32(0L, Z_NULL, 0);
630− state->mode = hold & 0x200 ? DICTID : TYPE;
631− INITBITS();
632− break;
633−#ifdef GUNZIP
634− case FLAGS:
635− NEEDBITS(16);
636− state->flags = (int)(hold);
637− if ((state->flags & 0xff) != Z_DEFLATED) {
638− strm->msg = (char *)"unknown compression method";
639− state->mode = BAD;
640− break;
641− }
642− if (state->flags & 0xe000) {
643− strm->msg = (char *)"unknown header flags set";
644− state->mode = BAD;
645− break;
646− }
647− if (state->head != Z_NULL)
648− state->head->text = (int)((hold >> 8) & 1);
649− if (state->flags & 0x0200) CRC2(state->check, hold);
650− INITBITS();
651− state->mode = TIME;
652− case TIME:
653− NEEDBITS(32);
654− if (state->head != Z_NULL)
655− state->head->time = hold;
656− if (state->flags & 0x0200) CRC4(state->check, hold);
657− INITBITS();
658− state->mode = OS;
659− case OS:
660− NEEDBITS(16);
661− if (state->head != Z_NULL) {
662− state->head->xflags = (int)(hold & 0xff);
663− state->head->os = (int)(hold >> 8);
664− }
665− if (state->flags & 0x0200) CRC2(state->check, hold);
666− INITBITS();
667− state->mode = EXLEN;
668− case EXLEN:
669− if (state->flags & 0x0400) {
670− NEEDBITS(16);
671− state->length = (unsigned)(hold);
672− if (state->head != Z_NULL)
673− state->head->extra_len = (unsigned)hold;
674− if (state->flags & 0x0200) CRC2(state->check, hold);
675− INITBITS();
676− }
677− else if (state->head != Z_NULL)
678− state->head->extra = Z_NULL;
679− state->mode = EXTRA;
680− case EXTRA:
681− if (state->flags & 0x0400) {
682− copy = state->length;
683− if (copy > have) copy = have;
684− if (copy) {
685− if (state->head != Z_NULL &&
686− state->head->extra != Z_NULL) {
687− len = state->head->extra_len - state->length;
688− zmemcpy(state->head->extra + len, next,
689− len + copy > state->head->extra_max ?
690− state->head->extra_max - len : copy);
691− }
692− if (state->flags & 0x0200)
693− state->check = crc32(state->check, next, copy);
694− have -= copy;
695− next += copy;
696− state->length -= copy;
697− }
698− if (state->length) goto inf_leave;
699− }
700− state->length = 0;
701− state->mode = NAME;
702− case NAME:
703− if (state->flags & 0x0800) {
704− if (have == 0) goto inf_leave;
705− copy = 0;
706− do {
707− len = (unsigned)(next[copy++]);
708− if (state->head != Z_NULL &&
709− state->head->name != Z_NULL &&
710− state->length < state->head->name_max)
711− state->head->name[state->length++] = len;
712− } while (len && copy < have);
713− if (state->flags & 0x0200)
714− state->check = crc32(state->check, next, copy);
715− have -= copy;
716− next += copy;
717− if (len) goto inf_leave;
718− }
719− else if (state->head != Z_NULL)
720− state->head->name = Z_NULL;
721− state->length = 0;
722− state->mode = COMMENT;
723− case COMMENT:
724− if (state->flags & 0x1000) {
725− if (have == 0) goto inf_leave;
726− copy = 0;
727− do {
728− len = (unsigned)(next[copy++]);
729− if (state->head != Z_NULL &&
730− state->head->comment != Z_NULL &&
731− state->length < state->head->comm_max)
732− state->head->comment[state->length++] = len;
733− } while (len && copy < have);
734− if (state->flags & 0x0200)
735− state->check = crc32(state->check, next, copy);
736− have -= copy;
737− next += copy;
738− if (len) goto inf_leave;
739− }
740− else if (state->head != Z_NULL)
741− state->head->comment = Z_NULL;
742− state->mode = HCRC;
743− case HCRC:
744− if (state->flags & 0x0200) {
745− NEEDBITS(16);
746− if (hold != (state->check & 0xffff)) {
747− strm->msg = (char *)"header crc mismatch";
748− state->mode = BAD;
749− break;
750− }
751− INITBITS();
752− }
753− if (state->head != Z_NULL) {
754− state->head->hcrc = (int)((state->flags >> 9) & 1);
755− state->head->done = 1;
756− }
757− strm->adler = state->check = crc32(0L, Z_NULL, 0);
758− state->mode = TYPE;
759− break;
760−#endif
761− case DICTID:
762− NEEDBITS(32);
763− strm->adler = state->check = REVERSE(hold);
764− INITBITS();
765− state->mode = DICT;
766− case DICT:
767− if (state->havedict == 0) {
768− RESTORE();
769− return Z_NEED_DICT;
770− }
771− strm->adler = state->check = adler32(0L, Z_NULL, 0);
772− state->mode = TYPE;
773− case TYPE:
774− if (flush == Z_BLOCK) goto inf_leave;
775− case TYPEDO:
776− if (state->last) {
777− BYTEBITS();
778− state->mode = CHECK;
779− break;
780− }
781− NEEDBITS(3);
782− state->last = BITS(1);
783− DROPBITS(1);
784− switch (BITS(2)) {
785− case 0: /* stored block */
786− Tracev((stderr, "inflate: stored block%s\n",
787− state->last ? " (last)" : ""));
788− state->mode = STORED;
789− break;
790− case 1: /* fixed block */
791− fixedtables(state);
792− Tracev((stderr, "inflate: fixed codes block%s\n",
793− state->last ? " (last)" : ""));
794− state->mode = LEN; /* decode codes */
795− break;
796− case 2: /* dynamic block */
797− Tracev((stderr, "inflate: dynamic codes block%s\n",
798− state->last ? " (last)" : ""));
799− state->mode = TABLE;
800− break;
801− case 3:
802− strm->msg = (char *)"invalid block type";
803− state->mode = BAD;
804− }
805− DROPBITS(2);
806− break;
807− case STORED:
808− BYTEBITS(); /* go to byte boundary */
809− NEEDBITS(32);
810− if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) {
811− strm->msg = (char *)"invalid stored block lengths";
812− state->mode = BAD;
813− break;
814− }
815− state->length = (unsigned)hold & 0xffff;
816− Tracev((stderr, "inflate: stored length %u\n",
817− state->length));
818− INITBITS();
819− state->mode = COPY;
820− case COPY:
821− copy = state->length;
822− if (copy) {
823− if (copy > have) copy = have;
824− if (copy > left) copy = left;
825− if (copy == 0) goto inf_leave;
826− zmemcpy(put, next, copy);
827− have -= copy;
828− next += copy;
829− left -= copy;
830− put += copy;
831− state->length -= copy;
832− break;
833− }
834− Tracev((stderr, "inflate: stored end\n"));
835− state->mode = TYPE;
836− break;
837− case TABLE:
838− NEEDBITS(14);
839− state->nlen = BITS(5) + 257;
840− DROPBITS(5);
841− state->ndist = BITS(5) + 1;
842− DROPBITS(5);
843− state->ncode = BITS(4) + 4;
844− DROPBITS(4);
845−#ifndef PKZIP_BUG_WORKAROUND
846− if (state->nlen > 286 || state->ndist > 30) {
847− strm->msg = (char *)"too many length or distance symbols";
848− state->mode = BAD;
849− break;
850− }
851−#endif
852− Tracev((stderr, "inflate: table sizes ok\n"));
853− state->have = 0;
854− state->mode = LENLENS;
855− case LENLENS:
856− while (state->have < state->ncode) {
857− NEEDBITS(3);
858− state->lens[order[state->have++]] = (unsigned short)BITS(3);
859− DROPBITS(3);
860− }
861− while (state->have < 19)
862− state->lens[order[state->have++]] = 0;
863− state->next = state->codes;
864− state->lencode = (code const FAR *)(state->next);
865− state->lenbits = 7;
866− ret = inflate_table(CODES, state->lens, 19, &(state->next),
867− &(state->lenbits), state->work);
868− if (ret) {
869− strm->msg = (char *)"invalid code lengths set";
870− state->mode = BAD;
871− break;
872− }
873− Tracev((stderr, "inflate: code lengths ok\n"));
874− state->have = 0;
875− state->mode = CODELENS;
876− case CODELENS:
877− while (state->have < state->nlen + state->ndist) {
878− for (;;) {
879− this = state->lencode[BITS(state->lenbits)];
880− if ((unsigned)(this.bits) <= bits) break;
881− PULLBYTE();
882− }
883− if (this.val < 16) {
884− NEEDBITS(this.bits);
885− DROPBITS(this.bits);
886− state->lens[state->have++] = this.val;
887− }
888− else {
889− if (this.val == 16) {
890− NEEDBITS(this.bits + 2);
891− DROPBITS(this.bits);
892− if (state->have == 0) {
893− strm->msg = (char *)"invalid bit length repeat";
894− state->mode = BAD;
895− break;
896− }
897− len = state->lens[state->have - 1];
898− copy = 3 + BITS(2);
899− DROPBITS(2);
900− }
901− else if (this.val == 17) {
902− NEEDBITS(this.bits + 3);
903− DROPBITS(this.bits);
904− len = 0;
905− copy = 3 + BITS(3);
906− DROPBITS(3);
907− }
908− else {
909− NEEDBITS(this.bits + 7);
910− DROPBITS(this.bits);
911− len = 0;
912− copy = 11 + BITS(7);
913− DROPBITS(7);
914− }
915− if (state->have + copy > state->nlen + state->ndist) {
916− strm->msg = (char *)"invalid bit length repeat";
917− state->mode = BAD;
918− break;
919− }
920− while (copy--)
921− state->lens[state->have++] = (unsigned short)len;
922− }
923− }
924−
925− /* handle error breaks in while */
926− if (state->mode == BAD) break;
927−
928− /* build code tables */
929− state->next = state->codes;
930− state->lencode = (code const FAR *)(state->next);
931− state->lenbits = 9;
932− ret = inflate_table(LENS, state->lens, state->nlen, &(state->next),
933− &(state->lenbits), state->work);
934− if (ret) {
935− strm->msg = (char *)"invalid literal/lengths set";
936− state->mode = BAD;
937− break;
938− }
939− state->distcode = (code const FAR *)(state->next);
940− state->distbits = 6;
941− ret = inflate_table(DISTS, state->lens + state->nlen, state->ndist,
942− &(state->next), &(state->distbits), state->work);
943− if (ret) {
944− strm->msg = (char *)"invalid distances set";
945− state->mode = BAD;
946− break;
947− }
948− Tracev((stderr, "inflate: codes ok\n"));
949− state->mode = LEN;
950− case LEN:
951− if (have >= 6 && left >= 258) {
952− RESTORE();
953− inflate_fast(strm, out);
954− LOAD();
955− break;
956− }
957− for (;;) {
958− this = state->lencode[BITS(state->lenbits)];
959− if ((unsigned)(this.bits) <= bits) break;
960− PULLBYTE();
961− }
962− if (this.op && (this.op & 0xf0) == 0) {
963− last = this;
964− for (;;) {
965− this = state->lencode[last.val +
966− (BITS(last.bits + last.op) >> last.bits)];
967− if ((unsigned)(last.bits + this.bits) <= bits) break;
968− PULLBYTE();
969− }
970− DROPBITS(last.bits);
971− }
972− DROPBITS(this.bits);
973− state->length = (unsigned)this.val;
974− if ((int)(this.op) == 0) {
975− Tracevv((stderr, this.val >= 0x20 && this.val < 0x7f ?
976− "inflate: literal '%c'\n" :
977− "inflate: literal 0x%02x\n", this.val));
978− state->mode = LIT;
979− break;
980− }
981− if (this.op & 32) {
982− Tracevv((stderr, "inflate: end of block\n"));
983− state->mode = TYPE;
984− break;
985− }
986− if (this.op & 64) {
987− strm->msg = (char *)"invalid literal/length code";
988− state->mode = BAD;
989− break;
990− }
991− state->extra = (unsigned)(this.op) & 15;
992− state->mode = LENEXT;
993− case LENEXT:
994− if (state->extra) {
995− NEEDBITS(state->extra);
996− state->length += BITS(state->extra);
997− DROPBITS(state->extra);
998− }
999− Tracevv((stderr, "inflate: length %u\n", state->length));
1000− state->mode = DIST;
1001− case DIST:
1002− for (;;) {
1003− this = state->distcode[BITS(state->distbits)];
1004− if ((unsigned)(this.bits) <= bits) break;
1005− PULLBYTE();
1006− }
1007− if ((this.op & 0xf0) == 0) {
1008− last = this;
1009− for (;;) {
1010− this = state->distcode[last.val +
1011− (BITS(last.bits + last.op) >> last.bits)];
1012− if ((unsigned)(last.bits + this.bits) <= bits) break;
1013− PULLBYTE();
1014− }
1015− DROPBITS(last.bits);
1016− }
1017− DROPBITS(this.bits);
1018− if (this.op & 64) {
1019− strm->msg = (char *)"invalid distance code";
1020− state->mode = BAD;
1021− break;
1022− }
1023− state->offset = (unsigned)this.val;
1024− state->extra = (unsigned)(this.op) & 15;
1025− state->mode = DISTEXT;
1026− case DISTEXT:
1027− if (state->extra) {
1028− NEEDBITS(state->extra);
1029− state->offset += BITS(state->extra);
1030− DROPBITS(state->extra);
1031− }
1032−#ifdef INFLATE_STRICT
1033− if (state->offset > state->dmax) {
1034− strm->msg = (char *)"invalid distance too far back";
1035− state->mode = BAD;
1036− break;
1037− }
1038−#endif
1039− if (state->offset > state->whave + out - left) {
1040− strm->msg = (char *)"invalid distance too far back";
1041− state->mode = BAD;
1042− break;
1043− }
1044− Tracevv((stderr, "inflate: distance %u\n", state->offset));
1045− state->mode = MATCH;
1046− case MATCH:
1047− if (left == 0) goto inf_leave;
1048− copy = out - left;
1049− if (state->offset > copy) { /* copy from window */
1050− copy = state->offset - copy;
1051− if (copy > state->write) {
1052− copy -= state->write;
1053− from = state->window + (state->wsize - copy);
1054− }
1055− else
1056− from = state->window + (state->write - copy);
1057− if (copy > state->length) copy = state->length;
1058− }
1059− else { /* copy from output */
1060− from = put - state->offset;
1061− copy = state->length;
1062− }
1063− if (copy > left) copy = left;
1064− left -= copy;
1065− state->length -= copy;
1066− do {
1067− *put++ = *from++;
1068− } while (--copy);
1069− if (state->length == 0) state->mode = LEN;
1070− break;
1071− case LIT:
1072− if (left == 0) goto inf_leave;
1073− *put++ = (unsigned char)(state->length);
1074− left--;
1075− state->mode = LEN;
1076− break;
1077− case CHECK:
1078− if (state->wrap) {
1079− NEEDBITS(32);
1080− out -= left;
1081− strm->total_out += out;
1082− state->total += out;
1083− if (out)
1084− strm->adler = state->check =
1085− UPDATE(state->check, put - out, out);
1086− out = left;
1087− if ((
1088−#ifdef GUNZIP
1089− state->flags ? hold :
1090−#endif
1091− REVERSE(hold)) != state->check) {
1092− strm->msg = (char *)"incorrect data check";
1093− state->mode = BAD;
1094− break;
1095− }
1096− INITBITS();
1097− Tracev((stderr, "inflate: check matches trailer\n"));
1098− }
1099−#ifdef GUNZIP
1100− state->mode = LENGTH;
1101− case LENGTH:
1102− if (state->wrap && state->flags) {
1103− NEEDBITS(32);
1104− if (hold != (state->total & 0xffffffffUL)) {
1105− strm->msg = (char *)"incorrect length check";
1106− state->mode = BAD;
1107− break;
1108− }
1109− INITBITS();
1110− Tracev((stderr, "inflate: length matches trailer\n"));
1111− }
1112−#endif
1113− state->mode = DONE;
1114− case DONE:
1115− ret = Z_STREAM_END;
1116− goto inf_leave;
1117− case BAD:
1118− ret = Z_DATA_ERROR;
1119− goto inf_leave;
1120− case MEM:
1121− return Z_MEM_ERROR;
1122− case SYNC:
1123− default:
1124− return Z_STREAM_ERROR;
1125− }
1126−
1127− /*
1128− Return from inflate(), updating the total counts and the check value.
1129− If there was no progress during the inflate() call, return a buffer
1130− error. Call updatewindow() to create and/or update the window state.
1131− Note: a memory error from inflate() is non-recoverable.
1132− */
1133− inf_leave:
1134− RESTORE();
1135− if (state->wsize || (state->mode < CHECK && out != strm->avail_out))
1136− if (updatewindow(strm, out)) {
1137− state->mode = MEM;
1138− return Z_MEM_ERROR;
1139− }
1140− in -= strm->avail_in;
1141− out -= strm->avail_out;
1142− strm->total_in += in;
1143− strm->total_out += out;
1144− state->total += out;
1145− if (state->wrap && out)
1146− strm->adler = state->check =
1147− UPDATE(state->check, strm->next_out - out, out);
1148− strm->data_type = state->bits + (state->last ? 64 : 0) +
1149− (state->mode == TYPE ? 128 : 0);
1150− if (((in == 0 && out == 0) || flush == Z_FINISH) && ret == Z_OK)
1151− ret = Z_BUF_ERROR;
1152− return ret;
1153−}
1154−
1155−int ZEXPORT inflateEnd(strm)
1156−z_streamp strm;
1157−{
1158− struct inflate_state FAR *state;
1159− if (strm == Z_NULL || strm->state == Z_NULL || strm->zfree == (free_func)0)
1160− return Z_STREAM_ERROR;
1161− state = (struct inflate_state FAR *)strm->state;
1162− if (state->window != Z_NULL) ZFREE(strm, state->window);
1163− ZFREE(strm, strm->state);
1164− strm->state = Z_NULL;
1165− Tracev((stderr, "inflate: end\n"));
1166− return Z_OK;
1167−}
1168−
1169−int ZEXPORT inflateSetDictionary(strm, dictionary, dictLength)
1170−z_streamp strm;
1171−const Bytef *dictionary;
1172−uInt dictLength;
1173−{
1174− struct inflate_state FAR *state;
1175− unsigned long id;
1176−
1177− /* check state */
1178− if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
1179− state = (struct inflate_state FAR *)strm->state;
1180− if (state->wrap != 0 && state->mode != DICT)
1181− return Z_STREAM_ERROR;
1182−
1183− /* check for correct dictionary id */
1184− if (state->mode == DICT) {
1185− id = adler32(0L, Z_NULL, 0);
1186− id = adler32(id, dictionary, dictLength);
1187− if (id != state->check)
1188− return Z_DATA_ERROR;
1189− }
1190−
1191− /* copy dictionary to window */
1192− if (updatewindow(strm, strm->avail_out)) {
1193− state->mode = MEM;
1194− return Z_MEM_ERROR;
1195− }
1196− if (dictLength > state->wsize) {
1197− zmemcpy(state->window, dictionary + dictLength - state->wsize,
1198− state->wsize);
1199− state->whave = state->wsize;
1200− }
1201− else {
1202− zmemcpy(state->window + state->wsize - dictLength, dictionary,
1203− dictLength);
1204− state->whave = dictLength;
1205− }
1206− state->havedict = 1;
1207− Tracev((stderr, "inflate: dictionary set\n"));
1208− return Z_OK;
1209−}
1210−
1211−int ZEXPORT inflateGetHeader(strm, head)
1212−z_streamp strm;
1213−gz_headerp head;
1214−{
1215− struct inflate_state FAR *state;
1216−
1217− /* check state */
1218− if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
1219− state = (struct inflate_state FAR *)strm->state;
1220− if ((state->wrap & 2) == 0) return Z_STREAM_ERROR;
1221−
1222− /* save header structure */
1223− state->head = head;
1224− head->done = 0;
1225− return Z_OK;
1226−}
1227−
1228−/*
1229− Search buf[0..len-1] for the pattern: 0, 0, 0xff, 0xff. Return when found
1230− or when out of input. When called, *have is the number of pattern bytes
1231− found in order so far, in 0..3. On return *have is updated to the new
1232− state. If on return *have equals four, then the pattern was found and the
1233− return value is how many bytes were read including the last byte of the
1234− pattern. If *have is less than four, then the pattern has not been found
1235− yet and the return value is len. In the latter case, syncsearch() can be
1236− called again with more data and the *have state. *have is initialized to
1237− zero for the first call.
1238− */
1239−local unsigned syncsearch(have, buf, len)
1240−unsigned FAR *have;
1241−unsigned char FAR *buf;
1242−unsigned len;
1243−{
1244− unsigned got;
1245− unsigned next;
1246−
1247− got = *have;
1248− next = 0;
1249− while (next < len && got < 4) {
1250− if ((int)(buf[next]) == (got < 2 ? 0 : 0xff))
1251− got++;
1252− else if (buf[next])
1253− got = 0;
1254− else
1255− got = 4 - got;
1256− next++;
1257− }
1258− *have = got;
1259− return next;
1260−}
1261−
1262−int ZEXPORT inflateSync(strm)
1263−z_streamp strm;
1264−{
1265− unsigned len; /* number of bytes to look at or looked at */
1266− unsigned long in, out; /* temporary to save total_in and total_out */
1267− unsigned char buf[4]; /* to restore bit buffer to byte string */
1268− struct inflate_state FAR *state;
1269−
1270− /* check parameters */
1271− if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
1272− state = (struct inflate_state FAR *)strm->state;
1273− if (strm->avail_in == 0 && state->bits < 8) return Z_BUF_ERROR;
1274−
1275− /* if first time, start search in bit buffer */
1276− if (state->mode != SYNC) {
1277− state->mode = SYNC;
1278− state->hold <<= state->bits & 7;
1279− state->bits -= state->bits & 7;
1280− len = 0;
1281− while (state->bits >= 8) {
1282− buf[len++] = (unsigned char)(state->hold);
1283− state->hold >>= 8;
1284− state->bits -= 8;
1285− }
1286− state->have = 0;
1287− syncsearch(&(state->have), buf, len);
1288− }
1289−
1290− /* search available input */
1291− len = syncsearch(&(state->have), strm->next_in, strm->avail_in);
1292− strm->avail_in -= len;
1293− strm->next_in += len;
1294− strm->total_in += len;
1295−
1296− /* return no joy or set up to restart inflate() on a new block */
1297− if (state->have != 4) return Z_DATA_ERROR;
1298− in = strm->total_in; out = strm->total_out;
1299− inflateReset(strm);
1300− strm->total_in = in; strm->total_out = out;
1301− state->mode = TYPE;
1302− return Z_OK;
1303−}
1304−
1305−/*
1306− Returns true if inflate is currently at the end of a block generated by
1307− Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP
1308− implementation to provide an additional safety check. PPP uses
1309− Z_SYNC_FLUSH but removes the length bytes of the resulting empty stored
1310− block. When decompressing, PPP checks that at the end of input packet,
1311− inflate is waiting for these length bytes.
1312− */
1313−int ZEXPORT inflateSyncPoint(strm)
1314−z_streamp strm;
1315−{
1316− struct inflate_state FAR *state;
1317−
1318− if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
1319− state = (struct inflate_state FAR *)strm->state;
1320− return state->mode == STORED && state->bits == 0;
1321−}
1322−
1323−int ZEXPORT inflateCopy(dest, source)
1324−z_streamp dest;
1325−z_streamp source;
1326−{
1327− struct inflate_state FAR *state;
1328− struct inflate_state FAR *copy;
1329− unsigned char FAR *window;
1330− unsigned wsize;
1331−
1332− /* check input */
1333− if (dest == Z_NULL || source == Z_NULL || source->state == Z_NULL ||
1334− source->zalloc == (alloc_func)0 || source->zfree == (free_func)0)
1335− return Z_STREAM_ERROR;
1336− state = (struct inflate_state FAR *)source->state;
1337−
1338− /* allocate space */
1339− copy = (struct inflate_state FAR *)
1340− ZALLOC(source, 1, sizeof(struct inflate_state));
1341− if (copy == Z_NULL) return Z_MEM_ERROR;
1342− window = Z_NULL;
1343− if (state->window != Z_NULL) {
1344− window = (unsigned char FAR *)
1345− ZALLOC(source, 1U << state->wbits, sizeof(unsigned char));
1346− if (window == Z_NULL) {
1347− ZFREE(source, copy);
1348− return Z_MEM_ERROR;
1349− }
1350− }
1351−
1352− /* copy state */
1353− zmemcpy(dest, source, sizeof(z_stream));
1354− zmemcpy(copy, state, sizeof(struct inflate_state));
1355− if (state->lencode >= state->codes &&
1356− state->lencode <= state->codes + ENOUGH - 1) {
1357− copy->lencode = copy->codes + (state->lencode - state->codes);
1358− copy->distcode = copy->codes + (state->distcode - state->codes);
1359− }
1360− copy->next = copy->codes + (state->next - state->codes);
1361− if (window != Z_NULL) {
1362− wsize = 1U << state->wbits;
1363− zmemcpy(window, state->window, wsize);
1364− }
1365− copy->window = window;
1366− dest->state = (struct internal_state FAR *)copy;
1367− return Z_OK;
1368−}
cbits/inflate.hdeleted−115
@@ -1,115 +0,0 @@
1−/* inflate.h -- internal inflate state definition
2− * Copyright (C) 1995-2004 Mark Adler
3− * For conditions of distribution and use, see copyright notice in zlib.h
4− */
5−
6−/* WARNING: this file should *not* be used by applications. It is
7− part of the implementation of the compression library and is
8− subject to change. Applications should only use zlib.h.
9− */
10−
11−/* define NO_GZIP when compiling if you want to disable gzip header and
12− trailer decoding by inflate(). NO_GZIP would be used to avoid linking in
13− the crc code when it is not needed. For shared libraries, gzip decoding
14− should be left enabled. */
15−#ifndef NO_GZIP
16−# define GUNZIP
17−#endif
18−
19−/* Possible inflate modes between inflate() calls */
20−typedef enum {
21− HEAD, /* i: waiting for magic header */
22− FLAGS, /* i: waiting for method and flags (gzip) */
23− TIME, /* i: waiting for modification time (gzip) */
24− OS, /* i: waiting for extra flags and operating system (gzip) */
25− EXLEN, /* i: waiting for extra length (gzip) */
26− EXTRA, /* i: waiting for extra bytes (gzip) */
27− NAME, /* i: waiting for end of file name (gzip) */
28− COMMENT, /* i: waiting for end of comment (gzip) */
29− HCRC, /* i: waiting for header crc (gzip) */
30− DICTID, /* i: waiting for dictionary check value */
31− DICT, /* waiting for inflateSetDictionary() call */
32− TYPE, /* i: waiting for type bits, including last-flag bit */
33− TYPEDO, /* i: same, but skip check to exit inflate on new block */
34− STORED, /* i: waiting for stored size (length and complement) */
35− COPY, /* i/o: waiting for input or output to copy stored block */
36− TABLE, /* i: waiting for dynamic block table lengths */
37− LENLENS, /* i: waiting for code length code lengths */
38− CODELENS, /* i: waiting for length/lit and distance code lengths */
39− LEN, /* i: waiting for length/lit code */
40− LENEXT, /* i: waiting for length extra bits */
41− DIST, /* i: waiting for distance code */
42− DISTEXT, /* i: waiting for distance extra bits */
43− MATCH, /* o: waiting for output space to copy string */
44− LIT, /* o: waiting for output space to write literal */
45− CHECK, /* i: waiting for 32-bit check value */
46− LENGTH, /* i: waiting for 32-bit length (gzip) */
47− DONE, /* finished check, done -- remain here until reset */
48− BAD, /* got a data error -- remain here until reset */
49− MEM, /* got an inflate() memory error -- remain here until reset */
50− SYNC /* looking for synchronization bytes to restart inflate() */
51−} inflate_mode;
52−
53−/*
54− State transitions between above modes -
55−
56− (most modes can go to the BAD or MEM mode -- not shown for clarity)
57−
58− Process header:
59− HEAD -> (gzip) or (zlib)
60− (gzip) -> FLAGS -> TIME -> OS -> EXLEN -> EXTRA -> NAME
61− NAME -> COMMENT -> HCRC -> TYPE
62− (zlib) -> DICTID or TYPE
63− DICTID -> DICT -> TYPE
64− Read deflate blocks:
65− TYPE -> STORED or TABLE or LEN or CHECK
66− STORED -> COPY -> TYPE
67− TABLE -> LENLENS -> CODELENS -> LEN
68− Read deflate codes:
69− LEN -> LENEXT or LIT or TYPE
70− LENEXT -> DIST -> DISTEXT -> MATCH -> LEN
71− LIT -> LEN
72− Process trailer:
73− CHECK -> LENGTH -> DONE
74− */
75−
76−/* state maintained between inflate() calls. Approximately 7K bytes. */
77−struct inflate_state {
78− inflate_mode mode; /* current inflate mode */
79− int last; /* true if processing last block */
80− int wrap; /* bit 0 true for zlib, bit 1 true for gzip */
81− int havedict; /* true if dictionary provided */
82− int flags; /* gzip header method and flags (0 if zlib) */
83− unsigned dmax; /* zlib header max distance (INFLATE_STRICT) */
84− unsigned long check; /* protected copy of check value */
85− unsigned long total; /* protected copy of output count */
86− gz_headerp head; /* where to save gzip header information */
87− /* sliding window */
88− unsigned wbits; /* log base 2 of requested window size */
89− unsigned wsize; /* window size or zero if not using window */
90− unsigned whave; /* valid bytes in the window */
91− unsigned write; /* window write index */
92− unsigned char FAR *window; /* allocated sliding window, if needed */
93− /* bit accumulator */
94− unsigned long hold; /* input bit accumulator */
95− unsigned bits; /* number of bits in "in" */
96− /* for string and stored block copying */
97− unsigned length; /* literal or length of data to copy */
98− unsigned offset; /* distance back to copy string from */
99− /* for table and code decoding */
100− unsigned extra; /* extra bits needed */
101− /* fixed and dynamic code tables */
102− code const FAR *lencode; /* starting table for length/literal codes */
103− code const FAR *distcode; /* starting table for distance codes */
104− unsigned lenbits; /* index bits for lencode */
105− unsigned distbits; /* index bits for distcode */
106− /* dynamic table building */
107− unsigned ncode; /* number of code length code lengths */
108− unsigned nlen; /* number of length code lengths */
109− unsigned ndist; /* number of distance code lengths */
110− unsigned have; /* number of code lengths in lens[] */
111− code FAR *next; /* next available space in codes[] */
112− unsigned short lens[320]; /* temporary storage for code lengths */
113− unsigned short work[288]; /* work area for code table building */
114− code codes[ENOUGH]; /* space for code tables */
115−};
cbits/inftrees.cdeleted−329
@@ -1,329 +0,0 @@
1−/* inftrees.c -- generate Huffman trees for efficient decoding
2− * Copyright (C) 1995-2005 Mark Adler
3− * For conditions of distribution and use, see copyright notice in zlib.h
4− */
5−
6−#include "zutil.h"
7−#include "inftrees.h"
8−
9−#define MAXBITS 15
10−
11−const char inflate_copyright[] =
12− " inflate 1.2.3 Copyright 1995-2005 Mark Adler ";
13−/*
14− If you use the zlib library in a product, an acknowledgment is welcome
15− in the documentation of your product. If for some reason you cannot
16− include such an acknowledgment, I would appreciate that you keep this
17− copyright string in the executable of your product.
18− */
19−
20−/*
21− Build a set of tables to decode the provided canonical Huffman code.
22− The code lengths are lens[0..codes-1]. The result starts at *table,
23− whose indices are 0..2^bits-1. work is a writable array of at least
24− lens shorts, which is used as a work area. type is the type of code
25− to be generated, CODES, LENS, or DISTS. On return, zero is success,
26− -1 is an invalid code, and +1 means that ENOUGH isn't enough. table
27− on return points to the next available entry's address. bits is the
28− requested root table index bits, and on return it is the actual root
29− table index bits. It will differ if the request is greater than the
30− longest code or if it is less than the shortest code.
31− */
32−int inflate_table(type, lens, codes, table, bits, work)
33−codetype type;
34−unsigned short FAR *lens;
35−unsigned codes;
36−code FAR * FAR *table;
37−unsigned FAR *bits;
38−unsigned short FAR *work;
39−{
40− unsigned len; /* a code's length in bits */
41− unsigned sym; /* index of code symbols */
42− unsigned min, max; /* minimum and maximum code lengths */
43− unsigned root; /* number of index bits for root table */
44− unsigned curr; /* number of index bits for current table */
45− unsigned drop; /* code bits to drop for sub-table */
46− int left; /* number of prefix codes available */
47− unsigned used; /* code entries in table used */
48− unsigned huff; /* Huffman code */
49− unsigned incr; /* for incrementing code, index */
50− unsigned fill; /* index for replicating entries */
51− unsigned low; /* low bits for current root entry */
52− unsigned mask; /* mask for low root bits */
53− code this; /* table entry for duplication */
54− code FAR *next; /* next available space in table */
55− const unsigned short FAR *base; /* base value table to use */
56− const unsigned short FAR *extra; /* extra bits table to use */
57− int end; /* use base and extra for symbol > end */
58− unsigned short count[MAXBITS+1]; /* number of codes of each length */
59− unsigned short offs[MAXBITS+1]; /* offsets in table for each length */
60− static const unsigned short lbase[31] = { /* Length codes 257..285 base */
61− 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
62− 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
63− static const unsigned short lext[31] = { /* Length codes 257..285 extra */
64− 16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18, 18,
65− 19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 16, 201, 196};
66− static const unsigned short dbase[32] = { /* Distance codes 0..29 base */
67− 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
68− 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
69− 8193, 12289, 16385, 24577, 0, 0};
70− static const unsigned short dext[32] = { /* Distance codes 0..29 extra */
71− 16, 16, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22,
72− 23, 23, 24, 24, 25, 25, 26, 26, 27, 27,
73− 28, 28, 29, 29, 64, 64};
74−
75− /*
76− Process a set of code lengths to create a canonical Huffman code. The
77− code lengths are lens[0..codes-1]. Each length corresponds to the
78− symbols 0..codes-1. The Huffman code is generated by first sorting the
79− symbols by length from short to long, and retaining the symbol order
80− for codes with equal lengths. Then the code starts with all zero bits
81− for the first code of the shortest length, and the codes are integer
82− increments for the same length, and zeros are appended as the length
83− increases. For the deflate format, these bits are stored backwards
84− from their more natural integer increment ordering, and so when the
85− decoding tables are built in the large loop below, the integer codes
86− are incremented backwards.
87−
88− This routine assumes, but does not check, that all of the entries in
89− lens[] are in the range 0..MAXBITS. The caller must assure this.
90− 1..MAXBITS is interpreted as that code length. zero means that that
91− symbol does not occur in this code.
92−
93− The codes are sorted by computing a count of codes for each length,
94− creating from that a table of starting indices for each length in the
95− sorted table, and then entering the symbols in order in the sorted
96− table. The sorted table is work[], with that space being provided by
97− the caller.
98−
99− The length counts are used for other purposes as well, i.e. finding
100− the minimum and maximum length codes, determining if there are any
101− codes at all, checking for a valid set of lengths, and looking ahead
102− at length counts to determine sub-table sizes when building the
103− decoding tables.
104− */
105−
106− /* accumulate lengths for codes (assumes lens[] all in 0..MAXBITS) */
107− for (len = 0; len <= MAXBITS; len++)
108− count[len] = 0;
109− for (sym = 0; sym < codes; sym++)
110− count[lens[sym]]++;
111−
112− /* bound code lengths, force root to be within code lengths */
113− root = *bits;
114− for (max = MAXBITS; max >= 1; max--)
115− if (count[max] != 0) break;
116− if (root > max) root = max;
117− if (max == 0) { /* no symbols to code at all */
118− this.op = (unsigned char)64; /* invalid code marker */
119− this.bits = (unsigned char)1;
120− this.val = (unsigned short)0;
121− *(*table)++ = this; /* make a table to force an error */
122− *(*table)++ = this;
123− *bits = 1;
124− return 0; /* no symbols, but wait for decoding to report error */
125− }
126− for (min = 1; min <= MAXBITS; min++)
127− if (count[min] != 0) break;
128− if (root < min) root = min;
129−
130− /* check for an over-subscribed or incomplete set of lengths */
131− left = 1;
132− for (len = 1; len <= MAXBITS; len++) {
133− left <<= 1;
134− left -= count[len];
135− if (left < 0) return -1; /* over-subscribed */
136− }
137− if (left > 0 && (type == CODES || max != 1))
138− return -1; /* incomplete set */
139−
140− /* generate offsets into symbol table for each length for sorting */
141− offs[1] = 0;
142− for (len = 1; len < MAXBITS; len++)
143− offs[len + 1] = offs[len] + count[len];
144−
145− /* sort symbols by length, by symbol order within each length */
146− for (sym = 0; sym < codes; sym++)
147− if (lens[sym] != 0) work[offs[lens[sym]]++] = (unsigned short)sym;
148−
149− /*
150− Create and fill in decoding tables. In this loop, the table being
151− filled is at next and has curr index bits. The code being used is huff
152− with length len. That code is converted to an index by dropping drop
153− bits off of the bottom. For codes where len is less than drop + curr,
154− those top drop + curr - len bits are incremented through all values to
155− fill the table with replicated entries.
156−
157− root is the number of index bits for the root table. When len exceeds
158− root, sub-tables are created pointed to by the root entry with an index
159− of the low root bits of huff. This is saved in low to check for when a
160− new sub-table should be started. drop is zero when the root table is
161− being filled, and drop is root when sub-tables are being filled.
162−
163− When a new sub-table is needed, it is necessary to look ahead in the
164− code lengths to determine what size sub-table is needed. The length
165− counts are used for this, and so count[] is decremented as codes are
166− entered in the tables.
167−
168− used keeps track of how many table entries have been allocated from the
169− provided *table space. It is checked when a LENS table is being made
170− against the space in *table, ENOUGH, minus the maximum space needed by
171− the worst case distance code, MAXD. This should never happen, but the
172− sufficiency of ENOUGH has not been proven exhaustively, hence the check.
173− This assumes that when type == LENS, bits == 9.
174−
175− sym increments through all symbols, and the loop terminates when
176− all codes of length max, i.e. all codes, have been processed. This
177− routine permits incomplete codes, so another loop after this one fills
178− in the rest of the decoding tables with invalid code markers.
179− */
180−
181− /* set up for code type */
182− switch (type) {
183− case CODES:
184− base = extra = work; /* dummy value--not used */
185− end = 19;
186− break;
187− case LENS:
188− base = lbase;
189− base -= 257;
190− extra = lext;
191− extra -= 257;
192− end = 256;
193− break;
194− default: /* DISTS */
195− base = dbase;
196− extra = dext;
197− end = -1;
198− }
199−
200− /* initialize state for loop */
201− huff = 0; /* starting code */
202− sym = 0; /* starting code symbol */
203− len = min; /* starting code length */
204− next = *table; /* current table to fill in */
205− curr = root; /* current table index bits */
206− drop = 0; /* current bits to drop from code for index */
207− low = (unsigned)(-1); /* trigger new sub-table when len > root */
208− used = 1U << root; /* use root table entries */
209− mask = used - 1; /* mask for comparing low */
210−
211− /* check available table space */
212− if (type == LENS && used >= ENOUGH - MAXD)
213− return 1;
214−
215− /* process all codes and make table entries */
216− for (;;) {
217− /* create table entry */
218− this.bits = (unsigned char)(len - drop);
219− if ((int)(work[sym]) < end) {
220− this.op = (unsigned char)0;
221− this.val = work[sym];
222− }
223− else if ((int)(work[sym]) > end) {
224− this.op = (unsigned char)(extra[work[sym]]);
225− this.val = base[work[sym]];
226− }
227− else {
228− this.op = (unsigned char)(32 + 64); /* end of block */
229− this.val = 0;
230− }
231−
232− /* replicate for those indices with low len bits equal to huff */
233− incr = 1U << (len - drop);
234− fill = 1U << curr;
235− min = fill; /* save offset to next table */
236− do {
237− fill -= incr;
238− next[(huff >> drop) + fill] = this;
239− } while (fill != 0);
240−
241− /* backwards increment the len-bit code huff */
242− incr = 1U << (len - 1);
243− while (huff & incr)
244− incr >>= 1;
245− if (incr != 0) {
246− huff &= incr - 1;
247− huff += incr;
248− }
249− else
250− huff = 0;
251−
252− /* go to next symbol, update count, len */
253− sym++;
254− if (--(count[len]) == 0) {
255− if (len == max) break;
256− len = lens[work[sym]];
257− }
258−
259− /* create new sub-table if needed */
260− if (len > root && (huff & mask) != low) {
261− /* if first time, transition to sub-tables */
262− if (drop == 0)
263− drop = root;
264−
265− /* increment past last table */
266− next += min; /* here min is 1 << curr */
267−
268− /* determine length of next table */
269− curr = len - drop;
270− left = (int)(1 << curr);
271− while (curr + drop < max) {
272− left -= count[curr + drop];
273− if (left <= 0) break;
274− curr++;
275− left <<= 1;
276− }
277−
278− /* check for enough space */
279− used += 1U << curr;
280− if (type == LENS && used >= ENOUGH - MAXD)
281− return 1;
282−
283− /* point entry in root table to sub-table */
284− low = huff & mask;
285− (*table)[low].op = (unsigned char)curr;
286− (*table)[low].bits = (unsigned char)root;
287− (*table)[low].val = (unsigned short)(next - *table);
288− }
289− }
290−
291− /*
292− Fill in rest of table for incomplete codes. This loop is similar to the
293− loop above in incrementing huff for table indices. It is assumed that
294− len is equal to curr + drop, so there is no loop needed to increment
295− through high index bits. When the current sub-table is filled, the loop
296− drops back to the root table to fill in any remaining entries there.
297− */
298− this.op = (unsigned char)64; /* invalid code marker */
299− this.bits = (unsigned char)(len - drop);
300− this.val = (unsigned short)0;
301− while (huff != 0) {
302− /* when done with sub-table, drop back to root table */
303− if (drop != 0 && (huff & mask) != low) {
304− drop = 0;
305− len = root;
306− next = *table;
307− this.bits = (unsigned char)len;
308− }
309−
310− /* put invalid code marker in table */
311− next[huff >> drop] = this;
312−
313− /* backwards increment the len-bit code huff */
314− incr = 1U << (len - 1);
315− while (huff & incr)
316− incr >>= 1;
317− if (incr != 0) {
318− huff &= incr - 1;
319− huff += incr;
320− }
321− else
322− huff = 0;
323− }
324−
325− /* set return parameters */
326− *table += used;
327− *bits = root;
328− return 0;
329−}
cbits/inftrees.hdeleted−55
@@ -1,55 +0,0 @@
1−/* inftrees.h -- header to use inftrees.c
2− * Copyright (C) 1995-2005 Mark Adler
3− * For conditions of distribution and use, see copyright notice in zlib.h
4− */
5−
6−/* WARNING: this file should *not* be used by applications. It is
7− part of the implementation of the compression library and is
8− subject to change. Applications should only use zlib.h.
9− */
10−
11−/* Structure for decoding tables. Each entry provides either the
12− information needed to do the operation requested by the code that
13− indexed that table entry, or it provides a pointer to another
14− table that indexes more bits of the code. op indicates whether
15− the entry is a pointer to another table, a literal, a length or
16− distance, an end-of-block, or an invalid code. For a table
17− pointer, the low four bits of op is the number of index bits of
18− that table. For a length or distance, the low four bits of op
19− is the number of extra bits to get after the code. bits is
20− the number of bits in this code or part of the code to drop off
21− of the bit buffer. val is the actual byte to output in the case
22− of a literal, the base length or distance, or the offset from
23− the current table to the next table. Each entry is four bytes. */
24−typedef struct {
25− unsigned char op; /* operation, extra bits, table bits */
26− unsigned char bits; /* bits in this part of the code */
27− unsigned short val; /* offset in table or code value */
28−} code;
29−
30−/* op values as set by inflate_table():
31− 00000000 - literal
32− 0000tttt - table link, tttt != 0 is the number of table index bits
33− 0001eeee - length or distance, eeee is the number of extra bits
34− 01100000 - end of block
35− 01000000 - invalid code
36− */
37−
38−/* Maximum size of dynamic tree. The maximum found in a long but non-
39− exhaustive search was 1444 code structures (852 for length/literals
40− and 592 for distances, the latter actually the result of an
41− exhaustive search). The true maximum is not known, but the value
42− below is more than safe. */
43−#define ENOUGH 2048
44−#define MAXD 592
45−
46−/* Type of code to build for inftable() */
47−typedef enum {
48− CODES,
49− LENS,
50− DISTS
51−} codetype;
52−
53−extern int inflate_table OF((codetype type, unsigned short FAR *lens,
54− unsigned codes, code FAR * FAR *table,
55− unsigned FAR *bits, unsigned short FAR *work));
cbits/trees.cdeleted−1219
@@ -1,1219 +0,0 @@
1−/* trees.c -- output deflated data using Huffman coding
2− * Copyright (C) 1995-2005 Jean-loup Gailly
3− * For conditions of distribution and use, see copyright notice in zlib.h
4− */
5−
6−/*
7− * ALGORITHM
8− *
9− * The "deflation" process uses several Huffman trees. The more
10− * common source values are represented by shorter bit sequences.
11− *
12− * Each code tree is stored in a compressed form which is itself
13− * a Huffman encoding of the lengths of all the code strings (in
14− * ascending order by source values). The actual code strings are
15− * reconstructed from the lengths in the inflate process, as described
16− * in the deflate specification.
17− *
18− * REFERENCES
19− *
20− * Deutsch, L.P.,"'Deflate' Compressed Data Format Specification".
21− * Available in ftp.uu.net:/pub/archiving/zip/doc/deflate-1.1.doc
22− *
23− * Storer, James A.
24− * Data Compression: Methods and Theory, pp. 49-50.
25− * Computer Science Press, 1988. ISBN 0-7167-8156-5.
26− *
27− * Sedgewick, R.
28− * Algorithms, p290.
29− * Addison-Wesley, 1983. ISBN 0-201-06672-6.
30− */
31−
32−/* @(#) $Id$ */
33−
34−/* #define GEN_TREES_H */
35−
36−#include "deflate.h"
37−
38−#ifdef DEBUG
39−# include <ctype.h>
40−#endif
41−
42−/* ===========================================================================
43− * Constants
44− */
45−
46−#define MAX_BL_BITS 7
47−/* Bit length codes must not exceed MAX_BL_BITS bits */
48−
49−#define END_BLOCK 256
50−/* end of block literal code */
51−
52−#define REP_3_6 16
53−/* repeat previous bit length 3-6 times (2 bits of repeat count) */
54−
55−#define REPZ_3_10 17
56−/* repeat a zero length 3-10 times (3 bits of repeat count) */
57−
58−#define REPZ_11_138 18
59−/* repeat a zero length 11-138 times (7 bits of repeat count) */
60−
61−local const int extra_lbits[LENGTH_CODES] /* extra bits for each length code */
62− = {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};
63−
64−local const int extra_dbits[D_CODES] /* extra bits for each distance code */
65− = {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};
66−
67−local const int extra_blbits[BL_CODES]/* extra bits for each bit length code */
68− = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,3,7};
69−
70−local const uch bl_order[BL_CODES]
71− = {16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15};
72−/* The lengths of the bit length codes are sent in order of decreasing
73− * probability, to avoid transmitting the lengths for unused bit length codes.
74− */
75−
76−#define Buf_size (8 * 2*sizeof(char))
77−/* Number of bits used within bi_buf. (bi_buf might be implemented on
78− * more than 16 bits on some systems.)
79− */
80−
81−/* ===========================================================================
82− * Local data. These are initialized only once.
83− */
84−
85−#define DIST_CODE_LEN 512 /* see definition of array dist_code below */
86−
87−#if defined(GEN_TREES_H) || !defined(STDC)
88−/* non ANSI compilers may not accept trees.h */
89−
90−local ct_data static_ltree[L_CODES+2];
91−/* The static literal tree. Since the bit lengths are imposed, there is no
92− * need for the L_CODES extra codes used during heap construction. However
93− * The codes 286 and 287 are needed to build a canonical tree (see _tr_init
94− * below).
95− */
96−
97−local ct_data static_dtree[D_CODES];
98−/* The static distance tree. (Actually a trivial tree since all codes use
99− * 5 bits.)
100− */
101−
102−uch _dist_code[DIST_CODE_LEN];
103−/* Distance codes. The first 256 values correspond to the distances
104− * 3 .. 258, the last 256 values correspond to the top 8 bits of
105− * the 15 bit distances.
106− */
107−
108−uch _length_code[MAX_MATCH-MIN_MATCH+1];
109−/* length code for each normalized match length (0 == MIN_MATCH) */
110−
111−local int base_length[LENGTH_CODES];
112−/* First normalized length for each code (0 = MIN_MATCH) */
113−
114−local int base_dist[D_CODES];
115−/* First normalized distance for each code (0 = distance of 1) */
116−
117−#else
118−# include "trees.h"
119−#endif /* GEN_TREES_H */
120−
121−struct static_tree_desc_s {
122− const ct_data *static_tree; /* static tree or NULL */
123− const intf *extra_bits; /* extra bits for each code or NULL */
124− int extra_base; /* base index for extra_bits */
125− int elems; /* max number of elements in the tree */
126− int max_length; /* max bit length for the codes */
127−};
128−
129−local static_tree_desc static_l_desc =
130−{static_ltree, extra_lbits, LITERALS+1, L_CODES, MAX_BITS};
131−
132−local static_tree_desc static_d_desc =
133−{static_dtree, extra_dbits, 0, D_CODES, MAX_BITS};
134−
135−local static_tree_desc static_bl_desc =
136−{(const ct_data *)0, extra_blbits, 0, BL_CODES, MAX_BL_BITS};
137−
138−/* ===========================================================================
139− * Local (static) routines in this file.
140− */
141−
142−local void tr_static_init OF((void));
143−local void init_block OF((deflate_state *s));
144−local void pqdownheap OF((deflate_state *s, ct_data *tree, int k));
145−local void gen_bitlen OF((deflate_state *s, tree_desc *desc));
146−local void gen_codes OF((ct_data *tree, int max_code, ushf *bl_count));
147−local void build_tree OF((deflate_state *s, tree_desc *desc));
148−local void scan_tree OF((deflate_state *s, ct_data *tree, int max_code));
149−local void send_tree OF((deflate_state *s, ct_data *tree, int max_code));
150−local int build_bl_tree OF((deflate_state *s));
151−local void send_all_trees OF((deflate_state *s, int lcodes, int dcodes,
152− int blcodes));
153−local void compress_block OF((deflate_state *s, ct_data *ltree,
154− ct_data *dtree));
155−local void set_data_type OF((deflate_state *s));
156−local unsigned bi_reverse OF((unsigned value, int length));
157−local void bi_windup OF((deflate_state *s));
158−local void bi_flush OF((deflate_state *s));
159−local void copy_block OF((deflate_state *s, charf *buf, unsigned len,
160− int header));
161−
162−#ifdef GEN_TREES_H
163−local void gen_trees_header OF((void));
164−#endif
165−
166−#ifndef DEBUG
167−# define send_code(s, c, tree) send_bits(s, tree[c].Code, tree[c].Len)
168− /* Send a code of the given tree. c and tree must not have side effects */
169−
170−#else /* DEBUG */
171−# define send_code(s, c, tree) \
172− { if (z_verbose>2) fprintf(stderr,"\ncd %3d ",(c)); \
173− send_bits(s, tree[c].Code, tree[c].Len); }
174−#endif
175−
176−/* ===========================================================================
177− * Output a short LSB first on the stream.
178− * IN assertion: there is enough room in pendingBuf.
179− */
180−#define put_short(s, w) { \
181− put_byte(s, (uch)((w) & 0xff)); \
182− put_byte(s, (uch)((ush)(w) >> 8)); \
183−}
184−
185−/* ===========================================================================
186− * Send a value on a given number of bits.
187− * IN assertion: length <= 16 and value fits in length bits.
188− */
189−#ifdef DEBUG
190−local void send_bits OF((deflate_state *s, int value, int length));
191−
192−local void send_bits(s, value, length)
193− deflate_state *s;
194− int value; /* value to send */
195− int length; /* number of bits */
196−{
197− Tracevv((stderr," l %2d v %4x ", length, value));
198− Assert(length > 0 && length <= 15, "invalid length");
199− s->bits_sent += (ulg)length;
200−
201− /* If not enough room in bi_buf, use (valid) bits from bi_buf and
202− * (16 - bi_valid) bits from value, leaving (width - (16-bi_valid))
203− * unused bits in value.
204− */
205− if (s->bi_valid > (int)Buf_size - length) {
206− s->bi_buf |= (value << s->bi_valid);
207− put_short(s, s->bi_buf);
208− s->bi_buf = (ush)value >> (Buf_size - s->bi_valid);
209− s->bi_valid += length - Buf_size;
210− } else {
211− s->bi_buf |= value << s->bi_valid;
212− s->bi_valid += length;
213− }
214−}
215−#else /* !DEBUG */
216−
217−#define send_bits(s, value, length) \
218−{ int len = length;\
219− if (s->bi_valid > (int)Buf_size - len) {\
220− int val = value;\
221− s->bi_buf |= (val << s->bi_valid);\
222− put_short(s, s->bi_buf);\
223− s->bi_buf = (ush)val >> (Buf_size - s->bi_valid);\
224− s->bi_valid += len - Buf_size;\
225− } else {\
226− s->bi_buf |= (value) << s->bi_valid;\
227− s->bi_valid += len;\
228− }\
229−}
230−#endif /* DEBUG */
231−
232−
233−/* the arguments must not have side effects */
234−
235−/* ===========================================================================
236− * Initialize the various 'constant' tables.
237− */
238−local void tr_static_init()
239−{
240−#if defined(GEN_TREES_H) || !defined(STDC)
241− static int static_init_done = 0;
242− int n; /* iterates over tree elements */
243− int bits; /* bit counter */
244− int length; /* length value */
245− int code; /* code value */
246− int dist; /* distance index */
247− ush bl_count[MAX_BITS+1];
248− /* number of codes at each bit length for an optimal tree */
249−
250− if (static_init_done) return;
251−
252− /* For some embedded targets, global variables are not initialized: */
253− static_l_desc.static_tree = static_ltree;
254− static_l_desc.extra_bits = extra_lbits;
255− static_d_desc.static_tree = static_dtree;
256− static_d_desc.extra_bits = extra_dbits;
257− static_bl_desc.extra_bits = extra_blbits;
258−
259− /* Initialize the mapping length (0..255) -> length code (0..28) */
260− length = 0;
261− for (code = 0; code < LENGTH_CODES-1; code++) {
262− base_length[code] = length;
263− for (n = 0; n < (1<<extra_lbits[code]); n++) {
264− _length_code[length++] = (uch)code;
265− }
266− }
267− Assert (length == 256, "tr_static_init: length != 256");
268− /* Note that the length 255 (match length 258) can be represented
269− * in two different ways: code 284 + 5 bits or code 285, so we
270− * overwrite length_code[255] to use the best encoding:
271− */
272− _length_code[length-1] = (uch)code;
273−
274− /* Initialize the mapping dist (0..32K) -> dist code (0..29) */
275− dist = 0;
276− for (code = 0 ; code < 16; code++) {
277− base_dist[code] = dist;
278− for (n = 0; n < (1<<extra_dbits[code]); n++) {
279− _dist_code[dist++] = (uch)code;
280− }
281− }
282− Assert (dist == 256, "tr_static_init: dist != 256");
283− dist >>= 7; /* from now on, all distances are divided by 128 */
284− for ( ; code < D_CODES; code++) {
285− base_dist[code] = dist << 7;
286− for (n = 0; n < (1<<(extra_dbits[code]-7)); n++) {
287− _dist_code[256 + dist++] = (uch)code;
288− }
289− }
290− Assert (dist == 256, "tr_static_init: 256+dist != 512");
291−
292− /* Construct the codes of the static literal tree */
293− for (bits = 0; bits <= MAX_BITS; bits++) bl_count[bits] = 0;
294− n = 0;
295− while (n <= 143) static_ltree[n++].Len = 8, bl_count[8]++;
296− while (n <= 255) static_ltree[n++].Len = 9, bl_count[9]++;
297− while (n <= 279) static_ltree[n++].Len = 7, bl_count[7]++;
298− while (n <= 287) static_ltree[n++].Len = 8, bl_count[8]++;
299− /* Codes 286 and 287 do not exist, but we must include them in the
300− * tree construction to get a canonical Huffman tree (longest code
301− * all ones)
302− */
303− gen_codes((ct_data *)static_ltree, L_CODES+1, bl_count);
304−
305− /* The static distance tree is trivial: */
306− for (n = 0; n < D_CODES; n++) {
307− static_dtree[n].Len = 5;
308− static_dtree[n].Code = bi_reverse((unsigned)n, 5);
309− }
310− static_init_done = 1;
311−
312−# ifdef GEN_TREES_H
313− gen_trees_header();
314−# endif
315−#endif /* defined(GEN_TREES_H) || !defined(STDC) */
316−}
317−
318−/* ===========================================================================
319− * Genererate the file trees.h describing the static trees.
320− */
321−#ifdef GEN_TREES_H
322−# ifndef DEBUG
323−# include <stdio.h>
324−# endif
325−
326−# define SEPARATOR(i, last, width) \
327− ((i) == (last)? "\n};\n\n" : \
328− ((i) % (width) == (width)-1 ? ",\n" : ", "))
329−
330−void gen_trees_header()
331−{
332− FILE *header = fopen("trees.h", "w");
333− int i;
334−
335− Assert (header != NULL, "Can't open trees.h");
336− fprintf(header,
337− "/* header created automatically with -DGEN_TREES_H */\n\n");
338−
339− fprintf(header, "local const ct_data static_ltree[L_CODES+2] = {\n");
340− for (i = 0; i < L_CODES+2; i++) {
341− fprintf(header, "{{%3u},{%3u}}%s", static_ltree[i].Code,
342− static_ltree[i].Len, SEPARATOR(i, L_CODES+1, 5));
343− }
344−
345− fprintf(header, "local const ct_data static_dtree[D_CODES] = {\n");
346− for (i = 0; i < D_CODES; i++) {
347− fprintf(header, "{{%2u},{%2u}}%s", static_dtree[i].Code,
348− static_dtree[i].Len, SEPARATOR(i, D_CODES-1, 5));
349− }
350−
351− fprintf(header, "const uch _dist_code[DIST_CODE_LEN] = {\n");
352− for (i = 0; i < DIST_CODE_LEN; i++) {
353− fprintf(header, "%2u%s", _dist_code[i],
354− SEPARATOR(i, DIST_CODE_LEN-1, 20));
355− }
356−
357− fprintf(header, "const uch _length_code[MAX_MATCH-MIN_MATCH+1]= {\n");
358− for (i = 0; i < MAX_MATCH-MIN_MATCH+1; i++) {
359− fprintf(header, "%2u%s", _length_code[i],
360− SEPARATOR(i, MAX_MATCH-MIN_MATCH, 20));
361− }
362−
363− fprintf(header, "local const int base_length[LENGTH_CODES] = {\n");
364− for (i = 0; i < LENGTH_CODES; i++) {
365− fprintf(header, "%1u%s", base_length[i],
366− SEPARATOR(i, LENGTH_CODES-1, 20));
367− }
368−
369− fprintf(header, "local const int base_dist[D_CODES] = {\n");
370− for (i = 0; i < D_CODES; i++) {
371− fprintf(header, "%5u%s", base_dist[i],
372− SEPARATOR(i, D_CODES-1, 10));
373− }
374−
375− fclose(header);
376−}
377−#endif /* GEN_TREES_H */
378−
379−/* ===========================================================================
380− * Initialize the tree data structures for a new zlib stream.
381− */
382−void _tr_init(s)
383− deflate_state *s;
384−{
385− tr_static_init();
386−
387− s->l_desc.dyn_tree = s->dyn_ltree;
388− s->l_desc.stat_desc = &static_l_desc;
389−
390− s->d_desc.dyn_tree = s->dyn_dtree;
391− s->d_desc.stat_desc = &static_d_desc;
392−
393− s->bl_desc.dyn_tree = s->bl_tree;
394− s->bl_desc.stat_desc = &static_bl_desc;
395−
396− s->bi_buf = 0;
397− s->bi_valid = 0;
398− s->last_eob_len = 8; /* enough lookahead for inflate */
399−#ifdef DEBUG
400− s->compressed_len = 0L;
401− s->bits_sent = 0L;
402−#endif
403−
404− /* Initialize the first block of the first file: */
405− init_block(s);
406−}
407−
408−/* ===========================================================================
409− * Initialize a new block.
410− */
411−local void init_block(s)
412− deflate_state *s;
413−{
414− int n; /* iterates over tree elements */
415−
416− /* Initialize the trees. */
417− for (n = 0; n < L_CODES; n++) s->dyn_ltree[n].Freq = 0;
418− for (n = 0; n < D_CODES; n++) s->dyn_dtree[n].Freq = 0;
419− for (n = 0; n < BL_CODES; n++) s->bl_tree[n].Freq = 0;
420−
421− s->dyn_ltree[END_BLOCK].Freq = 1;
422− s->opt_len = s->static_len = 0L;
423− s->last_lit = s->matches = 0;
424−}
425−
426−#define SMALLEST 1
427−/* Index within the heap array of least frequent node in the Huffman tree */
428−
429−
430−/* ===========================================================================
431− * Remove the smallest element from the heap and recreate the heap with
432− * one less element. Updates heap and heap_len.
433− */
434−#define pqremove(s, tree, top) \
435−{\
436− top = s->heap[SMALLEST]; \
437− s->heap[SMALLEST] = s->heap[s->heap_len--]; \
438− pqdownheap(s, tree, SMALLEST); \
439−}
440−
441−/* ===========================================================================
442− * Compares to subtrees, using the tree depth as tie breaker when
443− * the subtrees have equal frequency. This minimizes the worst case length.
444− */
445−#define smaller(tree, n, m, depth) \
446− (tree[n].Freq < tree[m].Freq || \
447− (tree[n].Freq == tree[m].Freq && depth[n] <= depth[m]))
448−
449−/* ===========================================================================
450− * Restore the heap property by moving down the tree starting at node k,
451− * exchanging a node with the smallest of its two sons if necessary, stopping
452− * when the heap property is re-established (each father smaller than its
453− * two sons).
454− */
455−local void pqdownheap(s, tree, k)
456− deflate_state *s;
457− ct_data *tree; /* the tree to restore */
458− int k; /* node to move down */
459−{
460− int v = s->heap[k];
461− int j = k << 1; /* left son of k */
462− while (j <= s->heap_len) {
463− /* Set j to the smallest of the two sons: */
464− if (j < s->heap_len &&
465− smaller(tree, s->heap[j+1], s->heap[j], s->depth)) {
466− j++;
467− }
468− /* Exit if v is smaller than both sons */
469− if (smaller(tree, v, s->heap[j], s->depth)) break;
470−
471− /* Exchange v with the smallest son */
472− s->heap[k] = s->heap[j]; k = j;
473−
474− /* And continue down the tree, setting j to the left son of k */
475− j <<= 1;
476− }
477− s->heap[k] = v;
478−}
479−
480−/* ===========================================================================
481− * Compute the optimal bit lengths for a tree and update the total bit length
482− * for the current block.
483− * IN assertion: the fields freq and dad are set, heap[heap_max] and
484− * above are the tree nodes sorted by increasing frequency.
485− * OUT assertions: the field len is set to the optimal bit length, the
486− * array bl_count contains the frequencies for each bit length.
487− * The length opt_len is updated; static_len is also updated if stree is
488− * not null.
489− */
490−local void gen_bitlen(s, desc)
491− deflate_state *s;
492− tree_desc *desc; /* the tree descriptor */
493−{
494− ct_data *tree = desc->dyn_tree;
495− int max_code = desc->max_code;
496− const ct_data *stree = desc->stat_desc->static_tree;
497− const intf *extra = desc->stat_desc->extra_bits;
498− int base = desc->stat_desc->extra_base;
499− int max_length = desc->stat_desc->max_length;
500− int h; /* heap index */
501− int n, m; /* iterate over the tree elements */
502− int bits; /* bit length */
503− int xbits; /* extra bits */
504− ush f; /* frequency */
505− int overflow = 0; /* number of elements with bit length too large */
506−
507− for (bits = 0; bits <= MAX_BITS; bits++) s->bl_count[bits] = 0;
508−
509− /* In a first pass, compute the optimal bit lengths (which may
510− * overflow in the case of the bit length tree).
511− */
512− tree[s->heap[s->heap_max]].Len = 0; /* root of the heap */
513−
514− for (h = s->heap_max+1; h < HEAP_SIZE; h++) {
515− n = s->heap[h];
516− bits = tree[tree[n].Dad].Len + 1;
517− if (bits > max_length) bits = max_length, overflow++;
518− tree[n].Len = (ush)bits;
519− /* We overwrite tree[n].Dad which is no longer needed */
520−
521− if (n > max_code) continue; /* not a leaf node */
522−
523− s->bl_count[bits]++;
524− xbits = 0;
525− if (n >= base) xbits = extra[n-base];
526− f = tree[n].Freq;
527− s->opt_len += (ulg)f * (bits + xbits);
528− if (stree) s->static_len += (ulg)f * (stree[n].Len + xbits);
529− }
530− if (overflow == 0) return;
531−
532− Trace((stderr,"\nbit length overflow\n"));
533− /* This happens for example on obj2 and pic of the Calgary corpus */
534−
535− /* Find the first bit length which could increase: */
536− do {
537− bits = max_length-1;
538− while (s->bl_count[bits] == 0) bits--;
539− s->bl_count[bits]--; /* move one leaf down the tree */
540− s->bl_count[bits+1] += 2; /* move one overflow item as its brother */
541− s->bl_count[max_length]--;
542− /* The brother of the overflow item also moves one step up,
543− * but this does not affect bl_count[max_length]
544− */
545− overflow -= 2;
546− } while (overflow > 0);
547−
548− /* Now recompute all bit lengths, scanning in increasing frequency.
549− * h is still equal to HEAP_SIZE. (It is simpler to reconstruct all
550− * lengths instead of fixing only the wrong ones. This idea is taken
551− * from 'ar' written by Haruhiko Okumura.)
552− */
553− for (bits = max_length; bits != 0; bits--) {
554− n = s->bl_count[bits];
555− while (n != 0) {
556− m = s->heap[--h];
557− if (m > max_code) continue;
558− if ((unsigned) tree[m].Len != (unsigned) bits) {
559− Trace((stderr,"code %d bits %d->%d\n", m, tree[m].Len, bits));
560− s->opt_len += ((long)bits - (long)tree[m].Len)
561− *(long)tree[m].Freq;
562− tree[m].Len = (ush)bits;
563− }
564− n--;
565− }
566− }
567−}
568−
569−/* ===========================================================================
570− * Generate the codes for a given tree and bit counts (which need not be
571− * optimal).
572− * IN assertion: the array bl_count contains the bit length statistics for
573− * the given tree and the field len is set for all tree elements.
574− * OUT assertion: the field code is set for all tree elements of non
575− * zero code length.
576− */
577−local void gen_codes (tree, max_code, bl_count)
578− ct_data *tree; /* the tree to decorate */
579− int max_code; /* largest code with non zero frequency */
580− ushf *bl_count; /* number of codes at each bit length */
581−{
582− ush next_code[MAX_BITS+1]; /* next code value for each bit length */
583− ush code = 0; /* running code value */
584− int bits; /* bit index */
585− int n; /* code index */
586−
587− /* The distribution counts are first used to generate the code values
588− * without bit reversal.
589− */
590− for (bits = 1; bits <= MAX_BITS; bits++) {
591− next_code[bits] = code = (code + bl_count[bits-1]) << 1;
592− }
593− /* Check that the bit counts in bl_count are consistent. The last code
594− * must be all ones.
595− */
596− Assert (code + bl_count[MAX_BITS]-1 == (1<<MAX_BITS)-1,
597− "inconsistent bit counts");
598− Tracev((stderr,"\ngen_codes: max_code %d ", max_code));
599−
600− for (n = 0; n <= max_code; n++) {
601− int len = tree[n].Len;
602− if (len == 0) continue;
603− /* Now reverse the bits */
604− tree[n].Code = bi_reverse(next_code[len]++, len);
605−
606− Tracecv(tree != static_ltree, (stderr,"\nn %3d %c l %2d c %4x (%x) ",
607− n, (isgraph(n) ? n : ' '), len, tree[n].Code, next_code[len]-1));
608− }
609−}
610−
611−/* ===========================================================================
612− * Construct one Huffman tree and assigns the code bit strings and lengths.
613− * Update the total bit length for the current block.
614− * IN assertion: the field freq is set for all tree elements.
615− * OUT assertions: the fields len and code are set to the optimal bit length
616− * and corresponding code. The length opt_len is updated; static_len is
617− * also updated if stree is not null. The field max_code is set.
618− */
619−local void build_tree(s, desc)
620− deflate_state *s;
621− tree_desc *desc; /* the tree descriptor */
622−{
623− ct_data *tree = desc->dyn_tree;
624− const ct_data *stree = desc->stat_desc->static_tree;
625− int elems = desc->stat_desc->elems;
626− int n, m; /* iterate over heap elements */
627− int max_code = -1; /* largest code with non zero frequency */
628− int node; /* new node being created */
629−
630− /* Construct the initial heap, with least frequent element in
631− * heap[SMALLEST]. The sons of heap[n] are heap[2*n] and heap[2*n+1].
632− * heap[0] is not used.
633− */
634− s->heap_len = 0, s->heap_max = HEAP_SIZE;
635−
636− for (n = 0; n < elems; n++) {
637− if (tree[n].Freq != 0) {
638− s->heap[++(s->heap_len)] = max_code = n;
639− s->depth[n] = 0;
640− } else {
641− tree[n].Len = 0;
642− }
643− }
644−
645− /* The pkzip format requires that at least one distance code exists,
646− * and that at least one bit should be sent even if there is only one
647− * possible code. So to avoid special checks later on we force at least
648− * two codes of non zero frequency.
649− */
650− while (s->heap_len < 2) {
651− node = s->heap[++(s->heap_len)] = (max_code < 2 ? ++max_code : 0);
652− tree[node].Freq = 1;
653− s->depth[node] = 0;
654− s->opt_len--; if (stree) s->static_len -= stree[node].Len;
655− /* node is 0 or 1 so it does not have extra bits */
656− }
657− desc->max_code = max_code;
658−
659− /* The elements heap[heap_len/2+1 .. heap_len] are leaves of the tree,
660− * establish sub-heaps of increasing lengths:
661− */
662− for (n = s->heap_len/2; n >= 1; n--) pqdownheap(s, tree, n);
663−
664− /* Construct the Huffman tree by repeatedly combining the least two
665− * frequent nodes.
666− */
667− node = elems; /* next internal node of the tree */
668− do {
669− pqremove(s, tree, n); /* n = node of least frequency */
670− m = s->heap[SMALLEST]; /* m = node of next least frequency */
671−
672− s->heap[--(s->heap_max)] = n; /* keep the nodes sorted by frequency */
673− s->heap[--(s->heap_max)] = m;
674−
675− /* Create a new node father of n and m */
676− tree[node].Freq = tree[n].Freq + tree[m].Freq;
677− s->depth[node] = (uch)((s->depth[n] >= s->depth[m] ?
678− s->depth[n] : s->depth[m]) + 1);
679− tree[n].Dad = tree[m].Dad = (ush)node;
680−#ifdef DUMP_BL_TREE
681− if (tree == s->bl_tree) {
682− fprintf(stderr,"\nnode %d(%d), sons %d(%d) %d(%d)",
683− node, tree[node].Freq, n, tree[n].Freq, m, tree[m].Freq);
684− }
685−#endif
686− /* and insert the new node in the heap */
687− s->heap[SMALLEST] = node++;
688− pqdownheap(s, tree, SMALLEST);
689−
690− } while (s->heap_len >= 2);
691−
692− s->heap[--(s->heap_max)] = s->heap[SMALLEST];
693−
694− /* At this point, the fields freq and dad are set. We can now
695− * generate the bit lengths.
696− */
697− gen_bitlen(s, (tree_desc *)desc);
698−
699− /* The field len is now set, we can generate the bit codes */
700− gen_codes ((ct_data *)tree, max_code, s->bl_count);
701−}
702−
703−/* ===========================================================================
704− * Scan a literal or distance tree to determine the frequencies of the codes
705− * in the bit length tree.
706− */
707−local void scan_tree (s, tree, max_code)
708− deflate_state *s;
709− ct_data *tree; /* the tree to be scanned */
710− int max_code; /* and its largest code of non zero frequency */
711−{
712− int n; /* iterates over all tree elements */
713− int prevlen = -1; /* last emitted length */
714− int curlen; /* length of current code */
715− int nextlen = tree[0].Len; /* length of next code */
716− int count = 0; /* repeat count of the current code */
717− int max_count = 7; /* max repeat count */
718− int min_count = 4; /* min repeat count */
719−
720− if (nextlen == 0) max_count = 138, min_count = 3;
721− tree[max_code+1].Len = (ush)0xffff; /* guard */
722−
723− for (n = 0; n <= max_code; n++) {
724− curlen = nextlen; nextlen = tree[n+1].Len;
725− if (++count < max_count && curlen == nextlen) {
726− continue;
727− } else if (count < min_count) {
728− s->bl_tree[curlen].Freq += count;
729− } else if (curlen != 0) {
730− if (curlen != prevlen) s->bl_tree[curlen].Freq++;
731− s->bl_tree[REP_3_6].Freq++;
732− } else if (count <= 10) {
733− s->bl_tree[REPZ_3_10].Freq++;
734− } else {
735− s->bl_tree[REPZ_11_138].Freq++;
736− }
737− count = 0; prevlen = curlen;
738− if (nextlen == 0) {
739− max_count = 138, min_count = 3;
740− } else if (curlen == nextlen) {
741− max_count = 6, min_count = 3;
742− } else {
743− max_count = 7, min_count = 4;
744− }
745− }
746−}
747−
748−/* ===========================================================================
749− * Send a literal or distance tree in compressed form, using the codes in
750− * bl_tree.
751− */
752−local void send_tree (s, tree, max_code)
753− deflate_state *s;
754− ct_data *tree; /* the tree to be scanned */
755− int max_code; /* and its largest code of non zero frequency */
756−{
757− int n; /* iterates over all tree elements */
758− int prevlen = -1; /* last emitted length */
759− int curlen; /* length of current code */
760− int nextlen = tree[0].Len; /* length of next code */
761− int count = 0; /* repeat count of the current code */
762− int max_count = 7; /* max repeat count */
763− int min_count = 4; /* min repeat count */
764−
765− /* tree[max_code+1].Len = -1; */ /* guard already set */
766− if (nextlen == 0) max_count = 138, min_count = 3;
767−
768− for (n = 0; n <= max_code; n++) {
769− curlen = nextlen; nextlen = tree[n+1].Len;
770− if (++count < max_count && curlen == nextlen) {
771− continue;
772− } else if (count < min_count) {
773− do { send_code(s, curlen, s->bl_tree); } while (--count != 0);
774−
775− } else if (curlen != 0) {
776− if (curlen != prevlen) {
777− send_code(s, curlen, s->bl_tree); count--;
778− }
779− Assert(count >= 3 && count <= 6, " 3_6?");
780− send_code(s, REP_3_6, s->bl_tree); send_bits(s, count-3, 2);
781−
782− } else if (count <= 10) {
783− send_code(s, REPZ_3_10, s->bl_tree); send_bits(s, count-3, 3);
784−
785− } else {
786− send_code(s, REPZ_11_138, s->bl_tree); send_bits(s, count-11, 7);
787− }
788− count = 0; prevlen = curlen;
789− if (nextlen == 0) {
790− max_count = 138, min_count = 3;
791− } else if (curlen == nextlen) {
792− max_count = 6, min_count = 3;
793− } else {
794− max_count = 7, min_count = 4;
795− }
796− }
797−}
798−
799−/* ===========================================================================
800− * Construct the Huffman tree for the bit lengths and return the index in
801− * bl_order of the last bit length code to send.
802− */
803−local int build_bl_tree(s)
804− deflate_state *s;
805−{
806− int max_blindex; /* index of last bit length code of non zero freq */
807−
808− /* Determine the bit length frequencies for literal and distance trees */
809− scan_tree(s, (ct_data *)s->dyn_ltree, s->l_desc.max_code);
810− scan_tree(s, (ct_data *)s->dyn_dtree, s->d_desc.max_code);
811−
812− /* Build the bit length tree: */
813− build_tree(s, (tree_desc *)(&(s->bl_desc)));
814− /* opt_len now includes the length of the tree representations, except
815− * the lengths of the bit lengths codes and the 5+5+4 bits for the counts.
816− */
817−
818− /* Determine the number of bit length codes to send. The pkzip format
819− * requires that at least 4 bit length codes be sent. (appnote.txt says
820− * 3 but the actual value used is 4.)
821− */
822− for (max_blindex = BL_CODES-1; max_blindex >= 3; max_blindex--) {
823− if (s->bl_tree[bl_order[max_blindex]].Len != 0) break;
824− }
825− /* Update opt_len to include the bit length tree and counts */
826− s->opt_len += 3*(max_blindex+1) + 5+5+4;
827− Tracev((stderr, "\ndyn trees: dyn %ld, stat %ld",
828− s->opt_len, s->static_len));
829−
830− return max_blindex;
831−}
832−
833−/* ===========================================================================
834− * Send the header for a block using dynamic Huffman trees: the counts, the
835− * lengths of the bit length codes, the literal tree and the distance tree.
836− * IN assertion: lcodes >= 257, dcodes >= 1, blcodes >= 4.
837− */
838−local void send_all_trees(s, lcodes, dcodes, blcodes)
839− deflate_state *s;
840− int lcodes, dcodes, blcodes; /* number of codes for each tree */
841−{
842− int rank; /* index in bl_order */
843−
844− Assert (lcodes >= 257 && dcodes >= 1 && blcodes >= 4, "not enough codes");
845− Assert (lcodes <= L_CODES && dcodes <= D_CODES && blcodes <= BL_CODES,
846− "too many codes");
847− Tracev((stderr, "\nbl counts: "));
848− send_bits(s, lcodes-257, 5); /* not +255 as stated in appnote.txt */
849− send_bits(s, dcodes-1, 5);
850− send_bits(s, blcodes-4, 4); /* not -3 as stated in appnote.txt */
851− for (rank = 0; rank < blcodes; rank++) {
852− Tracev((stderr, "\nbl code %2d ", bl_order[rank]));
853− send_bits(s, s->bl_tree[bl_order[rank]].Len, 3);
854− }
855− Tracev((stderr, "\nbl tree: sent %ld", s->bits_sent));
856−
857− send_tree(s, (ct_data *)s->dyn_ltree, lcodes-1); /* literal tree */
858− Tracev((stderr, "\nlit tree: sent %ld", s->bits_sent));
859−
860− send_tree(s, (ct_data *)s->dyn_dtree, dcodes-1); /* distance tree */
861− Tracev((stderr, "\ndist tree: sent %ld", s->bits_sent));
862−}
863−
864−/* ===========================================================================
865− * Send a stored block
866− */
867−void _tr_stored_block(s, buf, stored_len, eof)
868− deflate_state *s;
869− charf *buf; /* input block */
870− ulg stored_len; /* length of input block */
871− int eof; /* true if this is the last block for a file */
872−{
873− send_bits(s, (STORED_BLOCK<<1)+eof, 3); /* send block type */
874−#ifdef DEBUG
875− s->compressed_len = (s->compressed_len + 3 + 7) & (ulg)~7L;
876− s->compressed_len += (stored_len + 4) << 3;
877−#endif
878− copy_block(s, buf, (unsigned)stored_len, 1); /* with header */
879−}
880−
881−/* ===========================================================================
882− * Send one empty static block to give enough lookahead for inflate.
883− * This takes 10 bits, of which 7 may remain in the bit buffer.
884− * The current inflate code requires 9 bits of lookahead. If the
885− * last two codes for the previous block (real code plus EOB) were coded
886− * on 5 bits or less, inflate may have only 5+3 bits of lookahead to decode
887− * the last real code. In this case we send two empty static blocks instead
888− * of one. (There are no problems if the previous block is stored or fixed.)
889− * To simplify the code, we assume the worst case of last real code encoded
890− * on one bit only.
891− */
892−void _tr_align(s)
893− deflate_state *s;
894−{
895− send_bits(s, STATIC_TREES<<1, 3);
896− send_code(s, END_BLOCK, static_ltree);
897−#ifdef DEBUG
898− s->compressed_len += 10L; /* 3 for block type, 7 for EOB */
899−#endif
900− bi_flush(s);
901− /* Of the 10 bits for the empty block, we have already sent
902− * (10 - bi_valid) bits. The lookahead for the last real code (before
903− * the EOB of the previous block) was thus at least one plus the length
904− * of the EOB plus what we have just sent of the empty static block.
905− */
906− if (1 + s->last_eob_len + 10 - s->bi_valid < 9) {
907− send_bits(s, STATIC_TREES<<1, 3);
908− send_code(s, END_BLOCK, static_ltree);
909−#ifdef DEBUG
910− s->compressed_len += 10L;
911−#endif
912− bi_flush(s);
913− }
914− s->last_eob_len = 7;
915−}
916−
917−/* ===========================================================================
918− * Determine the best encoding for the current block: dynamic trees, static
919− * trees or store, and output the encoded block to the zip file.
920− */
921−void _tr_flush_block(s, buf, stored_len, eof)
922− deflate_state *s;
923− charf *buf; /* input block, or NULL if too old */
924− ulg stored_len; /* length of input block */
925− int eof; /* true if this is the last block for a file */
926−{
927− ulg opt_lenb, static_lenb; /* opt_len and static_len in bytes */
928− int max_blindex = 0; /* index of last bit length code of non zero freq */
929−
930− /* Build the Huffman trees unless a stored block is forced */
931− if (s->level > 0) {
932−
933− /* Check if the file is binary or text */
934− if (stored_len > 0 && s->strm->data_type == Z_UNKNOWN)
935− set_data_type(s);
936−
937− /* Construct the literal and distance trees */
938− build_tree(s, (tree_desc *)(&(s->l_desc)));
939− Tracev((stderr, "\nlit data: dyn %ld, stat %ld", s->opt_len,
940− s->static_len));
941−
942− build_tree(s, (tree_desc *)(&(s->d_desc)));
943− Tracev((stderr, "\ndist data: dyn %ld, stat %ld", s->opt_len,
944− s->static_len));
945− /* At this point, opt_len and static_len are the total bit lengths of
946− * the compressed block data, excluding the tree representations.
947− */
948−
949− /* Build the bit length tree for the above two trees, and get the index
950− * in bl_order of the last bit length code to send.
951− */
952− max_blindex = build_bl_tree(s);
953−
954− /* Determine the best encoding. Compute the block lengths in bytes. */
955− opt_lenb = (s->opt_len+3+7)>>3;
956− static_lenb = (s->static_len+3+7)>>3;
957−
958− Tracev((stderr, "\nopt %lu(%lu) stat %lu(%lu) stored %lu lit %u ",
959− opt_lenb, s->opt_len, static_lenb, s->static_len, stored_len,
960− s->last_lit));
961−
962− if (static_lenb <= opt_lenb) opt_lenb = static_lenb;
963−
964− } else {
965− Assert(buf != (char*)0, "lost buf");
966− opt_lenb = static_lenb = stored_len + 5; /* force a stored block */
967− }
968−
969−#ifdef FORCE_STORED
970− if (buf != (char*)0) { /* force stored block */
971−#else
972− if (stored_len+4 <= opt_lenb && buf != (char*)0) {
973− /* 4: two words for the lengths */
974−#endif
975− /* The test buf != NULL is only necessary if LIT_BUFSIZE > WSIZE.
976− * Otherwise we can't have processed more than WSIZE input bytes since
977− * the last block flush, because compression would have been
978− * successful. If LIT_BUFSIZE <= WSIZE, it is never too late to
979− * transform a block into a stored block.
980− */
981− _tr_stored_block(s, buf, stored_len, eof);
982−
983−#ifdef FORCE_STATIC
984− } else if (static_lenb >= 0) { /* force static trees */
985−#else
986− } else if (s->strategy == Z_FIXED || static_lenb == opt_lenb) {
987−#endif
988− send_bits(s, (STATIC_TREES<<1)+eof, 3);
989− compress_block(s, (ct_data *)static_ltree, (ct_data *)static_dtree);
990−#ifdef DEBUG
991− s->compressed_len += 3 + s->static_len;
992−#endif
993− } else {
994− send_bits(s, (DYN_TREES<<1)+eof, 3);
995− send_all_trees(s, s->l_desc.max_code+1, s->d_desc.max_code+1,
996− max_blindex+1);
997− compress_block(s, (ct_data *)s->dyn_ltree, (ct_data *)s->dyn_dtree);
998−#ifdef DEBUG
999− s->compressed_len += 3 + s->opt_len;
1000−#endif
1001− }
1002− Assert (s->compressed_len == s->bits_sent, "bad compressed size");
1003− /* The above check is made mod 2^32, for files larger than 512 MB
1004− * and uLong implemented on 32 bits.
1005− */
1006− init_block(s);
1007−
1008− if (eof) {
1009− bi_windup(s);
1010−#ifdef DEBUG
1011− s->compressed_len += 7; /* align on byte boundary */
1012−#endif
1013− }
1014− Tracev((stderr,"\ncomprlen %lu(%lu) ", s->compressed_len>>3,
1015− s->compressed_len-7*eof));
1016−}
1017−
1018−/* ===========================================================================
1019− * Save the match info and tally the frequency counts. Return true if
1020− * the current block must be flushed.
1021− */
1022−int _tr_tally (s, dist, lc)
1023− deflate_state *s;
1024− unsigned dist; /* distance of matched string */
1025− unsigned lc; /* match length-MIN_MATCH or unmatched char (if dist==0) */
1026−{
1027− s->d_buf[s->last_lit] = (ush)dist;
1028− s->l_buf[s->last_lit++] = (uch)lc;
1029− if (dist == 0) {
1030− /* lc is the unmatched char */
1031− s->dyn_ltree[lc].Freq++;
1032− } else {
1033− s->matches++;
1034− /* Here, lc is the match length - MIN_MATCH */
1035− dist--; /* dist = match distance - 1 */
1036− Assert((ush)dist < (ush)MAX_DIST(s) &&
1037− (ush)lc <= (ush)(MAX_MATCH-MIN_MATCH) &&
1038− (ush)d_code(dist) < (ush)D_CODES, "_tr_tally: bad match");
1039−
1040− s->dyn_ltree[_length_code[lc]+LITERALS+1].Freq++;
1041− s->dyn_dtree[d_code(dist)].Freq++;
1042− }
1043−
1044−#ifdef TRUNCATE_BLOCK
1045− /* Try to guess if it is profitable to stop the current block here */
1046− if ((s->last_lit & 0x1fff) == 0 && s->level > 2) {
1047− /* Compute an upper bound for the compressed length */
1048− ulg out_length = (ulg)s->last_lit*8L;
1049− ulg in_length = (ulg)((long)s->strstart - s->block_start);
1050− int dcode;
1051− for (dcode = 0; dcode < D_CODES; dcode++) {
1052− out_length += (ulg)s->dyn_dtree[dcode].Freq *
1053− (5L+extra_dbits[dcode]);
1054− }
1055− out_length >>= 3;
1056− Tracev((stderr,"\nlast_lit %u, in %ld, out ~%ld(%ld%%) ",
1057− s->last_lit, in_length, out_length,
1058− 100L - out_length*100L/in_length));
1059− if (s->matches < s->last_lit/2 && out_length < in_length/2) return 1;
1060− }
1061−#endif
1062− return (s->last_lit == s->lit_bufsize-1);
1063− /* We avoid equality with lit_bufsize because of wraparound at 64K
1064− * on 16 bit machines and because stored blocks are restricted to
1065− * 64K-1 bytes.
1066− */
1067−}
1068−
1069−/* ===========================================================================
1070− * Send the block data compressed using the given Huffman trees
1071− */
1072−local void compress_block(s, ltree, dtree)
1073− deflate_state *s;
1074− ct_data *ltree; /* literal tree */
1075− ct_data *dtree; /* distance tree */
1076−{
1077− unsigned dist; /* distance of matched string */
1078− int lc; /* match length or unmatched char (if dist == 0) */
1079− unsigned lx = 0; /* running index in l_buf */
1080− unsigned code; /* the code to send */
1081− int extra; /* number of extra bits to send */
1082−
1083− if (s->last_lit != 0) do {
1084− dist = s->d_buf[lx];
1085− lc = s->l_buf[lx++];
1086− if (dist == 0) {
1087− send_code(s, lc, ltree); /* send a literal byte */
1088− Tracecv(isgraph(lc), (stderr," '%c' ", lc));
1089− } else {
1090− /* Here, lc is the match length - MIN_MATCH */
1091− code = _length_code[lc];
1092− send_code(s, code+LITERALS+1, ltree); /* send the length code */
1093− extra = extra_lbits[code];
1094− if (extra != 0) {
1095− lc -= base_length[code];
1096− send_bits(s, lc, extra); /* send the extra length bits */
1097− }
1098− dist--; /* dist is now the match distance - 1 */
1099− code = d_code(dist);
1100− Assert (code < D_CODES, "bad d_code");
1101−
1102− send_code(s, code, dtree); /* send the distance code */
1103− extra = extra_dbits[code];
1104− if (extra != 0) {
1105− dist -= base_dist[code];
1106− send_bits(s, dist, extra); /* send the extra distance bits */
1107− }
1108− } /* literal or match pair ? */
1109−
1110− /* Check that the overlay between pending_buf and d_buf+l_buf is ok: */
1111− Assert((uInt)(s->pending) < s->lit_bufsize + 2*lx,
1112− "pendingBuf overflow");
1113−
1114− } while (lx < s->last_lit);
1115−
1116− send_code(s, END_BLOCK, ltree);
1117− s->last_eob_len = ltree[END_BLOCK].Len;
1118−}
1119−
1120−/* ===========================================================================
1121− * Set the data type to BINARY or TEXT, using a crude approximation:
1122− * set it to Z_TEXT if all symbols are either printable characters (33 to 255)
1123− * or white spaces (9 to 13, or 32); or set it to Z_BINARY otherwise.
1124− * IN assertion: the fields Freq of dyn_ltree are set.
1125− */
1126−local void set_data_type(s)
1127− deflate_state *s;
1128−{
1129− int n;
1130−
1131− for (n = 0; n < 9; n++)
1132− if (s->dyn_ltree[n].Freq != 0)
1133− break;
1134− if (n == 9)
1135− for (n = 14; n < 32; n++)
1136− if (s->dyn_ltree[n].Freq != 0)
1137− break;
1138− s->strm->data_type = (n == 32) ? Z_TEXT : Z_BINARY;
1139−}
1140−
1141−/* ===========================================================================
1142− * Reverse the first len bits of a code, using straightforward code (a faster
1143− * method would use a table)
1144− * IN assertion: 1 <= len <= 15
1145− */
1146−local unsigned bi_reverse(code, len)
1147− unsigned code; /* the value to invert */
1148− int len; /* its bit length */
1149−{
1150− register unsigned res = 0;
1151− do {
1152− res |= code & 1;
1153− code >>= 1, res <<= 1;
1154− } while (--len > 0);
1155− return res >> 1;
1156−}
1157−
1158−/* ===========================================================================
1159− * Flush the bit buffer, keeping at most 7 bits in it.
1160− */
1161−local void bi_flush(s)
1162− deflate_state *s;
1163−{
1164− if (s->bi_valid == 16) {
1165− put_short(s, s->bi_buf);
1166− s->bi_buf = 0;
1167− s->bi_valid = 0;
1168− } else if (s->bi_valid >= 8) {
1169− put_byte(s, (Byte)s->bi_buf);
1170− s->bi_buf >>= 8;
1171− s->bi_valid -= 8;
1172− }
1173−}
1174−
1175−/* ===========================================================================
1176− * Flush the bit buffer and align the output on a byte boundary
1177− */
1178−local void bi_windup(s)
1179− deflate_state *s;
1180−{
1181− if (s->bi_valid > 8) {
1182− put_short(s, s->bi_buf);
1183− } else if (s->bi_valid > 0) {
1184− put_byte(s, (Byte)s->bi_buf);
1185− }
1186− s->bi_buf = 0;
1187− s->bi_valid = 0;
1188−#ifdef DEBUG
1189− s->bits_sent = (s->bits_sent+7) & ~7;
1190−#endif
1191−}
1192−
1193−/* ===========================================================================
1194− * Copy a stored block, storing first the length and its
1195− * one's complement if requested.
1196− */
1197−local void copy_block(s, buf, len, header)
1198− deflate_state *s;
1199− charf *buf; /* the input data */
1200− unsigned len; /* its length */
1201− int header; /* true if block header must be written */
1202−{
1203− bi_windup(s); /* align on byte boundary */
1204− s->last_eob_len = 8; /* enough lookahead for inflate */
1205−
1206− if (header) {
1207− put_short(s, (ush)len);
1208− put_short(s, (ush)~len);
1209−#ifdef DEBUG
1210− s->bits_sent += 2*16;
1211−#endif
1212− }
1213−#ifdef DEBUG
1214− s->bits_sent += (ulg)len<<3;
1215−#endif
1216− while (len--) {
1217− put_byte(s, *buf++);
1218− }
1219−}
cbits/trees.hdeleted−128
@@ -1,128 +0,0 @@
1−/* header created automatically with -DGEN_TREES_H */
2−
3−local const ct_data static_ltree[L_CODES+2] = {
4−{{ 12},{ 8}}, {{140},{ 8}}, {{ 76},{ 8}}, {{204},{ 8}}, {{ 44},{ 8}},
5−{{172},{ 8}}, {{108},{ 8}}, {{236},{ 8}}, {{ 28},{ 8}}, {{156},{ 8}},
6−{{ 92},{ 8}}, {{220},{ 8}}, {{ 60},{ 8}}, {{188},{ 8}}, {{124},{ 8}},
7−{{252},{ 8}}, {{ 2},{ 8}}, {{130},{ 8}}, {{ 66},{ 8}}, {{194},{ 8}},
8−{{ 34},{ 8}}, {{162},{ 8}}, {{ 98},{ 8}}, {{226},{ 8}}, {{ 18},{ 8}},
9−{{146},{ 8}}, {{ 82},{ 8}}, {{210},{ 8}}, {{ 50},{ 8}}, {{178},{ 8}},
10−{{114},{ 8}}, {{242},{ 8}}, {{ 10},{ 8}}, {{138},{ 8}}, {{ 74},{ 8}},
11−{{202},{ 8}}, {{ 42},{ 8}}, {{170},{ 8}}, {{106},{ 8}}, {{234},{ 8}},
12−{{ 26},{ 8}}, {{154},{ 8}}, {{ 90},{ 8}}, {{218},{ 8}}, {{ 58},{ 8}},
13−{{186},{ 8}}, {{122},{ 8}}, {{250},{ 8}}, {{ 6},{ 8}}, {{134},{ 8}},
14−{{ 70},{ 8}}, {{198},{ 8}}, {{ 38},{ 8}}, {{166},{ 8}}, {{102},{ 8}},
15−{{230},{ 8}}, {{ 22},{ 8}}, {{150},{ 8}}, {{ 86},{ 8}}, {{214},{ 8}},
16−{{ 54},{ 8}}, {{182},{ 8}}, {{118},{ 8}}, {{246},{ 8}}, {{ 14},{ 8}},
17−{{142},{ 8}}, {{ 78},{ 8}}, {{206},{ 8}}, {{ 46},{ 8}}, {{174},{ 8}},
18−{{110},{ 8}}, {{238},{ 8}}, {{ 30},{ 8}}, {{158},{ 8}}, {{ 94},{ 8}},
19−{{222},{ 8}}, {{ 62},{ 8}}, {{190},{ 8}}, {{126},{ 8}}, {{254},{ 8}},
20−{{ 1},{ 8}}, {{129},{ 8}}, {{ 65},{ 8}}, {{193},{ 8}}, {{ 33},{ 8}},
21−{{161},{ 8}}, {{ 97},{ 8}}, {{225},{ 8}}, {{ 17},{ 8}}, {{145},{ 8}},
22−{{ 81},{ 8}}, {{209},{ 8}}, {{ 49},{ 8}}, {{177},{ 8}}, {{113},{ 8}},
23−{{241},{ 8}}, {{ 9},{ 8}}, {{137},{ 8}}, {{ 73},{ 8}}, {{201},{ 8}},
24−{{ 41},{ 8}}, {{169},{ 8}}, {{105},{ 8}}, {{233},{ 8}}, {{ 25},{ 8}},
25−{{153},{ 8}}, {{ 89},{ 8}}, {{217},{ 8}}, {{ 57},{ 8}}, {{185},{ 8}},
26−{{121},{ 8}}, {{249},{ 8}}, {{ 5},{ 8}}, {{133},{ 8}}, {{ 69},{ 8}},
27−{{197},{ 8}}, {{ 37},{ 8}}, {{165},{ 8}}, {{101},{ 8}}, {{229},{ 8}},
28−{{ 21},{ 8}}, {{149},{ 8}}, {{ 85},{ 8}}, {{213},{ 8}}, {{ 53},{ 8}},
29−{{181},{ 8}}, {{117},{ 8}}, {{245},{ 8}}, {{ 13},{ 8}}, {{141},{ 8}},
30−{{ 77},{ 8}}, {{205},{ 8}}, {{ 45},{ 8}}, {{173},{ 8}}, {{109},{ 8}},
31−{{237},{ 8}}, {{ 29},{ 8}}, {{157},{ 8}}, {{ 93},{ 8}}, {{221},{ 8}},
32−{{ 61},{ 8}}, {{189},{ 8}}, {{125},{ 8}}, {{253},{ 8}}, {{ 19},{ 9}},
33−{{275},{ 9}}, {{147},{ 9}}, {{403},{ 9}}, {{ 83},{ 9}}, {{339},{ 9}},
34−{{211},{ 9}}, {{467},{ 9}}, {{ 51},{ 9}}, {{307},{ 9}}, {{179},{ 9}},
35−{{435},{ 9}}, {{115},{ 9}}, {{371},{ 9}}, {{243},{ 9}}, {{499},{ 9}},
36−{{ 11},{ 9}}, {{267},{ 9}}, {{139},{ 9}}, {{395},{ 9}}, {{ 75},{ 9}},
37−{{331},{ 9}}, {{203},{ 9}}, {{459},{ 9}}, {{ 43},{ 9}}, {{299},{ 9}},
38−{{171},{ 9}}, {{427},{ 9}}, {{107},{ 9}}, {{363},{ 9}}, {{235},{ 9}},
39−{{491},{ 9}}, {{ 27},{ 9}}, {{283},{ 9}}, {{155},{ 9}}, {{411},{ 9}},
40−{{ 91},{ 9}}, {{347},{ 9}}, {{219},{ 9}}, {{475},{ 9}}, {{ 59},{ 9}},
41−{{315},{ 9}}, {{187},{ 9}}, {{443},{ 9}}, {{123},{ 9}}, {{379},{ 9}},
42−{{251},{ 9}}, {{507},{ 9}}, {{ 7},{ 9}}, {{263},{ 9}}, {{135},{ 9}},
43−{{391},{ 9}}, {{ 71},{ 9}}, {{327},{ 9}}, {{199},{ 9}}, {{455},{ 9}},
44−{{ 39},{ 9}}, {{295},{ 9}}, {{167},{ 9}}, {{423},{ 9}}, {{103},{ 9}},
45−{{359},{ 9}}, {{231},{ 9}}, {{487},{ 9}}, {{ 23},{ 9}}, {{279},{ 9}},
46−{{151},{ 9}}, {{407},{ 9}}, {{ 87},{ 9}}, {{343},{ 9}}, {{215},{ 9}},
47−{{471},{ 9}}, {{ 55},{ 9}}, {{311},{ 9}}, {{183},{ 9}}, {{439},{ 9}},
48−{{119},{ 9}}, {{375},{ 9}}, {{247},{ 9}}, {{503},{ 9}}, {{ 15},{ 9}},
49−{{271},{ 9}}, {{143},{ 9}}, {{399},{ 9}}, {{ 79},{ 9}}, {{335},{ 9}},
50−{{207},{ 9}}, {{463},{ 9}}, {{ 47},{ 9}}, {{303},{ 9}}, {{175},{ 9}},
51−{{431},{ 9}}, {{111},{ 9}}, {{367},{ 9}}, {{239},{ 9}}, {{495},{ 9}},
52−{{ 31},{ 9}}, {{287},{ 9}}, {{159},{ 9}}, {{415},{ 9}}, {{ 95},{ 9}},
53−{{351},{ 9}}, {{223},{ 9}}, {{479},{ 9}}, {{ 63},{ 9}}, {{319},{ 9}},
54−{{191},{ 9}}, {{447},{ 9}}, {{127},{ 9}}, {{383},{ 9}}, {{255},{ 9}},
55−{{511},{ 9}}, {{ 0},{ 7}}, {{ 64},{ 7}}, {{ 32},{ 7}}, {{ 96},{ 7}},
56−{{ 16},{ 7}}, {{ 80},{ 7}}, {{ 48},{ 7}}, {{112},{ 7}}, {{ 8},{ 7}},
57−{{ 72},{ 7}}, {{ 40},{ 7}}, {{104},{ 7}}, {{ 24},{ 7}}, {{ 88},{ 7}},
58−{{ 56},{ 7}}, {{120},{ 7}}, {{ 4},{ 7}}, {{ 68},{ 7}}, {{ 36},{ 7}},
59−{{100},{ 7}}, {{ 20},{ 7}}, {{ 84},{ 7}}, {{ 52},{ 7}}, {{116},{ 7}},
60−{{ 3},{ 8}}, {{131},{ 8}}, {{ 67},{ 8}}, {{195},{ 8}}, {{ 35},{ 8}},
61−{{163},{ 8}}, {{ 99},{ 8}}, {{227},{ 8}}
62−};
63−
64−local const ct_data static_dtree[D_CODES] = {
65−{{ 0},{ 5}}, {{16},{ 5}}, {{ 8},{ 5}}, {{24},{ 5}}, {{ 4},{ 5}},
66−{{20},{ 5}}, {{12},{ 5}}, {{28},{ 5}}, {{ 2},{ 5}}, {{18},{ 5}},
67−{{10},{ 5}}, {{26},{ 5}}, {{ 6},{ 5}}, {{22},{ 5}}, {{14},{ 5}},
68−{{30},{ 5}}, {{ 1},{ 5}}, {{17},{ 5}}, {{ 9},{ 5}}, {{25},{ 5}},
69−{{ 5},{ 5}}, {{21},{ 5}}, {{13},{ 5}}, {{29},{ 5}}, {{ 3},{ 5}},
70−{{19},{ 5}}, {{11},{ 5}}, {{27},{ 5}}, {{ 7},{ 5}}, {{23},{ 5}}
71−};
72−
73−const uch _dist_code[DIST_CODE_LEN] = {
74− 0, 1, 2, 3, 4, 4, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8,
75− 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10,
76−10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
77−11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
78−12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13,
79−13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13,
80−13, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
81−14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
82−14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
83−14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15, 15, 15,
84−15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
85−15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
86−15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 0, 0, 16, 17,
87−18, 18, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 22, 22, 22, 22, 22, 22, 22, 22,
88−23, 23, 23, 23, 23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
89−24, 24, 24, 24, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25,
90−26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
91−26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 27, 27, 27, 27, 27, 27, 27, 27,
92−27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
93−27, 27, 27, 27, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
94−28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
95−28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
96−28, 28, 28, 28, 28, 28, 28, 28, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
97−29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
98−29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
99−29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29
100−};
101−
102−const uch _length_code[MAX_MATCH-MIN_MATCH+1]= {
103− 0, 1, 2, 3, 4, 5, 6, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 12, 12,
104−13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 15, 16, 16, 16, 16, 16, 16, 16, 16,
105−17, 17, 17, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18, 18, 18, 18, 19, 19, 19, 19,
106−19, 19, 19, 19, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
107−21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 22, 22, 22, 22,
108−22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 23, 23, 23, 23, 23, 23, 23, 23,
109−23, 23, 23, 23, 23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
110−24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
111−25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25,
112−25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 26, 26, 26, 26, 26, 26, 26, 26,
113−26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
114−26, 26, 26, 26, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
115−27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 28
116−};
117−
118−local const int base_length[LENGTH_CODES] = {
119−0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16, 20, 24, 28, 32, 40, 48, 56,
120−64, 80, 96, 112, 128, 160, 192, 224, 0
121−};
122−
123−local const int base_dist[D_CODES] = {
124− 0, 1, 2, 3, 4, 6, 8, 12, 16, 24,
125− 32, 48, 64, 96, 128, 192, 256, 384, 512, 768,
126− 1024, 1536, 2048, 3072, 4096, 6144, 8192, 12288, 16384, 24576
127−};
128−
cbits/uncompr.cdeleted−61
@@ -1,61 +0,0 @@
1−/* uncompr.c -- decompress a memory buffer
2− * Copyright (C) 1995-2003 Jean-loup Gailly.
3− * For conditions of distribution and use, see copyright notice in zlib.h
4− */
5−
6−/* @(#) $Id$ */
7−
8−#define ZLIB_INTERNAL
9−#include "zlib.h"
10−
11−/* ===========================================================================
12− Decompresses the source buffer into the destination buffer. sourceLen is
13− the byte length of the source buffer. Upon entry, destLen is the total
14− size of the destination buffer, which must be large enough to hold the
15− entire uncompressed data. (The size of the uncompressed data must have
16− been saved previously by the compressor and transmitted to the decompressor
17− by some mechanism outside the scope of this compression library.)
18− Upon exit, destLen is the actual size of the compressed buffer.
19− This function can be used to decompress a whole file at once if the
20− input file is mmap'ed.
21−
22− uncompress returns Z_OK if success, Z_MEM_ERROR if there was not
23− enough memory, Z_BUF_ERROR if there was not enough room in the output
24− buffer, or Z_DATA_ERROR if the input data was corrupted.
25−*/
26−int ZEXPORT uncompress (dest, destLen, source, sourceLen)
27− Bytef *dest;
28− uLongf *destLen;
29− const Bytef *source;
30− uLong sourceLen;
31−{
32− z_stream stream;
33− int err;
34−
35− stream.next_in = (Bytef*)source;
36− stream.avail_in = (uInt)sourceLen;
37− /* Check for source > 64K on 16-bit machine: */
38− if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR;
39−
40− stream.next_out = dest;
41− stream.avail_out = (uInt)*destLen;
42− if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR;
43−
44− stream.zalloc = (alloc_func)0;
45− stream.zfree = (free_func)0;
46−
47− err = inflateInit(&stream);
48− if (err != Z_OK) return err;
49−
50− err = inflate(&stream, Z_FINISH);
51− if (err != Z_STREAM_END) {
52− inflateEnd(&stream);
53− if (err == Z_NEED_DICT || (err == Z_BUF_ERROR && stream.avail_in == 0))
54− return Z_DATA_ERROR;
55− return err;
56− }
57− *destLen = stream.total_out;
58−
59− err = inflateEnd(&stream);
60− return err;
61−}
cbits/zconf.hdeleted−332
@@ -1,332 +0,0 @@
1−/* zconf.h -- configuration of the zlib compression library
2− * Copyright (C) 1995-2005 Jean-loup Gailly.
3− * For conditions of distribution and use, see copyright notice in zlib.h
4− */
5−
6−/* @(#) $Id$ */
7−
8−#ifndef ZCONF_H
9−#define ZCONF_H
10−
11−/*
12− * If you *really* need a unique prefix for all types and library functions,
13− * compile with -DZ_PREFIX. The "standard" zlib should be compiled without it.
14− */
15−#ifdef Z_PREFIX
16−# define deflateInit_ z_deflateInit_
17−# define deflate z_deflate
18−# define deflateEnd z_deflateEnd
19−# define inflateInit_ z_inflateInit_
20−# define inflate z_inflate
21−# define inflateEnd z_inflateEnd
22−# define deflateInit2_ z_deflateInit2_
23−# define deflateSetDictionary z_deflateSetDictionary
24−# define deflateCopy z_deflateCopy
25−# define deflateReset z_deflateReset
26−# define deflateParams z_deflateParams
27−# define deflateBound z_deflateBound
28−# define deflatePrime z_deflatePrime
29−# define inflateInit2_ z_inflateInit2_
30−# define inflateSetDictionary z_inflateSetDictionary
31−# define inflateSync z_inflateSync
32−# define inflateSyncPoint z_inflateSyncPoint
33−# define inflateCopy z_inflateCopy
34−# define inflateReset z_inflateReset
35−# define inflateBack z_inflateBack
36−# define inflateBackEnd z_inflateBackEnd
37−# define compress z_compress
38−# define compress2 z_compress2
39−# define compressBound z_compressBound
40−# define uncompress z_uncompress
41−# define adler32 z_adler32
42−# define crc32 z_crc32
43−# define get_crc_table z_get_crc_table
44−# define zError z_zError
45−
46−# define alloc_func z_alloc_func
47−# define free_func z_free_func
48−# define in_func z_in_func
49−# define out_func z_out_func
50−# define Byte z_Byte
51−# define uInt z_uInt
52−# define uLong z_uLong
53−# define Bytef z_Bytef
54−# define charf z_charf
55−# define intf z_intf
56−# define uIntf z_uIntf
57−# define uLongf z_uLongf
58−# define voidpf z_voidpf
59−# define voidp z_voidp
60−#endif
61−
62−#if defined(__MSDOS__) && !defined(MSDOS)
63−# define MSDOS
64−#endif
65−#if (defined(OS_2) || defined(__OS2__)) && !defined(OS2)
66−# define OS2
67−#endif
68−#if defined(_WINDOWS) && !defined(WINDOWS)
69−# define WINDOWS
70−#endif
71−#if defined(_WIN32) || defined(_WIN32_WCE) || defined(__WIN32__)
72−# ifndef WIN32
73−# define WIN32
74−# endif
75−#endif
76−#if (defined(MSDOS) || defined(OS2) || defined(WINDOWS)) && !defined(WIN32)
77−# if !defined(__GNUC__) && !defined(__FLAT__) && !defined(__386__)
78−# ifndef SYS16BIT
79−# define SYS16BIT
80−# endif
81−# endif
82−#endif
83−
84−/*
85− * Compile with -DMAXSEG_64K if the alloc function cannot allocate more
86− * than 64k bytes at a time (needed on systems with 16-bit int).
87− */
88−#ifdef SYS16BIT
89−# define MAXSEG_64K
90−#endif
91−#ifdef MSDOS
92−# define UNALIGNED_OK
93−#endif
94−
95−#ifdef __STDC_VERSION__
96−# ifndef STDC
97−# define STDC
98−# endif
99−# if __STDC_VERSION__ >= 199901L
100−# ifndef STDC99
101−# define STDC99
102−# endif
103−# endif
104−#endif
105−#if !defined(STDC) && (defined(__STDC__) || defined(__cplusplus))
106−# define STDC
107−#endif
108−#if !defined(STDC) && (defined(__GNUC__) || defined(__BORLANDC__))
109−# define STDC
110−#endif
111−#if !defined(STDC) && (defined(MSDOS) || defined(WINDOWS) || defined(WIN32))
112−# define STDC
113−#endif
114−#if !defined(STDC) && (defined(OS2) || defined(__HOS_AIX__))
115−# define STDC
116−#endif
117−
118−#if defined(__OS400__) && !defined(STDC) /* iSeries (formerly AS/400). */
119−# define STDC
120−#endif
121−
122−#ifndef STDC
123−# ifndef const /* cannot use !defined(STDC) && !defined(const) on Mac */
124−# define const /* note: need a more gentle solution here */
125−# endif
126−#endif
127−
128−/* Some Mac compilers merge all .h files incorrectly: */
129−#if defined(__MWERKS__)||defined(applec)||defined(THINK_C)||defined(__SC__)
130−# define NO_DUMMY_DECL
131−#endif
132−
133−/* Maximum value for memLevel in deflateInit2 */
134−#ifndef MAX_MEM_LEVEL
135−# ifdef MAXSEG_64K
136−# define MAX_MEM_LEVEL 8
137−# else
138−# define MAX_MEM_LEVEL 9
139−# endif
140−#endif
141−
142−/* Maximum value for windowBits in deflateInit2 and inflateInit2.
143− * WARNING: reducing MAX_WBITS makes minigzip unable to extract .gz files
144− * created by gzip. (Files created by minigzip can still be extracted by
145− * gzip.)
146− */
147−#ifndef MAX_WBITS
148−# define MAX_WBITS 15 /* 32K LZ77 window */
149−#endif
150−
151−/* The memory requirements for deflate are (in bytes):
152− (1 << (windowBits+2)) + (1 << (memLevel+9))
153− that is: 128K for windowBits=15 + 128K for memLevel = 8 (default values)
154− plus a few kilobytes for small objects. For example, if you want to reduce
155− the default memory requirements from 256K to 128K, compile with
156− make CFLAGS="-O -DMAX_WBITS=14 -DMAX_MEM_LEVEL=7"
157− Of course this will generally degrade compression (there's no free lunch).
158−
159− The memory requirements for inflate are (in bytes) 1 << windowBits
160− that is, 32K for windowBits=15 (default value) plus a few kilobytes
161− for small objects.
162−*/
163−
164− /* Type declarations */
165−
166−#ifndef OF /* function prototypes */
167−# ifdef STDC
168−# define OF(args) args
169−# else
170−# define OF(args) ()
171−# endif
172−#endif
173−
174−/* The following definitions for FAR are needed only for MSDOS mixed
175− * model programming (small or medium model with some far allocations).
176− * This was tested only with MSC; for other MSDOS compilers you may have
177− * to define NO_MEMCPY in zutil.h. If you don't need the mixed model,
178− * just define FAR to be empty.
179− */
180−#ifdef SYS16BIT
181−# if defined(M_I86SM) || defined(M_I86MM)
182− /* MSC small or medium model */
183−# define SMALL_MEDIUM
184−# ifdef _MSC_VER
185−# define FAR _far
186−# else
187−# define FAR far
188−# endif
189−# endif
190−# if (defined(__SMALL__) || defined(__MEDIUM__))
191− /* Turbo C small or medium model */
192−# define SMALL_MEDIUM
193−# ifdef __BORLANDC__
194−# define FAR _far
195−# else
196−# define FAR far
197−# endif
198−# endif
199−#endif
200−
201−#if defined(WINDOWS) || defined(WIN32)
202− /* If building or using zlib as a DLL, define ZLIB_DLL.
203− * This is not mandatory, but it offers a little performance increase.
204− */
205−# ifdef ZLIB_DLL
206−# if defined(WIN32) && (!defined(__BORLANDC__) || (__BORLANDC__ >= 0x500))
207−# ifdef ZLIB_INTERNAL
208−# define ZEXTERN extern __declspec(dllexport)
209−# else
210−# define ZEXTERN extern __declspec(dllimport)
211−# endif
212−# endif
213−# endif /* ZLIB_DLL */
214− /* If building or using zlib with the WINAPI/WINAPIV calling convention,
215− * define ZLIB_WINAPI.
216− * Caution: the standard ZLIB1.DLL is NOT compiled using ZLIB_WINAPI.
217− */
218−# ifdef ZLIB_WINAPI
219−# ifdef FAR
220−# undef FAR
221−# endif
222−# include <windows.h>
223− /* No need for _export, use ZLIB.DEF instead. */
224− /* For complete Windows compatibility, use WINAPI, not __stdcall. */
225−# define ZEXPORT WINAPI
226−# ifdef WIN32
227−# define ZEXPORTVA WINAPIV
228−# else
229−# define ZEXPORTVA FAR CDECL
230−# endif
231−# endif
232−#endif
233−
234−#if defined (__BEOS__)
235−# ifdef ZLIB_DLL
236−# ifdef ZLIB_INTERNAL
237−# define ZEXPORT __declspec(dllexport)
238−# define ZEXPORTVA __declspec(dllexport)
239−# else
240−# define ZEXPORT __declspec(dllimport)
241−# define ZEXPORTVA __declspec(dllimport)
242−# endif
243−# endif
244−#endif
245−
246−#ifndef ZEXTERN
247−# define ZEXTERN extern
248−#endif
249−#ifndef ZEXPORT
250−# define ZEXPORT
251−#endif
252−#ifndef ZEXPORTVA
253−# define ZEXPORTVA
254−#endif
255−
256−#ifndef FAR
257−# define FAR
258−#endif
259−
260−#if !defined(__MACTYPES__)
261−typedef unsigned char Byte; /* 8 bits */
262−#endif
263−typedef unsigned int uInt; /* 16 bits or more */
264−typedef unsigned long uLong; /* 32 bits or more */
265−
266−#ifdef SMALL_MEDIUM
267− /* Borland C/C++ and some old MSC versions ignore FAR inside typedef */
268−# define Bytef Byte FAR
269−#else
270− typedef Byte FAR Bytef;
271−#endif
272−typedef char FAR charf;
273−typedef int FAR intf;
274−typedef uInt FAR uIntf;
275−typedef uLong FAR uLongf;
276−
277−#ifdef STDC
278− typedef void const *voidpc;
279− typedef void FAR *voidpf;
280− typedef void *voidp;
281−#else
282− typedef Byte const *voidpc;
283− typedef Byte FAR *voidpf;
284− typedef Byte *voidp;
285−#endif
286−
287−#if 1 /* HAVE_UNISTD_H -- this line is updated by ./configure */
288−# include <sys/types.h> /* for off_t */
289−# include <unistd.h> /* for SEEK_* and off_t */
290−# ifdef VMS
291−# include <unixio.h> /* for off_t */
292−# endif
293−# define z_off_t off_t
294−#endif
295−#ifndef SEEK_SET
296−# define SEEK_SET 0 /* Seek from beginning of file. */
297−# define SEEK_CUR 1 /* Seek from current position. */
298−# define SEEK_END 2 /* Set file pointer to EOF plus "offset" */
299−#endif
300−#ifndef z_off_t
301−# define z_off_t long
302−#endif
303−
304−#if defined(__OS400__)
305−# define NO_vsnprintf
306−#endif
307−
308−#if defined(__MVS__)
309−# define NO_vsnprintf
310−# ifdef FAR
311−# undef FAR
312−# endif
313−#endif
314−
315−/* MVS linker does not support external names larger than 8 bytes */
316−#if defined(__MVS__)
317−# pragma map(deflateInit_,"DEIN")
318−# pragma map(deflateInit2_,"DEIN2")
319−# pragma map(deflateEnd,"DEEND")
320−# pragma map(deflateBound,"DEBND")
321−# pragma map(inflateInit_,"ININ")
322−# pragma map(inflateInit2_,"ININ2")
323−# pragma map(inflateEnd,"INEND")
324−# pragma map(inflateSync,"INSY")
325−# pragma map(inflateSetDictionary,"INSEDI")
326−# pragma map(compressBound,"CMBND")
327−# pragma map(inflate_table,"INTABL")
328−# pragma map(inflate_fast,"INFA")
329−# pragma map(inflate_copyright,"INCOPY")
330−#endif
331−
332−#endif /* ZCONF_H */
cbits/zlib.hdeleted−1357
@@ -1,1357 +0,0 @@
1−/* zlib.h -- interface of the 'zlib' general purpose compression library
2− version 1.2.3, July 18th, 2005
3−
4− Copyright (C) 1995-2005 Jean-loup Gailly and Mark Adler
5−
6− This software is provided 'as-is', without any express or implied
7− warranty. In no event will the authors be held liable for any damages
8− arising from the use of this software.
9−
10− Permission is granted to anyone to use this software for any purpose,
11− including commercial applications, and to alter it and redistribute it
12− freely, subject to the following restrictions:
13−
14− 1. The origin of this software must not be misrepresented; you must not
15− claim that you wrote the original software. If you use this software
16− in a product, an acknowledgment in the product documentation would be
17− appreciated but is not required.
18− 2. Altered source versions must be plainly marked as such, and must not be
19− misrepresented as being the original software.
20− 3. This notice may not be removed or altered from any source distribution.
21−
22− Jean-loup Gailly Mark Adler
23− jloup@gzip.org madler@alumni.caltech.edu
24−
25−
26− The data format used by the zlib library is described by RFCs (Request for
27− Comments) 1950 to 1952 in the files http://www.ietf.org/rfc/rfc1950.txt
28− (zlib format), rfc1951.txt (deflate format) and rfc1952.txt (gzip format).
29−*/
30−
31−#ifndef ZLIB_H
32−#define ZLIB_H
33−
34−#include "zconf.h"
35−
36−#ifdef __cplusplus
37−extern "C" {
38−#endif
39−
40−#define ZLIB_VERSION "1.2.3"
41−#define ZLIB_VERNUM 0x1230
42−
43−/*
44− The 'zlib' compression library provides in-memory compression and
45− decompression functions, including integrity checks of the uncompressed
46− data. This version of the library supports only one compression method
47− (deflation) but other algorithms will be added later and will have the same
48− stream interface.
49−
50− Compression can be done in a single step if the buffers are large
51− enough (for example if an input file is mmap'ed), or can be done by
52− repeated calls of the compression function. In the latter case, the
53− application must provide more input and/or consume the output
54− (providing more output space) before each call.
55−
56− The compressed data format used by default by the in-memory functions is
57− the zlib format, which is a zlib wrapper documented in RFC 1950, wrapped
58− around a deflate stream, which is itself documented in RFC 1951.
59−
60− The library also supports reading and writing files in gzip (.gz) format
61− with an interface similar to that of stdio using the functions that start
62− with "gz". The gzip format is different from the zlib format. gzip is a
63− gzip wrapper, documented in RFC 1952, wrapped around a deflate stream.
64−
65− This library can optionally read and write gzip streams in memory as well.
66−
67− The zlib format was designed to be compact and fast for use in memory
68− and on communications channels. The gzip format was designed for single-
69− file compression on file systems, has a larger header than zlib to maintain
70− directory information, and uses a different, slower check method than zlib.
71−
72− The library does not install any signal handler. The decoder checks
73− the consistency of the compressed data, so the library should never
74− crash even in case of corrupted input.
75−*/
76−
77−typedef voidpf (*alloc_func) OF((voidpf opaque, uInt items, uInt size));
78−typedef void (*free_func) OF((voidpf opaque, voidpf address));
79−
80−struct internal_state;
81−
82−typedef struct z_stream_s {
83− Bytef *next_in; /* next input byte */
84− uInt avail_in; /* number of bytes available at next_in */
85− uLong total_in; /* total nb of input bytes read so far */
86−
87− Bytef *next_out; /* next output byte should be put there */
88− uInt avail_out; /* remaining free space at next_out */
89− uLong total_out; /* total nb of bytes output so far */
90−
91− char *msg; /* last error message, NULL if no error */
92− struct internal_state FAR *state; /* not visible by applications */
93−
94− alloc_func zalloc; /* used to allocate the internal state */
95− free_func zfree; /* used to free the internal state */
96− voidpf opaque; /* private data object passed to zalloc and zfree */
97−
98− int data_type; /* best guess about the data type: binary or text */
99− uLong adler; /* adler32 value of the uncompressed data */
100− uLong reserved; /* reserved for future use */
101−} z_stream;
102−
103−typedef z_stream FAR *z_streamp;
104−
105−/*
106− gzip header information passed to and from zlib routines. See RFC 1952
107− for more details on the meanings of these fields.
108−*/
109−typedef struct gz_header_s {
110− int text; /* true if compressed data believed to be text */
111− uLong time; /* modification time */
112− int xflags; /* extra flags (not used when writing a gzip file) */
113− int os; /* operating system */
114− Bytef *extra; /* pointer to extra field or Z_NULL if none */
115− uInt extra_len; /* extra field length (valid if extra != Z_NULL) */
116− uInt extra_max; /* space at extra (only when reading header) */
117− Bytef *name; /* pointer to zero-terminated file name or Z_NULL */
118− uInt name_max; /* space at name (only when reading header) */
119− Bytef *comment; /* pointer to zero-terminated comment or Z_NULL */
120− uInt comm_max; /* space at comment (only when reading header) */
121− int hcrc; /* true if there was or will be a header crc */
122− int done; /* true when done reading gzip header (not used
123− when writing a gzip file) */
124−} gz_header;
125−
126−typedef gz_header FAR *gz_headerp;
127−
128−/*
129− The application must update next_in and avail_in when avail_in has
130− dropped to zero. It must update next_out and avail_out when avail_out
131− has dropped to zero. The application must initialize zalloc, zfree and
132− opaque before calling the init function. All other fields are set by the
133− compression library and must not be updated by the application.
134−
135− The opaque value provided by the application will be passed as the first
136− parameter for calls of zalloc and zfree. This can be useful for custom
137− memory management. The compression library attaches no meaning to the
138− opaque value.
139−
140− zalloc must return Z_NULL if there is not enough memory for the object.
141− If zlib is used in a multi-threaded application, zalloc and zfree must be
142− thread safe.
143−
144− On 16-bit systems, the functions zalloc and zfree must be able to allocate
145− exactly 65536 bytes, but will not be required to allocate more than this
146− if the symbol MAXSEG_64K is defined (see zconf.h). WARNING: On MSDOS,
147− pointers returned by zalloc for objects of exactly 65536 bytes *must*
148− have their offset normalized to zero. The default allocation function
149− provided by this library ensures this (see zutil.c). To reduce memory
150− requirements and avoid any allocation of 64K objects, at the expense of
151− compression ratio, compile the library with -DMAX_WBITS=14 (see zconf.h).
152−
153− The fields total_in and total_out can be used for statistics or
154− progress reports. After compression, total_in holds the total size of
155− the uncompressed data and may be saved for use in the decompressor
156− (particularly if the decompressor wants to decompress everything in
157− a single step).
158−*/
159−
160− /* constants */
161−
162−#define Z_NO_FLUSH 0
163−#define Z_PARTIAL_FLUSH 1 /* will be removed, use Z_SYNC_FLUSH instead */
164−#define Z_SYNC_FLUSH 2
165−#define Z_FULL_FLUSH 3
166−#define Z_FINISH 4
167−#define Z_BLOCK 5
168−/* Allowed flush values; see deflate() and inflate() below for details */
169−
170−#define Z_OK 0
171−#define Z_STREAM_END 1
172−#define Z_NEED_DICT 2
173−#define Z_ERRNO (-1)
174−#define Z_STREAM_ERROR (-2)
175−#define Z_DATA_ERROR (-3)
176−#define Z_MEM_ERROR (-4)
177−#define Z_BUF_ERROR (-5)
178−#define Z_VERSION_ERROR (-6)
179−/* Return codes for the compression/decompression functions. Negative
180− * values are errors, positive values are used for special but normal events.
181− */
182−
183−#define Z_NO_COMPRESSION 0
184−#define Z_BEST_SPEED 1
185−#define Z_BEST_COMPRESSION 9
186−#define Z_DEFAULT_COMPRESSION (-1)
187−/* compression levels */
188−
189−#define Z_FILTERED 1
190−#define Z_HUFFMAN_ONLY 2
191−#define Z_RLE 3
192−#define Z_FIXED 4
193−#define Z_DEFAULT_STRATEGY 0
194−/* compression strategy; see deflateInit2() below for details */
195−
196−#define Z_BINARY 0
197−#define Z_TEXT 1
198−#define Z_ASCII Z_TEXT /* for compatibility with 1.2.2 and earlier */
199−#define Z_UNKNOWN 2
200−/* Possible values of the data_type field (though see inflate()) */
201−
202−#define Z_DEFLATED 8
203−/* The deflate compression method (the only one supported in this version) */
204−
205−#define Z_NULL 0 /* for initializing zalloc, zfree, opaque */
206−
207−#define zlib_version zlibVersion()
208−/* for compatibility with versions < 1.0.2 */
209−
210− /* basic functions */
211−
212−ZEXTERN const char * ZEXPORT zlibVersion OF((void));
213−/* The application can compare zlibVersion and ZLIB_VERSION for consistency.
214− If the first character differs, the library code actually used is
215− not compatible with the zlib.h header file used by the application.
216− This check is automatically made by deflateInit and inflateInit.
217− */
218−
219−/*
220−ZEXTERN int ZEXPORT deflateInit OF((z_streamp strm, int level));
221−
222− Initializes the internal stream state for compression. The fields
223− zalloc, zfree and opaque must be initialized before by the caller.
224− If zalloc and zfree are set to Z_NULL, deflateInit updates them to
225− use default allocation functions.
226−
227− The compression level must be Z_DEFAULT_COMPRESSION, or between 0 and 9:
228− 1 gives best speed, 9 gives best compression, 0 gives no compression at
229− all (the input data is simply copied a block at a time).
230− Z_DEFAULT_COMPRESSION requests a default compromise between speed and
231− compression (currently equivalent to level 6).
232−
233− deflateInit returns Z_OK if success, Z_MEM_ERROR if there was not
234− enough memory, Z_STREAM_ERROR if level is not a valid compression level,
235− Z_VERSION_ERROR if the zlib library version (zlib_version) is incompatible
236− with the version assumed by the caller (ZLIB_VERSION).
237− msg is set to null if there is no error message. deflateInit does not
238− perform any compression: this will be done by deflate().
239−*/
240−
241−
242−ZEXTERN int ZEXPORT deflate OF((z_streamp strm, int flush));
243−/*
244− deflate compresses as much data as possible, and stops when the input
245− buffer becomes empty or the output buffer becomes full. It may introduce some
246− output latency (reading input without producing any output) except when
247− forced to flush.
248−
249− The detailed semantics are as follows. deflate performs one or both of the
250− following actions:
251−
252− - Compress more input starting at next_in and update next_in and avail_in
253− accordingly. If not all input can be processed (because there is not
254− enough room in the output buffer), next_in and avail_in are updated and
255− processing will resume at this point for the next call of deflate().
256−
257− - Provide more output starting at next_out and update next_out and avail_out
258− accordingly. This action is forced if the parameter flush is non zero.
259− Forcing flush frequently degrades the compression ratio, so this parameter
260− should be set only when necessary (in interactive applications).
261− Some output may be provided even if flush is not set.
262−
263− Before the call of deflate(), the application should ensure that at least
264− one of the actions is possible, by providing more input and/or consuming
265− more output, and updating avail_in or avail_out accordingly; avail_out
266− should never be zero before the call. The application can consume the
267− compressed output when it wants, for example when the output buffer is full
268− (avail_out == 0), or after each call of deflate(). If deflate returns Z_OK
269− and with zero avail_out, it must be called again after making room in the
270− output buffer because there might be more output pending.
271−
272− Normally the parameter flush is set to Z_NO_FLUSH, which allows deflate to
273− decide how much data to accumualte before producing output, in order to
274− maximize compression.
275−
276− If the parameter flush is set to Z_SYNC_FLUSH, all pending output is
277− flushed to the output buffer and the output is aligned on a byte boundary, so
278− that the decompressor can get all input data available so far. (In particular
279− avail_in is zero after the call if enough output space has been provided
280− before the call.) Flushing may degrade compression for some compression
281− algorithms and so it should be used only when necessary.
282−
283− If flush is set to Z_FULL_FLUSH, all output is flushed as with
284− Z_SYNC_FLUSH, and the compression state is reset so that decompression can
285− restart from this point if previous compressed data has been damaged or if
286− random access is desired. Using Z_FULL_FLUSH too often can seriously degrade
287− compression.
288−
289− If deflate returns with avail_out == 0, this function must be called again
290− with the same value of the flush parameter and more output space (updated
291− avail_out), until the flush is complete (deflate returns with non-zero
292− avail_out). In the case of a Z_FULL_FLUSH or Z_SYNC_FLUSH, make sure that
293− avail_out is greater than six to avoid repeated flush markers due to
294− avail_out == 0 on return.
295−
296− If the parameter flush is set to Z_FINISH, pending input is processed,
297− pending output is flushed and deflate returns with Z_STREAM_END if there
298− was enough output space; if deflate returns with Z_OK, this function must be
299− called again with Z_FINISH and more output space (updated avail_out) but no
300− more input data, until it returns with Z_STREAM_END or an error. After
301− deflate has returned Z_STREAM_END, the only possible operations on the
302− stream are deflateReset or deflateEnd.
303−
304− Z_FINISH can be used immediately after deflateInit if all the compression
305− is to be done in a single step. In this case, avail_out must be at least
306− the value returned by deflateBound (see below). If deflate does not return
307− Z_STREAM_END, then it must be called again as described above.
308−
309− deflate() sets strm->adler to the adler32 checksum of all input read
310− so far (that is, total_in bytes).
311−
312− deflate() may update strm->data_type if it can make a good guess about
313− the input data type (Z_BINARY or Z_TEXT). In doubt, the data is considered
314− binary. This field is only for information purposes and does not affect
315− the compression algorithm in any manner.
316−
317− deflate() returns Z_OK if some progress has been made (more input
318− processed or more output produced), Z_STREAM_END if all input has been
319− consumed and all output has been produced (only when flush is set to
320− Z_FINISH), Z_STREAM_ERROR if the stream state was inconsistent (for example
321− if next_in or next_out was NULL), Z_BUF_ERROR if no progress is possible
322− (for example avail_in or avail_out was zero). Note that Z_BUF_ERROR is not
323− fatal, and deflate() can be called again with more input and more output
324− space to continue compressing.
325−*/
326−
327−
328−ZEXTERN int ZEXPORT deflateEnd OF((z_streamp strm));
329−/*
330− All dynamically allocated data structures for this stream are freed.
331− This function discards any unprocessed input and does not flush any
332− pending output.
333−
334− deflateEnd returns Z_OK if success, Z_STREAM_ERROR if the
335− stream state was inconsistent, Z_DATA_ERROR if the stream was freed
336− prematurely (some input or output was discarded). In the error case,
337− msg may be set but then points to a static string (which must not be
338− deallocated).
339−*/
340−
341−
342−/*
343−ZEXTERN int ZEXPORT inflateInit OF((z_streamp strm));
344−
345− Initializes the internal stream state for decompression. The fields
346− next_in, avail_in, zalloc, zfree and opaque must be initialized before by
347− the caller. If next_in is not Z_NULL and avail_in is large enough (the exact
348− value depends on the compression method), inflateInit determines the
349− compression method from the zlib header and allocates all data structures
350− accordingly; otherwise the allocation will be deferred to the first call of
351− inflate. If zalloc and zfree are set to Z_NULL, inflateInit updates them to
352− use default allocation functions.
353−
354− inflateInit returns Z_OK if success, Z_MEM_ERROR if there was not enough
355− memory, Z_VERSION_ERROR if the zlib library version is incompatible with the
356− version assumed by the caller. msg is set to null if there is no error
357− message. inflateInit does not perform any decompression apart from reading
358− the zlib header if present: this will be done by inflate(). (So next_in and
359− avail_in may be modified, but next_out and avail_out are unchanged.)
360−*/
361−
362−
363−ZEXTERN int ZEXPORT inflate OF((z_streamp strm, int flush));
364−/*
365− inflate decompresses as much data as possible, and stops when the input
366− buffer becomes empty or the output buffer becomes full. It may introduce
367− some output latency (reading input without producing any output) except when
368− forced to flush.
369−
370− The detailed semantics are as follows. inflate performs one or both of the
371− following actions:
372−
373− - Decompress more input starting at next_in and update next_in and avail_in
374− accordingly. If not all input can be processed (because there is not
375− enough room in the output buffer), next_in is updated and processing
376− will resume at this point for the next call of inflate().
377−
378− - Provide more output starting at next_out and update next_out and avail_out
379− accordingly. inflate() provides as much output as possible, until there
380− is no more input data or no more space in the output buffer (see below
381− about the flush parameter).
382−
383− Before the call of inflate(), the application should ensure that at least
384− one of the actions is possible, by providing more input and/or consuming
385− more output, and updating the next_* and avail_* values accordingly.
386− The application can consume the uncompressed output when it wants, for
387− example when the output buffer is full (avail_out == 0), or after each
388− call of inflate(). If inflate returns Z_OK and with zero avail_out, it
389− must be called again after making room in the output buffer because there
390− might be more output pending.
391−
392− The flush parameter of inflate() can be Z_NO_FLUSH, Z_SYNC_FLUSH,
393− Z_FINISH, or Z_BLOCK. Z_SYNC_FLUSH requests that inflate() flush as much
394− output as possible to the output buffer. Z_BLOCK requests that inflate() stop
395− if and when it gets to the next deflate block boundary. When decoding the
396− zlib or gzip format, this will cause inflate() to return immediately after
397− the header and before the first block. When doing a raw inflate, inflate()
398− will go ahead and process the first block, and will return when it gets to
399− the end of that block, or when it runs out of data.
400−
401− The Z_BLOCK option assists in appending to or combining deflate streams.
402− Also to assist in this, on return inflate() will set strm->data_type to the
403− number of unused bits in the last byte taken from strm->next_in, plus 64
404− if inflate() is currently decoding the last block in the deflate stream,
405− plus 128 if inflate() returned immediately after decoding an end-of-block
406− code or decoding the complete header up to just before the first byte of the
407− deflate stream. The end-of-block will not be indicated until all of the
408− uncompressed data from that block has been written to strm->next_out. The
409− number of unused bits may in general be greater than seven, except when
410− bit 7 of data_type is set, in which case the number of unused bits will be
411− less than eight.
412−
413− inflate() should normally be called until it returns Z_STREAM_END or an
414− error. However if all decompression is to be performed in a single step
415− (a single call of inflate), the parameter flush should be set to
416− Z_FINISH. In this case all pending input is processed and all pending
417− output is flushed; avail_out must be large enough to hold all the
418− uncompressed data. (The size of the uncompressed data may have been saved
419− by the compressor for this purpose.) The next operation on this stream must
420− be inflateEnd to deallocate the decompression state. The use of Z_FINISH
421− is never required, but can be used to inform inflate that a faster approach
422− may be used for the single inflate() call.
423−
424− In this implementation, inflate() always flushes as much output as
425− possible to the output buffer, and always uses the faster approach on the
426− first call. So the only effect of the flush parameter in this implementation
427− is on the return value of inflate(), as noted below, or when it returns early
428− because Z_BLOCK is used.
429−
430− If a preset dictionary is needed after this call (see inflateSetDictionary
431− below), inflate sets strm->adler to the adler32 checksum of the dictionary
432− chosen by the compressor and returns Z_NEED_DICT; otherwise it sets
433− strm->adler to the adler32 checksum of all output produced so far (that is,
434− total_out bytes) and returns Z_OK, Z_STREAM_END or an error code as described
435− below. At the end of the stream, inflate() checks that its computed adler32
436− checksum is equal to that saved by the compressor and returns Z_STREAM_END
437− only if the checksum is correct.
438−
439− inflate() will decompress and check either zlib-wrapped or gzip-wrapped
440− deflate data. The header type is detected automatically. Any information
441− contained in the gzip header is not retained, so applications that need that
442− information should instead use raw inflate, see inflateInit2() below, or
443− inflateBack() and perform their own processing of the gzip header and
444− trailer.
445−
446− inflate() returns Z_OK if some progress has been made (more input processed
447− or more output produced), Z_STREAM_END if the end of the compressed data has
448− been reached and all uncompressed output has been produced, Z_NEED_DICT if a
449− preset dictionary is needed at this point, Z_DATA_ERROR if the input data was
450− corrupted (input stream not conforming to the zlib format or incorrect check
451− value), Z_STREAM_ERROR if the stream structure was inconsistent (for example
452− if next_in or next_out was NULL), Z_MEM_ERROR if there was not enough memory,
453− Z_BUF_ERROR if no progress is possible or if there was not enough room in the
454− output buffer when Z_FINISH is used. Note that Z_BUF_ERROR is not fatal, and
455− inflate() can be called again with more input and more output space to
456− continue decompressing. If Z_DATA_ERROR is returned, the application may then
457− call inflateSync() to look for a good compression block if a partial recovery
458− of the data is desired.
459−*/
460−
461−
462−ZEXTERN int ZEXPORT inflateEnd OF((z_streamp strm));
463−/*
464− All dynamically allocated data structures for this stream are freed.
465− This function discards any unprocessed input and does not flush any
466− pending output.
467−
468− inflateEnd returns Z_OK if success, Z_STREAM_ERROR if the stream state
469− was inconsistent. In the error case, msg may be set but then points to a
470− static string (which must not be deallocated).
471−*/
472−
473− /* Advanced functions */
474−
475−/*
476− The following functions are needed only in some special applications.
477−*/
478−
479−/*
480−ZEXTERN int ZEXPORT deflateInit2 OF((z_streamp strm,
481− int level,
482− int method,
483− int windowBits,
484− int memLevel,
485− int strategy));
486−
487− This is another version of deflateInit with more compression options. The
488− fields next_in, zalloc, zfree and opaque must be initialized before by
489− the caller.
490−
491− The method parameter is the compression method. It must be Z_DEFLATED in
492− this version of the library.
493−
494− The windowBits parameter is the base two logarithm of the window size
495− (the size of the history buffer). It should be in the range 8..15 for this
496− version of the library. Larger values of this parameter result in better
497− compression at the expense of memory usage. The default value is 15 if
498− deflateInit is used instead.
499−
500− windowBits can also be -8..-15 for raw deflate. In this case, -windowBits
501− determines the window size. deflate() will then generate raw deflate data
502− with no zlib header or trailer, and will not compute an adler32 check value.
503−
504− windowBits can also be greater than 15 for optional gzip encoding. Add
505− 16 to windowBits to write a simple gzip header and trailer around the
506− compressed data instead of a zlib wrapper. The gzip header will have no
507− file name, no extra data, no comment, no modification time (set to zero),
508− no header crc, and the operating system will be set to 255 (unknown). If a
509− gzip stream is being written, strm->adler is a crc32 instead of an adler32.
510−
511− The memLevel parameter specifies how much memory should be allocated
512− for the internal compression state. memLevel=1 uses minimum memory but
513− is slow and reduces compression ratio; memLevel=9 uses maximum memory
514− for optimal speed. The default value is 8. See zconf.h for total memory
515− usage as a function of windowBits and memLevel.
516−
517− The strategy parameter is used to tune the compression algorithm. Use the
518− value Z_DEFAULT_STRATEGY for normal data, Z_FILTERED for data produced by a
519− filter (or predictor), Z_HUFFMAN_ONLY to force Huffman encoding only (no
520− string match), or Z_RLE to limit match distances to one (run-length
521− encoding). Filtered data consists mostly of small values with a somewhat
522− random distribution. In this case, the compression algorithm is tuned to
523− compress them better. The effect of Z_FILTERED is to force more Huffman
524− coding and less string matching; it is somewhat intermediate between
525− Z_DEFAULT and Z_HUFFMAN_ONLY. Z_RLE is designed to be almost as fast as
526− Z_HUFFMAN_ONLY, but give better compression for PNG image data. The strategy
527− parameter only affects the compression ratio but not the correctness of the
528− compressed output even if it is not set appropriately. Z_FIXED prevents the
529− use of dynamic Huffman codes, allowing for a simpler decoder for special
530− applications.
531−
532− deflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
533− memory, Z_STREAM_ERROR if a parameter is invalid (such as an invalid
534− method). msg is set to null if there is no error message. deflateInit2 does
535− not perform any compression: this will be done by deflate().
536−*/
537−
538−ZEXTERN int ZEXPORT deflateSetDictionary OF((z_streamp strm,
539− const Bytef *dictionary,
540− uInt dictLength));
541−/*
542− Initializes the compression dictionary from the given byte sequence
543− without producing any compressed output. This function must be called
544− immediately after deflateInit, deflateInit2 or deflateReset, before any
545− call of deflate. The compressor and decompressor must use exactly the same
546− dictionary (see inflateSetDictionary).
547−
548− The dictionary should consist of strings (byte sequences) that are likely
549− to be encountered later in the data to be compressed, with the most commonly
550− used strings preferably put towards the end of the dictionary. Using a
551− dictionary is most useful when the data to be compressed is short and can be
552− predicted with good accuracy; the data can then be compressed better than
553− with the default empty dictionary.
554−
555− Depending on the size of the compression data structures selected by
556− deflateInit or deflateInit2, a part of the dictionary may in effect be
557− discarded, for example if the dictionary is larger than the window size in
558− deflate or deflate2. Thus the strings most likely to be useful should be
559− put at the end of the dictionary, not at the front. In addition, the
560− current implementation of deflate will use at most the window size minus
561− 262 bytes of the provided dictionary.
562−
563− Upon return of this function, strm->adler is set to the adler32 value
564− of the dictionary; the decompressor may later use this value to determine
565− which dictionary has been used by the compressor. (The adler32 value
566− applies to the whole dictionary even if only a subset of the dictionary is
567− actually used by the compressor.) If a raw deflate was requested, then the
568− adler32 value is not computed and strm->adler is not set.
569−
570− deflateSetDictionary returns Z_OK if success, or Z_STREAM_ERROR if a
571− parameter is invalid (such as NULL dictionary) or the stream state is
572− inconsistent (for example if deflate has already been called for this stream
573− or if the compression method is bsort). deflateSetDictionary does not
574− perform any compression: this will be done by deflate().
575−*/
576−
577−ZEXTERN int ZEXPORT deflateCopy OF((z_streamp dest,
578− z_streamp source));
579−/*
580− Sets the destination stream as a complete copy of the source stream.
581−
582− This function can be useful when several compression strategies will be
583− tried, for example when there are several ways of pre-processing the input
584− data with a filter. The streams that will be discarded should then be freed
585− by calling deflateEnd. Note that deflateCopy duplicates the internal
586− compression state which can be quite large, so this strategy is slow and
587− can consume lots of memory.
588−
589− deflateCopy returns Z_OK if success, Z_MEM_ERROR if there was not
590− enough memory, Z_STREAM_ERROR if the source stream state was inconsistent
591− (such as zalloc being NULL). msg is left unchanged in both source and
592− destination.
593−*/
594−
595−ZEXTERN int ZEXPORT deflateReset OF((z_streamp strm));
596−/*
597− This function is equivalent to deflateEnd followed by deflateInit,
598− but does not free and reallocate all the internal compression state.
599− The stream will keep the same compression level and any other attributes
600− that may have been set by deflateInit2.
601−
602− deflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source
603− stream state was inconsistent (such as zalloc or state being NULL).
604−*/
605−
606−ZEXTERN int ZEXPORT deflateParams OF((z_streamp strm,
607− int level,
608− int strategy));
609−/*
610− Dynamically update the compression level and compression strategy. The
611− interpretation of level and strategy is as in deflateInit2. This can be
612− used to switch between compression and straight copy of the input data, or
613− to switch to a different kind of input data requiring a different
614− strategy. If the compression level is changed, the input available so far
615− is compressed with the old level (and may be flushed); the new level will
616− take effect only at the next call of deflate().
617−
618− Before the call of deflateParams, the stream state must be set as for
619− a call of deflate(), since the currently available input may have to
620− be compressed and flushed. In particular, strm->avail_out must be non-zero.
621−
622− deflateParams returns Z_OK if success, Z_STREAM_ERROR if the source
623− stream state was inconsistent or if a parameter was invalid, Z_BUF_ERROR
624− if strm->avail_out was zero.
625−*/
626−
627−ZEXTERN int ZEXPORT deflateTune OF((z_streamp strm,
628− int good_length,
629− int max_lazy,
630− int nice_length,
631− int max_chain));
632−/*
633− Fine tune deflate's internal compression parameters. This should only be
634− used by someone who understands the algorithm used by zlib's deflate for
635− searching for the best matching string, and even then only by the most
636− fanatic optimizer trying to squeeze out the last compressed bit for their
637− specific input data. Read the deflate.c source code for the meaning of the
638− max_lazy, good_length, nice_length, and max_chain parameters.
639−
640− deflateTune() can be called after deflateInit() or deflateInit2(), and
641− returns Z_OK on success, or Z_STREAM_ERROR for an invalid deflate stream.
642− */
643−
644−ZEXTERN uLong ZEXPORT deflateBound OF((z_streamp strm,
645− uLong sourceLen));
646−/*
647− deflateBound() returns an upper bound on the compressed size after
648− deflation of sourceLen bytes. It must be called after deflateInit()
649− or deflateInit2(). This would be used to allocate an output buffer
650− for deflation in a single pass, and so would be called before deflate().
651−*/
652−
653−ZEXTERN int ZEXPORT deflatePrime OF((z_streamp strm,
654− int bits,
655− int value));
656−/*
657− deflatePrime() inserts bits in the deflate output stream. The intent
658− is that this function is used to start off the deflate output with the
659− bits leftover from a previous deflate stream when appending to it. As such,
660− this function can only be used for raw deflate, and must be used before the
661− first deflate() call after a deflateInit2() or deflateReset(). bits must be
662− less than or equal to 16, and that many of the least significant bits of
663− value will be inserted in the output.
664−
665− deflatePrime returns Z_OK if success, or Z_STREAM_ERROR if the source
666− stream state was inconsistent.
667−*/
668−
669−ZEXTERN int ZEXPORT deflateSetHeader OF((z_streamp strm,
670− gz_headerp head));
671−/*
672− deflateSetHeader() provides gzip header information for when a gzip
673− stream is requested by deflateInit2(). deflateSetHeader() may be called
674− after deflateInit2() or deflateReset() and before the first call of
675− deflate(). The text, time, os, extra field, name, and comment information
676− in the provided gz_header structure are written to the gzip header (xflag is
677− ignored -- the extra flags are set according to the compression level). The
678− caller must assure that, if not Z_NULL, name and comment are terminated with
679− a zero byte, and that if extra is not Z_NULL, that extra_len bytes are
680− available there. If hcrc is true, a gzip header crc is included. Note that
681− the current versions of the command-line version of gzip (up through version
682− 1.3.x) do not support header crc's, and will report that it is a "multi-part
683− gzip file" and give up.
684−
685− If deflateSetHeader is not used, the default gzip header has text false,
686− the time set to zero, and os set to 255, with no extra, name, or comment
687− fields. The gzip header is returned to the default state by deflateReset().
688−
689− deflateSetHeader returns Z_OK if success, or Z_STREAM_ERROR if the source
690− stream state was inconsistent.
691−*/
692−
693−/*
694−ZEXTERN int ZEXPORT inflateInit2 OF((z_streamp strm,
695− int windowBits));
696−
697− This is another version of inflateInit with an extra parameter. The
698− fields next_in, avail_in, zalloc, zfree and opaque must be initialized
699− before by the caller.
700−
701− The windowBits parameter is the base two logarithm of the maximum window
702− size (the size of the history buffer). It should be in the range 8..15 for
703− this version of the library. The default value is 15 if inflateInit is used
704− instead. windowBits must be greater than or equal to the windowBits value
705− provided to deflateInit2() while compressing, or it must be equal to 15 if
706− deflateInit2() was not used. If a compressed stream with a larger window
707− size is given as input, inflate() will return with the error code
708− Z_DATA_ERROR instead of trying to allocate a larger window.
709−
710− windowBits can also be -8..-15 for raw inflate. In this case, -windowBits
711− determines the window size. inflate() will then process raw deflate data,
712− not looking for a zlib or gzip header, not generating a check value, and not
713− looking for any check values for comparison at the end of the stream. This
714− is for use with other formats that use the deflate compressed data format
715− such as zip. Those formats provide their own check values. If a custom
716− format is developed using the raw deflate format for compressed data, it is
717− recommended that a check value such as an adler32 or a crc32 be applied to
718− the uncompressed data as is done in the zlib, gzip, and zip formats. For
719− most applications, the zlib format should be used as is. Note that comments
720− above on the use in deflateInit2() applies to the magnitude of windowBits.
721−
722− windowBits can also be greater than 15 for optional gzip decoding. Add
723− 32 to windowBits to enable zlib and gzip decoding with automatic header
724− detection, or add 16 to decode only the gzip format (the zlib format will
725− return a Z_DATA_ERROR). If a gzip stream is being decoded, strm->adler is
726− a crc32 instead of an adler32.
727−
728− inflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
729− memory, Z_STREAM_ERROR if a parameter is invalid (such as a null strm). msg
730− is set to null if there is no error message. inflateInit2 does not perform
731− any decompression apart from reading the zlib header if present: this will
732− be done by inflate(). (So next_in and avail_in may be modified, but next_out
733− and avail_out are unchanged.)
734−*/
735−
736−ZEXTERN int ZEXPORT inflateSetDictionary OF((z_streamp strm,
737− const Bytef *dictionary,
738− uInt dictLength));
739−/*
740− Initializes the decompression dictionary from the given uncompressed byte
741− sequence. This function must be called immediately after a call of inflate,
742− if that call returned Z_NEED_DICT. The dictionary chosen by the compressor
743− can be determined from the adler32 value returned by that call of inflate.
744− The compressor and decompressor must use exactly the same dictionary (see
745− deflateSetDictionary). For raw inflate, this function can be called
746− immediately after inflateInit2() or inflateReset() and before any call of
747− inflate() to set the dictionary. The application must insure that the
748− dictionary that was used for compression is provided.
749−
750− inflateSetDictionary returns Z_OK if success, Z_STREAM_ERROR if a
751− parameter is invalid (such as NULL dictionary) or the stream state is
752− inconsistent, Z_DATA_ERROR if the given dictionary doesn't match the
753− expected one (incorrect adler32 value). inflateSetDictionary does not
754− perform any decompression: this will be done by subsequent calls of
755− inflate().
756−*/
757−
758−ZEXTERN int ZEXPORT inflateSync OF((z_streamp strm));
759−/*
760− Skips invalid compressed data until a full flush point (see above the
761− description of deflate with Z_FULL_FLUSH) can be found, or until all
762− available input is skipped. No output is provided.
763−
764− inflateSync returns Z_OK if a full flush point has been found, Z_BUF_ERROR
765− if no more input was provided, Z_DATA_ERROR if no flush point has been found,
766− or Z_STREAM_ERROR if the stream structure was inconsistent. In the success
767− case, the application may save the current current value of total_in which
768− indicates where valid compressed data was found. In the error case, the
769− application may repeatedly call inflateSync, providing more input each time,
770− until success or end of the input data.
771−*/
772−
773−ZEXTERN int ZEXPORT inflateCopy OF((z_streamp dest,
774− z_streamp source));
775−/*
776− Sets the destination stream as a complete copy of the source stream.
777−
778− This function can be useful when randomly accessing a large stream. The
779− first pass through the stream can periodically record the inflate state,
780− allowing restarting inflate at those points when randomly accessing the
781− stream.
782−
783− inflateCopy returns Z_OK if success, Z_MEM_ERROR if there was not
784− enough memory, Z_STREAM_ERROR if the source stream state was inconsistent
785− (such as zalloc being NULL). msg is left unchanged in both source and
786− destination.
787−*/
788−
789−ZEXTERN int ZEXPORT inflateReset OF((z_streamp strm));
790−/*
791− This function is equivalent to inflateEnd followed by inflateInit,
792− but does not free and reallocate all the internal decompression state.
793− The stream will keep attributes that may have been set by inflateInit2.
794−
795− inflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source
796− stream state was inconsistent (such as zalloc or state being NULL).
797−*/
798−
799−ZEXTERN int ZEXPORT inflatePrime OF((z_streamp strm,
800− int bits,
801− int value));
802−/*
803− This function inserts bits in the inflate input stream. The intent is
804− that this function is used to start inflating at a bit position in the
805− middle of a byte. The provided bits will be used before any bytes are used
806− from next_in. This function should only be used with raw inflate, and
807− should be used before the first inflate() call after inflateInit2() or
808− inflateReset(). bits must be less than or equal to 16, and that many of the
809− least significant bits of value will be inserted in the input.
810−
811− inflatePrime returns Z_OK if success, or Z_STREAM_ERROR if the source
812− stream state was inconsistent.
813−*/
814−
815−ZEXTERN int ZEXPORT inflateGetHeader OF((z_streamp strm,
816− gz_headerp head));
817−/*
818− inflateGetHeader() requests that gzip header information be stored in the
819− provided gz_header structure. inflateGetHeader() may be called after
820− inflateInit2() or inflateReset(), and before the first call of inflate().
821− As inflate() processes the gzip stream, head->done is zero until the header
822− is completed, at which time head->done is set to one. If a zlib stream is
823− being decoded, then head->done is set to -1 to indicate that there will be
824− no gzip header information forthcoming. Note that Z_BLOCK can be used to
825− force inflate() to return immediately after header processing is complete
826− and before any actual data is decompressed.
827−
828− The text, time, xflags, and os fields are filled in with the gzip header
829− contents. hcrc is set to true if there is a header CRC. (The header CRC
830− was valid if done is set to one.) If extra is not Z_NULL, then extra_max
831− contains the maximum number of bytes to write to extra. Once done is true,
832− extra_len contains the actual extra field length, and extra contains the
833− extra field, or that field truncated if extra_max is less than extra_len.
834− If name is not Z_NULL, then up to name_max characters are written there,
835− terminated with a zero unless the length is greater than name_max. If
836− comment is not Z_NULL, then up to comm_max characters are written there,
837− terminated with a zero unless the length is greater than comm_max. When
838− any of extra, name, or comment are not Z_NULL and the respective field is
839− not present in the header, then that field is set to Z_NULL to signal its
840− absence. This allows the use of deflateSetHeader() with the returned
841− structure to duplicate the header. However if those fields are set to
842− allocated memory, then the application will need to save those pointers
843− elsewhere so that they can be eventually freed.
844−
845− If inflateGetHeader is not used, then the header information is simply
846− discarded. The header is always checked for validity, including the header
847− CRC if present. inflateReset() will reset the process to discard the header
848− information. The application would need to call inflateGetHeader() again to
849− retrieve the header from the next gzip stream.
850−
851− inflateGetHeader returns Z_OK if success, or Z_STREAM_ERROR if the source
852− stream state was inconsistent.
853−*/
854−
855−/*
856−ZEXTERN int ZEXPORT inflateBackInit OF((z_streamp strm, int windowBits,
857− unsigned char FAR *window));
858−
859− Initialize the internal stream state for decompression using inflateBack()
860− calls. The fields zalloc, zfree and opaque in strm must be initialized
861− before the call. If zalloc and zfree are Z_NULL, then the default library-
862− derived memory allocation routines are used. windowBits is the base two
863− logarithm of the window size, in the range 8..15. window is a caller
864− supplied buffer of that size. Except for special applications where it is
865− assured that deflate was used with small window sizes, windowBits must be 15
866− and a 32K byte window must be supplied to be able to decompress general
867− deflate streams.
868−
869− See inflateBack() for the usage of these routines.
870−
871− inflateBackInit will return Z_OK on success, Z_STREAM_ERROR if any of
872− the paramaters are invalid, Z_MEM_ERROR if the internal state could not
873− be allocated, or Z_VERSION_ERROR if the version of the library does not
874− match the version of the header file.
875−*/
876−
877−typedef unsigned (*in_func) OF((void FAR *, unsigned char FAR * FAR *));
878−typedef int (*out_func) OF((void FAR *, unsigned char FAR *, unsigned));
879−
880−ZEXTERN int ZEXPORT inflateBack OF((z_streamp strm,
881− in_func in, void FAR *in_desc,
882− out_func out, void FAR *out_desc));
883−/*
884− inflateBack() does a raw inflate with a single call using a call-back
885− interface for input and output. This is more efficient than inflate() for
886− file i/o applications in that it avoids copying between the output and the
887− sliding window by simply making the window itself the output buffer. This
888− function trusts the application to not change the output buffer passed by
889− the output function, at least until inflateBack() returns.
890−
891− inflateBackInit() must be called first to allocate the internal state
892− and to initialize the state with the user-provided window buffer.
893− inflateBack() may then be used multiple times to inflate a complete, raw
894− deflate stream with each call. inflateBackEnd() is then called to free
895− the allocated state.
896−
897− A raw deflate stream is one with no zlib or gzip header or trailer.
898− This routine would normally be used in a utility that reads zip or gzip
899− files and writes out uncompressed files. The utility would decode the
900− header and process the trailer on its own, hence this routine expects
901− only the raw deflate stream to decompress. This is different from the
902− normal behavior of inflate(), which expects either a zlib or gzip header and
903− trailer around the deflate stream.
904−
905− inflateBack() uses two subroutines supplied by the caller that are then
906− called by inflateBack() for input and output. inflateBack() calls those
907− routines until it reads a complete deflate stream and writes out all of the
908− uncompressed data, or until it encounters an error. The function's
909− parameters and return types are defined above in the in_func and out_func
910− typedefs. inflateBack() will call in(in_desc, &buf) which should return the
911− number of bytes of provided input, and a pointer to that input in buf. If
912− there is no input available, in() must return zero--buf is ignored in that
913− case--and inflateBack() will return a buffer error. inflateBack() will call
914− out(out_desc, buf, len) to write the uncompressed data buf[0..len-1]. out()
915− should return zero on success, or non-zero on failure. If out() returns
916− non-zero, inflateBack() will return with an error. Neither in() nor out()
917− are permitted to change the contents of the window provided to
918− inflateBackInit(), which is also the buffer that out() uses to write from.
919− The length written by out() will be at most the window size. Any non-zero
920− amount of input may be provided by in().
921−
922− For convenience, inflateBack() can be provided input on the first call by
923− setting strm->next_in and strm->avail_in. If that input is exhausted, then
924− in() will be called. Therefore strm->next_in must be initialized before
925− calling inflateBack(). If strm->next_in is Z_NULL, then in() will be called
926− immediately for input. If strm->next_in is not Z_NULL, then strm->avail_in
927− must also be initialized, and then if strm->avail_in is not zero, input will
928− initially be taken from strm->next_in[0 .. strm->avail_in - 1].
929−
930− The in_desc and out_desc parameters of inflateBack() is passed as the
931− first parameter of in() and out() respectively when they are called. These
932− descriptors can be optionally used to pass any information that the caller-
933− supplied in() and out() functions need to do their job.
934−
935− On return, inflateBack() will set strm->next_in and strm->avail_in to
936− pass back any unused input that was provided by the last in() call. The
937− return values of inflateBack() can be Z_STREAM_END on success, Z_BUF_ERROR
938− if in() or out() returned an error, Z_DATA_ERROR if there was a format
939− error in the deflate stream (in which case strm->msg is set to indicate the
940− nature of the error), or Z_STREAM_ERROR if the stream was not properly
941− initialized. In the case of Z_BUF_ERROR, an input or output error can be
942− distinguished using strm->next_in which will be Z_NULL only if in() returned
943− an error. If strm->next is not Z_NULL, then the Z_BUF_ERROR was due to
944− out() returning non-zero. (in() will always be called before out(), so
945− strm->next_in is assured to be defined if out() returns non-zero.) Note
946− that inflateBack() cannot return Z_OK.
947−*/
948−
949−ZEXTERN int ZEXPORT inflateBackEnd OF((z_streamp strm));
950−/*
951− All memory allocated by inflateBackInit() is freed.
952−
953− inflateBackEnd() returns Z_OK on success, or Z_STREAM_ERROR if the stream
954− state was inconsistent.
955−*/
956−
957−ZEXTERN uLong ZEXPORT zlibCompileFlags OF((void));
958−/* Return flags indicating compile-time options.
959−
960− Type sizes, two bits each, 00 = 16 bits, 01 = 32, 10 = 64, 11 = other:
961− 1.0: size of uInt
962− 3.2: size of uLong
963− 5.4: size of voidpf (pointer)
964− 7.6: size of z_off_t
965−
966− Compiler, assembler, and debug options:
967− 8: DEBUG
968− 9: ASMV or ASMINF -- use ASM code
969− 10: ZLIB_WINAPI -- exported functions use the WINAPI calling convention
970− 11: 0 (reserved)
971−
972− One-time table building (smaller code, but not thread-safe if true):
973− 12: BUILDFIXED -- build static block decoding tables when needed
974− 13: DYNAMIC_CRC_TABLE -- build CRC calculation tables when needed
975− 14,15: 0 (reserved)
976−
977− Library content (indicates missing functionality):
978− 16: NO_GZCOMPRESS -- gz* functions cannot compress (to avoid linking
979− deflate code when not needed)
980− 17: NO_GZIP -- deflate can't write gzip streams, and inflate can't detect
981− and decode gzip streams (to avoid linking crc code)
982− 18-19: 0 (reserved)
983−
984− Operation variations (changes in library functionality):
985− 20: PKZIP_BUG_WORKAROUND -- slightly more permissive inflate
986− 21: FASTEST -- deflate algorithm with only one, lowest compression level
987− 22,23: 0 (reserved)
988−
989− The sprintf variant used by gzprintf (zero is best):
990− 24: 0 = vs*, 1 = s* -- 1 means limited to 20 arguments after the format
991− 25: 0 = *nprintf, 1 = *printf -- 1 means gzprintf() not secure!
992− 26: 0 = returns value, 1 = void -- 1 means inferred string length returned
993−
994− Remainder:
995− 27-31: 0 (reserved)
996− */
997−
998−
999− /* utility functions */
1000−
1001−/*
1002− The following utility functions are implemented on top of the
1003− basic stream-oriented functions. To simplify the interface, some
1004− default options are assumed (compression level and memory usage,
1005− standard memory allocation functions). The source code of these
1006− utility functions can easily be modified if you need special options.
1007−*/
1008−
1009−ZEXTERN int ZEXPORT compress OF((Bytef *dest, uLongf *destLen,
1010− const Bytef *source, uLong sourceLen));
1011−/*
1012− Compresses the source buffer into the destination buffer. sourceLen is
1013− the byte length of the source buffer. Upon entry, destLen is the total
1014− size of the destination buffer, which must be at least the value returned
1015− by compressBound(sourceLen). Upon exit, destLen is the actual size of the
1016− compressed buffer.
1017− This function can be used to compress a whole file at once if the
1018− input file is mmap'ed.
1019− compress returns Z_OK if success, Z_MEM_ERROR if there was not
1020− enough memory, Z_BUF_ERROR if there was not enough room in the output
1021− buffer.
1022−*/
1023−
1024−ZEXTERN int ZEXPORT compress2 OF((Bytef *dest, uLongf *destLen,
1025− const Bytef *source, uLong sourceLen,
1026− int level));
1027−/*
1028− Compresses the source buffer into the destination buffer. The level
1029− parameter has the same meaning as in deflateInit. sourceLen is the byte
1030− length of the source buffer. Upon entry, destLen is the total size of the
1031− destination buffer, which must be at least the value returned by
1032− compressBound(sourceLen). Upon exit, destLen is the actual size of the
1033− compressed buffer.
1034−
1035− compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
1036− memory, Z_BUF_ERROR if there was not enough room in the output buffer,
1037− Z_STREAM_ERROR if the level parameter is invalid.
1038−*/
1039−
1040−ZEXTERN uLong ZEXPORT compressBound OF((uLong sourceLen));
1041−/*
1042− compressBound() returns an upper bound on the compressed size after
1043− compress() or compress2() on sourceLen bytes. It would be used before
1044− a compress() or compress2() call to allocate the destination buffer.
1045−*/
1046−
1047−ZEXTERN int ZEXPORT uncompress OF((Bytef *dest, uLongf *destLen,
1048− const Bytef *source, uLong sourceLen));
1049−/*
1050− Decompresses the source buffer into the destination buffer. sourceLen is
1051− the byte length of the source buffer. Upon entry, destLen is the total
1052− size of the destination buffer, which must be large enough to hold the
1053− entire uncompressed data. (The size of the uncompressed data must have
1054− been saved previously by the compressor and transmitted to the decompressor
1055− by some mechanism outside the scope of this compression library.)
1056− Upon exit, destLen is the actual size of the compressed buffer.
1057− This function can be used to decompress a whole file at once if the
1058− input file is mmap'ed.
1059−
1060− uncompress returns Z_OK if success, Z_MEM_ERROR if there was not
1061− enough memory, Z_BUF_ERROR if there was not enough room in the output
1062− buffer, or Z_DATA_ERROR if the input data was corrupted or incomplete.
1063−*/
1064−
1065−
1066−typedef voidp gzFile;
1067−
1068−ZEXTERN gzFile ZEXPORT gzopen OF((const char *path, const char *mode));
1069−/*
1070− Opens a gzip (.gz) file for reading or writing. The mode parameter
1071− is as in fopen ("rb" or "wb") but can also include a compression level
1072− ("wb9") or a strategy: 'f' for filtered data as in "wb6f", 'h' for
1073− Huffman only compression as in "wb1h", or 'R' for run-length encoding
1074− as in "wb1R". (See the description of deflateInit2 for more information
1075− about the strategy parameter.)
1076−
1077− gzopen can be used to read a file which is not in gzip format; in this
1078− case gzread will directly read from the file without decompression.
1079−
1080− gzopen returns NULL if the file could not be opened or if there was
1081− insufficient memory to allocate the (de)compression state; errno
1082− can be checked to distinguish the two cases (if errno is zero, the
1083− zlib error is Z_MEM_ERROR). */
1084−
1085−ZEXTERN gzFile ZEXPORT gzdopen OF((int fd, const char *mode));
1086−/*
1087− gzdopen() associates a gzFile with the file descriptor fd. File
1088− descriptors are obtained from calls like open, dup, creat, pipe or
1089− fileno (in the file has been previously opened with fopen).
1090− The mode parameter is as in gzopen.
1091− The next call of gzclose on the returned gzFile will also close the
1092− file descriptor fd, just like fclose(fdopen(fd), mode) closes the file
1093− descriptor fd. If you want to keep fd open, use gzdopen(dup(fd), mode).
1094− gzdopen returns NULL if there was insufficient memory to allocate
1095− the (de)compression state.
1096−*/
1097−
1098−ZEXTERN int ZEXPORT gzsetparams OF((gzFile file, int level, int strategy));
1099−/*
1100− Dynamically update the compression level or strategy. See the description
1101− of deflateInit2 for the meaning of these parameters.
1102− gzsetparams returns Z_OK if success, or Z_STREAM_ERROR if the file was not
1103− opened for writing.
1104−*/
1105−
1106−ZEXTERN int ZEXPORT gzread OF((gzFile file, voidp buf, unsigned len));
1107−/*
1108− Reads the given number of uncompressed bytes from the compressed file.
1109− If the input file was not in gzip format, gzread copies the given number
1110− of bytes into the buffer.
1111− gzread returns the number of uncompressed bytes actually read (0 for
1112− end of file, -1 for error). */
1113−
1114−ZEXTERN int ZEXPORT gzwrite OF((gzFile file,
1115− voidpc buf, unsigned len));
1116−/*
1117− Writes the given number of uncompressed bytes into the compressed file.
1118− gzwrite returns the number of uncompressed bytes actually written
1119− (0 in case of error).
1120−*/
1121−
1122−ZEXTERN int ZEXPORTVA gzprintf OF((gzFile file, const char *format, ...));
1123−/*
1124− Converts, formats, and writes the args to the compressed file under
1125− control of the format string, as in fprintf. gzprintf returns the number of
1126− uncompressed bytes actually written (0 in case of error). The number of
1127− uncompressed bytes written is limited to 4095. The caller should assure that
1128− this limit is not exceeded. If it is exceeded, then gzprintf() will return
1129− return an error (0) with nothing written. In this case, there may also be a
1130− buffer overflow with unpredictable consequences, which is possible only if
1131− zlib was compiled with the insecure functions sprintf() or vsprintf()
1132− because the secure snprintf() or vsnprintf() functions were not available.
1133−*/
1134−
1135−ZEXTERN int ZEXPORT gzputs OF((gzFile file, const char *s));
1136−/*
1137− Writes the given null-terminated string to the compressed file, excluding
1138− the terminating null character.
1139− gzputs returns the number of characters written, or -1 in case of error.
1140−*/
1141−
1142−ZEXTERN char * ZEXPORT gzgets OF((gzFile file, char *buf, int len));
1143−/*
1144− Reads bytes from the compressed file until len-1 characters are read, or
1145− a newline character is read and transferred to buf, or an end-of-file
1146− condition is encountered. The string is then terminated with a null
1147− character.
1148− gzgets returns buf, or Z_NULL in case of error.
1149−*/
1150−
1151−ZEXTERN int ZEXPORT gzputc OF((gzFile file, int c));
1152−/*
1153− Writes c, converted to an unsigned char, into the compressed file.
1154− gzputc returns the value that was written, or -1 in case of error.
1155−*/
1156−
1157−ZEXTERN int ZEXPORT gzgetc OF((gzFile file));
1158−/*
1159− Reads one byte from the compressed file. gzgetc returns this byte
1160− or -1 in case of end of file or error.
1161−*/
1162−
1163−ZEXTERN int ZEXPORT gzungetc OF((int c, gzFile file));
1164−/*
1165− Push one character back onto the stream to be read again later.
1166− Only one character of push-back is allowed. gzungetc() returns the
1167− character pushed, or -1 on failure. gzungetc() will fail if a
1168− character has been pushed but not read yet, or if c is -1. The pushed
1169− character will be discarded if the stream is repositioned with gzseek()
1170− or gzrewind().
1171−*/
1172−
1173−ZEXTERN int ZEXPORT gzflush OF((gzFile file, int flush));
1174−/*
1175− Flushes all pending output into the compressed file. The parameter
1176− flush is as in the deflate() function. The return value is the zlib
1177− error number (see function gzerror below). gzflush returns Z_OK if
1178− the flush parameter is Z_FINISH and all output could be flushed.
1179− gzflush should be called only when strictly necessary because it can
1180− degrade compression.
1181−*/
1182−
1183−ZEXTERN z_off_t ZEXPORT gzseek OF((gzFile file,
1184− z_off_t offset, int whence));
1185−/*
1186− Sets the starting position for the next gzread or gzwrite on the
1187− given compressed file. The offset represents a number of bytes in the
1188− uncompressed data stream. The whence parameter is defined as in lseek(2);
1189− the value SEEK_END is not supported.
1190− If the file is opened for reading, this function is emulated but can be
1191− extremely slow. If the file is opened for writing, only forward seeks are
1192− supported; gzseek then compresses a sequence of zeroes up to the new
1193− starting position.
1194−
1195− gzseek returns the resulting offset location as measured in bytes from
1196− the beginning of the uncompressed stream, or -1 in case of error, in
1197− particular if the file is opened for writing and the new starting position
1198− would be before the current position.
1199−*/
1200−
1201−ZEXTERN int ZEXPORT gzrewind OF((gzFile file));
1202−/*
1203− Rewinds the given file. This function is supported only for reading.
1204−
1205− gzrewind(file) is equivalent to (int)gzseek(file, 0L, SEEK_SET)
1206−*/
1207−
1208−ZEXTERN z_off_t ZEXPORT gztell OF((gzFile file));
1209−/*
1210− Returns the starting position for the next gzread or gzwrite on the
1211− given compressed file. This position represents a number of bytes in the
1212− uncompressed data stream.
1213−
1214− gztell(file) is equivalent to gzseek(file, 0L, SEEK_CUR)
1215−*/
1216−
1217−ZEXTERN int ZEXPORT gzeof OF((gzFile file));
1218−/*
1219− Returns 1 when EOF has previously been detected reading the given
1220− input stream, otherwise zero.
1221−*/
1222−
1223−ZEXTERN int ZEXPORT gzdirect OF((gzFile file));
1224−/*
1225− Returns 1 if file is being read directly without decompression, otherwise
1226− zero.
1227−*/
1228−
1229−ZEXTERN int ZEXPORT gzclose OF((gzFile file));
1230−/*
1231− Flushes all pending output if necessary, closes the compressed file
1232− and deallocates all the (de)compression state. The return value is the zlib
1233− error number (see function gzerror below).
1234−*/
1235−
1236−ZEXTERN const char * ZEXPORT gzerror OF((gzFile file, int *errnum));
1237−/*
1238− Returns the error message for the last error which occurred on the
1239− given compressed file. errnum is set to zlib error number. If an
1240− error occurred in the file system and not in the compression library,
1241− errnum is set to Z_ERRNO and the application may consult errno
1242− to get the exact error code.
1243−*/
1244−
1245−ZEXTERN void ZEXPORT gzclearerr OF((gzFile file));
1246−/*
1247− Clears the error and end-of-file flags for file. This is analogous to the
1248− clearerr() function in stdio. This is useful for continuing to read a gzip
1249− file that is being written concurrently.
1250−*/
1251−
1252− /* checksum functions */
1253−
1254−/*
1255− These functions are not related to compression but are exported
1256− anyway because they might be useful in applications using the
1257− compression library.
1258−*/
1259−
1260−ZEXTERN uLong ZEXPORT adler32 OF((uLong adler, const Bytef *buf, uInt len));
1261−/*
1262− Update a running Adler-32 checksum with the bytes buf[0..len-1] and
1263− return the updated checksum. If buf is NULL, this function returns
1264− the required initial value for the checksum.
1265− An Adler-32 checksum is almost as reliable as a CRC32 but can be computed
1266− much faster. Usage example:
1267−
1268− uLong adler = adler32(0L, Z_NULL, 0);
1269−
1270− while (read_buffer(buffer, length) != EOF) {
1271− adler = adler32(adler, buffer, length);
1272− }
1273− if (adler != original_adler) error();
1274−*/
1275−
1276−ZEXTERN uLong ZEXPORT adler32_combine OF((uLong adler1, uLong adler2,
1277− z_off_t len2));
1278−/*
1279− Combine two Adler-32 checksums into one. For two sequences of bytes, seq1
1280− and seq2 with lengths len1 and len2, Adler-32 checksums were calculated for
1281− each, adler1 and adler2. adler32_combine() returns the Adler-32 checksum of
1282− seq1 and seq2 concatenated, requiring only adler1, adler2, and len2.
1283−*/
1284−
1285−ZEXTERN uLong ZEXPORT crc32 OF((uLong crc, const Bytef *buf, uInt len));
1286−/*
1287− Update a running CRC-32 with the bytes buf[0..len-1] and return the
1288− updated CRC-32. If buf is NULL, this function returns the required initial
1289− value for the for the crc. Pre- and post-conditioning (one's complement) is
1290− performed within this function so it shouldn't be done by the application.
1291− Usage example:
1292−
1293− uLong crc = crc32(0L, Z_NULL, 0);
1294−
1295− while (read_buffer(buffer, length) != EOF) {
1296− crc = crc32(crc, buffer, length);
1297− }
1298− if (crc != original_crc) error();
1299−*/
1300−
1301−ZEXTERN uLong ZEXPORT crc32_combine OF((uLong crc1, uLong crc2, z_off_t len2));
1302−
1303−/*
1304− Combine two CRC-32 check values into one. For two sequences of bytes,
1305− seq1 and seq2 with lengths len1 and len2, CRC-32 check values were
1306− calculated for each, crc1 and crc2. crc32_combine() returns the CRC-32
1307− check value of seq1 and seq2 concatenated, requiring only crc1, crc2, and
1308− len2.
1309−*/
1310−
1311−
1312− /* various hacks, don't look :) */
1313−
1314−/* deflateInit and inflateInit are macros to allow checking the zlib version
1315− * and the compiler's view of z_stream:
1316− */
1317−ZEXTERN int ZEXPORT deflateInit_ OF((z_streamp strm, int level,
1318− const char *version, int stream_size));
1319−ZEXTERN int ZEXPORT inflateInit_ OF((z_streamp strm,
1320− const char *version, int stream_size));
1321−ZEXTERN int ZEXPORT deflateInit2_ OF((z_streamp strm, int level, int method,
1322− int windowBits, int memLevel,
1323− int strategy, const char *version,
1324− int stream_size));
1325−ZEXTERN int ZEXPORT inflateInit2_ OF((z_streamp strm, int windowBits,
1326− const char *version, int stream_size));
1327−ZEXTERN int ZEXPORT inflateBackInit_ OF((z_streamp strm, int windowBits,
1328− unsigned char FAR *window,
1329− const char *version,
1330− int stream_size));
1331−#define deflateInit(strm, level) \
1332− deflateInit_((strm), (level), ZLIB_VERSION, sizeof(z_stream))
1333−#define inflateInit(strm) \
1334− inflateInit_((strm), ZLIB_VERSION, sizeof(z_stream))
1335−#define deflateInit2(strm, level, method, windowBits, memLevel, strategy) \
1336− deflateInit2_((strm),(level),(method),(windowBits),(memLevel),\
1337− (strategy), ZLIB_VERSION, sizeof(z_stream))
1338−#define inflateInit2(strm, windowBits) \
1339− inflateInit2_((strm), (windowBits), ZLIB_VERSION, sizeof(z_stream))
1340−#define inflateBackInit(strm, windowBits, window) \
1341− inflateBackInit_((strm), (windowBits), (window), \
1342− ZLIB_VERSION, sizeof(z_stream))
1343−
1344−
1345−#if !defined(ZUTIL_H) && !defined(NO_DUMMY_DECL)
1346− struct internal_state {int dummy;}; /* hack for buggy compilers */
1347−#endif
1348−
1349−ZEXTERN const char * ZEXPORT zError OF((int));
1350−ZEXTERN int ZEXPORT inflateSyncPoint OF((z_streamp z));
1351−ZEXTERN const uLongf * ZEXPORT get_crc_table OF((void));
1352−
1353−#ifdef __cplusplus
1354−}
1355−#endif
1356−
1357−#endif /* ZLIB_H */
cbits/zutil.cdeleted−318
@@ -1,318 +0,0 @@
1−/* zutil.c -- target dependent utility functions for the compression library
2− * Copyright (C) 1995-2005 Jean-loup Gailly.
3− * For conditions of distribution and use, see copyright notice in zlib.h
4− */
5−
6−/* @(#) $Id$ */
7−
8−#include "zutil.h"
9−
10−#ifndef NO_DUMMY_DECL
11−struct internal_state {int dummy;}; /* for buggy compilers */
12−#endif
13−
14−const char * const z_errmsg[10] = {
15−"need dictionary", /* Z_NEED_DICT 2 */
16−"stream end", /* Z_STREAM_END 1 */
17−"", /* Z_OK 0 */
18−"file error", /* Z_ERRNO (-1) */
19−"stream error", /* Z_STREAM_ERROR (-2) */
20−"data error", /* Z_DATA_ERROR (-3) */
21−"insufficient memory", /* Z_MEM_ERROR (-4) */
22−"buffer error", /* Z_BUF_ERROR (-5) */
23−"incompatible version",/* Z_VERSION_ERROR (-6) */
24−""};
25−
26−
27−const char * ZEXPORT zlibVersion()
28−{
29− return ZLIB_VERSION;
30−}
31−
32−uLong ZEXPORT zlibCompileFlags()
33−{
34− uLong flags;
35−
36− flags = 0;
37− switch (sizeof(uInt)) {
38− case 2: break;
39− case 4: flags += 1; break;
40− case 8: flags += 2; break;
41− default: flags += 3;
42− }
43− switch (sizeof(uLong)) {
44− case 2: break;
45− case 4: flags += 1 << 2; break;
46− case 8: flags += 2 << 2; break;
47− default: flags += 3 << 2;
48− }
49− switch (sizeof(voidpf)) {
50− case 2: break;
51− case 4: flags += 1 << 4; break;
52− case 8: flags += 2 << 4; break;
53− default: flags += 3 << 4;
54− }
55− switch (sizeof(z_off_t)) {
56− case 2: break;
57− case 4: flags += 1 << 6; break;
58− case 8: flags += 2 << 6; break;
59− default: flags += 3 << 6;
60− }
61−#ifdef DEBUG
62− flags += 1 << 8;
63−#endif
64−#if defined(ASMV) || defined(ASMINF)
65− flags += 1 << 9;
66−#endif
67−#ifdef ZLIB_WINAPI
68− flags += 1 << 10;
69−#endif
70−#ifdef BUILDFIXED
71− flags += 1 << 12;
72−#endif
73−#ifdef DYNAMIC_CRC_TABLE
74− flags += 1 << 13;
75−#endif
76−#ifdef NO_GZCOMPRESS
77− flags += 1L << 16;
78−#endif
79−#ifdef NO_GZIP
80− flags += 1L << 17;
81−#endif
82−#ifdef PKZIP_BUG_WORKAROUND
83− flags += 1L << 20;
84−#endif
85−#ifdef FASTEST
86− flags += 1L << 21;
87−#endif
88−#ifdef STDC
89−# ifdef NO_vsnprintf
90− flags += 1L << 25;
91−# ifdef HAS_vsprintf_void
92− flags += 1L << 26;
93−# endif
94−# else
95−# ifdef HAS_vsnprintf_void
96− flags += 1L << 26;
97−# endif
98−# endif
99−#else
100− flags += 1L << 24;
101−# ifdef NO_snprintf
102− flags += 1L << 25;
103−# ifdef HAS_sprintf_void
104− flags += 1L << 26;
105−# endif
106−# else
107−# ifdef HAS_snprintf_void
108− flags += 1L << 26;
109−# endif
110−# endif
111−#endif
112− return flags;
113−}
114−
115−#ifdef DEBUG
116−
117−# ifndef verbose
118−# define verbose 0
119−# endif
120−int z_verbose = verbose;
121−
122−void z_error (m)
123− char *m;
124−{
125− fprintf(stderr, "%s\n", m);
126− exit(1);
127−}
128−#endif
129−
130−/* exported to allow conversion of error code to string for compress() and
131− * uncompress()
132− */
133−const char * ZEXPORT zError(err)
134− int err;
135−{
136− return ERR_MSG(err);
137−}
138−
139−#if defined(_WIN32_WCE)
140− /* The Microsoft C Run-Time Library for Windows CE doesn't have
141− * errno. We define it as a global variable to simplify porting.
142− * Its value is always 0 and should not be used.
143− */
144− int errno = 0;
145−#endif
146−
147−#ifndef HAVE_MEMCPY
148−
149−void zmemcpy(dest, source, len)
150− Bytef* dest;
151− const Bytef* source;
152− uInt len;
153−{
154− if (len == 0) return;
155− do {
156− *dest++ = *source++; /* ??? to be unrolled */
157− } while (--len != 0);
158−}
159−
160−int zmemcmp(s1, s2, len)
161− const Bytef* s1;
162− const Bytef* s2;
163− uInt len;
164−{
165− uInt j;
166−
167− for (j = 0; j < len; j++) {
168− if (s1[j] != s2[j]) return 2*(s1[j] > s2[j])-1;
169− }
170− return 0;
171−}
172−
173−void zmemzero(dest, len)
174− Bytef* dest;
175− uInt len;
176−{
177− if (len == 0) return;
178− do {
179− *dest++ = 0; /* ??? to be unrolled */
180− } while (--len != 0);
181−}
182−#endif
183−
184−
185−#ifdef SYS16BIT
186−
187−#ifdef __TURBOC__
188−/* Turbo C in 16-bit mode */
189−
190−# define MY_ZCALLOC
191−
192−/* Turbo C malloc() does not allow dynamic allocation of 64K bytes
193− * and farmalloc(64K) returns a pointer with an offset of 8, so we
194− * must fix the pointer. Warning: the pointer must be put back to its
195− * original form in order to free it, use zcfree().
196− */
197−
198−#define MAX_PTR 10
199−/* 10*64K = 640K */
200−
201−local int next_ptr = 0;
202−
203−typedef struct ptr_table_s {
204− voidpf org_ptr;
205− voidpf new_ptr;
206−} ptr_table;
207−
208−local ptr_table table[MAX_PTR];
209−/* This table is used to remember the original form of pointers
210− * to large buffers (64K). Such pointers are normalized with a zero offset.
211− * Since MSDOS is not a preemptive multitasking OS, this table is not
212− * protected from concurrent access. This hack doesn't work anyway on
213− * a protected system like OS/2. Use Microsoft C instead.
214− */
215−
216−voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
217−{
218− voidpf buf = opaque; /* just to make some compilers happy */
219− ulg bsize = (ulg)items*size;
220−
221− /* If we allocate less than 65520 bytes, we assume that farmalloc
222− * will return a usable pointer which doesn't have to be normalized.
223− */
224− if (bsize < 65520L) {
225− buf = farmalloc(bsize);
226− if (*(ush*)&buf != 0) return buf;
227− } else {
228− buf = farmalloc(bsize + 16L);
229− }
230− if (buf == NULL || next_ptr >= MAX_PTR) return NULL;
231− table[next_ptr].org_ptr = buf;
232−
233− /* Normalize the pointer to seg:0 */
234− *((ush*)&buf+1) += ((ush)((uch*)buf-0) + 15) >> 4;
235− *(ush*)&buf = 0;
236− table[next_ptr++].new_ptr = buf;
237− return buf;
238−}
239−
240−void zcfree (voidpf opaque, voidpf ptr)
241−{
242− int n;
243− if (*(ush*)&ptr != 0) { /* object < 64K */
244− farfree(ptr);
245− return;
246− }
247− /* Find the original pointer */
248− for (n = 0; n < next_ptr; n++) {
249− if (ptr != table[n].new_ptr) continue;
250−
251− farfree(table[n].org_ptr);
252− while (++n < next_ptr) {
253− table[n-1] = table[n];
254− }
255− next_ptr--;
256− return;
257− }
258− ptr = opaque; /* just to make some compilers happy */
259− Assert(0, "zcfree: ptr not found");
260−}
261−
262−#endif /* __TURBOC__ */
263−
264−
265−#ifdef M_I86
266−/* Microsoft C in 16-bit mode */
267−
268−# define MY_ZCALLOC
269−
270−#if (!defined(_MSC_VER) || (_MSC_VER <= 600))
271−# define _halloc halloc
272−# define _hfree hfree
273−#endif
274−
275−voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
276−{
277− if (opaque) opaque = 0; /* to make compiler happy */
278− return _halloc((long)items, size);
279−}
280−
281−void zcfree (voidpf opaque, voidpf ptr)
282−{
283− if (opaque) opaque = 0; /* to make compiler happy */
284− _hfree(ptr);
285−}
286−
287−#endif /* M_I86 */
288−
289−#endif /* SYS16BIT */
290−
291−
292−#ifndef MY_ZCALLOC /* Any system without a special alloc function */
293−
294−#ifndef STDC
295−extern voidp malloc OF((uInt size));
296−extern voidp calloc OF((uInt items, uInt size));
297−extern void free OF((voidpf ptr));
298−#endif
299−
300−voidpf zcalloc (opaque, items, size)
301− voidpf opaque;
302− unsigned items;
303− unsigned size;
304−{
305− if (opaque) items += size - size; /* make compiler happy */
306− return sizeof(uInt) > 2 ? (voidpf)malloc(items * size) :
307− (voidpf)calloc(items, size);
308−}
309−
310−void zcfree (opaque, ptr)
311− voidpf opaque;
312− voidpf ptr;
313−{
314− free(ptr);
315− if (opaque) return; /* make compiler happy */
316−}
317−
318−#endif /* MY_ZCALLOC */
cbits/zutil.hdeleted−269
@@ -1,269 +0,0 @@
1−/* zutil.h -- internal interface and configuration of the compression library
2− * Copyright (C) 1995-2005 Jean-loup Gailly.
3− * For conditions of distribution and use, see copyright notice in zlib.h
4− */
5−
6−/* WARNING: this file should *not* be used by applications. It is
7− part of the implementation of the compression library and is
8− subject to change. Applications should only use zlib.h.
9− */
10−
11−/* @(#) $Id$ */
12−
13−#ifndef ZUTIL_H
14−#define ZUTIL_H
15−
16−#define ZLIB_INTERNAL
17−#include "zlib.h"
18−
19−#ifdef STDC
20−# ifndef _WIN32_WCE
21−# include <stddef.h>
22−# endif
23−# include <string.h>
24−# include <stdlib.h>
25−#endif
26−#ifdef NO_ERRNO_H
27−# ifdef _WIN32_WCE
28− /* The Microsoft C Run-Time Library for Windows CE doesn't have
29− * errno. We define it as a global variable to simplify porting.
30− * Its value is always 0 and should not be used. We rename it to
31− * avoid conflict with other libraries that use the same workaround.
32− */
33−# define errno z_errno
34−# endif
35− extern int errno;
36−#else
37−# ifndef _WIN32_WCE
38−# include <errno.h>
39−# endif
40−#endif
41−
42−#ifndef local
43−# define local static
44−#endif
45−/* compile with -Dlocal if your debugger can't find static symbols */
46−
47−typedef unsigned char uch;
48−typedef uch FAR uchf;
49−typedef unsigned short ush;
50−typedef ush FAR ushf;
51−typedef unsigned long ulg;
52−
53−extern const char * const z_errmsg[10]; /* indexed by 2-zlib_error */
54−/* (size given to avoid silly warnings with Visual C++) */
55−
56−#define ERR_MSG(err) z_errmsg[Z_NEED_DICT-(err)]
57−
58−#define ERR_RETURN(strm,err) \
59− return (strm->msg = (char*)ERR_MSG(err), (err))
60−/* To be used only when the state is known to be valid */
61−
62− /* common constants */
63−
64−#ifndef DEF_WBITS
65−# define DEF_WBITS MAX_WBITS
66−#endif
67−/* default windowBits for decompression. MAX_WBITS is for compression only */
68−
69−#if MAX_MEM_LEVEL >= 8
70−# define DEF_MEM_LEVEL 8
71−#else
72−# define DEF_MEM_LEVEL MAX_MEM_LEVEL
73−#endif
74−/* default memLevel */
75−
76−#define STORED_BLOCK 0
77−#define STATIC_TREES 1
78−#define DYN_TREES 2
79−/* The three kinds of block type */
80−
81−#define MIN_MATCH 3
82−#define MAX_MATCH 258
83−/* The minimum and maximum match lengths */
84−
85−#define PRESET_DICT 0x20 /* preset dictionary flag in zlib header */
86−
87− /* target dependencies */
88−
89−#if defined(MSDOS) || (defined(WINDOWS) && !defined(WIN32))
90−# define OS_CODE 0x00
91−# if defined(__TURBOC__) || defined(__BORLANDC__)
92−# if(__STDC__ == 1) && (defined(__LARGE__) || defined(__COMPACT__))
93− /* Allow compilation with ANSI keywords only enabled */
94− void _Cdecl farfree( void *block );
95− void *_Cdecl farmalloc( unsigned long nbytes );
96−# else
97−# include <alloc.h>
98−# endif
99−# else /* MSC or DJGPP */
100−# include <malloc.h>
101−# endif
102−#endif
103−
104−#ifdef AMIGA
105−# define OS_CODE 0x01
106−#endif
107−
108−#if defined(VAXC) || defined(VMS)
109−# define OS_CODE 0x02
110−# define F_OPEN(name, mode) \
111− fopen((name), (mode), "mbc=60", "ctx=stm", "rfm=fix", "mrs=512")
112−#endif
113−
114−#if defined(ATARI) || defined(atarist)
115−# define OS_CODE 0x05
116−#endif
117−
118−#ifdef OS2
119−# define OS_CODE 0x06
120−# ifdef M_I86
121− #include <malloc.h>
122−# endif
123−#endif
124−
125−#if defined(MACOS) || defined(TARGET_OS_MAC)
126−# define OS_CODE 0x07
127−# if defined(__MWERKS__) && __dest_os != __be_os && __dest_os != __win32_os
128−# include <unix.h> /* for fdopen */
129−# else
130−# ifndef fdopen
131−# define fdopen(fd,mode) NULL /* No fdopen() */
132−# endif
133−# endif
134−#endif
135−
136−#ifdef TOPS20
137−# define OS_CODE 0x0a
138−#endif
139−
140−#ifdef WIN32
141−# ifndef __CYGWIN__ /* Cygwin is Unix, not Win32 */
142−# define OS_CODE 0x0b
143−# endif
144−#endif
145−
146−#ifdef __50SERIES /* Prime/PRIMOS */
147−# define OS_CODE 0x0f
148−#endif
149−
150−#if defined(_BEOS_) || defined(RISCOS)
151−# define fdopen(fd,mode) NULL /* No fdopen() */
152−#endif
153−
154−#if (defined(_MSC_VER) && (_MSC_VER > 600))
155−# if defined(_WIN32_WCE)
156−# define fdopen(fd,mode) NULL /* No fdopen() */
157−# ifndef _PTRDIFF_T_DEFINED
158− typedef int ptrdiff_t;
159−# define _PTRDIFF_T_DEFINED
160−# endif
161−# else
162−# define fdopen(fd,type) _fdopen(fd,type)
163−# endif
164−#endif
165−
166− /* common defaults */
167−
168−#ifndef OS_CODE
169−# define OS_CODE 0x03 /* assume Unix */
170−#endif
171−
172−#ifndef F_OPEN
173−# define F_OPEN(name, mode) fopen((name), (mode))
174−#endif
175−
176− /* functions */
177−
178−#if defined(STDC99) || (defined(__TURBOC__) && __TURBOC__ >= 0x550)
179−# ifndef HAVE_VSNPRINTF
180−# define HAVE_VSNPRINTF
181−# endif
182−#endif
183−#if defined(__CYGWIN__)
184−# ifndef HAVE_VSNPRINTF
185−# define HAVE_VSNPRINTF
186−# endif
187−#endif
188−#ifndef HAVE_VSNPRINTF
189−# ifdef MSDOS
190− /* vsnprintf may exist on some MS-DOS compilers (DJGPP?),
191− but for now we just assume it doesn't. */
192−# define NO_vsnprintf
193−# endif
194−# ifdef __TURBOC__
195−# define NO_vsnprintf
196−# endif
197−# ifdef WIN32
198− /* In Win32, vsnprintf is available as the "non-ANSI" _vsnprintf. */
199−# if !defined(vsnprintf) && !defined(NO_vsnprintf)
200−# define vsnprintf _vsnprintf
201−# endif
202−# endif
203−# ifdef __SASC
204−# define NO_vsnprintf
205−# endif
206−#endif
207−#ifdef VMS
208−# define NO_vsnprintf
209−#endif
210−
211−#if defined(pyr)
212−# define NO_MEMCPY
213−#endif
214−#if defined(SMALL_MEDIUM) && !defined(_MSC_VER) && !defined(__SC__)
215− /* Use our own functions for small and medium model with MSC <= 5.0.
216− * You may have to use the same strategy for Borland C (untested).
217− * The __SC__ check is for Symantec.
218− */
219−# define NO_MEMCPY
220−#endif
221−#if defined(STDC) && !defined(HAVE_MEMCPY) && !defined(NO_MEMCPY)
222−# define HAVE_MEMCPY
223−#endif
224−#ifdef HAVE_MEMCPY
225−# ifdef SMALL_MEDIUM /* MSDOS small or medium model */
226−# define zmemcpy _fmemcpy
227−# define zmemcmp _fmemcmp
228−# define zmemzero(dest, len) _fmemset(dest, 0, len)
229−# else
230−# define zmemcpy memcpy
231−# define zmemcmp memcmp
232−# define zmemzero(dest, len) memset(dest, 0, len)
233−# endif
234−#else
235− extern void zmemcpy OF((Bytef* dest, const Bytef* source, uInt len));
236− extern int zmemcmp OF((const Bytef* s1, const Bytef* s2, uInt len));
237− extern void zmemzero OF((Bytef* dest, uInt len));
238−#endif
239−
240−/* Diagnostic functions */
241−#ifdef DEBUG
242−# include <stdio.h>
243− extern int z_verbose;
244− extern void z_error OF((char *m));
245−# define Assert(cond,msg) {if(!(cond)) z_error(msg);}
246−# define Trace(x) {if (z_verbose>=0) fprintf x ;}
247−# define Tracev(x) {if (z_verbose>0) fprintf x ;}
248−# define Tracevv(x) {if (z_verbose>1) fprintf x ;}
249−# define Tracec(c,x) {if (z_verbose>0 && (c)) fprintf x ;}
250−# define Tracecv(c,x) {if (z_verbose>1 && (c)) fprintf x ;}
251−#else
252−# define Assert(cond,msg)
253−# define Trace(x)
254−# define Tracev(x)
255−# define Tracevv(x)
256−# define Tracec(c,x)
257−# define Tracecv(c,x)
258−#endif
259−
260−
261−voidpf zcalloc OF((voidpf opaque, unsigned items, unsigned size));
262−void zcfree OF((voidpf opaque, voidpf ptr));
263−
264−#define ZALLOC(strm, items, size) \
265− (*((strm)->zalloc))((strm)->opaque, (items), (size))
266−#define ZFREE(strm, addr) (*((strm)->zfree))((strm)->opaque, (voidpf)(addr))
267−#define TRY_FREE(s, p) {if (p) ZFREE(s, p);}
268−
269−#endif /* ZUTIL_H */
mime-types/ChangeLog.mdnew file+15
@@ -0,0 +1,15 @@
1+## 0.1.0.9
2+
3+* Add mjs mime type
4+
5+## 0.1.0.8
6+
7+* Add wasm mime type
8+
9+## 0.1.0.7
10+
11+* Add support for .less files [#534](https://github.com/yesodweb/wai/pull/534)
12+
13+## 0.1.0.6
14+
15+* Add woff2 mime type [#350](https://github.com/yesodweb/wai/pull/350)
mime-types/LICENSEnew file+20
@@ -0,0 +1,20 @@
1+Copyright (c) 2012 Michael Snoyman, http://www.yesodweb.com/
2+
3+Permission is hereby granted, free of charge, to any person obtaining
4+a copy of this software and associated documentation files (the
5+"Software"), to deal in the Software without restriction, including
6+without limitation the rights to use, copy, modify, merge, publish,
7+distribute, sublicense, and/or sell copies of the Software, and to
8+permit persons to whom the Software is furnished to do so, subject to
9+the following conditions:
10+
11+The above copyright notice and this permission notice shall be
12+included in all copies or substantial portions of the Software.
13+
14+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
mime-types/Network/Mime.hsnew file+1096
@@ -0,0 +1,1096 @@
1+{-# LANGUAGE OverloadedStrings #-}
2+module Network.Mime
3+ ( -- * Lookups
4+ mimeByExt
5+ , defaultMimeLookup
6+ -- * Defaults
7+ , defaultMimeType
8+ , defaultMimeMap
9+ -- * Utilities
10+ , fileNameExtensions
11+ -- * Types
12+ , FileName
13+ , MimeType
14+ , MimeMap
15+ , Extension
16+ ) where
17+
18+import Data.Text (Text)
19+import qualified Data.Text as T
20+import Data.ByteString (ByteString)
21+import Data.ByteString.Char8 ()
22+import qualified Data.Map as Map
23+
24+-- | Maps extensions to mime types.
25+type MimeMap = Map.Map Extension MimeType
26+
27+-- | The filename component of a filepath, leaving off the directory but
28+-- keeping all extensions.
29+type FileName = Text
30+
31+-- | Individual mime type for be served over the wire.
32+type MimeType = ByteString
33+
34+-- | Look up a mime type from the given mime map and default mime type.
35+mimeByExt :: MimeMap
36+ -> MimeType -- ^ default mime type
37+ -> FileName
38+ -> MimeType
39+mimeByExt mm def =
40+ go . fileNameExtensions
41+ where
42+ go [] = def
43+ go (e:es) =
44+ case Map.lookup e mm of
45+ Nothing -> go es
46+ Just mt -> mt
47+
48+-- | @mimeByExt@ applied to @defaultMimeType@ and @defaultMimeMap@.
49+defaultMimeLookup :: FileName -> MimeType
50+defaultMimeLookup = mimeByExt defaultMimeMap defaultMimeType
51+
52+-- | Get a list of all of the file name extensions.
53+--
54+-- > fileNameExtensions "foo.tar.gz" == ["tar.gz", "gz"]
55+fileNameExtensions :: FileName -> [Extension]
56+fileNameExtensions =
57+ go . T.toLower . dropToExt
58+ where
59+ dropToExt = T.drop 1 . T.dropWhile (/= '.')
60+
61+ go e
62+ | T.null e = []
63+ | otherwise = e : go next
64+ where
65+ next = dropToExt e
66+
67+-- | Path extension. May include multiple components, e.g. tar.gz
68+type Extension = Text
69+
70+-- | The default fallback mime type \"application/octet-stream\".
71+defaultMimeType :: MimeType
72+defaultMimeType = "application/octet-stream"
73+
74+-- | A default mapping from filename extension to mime type.
75+--
76+-- Generated from the Apache and nginx mime.types files.
77+defaultMimeMap :: MimeMap
78+defaultMimeMap = Map.fromAscList [
79+ ("123", "application/vnd.lotus-1-2-3")
80+ , ("3dml", "text/vnd.in3d.3dml")
81+ , ("3ds", "image/x-3ds")
82+ , ("3g2", "video/3gpp2")
83+ , ("3gp", "video/3gpp")
84+ , ("3gpp", "video/3gpp")
85+ , ("7z", "application/x-7z-compressed")
86+ , ("aab", "application/x-authorware-bin")
87+ , ("aac", "audio/x-aac")
88+ , ("aam", "application/x-authorware-map")
89+ , ("aas", "application/x-authorware-seg")
90+ , ("abw", "application/x-abiword")
91+ , ("ac", "application/pkix-attr-cert")
92+ , ("acc", "application/vnd.americandynamics.acc")
93+ , ("ace", "application/x-ace-compressed")
94+ , ("acu", "application/vnd.acucobol")
95+ , ("acutc", "application/vnd.acucorp")
96+ , ("adp", "audio/adpcm")
97+ , ("aep", "application/vnd.audiograph")
98+ , ("afm", "application/x-font-type1")
99+ , ("afp", "application/vnd.ibm.modcap")
100+ , ("ahead", "application/vnd.ahead.space")
101+ , ("ai", "application/postscript")
102+ , ("aif", "audio/x-aiff")
103+ , ("aifc", "audio/x-aiff")
104+ , ("aiff", "audio/x-aiff")
105+ , ("air", "application/vnd.adobe.air-application-installer-package+zip")
106+ , ("ait", "application/vnd.dvb.ait")
107+ , ("ami", "application/vnd.amiga.ami")
108+ , ("apk", "application/vnd.android.package-archive")
109+ , ("appcache", "text/cache-manifest")
110+ , ("application", "application/x-ms-application")
111+ , ("apr", "application/vnd.lotus-approach")
112+ , ("arc", "application/x-freearc")
113+ , ("asc", "text/plain")
114+ , ("asf", "video/x-ms-asf")
115+ , ("asm", "text/x-asm")
116+ , ("aso", "application/vnd.accpac.simply.aso")
117+ , ("asx", "video/x-ms-asf")
118+ , ("atc", "application/vnd.acucorp")
119+ , ("atom", "application/atom+xml")
120+ , ("atomcat", "application/atomcat+xml")
121+ , ("atomsvc", "application/atomsvc+xml")
122+ , ("atx", "application/vnd.antix.game-component")
123+ , ("au", "audio/basic")
124+ , ("avi", "video/x-msvideo")
125+ , ("aw", "application/applixware")
126+ , ("azf", "application/vnd.airzip.filesecure.azf")
127+ , ("azs", "application/vnd.airzip.filesecure.azs")
128+ , ("azw", "application/vnd.amazon.ebook")
129+ , ("bat", "application/x-msdownload")
130+ , ("bcpio", "application/x-bcpio")
131+ , ("bdf", "application/x-font-bdf")
132+ , ("bdm", "application/vnd.syncml.dm+wbxml")
133+ , ("bed", "application/vnd.realvnc.bed")
134+ , ("bh2", "application/vnd.fujitsu.oasysprs")
135+ , ("bin", "application/octet-stream")
136+ , ("blb", "application/x-blorb")
137+ , ("blorb", "application/x-blorb")
138+ , ("bmi", "application/vnd.bmi")
139+ , ("bmp", "image/bmp")
140+ , ("book", "application/vnd.framemaker")
141+ , ("box", "application/vnd.previewsystems.box")
142+ , ("boz", "application/x-bzip2")
143+ , ("bpk", "application/octet-stream")
144+ , ("btif", "image/prs.btif")
145+ , ("bz", "application/x-bzip")
146+ , ("bz2", "application/x-bzip")
147+ , ("c", "text/plain")
148+ , ("c11amc", "application/vnd.cluetrust.cartomobile-config")
149+ , ("c11amz", "application/vnd.cluetrust.cartomobile-config-pkg")
150+ , ("c4d", "application/vnd.clonk.c4group")
151+ , ("c4f", "application/vnd.clonk.c4group")
152+ , ("c4g", "application/vnd.clonk.c4group")
153+ , ("c4p", "application/vnd.clonk.c4group")
154+ , ("c4u", "application/vnd.clonk.c4group")
155+ , ("cab", "application/vnd.ms-cab-compressed")
156+ , ("caf", "audio/x-caf")
157+ , ("cap", "application/vnd.tcpdump.pcap")
158+ , ("car", "application/vnd.curl.car")
159+ , ("cat", "application/vnd.ms-pki.seccat")
160+ , ("cb7", "application/x-cbr")
161+ , ("cba", "application/x-cbr")
162+ , ("cbr", "application/x-cbr")
163+ , ("cbt", "application/x-cbr")
164+ , ("cbz", "application/x-cbr")
165+ , ("cc", "text/x-c")
166+ , ("cco", "application/x-cocoa")
167+ , ("cct", "application/x-director")
168+ , ("ccxml", "application/ccxml+xml")
169+ , ("cdbcmsg", "application/vnd.contact.cmsg")
170+ , ("cdf", "application/x-netcdf")
171+ , ("cdkey", "application/vnd.mediastation.cdkey")
172+ , ("cdmia", "application/cdmi-capability")
173+ , ("cdmic", "application/cdmi-container")
174+ , ("cdmid", "application/cdmi-domain")
175+ , ("cdmio", "application/cdmi-object")
176+ , ("cdmiq", "application/cdmi-queue")
177+ , ("cdx", "chemical/x-cdx")
178+ , ("cdxml", "application/vnd.chemdraw+xml")
179+ , ("cdy", "application/vnd.cinderella")
180+ , ("cer", "application/pkix-cert")
181+ , ("cfs", "application/x-cfs-compressed")
182+ , ("cgm", "image/cgm")
183+ , ("chat", "application/x-chat")
184+ , ("chm", "application/vnd.ms-htmlhelp")
185+ , ("chrt", "application/vnd.kde.kchart")
186+ , ("cif", "chemical/x-cif")
187+ , ("cii", "application/vnd.anser-web-certificate-issue-initiation")
188+ , ("cil", "application/vnd.ms-artgalry")
189+ , ("cla", "application/vnd.claymore")
190+ , ("class", "application/java-vm")
191+ , ("clkk", "application/vnd.crick.clicker.keyboard")
192+ , ("clkp", "application/vnd.crick.clicker.palette")
193+ , ("clkt", "application/vnd.crick.clicker.template")
194+ , ("clkw", "application/vnd.crick.clicker.wordbank")
195+ , ("clkx", "application/vnd.crick.clicker")
196+ , ("clp", "application/x-msclip")
197+ , ("cmc", "application/vnd.cosmocaller")
198+ , ("cmdf", "chemical/x-cmdf")
199+ , ("cml", "chemical/x-cml")
200+ , ("cmp", "application/vnd.yellowriver-custom-menu")
201+ , ("cmx", "image/x-cmx")
202+ , ("cod", "application/vnd.rim.cod")
203+ , ("com", "application/x-msdownload")
204+ , ("conf", "text/plain")
205+ , ("cpio", "application/x-cpio")
206+ , ("cpp", "text/plain")
207+ , ("cpt", "application/mac-compactpro")
208+ , ("crd", "application/x-mscardfile")
209+ , ("crl", "application/pkix-crl")
210+ , ("crt", "application/x-x509-ca-cert")
211+ , ("cryptonote", "application/vnd.rig.cryptonote")
212+ , ("csh", "application/x-csh")
213+ , ("csml", "chemical/x-csml")
214+ , ("csp", "application/vnd.commonspace")
215+ , ("css", "text/css")
216+ , ("cst", "application/x-director")
217+ , ("csv", "text/csv")
218+ , ("cu", "application/cu-seeme")
219+ , ("curl", "text/vnd.curl")
220+ , ("cww", "application/prs.cww")
221+ , ("cxt", "application/x-director")
222+ , ("cxx", "text/plain")
223+ , ("dae", "model/vnd.collada+xml")
224+ , ("daf", "application/vnd.mobius.daf")
225+ , ("dart", "application/vnd.dart")
226+ , ("dataless", "application/vnd.fdsn.seed")
227+ , ("davmount", "application/davmount+xml")
228+ , ("dbk", "application/docbook+xml")
229+ , ("dcr", "application/x-director")
230+ , ("dcurl", "text/vnd.curl.dcurl")
231+ , ("dd2", "application/vnd.oma.dd2+xml")
232+ , ("ddd", "application/vnd.fujixerox.ddd")
233+ , ("deb", "application/x-debian-package")
234+ , ("def", "text/plain")
235+ , ("deploy", "application/octet-stream")
236+ , ("der", "application/x-x509-ca-cert")
237+ , ("dfac", "application/vnd.dreamfactory")
238+ , ("dgc", "application/x-dgc-compressed")
239+ , ("dic", "text/x-c")
240+ , ("dir", "application/x-director")
241+ , ("dis", "application/vnd.mobius.dis")
242+ , ("dist", "application/octet-stream")
243+ , ("distz", "application/octet-stream")
244+ , ("djv", "image/vnd.djvu")
245+ , ("djvu", "image/vnd.djvu")
246+ , ("dll", "application/x-msdownload")
247+ , ("dmg", "application/x-apple-diskimage")
248+ , ("dmp", "application/vnd.tcpdump.pcap")
249+ , ("dms", "application/octet-stream")
250+ , ("dna", "application/vnd.dna")
251+ , ("doc", "application/msword")
252+ , ("docm", "application/vnd.ms-word.document.macroenabled.12")
253+ , ("docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document")
254+ , ("dot", "application/msword")
255+ , ("dotm", "application/vnd.ms-word.template.macroenabled.12")
256+ , ("dotx", "application/vnd.openxmlformats-officedocument.wordprocessingml.template")
257+ , ("dp", "application/vnd.osgi.dp")
258+ , ("dpg", "application/vnd.dpgraph")
259+ , ("dra", "audio/vnd.dra")
260+ , ("dsc", "text/prs.lines.tag")
261+ , ("dssc", "application/dssc+der")
262+ , ("dtb", "application/x-dtbook+xml")
263+ , ("dtd", "application/xml-dtd")
264+ , ("dts", "audio/vnd.dts")
265+ , ("dtshd", "audio/vnd.dts.hd")
266+ , ("dump", "application/octet-stream")
267+ , ("dvb", "video/vnd.dvb.file")
268+ , ("dvi", "application/x-dvi")
269+ , ("dwf", "model/vnd.dwf")
270+ , ("dwg", "image/vnd.dwg")
271+ , ("dxf", "image/vnd.dxf")
272+ , ("dxp", "application/vnd.spotfire.dxp")
273+ , ("dxr", "application/x-director")
274+ , ("ear", "application/java-archive")
275+ , ("ecelp4800", "audio/vnd.nuera.ecelp4800")
276+ , ("ecelp7470", "audio/vnd.nuera.ecelp7470")
277+ , ("ecelp9600", "audio/vnd.nuera.ecelp9600")
278+ , ("ecma", "application/ecmascript")
279+ , ("edm", "application/vnd.novadigm.edm")
280+ , ("edx", "application/vnd.novadigm.edx")
281+ , ("efif", "application/vnd.picsel")
282+ , ("ei6", "application/vnd.pg.osasli")
283+ , ("elc", "application/octet-stream")
284+ , ("emf", "application/x-msmetafile")
285+ , ("eml", "message/rfc822")
286+ , ("emma", "application/emma+xml")
287+ , ("emz", "application/x-msmetafile")
288+ , ("eol", "audio/vnd.digital-winds")
289+ , ("eot", "application/vnd.ms-fontobject")
290+ , ("eps", "application/postscript")
291+ , ("epub", "application/epub+zip")
292+ , ("es3", "application/vnd.eszigno3+xml")
293+ , ("esa", "application/vnd.osgi.subsystem")
294+ , ("esf", "application/vnd.epson.esf")
295+ , ("et3", "application/vnd.eszigno3+xml")
296+ , ("etx", "text/x-setext")
297+ , ("eva", "application/x-eva")
298+ , ("evy", "application/x-envoy")
299+ , ("exe", "application/x-msdownload")
300+ , ("exi", "application/exi")
301+ , ("ext", "application/vnd.novadigm.ext")
302+ , ("ez", "application/andrew-inset")
303+ , ("ez2", "application/vnd.ezpix-album")
304+ , ("ez3", "application/vnd.ezpix-package")
305+ , ("f", "text/x-fortran")
306+ , ("f4v", "video/x-f4v")
307+ , ("f77", "text/x-fortran")
308+ , ("f90", "text/x-fortran")
309+ , ("fbs", "image/vnd.fastbidsheet")
310+ , ("fcdt", "application/vnd.adobe.formscentral.fcdt")
311+ , ("fcs", "application/vnd.isac.fcs")
312+ , ("fdf", "application/vnd.fdf")
313+ , ("fe_launch", "application/vnd.denovo.fcselayout-link")
314+ , ("fg5", "application/vnd.fujitsu.oasysgp")
315+ , ("fgd", "application/x-director")
316+ , ("fh", "image/x-freehand")
317+ , ("fh4", "image/x-freehand")
318+ , ("fh5", "image/x-freehand")
319+ , ("fh7", "image/x-freehand")
320+ , ("fhc", "image/x-freehand")
321+ , ("fig", "application/x-xfig")
322+ , ("flac", "audio/x-flac")
323+ , ("fli", "video/x-fli")
324+ , ("flo", "application/vnd.micrografx.flo")
325+ , ("flv", "video/x-flv")
326+ , ("flw", "application/vnd.kde.kivio")
327+ , ("flx", "text/vnd.fmi.flexstor")
328+ , ("fly", "text/vnd.fly")
329+ , ("fm", "application/vnd.framemaker")
330+ , ("fnc", "application/vnd.frogans.fnc")
331+ , ("for", "text/x-fortran")
332+ , ("fpx", "image/vnd.fpx")
333+ , ("frame", "application/vnd.framemaker")
334+ , ("fsc", "application/vnd.fsc.weblaunch")
335+ , ("fst", "image/vnd.fst")
336+ , ("ftc", "application/vnd.fluxtime.clip")
337+ , ("fti", "application/vnd.anser-web-funds-transfer-initiation")
338+ , ("fvt", "video/vnd.fvt")
339+ , ("fxp", "application/vnd.adobe.fxp")
340+ , ("fxpl", "application/vnd.adobe.fxp")
341+ , ("fzs", "application/vnd.fuzzysheet")
342+ , ("g2w", "application/vnd.geoplan")
343+ , ("g3", "image/g3fax")
344+ , ("g3w", "application/vnd.geospace")
345+ , ("gac", "application/vnd.groove-account")
346+ , ("gam", "application/x-tads")
347+ , ("gbr", "application/rpki-ghostbusters")
348+ , ("gca", "application/x-gca-compressed")
349+ , ("gdl", "model/vnd.gdl")
350+ , ("geo", "application/vnd.dynageo")
351+ , ("gex", "application/vnd.geometry-explorer")
352+ , ("ggb", "application/vnd.geogebra.file")
353+ , ("ggt", "application/vnd.geogebra.tool")
354+ , ("ghf", "application/vnd.groove-help")
355+ , ("gif", "image/gif")
356+ , ("gim", "application/vnd.groove-identity-message")
357+ , ("gml", "application/gml+xml")
358+ , ("gmx", "application/vnd.gmx")
359+ , ("gnumeric", "application/x-gnumeric")
360+ , ("gph", "application/vnd.flographit")
361+ , ("gpx", "application/gpx+xml")
362+ , ("gqf", "application/vnd.grafeq")
363+ , ("gqs", "application/vnd.grafeq")
364+ , ("gram", "application/srgs")
365+ , ("gramps", "application/x-gramps-xml")
366+ , ("gre", "application/vnd.geometry-explorer")
367+ , ("grv", "application/vnd.groove-injector")
368+ , ("grxml", "application/srgs+xml")
369+ , ("gsf", "application/x-font-ghostscript")
370+ , ("gtar", "application/x-gtar")
371+ , ("gtm", "application/vnd.groove-tool-message")
372+ , ("gtw", "model/vnd.gtw")
373+ , ("gv", "text/vnd.graphviz")
374+ , ("gxf", "application/gxf")
375+ , ("gxt", "application/vnd.geonext")
376+ , ("gz", "application/x-gzip")
377+ , ("h", "text/x-c")
378+ , ("h261", "video/h261")
379+ , ("h263", "video/h263")
380+ , ("h264", "video/h264")
381+ , ("hal", "application/vnd.hal+xml")
382+ , ("hbci", "application/vnd.hbci")
383+ , ("hdf", "application/x-hdf")
384+ , ("hh", "text/x-c")
385+ , ("hlp", "application/winhlp")
386+ , ("hpgl", "application/vnd.hp-hpgl")
387+ , ("hpid", "application/vnd.hp-hpid")
388+ , ("hps", "application/vnd.hp-hps")
389+ , ("hqx", "application/mac-binhex40")
390+ , ("hs", "text/plain")
391+ , ("htc", "text/x-component")
392+ , ("htke", "application/vnd.kenameaapp")
393+ , ("htm", "text/html")
394+ , ("html", "text/html")
395+ , ("hvd", "application/vnd.yamaha.hv-dic")
396+ , ("hvp", "application/vnd.yamaha.hv-voice")
397+ , ("hvs", "application/vnd.yamaha.hv-script")
398+ , ("i2g", "application/vnd.intergeo")
399+ , ("icc", "application/vnd.iccprofile")
400+ , ("ice", "x-conference/x-cooltalk")
401+ , ("icm", "application/vnd.iccprofile")
402+ , ("ico", "image/vnd.microsoft.icon")
403+ , ("ics", "text/calendar")
404+ , ("ief", "image/ief")
405+ , ("ifb", "text/calendar")
406+ , ("ifm", "application/vnd.shana.informed.formdata")
407+ , ("iges", "model/iges")
408+ , ("igl", "application/vnd.igloader")
409+ , ("igm", "application/vnd.insors.igm")
410+ , ("igs", "model/iges")
411+ , ("igx", "application/vnd.micrografx.igx")
412+ , ("iif", "application/vnd.shana.informed.interchange")
413+ , ("img", "application/octet-stream")
414+ , ("imp", "application/vnd.accpac.simply.imp")
415+ , ("ims", "application/vnd.ms-ims")
416+ , ("in", "text/plain")
417+ , ("ink", "application/inkml+xml")
418+ , ("inkml", "application/inkml+xml")
419+ , ("install", "application/x-install-instructions")
420+ , ("iota", "application/vnd.astraea-software.iota")
421+ , ("ipfix", "application/ipfix")
422+ , ("ipk", "application/vnd.shana.informed.package")
423+ , ("irm", "application/vnd.ibm.rights-management")
424+ , ("irp", "application/vnd.irepository.package+xml")
425+ , ("iso", "application/x-iso9660-image")
426+ , ("itp", "application/vnd.shana.informed.formtemplate")
427+ , ("ivp", "application/vnd.immervision-ivp")
428+ , ("ivu", "application/vnd.immervision-ivu")
429+ , ("jad", "text/vnd.sun.j2me.app-descriptor")
430+ , ("jam", "application/vnd.jam")
431+ , ("jar", "application/java-archive")
432+ , ("jardiff", "application/x-java-archive-diff")
433+ , ("java", "text/x-java-source")
434+ , ("jisp", "application/vnd.jisp")
435+ , ("jlt", "application/vnd.hp-jlyt")
436+ , ("jng", "image/x-jng")
437+ , ("jnlp", "application/x-java-jnlp-file")
438+ , ("joda", "application/vnd.joost.joda-archive")
439+ , ("jpe", "image/jpeg")
440+ , ("jpeg", "image/jpeg")
441+ , ("jpg", "image/jpeg")
442+ , ("jpgm", "video/jpm")
443+ , ("jpgv", "video/jpeg")
444+ , ("jpm", "video/jpm")
445+ , ("js", "application/javascript")
446+ , ("json", "application/json")
447+ , ("jsonml", "application/jsonml+json")
448+ , ("kar", "audio/midi")
449+ , ("karbon", "application/vnd.kde.karbon")
450+ , ("kfo", "application/vnd.kde.kformula")
451+ , ("kia", "application/vnd.kidspiration")
452+ , ("kml", "application/vnd.google-earth.kml+xml")
453+ , ("kmz", "application/vnd.google-earth.kmz")
454+ , ("kne", "application/vnd.kinar")
455+ , ("knp", "application/vnd.kinar")
456+ , ("kon", "application/vnd.kde.kontour")
457+ , ("kpr", "application/vnd.kde.kpresenter")
458+ , ("kpt", "application/vnd.kde.kpresenter")
459+ , ("kpxx", "application/vnd.ds-keypoint")
460+ , ("ksp", "application/vnd.kde.kspread")
461+ , ("ktr", "application/vnd.kahootz")
462+ , ("ktx", "image/ktx")
463+ , ("ktz", "application/vnd.kahootz")
464+ , ("kwd", "application/vnd.kde.kword")
465+ , ("kwt", "application/vnd.kde.kword")
466+ , ("lasxml", "application/vnd.las.las+xml")
467+ , ("latex", "application/x-latex")
468+ , ("lbd", "application/vnd.llamagraphics.life-balance.desktop")
469+ , ("lbe", "application/vnd.llamagraphics.life-balance.exchange+xml")
470+ , ("les", "application/vnd.hhe.lesson-player")
471+
472+ -- Added after deliberation in PR (https://github.com/yesodweb/wai/pull/534)
473+ -- Accepted mainly because of StackOverflow (http://stackoverflow.com/questions/7319555/how-to-add-less-to-iis-7-0)
474+ , ("less", "text/css")
475+
476+ , ("lha", "application/x-lzh-compressed")
477+ , ("link66", "application/vnd.route66.link66+xml")
478+ , ("list", "text/plain")
479+ , ("list3820", "application/vnd.ibm.modcap")
480+ , ("listafp", "application/vnd.ibm.modcap")
481+ , ("lnk", "application/x-ms-shortcut")
482+ , ("log", "text/plain")
483+ , ("lostxml", "application/lost+xml")
484+ , ("lrf", "application/octet-stream")
485+ , ("lrm", "application/vnd.ms-lrm")
486+ , ("ltf", "application/vnd.frogans.ltf")
487+ , ("lvp", "audio/vnd.lucent.voice")
488+ , ("lwp", "application/vnd.lotus-wordpro")
489+ , ("lzh", "application/x-lzh-compressed")
490+ , ("m13", "application/x-msmediaview")
491+ , ("m14", "application/x-msmediaview")
492+ , ("m1v", "video/mpeg")
493+ , ("m21", "application/mp21")
494+ , ("m2a", "audio/mpeg")
495+ , ("m2v", "video/mpeg")
496+ , ("m3a", "audio/mpeg")
497+ , ("m3u", "audio/x-mpegurl")
498+ , ("m3u8", "application/vnd.apple.mpegurl")
499+ , ("m4a", "audio/x-m4a")
500+ , ("m4u", "video/vnd.mpegurl")
501+ , ("m4v", "video/x-m4v")
502+ , ("ma", "application/mathematica")
503+ , ("mads", "application/mads+xml")
504+ , ("mag", "application/vnd.ecowin.chart")
505+ , ("maker", "application/vnd.framemaker")
506+ , ("man", "text/troff")
507+ , ("manifest", "text/cache-manifest")
508+ , ("mar", "application/octet-stream")
509+ , ("mathml", "application/mathml+xml")
510+ , ("mb", "application/mathematica")
511+ , ("mbk", "application/vnd.mobius.mbk")
512+ , ("mbox", "application/mbox")
513+ , ("mc1", "application/vnd.medcalcdata")
514+ , ("mcd", "application/vnd.mcd")
515+ , ("mcurl", "text/vnd.curl.mcurl")
516+ , ("mdb", "application/x-msaccess")
517+ , ("mdi", "image/vnd.ms-modi")
518+ , ("me", "text/troff")
519+ , ("mesh", "model/mesh")
520+ , ("meta4", "application/metalink4+xml")
521+ , ("metalink", "application/metalink+xml")
522+ , ("mets", "application/mets+xml")
523+ , ("mfm", "application/vnd.mfmp")
524+ , ("mft", "application/rpki-manifest")
525+ , ("mgp", "application/vnd.osgeo.mapguide.package")
526+ , ("mgz", "application/vnd.proteus.magazine")
527+ , ("mid", "audio/midi")
528+ , ("midi", "audio/midi")
529+ , ("mie", "application/x-mie")
530+ , ("mif", "application/vnd.mif")
531+ , ("mime", "message/rfc822")
532+ , ("mj2", "video/mj2")
533+ , ("mjp2", "video/mj2")
534+ , ("mjs", "application/javascript")
535+ , ("mk3d", "video/x-matroska")
536+ , ("mka", "audio/x-matroska")
537+ , ("mks", "video/x-matroska")
538+ , ("mkv", "video/x-matroska")
539+ , ("mlp", "application/vnd.dolby.mlp")
540+ , ("mmd", "application/vnd.chipnuts.karaoke-mmd")
541+ , ("mmf", "application/vnd.smaf")
542+ , ("mml", "text/mathml")
543+ , ("mmr", "image/vnd.fujixerox.edmics-mmr")
544+ , ("mng", "video/x-mng")
545+ , ("mny", "application/x-msmoney")
546+ , ("mobi", "application/x-mobipocket-ebook")
547+ , ("mods", "application/mods+xml")
548+ , ("mov", "video/quicktime")
549+ , ("movie", "video/x-sgi-movie")
550+ , ("mp2", "audio/mpeg")
551+ , ("mp21", "application/mp21")
552+ , ("mp2a", "audio/mpeg")
553+ , ("mp3", "audio/mpeg")
554+ , ("mp4", "video/mp4")
555+ , ("mp4a", "audio/mp4")
556+ , ("mp4s", "application/mp4")
557+ , ("mp4v", "video/mp4")
558+ , ("mpc", "application/vnd.mophun.certificate")
559+ , ("mpe", "video/mpeg")
560+ , ("mpeg", "video/mpeg")
561+ , ("mpg", "video/mpeg")
562+ , ("mpg4", "video/mp4")
563+ , ("mpga", "audio/mpeg")
564+ , ("mpkg", "application/vnd.apple.installer+xml")
565+ , ("mpm", "application/vnd.blueice.multipass")
566+ , ("mpn", "application/vnd.mophun.application")
567+ , ("mpp", "application/vnd.ms-project")
568+ , ("mpt", "application/vnd.ms-project")
569+ , ("mpy", "application/vnd.ibm.minipay")
570+ , ("mqy", "application/vnd.mobius.mqy")
571+ , ("mrc", "application/marc")
572+ , ("mrcx", "application/marcxml+xml")
573+ , ("ms", "text/troff")
574+ , ("mscml", "application/mediaservercontrol+xml")
575+ , ("mseed", "application/vnd.fdsn.mseed")
576+ , ("mseq", "application/vnd.mseq")
577+ , ("msf", "application/vnd.epson.msf")
578+ , ("msh", "model/mesh")
579+ , ("msi", "application/x-msdownload")
580+ , ("msl", "application/vnd.mobius.msl")
581+ , ("msm", "application/octet-stream")
582+ , ("msp", "application/octet-stream")
583+ , ("msty", "application/vnd.muvee.style")
584+ , ("mts", "model/vnd.mts")
585+ , ("mus", "application/vnd.musician")
586+ , ("musicxml", "application/vnd.recordare.musicxml+xml")
587+ , ("mvb", "application/x-msmediaview")
588+ , ("mwf", "application/vnd.mfer")
589+ , ("mxf", "application/mxf")
590+ , ("mxl", "application/vnd.recordare.musicxml")
591+ , ("mxml", "application/xv+xml")
592+ , ("mxs", "application/vnd.triscape.mxs")
593+ , ("mxu", "video/vnd.mpegurl")
594+ , ("n-gage", "application/vnd.nokia.n-gage.symbian.install")
595+ , ("n3", "text/n3")
596+ , ("nb", "application/mathematica")
597+ , ("nbp", "application/vnd.wolfram.player")
598+ , ("nc", "application/x-netcdf")
599+ , ("ncx", "application/x-dtbncx+xml")
600+ , ("nfo", "text/x-nfo")
601+ , ("ngdat", "application/vnd.nokia.n-gage.data")
602+ , ("nitf", "application/vnd.nitf")
603+ , ("nlu", "application/vnd.neurolanguage.nlu")
604+ , ("nml", "application/vnd.enliven")
605+ , ("nnd", "application/vnd.noblenet-directory")
606+ , ("nns", "application/vnd.noblenet-sealer")
607+ , ("nnw", "application/vnd.noblenet-web")
608+ , ("npx", "image/vnd.net-fpx")
609+ , ("nsc", "application/x-conference")
610+ , ("nsf", "application/vnd.lotus-notes")
611+ , ("ntf", "application/vnd.nitf")
612+ , ("nzb", "application/x-nzb")
613+ , ("oa2", "application/vnd.fujitsu.oasys2")
614+ , ("oa3", "application/vnd.fujitsu.oasys3")
615+ , ("oas", "application/vnd.fujitsu.oasys")
616+ , ("obd", "application/x-msbinder")
617+ , ("obj", "application/x-tgif")
618+ , ("oda", "application/oda")
619+ , ("odb", "application/vnd.oasis.opendocument.database")
620+ , ("odc", "application/vnd.oasis.opendocument.chart")
621+ , ("odf", "application/vnd.oasis.opendocument.formula")
622+ , ("odft", "application/vnd.oasis.opendocument.formula-template")
623+ , ("odg", "application/vnd.oasis.opendocument.graphics")
624+ , ("odi", "application/vnd.oasis.opendocument.image")
625+ , ("odm", "application/vnd.oasis.opendocument.text-master")
626+ , ("odp", "application/vnd.oasis.opendocument.presentation")
627+ , ("ods", "application/vnd.oasis.opendocument.spreadsheet")
628+ , ("odt", "application/vnd.oasis.opendocument.text")
629+ , ("oga", "audio/ogg")
630+ , ("ogg", "audio/ogg")
631+ , ("ogv", "video/ogg")
632+ , ("ogx", "application/ogg")
633+ , ("omdoc", "application/omdoc+xml")
634+ , ("onepkg", "application/onenote")
635+ , ("onetmp", "application/onenote")
636+ , ("onetoc", "application/onenote")
637+ , ("onetoc2", "application/onenote")
638+ , ("opf", "application/oebps-package+xml")
639+ , ("opml", "text/x-opml")
640+ , ("oprc", "application/vnd.palm")
641+ , ("org", "application/vnd.lotus-organizer")
642+ , ("osf", "application/vnd.yamaha.openscoreformat")
643+ , ("osfpvg", "application/vnd.yamaha.openscoreformat.osfpvg+xml")
644+ , ("otc", "application/vnd.oasis.opendocument.chart-template")
645+ , ("otf", "application/x-font-otf")
646+ , ("otg", "application/vnd.oasis.opendocument.graphics-template")
647+ , ("oth", "application/vnd.oasis.opendocument.text-web")
648+ , ("oti", "application/vnd.oasis.opendocument.image-template")
649+ , ("otp", "application/vnd.oasis.opendocument.presentation-template")
650+ , ("ots", "application/vnd.oasis.opendocument.spreadsheet-template")
651+ , ("ott", "application/vnd.oasis.opendocument.text-template")
652+ , ("oxps", "application/oxps")
653+ , ("oxt", "application/vnd.openofficeorg.extension")
654+ , ("p", "text/x-pascal")
655+ , ("p10", "application/pkcs10")
656+ , ("p12", "application/x-pkcs12")
657+ , ("p7b", "application/x-pkcs7-certificates")
658+ , ("p7c", "application/pkcs7-mime")
659+ , ("p7m", "application/pkcs7-mime")
660+ , ("p7r", "application/x-pkcs7-certreqresp")
661+ , ("p7s", "application/pkcs7-signature")
662+ , ("p8", "application/pkcs8")
663+ , ("pac", "application/x-ns-proxy-autoconfig")
664+ , ("pas", "text/x-pascal")
665+ , ("paw", "application/vnd.pawaafile")
666+ , ("pbd", "application/vnd.powerbuilder6")
667+ , ("pbm", "image/x-portable-bitmap")
668+ , ("pcap", "application/vnd.tcpdump.pcap")
669+ , ("pcf", "application/x-font-pcf")
670+ , ("pcl", "application/vnd.hp-pcl")
671+ , ("pclxl", "application/vnd.hp-pclxl")
672+ , ("pct", "image/x-pict")
673+ , ("pcurl", "application/vnd.curl.pcurl")
674+ , ("pcx", "image/x-pcx")
675+ , ("pdb", "application/vnd.palm")
676+ , ("pdf", "application/pdf")
677+ , ("pem", "application/x-x509-ca-cert")
678+ , ("pfa", "application/x-font-type1")
679+ , ("pfb", "application/x-font-type1")
680+ , ("pfm", "application/x-font-type1")
681+ , ("pfr", "application/font-tdpfr")
682+ , ("pfx", "application/x-pkcs12")
683+ , ("pgm", "image/x-portable-graymap")
684+ , ("pgn", "application/x-chess-pgn")
685+ , ("pgp", "application/pgp-encrypted")
686+ , ("pic", "image/x-pict")
687+ , ("pkg", "application/octet-stream")
688+ , ("pki", "application/pkixcmp")
689+ , ("pkipath", "application/pkix-pkipath")
690+ , ("pl", "application/x-perl")
691+ , ("plb", "application/vnd.3gpp.pic-bw-large")
692+ , ("plc", "application/vnd.mobius.plc")
693+ , ("plf", "application/vnd.pocketlearn")
694+ , ("pls", "application/pls+xml")
695+ , ("pm", "application/x-perl")
696+ , ("pml", "application/vnd.ctc-posml")
697+ , ("png", "image/png")
698+ , ("pnm", "image/x-portable-anymap")
699+ , ("portpkg", "application/vnd.macports.portpkg")
700+ , ("pot", "application/vnd.ms-powerpoint")
701+ , ("potm", "application/vnd.ms-powerpoint.template.macroenabled.12")
702+ , ("potx", "application/vnd.openxmlformats-officedocument.presentationml.template")
703+ , ("ppam", "application/vnd.ms-powerpoint.addin.macroenabled.12")
704+ , ("ppd", "application/vnd.cups-ppd")
705+ , ("ppm", "image/x-portable-pixmap")
706+ , ("pps", "application/vnd.ms-powerpoint")
707+ , ("ppsm", "application/vnd.ms-powerpoint.slideshow.macroenabled.12")
708+ , ("ppsx", "application/vnd.openxmlformats-officedocument.presentationml.slideshow")
709+ , ("ppt", "application/vnd.ms-powerpoint")
710+ , ("pptm", "application/vnd.ms-powerpoint.presentation.macroenabled.12")
711+ , ("pptx", "application/vnd.openxmlformats-officedocument.presentationml.presentation")
712+ , ("pqa", "application/vnd.palm")
713+ , ("prc", "application/x-mobipocket-ebook")
714+ , ("pre", "application/vnd.lotus-freelance")
715+ , ("prf", "application/pics-rules")
716+ , ("ps", "application/postscript")
717+ , ("psb", "application/vnd.3gpp.pic-bw-small")
718+ , ("psd", "image/vnd.adobe.photoshop")
719+ , ("psf", "application/x-font-linux-psf")
720+ , ("pskcxml", "application/pskc+xml")
721+ , ("ptid", "application/vnd.pvi.ptid1")
722+ , ("pub", "application/x-mspublisher")
723+ , ("pvb", "application/vnd.3gpp.pic-bw-var")
724+ , ("pwn", "application/vnd.3m.post-it-notes")
725+ , ("pya", "audio/vnd.ms-playready.media.pya")
726+ , ("pyv", "video/vnd.ms-playready.media.pyv")
727+ , ("qam", "application/vnd.epson.quickanime")
728+ , ("qbo", "application/vnd.intu.qbo")
729+ , ("qfx", "application/vnd.intu.qfx")
730+ , ("qps", "application/vnd.publishare-delta-tree")
731+ , ("qt", "video/quicktime")
732+ , ("qwd", "application/vnd.quark.quarkxpress")
733+ , ("qwt", "application/vnd.quark.quarkxpress")
734+ , ("qxb", "application/vnd.quark.quarkxpress")
735+ , ("qxd", "application/vnd.quark.quarkxpress")
736+ , ("qxl", "application/vnd.quark.quarkxpress")
737+ , ("qxt", "application/vnd.quark.quarkxpress")
738+ , ("ra", "audio/x-pn-realaudio")
739+ , ("ram", "audio/x-pn-realaudio")
740+ , ("rar", "application/x-rar-compressed")
741+ , ("ras", "image/x-cmu-raster")
742+ , ("rcprofile", "application/vnd.ipunplugged.rcprofile")
743+ , ("rdf", "application/rdf+xml")
744+ , ("rdz", "application/vnd.data-vision.rdz")
745+ , ("rep", "application/vnd.businessobjects")
746+ , ("res", "application/x-dtbresource+xml")
747+ , ("rgb", "image/x-rgb")
748+ , ("rif", "application/reginfo+xml")
749+ , ("rip", "audio/vnd.rip")
750+ , ("ris", "application/x-research-info-systems")
751+ , ("rl", "application/resource-lists+xml")
752+ , ("rlc", "image/vnd.fujixerox.edmics-rlc")
753+ , ("rld", "application/resource-lists-diff+xml")
754+ , ("rm", "application/vnd.rn-realmedia")
755+ , ("rmi", "audio/midi")
756+ , ("rmp", "audio/x-pn-realaudio-plugin")
757+ , ("rms", "application/vnd.jcp.javame.midlet-rms")
758+ , ("rmvb", "application/vnd.rn-realmedia-vbr")
759+ , ("rnc", "application/relax-ng-compact-syntax")
760+ , ("roa", "application/rpki-roa")
761+ , ("roff", "text/troff")
762+ , ("rp9", "application/vnd.cloanto.rp9")
763+ , ("rpm", "application/x-redhat-package-manager")
764+ , ("rpss", "application/vnd.nokia.radio-presets")
765+ , ("rpst", "application/vnd.nokia.radio-preset")
766+ , ("rq", "application/sparql-query")
767+ , ("rs", "application/rls-services+xml")
768+ , ("rsd", "application/rsd+xml")
769+ , ("rss", "application/rss+xml")
770+ , ("rtf", "application/rtf")
771+ , ("rtx", "text/richtext")
772+ , ("run", "application/x-makeself")
773+ , ("s", "text/x-asm")
774+ , ("s3m", "audio/s3m")
775+ , ("saf", "application/vnd.yamaha.smaf-audio")
776+ , ("sbml", "application/sbml+xml")
777+ , ("sc", "application/vnd.ibm.secure-container")
778+ , ("scd", "application/x-msschedule")
779+ , ("scm", "application/vnd.lotus-screencam")
780+ , ("scq", "application/scvp-cv-request")
781+ , ("scs", "application/scvp-cv-response")
782+ , ("scurl", "text/vnd.curl.scurl")
783+ , ("sda", "application/vnd.stardivision.draw")
784+ , ("sdc", "application/vnd.stardivision.calc")
785+ , ("sdd", "application/vnd.stardivision.impress")
786+ , ("sdkd", "application/vnd.solent.sdkm+xml")
787+ , ("sdkm", "application/vnd.solent.sdkm+xml")
788+ , ("sdp", "application/sdp")
789+ , ("sdw", "application/vnd.stardivision.writer")
790+ , ("sea", "application/x-sea")
791+ , ("see", "application/vnd.seemail")
792+ , ("seed", "application/vnd.fdsn.seed")
793+ , ("sema", "application/vnd.sema")
794+ , ("semd", "application/vnd.semd")
795+ , ("semf", "application/vnd.semf")
796+ , ("ser", "application/java-serialized-object")
797+ , ("setpay", "application/set-payment-initiation")
798+ , ("setreg", "application/set-registration-initiation")
799+ , ("sfd-hdstx", "application/vnd.hydrostatix.sof-data")
800+ , ("sfs", "application/vnd.spotfire.sfs")
801+ , ("sfv", "text/x-sfv")
802+ , ("sgi", "image/sgi")
803+ , ("sgl", "application/vnd.stardivision.writer-global")
804+ , ("sgm", "text/sgml")
805+ , ("sgml", "text/sgml")
806+ , ("sh", "application/x-sh")
807+ , ("shar", "application/x-shar")
808+ , ("shf", "application/shf+xml")
809+ , ("shtml", "text/html")
810+ , ("sid", "image/x-mrsid-image")
811+ , ("sig", "application/pgp-signature")
812+ , ("sil", "audio/silk")
813+ , ("silo", "model/mesh")
814+ , ("sis", "application/vnd.symbian.install")
815+ , ("sisx", "application/vnd.symbian.install")
816+ , ("sit", "application/x-stuffit")
817+ , ("sitx", "application/x-stuffitx")
818+ , ("skd", "application/vnd.koan")
819+ , ("skm", "application/vnd.koan")
820+ , ("skp", "application/vnd.koan")
821+ , ("skt", "application/vnd.koan")
822+ , ("sldm", "application/vnd.ms-powerpoint.slide.macroenabled.12")
823+ , ("sldx", "application/vnd.openxmlformats-officedocument.presentationml.slide")
824+ , ("slt", "application/vnd.epson.salt")
825+ , ("sm", "application/vnd.stepmania.stepchart")
826+ , ("smf", "application/vnd.stardivision.math")
827+ , ("smi", "application/smil+xml")
828+ , ("smil", "application/smil+xml")
829+ , ("smv", "video/x-smv")
830+ , ("smzip", "application/vnd.stepmania.package")
831+ , ("snd", "audio/basic")
832+ , ("snf", "application/x-font-snf")
833+ , ("so", "application/octet-stream")
834+ , ("spc", "application/x-pkcs7-certificates")
835+ , ("spf", "application/vnd.yamaha.smaf-phrase")
836+ , ("spl", "application/futuresplash")
837+ , ("spot", "text/vnd.in3d.spot")
838+ , ("spp", "application/scvp-vp-response")
839+ , ("spq", "application/scvp-vp-request")
840+ , ("spx", "audio/ogg")
841+ , ("sql", "application/x-sql")
842+ , ("src", "application/x-wais-source")
843+ , ("srt", "application/x-subrip")
844+ , ("sru", "application/sru+xml")
845+ , ("srx", "application/sparql-results+xml")
846+ , ("ssdl", "application/ssdl+xml")
847+ , ("sse", "application/vnd.kodak-descriptor")
848+ , ("ssf", "application/vnd.epson.ssf")
849+ , ("ssml", "application/ssml+xml")
850+ , ("st", "application/vnd.sailingtracker.track")
851+ , ("stc", "application/vnd.sun.xml.calc.template")
852+ , ("std", "application/vnd.sun.xml.draw.template")
853+ , ("stf", "application/vnd.wt.stf")
854+ , ("sti", "application/vnd.sun.xml.impress.template")
855+ , ("stk", "application/hyperstudio")
856+ , ("stl", "application/vnd.ms-pki.stl")
857+ , ("str", "application/vnd.pg.format")
858+ , ("stw", "application/vnd.sun.xml.writer.template")
859+ , ("sub", "text/vnd.dvb.subtitle")
860+ , ("sus", "application/vnd.sus-calendar")
861+ , ("susp", "application/vnd.sus-calendar")
862+ , ("sv4cpio", "application/x-sv4cpio")
863+ , ("sv4crc", "application/x-sv4crc")
864+ , ("svc", "application/vnd.dvb.service")
865+ , ("svd", "application/vnd.svd")
866+ , ("svg", "image/svg+xml")
867+ , ("svgz", "image/svg+xml")
868+ , ("swa", "application/x-director")
869+ , ("swf", "application/x-shockwave-flash")
870+ , ("swi", "application/vnd.aristanetworks.swi")
871+ , ("sxc", "application/vnd.sun.xml.calc")
872+ , ("sxd", "application/vnd.sun.xml.draw")
873+ , ("sxg", "application/vnd.sun.xml.writer.global")
874+ , ("sxi", "application/vnd.sun.xml.impress")
875+ , ("sxm", "application/vnd.sun.xml.math")
876+ , ("sxw", "application/vnd.sun.xml.writer")
877+ , ("t", "text/troff")
878+ , ("t3", "application/x-t3vm-image")
879+ , ("taglet", "application/vnd.mynfc")
880+ , ("tao", "application/vnd.tao.intent-module-archive")
881+ , ("tar", "application/x-tar")
882+ , ("tar.bz2", "application/x-bzip-compressed-tar")
883+ , ("tar.gz", "application/x-tgz")
884+ , ("tbz", "application/x-bzip-compressed-tar")
885+ , ("tcap", "application/vnd.3gpp2.tcap")
886+ , ("tcl", "application/x-tcl")
887+ , ("teacher", "application/vnd.smart.teacher")
888+ , ("tei", "application/tei+xml")
889+ , ("teicorpus", "application/tei+xml")
890+ , ("tex", "application/x-tex")
891+ , ("texi", "application/x-texinfo")
892+ , ("texinfo", "application/x-texinfo")
893+ , ("text", "text/plain")
894+ , ("tfi", "application/thraud+xml")
895+ , ("tfm", "application/x-tex-tfm")
896+ , ("tga", "image/x-tga")
897+ , ("tgz", "application/x-tgz")
898+ , ("thmx", "application/vnd.ms-officetheme")
899+ , ("tif", "image/tiff")
900+ , ("tiff", "image/tiff")
901+ , ("tk", "application/x-tcl")
902+ , ("tmo", "application/vnd.tmobile-livetv")
903+ , ("torrent", "application/x-bittorrent")
904+ , ("tpl", "application/vnd.groove-tool-template")
905+ , ("tpt", "application/vnd.trid.tpt")
906+ , ("tr", "text/troff")
907+ , ("tra", "application/vnd.trueapp")
908+ , ("trm", "application/x-msterminal")
909+ , ("tsd", "application/timestamped-data")
910+ , ("tsv", "text/tab-separated-values")
911+ , ("ttc", "application/x-font-ttf")
912+ , ("ttf", "application/x-font-ttf")
913+ , ("ttl", "text/turtle")
914+ , ("twd", "application/vnd.simtech-mindmapper")
915+ , ("twds", "application/vnd.simtech-mindmapper")
916+ , ("txd", "application/vnd.genomatix.tuxedo")
917+ , ("txf", "application/vnd.mobius.txf")
918+ , ("txt", "text/plain")
919+ , ("u32", "application/x-authorware-bin")
920+ , ("udeb", "application/x-debian-package")
921+ , ("ufd", "application/vnd.ufdl")
922+ , ("ufdl", "application/vnd.ufdl")
923+ , ("ulx", "application/x-glulx")
924+ , ("umj", "application/vnd.umajin")
925+ , ("unityweb", "application/vnd.unity")
926+ , ("uoml", "application/vnd.uoml+xml")
927+ , ("uri", "text/uri-list")
928+ , ("uris", "text/uri-list")
929+ , ("urls", "text/uri-list")
930+ , ("ustar", "application/x-ustar")
931+ , ("utz", "application/vnd.uiq.theme")
932+ , ("uu", "text/x-uuencode")
933+ , ("uva", "audio/vnd.dece.audio")
934+ , ("uvd", "application/vnd.dece.data")
935+ , ("uvf", "application/vnd.dece.data")
936+ , ("uvg", "image/vnd.dece.graphic")
937+ , ("uvh", "video/vnd.dece.hd")
938+ , ("uvi", "image/vnd.dece.graphic")
939+ , ("uvm", "video/vnd.dece.mobile")
940+ , ("uvp", "video/vnd.dece.pd")
941+ , ("uvs", "video/vnd.dece.sd")
942+ , ("uvt", "application/vnd.dece.ttml+xml")
943+ , ("uvu", "video/vnd.uvvu.mp4")
944+ , ("uvv", "video/vnd.dece.video")
945+ , ("uvva", "audio/vnd.dece.audio")
946+ , ("uvvd", "application/vnd.dece.data")
947+ , ("uvvf", "application/vnd.dece.data")
948+ , ("uvvg", "image/vnd.dece.graphic")
949+ , ("uvvh", "video/vnd.dece.hd")
950+ , ("uvvi", "image/vnd.dece.graphic")
951+ , ("uvvm", "video/vnd.dece.mobile")
952+ , ("uvvp", "video/vnd.dece.pd")
953+ , ("uvvs", "video/vnd.dece.sd")
954+ , ("uvvt", "application/vnd.dece.ttml+xml")
955+ , ("uvvu", "video/vnd.uvvu.mp4")
956+ , ("uvvv", "video/vnd.dece.video")
957+ , ("uvvx", "application/vnd.dece.unspecified")
958+ , ("uvvz", "application/vnd.dece.zip")
959+ , ("uvx", "application/vnd.dece.unspecified")
960+ , ("uvz", "application/vnd.dece.zip")
961+ , ("vcard", "text/vcard")
962+ , ("vcd", "application/x-cdlink")
963+ , ("vcf", "text/x-vcard")
964+ , ("vcg", "application/vnd.groove-vcard")
965+ , ("vcs", "text/x-vcalendar")
966+ , ("vcx", "application/vnd.vcx")
967+ , ("vis", "application/vnd.visionary")
968+ , ("viv", "video/vnd.vivo")
969+ , ("vob", "video/x-ms-vob")
970+ , ("vor", "application/vnd.stardivision.writer")
971+ , ("vox", "application/x-authorware-bin")
972+ , ("vrml", "model/vrml")
973+ , ("vsd", "application/vnd.visio")
974+ , ("vsf", "application/vnd.vsf")
975+ , ("vss", "application/vnd.visio")
976+ , ("vst", "application/vnd.visio")
977+ , ("vsw", "application/vnd.visio")
978+ , ("vtu", "model/vnd.vtu")
979+ , ("vxml", "application/voicexml+xml")
980+ , ("w3d", "application/x-director")
981+ , ("wad", "application/x-doom")
982+ , ("war", "application/java-archive")
983+ , ("wasm", "application/wasm")
984+ , ("wav", "audio/x-wav")
985+ , ("wax", "audio/x-ms-wax")
986+ , ("wbmp", "image/vnd.wap.wbmp")
987+ , ("wbs", "application/vnd.criticaltools.wbs+xml")
988+ , ("wbxml", "application/vnd.wap.wbxml")
989+ , ("wcm", "application/vnd.ms-works")
990+ , ("wdb", "application/vnd.ms-works")
991+ , ("wdp", "image/vnd.ms-photo")
992+ , ("weba", "audio/webm")
993+ , ("webm", "video/webm")
994+ , ("webp", "image/webp")
995+ , ("wg", "application/vnd.pmi.widget")
996+ , ("wgt", "application/widget")
997+ , ("wks", "application/vnd.ms-works")
998+ , ("wm", "video/x-ms-wm")
999+ , ("wma", "audio/x-ms-wma")
1000+ , ("wmd", "application/x-ms-wmd")
1001+ , ("wmf", "application/x-msmetafile")
1002+ , ("wml", "text/vnd.wap.wml")
1003+ , ("wmlc", "application/vnd.wap.wmlc")
1004+ , ("wmls", "text/vnd.wap.wmlscript")
1005+ , ("wmlsc", "application/vnd.wap.wmlscriptc")
1006+ , ("wmv", "video/x-ms-wmv")
1007+ , ("wmx", "video/x-ms-wmx")
1008+ , ("wmz", "application/x-msmetafile")
1009+ , ("woff", "application/font-woff")
1010+ , ("woff2", "application/font-woff2")
1011+ , ("wpd", "application/vnd.wordperfect")
1012+ , ("wpl", "application/vnd.ms-wpl")
1013+ , ("wps", "application/vnd.ms-works")
1014+ , ("wqd", "application/vnd.wqd")
1015+ , ("wri", "application/x-mswrite")
1016+ , ("wrl", "model/vrml")
1017+ , ("wsdl", "application/wsdl+xml")
1018+ , ("wspolicy", "application/wspolicy+xml")
1019+ , ("wtb", "application/vnd.webturbo")
1020+ , ("wvx", "video/x-ms-wvx")
1021+ , ("x32", "application/x-authorware-bin")
1022+ , ("x3d", "model/x3d+xml")
1023+ , ("x3db", "model/x3d+binary")
1024+ , ("x3dbz", "model/x3d+binary")
1025+ , ("x3dv", "model/x3d+vrml")
1026+ , ("x3dvz", "model/x3d+vrml")
1027+ , ("x3dz", "model/x3d+xml")
1028+ , ("xaml", "application/xaml+xml")
1029+ , ("xap", "application/x-silverlight-app")
1030+ , ("xar", "application/vnd.xara")
1031+ , ("xbap", "application/x-ms-xbap")
1032+ , ("xbd", "application/vnd.fujixerox.docuworks.binder")
1033+ , ("xbm", "image/x-xbitmap")
1034+ , ("xdf", "application/xcap-diff+xml")
1035+ , ("xdm", "application/vnd.syncml.dm+xml")
1036+ , ("xdp", "application/vnd.adobe.xdp+xml")
1037+ , ("xdssc", "application/dssc+xml")
1038+ , ("xdw", "application/vnd.fujixerox.docuworks")
1039+ , ("xenc", "application/xenc+xml")
1040+ , ("xer", "application/patch-ops-error+xml")
1041+ , ("xfdf", "application/vnd.adobe.xfdf")
1042+ , ("xfdl", "application/vnd.xfdl")
1043+ , ("xht", "application/xhtml+xml")
1044+ , ("xhtml", "application/xhtml+xml")
1045+ , ("xhvml", "application/xv+xml")
1046+ , ("xif", "image/vnd.xiff")
1047+ , ("xla", "application/vnd.ms-excel")
1048+ , ("xlam", "application/vnd.ms-excel.addin.macroenabled.12")
1049+ , ("xlc", "application/vnd.ms-excel")
1050+ , ("xlf", "application/x-xliff+xml")
1051+ , ("xlm", "application/vnd.ms-excel")
1052+ , ("xls", "application/vnd.ms-excel")
1053+ , ("xlsb", "application/vnd.ms-excel.sheet.binary.macroenabled.12")
1054+ , ("xlsm", "application/vnd.ms-excel.sheet.macroenabled.12")
1055+ , ("xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
1056+ , ("xlt", "application/vnd.ms-excel")
1057+ , ("xltm", "application/vnd.ms-excel.template.macroenabled.12")
1058+ , ("xltx", "application/vnd.openxmlformats-officedocument.spreadsheetml.template")
1059+ , ("xlw", "application/vnd.ms-excel")
1060+ , ("xm", "audio/xm")
1061+ , ("xml", "text/xml")
1062+ , ("xo", "application/vnd.olpc-sugar")
1063+ , ("xop", "application/xop+xml")
1064+ , ("xpi", "application/x-xpinstall")
1065+ , ("xpl", "application/xproc+xml")
1066+ , ("xpm", "image/x-xpixmap")
1067+ , ("xpr", "application/vnd.is-xpr")
1068+ , ("xps", "application/vnd.ms-xpsdocument")
1069+ , ("xpw", "application/vnd.intercon.formnet")
1070+ , ("xpx", "application/vnd.intercon.formnet")
1071+ , ("xsl", "application/xml")
1072+ , ("xslt", "application/xslt+xml")
1073+ , ("xsm", "application/vnd.syncml+xml")
1074+ , ("xspf", "application/xspf+xml")
1075+ , ("xul", "application/vnd.mozilla.xul+xml")
1076+ , ("xvm", "application/xv+xml")
1077+ , ("xvml", "application/xv+xml")
1078+ , ("xwd", "image/x-xwindowdump")
1079+ , ("xyz", "chemical/x-xyz")
1080+ , ("xz", "application/x-xz")
1081+ , ("yang", "application/yang")
1082+ , ("yin", "application/yin+xml")
1083+ , ("z1", "application/x-zmachine")
1084+ , ("z2", "application/x-zmachine")
1085+ , ("z3", "application/x-zmachine")
1086+ , ("z4", "application/x-zmachine")
1087+ , ("z5", "application/x-zmachine")
1088+ , ("z6", "application/x-zmachine")
1089+ , ("z7", "application/x-zmachine")
1090+ , ("z8", "application/x-zmachine")
1091+ , ("zaz", "application/vnd.zzazz.deck+xml")
1092+ , ("zip", "application/zip")
1093+ , ("zir", "application/vnd.zul")
1094+ , ("zirz", "application/vnd.zul")
1095+ , ("zmm", "application/vnd.handheld-entertainment+xml")
1096+ ]
mime-types/README.mdnew file+3
@@ -0,0 +1,3 @@
1+## mime-types
2+
3+Basic mime-type handling types and functions
Setup.lhs → mime-types/Setup.lhsrenamed

The content is the same.

mime-types/mime-types.cabalnew file+24
@@ -0,0 +1,24 @@
1+name: mime-types
2+version: 0.1.0.9
3+synopsis: Basic mime-type handling types and functions
4+description: API docs and the README are available at <http://www.stackage.org/package/mime-types>.
5+homepage: https://github.com/yesodweb/wai
6+license: MIT
7+license-file: LICENSE
8+author: Michael Snoyman
9+maintainer: michael@snoyman.com
10+category: Web
11+build-type: Simple
12+cabal-version: >=1.8
13+extra-source-files: README.md ChangeLog.md
14+
15+library
16+ exposed-modules: Network.Mime
17+ build-depends: base >= 4 && < 5
18+ , containers
19+ , text
20+ , bytestring
21+
22+source-repository head
23+ type: git
24+ location: git://github.com/yesodweb/wai.git
runtests.hsdeleted−196
@@ -1,196 +0,0 @@
1−import Test.Framework (defaultMain, testGroup, Test)
2−import Test.Framework.Providers.HUnit
3−import Test.HUnit hiding (Test)
4−
5−import Network.Wai
6−import Network.Wai.Parse
7−import qualified Data.ByteString.Char8 as S8
8−import qualified Data.ByteString.Lazy.Char8 as L8
9−import Control.Arrow
10−
11−main :: IO ()
12−main = defaultMain [testSuite]
13−
14−testSuite :: Test
15−testSuite = testGroup "Network.Wai.Parse"
16− [ testCase "parseQueryString" caseParseQueryString
17− , testCase "parseQueryString with question mark" caseParseQueryStringQM
18− , testCase "parseCookies" caseParseCookies
19− , testCase "parseHttpAccept" caseParseHttpAccept
20− , testCase "parseRequestBody" caseParseRequestBody
21− , testCase "findBound" caseFindBound
22− , testCase "sinkTillBound" caseSinkTillBound
23− ]
24−
25−caseParseQueryString :: Assertion
26−caseParseQueryString = do
27− let go l r =
28− map (S8.pack *** S8.pack) l @=? parseQueryString (S8.pack r)
29−
30− go [] ""
31− go [("foo", "")] "foo"
32− go [("foo", "bar")] "foo=bar"
33− go [("foo", "bar"), ("baz", "bin")] "foo=bar&baz=bin"
34− go [("%Q", "")] "%Q"
35− go [("%1Q", "")] "%1Q"
36− go [("%1", "")] "%1"
37− go [("/", "")] "%2F"
38− go [("/", "")] "%2f"
39− go [("foo bar", "")] "foo+bar"
40−
41−caseParseQueryStringQM :: Assertion
42−caseParseQueryStringQM = do
43− let go l r =
44− map (S8.pack *** S8.pack) l
45− @=? parseQueryString (S8.pack $ '?' : r)
46−
47− go [] ""
48− go [("foo", "")] "foo"
49− go [("foo", "bar")] "foo=bar"
50− go [("foo", "bar"), ("baz", "bin")] "foo=bar&baz=bin"
51− go [("%Q", "")] "%Q"
52− go [("%1Q", "")] "%1Q"
53− go [("%1", "")] "%1"
54− go [("/", "")] "%2F"
55− go [("/", "")] "%2f"
56− go [("foo bar", "")] "foo+bar"
57−
58−caseParseCookies :: Assertion
59−caseParseCookies = do
60− let input = S8.pack "a=a1;b=b2; c=c3"
61− expected = [("a", "a1"), ("b", "b2"), ("c", "c3")]
62− map (S8.pack *** S8.pack) expected @=? parseCookies input
63−
64−caseParseHttpAccept :: Assertion
65−caseParseHttpAccept = do
66− let input =
67− S8.pack "text/plain; q=0.5, text/html, text/x-dvi; q=0.8, text/x-c"
68− expected = ["text/html", "text/x-c", "text/x-dvi", "text/plain"]
69− map S8.pack expected @=? parseHttpAccept input
70−
71−caseParseRequestBody :: Assertion
72−caseParseRequestBody = t where
73− content2 = S8.pack $
74− "--AaB03x\n" ++
75− "Content-Disposition: form-data; name=\"document\"; filename=\"b.txt\"\n" ++
76− "Content-Type: text/plain; charset=iso-8859-1\n\n" ++
77− "This is a file.\n" ++
78− "It has two lines.\n" ++
79− "--AaB03x\n" ++
80− "Content-Disposition: form-data; name=\"title\"\n" ++
81− "Content-Type: text/plain; charset=iso-8859-1\n\n" ++
82− "A File\n" ++
83− "--AaB03x\n" ++
84− "Content-Disposition: form-data; name=\"summary\"\n" ++
85− "Content-Type: text/plain; charset=iso-8859-1\n\n" ++
86− "This is my file\n" ++
87− "file test\n" ++
88− "--AaB03x--"
89− content3 = S8.pack "------WebKitFormBoundaryB1pWXPZ6lNr8RiLh\r\nContent-Disposition: form-data; name=\"yaml\"; filename=\"README\"\r\nContent-Type: application/octet-stream\r\n\r\nPhoto blog using Hack.\n\r\n------WebKitFormBoundaryB1pWXPZ6lNr8RiLh--\r\n"
90− t = do
91− let content1 = S8.pack "foo=bar&baz=bin"
92− let ctype1 = S8.pack "application/x-www-form-urlencoded"
93− result1 <- parseRequestBody lbsSink $ toRequest ctype1 content1
94− assertEqual "parsing post x-www-form-urlencoded"
95− (map (S8.pack *** S8.pack) [("foo", "bar"), ("baz", "bin")], [])
96− result1
97−
98− let ctype2 = S8.pack "multipart/form-data; boundary=AaB03x"
99− result2 <- parseRequestBody lbsSink $ toRequest ctype2 content2
100− let expectedsmap2 =
101− [ ("title", "A File")
102− , ("summary", "This is my file\nfile test")
103− ]
104− let textPlain = S8.pack $ "text/plain; charset=iso-8859-1"
105− let expectedfile2 =
106− [(S8.pack "document", FileInfo (S8.pack "b.txt") textPlain $ L8.pack
107− "This is a file.\nIt has two lines.")]
108− let expected2 = (map (S8.pack *** S8.pack) expectedsmap2, expectedfile2)
109− assertEqual "parsing post multipart/form-data"
110− expected2
111− result2
112−
113− let ctype3 = S8.pack "multipart/form-data; boundary=----WebKitFormBoundaryB1pWXPZ6lNr8RiLh"
114− result3 <- parseRequestBody lbsSink $ toRequest ctype3 content3
115− let expectedsmap3 = []
116− let expectedfile3 = [(S8.pack "yaml", FileInfo (S8.pack "README") (S8.pack "application/octet-stream") $
117− L8.pack "Photo blog using Hack.\n")]
118− let expected3 = (expectedsmap3, expectedfile3)
119− assertEqual "parsing actual post multipart/form-data"
120− expected3
121− result3
122−
123− result2' <- parseRequestBody lbsSink $ toRequest' ctype2 content2
124− assertEqual "parsing post multipart/form-data 2"
125− expected2
126− result2'
127−
128− result3' <- parseRequestBody lbsSink $ toRequest' ctype3 content3
129− assertEqual "parsing actual post multipart/form-data 2"
130− expected3
131− result3'
132−
133−toRequest :: S8.ByteString -> S8.ByteString -> Request
134−toRequest ctype content = Request
135− { requestHeaders = [(ReqContentType, ctype)]
136− , requestBody = toSource content
137− , requestMethod = undefined
138− , httpVersion = undefined
139− , pathInfo = undefined
140− , queryString = undefined
141− , serverName = undefined
142− , serverPort = undefined
143− , urlScheme = undefined
144− , errorHandler = undefined
145− , remoteHost = undefined
146− }
147−
148−toRequest' :: S8.ByteString -> S8.ByteString -> Request
149−toRequest' ctype content = Request
150− { requestHeaders = [(ReqContentType, ctype)]
151− , requestBody = toSource' content
152− , requestMethod = undefined
153− , httpVersion = undefined
154− , pathInfo = undefined
155− , queryString = undefined
156− , serverName = undefined
157− , serverPort = undefined
158− , urlScheme = undefined
159− , errorHandler = undefined
160− , remoteHost = undefined
161− }
162−
163−toSource :: S8.ByteString -> Source
164−toSource bs = Source $
165− case S8.uncons bs of
166− Nothing -> return Nothing
167− Just (x, xs) -> return $ Just (S8.singleton x, toSource xs)
168−
169−toSource' :: S8.ByteString -> Source
170−toSource' bs = Source $ return $ Just (bs, Source $ return Nothing)
171−
172−caseFindBound :: Assertion
173−caseFindBound = do
174− findBound (S8.pack "def") (S8.pack "abcdefghi") @?=
175− FoundBound (S8.pack "abc") (S8.pack "ghi")
176− findBound (S8.pack "def") (S8.pack "ABC") @?= NoBound
177− findBound (S8.pack "def") (S8.pack "abcd") @?= PartialBound
178− findBound (S8.pack "def") (S8.pack "abcdE") @?= NoBound
179− findBound (S8.pack "def") (S8.pack "abcdEdef") @?=
180− FoundBound (S8.pack "abcdE") (S8.pack "")
181−
182−caseSinkTillBound :: Assertion
183−caseSinkTillBound = do
184− caseSinkTillBoundHelper $ Just . toSource
185− caseSinkTillBoundHelper $ Just . toSource'
186−
187−caseSinkTillBoundHelper :: (S8.ByteString -> Maybe Source) -> Assertion
188−caseSinkTillBoundHelper tosrc = do
189− let iter () _ = return ()
190− let src = S8.pack "this is some text"
191− bound1 = S8.pack "some"
192− bound2 = S8.pack "some!"
193− (_, res1, _) <- sinkTillBound bound1 (S8.empty, tosrc src) iter ()
194− res1 @?= True
195− (_, res2, _) <- sinkTillBound bound2 (S8.empty, tosrc src) iter ()
196− res2 @?= False
stack-lts-11.yamlnew file+27
@@ -0,0 +1,27 @@
1+resolver: lts-11.21
2+packages:
3+- ./auto-update
4+- ./mime-types
5+- ./wai
6+- ./wai-extra
7+- ./warp
8+- ./warp-tls
9+- ./wai-app-static
10+- ./wai-websockets
11+- ./wai-conduit
12+- ./time-manager
13+flags:
14+ wai-extra:
15+ build-example: true
16+extra-deps:
17+- streaming-commons-0.2.1.0@sha256:163af02944f374f316e35257c1afbffefd2c2852a85a151d1ea0ad22152cf28d,4990
18+- http2-2.0.0
19+- network-byte-order-0.1.1.0
20+- tls-1.5.3
21+- tls-session-manager-0.0.4
22+- x509-1.7.5
23+nix:
24+ enable: false
25+ packages:
26+ - fcgi
27+ - zlib
stack-lts-12.yamlnew file+26
@@ -0,0 +1,26 @@
1+resolver: lts-12.6
2+packages:
3+- ./auto-update
4+- ./mime-types
5+- ./wai
6+- ./wai-extra
7+- ./warp
8+- ./warp-tls
9+- ./wai-app-static
10+- ./wai-websockets
11+- ./wai-conduit
12+- ./time-manager
13+flags:
14+ wai-extra:
15+ build-example: true
16+nix:
17+ enable: false
18+ packages:
19+ - fcgi
20+ - zlib
21+extra-deps:
22+- http2-2.0.0
23+- network-byte-order-0.1.1.0
24+- tls-1.5.3
25+- tls-session-manager-0.0.4
26+- x509-1.7.5
stack-nightly.yamlnew file+20
@@ -0,0 +1,20 @@
1+resolver: nightly-2020-01-08
2+packages:
3+- ./auto-update
4+- ./mime-types
5+- ./wai
6+- ./wai-extra
7+- ./warp
8+- ./warp-tls
9+- ./wai-app-static
10+- ./wai-websockets
11+- ./wai-conduit
12+- ./time-manager
13+flags:
14+ wai-extra:
15+ build-example: true
16+nix:
17+ enable: false
18+ packages:
19+ - fcgi
20+ - zlib
stack.yamlnew file+25
@@ -0,0 +1,25 @@
1+resolver: lts-14.22
2+packages:
3+- ./auto-update
4+- ./mime-types
5+- ./wai
6+- ./wai-extra
7+- ./warp
8+- ./warp-tls
9+- ./wai-app-static
10+- ./wai-websockets
11+- ./wai-conduit
12+- ./time-manager
13+flags:
14+ wai-extra:
15+ build-example: true
16+nix:
17+ enable: false
18+ packages:
19+ - fcgi
20+ - zlib
21+extra-deps:
22+- http2-2.0.0
23+- network-byte-order-0.1.1.0
24+- tls-1.5.3
25+- tls-session-manager-0.0.4
test/sample.hsdeleted−25
@@ -1,25 +0,0 @@
1−import Data.ByteString.Char8 (unpack, pack)
2−import Data.ByteString.Lazy (fromChunks)
3−import Network.Wai
4−import Network.Wai.Enumerator
5−import Network.Wai.Middleware.Gzip
6−import Network.Wai.Middleware.Jsonp
7−import Network.Wai.Middleware.CleanPath
8−import Network.Wai.Handler.SimpleServer
9−
10−app :: [String] -> Application
11−app [] _ = return $ Response Status200 [] $ Right $ fromLBS
12− $ fromChunks $ flip map [1..10000] $ \i -> pack $
13− concat
14− [ "<p>Just this same paragraph again. "
15− , show i
16− , "</p>"
17− ]
18−app ["test.html"] _ = return $ Response Status200 [] $ Left "test.html"
19−app ["json"] _ =return $ Response Status200
20− [(ContentType, pack "application/json")]
21− $ Left "json"
22−app _ _ = return $ Response Status404 [] $ Left "../LICENSE"
23−
24−main :: IO ()
25−main = run 3000 $ jsonp $ gzip $ cleanPath app
time-manager/LICENSEnew file+20
@@ -0,0 +1,20 @@
1+Copyright (c) 2012 Michael Snoyman, http://www.yesodweb.com/
2+
3+Permission is hereby granted, free of charge, to any person obtaining
4+a copy of this software and associated documentation files (the
5+"Software"), to deal in the Software without restriction, including
6+without limitation the rights to use, copy, modify, merge, publish,
7+distribute, sublicense, and/or sell copies of the Software, and to
8+permit persons to whom the Software is furnished to do so, subject to
9+the following conditions:
10+
11+The above copyright notice and this permission notice shall be
12+included in all copies or substantial portions of the Software.
13+
14+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
time-manager/System/TimeManager.hsnew file+152
@@ -0,0 +1,152 @@
1+{-# LANGUAGE DeriveDataTypeable #-}
2+
3+module System.TimeManager (
4+ -- ** Types
5+ Manager
6+ , TimeoutAction
7+ , Handle
8+ -- ** Manager
9+ , initialize
10+ , stopManager
11+ , killManager
12+ , withManager
13+ -- ** Registration
14+ , register
15+ , registerKillThread
16+ -- ** Control
17+ , tickle
18+ , cancel
19+ , pause
20+ , resume
21+ -- ** Exceptions
22+ , TimeoutThread (..)
23+ ) where
24+
25+import Control.Concurrent (myThreadId)
26+import qualified Control.Exception as E
27+import Control.Reaper
28+import Data.Typeable (Typeable)
29+import Data.IORef (IORef)
30+import qualified Data.IORef as I
31+
32+----------------------------------------------------------------
33+
34+-- | A timeout manager
35+type Manager = Reaper [Handle] Handle
36+
37+-- | An action to be performed on timeout.
38+type TimeoutAction = IO ()
39+
40+-- | A handle used by 'Manager'
41+data Handle = Handle !(IORef TimeoutAction) !(IORef State)
42+
43+data State = Active -- Manager turns it to Inactive.
44+ | Inactive -- Manager removes it with timeout action.
45+ | Paused -- Manager does not change it.
46+ | Canceled -- Manager removes it without timeout action.
47+
48+----------------------------------------------------------------
49+
50+-- | Creating timeout manager which works every N micro seconds
51+-- where N is the first argument.
52+initialize :: Int -> IO Manager
53+initialize timeout = mkReaper defaultReaperSettings
54+ { reaperAction = mkListAction prune
55+ , reaperDelay = timeout
56+ }
57+ where
58+ prune m@(Handle actionRef stateRef) = do
59+ state <- I.atomicModifyIORef' stateRef (\x -> (inactivate x, x))
60+ case state of
61+ Inactive -> do
62+ onTimeout <- I.readIORef actionRef
63+ onTimeout `E.catch` ignoreAll
64+ return Nothing
65+ Canceled -> return Nothing
66+ _ -> return $ Just m
67+
68+ inactivate Active = Inactive
69+ inactivate x = x
70+
71+----------------------------------------------------------------
72+
73+-- | Stopping timeout manager with onTimeout fired.
74+stopManager :: Manager -> IO ()
75+stopManager mgr = E.mask_ (reaperStop mgr >>= mapM_ fire)
76+ where
77+ fire (Handle actionRef _) = do
78+ onTimeout <- I.readIORef actionRef
79+ onTimeout `E.catch` ignoreAll
80+
81+ignoreAll :: E.SomeException -> IO ()
82+ignoreAll _ = return ()
83+
84+-- | Killing timeout manager immediately without firing onTimeout.
85+killManager :: Manager -> IO ()
86+killManager = reaperKill
87+
88+----------------------------------------------------------------
89+
90+-- | Registering a timeout action.
91+register :: Manager -> TimeoutAction -> IO Handle
92+register mgr onTimeout = do
93+ actionRef <- I.newIORef onTimeout
94+ stateRef <- I.newIORef Active
95+ let h = Handle actionRef stateRef
96+ reaperAdd mgr h
97+ return h
98+
99+-- | Registering a timeout action of killing this thread.
100+registerKillThread :: Manager -> TimeoutAction -> IO Handle
101+registerKillThread m onTimeout = do
102+ -- If we hold ThreadId, the stack and data of the thread is leaked.
103+ -- If we hold Weak ThreadId, the stack is released. However, its
104+ -- data is still leaked probably because of a bug of GHC.
105+ -- So, let's just use ThreadId and release ThreadId by
106+ -- overriding the timeout action by "cancel".
107+ tid <- myThreadId
108+ -- First run the timeout action in case the child thread is masked.
109+ register m $ onTimeout `E.finally` E.throwTo tid TimeoutThread
110+
111+data TimeoutThread = TimeoutThread
112+ deriving Typeable
113+instance E.Exception TimeoutThread where
114+ toException = E.asyncExceptionToException
115+ fromException = E.asyncExceptionFromException
116+instance Show TimeoutThread where
117+ show TimeoutThread = "Thread killed by timeout manager"
118+
119+----------------------------------------------------------------
120+
121+-- | Setting the state to active.
122+-- 'Manager' turns active to inactive repeatedly.
123+tickle :: Handle -> IO ()
124+tickle (Handle _ stateRef) = I.writeIORef stateRef Active
125+
126+-- | Setting the state to canceled.
127+-- 'Manager' eventually removes this without timeout action.
128+cancel :: Handle -> IO ()
129+cancel (Handle actionRef stateRef) = do
130+ I.writeIORef actionRef (return ()) -- ensuring to release ThreadId
131+ I.writeIORef stateRef Canceled
132+
133+-- | Setting the state to paused.
134+-- 'Manager' does not change the value.
135+pause :: Handle -> IO ()
136+pause (Handle _ stateRef) = I.writeIORef stateRef Paused
137+
138+-- | Setting the paused state to active.
139+-- This is an alias to 'tickle'.
140+resume :: Handle -> IO ()
141+resume = tickle
142+
143+----------------------------------------------------------------
144+
145+-- | Call the inner function with a timeout manager.
146+withManager :: Int -- ^ timeout in microseconds
147+ -> (Manager -> IO a)
148+ -> IO a
149+withManager timeout f = do
150+ -- FIXME when stopManager is available, use it
151+ man <- initialize timeout
152+ f man
time-manager/time-manager.cabalnew file+19
@@ -0,0 +1,19 @@
1+Name: time-manager
2+Version: 0.0.0
3+Synopsis: Scalable timer
4+License: MIT
5+License-file: LICENSE
6+Author: Michael Snoyman and Kazu Yamamoto
7+Maintainer: kazu@iij.ad.jp
8+Homepage: http://github.com/yesodweb/wai
9+Category: System
10+Build-Type: Simple
11+Cabal-Version: >=1.8
12+Stability: Stable
13+Description: Scalable timer functions provided by a timer manager.
14+
15+Library
16+ Build-Depends: base >= 4.8 && < 5
17+ , auto-update
18+ Exposed-modules: System.TimeManager
19+ Ghc-Options: -Wall
wai-app-static/.ghcinew file+1
@@ -0,0 +1 @@
1+:set -itest -optP-include -optPdist/build/autogen/cabal_macros.h -XCPP
wai-app-static/.gitignorenew file+2
@@ -0,0 +1,2 @@
1+dist/
2+cabal-dev
wai-app-static/ChangeLog.mdnew file+76
@@ -0,0 +1,76 @@
1+# wai-app-static changelog
2+
3+## 3.1.7.1
4+
5+* Update the test suite too
6+
7+## 3.1.7
8+
9+* Use 302 instead of 301 redirect, to avoid caching the presence of an index.html file
10+
11+## 3.1.6.3
12+
13+* The executable warp obeys `-h` option properly for host
14+now. Previously this used to invoke the help option. That can be
15+reached via `--help` as before.
16+
17+## 3.1.6.2
18+
19+* Drop dependency on blaze-builder
20+
21+## 3.1.6.1
22+
23+* Add `<>` import
24+
25+## 3.1.6
26+
27+* Make ssAddTrailingSlash work in combination with ssIndices [#569](https://github.com/yesodweb/wai/pull/569)
28+* Make ssIndices work with ssLookupFile and trailing slashes [#570](https://github.com/yesodweb/wai/pull/570)
29+
30+## 3.1.5
31+
32+* Switch to cryponite
33+
34+## 3.1.4.1
35+
36+* Support wai/warp 3.2
37+
38+## 3.1.4
39+
40+* Reinstate redirectToIndex
41+
42+## 3.1.3
43+
44+* Add 404 handler [#467](https://github.com/yesodweb/wai/pull/467)
45+
46+## 3.1.2
47+
48+* Honor ssIndices when used with defaultWebAppSettings [#460](https://github.com/yesodweb/wai/pull/460)
49+
50+## 3.1.1
51+
52+* Make adding a trailing slash optional [#327](https://github.com/yesodweb/wai/issues/327) [yesod#988](https://github.com/yesodweb/yesod/issues/988)
53+
54+## 3.1.0
55+
56+* Drop system-filepath
57+
58+## 3.0.1.1
59+
60+* Fix root links
61+
62+## 3.0.1
63+
64+* Better HEAD support [#354](https://github.com/yesodweb/wai/issues/354)
65+
66+## 3.0.0.6
67+
68+Fix trailing slashes for `UrlMap` and other non-root setups [#325](https://github.com/yesodweb/wai/issues/325)
69+
70+## 3.0.0.4
71+
72+Add missing trailing slashes [#312](https://github.com/yesodweb/wai/issues/312)
73+
74+## 3.0.0.3
75+
76+Support for time 1.5
wai-app-static/LICENSEnew file+20
@@ -0,0 +1,20 @@
1+Copyright (c) 2012 Michael Snoyman, http://www.yesodweb.com/
2+
3+Permission is hereby granted, free of charge, to any person obtaining
4+a copy of this software and associated documentation files (the
5+"Software"), to deal in the Software without restriction, including
6+without limitation the rights to use, copy, modify, merge, publish,
7+distribute, sublicense, and/or sell copies of the Software, and to
8+permit persons to whom the Software is furnished to do so, subject to
9+the following conditions:
10+
11+The above copyright notice and this permission notice shall be
12+included in all copies or substantial portions of the Software.
13+
14+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
wai-app-static/Network/Wai/Application/Static.hsnew file+289
@@ -0,0 +1,289 @@
1+{-# LANGUAGE OverloadedStrings #-}
2+{-# LANGUAGE ScopedTypeVariables #-}
3+{-# LANGUAGE TemplateHaskell, CPP #-}
4+{-# LANGUAGE RecordWildCards #-}
5+{-# LANGUAGE PatternGuards #-}
6+-- | Static file serving for WAI.
7+module Network.Wai.Application.Static
8+ ( -- * WAI application
9+ staticApp
10+ -- ** Default Settings
11+ , defaultWebAppSettings
12+ , webAppSettingsWithLookup
13+ , defaultFileServerSettings
14+ , embeddedSettings
15+ -- ** Settings
16+ , StaticSettings
17+ , ssLookupFile
18+ , ssMkRedirect
19+ , ssGetMimeType
20+ , ssListing
21+ , ssIndices
22+ , ssMaxAge
23+ , ssRedirectToIndex
24+ , ssAddTrailingSlash
25+ , ss404Handler
26+ ) where
27+
28+import Prelude hiding (FilePath)
29+import qualified Network.Wai as W
30+import qualified Network.HTTP.Types as H
31+import Data.ByteString (ByteString)
32+import qualified Data.ByteString.Char8 as S8
33+import qualified Data.ByteString.Lazy as L
34+import Data.ByteString.Lazy.Char8 ()
35+import Control.Monad.IO.Class (liftIO)
36+
37+import Data.ByteString.Builder (toLazyByteString)
38+
39+import Data.FileEmbed (embedFile)
40+
41+import Data.Text (Text)
42+import qualified Data.Text as T
43+import qualified Data.Text.Encoding as TE
44+
45+import Network.HTTP.Date (parseHTTPDate, epochTimeToHTTPDate, formatHTTPDate)
46+
47+import WaiAppStatic.Types
48+import Util
49+import WaiAppStatic.Storage.Filesystem
50+import WaiAppStatic.Storage.Embedded
51+import Network.Mime (MimeType)
52+
53+data StaticResponse =
54+ -- | Just the etag hash or Nothing for no etag hash
55+ Redirect Pieces (Maybe ByteString)
56+ | RawRedirect ByteString
57+ | NotFound
58+ | FileResponse File H.ResponseHeaders
59+ | NotModified
60+ -- TODO: add file size
61+ | SendContent MimeType L.ByteString
62+ | WaiResponse W.Response
63+
64+safeInit :: [a] -> [a]
65+safeInit [] = []
66+safeInit xs = init xs
67+
68+filterButLast :: (a -> Bool) -> [a] -> [a]
69+filterButLast _ [] = []
70+filterButLast _ [x] = [x]
71+filterButLast f (x:xs)
72+ | f x = x : filterButLast f xs
73+ | otherwise = filterButLast f xs
74+
75+-- | Serve an appropriate response for a folder request.
76+serveFolder :: StaticSettings -> Pieces -> W.Request -> Folder -> IO StaticResponse
77+serveFolder StaticSettings {..} pieces req folder@Folder {..} =
78+ case ssListing of
79+ Just _ | Just path <- addTrailingSlash req, ssAddTrailingSlash ->
80+ return $ RawRedirect path
81+ Just listing -> do
82+ -- directory listings turned on, display it
83+ builder <- listing pieces folder
84+ return $ WaiResponse $ W.responseBuilder H.status200
85+ [ ("Content-Type", "text/html; charset=utf-8")
86+ ] builder
87+ Nothing -> return $ WaiResponse $ W.responseLBS H.status403
88+ [ ("Content-Type", "text/plain")
89+ ] "Directory listings disabled"
90+
91+addTrailingSlash :: W.Request -> Maybe ByteString
92+addTrailingSlash req
93+ | S8.null rp = Just "/"
94+ | S8.last rp == '/' = Nothing
95+ | otherwise = Just $ S8.snoc rp '/'
96+ where
97+ rp = W.rawPathInfo req
98+
99+checkPieces :: StaticSettings
100+ -> Pieces -- ^ parsed request
101+ -> W.Request
102+ -> IO StaticResponse
103+-- If we have any empty pieces in the middle of the requested path, generate a
104+-- redirect to get rid of them.
105+checkPieces _ pieces _ | any (T.null . fromPiece) $ safeInit pieces =
106+ return $ Redirect (filterButLast (not . T.null . fromPiece) pieces) Nothing
107+
108+checkPieces ss@StaticSettings {..} pieces req = do
109+ res <- lookupResult
110+ case res of
111+ Left location -> return $ RawRedirect location
112+ Right LRNotFound -> return NotFound
113+ Right (LRFile file) -> serveFile ss req file
114+ Right (LRFolder folder) -> serveFolder ss pieces req folder
115+ where
116+ lookupResult :: IO (Either ByteString LookupResult)
117+ lookupResult = do
118+ nonIndexResult <- ssLookupFile pieces
119+ case nonIndexResult of
120+ LRFile{} -> return $ Right nonIndexResult
121+ _ -> do
122+ eIndexResult <- lookupIndices (map (\ index -> dropLastIfNull pieces ++ [index]) ssIndices)
123+ return $ case eIndexResult of
124+ Left redirect -> Left redirect
125+ Right indexResult -> case indexResult of
126+ LRNotFound -> Right nonIndexResult
127+ LRFile file | ssRedirectToIndex ->
128+ let relPath =
129+ case reverse pieces of
130+ -- Served at root
131+ [] -> fromPiece $ fileName file
132+ lastSegment:_ ->
133+ case fromPiece lastSegment of
134+ -- Ends with a trailing slash
135+ "" -> fromPiece $ fileName file
136+ -- Lacks a trailing slash
137+ lastSegment' -> T.concat
138+ [ lastSegment'
139+ , "/"
140+ , fromPiece $ fileName file
141+ ]
142+ in Left $ TE.encodeUtf8 relPath
143+ _ -> Right indexResult
144+
145+ lookupIndices :: [Pieces] -> IO (Either ByteString LookupResult)
146+ lookupIndices (x : xs) = do
147+ res <- ssLookupFile x
148+ case res of
149+ LRNotFound -> lookupIndices xs
150+ _ -> return $ case (ssAddTrailingSlash, addTrailingSlash req) of
151+ (True, Just redirect) -> Left redirect
152+ _ -> Right res
153+ lookupIndices [] = return $ Right LRNotFound
154+
155+serveFile :: StaticSettings -> W.Request -> File -> IO StaticResponse
156+serveFile StaticSettings {..} req file
157+ -- First check etag values, if turned on
158+ | ssUseHash = do
159+ mHash <- fileGetHash file
160+ case (mHash, lookup "if-none-match" $ W.requestHeaders req) of
161+ -- if-none-match matches the actual hash, return a 304
162+ (Just hash, Just lastHash) | hash == lastHash -> return NotModified
163+
164+ -- Didn't match, but we have a hash value. Send the file contents
165+ -- with an ETag header.
166+ --
167+ -- RFC7232 (HTTP 1.1):
168+ -- > A recipient MUST ignore If-Modified-Since if the request contains an
169+ -- > If-None-Match header field; the condition in If-None-Match is
170+ -- > considered to be a more accurate replacement for the condition in
171+ -- > If-Modified-Since, and the two are only combined for the sake of
172+ -- > interoperating with older intermediaries that might not implement
173+ -- > If-None-Match.
174+ (Just hash, _) -> respond [("ETag", hash)]
175+
176+ -- No hash value available, fall back to last modified support.
177+ (Nothing, _) -> lastMod
178+ -- etag turned off, so jump straight to last modified
179+ | otherwise = lastMod
180+ where
181+ mLastSent = lookup "if-modified-since" (W.requestHeaders req) >>= parseHTTPDate
182+ lastMod =
183+ case (fmap epochTimeToHTTPDate $ fileGetModified file, mLastSent) of
184+ -- File modified time is equal to the if-modified-since header,
185+ -- return a 304.
186+ --
187+ -- Question: should the comparison be, date <= lastSent?
188+ (Just mdate, Just lastSent)
189+ | mdate == lastSent -> return NotModified
190+
191+ -- Did not match, but we have a new last-modified header
192+ (Just mdate, _) -> respond [("last-modified", formatHTTPDate mdate)]
193+
194+ -- No modification time available
195+ (Nothing, _) -> respond []
196+
197+ -- Send a file response with the additional weak headers provided.
198+ respond headers = return $ FileResponse file $ cacheControl ssMaxAge headers
199+
200+-- | Return a difference list of headers based on the specified MaxAge.
201+--
202+-- This function will return both Cache-Control and Expires headers, as
203+-- relevant.
204+cacheControl :: MaxAge -> (H.ResponseHeaders -> H.ResponseHeaders)
205+cacheControl maxage =
206+ headerCacheControl . headerExpires
207+ where
208+ ccInt =
209+ case maxage of
210+ NoMaxAge -> Nothing
211+ MaxAgeSeconds i -> Just i
212+ MaxAgeForever -> Just oneYear
213+ oneYear :: Int
214+ oneYear = 60 * 60 * 24 * 365
215+
216+ headerCacheControl =
217+ case ccInt of
218+ Nothing -> id
219+ Just i -> (:) ("Cache-Control", S8.append "public, max-age=" $ S8.pack $ show i)
220+ headerExpires =
221+ case maxage of
222+ NoMaxAge -> id
223+ MaxAgeSeconds _ -> id -- FIXME
224+ MaxAgeForever -> (:) ("Expires", "Thu, 31 Dec 2037 23:55:55 GMT")
225+
226+-- | Turn a @StaticSettings@ into a WAI application.
227+staticApp :: StaticSettings -> W.Application
228+staticApp set req = staticAppPieces set (W.pathInfo req) req
229+
230+staticAppPieces :: StaticSettings -> [Text] -> W.Application
231+staticAppPieces _ _ req sendResponse
232+ | notElem (W.requestMethod req) ["GET", "HEAD"] = sendResponse $ W.responseLBS
233+ H.status405
234+ [("Content-Type", "text/plain")]
235+ "Only GET or HEAD is supported"
236+staticAppPieces _ [".hidden", "folder.png"] _ sendResponse = sendResponse $ W.responseLBS H.status200 [("Content-Type", "image/png")] $ L.fromChunks [$(embedFile "images/folder.png")]
237+staticAppPieces _ [".hidden", "haskell.png"] _ sendResponse = sendResponse $ W.responseLBS H.status200 [("Content-Type", "image/png")] $ L.fromChunks [$(embedFile "images/haskell.png")]
238+staticAppPieces ss rawPieces req sendResponse = liftIO $ do
239+ case toPieces rawPieces of
240+ Just pieces -> checkPieces ss pieces req >>= response
241+ Nothing -> sendResponse $ W.responseLBS H.status403
242+ [ ("Content-Type", "text/plain")
243+ ] "Forbidden"
244+ where
245+ response :: StaticResponse -> IO W.ResponseReceived
246+ response (FileResponse file ch) = do
247+ mimetype <- ssGetMimeType ss file
248+ -- let filesize = fileGetSize file
249+ let headers = ("Content-Type", mimetype)
250+ -- Let Warp provide the content-length, since it takes
251+ -- range requests into account
252+ -- : ("Content-Length", S8.pack $ show filesize)
253+ : ch
254+ sendResponse $ fileToResponse file H.status200 headers
255+
256+ response NotModified =
257+ sendResponse $ W.responseLBS H.status304 [] ""
258+
259+ response (SendContent mt lbs) = do
260+ -- TODO: set caching headers
261+ sendResponse $ W.responseLBS H.status200
262+ [ ("Content-Type", mt)
263+ -- TODO: set Content-Length
264+ ] lbs
265+
266+ response (Redirect pieces' mHash) = do
267+ let loc = ssMkRedirect ss pieces' $ L.toStrict $ toLazyByteString (H.encodePathSegments $ map fromPiece pieces')
268+ let qString = case mHash of
269+ Just hash -> replace "etag" (Just hash) (W.queryString req)
270+ Nothing -> remove "etag" (W.queryString req)
271+
272+ sendResponse $ W.responseLBS H.status302
273+ [ ("Content-Type", "text/plain")
274+ , ("Location", S8.append loc $ H.renderQuery True qString)
275+ ] "Redirect"
276+
277+ response (RawRedirect path) =
278+ sendResponse $ W.responseLBS H.status302
279+ [ ("Content-Type", "text/plain")
280+ , ("Location", path)
281+ ] "Redirect"
282+
283+ response NotFound = case (ss404Handler ss) of
284+ Just app -> app req sendResponse
285+ Nothing -> sendResponse $ W.responseLBS H.status404
286+ [ ("Content-Type", "text/plain")
287+ ] "File not found"
288+
289+ response (WaiResponse r) = sendResponse r
wai-app-static/READMEnew file

No changes to the content.

wai-app-static/README.mdnew file+5
@@ -0,0 +1,5 @@
1+## wai-app-static
2+
3+WAI application for static serving
4+
5+Also provides some helper functions and datatypes for use outside of WAI.
wai-app-static/Setup.lhsnew file+7
@@ -0,0 +1,7 @@
1+#!/usr/bin/env runhaskell
2+
3+> module Main where
4+> import Distribution.Simple
5+
6+> main :: IO ()
7+> main = defaultMain
wai-app-static/Util.hsnew file+45
@@ -0,0 +1,45 @@
1+{-# LANGUAGE OverloadedStrings, ViewPatterns #-}
2+module Util
3+ ( relativeDirFromPieces
4+ , defaultMkRedirect
5+ , replace
6+ , remove
7+ , dropLastIfNull
8+ ) where
9+
10+import WaiAppStatic.Types
11+import qualified Data.Text as T
12+import Data.ByteString (ByteString)
13+import qualified Data.ByteString.Char8 as S8
14+import qualified Data.Text.Encoding as TE
15+
16+-- alist helper functions
17+replace :: Eq a => a -> b -> [(a, b)] -> [(a, b)]
18+replace k v [] = [(k,v)]
19+replace k v (x:xs) | fst x == k = (k,v):xs
20+ | otherwise = x:replace k v xs
21+
22+remove :: Eq a => a -> [(a, b)] -> [(a, b)]
23+remove _ [] = []
24+remove k (x:xs) | fst x == k = xs
25+ | otherwise = x:remove k xs
26+
27+-- | Turn a list of pieces into a relative path to the root folder.
28+relativeDirFromPieces :: Pieces -> T.Text
29+relativeDirFromPieces pieces = T.concat $ map (const "../") (drop 1 pieces) -- last piece is not a dir
30+
31+-- | Construct redirects with relative paths.
32+defaultMkRedirect :: Pieces -> ByteString -> S8.ByteString
33+defaultMkRedirect pieces newPath
34+ | S8.null newPath || S8.null relDir ||
35+ S8.last relDir /= '/' || S8.head newPath /= '/' =
36+ relDir `S8.append` newPath
37+ | otherwise = relDir `S8.append` S8.tail newPath
38+ where
39+ relDir = TE.encodeUtf8 (relativeDirFromPieces pieces)
40+
41+dropLastIfNull :: [Piece] -> [Piece]
42+dropLastIfNull pieces = case pieces of
43+ [fromPiece -> ""] -> []
44+ (a : r) -> a : dropLastIfNull r
45+ [] -> []
wai-app-static/WaiAppStatic/CmdLine.hsnew file+118
@@ -0,0 +1,118 @@
1+{-# LANGUAGE DeriveDataTypeable, RecordWildCards #-}
2+-- | Command line version of wai-app-static, used for the warp-static server.
3+module WaiAppStatic.CmdLine
4+ ( runCommandLine
5+ , Args (..)
6+ ) where
7+
8+import Network.Wai (Middleware)
9+import Network.Wai.Application.Static (staticApp, defaultFileServerSettings)
10+import Network.Wai.Handler.Warp
11+ ( runSettings, defaultSettings, setHost, setPort
12+ )
13+import Options.Applicative
14+import Text.Printf (printf)
15+import System.Directory (canonicalizePath)
16+import Control.Monad (unless)
17+import Network.Wai.Middleware.RequestLogger (logStdout)
18+import Network.Wai.Middleware.Gzip
19+import qualified Data.Map as Map
20+import qualified Data.ByteString.Char8 as S8
21+import Control.Arrow ((***))
22+import Data.Text (pack)
23+import Data.String (fromString)
24+import Network.Mime (defaultMimeMap, mimeByExt, defaultMimeType)
25+import WaiAppStatic.Types (ssIndices, toPiece, ssGetMimeType, fileName, fromPiece)
26+import Data.Maybe (mapMaybe)
27+import Control.Arrow (second)
28+import Data.Monoid ((<>))
29+
30+data Args = Args
31+ { docroot :: FilePath
32+ , index :: [FilePath]
33+ , port :: Int
34+ , noindex :: Bool
35+ , quiet :: Bool
36+ , verbose :: Bool
37+ , mime :: [(String, String)]
38+ , host :: String
39+ }
40+
41+#if MIN_VERSION_optparse_applicative(0, 10, 0)
42+option' :: Mod OptionFields Int -> Parser Int
43+option' = option auto
44+#else
45+option' = option
46+#endif
47+
48+args :: Parser Args
49+args = Args
50+ <$> strOption
51+ ( long "docroot"
52+ <> short 'd'
53+ <> metavar "DOCROOT"
54+ <> value "."
55+ <> help "directory containing files to serve")
56+ <*> (defIndex <$> many (strOption
57+ ( long "index"
58+ <> short 'i'
59+ <> metavar "INDEX"
60+ <> help "index files to serve when a directory is required"
61+ )))
62+ <*> option'
63+ ( long "port"
64+ <> short 'p'
65+ <> metavar "PORT"
66+ <> value 3000)
67+ <*> switch
68+ ( long "noindex"
69+ <> short 'n')
70+ <*> switch
71+ ( long "quiet"
72+ <> short 'q')
73+ <*> switch
74+ ( long "verbose"
75+ <> short 'v')
76+ <*> many (toPair <$> strOption
77+ ( long "mime"
78+ <> short 'm'
79+ <> metavar "MIME"
80+ <> help "extra file extension/mime type mappings"))
81+ <*> strOption
82+ ( long "host"
83+ <> short 'h'
84+ <> metavar "HOST"
85+ <> value "*"
86+ <> help "interface to bind to, special values: *, *4, *6")
87+ where
88+ toPair = second (drop 1) . break (== '=')
89+ defIndex [] = ["index.html", "index.htm"]
90+ defIndex x = x
91+
92+-- | Run with the given middleware and parsing options from the command line.
93+--
94+-- Since 2.0.1
95+runCommandLine :: (Args -> Middleware) -> IO ()
96+runCommandLine middleware = do
97+ args@Args {..} <- execParser $ info (helperOption <*> args) fullDesc
98+ let mime' = map (pack *** S8.pack) mime
99+ let mimeMap = Map.fromList mime' `Map.union` defaultMimeMap
100+ docroot' <- canonicalizePath docroot
101+ unless quiet $ printf "Serving directory %s on port %d with %s index files.\n" docroot' port (if noindex then "no" else show index)
102+ let middle = gzip def { gzipFiles = GzipCompress }
103+ . (if verbose then logStdout else id)
104+ . (middleware args)
105+ runSettings
106+ ( setPort port
107+ $ setHost (fromString host)
108+ defaultSettings
109+ )
110+ $ middle $ staticApp (defaultFileServerSettings $ fromString docroot)
111+ { ssIndices = if noindex then [] else mapMaybe (toPiece . pack) index
112+ , ssGetMimeType = return . mimeByExt mimeMap defaultMimeType . fromPiece . fileName
113+ }
114+ where
115+ helperOption :: Parser (a -> a)
116+ helperOption =
117+ abortOption ShowHelpText $
118+ mconcat [long "help", help "Show this help text", hidden]
wai-app-static/WaiAppStatic/Listing.hsnew file+150
@@ -0,0 +1,150 @@
1+{-# LANGUAGE CPP #-}
2+{-# LANGUAGE OverloadedStrings #-}
3+{-# LANGUAGE ViewPatterns #-}
4+module WaiAppStatic.Listing
5+ ( defaultListing
6+ ) where
7+
8+import qualified Text.Blaze.Html5.Attributes as A
9+import qualified Text.Blaze.Html5 as H
10+import Text.Blaze ((!))
11+import qualified Data.Text as T
12+import Data.Time
13+import Data.Time.Clock.POSIX
14+import WaiAppStatic.Types
15+#if !MIN_VERSION_time(1,5,0)
16+import System.Locale (defaultTimeLocale)
17+#endif
18+import Data.List (sortBy)
19+import Util
20+
21+import qualified Text.Blaze.Html.Renderer.Utf8 as HU
22+
23+-- | Provides a default directory listing, suitable for most apps.
24+--
25+-- Code below taken from Happstack: <https://github.com/Happstack/happstack-server/blob/87e6c01a65c687d06c61345430a112fc9a444a95/src/Happstack/Server/FileServe/BuildingBlocks.hs>
26+defaultListing :: Listing
27+defaultListing pieces (Folder contents) = do
28+ let isTop = null pieces || map Just pieces == [toPiece ""]
29+ let fps'' :: [Either FolderName File]
30+ fps'' = (if isTop then id else (Left (unsafeToPiece "") :)) contents -- FIXME emptyParentFolder feels like a bit of a hack
31+ return $ HU.renderHtmlBuilder
32+ $ H.html $ do
33+ H.head $ do
34+ let title = T.intercalate "/" $ map fromPiece pieces
35+ let title' = if T.null title then "root folder" else title
36+ H.title $ H.toHtml title'
37+ H.style $ H.toHtml $ unlines [ "table { margin: 0 auto; width: 760px; border-collapse: collapse; font-family: 'sans-serif'; }"
38+ , "table, th, td { border: 1px solid #353948; }"
39+ , "td.size { text-align: right; font-size: 0.7em; width: 50px }"
40+ , "td.date { text-align: right; font-size: 0.7em; width: 130px }"
41+ , "td { padding-right: 1em; padding-left: 1em; }"
42+ , "th.first { background-color: white; width: 24px }"
43+ , "td.first { padding-right: 0; padding-left: 0; text-align: center }"
44+ , "tr { background-color: white; }"
45+ , "tr.alt { background-color: #A3B5BA}"
46+ , "th { background-color: #3C4569; color: white; font-size: 1.125em; }"
47+ , "h1 { width: 760px; margin: 1em auto; font-size: 1em; font-family: sans-serif }"
48+ , "img { width: 20px }"
49+ , "a { text-decoration: none }"
50+ ]
51+ H.body $ do
52+ let hasTrailingSlash =
53+ case map fromPiece $ reverse $ pieces of
54+ "":_ -> True
55+ _ -> False
56+ H.h1 $ showFolder' hasTrailingSlash $ filter (not . T.null . fromPiece) pieces
57+ renderDirectoryContentsTable (map fromPiece pieces) haskellSrc folderSrc fps''
58+ where
59+ image x = T.unpack $ T.concat [(relativeDirFromPieces pieces), ".hidden/", x, ".png"]
60+ folderSrc = image "folder"
61+ haskellSrc = image "haskell"
62+ showName "" = "root"
63+ showName x = x
64+
65+ -- Add a link to the root of the tree
66+ showFolder' :: Bool -> Pieces -> H.Html
67+ showFolder' hasTrailingSlash pieces' = showFolder hasTrailingSlash (unsafeToPiece "root" : pieces')
68+
69+ showFolder :: Bool -> Pieces -> H.Html
70+ showFolder _ [] = "/" -- won't happen
71+ showFolder _ [x] = H.toHtml $ showName $ fromPiece x
72+ showFolder hasTrailingSlash (x:xs) = do
73+ let len = length xs - (if hasTrailingSlash then 0 else 1)
74+ href
75+ | len == 0 = "."
76+ | otherwise = concat $ replicate len "../" :: String
77+ H.a ! A.href (H.toValue href) $ H.toHtml $ showName $ fromPiece x
78+ " / " :: H.Html
79+ showFolder hasTrailingSlash xs
80+
81+-- | a function to generate an HTML table showing the contents of a directory on the disk
82+--
83+-- This function generates most of the content of the
84+-- 'renderDirectoryContents' page. If you want to style the page
85+-- differently, or add google analytics code, etc, you can just create
86+-- a new page template to wrap around this HTML.
87+--
88+-- see also: 'getMetaData', 'renderDirectoryContents'
89+renderDirectoryContentsTable :: [T.Text] -- ^ requested path info
90+ -> String
91+ -> String
92+ -> [Either FolderName File]
93+ -> H.Html
94+renderDirectoryContentsTable pathInfo' haskellSrc folderSrc fps =
95+ H.table $ do H.thead $ do H.th ! (A.class_ "first") $ H.img ! (A.src $ H.toValue haskellSrc)
96+ H.th "Name"
97+ H.th "Modified"
98+ H.th "Size"
99+ H.tbody $ mapM_ mkRow (zip (sortBy sortMD fps) $ cycle [False, True])
100+ where
101+ sortMD :: Either FolderName File -> Either FolderName File -> Ordering
102+ sortMD Left{} Right{} = LT
103+ sortMD Right{} Left{} = GT
104+ sortMD (Left a) (Left b) = compare a b
105+ sortMD (Right a) (Right b) = compare (fileName a) (fileName b)
106+
107+ mkRow :: (Either FolderName File, Bool) -> H.Html
108+ mkRow (md, alt) =
109+ (if alt then (! A.class_ "alt") else id) $
110+ H.tr $ do
111+ H.td ! A.class_ "first"
112+ $ case md of
113+ Left{} -> H.img ! A.src (H.toValue folderSrc)
114+ ! A.alt "Folder"
115+ Right{} -> return ()
116+ let name =
117+ case either id fileName md of
118+ (fromPiece -> "") -> unsafeToPiece ".."
119+ x -> x
120+ let isFile = either (const False) (const True) md
121+ href = addCurrentDir $ fromPiece name
122+ addCurrentDir x =
123+ case reverse pathInfo' of
124+ "":_ -> x -- has a trailing slash
125+ [] -> x -- at the root
126+ currentDir:_ -> T.concat [currentDir, "/", x]
127+ H.td (H.a ! A.href (H.toValue href) $ H.toHtml $ fromPiece name)
128+ H.td ! A.class_ "date" $ H.toHtml $
129+ case md of
130+ Right File { fileGetModified = Just t } ->
131+ formatCalendarTime defaultTimeLocale "%d-%b-%Y %X" t
132+ _ -> ""
133+ H.td ! A.class_ "size" $ H.toHtml $
134+ case md of
135+ Right File { fileGetSize = s } -> prettyShow s
136+ Left{} -> ""
137+ formatCalendarTime a b c = formatTime a b $ posixSecondsToUTCTime (realToFrac c :: POSIXTime)
138+ prettyShow x
139+ | x > 1024 = prettyShowK $ x `div` 1024
140+ | otherwise = addCommas "B" x
141+ prettyShowK x
142+ | x > 1024 = prettyShowM $ x `div` 1024
143+ | otherwise = addCommas "KB" x
144+ prettyShowM x
145+ | x > 1024 = prettyShowG $ x `div` 1024
146+ | otherwise = addCommas "MB" x
147+ prettyShowG x = addCommas "GB" x
148+ addCommas s = (++ (' ' : s)) . reverse . addCommas' . reverse . show
149+ addCommas' (a:b:c:d:e) = a : b : c : ',' : addCommas' (d : e)
150+ addCommas' x = x
wai-app-static/WaiAppStatic/Storage/Embedded.hsnew file+12
@@ -0,0 +1,12 @@
1+module WaiAppStatic.Storage.Embedded(
2+ -- * Basic
3+ embeddedSettings
4+
5+ -- * Template Haskell
6+ , Etag
7+ , EmbeddableEntry(..)
8+ , mkSettings
9+ ) where
10+
11+import WaiAppStatic.Storage.Embedded.Runtime
12+import WaiAppStatic.Storage.Embedded.TH
wai-app-static/WaiAppStatic/Storage/Embedded/Runtime.hsnew file+97
@@ -0,0 +1,97 @@
1+-- | Lookup files stored in memory instead of from the filesystem.
2+module WaiAppStatic.Storage.Embedded.Runtime
3+ ( -- * Settings
4+ embeddedSettings
5+ ) where
6+
7+import WaiAppStatic.Types
8+import Data.ByteString (ByteString)
9+import Control.Arrow ((&&&), second)
10+import Data.List
11+import Data.ByteString.Builder (byteString)
12+import qualified Network.Wai as W
13+import qualified Data.Map as Map
14+import Data.Function (on)
15+import qualified Data.Text as T
16+import Data.Ord
17+import qualified Data.ByteString as S
18+import Crypto.Hash (hash, MD5, Digest)
19+import Data.ByteArray.Encoding
20+import WaiAppStatic.Storage.Filesystem (defaultFileServerSettings)
21+import System.FilePath (isPathSeparator)
22+
23+-- | Serve the list of path/content pairs directly from memory.
24+embeddedSettings :: [(Prelude.FilePath, ByteString)] -> StaticSettings
25+embeddedSettings files = (defaultFileServerSettings $ error "unused")
26+ { ssLookupFile = embeddedLookup $ toEmbedded files
27+ }
28+
29+type Embedded = Map.Map Piece EmbeddedEntry
30+
31+data EmbeddedEntry = EEFile ByteString | EEFolder Embedded
32+
33+embeddedLookup :: Embedded -> Pieces -> IO LookupResult
34+embeddedLookup root pieces =
35+ return $ elookup pieces root
36+ where
37+ elookup :: Pieces -> Embedded -> LookupResult
38+ elookup [] x = LRFolder $ Folder $ map toEntry $ Map.toList x
39+ elookup [p] x | T.null (fromPiece p) = elookup [] x
40+ elookup (p:ps) x =
41+ case Map.lookup p x of
42+ Nothing -> LRNotFound
43+ Just (EEFile f) ->
44+ case ps of
45+ [] -> LRFile $ bsToFile p f
46+ _ -> LRNotFound
47+ Just (EEFolder y) -> elookup ps y
48+
49+toEntry :: (Piece, EmbeddedEntry) -> Either FolderName File
50+toEntry (name, EEFolder{}) = Left name
51+toEntry (name, EEFile bs) = Right File
52+ { fileGetSize = fromIntegral $ S.length bs
53+ , fileToResponse = \s h -> W.responseBuilder s h $ byteString bs
54+ , fileName = name
55+ , fileGetHash = return $ Just $ runHash bs
56+ , fileGetModified = Nothing
57+ }
58+
59+toEmbedded :: [(Prelude.FilePath, ByteString)] -> Embedded
60+toEmbedded fps =
61+ go texts
62+ where
63+ texts = map (\(x, y) -> (filter (not . T.null . fromPiece) $ toPieces' x, y)) fps
64+ toPieces' "" = []
65+ toPieces' x =
66+ -- See https://github.com/yesodweb/yesod/issues/626
67+ --
68+ -- We want to separate on the forward slash on *all* OSes, and on
69+ -- Windows, also separate on a backslash.
70+ let (y, z) = break isPathSeparator x
71+ in unsafeToPiece (T.pack y) : toPieces' (drop 1 z)
72+
73+ go :: [(Pieces, ByteString)] -> Embedded
74+ go orig =
75+ Map.fromList $ map (second go') hoisted
76+ where
77+ next = map (\(x, y) -> (head x, (tail x, y))) orig
78+ grouped :: [[(Piece, ([Piece], ByteString))]]
79+ grouped = groupBy ((==) `on` fst) $ sortBy (comparing fst) next
80+ hoisted :: [(Piece, [([Piece], ByteString)])]
81+ hoisted = map (fst . head &&& map snd) grouped
82+
83+ go' :: [(Pieces, ByteString)] -> EmbeddedEntry
84+ go' [([], content)] = EEFile content
85+ go' x = EEFolder $ go $ filter (\y -> not $ null $ fst y) x
86+
87+bsToFile :: Piece -> ByteString -> File
88+bsToFile name bs = File
89+ { fileGetSize = fromIntegral $ S.length bs
90+ , fileToResponse = \s h -> W.responseBuilder s h $ byteString bs
91+ , fileName = name
92+ , fileGetHash = return $ Just $ runHash bs
93+ , fileGetModified = Nothing
94+ }
95+
96+runHash :: ByteString -> ByteString
97+runHash = convertToBase Base64 . (hash :: S.ByteString -> Digest MD5)
wai-app-static/WaiAppStatic/Storage/Embedded/TH.hsnew file+277
@@ -0,0 +1,277 @@
1+{-# LANGUAGE TemplateHaskell, QuasiQuotes, OverloadedStrings, MagicHash #-}
2+module WaiAppStatic.Storage.Embedded.TH(
3+ Etag
4+ , EmbeddableEntry(..)
5+ , mkSettings
6+) where
7+
8+import Data.ByteString.Builder.Extra (byteStringInsert)
9+import Codec.Compression.GZip (compress)
10+import Data.ByteString.Unsafe (unsafePackAddressLen)
11+import Data.Either (lefts, rights)
12+import GHC.Exts (Int(..))
13+import Language.Haskell.TH
14+import Network.Mime (MimeType, defaultMimeLookup)
15+import System.IO.Unsafe (unsafeDupablePerformIO)
16+import WaiAppStatic.Types
17+import WaiAppStatic.Storage.Filesystem (defaultWebAppSettings)
18+import qualified Data.ByteString as B
19+import qualified Data.ByteString.Lazy as BL
20+#if !MIN_VERSION_template_haskell(2, 8, 0)
21+import qualified Data.ByteString.Char8 as B8
22+import qualified Data.ByteString.Lazy.Char8 as BL8
23+#endif
24+import qualified Data.HashMap.Strict as M
25+import qualified Data.Text as T
26+import qualified Data.Text.Encoding as T
27+import qualified Network.Wai as W
28+
29+-- | An Etag is used to return 304 Not Modified responses so the client does not need
30+-- to download resources a second time. Usually the etag is built from a hash of
31+-- the content. To disable Etags, you can pass the empty string. This will cause the
32+-- content to be redownloaded on every request.
33+type Etag = T.Text
34+
35+-- | Used at compile time to hold data about an entry to embed into the compiled executable.
36+data EmbeddableEntry = EmbeddableEntry {
37+ eLocation :: T.Text -- ^ The location where this resource should be served from. The
38+ -- location can contain forward slashes (/) to simulate directories,
39+ -- but must not end with a forward slash.
40+ , eMimeType :: MimeType -- ^ The mime type.
41+ , eContent :: Either (Etag, BL.ByteString) ExpQ
42+ -- ^ The content itself. The content can be given as a tag and bytestring,
43+ -- in which case the content will be embedded directly into the execuatble.
44+ -- Alternatively, the content can be given as a template haskell expression
45+ -- returning @IO ('Etag', 'BL.ByteString')@ in which case this action will
46+ -- be executed on every request to reload the content (this is useful
47+ -- for a debugging mode).
48+}
49+
50+-- | This structure is used at runtime to hold the entry.
51+data EmbeddedEntry = EmbeddedEntry {
52+ embLocation :: !T.Text
53+ , embMime :: !MimeType
54+ , embEtag :: !B.ByteString
55+ , embCompressed :: !Bool
56+ , embContent :: !B.ByteString
57+}
58+
59+-- | This structure is used at runtime to hold the reload entries.
60+data ReloadEntry = ReloadEntry {
61+ reloadLocation :: !T.Text
62+ , reloadMime :: !MimeType
63+ , reloadContent :: IO (T.Text, BL.ByteString)
64+}
65+
66+-- The use of unsafePackAddressLen is safe here because the length
67+-- is correct and we will only be reading from the bytestring, never
68+-- modifying it.
69+--
70+-- The only IO within unsafePackAddressLen is within newForeignPtr_ where
71+-- a new IORef is created as newIORef (NoFinalizers, []) to hold the finalizer
72+-- for the pointer. Since the pointer for the content will never have a finalizer
73+-- added, we do not care if this finalizer IORef gets created more than once since
74+-- the IORef will always be holding (NoFinalizers, []). Therefore
75+-- unsafeDupablePerformIO is safe.
76+bytestringE :: B.ByteString -> ExpQ
77+#if MIN_VERSION_template_haskell(2, 8, 0)
78+bytestringE b = [| unsafeDupablePerformIO (unsafePackAddressLen (I# $lenE) $ctE) |]
79+ where
80+ lenE = litE $ intPrimL $ toInteger $ B.length b
81+ ctE = litE $ stringPrimL $ B.unpack b
82+#else
83+bytestringE b =
84+ [| B8.pack $s |]
85+ where
86+ s = litE $ stringL $ B8.unpack b
87+#endif
88+
89+bytestringLazyE :: BL.ByteString -> ExpQ
90+#if MIN_VERSION_template_haskell(2, 8, 0)
91+bytestringLazyE b = [| unsafeDupablePerformIO (unsafePackAddressLen (I# $lenE) $ctE) |]
92+ where
93+ lenE = litE $ intPrimL $ toInteger $ BL.length b
94+ ctE = litE $ stringPrimL $ BL.unpack b
95+#else
96+bytestringLazyE b =
97+ [| B8.pack $s |]
98+ where
99+ s = litE $ stringL $ BL8.unpack b
100+#endif
101+
102+-- | A template haskell expression which creates either an EmbeddedEntry or ReloadEntry.
103+mkEntry :: EmbeddableEntry -> ExpQ
104+mkEntry (EmbeddableEntry loc mime (Left (etag, ct))) =
105+ [| Left $ EmbeddedEntry (T.pack $locE)
106+ $(bytestringE mime)
107+ $(bytestringE $ T.encodeUtf8 etag)
108+ (1 == I# $compressedE)
109+ $(bytestringLazyE ct')
110+ |]
111+ where
112+ locE = litE $ stringL $ T.unpack loc
113+ (compressed, ct') = tryCompress mime ct
114+ compressedE = litE $ intPrimL $ if compressed then 1 else 0
115+
116+mkEntry (EmbeddableEntry loc mime (Right expr)) =
117+ [| Right $ ReloadEntry (T.pack $locE)
118+ $(bytestringE mime)
119+ $expr
120+ |]
121+ where
122+ locE = litE $ stringL $ T.unpack loc
123+
124+-- | Converts an embedded entry to a file
125+embeddedToFile :: EmbeddedEntry -> File
126+embeddedToFile entry = File
127+ { fileGetSize = fromIntegral $ B.length $ embContent entry
128+ , fileToResponse = \s h ->
129+ let h' = if embCompressed entry
130+ then h ++ [("Content-Encoding", "gzip")]
131+ else h
132+ in W.responseBuilder s h' $ byteStringInsert $ embContent entry
133+
134+ -- Usually the fileName should just be the filename not the entire path,
135+ -- but we need the whole path to make the lookup within lookupMime
136+ -- possible. lookupMime is provided only with the File and from that
137+ -- we must find the mime type. Putting the path here is OK since
138+ -- within staticApp the fileName is used for directory listings which
139+ -- we have disabled.
140+ , fileName = unsafeToPiece $ embLocation entry
141+ , fileGetHash = return $ if B.null (embEtag entry)
142+ then Nothing
143+ else Just $ embEtag entry
144+ , fileGetModified = Nothing
145+ }
146+
147+-- | Converts a reload entry to a file
148+reloadToFile :: ReloadEntry -> IO File
149+reloadToFile entry = do
150+ (etag, ct) <- reloadContent entry
151+ let etag' = T.encodeUtf8 etag
152+ return $ File
153+ { fileGetSize = fromIntegral $ BL.length ct
154+ , fileToResponse = \s h -> W.responseLBS s h ct
155+ -- Similar to above the entire path needs to be in the fileName.
156+ , fileName = unsafeToPiece $ reloadLocation entry
157+ , fileGetHash = return $ if T.null etag then Nothing else Just etag'
158+ , fileGetModified = Nothing
159+ }
160+
161+
162+-- | Build a static settings based on a filemap.
163+filemapToSettings :: M.HashMap T.Text (MimeType, IO File) -> StaticSettings
164+filemapToSettings mfiles = (defaultWebAppSettings "")
165+ { ssLookupFile = lookupFile
166+ , ssGetMimeType = lookupMime
167+ }
168+ where
169+ piecesToFile p = T.intercalate "/" $ map fromPiece p
170+
171+ lookupFile [] = return LRNotFound
172+ lookupFile p =
173+ case M.lookup (piecesToFile p) mfiles of
174+ Nothing -> return LRNotFound
175+ Just (_,act) -> LRFile <$> act
176+
177+ lookupMime (File { fileName = p }) =
178+ case M.lookup (fromPiece p) mfiles of
179+ Just (mime,_) -> return mime
180+ Nothing -> return $ defaultMimeLookup $ fromPiece p
181+
182+-- | Create a 'StaticSettings' from a list of entries. Executed at run time.
183+entriesToSt :: [Either EmbeddedEntry ReloadEntry] -> StaticSettings
184+entriesToSt entries = hmap `seq` filemapToSettings hmap
185+ where
186+ embFiles = [ (embLocation e, (embMime e, return $ embeddedToFile e)) | e <- lefts entries]
187+ reloadFiles = [ (reloadLocation r, (reloadMime r, reloadToFile r)) | r <- rights entries]
188+ hmap = M.fromList $ embFiles ++ reloadFiles
189+
190+-- | Create a 'StaticSettings' at compile time that embeds resources directly into the compiled
191+-- executable. The embedded resources are precompressed (depending on mime type)
192+-- so that during runtime the resource can be served very quickly.
193+--
194+-- Because of GHC Template Haskell stage restrictions, you must define
195+-- the entries in a different module than where you create the 'StaticSettings'.
196+-- For example,
197+--
198+-- > {-# LANGUAGE TemplateHaskell, QuasiQuotes, OverloadedStrings #-}
199+-- > module A (mkEmbedded) where
200+-- >
201+-- > import WaiAppStatic.Storage.Embedded
202+-- > import Crypto.Hash.MD5 (hashlazy)
203+-- > import qualified Data.ByteString.Lazy as BL
204+-- > import qualified Data.ByteString.Base64 as B64
205+-- > import qualified Data.Text as T
206+-- > import qualified Data.Text.Encoding as T
207+-- >
208+-- > hash :: BL.ByteString -> T.Text
209+-- > hash = T.take 8 . T.decodeUtf8 . B64.encode . hashlazy
210+-- >
211+-- > mkEmbedded :: IO [EmbeddableEntry]
212+-- > mkEmbedded = do
213+-- > file <- BL.readFile "test.css"
214+-- > let emb = EmbeddableEntry {
215+-- > eLocation = "somedir/test.css"
216+-- > , eMimeType = "text/css"
217+-- > , eContent = Left (hash file, file)
218+-- > }
219+-- >
220+-- > let reload = EmbeddableEntry {
221+-- > eLocation = "anotherdir/test2.txt"
222+-- > , eMimeType = "text/plain"
223+-- > , eContent = Right [| BL.readFile "test2.txt" >>= \c -> return (hash c, c) |]
224+-- > }
225+-- >
226+-- > return [emb, reload]
227+--
228+-- The above @mkEmbedded@ will be executed at compile time. It loads the contents of test.css and
229+-- computes the hash of test.css for the etag. The content will be available at the URL somedir/test.css.
230+-- Internally, 'embedApp' below will attempt to compress the content at compile time. The compression will
231+-- only happen if the compressed content is shorter than the original and the mime type is either text or
232+-- javascript. If the content is compressed, at runtime the precomputed compressed content will be served
233+-- with the appropriate HTTP header. If 'embedApp' decides not to compress the content, it will be
234+-- served directly.
235+--
236+-- Secondly, @mkEmbedded@ creates a reloadable entry. This will be available at the URL anotherdir/test2.txt.
237+-- Whenver a request comes in for anotherdir/test2.txt, the action inside the quasiquote in eContent will
238+-- be executed. This will re-read the test2.txt file and recompute its hash.
239+--
240+-- Finally, here is a module which uses the above action to create a 'W.Application'.
241+--
242+-- > {-# LANGUAGE TemplateHaskell #-}
243+-- > module B where
244+-- >
245+-- > import A
246+-- > import Network.Wai (Application)
247+-- > import Network.Wai.Application.Static (staticApp)
248+-- > import WaiAppStatic.Storage.Embedded
249+-- > import Network.Wai.Handler.Warp (run)
250+-- >
251+-- > myApp :: Application
252+-- > myApp = staticApp $(mkSettings mkEmbedded)
253+-- >
254+-- > main :: IO ()
255+-- > main = run 3000 myApp
256+mkSettings :: IO [EmbeddableEntry] -> ExpQ
257+mkSettings action = do
258+ entries <- runIO action
259+ [| entriesToSt $(listE $ map mkEntry entries) |]
260+
261+shouldCompress :: MimeType -> Bool
262+shouldCompress m = "text/" `B.isPrefixOf` m || m `elem` extra
263+ where
264+ extra = [ "application/json"
265+ , "application/javascript"
266+ , "application/ecmascript"
267+ ]
268+
269+-- | Only compress if the mime type is correct and the compressed text is actually shorter.
270+tryCompress :: MimeType -> BL.ByteString -> (Bool, BL.ByteString)
271+tryCompress mime ct
272+ | shouldCompress mime = (c, ct')
273+ | otherwise = (False, ct)
274+ where
275+ compressed = compress ct
276+ c = BL.length compressed < BL.length ct
277+ ct' = if c then compressed else ct
wai-app-static/WaiAppStatic/Storage/Filesystem.hsnew file+170
@@ -0,0 +1,170 @@
1+{-# LANGUAGE ScopedTypeVariables #-}
2+{-# LANGUAGE OverloadedStrings #-}
3+{-# LANGUAGE BangPatterns #-}
4+-- | Access files on the filesystem.
5+module WaiAppStatic.Storage.Filesystem
6+ ( -- * Types
7+ ETagLookup
8+ -- * Settings
9+ , defaultWebAppSettings
10+ , defaultFileServerSettings
11+ , webAppSettingsWithLookup
12+ ) where
13+
14+import WaiAppStatic.Types
15+import System.FilePath ((</>))
16+import System.IO (withBinaryFile, IOMode(..))
17+import System.Directory (doesFileExist, doesDirectoryExist, getDirectoryContents)
18+import Data.List (foldl')
19+import Control.Monad (forM)
20+import Util
21+import Data.ByteString (ByteString)
22+import Control.Exception (SomeException, try)
23+import qualified Network.Wai as W
24+import WaiAppStatic.Listing
25+import Network.Mime
26+import System.PosixCompat.Files (fileSize, getFileStatus, modificationTime, isRegularFile)
27+import Data.Maybe (catMaybes)
28+import Data.ByteArray.Encoding
29+import Crypto.Hash (hashlazy, MD5, Digest)
30+import qualified Data.ByteString.Lazy as BL (hGetContents)
31+import qualified Data.Text as T
32+
33+-- | Construct a new path from a root and some @Pieces@.
34+pathFromPieces :: FilePath -> Pieces -> FilePath
35+pathFromPieces = foldl' (\fp p -> fp </> T.unpack (fromPiece p))
36+
37+-- | Settings optimized for a web application. Files will have aggressive
38+-- caching applied and hashes calculated, and indices and listings are disabled.
39+defaultWebAppSettings :: FilePath -- ^ root folder to serve from
40+ -> StaticSettings
41+defaultWebAppSettings root = StaticSettings
42+ { ssLookupFile = webAppLookup hashFileIfExists root
43+ , ssMkRedirect = defaultMkRedirect
44+ , ssGetMimeType = return . defaultMimeLookup . fromPiece . fileName
45+ , ssMaxAge = MaxAgeForever
46+ , ssListing = Nothing
47+ , ssIndices = []
48+ , ssRedirectToIndex = False
49+ , ssUseHash = True
50+ , ssAddTrailingSlash = False
51+ , ss404Handler = Nothing
52+ }
53+
54+-- | Settings optimized for a file server. More conservative caching will be
55+-- applied, and indices and listings are enabled.
56+defaultFileServerSettings :: FilePath -- ^ root folder to serve from
57+ -> StaticSettings
58+defaultFileServerSettings root = StaticSettings
59+ { ssLookupFile = fileSystemLookup (fmap Just . hashFile) root
60+ , ssMkRedirect = defaultMkRedirect
61+ , ssGetMimeType = return . defaultMimeLookup . fromPiece . fileName
62+ , ssMaxAge = NoMaxAge
63+ , ssListing = Just defaultListing
64+ , ssIndices = map unsafeToPiece ["index.html", "index.htm"]
65+ , ssRedirectToIndex = False
66+ , ssUseHash = False
67+ , ssAddTrailingSlash = False
68+ , ss404Handler = Nothing
69+ }
70+
71+-- | Same as @defaultWebAppSettings@, but additionally uses a specialized
72+-- @ETagLookup@ in place of the standard one. This can allow you to cache your
73+-- hash values, or even precompute them.
74+webAppSettingsWithLookup :: FilePath -- ^ root folder to serve from
75+ -> ETagLookup
76+ -> StaticSettings
77+webAppSettingsWithLookup dir etagLookup =
78+ (defaultWebAppSettings dir) { ssLookupFile = webAppLookup etagLookup dir}
79+
80+-- | Convenience wrapper for @fileHelper@.
81+fileHelperLR :: ETagLookup
82+ -> FilePath -- ^ file location
83+ -> Piece -- ^ file name
84+ -> IO LookupResult
85+fileHelperLR a b c = fmap (maybe LRNotFound LRFile) $ fileHelper a b c
86+
87+-- | Attempt to load up a @File@ from the given path.
88+fileHelper :: ETagLookup
89+ -> FilePath -- ^ file location
90+ -> Piece -- ^ file name
91+ -> IO (Maybe File)
92+fileHelper hashFunc fp name = do
93+ efs <- try $ getFileStatus fp
94+ case efs of
95+ Left (_ :: SomeException) -> return Nothing
96+ Right fs | isRegularFile fs -> return $ Just File
97+ { fileGetSize = fromIntegral $ fileSize fs
98+ , fileToResponse = \s h -> W.responseFile s h fp Nothing
99+ , fileName = name
100+ , fileGetHash = hashFunc fp
101+ , fileGetModified = Just $ modificationTime fs
102+ }
103+ Right _ -> return Nothing
104+
105+-- | How to calculate etags. Can perform filesystem reads on each call, or use
106+-- some caching mechanism.
107+type ETagLookup = FilePath -> IO (Maybe ByteString)
108+
109+-- | More efficient than @fileSystemLookup@ as it only concerns itself with
110+-- finding files, not folders.
111+webAppLookup :: ETagLookup -> FilePath -> Pieces -> IO LookupResult
112+webAppLookup hashFunc prefix pieces =
113+ fileHelperLR hashFunc fp lastPiece
114+ where
115+ fp = pathFromPieces prefix pieces
116+ lastPiece
117+ | null pieces = unsafeToPiece ""
118+ | otherwise = last pieces
119+
120+-- | MD5 hash and base64-encode the file contents. Does not check if the file
121+-- exists.
122+hashFile :: FilePath -> IO ByteString
123+hashFile fp = withBinaryFile fp ReadMode $ \h -> do
124+ f <- BL.hGetContents h
125+ let !hash = hashlazy f :: Digest MD5
126+ return $ convertToBase Base64 hash
127+
128+hashFileIfExists :: ETagLookup
129+hashFileIfExists fp = do
130+ res <- try $ hashFile fp
131+ return $ case res of
132+ Left (_ :: SomeException) -> Nothing
133+ Right x -> Just x
134+
135+isVisible :: FilePath -> Bool
136+isVisible ('.':_) = False
137+isVisible "" = False
138+isVisible _ = True
139+
140+-- | Get a proper @LookupResult@, checking if the path is a file or folder.
141+-- Compare with @webAppLookup@, which only deals with files.
142+fileSystemLookup :: ETagLookup
143+ -> FilePath -> Pieces -> IO LookupResult
144+fileSystemLookup hashFunc prefix pieces = do
145+ let fp = pathFromPieces prefix pieces
146+ fe <- doesFileExist fp
147+ if fe
148+ then fileHelperLR hashFunc fp lastPiece
149+ else do
150+ de <- doesDirectoryExist fp
151+ if de
152+ then do
153+ entries' <- fmap (filter isVisible) $ getDirectoryContents fp
154+ entries <- forM entries' $ \fpRel' -> do
155+ let name = unsafeToPiece $ T.pack fpRel'
156+ fp' = fp </> fpRel'
157+ de' <- doesDirectoryExist fp'
158+ if de'
159+ then return $ Just $ Left name
160+ else do
161+ mfile <- fileHelper hashFunc fp' name
162+ case mfile of
163+ Nothing -> return Nothing
164+ Just file -> return $ Just $ Right file
165+ return $ LRFolder $ Folder $ catMaybes entries
166+ else return LRNotFound
167+ where
168+ lastPiece
169+ | null pieces = unsafeToPiece ""
170+ | otherwise = last pieces
wai-app-static/WaiAppStatic/Types.hsnew file+153
@@ -0,0 +1,153 @@
1+module WaiAppStatic.Types
2+ ( -- * Pieces
3+ Piece
4+ , toPiece
5+ , fromPiece
6+ , unsafeToPiece
7+ , Pieces
8+ , toPieces
9+ -- * Caching
10+ , MaxAge (..)
11+ -- * File\/folder serving
12+ , FolderName
13+ , Folder (..)
14+ , File (..)
15+ , LookupResult (..)
16+ , Listing
17+ -- * Settings
18+ , StaticSettings (..)
19+ ) where
20+
21+import Data.Text (Text)
22+import qualified Network.HTTP.Types as H
23+import qualified Network.Wai as W
24+import Data.ByteString (ByteString)
25+import System.Posix.Types (EpochTime)
26+import qualified Data.Text as T
27+import Data.ByteString.Builder (Builder)
28+import Network.Mime (MimeType)
29+
30+-- | An individual component of a path, or of a filepath.
31+--
32+-- This is the core type used by wai-app-static for doing lookups. It provides
33+-- a smart constructor to avoid the possibility of constructing unsafe path
34+-- segments (though @unsafeToPiece@ can get around that as necessary).
35+--
36+-- Individual file lookup backends must know how to convert from a @Piece@ to
37+-- their storage system.
38+newtype Piece = Piece { fromPiece :: Text }
39+ deriving (Show, Eq, Ord)
40+
41+-- | Smart constructor for a @Piece@. Won\'t allow unsafe components, such as
42+-- pieces beginning with a period or containing a slash. This /will/, however,
43+-- allow null pieces.
44+toPiece :: Text -> Maybe Piece
45+toPiece t
46+ | T.null t = Just $ Piece t
47+ | T.head t == '.' = Nothing
48+ | T.any (== '/') t = Nothing
49+ | otherwise = Just $ Piece t
50+
51+-- | Construct a @Piece@ without input validation.
52+unsafeToPiece :: Text -> Piece
53+unsafeToPiece = Piece
54+
55+-- | Call @toPiece@ on a list.
56+--
57+-- > toPieces = mapM toPiece
58+toPieces :: [Text] -> Maybe Pieces
59+toPieces = mapM toPiece
60+
61+-- | Request coming from a user. Corresponds to @pathInfo@.
62+--
63+-- The root path is the empty list.
64+type Pieces = [Piece]
65+
66+-- | Values for the max-age component of the cache-control response header.
67+data MaxAge = NoMaxAge -- ^ no cache-control set
68+ | MaxAgeSeconds Int -- ^ set to the given number of seconds
69+ | MaxAgeForever -- ^ essentially infinite caching; in reality, probably one year
70+
71+-- | Just the name of a folder.
72+type FolderName = Piece
73+
74+-- | Represent contents of a single folder, which can be itself either a file
75+-- or a folder.
76+data Folder = Folder
77+ { folderContents :: [Either FolderName File]
78+ }
79+
80+-- | Information on an individual file.
81+data File = File
82+ { -- | Size of file in bytes
83+ fileGetSize :: Integer
84+ -- | How to construct a WAI response for this file. Some files are stored
85+ -- on the filesystem and can use @ResponseFile@, while others are stored
86+ -- in memory and should use @ResponseBuilder@.
87+ , fileToResponse :: H.Status -> H.ResponseHeaders -> W.Response
88+ -- | Last component of the filename.
89+ , fileName :: Piece
90+ -- | Calculate a hash of the contents of this file, such as for etag.
91+ , fileGetHash :: IO (Maybe ByteString)
92+ -- | Last modified time, used for both display in listings and if-modified-since.
93+ , fileGetModified :: Maybe EpochTime
94+ }
95+
96+-- | Result of looking up a file in some storage backend.
97+--
98+-- The lookup is either a file or folder, or does not exist.
99+data LookupResult = LRFile File
100+ | LRFolder Folder
101+ | LRNotFound
102+
103+-- | How to construct a directory listing page for the given request path and
104+-- the resulting folder.
105+type Listing = Pieces -> Folder -> IO Builder
106+
107+-- | All of the settings available to users for tweaking wai-app-static.
108+--
109+-- Note that you should use the settings type approach for modifying values.
110+-- See <http://www.yesodweb.com/book/settings-types> for more information.
111+data StaticSettings = StaticSettings
112+ {
113+ -- | Lookup a single file or folder. This is how you can control storage
114+ -- backend (filesystem, embedded, etc) and where to lookup.
115+ ssLookupFile :: Pieces -> IO LookupResult
116+
117+ -- | Determine the mime type of the given file. Note that this function
118+ -- lives in @IO@ in case you want to perform more complicated mimetype
119+ -- analysis, such as via the @file@ utility.
120+ , ssGetMimeType :: File -> IO MimeType
121+
122+ -- | Ordered list of filenames to be used for indices. If the user
123+ -- requests a folder, and a file with the given name is found in that
124+ -- folder, that file is served. This supercedes any directory listing.
125+ , ssIndices :: [Piece]
126+
127+ -- | How to perform a directory listing. Optional. Will be used when the
128+ -- user requested a folder.
129+ , ssListing :: Maybe Listing
130+
131+ -- | Value to provide for max age in the cache-control.
132+ , ssMaxAge :: MaxAge
133+
134+ -- | Given a requested path and a new destination, construct a string
135+ -- that will go there. Default implementation will use relative paths.
136+ , ssMkRedirect :: Pieces -> ByteString -> ByteString
137+
138+ -- | If @True@, send a redirect to the user when a folder is requested
139+ -- and an index page should be displayed. When @False@, display the
140+ -- content immediately.
141+ , ssRedirectToIndex :: Bool
142+
143+ -- | Prefer usage of etag caching to last-modified caching.
144+ , ssUseHash :: Bool
145+
146+ -- | Force a trailing slash at the end of directories
147+ , ssAddTrailingSlash :: Bool
148+
149+ -- | Optional `W.Application` to be used in case of 404 errors
150+ --
151+ -- Since 3.1.3
152+ , ss404Handler :: Maybe W.Application
153+ }
wai-app-static/app/warp-static.hsnew file+7
@@ -0,0 +1,7 @@
1+{-# LANGUAGE DeriveDataTypeable, RecordWildCards #-}
2+module Main (main) where
3+
4+import WaiAppStatic.CmdLine (runCommandLine)
5+
6+main :: IO ()
7+main = runCommandLine (const id)
wai-app-static/embedded-sample.hsnew file+13
@@ -0,0 +1,13 @@
1+{-# LANGUAGE TemplateHaskell #-}
2+{-# LANGUAGE OverloadedStrings #-}
3+import Network.Wai.Application.Static
4+import Network.Wai.Handler.Warp (run)
5+import Data.FileEmbed
6+import WaiAppStatic.Types
7+import WaiAppStatic.Storage.Embedded
8+
9+main :: IO ()
10+main = run 3000 $ staticApp (embeddedSettings $(embedDir "test"))
11+ { ssIndices = []
12+ , ssMaxAge = NoMaxAge
13+ }
wai-app-static/folder.svgnew file+424
@@ -0,0 +1,424 @@
1+<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2+<!-- Created with Inkscape (http://www.inkscape.org/) -->
3+<svg
4+ xmlns:dc="http://purl.org/dc/elements/1.1/"
5+ xmlns:cc="http://creativecommons.org/ns#"
6+ xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
7+ xmlns:svg="http://www.w3.org/2000/svg"
8+ xmlns="http://www.w3.org/2000/svg"
9+ xmlns:xlink="http://www.w3.org/1999/xlink"
10+ xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
11+ xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
12+ width="48.000000px"
13+ height="48.000000px"
14+ id="svg97"
15+ sodipodi:version="0.32"
16+ inkscape:version="0.46"
17+ sodipodi:docbase="/home/jimmac/src/cvs/tango-icon-theme/scalable/places"
18+ sodipodi:docname="folder.svg"
19+ inkscape:export-filename="/home/jimmac/Desktop/horlander-style3.png"
20+ inkscape:export-xdpi="90.000000"
21+ inkscape:export-ydpi="90.000000"
22+ inkscape:output_extension="org.inkscape.output.svg.inkscape">
23+ <defs
24+ id="defs3">
25+ <inkscape:perspective
26+ sodipodi:type="inkscape:persp3d"
27+ inkscape:vp_x="0 : 24 : 1"
28+ inkscape:vp_y="0 : 1000 : 0"
29+ inkscape:vp_z="48 : 24 : 1"
30+ inkscape:persp3d-origin="24 : 16 : 1"
31+ id="perspective68" />
32+ <radialGradient
33+ inkscape:collect="always"
34+ xlink:href="#linearGradient5060"
35+ id="radialGradient6719"
36+ gradientUnits="userSpaceOnUse"
37+ gradientTransform="matrix(-2.774389,0,0,1.969706,112.7623,-872.8854)"
38+ cx="605.71429"
39+ cy="486.64789"
40+ fx="605.71429"
41+ fy="486.64789"
42+ r="117.14286" />
43+ <linearGradient
44+ inkscape:collect="always"
45+ id="linearGradient5060">
46+ <stop
47+ style="stop-color:black;stop-opacity:1;"
48+ offset="0"
49+ id="stop5062" />
50+ <stop
51+ style="stop-color:black;stop-opacity:0;"
52+ offset="1"
53+ id="stop5064" />
54+ </linearGradient>
55+ <radialGradient
56+ inkscape:collect="always"
57+ xlink:href="#linearGradient5060"
58+ id="radialGradient6717"
59+ gradientUnits="userSpaceOnUse"
60+ gradientTransform="matrix(2.774389,0,0,1.969706,-1891.633,-872.8854)"
61+ cx="605.71429"
62+ cy="486.64789"
63+ fx="605.71429"
64+ fy="486.64789"
65+ r="117.14286" />
66+ <linearGradient
67+ id="linearGradient5048">
68+ <stop
69+ style="stop-color:black;stop-opacity:0;"
70+ offset="0"
71+ id="stop5050" />
72+ <stop
73+ id="stop5056"
74+ offset="0.5"
75+ style="stop-color:black;stop-opacity:1;" />
76+ <stop
77+ style="stop-color:black;stop-opacity:0;"
78+ offset="1"
79+ id="stop5052" />
80+ </linearGradient>
81+ <linearGradient
82+ inkscape:collect="always"
83+ xlink:href="#linearGradient5048"
84+ id="linearGradient6715"
85+ gradientUnits="userSpaceOnUse"
86+ gradientTransform="matrix(2.774389,0,0,1.969706,-1892.179,-872.8854)"
87+ x1="302.85715"
88+ y1="366.64789"
89+ x2="302.85715"
90+ y2="609.50507" />
91+ <linearGradient
92+ inkscape:collect="always"
93+ id="linearGradient9806">
94+ <stop
95+ style="stop-color:#000000;stop-opacity:1;"
96+ offset="0"
97+ id="stop9808" />
98+ <stop
99+ style="stop-color:#000000;stop-opacity:0;"
100+ offset="1"
101+ id="stop9810" />
102+ </linearGradient>
103+ <linearGradient
104+ id="linearGradient9766">
105+ <stop
106+ style="stop-color:#6194cb;stop-opacity:1;"
107+ offset="0"
108+ id="stop9768" />
109+ <stop
110+ style="stop-color:#729fcf;stop-opacity:1;"
111+ offset="1"
112+ id="stop9770" />
113+ </linearGradient>
114+ <linearGradient
115+ id="linearGradient3096">
116+ <stop
117+ id="stop3098"
118+ offset="0"
119+ style="stop-color:#424242;stop-opacity:1;" />
120+ <stop
121+ id="stop3100"
122+ offset="1.0000000"
123+ style="stop-color:#777777;stop-opacity:1.0000000;" />
124+ </linearGradient>
125+ <linearGradient
126+ id="linearGradient319"
127+ inkscape:collect="always">
128+ <stop
129+ id="stop320"
130+ offset="0"
131+ style="stop-color:#ffffff;stop-opacity:1;" />
132+ <stop
133+ id="stop321"
134+ offset="1"
135+ style="stop-color:#ffffff;stop-opacity:0;" />
136+ </linearGradient>
137+ <linearGradient
138+ id="linearGradient1789">
139+ <stop
140+ style="stop-color:#202020;stop-opacity:1.0000000;"
141+ offset="0.0000000"
142+ id="stop1790" />
143+ <stop
144+ style="stop-color:#b9b9b9;stop-opacity:1.0000000;"
145+ offset="1.0000000"
146+ id="stop1791" />
147+ </linearGradient>
148+ <radialGradient
149+ inkscape:collect="always"
150+ xlink:href="#linearGradient1789"
151+ id="radialGradient238"
152+ gradientUnits="userSpaceOnUse"
153+ gradientTransform="matrix(1.055022,-2.734504e-2,0.177703,1.190929,-3.572177,-7.125301)"
154+ cx="20.706017"
155+ cy="37.517986"
156+ fx="20.706017"
157+ fy="37.517986"
158+ r="30.905205" />
159+ <linearGradient
160+ id="linearGradient3983">
161+ <stop
162+ style="stop-color:#ffffff;stop-opacity:0.87628865;"
163+ offset="0.0000000"
164+ id="stop3984" />
165+ <stop
166+ style="stop-color:#fffffe;stop-opacity:0.0000000;"
167+ offset="1.0000000"
168+ id="stop3985" />
169+ </linearGradient>
170+ <linearGradient
171+ inkscape:collect="always"
172+ xlink:href="#linearGradient3983"
173+ id="linearGradient491"
174+ gradientUnits="userSpaceOnUse"
175+ gradientTransform="matrix(1.516844,0.000000,0.000000,0.708978,-0.879573,-1.318166)"
176+ x1="6.2297964"
177+ y1="13.773066"
178+ x2="9.8980894"
179+ y2="66.834053" />
180+ <linearGradient
181+ gradientUnits="userSpaceOnUse"
182+ y2="46.689312"
183+ x2="12.853771"
184+ y1="32.567184"
185+ x1="13.035696"
186+ gradientTransform="matrix(1.317489,0.000000,0.000000,0.816256,-0.879573,-1.318166)"
187+ id="linearGradient322"
188+ xlink:href="#linearGradient319"
189+ inkscape:collect="always" />
190+ <linearGradient
191+ gradientUnits="userSpaceOnUse"
192+ y2="6.1802502"
193+ x2="15.514889"
194+ y1="31.367750"
195+ x1="18.112709"
196+ id="linearGradient3104"
197+ xlink:href="#linearGradient3096"
198+ inkscape:collect="always" />
199+ <linearGradient
200+ inkscape:collect="always"
201+ xlink:href="#linearGradient9766"
202+ id="linearGradient9772"
203+ x1="22.175976"
204+ y1="36.987999"
205+ x2="22.065331"
206+ y2="32.050499"
207+ gradientUnits="userSpaceOnUse" />
208+ <radialGradient
209+ inkscape:collect="always"
210+ xlink:href="#linearGradient9806"
211+ id="radialGradient9812"
212+ cx="24.35099"
213+ cy="41.591846"
214+ fx="24.35099"
215+ fy="41.591846"
216+ r="19.136078"
217+ gradientTransform="matrix(1.000000,0.000000,0.000000,0.242494,1.565588e-16,31.50606)"
218+ gradientUnits="userSpaceOnUse" />
219+ </defs>
220+ <sodipodi:namedview
221+ fill="#729fcf"
222+ id="base"
223+ pagecolor="#ffffff"
224+ bordercolor="#666666"
225+ borderopacity="0.10196078"
226+ inkscape:pageopacity="0.0"
227+ inkscape:pageshadow="2"
228+ inkscape:zoom="4"
229+ inkscape:cx="54.359127"
230+ inkscape:cy="-13.803699"
231+ inkscape:current-layer="layer1"
232+ showgrid="false"
233+ inkscape:grid-bbox="true"
234+ inkscape:document-units="px"
235+ inkscape:window-width="1026"
236+ inkscape:window-height="818"
237+ inkscape:window-x="169"
238+ inkscape:window-y="30"
239+ inkscape:showpageshadow="false"
240+ stroke="#3465a4" />
241+ <metadata
242+ id="metadata4">
243+ <rdf:RDF>
244+ <cc:Work
245+ rdf:about="">
246+ <dc:format>image/svg+xml</dc:format>
247+ <dc:type
248+ rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
249+ <dc:title>Folder Icon</dc:title>
250+ <dc:date />
251+ <dc:creator>
252+ <cc:Agent>
253+ <dc:title>Jakub Steiner</dc:title>
254+ </cc:Agent>
255+ </dc:creator>
256+ <cc:license
257+ rdf:resource="http://creativecommons.org/licenses/publicdomain/" />
258+ <dc:source>http://jimmac.musichall.cz</dc:source>
259+ <dc:subject>
260+ <rdf:Bag>
261+ <rdf:li>folder</rdf:li>
262+ <rdf:li>directory</rdf:li>
263+ </rdf:Bag>
264+ </dc:subject>
265+ </cc:Work>
266+ <cc:License
267+ rdf:about="http://creativecommons.org/licenses/publicdomain/">
268+ <cc:permits
269+ rdf:resource="http://creativecommons.org/ns#Reproduction" />
270+ <cc:permits
271+ rdf:resource="http://creativecommons.org/ns#Distribution" />
272+ <cc:permits
273+ rdf:resource="http://creativecommons.org/ns#DerivativeWorks" />
274+ </cc:License>
275+ </rdf:RDF>
276+ </metadata>
277+ <g
278+ id="layer1"
279+ inkscape:label="Folder"
280+ inkscape:groupmode="layer">
281+ <g
282+ style="display:inline"
283+ transform="matrix(2.262383e-2,0,0,2.086758e-2,43.38343,36.36962)"
284+ id="g6707">
285+ <rect
286+ style="opacity:0.40206185;color:black;fill:url(#linearGradient6715);fill-opacity:1;fill-rule:nonzero;stroke:none;stroke-width:1;stroke-linecap:round;stroke-linejoin:miter;marker:none;marker-start:none;marker-mid:none;marker-end:none;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1;visibility:visible;display:inline;overflow:visible"
287+ id="rect6709"
288+ width="1339.6335"
289+ height="478.35718"
290+ x="-1559.2523"
291+ y="-150.69685" />
292+ <path
293+ style="opacity:0.40206185;color:black;fill:url(#radialGradient6717);fill-opacity:1;fill-rule:nonzero;stroke:none;stroke-width:1;stroke-linecap:round;stroke-linejoin:miter;marker:none;marker-start:none;marker-mid:none;marker-end:none;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1;visibility:visible;display:inline;overflow:visible"
294+ d="M -219.61876,-150.68038 C -219.61876,-150.68038 -219.61876,327.65041 -219.61876,327.65041 C -76.744594,328.55086 125.78146,220.48075 125.78138,88.454235 C 125.78138,-43.572302 -33.655436,-150.68036 -219.61876,-150.68038 z "
295+ id="path6711"
296+ sodipodi:nodetypes="cccc" />
297+ <path
298+ sodipodi:nodetypes="cccc"
299+ id="path6713"
300+ d="M -1559.2523,-150.68038 C -1559.2523,-150.68038 -1559.2523,327.65041 -1559.2523,327.65041 C -1702.1265,328.55086 -1904.6525,220.48075 -1904.6525,88.454235 C -1904.6525,-43.572302 -1745.2157,-150.68036 -1559.2523,-150.68038 z "
301+ style="opacity:0.40206185;color:black;fill:url(#radialGradient6719);fill-opacity:1;fill-rule:nonzero;stroke:none;stroke-width:1;stroke-linecap:round;stroke-linejoin:miter;marker:none;marker-start:none;marker-mid:none;marker-end:none;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1;visibility:visible;display:inline;overflow:visible" />
302+ </g>
303+ <path
304+ d="M 4.5217805,38.687417 C 4.5435766,39.103721 4.9816854,39.520026 5.3979900,39.520026 L 36.725011,39.520026 C 37.141313,39.520026 37.535823,39.103721 37.514027,38.687417 L 36.577584,11.460682 C 36.555788,11.044379 36.117687,10.628066 35.701383,10.628066 L 22.430510,10.628066 C 21.945453,10.628066 21.196037,10.312477 21.028866,9.5214338 L 20.417475,6.6283628 C 20.262006,5.8926895 19.535261,5.5904766 19.118957,5.5904766 L 4.3400975,5.5904766 C 3.9237847,5.5904766 3.5292767,6.0067807 3.5510726,6.4230849 L 4.5217805,38.687417 z "
305+ id="path216"
306+ style="fill:url(#radialGradient238);fill-opacity:1.0000000;fill-rule:nonzero;stroke:url(#linearGradient3104);stroke-width:1.0000000;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4.0000000;stroke-dasharray:none;stroke-opacity:1.0000000"
307+ sodipodi:nodetypes="ccccccssssccc" />
308+ <path
309+ sodipodi:nodetypes="cc"
310+ id="path9788"
311+ d="M 5.2265927,22.5625 L 35.492173,22.5625"
312+ style="opacity:0.11363633;color:#000000;fill:#729fcf;fill-opacity:1;fill-rule:nonzero;stroke:#000000;stroke-width:1.00000024;stroke-linecap:round;stroke-linejoin:round;marker:none;marker-start:none;marker-mid:none;marker-end:none;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1;visibility:visible;display:inline;overflow:visible" />
313+ <path
314+ sodipodi:nodetypes="cc"
315+ id="path9784"
316+ d="M 5.0421736,18.5625 L 35.489104,18.5625"
317+ style="opacity:0.11363633;color:#000000;fill:#729fcf;fill-opacity:1;fill-rule:nonzero;stroke:#000000;stroke-width:1.00000036;stroke-linecap:round;stroke-linejoin:round;marker:none;marker-start:none;marker-mid:none;marker-end:none;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1;visibility:visible;display:inline;overflow:visible" />
318+ <path
319+ style="opacity:0.11363633;color:#000000;fill:#729fcf;fill-opacity:1;fill-rule:nonzero;stroke:#000000;stroke-width:1;stroke-linecap:round;stroke-linejoin:round;marker:none;marker-start:none;marker-mid:none;marker-end:none;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1;visibility:visible;display:inline;overflow:visible"
320+ d="M 4.9806965,12.5625 L 35.488057,12.5625"
321+ id="path9778"
322+ sodipodi:nodetypes="cc" />
323+ <path
324+ style="opacity:0.11363633;color:#000000;fill:#729fcf;fill-opacity:1;fill-rule:nonzero;stroke:#000000;stroke-width:1.00000036;stroke-linecap:round;stroke-linejoin:round;marker:none;marker-start:none;marker-mid:none;marker-end:none;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1;visibility:visible;display:inline;overflow:visible"
325+ d="M 5.3861577,32.5625 L 35.494881,32.5625"
326+ id="path9798"
327+ sodipodi:nodetypes="cc" />
328+ <path
329+ sodipodi:nodetypes="cc"
330+ id="path9800"
331+ d="M 5.5091398,34.5625 L 35.496893,34.5625"
332+ style="opacity:0.11363633;color:#000000;fill:#729fcf;fill-opacity:1;fill-rule:nonzero;stroke:#000000;stroke-width:1.00000024;stroke-linecap:round;stroke-linejoin:round;marker:none;marker-start:none;marker-mid:none;marker-end:none;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1;visibility:visible;display:inline;overflow:visible" />
333+ <path
334+ style="opacity:0.11363633;color:#000000;fill:#729fcf;fill-opacity:1;fill-rule:nonzero;stroke:#000000;stroke-width:1.00000036;stroke-linecap:round;stroke-linejoin:round;marker:none;marker-start:none;marker-mid:none;marker-end:none;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1;visibility:visible;display:inline;overflow:visible"
335+ d="M 5.0421736,16.5625 L 35.489104,16.5625"
336+ id="path9782"
337+ sodipodi:nodetypes="cc" />
338+ <path
339+ sodipodi:nodetypes="cc"
340+ id="path9780"
341+ d="M 5.0114345,14.5625 L 35.48858,14.5625"
342+ style="opacity:0.11363633;color:#000000;fill:#729fcf;fill-opacity:1;fill-rule:nonzero;stroke:#000000;stroke-width:1.00000024;stroke-linecap:round;stroke-linejoin:round;marker:none;marker-start:none;marker-mid:none;marker-end:none;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1;visibility:visible;display:inline;overflow:visible" />
343+ <path
344+ sodipodi:nodetypes="cc"
345+ id="path9776"
346+ d="M 4.9220969,10.5625 L 20.202912,10.5625"
347+ style="opacity:0.11363633;color:#000000;fill:#729fcf;fill-opacity:1;fill-rule:nonzero;stroke:#000000;stroke-width:1.00000024;stroke-linecap:round;stroke-linejoin:round;marker:none;marker-start:none;marker-mid:none;marker-end:none;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1;visibility:visible;display:inline;overflow:visible" />
348+ <path
349+ style="opacity:0.11363633;color:#000000;fill:#729fcf;fill-opacity:1;fill-rule:nonzero;stroke:#000000;stroke-width:0.99999982;stroke-linecap:round;stroke-linejoin:round;marker:none;marker-start:none;marker-mid:none;marker-end:none;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1;visibility:visible;display:inline;overflow:visible"
350+ d="M 4.8737534,8.5624999 L 19.657487,8.5624999"
351+ id="path9774"
352+ sodipodi:nodetypes="cc" />
353+ <path
354+ style="opacity:0.11363633;color:#000000;fill:#729fcf;fill-opacity:1;fill-rule:nonzero;stroke:#000000;stroke-width:1.00000036;stroke-linecap:round;stroke-linejoin:round;marker:none;marker-start:none;marker-mid:none;marker-end:none;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1;visibility:visible;display:inline;overflow:visible"
355+ d="M 5.3246666,28.5625 L 35.493876,28.5625"
356+ id="path9794"
357+ sodipodi:nodetypes="cc" />
358+ <path
359+ sodipodi:nodetypes="cc"
360+ id="path9792"
361+ d="M 5.2880638,26.5625 L 35.493184,26.5625"
362+ style="opacity:0.11363633;color:#000000;fill:#729fcf;fill-opacity:1;fill-rule:nonzero;stroke:#000000;stroke-width:1;stroke-linecap:round;stroke-linejoin:round;marker:none;marker-start:none;marker-mid:none;marker-end:none;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1;visibility:visible;display:inline;overflow:visible" />
363+ <path
364+ style="opacity:0.11363633;color:#000000;fill:#729fcf;fill-opacity:1;fill-rule:nonzero;stroke:#000000;stroke-width:1.00000024;stroke-linecap:round;stroke-linejoin:round;marker:none;marker-start:none;marker-mid:none;marker-end:none;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1;visibility:visible;display:inline;overflow:visible"
365+ d="M 5.2265927,24.5625 L 35.492173,24.5625"
366+ id="path9790"
367+ sodipodi:nodetypes="cc" />
368+ <path
369+ style="opacity:0.11363633;color:#000000;fill:#729fcf;fill-opacity:1;fill-rule:nonzero;stroke:#000000;stroke-width:1.00000012;stroke-linecap:round;stroke-linejoin:round;marker:none;marker-start:none;marker-mid:none;marker-end:none;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1;visibility:visible;display:inline;overflow:visible"
370+ d="M 5.1958537,20.5625 L 35.491649,20.5625"
371+ id="path9786"
372+ sodipodi:nodetypes="cc" />
373+ <path
374+ sodipodi:nodetypes="cc"
375+ id="path9796"
376+ d="M 5.3246666,30.5625 L 35.493876,30.5625"
377+ style="opacity:0.11363633;color:#000000;fill:#729fcf;fill-opacity:1;fill-rule:nonzero;stroke:#000000;stroke-width:1.00000036;stroke-linecap:round;stroke-linejoin:round;marker:none;marker-start:none;marker-mid:none;marker-end:none;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1;visibility:visible;display:inline;overflow:visible" />
378+ <path
379+ style="opacity:0.11363633;color:#000000;fill:#729fcf;fill-opacity:1;fill-rule:nonzero;stroke:#000000;stroke-width:1.00000024;stroke-linecap:round;stroke-linejoin:round;marker:none;marker-start:none;marker-mid:none;marker-end:none;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1;visibility:visible;display:inline;overflow:visible"
380+ d="M 5.5091398,36.5625 L 35.496893,36.5625"
381+ id="path9802"
382+ sodipodi:nodetypes="cc" />
383+ <path
384+ style="color:#000000;fill:url(#linearGradient491);fill-opacity:1.0000000;fill-rule:nonzero;stroke:none;stroke-width:1.2138050;stroke-linecap:butt;stroke-linejoin:miter;marker:none;marker-start:none;marker-mid:none;marker-end:none;stroke-miterlimit:4.0000000;stroke-dashoffset:0.0000000;stroke-opacity:1.0000000;visibility:visible;display:block;overflow:visible;opacity:0.45142857"
385+ d="M 6.0683430,38.864023 C 6.0846856,39.176251 5.8874317,39.384402 5.5697582,39.280326 L 5.5697582,39.280326 C 5.2520766,39.176251 5.0330270,38.968099 5.0166756,38.655870 L 4.0689560,6.5913839 C 4.0526131,6.2791558 4.2341418,6.0906134 4.5463699,6.0906134 L 18.968420,6.0429196 C 19.280648,6.0429196 19.900363,6.3433923 20.101356,7.3651014 L 20.674845,10.180636 C 20.247791,9.7153790 20.255652,9.7010175 20.037287,9.0239299 L 19.631192,7.7647478 C 19.412142,7.0371009 18.932991,6.9328477 18.620763,6.9328477 L 5.7329889,6.9328477 C 5.4207613,6.9328477 5.2235075,7.1409999 5.2398583,7.4532364 L 6.1778636,38.968099 L 6.0683430,38.864023 z "
386+ id="path219"
387+ sodipodi:nodetypes="cccccccccscccccc" />
388+ <g
389+ style="stroke-miterlimit:4.0000000;stroke-width:0.99946535;stroke:none;fill-rule:nonzero;fill-opacity:0.75706214;fill:#ffffff"
390+ id="g220"
391+ transform="matrix(1.040764,0.000000,5.449252e-2,1.040764,-8.670199,2.670594)"
392+ inkscape:export-filename="/home/jimmac/ximian_art/icons/nautilus/suse93/gnome-fs-directory.png"
393+ inkscape:export-xdpi="74.800003"
394+ inkscape:export-ydpi="74.800003">
395+ <path
396+ style="fill-opacity:0.50847459;fill:#ffffff"
397+ d="M 42.417183,8.5151772 C 42.422267,8.4180642 42.289022,8.2681890 42.182066,8.2681716 L 29.150665,8.2660527 C 29.150665,8.2660527 30.062379,8.8540072 31.352477,8.8622963 L 42.405974,8.9333167 C 42.417060,8.7215889 42.408695,8.6772845 42.417183,8.5151772 z "
398+ id="path221"
399+ sodipodi:nodetypes="cscscs" />
400+ </g>
401+ <path
402+ style="color:#000000;fill:url(#linearGradient9772);fill-opacity:1.0;fill-rule:nonzero;stroke:#3465a4;stroke-width:1.0000000;stroke-linecap:butt;stroke-linejoin:round;marker:none;marker-start:none;marker-mid:none;marker-end:none;stroke-miterlimit:4.0000000;stroke-dasharray:none;stroke-dashoffset:0.0000000;stroke-opacity:1;visibility:visible;display:block"
403+ d="M 39.783532,39.510620 C 40.927426,39.466556 41.746608,38.414321 41.830567,37.189615 C 42.622354,25.640928 43.489927,15.957666 43.489927,15.957666 C 43.562082,15.710182 43.322016,15.462699 43.009787,15.462699 L 8.6386304,15.462699 C 8.6386304,15.462699 6.7883113,37.329591 6.7883113,37.329591 C 6.6737562,38.311657 6.3223038,39.134309 5.2384755,39.513304 L 39.783532,39.510620 z "
404+ id="path233"
405+ sodipodi:nodetypes="cscccscc"
406+ inkscape:export-filename="/home/jimmac/ximian_art/icons/nautilus/suse93/gnome-fs-directory.png"
407+ inkscape:export-xdpi="74.800003"
408+ inkscape:export-ydpi="74.800003" />
409+ <path
410+ sodipodi:nodetypes="ccsscsc"
411+ id="path304"
412+ d="M 9.6202444,16.463921 L 42.411343,16.528735 L 40.837297,36.530714 C 40.752975,37.602225 40.386619,37.958929 38.964641,37.958929 C 37.093139,37.958929 10.286673,37.926522 7.569899,37.926522 C 7.8034973,37.605711 7.9036547,36.937899 7.9049953,36.92191 L 9.6202444,16.463921 z "
413+ style="opacity:0.46590909;fill:none;fill-opacity:1.0000000;fill-rule:evenodd;stroke:url(#linearGradient322);stroke-width:0.99999970px;stroke-linecap:round;stroke-linejoin:miter;stroke-opacity:1.0000000" />
414+ <path
415+ sodipodi:nodetypes="ccccc"
416+ id="path323"
417+ d="M 9.6202481,16.223182 L 8.4536014,31.866453 C 8.4536014,31.866453 16.749756,27.718375 27.119949,27.718375 C 37.490142,27.718375 42.675239,16.223182 42.675239,16.223182 L 9.6202481,16.223182 z "
418+ style="fill:#ffffff;fill-opacity:0.089285679;fill-rule:evenodd;stroke:none;stroke-width:1.0000000px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1.0000000" />
419+ </g>
420+ <g
421+ inkscape:groupmode="layer"
422+ id="layer2"
423+ inkscape:label="pattern" />
424+</svg>
wai-app-static/images/folder.pngnew file

Binary file not shown.

wai-app-static/images/haskell.pngnew file

Binary file not shown.

wai-app-static/sample.hsnew file+17
@@ -0,0 +1,17 @@
1+{-# LANGUAGE OverloadedStrings #-}
2+
3+import Data.String
4+import Data.Text (pack)
5+import Data.Maybe (mapMaybe)
6+import WaiAppStatic.Types (ssIndices, toPiece)
7+import Network.Wai.Application.Static (staticApp,defaultFileServerSettings)
8+import Network.Wai.Handler.Warp (runSettings, settingsPort, defaultSettings)
9+
10+main :: IO ()
11+main = runSettings defaultSettings
12+ {
13+ settingsPort = 3000
14+ } $ staticApp (defaultFileServerSettings $ fromString ".")
15+ {
16+ ssIndices = mapMaybe (toPiece . pack) ["index.html"]
17+ }
wai-app-static/test/EmbeddedTestEntries.hsnew file+55
@@ -0,0 +1,55 @@
1+{-# LANGUAGE TemplateHaskell, QuasiQuotes, OverloadedStrings #-}
2+
3+module EmbeddedTestEntries where
4+
5+import WaiAppStatic.Storage.Embedded
6+import qualified Data.Text as T
7+import qualified Data.Text.Lazy as TL
8+import qualified Data.Text.Lazy.Encoding as TL
9+import qualified Data.ByteString.Lazy as BL
10+
11+body :: Int -> Char -> BL.ByteString
12+body i c = TL.encodeUtf8 $ TL.pack $ take i $ repeat c
13+
14+mkEntries :: IO [EmbeddableEntry]
15+mkEntries = return
16+ -- An entry that should be compressed
17+ [ EmbeddableEntry "e1.txt"
18+ "text/plain"
19+ (Left ("Etag 1", body 1000 'A'))
20+
21+ -- An entry so short that the compressed text is longer
22+ , EmbeddableEntry "e2.txt"
23+ "text/plain"
24+ (Left ("Etag 2", "ABC"))
25+
26+ -- An entry that is not compressed because of the mime
27+ , EmbeddableEntry "somedir/e3.txt"
28+ "xxx"
29+ (Left ("Etag 3", body 1000 'A'))
30+
31+ -- A reloadable entry
32+ , EmbeddableEntry "e4.css"
33+ "text/css"
34+ (Right [| return ("Etag 4" :: T.Text, body 2000 'Q') |])
35+
36+ -- An entry without etag
37+ , EmbeddableEntry "e5.txt"
38+ "text/plain"
39+ (Left ("", body 1000 'Z'))
40+
41+ -- A reloadable entry without etag
42+ , EmbeddableEntry "e6.txt"
43+ "text/plain"
44+ (Right [| return ("" :: T.Text, body 1000 'W') |] )
45+
46+ -- An index file
47+ , EmbeddableEntry "index.html"
48+ "text/html"
49+ (Right [| return ("" :: T.Text, "index file") |] )
50+
51+ -- An index file in a subdir
52+ , EmbeddableEntry "foo/index.html"
53+ "text/html"
54+ (Right [| return ("" :: T.Text, "index file in subdir") |] )
55+ ]
wai-app-static/test/WaiAppEmbeddedTest.hsnew file+94
@@ -0,0 +1,94 @@
1+{-# LANGUAGE TemplateHaskell, OverloadedStrings #-}
2+module WaiAppEmbeddedTest (embSpec) where
3+
4+import Codec.Compression.GZip (compress)
5+import EmbeddedTestEntries
6+import Network.Wai
7+import Network.Wai.Application.Static (staticApp)
8+import Network.Wai.Test
9+import Test.Hspec
10+import WaiAppStatic.Storage.Embedded
11+import WaiAppStatic.Types
12+
13+defRequest :: Request
14+defRequest = defaultRequest
15+
16+embSpec :: Spec
17+embSpec = do
18+ let embedSettings settings = flip runSession (staticApp settings)
19+ let embed = embedSettings $(mkSettings mkEntries)
20+ describe "embedded, compressed entry" $ do
21+ it "served correctly" $ embed $ do
22+ req <- request (setRawPathInfo defRequest "e1.txt")
23+ assertStatus 200 req
24+ assertHeader "Content-Type" "text/plain" req
25+ assertHeader "Content-Encoding" "gzip" req
26+ assertHeader "ETag" "Etag 1" req
27+ assertNoHeader "Last-Modified req" req
28+ assertBody (compress $ body 1000 'A') req
29+
30+ it "304 when valid if-none-match sent" $ embed $ do
31+ req <- request (setRawPathInfo defRequest "e1.txt")
32+ { requestHeaders = [("If-None-Match", "Etag 1")] }
33+ assertStatus 304 req
34+
35+ it "ssIndices works" $ do
36+ let testSettings = $(mkSettings mkEntries){
37+ ssIndices = [unsafeToPiece "index.html"]
38+ }
39+ embedSettings testSettings $ do
40+ req <- request defRequest
41+ assertStatus 200 req
42+ assertBody "index file" req
43+
44+ it "ssIndices works with trailing slashes" $ do
45+ let testSettings = $(mkSettings mkEntries){
46+ ssIndices = [unsafeToPiece "index.html"]
47+ }
48+ embedSettings testSettings $ do
49+ req <- request (setRawPathInfo defRequest "/foo/")
50+ assertStatus 200 req
51+ assertBody "index file in subdir" req
52+
53+ describe "embedded, uncompressed entry" $ do
54+ it "too short" $ embed $ do
55+ req <- request (setRawPathInfo defRequest "e2.txt")
56+ assertStatus 200 req
57+ assertHeader "Content-Type" "text/plain" req
58+ assertNoHeader "Content-Encoding" req
59+ assertHeader "ETag" "Etag 2" req
60+ assertBody "ABC" req
61+
62+ it "wrong mime" $ embed $ do
63+ req <- request (setRawPathInfo defRequest "somedir/e3.txt")
64+ assertStatus 200 req
65+ assertHeader "Content-Type" "xxx" req
66+ assertNoHeader "Content-Encoding" req
67+ assertHeader "ETag" "Etag 3" req
68+ assertBody (body 1000 'A') req
69+
70+ describe "reloadable entry" $
71+ it "served correctly" $ embed $ do
72+ req <- request (setRawPathInfo defRequest "e4.css")
73+ assertStatus 200 req
74+ assertHeader "Content-Type" "text/css" req
75+ assertNoHeader "Content-Encoding" req
76+ assertHeader "ETag" "Etag 4" req
77+ assertBody (body 2000 'Q') req
78+
79+ describe "entries without etags" $ do
80+ it "embedded entry" $ embed $ do
81+ req <- request (setRawPathInfo defRequest "e5.txt")
82+ assertStatus 200 req
83+ assertHeader "Content-Type" "text/plain" req
84+ assertHeader "Content-Encoding" "gzip" req
85+ assertNoHeader "ETag" req
86+ assertBody (compress $ body 1000 'Z') req
87+
88+ it "reload entry" $ embed $ do
89+ req <- request (setRawPathInfo defRequest "e6.txt")
90+ assertStatus 200 req
91+ assertHeader "Content-Type" "text/plain" req
92+ assertNoHeader "Content-Encoding" req
93+ assertNoHeader "ETag" req
94+ assertBody (body 1000 'W') req
wai-app-static/test/WaiAppStaticTest.hsnew file+184
@@ -0,0 +1,184 @@
1+{-# LANGUAGE OverloadedStrings, NoMonomorphismRestriction #-}
2+module WaiAppStaticTest (spec) where
3+
4+import Network.Wai.Application.Static
5+import WaiAppStatic.Types
6+
7+import Test.Hspec
8+import Test.Mockery.Directory
9+import qualified Data.ByteString.Char8 as S8
10+-- import qualified Data.ByteString.Lazy.Char8 as L8
11+import System.PosixCompat.Files (getFileStatus, modificationTime)
12+import System.FilePath
13+import System.IO.Temp
14+
15+import Network.HTTP.Date
16+import Network.HTTP.Types (status500)
17+{-import System.Locale (defaultTimeLocale)-}
18+{-import Data.Time.Format (formatTime)-}
19+
20+import Network.Wai
21+import Network.Wai.Test
22+
23+import Control.Monad.IO.Class (liftIO)
24+import Network.Mime
25+
26+defRequest :: Request
27+defRequest = defaultRequest
28+
29+spec :: Spec
30+spec = do
31+ let webApp = flip runSession $ staticApp $ defaultWebAppSettings "test"
32+ let fileServerAppWithSettings settings = flip runSession $ staticApp settings
33+ let fileServerApp = fileServerAppWithSettings (defaultFileServerSettings "test")
34+ { ssAddTrailingSlash = True
35+ }
36+
37+ let etag = "1B2M2Y8AsgTpgAmY7PhCfg=="
38+ let file = "a/b"
39+ let statFile = setRawPathInfo defRequest file
40+
41+ describe "mime types" $ do
42+ it "fileNameExtensions" $
43+ fileNameExtensions "foo.tar.gz" `shouldBe` ["tar.gz", "gz"]
44+ it "handles multi-extensions" $
45+ defaultMimeLookup "foo.tar.gz" `shouldBe` "application/x-tgz"
46+ it "defaults correctly" $
47+ defaultMimeLookup "foo.unknown" `shouldBe` "application/octet-stream"
48+
49+ describe "webApp" $ do
50+ it "403 for unsafe paths" $ webApp $
51+ flip mapM_ ["..", "."] $ \path ->
52+ assertStatus 403 =<<
53+ request (setRawPathInfo defRequest path)
54+
55+ it "200 for hidden paths" $ webApp $
56+ flip mapM_ [".hidden/folder.png", ".hidden/haskell.png"] $ \path ->
57+ assertStatus 200 =<<
58+ request (setRawPathInfo defRequest path)
59+
60+ it "404 for non-existent files" $ webApp $
61+ assertStatus 404 =<<
62+ request (setRawPathInfo defRequest "doesNotExist")
63+
64+ it "302 redirect when multiple slashes" $ webApp $ do
65+ req <- request (setRawPathInfo defRequest "a//b/c")
66+ assertStatus 302 req
67+ assertHeader "Location" "../../a/b/c" req
68+
69+ let absoluteApp = flip runSession $ staticApp $ (defaultWebAppSettings "test") {
70+ ssMkRedirect = \_ u -> S8.append "http://www.example.com" u
71+ }
72+ it "302 redirect when multiple slashes" $ absoluteApp $
73+ flip mapM_ ["/a//b/c", "a//b/c"] $ \path -> do
74+ req <- request (setRawPathInfo defRequest path)
75+ assertStatus 302 req
76+ assertHeader "Location" "http://www.example.com/a/b/c" req
77+
78+ describe "webApp when requesting a static asset" $ do
79+ it "200 and etag when no etag query parameters" $ webApp $ do
80+ req <- request statFile
81+ assertStatus 200 req
82+ assertHeader "ETag" etag req
83+ assertNoHeader "Last-Modified" req
84+
85+ it "Cache-Control set when etag parameter is correct" $ webApp $ do
86+ req <- request statFile { queryString = [("etag", Just etag)] }
87+ assertStatus 200 req
88+ assertHeader "Cache-Control" "public, max-age=31536000" req
89+ assertNoHeader "Last-Modified" req
90+
91+ it "200 when invalid in-none-match sent" $ webApp $
92+ flip mapM_ ["cached", ""] $ \badETag -> do
93+ req <- request statFile { requestHeaders = [("If-None-Match", badETag)] }
94+ assertStatus 200 req
95+ assertHeader "ETag" etag req
96+ assertNoHeader "Last-Modified" req
97+
98+ it "304 when valid if-none-match sent" $ webApp $ do
99+ req <- request statFile { requestHeaders = [("If-None-Match", etag)] }
100+ assertStatus 304 req
101+ assertNoHeader "Etag" req
102+ assertNoHeader "Last-Modified" req
103+
104+ describe "fileServerApp" $ do
105+ let fileDate = do
106+ stat <- liftIO $ getFileStatus $ "test/" ++ file
107+ return $ formatHTTPDate . epochTimeToHTTPDate $ modificationTime stat
108+
109+ it "directory listing for index" $ fileServerApp $ do
110+ resp <- request (setRawPathInfo defRequest "a/")
111+ assertStatus 200 resp
112+ -- note the unclosed img tags so both /> and > will pass
113+ assertBodyContains "<img src=\"../.hidden/haskell.png\"" resp
114+ assertBodyContains "<img src=\"../.hidden/folder.png\" alt=\"Folder\"" resp
115+ assertBodyContains "<a href=\"b\">b</a>" resp
116+
117+ it "200 when invalid if-modified-since header" $ fileServerApp $ do
118+ flip mapM_ ["123", ""] $ \badDate -> do
119+ req <- request statFile {
120+ requestHeaders = [("If-Modified-Since", badDate)]
121+ }
122+ assertStatus 200 req
123+ fdate <- fileDate
124+ assertHeader "Last-Modified" fdate req
125+
126+ it "304 when if-modified-since matches" $ fileServerApp $ do
127+ fdate <- fileDate
128+ req <- request statFile {
129+ requestHeaders = [("If-Modified-Since", fdate)]
130+ }
131+ assertStatus 304 req
132+ assertNoHeader "Cache-Control" req
133+
134+ context "302 redirect to add a trailing slash on directories if missing" $ do
135+ it "works at the root" $ fileServerApp $ do
136+ req <- request (setRawPathInfo defRequest "/a")
137+ assertStatus 302 req
138+ assertHeader "Location" "/a/" req
139+
140+ it "works when an index.html is delivered" $ do
141+ let settings = (defaultFileServerSettings "."){
142+ ssAddTrailingSlash = True
143+ }
144+ inTempDirectory $ fileServerAppWithSettings settings $ do
145+ liftIO $ touch "foo/index.html"
146+ req <- request (setRawPathInfo defRequest "/foo")
147+ assertStatus 302 req
148+ assertHeader "Location" "/foo/" req
149+
150+ let urlMapApp = flip runSession $ \req send ->
151+ case pathInfo req of
152+ "subPath":rest ->
153+ let req' = req { pathInfo = rest }
154+ in (staticApp (defaultFileServerSettings "test")
155+ { ssAddTrailingSlash = True
156+ }) req' send
157+ _ -> send $ responseLBS status500 []
158+ "urlMapApp: only works at subPath"
159+ it "works with subpath at the root of the file server" $ urlMapApp $ do
160+ req <- request (setRawPathInfo defRequest "/subPath")
161+ assertStatus 302 req
162+ assertHeader "Location" "/subPath/" req
163+
164+ context "with defaultWebAppSettings" $ do
165+ it "ssIndices works" $ do
166+ withSystemTempDirectory "wai-app-static-test" $ \ dir -> do
167+ writeFile (dir </> "index.html") "foo"
168+ let testSettings = (defaultWebAppSettings dir) {
169+ ssIndices = [unsafeToPiece "index.html"]
170+ }
171+ fileServerAppWithSettings testSettings $ do
172+ resp <- request (setRawPathInfo defRequest "/")
173+ assertStatus 200 resp
174+ assertBody "foo" resp
175+
176+ context "with defaultFileServerSettings" $ do
177+ it "prefers ssIndices over ssListing" $ do
178+ withSystemTempDirectory "wai-app-static-test" $ \ dir -> do
179+ writeFile (dir </> "index.html") "foo"
180+ let testSettings = defaultFileServerSettings dir
181+ fileServerAppWithSettings testSettings $ do
182+ resp <- request (setRawPathInfo defRequest "/")
183+ assertStatus 200 resp
184+ assertBody "foo" resp
wai-app-static/test/a/bnew file

No changes to the content.

wai-app-static/tests.hsnew file+6
@@ -0,0 +1,6 @@
1+import WaiAppStaticTest (spec)
2+import WaiAppEmbeddedTest (embSpec)
3+import Test.Hspec
4+
5+main :: IO ()
6+main = hspec $ spec >> embSpec
wai-app-static/wai-app-static.cabalnew file+109
@@ -0,0 +1,109 @@
1+name: wai-app-static
2+version: 3.1.7.1
3+license: MIT
4+license-file: LICENSE
5+author: Michael Snoyman <michael@snoyman.com>
6+maintainer: Michael Snoyman <michael@snoyman.com>, Greg Weber <greg@gregweber.info>
7+synopsis: WAI application for static serving
8+description: API docs and the README are available at <http://www.stackage.org/package/wai-app-static>.
9+category: Web, Yesod
10+stability: Stable
11+cabal-version: >= 1.8
12+build-type: Simple
13+homepage: http://www.yesodweb.com/book/web-application-interface
14+Extra-source-files:
15+ images/folder.png
16+ images/haskell.png
17+ test/*.hs
18+ test/a/b
19+ tests.hs
20+ README.md
21+ ChangeLog.md
22+
23+Flag print
24+ Description: print debug info
25+ Default: False
26+
27+library
28+ build-depends: base >= 4 && < 5
29+ , wai >= 3.0 && < 3.3
30+ , bytestring >= 0.10.4
31+ , http-types >= 0.7
32+ , transformers >= 0.2.2
33+ , unix-compat >= 0.2
34+ , directory >= 1.0.1
35+ , containers >= 0.2
36+ , time >= 1.1.4
37+ , old-locale >= 1.0.0.2
38+ , file-embed >= 0.0.3.1
39+ , text >= 0.7
40+ , cryptonite >= 0.6
41+ , memory >= 0.7
42+ , http-date
43+ , blaze-html >= 0.5
44+ , blaze-markup >= 0.5.1
45+ , mime-types >= 0.1 && < 0.2
46+ , unordered-containers >= 0.2
47+ , template-haskell >= 2.7
48+ , zlib >= 0.5
49+ , filepath
50+ , wai-extra >= 3.0 && < 3.1
51+ , optparse-applicative >= 0.7
52+ , warp >= 3.0.11 && < 3.4
53+
54+ exposed-modules: Network.Wai.Application.Static
55+ WaiAppStatic.Storage.Filesystem
56+ WaiAppStatic.Storage.Embedded
57+ WaiAppStatic.Listing
58+ WaiAppStatic.Types
59+ WaiAppStatic.CmdLine
60+ other-modules: Util
61+ WaiAppStatic.Storage.Embedded.Runtime
62+ WaiAppStatic.Storage.Embedded.TH
63+ ghc-options: -Wall
64+ extensions: CPP
65+
66+ if flag(print)
67+ cpp-options: -DPRINT
68+
69+Executable warp
70+ Main-is: warp-static.hs
71+ hs-source-dirs: app
72+ Build-depends: base >= 4 && < 5
73+ , wai-app-static
74+ , directory >= 1.0
75+ , containers >= 0.2
76+ , bytestring >= 0.10.4
77+ , text >= 0.7
78+ , mime-types >= 0.1 && < 0.2
79+
80+test-suite runtests
81+ hs-source-dirs: test
82+ main-is: ../tests.hs
83+ type: exitcode-stdio-1.0
84+
85+ build-depends: base >= 4 && < 5
86+ , hspec >= 1.3
87+ , unix-compat
88+ , time
89+ , old-locale
90+ , http-date
91+ , wai-app-static
92+ , wai-extra
93+ , wai
94+ , http-types
95+ , network
96+ , bytestring
97+ , text
98+ , transformers
99+ , mime-types
100+ , zlib
101+ , filepath
102+ , temporary
103+ , mockery
104+ -- , containers
105+ ghc-options: -Wall
106+
107+source-repository head
108+ type: git
109+ location: git://github.com/yesodweb/wai.git
wai-app-static/קרררר.htmlnew file+1
@@ -0,0 +1 @@
1+<h1>HELLO WORLD</h1>
wai-conduit/ChangeLog.mdnew file+7
@@ -0,0 +1,7 @@
1+## 3.0.0.4
2+
3+* Drop dependency on blaze-builder
4+
5+## 3.0.0.3
6+
7+* Support wai 3.2
wai-conduit/LICENSEnew file+20
@@ -0,0 +1,20 @@
1+Copyright (c) 2014 Michael Snoyman
2+
3+Permission is hereby granted, free of charge, to any person obtaining
4+a copy of this software and associated documentation files (the
5+"Software"), to deal in the Software without restriction, including
6+without limitation the rights to use, copy, modify, merge, publish,
7+distribute, sublicense, and/or sell copies of the Software, and to
8+permit persons to whom the Software is furnished to do so, subject to
9+the following conditions:
10+
11+The above copyright notice and this permission notice shall be included
12+in all copies or substantial portions of the Software.
13+
14+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
wai-conduit/Network/Wai/Conduit.hsnew file+66
@@ -0,0 +1,66 @@
1+-- | A light-weight wrapper around @Network.Wai@ to provide easy conduit support.
2+module Network.Wai.Conduit
3+ ( -- * Request body
4+ sourceRequestBody
5+ -- * Response body
6+ , responseSource
7+ , responseRawSource
8+ -- * Re-export
9+ , module Network.Wai
10+ ) where
11+
12+import Network.Wai
13+import Data.Conduit
14+import Control.Monad.IO.Class (MonadIO, liftIO)
15+import Data.ByteString (ByteString)
16+import qualified Data.ByteString as S
17+import Control.Monad (unless)
18+import Network.HTTP.Types
19+import Data.ByteString.Builder (Builder)
20+import qualified Data.Conduit.List as CL
21+
22+-- | Stream the request body.
23+--
24+-- Since 3.0.0
25+sourceRequestBody :: MonadIO m => Request -> ConduitT () ByteString m ()
26+sourceRequestBody req =
27+ loop
28+ where
29+ go = liftIO (getRequestBodyChunk req)
30+
31+ loop = do
32+ bs <- go
33+ unless (S.null bs) $ do
34+ yield bs
35+ loop
36+
37+-- | Create an HTTP response out of a @Source@.
38+--
39+-- Since 3.0.0
40+responseSource :: Status -> ResponseHeaders -> ConduitT () (Flush Builder) IO () -> Response
41+responseSource s hs src = responseStream s hs $ \send flush ->
42+ runConduit $ src .| CL.mapM_ (\mbuilder ->
43+ case mbuilder of
44+ Chunk b -> send b
45+ Flush -> flush)
46+
47+-- | Create a raw response using a @Source@ and @Sink@ to represent the input
48+-- and output, respectively.
49+--
50+-- Since 3.0.0
51+responseRawSource :: (MonadIO m, MonadIO n)
52+ => (ConduitT () ByteString m () -> ConduitT ByteString Void n () -> IO ())
53+ -> Response
54+ -> Response
55+responseRawSource app =
56+ responseRaw app'
57+ where
58+ app' recv send =
59+ app src sink
60+ where
61+ src = do
62+ bs <- liftIO recv
63+ unless (S.null bs) $ do
64+ yield bs
65+ src
66+ sink = CL.mapM_ $ liftIO . send
wai-conduit/README.mdnew file+4
@@ -0,0 +1,4 @@
1+## wai-conduit
2+
3+Since version 3.0.0, WAI has no built-in streaming data abstraction. This
4+library provides similar functionality to what existed in WAI 2.x.
wai-conduit/Setup.hsnew file+2
@@ -0,0 +1,2 @@
1+import Distribution.Simple
2+main = defaultMain
wai-conduit/example/Main.hsnew file+19
@@ -0,0 +1,19 @@
1+#!/usr/bin/env stack
2+-- stack --resolver lts-11.10 script
3+import Network.Wai
4+import Network.Wai.Handler.Warp
5+import Network.Wai.Conduit
6+import Network.HTTP.Types
7+import Conduit
8+import Data.ByteString.Builder (byteString)
9+import Control.Monad.Trans.Resource
10+
11+main :: IO ()
12+main = run 3000 app
13+
14+app :: Application
15+app _req respond =
16+ runResourceT $ withInternalState $ \is ->
17+ respond $ responseSource status200 [] $
18+ transPipe (`runInternalState` is) (sourceFile "Main.hs") .|
19+ mapC (Chunk . byteString)
wai-conduit/wai-conduit.cabalnew file+23
@@ -0,0 +1,23 @@
1+name: wai-conduit
2+version: 3.0.0.4
3+synopsis: conduit wrappers for WAI
4+description: API docs and the README are available at <http://www.stackage.org/package/wai-conduit>.
5+homepage: https://github.com/yesodweb/wai
6+license: MIT
7+license-file: LICENSE
8+author: Michael Snoyman
9+maintainer: michael@snoyman.com
10+category: Web, Conduit
11+build-type: Simple
12+cabal-version: >=1.10
13+extra-source-files: README.md ChangeLog.md
14+
15+library
16+ exposed-modules: Network.Wai.Conduit
17+ build-depends: base >= 4 && < 5
18+ , wai >= 3.0 && < 3.3
19+ , conduit
20+ , transformers
21+ , bytestring >= 0.10.4
22+ , http-types
23+ default-language: Haskell2010
wai-extra.cabaldeleted−50
@@ -1,50 +0,0 @@
1−Name: wai-extra
2−Version: 0.2.1
3−Synopsis: Provides some basic WAI handlers and middleware.
4−Description: The goal here is to provide common features without many dependencies.
5−License: BSD3
6−License-file: LICENSE
7−Author: Michael Snoyman
8−Maintainer: michael@snoyman.com
9−Homepage: http://github.com/snoyberg/wai-extra
10−Category: Web
11−Build-Type: Simple
12−Cabal-Version: >=1.2
13−Stability: Stable
14−extra-source-files: cbits/crc32.h cbits/inffast.h cbits/inflate.h
15− cbits/trees.h cbits/deflate.h cbits/inffixed.h
16− cbits/inftrees.h cbits/zutil.h
17−
18−Library
19− Build-Depends: base >= 3 && < 5,
20− bytestring >= 0.9 && < 0.10,
21− wai >= 0.2.0 && < 0.3,
22− old-locale >= 1.0 && < 1.1,
23− time >= 1.1.4 && < 1.3,
24− sendfile >= 0.6.1 && < 0.7,
25− network >= 2.2.1.5 && < 2.3,
26− directory >= 1.0.1 && < 1.1
27− Exposed-modules: Network.Wai.Handler.CGI
28− Network.Wai.Handler.SimpleServer
29− Network.Wai.Middleware.CleanPath
30− Network.Wai.Middleware.Gzip
31− Network.Wai.Middleware.Jsonp
32− Network.Wai.Zlib
33− Network.Wai.Parse
34− Network.Wai.Handler.Helper
35− ghc-options: -Wall
36− c-sources: c/helper.c
37− include-dirs: c
38− -- Following lines and cbits folder taken from zlib package.
39− if !os(windows)
40− -- Normally we use the the standard system zlib:
41− extra-libraries: z
42− else
43− -- However for the benefit of users of Windows (which does not have zlib
44− -- by default) we bundle a complete copy of the C sources of zlib-1.2.3
45− c-sources: cbits/adler32.c cbits/compress.c cbits/crc32.c
46− cbits/deflate.c cbits/gzio.c cbits/infback.c
47− cbits/inffast.c cbits/inflate.c cbits/inftrees.c
48− cbits/trees.c cbits/uncompr.c cbits/zutil.c
49− include-dirs: cbits
50− install-includes: zlib.h zconf.h
wai-extra/.ghcinew file+1
@@ -0,0 +1 @@
1+:set -itest -optP-include -optPdist/build/autogen/cabal_macros.h
wai-extra/.gitignorenew file+2
@@ -0,0 +1,2 @@
1+*.swp
2+dist
wai-extra/ChangeLog.mdnew file+209
@@ -0,0 +1,209 @@
1+# Changelog for wai-extra
2+
3+## 3.0.29.1
4+
5+* Fix `Network.Wai.Test.request` always sending an empty request body [#794](https://github.com/yesodweb/wai/pull/794)
6+
7+## 3.0.29
8+
9+* Export `Network.Wai.EventSource.eventStreamAppRaw` [#786](https://github.com/yesodweb/wai/pull/786)
10+
11+## 3.0.28
12+
13+* Add `Network.Wai.EventSource.eventStreamAppRaw` [#767](https://github.com/yesodweb/wai/pull/767)
14+
15+## 3.0.27
16+
17+* Add custom request log formatter which includes response headers [#762](https://github.com/yesodweb/wai/pull/762)
18+
19+## 3.0.26.1
20+
21+* When available, supply the response size to custom loggers
22+ [#757](https://github.com/yesodweb/wai/pull/757)
23+
24+## 3.0.26
25+
26+* Throw 413 for too large payload
27+* Throw 431 for too large headers
28+ [#741](https://github.com/yesodweb/wai/pull/741)
29+
30+## 3.0.25
31+
32+* Supporting `network` version 3.0.
33+
34+## 3.0.24.3
35+
36+* Drop unnecessary `lifted-base` dependency
37+* Drop unnecessary `stringsearch` dependency [#714](https://github.com/yesodweb/wai/pull/714)
38+
39+## 3.0.24.2
40+
41+* Consider quoted multipart form boundary markers [#700](https://github.com/yesodweb/wai/pull/700).
42+* Don't raise exceptions in `formatAsJSON` [#709](https://github.com/yesodweb/wai/pull/709)
43+
44+## 3.0.24.1
45+
46+* Fix a "file not found" exception in wai-extra [#705](https://github.com/yesodweb/wai/pull/706)
47+
48+## 3.0.24.0
49+
50+* Add timeout middleware [#702](https://github.com/yesodweb/wai/pull/702).
51+
52+## 3.0.23.0
53+
54+* Add rewriteRoot middleware [#697](https://github.com/yesodweb/wai/pull/697).
55+
56+## 3.0.22.1
57+
58+* Drop dependency on blaze-builder, requiring streaming-commons >= 0.2
59+
60+## 3.0.22.0
61+
62+* Support for streaming-commons 0.2
63+* Support for resourcet 1.2
64+
65+## 3.0.21.0
66+
67+* Export `Network.Wai.Parse.noLimitParseRequestBodyOptions` [#662](https://github.com/yesodweb/wai/pull/662).
68+
69+## 3.0.20.2
70+
71+* Revert previous change to `srequest`, which caused breakage
72+
73+## 3.0.20.1
74+
75+* Set `requestBodyLength` for `srequest` [#654](https://github.com/yesodweb/wai/pull/654)
76+
77+## 3.0.20.0
78+
79+* runSessionWith (runSession variant that gives access to ClientState) [#629](https://github.com/yesodweb/wai/pull/629)
80+
81+## 3.0.19.1
82+
83+* All loggers follow the autoFlush setting [#604](https://github.com/yesodweb/wai/pull/604)
84+
85+## 3.0.19
86+
87+* Add a new function basicAuth', which passes request to the CheckCreds argument.
88+
89+## 3.0.18
90+
91+* ForceSSL: preserve port number when redirecting to https. [#582](https://github.com/yesodweb/wai/pull/582)
92+
93+## 3.0.17
94+
95+* Gzip pre compressed [#580](https://github.com/yesodweb/wai/pull/580)
96+
97+## 3.0.16.1
98+
99+* Fix the way the header length is checked (for limiting the max header length)
100+
101+## 3.0.16.0
102+
103+* Add a new function "parseRequestBodyEx" that allows various size limits to be set.
104+
105+## 3.0.15.3
106+
107+* Allow wai-logger 2.3
108+
109+## 3.0.15.2
110+
111+* Doc improvements
112+
113+## 3.0.15.1
114+
115+* don't use deprecated CRT functions on Windows [#544](https://github.com/yesodweb/wai/pull/544)
116+
117+## 3.0.15
118+
119+* add requestSizeCheck [#525](https://github.com/yesodweb/wai/pull/525)
120+
121+## 3.0.14.3
122+
123+* Add missing `requestHeaderReferer` and `requestHeaderUserAgent` fields to CGI [yesod#1186](https://github.com/yesodweb/yesod/issues/1186)
124+
125+## 3.0.14.2
126+
127+* Case insensitive multipart request header lookup [#518](https://github.com/yesodweb/wai/pull/518)
128+
129+## 3.0.14.1
130+
131+* Doc update for logStdout and logStdoutDev [#515](https://github.com/yesodweb/wai/issues/515)
132+
133+## 3.0.14
134+
135+* Middleware to force domain names. [#506](https://github.com/yesodweb/wai/issues/506) [#507](https://github.com/yesodweb/wai/pull/507)
136+
137+## 3.0.13.1
138+
139+* Support wai 3.2
140+
141+## 3.0.13
142+
143+* Autoflush handle [#466](https://github.com/yesodweb/wai/pull/466)
144+
145+## 3.0.12
146+
147+* Add Network.Wai.Header.contentLength to read the Content-Length header of a response
148+* The gzip middleware no longer zips responses smaller than 860 bytes
149+
150+## 3.0.11
151+
152+* Add constructor for more detailed custom output formats for RequestLogger
153+* Add JSON output formatter for RequestLogger
154+
155+## 3.0.10
156+
157+* Adding Request Body to RequestLogger [#401](https://github.com/yesodweb/wai/pull/401)
158+
159+## 3.0.9
160+
161+* Network.Wai.Middleware.Routed module added
162+
163+## 3.0.7
164+
165+* Add appearsSecure: check if a request appears to be using SSL even in the
166+ presence of reverse proxies [#362](https://github.com/yesodweb/wai/pull/362)
167+* Add ForceSSL middleware [#363](https://github.com/yesodweb/wai/pull/363)
168+* Add Approot middleware
169+
170+## 3.0.6.1
171+
172+* Test code: only include a Cookie header if there are cookies. Without this
173+ patch, yesod-test cookie handling is broken.
174+
175+## 3.0.6
176+
177+* Add Cookie Handling to Network.Wai.Test [#356](https://github.com/yesodweb/wai/pull/356)
178+
179+## 3.0.5
180+
181+* add functions to extract authentication data from Authorization header [#352](add functions to extract authentication data from Authorization header #352)
182+
183+## 3.0.4.6
184+
185+* Access log sequence not valid [#336](https://github.com/yesodweb/wai/issues/336)
186+
187+## 3.0.4.5
188+
189+* Allow fast-logger 2.3
190+
191+## 3.0.4.3
192+
193+Test suite warning cleanup
194+
195+## 3.0.4.2
196+
197+Allow blaze-builder 0.4
198+
199+## 3.0.4.1
200+
201+Fix compilation failure on Windows [#321](https://github.com/yesodweb/wai/issues/321)
202+
203+## 3.0.4
204+
205+Add the `StreamFile` middleware.
206+
207+## 3.0.3
208+
209+Add the `AddHeaders` middleware.
wai-extra/LICENSEnew file+20
@@ -0,0 +1,20 @@
1+Copyright (c) 2012 Michael Snoyman, http://www.yesodweb.com/
2+
3+Permission is hereby granted, free of charge, to any person obtaining
4+a copy of this software and associated documentation files (the
5+"Software"), to deal in the Software without restriction, including
6+without limitation the rights to use, copy, modify, merge, publish,
7+distribute, sublicense, and/or sell copies of the Software, and to
8+permit persons to whom the Software is furnished to do so, subject to
9+the following conditions:
10+
11+The above copyright notice and this permission notice shall be
12+included in all copies or substantial portions of the Software.
13+
14+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
wai-extra/Network/Wai/EventSource.hsnew file+59
@@ -0,0 +1,59 @@
1+{-|
2+ A WAI adapter to the HTML5 Server-Sent Events API.
3+
4+ If running through a proxy like Nginx you might need to add the
5+ headers:
6+
7+ > [ ("X-Accel-Buffering", "no"), ("Cache-Control", "no-cache")]
8+
9+ There is a small example using these functions in the @example@ directory.
10+-}
11+module Network.Wai.EventSource (
12+ ServerEvent(..),
13+ eventSourceAppChan,
14+ eventSourceAppIO,
15+ eventStreamAppRaw
16+ ) where
17+
18+import Data.Function (fix)
19+import Control.Concurrent.Chan (Chan, dupChan, readChan)
20+import Control.Monad.IO.Class (liftIO)
21+import Network.HTTP.Types (status200, hContentType)
22+import Network.Wai (Application, responseStream)
23+
24+import Network.Wai.EventSource.EventStream
25+
26+-- | Make a new WAI EventSource application reading events from
27+-- the given channel.
28+eventSourceAppChan :: Chan ServerEvent -> Application
29+eventSourceAppChan chan req sendResponse = do
30+ chan' <- liftIO $ dupChan chan
31+ eventSourceAppIO (readChan chan') req sendResponse
32+
33+-- | Make a new WAI EventSource application reading events from
34+-- the given IO action.
35+eventSourceAppIO :: IO ServerEvent -> Application
36+eventSourceAppIO src _ sendResponse =
37+ sendResponse $ responseStream
38+ status200
39+ [(hContentType, "text/event-stream")]
40+ $ \sendChunk flush -> fix $ \loop -> do
41+ se <- src
42+ case eventToBuilder se of
43+ Nothing -> return ()
44+ Just b -> sendChunk b >> flush >> loop
45+
46+-- | Make a new WAI EventSource application with a handler that emits events.
47+--
48+-- @since 3.0.28
49+eventStreamAppRaw :: ((ServerEvent -> IO()) -> IO () -> IO ()) -> Application
50+eventStreamAppRaw handler _ sendResponse =
51+ sendResponse $ responseStream
52+ status200
53+ [(hContentType, "text/event-stream")]
54+ $ \sendChunk flush -> handler (sendEvent sendChunk) flush
55+ where
56+ sendEvent sendChunk event =
57+ case eventToBuilder event of
58+ Nothing -> return ()
59+ Just b -> sendChunk b
wai-extra/Network/Wai/EventSource/EventStream.hsnew file+76
@@ -0,0 +1,76 @@
1+{-# LANGUAGE CPP #-}
2+{- code adapted by Mathias Billman originally from Chris Smith https://github.com/cdsmith/gloss-web -}
3+
4+{-|
5+ Internal module, usually you don't need to use it.
6+-}
7+module Network.Wai.EventSource.EventStream (
8+ ServerEvent(..),
9+ eventToBuilder
10+ ) where
11+
12+import Data.ByteString.Builder
13+#if __GLASGOW_HASKELL__ < 710
14+import Data.Monoid
15+#endif
16+
17+{-|
18+ Type representing a communication over an event stream. This can be an
19+ actual event, a comment, a modification to the retry timer, or a special
20+ "close" event indicating the server should close the connection.
21+-}
22+data ServerEvent
23+ = ServerEvent {
24+ eventName :: Maybe Builder,
25+ eventId :: Maybe Builder,
26+ eventData :: [Builder]
27+ }
28+ | CommentEvent {
29+ eventComment :: Builder
30+ }
31+ | RetryEvent {
32+ eventRetry :: Int
33+ }
34+ | CloseEvent
35+
36+
37+{-|
38+ Newline as a Builder.
39+-}
40+nl :: Builder
41+nl = char7 '\n'
42+
43+
44+{-|
45+ Field names as Builder
46+-}
47+nameField, idField, dataField, retryField, commentField :: Builder
48+nameField = string7 "event:"
49+idField = string7 "id:"
50+dataField = string7 "data:"
51+retryField = string7 "retry:"
52+commentField = char7 ':'
53+
54+
55+{-|
56+ Wraps the text as a labeled field of an event stream.
57+-}
58+field :: Builder -> Builder -> Builder
59+field l b = l `mappend` b `mappend` nl
60+
61+
62+{-|
63+ Converts a 'ServerEvent' to its wire representation as specified by the
64+ @text/event-stream@ content type.
65+-}
66+eventToBuilder :: ServerEvent -> Maybe Builder
67+eventToBuilder (CommentEvent txt) = Just $ field commentField txt
68+eventToBuilder (RetryEvent n) = Just $ field retryField (string8 . show $ n)
69+eventToBuilder (CloseEvent) = Nothing
70+eventToBuilder (ServerEvent n i d)= Just $
71+ (name n $ evid i $ mconcat (map (field dataField) d)) `mappend` nl
72+ where
73+ name Nothing = id
74+ name (Just n') = mappend (field nameField n')
75+ evid Nothing = id
76+ evid (Just i') = mappend (field idField i')
wai-extra/Network/Wai/Handler/CGI.hsnew file+201
@@ -0,0 +1,201 @@
1+{-# LANGUAGE RankNTypes, CPP #-}
2+-- | Backend for Common Gateway Interface. Almost all users should use the
3+-- 'run' function.
4+module Network.Wai.Handler.CGI
5+ ( run
6+ , runSendfile
7+ , runGeneric
8+ , requestBodyFunc
9+ ) where
10+
11+import Network.Wai
12+import Network.Wai.Internal
13+import Network.Socket (getAddrInfo, addrAddress)
14+import Data.IORef
15+import Data.Maybe (fromMaybe)
16+import qualified Data.ByteString.Char8 as B
17+import qualified Data.ByteString.Lazy as L
18+import Control.Arrow ((***))
19+import Data.Char (toLower)
20+import qualified System.IO
21+import qualified Data.String as String
22+import Data.ByteString.Builder (byteString, toLazyByteString, char7, string8)
23+import Data.ByteString.Builder.Extra (flush)
24+import Data.ByteString.Lazy.Internal (defaultChunkSize)
25+import System.IO (Handle)
26+import Network.HTTP.Types (Status (..), hRange, hContentType, hContentLength)
27+import qualified Network.HTTP.Types as H
28+import qualified Data.CaseInsensitive as CI
29+#if __GLASGOW_HASKELL__ < 710
30+import Data.Monoid (mconcat, mempty, mappend)
31+#endif
32+
33+import qualified Data.Streaming.ByteString.Builder as Builder
34+import Data.Function (fix)
35+import Control.Monad (unless, void)
36+
37+#if WINDOWS
38+import System.Environment (getEnvironment)
39+#else
40+import qualified System.Posix.Env.ByteString as Env
41+
42+getEnvironment :: IO [(String, String)]
43+getEnvironment = map (B.unpack *** B.unpack) `fmap` Env.getEnvironment
44+#endif
45+
46+safeRead :: Read a => a -> String -> a
47+safeRead d s =
48+ case reads s of
49+ ((x, _):_) -> x
50+ [] -> d
51+
52+lookup' :: String -> [(String, String)] -> String
53+lookup' key pairs = fromMaybe "" $ lookup key pairs
54+
55+-- | Run an application using CGI.
56+run :: Application -> IO ()
57+run app = do
58+ vars <- getEnvironment
59+ let input = requestBodyHandle System.IO.stdin
60+ output = B.hPut System.IO.stdout
61+ runGeneric vars input output Nothing app
62+
63+-- | Some web servers provide an optimization for sending files via a sendfile
64+-- system call via a special header. To use this feature, provide that header
65+-- name here.
66+runSendfile :: B.ByteString -- ^ sendfile header
67+ -> Application -> IO ()
68+runSendfile sf app = do
69+ vars <- getEnvironment
70+ let input = requestBodyHandle System.IO.stdin
71+ output = B.hPut System.IO.stdout
72+ runGeneric vars input output (Just sf) app
73+
74+-- | A generic CGI helper, which allows other backends (FastCGI and SCGI) to
75+-- use the same code as CGI. Most users will not need this function, and can
76+-- stick with 'run' or 'runSendfile'.
77+runGeneric
78+ :: [(String, String)] -- ^ all variables
79+ -> (Int -> IO (IO B.ByteString)) -- ^ responseBody of input
80+ -> (B.ByteString -> IO ()) -- ^ destination for output
81+ -> Maybe B.ByteString -- ^ does the server support the X-Sendfile header?
82+ -> Application
83+ -> IO ()
84+runGeneric vars inputH outputH xsendfile app = do
85+ let rmethod = B.pack $ lookup' "REQUEST_METHOD" vars
86+ pinfo = lookup' "PATH_INFO" vars
87+ qstring = lookup' "QUERY_STRING" vars
88+ contentLength = safeRead 0 $ lookup' "CONTENT_LENGTH" vars
89+ remoteHost' =
90+ case lookup "REMOTE_ADDR" vars of
91+ Just x -> x
92+ Nothing ->
93+ case lookup "REMOTE_HOST" vars of
94+ Just x -> x
95+ Nothing -> ""
96+ isSecure' =
97+ case map toLower $ lookup' "SERVER_PROTOCOL" vars of
98+ "https" -> True
99+ _ -> False
100+ addrs <- getAddrInfo Nothing (Just remoteHost') Nothing
101+ requestBody' <- inputH contentLength
102+ let addr =
103+ case addrs of
104+ a:_ -> addrAddress a
105+ [] -> error $ "Invalid REMOTE_ADDR or REMOTE_HOST: " ++ remoteHost'
106+ reqHeaders = map (cleanupVarName *** B.pack) vars
107+ env = Request
108+ { requestMethod = rmethod
109+ , rawPathInfo = B.pack pinfo
110+ , pathInfo = H.decodePathSegments $ B.pack pinfo
111+ , rawQueryString = B.pack qstring
112+ , queryString = H.parseQuery $ B.pack qstring
113+ , requestHeaders = reqHeaders
114+ , isSecure = isSecure'
115+ , remoteHost = addr
116+ , httpVersion = H.http11 -- FIXME
117+ , requestBody = requestBody'
118+ , vault = mempty
119+ , requestBodyLength = KnownLength $ fromIntegral contentLength
120+ , requestHeaderHost = lookup "host" reqHeaders
121+ , requestHeaderRange = lookup hRange reqHeaders
122+#if MIN_VERSION_wai(3,2,0)
123+ , requestHeaderReferer = lookup "referer" reqHeaders
124+ , requestHeaderUserAgent = lookup "user-agent" reqHeaders
125+#endif
126+ }
127+ void $ app env $ \res ->
128+ case (xsendfile, res) of
129+ (Just sf, ResponseFile s hs fp Nothing) -> do
130+ mapM_ outputH $ L.toChunks $ toLazyByteString $ sfBuilder s hs sf fp
131+ return ResponseReceived
132+ _ -> do
133+ let (s, hs, wb) = responseToStream res
134+ (blazeRecv, blazeFinish) <- Builder.newBuilderRecv Builder.defaultStrategy
135+ wb $ \b -> do
136+ let sendBuilder builder = do
137+ popper <- blazeRecv builder
138+ fix $ \loop -> do
139+ bs <- popper
140+ unless (B.null bs) $ do
141+ outputH bs
142+ loop
143+ sendBuilder $ headers s hs `mappend` char7 '\n'
144+ b sendBuilder (sendBuilder flush)
145+ blazeFinish >>= maybe (return ()) outputH
146+ return ResponseReceived
147+ where
148+ headers s hs = mconcat (map header $ status s : map header' (fixHeaders hs))
149+ status (Status i m) = (byteString "Status", mconcat
150+ [ string8 $ show i
151+ , char7 ' '
152+ , byteString m
153+ ])
154+ header' (x, y) = (byteString $ CI.original x, byteString y)
155+ header (x, y) = mconcat
156+ [ x
157+ , byteString ": "
158+ , y
159+ , char7 '\n'
160+ ]
161+ sfBuilder s hs sf fp = mconcat
162+ [ headers s hs
163+ , header $ (byteString sf, string8 fp)
164+ , char7 '\n'
165+ , byteString sf
166+ , byteString " not supported"
167+ ]
168+ fixHeaders h =
169+ case lookup hContentType h of
170+ Nothing -> (hContentType, "text/html; charset=utf-8") : h
171+ Just _ -> h
172+
173+cleanupVarName :: String -> CI.CI B.ByteString
174+cleanupVarName "CONTENT_TYPE" = hContentType
175+cleanupVarName "CONTENT_LENGTH" = hContentLength
176+cleanupVarName "SCRIPT_NAME" = "CGI-Script-Name"
177+cleanupVarName s =
178+ case s of
179+ 'H':'T':'T':'P':'_':a:as -> String.fromString $ a : helper' as
180+ _ -> String.fromString s -- FIXME remove?
181+ where
182+ helper' ('_':x:rest) = '-' : x : helper' rest
183+ helper' (x:rest) = toLower x : helper' rest
184+ helper' [] = []
185+
186+requestBodyHandle :: Handle -> Int -> IO (IO B.ByteString)
187+requestBodyHandle h = requestBodyFunc $ \i -> do
188+ bs <- B.hGet h i
189+ return $ if B.null bs then Nothing else Just bs
190+
191+requestBodyFunc :: (Int -> IO (Maybe B.ByteString)) -> Int -> IO (IO B.ByteString)
192+requestBodyFunc get count0 = do
193+ ref <- newIORef count0
194+ return $ do
195+ count <- readIORef ref
196+ if count <= 0
197+ then return B.empty
198+ else do
199+ mbs <- get $ min count defaultChunkSize
200+ writeIORef ref $ count - maybe 0 B.length mbs
201+ return $ fromMaybe B.empty mbs
wai-extra/Network/Wai/Handler/SCGI.hsnew file+107
@@ -0,0 +1,107 @@
1+{-# LANGUAGE CPP, ForeignFunctionInterface #-}
2+module Network.Wai.Handler.SCGI
3+ ( run
4+ , runSendfile
5+ ) where
6+
7+import Network.Wai
8+import Network.Wai.Handler.CGI (runGeneric, requestBodyFunc)
9+import Foreign.Ptr
10+import Foreign.Marshal.Alloc
11+import Foreign.C
12+import Data.ByteString (ByteString)
13+import qualified Data.ByteString as S
14+import qualified Data.ByteString.Unsafe as S
15+import qualified Data.ByteString.Char8 as S8
16+import Data.IORef
17+import Data.ByteString.Lazy.Internal (defaultChunkSize)
18+
19+run :: Application -> IO ()
20+run app = runOne Nothing app >> run app
21+
22+runSendfile :: ByteString -> Application -> IO ()
23+runSendfile sf app = runOne (Just sf) app >> runSendfile sf app
24+
25+runOne :: Maybe ByteString -> Application -> IO ()
26+runOne sf app = do
27+ socket <- c'accept 0 nullPtr nullPtr
28+ headersBS <- readNetstring socket
29+ let headers@((_, conLenS):_) = parseHeaders $ S.split 0 headersBS
30+ let conLen = case reads conLenS of
31+ (i, _):_ -> i
32+ [] -> 0
33+ conLenI <- newIORef conLen
34+ runGeneric headers (requestBodyFunc $ input socket conLenI)
35+ (write socket) sf app
36+ drain socket conLenI
37+ _ <- c'close socket
38+ return ()
39+
40+write :: CInt -> S.ByteString -> IO ()
41+write socket bs = S.unsafeUseAsCStringLen bs $ \(s, l) -> do
42+ _ <- c'write socket s (fromIntegral l)
43+ return ()
44+
45+input :: CInt -> IORef Int -> Int -> IO (Maybe S.ByteString)
46+input socket ilen rlen = do
47+ len <- readIORef ilen
48+ case len of
49+ 0 -> return Nothing
50+ _ -> do
51+ bs <- readByteString socket
52+ $ minimum [defaultChunkSize, len, rlen]
53+ writeIORef ilen $ len - S.length bs
54+ return $ Just bs
55+
56+drain :: CInt -> IORef Int -> IO () -- FIXME do it in chunks
57+drain socket ilen = do
58+ len <- readIORef ilen
59+ _ <- readByteString socket len
60+ return ()
61+
62+parseHeaders :: [S.ByteString] -> [(String, String)]
63+parseHeaders (x:y:z) = (S8.unpack x, S8.unpack y) : parseHeaders z
64+parseHeaders _ = []
65+
66+readNetstring :: CInt -> IO S.ByteString
67+readNetstring socket = do
68+ len <- readLen 0
69+ bs <- readByteString socket len
70+ _ <- readByteString socket 1 -- the comma
71+ return bs
72+ where
73+ readLen l = do
74+ bs <- readByteString socket 1
75+ let [c] = S8.unpack bs
76+ if c == ':'
77+ then return l
78+ else readLen $ l * 10 + (fromEnum c - fromEnum '0')
79+
80+readByteString :: CInt -> Int -> IO S.ByteString
81+readByteString socket len = do
82+ buf <- mallocBytes len
83+ _ <- c'read socket buf $ fromIntegral len
84+ S.unsafePackCStringFinalizer (castPtr buf) len $ free buf
85+
86+foreign import ccall unsafe "accept"
87+ c'accept :: CInt -> Ptr a -> Ptr a -> IO CInt
88+
89+#if WINDOWS
90+foreign import ccall unsafe "_close"
91+ c'close :: CInt -> IO CInt
92+
93+foreign import ccall unsafe "_write"
94+ c'write :: CInt -> Ptr CChar -> CInt -> IO CInt
95+
96+foreign import ccall unsafe "_read"
97+ c'read :: CInt -> Ptr CChar -> CInt -> IO CInt
98+#else
99+foreign import ccall unsafe "close"
100+ c'close :: CInt -> IO CInt
101+
102+foreign import ccall unsafe "write"
103+ c'write :: CInt -> Ptr CChar -> CInt -> IO CInt
104+
105+foreign import ccall unsafe "read"
106+ c'read :: CInt -> Ptr CChar -> CInt -> IO CInt
107+#endif
wai-extra/Network/Wai/Header.hsnew file+18
@@ -0,0 +1,18 @@
1+-- | Some helpers for dealing with WAI 'Header's.
2+
3+module Network.Wai.Header
4+ ( contentLength
5+ ) where
6+
7+import qualified Data.ByteString.Char8 as S8
8+import Network.HTTP.Types as H
9+
10+-- | More useful for a response. A Wai Request already has a requestBodyLength
11+contentLength :: [(HeaderName, S8.ByteString)] -> Maybe Integer
12+contentLength hdrs = lookup H.hContentLength hdrs >>= readInt
13+
14+readInt :: S8.ByteString -> Maybe Integer
15+readInt bs =
16+ case S8.readInteger bs of
17+ Just (i, rest) | S8.null rest -> Just i
18+ _ -> Nothing
wai-extra/Network/Wai/Middleware/AcceptOverride.hsnew file+24
@@ -0,0 +1,24 @@
1+module Network.Wai.Middleware.AcceptOverride
2+ ( acceptOverride
3+ ) where
4+
5+import Network.Wai
6+import Control.Monad (join)
7+import Data.ByteString (ByteString)
8+
9+acceptOverride :: Middleware
10+acceptOverride app req =
11+ app req'
12+ where
13+ req' =
14+ case join $ lookup "_accept" $ queryString req of
15+ Nothing -> req
16+ Just a -> req { requestHeaders = changeVal "Accept" a $ requestHeaders req}
17+
18+changeVal :: Eq a
19+ => a
20+ -> ByteString
21+ -> [(a, ByteString)]
22+ -> [(a, ByteString)]
23+changeVal key val old = (key, val)
24+ : filter (\(k, _) -> k /= key) old
wai-extra/Network/Wai/Middleware/AddHeaders.hsnew file+24
@@ -0,0 +1,24 @@
1+-- |
2+--
3+-- Since 3.0.3
4+module Network.Wai.Middleware.AddHeaders
5+ ( addHeaders
6+ ) where
7+
8+import Network.HTTP.Types (Header)
9+import Network.Wai (Middleware, modifyResponse, mapResponseHeaders)
10+import Network.Wai.Internal (Response(..))
11+import Data.ByteString (ByteString)
12+
13+import qualified Data.CaseInsensitive as CI
14+import Control.Arrow (first)
15+
16+addHeaders :: [(ByteString, ByteString)] -> Middleware
17+-- ^ Prepend a list of headers without any checks
18+--
19+-- Since 3.0.3
20+
21+addHeaders h = modifyResponse $ addHeaders' (map (first CI.mk) h)
22+
23+addHeaders' :: [Header] -> Response -> Response
24+addHeaders' h = mapResponseHeaders (\hs -> h ++ hs)
wai-extra/Network/Wai/Middleware/Approot.hsnew file+121
@@ -0,0 +1,121 @@
1+{-# LANGUAGE DeriveDataTypeable #-}
2+-- | Middleware for establishing the root of the application.
3+--
4+-- Many application need the ability to create URLs referring back to the
5+-- application itself. For example: generate RSS feeds or sitemaps, giving
6+-- users copy-paste links, or sending emails. In many cases, the approot can be
7+-- determined correctly from the request headers. However, some things can
8+-- prevent this, especially reverse proxies. This module provides multiple ways
9+-- of configuring approot discovery, and functions for applications to get that
10+-- approot.
11+--
12+-- Approots are structured such that they can be prepended to a string such as
13+-- @/foo/bar?baz=bin@. For example, if your application is hosted on
14+-- example.com using HTTPS, the approot would be @https://example.com@. Note
15+-- the lack of a trailing slash.
16+module Network.Wai.Middleware.Approot
17+ ( -- * Middleware
18+ approotMiddleware
19+ -- * Common providers
20+ , envFallback
21+ , envFallbackNamed
22+ , hardcoded
23+ , fromRequest
24+ -- * Functions for applications
25+ , getApproot
26+ , getApprootMay
27+ ) where
28+
29+import Control.Exception (Exception, throw)
30+import Data.ByteString (ByteString)
31+import qualified Data.ByteString.Char8 as S8
32+import Data.Maybe (fromMaybe)
33+import Data.Typeable (Typeable)
34+import qualified Data.Vault.Lazy as V
35+import Network.Wai (Request, vault, Middleware)
36+import Network.Wai.Request (guessApproot)
37+import System.Environment (getEnvironment)
38+import System.IO.Unsafe (unsafePerformIO)
39+
40+approotKey :: V.Key ByteString
41+approotKey = unsafePerformIO V.newKey
42+{-# NOINLINE approotKey #-}
43+
44+-- | The most generic version of the middleware, allowing you to provide a
45+-- function to get the approot for each request. For many use cases, one of the
46+-- helper functions provided by this module will give the necessary
47+-- functionality more conveniently.
48+--
49+-- Since 3.0.7
50+approotMiddleware :: (Request -> IO ByteString) -- ^ get the approot
51+ -> Middleware
52+approotMiddleware getRoot app req respond = do
53+ ar <- getRoot req
54+ let req' = req { vault = V.insert approotKey ar $ vault req }
55+ app req' respond
56+
57+-- | Same as @'envFallbackNamed' "APPROOT"@.
58+--
59+-- The environment variable @APPROOT@ is used by Keter, School of Haskell, and yesod-devel.
60+--
61+-- Since 3.0.7
62+envFallback :: IO Middleware
63+envFallback = envFallbackNamed "APPROOT"
64+
65+-- | Produce a middleware that takes the approot from the given environment
66+-- variable, falling back to the behavior of 'fromRequest' if the variable is
67+-- not set.
68+--
69+-- Since 3.0.7
70+envFallbackNamed :: String -> IO Middleware
71+envFallbackNamed name = do
72+ env <- getEnvironment
73+ case lookup name env of
74+ Just s -> return $ hardcoded $ S8.pack s
75+ Nothing -> return fromRequest
76+
77+-- | Hard-code the given value as the approot.
78+--
79+-- Since 3.0.7
80+hardcoded :: ByteString -> Middleware
81+hardcoded ar = approotMiddleware (const $ return ar)
82+
83+-- | Get the approot by analyzing the request. This is not a full-proof
84+-- approach, but in many common cases will work. Situations that can break this
85+-- are:
86+--
87+-- * Requests which spoof headers and imply the connection is over HTTPS
88+--
89+-- * Reverse proxies that change ports in surprising ways
90+--
91+-- * Invalid Host headers
92+--
93+-- * Reverse proxies which modify the path info
94+--
95+-- Normally trusting headers in this way is insecure, however in the case of
96+-- approot, the worst that can happen is that the client will get an incorrect
97+-- URL. If you are relying on the approot for some security-sensitive purpose,
98+-- it is highly recommended to use @hardcoded@, which cannot be spoofed.
99+--
100+-- Since 3.0.7
101+fromRequest :: Middleware
102+fromRequest = approotMiddleware (return . guessApproot)
103+
104+data ApprootMiddlewareNotSetup = ApprootMiddlewareNotSetup
105+ deriving (Show, Typeable)
106+instance Exception ApprootMiddlewareNotSetup
107+
108+-- | Get the approot set by the middleware. If the middleware is not in use,
109+-- then this function will return an exception. For a total version of the
110+-- function, see 'getApprootMay'.
111+--
112+-- Since 3.0.7
113+getApproot :: Request -> ByteString
114+getApproot = fromMaybe (throw ApprootMiddlewareNotSetup) . getApprootMay
115+
116+-- | A total version of 'getApproot', which returns 'Nothing' if the middleware
117+-- is not in use.
118+--
119+-- Since 3.0.7
120+getApprootMay :: Request -> Maybe ByteString
121+getApprootMay req = V.lookup approotKey $ vault req
wai-extra/Network/Wai/Middleware/Autohead.hsnew file+17
@@ -0,0 +1,17 @@
1+{-# LANGUAGE CPP #-}
2+-- | Automatically produce responses to HEAD requests based on the underlying
3+-- applications GET response.
4+module Network.Wai.Middleware.Autohead (autohead) where
5+
6+import Network.Wai
7+#if __GLASGOW_HASKELL__ < 710
8+import Data.Monoid (mempty)
9+#endif
10+
11+autohead :: Middleware
12+autohead app req sendResponse
13+ | requestMethod req == "HEAD" = app req { requestMethod = "GET" } $ \res -> do
14+ let (s, hs, _) = responseToStream res
15+ sendResponse $ responseBuilder s hs mempty
16+ | otherwise = app req sendResponse
17+
wai-extra/Network/Wai/Middleware/CleanPath.hsnew file+32
@@ -0,0 +1,32 @@
1+{-# LANGUAGE CPP #-}
2+module Network.Wai.Middleware.CleanPath
3+ ( cleanPath
4+ ) where
5+
6+import Network.Wai
7+import qualified Data.ByteString.Char8 as B
8+import qualified Data.ByteString.Lazy as L
9+import Network.HTTP.Types (status301, hLocation)
10+import Data.Text (Text)
11+#if __GLASGOW_HASKELL__ < 710
12+import Data.Monoid (mconcat)
13+#endif
14+
15+cleanPath :: ([Text] -> Either B.ByteString [Text])
16+ -> B.ByteString
17+ -> ([Text] -> Application)
18+ -> Application
19+cleanPath splitter prefix app env sendResponse =
20+ case splitter $ pathInfo env of
21+ Right pieces -> app pieces env sendResponse
22+ Left p -> sendResponse
23+ $ responseLBS status301
24+ [(hLocation, mconcat [prefix, p, suffix])]
25+ $ L.empty
26+ where
27+ -- include the query string if present
28+ suffix =
29+ case B.uncons $ rawQueryString env of
30+ Nothing -> B.empty
31+ Just ('?', _) -> rawQueryString env
32+ _ -> B.cons '?' $ rawQueryString env
wai-extra/Network/Wai/Middleware/ForceDomain.hsnew file+38
@@ -0,0 +1,38 @@
1+-- |
2+--
3+-- @since 3.0.14
4+module Network.Wai.Middleware.ForceDomain where
5+
6+import Data.ByteString (ByteString)
7+import Data.Monoid ((<>), mempty)
8+import Network.HTTP.Types (hLocation, methodGet, status301, status307)
9+import Prelude
10+
11+import Network.Wai
12+import Network.Wai.Request
13+
14+-- | Force a domain by redirecting.
15+-- The `checkDomain` function takes the current domain and checks whether it is correct.
16+-- It should return `Nothing` if the domain is correct, or `Just "domain.com"` if it is incorrect.
17+--
18+-- @since 3.0.14
19+forceDomain :: (ByteString -> Maybe ByteString) -> Middleware
20+forceDomain checkDomain app req sendResponse =
21+ case requestHeaderHost req >>= checkDomain of
22+ Nothing ->
23+ app req sendResponse
24+ Just domain ->
25+ sendResponse $ redirectResponse domain
26+
27+ where
28+ -- From: Network.Wai.Middleware.ForceSSL
29+ redirectResponse domain =
30+ responseBuilder status [(hLocation, location domain)] mempty
31+
32+ location h =
33+ let p = if appearsSecure req then "https://" else "http://" in
34+ p <> h <> rawPathInfo req <> rawQueryString req
35+
36+ status
37+ | requestMethod req == methodGet = status301
38+ | otherwise = status307
wai-extra/Network/Wai/Middleware/ForceSSL.hsnew file+37
@@ -0,0 +1,37 @@
1+{-# LANGUAGE CPP #-}
2+-- | Redirect non-SSL requests to https
3+--
4+-- Since 3.0.7
5+module Network.Wai.Middleware.ForceSSL
6+ ( forceSSL
7+ ) where
8+
9+import Network.Wai
10+import Network.Wai.Request
11+
12+#if __GLASGOW_HASKELL__ < 710
13+import Control.Applicative ((<$>))
14+import Data.Monoid (mempty)
15+#endif
16+
17+import Data.Monoid ((<>))
18+import Network.HTTP.Types (hLocation, methodGet, status301, status307)
19+
20+-- | For requests that don't appear secure, redirect to https
21+--
22+-- Since 3.0.7
23+forceSSL :: Middleware
24+forceSSL app req sendResponse =
25+ case (appearsSecure req, redirectResponse req) of
26+ (False, Just resp) -> sendResponse resp
27+ _ -> app req sendResponse
28+
29+redirectResponse :: Request -> Maybe Response
30+redirectResponse req = do
31+ host <- requestHeaderHost req
32+ return $ responseBuilder status [(hLocation, location host)] mempty
33+ where
34+ location h = "https://" <> h <> rawPathInfo req <> rawQueryString req
35+ status
36+ | requestMethod req == methodGet = status301
37+ | otherwise = status307
wai-extra/Network/Wai/Middleware/Gzip.hsnew file+230
@@ -0,0 +1,230 @@
1+{-# LANGUAGE Rank2Types, ScopedTypeVariables #-}
2+---------------------------------------------------------
3+-- |
4+-- Module : Network.Wai.Middleware.Gzip
5+-- Copyright : Michael Snoyman
6+-- License : BSD3
7+--
8+-- Maintainer : Michael Snoyman <michael@snoyman.com>
9+-- Stability : Unstable
10+-- Portability : portable
11+--
12+-- Automatic gzip compression of responses.
13+--
14+---------------------------------------------------------
15+module Network.Wai.Middleware.Gzip
16+ ( gzip
17+ , GzipSettings
18+ , gzipFiles
19+ , GzipFiles (..)
20+ , gzipCheckMime
21+ , def
22+ , defaultCheckMime
23+ ) where
24+
25+import Network.Wai
26+import Data.Maybe (fromMaybe, isJust)
27+import qualified Data.ByteString.Char8 as S8
28+import qualified Data.ByteString as S
29+import Data.Default.Class
30+import Network.HTTP.Types ( Status, Header, hContentEncoding, hUserAgent
31+ , hContentType, hContentLength)
32+import System.Directory (doesFileExist, createDirectoryIfMissing)
33+import Data.ByteString.Builder (byteString)
34+import qualified Data.ByteString.Builder.Extra as Blaze (flush)
35+import Control.Exception (try, SomeException)
36+import qualified Data.Set as Set
37+import Network.Wai.Header
38+import Network.Wai.Internal
39+import qualified Data.Streaming.ByteString.Builder as B
40+import qualified Data.Streaming.Zlib as Z
41+import Control.Monad (unless)
42+import Data.Function (fix)
43+import Control.Exception (throwIO)
44+import qualified System.IO as IO
45+import Data.ByteString.Lazy.Internal (defaultChunkSize)
46+import Data.Word8 (_semicolon)
47+
48+data GzipSettings = GzipSettings
49+ { gzipFiles :: GzipFiles
50+ , gzipCheckMime :: S.ByteString -> Bool
51+ }
52+
53+-- | Gzip behavior for files.
54+data GzipFiles
55+ = GzipIgnore -- ^ Do not compress file responses.
56+ | GzipCompress -- ^ Compress files. Note that this may counteract
57+ -- zero-copy response optimizations on some
58+ -- platforms.
59+ | GzipCacheFolder FilePath -- ^ Compress files, caching them in
60+ -- some directory.
61+ | GzipPreCompressed GzipFiles -- ^ If we use compression then try to use the filename with ".gz"
62+ -- appended to it, if the file is missing then try next action
63+ --
64+ -- @since 3.0.17
65+ deriving (Show, Eq, Read)
66+
67+-- | Use default MIME settings; /do not/ compress files.
68+instance Default GzipSettings where
69+ def = GzipSettings GzipIgnore defaultCheckMime
70+
71+-- | MIME types that will be compressed by default:
72+-- @text/@ @*@, @application/json@, @application/javascript@,
73+-- @application/ecmascript@, @image/x-icon@.
74+defaultCheckMime :: S.ByteString -> Bool
75+defaultCheckMime bs =
76+ S8.isPrefixOf "text/" bs || bs' `Set.member` toCompress
77+ where
78+ bs' = fst $ S.break (== _semicolon) bs
79+ toCompress = Set.fromList
80+ [ "application/json"
81+ , "application/javascript"
82+ , "application/ecmascript"
83+ , "image/x-icon"
84+ ]
85+
86+-- | Use gzip to compress the body of the response.
87+--
88+-- Analyzes the \"Accept-Encoding\" header from the client to determine
89+-- if gzip is supported.
90+--
91+-- File responses will be compressed according to the 'GzipFiles' setting.
92+--
93+-- Will only be applied based on the 'gzipCheckMime' setting. For default
94+-- behavior, see 'defaultCheckMime'.
95+gzip :: GzipSettings -> Middleware
96+gzip set app env sendResponse = app env $ \res ->
97+ case res of
98+ ResponseRaw{} -> sendResponse res
99+ ResponseFile{} | gzipFiles set == GzipIgnore -> sendResponse res
100+ _ -> if "gzip" `elem` enc && not isMSIE6 && not (isEncoded res) && (bigEnough res)
101+ then
102+ let runAction x = case x of
103+ (ResponseFile s hs file Nothing, GzipPreCompressed nextAction) ->
104+ let
105+ compressedVersion = file ++ ".gz"
106+ in
107+ doesFileExist compressedVersion >>= \y ->
108+ if y
109+ then (sendResponse $ ResponseFile s (fixHeaders hs) compressedVersion Nothing)
110+ else (runAction (ResponseFile s hs file Nothing, nextAction))
111+ (ResponseFile s hs file Nothing, GzipCacheFolder cache) ->
112+ case lookup hContentType hs of
113+ Just m
114+ | gzipCheckMime set m -> compressFile s hs file cache sendResponse
115+ _ -> sendResponse res
116+ (ResponseFile {}, GzipIgnore) -> sendResponse res
117+ _ -> compressE set res sendResponse
118+ in runAction (res, gzipFiles set)
119+ else sendResponse res
120+ where
121+ enc = fromMaybe [] $ (splitCommas . S8.unpack)
122+ `fmap` lookup "Accept-Encoding" (requestHeaders env)
123+ ua = fromMaybe "" $ lookup hUserAgent $ requestHeaders env
124+ isMSIE6 = "MSIE 6" `S.isInfixOf` ua
125+ isEncoded res = isJust $ lookup hContentEncoding $ responseHeaders res
126+
127+ bigEnough rsp = case contentLength (responseHeaders rsp) of
128+ Nothing -> True -- This could be a streaming case
129+ Just len -> len >= minimumLength
130+
131+ -- For a small enough response, gzipping will actually increase the size
132+ -- Potentially for anything less than 860 bytes gzipping could be a net loss
133+ -- The actual number is application specific though and may need to be adjusted
134+ -- http://webmasters.stackexchange.com/questions/31750/what-is-recommended-minimum-object-size-for-gzip-performance-benefits
135+ minimumLength = 860
136+
137+compressFile :: Status -> [Header] -> FilePath -> FilePath -> (Response -> IO a) -> IO a
138+compressFile s hs file cache sendResponse = do
139+ e <- doesFileExist tmpfile
140+ if e
141+ then onSucc
142+ else do
143+ createDirectoryIfMissing True cache
144+ x <- try $
145+ IO.withBinaryFile file IO.ReadMode $ \inH ->
146+ IO.withBinaryFile tmpfile IO.WriteMode $ \outH -> do
147+ deflate <- Z.initDeflate 7 $ Z.WindowBits 31
148+ -- FIXME this code should write to a temporary file, then
149+ -- rename to the final file
150+ let goPopper popper = fix $ \loop -> do
151+ res <- popper
152+ case res of
153+ Z.PRDone -> return ()
154+ Z.PRNext bs -> do
155+ S.hPut outH bs
156+ loop
157+ Z.PRError ex -> throwIO ex
158+ fix $ \loop -> do
159+ bs <- S.hGetSome inH defaultChunkSize
160+ unless (S.null bs) $ do
161+ Z.feedDeflate deflate bs >>= goPopper
162+ loop
163+ goPopper $ Z.finishDeflate deflate
164+ either onErr (const onSucc) (x :: Either SomeException ()) -- FIXME bad! don't catch all exceptions like that!
165+ where
166+ onSucc = sendResponse $ responseFile s (fixHeaders hs) tmpfile Nothing
167+
168+ onErr _ = sendResponse $ responseFile s hs file Nothing -- FIXME log the error message
169+
170+ tmpfile = cache ++ '/' : map safe file
171+ safe c
172+ | 'A' <= c && c <= 'Z' = c
173+ | 'a' <= c && c <= 'z' = c
174+ | '0' <= c && c <= '9' = c
175+ safe '-' = '-'
176+ safe '_' = '_'
177+ safe _ = '_'
178+
179+compressE :: GzipSettings
180+ -> Response
181+ -> (Response -> IO a)
182+ -> IO a
183+compressE set res sendResponse =
184+ case lookup hContentType hs of
185+ Just m | gzipCheckMime set m ->
186+ let hs' = fixHeaders hs
187+ in wb $ \body -> sendResponse $ responseStream s hs' $ \sendChunk flush -> do
188+ (blazeRecv, _) <- B.newBuilderRecv B.defaultStrategy
189+ deflate <- Z.initDeflate 1 (Z.WindowBits 31)
190+ let sendBuilder builder = do
191+ popper <- blazeRecv builder
192+ fix $ \loop -> do
193+ bs <- popper
194+ unless (S.null bs) $ do
195+ sendBS bs
196+ loop
197+ sendBS bs = Z.feedDeflate deflate bs >>= deflatePopper
198+ flushBuilder = do
199+ sendBuilder Blaze.flush
200+ deflatePopper $ Z.flushDeflate deflate
201+ flush
202+ deflatePopper popper = fix $ \loop -> do
203+ result <- popper
204+ case result of
205+ Z.PRDone -> return ()
206+ Z.PRNext bs' -> do
207+ sendChunk $ byteString bs'
208+ loop
209+ Z.PRError e -> throwIO e
210+
211+ body sendBuilder flushBuilder
212+ sendBuilder Blaze.flush
213+ deflatePopper $ Z.finishDeflate deflate
214+ _ -> sendResponse res
215+ where
216+ (s, hs, wb) = responseToStream res
217+
218+-- Remove Content-Length header, since we will certainly have a
219+-- different length after gzip compression.
220+fixHeaders :: [Header] -> [Header]
221+fixHeaders =
222+ ((hContentEncoding, "gzip") :) . filter notLength
223+ where
224+ notLength (x, _) = x /= hContentLength
225+
226+splitCommas :: String -> [String]
227+splitCommas [] = []
228+splitCommas x =
229+ let (y, z) = break (== ',') x
230+ in y : splitCommas (dropWhile (== ' ') $ drop 1 z)
wai-extra/Network/Wai/Middleware/HttpAuth.hsnew file+132
@@ -0,0 +1,132 @@
1+{-# LANGUAGE RecordWildCards, TupleSections, CPP #-}
2+-- | Implements HTTP Basic Authentication.
3+--
4+-- This module may add digest authentication in the future.
5+module Network.Wai.Middleware.HttpAuth
6+ ( -- * Middleware
7+ basicAuth
8+ , basicAuth'
9+ , CheckCreds
10+ , AuthSettings
11+ , authRealm
12+ , authOnNoAuth
13+ , authIsProtected
14+ -- * Helping functions
15+ , extractBasicAuth
16+ , extractBearerAuth
17+ ) where
18+#if __GLASGOW_HASKELL__ < 710
19+import Control.Applicative
20+#endif
21+import Data.ByteString (ByteString)
22+import Data.ByteString.Base64 (decodeLenient)
23+import Data.String (IsString (..))
24+import Data.Word8 (isSpace, _colon, toLower)
25+import Network.HTTP.Types (status401, hContentType, hAuthorization)
26+import Network.Wai
27+
28+import qualified Data.ByteString as S
29+
30+
31+-- | Check if a given username and password is valid.
32+type CheckCreds = ByteString
33+ -> ByteString
34+ -> IO Bool
35+
36+-- | Perform basic authentication.
37+--
38+-- > basicAuth (\u p -> return $ u == "michael" && p == "mypass") "My Realm"
39+--
40+-- @since 1.3.4
41+basicAuth :: CheckCreds
42+ -> AuthSettings
43+ -> Middleware
44+basicAuth checkCreds = basicAuth' (\_ -> checkCreds)
45+
46+-- | Like 'basicAuth', but also passes a request to the authentication function.
47+--
48+-- @since 3.0.19
49+basicAuth' :: (Request -> CheckCreds)
50+ -> AuthSettings
51+ -> Middleware
52+basicAuth' checkCreds AuthSettings {..} app req sendResponse = do
53+ isProtected <- authIsProtected req
54+ allowed <- if isProtected then check else return True
55+ if allowed
56+ then app req sendResponse
57+ else authOnNoAuth authRealm req sendResponse
58+ where
59+ check =
60+ case (lookup hAuthorization $ requestHeaders req)
61+ >>= extractBasicAuth of
62+ Nothing -> return False
63+ Just (username, password) -> checkCreds req username password
64+
65+-- | Basic authentication settings. This value is an instance of
66+-- @IsString@, so the recommended approach to create a value is to
67+-- provide a string literal (which will be the realm) and then
68+-- overriding individual fields.
69+--
70+-- > "My Realm" { authIsProtected = someFunc } :: AuthSettings
71+--
72+-- @since 1.3.4
73+data AuthSettings = AuthSettings
74+ { authRealm :: !ByteString
75+ -- ^
76+ --
77+ -- @since 1.3.4
78+ , authOnNoAuth :: !(ByteString -> Application)
79+ -- ^ Takes the realm and returns an appropriate 401 response when
80+ -- authentication is not provided.
81+ --
82+ -- @since 1.3.4
83+ , authIsProtected :: !(Request -> IO Bool)
84+ -- ^ Determine if access to the requested resource is restricted.
85+ --
86+ -- Default: always returns @True@.
87+ --
88+ -- @since 1.3.4
89+ }
90+
91+instance IsString AuthSettings where
92+ fromString s = AuthSettings
93+ { authRealm = fromString s
94+ , authOnNoAuth = \realm _req f -> f $ responseLBS
95+ status401
96+ [ (hContentType, "text/plain")
97+ , ("WWW-Authenticate", S.concat
98+ [ "Basic realm=\""
99+ , realm
100+ , "\""
101+ ])
102+ ]
103+ "Basic authentication is required"
104+ , authIsProtected = const $ return True
105+ }
106+
107+-- | Extract basic authentication data from usually __Authorization__
108+-- header value. Returns username and password
109+--
110+-- @since 3.0.5
111+extractBasicAuth :: ByteString -> Maybe (ByteString, ByteString)
112+extractBasicAuth bs =
113+ let (x, y) = S.break isSpace bs
114+ in if S.map toLower x == "basic"
115+ then extract $ S.dropWhile isSpace y
116+ else Nothing
117+ where
118+ extract encoded =
119+ let raw = decodeLenient encoded
120+ (username, password') = S.break (== _colon) raw
121+ in ((username,) . snd) <$> S.uncons password'
122+
123+-- | Extract bearer authentication data from __Authorization__ header
124+-- value. Returns bearer token
125+--
126+-- @since 3.0.5
127+extractBearerAuth :: ByteString -> Maybe ByteString
128+extractBearerAuth bs =
129+ let (x, y) = S.break isSpace bs
130+ in if S.map toLower x == "bearer"
131+ then Just $ S.dropWhile isSpace y
132+ else Nothing
wai-extra/Network/Wai/Middleware/Jsonp.hsnew file+94
@@ -0,0 +1,94 @@
1+{-# LANGUAGE RankNTypes, CPP #-}
2+---------------------------------------------------------
3+-- |
4+-- Module : Network.Wai.Middleware.Jsonp
5+-- Copyright : Michael Snoyman
6+-- License : BSD3
7+--
8+-- Maintainer : Michael Snoyman <michael@snoyman.com>
9+-- Stability : Unstable
10+-- Portability : portable
11+--
12+-- Automatic wrapping of JSON responses to convert into JSONP.
13+--
14+---------------------------------------------------------
15+module Network.Wai.Middleware.Jsonp (jsonp) where
16+
17+import Network.Wai
18+import Network.Wai.Internal
19+import Data.ByteString (ByteString)
20+import qualified Data.ByteString.Char8 as B8
21+import Data.ByteString.Builder.Extra (byteStringCopy)
22+import Data.ByteString.Builder (char7)
23+#if __GLASGOW_HASKELL__ < 710
24+import Data.Monoid (mappend)
25+#endif
26+import Control.Monad (join)
27+import Data.Maybe (fromMaybe)
28+import qualified Data.ByteString as S
29+import Network.HTTP.Types (hAccept, hContentType)
30+
31+-- | Wrap json responses in a jsonp callback.
32+--
33+-- Basically, if the user requested a \"text\/javascript\" and supplied a
34+-- \"callback\" GET parameter, ask the application for an
35+-- \"application/json\" response, then convert that into a JSONP response,
36+-- having a content type of \"text\/javascript\" and calling the specified
37+-- callback function.
38+jsonp :: Middleware
39+jsonp app env sendResponse = do
40+ let accept = fromMaybe B8.empty $ lookup hAccept $ requestHeaders env
41+ let callback :: Maybe B8.ByteString
42+ callback =
43+ if B8.pack "text/javascript" `B8.isInfixOf` accept
44+ then join $ lookup "callback" $ queryString env
45+ else Nothing
46+ let env' =
47+ case callback of
48+ Nothing -> env
49+ Just _ -> env
50+ { requestHeaders = changeVal hAccept
51+ "application/json"
52+ $ requestHeaders env
53+ }
54+ app env' $ \res ->
55+ case callback of
56+ Nothing -> sendResponse res
57+ Just c -> go c res
58+ where
59+ go c r@(ResponseBuilder s hs b) =
60+ sendResponse $ case checkJSON hs of
61+ Nothing -> r
62+ Just hs' -> responseBuilder s hs' $
63+ byteStringCopy c
64+ `mappend` char7 '('
65+ `mappend` b
66+ `mappend` char7 ')'
67+ go c r =
68+ case checkJSON hs of
69+ Just hs' -> addCallback c s hs' wb
70+ Nothing -> sendResponse r
71+ where
72+ (s, hs, wb) = responseToStream r
73+
74+ checkJSON hs =
75+ case lookup hContentType hs of
76+ Just x
77+ | B8.pack "application/json" `S.isPrefixOf` x ->
78+ Just $ fixHeaders hs
79+ _ -> Nothing
80+ fixHeaders = changeVal hContentType "text/javascript"
81+
82+ addCallback cb s hs wb =
83+ wb $ \body -> sendResponse $ responseStream s hs $ \sendChunk flush -> do
84+ sendChunk $ byteStringCopy cb `mappend` char7 '('
85+ _ <- body sendChunk flush
86+ sendChunk $ char7 ')'
87+
88+changeVal :: Eq a
89+ => a
90+ -> ByteString
91+ -> [(a, ByteString)]
92+ -> [(a, ByteString)]
93+changeVal key val old = (key, val)
94+ : filter (\(k, _) -> k /= key) old
wai-extra/Network/Wai/Middleware/Local.hsnew file+26
@@ -0,0 +1,26 @@
1+{-# LANGUAGE CPP #-}
2+-- | Only allow local connections.
3+--
4+module Network.Wai.Middleware.Local
5+ ( local
6+ ) where
7+
8+import Network.Wai (Middleware,remoteHost, Response)
9+import Network.Socket (SockAddr(..))
10+
11+-- | This middleware rejects non-local connections with a specific response.
12+-- It is useful when supporting web-based local applications, which would
13+-- typically want to reject external connections.
14+
15+local :: Response -> Middleware
16+local resp f r k = case remoteHost r of
17+ SockAddrInet _ h | h == fromIntegral home
18+ -> f r k
19+#if !defined(mingw32_HOST_OS) && !defined(_WIN32)
20+ SockAddrUnix _ -> f r k
21+#endif
22+ _ -> k $ resp
23+ where
24+ home :: Integer
25+ home = 127 + (256 * 256 * 256) * 1
26+
wai-extra/Network/Wai/Middleware/MethodOverride.hsnew file+20
@@ -0,0 +1,20 @@
1+module Network.Wai.Middleware.MethodOverride
2+ ( methodOverride
3+ ) where
4+
5+import Network.Wai
6+import Control.Monad (join)
7+
8+-- | Overriding of HTTP request method via `_method` query string parameter.
9+--
10+-- This middleware only applies when the initial request method is POST.
11+-- Allows submitting of normal HTML forms, without worries of semantic
12+-- mismatches with the HTTP spec.
13+methodOverride :: Middleware
14+methodOverride app req =
15+ app req'
16+ where
17+ req' =
18+ case (requestMethod req, join $ lookup "_method" $ queryString req) of
19+ ("POST", Just m) -> req { requestMethod = m }
20+ _ -> req
wai-extra/Network/Wai/Middleware/MethodOverridePost.hsnew file+44
@@ -0,0 +1,44 @@
1+{-# LANGUAGE CPP #-}
2+-----------------------------------------------------------------
3+-- | Module : Network.Wai.Middleware.MethodOverridePost
4+--
5+-- Changes the request-method via first post-parameter _method.
6+-----------------------------------------------------------------
7+module Network.Wai.Middleware.MethodOverridePost
8+ ( methodOverridePost
9+ ) where
10+
11+import Network.Wai
12+import Network.HTTP.Types (parseQuery, hContentType)
13+
14+#if __GLASGOW_HASKELL__ < 710
15+import Data.Monoid (mconcat, mempty)
16+#endif
17+import Data.IORef
18+import Data.ByteString.Lazy (toChunks)
19+
20+-- | Allows overriding of the HTTP request method via the _method post string parameter.
21+--
22+-- * Looks for the Content-Type requestHeader.
23+--
24+-- * If the header is set to application/x-www-form-urlencoded
25+-- and the first POST parameter is _method
26+-- then it changes the request-method to the value of that
27+-- parameter.
28+--
29+-- * This middleware only applies when the initial request method is POST.
30+--
31+methodOverridePost :: Middleware
32+methodOverridePost app req send =
33+ case (requestMethod req, lookup hContentType (requestHeaders req)) of
34+ ("POST", Just "application/x-www-form-urlencoded") -> setPost req >>= flip app send
35+ _ -> app req send
36+
37+setPost :: Request -> IO Request
38+setPost req = do
39+ body <- (mconcat . toChunks) `fmap` lazyRequestBody req
40+ ref <- newIORef body
41+ let rb = atomicModifyIORef ref $ \bs -> (mempty, bs)
42+ case parseQuery body of
43+ (("_method", Just newmethod):_) -> return $ req {requestBody = rb, requestMethod = newmethod}
44+ _ -> return $ req {requestBody = rb}
wai-extra/Network/Wai/Middleware/RequestLogger.hsnew file+371
@@ -0,0 +1,371 @@
1+{-# LANGUAGE RecordWildCards #-}
2+-- NOTE: Due to https://github.com/yesodweb/wai/issues/192, this module should
3+-- not use CPP.
4+module Network.Wai.Middleware.RequestLogger
5+ ( -- * Basic stdout logging
6+ logStdout
7+ , logStdoutDev
8+ -- * Create more versions
9+ , mkRequestLogger
10+ , RequestLoggerSettings
11+ , outputFormat
12+ , autoFlush
13+ , destination
14+ , OutputFormat (..)
15+ , OutputFormatter
16+ , OutputFormatterWithDetails
17+ , OutputFormatterWithDetailsAndHeaders
18+ , Destination (..)
19+ , Callback
20+ , IPAddrSource (..)
21+ ) where
22+
23+import System.IO (Handle, hFlush, stdout)
24+import qualified Data.ByteString.Builder as B (Builder, byteString)
25+import qualified Data.ByteString as BS
26+import Data.ByteString.Char8 (pack)
27+import Control.Monad (when)
28+import Control.Monad.IO.Class (liftIO)
29+import Network.Wai
30+ ( Request(..), requestBodyLength, RequestBodyLength(..)
31+ , Middleware
32+ , Response, responseStatus, responseHeaders
33+ )
34+import System.Log.FastLogger
35+import Network.HTTP.Types as H
36+import Data.Maybe (fromMaybe)
37+import Data.Monoid (mconcat, (<>))
38+import Data.Time (getCurrentTime, diffUTCTime, NominalDiffTime)
39+import Network.Wai.Parse (sinkRequestBody, lbsBackEnd, fileName, Param, File
40+ , getRequestBodyType)
41+import qualified Data.ByteString.Lazy as LBS
42+import qualified Data.ByteString.Char8 as S8
43+import System.Console.ANSI
44+import Data.IORef
45+import System.IO.Unsafe
46+import Network.Wai.Internal (Response (..))
47+import Data.Default.Class (Default (def))
48+import Network.Wai.Logger
49+import Network.Wai.Middleware.RequestLogger.Internal
50+import Network.Wai.Header (contentLength)
51+import Data.Text.Encoding (decodeUtf8')
52+
53+data OutputFormat
54+ = Apache IPAddrSource
55+ | Detailed Bool -- ^ use colors?
56+ | CustomOutputFormat OutputFormatter
57+ | CustomOutputFormatWithDetails OutputFormatterWithDetails
58+ | CustomOutputFormatWithDetailsAndHeaders OutputFormatterWithDetailsAndHeaders
59+
60+type OutputFormatter = ZonedDate -> Request -> Status -> Maybe Integer -> LogStr
61+
62+type OutputFormatterWithDetails
63+ = ZonedDate
64+ -> Request
65+ -> Status
66+ -> Maybe Integer
67+ -> NominalDiffTime
68+ -> [S8.ByteString]
69+ -> B.Builder
70+ -> LogStr
71+
72+-- | Same as @OutputFormatterWithDetails@ but with response headers included
73+--
74+-- This is useful if you wish to include arbitrary application data in your
75+-- logs, e.g., an authenticated user ID, which you would set in a response
76+-- header in your application and retrieve in the log formatter.
77+--
78+-- @since 3.0.27
79+type OutputFormatterWithDetailsAndHeaders
80+ = ZonedDate -- ^ When the log message was generated
81+ -> Request -- ^ The WAI request
82+ -> Status -- ^ HTTP status code
83+ -> Maybe Integer -- ^ Response size
84+ -> NominalDiffTime -- ^ Duration of the request
85+ -> [S8.ByteString] -- ^ The request body
86+ -> B.Builder -- ^ Raw response
87+ -> [Header] -- ^ The response headers
88+ -> LogStr
89+
90+data Destination = Handle Handle
91+ | Logger LoggerSet
92+ | Callback Callback
93+
94+type Callback = LogStr -> IO ()
95+
96+-- | @RequestLoggerSettings@ is an instance of Default. See <https://hackage.haskell.org/package/data-default Data.Default> for more information.
97+--
98+-- @outputFormat@, @autoFlush@, and @destination@ are record fields
99+-- for the record type @RequestLoggerSettings@, so they can be used to
100+-- modify settings values using record syntax.
101+data RequestLoggerSettings = RequestLoggerSettings
102+ {
103+ -- | Default value: @Detailed@ @True@.
104+ outputFormat :: OutputFormat
105+ -- | Only applies when using the @Handle@ constructor for @destination@.
106+ --
107+ -- Default value: @True@.
108+ , autoFlush :: Bool
109+ -- | Default: @Handle@ @stdout@.
110+ , destination :: Destination
111+ }
112+
113+instance Default RequestLoggerSettings where
114+ def = RequestLoggerSettings
115+ { outputFormat = Detailed True
116+ , autoFlush = True
117+ , destination = Handle stdout
118+ }
119+
120+mkRequestLogger :: RequestLoggerSettings -> IO Middleware
121+mkRequestLogger RequestLoggerSettings{..} = do
122+ let (callback, flusher) =
123+ case destination of
124+ Handle h -> (BS.hPutStr h . logToByteString, when autoFlush (hFlush h))
125+ Logger l -> (pushLogStr l, when autoFlush (flushLogStr l))
126+ Callback c -> (c, return ())
127+ callbackAndFlush str = callback str >> flusher
128+ case outputFormat of
129+ Apache ipsrc -> do
130+ getdate <- getDateGetter flusher
131+ apache <- initLogger ipsrc (LogCallback callback flusher) getdate
132+ return $ apacheMiddleware apache
133+ Detailed useColors -> detailedMiddleware callbackAndFlush useColors
134+ CustomOutputFormat formatter -> do
135+ getDate <- getDateGetter flusher
136+ return $ customMiddleware callbackAndFlush getDate formatter
137+ CustomOutputFormatWithDetails formatter -> do
138+ getdate <- getDateGetter flusher
139+ return $ customMiddlewareWithDetails callbackAndFlush getdate formatter
140+ CustomOutputFormatWithDetailsAndHeaders formatter -> do
141+ getdate <- getDateGetter flusher
142+ return $ customMiddlewareWithDetailsAndHeaders callbackAndFlush getdate formatter
143+
144+apacheMiddleware :: ApacheLoggerActions -> Middleware
145+apacheMiddleware ala app req sendResponse = app req $ \res -> do
146+ let msize = contentLength (responseHeaders res)
147+ apacheLogger ala req (responseStatus res) msize
148+ sendResponse res
149+
150+customMiddleware :: Callback -> IO ZonedDate -> OutputFormatter -> Middleware
151+customMiddleware cb getdate formatter app req sendResponse = app req $ \res -> do
152+ date <- liftIO getdate
153+ let msize = contentLength (responseHeaders res)
154+ liftIO $ cb $ formatter date req (responseStatus res) msize
155+ sendResponse res
156+
157+customMiddlewareWithDetails :: Callback -> IO ZonedDate -> OutputFormatterWithDetails -> Middleware
158+customMiddlewareWithDetails cb getdate formatter app req sendResponse = do
159+ (req', reqBody) <- getRequestBody req
160+ t0 <- getCurrentTime
161+ app req' $ \res -> do
162+ t1 <- getCurrentTime
163+ date <- liftIO getdate
164+ let msize = contentLength (responseHeaders res)
165+ builderIO <- newIORef $ B.byteString ""
166+ res' <- recordChunks builderIO res
167+ rspRcv <- sendResponse res'
168+ _ <- liftIO . cb .
169+ formatter date req' (responseStatus res') msize (t1 `diffUTCTime` t0) reqBody =<<
170+ readIORef builderIO
171+ return rspRcv
172+
173+customMiddlewareWithDetailsAndHeaders :: Callback -> IO ZonedDate -> OutputFormatterWithDetailsAndHeaders -> Middleware
174+customMiddlewareWithDetailsAndHeaders cb getdate formatter app req sendResponse = do
175+ (req', reqBody) <- getRequestBody req
176+ t0 <- getCurrentTime
177+ app req' $ \res -> do
178+ t1 <- getCurrentTime
179+ date <- liftIO getdate
180+ let msize = contentLength (responseHeaders res)
181+ builderIO <- newIORef $ B.byteString ""
182+ res' <- recordChunks builderIO res
183+ rspRcv <- sendResponse res'
184+ _ <- do
185+ rawResponse <- readIORef builderIO
186+ let status = responseStatus res'
187+ duration = t1 `diffUTCTime` t0
188+ resHeaders = responseHeaders res'
189+ liftIO . cb $ formatter date req' status msize duration reqBody rawResponse resHeaders
190+ return rspRcv
191+-- | Production request logger middleware.
192+--
193+-- This uses the 'Apache' logging format, and takes IP addresses for clients from
194+-- the socket (see 'IPAddrSource' for more information). It logs to 'stdout'.
195+{-# NOINLINE logStdout #-}
196+logStdout :: Middleware
197+logStdout = unsafePerformIO $ mkRequestLogger def { outputFormat = Apache FromSocket }
198+
199+-- | Development request logger middleware.
200+--
201+-- This uses the 'Detailed' 'True' logging format and logs to 'stdout'.
202+{-# NOINLINE logStdoutDev #-}
203+logStdoutDev :: Middleware
204+logStdoutDev = unsafePerformIO $ mkRequestLogger def
205+
206+-- | Prints a message using the given callback function for each request.
207+-- This is not for serious production use- it is inefficient.
208+-- It immediately consumes a POST body and fills it back in and is otherwise inefficient
209+--
210+-- Note that it logs the request immediately when it is received.
211+-- This meanst that you can accurately see the interleaving of requests.
212+-- And if the app crashes you have still logged the request.
213+-- However, if you are simulating 10 simultaneous users you may find this confusing.
214+--
215+-- This is lower-level - use 'logStdoutDev' unless you need greater control.
216+--
217+-- Example ouput:
218+--
219+-- > GET search
220+-- > Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
221+-- > Status: 200 OK 0.010555s
222+-- >
223+-- > GET static/css/normalize.css
224+-- > Params: [("LXwioiBG","")]
225+-- > Accept: text/css,*/*;q=0.1
226+-- > Status: 304 Not Modified 0.010555s
227+
228+detailedMiddleware :: Callback -> Bool -> IO Middleware
229+detailedMiddleware cb useColors =
230+ let (ansiColor, ansiMethod, ansiStatusCode) =
231+ if useColors
232+ then (ansiColor', ansiMethod', ansiStatusCode')
233+ else (\_ t -> [t], (:[]), \_ t -> [t])
234+
235+ in return $ detailedMiddleware' cb ansiColor ansiMethod ansiStatusCode
236+
237+ansiColor' :: Color -> BS.ByteString -> [BS.ByteString]
238+ansiColor' color bs =
239+ [ pack $ setSGRCode [SetColor Foreground Dull color]
240+ , bs
241+ , pack $ setSGRCode [Reset]
242+ ]
243+
244+-- | Tags http method with a unique color.
245+ansiMethod' :: BS.ByteString -> [BS.ByteString]
246+ansiMethod' m = case m of
247+ "GET" -> ansiColor' Cyan m
248+ "HEAD" -> ansiColor' Cyan m
249+ "PUT" -> ansiColor' Green m
250+ "POST" -> ansiColor' Yellow m
251+ "DELETE" -> ansiColor' Red m
252+ _ -> ansiColor' Magenta m
253+
254+ansiStatusCode' :: BS.ByteString -> BS.ByteString -> [BS.ByteString]
255+ansiStatusCode' c t = case S8.take 1 c of
256+ "2" -> ansiColor' Green t
257+ "3" -> ansiColor' Yellow t
258+ "4" -> ansiColor' Red t
259+ "5" -> ansiColor' Magenta t
260+ _ -> ansiColor' Blue t
261+
262+recordChunks :: IORef B.Builder -> Response -> IO Response
263+recordChunks i (ResponseStream s h sb) =
264+ return . ResponseStream s h $ (\send flush -> sb (\b -> modifyIORef i (<> b) >> send b) flush)
265+recordChunks i (ResponseBuilder s h b) =
266+ modifyIORef i (<> b) >> (return $ ResponseBuilder s h b)
267+recordChunks _ r =
268+ return r
269+
270+getRequestBody :: Request -> IO (Request, [S8.ByteString])
271+getRequestBody req = do
272+ let loop front = do
273+ bs <- requestBody req
274+ if S8.null bs
275+ then return $ front []
276+ else loop $ front . (bs:)
277+ body <- loop id
278+ -- logging the body here consumes it, so fill it back up
279+ -- obviously not efficient, but this is the development logger
280+ --
281+ -- Note: previously, we simply used CL.sourceList. However,
282+ -- that meant that you could read the request body in twice.
283+ -- While that in itself is not a problem, the issue is that,
284+ -- in production, you wouldn't be able to do this, and
285+ -- therefore some bugs wouldn't show up during testing. This
286+ -- implementation ensures that each chunk is only returned
287+ -- once.
288+ ichunks <- newIORef body
289+ let rbody = atomicModifyIORef ichunks $ \chunks ->
290+ case chunks of
291+ [] -> ([], S8.empty)
292+ x:y -> (y, x)
293+ let req' = req { requestBody = rbody }
294+ return (req', body)
295+
296+detailedMiddleware' :: Callback
297+ -> (Color -> BS.ByteString -> [BS.ByteString])
298+ -> (BS.ByteString -> [BS.ByteString])
299+ -> (BS.ByteString -> BS.ByteString -> [BS.ByteString])
300+ -> Middleware
301+detailedMiddleware' cb ansiColor ansiMethod ansiStatusCode app req sendResponse = do
302+ (req', body) <-
303+ -- second tuple item should not be necessary, but a test runner might mess it up
304+ case (requestBodyLength req, contentLength (requestHeaders req)) of
305+ -- log the request body if it is small
306+ (KnownLength len, _) | len <= 2048 -> getRequestBody req
307+ (_, Just len) | len <= 2048 -> getRequestBody req
308+ _ -> return (req, [])
309+
310+ let reqbodylog _ = if null body then [""] else ansiColor White " Request Body: " <> body <> ["\n"]
311+ reqbody = concatMap (either (const [""]) reqbodylog . decodeUtf8') body
312+ postParams <- if requestMethod req `elem` ["GET", "HEAD"]
313+ then return []
314+ else do postParams <- liftIO $ allPostParams body
315+ return $ collectPostParams postParams
316+
317+ let getParams = map emptyGetParam $ queryString req
318+ accept = fromMaybe "" $ lookup H.hAccept $ requestHeaders req
319+ params = let par | not $ null postParams = [pack (show postParams)]
320+ | not $ null getParams = [pack (show getParams)]
321+ | otherwise = []
322+ in if null par then [""] else ansiColor White " Params: " <> par <> ["\n"]
323+
324+ t0 <- getCurrentTime
325+ app req' $ \rsp -> do
326+ let isRaw =
327+ case rsp of
328+ ResponseRaw{} -> True
329+ _ -> False
330+ stCode = statusBS rsp
331+ stMsg = msgBS rsp
332+ t1 <- getCurrentTime
333+
334+ -- log the status of the response
335+ cb $ mconcat $ map toLogStr $
336+ ansiMethod (requestMethod req) ++ [" ", rawPathInfo req, "\n"] ++
337+ params ++ reqbody ++
338+ ansiColor White " Accept: " ++ [accept, "\n"] ++
339+ if isRaw then [] else
340+ ansiColor White " Status: " ++
341+ ansiStatusCode stCode (stCode <> " " <> stMsg) ++
342+ [" ", pack $ show $ diffUTCTime t1 t0, "\n"]
343+
344+ sendResponse rsp
345+ where
346+ allPostParams body =
347+ case getRequestBodyType req of
348+ Nothing -> return ([], [])
349+ Just rbt -> do
350+ ichunks <- newIORef body
351+ let rbody = atomicModifyIORef ichunks $ \chunks ->
352+ case chunks of
353+ [] -> ([], S8.empty)
354+ x:y -> (y, x)
355+ sinkRequestBody lbsBackEnd rbt rbody
356+
357+ emptyGetParam :: (BS.ByteString, Maybe BS.ByteString) -> (BS.ByteString, BS.ByteString)
358+ emptyGetParam (k, Just v) = (k,v)
359+ emptyGetParam (k, Nothing) = (k,"")
360+
361+ collectPostParams :: ([Param], [File LBS.ByteString]) -> [Param]
362+ collectPostParams (postParams, files) = postParams ++
363+ map (\(k,v) -> (k, "FILE: " <> fileName v)) files
364+
365+
366+
367+statusBS :: Response -> BS.ByteString
368+statusBS = pack . show . statusCode . responseStatus
369+
370+msgBS :: Response -> BS.ByteString
371+msgBS = statusMessage . responseStatus
wai-extra/Network/Wai/Middleware/RequestLogger/Internal.hsnew file+33
@@ -0,0 +1,33 @@
1+{-# LANGUAGE CPP #-}
2+-- | A module for containing some CPPed code, due to:
3+--
4+-- https://github.com/yesodweb/wai/issues/192
5+module Network.Wai.Middleware.RequestLogger.Internal
6+ ( module Network.Wai.Middleware.RequestLogger.Internal
7+ ) where
8+
9+import Data.ByteString (ByteString)
10+import Network.Wai.Logger (clockDateCacher)
11+#if !MIN_VERSION_wai_logger(2, 2, 0)
12+import Control.Concurrent (forkIO, threadDelay)
13+import Control.Monad (forever)
14+#endif
15+import System.Log.FastLogger (LogStr, fromLogStr)
16+
17+logToByteString :: LogStr -> ByteString
18+logToByteString = fromLogStr
19+
20+getDateGetter :: IO () -- ^ flusher
21+ -> IO (IO ByteString)
22+#if !MIN_VERSION_wai_logger(2, 2, 0)
23+getDateGetter flusher = do
24+ (getter, updater) <- clockDateCacher
25+ _ <- forkIO $ forever $ do
26+ threadDelay 1000000
27+ updater
28+ flusher
29+#else
30+getDateGetter _ = do
31+ (getter, _) <- clockDateCacher
32+#endif
33+ return getter
wai-extra/Network/Wai/Middleware/RequestLogger/JSON.hsnew file+137
@@ -0,0 +1,137 @@
1+{-# LANGUAGE RecordWildCards #-}
2+{-# LANGUAGE CPP #-}
3+
4+module Network.Wai.Middleware.RequestLogger.JSON
5+ ( formatAsJSON
6+ , formatAsJSONWithHeaders
7+ ) where
8+
9+import qualified Data.ByteString.Builder as BB (toLazyByteString)
10+import Data.ByteString.Lazy (toStrict)
11+import Data.Aeson
12+import Data.CaseInsensitive (original)
13+import Data.Monoid ((<>))
14+import qualified Data.ByteString.Char8 as S8
15+import Data.IP
16+import qualified Data.Text as T
17+import Data.Text (Text)
18+import Data.Text.Encoding (decodeUtf8With)
19+import Data.Text.Encoding.Error (lenientDecode)
20+import Data.Time (NominalDiffTime)
21+import Data.Word (Word32)
22+import Network.HTTP.Types as H
23+import Network.Socket (SockAddr (..), PortNumber)
24+import Network.Wai
25+import Network.Wai.Middleware.RequestLogger
26+import System.Log.FastLogger (toLogStr)
27+import Text.Printf (printf)
28+
29+formatAsJSON :: OutputFormatterWithDetails
30+formatAsJSON date req status responseSize duration reqBody response =
31+ toLogStr (encode $
32+ object
33+ [ "request" .= requestToJSON duration req reqBody
34+ , "response" .=
35+ object
36+ [ "status" .= statusCode status
37+ , "size" .= responseSize
38+ , "body" .=
39+ if statusCode status >= 400
40+ then Just . decodeUtf8With lenientDecode . toStrict . BB.toLazyByteString $ response
41+ else Nothing
42+ ]
43+ , "time" .= decodeUtf8With lenientDecode date
44+ ]) <> "\n"
45+
46+-- | Same as @formatAsJSON@ but with response headers included
47+--
48+-- This is useful for passing arbitrary data from your application out to the
49+-- WAI layer for it to be logged, but you may need to be careful to
50+-- subsequently redact any headers which may contain sensitive data.
51+--
52+-- @since 3.0.27
53+formatAsJSONWithHeaders :: OutputFormatterWithDetailsAndHeaders
54+formatAsJSONWithHeaders date req status resSize duration reqBody res resHeaders =
55+ toLogStr (encode $
56+ object
57+ [ "request" .= requestToJSON duration req reqBody
58+ , "response" .= object
59+ [ "status" .= statusCode status
60+ , "size" .= resSize
61+ , "headers" .= responseHeadersToJSON resHeaders
62+ , "body" .=
63+ if statusCode status >= 400
64+ then Just . decodeUtf8With lenientDecode . toStrict . BB.toLazyByteString $ res
65+ else Nothing
66+ ]
67+ , "time" .= decodeUtf8With lenientDecode date
68+ ]) <> "\n"
69+
70+word32ToHostAddress :: Word32 -> Text
71+word32ToHostAddress = T.intercalate "." . map (T.pack . show) . fromIPv4 . fromHostAddress
72+
73+readAsDouble :: String -> Double
74+readAsDouble = read
75+
76+requestToJSON :: NominalDiffTime -> Request -> [S8.ByteString] -> Value
77+requestToJSON duration req reqBody =
78+ object
79+ [ "method" .= decodeUtf8With lenientDecode (requestMethod req)
80+ , "path" .= decodeUtf8With lenientDecode (rawPathInfo req)
81+ , "queryString" .= map queryItemToJSON (queryString req)
82+ , "durationMs" .= (readAsDouble . printf "%.2f" . rationalToDouble $ toRational duration * 1000)
83+ , "size" .= requestBodyLengthToJSON (requestBodyLength req)
84+ , "body" .= decodeUtf8With lenientDecode (S8.concat reqBody)
85+ , "remoteHost" .= sockToJSON (remoteHost req)
86+ , "httpVersion" .= httpVersionToJSON (httpVersion req)
87+ , "headers" .= requestHeadersToJSON (requestHeaders req)
88+ ]
89+ where
90+ rationalToDouble :: Rational -> Double
91+ rationalToDouble = fromRational
92+
93+sockToJSON :: SockAddr -> Value
94+sockToJSON (SockAddrInet pn ha) =
95+ object
96+ [ "port" .= portToJSON pn
97+ , "hostAddress" .= word32ToHostAddress ha
98+ ]
99+sockToJSON (SockAddrInet6 pn _ ha _) =
100+ object
101+ [ "port" .= portToJSON pn
102+ , "hostAddress" .= ha
103+ ]
104+sockToJSON (SockAddrUnix sock) =
105+ object [ "unix" .= sock ]
106+#if !MIN_VERSION_network(3,0,0)
107+sockToJSON (SockAddrCan i) =
108+ object [ "can" .= i ]
109+#endif
110+
111+queryItemToJSON :: QueryItem -> Value
112+queryItemToJSON (name, mValue) = toJSON (decodeUtf8With lenientDecode name, fmap (decodeUtf8With lenientDecode) mValue)
113+
114+requestHeadersToJSON :: RequestHeaders -> Value
115+requestHeadersToJSON = toJSON . map hToJ where
116+ -- Redact cookies
117+ hToJ ("Cookie", _) = toJSON ("Cookie" :: Text, "-RDCT-" :: Text)
118+ hToJ hd = headerToJSON hd
119+
120+responseHeadersToJSON :: [Header] -> Value
121+responseHeadersToJSON = toJSON . map hToJ where
122+ -- Redact cookies
123+ hToJ ("Set-Cookie", _) = toJSON ("Set-Cookie" :: Text, "-RDCT-" :: Text)
124+ hToJ hd = headerToJSON hd
125+
126+headerToJSON :: Header -> Value
127+headerToJSON (headerName, header) = toJSON (decodeUtf8With lenientDecode . original $ headerName, decodeUtf8With lenientDecode header)
128+
129+portToJSON :: PortNumber -> Value
130+portToJSON = toJSON . toInteger
131+
132+httpVersionToJSON :: HttpVersion -> Value
133+httpVersionToJSON (HttpVersion major minor) = String $ T.pack (show major) <> "." <> T.pack (show minor)
134+
135+requestBodyLengthToJSON :: RequestBodyLength -> Value
136+requestBodyLengthToJSON ChunkedBody = String "Unknown"
137+requestBodyLengthToJSON (KnownLength l) = toJSON l
wai-extra/Network/Wai/Middleware/Rewrite.hsnew file+339
@@ -0,0 +1,339 @@
1+{-# LANGUAGE TupleSections #-}
2+{-# LANGUAGE CPP #-}
3+
4+module Network.Wai.Middleware.Rewrite
5+ ( -- * How to use this module
6+ -- $howto
7+
8+ -- ** A note on semantics
9+
10+ -- $semantics
11+
12+ -- ** Paths and Queries
13+
14+ -- $pathsandqueries
15+ PathsAndQueries
16+
17+ -- ** An example rewriting paths with queries
18+
19+ -- $takeover
20+
21+ -- ** Upgrading from wai-extra ≤ 3.0.16.1
22+
23+ -- $upgrading
24+
25+ -- * 'Middleware'
26+
27+ -- ** Recommended functions
28+ , rewriteWithQueries
29+ , rewritePureWithQueries
30+ , rewriteRoot
31+
32+ -- ** Deprecated
33+ , rewrite
34+ , rewritePure
35+
36+ -- * Operating on 'Request's
37+
38+ , rewriteRequest
39+ , rewriteRequestPure
40+ ) where
41+
42+import Network.Wai
43+import Control.Monad.IO.Class (liftIO)
44+import Data.Text (Text)
45+import Data.Functor.Identity (Identity(..))
46+import qualified Data.Text.Encoding as TE
47+import qualified Data.Text as T
48+import Network.HTTP.Types as H
49+
50+-- GHC ≤ 7.10 does not export Applicative functions from the prelude.
51+#if __GLASGOW_HASKELL__ <= 710
52+import Control.Applicative
53+#endif
54+
55+-- $howto
56+-- This module provides 'Middleware' to rewrite URL paths. It also provides
57+-- functions that will convert a 'Request' to a modified 'Request'.
58+-- Both operations require a function that takes URL parameters and
59+-- headers, and returns new URL parameters. Parameters are pieces of URL
60+-- paths and query parameters.
61+--
62+-- If you are a new user of the library, use 'rewriteWithQueries' or
63+-- 'rewritePureWithQueries' for middleware. For modifying 'Request's
64+-- directly, use 'rewriteRequest' or 'rewriteRequestPure'.
65+
66+-- $semantics
67+--
68+-- Versions of this library in wai-extra ≤ 3.0.16.1 exported only
69+-- 'rewrite' and 'rewritePure' and both modified 'rawPathInfo' of the
70+-- underlying requests. Such modification has been proscribed. The
71+-- semantics of these functions have not changed; instead the recommended
72+-- approach is to use 'rewriteWithQueries' and 'rewritePureWithQueries'.
73+-- The new functions are slightly different, as described in the section
74+-- on upgrading; code for previous library versions can be upgraded with
75+-- a single change, and as the type of the new function is different the
76+-- compiler will indicate where this change must be made.
77+--
78+-- The 'rewriteRequest' and 'rewriteRequestPure' functions use the new
79+-- semantics, too.
80+
81+-- $pathsandqueries
82+--
83+-- This library defines the type synonym `PathsAndQueries` to make code
84+-- handling paths and queries easier to read.
85+--
86+-- /e.g./ /\/foo\/bar/ would look like
87+--
88+-- > ["foo", "bar"] :: Text
89+--
90+-- /?bar=baz/ would look like
91+--
92+-- > [("bar", Just "baz")] :: QueryText
93+--
94+-- Together,
95+--
96+-- /\/foo?bar=baz/ would look like
97+--
98+-- > (["foo"],[("bar", Just "baz")]) :: PathsAndQueries
99+
100+-- $takeover
101+-- Let’s say we want to replace a website written in PHP with one written
102+-- using WAI. We’ll use the
103+-- <https://hackage.haskell.org/package/http-reverse-proxy http-reverse-proxy>
104+-- package to serve the old
105+-- site from the new site, but there’s a problem. The old site uses pages like
106+--
107+-- @
108+-- index.php?page=/page/
109+-- @
110+--
111+-- whereas the new site would look like
112+--
113+-- @
114+-- index\//page/
115+-- @
116+--
117+-- In doing this, we want to separate the migration code from our new
118+-- website. So we’d like to handle links internally using the path
119+-- formulation, but externally have the old links still work.
120+--
121+-- Therefore, we will use middleware ('rewritePureWithQueries') from this
122+-- module to rewrite incoming requests from the query formulation to the
123+-- paths formulation.
124+--
125+-- > {-# LANGUAGE ViewPatterns #-}
126+-- >
127+-- > rewritePathFromPhp :: Middleware
128+-- > rewritePathFromPhp = rewritePureWithQueries pathFromPhp
129+-- >
130+-- > pathFromPhp :: PathsAndQueries -> H.RequestHeaders -> PathsAndQueries
131+-- > pathFromPhp (pieces, queries) _ = piecesConvert pieces queries
132+-- > where
133+-- > piecesConvert :: [Text] -> H.Query -> PathsAndQueries
134+-- > piecesConvert ["index.php"] qs@(join . lookup "page" -> Just page) =
135+-- > ( ["index", TE.decodeUtf8With TE.lenientDecode page]
136+-- > , delete ("page", pure page) qs
137+-- > )
138+-- > piecesConvert ps qs = (ps, qs)
139+--
140+-- On the other side, we will use 'rewriteRequestPure' to rewrite outgoing
141+-- requests to the original website from the reverse proxy code (using the
142+-- 'Network.HTTP.ReverseProxy.WPRModifiedRequest' or
143+-- 'Network.HTTP.ReverseProxy.WPRModifiedRequestSecure' constructors. Note,
144+-- these links will only work if the haddock documentation for
145+-- <https://hackage.haskell.org/package/http-reverse-proxy http-reverse-proxy>
146+-- is installed).
147+--
148+-- > rewritePhpFromPath :: Request -> Request
149+-- > rewritePhpFromPath = rewriteRequestPure phpFromPath
150+-- >
151+-- > phpFromPath :: PathsAndQueries -> H.RequestHeaders -> PathsAndQueries
152+-- > phpFromPath (pieces, queries) _ = piecesConvert pieces queries
153+-- > where
154+-- > piecesConvert :: [Text] -> H.Query -> PathsAndQueries
155+-- > piecesConvert ["index", page] qs = ( ["index.php"], ("page", pure . TE.encodeUtf8 $ page) : qs )
156+-- > piecesConvert ps qs = (ps, qs)
157+--
158+-- For the whole example, see
159+-- <https://gist.github.com/dbaynard/c844d0df124f68ec8b6da152c581ce6d>.
160+
161+-- $upgrading
162+-- It is quite simple to upgrade from 'rewrite' and 'rewritePure', to
163+-- 'rewriteWithQueries' and 'rewritePureWithQueries'.
164+-- Insert 'Data.Bifunctor.first', which specialises to
165+--
166+-- @
167+-- 'Data.Bifunctor.first' :: (['Text'] -> ['Text']) -> 'PathsAndQueries' -> 'PathsAndQueries'
168+-- @
169+--
170+-- as the following example demonstrates.
171+--
172+-- Old versions of the library could only handle path pieces, not queries.
173+-- This could have been supplied to 'rewritePure'.
174+--
175+-- @
176+-- staticConvert' :: [Text] -> H.RequestHeaders -> [Text]
177+-- staticConvert' pieces _ = piecesConvert pieces
178+-- where
179+-- piecesConvert [] = ["static", "html", "pages.html"]
180+-- piecesConvert route@("pages":_) = "static":"html":route
181+-- @
182+--
183+-- Instead, use this function, supplied to 'rewritePureWithQueries'.
184+--
185+-- @
186+-- staticConvert :: 'PathsAndQueries' -> H.RequestHeaders -> 'PathsAndQueries'
187+-- staticConvert pathsAndQueries _ = 'Data.Bifunctor.first' piecesConvert pathsAndQueries
188+-- where
189+-- piecesConvert [] = ["static", "html", "pages.html"]
190+-- piecesConvert route@("pages":_) = "static":"html":route
191+-- @
192+--
193+-- The former formulation is deprecated for two reasons:
194+--
195+-- 1. The original formulation of 'rewrite' modified 'rawPathInfo', which
196+-- is deprecated behaviour.
197+--
198+-- 2. The original formulation did not allow query parameters to
199+-- influence the path.
200+--
201+-- Concerning the first point, take care with semantics of your program when
202+-- upgrading as the upgraded functions no longer modify 'rawPathInfo'.
203+
204+--------------------------------------------------
205+-- * Types
206+--------------------------------------------------
207+
208+-- | A tuple of the path sections as ['Text'] and query parameters as
209+-- 'H.Query'. This makes writing type signatures for the conversion
210+-- function far more pleasant.
211+--
212+-- Note that this uses 'H.Query' not 'H.QueryText' to more accurately
213+-- reflect the paramaters that can be supplied in URLs. It may be safe to
214+-- treat parameters as text; use the 'H.queryToQueryText' and
215+-- 'H.queryTextToQuery' functions to interconvert.
216+type PathsAndQueries = ([Text], H.Query)
217+
218+--------------------------------------------------
219+-- * Rewriting 'Middleware'
220+--------------------------------------------------
221+
222+-- | Rewrite based on your own conversion function for paths only, to be
223+-- supplied by users of this library (with the conversion operating in 'IO').
224+--
225+-- For new code, use 'rewriteWithQueries' instead.
226+rewrite :: ([Text] -> H.RequestHeaders -> IO [Text]) -> Middleware
227+rewrite convert app req sendResponse = do
228+ let convertIO = pathsOnly . curry $ liftIO . uncurry convert
229+ newReq <- rewriteRequestRawM convertIO req
230+ app newReq sendResponse
231+{-# WARNING rewrite [
232+ "This modifies the 'rawPathInfo' field of a 'Request'."
233+ , " This is not recommended behaviour; it is however how"
234+ , " this function has worked in the past."
235+ , " Use 'rewriteWithQueries' instead"] #-}
236+
237+-- | Rewrite based on pure conversion function for paths only, to be
238+-- supplied by users of this library.
239+--
240+-- For new code, use 'rewritePureWithQueries' instead.
241+rewritePure :: ([Text] -> H.RequestHeaders -> [Text]) -> Middleware
242+rewritePure convert app req =
243+ let convertPure = pathsOnly . curry $ Identity . uncurry convert
244+ newReq = runIdentity $ rewriteRequestRawM convertPure req
245+ in app newReq
246+{-# WARNING rewritePure [
247+ "This modifies the 'rawPathInfo' field of a 'Request'."
248+ , " This is not recommended behaviour; it is however how"
249+ , " this function has worked in the past."
250+ , " Use 'rewritePureWithQueries' instead"] #-}
251+
252+-- | Rewrite based on your own conversion function for paths and queries.
253+-- This function is to be supplied by users of this library, and operates
254+-- in 'IO'.
255+rewriteWithQueries :: (PathsAndQueries -> H.RequestHeaders -> IO PathsAndQueries)
256+ -> Middleware
257+rewriteWithQueries convert app req sendResponse = do
258+ newReq <- rewriteRequestM convert req
259+ app newReq sendResponse
260+
261+-- | Rewrite based on pure conversion function for paths and queries. This
262+-- function is to be supplied by users of this library.
263+rewritePureWithQueries :: (PathsAndQueries -> H.RequestHeaders -> PathsAndQueries)
264+ -> Middleware
265+rewritePureWithQueries convert app req = app $ rewriteRequestPure convert req
266+
267+-- | Rewrite root requests (/) to a specified path
268+--
269+-- Note that /index.html/ in example below should already be a valid route.
270+--
271+-- @
272+-- rewriteRoot "index.html" :: Middleware
273+-- @
274+--
275+-- @since 3.0.23.0
276+rewriteRoot :: Text -> Middleware
277+rewriteRoot root = rewritePureWithQueries onlyRoot
278+ where
279+ onlyRoot ([], q) _ = ([root], q)
280+ onlyRoot paths _ = paths
281+
282+--------------------------------------------------
283+-- * Modifying 'Request's directly
284+--------------------------------------------------
285+
286+-- | Modify a 'Request' using the supplied function in 'IO'. This is suitable for
287+-- the reverse proxy example.
288+rewriteRequest :: (PathsAndQueries -> H.RequestHeaders -> IO PathsAndQueries)
289+ -> Request -> IO Request
290+rewriteRequest convert req =
291+ let convertIO = curry $ liftIO . uncurry convert
292+ in rewriteRequestRawM convertIO req
293+
294+-- | Modify a 'Request' using the pure supplied function. This is suitable for
295+-- the reverse proxy example.
296+rewriteRequestPure :: (PathsAndQueries -> H.RequestHeaders -> PathsAndQueries)
297+ -> Request -> Request
298+rewriteRequestPure convert req =
299+ let convertPure = curry $ Identity . uncurry convert
300+ in runIdentity $ rewriteRequestRawM convertPure req
301+
302+--------------------------------------------------
303+-- * Helper functions
304+--------------------------------------------------
305+
306+-- | This helper function factors out the common behaviour of rewriting requests.
307+rewriteRequestM :: (Applicative m, Monad m)
308+ => (PathsAndQueries -> H.RequestHeaders -> m PathsAndQueries)
309+ -> Request -> m Request
310+rewriteRequestM convert req = do
311+ (pInfo, qByteStrings) <- curry convert (pathInfo req) (queryString req) (requestHeaders req)
312+ pure req {pathInfo = pInfo, queryString = qByteStrings}
313+
314+-- | This helper function preserves the semantics of wai-extra ≤ 3.0, in
315+-- which the rewrite functions modify the 'rawPathInfo' parameter. Note
316+-- that this has not been extended to modify the 'rawQueryInfo' as
317+-- modifying either of these values has been deprecated.
318+rewriteRequestRawM :: (Applicative m, Monad m)
319+ => (PathsAndQueries -> H.RequestHeaders -> m PathsAndQueries)
320+ -> Request -> m Request
321+rewriteRequestRawM convert req = do
322+ newReq <- rewriteRequestM convert req
323+ let rawPInfo = TE.encodeUtf8 . T.intercalate "/" . pathInfo $ newReq
324+ pure newReq { rawPathInfo = rawPInfo }
325+{-# WARNING rewriteRequestRawM [
326+ "This modifies the 'rawPathInfo' field of a 'Request'."
327+ , " This is not recommended behaviour; it is however how"
328+ , " this function has worked in the past."
329+ , " Use 'rewriteRequestM' instead"] #-}
330+
331+-- | Produce a function that works on 'PathsAndQueries' from one working
332+-- only on paths. This is not exported, as it is only needed to handle
333+-- code written for versions ≤ 3.0 of the library; see the
334+-- example above using 'Data.Bifunctor.first' to do something similar.
335+pathsOnly :: (Applicative m, Monad m)
336+ => ([Text] -> H.RequestHeaders -> m [Text])
337+ -> PathsAndQueries -> H.RequestHeaders -> m PathsAndQueries
338+pathsOnly convert psAndQs headers = (,[]) <$> convert (fst psAndQs) headers
339+{-# INLINE pathsOnly #-}
wai-extra/Network/Wai/Middleware/Routed.hsnew file+39
@@ -0,0 +1,39 @@
1+-- |
2+--
3+-- Since 3.0.9
4+module Network.Wai.Middleware.Routed
5+ ( routedMiddleware
6+ , hostedMiddleware
7+ ) where
8+
9+import Network.Wai
10+import Data.ByteString (ByteString)
11+import Data.Text (Text)
12+
13+-- | Apply a middleware based on a test of pathInfo
14+--
15+-- example:
16+--
17+-- > let corsify = routedMiddleWare ("static" `elem`) addCorsHeaders
18+--
19+-- Since 3.0.9
20+routedMiddleware :: ([Text] -> Bool) -- ^ Only use middleware if this pathInfo test returns True
21+ -> Middleware -- ^ middleware to apply the path prefix guard to
22+ -> Middleware -- ^ modified middleware
23+routedMiddleware pathCheck middle app req
24+ | pathCheck (pathInfo req) = middle app req
25+ | otherwise = app req
26+
27+-- | Only apply the middleware to certain hosts
28+--
29+-- Since 3.0.9
30+hostedMiddleware :: ByteString -- ^ Domain the middleware applies to
31+ -> Middleware -- ^ middleware to apply the path prefix guard to
32+ -> Middleware -- ^ modified middleware
33+hostedMiddleware domain middle app req
34+ | hasDomain domain req = middle app req
35+ | otherwise = app req
36+
37+hasDomain :: ByteString -> Request -> Bool
38+hasDomain domain req = maybe False (== domain) mHost
39+ where mHost = requestHeaderHost req
wai-extra/Network/Wai/Middleware/StreamFile.hsnew file+43
@@ -0,0 +1,43 @@
1+-- |
2+--
3+-- Since 3.0.4
4+module Network.Wai.Middleware.StreamFile
5+ (streamFile) where
6+
7+import Network.Wai (responseStream)
8+import Network.Wai.Internal
9+import Network.Wai (Middleware, responseToStream)
10+import qualified Data.ByteString.Char8 as S8
11+import System.PosixCompat (getFileStatus, fileSize, FileOffset)
12+import Network.HTTP.Types (hContentLength)
13+
14+-- |Convert ResponseFile type responses into ResponseStream type
15+--
16+-- Checks the response type, and if it's a ResponseFile, converts it
17+-- into a ResponseStream. Other response types are passed through
18+-- unchanged.
19+--
20+-- Converted responses get a Content-Length header.
21+--
22+-- Streaming a file will bypass a sendfile system call, and may be
23+-- useful to work around systems without working sendfile
24+-- implementations.
25+--
26+-- Since 3.0.4
27+streamFile :: Middleware
28+streamFile app env sendResponse = app env $ \res ->
29+ case res of
30+ ResponseFile _ _ fp _ -> withBody sendBody
31+ where
32+ (s, hs, withBody) = responseToStream res
33+ sendBody :: StreamingBody -> IO ResponseReceived
34+ sendBody body = do
35+ len <- getFileSize fp
36+ let hs' = (hContentLength, (S8.pack (show len))) : hs
37+ sendResponse $ responseStream s hs' body
38+ _ -> sendResponse res
39+
40+getFileSize :: FilePath -> IO FileOffset
41+getFileSize path = do
42+ stat <- getFileStatus path
43+ return (fileSize stat)
wai-extra/Network/Wai/Middleware/StripHeaders.hsnew file+45
@@ -0,0 +1,45 @@
1+-- This was written for one specific use case and then generalized.
2+
3+-- The specific use case was a JSON API with a consumer that would choke on the
4+-- "Set-Cookie" response header. The solution was to test for the API's
5+-- `pathInfo` in the Request and if it matched, filter the response headers.
6+
7+-- When using this, care should be taken not to strip out headers that are
8+-- required for correct operation of the client (eg Content-Type).
9+
10+module Network.Wai.Middleware.StripHeaders
11+ ( stripHeader
12+ , stripHeaders
13+ , stripHeaderIf
14+ , stripHeadersIf
15+ ) where
16+
17+import Network.Wai (Middleware, Request, modifyResponse, mapResponseHeaders, ifRequest)
18+import Network.Wai.Internal (Response)
19+import Data.ByteString (ByteString)
20+
21+import qualified Data.CaseInsensitive as CI
22+
23+stripHeader :: ByteString -> (Response -> Response)
24+stripHeader h = mapResponseHeaders (filter (\ hdr -> fst hdr /= CI.mk h))
25+
26+stripHeaders :: [ByteString] -> (Response -> Response)
27+stripHeaders hs =
28+ let hnames = map CI.mk hs
29+ in mapResponseHeaders (filter (\ hdr -> fst hdr `notElem` hnames))
30+
31+-- | If the request satisifes the provided predicate, strip headers matching
32+-- the provided header name.
33+--
34+-- Since 3.0.8
35+stripHeaderIf :: ByteString -> (Request -> Bool) -> Middleware
36+stripHeaderIf h rpred =
37+ ifRequest rpred (modifyResponse $ stripHeader h)
38+
39+-- | If the request satisifes the provided predicate, strip all headers whose
40+-- header name is in the list of provided header names.
41+--
42+-- Since 3.0.8
43+stripHeadersIf :: [ByteString] -> (Request -> Bool) -> Middleware
44+stripHeadersIf hs rpred
45+ = ifRequest rpred (modifyResponse $ stripHeaders hs)
wai-extra/Network/Wai/Middleware/Timeout.hsnew file+33

This diff is too big to show here.

wai-extra/Network/Wai/Middleware/Vhost.hsnew file+37

This diff is too big to show here.

wai-extra/Network/Wai/Parse.hsnew file+715

This diff is too big to show here.

wai-extra/Network/Wai/Request.hsnew file+101

This diff is too big to show here.

wai-extra/Network/Wai/Test.hsnew file+333

This diff is too big to show here.

wai-extra/Network/Wai/Test/Internal.hsnew file+35

This diff is too big to show here.

wai-extra/Network/Wai/UrlMap.hsnew file+104

This diff is too big to show here.

wai-extra/README.mdnew file+16

This diff is too big to show here.

wai-extra/Setup.lhsnew file+7

This diff is too big to show here.

wai-extra/example/Main.hsnew file+62

This diff is too big to show here.

wai-extra/example/index.htmlnew file+20

This diff is too big to show here.

wai-extra/proxy.hsnew file+25

This diff is too big to show here.

wai-extra/test/Network/Wai/Middleware/ApprootSpec.hsnew file+37

This diff is too big to show here.

wai-extra/test/Network/Wai/Middleware/ForceSSLSpec.hsnew file+60

This diff is too big to show here.

wai-extra/test/Network/Wai/Middleware/RoutedSpec.hsnew file+53

This diff is too big to show here.

wai-extra/test/Network/Wai/Middleware/StripHeadersSpec.hsnew file+58

This diff is too big to show here.

wai-extra/test/Network/Wai/Middleware/TimeoutSpec.hsnew file+57

This diff is too big to show here.

wai-extra/test/Network/Wai/ParseSpec.hsnew file+294

This diff is too big to show here.

wai-extra/test/Network/Wai/RequestSpec.hsnew file+93

This diff is too big to show here.

wai-extra/test/Network/Wai/TestSpec.hsnew file+227

This diff is too big to show here.

wai-extra/test/Spec.hsnew file+1

This diff is too big to show here.

wai-extra/test/WaiExtraSpec.hsnew file+451

This diff is too big to show here.

test/json → wai-extra/test/jsonrenamed

The content is the same.

wai-extra/test/requests/dalvik-requestnew file

Binary file not shown.

wai-extra/test/sample.hsnew file+26

This diff is too big to show here.

test/test.html → wai-extra/test/test.htmlrenamed

The content is the same.

wai-extra/wai-extra.cabalnew file+212

This diff is too big to show here.

wai-frontend-monadcgi/.gitignorenew file+2

This diff is too big to show here.

wai-frontend-monadcgi/ChangeLog.mdnew file+7

This diff is too big to show here.

wai-frontend-monadcgi/LICENSEnew file+20

This diff is too big to show here.

wai-frontend-monadcgi/Network/Wai/Frontend/MonadCGI.hsnew file+71

This diff is too big to show here.

wai-frontend-monadcgi/README.mdnew file+5

This diff is too big to show here.

wai-frontend-monadcgi/Setup.lhsnew file+7

This diff is too big to show here.

wai-frontend-monadcgi/samples/.htaccessnew file+5

This diff is too big to show here.

wai-frontend-monadcgi/samples/plain_cgi.hsnew file+5

This diff is too big to show here.

wai-frontend-monadcgi/samples/wai_cgi.hsnew file+8

This diff is too big to show here.

wai-frontend-monadcgi/samples/wai_cgi_generic.hsnew file+19

This diff is too big to show here.

wai-frontend-monadcgi/samples/wai_fastcgi.hsnew file+8

This diff is too big to show here.

wai-frontend-monadcgi/wai-frontend-monadcgi.cabalnew file+29

This diff is too big to show here.

wai-http2-extra/LICENSEnew file+20

This diff is too big to show here.

wai-http2-extra/Network/Wai/Middleware/Push/Referer.hsnew file+236

This diff is too big to show here.

wai-http2-extra/Network/Wai/Middleware/Push/Referer/LimitMultiMap.hsnew file+40

This diff is too big to show here.

wai-http2-extra/test/doctests.hsnew file+4

This diff is too big to show here.

wai-http2-extra/wai-http2-extra.cabalnew file+44

This diff is too big to show here.

wai-websockets/ChangeLog.mdnew file+27

This diff is too big to show here.

wai-websockets/LICENSEnew file+20

This diff is too big to show here.

wai-websockets/Network/Wai/Handler/WebSockets.hsnew file+118

This diff is too big to show here.

wai-websockets/README.mdnew file+3

This diff is too big to show here.

wai-websockets/Setup.lhsnew file+7

This diff is too big to show here.

wai-websockets/server.lhsnew file+168

This diff is too big to show here.

wai-websockets/static/client.htmlnew file+41

This diff is too big to show here.

wai-websockets/static/client.jsnew file+80

This diff is too big to show here.

wai-websockets/static/screen.cssnew file+86

This diff is too big to show here.

wai-websockets/wai-websockets.cabalnew file+57

This diff is too big to show here.

wai/.ghcinew file+2

This diff is too big to show here.

wai/ChangeLog.mdnew file+56

This diff is too big to show here.

wai/LICENSEnew file+20

This diff is too big to show here.

wai/Network/Wai.hsnew file+348

This diff is too big to show here.

wai/Network/Wai/Internal.hsnew file+161

This diff is too big to show here.

wai/README.lhsnew file+1

This diff is too big to show here.

wai/README.mdnew file+116

This diff is too big to show here.

wai/Setup.lhsnew file+7

This diff is too big to show here.

wai/test/Network/WaiSpec.hsnew file+82

This diff is too big to show here.

wai/test/Spec.hsnew file+1

This diff is too big to show here.

wai/wai.cabalnew file+48

This diff is too big to show here.

wai/webkit-sample/index.htmlnew file+17

This diff is too big to show here.

wai/webkit-sample/webkit-sample.hsnew file+30

This diff is too big to show here.

warp-tls/ChangeLog.mdnew file+117

This diff is too big to show here.

warp-tls/LICENSEnew file+20

This diff is too big to show here.

warp-tls/Network/Wai/Handler/WarpTLS.hsnew file+531

This diff is too big to show here.

warp-tls/README.mdnew file+9

This diff is too big to show here.

warp-tls/Setup.lhsnew file+7

This diff is too big to show here.

warp-tls/certificate.pemnew file+15

This diff is too big to show here.

warp-tls/graceful-pong.hsnew file+125

This diff is too big to show here.

warp-tls/key.pemnew file+15

This diff is too big to show here.

warp-tls/pong.hsnew file+80

This diff is too big to show here.

warp-tls/pong.txtnew file+1

This diff is too big to show here.

warp-tls/warp-tls.cabalnew file+42

This diff is too big to show here.

warp/.ghcinew file+1

This diff is too big to show here.

warp/ChangeLog.mdnew file+404

This diff is too big to show here.

warp/LICENSEnew file+20

This diff is too big to show here.

warp/Network/Wai/Handler/Warp.hsnew file+509

This diff is too big to show here.

warp/Network/Wai/Handler/Warp/Buffer.hsnew file+109

This diff is too big to show here.

warp/Network/Wai/Handler/Warp/Conduit.hsnew file+169

This diff is too big to show here.

warp/Network/Wai/Handler/Warp/Counter.hsnew file+30

This diff is too big to show here.

warp/Network/Wai/Handler/Warp/Date.hsnew file+41

This diff is too big to show here.

warp/Network/Wai/Handler/Warp/FdCache.hsnew file+142

This diff is too big to show here.

warp/Network/Wai/Handler/Warp/File.hsnew file+152

This diff is too big to show here.

warp/Network/Wai/Handler/Warp/FileInfoCache.hsnew file+108

This diff is too big to show here.

warp/Network/Wai/Handler/Warp/HTTP2.hsnew file+85

This diff is too big to show here.

warp/Network/Wai/Handler/Warp/HTTP2/File.hsnew file+30

This diff is too big to show here.

warp/Network/Wai/Handler/Warp/HTTP2/PushPromise.hsnew file+36

This diff is too big to show here.

warp/Network/Wai/Handler/Warp/HTTP2/Request.hsnew file+126

This diff is too big to show here.

warp/Network/Wai/Handler/Warp/HTTP2/Response.hsnew file+127

This diff is too big to show here.

warp/Network/Wai/Handler/Warp/HTTP2/Types.hsnew file+81

This diff is too big to show here.

warp/Network/Wai/Handler/Warp/HashMap.hsnew file+42

This diff is too big to show here.

warp/Network/Wai/Handler/Warp/Header.hsnew file+101

This diff is too big to show here.

warp/Network/Wai/Handler/Warp/IO.hsnew file+30

This diff is too big to show here.

warp/Network/Wai/Handler/Warp/Imports.hsnew file+27

This diff is too big to show here.

warp/Network/Wai/Handler/Warp/Internal.hsnew file+86

This diff is too big to show here.

warp/Network/Wai/Handler/Warp/MultiMap.hsnew file+111

This diff is too big to show here.

warp/Network/Wai/Handler/Warp/PackInt.hsnew file+56

This diff is too big to show here.

warp/Network/Wai/Handler/Warp/ReadInt.hsnew file+67

This diff is too big to show here.

warp/Network/Wai/Handler/Warp/Recv.hsnew file+160

This diff is too big to show here.

warp/Network/Wai/Handler/Warp/Request.hsnew file+303

This diff is too big to show here.

warp/Network/Wai/Handler/Warp/RequestHeader.hsnew file+133

This diff is too big to show here.

warp/Network/Wai/Handler/Warp/Response.hsnew file+427

This diff is too big to show here.

warp/Network/Wai/Handler/Warp/ResponseHeader.hsnew file+84

This diff is too big to show here.

warp/Network/Wai/Handler/Warp/Run.hsnew file+599

This diff is too big to show here.

warp/Network/Wai/Handler/Warp/SendFile.hsnew file+149

This diff is too big to show here.

warp/Network/Wai/Handler/Warp/Settings.hsnew file+231

This diff is too big to show here.

warp/Network/Wai/Handler/Warp/Types.hsnew file+188

This diff is too big to show here.

warp/Network/Wai/Handler/Warp/Windows.hsnew file+27

This diff is too big to show here.

warp/Network/Wai/Handler/Warp/WithApplication.hsnew file+104

This diff is too big to show here.

warp/README.mdnew file+3

This diff is too big to show here.

warp/Setup.lhsnew file+7

This diff is too big to show here.

warp/attic/bigtable-single.hsnew file+22

This diff is too big to show here.

warp/attic/bigtable-stream.hsnew file+24

This diff is too big to show here.

warp/attic/file-nolen.hsnew file+6

This diff is too big to show here.

warp/attic/file.hsnew file+6

This diff is too big to show here.

warp/attic/hexnew file+1

This diff is too big to show here.

warp/attic/pong.hsnew file+72

This diff is too big to show here.

warp/attic/pong.txtnew file+1

This diff is too big to show here.

warp/attic/print-post.hsnew file+18

This diff is too big to show here.

warp/attic/readInt.hsnew file+162

This diff is too big to show here.

warp/attic/runtests.hsnew file+43

This diff is too big to show here.

warp/attic/server-no-keepalive.hsnew file+34

This diff is too big to show here.

warp/attic/statuses.hsnew file+18

This diff is too big to show here.

warp/attic/test.txtnew file+1

This diff is too big to show here.

warp/attic/undrained.hsnew file+9

This diff is too big to show here.

warp/bench/Parser.hsnew file+220

This diff is too big to show here.

warp/test/BufferPoolSpec.hsnew file+47

This diff is too big to show here.

warp/test/ConduitSpec.hsnew file+67

This diff is too big to show here.

warp/test/ExceptionSpec.hsnew file+66

This diff is too big to show here.

warp/test/FdCacheSpec.hsnew file+27

This diff is too big to show here.

warp/test/FileSpec.hsnew file+45

This diff is too big to show here.

warp/test/HTTP.hsnew file+41

This diff is too big to show here.

warp/test/ReadIntSpec.hsnew file+26

This diff is too big to show here.

warp/test/RequestSpec.hsnew file+149

This diff is too big to show here.

warp/test/ResponseHeaderSpec.hsnew file+48

This diff is too big to show here.

warp/test/ResponseSpec.hsnew file+107

This diff is too big to show here.

warp/test/RunSpec.hsnew file+463

This diff is too big to show here.

warp/test/SendFileSpec.hsnew file+116

This diff is too big to show here.

warp/test/Spec.hsnew file+1

This diff is too big to show here.

warp/test/WithApplicationSpec.hsnew file+45

This diff is too big to show here.

warp/test/doctests.hsnew file+4

This diff is too big to show here.

warp/test/head-responsenew file+1

This diff is too big to show here.

warp/test/inputFilenew file+100

This diff is too big to show here.

warp/warp.cabalnew file+270

This diff is too big to show here.