├── .clang-format ├── .gitignore ├── .travis.yml ├── .vscode └── settings.json_template ├── CMakeLists.txt ├── CMakeLists.txt.in ├── LICENSE ├── README.md ├── analyser.cpp ├── analyser.h ├── appveyor.yml ├── array.h ├── block.cpp ├── block.h ├── common.h ├── contrib ├── CLI11 │ └── CLI11.hpp └── zlib │ ├── adler32.c │ ├── compress.c │ ├── crc32.c │ ├── crc32.h │ ├── deflate.c │ ├── deflate.h │ ├── gzclose.c │ ├── gzguts.h │ ├── gzlib.c │ ├── gzread.c │ ├── gzwrite.c │ ├── infback.c │ ├── inffast.c │ ├── inffast.h │ ├── inffixed.h │ ├── inflate.c │ ├── inflate.h │ ├── inftrees.c │ ├── inftrees.h │ ├── trees.c │ ├── trees.h │ ├── uncompr.c │ ├── zconf.h │ ├── zlib.h │ ├── zutil.c │ └── zutil.h ├── crc32.h ├── deduper.cpp ├── deduper.h ├── fairytale.cpp ├── filestream.cpp ├── filestream.h ├── hybridstream.cpp ├── hybridstream.h ├── image.h ├── parser.h ├── parsers ├── bitmapparser.h ├── ddsparser.cpp ├── ddsparser.h ├── deflateparser.h ├── jpegparser.h ├── jsonparser.cpp ├── jsonparser.h ├── modparser.cpp ├── modparser.h ├── textparser.cpp └── textparser.h ├── storagemanager.cpp ├── storagemanager.h ├── stream.h ├── structs.h ├── test ├── testMTFList.cpp └── testMain.cpp ├── transform.h ├── transformfactory.h └── transforms ├── mtflist.h ├── zlibtransform.cpp └── zlibtransform.h /.clang-format: -------------------------------------------------------------------------------- 1 | --- 2 | AccessModifierOffset: -2 3 | AlignAfterOpenBracket: AlwaysBreak 4 | AlignConsecutiveAssignments: false 5 | AlignConsecutiveDeclarations: false 6 | AlignEscapedNewlines: DontAlign 7 | AlignOperands: false 8 | AlignTrailingComments: true 9 | AllowAllParametersOfDeclarationOnNextLine: false 10 | AllowShortBlocksOnASingleLine: false 11 | AllowShortCaseLabelsOnASingleLine: true 12 | AllowShortFunctionsOnASingleLine: Empty 13 | AllowShortIfStatementsOnASingleLine: false 14 | AllowShortLoopsOnASingleLine: false 15 | AlwaysBreakAfterDefinitionReturnType: None 16 | AlwaysBreakAfterReturnType: None 17 | AlwaysBreakBeforeMultilineStrings: true 18 | AlwaysBreakTemplateDeclarations: false 19 | BinPackArguments: true 20 | BinPackParameters: false 21 | BraceWrapping: 22 | AfterClass: false 23 | AfterControlStatement: false 24 | AfterEnum: true 25 | AfterExternBlock: false 26 | AfterFunction: false 27 | AfterNamespace: false 28 | AfterObjCDeclaration: false 29 | AfterStruct: false 30 | AfterUnion: true 31 | BeforeCatch: true 32 | BeforeElse: true 33 | IndentBraces: false 34 | SplitEmptyFunction: false 35 | SplitEmptyNamespace: true 36 | SplitEmptyRecord: false 37 | BreakAfterJavaFieldAnnotations: true 38 | BreakBeforeBinaryOperators: None 39 | BreakBeforeBraces: Custom 40 | BreakBeforeInheritanceComma: true 41 | BreakBeforeTernaryOperators: true 42 | BreakConstructorInitializers: AfterColon 43 | BreakConstructorInitializersBeforeComma: true 44 | BreakStringLiterals: false 45 | ColumnLimit: 152 46 | CommentPragmas: '^ IWYU pragma:' 47 | CompactNamespaces: true 48 | ConstructorInitializerAllOnOneLineOrOnePerLine: false 49 | ConstructorInitializerIndentWidth: 0 50 | ContinuationIndentWidth: 0 51 | Cpp11BracedListStyle: false 52 | DerivePointerAlignment: true 53 | DisableFormat: false 54 | ExperimentalAutoDetectBinPacking: false 55 | FixNamespaceComments: false 56 | ForEachMacros: 57 | - foreach 58 | - Q_FOREACH 59 | - BOOST_FOREACH 60 | IncludeCategories: 61 | - Priority: 2 62 | Regex: ^ 63 | - Priority: 1 64 | Regex: ^<.*\.h> 65 | - Priority: 2 66 | Regex: ^<.* 67 | - Priority: 3 68 | Regex: .* 69 | IncludeIsMainRegex: ([-_](test|unittest))?$ 70 | IndentCaseLabels: true 71 | IndentPPDirectives: AfterHash 72 | IndentWidth: 2 73 | IndentWrappedFunctionNames: false 74 | KeepEmptyLinesAtTheStartOfBlocks: true 75 | Language: Cpp 76 | MacroBlockBegin: '' 77 | MacroBlockEnd: '' 78 | MaxEmptyLinesToKeep: 5 79 | NamespaceIndentation: Inner 80 | PenaltyBreakAssignment: 3 81 | PenaltyBreakBeforeFirstCallParameter: 4 82 | PenaltyBreakComment: 358 83 | PenaltyBreakFirstLessLess: 52 84 | PenaltyBreakString: 1492 85 | PenaltyExcessCharacter: 0 86 | PenaltyReturnTypeOnItsOwnLine: 254 87 | PointerAlignment: Right 88 | ReflowComments: false 89 | SortIncludes: true 90 | SortUsingDeclarations: true 91 | SpaceAfterCStyleCast: false 92 | SpaceAfterTemplateKeyword: true 93 | SpaceBeforeAssignmentOperators: true 94 | SpaceBeforeParens: ControlStatements 95 | SpaceInEmptyParentheses: false 96 | SpacesBeforeTrailingComments: 1 97 | SpacesInAngles: false 98 | SpacesInCStyleCastParentheses: false 99 | SpacesInContainerLiterals: true 100 | SpacesInParentheses: false 101 | SpacesInSquareBrackets: false 102 | Standard: Cpp11 103 | TabWidth: 2 104 | UseTab: Never 105 | ... 106 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled Object files 2 | *.slo 3 | *.lo 4 | *.o 5 | *.obj 6 | 7 | # Precompiled Headers 8 | *.gch 9 | *.pch 10 | 11 | # Compiled Dynamic libraries 12 | *.so 13 | *.dylib 14 | *.dll 15 | 16 | # Compiled Static libraries 17 | *.lai 18 | *.la 19 | *.a 20 | *.lib 21 | 22 | # Executables 23 | *.exe 24 | *.out 25 | *.app 26 | 27 | # CMAKE 28 | CMakeFiles/ 29 | build/ 30 | *.cmake 31 | CMakeCache.txt 32 | 33 | # Visual Studio Code 34 | .vscode/settings.json 35 | 36 | fairytale 37 | fairytale.dSYM 38 | runTests 39 | gtest_main.pc 40 | gtest.pc 41 | googletest-src/ 42 | googletest-download/ 43 | googletest-build/ 44 | gmock_main.pc 45 | gmock.pc 46 | .idea/ 47 | *.swp 48 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: cpp 2 | sudo: required 3 | dist: trusty 4 | compiler: 5 | - clang 6 | - gcc 7 | os: 8 | - linux 9 | - osx 10 | 11 | matrix: 12 | exclude: 13 | - os: osx 14 | compiler: gcc 15 | 16 | before_install: 17 | - echo $LANG 18 | - echo $LC_ALL 19 | 20 | script: 21 | - if [ $TRAVIS_OS_NAME == linux ]; then cmake -Bbuild -H. -G"Unix Makefiles"; cd build; make; ./runTests; fi 22 | - if [ $TRAVIS_OS_NAME == osx ]; then cmake -Bbuild -H. -G"Unix Makefiles"; cd build; make; ./runTests; fi -------------------------------------------------------------------------------- /.vscode/settings.json_template: -------------------------------------------------------------------------------- 1 | // this is a template for Visual Studio workspace settings 2 | // follow this step-by-step instructions to use: 3 | 4 | // - install the ClangFormat extension from https://marketplace.visualstudio.com/items?itemName=LLVMExtensions.ClangFormat 5 | // - adjust the file path to clang-format.exe below ("clang-format.executable") to your local environment 6 | // - copy this file to ".vscode/settings.json" 7 | // - open the fairytale folder to use the settings in Visual Studio Code 8 | 9 | // - you can test success by changing indentation in a .cpp/.h file and saving, 10 | // the indentation should be automatically corrected 11 | 12 | { 13 | "files.encoding": "windows1252", 14 | "editor.formatOnSave": true, 15 | "clang-format.executable": "C:/Program Files/LLVM/bin/clang-format.exe", 16 | } -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Download and unpack googletest at configure time 2 | configure_file(CMakeLists.txt.in googletest-download/CMakeLists.txt) 3 | execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" . 4 | RESULT_VARIABLE result 5 | WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/googletest-download ) 6 | if(result) 7 | message(FATAL_ERROR "CMake step for googletest failed: ${result}") 8 | endif() 9 | execute_process(COMMAND ${CMAKE_COMMAND} --build . 10 | RESULT_VARIABLE result 11 | WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/googletest-download ) 12 | if(result) 13 | message(FATAL_ERROR "Build step for googletest failed: ${result}") 14 | endif() 15 | 16 | # Prevent overriding the parent project's compiler/linker 17 | # settings on Windows 18 | set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) 19 | 20 | # Add googletest directly to our build. This defines 21 | # the gtest and gtest_main targets. 22 | add_subdirectory(${CMAKE_BINARY_DIR}/googletest-src 23 | ${CMAKE_BINARY_DIR}/googletest-build 24 | EXCLUDE_FROM_ALL) 25 | 26 | cmake_minimum_required(VERSION 3.1) 27 | project (fairytale) 28 | option(BUILD_ZLIB "BUILD ZLIB" ON) 29 | 30 | set (CMAKE_CXX_STANDARD 11) 31 | 32 | if(NOT CMAKE_BUILD_TYPE) 33 | set(CMAKE_BUILD_TYPE Release) 34 | endif() 35 | 36 | ## SOURCES 37 | set(TRANSFORMSCPP transforms/zlibtransform.cpp) 38 | set(PARSERSCPP parsers/ddsparser.cpp parsers/modparser.cpp parsers/textparser.cpp parsers/jsonparser.cpp) 39 | set(MAINCPP fairytale.cpp analyser.cpp block.cpp deduper.cpp filestream.cpp hybridstream.cpp storagemanager.cpp) 40 | 41 | ## CONFIGURE ZLIB FOR BUILD 42 | if (BUILD_ZLIB) 43 | message("BUILDING ZLIB TOO") 44 | include_directories(fairytale contrib/zlib) 45 | list(APPEND ZLIB_SOURCES 46 | contrib/zlib/inflate.c 47 | contrib/zlib/infback.c 48 | contrib/zlib/trees.c 49 | contrib/zlib/compress.c 50 | contrib/zlib/inftrees.c 51 | contrib/zlib/zutil.c 52 | contrib/zlib/deflate.c 53 | contrib/zlib/inffast.c 54 | contrib/zlib/uncompr.c 55 | contrib/zlib/adler32.c 56 | contrib/zlib/crc32.c) 57 | endif(BUILD_ZLIB) 58 | 59 | add_executable(fairytale ${TRANSFORMSCPP} ${PARSERSCPP} ${MAINCPP} ${ZLIB_SOURCES}) 60 | 61 | if (NOT BUILD_ZLIB) 62 | message("Using Installed ZLIB") 63 | find_package(ZLIB REQUIRED) 64 | if (ZLIB_FOUND) 65 | include_directories(${ZLIB_INCLUDE_DIRS}) 66 | target_link_libraries(fairytale ${ZLIB_LIBRARIES}) 67 | endif(ZLIB_FOUND) 68 | endif(NOT BUILD_ZLIB) 69 | 70 | file(GLOB test_source test/*.cpp) 71 | add_executable(runTests ${test_source}) 72 | 73 | target_link_libraries(runTests gtest_main) 74 | 75 | # Set compiler flags in the end. that way all targets are available. 76 | if(CMAKE_C_COMPILER_ID MATCHES "Clang|GNU") 77 | set(CMAKE_C_FLAGS "-Wall") 78 | set(CMAKE_C_FLAGS_DEBUG "-g") 79 | set(CMAKE_C_FLAGS_RELEASE "-O2") 80 | set(CMAKE_C_FLAGS_CUSTOM "-Ofast -flto -march=native") 81 | endif() 82 | 83 | if (CMAKE_C_COMPILER_ID MATCHES "GNU") 84 | set(CMAKE_C_FLAGS_RELEASE "-flto") 85 | endif() 86 | 87 | if(CMAKE_C_COMPILER_ID MATCHES "MSVC") 88 | set(CMAKE_C_FLAGS "/Wall") 89 | set(CMAKE_C_FLAGS "/MP") #Multi-processor Compilation. For more hints on compiling speedups refer to https://randomascii.wordpress.com/2014/03/22/make-vc-compiles-fast-through-parallel-compilation/ 90 | set(CMAKE_C_FLAGS_RELEASE "/GL") #prerequisite for /LTCG 91 | set_target_properties(runTests PROPERTIES COMPILE_FLAGS "/Zi") #needed for source file detection of the google test interface in Visual Studio 92 | 93 | set(CMAKE_EXE_LINKER_FLAGS_RELEASE "/INCREMENTAL:NO") #incompatible with /LTCG 94 | set(CMAKE_EXE_LINKER_FLAGS_RELEASE "/LTCG") #link time code generation: allows for more optimizations, now even functions from other .cpp files and even static libraries in the same solution can be inlined. 95 | set_target_properties(runTests PROPERTIES LINK_FLAGS_RELEASE "/DEBUG") #needed for source file detection of the google test interface in Visual Studio 96 | endif() -------------------------------------------------------------------------------- /CMakeLists.txt.in: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8.2) 2 | 3 | project(googletest-download NONE) 4 | 5 | include(ExternalProject) 6 | ExternalProject_Add(googletest 7 | GIT_REPOSITORY https://github.com/google/googletest.git 8 | GIT_TAG master 9 | SOURCE_DIR "${CMAKE_BINARY_DIR}/googletest-src" 10 | BINARY_DIR "${CMAKE_BINARY_DIR}/googletest-build" 11 | CONFIGURE_COMMAND "" 12 | BUILD_COMMAND "" 13 | INSTALL_COMMAND "" 14 | TEST_COMMAND "" 15 | ) -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU LESSER GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | 9 | This version of the GNU Lesser General Public License incorporates 10 | the terms and conditions of version 3 of the GNU General Public 11 | License, supplemented by the additional permissions listed below. 12 | 13 | 0. Additional Definitions. 14 | 15 | As used herein, "this License" refers to version 3 of the GNU Lesser 16 | General Public License, and the "GNU GPL" refers to version 3 of the GNU 17 | General Public License. 18 | 19 | "The Library" refers to a covered work governed by this License, 20 | other than an Application or a Combined Work as defined below. 21 | 22 | An "Application" is any work that makes use of an interface provided 23 | by the Library, but which is not otherwise based on the Library. 24 | Defining a subclass of a class defined by the Library is deemed a mode 25 | of using an interface provided by the Library. 26 | 27 | A "Combined Work" is a work produced by combining or linking an 28 | Application with the Library. The particular version of the Library 29 | with which the Combined Work was made is also called the "Linked 30 | Version". 31 | 32 | The "Minimal Corresponding Source" for a Combined Work means the 33 | Corresponding Source for the Combined Work, excluding any source code 34 | for portions of the Combined Work that, considered in isolation, are 35 | based on the Application, and not on the Linked Version. 36 | 37 | The "Corresponding Application Code" for a Combined Work means the 38 | object code and/or source code for the Application, including any data 39 | and utility programs needed for reproducing the Combined Work from the 40 | Application, but excluding the System Libraries of the Combined Work. 41 | 42 | 1. Exception to Section 3 of the GNU GPL. 43 | 44 | You may convey a covered work under sections 3 and 4 of this License 45 | without being bound by section 3 of the GNU GPL. 46 | 47 | 2. Conveying Modified Versions. 48 | 49 | If you modify a copy of the Library, and, in your modifications, a 50 | facility refers to a function or data to be supplied by an Application 51 | that uses the facility (other than as an argument passed when the 52 | facility is invoked), then you may convey a copy of the modified 53 | version: 54 | 55 | a) under this License, provided that you make a good faith effort to 56 | ensure that, in the event an Application does not supply the 57 | function or data, the facility still operates, and performs 58 | whatever part of its purpose remains meaningful, or 59 | 60 | b) under the GNU GPL, with none of the additional permissions of 61 | this License applicable to that copy. 62 | 63 | 3. Object Code Incorporating Material from Library Header Files. 64 | 65 | The object code form of an Application may incorporate material from 66 | a header file that is part of the Library. You may convey such object 67 | code under terms of your choice, provided that, if the incorporated 68 | material is not limited to numerical parameters, data structure 69 | layouts and accessors, or small macros, inline functions and templates 70 | (ten or fewer lines in length), you do both of the following: 71 | 72 | a) Give prominent notice with each copy of the object code that the 73 | Library is used in it and that the Library and its use are 74 | covered by this License. 75 | 76 | b) Accompany the object code with a copy of the GNU GPL and this license 77 | document. 78 | 79 | 4. Combined Works. 80 | 81 | You may convey a Combined Work under terms of your choice that, 82 | taken together, effectively do not restrict modification of the 83 | portions of the Library contained in the Combined Work and reverse 84 | engineering for debugging such modifications, if you also do each of 85 | the following: 86 | 87 | a) Give prominent notice with each copy of the Combined Work that 88 | the Library is used in it and that the Library and its use are 89 | covered by this License. 90 | 91 | b) Accompany the Combined Work with a copy of the GNU GPL and this license 92 | document. 93 | 94 | c) For a Combined Work that displays copyright notices during 95 | execution, include the copyright notice for the Library among 96 | these notices, as well as a reference directing the user to the 97 | copies of the GNU GPL and this license document. 98 | 99 | d) Do one of the following: 100 | 101 | 0) Convey the Minimal Corresponding Source under the terms of this 102 | License, and the Corresponding Application Code in a form 103 | suitable for, and under terms that permit, the user to 104 | recombine or relink the Application with a modified version of 105 | the Linked Version to produce a modified Combined Work, in the 106 | manner specified by section 6 of the GNU GPL for conveying 107 | Corresponding Source. 108 | 109 | 1) Use a suitable shared library mechanism for linking with the 110 | Library. A suitable mechanism is one that (a) uses at run time 111 | a copy of the Library already present on the user's computer 112 | system, and (b) will operate properly with a modified version 113 | of the Library that is interface-compatible with the Linked 114 | Version. 115 | 116 | e) Provide Installation Information, but only if you would otherwise 117 | be required to provide such information under section 6 of the 118 | GNU GPL, and only to the extent that such information is 119 | necessary to install and execute a modified version of the 120 | Combined Work produced by recombining or relinking the 121 | Application with a modified version of the Linked Version. (If 122 | you use option 4d0, the Installation Information must accompany 123 | the Minimal Corresponding Source and Corresponding Application 124 | Code. If you use option 4d1, you must provide the Installation 125 | Information in the manner specified by section 6 of the GNU GPL 126 | for conveying Corresponding Source.) 127 | 128 | 5. Combined Libraries. 129 | 130 | You may place library facilities that are a work based on the 131 | Library side by side in a single library together with other library 132 | facilities that are not Applications and are not covered by this 133 | License, and convey such a combined library under terms of your 134 | choice, if you do both of the following: 135 | 136 | a) Accompany the combined library with a copy of the same work based 137 | on the Library, uncombined with any other library facilities, 138 | conveyed under the terms of this License. 139 | 140 | b) Give prominent notice with the combined library that part of it 141 | is a work based on the Library, and explaining where to find the 142 | accompanying uncombined form of the same work. 143 | 144 | 6. Revised Versions of the GNU Lesser General Public License. 145 | 146 | The Free Software Foundation may publish revised and/or new versions 147 | of the GNU Lesser General Public License from time to time. Such new 148 | versions will be similar in spirit to the present version, but may 149 | differ in detail to address new problems or concerns. 150 | 151 | Each version is given a distinguishing version number. If the 152 | Library as you received it specifies that a certain numbered version 153 | of the GNU Lesser General Public License "or any later version" 154 | applies to it, you have the option of following the terms and 155 | conditions either of that published version or of any later version 156 | published by the Free Software Foundation. If the Library as you 157 | received it does not specify a version number of the GNU Lesser 158 | General Public License, you may choose any version of the GNU Lesser 159 | General Public License ever published by the Free Software Foundation. 160 | 161 | If the Library as you received it specifies that a proxy can decide 162 | whether future versions of the GNU Lesser General Public License shall 163 | apply, that proxy's public statement of acceptance of any version is 164 | permanent authorization for you to choose that version for the 165 | Library. 166 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Build Status](https://travis-ci.org/schnaader/fairytale.svg?branch=master)](https://travis-ci.org/schnaader/fairytale) 2 | [![Build status](https://ci.appveyor.com/api/projects/status/k3y23dpxfu4rm108?svg=true)](https://ci.appveyor.com/project/schnaader/fairytale) 3 | [![Join the chat at https://gitter.im/encode-ru-Community-Archiver/Lobby](https://badges.gitter.im/encode-ru-Community-Archiver/Lobby.svg)](https://gitter.im/encode-ru-Community-Archiver/Lobby?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) 4 | 5 | Important 6 | --------- 7 | **This repository is moving to [Gitlab](https://gitlab.com/schnaader/fairytale) - please update your bookmarks and do any contributions there. The GitHub repository will be removed after successful migration.** 8 | 9 | # Fairytale 10 | modern lossless community archiver that features 11 | 12 | * state of the art analysis of input data 13 | * detection and transform / recompression of data to improve compression 14 | * sorting input and arrange it in different streams 15 | * deduplication across all streams and recursion levels 16 | * applying different (best fit) compression algorithms on each stream 17 | * storing compressed data in a modern archiver format 18 | 19 | It offers great modularity so that any algorithm can be added to it 20 | and enough flexibility to chose between fast, best practical or experimental state-of-the-art compression. 21 | Fairytale is a dream of a next generation archiver and it is a work in progress 22 | so if you share our dream and want to contribute, [join our great community here](https://gitter.im/encode-ru-Community-Archiver) 23 | 24 | How to build 25 | ------------ 26 | 27 | Using CMake (download it [here](https://cmake.org/download/)), you can build on many platforms using your favorite compiler (Visual Studio, MinGW, CodeBlocks, XCode, Unix Makefiles, ...). It will also detect automatically if zlib is installed and if not, compiles it from source. 28 | 29 | For Windows, there's a make.bat batch script that works with MinGW. Use `make` for a 64-bit build, `make 32` for a 32-bit build. 30 | 31 | For Linux, OSX and ARM, there are Makefiles. Use `make` for a 64-bit build, `make -f Makefile.32` for a 32-bit build. 32 | 33 | Releases/Binaries 34 | ----------------- 35 | 36 | Please note that this is a very rough prototype that allows for testing of the pre-processing library. 37 | It doesn't apply any compression right now. 38 | 39 | [for ARM](https://drive.google.com/file/d/1Uc1w3Sf0J8A2wGZtcYtIDpHjcSuX8oY7/view) 40 | 41 | [for Linux](..) 42 | 43 | [for OSX](..) 44 | 45 | [for Windows](https://drive.google.com/drive/folders/1uj2YVjpbRscJiM0llTU-9uJuY5BmgBvt) 46 | 47 | License 48 | ------- 49 | 50 | Licensed under the [LGPL-3.0 license](https://github.com/schnaader/fairytale/blob/master/LICENSE) 51 | -------------------------------------------------------------------------------- /analyser.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schnaader/fairytale/de6d7a1cde2fc3ff89d341015679332da138330f/analyser.cpp -------------------------------------------------------------------------------- /analyser.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schnaader/fairytale/de6d7a1cde2fc3ff89d341015679332da138330f/analyser.h -------------------------------------------------------------------------------- /appveyor.yml: -------------------------------------------------------------------------------- 1 | version: 1.0.{build} 2 | build_script: 3 | - cmd: >- 4 | cmake -Bbuild -H. -G"Visual Studio 14 2015 Win64" 5 | 6 | msbuild build\fairytale.sln 7 | 8 | cd build\debug 9 | 10 | runTests -------------------------------------------------------------------------------- /array.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schnaader/fairytale/de6d7a1cde2fc3ff89d341015679332da138330f/array.h -------------------------------------------------------------------------------- /block.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schnaader/fairytale/de6d7a1cde2fc3ff89d341015679332da138330f/block.cpp -------------------------------------------------------------------------------- /block.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schnaader/fairytale/de6d7a1cde2fc3ff89d341015679332da138330f/block.h -------------------------------------------------------------------------------- /common.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the Fairytale project 3 | 4 | Copyright (C) 2018 Márcio Pais 5 | 6 | This library is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU Lesser General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | This library is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU Lesser General Public License for more details. 15 | 16 | You should have received a copy of the GNU Lesser General Public License 17 | along with this program. If not, see . 18 | */ 19 | 20 | #ifndef COMMON_H 21 | #define COMMON_H 22 | 23 | #if defined(_WIN32) || defined(_MSC_VER) 24 | # ifndef WINDOWS 25 | # define WINDOWS //to compile for Windows 26 | # endif 27 | #elif defined(unix) || defined(__unix__) || defined(__unix) || defined(__APPLE__) 28 | # ifndef UNIX 29 | # define UNIX //to compile for Unix, Linux, Solaris, MacOS / Darwin, etc) 30 | # endif 31 | #endif 32 | 33 | #if !defined(WINDOWS) && !defined(UNIX) 34 | # error Unknown target system 35 | #endif 36 | 37 | enum class Endian 38 | { 39 | Little, 40 | Big 41 | }; 42 | 43 | #ifdef BIG_ENDIAN 44 | # define betoh64(x) (x) // Big-Endian to Host translation, 64 bit 45 | # define betoh32(x) (x) // Big-Endian to Host translation, 32 bit 46 | # define betoh16(x) (x) // Big-Endian to Host translation, 16 bit 47 | # if defined(__GNUG__) || defined(__clang__) 48 | # define letoh64(x) (x = __builtin_bswap64(x)) // Little-Endian to Host translation, 64 bit 49 | # define letoh32(x) (x = __builtin_bswap32(x)) // Little-Endian to Host translation, 32 bit 50 | # define letoh16(x) (x = __builtin_bswap16(x)) // Little-Endian to Host translation, 16 bit 51 | # elif defined(_MSC_VER) 52 | # define letoh64(x) (x = _byteswap_uint64(x)) // Little-Endian to Host translation, 64 bit 53 | # define letoh32(x) (x = _byteswap_ulong(x)) // Little-Endian to Host translation, 32 bit 54 | # define letoh16(x) (x = _byteswap_ushort(x)) // Little-Endian to Host translation, 16 bit 55 | # endif 56 | #else 57 | # define letoh64(x) (x) // Little-Endian to Host translation, 64 bit 58 | # define letoh32(x) (x) // Little-Endian to Host translation, 32 bit 59 | # define letoh16(x) (x) // Little-Endian to Host translation, 16 bit 60 | # if defined(__GNUG__) || defined(__clang__) 61 | # define betoh64(x) (x = __builtin_bswap64(x)) // Big-Endian to Host translation, 64 bit 62 | # define betoh32(x) (x = __builtin_bswap32(x)) // Big-Endian to Host translation, 32 bit 63 | # define betoh16(x) (x = __builtin_bswap16(x)) // Big-Endian to Host translation, 16 bit 64 | # elif defined(_MSC_VER) 65 | # define betoh64(x) (x = _byteswap_uint64(x)) // Big-Endian to Host translation, 64 bit 66 | # define betoh32(x) (x = _byteswap_ulong(x)) // Big-Endian to Host translation, 32 bit 67 | # define betoh16(x) (x = _byteswap_ushort(x)) // Big-Endian to Host translation, 16 bit 68 | # endif 69 | #endif 70 | 71 | #ifdef UNIX 72 | # include //opendir(), readdir(), dirent() 73 | # include //errno 74 | # include //PATH_MAX (for OSX) 75 | # include //strlen(), strcpy(), strcat(), strerror(), memset(), memcpy(), memmove() 76 | # include //isatty() 77 | # include 78 | #else 79 | # ifndef NOMINMAX 80 | # define NOMINMAX 81 | # endif 82 | # include //_O_APPEND 83 | # include //_open_osfhandle 84 | # include //clock_t, CLOCKS_PER_SEC 85 | # include 86 | #endif 87 | 88 | // Platform-independent includes 89 | #include 90 | #include //floor(), sqrt() 91 | #include //stat(), mkdir(), stat() 92 | #include //PRIu64 (C99 standard, available since VS 2013 RTM) 93 | #include //std::exception 94 | #ifndef _MSC_VER 95 | # include 96 | #endif 97 | 98 | #define _FILE_OFFSET_BITS 64 99 | #if defined(WINDOWS) && (defined(__x86_64) || defined(_M_X64)) 100 | # define off_t int64_t 101 | #endif 102 | #ifdef _MSC_VER 103 | # define fseeko(a, b, c) _fseeki64(a, b, c) 104 | # define ftello(a) _ftelli64(a) 105 | #else 106 | # ifndef UNIX 107 | # ifndef fseeko 108 | # define fseeko(a, b, c) fseeko64(a, b, c) 109 | # endif 110 | # ifndef ftello 111 | # define ftello(a) ftello64(a) 112 | # endif 113 | # endif 114 | #endif 115 | 116 | #ifdef WINDOWS 117 | # ifdef GetTempFileName 118 | # undef GetTempFileName 119 | # endif 120 | # define GetTempFileName GetTempFileNameW 121 | # ifdef CreateFile 122 | # undef CreateFile 123 | # endif 124 | # define CreateFile CreateFileW 125 | #endif 126 | 127 | extern int verbose; 128 | #define LOG(fmt, ...) \ 129 | do { \ 130 | if (verbose) \ 131 | printf(fmt, ##__VA_ARGS__); \ 132 | } while (0) 133 | #define TRACE(fmt, ...) \ 134 | do { \ 135 | if (verbose > 1) \ 136 | fprintf(stderr, fmt, ##__VA_ARGS__); \ 137 | } while (0) 138 | 139 | #define MAX_INDEXABLE size_t((1ull << (sizeof(void*) * 8 - 1)) - 1) 140 | #define MEM_LIMIT(mem) (mem > (int64_t)MAX_INDEXABLE ? MAX_INDEXABLE : size_t(mem)) 141 | 142 | #define GENERIC_BUFFER_SIZE 0x8000ll //32KB 143 | #if (GENERIC_BUFFER_SIZE & (GENERIC_BUFFER_SIZE - 1)) || (GENERIC_BUFFER_SIZE <= 4096) 144 | # error GENERIC_BUFFER_SIZE must be a power of 2 bigger than 4096 145 | #endif 146 | 147 | #define MAX_RECURSION_LEVEL 4 148 | 149 | class ExhaustedStorageException : public std::exception {}; 150 | 151 | # define TAB 0x09 152 | # define NEW_LINE 0x0A 153 | # define CARRIAGE_RETURN 0x0D 154 | # define SPACE 0x20 155 | 156 | #endif 157 | -------------------------------------------------------------------------------- /contrib/zlib/adler32.c: -------------------------------------------------------------------------------- 1 | /* adler32.c -- compute the Adler-32 checksum of a data stream 2 | * Copyright (C) 1995-2011 Mark Adler 3 | * For conditions of distribution and use, see copyright notice in zlib.h 4 | */ 5 | 6 | /* @(#) $Id$ */ 7 | 8 | #include "zutil.h" 9 | 10 | #define local static 11 | 12 | local uLong adler32_combine_ OF((uLong adler1, uLong adler2, z_off64_t len2)); 13 | 14 | #define BASE 65521 /* largest prime smaller than 65536 */ 15 | #define NMAX 5552 16 | /* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */ 17 | 18 | #define DO1(buf,i) {adler += (buf)[i]; sum2 += adler;} 19 | #define DO2(buf,i) DO1(buf,i); DO1(buf,i+1); 20 | #define DO4(buf,i) DO2(buf,i); DO2(buf,i+2); 21 | #define DO8(buf,i) DO4(buf,i); DO4(buf,i+4); 22 | #define DO16(buf) DO8(buf,0); DO8(buf,8); 23 | 24 | /* use NO_DIVIDE if your processor does not do division in hardware -- 25 | try it both ways to see which is faster */ 26 | #ifdef NO_DIVIDE 27 | /* note that this assumes BASE is 65521, where 65536 % 65521 == 15 28 | (thank you to John Reiser for pointing this out) */ 29 | # define CHOP(a) \ 30 | do { \ 31 | unsigned long tmp = a >> 16; \ 32 | a &= 0xffffUL; \ 33 | a += (tmp << 4) - tmp; \ 34 | } while (0) 35 | # define MOD28(a) \ 36 | do { \ 37 | CHOP(a); \ 38 | if (a >= BASE) a -= BASE; \ 39 | } while (0) 40 | # define MOD(a) \ 41 | do { \ 42 | CHOP(a); \ 43 | MOD28(a); \ 44 | } while (0) 45 | # define MOD63(a) \ 46 | do { /* this assumes a is not negative */ \ 47 | z_off64_t tmp = a >> 32; \ 48 | a &= 0xffffffffL; \ 49 | a += (tmp << 8) - (tmp << 5) + tmp; \ 50 | tmp = a >> 16; \ 51 | a &= 0xffffL; \ 52 | a += (tmp << 4) - tmp; \ 53 | tmp = a >> 16; \ 54 | a &= 0xffffL; \ 55 | a += (tmp << 4) - tmp; \ 56 | if (a >= BASE) a -= BASE; \ 57 | } while (0) 58 | #else 59 | # define MOD(a) a %= BASE 60 | # define MOD28(a) a %= BASE 61 | # define MOD63(a) a %= BASE 62 | #endif 63 | 64 | /* ========================================================================= */ 65 | uLong ZEXPORT adler32(adler, buf, len) 66 | uLong adler; 67 | const Bytef *buf; 68 | uInt len; 69 | { 70 | unsigned long sum2; 71 | unsigned n; 72 | 73 | /* split Adler-32 into component sums */ 74 | sum2 = (adler >> 16) & 0xffff; 75 | adler &= 0xffff; 76 | 77 | /* in case user likes doing a byte at a time, keep it fast */ 78 | if (len == 1) { 79 | adler += buf[0]; 80 | if (adler >= BASE) 81 | adler -= BASE; 82 | sum2 += adler; 83 | if (sum2 >= BASE) 84 | sum2 -= BASE; 85 | return adler | (sum2 << 16); 86 | } 87 | 88 | /* initial Adler-32 value (deferred check for len == 1 speed) */ 89 | if (buf == Z_NULL) 90 | return 1L; 91 | 92 | /* in case short lengths are provided, keep it somewhat fast */ 93 | if (len < 16) { 94 | while (len--) { 95 | adler += *buf++; 96 | sum2 += adler; 97 | } 98 | if (adler >= BASE) 99 | adler -= BASE; 100 | MOD28(sum2); /* only added so many BASE's */ 101 | return adler | (sum2 << 16); 102 | } 103 | 104 | /* do length NMAX blocks -- requires just one modulo operation */ 105 | while (len >= NMAX) { 106 | len -= NMAX; 107 | n = NMAX / 16; /* NMAX is divisible by 16 */ 108 | do { 109 | DO16(buf); /* 16 sums unrolled */ 110 | buf += 16; 111 | } while (--n); 112 | MOD(adler); 113 | MOD(sum2); 114 | } 115 | 116 | /* do remaining bytes (less than NMAX, still just one modulo) */ 117 | if (len) { /* avoid modulos if none remaining */ 118 | while (len >= 16) { 119 | len -= 16; 120 | DO16(buf); 121 | buf += 16; 122 | } 123 | while (len--) { 124 | adler += *buf++; 125 | sum2 += adler; 126 | } 127 | MOD(adler); 128 | MOD(sum2); 129 | } 130 | 131 | /* return recombined sums */ 132 | return adler | (sum2 << 16); 133 | } 134 | 135 | /* ========================================================================= */ 136 | local uLong adler32_combine_(adler1, adler2, len2) 137 | uLong adler1; 138 | uLong adler2; 139 | z_off64_t len2; 140 | { 141 | unsigned long sum1; 142 | unsigned long sum2; 143 | unsigned rem; 144 | 145 | /* for negative len, return invalid adler32 as a clue for debugging */ 146 | if (len2 < 0) 147 | return 0xffffffffUL; 148 | 149 | /* the derivation of this formula is left as an exercise for the reader */ 150 | MOD63(len2); /* assumes len2 >= 0 */ 151 | rem = (unsigned)len2; 152 | sum1 = adler1 & 0xffff; 153 | sum2 = rem * sum1; 154 | MOD(sum2); 155 | sum1 += (adler2 & 0xffff) + BASE - 1; 156 | sum2 += ((adler1 >> 16) & 0xffff) + ((adler2 >> 16) & 0xffff) + BASE - rem; 157 | if (sum1 >= BASE) sum1 -= BASE; 158 | if (sum1 >= BASE) sum1 -= BASE; 159 | if (sum2 >= (BASE << 1)) sum2 -= (BASE << 1); 160 | if (sum2 >= BASE) sum2 -= BASE; 161 | return sum1 | (sum2 << 16); 162 | } 163 | 164 | /* ========================================================================= */ 165 | uLong ZEXPORT adler32_combine(adler1, adler2, len2) 166 | uLong adler1; 167 | uLong adler2; 168 | z_off_t len2; 169 | { 170 | return adler32_combine_(adler1, adler2, len2); 171 | } 172 | 173 | uLong ZEXPORT adler32_combine64(adler1, adler2, len2) 174 | uLong adler1; 175 | uLong adler2; 176 | z_off64_t len2; 177 | { 178 | return adler32_combine_(adler1, adler2, len2); 179 | } 180 | -------------------------------------------------------------------------------- /contrib/zlib/compress.c: -------------------------------------------------------------------------------- 1 | /* compress.c -- compress a memory buffer 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 | #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 = (z_const 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) + 79 | (sourceLen >> 25) + 13; 80 | } 81 | -------------------------------------------------------------------------------- /contrib/zlib/crc32.c: -------------------------------------------------------------------------------- 1 | /* crc32.c -- compute the CRC-32 of a data stream 2 | * Copyright (C) 1995-2006, 2010, 2011, 2012 Mark Adler 3 | * For conditions of distribution and use, see copyright notice in zlib.h 4 | * 5 | * Thanks to Rodney Brown 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 | DYNAMIC_CRC_TABLE and MAKECRCH can be #defined to write out crc32.h. 22 | */ 23 | 24 | #ifdef MAKECRCH 25 | # include 26 | # ifndef DYNAMIC_CRC_TABLE 27 | # define DYNAMIC_CRC_TABLE 28 | # endif /* !DYNAMIC_CRC_TABLE */ 29 | #endif /* MAKECRCH */ 30 | 31 | #include "zutil.h" /* for STDC and FAR definitions */ 32 | 33 | #define local static 34 | 35 | /* Definitions for doing the crc four data bytes at a time. */ 36 | #if !defined(NOBYFOUR) && defined(Z_U4) 37 | # define BYFOUR 38 | #endif 39 | #ifdef BYFOUR 40 | local unsigned long crc32_little OF((unsigned long, 41 | const unsigned char FAR *, unsigned)); 42 | local unsigned long crc32_big OF((unsigned long, 43 | const unsigned char FAR *, unsigned)); 44 | # define TBLS 8 45 | #else 46 | # define TBLS 1 47 | #endif /* BYFOUR */ 48 | 49 | /* Local functions for crc concatenation */ 50 | local unsigned long gf2_matrix_times OF((unsigned long *mat, 51 | unsigned long vec)); 52 | local void gf2_matrix_square OF((unsigned long *square, unsigned long *mat)); 53 | local uLong crc32_combine_ OF((uLong crc1, uLong crc2, z_off64_t len2)); 54 | 55 | 56 | #ifdef DYNAMIC_CRC_TABLE 57 | 58 | local volatile int crc_table_empty = 1; 59 | local z_crc_t FAR crc_table[TBLS][256]; 60 | local void make_crc_table OF((void)); 61 | #ifdef MAKECRCH 62 | local void write_table OF((FILE *, const z_crc_t FAR *)); 63 | #endif /* MAKECRCH */ 64 | /* 65 | Generate tables for a byte-wise 32-bit CRC calculation on the polynomial: 66 | 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. 67 | 68 | Polynomials over GF(2) are represented in binary, one bit per coefficient, 69 | with the lowest powers in the most significant bit. Then adding polynomials 70 | is just exclusive-or, and multiplying a polynomial by x is a right shift by 71 | one. If we call the above polynomial p, and represent a byte as the 72 | polynomial q, also with the lowest power in the most significant bit (so the 73 | byte 0xb1 is the polynomial x^7+x^3+x+1), then the CRC is (q*x^32) mod p, 74 | where a mod b means the remainder after dividing a by b. 75 | 76 | This calculation is done using the shift-register method of multiplying and 77 | taking the remainder. The register is initialized to zero, and for each 78 | incoming bit, x^32 is added mod p to the register if the bit is a one (where 79 | x^32 mod p is p+x^32 = x^26+...+1), and the register is multiplied mod p by 80 | x (which is shifting right by one and adding x^32 mod p if the bit shifted 81 | out is a one). We start with the highest power (least significant bit) of 82 | q and repeat for all eight bits of q. 83 | 84 | The first table is simply the CRC of all possible eight bit values. This is 85 | all the information needed to generate CRCs on data a byte at a time for all 86 | combinations of CRC register values and incoming bytes. The remaining tables 87 | allow for word-at-a-time CRC calculation for both big-endian and little- 88 | endian machines, where a word is four bytes. 89 | */ 90 | local void make_crc_table() 91 | { 92 | z_crc_t c; 93 | int n, k; 94 | z_crc_t poly; /* polynomial exclusive-or pattern */ 95 | /* terms of polynomial defining this crc (except x^32): */ 96 | static volatile int first = 1; /* flag to limit concurrent making */ 97 | static const unsigned char p[] = {0,1,2,4,5,7,8,10,11,12,16,22,23,26}; 98 | 99 | /* See if another task is already doing this (not thread-safe, but better 100 | than nothing -- significantly reduces duration of vulnerability in 101 | case the advice about DYNAMIC_CRC_TABLE is ignored) */ 102 | if (first) { 103 | first = 0; 104 | 105 | /* make exclusive-or pattern from polynomial (0xedb88320UL) */ 106 | poly = 0; 107 | for (n = 0; n < (int)(sizeof(p)/sizeof(unsigned char)); n++) 108 | poly |= (z_crc_t)1 << (31 - p[n]); 109 | 110 | /* generate a crc for every 8-bit value */ 111 | for (n = 0; n < 256; n++) { 112 | c = (z_crc_t)n; 113 | for (k = 0; k < 8; k++) 114 | c = c & 1 ? poly ^ (c >> 1) : c >> 1; 115 | crc_table[0][n] = c; 116 | } 117 | 118 | #ifdef BYFOUR 119 | /* generate crc for each value followed by one, two, and three zeros, 120 | and then the byte reversal of those as well as the first table */ 121 | for (n = 0; n < 256; n++) { 122 | c = crc_table[0][n]; 123 | crc_table[4][n] = ZSWAP32(c); 124 | for (k = 1; k < 4; k++) { 125 | c = crc_table[0][c & 0xff] ^ (c >> 8); 126 | crc_table[k][n] = c; 127 | crc_table[k + 4][n] = ZSWAP32(c); 128 | } 129 | } 130 | #endif /* BYFOUR */ 131 | 132 | crc_table_empty = 0; 133 | } 134 | else { /* not first */ 135 | /* wait for the other guy to finish (not efficient, but rare) */ 136 | while (crc_table_empty) 137 | ; 138 | } 139 | 140 | #ifdef MAKECRCH 141 | /* write out CRC tables to crc32.h */ 142 | { 143 | FILE *out; 144 | 145 | out = fopen("crc32.h", "w"); 146 | if (out == NULL) return; 147 | fprintf(out, "/* crc32.h -- tables for rapid CRC calculation\n"); 148 | fprintf(out, " * Generated automatically by crc32.c\n */\n\n"); 149 | fprintf(out, "local const z_crc_t FAR "); 150 | fprintf(out, "crc_table[TBLS][256] =\n{\n {\n"); 151 | write_table(out, crc_table[0]); 152 | # ifdef BYFOUR 153 | fprintf(out, "#ifdef BYFOUR\n"); 154 | for (k = 1; k < 8; k++) { 155 | fprintf(out, " },\n {\n"); 156 | write_table(out, crc_table[k]); 157 | } 158 | fprintf(out, "#endif\n"); 159 | # endif /* BYFOUR */ 160 | fprintf(out, " }\n};\n"); 161 | fclose(out); 162 | } 163 | #endif /* MAKECRCH */ 164 | } 165 | 166 | #ifdef MAKECRCH 167 | local void write_table(out, table) 168 | FILE *out; 169 | const z_crc_t FAR *table; 170 | { 171 | int n; 172 | 173 | for (n = 0; n < 256; n++) 174 | fprintf(out, "%s0x%08lxUL%s", n % 5 ? "" : " ", 175 | (unsigned long)(table[n]), 176 | n == 255 ? "\n" : (n % 5 == 4 ? ",\n" : ", ")); 177 | } 178 | #endif /* MAKECRCH */ 179 | 180 | #else /* !DYNAMIC_CRC_TABLE */ 181 | /* ======================================================================== 182 | * Tables of CRC-32s of all single-byte values, made by make_crc_table(). 183 | */ 184 | #include "crc32.h" 185 | #endif /* DYNAMIC_CRC_TABLE */ 186 | 187 | /* ========================================================================= 188 | * This function can be used by asm versions of crc32() 189 | */ 190 | const z_crc_t FAR * ZEXPORT get_crc_table() 191 | { 192 | #ifdef DYNAMIC_CRC_TABLE 193 | if (crc_table_empty) 194 | make_crc_table(); 195 | #endif /* DYNAMIC_CRC_TABLE */ 196 | return (const z_crc_t FAR *)crc_table; 197 | } 198 | 199 | /* ========================================================================= */ 200 | #define DO1 crc = crc_table[0][((int)crc ^ (*buf++)) & 0xff] ^ (crc >> 8) 201 | #define DO8 DO1; DO1; DO1; DO1; DO1; DO1; DO1; DO1 202 | 203 | /* ========================================================================= */ 204 | unsigned long ZEXPORT crc32(crc, buf, len) 205 | unsigned long crc; 206 | const unsigned char FAR *buf; 207 | uInt len; 208 | { 209 | if (buf == Z_NULL) return 0UL; 210 | 211 | #ifdef DYNAMIC_CRC_TABLE 212 | if (crc_table_empty) 213 | make_crc_table(); 214 | #endif /* DYNAMIC_CRC_TABLE */ 215 | 216 | #ifdef BYFOUR 217 | if (sizeof(void *) == sizeof(ptrdiff_t)) { 218 | z_crc_t endian; 219 | 220 | endian = 1; 221 | if (*((unsigned char *)(&endian))) 222 | return crc32_little(crc, buf, len); 223 | else 224 | return crc32_big(crc, buf, len); 225 | } 226 | #endif /* BYFOUR */ 227 | crc = crc ^ 0xffffffffUL; 228 | while (len >= 8) { 229 | DO8; 230 | len -= 8; 231 | } 232 | if (len) do { 233 | DO1; 234 | } while (--len); 235 | return crc ^ 0xffffffffUL; 236 | } 237 | 238 | #ifdef BYFOUR 239 | 240 | /* ========================================================================= */ 241 | #define DOLIT4 c ^= *buf4++; \ 242 | c = crc_table[3][c & 0xff] ^ crc_table[2][(c >> 8) & 0xff] ^ \ 243 | crc_table[1][(c >> 16) & 0xff] ^ crc_table[0][c >> 24] 244 | #define DOLIT32 DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4 245 | 246 | /* ========================================================================= */ 247 | local unsigned long crc32_little(crc, buf, len) 248 | unsigned long crc; 249 | const unsigned char FAR *buf; 250 | unsigned len; 251 | { 252 | register z_crc_t c; 253 | register const z_crc_t FAR *buf4; 254 | 255 | c = (z_crc_t)crc; 256 | c = ~c; 257 | while (len && ((ptrdiff_t)buf & 3)) { 258 | c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8); 259 | len--; 260 | } 261 | 262 | buf4 = (const z_crc_t FAR *)(const void FAR *)buf; 263 | while (len >= 32) { 264 | DOLIT32; 265 | len -= 32; 266 | } 267 | while (len >= 4) { 268 | DOLIT4; 269 | len -= 4; 270 | } 271 | buf = (const unsigned char FAR *)buf4; 272 | 273 | if (len) do { 274 | c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8); 275 | } while (--len); 276 | c = ~c; 277 | return (unsigned long)c; 278 | } 279 | 280 | /* ========================================================================= */ 281 | #define DOBIG4 c ^= *++buf4; \ 282 | c = crc_table[4][c & 0xff] ^ crc_table[5][(c >> 8) & 0xff] ^ \ 283 | crc_table[6][(c >> 16) & 0xff] ^ crc_table[7][c >> 24] 284 | #define DOBIG32 DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4 285 | 286 | /* ========================================================================= */ 287 | local unsigned long crc32_big(crc, buf, len) 288 | unsigned long crc; 289 | const unsigned char FAR *buf; 290 | unsigned len; 291 | { 292 | register z_crc_t c; 293 | register const z_crc_t FAR *buf4; 294 | 295 | c = ZSWAP32((z_crc_t)crc); 296 | c = ~c; 297 | while (len && ((ptrdiff_t)buf & 3)) { 298 | c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8); 299 | len--; 300 | } 301 | 302 | buf4 = (const z_crc_t FAR *)(const void FAR *)buf; 303 | buf4--; 304 | while (len >= 32) { 305 | DOBIG32; 306 | len -= 32; 307 | } 308 | while (len >= 4) { 309 | DOBIG4; 310 | len -= 4; 311 | } 312 | buf4++; 313 | buf = (const unsigned char FAR *)buf4; 314 | 315 | if (len) do { 316 | c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8); 317 | } while (--len); 318 | c = ~c; 319 | return (unsigned long)(ZSWAP32(c)); 320 | } 321 | 322 | #endif /* BYFOUR */ 323 | 324 | #define GF2_DIM 32 /* dimension of GF(2) vectors (length of CRC) */ 325 | 326 | /* ========================================================================= */ 327 | local unsigned long gf2_matrix_times(mat, vec) 328 | unsigned long *mat; 329 | unsigned long vec; 330 | { 331 | unsigned long sum; 332 | 333 | sum = 0; 334 | while (vec) { 335 | if (vec & 1) 336 | sum ^= *mat; 337 | vec >>= 1; 338 | mat++; 339 | } 340 | return sum; 341 | } 342 | 343 | /* ========================================================================= */ 344 | local void gf2_matrix_square(square, mat) 345 | unsigned long *square; 346 | unsigned long *mat; 347 | { 348 | int n; 349 | 350 | for (n = 0; n < GF2_DIM; n++) 351 | square[n] = gf2_matrix_times(mat, mat[n]); 352 | } 353 | 354 | /* ========================================================================= */ 355 | local uLong crc32_combine_(crc1, crc2, len2) 356 | uLong crc1; 357 | uLong crc2; 358 | z_off64_t len2; 359 | { 360 | int n; 361 | unsigned long row; 362 | unsigned long even[GF2_DIM]; /* even-power-of-two zeros operator */ 363 | unsigned long odd[GF2_DIM]; /* odd-power-of-two zeros operator */ 364 | 365 | /* degenerate case (also disallow negative lengths) */ 366 | if (len2 <= 0) 367 | return crc1; 368 | 369 | /* put operator for one zero bit in odd */ 370 | odd[0] = 0xedb88320UL; /* CRC-32 polynomial */ 371 | row = 1; 372 | for (n = 1; n < GF2_DIM; n++) { 373 | odd[n] = row; 374 | row <<= 1; 375 | } 376 | 377 | /* put operator for two zero bits in even */ 378 | gf2_matrix_square(even, odd); 379 | 380 | /* put operator for four zero bits in odd */ 381 | gf2_matrix_square(odd, even); 382 | 383 | /* apply len2 zeros to crc1 (first square will put the operator for one 384 | zero byte, eight zero bits, in even) */ 385 | do { 386 | /* apply zeros operator for this bit of len2 */ 387 | gf2_matrix_square(even, odd); 388 | if (len2 & 1) 389 | crc1 = gf2_matrix_times(even, crc1); 390 | len2 >>= 1; 391 | 392 | /* if no more bits set, then done */ 393 | if (len2 == 0) 394 | break; 395 | 396 | /* another iteration of the loop with odd and even swapped */ 397 | gf2_matrix_square(odd, even); 398 | if (len2 & 1) 399 | crc1 = gf2_matrix_times(odd, crc1); 400 | len2 >>= 1; 401 | 402 | /* if no more bits set, then done */ 403 | } while (len2 != 0); 404 | 405 | /* return combined crc */ 406 | crc1 ^= crc2; 407 | return crc1; 408 | } 409 | 410 | /* ========================================================================= */ 411 | uLong ZEXPORT crc32_combine(crc1, crc2, len2) 412 | uLong crc1; 413 | uLong crc2; 414 | z_off_t len2; 415 | { 416 | return crc32_combine_(crc1, crc2, len2); 417 | } 418 | 419 | uLong ZEXPORT crc32_combine64(crc1, crc2, len2) 420 | uLong crc1; 421 | uLong crc2; 422 | z_off64_t len2; 423 | { 424 | return crc32_combine_(crc1, crc2, len2); 425 | } 426 | -------------------------------------------------------------------------------- /contrib/zlib/deflate.h: -------------------------------------------------------------------------------- 1 | /* deflate.h -- internal compression state 2 | * Copyright (C) 1995-2012 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 Buf_size 16 52 | /* size of bit buffer in bi_buf */ 53 | 54 | #define INIT_STATE 42 55 | #define EXTRA_STATE 69 56 | #define NAME_STATE 73 57 | #define COMMENT_STATE 91 58 | #define HCRC_STATE 103 59 | #define BUSY_STATE 113 60 | #define FINISH_STATE 666 61 | /* Stream status */ 62 | 63 | 64 | /* Data structure describing a single value and its code string. */ 65 | typedef struct ct_data_s { 66 | union 67 | { 68 | ush freq; /* frequency count */ 69 | ush code; /* bit string */ 70 | } fc; 71 | union 72 | { 73 | ush dad; /* father node in Huffman tree */ 74 | ush len; /* length of bit string */ 75 | } dl; 76 | } FAR ct_data; 77 | 78 | #define Freq fc.freq 79 | #define Code fc.code 80 | #define Dad dl.dad 81 | #define Len dl.len 82 | 83 | typedef struct static_tree_desc_s static_tree_desc; 84 | 85 | typedef struct tree_desc_s { 86 | ct_data *dyn_tree; /* the dynamic tree */ 87 | int max_code; /* largest code with non zero frequency */ 88 | static_tree_desc *stat_desc; /* the corresponding static tree */ 89 | } FAR tree_desc; 90 | 91 | typedef ush Pos; 92 | typedef Pos FAR Posf; 93 | typedef unsigned IPos; 94 | 95 | /* A Pos is an index in the character window. We use short instead of int to 96 | * save space in the various tables. IPos is used only for parameter passing. 97 | */ 98 | 99 | typedef struct internal_state { 100 | z_streamp strm; /* pointer back to this zlib stream */ 101 | int status; /* as the name implies */ 102 | Bytef *pending_buf; /* output still pending */ 103 | ulg pending_buf_size; /* size of pending_buf */ 104 | Bytef *pending_out; /* next pending byte to output to the stream */ 105 | uInt pending; /* nb of bytes in the pending buffer */ 106 | int wrap; /* bit 0 true for zlib, bit 1 true for gzip */ 107 | gz_headerp gzhead; /* gzip header information to write */ 108 | uInt gzindex; /* where in extra, name, or comment */ 109 | Byte method; /* can only be DEFLATED */ 110 | int last_flush; /* value of flush param for previous deflate call */ 111 | 112 | /* used by deflate.c: */ 113 | 114 | uInt w_size; /* LZ77 window size (32K by default) */ 115 | uInt w_bits; /* log2(w_size) (8..16) */ 116 | uInt w_mask; /* w_size - 1 */ 117 | 118 | Bytef *window; 119 | /* Sliding window. Input bytes are read into the second half of the window, 120 | * and move to the first half later to keep a dictionary of at least wSize 121 | * bytes. With this organization, matches are limited to a distance of 122 | * wSize-MAX_MATCH bytes, but this ensures that IO is always 123 | * performed with a length multiple of the block size. Also, it limits 124 | * the window size to 64K, which is quite useful on MSDOS. 125 | * To do: use the user input buffer as sliding window. 126 | */ 127 | 128 | ulg window_size; 129 | /* Actual size of window: 2*wSize, except when the user input buffer 130 | * is directly used as sliding window. 131 | */ 132 | 133 | Posf *prev; 134 | /* Link to older string with same hash index. To limit the size of this 135 | * array to 64K, this link is maintained only for the last 32K strings. 136 | * An index in this array is thus a window index modulo 32K. 137 | */ 138 | 139 | Posf *head; /* Heads of the hash chains or NIL. */ 140 | 141 | uInt ins_h; /* hash index of string to be inserted */ 142 | uInt hash_size; /* number of elements in hash table */ 143 | uInt hash_bits; /* log2(hash_size) */ 144 | uInt hash_mask; /* hash_size-1 */ 145 | 146 | uInt hash_shift; 147 | /* Number of bits by which ins_h must be shifted at each input 148 | * step. It must be such that after MIN_MATCH steps, the oldest 149 | * byte no longer takes part in the hash key, that is: 150 | * hash_shift * MIN_MATCH >= hash_bits 151 | */ 152 | 153 | long block_start; 154 | /* Window position at the beginning of the current output block. Gets 155 | * negative when the window is moved backwards. 156 | */ 157 | 158 | uInt match_length; /* length of best match */ 159 | IPos prev_match; /* previous match */ 160 | int match_available; /* set if previous match exists */ 161 | uInt strstart; /* start of string to insert */ 162 | uInt match_start; /* start of matching string */ 163 | uInt lookahead; /* number of valid bytes ahead in window */ 164 | 165 | uInt prev_length; 166 | /* Length of the best match at previous step. Matches not greater than this 167 | * are discarded. This is used in the lazy match evaluation. 168 | */ 169 | 170 | uInt max_chain_length; 171 | /* To speed up deflation, hash chains are never searched beyond this 172 | * length. A higher limit improves compression ratio but degrades the 173 | * speed. 174 | */ 175 | 176 | uInt max_lazy_match; 177 | /* Attempt to find a better match only when the current match is strictly 178 | * smaller than this value. This mechanism is used only for compression 179 | * levels >= 4. 180 | */ 181 | #define max_insert_length max_lazy_match 182 | /* Insert new strings in the hash table only if the match length is not 183 | * greater than this length. This saves time but degrades compression. 184 | * max_insert_length is used only for compression levels <= 3. 185 | */ 186 | 187 | int level; /* compression level (1..9) */ 188 | int strategy; /* favor or force Huffman coding*/ 189 | 190 | uInt good_match; 191 | /* Use a faster search when the previous match is longer than this */ 192 | 193 | int nice_match; /* Stop searching when current match exceeds this */ 194 | 195 | /* used by trees.c: */ 196 | /* Didn't use ct_data typedef below to suppress compiler warning */ 197 | struct ct_data_s dyn_ltree[HEAP_SIZE]; /* literal and length tree */ 198 | struct ct_data_s dyn_dtree[2 * D_CODES + 1]; /* distance tree */ 199 | struct ct_data_s bl_tree[2 * BL_CODES + 1]; /* Huffman tree for bit lengths */ 200 | 201 | struct tree_desc_s l_desc; /* desc. for literal tree */ 202 | struct tree_desc_s d_desc; /* desc. for distance tree */ 203 | struct tree_desc_s bl_desc; /* desc. for bit length tree */ 204 | 205 | ush bl_count[MAX_BITS + 1]; 206 | /* number of codes at each bit length for an optimal tree */ 207 | 208 | int heap[2 * L_CODES + 1]; /* heap used to build the Huffman trees */ 209 | int heap_len; /* number of elements in the heap */ 210 | int heap_max; /* element of largest frequency */ 211 | /* The sons of heap[n] are heap[2*n] and heap[2*n+1]. heap[0] is not used. 212 | * The same heap array is used to build all trees. 213 | */ 214 | 215 | uch depth[2 * L_CODES + 1]; 216 | /* Depth of each subtree used as tie breaker for trees of equal frequency 217 | */ 218 | 219 | uchf *l_buf; /* buffer for literals or lengths */ 220 | 221 | uInt lit_bufsize; 222 | /* Size of match buffer for literals/lengths. There are 4 reasons for 223 | * limiting lit_bufsize to 64K: 224 | * - frequencies can be kept in 16 bit counters 225 | * - if compression is not successful for the first block, all input 226 | * data is still in the window so we can still emit a stored block even 227 | * when input comes from standard input. (This can also be done for 228 | * all blocks if lit_bufsize is not greater than 32K.) 229 | * - if compression is not successful for a file smaller than 64K, we can 230 | * even emit a stored file instead of a stored block (saving 5 bytes). 231 | * This is applicable only for zip (not gzip or zlib). 232 | * - creating new Huffman trees less frequently may not provide fast 233 | * adaptation to changes in the input data statistics. (Take for 234 | * example a binary file with poorly compressible code followed by 235 | * a highly compressible string table.) Smaller buffer sizes give 236 | * fast adaptation but have of course the overhead of transmitting 237 | * trees more frequently. 238 | * - I can't count above 4 239 | */ 240 | 241 | uInt last_lit; /* running index in l_buf */ 242 | 243 | ushf *d_buf; 244 | /* Buffer for distances. To simplify the code, d_buf and l_buf have 245 | * the same number of elements. To use different lengths, an extra flag 246 | * array would be necessary. 247 | */ 248 | 249 | ulg opt_len; /* bit length of current block with optimal trees */ 250 | ulg static_len; /* bit length of current block with static trees */ 251 | uInt matches; /* number of string matches in current block */ 252 | uInt insert; /* bytes at end of window left to insert */ 253 | 254 | #ifdef DEBUG 255 | ulg compressed_len; /* total bit length of compressed file mod 2^32 */ 256 | ulg bits_sent; /* bit length of compressed data sent mod 2^32 */ 257 | #endif 258 | 259 | ush bi_buf; 260 | /* Output buffer. bits are inserted starting at the bottom (least 261 | * significant bits). 262 | */ 263 | int bi_valid; 264 | /* Number of valid bits in bi_buf. All bits above the last valid bit 265 | * are always zero. 266 | */ 267 | 268 | ulg high_water; 269 | /* High water mark offset in window for initialized bytes -- bytes above 270 | * this are set to zero in order to avoid memory check warnings when 271 | * longest match routines access bytes past the input. This is then 272 | * updated to the new high water mark. 273 | */ 274 | 275 | } FAR deflate_state; 276 | 277 | /* Output a byte on the stream. 278 | * IN assertion: there is enough room in pending_buf. 279 | */ 280 | #define put_byte(s, c) \ 281 | { s->pending_buf[s->pending++] = (c); } 282 | 283 | 284 | #define MIN_LOOKAHEAD (MAX_MATCH + MIN_MATCH + 1) 285 | /* Minimum amount of lookahead, except at the end of the input file. 286 | * See deflate.c for comments about the MIN_MATCH+1. 287 | */ 288 | 289 | #define MAX_DIST(s) ((s)->w_size - MIN_LOOKAHEAD) 290 | /* In order to simplify the code, particularly on 16 bit machines, match 291 | * distances are limited to MAX_DIST instead of WSIZE. 292 | */ 293 | 294 | #define WIN_INIT MAX_MATCH 295 | /* Number of bytes after end of data in window to initialize in order to avoid 296 | memory checker errors from longest match routines */ 297 | 298 | /* in trees.c */ 299 | void ZLIB_INTERNAL _tr_init OF((deflate_state * s)); 300 | int ZLIB_INTERNAL _tr_tally OF((deflate_state * s, unsigned dist, unsigned lc)); 301 | void ZLIB_INTERNAL _tr_flush_block OF((deflate_state * s, charf *buf, ulg stored_len, int last)); 302 | void ZLIB_INTERNAL _tr_flush_bits OF((deflate_state * s)); 303 | void ZLIB_INTERNAL _tr_align OF((deflate_state * s)); 304 | void ZLIB_INTERNAL _tr_stored_block OF((deflate_state * s, charf *buf, ulg stored_len, int last)); 305 | 306 | #define d_code(dist) ((dist) < 256 ? _dist_code[dist] : _dist_code[256 + ((dist) >> 7)]) 307 | /* Mapping from a distance to a distance code. dist is the distance - 1 and 308 | * must not have side effects. _dist_code[256] and _dist_code[257] are never 309 | * used. 310 | */ 311 | 312 | #ifndef DEBUG 313 | /* Inline versions of _tr_tally for speed: */ 314 | 315 | # if defined(GEN_TREES_H) || !defined(STDC) 316 | extern uch ZLIB_INTERNAL _length_code[]; 317 | extern uch ZLIB_INTERNAL _dist_code[]; 318 | # else 319 | extern const uch ZLIB_INTERNAL _length_code[]; 320 | extern const uch ZLIB_INTERNAL _dist_code[]; 321 | # endif 322 | 323 | # define _tr_tally_lit(s, c, flush) \ 324 | { \ 325 | uch cc = (c); \ 326 | s->d_buf[s->last_lit] = 0; \ 327 | s->l_buf[s->last_lit++] = cc; \ 328 | s->dyn_ltree[cc].Freq++; \ 329 | flush = (s->last_lit == s->lit_bufsize - 1); \ 330 | } 331 | # define _tr_tally_dist(s, distance, length, flush) \ 332 | { \ 333 | uch len = (length); \ 334 | ush dist = (distance); \ 335 | s->d_buf[s->last_lit] = dist; \ 336 | s->l_buf[s->last_lit++] = len; \ 337 | dist--; \ 338 | s->dyn_ltree[_length_code[len] + LITERALS + 1].Freq++; \ 339 | s->dyn_dtree[d_code(dist)].Freq++; \ 340 | flush = (s->last_lit == s->lit_bufsize - 1); \ 341 | } 342 | # else 343 | # define _tr_tally_lit(s, c, flush) flush = _tr_tally(s, 0, c) 344 | # define _tr_tally_dist(s, distance, length, flush) flush = _tr_tally(s, distance, length) 345 | # endif 346 | 347 | #endif /* DEFLATE_H */ 348 | -------------------------------------------------------------------------------- /contrib/zlib/gzclose.c: -------------------------------------------------------------------------------- 1 | /* gzclose.c -- zlib gzclose() function 2 | * Copyright (C) 2004, 2010 Mark Adler 3 | * For conditions of distribution and use, see copyright notice in zlib.h 4 | */ 5 | 6 | #include "gzguts.h" 7 | 8 | /* gzclose() is in a separate file so that it is linked in only if it is used. 9 | That way the other gzclose functions can be used instead to avoid linking in 10 | unneeded compression or decompression routines. */ 11 | int ZEXPORT gzclose(file) 12 | gzFile file; 13 | { 14 | #ifndef NO_GZCOMPRESS 15 | gz_statep state; 16 | 17 | if (file == NULL) 18 | return Z_STREAM_ERROR; 19 | state = (gz_statep)file; 20 | 21 | return state->mode == GZ_READ ? gzclose_r(file) : gzclose_w(file); 22 | #else 23 | return gzclose_r(file); 24 | #endif 25 | } 26 | -------------------------------------------------------------------------------- /contrib/zlib/gzguts.h: -------------------------------------------------------------------------------- 1 | /* gzguts.h -- zlib internal header definitions for gz* operations 2 | * Copyright (C) 2004, 2005, 2010, 2011, 2012, 2013 Mark Adler 3 | * For conditions of distribution and use, see copyright notice in zlib.h 4 | */ 5 | 6 | #ifdef _LARGEFILE64_SOURCE 7 | # ifndef _LARGEFILE_SOURCE 8 | # define _LARGEFILE_SOURCE 1 9 | # endif 10 | # ifdef _FILE_OFFSET_BITS 11 | # undef _FILE_OFFSET_BITS 12 | # endif 13 | #endif 14 | 15 | #ifdef HAVE_HIDDEN 16 | # define ZLIB_INTERNAL __attribute__((visibility("hidden"))) 17 | #else 18 | # define ZLIB_INTERNAL 19 | #endif 20 | 21 | #include 22 | #include "zlib.h" 23 | #ifdef STDC 24 | # include 25 | # include 26 | # include 27 | #endif 28 | #include 29 | 30 | #ifdef _WIN32 31 | # include 32 | #endif 33 | 34 | #if defined(__TURBOC__) || defined(_MSC_VER) || defined(_WIN32) 35 | # include 36 | #endif 37 | 38 | #ifdef WINAPI_FAMILY 39 | # define open _open 40 | # define read _read 41 | # define write _write 42 | # define close _close 43 | #endif 44 | 45 | #ifdef NO_DEFLATE /* for compatibility with old definition */ 46 | # define NO_GZCOMPRESS 47 | #endif 48 | 49 | #if defined(STDC99) || (defined(__TURBOC__) && __TURBOC__ >= 0x550) 50 | # ifndef HAVE_VSNPRINTF 51 | # define HAVE_VSNPRINTF 52 | # endif 53 | #endif 54 | 55 | #if defined(__CYGWIN__) 56 | # ifndef HAVE_VSNPRINTF 57 | # define HAVE_VSNPRINTF 58 | # endif 59 | #endif 60 | 61 | #if defined(MSDOS) && defined(__BORLANDC__) && (BORLANDC > 0x410) 62 | # ifndef HAVE_VSNPRINTF 63 | # define HAVE_VSNPRINTF 64 | # endif 65 | #endif 66 | 67 | #ifndef HAVE_VSNPRINTF 68 | # ifdef MSDOS 69 | /* vsnprintf may exist on some MS-DOS compilers (DJGPP?), 70 | but for now we just assume it doesn't. */ 71 | # define NO_vsnprintf 72 | # endif 73 | # ifdef __TURBOC__ 74 | # define NO_vsnprintf 75 | # endif 76 | # ifdef WIN32 77 | /* In Win32, vsnprintf is available as the "non-ANSI" _vsnprintf. */ 78 | # if !defined(vsnprintf) && !defined(NO_vsnprintf) 79 | # if !defined(_MSC_VER) || (defined(_MSC_VER) && _MSC_VER < 1500) 80 | # define vsnprintf _vsnprintf 81 | # endif 82 | # endif 83 | # endif 84 | # ifdef __SASC 85 | # define NO_vsnprintf 86 | # endif 87 | # ifdef VMS 88 | # define NO_vsnprintf 89 | # endif 90 | # ifdef __OS400__ 91 | # define NO_vsnprintf 92 | # endif 93 | # ifdef __MVS__ 94 | # define NO_vsnprintf 95 | # endif 96 | #endif 97 | 98 | /* unlike snprintf (which is required in C99, yet still not supported by 99 | Microsoft more than a decade later!), _snprintf does not guarantee null 100 | termination of the result -- however this is only used in gzlib.c where 101 | the result is assured to fit in the space provided */ 102 | #ifdef _MSC_VER 103 | # define snprintf _snprintf 104 | #endif 105 | 106 | #ifndef local 107 | # define local static 108 | #endif 109 | /* compile with -Dlocal if your debugger can't find static symbols */ 110 | 111 | /* gz* functions always use library allocation functions */ 112 | #ifndef STDC 113 | extern voidp malloc OF((uInt size)); 114 | extern void free OF((voidpf ptr)); 115 | #endif 116 | 117 | /* get errno and strerror definition */ 118 | #if defined UNDER_CE 119 | # include 120 | # define zstrerror() gz_strwinerror((DWORD)GetLastError()) 121 | #else 122 | # ifndef NO_STRERROR 123 | # include 124 | # define zstrerror() strerror(errno) 125 | # else 126 | # define zstrerror() "stdio error (consult errno)" 127 | # endif 128 | #endif 129 | 130 | /* provide prototypes for these when building zlib without LFS */ 131 | #if !defined(_LARGEFILE64_SOURCE) || _LFS64_LARGEFILE - 0 == 0 132 | ZEXTERN gzFile ZEXPORT gzopen64 OF((const char *, const char *)); 133 | ZEXTERN z_off64_t ZEXPORT gzseek64 OF((gzFile, z_off64_t, int)); 134 | ZEXTERN z_off64_t ZEXPORT gztell64 OF((gzFile)); 135 | ZEXTERN z_off64_t ZEXPORT gzoffset64 OF((gzFile)); 136 | #endif 137 | 138 | /* default memLevel */ 139 | #if MAX_MEM_LEVEL >= 8 140 | # define DEF_MEM_LEVEL 8 141 | #else 142 | # define DEF_MEM_LEVEL MAX_MEM_LEVEL 143 | #endif 144 | 145 | /* default i/o buffer size -- double this for output when reading (this and 146 | twice this must be able to fit in an unsigned type) */ 147 | #define GZBUFSIZE 8192 148 | 149 | /* gzip modes, also provide a little integrity check on the passed structure */ 150 | #define GZ_NONE 0 151 | #define GZ_READ 7247 152 | #define GZ_WRITE 31153 153 | #define GZ_APPEND 1 /* mode set to GZ_WRITE after the file is opened */ 154 | 155 | /* values for gz_state how */ 156 | #define LOOK 0 /* look for a gzip header */ 157 | #define COPY 1 /* copy input directly */ 158 | #define GZIP 2 /* decompress a gzip stream */ 159 | 160 | /* internal gzip file state data structure */ 161 | typedef struct { 162 | /* exposed contents for gzgetc() macro */ 163 | struct gzFile_s x; /* "x" for exposed */ 164 | /* x.have: number of bytes available at x.next */ 165 | /* x.next: next output data to deliver or write */ 166 | /* x.pos: current position in uncompressed data */ 167 | /* used for both reading and writing */ 168 | int mode; /* see gzip modes above */ 169 | int fd; /* file descriptor */ 170 | char *path; /* path or fd for error messages */ 171 | unsigned size; /* buffer size, zero if not allocated yet */ 172 | unsigned want; /* requested buffer size, default is GZBUFSIZE */ 173 | unsigned char *in; /* input buffer */ 174 | unsigned char *out; /* output buffer (double-sized when reading) */ 175 | int direct; /* 0 if processing gzip, 1 if transparent */ 176 | /* just for reading */ 177 | int how; /* 0: get header, 1: copy, 2: decompress */ 178 | z_off64_t start; /* where the gzip data started, for rewinding */ 179 | int eof; /* true if end of input file reached */ 180 | int past; /* true if read requested past end */ 181 | /* just for writing */ 182 | int level; /* compression level */ 183 | int strategy; /* compression strategy */ 184 | /* seek request */ 185 | z_off64_t skip; /* amount to skip (already rewound if backwards) */ 186 | int seek; /* true if seek request pending */ 187 | /* error information */ 188 | int err; /* error code */ 189 | char *msg; /* error message */ 190 | /* zlib inflate or deflate stream */ 191 | z_stream strm; /* stream structure in-place (not a pointer) */ 192 | } gz_state; 193 | typedef gz_state FAR *gz_statep; 194 | 195 | /* shared functions */ 196 | void ZLIB_INTERNAL gz_error OF((gz_statep, int, const char *)); 197 | #if defined UNDER_CE 198 | char ZLIB_INTERNAL *gz_strwinerror OF((DWORD error)); 199 | #endif 200 | 201 | /* GT_OFF(x), where x is an unsigned value, is true if x > maximum z_off64_t 202 | value -- needed when comparing unsigned to z_off64_t, which is signed 203 | (possible z_off64_t types off_t, off64_t, and long are all signed) */ 204 | #ifdef INT_MAX 205 | # define GT_OFF(x) (sizeof(int) == sizeof(z_off64_t) && (x) > INT_MAX) 206 | #else 207 | unsigned ZLIB_INTERNAL gz_intmax OF((void)); 208 | # define GT_OFF(x) (sizeof(int) == sizeof(z_off64_t) && (x) > gz_intmax()) 209 | #endif 210 | -------------------------------------------------------------------------------- /contrib/zlib/inffast.c: -------------------------------------------------------------------------------- 1 | /* inffast.c -- fast decoding 2 | * Copyright (C) 1995-2008, 2010, 2013 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 ZLIB_INTERNAL 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 | z_const unsigned char FAR *in; /* local strm->next_in */ 73 | z_const unsigned char FAR *last; /* have enough input while in < last */ 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 wnext; /* 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 here; /* 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 | wnext = state->wnext; 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 | here = lcode[hold & lmask]; 128 | dolen: 129 | op = (unsigned)(here.bits); 130 | hold >>= op; 131 | bits -= op; 132 | op = (unsigned)(here.op); 133 | if (op == 0) { /* literal */ 134 | Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ? 135 | "inflate: literal '%c'\n" : 136 | "inflate: literal 0x%02x\n", here.val)); 137 | PUP(out) = (unsigned char)(here.val); 138 | } 139 | else if (op & 16) { /* length base */ 140 | len = (unsigned)(here.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 | here = dcode[hold & dmask]; 159 | dodist: 160 | op = (unsigned)(here.bits); 161 | hold >>= op; 162 | bits -= op; 163 | op = (unsigned)(here.op); 164 | if (op & 16) { /* distance base */ 165 | dist = (unsigned)(here.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 | if (state->sane) { 191 | strm->msg = 192 | (char *)"invalid distance too far back"; 193 | state->mode = BAD; 194 | break; 195 | } 196 | #ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR 197 | if (len <= op - whave) { 198 | do { 199 | PUP(out) = 0; 200 | } while (--len); 201 | continue; 202 | } 203 | len -= op - whave; 204 | do { 205 | PUP(out) = 0; 206 | } while (--op > whave); 207 | if (op == 0) { 208 | from = out - dist; 209 | do { 210 | PUP(out) = PUP(from); 211 | } while (--len); 212 | continue; 213 | } 214 | #endif 215 | } 216 | from = window - OFF; 217 | if (wnext == 0) { /* very common case */ 218 | from += wsize - op; 219 | if (op < len) { /* some from window */ 220 | len -= op; 221 | do { 222 | PUP(out) = PUP(from); 223 | } while (--op); 224 | from = out - dist; /* rest from output */ 225 | } 226 | } 227 | else if (wnext < op) { /* wrap around window */ 228 | from += wsize + wnext - op; 229 | op -= wnext; 230 | if (op < len) { /* some from end of window */ 231 | len -= op; 232 | do { 233 | PUP(out) = PUP(from); 234 | } while (--op); 235 | from = window - OFF; 236 | if (wnext < len) { /* some from start of window */ 237 | op = wnext; 238 | len -= op; 239 | do { 240 | PUP(out) = PUP(from); 241 | } while (--op); 242 | from = out - dist; /* rest from output */ 243 | } 244 | } 245 | } 246 | else { /* contiguous in window */ 247 | from += wnext - op; 248 | if (op < len) { /* some from window */ 249 | len -= op; 250 | do { 251 | PUP(out) = PUP(from); 252 | } while (--op); 253 | from = out - dist; /* rest from output */ 254 | } 255 | } 256 | while (len > 2) { 257 | PUP(out) = PUP(from); 258 | PUP(out) = PUP(from); 259 | PUP(out) = PUP(from); 260 | len -= 3; 261 | } 262 | if (len) { 263 | PUP(out) = PUP(from); 264 | if (len > 1) 265 | PUP(out) = PUP(from); 266 | } 267 | } 268 | else { 269 | from = out - dist; /* copy direct from output */ 270 | do { /* minimum length is three */ 271 | PUP(out) = PUP(from); 272 | PUP(out) = PUP(from); 273 | PUP(out) = PUP(from); 274 | len -= 3; 275 | } while (len > 2); 276 | if (len) { 277 | PUP(out) = PUP(from); 278 | if (len > 1) 279 | PUP(out) = PUP(from); 280 | } 281 | } 282 | } 283 | else if ((op & 64) == 0) { /* 2nd level distance code */ 284 | here = dcode[here.val + (hold & ((1U << op) - 1))]; 285 | goto dodist; 286 | } 287 | else { 288 | strm->msg = (char *)"invalid distance code"; 289 | state->mode = BAD; 290 | break; 291 | } 292 | } 293 | else if ((op & 64) == 0) { /* 2nd level length code */ 294 | here = lcode[here.val + (hold & ((1U << op) - 1))]; 295 | goto dolen; 296 | } 297 | else if (op & 32) { /* end-of-block */ 298 | Tracevv((stderr, "inflate: end of block\n")); 299 | state->mode = TYPE; 300 | break; 301 | } 302 | else { 303 | strm->msg = (char *)"invalid literal/length code"; 304 | state->mode = BAD; 305 | break; 306 | } 307 | } while (in < last && out < end); 308 | 309 | /* return unused bytes (on entry, bits < 8, so in won't go too far back) */ 310 | len = bits >> 3; 311 | in -= len; 312 | bits -= len << 3; 313 | hold &= (1U << bits) - 1; 314 | 315 | /* update state and return */ 316 | strm->next_in = in + OFF; 317 | strm->next_out = out + OFF; 318 | strm->avail_in = (unsigned)(in < last ? 5 + (last - in) : 5 - (in - last)); 319 | strm->avail_out = (unsigned)(out < end ? 320 | 257 + (end - out) : 257 - (out - end)); 321 | state->hold = hold; 322 | state->bits = bits; 323 | return; 324 | } 325 | 326 | /* 327 | inflate_fast() speedups that turned out slower (on a PowerPC G3 750CXe): 328 | - Using bit fields for code structure 329 | - Different op definition to avoid & for extra bits (do & for table bits) 330 | - Three separate decoding do-loops for direct, window, and wnext == 0 331 | - Special case for distance > 1 copies to do overlapped load and store copy 332 | - Explicit branch predictions (based on measured branch probabilities) 333 | - Deferring match copy and interspersed it with decoding subsequent codes 334 | - Swapping literal/length else 335 | - Swapping window/direct else 336 | - Larger unrolled copy loops (three is about right) 337 | - Moving len -= 3 statement into middle of loop 338 | */ 339 | 340 | #endif /* !ASMINF */ 341 | -------------------------------------------------------------------------------- /contrib/zlib/inffast.h: -------------------------------------------------------------------------------- 1 | /* inffast.h -- header to use inffast.c 2 | * Copyright (C) 1995-2003, 2010 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 ZLIB_INTERNAL inflate_fast OF((z_streamp strm, unsigned start)); 12 | -------------------------------------------------------------------------------- /contrib/zlib/inffixed.h: -------------------------------------------------------------------------------- 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. 6 | It is part of the implementation of this library and is 7 | 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 }, { 0, 9, 192 }, { 16, 7, 10 }, 12 | { 0, 8, 96 }, { 0, 8, 32 }, { 0, 9, 160 }, { 0, 8, 0 }, { 0, 8, 128 }, { 0, 8, 64 }, { 0, 9, 224 }, { 16, 7, 6 }, { 0, 8, 88 }, 13 | { 0, 8, 24 }, { 0, 9, 144 }, { 19, 7, 59 }, { 0, 8, 120 }, { 0, 8, 56 }, { 0, 9, 208 }, { 17, 7, 17 }, { 0, 8, 104 }, { 0, 8, 40 }, 14 | { 0, 9, 176 }, { 0, 8, 8 }, { 0, 8, 136 }, { 0, 8, 72 }, { 0, 9, 240 }, { 16, 7, 4 }, { 0, 8, 84 }, { 0, 8, 20 }, { 21, 8, 227 }, 15 | { 19, 7, 43 }, { 0, 8, 116 }, { 0, 8, 52 }, { 0, 9, 200 }, { 17, 7, 13 }, { 0, 8, 100 }, { 0, 8, 36 }, { 0, 9, 168 }, { 0, 8, 4 }, 16 | { 0, 8, 132 }, { 0, 8, 68 }, { 0, 9, 232 }, { 16, 7, 8 }, { 0, 8, 92 }, { 0, 8, 28 }, { 0, 9, 152 }, { 20, 7, 83 }, { 0, 8, 124 }, 17 | { 0, 8, 60 }, { 0, 9, 216 }, { 18, 7, 23 }, { 0, 8, 108 }, { 0, 8, 44 }, { 0, 9, 184 }, { 0, 8, 12 }, { 0, 8, 140 }, { 0, 8, 76 }, 18 | { 0, 9, 248 }, { 16, 7, 3 }, { 0, 8, 82 }, { 0, 8, 18 }, { 21, 8, 163 }, { 19, 7, 35 }, { 0, 8, 114 }, { 0, 8, 50 }, { 0, 9, 196 }, 19 | { 17, 7, 11 }, { 0, 8, 98 }, { 0, 8, 34 }, { 0, 9, 164 }, { 0, 8, 2 }, { 0, 8, 130 }, { 0, 8, 66 }, { 0, 9, 228 }, { 16, 7, 7 }, 20 | { 0, 8, 90 }, { 0, 8, 26 }, { 0, 9, 148 }, { 20, 7, 67 }, { 0, 8, 122 }, { 0, 8, 58 }, { 0, 9, 212 }, { 18, 7, 19 }, { 0, 8, 106 }, 21 | { 0, 8, 42 }, { 0, 9, 180 }, { 0, 8, 10 }, { 0, 8, 138 }, { 0, 8, 74 }, { 0, 9, 244 }, { 16, 7, 5 }, { 0, 8, 86 }, { 0, 8, 22 }, 22 | { 64, 8, 0 }, { 19, 7, 51 }, { 0, 8, 118 }, { 0, 8, 54 }, { 0, 9, 204 }, { 17, 7, 15 }, { 0, 8, 102 }, { 0, 8, 38 }, { 0, 9, 172 }, 23 | { 0, 8, 6 }, { 0, 8, 134 }, { 0, 8, 70 }, { 0, 9, 236 }, { 16, 7, 9 }, { 0, 8, 94 }, { 0, 8, 30 }, { 0, 9, 156 }, { 20, 7, 99 }, 24 | { 0, 8, 126 }, { 0, 8, 62 }, { 0, 9, 220 }, { 18, 7, 27 }, { 0, 8, 110 }, { 0, 8, 46 }, { 0, 9, 188 }, { 0, 8, 14 }, { 0, 8, 142 }, 25 | { 0, 8, 78 }, { 0, 9, 252 }, { 96, 7, 0 }, { 0, 8, 81 }, { 0, 8, 17 }, { 21, 8, 131 }, { 18, 7, 31 }, { 0, 8, 113 }, { 0, 8, 49 }, 26 | { 0, 9, 194 }, { 16, 7, 10 }, { 0, 8, 97 }, { 0, 8, 33 }, { 0, 9, 162 }, { 0, 8, 1 }, { 0, 8, 129 }, { 0, 8, 65 }, { 0, 9, 226 }, 27 | { 16, 7, 6 }, { 0, 8, 89 }, { 0, 8, 25 }, { 0, 9, 146 }, { 19, 7, 59 }, { 0, 8, 121 }, { 0, 8, 57 }, { 0, 9, 210 }, { 17, 7, 17 }, 28 | { 0, 8, 105 }, { 0, 8, 41 }, { 0, 9, 178 }, { 0, 8, 9 }, { 0, 8, 137 }, { 0, 8, 73 }, { 0, 9, 242 }, { 16, 7, 4 }, { 0, 8, 85 }, 29 | { 0, 8, 21 }, { 16, 8, 258 }, { 19, 7, 43 }, { 0, 8, 117 }, { 0, 8, 53 }, { 0, 9, 202 }, { 17, 7, 13 }, { 0, 8, 101 }, { 0, 8, 37 }, 30 | { 0, 9, 170 }, { 0, 8, 5 }, { 0, 8, 133 }, { 0, 8, 69 }, { 0, 9, 234 }, { 16, 7, 8 }, { 0, 8, 93 }, { 0, 8, 29 }, { 0, 9, 154 }, 31 | { 20, 7, 83 }, { 0, 8, 125 }, { 0, 8, 61 }, { 0, 9, 218 }, { 18, 7, 23 }, { 0, 8, 109 }, { 0, 8, 45 }, { 0, 9, 186 }, { 0, 8, 13 }, 32 | { 0, 8, 141 }, { 0, 8, 77 }, { 0, 9, 250 }, { 16, 7, 3 }, { 0, 8, 83 }, { 0, 8, 19 }, { 21, 8, 195 }, { 19, 7, 35 }, { 0, 8, 115 }, 33 | { 0, 8, 51 }, { 0, 9, 198 }, { 17, 7, 11 }, { 0, 8, 99 }, { 0, 8, 35 }, { 0, 9, 166 }, { 0, 8, 3 }, { 0, 8, 131 }, { 0, 8, 67 }, 34 | { 0, 9, 230 }, { 16, 7, 7 }, { 0, 8, 91 }, { 0, 8, 27 }, { 0, 9, 150 }, { 20, 7, 67 }, { 0, 8, 123 }, { 0, 8, 59 }, { 0, 9, 214 }, 35 | { 18, 7, 19 }, { 0, 8, 107 }, { 0, 8, 43 }, { 0, 9, 182 }, { 0, 8, 11 }, { 0, 8, 139 }, { 0, 8, 75 }, { 0, 9, 246 }, { 16, 7, 5 }, 36 | { 0, 8, 87 }, { 0, 8, 23 }, { 64, 8, 0 }, { 19, 7, 51 }, { 0, 8, 119 }, { 0, 8, 55 }, { 0, 9, 206 }, { 17, 7, 15 }, { 0, 8, 103 }, 37 | { 0, 8, 39 }, { 0, 9, 174 }, { 0, 8, 7 }, { 0, 8, 135 }, { 0, 8, 71 }, { 0, 9, 238 }, { 16, 7, 9 }, { 0, 8, 95 }, { 0, 8, 31 }, 38 | { 0, 9, 158 }, { 20, 7, 99 }, { 0, 8, 127 }, { 0, 8, 63 }, { 0, 9, 222 }, { 18, 7, 27 }, { 0, 8, 111 }, { 0, 8, 47 }, { 0, 9, 190 }, 39 | { 0, 8, 15 }, { 0, 8, 143 }, { 0, 8, 79 }, { 0, 9, 254 }, { 96, 7, 0 }, { 0, 8, 80 }, { 0, 8, 16 }, { 20, 8, 115 }, { 18, 7, 31 }, 40 | { 0, 8, 112 }, { 0, 8, 48 }, { 0, 9, 193 }, { 16, 7, 10 }, { 0, 8, 96 }, { 0, 8, 32 }, { 0, 9, 161 }, { 0, 8, 0 }, { 0, 8, 128 }, 41 | { 0, 8, 64 }, { 0, 9, 225 }, { 16, 7, 6 }, { 0, 8, 88 }, { 0, 8, 24 }, { 0, 9, 145 }, { 19, 7, 59 }, { 0, 8, 120 }, { 0, 8, 56 }, 42 | { 0, 9, 209 }, { 17, 7, 17 }, { 0, 8, 104 }, { 0, 8, 40 }, { 0, 9, 177 }, { 0, 8, 8 }, { 0, 8, 136 }, { 0, 8, 72 }, { 0, 9, 241 }, 43 | { 16, 7, 4 }, { 0, 8, 84 }, { 0, 8, 20 }, { 21, 8, 227 }, { 19, 7, 43 }, { 0, 8, 116 }, { 0, 8, 52 }, { 0, 9, 201 }, { 17, 7, 13 }, 44 | { 0, 8, 100 }, { 0, 8, 36 }, { 0, 9, 169 }, { 0, 8, 4 }, { 0, 8, 132 }, { 0, 8, 68 }, { 0, 9, 233 }, { 16, 7, 8 }, { 0, 8, 92 }, 45 | { 0, 8, 28 }, { 0, 9, 153 }, { 20, 7, 83 }, { 0, 8, 124 }, { 0, 8, 60 }, { 0, 9, 217 }, { 18, 7, 23 }, { 0, 8, 108 }, { 0, 8, 44 }, 46 | { 0, 9, 185 }, { 0, 8, 12 }, { 0, 8, 140 }, { 0, 8, 76 }, { 0, 9, 249 }, { 16, 7, 3 }, { 0, 8, 82 }, { 0, 8, 18 }, { 21, 8, 163 }, 47 | { 19, 7, 35 }, { 0, 8, 114 }, { 0, 8, 50 }, { 0, 9, 197 }, { 17, 7, 11 }, { 0, 8, 98 }, { 0, 8, 34 }, { 0, 9, 165 }, { 0, 8, 2 }, 48 | { 0, 8, 130 }, { 0, 8, 66 }, { 0, 9, 229 }, { 16, 7, 7 }, { 0, 8, 90 }, { 0, 8, 26 }, { 0, 9, 149 }, { 20, 7, 67 }, { 0, 8, 122 }, 49 | { 0, 8, 58 }, { 0, 9, 213 }, { 18, 7, 19 }, { 0, 8, 106 }, { 0, 8, 42 }, { 0, 9, 181 }, { 0, 8, 10 }, { 0, 8, 138 }, { 0, 8, 74 }, 50 | { 0, 9, 245 }, { 16, 7, 5 }, { 0, 8, 86 }, { 0, 8, 22 }, { 64, 8, 0 }, { 19, 7, 51 }, { 0, 8, 118 }, { 0, 8, 54 }, { 0, 9, 205 }, 51 | { 17, 7, 15 }, { 0, 8, 102 }, { 0, 8, 38 }, { 0, 9, 173 }, { 0, 8, 6 }, { 0, 8, 134 }, { 0, 8, 70 }, { 0, 9, 237 }, { 16, 7, 9 }, 52 | { 0, 8, 94 }, { 0, 8, 30 }, { 0, 9, 157 }, { 20, 7, 99 }, { 0, 8, 126 }, { 0, 8, 62 }, { 0, 9, 221 }, { 18, 7, 27 }, { 0, 8, 110 }, 53 | { 0, 8, 46 }, { 0, 9, 189 }, { 0, 8, 14 }, { 0, 8, 142 }, { 0, 8, 78 }, { 0, 9, 253 }, { 96, 7, 0 }, { 0, 8, 81 }, { 0, 8, 17 }, 54 | { 21, 8, 131 }, { 18, 7, 31 }, { 0, 8, 113 }, { 0, 8, 49 }, { 0, 9, 195 }, { 16, 7, 10 }, { 0, 8, 97 }, { 0, 8, 33 }, { 0, 9, 163 }, 55 | { 0, 8, 1 }, { 0, 8, 129 }, { 0, 8, 65 }, { 0, 9, 227 }, { 16, 7, 6 }, { 0, 8, 89 }, { 0, 8, 25 }, { 0, 9, 147 }, { 19, 7, 59 }, 56 | { 0, 8, 121 }, { 0, 8, 57 }, { 0, 9, 211 }, { 17, 7, 17 }, { 0, 8, 105 }, { 0, 8, 41 }, { 0, 9, 179 }, { 0, 8, 9 }, { 0, 8, 137 }, 57 | { 0, 8, 73 }, { 0, 9, 243 }, { 16, 7, 4 }, { 0, 8, 85 }, { 0, 8, 21 }, { 16, 8, 258 }, { 19, 7, 43 }, { 0, 8, 117 }, { 0, 8, 53 }, 58 | { 0, 9, 203 }, { 17, 7, 13 }, { 0, 8, 101 }, { 0, 8, 37 }, { 0, 9, 171 }, { 0, 8, 5 }, { 0, 8, 133 }, { 0, 8, 69 }, { 0, 9, 235 }, 59 | { 16, 7, 8 }, { 0, 8, 93 }, { 0, 8, 29 }, { 0, 9, 155 }, { 20, 7, 83 }, { 0, 8, 125 }, { 0, 8, 61 }, { 0, 9, 219 }, { 18, 7, 23 }, 60 | { 0, 8, 109 }, { 0, 8, 45 }, { 0, 9, 187 }, { 0, 8, 13 }, { 0, 8, 141 }, { 0, 8, 77 }, { 0, 9, 251 }, { 16, 7, 3 }, { 0, 8, 83 }, 61 | { 0, 8, 19 }, { 21, 8, 195 }, { 19, 7, 35 }, { 0, 8, 115 }, { 0, 8, 51 }, { 0, 9, 199 }, { 17, 7, 11 }, { 0, 8, 99 }, { 0, 8, 35 }, 62 | { 0, 9, 167 }, { 0, 8, 3 }, { 0, 8, 131 }, { 0, 8, 67 }, { 0, 9, 231 }, { 16, 7, 7 }, { 0, 8, 91 }, { 0, 8, 27 }, { 0, 9, 151 }, 63 | { 20, 7, 67 }, { 0, 8, 123 }, { 0, 8, 59 }, { 0, 9, 215 }, { 18, 7, 19 }, { 0, 8, 107 }, { 0, 8, 43 }, { 0, 9, 183 }, { 0, 8, 11 }, 64 | { 0, 8, 139 }, { 0, 8, 75 }, { 0, 9, 247 }, { 16, 7, 5 }, { 0, 8, 87 }, { 0, 8, 23 }, { 64, 8, 0 }, { 19, 7, 51 }, { 0, 8, 119 }, 65 | { 0, 8, 55 }, { 0, 9, 207 }, { 17, 7, 15 }, { 0, 8, 103 }, { 0, 8, 39 }, { 0, 9, 175 }, { 0, 8, 7 }, { 0, 8, 135 }, { 0, 8, 71 }, 66 | { 0, 9, 239 }, { 16, 7, 9 }, { 0, 8, 95 }, { 0, 8, 31 }, { 0, 9, 159 }, { 20, 7, 99 }, { 0, 8, 127 }, { 0, 8, 63 }, { 0, 9, 223 }, 67 | { 18, 7, 27 }, { 0, 8, 111 }, { 0, 8, 47 }, { 0, 9, 191 }, { 0, 8, 15 }, { 0, 8, 143 }, { 0, 8, 79 }, { 0, 9, 255 } 68 | }; 69 | 70 | static const code distfix[32] = { { 16, 5, 1 }, { 23, 5, 257 }, { 19, 5, 17 }, { 27, 5, 4097 }, { 17, 5, 5 }, { 25, 5, 1025 }, 71 | { 21, 5, 65 }, { 29, 5, 16385 }, { 16, 5, 3 }, { 24, 5, 513 }, { 20, 5, 33 }, { 28, 5, 8193 }, 72 | { 18, 5, 9 }, { 26, 5, 2049 }, { 22, 5, 129 }, { 64, 5, 0 }, { 16, 5, 2 }, { 23, 5, 385 }, 73 | { 19, 5, 25 }, { 27, 5, 6145 }, { 17, 5, 7 }, { 25, 5, 1537 }, { 21, 5, 97 }, { 29, 5, 24577 }, 74 | { 16, 5, 4 }, { 24, 5, 769 }, { 20, 5, 49 }, { 28, 5, 12289 }, { 18, 5, 13 }, { 26, 5, 3073 }, 75 | { 22, 5, 193 }, { 64, 5, 0 } }; 76 | -------------------------------------------------------------------------------- /contrib/zlib/inflate.h: -------------------------------------------------------------------------------- 1 | /* inflate.h -- internal inflate state definition 2 | * Copyright (C) 1995-2009 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 | { 22 | HEAD, /* i: waiting for magic header */ 23 | FLAGS, /* i: waiting for method and flags (gzip) */ 24 | TIME, /* i: waiting for modification time (gzip) */ 25 | OS, /* i: waiting for extra flags and operating system (gzip) */ 26 | EXLEN, /* i: waiting for extra length (gzip) */ 27 | EXTRA, /* i: waiting for extra bytes (gzip) */ 28 | NAME, /* i: waiting for end of file name (gzip) */ 29 | COMMENT, /* i: waiting for end of comment (gzip) */ 30 | HCRC, /* i: waiting for header crc (gzip) */ 31 | DICTID, /* i: waiting for dictionary check value */ 32 | DICT, /* waiting for inflateSetDictionary() call */ 33 | TYPE, /* i: waiting for type bits, including last-flag bit */ 34 | TYPEDO, /* i: same, but skip check to exit inflate on new block */ 35 | STORED, /* i: waiting for stored size (length and complement) */ 36 | COPY_, /* i/o: same as COPY below, but only first time in */ 37 | COPY, /* i/o: waiting for input or output to copy stored block */ 38 | TABLE, /* i: waiting for dynamic block table lengths */ 39 | LENLENS, /* i: waiting for code length code lengths */ 40 | CODELENS, /* i: waiting for length/lit and distance code lengths */ 41 | LEN_, /* i: same as LEN below, but only first time in */ 42 | LEN, /* i: waiting for length/lit/eob code */ 43 | LENEXT, /* i: waiting for length extra bits */ 44 | DIST, /* i: waiting for distance code */ 45 | DISTEXT, /* i: waiting for distance extra bits */ 46 | MATCH, /* o: waiting for output space to copy string */ 47 | LIT, /* o: waiting for output space to write literal */ 48 | CHECK, /* i: waiting for 32-bit check value */ 49 | LENGTH, /* i: waiting for 32-bit length (gzip) */ 50 | DONE, /* finished check, done -- remain here until reset */ 51 | BAD, /* got a data error -- remain here until reset */ 52 | MEM, /* got an inflate() memory error -- remain here until reset */ 53 | SYNC /* looking for synchronization bytes to restart inflate() */ 54 | } inflate_mode; 55 | 56 | /* 57 | State transitions between above modes - 58 | 59 | (most modes can go to BAD or MEM on error -- not shown for clarity) 60 | 61 | Process header: 62 | HEAD -> (gzip) or (zlib) or (raw) 63 | (gzip) -> FLAGS -> TIME -> OS -> EXLEN -> EXTRA -> NAME -> COMMENT -> 64 | HCRC -> TYPE 65 | (zlib) -> DICTID or TYPE 66 | DICTID -> DICT -> TYPE 67 | (raw) -> TYPEDO 68 | Read deflate blocks: 69 | TYPE -> TYPEDO -> STORED or TABLE or LEN_ or CHECK 70 | STORED -> COPY_ -> COPY -> TYPE 71 | TABLE -> LENLENS -> CODELENS -> LEN_ 72 | LEN_ -> LEN 73 | Read deflate codes in fixed or dynamic block: 74 | LEN -> LENEXT or LIT or TYPE 75 | LENEXT -> DIST -> DISTEXT -> MATCH -> LEN 76 | LIT -> LEN 77 | Process trailer: 78 | CHECK -> LENGTH -> DONE 79 | */ 80 | 81 | /* state maintained between inflate() calls. Approximately 10K bytes. */ 82 | struct inflate_state { 83 | inflate_mode mode; /* current inflate mode */ 84 | int last; /* true if processing last block */ 85 | int wrap; /* bit 0 true for zlib, bit 1 true for gzip */ 86 | int havedict; /* true if dictionary provided */ 87 | int flags; /* gzip header method and flags (0 if zlib) */ 88 | unsigned dmax; /* zlib header max distance (INFLATE_STRICT) */ 89 | unsigned long check; /* protected copy of check value */ 90 | unsigned long total; /* protected copy of output count */ 91 | gz_headerp head; /* where to save gzip header information */ 92 | /* sliding window */ 93 | unsigned wbits; /* log base 2 of requested window size */ 94 | unsigned wsize; /* window size or zero if not using window */ 95 | unsigned whave; /* valid bytes in the window */ 96 | unsigned wnext; /* window write index */ 97 | unsigned char FAR *window; /* allocated sliding window, if needed */ 98 | /* bit accumulator */ 99 | unsigned long hold; /* input bit accumulator */ 100 | unsigned bits; /* number of bits in "in" */ 101 | /* for string and stored block copying */ 102 | unsigned length; /* literal or length of data to copy */ 103 | unsigned offset; /* distance back to copy string from */ 104 | /* for table and code decoding */ 105 | unsigned extra; /* extra bits needed */ 106 | /* fixed and dynamic code tables */ 107 | code const FAR *lencode; /* starting table for length/literal codes */ 108 | code const FAR *distcode; /* starting table for distance codes */ 109 | unsigned lenbits; /* index bits for lencode */ 110 | unsigned distbits; /* index bits for distcode */ 111 | /* dynamic table building */ 112 | unsigned ncode; /* number of code length code lengths */ 113 | unsigned nlen; /* number of length code lengths */ 114 | unsigned ndist; /* number of distance code lengths */ 115 | unsigned have; /* number of code lengths in lens[] */ 116 | code FAR *next; /* next available space in codes[] */ 117 | unsigned short lens[320]; /* temporary storage for code lengths */ 118 | unsigned short work[288]; /* work area for code table building */ 119 | code codes[ENOUGH]; /* space for code tables */ 120 | int sane; /* if false, allow invalid distance too far */ 121 | int back; /* bits back of last unprocessed length/lit */ 122 | unsigned was; /* initial length of match */ 123 | }; 124 | -------------------------------------------------------------------------------- /contrib/zlib/inftrees.c: -------------------------------------------------------------------------------- 1 | /* inftrees.c -- generate Huffman trees for efficient decoding 2 | * Copyright (C) 1995-2013 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.8 Copyright 1995-2013 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 ZLIB_INTERNAL 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 here; /* 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, 72, 78}; 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 | here.op = (unsigned char)64; /* invalid code marker */ 119 | here.bits = (unsigned char)1; 120 | here.val = (unsigned short)0; 121 | *(*table)++ = here; /* make a table to force an error */ 122 | *(*table)++ = here; 123 | *bits = 1; 124 | return 0; /* no symbols, but wait for decoding to report error */ 125 | } 126 | for (min = 1; min < max; 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 for LENS and DIST tables against 170 | the constants ENOUGH_LENS and ENOUGH_DISTS to guard against changes in 171 | the initial root table size constants. See the comments in inftrees.h 172 | for more information. 173 | 174 | sym increments through all symbols, and the loop terminates when 175 | all codes of length max, i.e. all codes, have been processed. This 176 | routine permits incomplete codes, so another loop after this one fills 177 | in the rest of the decoding tables with invalid code markers. 178 | */ 179 | 180 | /* set up for code type */ 181 | switch (type) { 182 | case CODES: 183 | base = extra = work; /* dummy value--not used */ 184 | end = 19; 185 | break; 186 | case LENS: 187 | base = lbase; 188 | base -= 257; 189 | extra = lext; 190 | extra -= 257; 191 | end = 256; 192 | break; 193 | default: /* DISTS */ 194 | base = dbase; 195 | extra = dext; 196 | end = -1; 197 | } 198 | 199 | /* initialize state for loop */ 200 | huff = 0; /* starting code */ 201 | sym = 0; /* starting code symbol */ 202 | len = min; /* starting code length */ 203 | next = *table; /* current table to fill in */ 204 | curr = root; /* current table index bits */ 205 | drop = 0; /* current bits to drop from code for index */ 206 | low = (unsigned)(-1); /* trigger new sub-table when len > root */ 207 | used = 1U << root; /* use root table entries */ 208 | mask = used - 1; /* mask for comparing low */ 209 | 210 | /* check available table space */ 211 | if ((type == LENS && used > ENOUGH_LENS) || 212 | (type == DISTS && used > ENOUGH_DISTS)) 213 | return 1; 214 | 215 | /* process all codes and make table entries */ 216 | for (;;) { 217 | /* create table entry */ 218 | here.bits = (unsigned char)(len - drop); 219 | if ((int)(work[sym]) < end) { 220 | here.op = (unsigned char)0; 221 | here.val = work[sym]; 222 | } 223 | else if ((int)(work[sym]) > end) { 224 | here.op = (unsigned char)(extra[work[sym]]); 225 | here.val = base[work[sym]]; 226 | } 227 | else { 228 | here.op = (unsigned char)(32 + 64); /* end of block */ 229 | here.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] = here; 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_LENS) || 281 | (type == DISTS && used > ENOUGH_DISTS)) 282 | return 1; 283 | 284 | /* point entry in root table to sub-table */ 285 | low = huff & mask; 286 | (*table)[low].op = (unsigned char)curr; 287 | (*table)[low].bits = (unsigned char)root; 288 | (*table)[low].val = (unsigned short)(next - *table); 289 | } 290 | } 291 | 292 | /* fill in remaining table entry if code is incomplete (guaranteed to have 293 | at most one remaining entry, since if the code is incomplete, the 294 | maximum code length that was allowed to get this far is one bit) */ 295 | if (huff != 0) { 296 | here.op = (unsigned char)64; /* invalid code marker */ 297 | here.bits = (unsigned char)(len - drop); 298 | here.val = (unsigned short)0; 299 | next[huff] = here; 300 | } 301 | 302 | /* set return parameters */ 303 | *table += used; 304 | *bits = root; 305 | return 0; 306 | } 307 | -------------------------------------------------------------------------------- /contrib/zlib/inftrees.h: -------------------------------------------------------------------------------- 1 | /* inftrees.h -- header to use inftrees.c 2 | * Copyright (C) 1995-2005, 2010 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 the dynamic table. The maximum number of code structures is 39 | 1444, which is the sum of 852 for literal/length codes and 592 for distance 40 | codes. These values were found by exhaustive searches using the program 41 | examples/enough.c found in the zlib distribtution. The arguments to that 42 | program are the number of symbols, the initial root table size, and the 43 | maximum bit length of a code. "enough 286 9 15" for literal/length codes 44 | returns returns 852, and "enough 30 6 15" for distance codes returns 592. 45 | The initial root table size (9 or 6) is found in the fifth argument of the 46 | inflate_table() calls in inflate.c and infback.c. If the root table size is 47 | changed, then these maximum sizes would be need to be recalculated and 48 | updated. */ 49 | #define ENOUGH_LENS 852 50 | #define ENOUGH_DISTS 592 51 | #define ENOUGH (ENOUGH_LENS + ENOUGH_DISTS) 52 | 53 | /* Type of code to build for inflate_table() */ 54 | typedef enum 55 | { 56 | CODES, 57 | LENS, 58 | DISTS 59 | } codetype; 60 | 61 | int ZLIB_INTERNAL inflate_table OF((codetype type, unsigned short FAR *lens, unsigned codes, code FAR *FAR *table, unsigned FAR *bits, unsigned short FAR *work)); 62 | -------------------------------------------------------------------------------- /contrib/zlib/trees.h: -------------------------------------------------------------------------------- 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 } }, { { 172 }, { 8 } }, { { 108 }, { 8 } }, 5 | { { 236 }, { 8 } }, { { 28 }, { 8 } }, { { 156 }, { 8 } }, { { 92 }, { 8 } }, { { 220 }, { 8 } }, { { 60 }, { 8 } }, { { 188 }, { 8 } }, 6 | { { 124 }, { 8 } }, { { 252 }, { 8 } }, { { 2 }, { 8 } }, { { 130 }, { 8 } }, { { 66 }, { 8 } }, { { 194 }, { 8 } }, { { 34 }, { 8 } }, 7 | { { 162 }, { 8 } }, { { 98 }, { 8 } }, { { 226 }, { 8 } }, { { 18 }, { 8 } }, { { 146 }, { 8 } }, { { 82 }, { 8 } }, { { 210 }, { 8 } }, 8 | { { 50 }, { 8 } }, { { 178 }, { 8 } }, { { 114 }, { 8 } }, { { 242 }, { 8 } }, { { 10 }, { 8 } }, { { 138 }, { 8 } }, { { 74 }, { 8 } }, 9 | { { 202 }, { 8 } }, { { 42 }, { 8 } }, { { 170 }, { 8 } }, { { 106 }, { 8 } }, { { 234 }, { 8 } }, { { 26 }, { 8 } }, { { 154 }, { 8 } }, 10 | { { 90 }, { 8 } }, { { 218 }, { 8 } }, { { 58 }, { 8 } }, { { 186 }, { 8 } }, { { 122 }, { 8 } }, { { 250 }, { 8 } }, { { 6 }, { 8 } }, 11 | { { 134 }, { 8 } }, { { 70 }, { 8 } }, { { 198 }, { 8 } }, { { 38 }, { 8 } }, { { 166 }, { 8 } }, { { 102 }, { 8 } }, { { 230 }, { 8 } }, 12 | { { 22 }, { 8 } }, { { 150 }, { 8 } }, { { 86 }, { 8 } }, { { 214 }, { 8 } }, { { 54 }, { 8 } }, { { 182 }, { 8 } }, { { 118 }, { 8 } }, 13 | { { 246 }, { 8 } }, { { 14 }, { 8 } }, { { 142 }, { 8 } }, { { 78 }, { 8 } }, { { 206 }, { 8 } }, { { 46 }, { 8 } }, { { 174 }, { 8 } }, 14 | { { 110 }, { 8 } }, { { 238 }, { 8 } }, { { 30 }, { 8 } }, { { 158 }, { 8 } }, { { 94 }, { 8 } }, { { 222 }, { 8 } }, { { 62 }, { 8 } }, 15 | { { 190 }, { 8 } }, { { 126 }, { 8 } }, { { 254 }, { 8 } }, { { 1 }, { 8 } }, { { 129 }, { 8 } }, { { 65 }, { 8 } }, { { 193 }, { 8 } }, 16 | { { 33 }, { 8 } }, { { 161 }, { 8 } }, { { 97 }, { 8 } }, { { 225 }, { 8 } }, { { 17 }, { 8 } }, { { 145 }, { 8 } }, { { 81 }, { 8 } }, 17 | { { 209 }, { 8 } }, { { 49 }, { 8 } }, { { 177 }, { 8 } }, { { 113 }, { 8 } }, { { 241 }, { 8 } }, { { 9 }, { 8 } }, { { 137 }, { 8 } }, 18 | { { 73 }, { 8 } }, { { 201 }, { 8 } }, { { 41 }, { 8 } }, { { 169 }, { 8 } }, { { 105 }, { 8 } }, { { 233 }, { 8 } }, { { 25 }, { 8 } }, 19 | { { 153 }, { 8 } }, { { 89 }, { 8 } }, { { 217 }, { 8 } }, { { 57 }, { 8 } }, { { 185 }, { 8 } }, { { 121 }, { 8 } }, { { 249 }, { 8 } }, 20 | { { 5 }, { 8 } }, { { 133 }, { 8 } }, { { 69 }, { 8 } }, { { 197 }, { 8 } }, { { 37 }, { 8 } }, { { 165 }, { 8 } }, { { 101 }, { 8 } }, 21 | { { 229 }, { 8 } }, { { 21 }, { 8 } }, { { 149 }, { 8 } }, { { 85 }, { 8 } }, { { 213 }, { 8 } }, { { 53 }, { 8 } }, { { 181 }, { 8 } }, 22 | { { 117 }, { 8 } }, { { 245 }, { 8 } }, { { 13 }, { 8 } }, { { 141 }, { 8 } }, { { 77 }, { 8 } }, { { 205 }, { 8 } }, { { 45 }, { 8 } }, 23 | { { 173 }, { 8 } }, { { 109 }, { 8 } }, { { 237 }, { 8 } }, { { 29 }, { 8 } }, { { 157 }, { 8 } }, { { 93 }, { 8 } }, { { 221 }, { 8 } }, 24 | { { 61 }, { 8 } }, { { 189 }, { 8 } }, { { 125 }, { 8 } }, { { 253 }, { 8 } }, { { 19 }, { 9 } }, { { 275 }, { 9 } }, { { 147 }, { 9 } }, 25 | { { 403 }, { 9 } }, { { 83 }, { 9 } }, { { 339 }, { 9 } }, { { 211 }, { 9 } }, { { 467 }, { 9 } }, { { 51 }, { 9 } }, { { 307 }, { 9 } }, 26 | { { 179 }, { 9 } }, { { 435 }, { 9 } }, { { 115 }, { 9 } }, { { 371 }, { 9 } }, { { 243 }, { 9 } }, { { 499 }, { 9 } }, { { 11 }, { 9 } }, 27 | { { 267 }, { 9 } }, { { 139 }, { 9 } }, { { 395 }, { 9 } }, { { 75 }, { 9 } }, { { 331 }, { 9 } }, { { 203 }, { 9 } }, { { 459 }, { 9 } }, 28 | { { 43 }, { 9 } }, { { 299 }, { 9 } }, { { 171 }, { 9 } }, { { 427 }, { 9 } }, { { 107 }, { 9 } }, { { 363 }, { 9 } }, { { 235 }, { 9 } }, 29 | { { 491 }, { 9 } }, { { 27 }, { 9 } }, { { 283 }, { 9 } }, { { 155 }, { 9 } }, { { 411 }, { 9 } }, { { 91 }, { 9 } }, { { 347 }, { 9 } }, 30 | { { 219 }, { 9 } }, { { 475 }, { 9 } }, { { 59 }, { 9 } }, { { 315 }, { 9 } }, { { 187 }, { 9 } }, { { 443 }, { 9 } }, { { 123 }, { 9 } }, 31 | { { 379 }, { 9 } }, { { 251 }, { 9 } }, { { 507 }, { 9 } }, { { 7 }, { 9 } }, { { 263 }, { 9 } }, { { 135 }, { 9 } }, { { 391 }, { 9 } }, 32 | { { 71 }, { 9 } }, { { 327 }, { 9 } }, { { 199 }, { 9 } }, { { 455 }, { 9 } }, { { 39 }, { 9 } }, { { 295 }, { 9 } }, { { 167 }, { 9 } }, 33 | { { 423 }, { 9 } }, { { 103 }, { 9 } }, { { 359 }, { 9 } }, { { 231 }, { 9 } }, { { 487 }, { 9 } }, { { 23 }, { 9 } }, { { 279 }, { 9 } }, 34 | { { 151 }, { 9 } }, { { 407 }, { 9 } }, { { 87 }, { 9 } }, { { 343 }, { 9 } }, { { 215 }, { 9 } }, { { 471 }, { 9 } }, { { 55 }, { 9 } }, 35 | { { 311 }, { 9 } }, { { 183 }, { 9 } }, { { 439 }, { 9 } }, { { 119 }, { 9 } }, { { 375 }, { 9 } }, { { 247 }, { 9 } }, { { 503 }, { 9 } }, 36 | { { 15 }, { 9 } }, { { 271 }, { 9 } }, { { 143 }, { 9 } }, { { 399 }, { 9 } }, { { 79 }, { 9 } }, { { 335 }, { 9 } }, { { 207 }, { 9 } }, 37 | { { 463 }, { 9 } }, { { 47 }, { 9 } }, { { 303 }, { 9 } }, { { 175 }, { 9 } }, { { 431 }, { 9 } }, { { 111 }, { 9 } }, { { 367 }, { 9 } }, 38 | { { 239 }, { 9 } }, { { 495 }, { 9 } }, { { 31 }, { 9 } }, { { 287 }, { 9 } }, { { 159 }, { 9 } }, { { 415 }, { 9 } }, { { 95 }, { 9 } }, 39 | { { 351 }, { 9 } }, { { 223 }, { 9 } }, { { 479 }, { 9 } }, { { 63 }, { 9 } }, { { 319 }, { 9 } }, { { 191 }, { 9 } }, { { 447 }, { 9 } }, 40 | { { 127 }, { 9 } }, { { 383 }, { 9 } }, { { 255 }, { 9 } }, { { 511 }, { 9 } }, { { 0 }, { 7 } }, { { 64 }, { 7 } }, { { 32 }, { 7 } }, 41 | { { 96 }, { 7 } }, { { 16 }, { 7 } }, { { 80 }, { 7 } }, { { 48 }, { 7 } }, { { 112 }, { 7 } }, { { 8 }, { 7 } }, { { 72 }, { 7 } }, 42 | { { 40 }, { 7 } }, { { 104 }, { 7 } }, { { 24 }, { 7 } }, { { 88 }, { 7 } }, { { 56 }, { 7 } }, { { 120 }, { 7 } }, { { 4 }, { 7 } }, 43 | { { 68 }, { 7 } }, { { 36 }, { 7 } }, { { 100 }, { 7 } }, { { 20 }, { 7 } }, { { 84 }, { 7 } }, { { 52 }, { 7 } }, { { 116 }, { 7 } }, 44 | { { 3 }, { 8 } }, { { 131 }, { 8 } }, { { 67 }, { 8 } }, { { 195 }, { 8 } }, { { 35 }, { 8 } }, { { 163 }, { 8 } }, { { 99 }, { 8 } }, 45 | { { 227 }, { 8 } } 46 | }; 47 | 48 | local const ct_data static_dtree[D_CODES] = { { { 0 }, { 5 } }, { { 16 }, { 5 } }, { { 8 }, { 5 } }, { { 24 }, { 5 } }, { { 4 }, { 5 } }, 49 | { { 20 }, { 5 } }, { { 12 }, { 5 } }, { { 28 }, { 5 } }, { { 2 }, { 5 } }, { { 18 }, { 5 } }, 50 | { { 10 }, { 5 } }, { { 26 }, { 5 } }, { { 6 }, { 5 } }, { { 22 }, { 5 } }, { { 14 }, { 5 } }, 51 | { { 30 }, { 5 } }, { { 1 }, { 5 } }, { { 17 }, { 5 } }, { { 9 }, { 5 } }, { { 25 }, { 5 } }, 52 | { { 5 }, { 5 } }, { { 21 }, { 5 } }, { { 13 }, { 5 } }, { { 29 }, { 5 } }, { { 3 }, { 5 } }, 53 | { { 19 }, { 5 } }, { { 11 }, { 5 } }, { { 27 }, { 5 } }, { { 7 }, { 5 } }, { { 23 }, { 5 } } }; 54 | 55 | const uch ZLIB_INTERNAL _dist_code[DIST_CODE_LEN] = { 56 | 0, 1, 2, 3, 4, 4, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 57 | 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 58 | 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 59 | 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 60 | 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 61 | 14, 14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 62 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 0, 0, 16, 63 | 17, 18, 18, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 22, 22, 22, 22, 22, 22, 22, 22, 23, 23, 23, 23, 23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 64 | 24, 24, 24, 24, 24, 24, 24, 24, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 65 | 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 66 | 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 67 | 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 68 | 28, 28, 28, 28, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 69 | 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29 70 | }; 71 | 72 | const uch ZLIB_INTERNAL _length_code[MAX_MATCH - MIN_MATCH + 1] = { 73 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 12, 12, 13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 15, 16, 16, 16, 16, 16, 74 | 16, 16, 16, 17, 17, 17, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18, 18, 18, 18, 19, 19, 19, 19, 19, 19, 19, 19, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 75 | 20, 20, 20, 20, 20, 20, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 76 | 22, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 77 | 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 78 | 25, 25, 25, 25, 25, 25, 25, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 79 | 26, 26, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 28 80 | }; 81 | 82 | local const int base_length[LENGTH_CODES] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16, 20, 24, 83 | 28, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 0 }; 84 | 85 | local const int base_dist[D_CODES] = { 0, 1, 2, 3, 4, 6, 8, 12, 16, 24, 32, 48, 64, 96, 128, 86 | 192, 256, 384, 512, 768, 1024, 1536, 2048, 3072, 4096, 6144, 8192, 12288, 16384, 24576 }; 87 | -------------------------------------------------------------------------------- /contrib/zlib/uncompr.c: -------------------------------------------------------------------------------- 1 | /* uncompr.c -- decompress a memory buffer 2 | * Copyright (C) 1995-2003, 2010 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 | 20 | uncompress returns Z_OK if success, Z_MEM_ERROR if there was not 21 | enough memory, Z_BUF_ERROR if there was not enough room in the output 22 | buffer, or Z_DATA_ERROR if the input data was corrupted. 23 | */ 24 | int ZEXPORT uncompress (dest, destLen, source, sourceLen) 25 | Bytef *dest; 26 | uLongf *destLen; 27 | const Bytef *source; 28 | uLong sourceLen; 29 | { 30 | z_stream stream; 31 | int err; 32 | 33 | stream.next_in = (z_const Bytef *)source; 34 | stream.avail_in = (uInt)sourceLen; 35 | /* Check for source > 64K on 16-bit machine: */ 36 | if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR; 37 | 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 | 45 | err = inflateInit(&stream); 46 | if (err != Z_OK) return err; 47 | 48 | err = inflate(&stream, Z_FINISH); 49 | if (err != Z_STREAM_END) { 50 | inflateEnd(&stream); 51 | if (err == Z_NEED_DICT || (err == Z_BUF_ERROR && stream.avail_in == 0)) 52 | return Z_DATA_ERROR; 53 | return err; 54 | } 55 | *destLen = stream.total_out; 56 | 57 | err = inflateEnd(&stream); 58 | return err; 59 | } 60 | -------------------------------------------------------------------------------- /contrib/zlib/zconf.h: -------------------------------------------------------------------------------- 1 | /* zconf.h -- configuration of the zlib compression library 2 | * Copyright (C) 1995-2013 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 | * Even better than compiling with -DZ_PREFIX would be to use configure to set 15 | * this permanently in zconf.h using "./configure --zprefix". 16 | */ 17 | #ifdef Z_PREFIX /* may be set to #if 1 by ./configure */ 18 | # define Z_PREFIX_SET 19 | 20 | /* all linked symbols */ 21 | # define _dist_code z__dist_code 22 | # define _length_code z__length_code 23 | # define _tr_align z__tr_align 24 | # define _tr_flush_bits z__tr_flush_bits 25 | # define _tr_flush_block z__tr_flush_block 26 | # define _tr_init z__tr_init 27 | # define _tr_stored_block z__tr_stored_block 28 | # define _tr_tally z__tr_tally 29 | # define adler32 z_adler32 30 | # define adler32_combine z_adler32_combine 31 | # define adler32_combine64 z_adler32_combine64 32 | # ifndef Z_SOLO 33 | # define compress z_compress 34 | # define compress2 z_compress2 35 | # define compressBound z_compressBound 36 | # endif 37 | # define crc32 z_crc32 38 | # define crc32_combine z_crc32_combine 39 | # define crc32_combine64 z_crc32_combine64 40 | # define deflate z_deflate 41 | # define deflateBound z_deflateBound 42 | # define deflateCopy z_deflateCopy 43 | # define deflateEnd z_deflateEnd 44 | # define deflateInit2_ z_deflateInit2_ 45 | # define deflateInit_ z_deflateInit_ 46 | # define deflateParams z_deflateParams 47 | # define deflatePending z_deflatePending 48 | # define deflatePrime z_deflatePrime 49 | # define deflateReset z_deflateReset 50 | # define deflateResetKeep z_deflateResetKeep 51 | # define deflateSetDictionary z_deflateSetDictionary 52 | # define deflateSetHeader z_deflateSetHeader 53 | # define deflateTune z_deflateTune 54 | # define deflate_copyright z_deflate_copyright 55 | # define get_crc_table z_get_crc_table 56 | # ifndef Z_SOLO 57 | # define gz_error z_gz_error 58 | # define gz_intmax z_gz_intmax 59 | # define gz_strwinerror z_gz_strwinerror 60 | # define gzbuffer z_gzbuffer 61 | # define gzclearerr z_gzclearerr 62 | # define gzclose z_gzclose 63 | # define gzclose_r z_gzclose_r 64 | # define gzclose_w z_gzclose_w 65 | # define gzdirect z_gzdirect 66 | # define gzdopen z_gzdopen 67 | # define gzeof z_gzeof 68 | # define gzerror z_gzerror 69 | # define gzflush z_gzflush 70 | # define gzgetc z_gzgetc 71 | # define gzgetc_ z_gzgetc_ 72 | # define gzgets z_gzgets 73 | # define gzoffset z_gzoffset 74 | # define gzoffset64 z_gzoffset64 75 | # define gzopen z_gzopen 76 | # define gzopen64 z_gzopen64 77 | # ifdef _WIN32 78 | # define gzopen_w z_gzopen_w 79 | # endif 80 | # define gzprintf z_gzprintf 81 | # define gzvprintf z_gzvprintf 82 | # define gzputc z_gzputc 83 | # define gzputs z_gzputs 84 | # define gzread z_gzread 85 | # define gzrewind z_gzrewind 86 | # define gzseek z_gzseek 87 | # define gzseek64 z_gzseek64 88 | # define gzsetparams z_gzsetparams 89 | # define gztell z_gztell 90 | # define gztell64 z_gztell64 91 | # define gzungetc z_gzungetc 92 | # define gzwrite z_gzwrite 93 | # endif 94 | # define inflate z_inflate 95 | # define inflateBack z_inflateBack 96 | # define inflateBackEnd z_inflateBackEnd 97 | # define inflateBackInit_ z_inflateBackInit_ 98 | # define inflateCopy z_inflateCopy 99 | # define inflateEnd z_inflateEnd 100 | # define inflateGetHeader z_inflateGetHeader 101 | # define inflateInit2_ z_inflateInit2_ 102 | # define inflateInit_ z_inflateInit_ 103 | # define inflateMark z_inflateMark 104 | # define inflatePrime z_inflatePrime 105 | # define inflateReset z_inflateReset 106 | # define inflateReset2 z_inflateReset2 107 | # define inflateSetDictionary z_inflateSetDictionary 108 | # define inflateGetDictionary z_inflateGetDictionary 109 | # define inflateSync z_inflateSync 110 | # define inflateSyncPoint z_inflateSyncPoint 111 | # define inflateUndermine z_inflateUndermine 112 | # define inflateResetKeep z_inflateResetKeep 113 | # define inflate_copyright z_inflate_copyright 114 | # define inflate_fast z_inflate_fast 115 | # define inflate_table z_inflate_table 116 | # ifndef Z_SOLO 117 | # define uncompress z_uncompress 118 | # endif 119 | # define zError z_zError 120 | # ifndef Z_SOLO 121 | # define zcalloc z_zcalloc 122 | # define zcfree z_zcfree 123 | # endif 124 | # define zlibCompileFlags z_zlibCompileFlags 125 | # define zlibVersion z_zlibVersion 126 | 127 | /* all zlib typedefs in zlib.h and zconf.h */ 128 | # define Byte z_Byte 129 | # define Bytef z_Bytef 130 | # define alloc_func z_alloc_func 131 | # define charf z_charf 132 | # define free_func z_free_func 133 | # ifndef Z_SOLO 134 | # define gzFile z_gzFile 135 | # endif 136 | # define gz_header z_gz_header 137 | # define gz_headerp z_gz_headerp 138 | # define in_func z_in_func 139 | # define intf z_intf 140 | # define out_func z_out_func 141 | # define uInt z_uInt 142 | # define uIntf z_uIntf 143 | # define uLong z_uLong 144 | # define uLongf z_uLongf 145 | # define voidp z_voidp 146 | # define voidpc z_voidpc 147 | # define voidpf z_voidpf 148 | 149 | /* all zlib structs in zlib.h and zconf.h */ 150 | # define gz_header_s z_gz_header_s 151 | # define internal_state z_internal_state 152 | 153 | #endif 154 | 155 | #if defined(__MSDOS__) && !defined(MSDOS) 156 | # define MSDOS 157 | #endif 158 | #if (defined(OS_2) || defined(__OS2__)) && !defined(OS2) 159 | # define OS2 160 | #endif 161 | #if defined(_WINDOWS) && !defined(WINDOWS) 162 | # define WINDOWS 163 | #endif 164 | #if defined(_WIN32) || defined(_WIN32_WCE) || defined(__WIN32__) 165 | # ifndef WIN32 166 | # define WIN32 167 | # endif 168 | #endif 169 | #if (defined(MSDOS) || defined(OS2) || defined(WINDOWS)) && !defined(WIN32) 170 | # if !defined(__GNUC__) && !defined(__FLAT__) && !defined(__386__) 171 | # ifndef SYS16BIT 172 | # define SYS16BIT 173 | # endif 174 | # endif 175 | #endif 176 | 177 | /* 178 | * Compile with -DMAXSEG_64K if the alloc function cannot allocate more 179 | * than 64k bytes at a time (needed on systems with 16-bit int). 180 | */ 181 | #ifdef SYS16BIT 182 | # define MAXSEG_64K 183 | #endif 184 | #ifdef MSDOS 185 | # define UNALIGNED_OK 186 | #endif 187 | 188 | #ifdef __STDC_VERSION__ 189 | # ifndef STDC 190 | # define STDC 191 | # endif 192 | # if __STDC_VERSION__ >= 199901L 193 | # ifndef STDC99 194 | # define STDC99 195 | # endif 196 | # endif 197 | #endif 198 | #if !defined(STDC) && (defined(__STDC__) || defined(__cplusplus)) 199 | # define STDC 200 | #endif 201 | #if !defined(STDC) && (defined(__GNUC__) || defined(__BORLANDC__)) 202 | # define STDC 203 | #endif 204 | #if !defined(STDC) && (defined(MSDOS) || defined(WINDOWS) || defined(WIN32)) 205 | # define STDC 206 | #endif 207 | #if !defined(STDC) && (defined(OS2) || defined(__HOS_AIX__)) 208 | # define STDC 209 | #endif 210 | 211 | #if defined(__OS400__) && !defined(STDC) /* iSeries (formerly AS/400). */ 212 | # define STDC 213 | #endif 214 | 215 | #ifndef STDC 216 | # ifndef const /* cannot use !defined(STDC) && !defined(const) on Mac */ 217 | # define const /* note: need a more gentle solution here */ 218 | # endif 219 | #endif 220 | 221 | #if defined(ZLIB_CONST) && !defined(z_const) 222 | # define z_const const 223 | #else 224 | # define z_const 225 | #endif 226 | 227 | /* Some Mac compilers merge all .h files incorrectly: */ 228 | #if defined(__MWERKS__) || defined(applec) || defined(THINK_C) || defined(__SC__) 229 | # define NO_DUMMY_DECL 230 | #endif 231 | 232 | /* Maximum value for memLevel in deflateInit2 */ 233 | #ifndef MAX_MEM_LEVEL 234 | # ifdef MAXSEG_64K 235 | # define MAX_MEM_LEVEL 8 236 | # else 237 | # define MAX_MEM_LEVEL 9 238 | # endif 239 | #endif 240 | 241 | /* Maximum value for windowBits in deflateInit2 and inflateInit2. 242 | * WARNING: reducing MAX_WBITS makes minigzip unable to extract .gz files 243 | * created by gzip. (Files created by minigzip can still be extracted by 244 | * gzip.) 245 | */ 246 | #ifndef MAX_WBITS 247 | # define MAX_WBITS 15 /* 32K LZ77 window */ 248 | #endif 249 | 250 | /* The memory requirements for deflate are (in bytes): 251 | (1 << (windowBits+2)) + (1 << (memLevel+9)) 252 | that is: 128K for windowBits=15 + 128K for memLevel = 8 (default values) 253 | plus a few kilobytes for small objects. For example, if you want to reduce 254 | the default memory requirements from 256K to 128K, compile with 255 | make CFLAGS="-O -DMAX_WBITS=14 -DMAX_MEM_LEVEL=7" 256 | Of course this will generally degrade compression (there's no free lunch). 257 | 258 | The memory requirements for inflate are (in bytes) 1 << windowBits 259 | that is, 32K for windowBits=15 (default value) plus a few kilobytes 260 | for small objects. 261 | */ 262 | 263 | /* Type declarations */ 264 | 265 | #ifndef OF /* function prototypes */ 266 | # ifdef STDC 267 | # define OF(args) args 268 | # else 269 | # define OF(args) () 270 | # endif 271 | #endif 272 | 273 | #ifndef Z_ARG /* function prototypes for stdarg */ 274 | # if defined(STDC) || defined(Z_HAVE_STDARG_H) 275 | # define Z_ARG(args) args 276 | # else 277 | # define Z_ARG(args) () 278 | # endif 279 | #endif 280 | 281 | /* The following definitions for FAR are needed only for MSDOS mixed 282 | * model programming (small or medium model with some far allocations). 283 | * This was tested only with MSC; for other MSDOS compilers you may have 284 | * to define NO_MEMCPY in zutil.h. If you don't need the mixed model, 285 | * just define FAR to be empty. 286 | */ 287 | #ifdef SYS16BIT 288 | # if defined(M_I86SM) || defined(M_I86MM) 289 | /* MSC small or medium model */ 290 | # define SMALL_MEDIUM 291 | # ifdef _MSC_VER 292 | # define FAR _far 293 | # else 294 | # define FAR far 295 | # endif 296 | # endif 297 | # if (defined(__SMALL__) || defined(__MEDIUM__)) 298 | /* Turbo C small or medium model */ 299 | # define SMALL_MEDIUM 300 | # ifdef __BORLANDC__ 301 | # define FAR _far 302 | # else 303 | # define FAR far 304 | # endif 305 | # endif 306 | #endif 307 | 308 | #if defined(WINDOWS) || defined(WIN32) 309 | /* If building or using zlib as a DLL, define ZLIB_DLL. 310 | * This is not mandatory, but it offers a little performance increase. 311 | */ 312 | # ifdef ZLIB_DLL 313 | # if defined(WIN32) && (!defined(__BORLANDC__) || (__BORLANDC__ >= 0x500)) 314 | # ifdef ZLIB_INTERNAL 315 | # define ZEXTERN extern __declspec(dllexport) 316 | # else 317 | # define ZEXTERN extern __declspec(dllimport) 318 | # endif 319 | # endif 320 | # endif /* ZLIB_DLL */ 321 | /* If building or using zlib with the WINAPI/WINAPIV calling convention, 322 | * define ZLIB_WINAPI. 323 | * Caution: the standard ZLIB1.DLL is NOT compiled using ZLIB_WINAPI. 324 | */ 325 | # ifdef ZLIB_WINAPI 326 | # ifdef FAR 327 | # undef FAR 328 | # endif 329 | # include 330 | /* No need for _export, use ZLIB.DEF instead. */ 331 | /* For complete Windows compatibility, use WINAPI, not __stdcall. */ 332 | # define ZEXPORT WINAPI 333 | # ifdef WIN32 334 | # define ZEXPORTVA WINAPIV 335 | # else 336 | # define ZEXPORTVA FAR CDECL 337 | # endif 338 | # endif 339 | #endif 340 | 341 | #if defined(__BEOS__) 342 | # ifdef ZLIB_DLL 343 | # ifdef ZLIB_INTERNAL 344 | # define ZEXPORT __declspec(dllexport) 345 | # define ZEXPORTVA __declspec(dllexport) 346 | # else 347 | # define ZEXPORT __declspec(dllimport) 348 | # define ZEXPORTVA __declspec(dllimport) 349 | # endif 350 | # endif 351 | #endif 352 | 353 | #ifndef ZEXTERN 354 | # define ZEXTERN extern 355 | #endif 356 | #ifndef ZEXPORT 357 | # define ZEXPORT 358 | #endif 359 | #ifndef ZEXPORTVA 360 | # define ZEXPORTVA 361 | #endif 362 | 363 | #ifndef FAR 364 | # define FAR 365 | #endif 366 | 367 | #if !defined(__MACTYPES__) 368 | typedef unsigned char Byte; /* 8 bits */ 369 | #endif 370 | typedef unsigned int uInt; /* 16 bits or more */ 371 | typedef unsigned long uLong; /* 32 bits or more */ 372 | 373 | #ifdef SMALL_MEDIUM 374 | /* Borland C/C++ and some old MSC versions ignore FAR inside typedef */ 375 | # define Bytef Byte FAR 376 | #else 377 | typedef Byte FAR Bytef; 378 | #endif 379 | typedef char FAR charf; 380 | typedef int FAR intf; 381 | typedef uInt FAR uIntf; 382 | typedef uLong FAR uLongf; 383 | 384 | #ifdef STDC 385 | typedef void const *voidpc; 386 | typedef void FAR *voidpf; 387 | typedef void *voidp; 388 | #else 389 | typedef Byte const *voidpc; 390 | typedef Byte FAR *voidpf; 391 | typedef Byte *voidp; 392 | #endif 393 | 394 | #if !defined(Z_U4) && !defined(Z_SOLO) && defined(STDC) 395 | # include 396 | # if (UINT_MAX == 0xffffffffUL) 397 | # define Z_U4 unsigned 398 | # elif (ULONG_MAX == 0xffffffffUL) 399 | # define Z_U4 unsigned long 400 | # elif (USHRT_MAX == 0xffffffffUL) 401 | # define Z_U4 unsigned short 402 | # endif 403 | #endif 404 | 405 | #ifdef Z_U4 406 | typedef Z_U4 z_crc_t; 407 | # else 408 | typedef unsigned long z_crc_t; 409 | # endif 410 | 411 | # ifdef HAVE_UNISTD_H /* may be set to #if 1 by ./configure */ 412 | # define Z_HAVE_UNISTD_H 413 | # endif 414 | 415 | # ifdef HAVE_STDARG_H /* may be set to #if 1 by ./configure */ 416 | # define Z_HAVE_STDARG_H 417 | # endif 418 | 419 | # ifdef STDC 420 | # ifndef Z_SOLO 421 | # include /* for off_t */ 422 | # endif 423 | # endif 424 | 425 | # if defined(STDC) || defined(Z_HAVE_STDARG_H) 426 | # ifndef Z_SOLO 427 | # include /* for va_list */ 428 | # endif 429 | # endif 430 | 431 | # ifdef _WIN32 432 | # ifndef Z_SOLO 433 | # include /* for wchar_t */ 434 | # endif 435 | # endif 436 | 437 | /* a little trick to accommodate both "#define _LARGEFILE64_SOURCE" and 438 | * "#define _LARGEFILE64_SOURCE 1" as requesting 64-bit operations, (even 439 | * though the former does not conform to the LFS document), but considering 440 | * both "#undef _LARGEFILE64_SOURCE" and "#define _LARGEFILE64_SOURCE 0" as 441 | * equivalently requesting no 64-bit operations 442 | */ 443 | # if defined(_LARGEFILE64_SOURCE) && -_LARGEFILE64_SOURCE - -1 == 1 444 | # undef _LARGEFILE64_SOURCE 445 | # endif 446 | 447 | # if defined(__WATCOMC__) && !defined(Z_HAVE_UNISTD_H) 448 | # define Z_HAVE_UNISTD_H 449 | # endif 450 | # ifndef Z_SOLO 451 | # if defined(Z_HAVE_UNISTD_H) || defined(_LARGEFILE64_SOURCE) 452 | # include /* for SEEK_*, off_t, and _LFS64_LARGEFILE */ 453 | # ifdef VMS 454 | # include /* for off_t */ 455 | # endif 456 | # ifndef z_off_t 457 | # define z_off_t off_t 458 | # endif 459 | # endif 460 | # endif 461 | 462 | # if defined(_LFS64_LARGEFILE) && _LFS64_LARGEFILE - 0 463 | # define Z_LFS64 464 | # endif 465 | 466 | # if defined(_LARGEFILE64_SOURCE) && defined(Z_LFS64) 467 | # define Z_LARGE64 468 | # endif 469 | 470 | # if defined(_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS - 0 == 64 && defined(Z_LFS64) 471 | # define Z_WANT64 472 | # endif 473 | 474 | # if !defined(SEEK_SET) && !defined(Z_SOLO) 475 | # define SEEK_SET 0 /* Seek from beginning of file. */ 476 | # define SEEK_CUR 1 /* Seek from current position. */ 477 | # define SEEK_END 2 /* Set file pointer to EOF plus "offset" */ 478 | # endif 479 | 480 | # ifndef z_off_t 481 | # define z_off_t long 482 | # endif 483 | 484 | # if !defined(_WIN32) && defined(Z_LARGE64) 485 | # define z_off64_t off64_t 486 | # else 487 | # if defined(_WIN32) && !defined(__GNUC__) && !defined(Z_SOLO) 488 | # define z_off64_t __int64 489 | # else 490 | # define z_off64_t z_off_t 491 | # endif 492 | # endif 493 | 494 | /* MVS linker does not support external names larger than 8 bytes */ 495 | # if defined(__MVS__) 496 | # pragma map(deflateInit_, "DEIN") 497 | # pragma map(deflateInit2_, "DEIN2") 498 | # pragma map(deflateEnd, "DEEND") 499 | # pragma map(deflateBound, "DEBND") 500 | # pragma map(inflateInit_, "ININ") 501 | # pragma map(inflateInit2_, "ININ2") 502 | # pragma map(inflateEnd, "INEND") 503 | # pragma map(inflateSync, "INSY") 504 | # pragma map(inflateSetDictionary, "INSEDI") 505 | # pragma map(compressBound, "CMBND") 506 | # pragma map(inflate_table, "INTABL") 507 | # pragma map(inflate_fast, "INFA") 508 | # pragma map(inflate_copyright, "INCOPY") 509 | # endif 510 | 511 | #endif /* ZCONF_H */ 512 | -------------------------------------------------------------------------------- /contrib/zlib/zutil.c: -------------------------------------------------------------------------------- 1 | /* zutil.c -- target dependent utility functions for the compression library 2 | * Copyright (C) 1995-2005, 2010, 2011, 2012 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 | #ifndef Z_SOLO 10 | # include "gzguts.h" 11 | #endif 12 | 13 | #ifndef NO_DUMMY_DECL 14 | struct internal_state {int dummy;}; /* for buggy compilers */ 15 | #endif 16 | 17 | z_const char * const z_errmsg[10] = { 18 | "need dictionary", /* Z_NEED_DICT 2 */ 19 | "stream end", /* Z_STREAM_END 1 */ 20 | "", /* Z_OK 0 */ 21 | "file error", /* Z_ERRNO (-1) */ 22 | "stream error", /* Z_STREAM_ERROR (-2) */ 23 | "data error", /* Z_DATA_ERROR (-3) */ 24 | "insufficient memory", /* Z_MEM_ERROR (-4) */ 25 | "buffer error", /* Z_BUF_ERROR (-5) */ 26 | "incompatible version",/* Z_VERSION_ERROR (-6) */ 27 | ""}; 28 | 29 | 30 | const char * ZEXPORT zlibVersion() 31 | { 32 | return ZLIB_VERSION; 33 | } 34 | 35 | uLong ZEXPORT zlibCompileFlags() 36 | { 37 | uLong flags; 38 | 39 | flags = 0; 40 | switch ((int)(sizeof(uInt))) { 41 | case 2: break; 42 | case 4: flags += 1; break; 43 | case 8: flags += 2; break; 44 | default: flags += 3; 45 | } 46 | switch ((int)(sizeof(uLong))) { 47 | case 2: break; 48 | case 4: flags += 1 << 2; break; 49 | case 8: flags += 2 << 2; break; 50 | default: flags += 3 << 2; 51 | } 52 | switch ((int)(sizeof(voidpf))) { 53 | case 2: break; 54 | case 4: flags += 1 << 4; break; 55 | case 8: flags += 2 << 4; break; 56 | default: flags += 3 << 4; 57 | } 58 | switch ((int)(sizeof(z_off_t))) { 59 | case 2: break; 60 | case 4: flags += 1 << 6; break; 61 | case 8: flags += 2 << 6; break; 62 | default: flags += 3 << 6; 63 | } 64 | #ifdef DEBUG 65 | flags += 1 << 8; 66 | #endif 67 | #if defined(ASMV) || defined(ASMINF) 68 | flags += 1 << 9; 69 | #endif 70 | #ifdef ZLIB_WINAPI 71 | flags += 1 << 10; 72 | #endif 73 | #ifdef BUILDFIXED 74 | flags += 1 << 12; 75 | #endif 76 | #ifdef DYNAMIC_CRC_TABLE 77 | flags += 1 << 13; 78 | #endif 79 | #ifdef NO_GZCOMPRESS 80 | flags += 1L << 16; 81 | #endif 82 | #ifdef NO_GZIP 83 | flags += 1L << 17; 84 | #endif 85 | #ifdef PKZIP_BUG_WORKAROUND 86 | flags += 1L << 20; 87 | #endif 88 | #ifdef FASTEST 89 | flags += 1L << 21; 90 | #endif 91 | #if defined(STDC) || defined(Z_HAVE_STDARG_H) 92 | # ifdef NO_vsnprintf 93 | flags += 1L << 25; 94 | # ifdef HAS_vsprintf_void 95 | flags += 1L << 26; 96 | # endif 97 | # else 98 | # ifdef HAS_vsnprintf_void 99 | flags += 1L << 26; 100 | # endif 101 | # endif 102 | #else 103 | flags += 1L << 24; 104 | # ifdef NO_snprintf 105 | flags += 1L << 25; 106 | # ifdef HAS_sprintf_void 107 | flags += 1L << 26; 108 | # endif 109 | # else 110 | # ifdef HAS_snprintf_void 111 | flags += 1L << 26; 112 | # endif 113 | # endif 114 | #endif 115 | return flags; 116 | } 117 | 118 | #ifdef DEBUG 119 | 120 | # ifndef verbose 121 | # define verbose 0 122 | # endif 123 | int ZLIB_INTERNAL z_verbose = verbose; 124 | 125 | void ZLIB_INTERNAL z_error (m) 126 | char *m; 127 | { 128 | fprintf(stderr, "%s\n", m); 129 | exit(1); 130 | } 131 | #endif 132 | 133 | /* exported to allow conversion of error code to string for compress() and 134 | * uncompress() 135 | */ 136 | const char * ZEXPORT zError(err) 137 | int err; 138 | { 139 | return ERR_MSG(err); 140 | } 141 | 142 | #if defined(_WIN32_WCE) 143 | /* The Microsoft C Run-Time Library for Windows CE doesn't have 144 | * errno. We define it as a global variable to simplify porting. 145 | * Its value is always 0 and should not be used. 146 | */ 147 | int errno = 0; 148 | #endif 149 | 150 | #ifndef HAVE_MEMCPY 151 | 152 | void ZLIB_INTERNAL zmemcpy(dest, source, len) 153 | Bytef* dest; 154 | const Bytef* source; 155 | uInt len; 156 | { 157 | if (len == 0) return; 158 | do { 159 | *dest++ = *source++; /* ??? to be unrolled */ 160 | } while (--len != 0); 161 | } 162 | 163 | int ZLIB_INTERNAL zmemcmp(s1, s2, len) 164 | const Bytef* s1; 165 | const Bytef* s2; 166 | uInt len; 167 | { 168 | uInt j; 169 | 170 | for (j = 0; j < len; j++) { 171 | if (s1[j] != s2[j]) return 2*(s1[j] > s2[j])-1; 172 | } 173 | return 0; 174 | } 175 | 176 | void ZLIB_INTERNAL zmemzero(dest, len) 177 | Bytef* dest; 178 | uInt len; 179 | { 180 | if (len == 0) return; 181 | do { 182 | *dest++ = 0; /* ??? to be unrolled */ 183 | } while (--len != 0); 184 | } 185 | #endif 186 | 187 | #ifndef Z_SOLO 188 | 189 | #ifdef SYS16BIT 190 | 191 | #ifdef __TURBOC__ 192 | /* Turbo C in 16-bit mode */ 193 | 194 | # define MY_ZCALLOC 195 | 196 | /* Turbo C malloc() does not allow dynamic allocation of 64K bytes 197 | * and farmalloc(64K) returns a pointer with an offset of 8, so we 198 | * must fix the pointer. Warning: the pointer must be put back to its 199 | * original form in order to free it, use zcfree(). 200 | */ 201 | 202 | #define MAX_PTR 10 203 | /* 10*64K = 640K */ 204 | 205 | local int next_ptr = 0; 206 | 207 | typedef struct ptr_table_s { 208 | voidpf org_ptr; 209 | voidpf new_ptr; 210 | } ptr_table; 211 | 212 | local ptr_table table[MAX_PTR]; 213 | /* This table is used to remember the original form of pointers 214 | * to large buffers (64K). Such pointers are normalized with a zero offset. 215 | * Since MSDOS is not a preemptive multitasking OS, this table is not 216 | * protected from concurrent access. This hack doesn't work anyway on 217 | * a protected system like OS/2. Use Microsoft C instead. 218 | */ 219 | 220 | voidpf ZLIB_INTERNAL zcalloc (voidpf opaque, unsigned items, unsigned size) 221 | { 222 | voidpf buf = opaque; /* just to make some compilers happy */ 223 | ulg bsize = (ulg)items*size; 224 | 225 | /* If we allocate less than 65520 bytes, we assume that farmalloc 226 | * will return a usable pointer which doesn't have to be normalized. 227 | */ 228 | if (bsize < 65520L) { 229 | buf = farmalloc(bsize); 230 | if (*(ush*)&buf != 0) return buf; 231 | } else { 232 | buf = farmalloc(bsize + 16L); 233 | } 234 | if (buf == NULL || next_ptr >= MAX_PTR) return NULL; 235 | table[next_ptr].org_ptr = buf; 236 | 237 | /* Normalize the pointer to seg:0 */ 238 | *((ush*)&buf+1) += ((ush)((uch*)buf-0) + 15) >> 4; 239 | *(ush*)&buf = 0; 240 | table[next_ptr++].new_ptr = buf; 241 | return buf; 242 | } 243 | 244 | void ZLIB_INTERNAL zcfree (voidpf opaque, voidpf ptr) 245 | { 246 | int n; 247 | if (*(ush*)&ptr != 0) { /* object < 64K */ 248 | farfree(ptr); 249 | return; 250 | } 251 | /* Find the original pointer */ 252 | for (n = 0; n < next_ptr; n++) { 253 | if (ptr != table[n].new_ptr) continue; 254 | 255 | farfree(table[n].org_ptr); 256 | while (++n < next_ptr) { 257 | table[n-1] = table[n]; 258 | } 259 | next_ptr--; 260 | return; 261 | } 262 | ptr = opaque; /* just to make some compilers happy */ 263 | Assert(0, "zcfree: ptr not found"); 264 | } 265 | 266 | #endif /* __TURBOC__ */ 267 | 268 | 269 | #ifdef M_I86 270 | /* Microsoft C in 16-bit mode */ 271 | 272 | # define MY_ZCALLOC 273 | 274 | #if (!defined(_MSC_VER) || (_MSC_VER <= 600)) 275 | # define _halloc halloc 276 | # define _hfree hfree 277 | #endif 278 | 279 | voidpf ZLIB_INTERNAL zcalloc (voidpf opaque, uInt items, uInt size) 280 | { 281 | if (opaque) opaque = 0; /* to make compiler happy */ 282 | return _halloc((long)items, size); 283 | } 284 | 285 | void ZLIB_INTERNAL zcfree (voidpf opaque, voidpf ptr) 286 | { 287 | if (opaque) opaque = 0; /* to make compiler happy */ 288 | _hfree(ptr); 289 | } 290 | 291 | #endif /* M_I86 */ 292 | 293 | #endif /* SYS16BIT */ 294 | 295 | 296 | #ifndef MY_ZCALLOC /* Any system without a special alloc function */ 297 | 298 | #ifndef STDC 299 | extern voidp malloc OF((uInt size)); 300 | extern voidp calloc OF((uInt items, uInt size)); 301 | extern void free OF((voidpf ptr)); 302 | #endif 303 | 304 | voidpf ZLIB_INTERNAL zcalloc (opaque, items, size) 305 | voidpf opaque; 306 | unsigned items; 307 | unsigned size; 308 | { 309 | if (opaque) items += size - size; /* make compiler happy */ 310 | return sizeof(uInt) > 2 ? (voidpf)malloc(items * size) : 311 | (voidpf)calloc(items, size); 312 | } 313 | 314 | void ZLIB_INTERNAL zcfree (opaque, ptr) 315 | voidpf opaque; 316 | voidpf ptr; 317 | { 318 | free(ptr); 319 | if (opaque) return; /* make compiler happy */ 320 | } 321 | 322 | #endif /* MY_ZCALLOC */ 323 | 324 | #endif /* !Z_SOLO */ 325 | -------------------------------------------------------------------------------- /contrib/zlib/zutil.h: -------------------------------------------------------------------------------- 1 | /* zutil.h -- internal interface and configuration of the compression library 2 | * Copyright (C) 1995-2013 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 | #ifdef HAVE_HIDDEN 17 | # define ZLIB_INTERNAL __attribute__((visibility("hidden"))) 18 | #else 19 | # define ZLIB_INTERNAL 20 | #endif 21 | 22 | #include "zlib.h" 23 | 24 | #if defined(STDC) && !defined(Z_SOLO) 25 | # if !(defined(_WIN32_WCE) && defined(_MSC_VER)) 26 | # include 27 | # endif 28 | # include 29 | # include 30 | #endif 31 | 32 | #ifdef Z_SOLO 33 | typedef long ptrdiff_t; /* guess -- will be caught if guess is wrong */ 34 | #endif 35 | 36 | #ifndef local 37 | # define local static 38 | #endif 39 | /* compile with -Dlocal if your debugger can't find static symbols */ 40 | 41 | typedef unsigned char uch; 42 | typedef uch FAR uchf; 43 | typedef unsigned short ush; 44 | typedef ush FAR ushf; 45 | typedef unsigned long ulg; 46 | 47 | extern z_const char *const z_errmsg[10]; /* indexed by 2-zlib_error */ 48 | /* (size given to avoid silly warnings with Visual C++) */ 49 | 50 | #define ERR_MSG(err) z_errmsg[Z_NEED_DICT - (err)] 51 | 52 | #define ERR_RETURN(strm, err) return (strm->msg = ERR_MSG(err), (err)) 53 | /* To be used only when the state is known to be valid */ 54 | 55 | /* common constants */ 56 | 57 | #ifndef DEF_WBITS 58 | # define DEF_WBITS MAX_WBITS 59 | #endif 60 | /* default windowBits for decompression. MAX_WBITS is for compression only */ 61 | 62 | #if MAX_MEM_LEVEL >= 8 63 | # define DEF_MEM_LEVEL 8 64 | #else 65 | # define DEF_MEM_LEVEL MAX_MEM_LEVEL 66 | #endif 67 | /* default memLevel */ 68 | 69 | #define STORED_BLOCK 0 70 | #define STATIC_TREES 1 71 | #define DYN_TREES 2 72 | /* The three kinds of block type */ 73 | 74 | #define MIN_MATCH 3 75 | #define MAX_MATCH 258 76 | /* The minimum and maximum match lengths */ 77 | 78 | #define PRESET_DICT 0x20 /* preset dictionary flag in zlib header */ 79 | 80 | /* target dependencies */ 81 | 82 | #if defined(MSDOS) || (defined(WINDOWS) && !defined(WIN32)) 83 | # define OS_CODE 0x00 84 | # ifndef Z_SOLO 85 | # if defined(__TURBOC__) || defined(__BORLANDC__) 86 | # if (__STDC__ == 1) && (defined(__LARGE__) || defined(__COMPACT__)) 87 | /* Allow compilation with ANSI keywords only enabled */ 88 | void _Cdecl farfree(void *block); 89 | void *_Cdecl farmalloc(unsigned long nbytes); 90 | # else 91 | # include 92 | # endif 93 | # else /* MSC or DJGPP */ 94 | # include 95 | # endif 96 | # endif 97 | #endif 98 | 99 | #ifdef AMIGA 100 | # define OS_CODE 0x01 101 | #endif 102 | 103 | #if defined(VAXC) || defined(VMS) 104 | # define OS_CODE 0x02 105 | # define F_OPEN(name, mode) fopen((name), (mode), "mbc=60", "ctx=stm", "rfm=fix", "mrs=512") 106 | #endif 107 | 108 | #if defined(ATARI) || defined(atarist) 109 | # define OS_CODE 0x05 110 | #endif 111 | 112 | #ifdef OS2 113 | # define OS_CODE 0x06 114 | # if defined(M_I86) && !defined(Z_SOLO) 115 | # include 116 | # endif 117 | #endif 118 | 119 | #if defined(MACOS) || defined(TARGET_OS_MAC) 120 | # define OS_CODE 0x07 121 | # ifndef Z_SOLO 122 | # if defined(__MWERKS__) && __dest_os != __be_os && __dest_os != __win32_os 123 | # include /* for fdopen */ 124 | # else 125 | # ifndef fdopen 126 | # define fdopen(fd, mode) NULL /* No fdopen() */ 127 | # endif 128 | # endif 129 | # endif 130 | #endif 131 | 132 | #ifdef TOPS20 133 | # define OS_CODE 0x0a 134 | #endif 135 | 136 | #ifdef WIN32 137 | # ifndef __CYGWIN__ /* Cygwin is Unix, not Win32 */ 138 | # define OS_CODE 0x0b 139 | # endif 140 | #endif 141 | 142 | #ifdef __50SERIES /* Prime/PRIMOS */ 143 | # define OS_CODE 0x0f 144 | #endif 145 | 146 | #if defined(_BEOS_) || defined(RISCOS) 147 | # define fdopen(fd, mode) NULL /* No fdopen() */ 148 | #endif 149 | 150 | #if (defined(_MSC_VER) && (_MSC_VER > 600)) && !defined __INTERIX 151 | # if defined(_WIN32_WCE) 152 | # define fdopen(fd, mode) NULL /* No fdopen() */ 153 | # ifndef _PTRDIFF_T_DEFINED 154 | typedef int ptrdiff_t; 155 | # define _PTRDIFF_T_DEFINED 156 | # endif 157 | # else 158 | # define fdopen(fd, type) _fdopen(fd, type) 159 | # endif 160 | #endif 161 | 162 | #if defined(__BORLANDC__) && !defined(MSDOS) 163 | # pragma warn - 8004 164 | # pragma warn - 8008 165 | # pragma warn - 8066 166 | #endif 167 | 168 | /* provide prototypes for these when building zlib without LFS */ 169 | #if !defined(_WIN32) && (!defined(_LARGEFILE64_SOURCE) || _LFS64_LARGEFILE - 0 == 0) 170 | ZEXTERN uLong ZEXPORT adler32_combine64 OF((uLong, uLong, z_off_t)); 171 | ZEXTERN uLong ZEXPORT crc32_combine64 OF((uLong, uLong, z_off_t)); 172 | #endif 173 | 174 | /* common defaults */ 175 | 176 | #ifndef OS_CODE 177 | # define OS_CODE 0x03 /* assume Unix */ 178 | #endif 179 | 180 | #ifndef F_OPEN 181 | # define F_OPEN(name, mode) fopen((name), (mode)) 182 | #endif 183 | 184 | /* functions */ 185 | 186 | #if defined(pyr) || defined(Z_SOLO) 187 | # define NO_MEMCPY 188 | #endif 189 | #if defined(SMALL_MEDIUM) && !defined(_MSC_VER) && !defined(__SC__) 190 | /* Use our own functions for small and medium model with MSC <= 5.0. 191 | * You may have to use the same strategy for Borland C (untested). 192 | * The __SC__ check is for Symantec. 193 | */ 194 | # define NO_MEMCPY 195 | #endif 196 | #if defined(STDC) && !defined(HAVE_MEMCPY) && !defined(NO_MEMCPY) 197 | # define HAVE_MEMCPY 198 | #endif 199 | #ifdef HAVE_MEMCPY 200 | # ifdef SMALL_MEDIUM /* MSDOS small or medium model */ 201 | # define zmemcpy _fmemcpy 202 | # define zmemcmp _fmemcmp 203 | # define zmemzero(dest, len) _fmemset(dest, 0, len) 204 | # else 205 | # define zmemcpy memcpy 206 | # define zmemcmp memcmp 207 | # define zmemzero(dest, len) memset(dest, 0, len) 208 | # endif 209 | #else 210 | void ZLIB_INTERNAL zmemcpy OF((Bytef * dest, const Bytef* source, uInt len)); 211 | int ZLIB_INTERNAL zmemcmp OF((const Bytef* s1, const Bytef* s2, uInt len)); 212 | void ZLIB_INTERNAL zmemzero OF((Bytef * dest, uInt len)); 213 | #endif 214 | 215 | /* Diagnostic functions */ 216 | #ifdef DEBUG 217 | # include 218 | extern int ZLIB_INTERNAL z_verbose; 219 | extern void ZLIB_INTERNAL z_error OF((char *m)); 220 | # define Assert(cond, msg) \ 221 | { \ 222 | if (!(cond)) \ 223 | z_error(msg); \ 224 | } 225 | # define Trace(x) \ 226 | { \ 227 | if (z_verbose >= 0) \ 228 | fprintf x; \ 229 | } 230 | # define Tracev(x) \ 231 | { \ 232 | if (z_verbose > 0) \ 233 | fprintf x; \ 234 | } 235 | # define Tracevv(x) \ 236 | { \ 237 | if (z_verbose > 1) \ 238 | fprintf x; \ 239 | } 240 | # define Tracec(c, x) \ 241 | { \ 242 | if (z_verbose > 0 && (c)) \ 243 | fprintf x; \ 244 | } 245 | # define Tracecv(c, x) \ 246 | { \ 247 | if (z_verbose > 1 && (c)) \ 248 | fprintf x; \ 249 | } 250 | #else 251 | # define Assert(cond, msg) 252 | # define Trace(x) 253 | # define Tracev(x) 254 | # define Tracevv(x) 255 | # define Tracec(c, x) 256 | # define Tracecv(c, x) 257 | #endif 258 | 259 | #ifndef Z_SOLO 260 | voidpf ZLIB_INTERNAL zcalloc OF((voidpf opaque, unsigned items, unsigned size)); 261 | void ZLIB_INTERNAL zcfree OF((voidpf opaque, voidpf ptr)); 262 | # endif 263 | 264 | # define ZALLOC(strm, items, size) (*((strm)->zalloc))((strm)->opaque, (items), (size)) 265 | # define ZFREE(strm, addr) (*((strm)->zfree))((strm)->opaque, (voidpf)(addr)) 266 | # define TRY_FREE(s, p) \ 267 | { \ 268 | if (p) \ 269 | ZFREE(s, p); \ 270 | } 271 | 272 | /* Reverse the bytes in a 32-bit value */ 273 | # define ZSWAP32(q) ((((q) >> 24) & 0xff) + (((q) >> 8) & 0xff00) + (((q)&0xff00) << 8) + (((q)&0xff) << 24)) 274 | 275 | #endif /* ZUTIL_H */ 276 | -------------------------------------------------------------------------------- /crc32.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the Fairytale project 3 | 4 | Copyright (C) 2018 Márcio Pais 5 | 6 | This library is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU Lesser General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | This library is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU Lesser General Public License for more details. 15 | 16 | You should have received a copy of the GNU Lesser General Public License 17 | along with this program. If not, see . 18 | */ 19 | 20 | #ifndef CRC32_H 21 | #define CRC32_H 22 | 23 | #include "common.h" 24 | #include "stream.h" 25 | 26 | #include 27 | 28 | class CRC32 { 29 | private: 30 | static const uint32_t CRC32LUT[]; 31 | 32 | public: 33 | static uint32_t process(Stream* stream, const off_t offset, int64_t length) { 34 | uint8_t* buffer = new uint8_t[GENERIC_BUFFER_SIZE]; 35 | uint32_t crc = 0xFFFFFFFFu; 36 | try { 37 | stream->setPos(offset); 38 | } 39 | catch (ExhaustedStorageException const&) { 40 | return ~crc; 41 | } 42 | while (length > 0) { 43 | size_t l = stream->blockRead(&buffer[0], std::min(GENERIC_BUFFER_SIZE, length)); 44 | if (l == 0) 45 | break; 46 | for (size_t i = 0; i < l; i++) 47 | crc = CRC32LUT[(crc ^ buffer[i]) & 0xFF] ^ (crc >> 8); 48 | length -= l; 49 | } 50 | delete[] buffer; 51 | return ~crc; 52 | } 53 | }; 54 | 55 | const uint32_t CRC32::CRC32LUT[] = 56 | { /* CRC polynomial 0xedb88320 */ 57 | 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f, 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988, 58 | 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2, 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7, 59 | 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9, 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172, 60 | 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c, 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59, 61 | 0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, 0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924, 62 | 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106, 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433, 63 | 0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d, 0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e, 64 | 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950, 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65, 65 | 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7, 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0, 66 | 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa, 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f, 67 | 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81, 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a, 68 | 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84, 0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1, 69 | 0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb, 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc, 70 | 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e, 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b, 71 | 0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, 0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236, 72 | 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28, 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d, 73 | 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f, 0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38, 74 | 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242, 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777, 75 | 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69, 0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2, 76 | 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc, 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9, 77 | 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693, 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94, 78 | 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d 79 | }; 80 | 81 | #endif 82 | -------------------------------------------------------------------------------- /deduper.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schnaader/fairytale/de6d7a1cde2fc3ff89d341015679332da138330f/deduper.cpp -------------------------------------------------------------------------------- /deduper.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schnaader/fairytale/de6d7a1cde2fc3ff89d341015679332da138330f/deduper.h -------------------------------------------------------------------------------- /fairytale.cpp: -------------------------------------------------------------------------------- 1 | #include "analyser.h" 2 | #include "contrib/CLI11/CLI11.hpp" 3 | 4 | #include 5 | 6 | struct Stats { 7 | uint64_t deduped, zlib, jpeg, img1, img4, img8, img8gray, img24, img32, text, dds, mod, json; 8 | struct { 9 | uint64_t zlib, jpeg, img1, img4, img8, img8gray, img24, img32, text, dds, mod, json; 10 | } totals; 11 | } stats = { 0 }; 12 | 13 | void vliEncode(uint64_t i, FileStream* stream) { 14 | while (i > 0x7F) { 15 | stream->putChar(0x80 | (i & 0x7F)); 16 | i >>= 7; 17 | } 18 | stream->putChar((uint8_t)i); 19 | } 20 | 21 | uint64_t vliDecode(uint8_t* x) { 22 | uint64_t result = 0; 23 | uint8_t i = 0; 24 | while (x[i] & 0x80) { 25 | result |= (x[i] & 0x7F) << (7 * i); 26 | ++i; 27 | } 28 | result |= (x[i] & 0x7F) << (7 * i); 29 | return result; 30 | } 31 | 32 | void assignIds(Block* block, int64_t* from) { 33 | do { 34 | block->id = (*from)++; 35 | if (block->child != nullptr) 36 | assignIds(block->child, from); 37 | block = block->next; 38 | } while (block != nullptr); 39 | } 40 | 41 | void dumpToFile(Block* block, StorageManager* manager, FileStream* stream) { 42 | uint8_t buffer[GENERIC_BUFFER_SIZE]; 43 | do { 44 | vliEncode(block->id, stream); 45 | vliEncode(static_cast(block->type), stream); 46 | bool wasDormant = false; 47 | 48 | switch (block->type) { 49 | case BlockType::DEDUP: { 50 | stats.deduped++; 51 | vliEncode(((Block*)block->info)->id, stream); 52 | break; 53 | } 54 | case BlockType::DEFLATE: { 55 | DeflateInfo* info = (DeflateInfo*)block->info; 56 | stream->putChar(info->zlibCombination); 57 | stream->putChar(info->zlibWindow); 58 | vliEncode(info->penaltyBytesUsed, stream); 59 | for (int i = 0; i < info->penaltyBytesUsed; i++) 60 | stream->putChar(info->penaltyBytes[i]); 61 | for (int i = 0; i <= info->penaltyBytesUsed; i++) 62 | vliEncode(info->posDiff[i], stream); 63 | stats.zlib++; 64 | stats.totals.zlib += block->length; 65 | break; 66 | } 67 | default: { 68 | switch (block->type) { 69 | case BlockType::IMAGE: { 70 | ImageInfo* info = (ImageInfo*)block->info; 71 | uint8_t c = (info->bpp * 2) | uint8_t(info->grayscale); 72 | stream->putChar(c); 73 | vliEncode(info->width, stream); 74 | vliEncode(info->height, stream); 75 | switch (info->bpp) { 76 | case 32: { 77 | stats.img32++; 78 | stats.totals.img32 += block->length; 79 | break; 80 | } 81 | case 24: { 82 | stats.img24++; 83 | stats.totals.img24 += block->length; 84 | break; 85 | } 86 | case 8: { 87 | if (info->grayscale) 88 | stats.img8gray++, stats.totals.img8gray += block->length; 89 | else 90 | stats.img8++, stats.totals.img8 += block->length; 91 | break; 92 | } 93 | case 4: { 94 | stats.img4++; 95 | stats.totals.img4 += block->length; 96 | break; 97 | } 98 | case 1: { 99 | stats.img1++; 100 | stats.totals.img1 += block->length; 101 | break; 102 | } 103 | } 104 | break; 105 | } 106 | case BlockType::AUDIO: { 107 | AudioInfo* info = (AudioInfo*)block->info; 108 | stream->putChar(info->mode); 109 | stats.mod++; 110 | stats.totals.mod += block->length; 111 | break; 112 | } 113 | case BlockType::DDS: { 114 | stats.dds++; 115 | stats.totals.dds += block->length; 116 | break; 117 | } 118 | case BlockType::JPEG: { 119 | stats.jpeg++; 120 | stats.totals.jpeg += block->length; 121 | break; 122 | } 123 | case BlockType::JSON: { 124 | stats.json++; 125 | stats.totals.json += block->length; 126 | break; 127 | } 128 | case BlockType::TEXT: { 129 | stats.text++; 130 | stats.totals.text += block->length; 131 | break; 132 | } 133 | default: {} 134 | } 135 | vliEncode(block->length, stream); 136 | 137 | // attempt to revive stream if needed 138 | if (block->level > 0) { 139 | if (((HybridStream*)block->data)->wasPurged()) { 140 | if (!block->attemptRevival(manager)) 141 | break; 142 | } 143 | else 144 | ((HybridStream*)block->data)->setPurgeStatus(false); 145 | } 146 | else { 147 | if ((wasDormant = ((FileStream*)block->data)->dormant()) && !manager->wakeUp((FileStream*)block->data)) 148 | break; 149 | } 150 | 151 | int64_t length = block->length; 152 | block->data->setPos(block->offset); 153 | while (length > 0) { 154 | size_t l = block->data->blockRead(&buffer[0], std::min(GENERIC_BUFFER_SIZE, length)); 155 | if (l == 0) 156 | break; 157 | stream->blockWrite(&buffer[0], l); 158 | length -= l; 159 | } 160 | } 161 | } 162 | if (block->level > 0) 163 | ((HybridStream*)block->data)->setPurgeStatus(true); 164 | else if (wasDormant) 165 | ((FileStream*)block->data)->goToSleep(); 166 | 167 | if (block->child != nullptr) 168 | dumpToFile(block->child, manager, stream); 169 | 170 | block = block->next; 171 | } while (block != nullptr); 172 | } 173 | 174 | int verbose; 175 | int main(int argc, char** argv) { 176 | #ifdef WINDOWS 177 | _setmaxstdio(2048); 178 | #endif 179 | CLI::App app{ "Fairytale Prototype v0.017 by M. Pais, 2018" }; 180 | int memory = 9; 181 | int total_storage = 9; 182 | bool brute_mode = false; 183 | bool deduplication = false; 184 | verbose = 0; 185 | std::string output_file; 186 | std::vector input_files; 187 | app.add_option("-m,--memory", memory, "Memory cache coefficient [4MB..2048MB]", 9)->check(CLI::Range(0, 9)); 188 | app.add_option("-t,--total-storage", total_storage, "Total storage coefficient [8MB..4096MB]", 9)->check(CLI::Range(0, 9)); 189 | app.add_flag("-b,--brute", brute_mode, "Brute force DEFLATE streams"); 190 | app.add_flag("-d,--deduplication", deduplication, "Perform deduplication stage"); 191 | app.add_flag("-v,--verbose", verbose, "Enable verbose output. Specify twice for more verbosity"); 192 | app.add_option("output_file,-o", output_file, "output_file")->required(); 193 | app.add_option("input_files,-i,", input_files, "input_files")->required(); 194 | CLI11_PARSE(app, argc, argv); 195 | if (memory > total_storage) { 196 | printf("Error: Total storage coefficient must be higher than memory cache coefficient"); 197 | return 0; 198 | } 199 | 200 | clock_t start_time = clock(); 201 | FileStream output; 202 | output.create(output_file.c_str()); 203 | 204 | Array input(input_files.size()); 205 | Block* block = new Block{ 0 }; 206 | Block* root = block; 207 | for (size_t i = 0; i < input_files.size(); i++) { 208 | input[i] = new FileStream; 209 | if (!input[i]->open(input_files[i].c_str())) { 210 | printf("File not found: %s\n", input_files[i].c_str()); 211 | getchar(); 212 | return 0; 213 | } 214 | block->data = input[i]; 215 | block->length = block->data->getSize(); 216 | printf("Loaded %s, (%" PRIu64 " bytes), hashing... ", input_files[i].c_str(), block->length); 217 | block->calculateHash(); 218 | printf("done\n"); 219 | if (i > 0) 220 | input[i]->goToSleep(); 221 | if (i + 1 < input_files.size()) { 222 | block->next = new Block{ 0 }; 223 | block = block->next; 224 | } 225 | } 226 | block->next = nullptr; 227 | block = root; 228 | 229 | StorageManager pool(1ull << (22 + memory), 1ull << (23 + total_storage)); 230 | Deduper deduper; 231 | Array parsers(0); 232 | parsers.push_back(Parsers::JPEG_PROGRESSIVE); 233 | parsers.push_back(brute_mode ? Parsers::DEFLATE_BRUTE : Parsers::DEFLATE); 234 | parsers.push_back(Parsers::BITMAP_NOHDR); 235 | parsers.push_back(Parsers::JSON); 236 | parsers.push_back(Parsers::TEXT); 237 | parsers.push_back(Parsers::DDS); 238 | parsers.push_back(Parsers::MOD); 239 | Analyser analyser(&parsers); 240 | analyser.analyse(block, &pool, deduplication ? &deduper : nullptr); 241 | 242 | int64_t id = 0; 243 | assignIds(block, &id); 244 | try { 245 | dumpToFile(block, &pool, &output); 246 | } 247 | catch (...) { 248 | printf("\n\nError writing output file!"); 249 | return 0; 250 | } 251 | uint64_t total = output.getSize(); 252 | printf("\n"); 253 | if (stats.zlib > 0) 254 | printf("DEFLATE streams found: %" PRIu64 " (%" PRIu64 " bytes)\n", stats.zlib, stats.totals.zlib); 255 | if (stats.jpeg > 0) 256 | printf("JPEG streams found: %" PRIu64 " (%" PRIu64 " bytes)\n", stats.jpeg, stats.totals.jpeg); 257 | if (stats.img32 + stats.img24 + stats.totals.img8 + stats.img4 + stats.img1 > 0) 258 | printf("Uncompressed image streams stats:\n"); 259 | if (stats.img32 > 0) 260 | printf("%" PRIu64 " @32bpp (%" PRIu64 " bytes)\n", stats.img32, stats.totals.img32); 261 | if (stats.img24 > 0) 262 | printf("%" PRIu64 " @24bpp (%" PRIu64 " bytes)\n", stats.img24, stats.totals.img24); 263 | if (stats.totals.img8 > 0) 264 | printf("%" PRIu64 " @8bpp palette-indexed, %" PRIu64 " @8bpp grayscale (%" PRIu64 " bytes)\n", stats.img8, stats.img8gray, stats.totals.img8); 265 | if (stats.img4 > 0) 266 | printf("%" PRIu64 " @4bpp (%" PRIu64 " bytes)\n", stats.img4, stats.totals.img4); 267 | if (stats.img1 > 0) 268 | printf("%" PRIu64 " @1bpp (%" PRIu64 " bytes)\n", stats.img1, stats.totals.img1); 269 | if (stats.dds > 0) 270 | printf("DDS textures found: %" PRIu64 ", (%" PRIu64 " bytes)\n", stats.dds, stats.totals.dds); 271 | if (stats.mod > 0) 272 | printf("MOD audio streams found: %" PRIu64 ", (%" PRIu64 " bytes)\n", stats.mod, stats.totals.mod); 273 | if (stats.json > 0) 274 | printf("JSON objects found: %" PRIu64 ", (%" PRIu64 " bytes)\n", stats.json, stats.totals.json); 275 | if (stats.text > 0) 276 | printf("Text streams found: %" PRIu64 " (%" PRIu64 " bytes)\n\n", stats.text, stats.totals.text); 277 | printf("\nDone in %1.2f sec, %" PRIu64 " bytes, %" PRIu64 " blocks were deduped", double(clock() - start_time) / CLOCKS_PER_SEC, total, stats.deduped); 278 | getchar(); 279 | return 0; 280 | } 281 | -------------------------------------------------------------------------------- /filestream.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schnaader/fairytale/de6d7a1cde2fc3ff89d341015679332da138330f/filestream.cpp -------------------------------------------------------------------------------- /filestream.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schnaader/fairytale/de6d7a1cde2fc3ff89d341015679332da138330f/filestream.h -------------------------------------------------------------------------------- /hybridstream.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schnaader/fairytale/de6d7a1cde2fc3ff89d341015679332da138330f/hybridstream.cpp -------------------------------------------------------------------------------- /hybridstream.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schnaader/fairytale/de6d7a1cde2fc3ff89d341015679332da138330f/hybridstream.h -------------------------------------------------------------------------------- /image.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the Fairytale project 3 | 4 | Copyright (C) 2018 Márcio Pais 5 | 6 | This library is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU Lesser General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | This library is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU Lesser General Public License for more details. 15 | 16 | You should have received a copy of the GNU Lesser General Public License 17 | along with this program. If not, see . 18 | */ 19 | 20 | #ifndef IMAGE_H 21 | #define IMAGE_H 22 | 23 | #include "stream.h" 24 | 25 | class Image { 26 | public: 27 | static bool isGrayscalePalette(Stream* input, const int n = 256, const bool isRGBA = false) { 28 | off_t offset = input->curPos(); 29 | int stride = 3 + (int)isRGBA, res = (n > 0) << 8, order = 1; 30 | for (int i = 0; i < n * stride && (res >> 8) > 0; i++) { 31 | int b = input->getChar(); 32 | if (b == EOF) { 33 | res = 0; 34 | break; 35 | } 36 | if (i == 0) { 37 | res = 0x100 | b; 38 | order = 1 - 2 * (b > 0); 39 | continue; 40 | } 41 | 42 | //"j" is the index of the current byte in this color entry 43 | int j = i % stride; 44 | if (j == 0) { 45 | // load first component of this entry 46 | res = (res & ((b - (res & 0xFF) == order) << 8)); 47 | res |= (res) ? b : 0; 48 | } 49 | else if (j == 3) 50 | res &= ((b == 0 || b == 0xFF) * 0x1FF); // alpha/attribute component must be zero or 0xFF 51 | else 52 | res &= ((b == (res & 0xFF)) * 0x1FF); 53 | } 54 | input->setPos(offset); 55 | return (res >> 8) > 0; 56 | } 57 | }; 58 | 59 | #endif 60 | -------------------------------------------------------------------------------- /parser.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schnaader/fairytale/de6d7a1cde2fc3ff89d341015679332da138330f/parser.h -------------------------------------------------------------------------------- /parsers/bitmapparser.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schnaader/fairytale/de6d7a1cde2fc3ff89d341015679332da138330f/parsers/bitmapparser.h -------------------------------------------------------------------------------- /parsers/ddsparser.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schnaader/fairytale/de6d7a1cde2fc3ff89d341015679332da138330f/parsers/ddsparser.cpp -------------------------------------------------------------------------------- /parsers/ddsparser.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schnaader/fairytale/de6d7a1cde2fc3ff89d341015679332da138330f/parsers/ddsparser.h -------------------------------------------------------------------------------- /parsers/deflateparser.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the Fairytale project 3 | 4 | Copyright (C) 2018 Márcio Pais 5 | 6 | This library is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU Lesser General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | This library is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU Lesser General Public License for more details. 15 | 16 | You should have received a copy of the GNU Lesser General Public License 17 | along with this program. If not, see . 18 | */ 19 | 20 | #ifndef DEFLATEPARSER_H 21 | #define DEFLATEPARSER_H 22 | 23 | #include "../parser.h" 24 | #include "../transforms/zlibtransform.h" 25 | 26 | #include 27 | 28 | #define WINDOW_LOOKBACK 32 29 | #define BRUTE_LOOKBACK 256 30 | #define WINDOW_SIZE (BRUTE_LOOKBACK + WINDOW_LOOKBACK) 31 | #define WINDOW_ACCESS_MASK (BRUTE_LOOKBACK - 1) 32 | #define BRUTE_ROUNDS (BRUTE_LOOKBACK >> 6) 33 | #define BUFFER(x) (window[(wndPos - WINDOW_LOOKBACK + (x)) & WINDOW_ACCESS_MASK]) 34 | #if (BRUTE_LOOKBACK & WINDOW_ACCESS_MASK) || (BRUTE_LOOKBACK <= 64) 35 | # error BRUTE_LOOKBACK must be a power of 2 bigger than 64 36 | #endif 37 | #if (WINDOW_LOOKBACK > BRUTE_LOOKBACK) || (WINDOW_LOOKBACK < 32) 38 | # error WINDOW_LOOKBACK cannot be bigger than BRUTE_LOOKBACK and must be >=32 39 | #endif 40 | 41 | template class DeflateParser : public Parser { 42 | private: 43 | zLibTransform transform; 44 | uint8_t buffer[GENERIC_BUFFER_SIZE]; 45 | uint8_t window[WINDOW_SIZE], blockIn[ZLIB_BLOCK_SIZE], blockOut[ZLIB_BLOCK_SIZE]; 46 | bool skipPos[BRUTE_LOOKBACK]; 47 | int histogram[256]; 48 | struct gZip { 49 | enum Flags 50 | { 51 | NONE = 0, 52 | CRC = 2, 53 | EXTRA = 4, 54 | NAME = 8, 55 | COMMENT = 16 56 | } flags; 57 | off_t offset; 58 | uint8_t options; 59 | } gzip; 60 | off_t zipPos; 61 | off_t index, position; 62 | int wndPos; 63 | void clearBuffers() { 64 | memset(&blockIn[0], 0, ZLIB_BLOCK_SIZE); 65 | memset(&blockOut[0], 0, ZLIB_BLOCK_SIZE); 66 | memset(&window[0], 0, WINDOW_SIZE); 67 | memset(&skipPos[0], 0, BRUTE_LOOKBACK * sizeof(bool)); 68 | memset(&histogram[0], 0, 256 * sizeof(int)); 69 | memset(&gzip, 0, sizeof(gZip)); 70 | wndPos = 0, zipPos = 0; 71 | } 72 | void processByte(const uint8_t b) { 73 | histogram[b]++; 74 | if (index >= 256) { 75 | assert(histogram[window[wndPos]] > 0); 76 | histogram[window[wndPos]]--; 77 | } 78 | window[wndPos] = b; 79 | if (wndPos < WINDOW_LOOKBACK) 80 | window[wndPos + BRUTE_LOOKBACK] = b; 81 | wndPos = (wndPos + 1) & WINDOW_ACCESS_MASK; 82 | } 83 | void doBruteModeSearch(bool* result) { 84 | if (skipPos[wndPos]) { 85 | skipPos[wndPos] = false; 86 | return; 87 | } 88 | uint8_t BTYPE = (window[wndPos] & 7) >> 1; 89 | if (((*result) = (BTYPE == 1 || BTYPE == 2))) { 90 | int maximum = 0, used = 0, offset = wndPos; 91 | for (int i = 0; i < BRUTE_ROUNDS; i++, offset += 64) { 92 | for (int j = 0; j < 64; j++) { 93 | int freq = histogram[window[(offset + j) & WINDOW_ACCESS_MASK]]; 94 | used += (freq > 0); 95 | maximum += (freq > maximum); 96 | } 97 | if (maximum >= ((12 + i) << i) || (used * (6 - i)) < ((i + 1) * 64)) { 98 | (*result) = false; 99 | break; 100 | } 101 | } 102 | } 103 | } 104 | inline bool validate(const uint32_t in, const uint32_t out, const bool brute) { 105 | return (in > std::max((uint32_t)32ul / std::max((uint32_t)1ul, out / std::max((uint32_t)1ul, in)), uint32_t(brute) << 7)); 106 | } 107 | void getStreamInfo(Block* block, DeflateInfo* info, const bool brute) { 108 | int ret = 0; 109 | z_stream strm; 110 | strm.zalloc = Z_NULL, strm.zfree = Z_NULL, strm.opaque = Z_NULL, strm.next_in = Z_NULL, strm.avail_in = 0; 111 | // Quick check possible stream by decompressing first WINDOW_LOOKBACK bytes 112 | if (transform.inflateInitAs(&strm, info->zlibParameters) == Z_OK) { 113 | strm.next_in = &window[(wndPos - (brute ? 0 : WINDOW_LOOKBACK)) & WINDOW_ACCESS_MASK], strm.avail_in = WINDOW_LOOKBACK; 114 | strm.next_out = blockOut, strm.avail_out = ZLIB_BLOCK_SIZE; 115 | ret = inflate(&strm, Z_FINISH); 116 | ret = (inflateEnd(&strm) == Z_OK && (ret == Z_STREAM_END || ret == Z_BUF_ERROR) && strm.total_in >= 16); 117 | } 118 | // Verify valid stream and determine stream length 119 | if (ret) { 120 | strm.zalloc = Z_NULL, strm.zfree = Z_NULL, strm.opaque = Z_NULL; 121 | strm.next_in = Z_NULL, strm.avail_in = 0, strm.total_in = strm.total_out = 0; 122 | if (transform.inflateInitAs(&strm, info->zlibParameters) == Z_OK) { 123 | try { 124 | block->data->setPos(position - (brute ? BRUTE_LOOKBACK : WINDOW_LOOKBACK)); 125 | } 126 | catch (ExhaustedStorageException const&) { 127 | return; 128 | } 129 | do { 130 | size_t blockSize = block->data->blockRead(blockIn, ZLIB_BLOCK_SIZE); 131 | strm.next_in = blockIn, strm.avail_in = (uint32_t)blockSize; 132 | do { 133 | strm.next_out = blockOut, strm.avail_out = ZLIB_BLOCK_SIZE; 134 | ret = inflate(&strm, Z_FINISH); 135 | } while (strm.avail_out == 0 && ret == Z_BUF_ERROR); 136 | if (ret == Z_STREAM_END && validate(strm.total_in, strm.total_out, brute)) 137 | info->lengthIn = strm.total_in, info->lengthOut = strm.total_out; 138 | if (ret != Z_BUF_ERROR) 139 | break; 140 | } while (!block->data->eof()); 141 | if (inflateEnd(&strm) != Z_OK) 142 | info->lengthIn = info->lengthOut = 0; 143 | } 144 | } 145 | } 146 | 147 | public: 148 | explicit DeflateParser(void) : window{ 0 }, blockIn{ 0 }, blockOut{ 0 }, histogram{ 0 }, gzip{ gZip::NONE, 0, 0 }, zipPos(0), index(0), wndPos(0) { 149 | priority = PARSER_PRIORITY_DEFLATE; 150 | } 151 | 152 | bool parse(Block* block, ParseData* data, StorageManager* manager) { 153 | assert(!block->done); 154 | assert(block->type == BlockType::DEFAULT); 155 | assert(block->data != nullptr && (block->level == 0 || !((HybridStream*)block->data)->wasPurged())); 156 | int64_t i = 0, l = block->length; 157 | if (l < WINDOW_LOOKBACK) 158 | return false; 159 | index = 0; 160 | bool res = false; 161 | clearBuffers(); 162 | position = block->offset; 163 | try { 164 | block->data->setPos(position); 165 | } 166 | catch (ExhaustedStorageException const&) { 167 | return false; 168 | } 169 | while (i < l) { 170 | int k = 0, bytesRead = (int)block->data->blockRead(&buffer[0], GENERIC_BUFFER_SIZE); 171 | while (k < bytesRead && i < l) { 172 | processByte(buffer[k++]); 173 | index++, i++, position++; 174 | 175 | data->deflate.zlibParameters = transform.parse_zlib_header((BUFFER(0) << 8) | BUFFER(1)); 176 | data->deflate.lengthIn = 0, data->deflate.lengthOut = 0; 177 | bool valid = (index >= (WINDOW_LOOKBACK - 1) && data->deflate.zlibParameters != -1); 178 | 179 | if (BruteMode && !valid && index >= WINDOW_ACCESS_MASK) 180 | doBruteModeSearch(&valid); 181 | bool brute = (data->deflate.zlibParameters == -1 && index != zipPos && index != gzip.offset); 182 | 183 | if (valid || (zipPos > 0 && index == zipPos) || (gzip.offset > 0 && index == gzip.offset)) { 184 | skipPos[(wndPos - WINDOW_LOOKBACK) & WINDOW_ACCESS_MASK] = !brute; 185 | getStreamInfo(block, &data->deflate, brute); 186 | } 187 | 188 | if (data->deflate.lengthIn > 0) { 189 | off_t offset = position - (brute ? BRUTE_LOOKBACK : WINDOW_LOOKBACK); 190 | try { 191 | block->data->setPos(offset); 192 | } 193 | catch (ExhaustedStorageException const&) { 194 | break; 195 | } 196 | HybridStream* output = transform.attempt(block->data, manager, &data->deflate); 197 | if (output != nullptr) { 198 | LOG( 199 | "zLib stream found at %" PRIu64 ", length %u bytes, decompresses to %u bytes, %d penalty bytes\n", offset, data->deflate.lengthIn, 200 | data->deflate.lengthOut, data->deflate.penaltyBytesUsed); 201 | res = true; 202 | block = block->segmentAround(offset, data->deflate.lengthIn, BlockType::DEFLATE, &data->deflate, sizeof(DeflateInfo), output); 203 | output->setPriority(STREAM_PRIORITY_HIGH); 204 | if (block == nullptr) 205 | return res; 206 | } 207 | // we succeeded, or it was a valid stream but we didn't have enough storage budget for it, so skip it anyway 208 | if (output != nullptr || data->deflate.zlibCombination != 0xFF) { 209 | index = 0; 210 | i += data->deflate.lengthIn, i -= (brute ? BRUTE_LOOKBACK : WINDOW_LOOKBACK); 211 | position = offset + data->deflate.lengthIn; 212 | clearBuffers(); 213 | bytesRead = 0; // to ensure we break 214 | } 215 | } 216 | 217 | if (index > gzip.offset) 218 | gzip.offset = 0; 219 | if (index > zipPos) 220 | zipPos = 0; 221 | 222 | if (index > 0 && data->deflate.zlibParameters == -1) { 223 | // detect ZIP streams 224 | if (zipPos == 0 && BUFFER(0) == 'P' && BUFFER(1) == 'K' && BUFFER(2) == '\x3' && BUFFER(3) == '\x4' && BUFFER(8) == '\x8' && BUFFER(9) == '\0') { 225 | int nlen = (int)BUFFER(26) + (int)BUFFER(27) * 256 + (int)BUFFER(28) + (int)BUFFER(29) * 256; 226 | if (nlen < 256 && block->offset + index + 30 + nlen < (off_t)block->data->getSize()) 227 | zipPos = index + 30 + nlen; 228 | } 229 | // detect gZip streams 230 | else if (gzip.offset == 0 && BUFFER(0) == 0x1F && BUFFER(1) == 0x8B && BUFFER(2) == 0x08 && ((gzip.options = BUFFER(3)) & 0xC0) == 0) { 231 | gzip.offset = index + 10; 232 | if (gzip.options & gZip::EXTRA) 233 | gzip.offset += 2 + BUFFER(10) + BUFFER(11) * 256; 234 | 235 | if (gzip.offset >= (off_t)block->length) 236 | gzip.offset = 0; 237 | else { 238 | try { 239 | block->data->setPos(position + gzip.offset - (2 * WINDOW_LOOKBACK - 1)); 240 | } 241 | catch (ExhaustedStorageException const&) { 242 | break; 243 | } 244 | 245 | if (gzip.options & gZip::NAME) { 246 | do { 247 | gzip.offset++; 248 | } while (block->data->getChar() > 0); 249 | if (gzip.offset >= (off_t)block->length) 250 | gzip.offset = 0; 251 | } 252 | if (gzip.offset && gzip.options & gZip::COMMENT) { 253 | do { 254 | gzip.offset++; 255 | } while (block->data->getChar() > 0); 256 | if (gzip.offset >= (off_t)block->length) 257 | gzip.offset = 0; 258 | } 259 | if (gzip.offset && gzip.options & gZip::CRC) { 260 | if ((gzip.offset += 2) >= (off_t)block->length) 261 | gzip.offset = 0; 262 | } 263 | } 264 | } 265 | } 266 | } 267 | if (i < l) { 268 | try { 269 | block->data->setPos(position); 270 | } 271 | catch (ExhaustedStorageException const&) { 272 | return res; 273 | } 274 | } 275 | } 276 | return res; 277 | } 278 | }; 279 | 280 | # undef WINDOW_SIZE 281 | # undef WINDOW_ACCESS_MASK 282 | # undef BUFFER 283 | 284 | #endif 285 | -------------------------------------------------------------------------------- /parsers/jpegparser.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schnaader/fairytale/de6d7a1cde2fc3ff89d341015679332da138330f/parsers/jpegparser.h -------------------------------------------------------------------------------- /parsers/jsonparser.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the Fairytale project 3 | 4 | Copyright (C) 2018 Andrew Epstein 5 | 6 | This library is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU Lesser General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | This library is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU Lesser General Public License for more details. 15 | 16 | You should have received a copy of the GNU Lesser General Public License 17 | along with this program. If not, see . 18 | */ 19 | 20 | #include "jsonparser.h" 21 | 22 | JsonParser::JsonParser(void) { 23 | priority = PARSER_PRIORITY_JSON; 24 | } 25 | 26 | bool JsonParser::parse(Block* block, ParseData* data, StorageManager* manager) { 27 | assert(!block->done); 28 | assert(block->type == BlockType::DEFAULT); 29 | assert(block->data != nullptr && (block->level == 0 || !((HybridStream*)block->data)->wasPurged())); 30 | int64_t i = 0; 31 | int64_t blockLength = block->length; 32 | position = block->offset; 33 | int32_t curlyBraceCount = 0; 34 | int32_t squareBracketCount = 0; 35 | int64_t firstBracePosition = -1; 36 | int64_t lastBracePosition = 0; 37 | uint64_t quoteCount = 0; 38 | try { 39 | block->data->setPos(position); 40 | } 41 | catch (ExhaustedStorageException const&) { 42 | return false; 43 | } 44 | while (i < blockLength) { 45 | int k = 0; 46 | auto bytesRead = (int)block->data->blockRead(&buffer[0], GENERIC_BUFFER_SIZE); 47 | while (k < bytesRead && i < blockLength) { 48 | uint8_t c = buffer[k++]; 49 | switch (c) { 50 | case '{': { 51 | curlyBraceCount++; 52 | if (firstBracePosition == -1) { 53 | squareBracketCount = 0; 54 | firstBracePosition = position; 55 | } 56 | break; 57 | } 58 | case '}': { 59 | curlyBraceCount--; 60 | if (firstBracePosition != -1) 61 | lastBracePosition = position + 1; 62 | break; 63 | } 64 | case '"': { 65 | quoteCount++; 66 | break; 67 | } 68 | case '[': { 69 | squareBracketCount++; 70 | break; 71 | } 72 | case ']': { 73 | squareBracketCount--; 74 | break; 75 | } 76 | default: break; 77 | } 78 | i++; 79 | position++; 80 | } 81 | } 82 | if (curlyBraceCount == 0 && squareBracketCount == 0 && (quoteCount % 2 == 0) && firstBracePosition != -1 && lastBracePosition > firstBracePosition) { 83 | LOG("Possible JSON detection at %" PRIu64 ", %" PRIu64 " bytes\n", firstBracePosition, lastBracePosition - firstBracePosition); 84 | block = block->segmentAround(firstBracePosition, lastBracePosition - firstBracePosition, BlockType::JSON); 85 | return true; 86 | } 87 | 88 | return false; 89 | } 90 | -------------------------------------------------------------------------------- /parsers/jsonparser.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the Fairytale project 3 | 4 | Copyright (C) 2018 Andrew Epstein 5 | 6 | This library is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU Lesser General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | This library is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU Lesser General Public License for more details. 15 | 16 | You should have received a copy of the GNU Lesser General Public License 17 | along with this program. If not, see . 18 | */ 19 | 20 | #ifndef JSONPARSER_H 21 | #define JSONPARSER_H 22 | 23 | #include "../parser.h" 24 | 25 | class JsonParser : public Parser { 26 | private: 27 | uint8_t buffer[GENERIC_BUFFER_SIZE]; 28 | off_t position; 29 | 30 | public: 31 | explicit JsonParser(void); 32 | bool parse(Block* block, ParseData* data, StorageManager* manager); 33 | }; 34 | 35 | #endif 36 | -------------------------------------------------------------------------------- /parsers/modparser.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schnaader/fairytale/de6d7a1cde2fc3ff89d341015679332da138330f/parsers/modparser.cpp -------------------------------------------------------------------------------- /parsers/modparser.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schnaader/fairytale/de6d7a1cde2fc3ff89d341015679332da138330f/parsers/modparser.h -------------------------------------------------------------------------------- /parsers/textparser.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schnaader/fairytale/de6d7a1cde2fc3ff89d341015679332da138330f/parsers/textparser.cpp -------------------------------------------------------------------------------- /parsers/textparser.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schnaader/fairytale/de6d7a1cde2fc3ff89d341015679332da138330f/parsers/textparser.h -------------------------------------------------------------------------------- /storagemanager.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schnaader/fairytale/de6d7a1cde2fc3ff89d341015679332da138330f/storagemanager.cpp -------------------------------------------------------------------------------- /storagemanager.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schnaader/fairytale/de6d7a1cde2fc3ff89d341015679332da138330f/storagemanager.h -------------------------------------------------------------------------------- /stream.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schnaader/fairytale/de6d7a1cde2fc3ff89d341015679332da138330f/stream.h -------------------------------------------------------------------------------- /structs.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schnaader/fairytale/de6d7a1cde2fc3ff89d341015679332da138330f/structs.h -------------------------------------------------------------------------------- /test/testMTFList.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "../transforms/mtflist.h" 4 | 5 | TEST(MTFList, CreatedInOrder) { 6 | MTFList<4> MTF; 7 | ASSERT_EQ(MTF.getFirst(), 0); 8 | ASSERT_EQ(MTF.getNext(), 1); 9 | ASSERT_EQ(MTF.getNext(), 2); 10 | ASSERT_EQ(MTF.getNext(), 3); 11 | ASSERT_EQ(MTF.getNext(), -1); 12 | } 13 | 14 | TEST(MTFList, MoveToFront) { 15 | MTFList<4> MTF; 16 | MTF.moveToFront(2); 17 | ASSERT_EQ(MTF.getFirst(), 2); 18 | ASSERT_EQ(MTF.getNext(), 0); 19 | ASSERT_EQ(MTF.getNext(), 1); 20 | ASSERT_EQ(MTF.getNext(), 3); 21 | ASSERT_EQ(MTF.getNext(), -1); 22 | } 23 | -------------------------------------------------------------------------------- /test/testMain.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(int argc, char** argv) { 4 | testing::InitGoogleTest(&argc, argv); 5 | return RUN_ALL_TESTS(); 6 | } 7 | -------------------------------------------------------------------------------- /transform.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schnaader/fairytale/de6d7a1cde2fc3ff89d341015679332da138330f/transform.h -------------------------------------------------------------------------------- /transformfactory.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schnaader/fairytale/de6d7a1cde2fc3ff89d341015679332da138330f/transformfactory.h -------------------------------------------------------------------------------- /transforms/mtflist.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the Fairytale project 3 | 4 | Copyright (C) 2018 Márcio Pais 5 | 6 | This library is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU Lesser General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | This library is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU Lesser General Public License for more details. 15 | 16 | You should have received a copy of the GNU Lesser General Public License 17 | along with this program. If not, see . 18 | */ 19 | 20 | #ifndef MTFLIST_H 21 | #define MTFLIST_H 22 | 23 | template class MTFList { 24 | static_assert(size > 0, "MTF list size must be a positive integer"); 25 | 26 | private: 27 | struct MTFItem { 28 | int next, previous; 29 | }; 30 | MTFItem list[size]; 31 | int root, index; 32 | 33 | public: 34 | MTFList() : root(0), index(0) { 35 | for (int i = 0; i < size; i++) { 36 | list[i].next = i + 1; 37 | list[i].previous = i - 1; 38 | } 39 | list[size - 1].next = -1; 40 | } 41 | inline int getFirst() { 42 | index = root; 43 | return index; 44 | } 45 | inline int getNext() { 46 | if (index >= 0) 47 | index = list[index].next; 48 | return index; 49 | } 50 | inline void moveToFront(const int i) { 51 | index = i; 52 | if (index == root) 53 | return; 54 | list[list[index].previous].next = list[index].next; 55 | if (list[index].next >= 0) 56 | list[list[index].next].previous = list[index].previous; 57 | list[root].previous = index; 58 | list[index].next = root; 59 | root = index; 60 | list[root].previous = -1; 61 | } 62 | }; 63 | 64 | #endif // MTFLIST_H 65 | -------------------------------------------------------------------------------- /transforms/zlibtransform.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schnaader/fairytale/de6d7a1cde2fc3ff89d341015679332da138330f/transforms/zlibtransform.cpp -------------------------------------------------------------------------------- /transforms/zlibtransform.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of the Fairytale project 3 | 4 | Copyright (C) 2018 Márcio Pais 5 | 6 | This library is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU Lesser General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | This library is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU Lesser General Public License for more details. 15 | 16 | You should have received a copy of the GNU Lesser General Public License 17 | along with this program. If not, see . 18 | */ 19 | 20 | #ifndef ZLIBTRANSFORM_H 21 | #define ZLIBTRANSFORM_H 22 | 23 | #include 24 | #include "../structs.h" 25 | #include "../transform.h" 26 | #include "mtflist.h" 27 | 28 | #define ZLIB_BLOCK_SIZE (0x10000ul) 29 | #define ZLIB_NUM_COMBINATIONS 81 30 | 31 | class zLibTransform : public Transform { 32 | private: 33 | uint8_t blockIn[ZLIB_BLOCK_SIZE * 2]; 34 | uint8_t blockOut[ZLIB_BLOCK_SIZE]; 35 | uint8_t blockRec[ZLIB_BLOCK_SIZE * 2]; 36 | uint8_t penaltyBytes[ZLIB_NUM_COMBINATIONS * ZLIB_MAX_PENALTY_BYTES]; 37 | uint32_t diffPos[ZLIB_NUM_COMBINATIONS * ZLIB_MAX_PENALTY_BYTES]; 38 | z_stream main_strm, rec_strm[ZLIB_NUM_COMBINATIONS]; 39 | int diffCount[ZLIB_NUM_COMBINATIONS], recPos[ZLIB_NUM_COMBINATIONS]; 40 | MTFList MTF; 41 | int ret; 42 | void clearBuffers(); 43 | void setupStream(z_streamp strm); 44 | inline bool validate(const uint32_t in, const uint32_t out, const uint8_t penalty) { 45 | return (penalty < ZLIB_MAX_PENALTY_BYTES) && (int64_t(in * 8) < int64_t(out * 9 + 8 + penalty * 5) || in > 512u); 46 | } 47 | 48 | public: 49 | int parse_zlib_header(const uint16_t header); 50 | int inflateInitAs(z_streamp strm, int parameters); 51 | HybridStream* attempt(Stream* input, StorageManager* manager, void* info = nullptr); 52 | bool apply(Stream* input, Stream* output, void* info = nullptr); 53 | bool undo(Stream* input, Stream* output, void* info = nullptr); 54 | }; 55 | 56 | #endif 57 | --------------------------------------------------------------------------------