92 Commits
1.1.0 ... 2.1.0

Author SHA1 Message Date
Jack Andersen
602f6b6e15 bumped version to 2.1.0 2015-07-21 09:32:11 -10:00
Jack Andersen
78286b0f67 corrected type mismatch 2015-07-21 09:26:02 -10:00
Jack Andersen
c6a6d3b9c4 renamed read/writeUnicode methods to be more reflective of functionality 2015-07-21 09:04:25 -10:00
Jack Andersen
6405bffdd2 Un-derpified major derp 2015-07-19 17:48:44 -10:00
Jack Andersen
7442d618e7 Added virtual destructor to IAES 2015-07-15 16:01:37 -10:00
Jack Andersen
f3ba8819a4 fixed incomplete-type warnings involving recursively-derived DNA types 2015-07-14 12:57:42 -10:00
Jack Andersen
24fedff58e specified release build for PKGBUILD 2015-07-14 10:47:00 -10:00
Jack Andersen
87306a18d8 minor AES optimization 2015-07-13 16:23:36 -10:00
Jack Andersen
09e3d33ff2 spelling corrections 2015-07-13 14:39:51 -10:00
Jack Andersen
11331e068e restored c++ abi to system default 2015-07-12 08:06:07 -10:00
Jack Andersen
7ec5a5971a compile-time system endian check 2015-07-12 06:51:04 -10:00
Jack Andersen
7ef451c86a Merge branch 'master' of https://github.com/libAthena/Athena 2015-07-11 20:05:37 -10:00
Jack Andersen
ece50c6968 forced c++ abi to c++11 2015-07-11 20:05:16 -10:00
4bc6004a6a Prevent CMake from failing if an empty atdna directory exists 2015-07-11 16:01:01 -07:00
Jack Andersen
55adeec69c windows packaging and CMake adjustments 2015-07-10 16:14:06 -10:00
4f8df65a3d * add atVec2f 2015-07-10 18:45:26 -07:00
8d3524b1f6 Fix sign bug
Prevent blockSize from exceeding file's length
2015-07-09 20:47:56 -07:00
a7c180db00 * Add support for cached reads 2015-07-09 20:32:55 -07:00
Jack Andersen
a19dcad876 corrected include path for CMake export 2015-07-08 16:09:57 -10:00
Jack Andersen
3ce2062686 removed unnecessary header glob 2015-07-08 15:50:16 -10:00
Jack Andersen
f874dfff31 removed qt files 2015-07-08 15:41:51 -10:00
Jack Andersen
e45679ffba PKGBUILD fix 2015-07-08 15:29:08 -10:00
Jack Andersen
1e55ce5138 Merge branch 'copy-streamers' 2015-07-08 15:15:56 -10:00
Jack Andersen
35a5736c35 Merge branch 'master' of https://github.com/libAthena/Athena 2015-07-08 15:15:48 -10:00
Jack Andersen
b7ba46b995 MemoryCopy streamers and CMake features 2015-07-08 15:14:02 -10:00
Jack Andersen
c65ef591f5 significant virtual-method refactor to streaming interface 2015-07-08 15:13:14 -10:00
Jack Andersen
1b40613c05 Update README.md 2015-07-08 15:12:24 -10:00
Jack Andersen
dde7164651 Updated PKGBUILD 2015-07-08 15:08:29 -10:00
Jack Andersen
916515f0d7 OS X build fixes 2015-07-08 14:57:51 -10:00
Jack Andersen
e1f46b824f added pkgconfig install to CMake 2015-07-08 14:01:05 -10:00
Jack Andersen
c49ec5c13d CMake package exports 2015-07-08 13:53:40 -10:00
Jack Andersen
8ca702785c added icons; updated PKGBUILD for CMake 2015-07-08 10:21:50 -10:00
Jack Andersen
44e25e09ca updated MemoryCopyWriter 2015-07-07 20:34:59 -10:00
Jack Andersen
ec38ba088e added buffer-copying subclass to MemoryReader/Writer 2015-07-07 20:03:08 -10:00
Jack Andersen
108a3aa6c1 Update README.md 2015-07-07 18:36:29 -10:00
Jack Andersen
8a397a1622 Merge pull request #18 from libAthena/new-streams
significant virtual-method refactor to streaming interface
2015-07-07 21:16:04 -07:00
Jack Andersen
97dfabb265 significant virtual-method refactor to streaming interface 2015-07-07 18:01:13 -10:00
Jack Andersen
ce917d4aca Added CMakeLists.txt 2015-07-06 17:23:37 -10:00
Jack Andersen
6ac88b37f4 Merge pull request #17 from libAthena/opt
Various Header Optimizations
2015-07-02 16:43:53 -07:00
Jack Andersen
a63da8db2f header-based read/write optimizations 2015-07-02 13:36:17 -10:00
Jack Andersen
2b37765eff nested type ordering fix 2015-06-30 21:50:58 -07:00
Jack Andersen
fa016115c2 const buffer pointer for write[U]Bytes 2015-06-30 17:01:04 -10:00
a76a3c584a * Revert swapFloat/Double "fixes" 2015-06-30 19:09:35 -07:00
Jack Andersen
bfa08ce52b Merge pull request #16 from libAthena/dna-templates
Made String and Buffer types templates with DNA base
2015-06-26 14:38:33 -07:00
Jack Andersen
7524b1f063 made string and buffer types templates with DNA base; may now be used in Vector 2015-06-25 09:37:31 -10:00
Jack Andersen
49937cba50 Update README.md 2015-06-22 14:46:45 -10:00
Jack Andersen
3ff34ec749 removed atdna from Athena's travis 2015-06-22 14:28:29 -10:00
Jack Andersen
0bb90ff2e0 spun off atdna into separate repo 2015-06-22 14:26:43 -10:00
Jack Andersen
071d91a671 more stable atdna type canonicalization; recursive DNA subclassing 2015-06-22 12:52:09 -10:00
Jack Andersen
490c6d53f5 Merge pull request #15 from Antidote/msvc
Fixed variadic macros for MSVC
2015-06-20 11:13:25 -07:00
Jack Andersen
a0f0690719 confirmed linux build working; solved signing warnings 2015-06-20 08:07:11 -10:00
Jack Andersen
5a0f2e8ed4 MSVC build fixes 2015-06-19 21:40:15 -07:00
Jack Andersen
687a7eef64 fixed length string read refinement 2015-06-19 10:54:21 -10:00
Jack Andersen
2d8aeb70e8 updated doxy comments 2015-06-19 10:44:01 -10:00
Jack Andersen
e641dbbe4b added seek directive to fixed-length string reads 2015-06-19 10:40:59 -10:00
Jack Andersen
3dfb001f3d added fixed-length string writing 2015-06-19 10:12:25 -10:00
Jack Andersen
242a06915b diagnostic duplicates no longer raised 2015-06-19 08:25:08 -10:00
Jack Andersen
fd3db3e1db fix for vector readers 2015-06-18 19:33:46 -10:00
Jack Andersen
7b1b027192 Merge pull request #14 from Antidote/travis-fix
Adjusting travis config to support atdna
2015-06-18 19:27:22 -10:00
Jack Andersen
7cdfcab55a lots of new 'atdna' types; travis update 2015-06-18 19:18:39 -10:00
Jack Andersen
1aa3a705af added commenting to output 2015-06-17 21:57:53 -10:00
Jack Andersen
d6a489753d generator fix 2015-06-17 21:37:04 -10:00
Jack Andersen
c08695709e Minor argument tweak 2015-06-17 21:34:36 -10:00
Jack Andersen
6c7f5ebd29 Updated with Vector support for DNA subtypes 2015-06-17 21:32:11 -10:00
Jack Andersen
2c63768ec4 Added DNA.hpp warning 2015-06-16 16:50:12 -10:00
Jack Andersen
791a355ace Merge pull request #13 from Antidote/atdna
Added `atdna` tool to Athena
2015-06-16 16:25:15 -10:00
Jack Andersen
9ebdb37b85 removed unneeded libs 2015-06-16 16:20:38 -10:00
Jack Andersen
3071db8f61 hard-path comments 2015-06-16 16:02:35 -10:00
Jack Andersen
0bf9815fc0 Restored master branch for PKGBUILDs 2015-06-16 15:43:35 -10:00
Jack Andersen
647f9c4dec Another tweak 2015-06-16 15:20:31 -10:00
Jack Andersen
ec4ac688f4 hopefully the last tweak 2015-06-16 15:08:39 -10:00
Jack Andersen
7fccd416f3 even more tweaks 2015-06-16 15:05:43 -10:00
Jack Andersen
b195c194f7 futher adjustments 2015-06-16 15:02:58 -10:00
Jack Andersen
96ed8a2ce2 more build system tweaks 2015-06-16 14:57:06 -10:00
Jack Andersen
2bb60ab152 build system tweaks 2015-06-16 14:33:04 -10:00
Jack Andersen
fb722a9f5b further updates to atdna 2015-06-16 14:25:48 -10:00
Jack Andersen
775b51fd00 minor tweak 2015-06-15 21:15:00 -10:00
Jack Andersen
0452e34150 initial working reader/writer values 2015-06-15 21:11:56 -10:00
Jack Andersen
c09f06b436 working release build of atdna 2015-06-15 16:29:53 -10:00
Jack Andersen
dde8ac1a1c initial atdna commit 2015-06-14 16:49:02 -10:00
1ea07c20fb Add MIT license 2015-05-28 18:43:50 -07:00
7785983093 Merge pull request #10 from Antidote/file-dir-api
File and Dir API
2015-05-20 22:54:27 -07:00
6a611ef2cc * Fix last commit 2015-05-20 22:41:29 -07:00
d5ccf159f7 * Fix Wii/GC Compiling
* Initial FileInfo/Dir APIs
* Get rid of type punning warnings
2015-05-20 22:35:26 -07:00
Jack Andersen
3223d3faa6 Merge branch 'file-dir-api' of https://github.com/Antidote/Athena into file-dir-api 2015-05-20 19:16:28 -10:00
Jack Andersen
92d86a2673 More portable 'utimes' 2015-05-20 19:14:44 -10:00
47dafe3251 * struct stat64 -> stat64_t 2015-05-20 22:04:35 -07:00
011496db8b * Fix Wii/GC Compiling
* Finish initial FileInfo/Dir APIs
* Get rid of type punning warnings
2015-05-20 21:58:21 -07:00
29a203522d * Fix merge 2015-05-18 20:36:45 -07:00
6ee11b9a08 * Fix styling 2015-05-18 20:24:56 -07:00
423a9a37d2 Reimplement Exceptions. 2015-05-18 16:38:02 -07:00
01f989c21c Initial API implementation 2015-05-18 14:03:13 -07:00
215 changed files with 17007 additions and 11425 deletions

View File

@@ -1,17 +0,0 @@
compiler:
- gcc-4.8
before_install:
- sudo add-apt-repository --yes ppa:ubuntu-sdk-team/ppa
- sudo add-apt-repository --yes ppa:ubuntu-toolchain-r/test
- sudo apt-get update -qq
- sudo apt-get install -qq g++-4.8
- sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.8 40 --slave /usr/bin/g++ g++ /usr/bin/g++-4.8
- sudo update-alternatives --config gcc
script:
- qmake Athena.pro
- make
notifications:
irc: "irc.wiiking2.com#wiiking2"

BIN
Athena.icns Normal file

Binary file not shown.

BIN
Athena.ico Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 90 KiB

View File

@@ -1,4 +0,0 @@
include(AthenaCore.pri)
include(AthenaZelda.pri)
include(AthenaWiiSave.pri)
include(AthenaSakura.pri)

View File

@@ -1,63 +0,0 @@
CONFIG += staticlib c++11
QMAKE_CXXFLAGS += -std=c++11
TEMPLATE= lib
DESTDIR = ./lib
INCLUDEPATH += $$PWD/include
# Uncomment this if you wish to use Qt with libAthena
#DEFINES += ATHENA_USE_QT
contains(DEFINES, ATHENA_USE_QT){
QT += qt core
} else {
QT =
}
CONFIG(debug, debug|release): {
DEFINES += DEBUG
TARGET=Athena-d
# We don't want the objects,
# in the project directory, so tell qmake
# where to put them
OBJECTS_DIR = obj/debug
}
CONFIG(release, release|debug): {
DEFINES -= DEBUG
TARGET=Athena
# We don't want the objects,
# in the project directory, so tell qmake
# where to put them
OBJECTS_DIR = obj/release
}
ATHENA_PRO=true
include(Athena.pri)
OTHER_FILES += \
.travis.yml
unix {
isEmpty(PREFIX) {
PREFIX = /usr/local
}
libFiles.path = $$PREFIX/lib
libFiles.files = $$PWD/lib/*
headerFiles.files = $$PWD/include/*
headerFiles.path = $$PREFIX/include/Athena
INSTALLS += libFiles headerFiles
}
win32 {
isEmpty(PREFIX) {
PREFIX = $$PWD/pkg
}
libFiles.path = $$PREFIX/lib
libFiles.files = $$PWD/lib/*
headerFiles.path = $$PREFIX/include/Athena
headerFiles.files = $$PWD/include/*
INSTALLS += libFiles headerFiles
}

22
AthenaConfig.cmake.in Normal file
View File

@@ -0,0 +1,22 @@
# - Config file for the Athena package
# It defines the following variables
# ATHENA_INCLUDE_DIR - include directory for Athena
# ATHENA_LIBRARIES - libraries to link against
# ATHENA_ICON - path to Athena.ico
# Compute paths
get_filename_component(ATHENA_CMAKE_DIR "${CMAKE_CURRENT_LIST_FILE}" PATH)
set(ATHENA_INCLUDE_DIR "@CONF_INCLUDE_DIR@")
# Our library dependencies (contains definitions for IMPORTED targets)
if(NOT TARGET AthenaCore AND NOT Athena_BINARY_DIR)
include("${ATHENA_CMAKE_DIR}/AthenaTargets.cmake")
endif()
# These are IMPORTED targets created by AthenaTargets.cmake
set(ATHENA_LIBRARIES AthenaCore AthenaSakura AthenaZelda AthenaWiiSave)
# Set icon location if on windows
if(WIN32 AND NOT CYGWIN)
set(ATHENA_ICON "@CONF_ICON_DIR@")
endif()

View File

@@ -0,0 +1,11 @@
set(PACKAGE_VERSION "@ATHENA_VERSION@")
# Check whether the requested PACKAGE_FIND_VERSION is compatible
if("${PACKAGE_VERSION}" VERSION_LESS "${PACKAGE_FIND_VERSION}")
set(PACKAGE_VERSION_COMPATIBLE FALSE)
else()
set(PACKAGE_VERSION_COMPATIBLE TRUE)
if ("${PACKAGE_VERSION}" VERSION_EQUAL "${PACKAGE_FIND_VERSION}")
set(PACKAGE_VERSION_EXACT TRUE)
endif()
endif()

View File

@@ -1,66 +0,0 @@
!contains(ATHENA_PRO, true): {
INCLUDEPATH += $$PWD/include
!contains(CONFIG, c++11):CONFIG += c++11
mac:QMAKE_CXXFLAGS += -stdlib=libc++
}
include(extern/lzo/lzo.pri)
include(extern/zlib/zlib.pri)
ATHENA_CORE=true
SOURCES += \
$$PWD/src/Athena/Utility.cpp \
$$PWD/src/Athena/FileReader.cpp \
$$PWD/src/Athena/FileWriter.cpp \
$$PWD/src/Athena/MemoryReader.cpp \
$$PWD/src/Athena/MemoryWriter.cpp \
$$PWD/src/Athena/Global.cpp \
$$PWD/src/Athena/Checksums.cpp \
$$PWD/src/Athena/Compression.cpp \
$$PWD/src/LZ77/LZLookupTable.cpp \
$$PWD/src/LZ77/LZType10.cpp \
$$PWD/src/LZ77/LZType11.cpp \
$$PWD/src/LZ77/LZBase.cpp
win32:SOURCES += \
$$PWD/src/win32_largefilewrapper.c
mac:SOURCES += \
$$PWD/src/osx_largefilewrapper.c
HEADERS += \
$$PWD/include/Athena/IStream.hpp \
$$PWD/include/Athena/IStreamReader.hpp \
$$PWD/include/Athena/IStreamWriter.hpp \
$$PWD/include/Athena/Types.hpp \
$$PWD/include/Athena/Utility.hpp \
$$PWD/include/Athena/Global.hpp \
$$PWD/include/Athena/Exception.hpp \
$$PWD/include/Athena/FileNotFoundException.hpp \
$$PWD/include/Athena/IOException.hpp \
$$PWD/include/Athena/InvalidDataException.hpp \
$$PWD/include/Athena/InvalidOperationException.hpp \
$$PWD/include/Athena/FileReader.hpp \
$$PWD/include/Athena/FileWriter.hpp \
$$PWD/include/Athena/MemoryReader.hpp \
$$PWD/include/Athena/MemoryWriter.hpp \
$$PWD/include/Athena/NotImplementedException.hpp \
$$PWD/include/Athena/Checksums.hpp \
$$PWD/include/Athena/Compression.hpp \
$$PWD/include/LZ77/LZBase.hpp \
$$PWD/include/LZ77/LZLookupTable.hpp \
$$PWD/include/LZ77/LZType10.hpp \
$$PWD/include/LZ77/LZType11.hpp \
$$PWD/include/utf8.h \
$$PWD/include/utf8/checked.h \
$$PWD/include/utf8/core.h \
$$PWD/include/utf8/unchecked.h
win32:HEADERS += \
$$PWD/include/win32_largefilewrapper.h
mac:HEADERS += \
$$PWD/include/osx_largefilewrapper.h

BIN
AthenaNSIS.bmp Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 30 KiB

View File

@@ -1,20 +0,0 @@
!contains(ATHENA_CORE, true): {
message("including core")
include(AthenaCore.pri)
}
SOURCES += \
$$PWD/src/Athena/Sprite.cpp \
$$PWD/src/Athena/SpriteFile.cpp \
$$PWD/src/Athena/SpriteFileReader.cpp \
$$PWD/src/Athena/SpriteFileWriter.cpp \
$$PWD/src/Athena/SpriteFrame.cpp \
$$PWD/src/Athena/SpritePart.cpp
HEADERS += \
$$PWD/include/Athena/Sprite.hpp \
$$PWD/include/Athena/SpriteFile.hpp \
$$PWD/include/Athena/SpriteFileReader.hpp \
$$PWD/include/Athena/SpriteFileWriter.hpp \
$$PWD/include/Athena/SpriteFrame.hpp \
$$PWD/include/Athena/SpritePart.hpp

View File

@@ -1,30 +0,0 @@
!contains(ATHENA_CORE, true): {
message("including core")
include(AthenaCore.pri)
}
SOURCES += \
$$PWD/src/Athena/WiiBanner.cpp \
$$PWD/src/Athena/WiiFile.cpp \
$$PWD/src/Athena/WiiImage.cpp \
$$PWD/src/Athena/WiiSave.cpp \
$$PWD/src/Athena/WiiSaveReader.cpp \
$$PWD/src/Athena/WiiSaveWriter.cpp \
$$PWD/src/bn.cpp \
$$PWD/src/ec.cpp \
$$PWD/src/md5.cpp \
$$PWD/src/sha1.cpp \
$$PWD/src/aes.c
HEADERS += \
$$PWD/include/Athena/WiiBanner.hpp \
$$PWD/include/Athena/WiiFile.hpp \
$$PWD/include/Athena/WiiImage.hpp \
$$PWD/include/Athena/WiiSave.hpp \
$$PWD/include/Athena/WiiSaveReader.hpp \
$$PWD/include/Athena/WiiSaveWriter.hpp \
$$PWD/include/aes.h \
$$PWD/include/bn.h \
$$PWD/include/ec.h \
$$PWD/include/md5.h \
$$PWD/include/sha1.h

View File

@@ -1,36 +0,0 @@
!contains(ATHENA_CORE, true): {
message("including core")
include(AthenaCore.pri)
}
SOURCES += \
$$PWD/src/Athena/ALTTPFile.cpp \
$$PWD/src/Athena/ALTTPFileReader.cpp \
$$PWD/src/Athena/ALTTPFileWriter.cpp \
$$PWD/src/Athena/ALTTPQuest.cpp \
$$PWD/src/Athena/MCFile.cpp \
$$PWD/src/Athena/MCFileReader.cpp \
$$PWD/src/Athena/MCFileWriter.cpp \
$$PWD/src/Athena/MCSlot.cpp \
$$PWD/src/Athena/SkywardSwordFile.cpp \
$$PWD/src/Athena/SkywardSwordFileReader.cpp \
$$PWD/src/Athena/SkywardSwordFileWriter.cpp \
$$PWD/src/Athena/SkywardSwordQuest.cpp \
$$PWD/src/Athena/ZQuestFile.cpp \
$$PWD/src/Athena/ZQuestFileReader.cpp \
$$PWD/src/Athena/ZQuestFileWriter.cpp
HEADERS += \
$$PWD/include/Athena/ALTTPEnums.hpp \
$$PWD/include/Athena/ALTTPFile.hpp \
$$PWD/include/Athena/ALTTPFileReader.hpp \
$$PWD/include/Athena/ALTTPFileWriter.hpp \
$$PWD/include/Athena/ALTTPQuest.hpp \
$$PWD/include/Athena/ALTTPStructs.hpp \
$$PWD/include/Athena/MCFile.hpp \
$$PWD/include/Athena/MCFileReader.hpp \
$$PWD/include/Athena/MCFileWriter.hpp \
$$PWD/include/Athena/SkywardSwordFile.hpp \
$$PWD/include/Athena/SkywardSwordFileReader.hpp \
$$PWD/include/Athena/SkywardSwordFileWriter.hpp \
$$PWD/include/Athena/SkywardSwordQuest.hpp

252
CMakeLists.txt Normal file
View File

@@ -0,0 +1,252 @@
cmake_minimum_required(VERSION 3.0)
project(Athena)
##################
# Athena Version #
##################
set(ATHENA_MAJOR_VERSION 2)
set(ATHENA_MINOR_VERSION 1)
set(ATHENA_PATCH_VERSION 0)
set(ATHENA_VERSION
${ATHENA_MAJOR_VERSION}.${ATHENA_MINOR_VERSION}.${ATHENA_PATCH_VERSION})
################
# Athena Build #
################
add_subdirectory(extern)
include_directories(include ${LZO_INCLUDE_DIR} ${ZLIB_INCLUDE_DIR})
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
if(WIN32)
list(APPEND CORE_EXTRA src/win32_largefilewrapper.c)
elseif(APPLE)
list(APPEND CORE_EXTRA src/osx_largefilewrapper.c)
elseif(GEKKO)
list(APPEND CORE_EXTRA src/gekko_support.c)
endif()
add_library(AthenaCore
src/Athena/Utility.cpp
src/Athena/FileReader.cpp
src/Athena/FileWriter.cpp
src/Athena/MemoryReader.cpp
src/Athena/MemoryWriter.cpp
src/Athena/Global.cpp
src/Athena/Checksums.cpp
src/Athena/Compression.cpp
src/LZ77/LZLookupTable.cpp
src/LZ77/LZType10.cpp
src/LZ77/LZType11.cpp
src/LZ77/LZBase.cpp
src/Athena/FileInfo.cpp
src/Athena/Dir.cpp
${CORE_EXTRA}
include/Athena/IStream.hpp
include/Athena/IStreamReader.hpp
include/Athena/IStreamWriter.hpp
include/Athena/Types.hpp
include/Athena/Utility.hpp
include/Athena/Global.hpp
include/Athena/Exception.hpp
include/Athena/FileNotFoundException.hpp
include/Athena/IOException.hpp
include/Athena/InvalidDataException.hpp
include/Athena/InvalidOperationException.hpp
include/Athena/FileReader.hpp
include/Athena/FileWriter.hpp
include/Athena/MemoryReader.hpp
include/Athena/MemoryWriter.hpp
include/Athena/NotImplementedException.hpp
include/Athena/Checksums.hpp
include/Athena/Compression.hpp
include/LZ77/LZBase.hpp
include/LZ77/LZLookupTable.hpp
include/LZ77/LZType10.hpp
include/LZ77/LZType11.hpp
include/Athena/FileInfo.hpp
include/Athena/Dir.hpp
include/gekko_support.h
include/Athena/DNA.hpp
)
add_library(AthenaSakura
src/Athena/Sprite.cpp
src/Athena/SpriteFile.cpp
src/Athena/SpriteFileReader.cpp
src/Athena/SpriteFileWriter.cpp
src/Athena/SpriteFrame.cpp
src/Athena/SpritePart.cpp
include/Athena/Sprite.hpp
include/Athena/SpriteFile.hpp
include/Athena/SpriteFileReader.hpp
include/Athena/SpriteFileWriter.hpp
include/Athena/SpriteFrame.hpp
include/Athena/SpritePart.hpp
)
add_library(AthenaWiiSave
src/Athena/WiiBanner.cpp
src/Athena/WiiFile.cpp
src/Athena/WiiImage.cpp
src/Athena/WiiSave.cpp
src/Athena/WiiSaveReader.cpp
src/Athena/WiiSaveWriter.cpp
src/bn.cpp
src/ec.cpp
src/md5.cpp
src/sha1.cpp
src/aes.cpp
include/Athena/WiiBanner.hpp
include/Athena/WiiFile.hpp
include/Athena/WiiImage.hpp
include/Athena/WiiSave.hpp
include/Athena/WiiSaveReader.hpp
include/Athena/WiiSaveWriter.hpp
include/aes.hpp
include/bn.h
include/ec.h
include/md5.h
include/sha1.h
)
set_source_files_properties(src/aes.cpp PROPERTIES COMPILE_FLAGS -maes)
add_library(AthenaZelda
src/Athena/ALTTPFile.cpp
src/Athena/ALTTPFileReader.cpp
src/Athena/ALTTPFileWriter.cpp
src/Athena/ALTTPQuest.cpp
src/Athena/MCFile.cpp
src/Athena/MCFileReader.cpp
src/Athena/MCFileWriter.cpp
src/Athena/MCSlot.cpp
src/Athena/SkywardSwordFile.cpp
src/Athena/SkywardSwordFileReader.cpp
src/Athena/SkywardSwordFileWriter.cpp
src/Athena/SkywardSwordQuest.cpp
src/Athena/ZQuestFile.cpp
src/Athena/ZQuestFileReader.cpp
src/Athena/ZQuestFileWriter.cpp
include/Athena/ALTTPEnums.hpp
include/Athena/ALTTPFile.hpp
include/Athena/ALTTPFileReader.hpp
include/Athena/ALTTPFileWriter.hpp
include/Athena/ALTTPQuest.hpp
include/Athena/ALTTPStructs.hpp
include/Athena/MCFile.hpp
include/Athena/MCFileReader.hpp
include/Athena/MCFileWriter.hpp
include/Athena/SkywardSwordFile.hpp
include/Athena/SkywardSwordFileReader.hpp
include/Athena/SkywardSwordFileWriter.hpp
include/Athena/SkywardSwordQuest.hpp
)
# Offer the user the choice of overriding the installation directories
set(INSTALL_LIB_DIR lib CACHE PATH "Installation directory for libraries")
set(INSTALL_INCLUDE_DIR include CACHE PATH "Installation directory for header files")
if(WIN32 AND NOT CYGWIN)
set(INSTALL_CMAKE_DIR cmake)
else()
set(INSTALL_CMAKE_DIR lib/cmake/Athena)
endif()
# Make relative paths absolute (needed later on)
foreach(p LIB INCLUDE CMAKE)
set(var INSTALL_${p}_DIR)
if(NOT IS_ABSOLUTE "${${var}}")
set(ABS_${var} "${CMAKE_INSTALL_PREFIX}/${${var}}")
else()
set(ABS_${var} "${${var}}")
endif()
endforeach()
# Define installs
install(DIRECTORY include/ DESTINATION ${INSTALL_INCLUDE_DIR}/Athena COMPONENT Athena)
install(TARGETS AthenaCore AthenaSakura AthenaWiiSave AthenaZelda
DESTINATION ${INSTALL_LIB_DIR} EXPORT AthenaTargets COMPONENT Athena)
if(WIN32 AND NOT CYGWIN)
install(FILES Athena.ico DESTINATION ${INSTALL_LIB_DIR} COMPONENT Athena)
elseif(UNIX)
configure_file(libAthena.pc.in libAthena.pc @ONLY)
install(FILES ${PROJECT_BINARY_DIR}/libAthena.pc
DESTINATION ${INSTALL_LIB_DIR}/pkgconfig COMPONENT Athena)
endif()
##################
# Package Export #
##################
# Add all targets to the build-tree export set
export(TARGETS AthenaCore AthenaSakura AthenaWiiSave AthenaZelda
FILE "${PROJECT_BINARY_DIR}/AthenaTargets.cmake")
# Export the package for use from the build-tree
# (this registers the build-tree with a global CMake-registry)
export(PACKAGE Athena)
# Create the AthenaConfig.cmake
# ... for the build tree
set(CONF_INCLUDE_DIR "${PROJECT_SOURCE_DIR}/include/Athena")
set(CONF_ICON_DIR "${PROJECT_SOURCE_DIR}/Athena.ico")
configure_file(AthenaConfig.cmake.in "${PROJECT_BINARY_DIR}/AthenaConfig.cmake" @ONLY)
# ... for the install tree
file(RELATIVE_PATH REL_INCLUDE_DIR "${ABS_INSTALL_CMAKE_DIR}" "${ABS_INSTALL_INCLUDE_DIR}")
file(RELATIVE_PATH REL_ICON_DIR "${ABS_INSTALL_CMAKE_DIR}" "${ABS_INSTALL_LIB_DIR}/Athena.ico")
set(CONF_INCLUDE_DIR "\${ATHENA_CMAKE_DIR}/${REL_INCLUDE_DIR}/Athena")
set(CONF_ICON_DIR "\${ATHENA_CMAKE_DIR}/${REL_ICON_DIR}")
configure_file(AthenaConfig.cmake.in "${PROJECT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/AthenaConfig.cmake" @ONLY)
# ... for both
configure_file(AthenaConfigVersion.cmake.in "${PROJECT_BINARY_DIR}/AthenaConfigVersion.cmake" @ONLY)
# Install AthenaConfig.cmake
install(FILES
"${PROJECT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/AthenaConfig.cmake"
"${PROJECT_BINARY_DIR}/AthenaConfigVersion.cmake"
DESTINATION ${INSTALL_CMAKE_DIR} COMPONENT Athena)
# Install the export set for use with the install-tree
install(EXPORT AthenaTargets DESTINATION ${INSTALL_CMAKE_DIR} COMPONENT Athena)
################
# atdna import #
################
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/atdna/CMakeLists.txt")
add_subdirectory(atdna)
endif()
#########
# CPack #
#########
include(InstallRequiredSystemLibraries)
set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "Athena")
set(CPACK_PACKAGE_VENDOR "Antidote / Jackoalan")
set(CPACK_PACKAGE_DESCRIPTION_FILE "${CMAKE_CURRENT_SOURCE_DIR}/README.md")
set(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_SOURCE_DIR}/LICENSE")
set(CPACK_PACKAGE_VERSION_MAJOR ${ATHENA_MAJOR_VERSION})
set(CPACK_PACKAGE_VERSION_MINOR ${ATHENA_MINOR_VERSION})
set(CPACK_PACKAGE_VERSION_PATCH ${ATHENA_PATCH_VERSION})
set(CPACK_PACKAGE_INSTALL_DIRECTORY "Athena")
if(WIN32 AND NOT UNIX)
# There is a bug in NSI that does not handle full unix paths properly. Make
# sure there is at least one set of four (4) backlasshes.
set(CPACK_PACKAGE_INSTALL_REGISTRY_KEY "Athena")
set(CPACK_PACKAGE_ICON "${CMAKE_CURRENT_SOURCE_DIR}\\\\AthenaNSIS.bmp")
set(CPACK_NSIS_MODIFY_PATH ON)
set(CPACK_NSIS_MUI_ICON "${CMAKE_CURRENT_SOURCE_DIR}\\\\Athena.ico")
set(CPACK_NSIS_MUI_UNIICON "${CMAKE_CURRENT_SOURCE_DIR}\\\\Athena.ico")
set(CPACK_NSIS_INSTALLED_ICON_NAME "Uninstall.exe")
set(CPACK_NSIS_DISPLAY_NAME "Athena")
set(CPACK_NSIS_URL_INFO_ABOUT "http://libathena.github.io")
set(CPACK_NSIS_CONTACT "antidote.crk@gmail.com")
endif()
include(CPack)

2413
Doxyfile Normal file

File diff suppressed because it is too large Load Diff

20
LICENSE Normal file
View File

@@ -0,0 +1,20 @@
Copyright (c) 2011-2015 Phillip "Antidote" Stephens, Jack Andersen, et al.
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View File

@@ -1,14 +1,14 @@
# PKGBUILD for libAthena
_pkgname=libathena
pkgname=$_pkgname-git
pkgver=v1.0.0
pkgver=2.0.0.9.gf3ba881
pkgrel=1
pkgdesc="Basic cross platform IO library"
arch=('i686' 'x86_64')
source=("${pkgname%-*}::git+https://github.com/Antidote/Athena.git")
source=("${pkgname%-*}::git+https://github.com/libAthena/Athena.git")
options=(staticlibs)
license="GPL3"
makedepends=('git qt5-base sed')
license="MIT"
makedepends=('git cmake sed')
md5sums=('SKIP')
sha256sums=('SKIP')
@@ -19,11 +19,14 @@ pkgver() {
build() {
cd "$srcdir/$_pkgname"
qmake && make
mkdir -p build
cd build
cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX="$pkgdir/usr" ..
make
}
package() {
cd "$srcdir/$_pkgname"
qmake PREFIX="$pkgdir/usr" && make install
cd "$srcdir/$_pkgname/build"
make install
}

View File

@@ -1,2 +1 @@
# Athena
[![Build Status](https://travis-ci.org/Antidote/Athena.svg?branch=master)](https://travis-ci.org/Antidote/Athena)

2
extern/CMakeLists.txt vendored Normal file
View File

@@ -0,0 +1,2 @@
add_subdirectory(lzo)
add_subdirectory(zlib)

85
extern/lzo/CMakeLists.txt vendored Normal file
View File

@@ -0,0 +1,85 @@
if(NOT WIN32)
find_library(LZO_LIB lzo2)
endif()
if(WIN32 OR LZO_LIB STREQUAL LZO_LIB-NOTFOUND)
include_directories(include)
message("-- Using Athena's built-in lzo")
add_library(lzo2
src/lzo1.c
src/lzo1_99.c
src/lzo1a.c
src/lzo1a_99.c
src/lzo1b_1.c
src/lzo1b_2.c
src/lzo1b_3.c
src/lzo1b_4.c
src/lzo1b_5.c
src/lzo1b_6.c
src/lzo1b_7.c
src/lzo1b_8.c
src/lzo1b_9.c
src/lzo1b_99.c
src/lzo1b_9x.c
src/lzo1b_cc.c
src/lzo1b_d1.c
src/lzo1b_d2.c
src/lzo1b_rr.c
src/lzo1b_xx.c
src/lzo1c_1.c
src/lzo1c_2.c
src/lzo1c_3.c
src/lzo1c_4.c
src/lzo1c_5.c
src/lzo1c_6.c
src/lzo1c_7.c
src/lzo1c_8.c
src/lzo1c_9.c
src/lzo1c_99.c
src/lzo1c_9x.c
src/lzo1c_cc.c
src/lzo1c_d1.c
src/lzo1c_d2.c
src/lzo1c_rr.c
src/lzo1c_xx.c
src/lzo1f_1.c
src/lzo1f_9x.c
src/lzo1f_d1.c
src/lzo1f_d2.c
src/lzo1x_1.c
src/lzo1x_1k.c
src/lzo1x_1l.c
src/lzo1x_1o.c
src/lzo1x_9x.c
src/lzo1x_d1.c
src/lzo1x_d2.c
src/lzo1x_d3.c
src/lzo1x_o.c
src/lzo1y_1.c
src/lzo1y_9x.c
src/lzo1y_d1.c
src/lzo1y_d2.c
src/lzo1y_d3.c
src/lzo1y_o.c
src/lzo1z_9x.c
src/lzo1z_d1.c
src/lzo1z_d2.c
src/lzo1z_d3.c
src/lzo2a_9x.c
src/lzo2a_d1.c
src/lzo2a_d2.c
src/lzo_crc.c
src/lzo_init.c
src/lzo_ptr.c
src/lzo_str.c
src/lzo_util.c
include/lzo/lzo1.h
include/lzo/lzo1x.h
include/lzo/lzoconf.h
include/lzo/lzodefs.h
include/lzo/lzoutil.h)
if(WIN32 AND NOT UNIX)
install(DIRECTORY include/lzo DESTINATION include COMPONENT lzo2)
install(TARGETS lzo2 DESTINATION lib COMPONENT lzo2)
endif()
set(LZO_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/include CACHE PATH "LZO include path" FORCE)
endif()

View File

@@ -366,9 +366,18 @@ LZO_EXTERN(const lzo_uint32_tp)
/* misc. */
LZO_EXTERN(int) _lzo_config_check(void);
typedef union {
lzo_voidp a00; lzo_bytep a01; lzo_uint a02; lzo_xint a03; lzo_uintptr_t a04;
void *a05; unsigned char *a06; unsigned long a07; size_t a08; ptrdiff_t a09;
typedef union
{
lzo_voidp a00;
lzo_bytep a01;
lzo_uint a02;
lzo_xint a03;
lzo_uintptr_t a04;
void* a05;
unsigned char* a06;
unsigned long a07;
size_t a08;
ptrdiff_t a09;
#if defined(lzo_int64_t)
lzo_uint64_t a10;
#endif

78
extern/lzo/lzo.pri vendored
View File

@@ -1,78 +0,0 @@
SOURCES += \
$$PWD/src/lzo1.c \
$$PWD/src/lzo1_99.c \
$$PWD/src/lzo1a.c \
$$PWD/src/lzo1a_99.c \
$$PWD/src/lzo1b_1.c \
$$PWD/src/lzo1b_2.c \
$$PWD/src/lzo1b_3.c \
$$PWD/src/lzo1b_4.c \
$$PWD/src/lzo1b_5.c \
$$PWD/src/lzo1b_6.c \
$$PWD/src/lzo1b_7.c \
$$PWD/src/lzo1b_8.c \
$$PWD/src/lzo1b_9.c \
$$PWD/src/lzo1b_99.c \
$$PWD/src/lzo1b_9x.c \
$$PWD/src/lzo1b_cc.c \
$$PWD/src/lzo1b_d1.c \
$$PWD/src/lzo1b_d2.c \
$$PWD/src/lzo1b_rr.c \
$$PWD/src/lzo1b_xx.c \
$$PWD/src/lzo1c_1.c \
$$PWD/src/lzo1c_2.c \
$$PWD/src/lzo1c_3.c \
$$PWD/src/lzo1c_4.c \
$$PWD/src/lzo1c_5.c \
$$PWD/src/lzo1c_6.c \
$$PWD/src/lzo1c_7.c \
$$PWD/src/lzo1c_8.c \
$$PWD/src/lzo1c_9.c \
$$PWD/src/lzo1c_99.c \
$$PWD/src/lzo1c_9x.c \
$$PWD/src/lzo1c_cc.c \
$$PWD/src/lzo1c_d1.c \
$$PWD/src/lzo1c_d2.c \
$$PWD/src/lzo1c_rr.c \
$$PWD/src/lzo1c_xx.c \
$$PWD/src/lzo1f_1.c \
$$PWD/src/lzo1f_9x.c \
$$PWD/src/lzo1f_d1.c \
$$PWD/src/lzo1f_d2.c \
$$PWD/src/lzo1x_1.c \
$$PWD/src/lzo1x_1k.c \
$$PWD/src/lzo1x_1l.c \
$$PWD/src/lzo1x_1o.c \
$$PWD/src/lzo1x_9x.c \
$$PWD/src/lzo1x_d1.c \
$$PWD/src/lzo1x_d2.c \
$$PWD/src/lzo1x_d3.c \
$$PWD/src/lzo1x_o.c \
$$PWD/src/lzo1y_1.c \
$$PWD/src/lzo1y_9x.c \
$$PWD/src/lzo1y_d1.c \
$$PWD/src/lzo1y_d2.c \
$$PWD/src/lzo1y_d3.c \
$$PWD/src/lzo1y_o.c \
$$PWD/src/lzo1z_9x.c \
$$PWD/src/lzo1z_d1.c \
$$PWD/src/lzo1z_d2.c \
$$PWD/src/lzo1z_d3.c \
$$PWD/src/lzo2a_9x.c \
$$PWD/src/lzo2a_d1.c \
$$PWD/src/lzo2a_d2.c \
$$PWD/src/lzo_crc.c \
$$PWD/src/lzo_init.c \
$$PWD/src/lzo_ptr.c \
$$PWD/src/lzo_str.c \
$$PWD/src/lzo_util.c
HEADERS += \
$$PWD/include/lzo/lzo1.h \
$$PWD/include/lzo/lzo1x.h \
$$PWD/include/lzo/lzoconf.h \
$$PWD/include/lzo/lzodefs.h \
$$PWD/include/lzo/lzoutil.h
INCLUDEPATH += $$PWD/include

68
extern/lzo/src/lzo1.c vendored
View File

@@ -160,8 +160,10 @@ lzo1_info ( int *rbits, int *clevel )
{
if (rbits)
*rbits = RBITS;
if (clevel)
*clevel = CLEVEL;
return D_SIZE * lzo_sizeof(lzo_bytep);
}
@@ -203,6 +205,7 @@ lzo1_decompress ( const lzo_bytep in , lzo_uint in_len,
op = out;
ip = in;
while (ip < ip_end)
{
t = *ip++; /* get marker */
@@ -212,9 +215,11 @@ lzo1_decompress ( const lzo_bytep in , lzo_uint in_len,
if (t == 0) /* a R0 literal run */
{
t = *ip++;
if (t >= R0FAST - R0MIN) /* a long R0 run */
{
t -= R0FAST - R0MIN;
if (t == 0)
t = R0FAST;
else
@@ -224,15 +229,22 @@ lzo1_decompress ( const lzo_bytep in , lzo_uint in_len,
#else
/* help the optimizer */
lzo_uint tt = 256;
do tt <<= 1; while (--t > 0);
do tt <<= 1;
while (--t > 0);
t = tt;
#endif
}
MEMCPY8_DS(op, ip, t);
continue;
}
t += R0MIN;
}
MEMCPY_DS(op, ip, t);
}
else /* a match */
@@ -282,26 +294,33 @@ store_run(lzo_bytep op, const lzo_bytep ii, lzo_uint r_len)
if (r_len >= 512)
{
unsigned r_bits = 7; /* 256 << 7 == 32768 */
do {
do
{
while (r_len >= (256u << r_bits))
{
r_len -= (256u << r_bits);
*op++ = 0; *op++ = LZO_BYTE((R0FAST - R0MIN) + r_bits);
*op++ = 0;
*op++ = LZO_BYTE((R0FAST - R0MIN) + r_bits);
MEMCPY8_DS(op, ii, (256u << r_bits));
}
} while (--r_bits > 0);
}
while (--r_bits > 0);
}
while (r_len >= R0FAST)
{
r_len -= R0FAST;
*op++ = 0; *op++ = R0FAST - R0MIN;
*op++ = 0;
*op++ = R0FAST - R0MIN;
MEMCPY8_DS(op, ii, R0FAST);
}
if (r_len >= R0MIN)
{
/* code a short R0 run */
*op++ = 0; *op++ = LZO_BYTE(r_len - R0MIN);
*op++ = 0;
*op++ = LZO_BYTE(r_len - R0MIN);
MEMCPY_DS(op, ii, r_len);
}
else if (r_len > 0)
@@ -348,6 +367,7 @@ do_compress ( const lzo_bytep in , lzo_uint in_len,
op = out;
ip = in;
ii = ip; /* point to start of literal run */
if (in_len <= MIN_MATCH_LONG + DVAL_LEN + 1)
goto the_end;
@@ -361,29 +381,38 @@ do_compress ( const lzo_bytep in , lzo_uint in_len,
ip++;
DVAL_NEXT(dv, ip);
do {
do
{
LZO_DEFINE_UNINITIALIZED_VAR(lzo_uint, m_off, 0);
lzo_uint dindex;
DINDEX1(dindex, ip);
GINDEX(m_pos, m_off, dict, dindex, in);
if (LZO_CHECK_MPOS(m_pos, m_off, in, ip, MAX_OFFSET))
goto literal;
if (m_pos[0] == ip[0] && m_pos[1] == ip[1] && m_pos[2] == ip[2])
goto match;
DINDEX2(dindex, ip);
GINDEX(m_pos, m_off, dict, dindex, in);
if (LZO_CHECK_MPOS(m_pos, m_off, in, ip, MAX_OFFSET))
goto literal;
if (m_pos[0] == ip[0] && m_pos[1] == ip[1] && m_pos[2] == ip[2])
goto match;
goto literal;
literal:
UPDATE_I(dict, 0, dindex, ip, in);
if (++ip >= ip_end)
break;
continue;
match:
@@ -397,11 +426,13 @@ match:
#if !defined(NDEBUG) && !(LZO_DICT_USE_PTR)
assert((m_pos_sav = ip - m_off) == (m_pos - 3));
#endif
/* 1) store the current literal run */
if (pd(ip, ii) > 0)
{
lzo_uint t = pd(ip, ii);
#if 1
/* OPTIMIZED: inline the copying of a short run */
if (t < R0MIN)
{
@@ -427,6 +458,7 @@ match:
#define PS *m_pos++ != *ip++
#if (MIN_MATCH_LONG - MIN_MATCH == 2) /* MBITS == 2 */
if (PS || PS)
#elif (MIN_MATCH_LONG - MIN_MATCH == 6) /* MBITS == 3 */
if (PS || PS || PS || PS || PS || PS)
@@ -471,10 +503,14 @@ match:
#if (CLEVEL == 9) || (CLEVEL >= 7 && MBITS <= 4) || (CLEVEL >= 5 && MBITS <= 3)
/* Insert the whole match (ii+1)..(ip-1) into dictionary. */
++ii;
do {
do
{
DVAL_NEXT(dv, ii);
UPDATE_D(dict, 0, dv, ii, in);
} while (++ii < ip);
}
while (++ii < ip);
DVAL_NEXT(dv, ii);
assert(ii == ip);
DVAL_ASSERT(dv, ip);
@@ -506,6 +542,7 @@ match:
while (ip < end && *m_pos == *ip)
m_pos++, ip++;
assert(ip <= in_end);
/* 2b) code the long match */
@@ -531,10 +568,14 @@ match:
/* Insert the whole match (ii+1)..(ip-1) into dictionary. */
/* This is not recommended because it is slow. */
++ii;
do {
do
{
DVAL_NEXT(dv, ii);
UPDATE_D(dict, 0, dv, ii, in);
} while (++ii < ip);
}
while (++ii < ip);
DVAL_NEXT(dv, ii);
assert(ii == ip);
DVAL_ASSERT(dv, ip);
@@ -560,7 +601,8 @@ match:
/* ii now points to the start of next literal run */
assert(ii == ip);
}
} while (ip < ip_end);
}
while (ip < ip_end);
@@ -569,6 +611,7 @@ the_end:
#if defined(LZO_RETURN_IF_NOT_COMPRESSIBLE)
/* return -1 if op == out to indicate that we
* couldn't compress and didn't copy anything.
*/
@@ -577,6 +620,7 @@ the_end:
*out_len = 0;
return LZO_E_NOT_COMPRESSIBLE;
}
#endif

View File

@@ -108,8 +108,10 @@ lzo1a_info ( int *rbits, int *clevel )
{
if (rbits)
*rbits = RBITS;
if (clevel)
*clevel = CLEVEL;
return D_SIZE * lzo_sizeof(lzo_bytep);
}
@@ -135,6 +137,7 @@ lzo1a_decompress ( const lzo_bytep in , lzo_uint in_len,
op = out;
ip = in;
while (ip < ip_end)
{
t = *ip++; /* get marker */
@@ -143,9 +146,11 @@ lzo1a_decompress ( const lzo_bytep in , lzo_uint in_len,
if (t == 0) /* a R0 literal run */
{
t = *ip++;
if (t >= R0FAST - R0MIN) /* a long R0 run */
{
t -= R0FAST - R0MIN;
if (t == 0)
t = R0FAST;
else
@@ -155,13 +160,19 @@ lzo1a_decompress ( const lzo_bytep in , lzo_uint in_len,
#else
/* help the optimizer */
lzo_uint tt = 256;
do tt <<= 1; while (--t > 0);
do tt <<= 1;
while (--t > 0);
t = tt;
#endif
}
MEMCPY8_DS(op, ip, t);
continue;
}
t += R0MIN;
goto literal;
}
@@ -174,6 +185,7 @@ literal:
while (ip < ip_end)
{
t = *ip++; /* get R1 marker */
if (t >= R0MIN)
goto match;
@@ -181,7 +193,8 @@ literal:
assert((t & OMASK) == t);
m_pos = op - MIN_OFFSET;
m_pos -= t | (((lzo_uint) * ip++) << OBITS);
assert(m_pos >= out); assert(m_pos < op);
assert(m_pos >= out);
assert(m_pos < op);
*op++ = m_pos[0];
*op++ = m_pos[1];
*op++ = m_pos[2];
@@ -194,7 +207,8 @@ match:
/* get match offset */
m_pos = op - MIN_OFFSET;
m_pos -= (t & OMASK) | (((lzo_uint) * ip++) << OBITS);
assert(m_pos >= out); assert(m_pos < op);
assert(m_pos >= out);
assert(m_pos < op);
/* get match len */
if (t < ((MSIZE - 1) << OBITS)) /* a short match */
@@ -217,9 +231,12 @@ match:
#if (LBITS < 8)
/* a very short literal following a long match */
t = ip[-1] >> LBITS;
if (t) do
*op++ = *ip++;
while (--t);
#endif
}
}
@@ -275,31 +292,42 @@ do_compress ( const lzo_bytep in , lzo_uint in_len,
BZERO8_PTR(wrkmem, sizeof(lzo_dict_t), D_SIZE);
#endif
DVAL_FIRST(dv,ip); UPDATE_D(dict,0,dv,ip,in); ip++;
DVAL_FIRST(dv, ip);
UPDATE_D(dict, 0, dv, ip, in);
ip++;
DVAL_NEXT(dv, ip);
do {
do
{
LZO_DEFINE_UNINITIALIZED_VAR(lzo_uint, m_off, 0);
lzo_uint dindex;
DINDEX1(dindex, ip);
GINDEX(m_pos, m_off, dict, dindex, in);
if (LZO_CHECK_MPOS_NON_DET(m_pos, m_off, in, ip, MAX_OFFSET))
goto literal;
if (m_pos[0] == ip[0] && m_pos[1] == ip[1] && m_pos[2] == ip[2])
goto match;
DINDEX2(dindex, ip);
GINDEX(m_pos, m_off, dict, dindex, in);
if (LZO_CHECK_MPOS_NON_DET(m_pos, m_off, in, ip, MAX_OFFSET))
goto literal;
if (m_pos[0] == ip[0] && m_pos[1] == ip[1] && m_pos[2] == ip[2])
goto match;
goto literal;
literal:
UPDATE_I(dict, 0, dindex, ip, in);
if (++ip >= ip_end)
break;
continue;
match:
@@ -376,13 +404,15 @@ match:
{
/* inline the copying of a short R0 run */
LZO_STATS(lzo_stats->r0short_runs++);
*op++ = 0; *op++ = LZO_BYTE(t - R0MIN);
*op++ = 0;
*op++ = LZO_BYTE(t - R0MIN);
MEMCPY_DS(op, ii, t);
r1 = ip; /* set new R1 pointer */
}
else
op = store_run(op, ii, t);
}
#if (LBITS < 8)
im = ip;
#endif
@@ -402,6 +432,7 @@ match:
#define PS *m_pos++ != *ip++
#if (MIN_MATCH_LONG - MIN_MATCH == 2) /* MBITS == 2 */
if (PS || PS)
#elif (MIN_MATCH_LONG - MIN_MATCH == 6) /* MBITS == 3 */
if (PS || PS || PS || PS || PS || PS)
@@ -443,12 +474,16 @@ match:
#if (LZO_COLLECT_STATS)
lzo_stats->short_matches++;
lzo_stats->short_match[m_len]++;
if (m_off < OSIZE)
lzo_stats->short_match_offset_osize[m_len]++;
if (m_off < 256)
lzo_stats->short_match_offset_256[m_len]++;
if (m_off < 1024)
lzo_stats->short_match_offset_1024[m_len]++;
#endif
@@ -461,10 +496,14 @@ match:
#if (CLEVEL == 9) || (CLEVEL >= 7 && MBITS <= 4) || (CLEVEL >= 5 && MBITS <= 3)
/* Insert the whole match (ii+1)..(ip-1) into dictionary. */
++ii;
do {
do
{
DVAL_NEXT(dv, ii);
UPDATE_D(dict, 0, dv, ii, in);
} while (++ii < ip);
}
while (++ii < ip);
DVAL_NEXT(dv, ii);
assert(ii == ip);
DVAL_ASSERT(dv, ip);
@@ -496,6 +535,7 @@ match:
while (ip < end && *m_pos == *ip)
m_pos++, ip++;
assert(ip <= in_end);
/* 2a) compute match parameters */
@@ -529,10 +569,14 @@ match:
/* Insert the whole match (ii+1)..(ip-1) into dictionary. */
/* This is not recommended because it is slow. */
++ii;
do {
do
{
DVAL_NEXT(dv, ii);
UPDATE_D(dict, 0, dv, ii, in);
} while (++ii < ip);
}
while (++ii < ip);
DVAL_NEXT(dv, ii);
assert(ii == ip);
DVAL_ASSERT(dv, ip);
@@ -559,12 +603,14 @@ match:
assert(ii == ip);
}
} while (ip < ip_end);
}
while (ip < ip_end);
assert(ip <= in_end);
#if defined(LZO_RETURN_IF_NOT_COMPRESSIBLE)
/* return -1 if op == out to indicate that we
* couldn't compress and didn't copy anything.
*/
@@ -573,6 +619,7 @@ match:
*out_len = 0;
return LZO_E_NOT_COMPRESSIBLE;
}
#endif
/* store the final literal run */

View File

@@ -108,7 +108,9 @@
#if (CLEVEL == 9) || (CLEVEL >= 7 && M2L_BITS <= 4) || (CLEVEL >= 5 && M2L_BITS <= 3)
/* Insert the whole match (ii+1)..(ip-1) into dictionary. */
++ii;
do {
do
{
DVAL_NEXT(dv, ii);
#if 0
UPDATE_D(dict, drun, dv, ii, in);
@@ -116,7 +118,9 @@
dict[ DINDEX(dv, ii) ] = DENTRY(ii, in);
#endif
MI
} while (++ii < ip);
}
while (++ii < ip);
DVAL_NEXT(dv, ii);
assert(ii == ip);
DVAL_ASSERT(dv, ip);
@@ -155,6 +159,7 @@
while (ip < end && *m_pos == *ip)
m_pos++, ip++;
assert(ip <= in_end);
/* 2a) compute match parameters */
@@ -190,7 +195,9 @@
/* Insert the whole match (ii+1)..(ip-1) into dictionary. */
/* This is not recommended because it can be slow. */
++ii;
do {
do
{
DVAL_NEXT(dv, ii);
#if 0
UPDATE_D(dict, drun, dv, ii, in);
@@ -198,7 +205,9 @@
dict[ DINDEX(dv, ii) ] = DENTRY(ii, in);
#endif
MI
} while (++ii < ip);
}
while (++ii < ip);
DVAL_NEXT(dv, ii);
assert(ii == ip);
DVAL_ASSERT(dv, ip);

View File

@@ -60,37 +60,48 @@ store_run(lzo_bytep const oo, const lzo_bytep const ii, lzo_uint r_len)
while (r_len >= (t = tt))
{
r_len -= t;
*op++ = 0; *op++ = (R0MAX - R0MIN);
*op++ = 0;
*op++ = (R0MAX - R0MIN);
MEMCPY8_DS(op, ip, t);
LZO_STATS(lzo_stats->r0long_runs++);
}
tt >>= 1;
do {
do
{
if (r_len >= (t = tt))
{
r_len -= t;
*op++ = 0; *op++ = LZO_BYTE((R0FAST - R0MIN) + r_bits);
*op++ = 0;
*op++ = LZO_BYTE((R0FAST - R0MIN) + r_bits);
MEMCPY8_DS(op, ip, t);
LZO_STATS(lzo_stats->r0long_runs++);
}
tt >>= 1;
} while (--r_bits > 0);
}
while (--r_bits > 0);
}
assert(r_len < 512);
while (r_len >= (t = R0FAST))
{
r_len -= t;
*op++ = 0; *op++ = (R0FAST - R0MIN);
*op++ = 0;
*op++ = (R0FAST - R0MIN);
MEMCPY8_DS(op, ip, t);
LZO_STATS(lzo_stats->r0fast_runs++);
}
t = r_len;
if (t >= R0MIN)
{
/* code a short R0 run */
*op++ = 0; *op++ = LZO_BYTE(t - R0MIN);
*op++ = 0;
*op++ = LZO_BYTE(t - R0MIN);
MEMCPY_DS(op, ip, t);
LZO_STATS(lzo_stats->r0short_runs++);
}

View File

@@ -71,6 +71,7 @@ code_match ( LZO_COMPRESS_T *c, lzo_bytep op, lzo_uint m_len, lzo_uint m_off )
assert(m_off <= M3_MAX_OFFSET);
m_off -= M3_MIN_OFFSET - M3_EOF_OFFSET;
/* code match len */
if (m_len <= M3_MAX_LEN)
*op++ = LZO_BYTE(M3_MARKER | (m_len - (M3_MIN_LEN - 1)));
@@ -81,14 +82,17 @@ code_match ( LZO_COMPRESS_T *c, lzo_bytep op, lzo_uint m_len, lzo_uint m_off )
*op++ = M4_MARKER;
/* code match len */
m_len -= M4_MIN_LEN - 1;
while (m_len > 255)
{
m_len -= 255;
*op++ = 0;
}
assert(m_len > 0);
*op++ = LZO_BYTE(m_len);
}
/* code low offset bits */
*op++ = LZO_BYTE(m_off & M3O_MASK);
/* code high offset bits */
@@ -97,6 +101,7 @@ code_match ( LZO_COMPRESS_T *c, lzo_bytep op, lzo_uint m_len, lzo_uint m_off )
c->r1_m_len = 0;
c->m3_m++;
}
return op;
}
@@ -143,14 +148,18 @@ lzo1b_999_compress_callback ( const lzo_bytep in , lzo_uint in_len,
c->r1_m_len = 0;
r = init_match(c, swd, NULL, 0, 0);
if (r != 0)
return r;
if (max_chain > 0)
swd->max_chain = max_chain;
r = find_match(c, swd, 0, 0);
if (r != 0)
return r;
while (c->look > 0)
{
int lazy_match_min_gain = -1;
@@ -165,8 +174,10 @@ lzo1b_999_compress_callback ( const lzo_bytep in , lzo_uint in_len,
#endif
assert(c->ip - c->look >= in);
if (lit == 0)
ii = c->ip - c->look;
assert(ii + lit == c->ip - c->look);
assert(swd->b_char == *(c->ip - c->look));
@@ -192,20 +203,24 @@ lzo1b_999_compress_callback ( const lzo_bytep in , lzo_uint in_len,
lazy_match_min_gain = 1;
#if (M2_MIN_LEN == 2)
if (m_len == 2)
{
/* don't code a match of len 2 if we have to
code a literal run. Code a literal instead. */
m_len = 0;
}
#endif
#if (M2_MIN_LEN == M3_MIN_LEN)
if (m_len == M2_MIN_LEN && m_off > M2_MAX_OFFSET)
{
/* don't code a M3 match of len 3 if we have to
code a literal run. Code a literal instead. */
m_len = 0;
}
#endif
}
else
@@ -223,12 +238,14 @@ lzo1b_999_compress_callback ( const lzo_bytep in , lzo_uint in_len,
/* try a lazy match */
if (m_len == 0)
lazy_match_min_gain = -1;
if (lazy_match_min_gain >= 0 && c->look > m_len)
{
assert(m_len > 0);
r = find_match(c, swd, 1, 0);
assert(r == 0); LZO_UNUSED(r);
assert(r == 0);
LZO_UNUSED(r);
assert(c->look > 0);
if (m_len <= M2_MAX_LEN && m_off <= M2_MAX_OFFSET &&
@@ -253,8 +270,10 @@ lzo1b_999_compress_callback ( const lzo_bytep in , lzo_uint in_len,
ahead = 1;
assert(ii + lit + 1 == c->ip - c->look);
}
assert(m_len > 0);
}
assert(ii + lit + ahead == c->ip - c->look);
@@ -263,7 +282,8 @@ lzo1b_999_compress_callback ( const lzo_bytep in , lzo_uint in_len,
/* a literal */
lit++;
r = find_match(c, swd, 1, 0);
assert(r == 0); LZO_UNUSED(r);
assert(r == 0);
LZO_UNUSED(r);
}
else
{
@@ -286,10 +306,12 @@ lzo1b_999_compress_callback ( const lzo_bytep in , lzo_uint in_len,
{
op = STORE_RUN(op, ii, lit);
}
if (lit < R0FAST)
c->r1_m_len = m_len;
else
c->r1_m_len = 0;
lit = 0;
}
else
@@ -298,7 +320,8 @@ lzo1b_999_compress_callback ( const lzo_bytep in , lzo_uint in_len,
/* 2 - code match */
op = code_match(c, op, m_len, m_off);
r = find_match(c, swd, m_len, 1 + ahead);
assert(r == 0); LZO_UNUSED(r);
assert(r == 0);
LZO_UNUSED(r);
}
c->codesize = pd(op, out);

View File

@@ -120,6 +120,7 @@ do_compress ( const lzo_bytep in , lzo_uint in_len,
#endif
assert(ip < ip_end);
for (;;)
{
const lzo_bytep m_pos;
@@ -162,8 +163,10 @@ literal:
#if (DD_BITS == 0)
UPDATE_I(dict, 0, dindex, ip, in);
#endif
if (++ip >= ip_end)
break;
#if (DD_BITS > 0)
DVAL_NEXT(dv, ip);
#endif
@@ -224,9 +227,11 @@ match:
for (i = 0; i < D_SIZE; i++)
{
p = dict[i];
if (BOUNDS_CHECKING_OFF_IN_EXPR(p == NULL || p < in || p > in_end))
lzo_stats->unused_dict_entries++;
}
lzo_stats->unused_dict_entries_percent =
100.0 * lzo_stats->unused_dict_entries / D_SIZE;
}
@@ -234,6 +239,7 @@ match:
#if defined(LZO_RETURN_IF_NOT_COMPRESSIBLE)
/* return if op == out to indicate that we
* couldn't compress and didn't copy anything.
*/
@@ -242,6 +248,7 @@ match:
*out_len = 0;
return LZO_E_NOT_COMPRESSIBLE;
}
#endif
/* store the final literal run */

View File

@@ -74,9 +74,12 @@ _lzo1b_do_compress ( const lzo_bytep in, lzo_uint in_len,
#if defined(LZO_EOF_CODE)
#if defined(LZO_TEST_COMPRESS_OVERRUN)
if (r == LZO_E_OK && avail_out - *out_len < 3)
r = LZO_E_COMPRESS_OVERRUN;
#endif
if (r == LZO_E_OK)
{
lzo_bytep op = out + *out_len;
@@ -85,6 +88,7 @@ _lzo1b_do_compress ( const lzo_bytep in, lzo_uint in_len,
op[2] = 0;
*out_len += 3;
}
#endif

View File

@@ -86,6 +86,7 @@
/* 2b) code the match */
#if (_M2_MAX_OFFSET != _M3_MAX_OFFSET)
if (m_off <= M2_MAX_OFFSET)
{
#else
@@ -122,6 +123,7 @@
m3 = op; /* set M3 pointer */
#endif
}
#endif /* (_M2_MAX_OFFSET != _M3_MAX_OFFSET) */
@@ -137,7 +139,9 @@
#if (CLEVEL == 9) || (CLEVEL >= 7 && M2L_BITS <= 4) || (CLEVEL >= 5 && M2L_BITS <= 3)
/* Insert the whole match (ii+1)..(ip-1) into dictionary. */
++ii;
do {
do
{
DVAL_NEXT(dv, ii);
#if 0
UPDATE_D(dict, drun, dv, ii, in);
@@ -145,7 +149,9 @@
dict[ DINDEX(dv, ii) ] = DENTRY(ii, in);
#endif
MI
} while (++ii < ip);
}
while (++ii < ip);
DVAL_NEXT(dv, ii);
assert(ii == ip);
DVAL_ASSERT(dv, ip);
@@ -176,8 +182,10 @@
{
const lzo_bytep end;
end = in_end;
while (ip < end && *m_pos == *ip)
m_pos++, ip++;
assert(ip <= in_end);
m_len = pd(ip, ii);
}
@@ -209,11 +217,13 @@
*op++ = M4_MARKER;
/* code match len */
m_len -= M4_MIN_LEN - 1;
while (m_len > 255)
{
m_len -= 255;
*op++ = 0;
}
assert(m_len > 0);
*op++ = LZO_BYTE(m_len);
LZO_STATS(lzo_stats->m4_matches++);
@@ -242,7 +252,9 @@
/* Insert the whole match (ii+1)..(ip-1) into dictionary. */
/* This is not recommended because it can be slow. */
++ii;
do {
do
{
DVAL_NEXT(dv, ii);
#if 0
UPDATE_D(dict, drun, dv, ii, in);
@@ -250,7 +262,9 @@
dict[ DINDEX(dv, ii) ] = DENTRY(ii, in);
#endif
MI
} while (++ii < ip);
}
while (++ii < ip);
DVAL_NEXT(dv, ii);
assert(ii == ip);
DVAL_ASSERT(dv, ip);

View File

@@ -38,11 +38,13 @@
************************************************************************/
assert(ip < ip_end);
if (pd(ip, ii) > 0)
{
lzo_uint t = pd(ip, ii);
#if defined(LZO_HAVE_R1)
if (ip == r1)
{
/* Code a context sensitive R1 match. */
@@ -66,6 +68,7 @@
LZO_STATS(lzo_stats->lit_runs++);
LZO_STATS(lzo_stats->lit_run[t]++);
#if defined(LZO_HAVE_M3)
if (t < LZO_SIZE(8 - M3O_BITS) && op == m3)
{
/* Code a very short literal run into the low offset bits
@@ -81,6 +84,7 @@
{
*op++ = LZO_BYTE(t);
}
MEMCPY_DS(op, ii, t);
#if defined(LZO_HAVE_R1)
r1 = ip + (M2_MIN_LEN + 1); /* set new R1 pointer */
@@ -91,7 +95,8 @@
/* inline the copying of a short R0 run */
LZO_STATS(lzo_stats->literals += t);
LZO_STATS(lzo_stats->r0short_runs++);
*op++ = 0; *op++ = LZO_BYTE(t - R0MIN);
*op++ = 0;
*op++ = LZO_BYTE(t - R0MIN);
MEMCPY_DS(op, ii, t);
#if defined(LZO_HAVE_R1)
r1 = ip + (M2_MIN_LEN + 1); /* set new R1 pointer */

View File

@@ -63,9 +63,11 @@ DO_DECOMPRESS ( const lzo_bytep in , lzo_uint in_len,
{
NEED_IP(1);
t = *ip++;
if (t >= R0FAST - R0MIN) /* a long R0 run */
{
t -= R0FAST - R0MIN;
if (t == 0)
t = R0FAST;
else
@@ -75,37 +77,57 @@ DO_DECOMPRESS ( const lzo_bytep in , lzo_uint in_len,
#else
/* help the optimizer */
lzo_uint tt = 256;
do tt <<= 1; while (--t > 0);
do tt <<= 1;
while (--t > 0);
t = tt;
#endif
}
NEED_IP(t); NEED_OP(t);
NEED_IP(t);
NEED_OP(t);
#if 1 && (LZO_OPT_UNALIGNED32)
do {
do
{
UA_COPY4(op + 0, ip + 0);
UA_COPY4(op + 4, ip + 4);
op += 8; ip += 8;
op += 8;
ip += 8;
t -= 8;
} while (t > 0);
}
while (t > 0);
#else
MEMCPY8_DS(op, ip, t);
#endif
continue;
}
t += R0MIN; /* a short R0 run */
}
NEED_IP(t); NEED_OP(t);
NEED_IP(t);
NEED_OP(t);
/* copy literal run */
#if 1 && (LZO_OPT_UNALIGNED32)
if (t >= 4)
{
do {
do
{
UA_COPY4(op, ip);
op += 4; ip += 4; t -= 4;
} while (t >= 4);
if (t > 0) do *op++ = *ip++; while (--t > 0);
op += 4;
ip += 4;
t -= 4;
}
while (t >= 4);
if (t > 0) do* op++ = *ip++;
while (--t > 0);
}
else
#endif
@@ -113,7 +135,10 @@ DO_DECOMPRESS ( const lzo_bytep in , lzo_uint in_len,
#if (M3O_BITS < 7)
literal1:
#endif
do *op++ = *ip++; while (--t > 0);
do* op++ = *ip++;
while (--t > 0);
}
#if (M3O_BITS == 7)
@@ -124,16 +149,19 @@ literal2:
while (TEST_IP_AND_TEST_OP)
{
t = *ip++; /* get R1 marker */
if (t >= R0MIN)
goto match;
NEED_IP(2); NEED_OP(M2_MIN_LEN + 1);
NEED_IP(2);
NEED_OP(M2_MIN_LEN + 1);
/* R1 match - a M2_MIN_LEN match + 1 byte literal */
assert((t & M2O_MASK) == t);
m_pos = op - M2_MIN_OFFSET;
m_pos -= t | (((lzo_uint) * ip++) << M2O_BITS);
assert(m_pos >= out); assert(m_pos < op);
assert(m_pos >= out);
assert(m_pos < op);
TEST_LB(m_pos);
COPY_M2;
*op++ = *ip++;
@@ -152,7 +180,8 @@ match:
NEED_IP(1);
m_pos = op - M2_MIN_OFFSET;
m_pos -= (t & M2O_MASK) | (((lzo_uint) * ip++) << M2O_BITS);
assert(m_pos >= out); assert(m_pos < op);
assert(m_pos >= out);
assert(m_pos < op);
TEST_LB(m_pos);
/* get match len */
@@ -165,9 +194,11 @@ match:
{
/* get match len */
t &= M3L_MASK;
if (t == 0) /* a M4 match */
{
NEED_IP(1);
while (*ip == 0)
{
t += 255;
@@ -175,6 +206,7 @@ match:
TEST_OV(t);
NEED_IP(1);
}
t += (M4_MIN_LEN - M3_MIN_LEN) + *ip++;
}
@@ -184,23 +216,38 @@ match:
m_pos -= *ip++ & M3O_MASK;
m_pos -= (lzo_uint)(*ip++) << M3O_BITS;
#if defined(LZO_EOF_CODE)
if (m_pos == op)
goto eof_found;
#endif
/* copy match */
assert(m_pos >= out); assert(m_pos < op);
TEST_LB(m_pos); NEED_OP(t + M3_MIN_LEN - 1);
assert(m_pos >= out);
assert(m_pos < op);
TEST_LB(m_pos);
NEED_OP(t + M3_MIN_LEN - 1);
#if (LZO_OPT_UNALIGNED32)
if (t >= 2 * 4 - (M3_MIN_LEN - 1) && (op - m_pos) >= 4)
{
UA_COPY4(op, m_pos);
op += 4; m_pos += 4; t -= 4 - (M3_MIN_LEN - 1);
do {
op += 4;
m_pos += 4;
t -= 4 - (M3_MIN_LEN - 1);
do
{
UA_COPY4(op, m_pos);
op += 4; m_pos += 4; t -= 4;
} while (t >= 4);
if (t > 0) do *op++ = *m_pos++; while (--t > 0);
op += 4;
m_pos += 4;
t -= 4;
}
while (t >= 4);
if (t > 0) do* op++ = *m_pos++;
while (--t > 0);
}
else
#endif
@@ -212,19 +259,25 @@ match:
#if (M3O_BITS < 7)
t = ip[-2] >> M3O_BITS;
if (t)
{
NEED_IP(t); NEED_OP(t);
NEED_IP(t);
NEED_OP(t);
goto literal1;
}
#elif (M3O_BITS == 7)
/* optimized version */
if (ip[-2] & (1 << M3O_BITS))
{
NEED_IP(1); NEED_OP(1);
NEED_IP(1);
NEED_OP(1);
*op++ = *ip++;
goto literal2;
}
#endif
}
}

View File

@@ -52,37 +52,48 @@ STORE_RUN ( lzo_bytep const oo, const lzo_bytep const ii, lzo_uint r_len)
while (r_len >= (t = tt))
{
r_len -= t;
*op++ = 0; *op++ = (R0FAST - R0MIN) + 7;
*op++ = 0;
*op++ = (R0FAST - R0MIN) + 7;
MEMCPY8_DS(op, ip, t);
LZO_STATS(lzo_stats->r0long_runs++);
}
tt >>= 1;
do {
do
{
if (r_len >= (t = tt))
{
r_len -= t;
*op++ = 0; *op++ = LZO_BYTE((R0FAST - R0MIN) + r_bits);
*op++ = 0;
*op++ = LZO_BYTE((R0FAST - R0MIN) + r_bits);
MEMCPY8_DS(op, ip, t);
LZO_STATS(lzo_stats->r0long_runs++);
}
tt >>= 1;
} while (--r_bits > 0);
}
while (--r_bits > 0);
}
assert(r_len < 512);
while (r_len >= (t = R0FAST))
{
r_len -= t;
*op++ = 0; *op++ = (R0FAST - R0MIN);
*op++ = 0;
*op++ = (R0FAST - R0MIN);
MEMCPY8_DS(op, ip, t);
LZO_STATS(lzo_stats->r0fast_runs++);
}
t = r_len;
if (t >= R0MIN)
{
/* code a short R0 run */
*op++ = 0; *op++ = LZO_BYTE(t - R0MIN);
*op++ = 0;
*op++ = LZO_BYTE(t - R0MIN);
MEMCPY_DS(op, ip, t);
LZO_STATS(lzo_stats->r0short_runs++);
}

View File

@@ -42,18 +42,25 @@
/* search ip in the dictionary */
DINDEX1(dindex, ip);
GINDEX(m_pos, m_off, dict, dindex, in);
if (LZO_CHECK_MPOS_NON_DET(m_pos, m_off, in, ip, M3_MAX_OFFSET))
goto literal;
#if 1
if (m_off <= M2_MAX_OFFSET || m_pos[3] == ip[3])
goto try_match;
DINDEX2(dindex, ip);
#endif
GINDEX(m_pos, m_off, dict, dindex, in);
if (LZO_CHECK_MPOS_NON_DET(m_pos, m_off, in, ip, M3_MAX_OFFSET))
goto literal;
if (m_off <= M2_MAX_OFFSET || m_pos[3] == ip[3])
goto try_match;
goto literal;
@@ -73,7 +80,9 @@
ip_sav = ip;
m_len = 0;
do {
do
{
#if !defined(NDEBUG)
const lzo_bytep z_pos = NULL;
#endif
@@ -92,6 +101,7 @@
if (LZO_CHECK_MPOS(m_pos, x_off, in, ip, MAX_OFFSET))
#if (CLEVEL == 9)
*d = DENTRY(ip, in);
#else
((void)(0));
#endif
@@ -102,16 +112,19 @@
#if !(LZO_DICT_USE_PTR)
assert((z_pos = ip - 3 - x_off) == (m_pos - 3));
#endif
/* a match */
if (MATCH_M2)
{
x_len = pd((ip - 1), ip_sav);
if (x_len > m_len)
{
m_len = x_len;
m_off = x_off;
assert((m_pos_sav = z_pos) != NULL);
}
#if (CLEVEL == 9)
/* try to find a closer match */
else if (x_len == m_len && x_off < m_off)
@@ -119,6 +132,7 @@
m_off = x_off;
assert((m_pos_sav = z_pos) != NULL);
}
#endif
}
else
@@ -129,16 +143,20 @@
{
const lzo_bytep end;
end = MATCH_IP_END;
while (ip < end && *m_pos == *ip)
m_pos++, ip++;
assert(ip <= in_end);
x_len = pd(ip, ip_sav);
}
if (x_len > m_len)
{
m_len = x_len;
m_off = x_off;
assert((m_pos_sav = z_pos) != NULL);
if (ip >= MATCH_IP_END)
{
ip = ip_sav;
@@ -156,7 +174,9 @@
m_off = x_off;
assert((m_pos_sav = z_pos) != NULL);
}
#else
/* try to find a closer match */
if (m_len < M2_MAX_LEN + 1 || x_off < m_off)
{
@@ -164,6 +184,7 @@
m_off = x_off;
assert((m_pos_sav = z_pos) != NULL);
}
#endif
#else
/* don't search for a longer/closer match */
@@ -177,12 +198,16 @@
goto match;
#endif
}
ip = ip_sav;
}
else
ip = ip_sav;
d++;
} while (--j > 0);
}
while (--j > 0);
assert(ip == ip_sav);
d -= DD_SIZE;

View File

@@ -48,6 +48,7 @@ try_match:
#endif
m_pos_sav = m_pos;
#endif
if (m_pos[0] == ip[0] && m_pos[1] == ip[1] && m_pos[2] == ip[2])
{
m_pos += 3;
@@ -62,19 +63,25 @@ try_match:
if (m_len > M2_MIN_LEN)
goto match;
if (m_len == M2_MIN_LEN)
{
#if (_MAX_OFFSET == _M2_MAX_OFFSET)
goto match;
#else
if (m_off <= M2_MAX_OFFSET)
goto match;
#if 0 && (M3_MIN_LEN == M2_MIN_LEN)
if (ip == ii)
goto match;
#endif
#endif
}
goto literal;

View File

@@ -58,6 +58,7 @@ static lzo_compress_t lzo1b_get_compress_func(int clevel)
else
return (lzo_compress_t) 0;
}
f = c_funcs[clevel - 1];
assert(f && *f);
return *f;
@@ -73,8 +74,10 @@ lzo1b_compress ( const lzo_bytep src, lzo_uint src_len,
lzo_compress_t f;
f = lzo1b_get_compress_func(clevel);
if (!f)
return LZO_E_ERROR;
return _lzo1b_do_compress(src, src_len, dst, dst_len, wrkmem, f);
}

View File

@@ -71,6 +71,7 @@ code_match ( LZO_COMPRESS_T *c, lzo_bytep op, lzo_uint m_len, lzo_uint m_off )
assert(m_off <= M3_MAX_OFFSET);
m_off -= M3_MIN_OFFSET - M3_EOF_OFFSET;
/* code match len */
if (m_len <= M3_MAX_LEN)
*op++ = LZO_BYTE(M3_MARKER | (m_len - (M3_MIN_LEN - 1)));
@@ -81,14 +82,17 @@ code_match ( LZO_COMPRESS_T *c, lzo_bytep op, lzo_uint m_len, lzo_uint m_off )
*op++ = M4_MARKER;
/* code match len */
m_len -= M4_MIN_LEN - 1;
while (m_len > 255)
{
m_len -= 255;
*op++ = 0;
}
assert(m_len > 0);
*op++ = LZO_BYTE(m_len);
}
/* code low offset bits */
*op++ = LZO_BYTE(m_off & M3O_MASK);
/* code high offset bits */
@@ -98,6 +102,7 @@ code_match ( LZO_COMPRESS_T *c, lzo_bytep op, lzo_uint m_len, lzo_uint m_off )
c->m3 = op;
c->m3_m++;
}
return op;
}
@@ -145,14 +150,18 @@ lzo1c_999_compress_callback ( const lzo_bytep in , lzo_uint in_len,
c->m3 = out + 1; /* pointer after last m3/m4 match */
r = init_match(c, swd, NULL, 0, 0);
if (r != 0)
return r;
if (max_chain > 0)
swd->max_chain = max_chain;
r = find_match(c, swd, 0, 0);
if (r != 0)
return r;
while (c->look > 0)
{
int lazy_match_min_gain = -1;
@@ -167,8 +176,10 @@ lzo1c_999_compress_callback ( const lzo_bytep in , lzo_uint in_len,
#endif
assert(c->ip - c->look >= in);
if (lit == 0)
ii = c->ip - c->look;
assert(ii + lit == c->ip - c->look);
assert(swd->b_char == *(c->ip - c->look));
@@ -198,20 +209,24 @@ lzo1c_999_compress_callback ( const lzo_bytep in , lzo_uint in_len,
lazy_match_min_gain = 1;
#if (M2_MIN_LEN == 2)
if (m_len == 2)
{
/* don't code a match of len 2 if we have to
code a literal run. Code a literal instead. */
m_len = 0;
}
#endif
#if (M2_MIN_LEN == M3_MIN_LEN)
if (m_len == M2_MIN_LEN && m_off > M2_MAX_OFFSET)
{
/* don't code a M3 match of len 3 if we have to
code a literal run. Code a literal instead. */
m_len = 0;
}
#endif
}
else
@@ -229,12 +244,14 @@ lzo1c_999_compress_callback ( const lzo_bytep in , lzo_uint in_len,
/* try a lazy match */
if (m_len == 0)
lazy_match_min_gain = -1;
if (lazy_match_min_gain >= 0 && c->look > m_len)
{
assert(m_len > 0);
r = find_match(c, swd, 1, 0);
assert(r == 0); LZO_UNUSED(r);
assert(r == 0);
LZO_UNUSED(r);
assert(c->look > 0);
if (m_len <= M2_MAX_LEN && m_off <= M2_MAX_OFFSET &&
@@ -259,8 +276,10 @@ lzo1c_999_compress_callback ( const lzo_bytep in , lzo_uint in_len,
ahead = 1;
assert(ii + lit + 1 == c->ip - c->look);
}
assert(m_len > 0);
}
assert(ii + lit + ahead == c->ip - c->look);
@@ -269,7 +288,8 @@ lzo1c_999_compress_callback ( const lzo_bytep in , lzo_uint in_len,
/* a literal */
lit++;
r = find_match(c, swd, 1, 0);
assert(r == 0); LZO_UNUSED(r);
assert(r == 0);
LZO_UNUSED(r);
}
else
{
@@ -300,10 +320,12 @@ lzo1c_999_compress_callback ( const lzo_bytep in , lzo_uint in_len,
{
op = STORE_RUN(op, ii, lit);
}
if (lit < R0FAST)
c->r1_m_len = m_len;
else
c->r1_m_len = 0;
lit = 0;
}
else
@@ -312,7 +334,8 @@ lzo1c_999_compress_callback ( const lzo_bytep in , lzo_uint in_len,
/* 2 - code match */
op = code_match(c, op, m_len, m_off);
r = find_match(c, swd, m_len, 1 + ahead);
assert(r == 0); LZO_UNUSED(r);
assert(r == 0);
LZO_UNUSED(r);
}
c->codesize = pd(op, out);

View File

@@ -74,9 +74,12 @@ _lzo1c_do_compress ( const lzo_bytep in, lzo_uint in_len,
#if defined(LZO_EOF_CODE)
#if defined(LZO_TEST_COMPRESS_OVERRUN)
if (r == LZO_E_OK && avail_out - *out_len < 3)
r = LZO_E_COMPRESS_OVERRUN;
#endif
if (r == LZO_E_OK)
{
lzo_bytep op = out + *out_len;
@@ -85,6 +88,7 @@ _lzo1c_do_compress ( const lzo_bytep in, lzo_uint in_len,
op[2] = 0;
*out_len += 3;
}
#endif

View File

@@ -58,6 +58,7 @@ static lzo_compress_t lzo1c_get_compress_func(int clevel)
else
return (lzo_compress_t) 0;
}
f = c_funcs[clevel - 1];
assert(f && *f);
return *f;
@@ -73,8 +74,10 @@ lzo1c_compress ( const lzo_bytep src, lzo_uint src_len,
lzo_compress_t f;
f = lzo1c_get_compress_func(clevel);
if (!f)
return LZO_E_ERROR;
return _lzo1c_do_compress(src, src_len, dst, dst_len, wrkmem, f);
}

View File

@@ -69,6 +69,7 @@ int do_compress ( const lzo_bytep in , lzo_uint in_len,
ii = ip;
ip++;
for (;;)
{
const lzo_bytep m_pos;
@@ -79,23 +80,31 @@ int do_compress ( const lzo_bytep in , lzo_uint in_len,
DINDEX1(dindex, ip);
GINDEX(m_pos, m_off, dict, dindex, in);
if (LZO_CHECK_MPOS_NON_DET(m_pos, m_off, in, ip, M3_MAX_OFFSET))
goto literal;
#if 1
if (m_off <= M2_MAX_OFFSET || m_pos[3] == ip[3])
goto try_match;
DINDEX2(dindex, ip);
#endif
GINDEX(m_pos, m_off, dict, dindex, in);
if (LZO_CHECK_MPOS_NON_DET(m_pos, m_off, in, ip, M3_MAX_OFFSET))
goto literal;
if (m_off <= M2_MAX_OFFSET || m_pos[3] == ip[3])
goto try_match;
goto literal;
try_match:
#if 0 && (LZO_OPT_UNALIGNED16)
if (UA_GET_NE16(m_pos) != UA_GET_NE16(ip))
#else
if (m_pos[0] != ip[0] || m_pos[1] != ip[1])
@@ -108,16 +117,23 @@ try_match:
{
m_pos += 3;
#if 0
if (m_off <= M2_MAX_OFFSET)
goto match;
if (lit <= 3)
goto match;
if (lit == 3) /* better compression, but slower */
{
assert(op - 2 > out); op[-2] |= LZO_BYTE(3);
*op++ = *ii++; *op++ = *ii++; *op++ = *ii++;
assert(op - 2 > out);
op[-2] |= LZO_BYTE(3);
*op++ = *ii++;
*op++ = *ii++;
*op++ = *ii++;
goto code_match;
}
if (*m_pos == ip[3])
#endif
goto match;
@@ -128,8 +144,10 @@ try_match:
/* a literal */
literal:
UPDATE_I(dict, 0, dindex, ip, in);
if (++ip >= ip_end)
break;
continue;
@@ -138,6 +156,7 @@ match:
UPDATE_I(dict, 0, dindex, ip, in);
/* store current literal run */
lit = pd(ip, ii);
if (lit > 0)
{
lzo_uint t = lit;
@@ -151,28 +170,36 @@ match:
lzo_uint tt = t - 31;
*op++ = 0;
while (tt > 255)
{
tt -= 255;
UA_SET1(op, 0);
op++;
}
assert(tt > 0);
*op++ = LZO_BYTE(tt);
}
do *op++ = *ii++; while (--t > 0);
do* op++ = *ii++;
while (--t > 0);
}
assert(ii == ip);
/* code the match */
ip += 3;
if (*m_pos++ != *ip++ || *m_pos++ != *ip++ || *m_pos++ != *ip++ ||
*m_pos++ != *ip++ || *m_pos++ != *ip++ || *m_pos++ != *ip++)
{
--ip;
m_len = pd(ip, ii);
assert(m_len >= 3); assert(m_len <= 8);
assert(m_len >= 3);
assert(m_len <= 8);
if (m_off <= M2_MAX_OFFSET)
{
@@ -199,8 +226,10 @@ match:
{
const lzo_bytep end;
end = in_end;
while (ip < end && *m_pos == *ip)
m_pos++, ip++;
m_len = pd(ip, ii);
}
assert(m_len >= 3);
@@ -211,20 +240,24 @@ match:
{
m_len -= 33;
*op++ = M3_MARKER | 0;
while (m_len > 255)
{
m_len -= 255;
UA_SET1(op, 0);
op++;
}
assert(m_len > 0);
*op++ = LZO_BYTE(m_len);
}
*op++ = LZO_BYTE((m_off & 63) << 2);
*op++ = LZO_BYTE(m_off >> 6);
}
ii = ip;
if (ip >= ip_end)
break;
}
@@ -244,15 +277,18 @@ match:
lzo_uint tt = t - 31;
*op++ = 0;
while (tt > 255)
{
tt -= 255;
UA_SET1(op, 0);
op++;
}
assert(tt > 0);
*op++ = LZO_BYTE(tt);
}
UA_COPYN(op, ii, t);
op += t;
}
@@ -279,7 +315,11 @@ lzo1f_1_compress ( const lzo_bytep in , lzo_uint in_len,
else if (in_len <= 10)
{
*op++ = LZO_BYTE(in_len);
do *op++ = *in++; while (--in_len > 0);
do* op++ = *in++;
while (--in_len > 0);
*out_len = pd(op, out);
}
else

View File

@@ -76,14 +76,17 @@ code_match ( LZO_COMPRESS_T *c, lzo_bytep op, lzo_uint m_len, lzo_uint m_off )
{
m_len -= M3_MAX_LEN;
*op++ = M3_MARKER | 0;
while (m_len > 255)
{
m_len -= 255;
*op++ = 0;
}
assert(m_len > 0);
*op++ = LZO_BYTE(m_len);
}
*op++ = LZO_BYTE((m_off & 63) << 2);
*op++ = LZO_BYTE(m_off >> 6);
c->m3_m++;
@@ -105,15 +108,20 @@ STORE_RUN ( lzo_bytep op, const lzo_bytep ii, lzo_uint t, lzo_bytep out )
lzo_uint tt = t - 31;
*op++ = 0;
while (tt > 255)
{
tt -= 255;
*op++ = 0;
}
assert(tt > 0);
*op++ = LZO_BYTE(tt);
}
do *op++ = *ii++; while (--t > 0);
do* op++ = *ii++;
while (--t > 0);
return op;
}
@@ -161,14 +169,18 @@ lzo1f_999_compress_callback ( const lzo_bytep in , lzo_uint in_len,
c->r1_lit = c->r1_m_len = 0;
r = init_match(c, swd, NULL, 0, 0);
if (r != 0)
return r;
if (max_chain > 0)
swd->max_chain = max_chain;
r = find_match(c, swd, 0, 0);
if (r != 0)
return r;
while (c->look > 0)
{
int lazy_match_min_gain = -1;
@@ -178,8 +190,10 @@ lzo1f_999_compress_callback ( const lzo_bytep in , lzo_uint in_len,
m_off = c->m_off;
assert(c->ip - c->look >= in);
if (lit == 0)
ii = c->ip - c->look;
assert(ii + lit == c->ip - c->look);
assert(swd->b_char == *(c->ip - c->look));
@@ -209,7 +223,8 @@ lzo1f_999_compress_callback ( const lzo_bytep in , lzo_uint in_len,
if (m_len > 0 && lazy_match_min_gain >= 0 && c->look > m_len)
{
r = find_match(c, swd, 1, 0);
assert(r == 0); LZO_UNUSED(r);
assert(r == 0);
LZO_UNUSED(r);
assert(c->look > 0);
if (m_len <= M2_MAX_LEN && m_off <= M2_MAX_OFFSET &&
@@ -250,8 +265,10 @@ lzo1f_999_compress_callback ( const lzo_bytep in , lzo_uint in_len,
ahead = 1;
assert(ii + lit + 1 == c->ip - c->look);
}
assert(m_len > 0);
}
assert(ii + lit + ahead == c->ip - c->look);
@@ -260,7 +277,8 @@ lzo1f_999_compress_callback ( const lzo_bytep in , lzo_uint in_len,
/* a literal */
lit++;
r = find_match(c, swd, 1, 0);
assert(r == 0); LZO_UNUSED(r);
assert(r == 0);
LZO_UNUSED(r);
}
else
{
@@ -278,7 +296,8 @@ lzo1f_999_compress_callback ( const lzo_bytep in , lzo_uint in_len,
/* 2 - code match */
op = code_match(c, op, m_len, m_off);
r = find_match(c, swd, m_len, 1 + ahead);
assert(r == 0); LZO_UNUSED(r);
assert(r == 0);
LZO_UNUSED(r);
}
c->codesize = pd(op, out);

View File

@@ -58,6 +58,7 @@ DO_DECOMPRESS ( const lzo_bytep in , lzo_uint in_len,
while (TEST_IP_AND_TEST_OP)
{
t = *ip++;
if (t > 31)
goto match;
@@ -65,6 +66,7 @@ DO_DECOMPRESS ( const lzo_bytep in , lzo_uint in_len,
if (t == 0)
{
NEED_IP(1);
while (*ip == 0)
{
t += 255;
@@ -72,22 +74,36 @@ DO_DECOMPRESS ( const lzo_bytep in , lzo_uint in_len,
TEST_IV(t);
NEED_IP(1);
}
t += 31 + *ip++;
}
/* copy literals */
assert(t > 0); NEED_OP(t); NEED_IP(t+1);
assert(t > 0);
NEED_OP(t);
NEED_IP(t + 1);
#if (LZO_OPT_UNALIGNED32)
if (t >= 4)
{
do {
do
{
UA_COPY4(op, ip);
op += 4; ip += 4; t -= 4;
} while (t >= 4);
if (t > 0) do *op++ = *ip++; while (--t > 0);
op += 4;
ip += 4;
t -= 4;
}
while (t >= 4);
if (t > 0) do* op++ = *ip++;
while (--t > 0);
}
else
#endif
do *op++ = *ip++; while (--t > 0);
do* op++ = *ip++;
while (--t > 0);
t = *ip++;
@@ -99,27 +115,35 @@ DO_DECOMPRESS ( const lzo_bytep in , lzo_uint in_len,
m_pos = op - 1 - 0x800;
m_pos -= (t >> 2) & 7;
m_pos -= *ip++ << 3;
TEST_LB(m_pos); NEED_OP(3);
*op++ = *m_pos++; *op++ = *m_pos++; *op++ = *m_pos++;
TEST_LB(m_pos);
NEED_OP(3);
*op++ = *m_pos++;
*op++ = *m_pos++;
*op++ = *m_pos++;
}
else
{
match:
if (t < M3_MARKER)
{
m_pos = op - 1;
m_pos -= (t >> 2) & 7;
m_pos -= *ip++ << 3;
t >>= 5;
TEST_LB(m_pos); assert(t > 0); NEED_OP(t+3-1);
TEST_LB(m_pos);
assert(t > 0);
NEED_OP(t + 3 - 1);
goto copy_match;
}
else
{
t &= 31;
if (t == 0)
{
NEED_IP(1);
while (*ip == 0)
{
t += 255;
@@ -127,8 +151,10 @@ match:
TEST_OV(t);
NEED_IP(1);
}
t += 31 + *ip++;
}
NEED_IP(2);
m_pos = op;
#if (LZO_OPT_UNALIGNED16) && (LZO_ABI_LITTLE_ENDIAN)
@@ -138,38 +164,64 @@ match:
m_pos -= *ip++ >> 2;
m_pos -= *ip++ << 6;
#endif
if (m_pos == op)
goto eof_found;
}
/* copy match */
TEST_LB(m_pos); assert(t > 0); NEED_OP(t+3-1);
TEST_LB(m_pos);
assert(t > 0);
NEED_OP(t + 3 - 1);
#if (LZO_OPT_UNALIGNED32)
if (t >= 2 * 4 - (3 - 1) && (op - m_pos) >= 4)
{
UA_COPY4(op, m_pos);
op += 4; m_pos += 4; t -= 4 - (3 - 1);
do {
op += 4;
m_pos += 4;
t -= 4 - (3 - 1);
do
{
UA_COPY4(op, m_pos);
op += 4; m_pos += 4; t -= 4;
} while (t >= 4);
if (t > 0) do *op++ = *m_pos++; while (--t > 0);
op += 4;
m_pos += 4;
t -= 4;
}
while (t >= 4);
if (t > 0) do* op++ = *m_pos++;
while (--t > 0);
}
else
#endif
{
copy_match:
*op++ = *m_pos++; *op++ = *m_pos++;
do *op++ = *m_pos++; while (--t > 0);
*op++ = *m_pos++;
*op++ = *m_pos++;
do* op++ = *m_pos++;
while (--t > 0);
}
}
t = ip[-2] & 3;
if (t == 0)
break;
/* copy literals */
assert(t > 0); NEED_OP(t); NEED_IP(t+1);
do *op++ = *ip++; while (--t > 0);
assert(t > 0);
NEED_OP(t);
NEED_IP(t + 1);
do* op++ = *ip++;
while (--t > 0);
t = *ip++;
}
}

View File

@@ -140,10 +140,12 @@ code_match ( LZO_COMPRESS_T *c, lzo_bytep op, lzo_uint m_len, lzo_uint m_off )
#endif
assert(op > c->out);
if (m_len == 2)
{
assert(m_off <= M1_MAX_OFFSET);
assert(c->r1_lit > 0); assert(c->r1_lit < 4);
assert(c->r1_lit > 0);
assert(c->r1_lit < 4);
m_off -= 1;
#if defined(LZO1Z)
*op++ = LZO_BYTE(M1_MARKER | (m_off >> 6));
@@ -154,6 +156,7 @@ code_match ( LZO_COMPRESS_T *c, lzo_bytep op, lzo_uint m_len, lzo_uint m_off )
#endif
c->m1a_m++;
}
#if defined(LZO1Z)
else if (m_len <= M2_MAX_LEN && (m_off <= M2_MAX_OFFSET || m_off == c->last_m_off))
#else
@@ -172,6 +175,7 @@ code_match ( LZO_COMPRESS_T *c, lzo_bytep op, lzo_uint m_len, lzo_uint m_off )
*op++ = LZO_BYTE(m_off >> 2);
assert(op[-2] >= M2_MARKER);
#elif defined(LZO1Z)
if (m_off == c->last_m_off)
*op++ = LZO_BYTE(((m_len - 1) << 5) | (0x700 >> 6));
else
@@ -180,6 +184,7 @@ code_match ( LZO_COMPRESS_T *c, lzo_bytep op, lzo_uint m_len, lzo_uint m_off )
*op++ = LZO_BYTE(((m_len - 1) << 5) | (m_off >> 6));
*op++ = LZO_BYTE(m_off << 2);
}
#endif
c->m2_m++;
}
@@ -201,20 +206,24 @@ code_match ( LZO_COMPRESS_T *c, lzo_bytep op, lzo_uint m_len, lzo_uint m_off )
{
assert(m_len >= 3);
m_off -= 1;
if (m_len <= M3_MAX_LEN)
*op++ = LZO_BYTE(M3_MARKER | (m_len - 2));
else
{
m_len -= M3_MAX_LEN;
*op++ = M3_MARKER | 0;
while (m_len > 255)
{
m_len -= 255;
*op++ = 0;
}
assert(m_len > 0);
*op++ = LZO_BYTE(m_len);
}
#if defined(LZO1Z)
*op++ = LZO_BYTE(m_off >> 6);
*op++ = LZO_BYTE(m_off << 2);
@@ -229,23 +238,28 @@ code_match ( LZO_COMPRESS_T *c, lzo_bytep op, lzo_uint m_len, lzo_uint m_off )
lzo_uint k;
assert(m_len >= 3);
assert(m_off > 0x4000); assert(m_off <= 0xbfff);
assert(m_off > 0x4000);
assert(m_off <= 0xbfff);
m_off -= 0x4000;
k = (m_off & 0x4000) >> 11;
if (m_len <= M4_MAX_LEN)
*op++ = LZO_BYTE(M4_MARKER | k | (m_len - 2));
else
{
m_len -= M4_MAX_LEN;
*op++ = LZO_BYTE(M4_MARKER | k | 0);
while (m_len > 255)
{
m_len -= 255;
*op++ = 0;
}
assert(m_len > 0);
*op++ = LZO_BYTE(m_len);
}
#if defined(LZO1Z)
*op++ = LZO_BYTE(m_off >> 6);
*op++ = LZO_BYTE(m_off << 2);
@@ -290,16 +304,21 @@ STORE_RUN ( LZO_COMPRESS_T *c, lzo_bytep op, const lzo_bytep ii, lzo_uint t )
lzo_uint tt = t - 18;
*op++ = 0;
while (tt > 255)
{
tt -= 255;
*op++ = 0;
}
assert(tt > 0);
*op++ = LZO_BYTE(tt);
c->lit3_r++;
}
do *op++ = *ii++; while (--t > 0);
do* op++ = *ii++;
while (--t > 0);
return op;
}
@@ -338,36 +357,48 @@ len_of_coded_match ( lzo_uint m_len, lzo_uint m_off, lzo_uint lit )
if (m_len < 2)
return 0;
if (m_len == 2)
return (m_off <= M1_MAX_OFFSET && lit > 0 && lit < 4) ? 2 : 0;
if (m_len <= M2_MAX_LEN && m_off <= M2_MAX_OFFSET)
return 2;
if (m_len == M2_MIN_LEN && m_off <= MX_MAX_OFFSET && lit >= 4)
return 2;
if (m_off <= M3_MAX_OFFSET)
{
if (m_len <= M3_MAX_LEN)
return 3;
m_len -= M3_MAX_LEN;
while (m_len > 255)
{
m_len -= 255;
n++;
}
return n;
}
if (m_off <= M4_MAX_OFFSET)
{
if (m_len <= M4_MAX_LEN)
return 3;
m_len -= M4_MAX_LEN;
while (m_len > 255)
{
m_len -= 255;
n++;
}
return n;
}
return 0;
}
@@ -381,8 +412,10 @@ min_gain(lzo_uint ahead, lzo_uint lit1, lzo_uint lit2, lzo_uint l1, lzo_uint l2,
lazy_match_min_gain = ahead;
#if 0
if (l3)
lit2 -= ahead;
#endif
if (lit1 <= 3)
@@ -391,6 +424,7 @@ min_gain(lzo_uint ahead, lzo_uint lit1, lzo_uint lit2, lzo_uint l1, lzo_uint l2,
lazy_match_min_gain += (lit2 <= 18) ? 0 : 1;
lazy_match_min_gain += (l2 - l1) * 2;
if (l3)
lazy_match_min_gain -= (ahead - l3) * 2;
@@ -398,9 +432,11 @@ min_gain(lzo_uint ahead, lzo_uint lit1, lzo_uint lit2, lzo_uint l1, lzo_uint l2,
lazy_match_min_gain = 0;
#if 0
if (l1 == 2)
if (lazy_match_min_gain == 0)
lazy_match_min_gain = 1;
#endif
return lazy_match_min_gain;
@@ -419,6 +455,7 @@ void assert_match( const lzo_swd_p swd, lzo_uint m_len, lzo_uint m_off )
lzo_uint d_off;
assert(m_len >= 2);
if (m_off <= (lzo_uint)(c->bp - c->in))
{
assert(c->bp - m_off + m_len < c->ip);
@@ -429,6 +466,7 @@ void assert_match( const lzo_swd_p swd, lzo_uint m_len, lzo_uint m_off )
assert(swd->dict != NULL);
d_off = m_off - (lzo_uint)(c->bp - c->in);
assert(d_off <= swd->dict_len);
if (m_len > d_off)
{
assert(lzo_memcmp(c->bp, swd->dict_end - d_off, d_off) == 0);
@@ -457,10 +495,14 @@ better_match ( const lzo_swd_p swd, lzo_uint *m_len, lzo_uint *m_off )
if (*m_len <= M2_MIN_LEN)
return;
#if defined(LZO1Z)
if (*m_off == c->last_m_off && *m_len <= M2_MAX_LEN)
return;
#if 1
if (*m_len >= M2_MIN_LEN + 1 && *m_len <= M2_MAX_LEN + 1 &&
c->last_m_off && swd->best_off[*m_len - 1] == c->last_m_off)
{
@@ -468,6 +510,7 @@ better_match ( const lzo_swd_p swd, lzo_uint *m_len, lzo_uint *m_off )
*m_off = swd->best_off[*m_len];
return;
}
#endif
#endif
@@ -475,6 +518,7 @@ better_match ( const lzo_swd_p swd, lzo_uint *m_len, lzo_uint *m_off )
return;
#if 1
/* M3/M4 -> M2 */
if (*m_off > M2_MAX_OFFSET &&
*m_len >= M2_MIN_LEN + 1 && *m_len <= M2_MAX_LEN + 1 &&
@@ -484,9 +528,11 @@ better_match ( const lzo_swd_p swd, lzo_uint *m_len, lzo_uint *m_off )
*m_off = swd->best_off[*m_len];
return;
}
#endif
#if 1
/* M4 -> M2 */
if (*m_off > M3_MAX_OFFSET &&
*m_len >= M4_MAX_LEN + 1 && *m_len <= M2_MAX_LEN + 2 &&
@@ -496,9 +542,11 @@ better_match ( const lzo_swd_p swd, lzo_uint *m_len, lzo_uint *m_off )
*m_off = swd->best_off[*m_len];
return;
}
#endif
#if 1
/* M4 -> M3 */
if (*m_off > M3_MAX_OFFSET &&
*m_len >= M4_MAX_LEN + 1 && *m_len <= M3_MAX_LEN + 1 &&
@@ -507,6 +555,7 @@ better_match ( const lzo_swd_p swd, lzo_uint *m_len, lzo_uint *m_off )
*m_len = *m_len - 1;
*m_off = swd->best_off[*m_len];
}
#endif
}
@@ -554,17 +603,22 @@ lzo1x_999_compress_internal ( const lzo_bytep in , lzo_uint in_len,
/* setup parameter defaults */
/* number of lazy match tries */
try_lazy = (lzo_uint) try_lazy_parm;
if (try_lazy_parm < 0)
try_lazy = 1;
/* reduce lazy match search if we already have a match with this length */
if (good_length == 0)
good_length = 32;
/* do not try a lazy match if we already have a match with this length */
if (max_lazy == 0)
max_lazy = 32;
/* stop searching for longer matches than this one */
if (nice_length == 0)
nice_length = 0;
/* don't search more positions than this */
if (max_chain == 0)
max_chain = SWD_MAX_CHAIN;
@@ -583,16 +637,21 @@ lzo1x_999_compress_internal ( const lzo_bytep in , lzo_uint in_len,
c->r1_lit = c->r1_m_len = 0;
r = init_match(c, swd, dict, dict_len, flags);
if (r != 0)
return r;
if (max_chain > 0)
swd->max_chain = max_chain;
if (nice_length > 0)
swd->nice_length = nice_length;
r = find_match(c, swd, 0, 0);
if (r != 0)
return r;
while (c->look > 0)
{
lzo_uint ahead;
@@ -606,8 +665,10 @@ lzo1x_999_compress_internal ( const lzo_bytep in , lzo_uint in_len,
assert(c->bp == c->ip - c->look);
assert(c->bp >= in);
if (lit == 0)
ii = c->bp;
assert(ii + lit == c->bp);
assert(swd->b_char == *(c->bp));
@@ -638,20 +699,24 @@ lzo1x_999_compress_internal ( const lzo_bytep in , lzo_uint in_len,
lit++;
swd->max_chain = max_chain;
r = find_match(c, swd, 1, 0);
assert(r == 0); LZO_UNUSED(r);
assert(r == 0);
LZO_UNUSED(r);
continue;
}
/* a match */
#if defined(SWD_BEST_OFF)
if (swd->use_best_off)
better_match(swd, &m_len, &m_off);
#endif
assert_match(swd, m_len, m_off);
/* shall we try a lazy match ? */
ahead = 0;
if (try_lazy == 0 || m_len >= max_lazy)
{
/* no */
@@ -679,34 +744,48 @@ lzo1x_999_compress_internal ( const lzo_bytep in , lzo_uint in_len,
swd->max_chain = max_chain >> 2;
else
swd->max_chain = max_chain;
r = find_match(c, swd, 1, 0);
ahead++;
assert(r == 0); LZO_UNUSED(r);
assert(r == 0);
LZO_UNUSED(r);
assert(c->look > 0);
assert(ii + lit + ahead == c->bp);
#if defined(LZO1Z)
if (m_off == c->last_m_off && c->m_off != c->last_m_off)
if (m_len >= M2_MIN_LEN && m_len <= M2_MAX_LEN)
c->m_len = 0;
#endif
if (c->m_len < m_len)
continue;
#if 1
if (c->m_len == m_len && c->m_off >= m_off)
continue;
#endif
#if defined(SWD_BEST_OFF)
if (swd->use_best_off)
better_match(swd, &c->m_len, &c->m_off);
#endif
l2 = len_of_coded_match(c->m_len, c->m_off, lit + ahead);
if (l2 == 0)
continue;
#if 0
if (c->m_len == m_len && l2 >= l1)
continue;
#endif
@@ -718,6 +797,7 @@ lzo1x_999_compress_internal ( const lzo_bytep in , lzo_uint in_len,
#endif
lazy_match_min_gain = min_gain(ahead, lit, lit + ahead, l1, l2, l3);
if (c->m_len >= m_len + lazy_match_min_gain)
{
c->lazy++;
@@ -736,6 +816,7 @@ lzo1x_999_compress_internal ( const lzo_bytep in , lzo_uint in_len,
lit += ahead;
assert(ii + lit == c->bp);
}
goto lazy_match_done;
}
}
@@ -751,9 +832,11 @@ lzo1x_999_compress_internal ( const lzo_bytep in , lzo_uint in_len,
op = code_match(c, op, m_len, m_off);
swd->max_chain = max_chain;
r = find_match(c, swd, m_len, 1 + ahead);
assert(r == 0); LZO_UNUSED(r);
assert(r == 0);
LZO_UNUSED(r);
lazy_match_done: ;
lazy_match_done:
;
}
@@ -807,7 +890,8 @@ lzo1x_999_compress_level ( const lzo_bytep in , lzo_uint in_len,
lzo_uint nice_length;
lzo_uint max_chain;
lzo_uint32_t flags;
} c[9] = {
} c[9] =
{
/* faster compression */
{ 0, 0, 0, 8, 4, 0 },
{ 0, 0, 0, 16, 8, 0 },

View File

@@ -54,6 +54,7 @@ do_compress ( const lzo_bytep in , lzo_uint in_len,
ii = ip;
ip += ti < 4 ? 4 - ti : 0;
for (;;)
{
const lzo_bytep m_pos;
@@ -62,26 +63,36 @@ do_compress ( const lzo_bytep in , lzo_uint in_len,
lzo_uint m_len;
lzo_uint dindex;
next:
if __lzo_unlikely(ip >= ip_end)
break;
DINDEX1(dindex, ip);
GINDEX(m_pos, m_off, dict, dindex, in);
if (LZO_CHECK_MPOS_NON_DET(m_pos, m_off, in, ip, M4_MAX_OFFSET))
goto literal;
#if 1
if (m_off <= M2_MAX_OFFSET || m_pos[3] == ip[3])
goto try_match;
DINDEX2(dindex, ip);
#endif
GINDEX(m_pos, m_off, dict, dindex, in);
if (LZO_CHECK_MPOS_NON_DET(m_pos, m_off, in, ip, M4_MAX_OFFSET))
goto literal;
if (m_off <= M2_MAX_OFFSET || m_pos[3] == ip[3])
goto try_match;
goto literal;
try_match:
#if (LZO_OPT_UNALIGNED32)
if (UA_GET_NE32(m_pos) != UA_GET_NE32(ip))
#else
if (m_pos[0] != ip[0] || m_pos[1] != ip[1] || m_pos[2] != ip[2] || m_pos[3] != ip[3])
@@ -93,6 +104,7 @@ literal:
ip += 1 + ((ip - ii) >> 5);
continue;
}
/*match:*/
UPDATE_I(dict, 0, dindex, ip, in);
#else
@@ -104,12 +116,15 @@ literal:
literal:
ip += 1 + ((ip - ii) >> 5);
next:
if __lzo_unlikely(ip >= ip_end)
break;
dv = UA_GET_LE32(ip);
dindex = DINDEX(dv, ip);
GINDEX(m_off, m_pos, in + dict, dindex, in);
UPDATE_I(dict, 0, dindex, ip, in);
if __lzo_unlikely(dv != UA_GET_LE32(m_pos))
goto literal;
}
@@ -117,9 +132,11 @@ next:
/* a match */
ii -= ti; ti = 0;
ii -= ti;
ti = 0;
{
lzo_uint t = pd(ip, ii);
if (t != 0)
{
if (t <= 3)
@@ -132,6 +149,7 @@ next:
{ do* op++ = *ii++; while (--t > 0); }
#endif
}
#if (LZO_OPT_UNALIGNED32) || (LZO_OPT_UNALIGNED64)
else if (t <= 16)
{
@@ -140,6 +158,7 @@ next:
UA_COPY8(op + 8, ii + 8);
op += t;
}
#endif
else
{
@@ -149,21 +168,31 @@ next:
{
lzo_uint tt = t - 18;
*op++ = 0;
while __lzo_unlikely(tt > 255)
{
tt -= 255;
UA_SET1(op, 0);
op++;
}
assert(tt > 0);
*op++ = LZO_BYTE(tt);
}
#if (LZO_OPT_UNALIGNED32) || (LZO_OPT_UNALIGNED64)
do {
do
{
UA_COPY8(op, ii);
UA_COPY8(op + 8, ii + 8);
op += 16; ii += 16; t -= 16;
} while (t >= 16); if (t > 0)
op += 16;
ii += 16;
t -= 16;
}
while (t >= 16);
if (t > 0)
#endif
{ do* op++ = *ii++; while (--t > 0); }
}
@@ -174,102 +203,161 @@ next:
#if (LZO_OPT_UNALIGNED64)
lzo_uint64_t v;
v = UA_GET_NE64(ip + m_len) ^ UA_GET_NE64(m_pos + m_len);
if __lzo_unlikely(v == 0) {
do {
if __lzo_unlikely(v == 0)
{
do
{
m_len += 8;
v = UA_GET_NE64(ip + m_len) ^ UA_GET_NE64(m_pos + m_len);
if __lzo_unlikely(ip + m_len >= ip_end)
goto m_len_done;
} while (v == 0);
}
while (v == 0);
}
#if (LZO_ABI_BIG_ENDIAN) && defined(lzo_bitops_ctlz64)
m_len += lzo_bitops_ctlz64(v) / CHAR_BIT;
#elif (LZO_ABI_BIG_ENDIAN)
if ((v >> (64 - CHAR_BIT)) == 0) do {
if ((v >> (64 - CHAR_BIT)) == 0) do
{
v <<= CHAR_BIT;
m_len += 1;
} while ((v >> (64 - CHAR_BIT)) == 0);
}
while ((v >> (64 - CHAR_BIT)) == 0);
#elif (LZO_ABI_LITTLE_ENDIAN) && defined(lzo_bitops_cttz64)
m_len += lzo_bitops_cttz64(v) / CHAR_BIT;
#elif (LZO_ABI_LITTLE_ENDIAN)
if ((v & UCHAR_MAX) == 0) do {
if ((v & UCHAR_MAX) == 0) do
{
v >>= CHAR_BIT;
m_len += 1;
} while ((v & UCHAR_MAX) == 0);
}
while ((v & UCHAR_MAX) == 0);
#else
if (ip[m_len] == m_pos[m_len]) do {
if (ip[m_len] == m_pos[m_len]) do
{
m_len += 1;
} while (ip[m_len] == m_pos[m_len]);
}
while (ip[m_len] == m_pos[m_len]);
#endif
#elif (LZO_OPT_UNALIGNED32)
lzo_uint32_t v;
v = UA_GET_NE32(ip + m_len) ^ UA_GET_NE32(m_pos + m_len);
if __lzo_unlikely(v == 0) {
do {
if __lzo_unlikely(v == 0)
{
do
{
m_len += 4;
v = UA_GET_NE32(ip + m_len) ^ UA_GET_NE32(m_pos + m_len);
if (v != 0)
break;
m_len += 4;
v = UA_GET_NE32(ip + m_len) ^ UA_GET_NE32(m_pos + m_len);
if __lzo_unlikely(ip + m_len >= ip_end)
goto m_len_done;
} while (v == 0);
}
while (v == 0);
}
#if (LZO_ABI_BIG_ENDIAN) && defined(lzo_bitops_ctlz32)
m_len += lzo_bitops_ctlz32(v) / CHAR_BIT;
#elif (LZO_ABI_BIG_ENDIAN)
if ((v >> (32 - CHAR_BIT)) == 0) do {
if ((v >> (32 - CHAR_BIT)) == 0) do
{
v <<= CHAR_BIT;
m_len += 1;
} while ((v >> (32 - CHAR_BIT)) == 0);
}
while ((v >> (32 - CHAR_BIT)) == 0);
#elif (LZO_ABI_LITTLE_ENDIAN) && defined(lzo_bitops_cttz32)
m_len += lzo_bitops_cttz32(v) / CHAR_BIT;
#elif (LZO_ABI_LITTLE_ENDIAN)
if ((v & UCHAR_MAX) == 0) do {
if ((v & UCHAR_MAX) == 0) do
{
v >>= CHAR_BIT;
m_len += 1;
} while ((v & UCHAR_MAX) == 0);
}
while ((v & UCHAR_MAX) == 0);
#else
if (ip[m_len] == m_pos[m_len]) do {
if (ip[m_len] == m_pos[m_len]) do
{
m_len += 1;
} while (ip[m_len] == m_pos[m_len]);
}
while (ip[m_len] == m_pos[m_len]);
#endif
#else
if __lzo_unlikely(ip[m_len] == m_pos[m_len]) {
do {
if __lzo_unlikely(ip[m_len] == m_pos[m_len])
{
do
{
m_len += 1;
if (ip[m_len] != m_pos[m_len])
break;
m_len += 1;
if (ip[m_len] != m_pos[m_len])
break;
m_len += 1;
if (ip[m_len] != m_pos[m_len])
break;
m_len += 1;
if (ip[m_len] != m_pos[m_len])
break;
m_len += 1;
if (ip[m_len] != m_pos[m_len])
break;
m_len += 1;
if (ip[m_len] != m_pos[m_len])
break;
m_len += 1;
if (ip[m_len] != m_pos[m_len])
break;
m_len += 1;
if __lzo_unlikely(ip + m_len >= ip_end)
goto m_len_done;
} while (ip[m_len] == m_pos[m_len]);
}
while (ip[m_len] == m_pos[m_len]);
}
#endif
}
m_len_done:
m_off = pd(ip, m_pos);
ip += m_len;
ii = ip;
if (m_len <= M2_MAX_LEN && m_off <= M2_MAX_OFFSET)
{
m_off -= 1;
@@ -284,43 +372,52 @@ m_len_done:
else if (m_off <= M3_MAX_OFFSET)
{
m_off -= 1;
if (m_len <= M3_MAX_LEN)
*op++ = LZO_BYTE(M3_MARKER | (m_len - 2));
else
{
m_len -= M3_MAX_LEN;
*op++ = M3_MARKER | 0;
while __lzo_unlikely(m_len > 255)
{
m_len -= 255;
UA_SET1(op, 0);
op++;
}
*op++ = LZO_BYTE(m_len);
}
*op++ = LZO_BYTE(m_off << 2);
*op++ = LZO_BYTE(m_off >> 6);
}
else
{
m_off -= 0x4000;
if (m_len <= M4_MAX_LEN)
*op++ = LZO_BYTE(M4_MARKER | ((m_off >> 11) & 8) | (m_len - 2));
else
{
m_len -= M4_MAX_LEN;
*op++ = LZO_BYTE(M4_MARKER | ((m_off >> 11) & 8));
while __lzo_unlikely(m_len > 255)
{
m_len -= 255;
UA_SET1(op, 0);
op++;
}
*op++ = LZO_BYTE(m_len);
}
*op++ = LZO_BYTE(m_off << 2);
*op++ = LZO_BYTE(m_off >> 6);
}
goto next;
}
@@ -351,8 +448,10 @@ DO_COMPRESS ( const lzo_bytep in , lzo_uint in_len,
ll = LZO_MIN(ll, 49152);
#endif
ll_end = (lzo_uintptr_t)ip + ll;
if ((ll_end + ((t + ll) >> 5)) <= ll_end || (const lzo_bytep)(ll_end + ((t + ll) >> 5)) <= ip + ll)
break;
#if (LZO_DETERMINISTIC)
lzo_memset(wrkmem, 0, ((lzo_uint)1 << D_BITS) * sizeof(lzo_dict_t));
#endif
@@ -361,6 +460,7 @@ DO_COMPRESS ( const lzo_bytep in , lzo_uint in_len,
op += *out_len;
l -= ll;
}
t += l;
if (t > 0)
@@ -378,15 +478,18 @@ DO_COMPRESS ( const lzo_bytep in , lzo_uint in_len,
lzo_uint tt = t - 18;
*op++ = 0;
while (tt > 255)
{
tt -= 255;
UA_SET1(op, 0);
op++;
}
assert(tt > 0);
*op++ = LZO_BYTE(tt);
}
UA_COPYN(op, ii, t);
op += t;
}

View File

@@ -61,6 +61,7 @@ DO_DECOMPRESS ( const lzo_bytep in , lzo_uint in_len,
LZO_UNUSED(wrkmem);
#if defined(COPY_DICT)
if (dict)
{
if (dict_len > M4_MAX_OFFSET)
@@ -68,6 +69,7 @@ DO_DECOMPRESS ( const lzo_bytep in , lzo_uint in_len,
dict += dict_len - M4_MAX_OFFSET;
dict_len = M4_MAX_OFFSET;
}
dict_end = dict + dict_len;
}
else
@@ -75,6 +77,7 @@ DO_DECOMPRESS ( const lzo_bytep in , lzo_uint in_len,
dict_len = 0;
dict_end = NULL;
}
#endif /* COPY_DICT */
*out_len = 0;
@@ -83,13 +86,22 @@ DO_DECOMPRESS ( const lzo_bytep in , lzo_uint in_len,
ip = in;
NEED_IP(1);
if (*ip > 17)
{
t = *ip++ - 17;
if (t < 4)
goto match_next;
assert(t > 0); NEED_OP(t); NEED_IP(t+3);
do *op++ = *ip++; while (--t > 0);
assert(t > 0);
NEED_OP(t);
NEED_IP(t + 3);
do* op++ = *ip++;
while (--t > 0);
goto first_literal_run;
}
@@ -97,8 +109,10 @@ DO_DECOMPRESS ( const lzo_bytep in , lzo_uint in_len,
{
NEED_IP(3);
t = *ip++;
if (t >= 16)
goto match;
/* a literal run */
if (t == 0)
{
@@ -109,47 +123,74 @@ DO_DECOMPRESS ( const lzo_bytep in , lzo_uint in_len,
TEST_IV(t);
NEED_IP(1);
}
t += 15 + *ip++;
}
/* copy literals */
assert(t > 0); NEED_OP(t+3); NEED_IP(t+6);
assert(t > 0);
NEED_OP(t + 3);
NEED_IP(t + 6);
#if (LZO_OPT_UNALIGNED64) && (LZO_OPT_UNALIGNED32)
t += 3;
if (t >= 8) do
{
UA_COPY8(op, ip);
op += 8; ip += 8; t -= 8;
} while (t >= 8);
op += 8;
ip += 8;
t -= 8;
}
while (t >= 8);
if (t >= 4)
{
UA_COPY4(op, ip);
op += 4; ip += 4; t -= 4;
op += 4;
ip += 4;
t -= 4;
}
if (t > 0)
{
*op++ = *ip++;
if (t > 1) { *op++ = *ip++; if (t > 2) { *op++ = *ip++; } }
}
#elif (LZO_OPT_UNALIGNED32) || (LZO_ALIGNED_OK_4)
#if !(LZO_OPT_UNALIGNED32)
if (PTR_ALIGNED2_4(op, ip))
{
#endif
UA_COPY4(op, ip);
op += 4; ip += 4;
op += 4;
ip += 4;
if (--t > 0)
{
if (t >= 4)
{
do {
do
{
UA_COPY4(op, ip);
op += 4; ip += 4; t -= 4;
} while (t >= 4);
if (t > 0) do *op++ = *ip++; while (--t > 0);
op += 4;
ip += 4;
t -= 4;
}
while (t >= 4);
if (t > 0) do* op++ = *ip++;
while (--t > 0);
}
else
do *op++ = *ip++; while (--t > 0);
do* op++ = *ip++;
while (--t > 0);
}
#if !(LZO_OPT_UNALIGNED32)
}
else
@@ -157,9 +198,15 @@ DO_DECOMPRESS ( const lzo_bytep in , lzo_uint in_len,
#endif
#if !(LZO_OPT_UNALIGNED32)
{
*op++ = *ip++; *op++ = *ip++; *op++ = *ip++;
do *op++ = *ip++; while (--t > 0);
*op++ = *ip++;
*op++ = *ip++;
*op++ = *ip++;
do* op++ = *ip++;
while (--t > 0);
}
#endif
@@ -167,8 +214,10 @@ first_literal_run:
t = *ip++;
if (t >= 16)
goto match;
#if defined(COPY_DICT)
#if defined(LZO1Z)
m_off = (1 + M2_MAX_OFFSET) + (t << 6) + (*ip++ >> 2);
@@ -177,7 +226,8 @@ first_literal_run:
m_off = (1 + M2_MAX_OFFSET) + (t >> 2) + (*ip++ << 2);
#endif
NEED_OP(3);
t = 3; COPY_DICT(t,m_off)
t = 3;
COPY_DICT(t, m_off)
#else /* !COPY_DICT */
#if defined(LZO1Z)
t = (1 + M2_MAX_OFFSET) + (t << 6) + (*ip++ >> 2);
@@ -188,15 +238,20 @@ first_literal_run:
m_pos -= t >> 2;
m_pos -= *ip++ << 2;
#endif
TEST_LB(m_pos); NEED_OP(3);
*op++ = *m_pos++; *op++ = *m_pos++; *op++ = *m_pos;
TEST_LB(m_pos);
NEED_OP(3);
*op++ = *m_pos++;
*op++ = *m_pos++;
*op++ = *m_pos;
#endif /* COPY_DICT */
goto match_done;
/* handle matches */
for (;;) {
for (;;)
{
match:
if (t >= 64) /* a M2 match */
{
#if defined(COPY_DICT)
@@ -208,6 +263,7 @@ match:
t = (t >> 4) - 3;
#elif defined(LZO1Z)
m_off = t & 0x1f;
if (m_off >= 0x1c)
m_off = last_m_off;
else
@@ -215,6 +271,7 @@ match:
m_off = 1 + (m_off << 6) + (*ip++ >> 2);
last_m_off = m_off;
}
t = (t >> 5) - 1;
#endif
#else /* !COPY_DICT */
@@ -232,6 +289,7 @@ match:
{
lzo_uint off = t & 0x1f;
m_pos = op;
if (off >= 0x1c)
{
assert(last_m_off > 0);
@@ -246,13 +304,16 @@ match:
}
t = (t >> 5) - 1;
#endif
TEST_LB(m_pos); assert(t > 0); NEED_OP(t+3-1);
TEST_LB(m_pos);
assert(t > 0);
NEED_OP(t + 3 - 1);
goto copy_match;
#endif /* COPY_DICT */
}
else if (t >= 32) /* a M3 match */
{
t &= 31;
if (t == 0)
{
while (*ip == 0)
@@ -262,9 +323,11 @@ match:
TEST_OV(t);
NEED_IP(1);
}
t += 31 + *ip++;
NEED_IP(2);
}
#if defined(COPY_DICT)
#if defined(LZO1Z)
m_off = 1 + (ip[0] << 6) + (ip[1] >> 2);
@@ -298,6 +361,7 @@ match:
m_pos -= (t & 8) << 11;
#endif /* COPY_DICT */
t &= 7;
if (t == 0)
{
while (*ip == 0)
@@ -307,9 +371,11 @@ match:
TEST_OV(t);
NEED_IP(1);
}
t += 7 + *ip++;
NEED_IP(2);
}
#if defined(COPY_DICT)
#if defined(LZO1Z)
m_off += (ip[0] << 6) + (ip[1] >> 2);
@@ -317,8 +383,10 @@ match:
m_off += (ip[0] >> 2) + (ip[1] << 6);
#endif
ip += 2;
if (m_off == 0)
goto eof_found;
m_off += 0x4000;
#if defined(LZO1Z)
last_m_off = m_off;
@@ -332,8 +400,10 @@ match:
m_pos -= (ip[0] >> 2) + (ip[1] << 6);
#endif
ip += 2;
if (m_pos == op)
goto eof_found;
m_pos -= 0x4000;
#if defined(LZO1Z)
last_m_off = pd((const lzo_bytep)op, m_pos);
@@ -350,7 +420,8 @@ match:
m_off = 1 + (t >> 2) + (*ip++ << 2);
#endif
NEED_OP(2);
t = 2; COPY_DICT(t,m_off)
t = 2;
COPY_DICT(t, m_off)
#else /* !COPY_DICT */
#if defined(LZO1Z)
t = 1 + (t << 6) + (*ip++ >> 2);
@@ -361,8 +432,10 @@ match:
m_pos -= t >> 2;
m_pos -= *ip++ << 2;
#endif
TEST_LB(m_pos); NEED_OP(2);
*op++ = *m_pos++; *op++ = *m_pos;
TEST_LB(m_pos);
NEED_OP(2);
*op++ = *m_pos++;
*op++ = *m_pos;
#endif /* COPY_DICT */
goto match_done;
}
@@ -371,28 +444,41 @@ match:
#if defined(COPY_DICT)
NEED_OP(t + 3 - 1);
t += 3-1; COPY_DICT(t,m_off)
t += 3 - 1;
COPY_DICT(t, m_off)
#else /* !COPY_DICT */
TEST_LB(m_pos); assert(t > 0); NEED_OP(t+3-1);
TEST_LB(m_pos);
assert(t > 0);
NEED_OP(t + 3 - 1);
#if (LZO_OPT_UNALIGNED64) && (LZO_OPT_UNALIGNED32)
if (op - m_pos >= 8)
{
t += (3 - 1);
if (t >= 8) do
{
UA_COPY8(op, m_pos);
op += 8; m_pos += 8; t -= 8;
} while (t >= 8);
op += 8;
m_pos += 8;
t -= 8;
}
while (t >= 8);
if (t >= 4)
{
UA_COPY4(op, m_pos);
op += 4; m_pos += 4; t -= 4;
op += 4;
m_pos += 4;
t -= 4;
}
if (t > 0)
{
*op++ = m_pos[0];
if (t > 1) { *op++ = m_pos[1]; if (t > 2) { *op++ = m_pos[2]; } }
}
}
@@ -403,23 +489,38 @@ match:
{
assert((op - m_pos) >= 4); /* both pointers are aligned */
#else
if (t >= 2 * 4 - (3 - 1) && (op - m_pos) >= 4)
{
#endif
UA_COPY4(op, m_pos);
op += 4; m_pos += 4; t -= 4 - (3 - 1);
do {
op += 4;
m_pos += 4;
t -= 4 - (3 - 1);
do
{
UA_COPY4(op, m_pos);
op += 4; m_pos += 4; t -= 4;
} while (t >= 4);
if (t > 0) do *op++ = *m_pos++; while (--t > 0);
op += 4;
m_pos += 4;
t -= 4;
}
while (t >= 4);
if (t > 0) do* op++ = *m_pos++;
while (--t > 0);
}
else
#endif
{
copy_match:
*op++ = *m_pos++; *op++ = *m_pos++;
do *op++ = *m_pos++; while (--t > 0);
*op++ = *m_pos++;
*op++ = *m_pos++;
do* op++ = *m_pos++;
while (--t > 0);
}
#endif /* COPY_DICT */
@@ -430,17 +531,27 @@ match_done:
#else
t = ip[-2] & 3;
#endif
if (t == 0)
break;
/* copy literals */
match_next:
assert(t > 0); assert(t < 4); NEED_OP(t); NEED_IP(t+3);
assert(t > 0);
assert(t < 4);
NEED_OP(t);
NEED_IP(t + 3);
#if 0
do *op++ = *ip++; while (--t > 0);
do* op++ = *ip++;
while (--t > 0);
#else
*op++ = *ip++;
if (t > 1) { *op++ = *ip++; if (t > 2) { *op++ = *ip++; } }
#endif
t = *ip++;
}

View File

@@ -41,6 +41,7 @@ static void copy2(lzo_bytep ip, const lzo_bytep m_pos, lzo_uint off)
{
assert(off > 0);
ip[0] = m_pos[0];
if (off == 1)
ip[1] = m_pos[0];
else
@@ -52,6 +53,7 @@ static void copy3(lzo_bytep ip, const lzo_bytep m_pos, lzo_uint off)
{
assert(off > 0);
ip[0] = m_pos[0];
if (off == 1)
{
ip[2] = ip[1] = m_pos[0];
@@ -98,41 +100,57 @@ DO_OPTIMIZE ( lzo_bytep in , lzo_uint in_len,
ip = in;
assert(in_len >= 3);
if (*ip > 17)
{
t = *ip++ - 17;
if (t < 4)
goto match_next;
goto first_literal_run;
}
assert(*ip < 16 || (*ip == 17 && in_len == 3));
while (TEST_IP_AND_TEST_OP)
{
t = *ip++;
if (t >= 16)
goto match;
/* a literal run */
litp = ip - 1;
if (t == 0)
{
t = 15;
while (*ip == 0)
t += 255, ip++;
t += *ip++;
}
lit = t + 3;
/* copy literals */
copy_literal_run:
*op++ = *ip++; *op++ = *ip++; *op++ = *ip++;
*op++ = *ip++;
*op++ = *ip++;
*op++ = *ip++;
first_literal_run:
do *op++ = *ip++; while (--t > 0);
do* op++ = *ip++;
while (--t > 0);
t = *ip++;
if (t >= 16)
goto match;
#if defined(LZO1X)
m_pos = op - 1 - 0x800;
#elif defined(LZO1Y)
@@ -140,13 +158,16 @@ first_literal_run:
#endif
m_pos -= t >> 2;
m_pos -= *ip++ << 2;
*op++ = *m_pos++; *op++ = *m_pos++; *op++ = *m_pos++;
*op++ = *m_pos++;
*op++ = *m_pos++;
*op++ = *m_pos++;
lit = 0;
goto match_done;
/* handle matches */
do {
do
{
if (t < 16) /* a M1 match */
{
m_pos = op - 1;
@@ -161,6 +182,7 @@ first_literal_run:
assert(litp == ip - 2 - lit - 2);
assert((lzo_uint)(*litp & 3) == lit);
nl = ip[-2] & 3;
/* test if a match follows */
if (nl == 0 && lit == 1 && ip[0] >= 16)
{
@@ -183,22 +205,29 @@ first_literal_run:
copy2(ip - 3 + 1, m_pos, pd(op, m_pos));
/* move literals 1 byte ahead */
litp += 2;
if (lit > 0)
lzo_memmove(litp + 1, litp, lit);
/* insert new length of long literal run */
lit += 2 + t + 3; assert(lit <= 18);
lit += 2 + t + 3;
assert(lit <= 18);
*litp = LZO_BYTE(lit - 3);
o_m1_b++;
*op++ = *m_pos++; *op++ = *m_pos++;
*op++ = *m_pos++;
*op++ = *m_pos++;
goto copy_literal_run;
}
copy_m1:
*op++ = *m_pos++; *op++ = *m_pos++;
*op++ = *m_pos++;
*op++ = *m_pos++;
}
else
{
match:
if (t >= 64) /* a M2 match */
{
m_pos = op - 1;
@@ -211,10 +240,12 @@ match:
m_pos -= *ip++ << 2;
t = (t >> 4) - 3;
#endif
if (litp == NULL)
goto copy_m;
nl = ip[-2] & 3;
/* test if in beetween two long literal runs */
if (t == 1 && lit > 3 && nl == 0 &&
ip[0] < 16 && ip[0] != 0 && (lit + 3 + ip[0] < 16))
@@ -224,10 +255,13 @@ match:
/* copy over the 3 literals that replace the match */
copy3(ip - 1 - 2, m_pos, pd(op, m_pos));
/* set new length of previous literal run */
lit += 3 + t + 3; assert(lit <= 18);
lit += 3 + t + 3;
assert(lit <= 18);
*litp = LZO_BYTE(lit - 3);
o_m2++;
*op++ = *m_pos++; *op++ = *m_pos++; *op++ = *m_pos++;
*op++ = *m_pos++;
*op++ = *m_pos++;
*op++ = *m_pos++;
goto copy_literal_run;
}
}
@@ -236,13 +270,17 @@ match:
if (t >= 32) /* a M3 match */
{
t &= 31;
if (t == 0)
{
t = 31;
while (*ip == 0)
t += 255, ip++;
t += *ip++;
}
m_pos = op - 1;
m_pos -= *ip++ >> 2;
m_pos -= *ip++ << 6;
@@ -252,23 +290,31 @@ match:
m_pos = op;
m_pos -= (t & 8) << 11;
t &= 7;
if (t == 0)
{
t = 7;
while (*ip == 0)
t += 255, ip++;
t += *ip++;
}
m_pos -= *ip++ >> 2;
m_pos -= *ip++ << 6;
if (m_pos == op)
goto eof_found;
m_pos -= 0x4000;
}
if (litp == NULL)
goto copy_m;
nl = ip[-2] & 3;
/* test if in beetween two matches */
if (t == 1 && lit == 0 && nl == 0 && ip[0] >= 16)
{
@@ -295,23 +341,34 @@ match:
copy3(ip - 4 + 1, m_pos, pd(op, m_pos));
/* move literals 1 byte ahead */
litp += 2;
if (lit > 0)
lzo_memmove(litp + 1, litp, lit);
/* insert new length of long literal run */
lit += 3 + t + 3; assert(lit <= 18);
lit += 3 + t + 3;
assert(lit <= 18);
*litp = LZO_BYTE(lit - 3);
o_m3_b++;
*op++ = *m_pos++; *op++ = *m_pos++; *op++ = *m_pos++;
*op++ = *m_pos++;
*op++ = *m_pos++;
*op++ = *m_pos++;
goto copy_literal_run;
}
}
copy_m:
*op++ = *m_pos++; *op++ = *m_pos++;
do *op++ = *m_pos++; while (--t > 0);
*op++ = *m_pos++;
*op++ = *m_pos++;
do* op++ = *m_pos++;
while (--t > 0);
}
match_done:
if (next_lit == NO_LIT)
{
t = ip[-2] & 3;
@@ -320,15 +377,23 @@ match_done:
}
else
t = next_lit;
assert(t <= 3);
next_lit = NO_LIT;
if (t == 0)
break;
/* copy literals */
match_next:
do *op++ = *ip++; while (--t > 0);
do* op++ = *ip++;
while (--t > 0);
t = *ip++;
} while (TEST_IP_AND_TEST_OP);
}
while (TEST_IP_AND_TEST_OP);
}
/* no EOF code was found */
@@ -341,8 +406,11 @@ eof_found:
printf("optimize: %5lu %5lu %5lu %5lu %5lu\n",
o_m1_a, o_m1_b, o_m2, o_m3_a, o_m3_b);
#endif
LZO_UNUSED(o_m1_a); LZO_UNUSED(o_m1_b); LZO_UNUSED(o_m2);
LZO_UNUSED(o_m3_a); LZO_UNUSED(o_m3_b);
LZO_UNUSED(o_m1_a);
LZO_UNUSED(o_m1_b);
LZO_UNUSED(o_m2);
LZO_UNUSED(o_m3_a);
LZO_UNUSED(o_m3_b);
*out_len = pd(op, out);
return (ip == ip_end ? LZO_E_OK :
(ip < ip_end ? LZO_E_INPUT_NOT_CONSUMED : LZO_E_INPUT_OVERRUN));

View File

@@ -108,14 +108,18 @@ lzo2a_999_compress_callback ( const lzo_bytep in , lzo_uint in_len,
op = out;
r = init_match(c, swd, NULL, 0, 0);
if (r != 0)
return r;
if (max_chain > 0)
swd->max_chain = max_chain;
r = find_match(c, swd, 0, 0);
if (r != 0)
return r;
while (c->look > 0)
{
lzo_uint lazy_match_min_gain = 0;
@@ -129,6 +133,7 @@ lzo2a_999_compress_callback ( const lzo_bytep in , lzo_uint in_len,
m_off = c->m_off;
#if (SWD_N >= 8192)
if (m_off >= 8192)
{
if (m_len < M3_MIN_LEN)
@@ -165,10 +170,12 @@ lzo2a_999_compress_callback ( const lzo_bytep in , lzo_uint in_len,
unsigned char lit = LZO_BYTE(swd->b_char);
r = find_match(c, swd, 1, 0);
assert(r == 0); LZO_UNUSED(r);
assert(r == 0);
LZO_UNUSED(r);
assert(c->look > 0);
#if (SWD_N >= 8192)
if (m_off < 8192 && c->m_off >= 8192)
lazy_match_min_gain += extra1;
else
@@ -179,6 +186,7 @@ lzo2a_999_compress_callback ( const lzo_bytep in , lzo_uint in_len,
c->m_len <= M1_MAX_LEN && c->m_off <= 256))
lazy_match_min_gain += extra2;
}
if (c->m_len >= M1_MIN_LEN &&
c->m_len <= M1_MAX_LEN && c->m_off <= 256)
{
@@ -206,6 +214,7 @@ lzo2a_999_compress_callback ( const lzo_bytep in , lzo_uint in_len,
}
else
ahead = 1;
assert(m_len > 0);
}
@@ -217,7 +226,8 @@ lzo2a_999_compress_callback ( const lzo_bytep in , lzo_uint in_len,
putbyte(swd->b_char);
c->lit_bytes++;
r = find_match(c, swd, 1, 0);
assert(r == 0); LZO_UNUSED(r);
assert(r == 0);
LZO_UNUSED(r);
}
else
{
@@ -234,6 +244,7 @@ lzo2a_999_compress_callback ( const lzo_bytep in , lzo_uint in_len,
putbyte(m_off - 1);
c->m1++;
}
#if (SWD_N >= 8192)
else if (m_off >= 8192)
{
@@ -245,14 +256,17 @@ lzo2a_999_compress_callback ( const lzo_bytep in , lzo_uint in_len,
putbyte(m_off >> 5);
putbit(1);
len -= M3_MIN_LEN - 1;
while (len > 255)
{
len -= 255;
putbyte(0);
}
putbyte(len);
c->m4++;
}
#endif
else
{
@@ -260,6 +274,7 @@ lzo2a_999_compress_callback ( const lzo_bytep in , lzo_uint in_len,
putbit(1);
putbit(1);
if (m_len <= 9)
{
putbyte(((m_len - 2) << 5) | (m_off & 31));
@@ -275,17 +290,21 @@ lzo2a_999_compress_callback ( const lzo_bytep in , lzo_uint in_len,
putbit(0);
#endif
len -= 10 - 1;
while (len > 255)
{
len -= 255;
putbyte(0);
}
putbyte(len);
c->m3++;
}
}
r = find_match(c, swd, m_len, 1 + ahead);
assert(r == 0); LZO_UNUSED(r);
assert(r == 0);
LZO_UNUSED(r);
}
c->codesize = pd(op, out);
@@ -301,6 +320,7 @@ lzo2a_999_compress_callback ( const lzo_bytep in , lzo_uint in_len,
/* flush remaining bits */
assert(k < CHAR_BIT);
if (k > 0)
{
assert(b == MASKBITS(k));

View File

@@ -62,17 +62,21 @@ DO_DECOMPRESS ( const lzo_bytep in , lzo_uint in_len,
while (TEST_IP_AND_TEST_OP)
{
NEEDBITS(1);
if (MASKBITS(1) == 0)
{
DUMPBITS(1);
/* a literal */
NEED_IP(1); NEED_OP(1);
NEED_IP(1);
NEED_OP(1);
*op++ = *ip++;
continue;
}
DUMPBITS(1);
NEEDBITS(1);
if (MASKBITS(1) == 0)
{
DUMPBITS(1);
@@ -80,13 +84,16 @@ DO_DECOMPRESS ( const lzo_bytep in , lzo_uint in_len,
NEEDBITS(2);
t = M1_MIN_LEN + (lzo_uint) MASKBITS(2);
DUMPBITS(2);
NEED_IP(1); NEED_OP(t);
NEED_IP(1);
NEED_OP(t);
m_pos = op - 1 - *ip++;
assert(m_pos >= out); assert(m_pos < op);
assert(m_pos >= out);
assert(m_pos < op);
TEST_LB(m_pos);
MEMCPY_DS(op, m_pos, t);
continue;
}
DUMPBITS(1);
NEED_IP(2);
@@ -94,12 +101,14 @@ DO_DECOMPRESS ( const lzo_bytep in , lzo_uint in_len,
m_pos = op;
m_pos -= (t & 31) | (((lzo_uint) * ip++) << 5);
t >>= 5;
if (t == 0)
{
#if (SWD_N >= 8192)
NEEDBITS(1);
t = MASKBITS(1);
DUMPBITS(1);
if (t == 0)
t = 10 - 1;
else
@@ -108,10 +117,12 @@ DO_DECOMPRESS ( const lzo_bytep in , lzo_uint in_len,
m_pos -= 8192; /* t << 13 */
t = M3_MIN_LEN - 1;
}
#else
t = 10 - 1;
#endif
NEED_IP(1);
while (*ip == 0)
{
t += 255;
@@ -119,17 +130,22 @@ DO_DECOMPRESS ( const lzo_bytep in , lzo_uint in_len,
TEST_OV(t);
NEED_IP(1);
}
t += *ip++;
}
else
{
#if defined(LZO_EOF_CODE)
if (m_pos == op)
goto eof_found;
#endif
t += 2;
}
assert(m_pos >= out); assert(m_pos < op);
assert(m_pos >= out);
assert(m_pos < op);
TEST_LB(m_pos);
NEED_OP(t);
MEMCPY_DS(op, m_pos, t);

View File

@@ -35,7 +35,8 @@
// see http://www.zlib.org/
************************************************************************/
static const lzo_uint32_t lzo_crc32_table[256] = {
static const lzo_uint32_t lzo_crc32_table[256] =
{
0x00000000L, 0x77073096L, 0xee0e612cL, 0x990951baL, 0x076dc419L,
0x706af48fL, 0xe963a535L, 0x9e6495a3L, 0x0edb8832L, 0x79dcb8a4L,
0xe0d5e91eL, 0x97d2d988L, 0x09b64c2bL, 0x7eb17cbdL, 0xe7b82d07L,
@@ -126,18 +127,22 @@ lzo_crc32(lzo_uint32_t c, const lzo_bytep buf, lzo_uint len)
return 0;
crc = (c & LZO_UINT32_C(0xffffffff)) ^ LZO_UINT32_C(0xffffffff);
if (len >= 16) do
{
LZO_DO16(buf, 0);
buf += 16;
len -= 16;
} while (len >= 16);
}
while (len >= 16);
if (len != 0) do
{
LZO_DO1(buf, 0);
buf += 1;
len -= 1;
} while (len > 0);
}
while (len > 0);
return crc ^ LZO_UINT32_C(0xffffffff);
#undef table

View File

@@ -40,7 +40,10 @@ BOOL FAR PASCAL LibMain ( HANDLE hInstance, WORD wDataSegment,
int __far __pascal LibMain(int a, short b, short c, long d)
#endif
{
LZO_UNUSED(a); LZO_UNUSED(b); LZO_UNUSED(c); LZO_UNUSED(d);
LZO_UNUSED(a);
LZO_UNUSED(b);
LZO_UNUSED(c);
LZO_UNUSED(d);
return 1;
}

View File

@@ -64,7 +64,9 @@
__lzo_static_forceinline unsigned lzo_bitops_ctlz32_func(lzo_uint32_t v)
{
#if (LZO_BITOPS_USE_MSC_BITSCAN) && (LZO_ARCH_AMD64 || LZO_ARCH_I386)
unsigned long r; (void) _BitScanReverse(&r, v); return (unsigned) r ^ 31;
unsigned long r;
(void) _BitScanReverse(&r, v);
return (unsigned) r ^ 31;
#define lzo_bitops_ctlz32(v) lzo_bitops_ctlz32_func(v)
#elif (LZO_BITOPS_USE_ASM_BITSCAN) && (LZO_ARCH_AMD64 || LZO_ARCH_I386) && (LZO_ASM_SYNTAX_GNUC)
lzo_uint32_t r;
@@ -72,13 +74,18 @@ __lzo_static_forceinline unsigned lzo_bitops_ctlz32_func(lzo_uint32_t v)
return (unsigned) r ^ 31;
#define lzo_bitops_ctlz32(v) lzo_bitops_ctlz32_func(v)
#elif (LZO_BITOPS_USE_GNUC_BITSCAN) && (LZO_SIZEOF_INT == 4)
unsigned r; r = (unsigned) __builtin_clz(v); return r;
unsigned r;
r = (unsigned) __builtin_clz(v);
return r;
#define lzo_bitops_ctlz32(v) ((unsigned) __builtin_clz(v))
#elif (LZO_BITOPS_USE_GNUC_BITSCAN) && (LZO_SIZEOF_LONG == 8) && (LZO_WORDSIZE >= 8)
unsigned r; r = (unsigned) __builtin_clzl(v); return r ^ 32;
unsigned r;
r = (unsigned) __builtin_clzl(v);
return r ^ 32;
#define lzo_bitops_ctlz32(v) (((unsigned) __builtin_clzl(v)) ^ 32)
#else
LZO_UNUSED(v); return 0;
LZO_UNUSED(v);
return 0;
#endif
}
@@ -86,7 +93,9 @@ __lzo_static_forceinline unsigned lzo_bitops_ctlz32_func(lzo_uint32_t v)
__lzo_static_forceinline unsigned lzo_bitops_ctlz64_func(lzo_uint64_t v)
{
#if (LZO_BITOPS_USE_MSC_BITSCAN) && (LZO_ARCH_AMD64)
unsigned long r; (void) _BitScanReverse64(&r, v); return (unsigned) r ^ 63;
unsigned long r;
(void) _BitScanReverse64(&r, v);
return (unsigned) r ^ 63;
#define lzo_bitops_ctlz64(v) lzo_bitops_ctlz64_func(v)
#elif (LZO_BITOPS_USE_ASM_BITSCAN) && (LZO_ARCH_AMD64) && (LZO_ASM_SYNTAX_GNUC)
lzo_uint64_t r;
@@ -94,13 +103,18 @@ __lzo_static_forceinline unsigned lzo_bitops_ctlz64_func(lzo_uint64_t v)
return (unsigned) r ^ 63;
#define lzo_bitops_ctlz64(v) lzo_bitops_ctlz64_func(v)
#elif (LZO_BITOPS_USE_GNUC_BITSCAN) && (LZO_SIZEOF_LONG == 8) && (LZO_WORDSIZE >= 8)
unsigned r; r = (unsigned) __builtin_clzl(v); return r;
unsigned r;
r = (unsigned) __builtin_clzl(v);
return r;
#define lzo_bitops_ctlz64(v) ((unsigned) __builtin_clzl(v))
#elif (LZO_BITOPS_USE_GNUC_BITSCAN) && (LZO_SIZEOF_LONG_LONG == 8) && (LZO_WORDSIZE >= 8)
unsigned r; r = (unsigned) __builtin_clzll(v); return r;
unsigned r;
r = (unsigned) __builtin_clzll(v);
return r;
#define lzo_bitops_ctlz64(v) ((unsigned) __builtin_clzll(v))
#else
LZO_UNUSED(v); return 0;
LZO_UNUSED(v);
return 0;
#endif
}
#endif
@@ -108,7 +122,9 @@ __lzo_static_forceinline unsigned lzo_bitops_ctlz64_func(lzo_uint64_t v)
__lzo_static_forceinline unsigned lzo_bitops_cttz32_func(lzo_uint32_t v)
{
#if (LZO_BITOPS_USE_MSC_BITSCAN) && (LZO_ARCH_AMD64 || LZO_ARCH_I386)
unsigned long r; (void) _BitScanForward(&r, v); return (unsigned) r;
unsigned long r;
(void) _BitScanForward(&r, v);
return (unsigned) r;
#define lzo_bitops_cttz32(v) lzo_bitops_cttz32_func(v)
#elif (LZO_BITOPS_USE_ASM_BITSCAN) && (LZO_ARCH_AMD64 || LZO_ARCH_I386) && (LZO_ASM_SYNTAX_GNUC)
lzo_uint32_t r;
@@ -116,10 +132,13 @@ __lzo_static_forceinline unsigned lzo_bitops_cttz32_func(lzo_uint32_t v)
return (unsigned) r;
#define lzo_bitops_cttz32(v) lzo_bitops_cttz32_func(v)
#elif (LZO_BITOPS_USE_GNUC_BITSCAN) && (LZO_SIZEOF_INT >= 4)
unsigned r; r = (unsigned) __builtin_ctz(v); return r;
unsigned r;
r = (unsigned) __builtin_ctz(v);
return r;
#define lzo_bitops_cttz32(v) ((unsigned) __builtin_ctz(v))
#else
LZO_UNUSED(v); return 0;
LZO_UNUSED(v);
return 0;
#endif
}
@@ -127,7 +146,9 @@ __lzo_static_forceinline unsigned lzo_bitops_cttz32_func(lzo_uint32_t v)
__lzo_static_forceinline unsigned lzo_bitops_cttz64_func(lzo_uint64_t v)
{
#if (LZO_BITOPS_USE_MSC_BITSCAN) && (LZO_ARCH_AMD64)
unsigned long r; (void) _BitScanForward64(&r, v); return (unsigned) r;
unsigned long r;
(void) _BitScanForward64(&r, v);
return (unsigned) r;
#define lzo_bitops_cttz64(v) lzo_bitops_cttz64_func(v)
#elif (LZO_BITOPS_USE_ASM_BITSCAN) && (LZO_ARCH_AMD64) && (LZO_ASM_SYNTAX_GNUC)
lzo_uint64_t r;
@@ -135,13 +156,18 @@ __lzo_static_forceinline unsigned lzo_bitops_cttz64_func(lzo_uint64_t v)
return (unsigned) r;
#define lzo_bitops_cttz64(v) lzo_bitops_cttz64_func(v)
#elif (LZO_BITOPS_USE_GNUC_BITSCAN) && (LZO_SIZEOF_LONG >= 8) && (LZO_WORDSIZE >= 8)
unsigned r; r = (unsigned) __builtin_ctzl(v); return r;
unsigned r;
r = (unsigned) __builtin_ctzl(v);
return r;
#define lzo_bitops_cttz64(v) ((unsigned) __builtin_ctzl(v))
#elif (LZO_BITOPS_USE_GNUC_BITSCAN) && (LZO_SIZEOF_LONG_LONG >= 8) && (LZO_WORDSIZE >= 8)
unsigned r; r = (unsigned) __builtin_ctzll(v); return r;
unsigned r;
r = (unsigned) __builtin_ctzll(v);
return r;
#define lzo_bitops_cttz64(v) ((unsigned) __builtin_ctzll(v))
#else
LZO_UNUSED(v); return 0;
LZO_UNUSED(v);
return 0;
#endif
}
#endif

View File

@@ -65,7 +65,8 @@
//
************************************************************************/
union lzo_config_check_union {
union lzo_config_check_union
{
lzo_uint a[2];
unsigned char b[2 * LZO_MAX(8, sizeof(lzo_uint))];
#if defined(lzo_uint64_t)
@@ -102,17 +103,20 @@ _lzo_config_check(void)
r &= ((* (lzo_bytep) p) == 0);
#if !(LZO_CFG_NO_CONFIG_CHECK)
#if (LZO_ABI_BIG_ENDIAN)
u.a[0] = u.a[1] = 0; u.b[sizeof(lzo_uint) - 1] = 128;
u.a[0] = u.a[1] = 0;
u.b[sizeof(lzo_uint) - 1] = 128;
p = u2p(&u, 0);
r &= ((* (lzo_uintp) p) == 128);
#endif
#if (LZO_ABI_LITTLE_ENDIAN)
u.a[0] = u.a[1] = 0; u.b[0] = 128;
u.a[0] = u.a[1] = 0;
u.b[0] = 128;
p = u2p(&u, 0);
r &= ((* (lzo_uintp) p) == 128);
#endif
u.a[0] = u.a[1] = 0;
u.b[0] = 1; u.b[3] = 2;
u.b[0] = 1;
u.b[3] = 2;
p = u2p(&u, 1);
r &= UA_GET_NE16(p) == 0;
r &= UA_GET_LE16(p) == 0;
@@ -127,13 +131,16 @@ _lzo_config_check(void)
r &= UA_GET_NE16(p) == LZO_UINT16_C(0x8180);
#endif
u.a[0] = u.a[1] = 0;
u.b[0] = 3; u.b[5] = 4;
u.b[0] = 3;
u.b[5] = 4;
p = u2p(&u, 1);
r &= UA_GET_NE32(p) == 0;
r &= UA_GET_LE32(p) == 0;
u.b[1] = 128;
r &= UA_GET_LE32(p) == 128;
u.b[2] = 129; u.b[3] = 130; u.b[4] = 131;
u.b[2] = 129;
u.b[3] = 130;
u.b[4] = 131;
r &= UA_GET_LE32(p) == LZO_UINT32_C(0x83828180);
#if (LZO_ABI_BIG_ENDIAN)
r &= UA_GET_NE32(p) == LZO_UINT32_C(0x80818283);
@@ -143,7 +150,8 @@ _lzo_config_check(void)
#endif
#if defined(UA_GET_NE64)
u.c[0] = u.c[1] = 0;
u.b[0] = 5; u.b[9] = 6;
u.b[0] = 5;
u.b[9] = 6;
p = u2p(&u, 1);
u.c[0] = u.c[1] = 0;
r &= UA_GET_NE64(p) == 0;
@@ -154,32 +162,52 @@ _lzo_config_check(void)
#endif
#endif
#if defined(lzo_bitops_ctlz32)
{ unsigned i = 0; lzo_uint32_t v;
for (v = 1; v != 0 && r == 1; v <<= 1, i++) {
{
unsigned i = 0;
lzo_uint32_t v;
for (v = 1; v != 0 && r == 1; v <<= 1, i++)
{
r &= lzo_bitops_ctlz32(v) == 31 - i;
r &= lzo_bitops_ctlz32_func(v) == 31 - i;
}}
}
}
#endif
#if defined(lzo_bitops_ctlz64)
{ unsigned i = 0; lzo_uint64_t v;
for (v = 1; v != 0 && r == 1; v <<= 1, i++) {
{
unsigned i = 0;
lzo_uint64_t v;
for (v = 1; v != 0 && r == 1; v <<= 1, i++)
{
r &= lzo_bitops_ctlz64(v) == 63 - i;
r &= lzo_bitops_ctlz64_func(v) == 63 - i;
}}
}
}
#endif
#if defined(lzo_bitops_cttz32)
{ unsigned i = 0; lzo_uint32_t v;
for (v = 1; v != 0 && r == 1; v <<= 1, i++) {
{
unsigned i = 0;
lzo_uint32_t v;
for (v = 1; v != 0 && r == 1; v <<= 1, i++)
{
r &= lzo_bitops_cttz32(v) == i;
r &= lzo_bitops_cttz32_func(v) == i;
}}
}
}
#endif
#if defined(lzo_bitops_cttz64)
{ unsigned i = 0; lzo_uint64_t v;
for (v = 1; v != 0 && r == 1; v <<= 1, i++) {
{
unsigned i = 0;
lzo_uint64_t v;
for (v = 1; v != 0 && r == 1; v <<= 1, i++)
{
r &= lzo_bitops_cttz64(v) == i;
r &= lzo_bitops_cttz64_func(v) == i;
}}
}
}
#endif
#endif
LZO_UNUSED_FUNC(lzo_bitops_unused_funcs);
@@ -220,10 +248,12 @@ __lzo_init_v2(unsigned v, int s1, int s2, int s3, int s4, int s5,
(s7 == -1 || s7 == (int) sizeof(char*)) &&
(s8 == -1 || s8 == (int) sizeof(lzo_voidp)) &&
(s9 == -1 || s9 == (int) sizeof(lzo_callback_t));
if (!r)
return LZO_E_ERROR;
r = _lzo_config_check();
if (r != LZO_E_OK)
return r;

View File

@@ -128,6 +128,7 @@ init_match ( LZO_COMPRESS_T *c, lzo_swd_p s,
c->lazy = 0;
r = swd_init(s, dict, dict_len);
if (r != LZO_E_OK)
{
swd_exit(s);
@@ -164,8 +165,10 @@ find_match ( LZO_COMPRESS_T *c, lzo_swd_p s,
s->m_len = SWD_THRESHOLD;
s->m_off = 0;
#ifdef SWD_BEST_OFF
if (s->use_best_off)
lzo_memset(s->best_pos, 0, sizeof(s->best_pos));
#endif
swd_findbest(s);
c->m_len = s->m_len;
@@ -183,9 +186,11 @@ find_match ( LZO_COMPRESS_T *c, lzo_swd_p s,
{
c->look = s->look + 1;
}
c->bp = c->ip - c->look;
#if 0
/* brute force match search */
if (c->m_len > SWD_THRESHOLD && c->m_len + 1 <= c->look)
{
@@ -195,18 +200,23 @@ find_match ( LZO_COMPRESS_T *c, lzo_swd_p s,
if (ip - in > s->swd_n)
in = ip - s->swd_n;
for (;;)
{
while (*in != *ip)
in++;
if (in == ip)
break;
if (in != m)
if (lzo_memcmp(in, ip, c->m_len + 1) == 0)
printf("%p %p %p %5d\n", in, ip, m, c->m_len);
in++;
}
}
#endif
if (c->cb && c->cb->nprogress && c->textsize > c->printcount)

View File

@@ -61,14 +61,19 @@ __lzo_align_gap(const lzo_voidp ptr, lzo_uint size)
#error "__LZO_UINTPTR_T_IS_POINTER is unsupported"
#else
lzo_uintptr_t p, n;
if (size < 2) return 0;
p = __lzo_ptr_linear(ptr);
#if 0
n = (((p + size - 1) / size) * size) - p;
#else
if ((size & (size - 1)) != 0)
return 0;
n = size; n = ((p + n - 1) & ~(n - 1)) - p;
n = size;
n = ((p + n - 1) & ~(n - 1)) - p;
#endif
#endif
assert((long)n >= 0);

File diff suppressed because it is too large Load Diff

View File

@@ -189,6 +189,7 @@ void swd_initdict(lzo_swd_p s, const lzo_bytep dict, lzo_uint dict_len)
if (!dict || dict_len == 0)
return;
if (dict_len > s->swd_n)
{
dict += dict_len - s->swd_n;
@@ -221,7 +222,8 @@ void swd_insertdict(lzo_swd_p s, lzo_uint node, lzo_uint len)
assert(s_llen3(s)[key] <= s->swd_n);
#ifdef HEAD2
IF_HEAD2(s) {
IF_HEAD2(s)
{
key = HEAD2(s_b(s), node);
s_head2(s)[key] = SWD_UINT(node);
}
@@ -255,15 +257,19 @@ int swd_init(lzo_swd_p s, const lzo_bytep dict, lzo_uint dict_len)
r &= s->best3 != NULL;
r &= s->llen3 != NULL;
#ifdef HEAD2
IF_HEAD2(s) {
IF_HEAD2(s)
{
s->head2 = (swd_uintp) malloc(sizeof(swd_uint) * 65536L);
r &= s->head2 != NULL;
}
#endif
if (r != 1) {
if (r != 1)
{
swd_exit(s);
return LZO_E_OUT_OF_MEMORY;
}
#endif
s->m_len = 0;
@@ -271,6 +277,7 @@ int swd_init(lzo_swd_p s, const lzo_bytep dict, lzo_uint dict_len)
#if defined(SWD_BEST_OFF)
{
unsigned i;
for (i = 0; i < SWD_BEST_OFF; i++)
s->best_off[i] = s->best_pos[i] = 0;
}
@@ -288,8 +295,10 @@ int swd_init(lzo_swd_p s, const lzo_bytep dict, lzo_uint dict_len)
s->b_size = s->swd_n + s->swd_f;
#if 0
if (2 * s->swd_f >= s->swd_n || s->b_size + s->swd_f >= SWD_UINT_MAX)
return LZO_E_ERROR;
#else
LZO_COMPILE_TIME_ASSERT(!(0ul + 2 * SWD_F >= SWD_N))
LZO_COMPILE_TIME_ASSERT(!(0ul + SWD_N + SWD_F + SWD_F >= SWD_UINT_MAX))
@@ -299,14 +308,17 @@ int swd_init(lzo_swd_p s, const lzo_bytep dict, lzo_uint dict_len)
lzo_memset(s_llen3(s), 0, (lzo_uint)sizeof(s_llen3(s)[0]) * (lzo_uint)SWD_HSIZE);
#ifdef HEAD2
IF_HEAD2(s) {
IF_HEAD2(s)
{
#if 1
lzo_memset(s_head2(s), 0xff, (lzo_uint)sizeof(s_head2(s)[0]) * 65536L);
assert(s_head2(s)[0] == NIL2);
#else
lzo_xint i;
for (i = 0; i < 65536L; i++)
s_head2(s)[i] = NIL2;
#endif
}
#endif
@@ -319,26 +331,34 @@ int swd_init(lzo_swd_p s, const lzo_bytep dict, lzo_uint dict_len)
assert(s->ip + s->swd_f <= s->b_size);
#if 1
s->look = (lzo_uint)(s->c->in_end - s->c->ip);
if (s->look > 0)
{
if (s->look > s->swd_f)
s->look = s->swd_f;
lzo_memcpy(&s_b(s)[s->ip], s->c->ip, s->look);
s->c->ip += s->look;
s->ip += s->look;
}
#else
s->look = 0;
while (s->look < s->swd_f)
{
int c;
if ((c = getbyte(*(s->c))) < 0)
break;
s_b(s)[s->ip] = LZO_BYTE(c);
s->ip++;
s->look++;
}
#endif
if (s->ip == s->b_size)
s->ip = 0;
@@ -346,18 +366,22 @@ int swd_init(lzo_swd_p s, const lzo_bytep dict, lzo_uint dict_len)
swd_insertdict(s, 0, s->dict_len);
s->rp = s->first_rp;
if (s->rp >= s->node_count)
s->rp -= s->node_count;
else
s->rp += s->b_size - s->node_count;
#if 1 || defined(__LZO_CHECKER)
/* initialize memory for the first few HEAD3 (if s->ip is not far
* enough ahead to do this job for us). The value doesn't matter. */
if (s->look < 3) {
if (s->look < 3)
{
lzo_bytep p = &s_b(s)[s->bp + s->look];
p[0] = p[1] = p[2] = 0;
}
#endif
return LZO_E_OK;
@@ -370,13 +394,19 @@ void swd_exit(lzo_swd_p s)
#if defined(__LZO_CHECKER)
/* free in reverse order of allocations */
#ifdef HEAD2
free(s->head2); s->head2 = NULL;
free(s->head2);
s->head2 = NULL;
#endif
free(s->llen3); s->llen3 = NULL;
free(s->best3); s->best3 = NULL;
free(s->succ3); s->succ3 = NULL;
free(s->head3); s->head3 = NULL;
free(s->b); s->b = NULL;
free(s->llen3);
s->llen3 = NULL;
free(s->best3);
s->best3 = NULL;
free(s->succ3);
s->succ3 = NULL;
free(s->head3);
s->head3 = NULL;
free(s->b);
s->b = NULL;
#else
LZO_UNUSED(s);
#endif
@@ -400,23 +430,30 @@ void swd_getbyte(lzo_swd_p s)
{
if (s->look > 0)
--s->look;
#if 1 || defined(__LZO_CHECKER)
/* initialize memory - value doesn't matter */
s_b(s)[s->ip] = 0;
if (s->ip < s->swd_f)
s->b_wrap[s->ip] = 0;
#endif
}
else
{
s_b(s)[s->ip] = LZO_BYTE(c);
if (s->ip < s->swd_f)
s->b_wrap[s->ip] = LZO_BYTE(c);
}
if (++s->ip == s->b_size)
s->ip = 0;
if (++s->bp == s->b_size)
s->bp = 0;
if (++s->rp == s->b_size)
s->rp = 0;
}
@@ -434,6 +471,7 @@ void swd_remove_node(lzo_swd_p s, lzo_uint node)
lzo_uint key;
#ifdef LZO_DEBUG
if (s->first_rp != LZO_UINT_MAX)
{
if (node != s->first_rp)
@@ -441,9 +479,11 @@ void swd_remove_node(lzo_swd_p s, lzo_uint node)
(long)node, (long)s->rp, (long)s->ip, (long)s->bp,
(long)s->first_rp, (long)(s->ip - node),
(long)(s->ip - s->bp));
assert(node == s->first_rp);
s->first_rp = LZO_UINT_MAX;
}
#endif
key = HEAD3(s_b(s), node);
@@ -451,9 +491,11 @@ void swd_remove_node(lzo_swd_p s, lzo_uint node)
--s_llen3(s)[key];
#ifdef HEAD2
IF_HEAD2(s) {
IF_HEAD2(s)
{
key = HEAD2(s_b(s), node);
assert(s_head2(s)[key] != NIL2);
if ((lzo_uint) s_head2(s)[key] == node)
s_head2(s)[key] = NIL2;
}
@@ -489,14 +531,16 @@ void swd_accept(lzo_swd_p s, lzo_uint n)
#ifdef HEAD2
/* add bp into HEAD2 */
IF_HEAD2(s) {
IF_HEAD2(s)
{
key = HEAD2(s_b(s), s->bp);
s_head2(s)[key] = SWD_UINT(s->bp);
}
#endif
swd_getbyte(s);
} while (--n != 0);
}
while (--n != 0);
}
@@ -519,6 +563,7 @@ void swd_search(lzo_swd_p s, lzo_uint node, lzo_uint cnt)
assert(s->m_len > 0);
scan_end1 = bp[m_len - 1];
for (; cnt-- > 0; node = s_succ3(s)[node])
{
p1 = bp;
@@ -539,42 +584,59 @@ void swd_search(lzo_swd_p s, lzo_uint node, lzo_uint cnt)
assert(lzo_memcmp(bp, &b[node], 3) == 0);
#if 0 && (LZO_OPT_UNALIGNED32)
p1 += 3; p2 += 3;
p1 += 3;
p2 += 3;
while (p1 + 4 <= px && UA_GET_NE32(p1) == UA_GET_NE32(p2))
p1 += 4, p2 += 4;
while (p1 < px && *p1 == *p2)
p1 += 1, p2 += 1;
#else
p1 += 2; p2 += 2;
do {} while (++p1 < px && *p1 == *++p2);
p1 += 2;
p2 += 2;
do {}
while (++p1 < px && *p1 == *++p2);
#endif
i = pd(p1, bp);
#ifdef LZO_DEBUG
if (lzo_memcmp(bp, &b[node], i) != 0)
printf("%5ld %5ld %5ld %02x/%02x %02x/%02x\n",
(long)s->bp, (long) node, (long) i,
bp[0], bp[1], b[node], b[node + 1]);
#endif
assert(lzo_memcmp(bp, &b[node], i) == 0);
#if defined(SWD_BEST_OFF)
if (i < SWD_BEST_OFF)
{
if (s->best_pos[i] == 0)
s->best_pos[i] = node + 1;
}
#endif
if (i > m_len)
{
s->m_len = m_len = i;
s->m_pos = node;
if (m_len == s->look)
return;
if (m_len >= s->nice_length)
return;
if (m_len > (lzo_uint) s_best3(s)[node])
return;
scan_end1 = bp[m_len - 1];
}
}
@@ -597,17 +659,23 @@ lzo_bool swd_search2(lzo_swd_p s)
assert(s->m_len > 0);
key = s_head2(s)[ HEAD2(s_b(s), s->bp) ];
if (key == NIL2)
return 0;
#ifdef LZO_DEBUG
if (lzo_memcmp(&s_b(s)[s->bp], &s_b(s)[key], 2) != 0)
printf("%5ld %5ld %02x/%02x %02x/%02x\n", (long)s->bp, (long)key,
s_b(s)[s->bp], s_b(s)[s->bp + 1], s_b(s)[key], s_b(s)[key + 1]);
#endif
assert(lzo_memcmp(&s_b(s)[s->bp], &s_b(s)[key], 2) == 0);
#if defined(SWD_BEST_OFF)
if (s->best_pos[2] == 0)
s->best_pos[2] = key + 1;
#endif
if (s->m_len < 2)
@@ -615,6 +683,7 @@ lzo_bool swd_search2(lzo_swd_p s)
s->m_len = 2;
s->m_pos = key;
}
return 1;
}
@@ -639,42 +708,55 @@ void swd_findbest(lzo_swd_p s)
node = s_succ3(s)[s->bp] = s_get_head3(s, key);
cnt = s_llen3(s)[key]++;
assert(s_llen3(s)[key] <= s->swd_n + s->swd_f);
if (cnt > s->max_chain && s->max_chain > 0)
cnt = s->max_chain;
s_head3(s)[key] = SWD_UINT(s->bp);
s->b_char = s_b(s)[s->bp];
len = s->m_len;
if (s->m_len >= s->look)
{
if (s->look == 0)
s->b_char = -1;
s->m_off = 0;
s_best3(s)[s->bp] = SWD_UINT(s->swd_f + 1);
}
else
{
#if defined(HEAD2)
if (swd_search2(s) && s->look >= 3)
swd_search(s, node, cnt);
#else
if (s->look >= 3)
swd_search(s, node, cnt);
#endif
if (s->m_len > len)
s->m_off = swd_pos2off(s, s->m_pos);
s_best3(s)[s->bp] = SWD_UINT(s->m_len);
#if defined(SWD_BEST_OFF)
if (s->use_best_off)
{
unsigned i;
for (i = 2; i < SWD_BEST_OFF; i++)
if (s->best_pos[i] > 0)
s->best_off[i] = swd_pos2off(s, s->best_pos[i] - 1);
else
s->best_off[i] = 0;
}
#endif
}
@@ -682,7 +764,8 @@ void swd_findbest(lzo_swd_p s)
#ifdef HEAD2
/* add bp into HEAD2 */
IF_HEAD2(s) {
IF_HEAD2(s)
{
key = HEAD2(s_b(s), s->bp);
s_head2(s)[key] = SWD_UINT(s->bp);
}

View File

@@ -121,20 +121,26 @@ lzo_adler32(lzo_uint32_t adler, const lzo_bytep buf, lzo_uint len)
{
k = len < LZO_NMAX ? (unsigned) len : LZO_NMAX;
len -= k;
if (k >= 16) do
{
LZO_DO16(buf, 0);
buf += 16;
k -= 16;
} while (k >= 16);
}
while (k >= 16);
if (k != 0) do
{
s1 += *buf++;
s2 += s1;
} while (--k > 0);
}
while (--k > 0);
s1 %= LZO_BASE;
s2 %= LZO_BASE;
}
return (s2 << 16) | s1;
}

View File

@@ -57,7 +57,8 @@ extern "C" {
//
************************************************************************/
typedef struct {
typedef struct
{
/* configuration */
unsigned rbits;

34
extern/zlib/CMakeLists.txt vendored Normal file
View File

@@ -0,0 +1,34 @@
if(NOT WIN32)
find_package(ZLIB)
endif()
if(WIN32 OR NOT ZLIB_FOUND)
message("-- Using Athena's built-in zlib")
add_library(z
adler32.c
compress.c
crc32.c
deflate.c
infback.c
inffast.c
inflate.c
inftrees.c
trees.c
uncompr.c
zutil.c
crc32.h
deflate.h
gzguts.h
inffast.h
inffixed.h
inflate.h
inftrees.h
trees.h
zconf.h
zlib.h
zutil.h)
if(WIN32 AND NOT UNIX)
install(FILES zconf.h zlib.h DESTINATION include COMPONENT zlib)
install(TARGETS z DESTINATION lib COMPONENT zlib)
endif()
set(ZLIB_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR} CACHE PATH "Zlib include path" FORCE)
endif()

42
extern/zlib/adler32.c vendored
View File

@@ -79,13 +79,18 @@ uLong ZEXPORT adler32(adler, buf, len)
adler &= 0xffff;
/* in case user likes doing a byte at a time, keep it fast */
if (len == 1) {
if (len == 1)
{
adler += buf[0];
if (adler >= BASE)
adler -= BASE;
sum2 += adler;
if (sum2 >= BASE)
sum2 -= BASE;
return adler | (sum2 << 16);
}
@@ -94,40 +99,54 @@ uLong ZEXPORT adler32(adler, buf, len)
return 1L;
/* in case short lengths are provided, keep it somewhat fast */
if (len < 16) {
while (len--) {
if (len < 16)
{
while (len--)
{
adler += *buf++;
sum2 += adler;
}
if (adler >= BASE)
adler -= BASE;
MOD28(sum2); /* only added so many BASE's */
return adler | (sum2 << 16);
}
/* do length NMAX blocks -- requires just one modulo operation */
while (len >= NMAX) {
while (len >= NMAX)
{
len -= NMAX;
n = NMAX / 16; /* NMAX is divisible by 16 */
do {
do
{
DO16(buf); /* 16 sums unrolled */
buf += 16;
} while (--n);
}
while (--n);
MOD(adler);
MOD(sum2);
}
/* do remaining bytes (less than NMAX, still just one modulo) */
if (len) { /* avoid modulos if none remaining */
while (len >= 16) {
if (len) /* avoid modulos if none remaining */
{
while (len >= 16)
{
len -= 16;
DO16(buf);
buf += 16;
}
while (len--) {
while (len--)
{
adler += *buf++;
sum2 += adler;
}
MOD(adler);
MOD(sum2);
}
@@ -162,10 +181,15 @@ local uLong adler32_combine_(adler1, adler2, len2)
MOD(sum2);
sum1 += (adler2 & 0xffff) + BASE - 1;
sum2 += ((adler1 >> 16) & 0xffff) + ((adler2 >> 16) & 0xffff) + BASE - rem;
if (sum1 >= BASE) sum1 -= BASE;
if (sum1 >= BASE) sum1 -= BASE;
if (sum2 >= (BASE << 1)) sum2 -= (BASE << 1);
if (sum2 >= BASE) sum2 -= BASE;
return sum1 | (sum2 << 16);
}

View File

@@ -36,11 +36,14 @@ int ZEXPORT compress2 (dest, destLen, source, sourceLen, level)
stream.next_in = (Bytef*)source;
stream.avail_in = (uInt)sourceLen;
#ifdef MAXSEG_64K
/* Check for source > 64K on 16-bit machine: */
if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR;
#endif
stream.next_out = dest;
stream.avail_out = (uInt) * destLen;
if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR;
stream.zalloc = (alloc_func)0;
@@ -48,13 +51,17 @@ int ZEXPORT compress2 (dest, destLen, source, sourceLen, level)
stream.opaque = (voidpf)0;
err = deflateInit(&stream, level);
if (err != Z_OK) return err;
err = deflate(&stream, Z_FINISH);
if (err != Z_STREAM_END) {
if (err != Z_STREAM_END)
{
deflateEnd(&stream);
return err == Z_OK ? Z_BUF_ERROR : err;
}
*destLen = stream.total_out;
err = deflateEnd(&stream);

114
extern/zlib/crc32.c vendored
View File

@@ -99,39 +99,50 @@ local void make_crc_table()
/* See if another task is already doing this (not thread-safe, but better
than nothing -- significantly reduces duration of vulnerability in
case the advice about DYNAMIC_CRC_TABLE is ignored) */
if (first) {
if (first)
{
first = 0;
/* make exclusive-or pattern from polynomial (0xedb88320UL) */
poly = 0;
for (n = 0; n < (int)(sizeof(p) / sizeof(unsigned char)); n++)
poly |= (z_crc_t)1 << (31 - p[n]);
/* generate a crc for every 8-bit value */
for (n = 0; n < 256; n++) {
for (n = 0; n < 256; n++)
{
c = (z_crc_t)n;
for (k = 0; k < 8; k++)
c = c & 1 ? poly ^ (c >> 1) : c >> 1;
crc_table[0][n] = c;
}
#ifdef BYFOUR
/* generate crc for each value followed by one, two, and three zeros,
and then the byte reversal of those as well as the first table */
for (n = 0; n < 256; n++) {
for (n = 0; n < 256; n++)
{
c = crc_table[0][n];
crc_table[4][n] = ZSWAP32(c);
for (k = 1; k < 4; k++) {
for (k = 1; k < 4; k++)
{
c = crc_table[0][c & 0xff] ^ (c >> 8);
crc_table[k][n] = c;
crc_table[k + 4][n] = ZSWAP32(c);
}
}
#endif /* BYFOUR */
crc_table_empty = 0;
}
else { /* not first */
else /* not first */
{
/* wait for the other guy to finish (not efficient, but rare) */
while (crc_table_empty)
;
@@ -143,7 +154,9 @@ local void make_crc_table()
FILE* out;
out = fopen("crc32.h", "w");
if (out == NULL) return;
fprintf(out, "/* crc32.h -- tables for rapid CRC calculation\n");
fprintf(out, " * Generated automatically by crc32.c\n */\n\n");
fprintf(out, "local const z_crc_t FAR ");
@@ -151,10 +164,13 @@ local void make_crc_table()
write_table(out, crc_table[0]);
# ifdef BYFOUR
fprintf(out, "#ifdef BYFOUR\n");
for (k = 1; k < 8; k++) {
for (k = 1; k < 8; k++)
{
fprintf(out, " },\n {\n");
write_table(out, crc_table[k]);
}
fprintf(out, "#endif\n");
# endif /* BYFOUR */
fprintf(out, " }\n};\n");
@@ -190,8 +206,10 @@ local void write_table(out, table)
const z_crc_t FAR* ZEXPORT get_crc_table()
{
#ifdef DYNAMIC_CRC_TABLE
if (crc_table_empty)
make_crc_table();
#endif /* DYNAMIC_CRC_TABLE */
return (const z_crc_t FAR*)crc_table;
}
@@ -213,34 +231,48 @@ unsigned long ZEXPORT crc32(crc, buf, len)
if (buf == Z_NULL) return 0UL;
#ifdef DYNAMIC_CRC_TABLE
if (crc_table_empty)
make_crc_table();
#endif /* DYNAMIC_CRC_TABLE */
#ifdef BYFOUR
#ifdef WIN32
int size_match = (sizeof(void*) == sizeof(ptrdiff_t));
if (size_match) {
if (size_match)
{
#else
if (sizeof(void *) == sizeof(ptrdiff_t)) {
if (sizeof(void*) == sizeof(ptrdiff_t))
{
#endif /* WIN32 */
z_crc_t endian;
endian = 1;
if (*((unsigned char*)(&endian)))
return crc32_little(crc, buf, len);
else
return crc32_big(crc, buf, len);
}
#endif /* BYFOUR */
crc = crc ^ 0xffffffffUL;
while (len >= 8) {
while (len >= 8)
{
DO8;
len -= 8;
}
if (len) do {
if (len) do
{
DO1;
} while (--len);
}
while (--len);
return crc ^ 0xffffffffUL;
}
@@ -267,25 +299,35 @@ local unsigned long crc32_little(crc, buf, len)
c = (z_crc_t)crc;
c = ~c;
while (len && ((ptrdiff_t)buf & 3)) {
while (len && ((ptrdiff_t)buf & 3))
{
c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8);
len--;
}
buf4 = (const z_crc_t FAR*)(const void FAR*)buf;
while (len >= 32) {
while (len >= 32)
{
DOLIT32;
len -= 32;
}
while (len >= 4) {
while (len >= 4)
{
DOLIT4;
len -= 4;
}
buf = (const unsigned char FAR*)buf4;
if (len) do {
if (len) do
{
c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8);
} while (--len);
}
while (--len);
c = ~c;
return (unsigned long)c;
}
@@ -311,27 +353,37 @@ local unsigned long crc32_big(crc, buf, len)
c = ZSWAP32((z_crc_t)crc);
c = ~c;
while (len && ((ptrdiff_t)buf & 3)) {
while (len && ((ptrdiff_t)buf & 3))
{
c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8);
len--;
}
buf4 = (const z_crc_t FAR*)(const void FAR*)buf;
buf4--;
while (len >= 32) {
while (len >= 32)
{
DOBIG32;
len -= 32;
}
while (len >= 4) {
while (len >= 4)
{
DOBIG4;
len -= 4;
}
buf4++;
buf = (const unsigned char FAR*)buf4;
if (len) do {
if (len) do
{
c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8);
} while (--len);
}
while (--len);
c = ~c;
return (unsigned long)(ZSWAP32(c));
}
@@ -352,12 +404,16 @@ local unsigned long gf2_matrix_times(mat, vec)
unsigned long sum;
sum = 0;
while (vec) {
while (vec)
{
if (vec & 1)
sum ^= *mat;
vec >>= 1;
mat++;
}
return sum;
}
@@ -398,7 +454,9 @@ local uLong crc32_combine_(crc1, crc2, len2)
/* put operator for one zero bit in odd */
odd[0] = 0xedb88320UL; /* CRC-32 polynomial */
row = 1;
for (n = 1; n < GF2_DIM; n++) {
for (n = 1; n < GF2_DIM; n++)
{
odd[n] = row;
row <<= 1;
}
@@ -411,11 +469,14 @@ local uLong crc32_combine_(crc1, crc2, len2)
/* apply len2 zeros to crc1 (first square will put the operator for one
zero byte, eight zero bits, in even) */
do {
do
{
/* apply zeros operator for this bit of len2 */
gf2_matrix_square(even, odd);
if (len2 & 1)
crc1 = gf2_matrix_times(even, crc1);
len2 >>= 1;
/* if no more bits set, then done */
@@ -424,12 +485,15 @@ local uLong crc32_combine_(crc1, crc2, len2)
/* another iteration of the loop with odd and even swapped */
gf2_matrix_square(odd, even);
if (len2 & 1)
crc1 = gf2_matrix_times(odd, crc1);
len2 >>= 1;
/* if no more bits set, then done */
} while (len2 != 0);
}
while (len2 != 0);
/* return combined crc */
crc1 ^= crc2;

676
extern/zlib/deflate.c vendored

File diff suppressed because it is too large Load Diff

15
extern/zlib/deflate.h vendored
View File

@@ -62,12 +62,15 @@
/* Data structure describing a single value and its code string. */
typedef struct ct_data_s {
union {
typedef struct ct_data_s
{
union
{
ush freq; /* frequency count */
ush code; /* bit string */
} fc;
union {
union
{
ush dad; /* father node in Huffman tree */
ush len; /* length of bit string */
} dl;
@@ -80,7 +83,8 @@ typedef struct ct_data_s {
typedef struct static_tree_desc_s static_tree_desc;
typedef struct tree_desc_s {
typedef struct tree_desc_s
{
ct_data* dyn_tree; /* the dynamic tree */
int max_code; /* largest code with non zero frequency */
static_tree_desc* stat_desc; /* the corresponding static tree */
@@ -94,7 +98,8 @@ typedef unsigned IPos;
* save space in the various tables. IPos is used only for parameter passing.
*/
typedef struct internal_state {
typedef struct internal_state
{
z_streamp strm; /* pointer back to this zlib stream */
int status; /* as the name implies */
Bytef* pending_buf; /* output still pending */

View File

@@ -20,6 +20,7 @@ int ZEXPORT gzclose(file)
if (file == NULL)
return Z_STREAM_ERROR;
state = (gz_statep)file;
return state->mode == GZ_READ ? gzclose_r(file) : gzclose_w(file);

View File

@@ -147,7 +147,8 @@
#define GZIP 2 /* decompress a gzip stream */
/* internal gzip file state data structure */
typedef struct {
typedef struct
{
/* exposed contents for gzgetc() macro */
struct gzFile_s x; /* "x" for exposed */
/* x.have: number of bytes available at x.next */

136
extern/zlib/gzlib.c vendored
View File

@@ -45,15 +45,19 @@ char ZLIB_INTERNAL *gz_strwinerror (error)
(LPVOID)&msgbuf,
0,
NULL);
if (chars != 0) {
if (chars != 0)
{
/* If there is an \r\n appended, zap it. */
if (chars >= 2
&& msgbuf[chars - 2] == '\r' && msgbuf[chars - 1] == '\n') {
&& msgbuf[chars - 2] == '\r' && msgbuf[chars - 1] == '\n')
{
chars -= 2;
msgbuf[chars] = 0;
}
if (chars > sizeof (buf) - 1) {
if (chars > sizeof(buf) - 1)
{
chars = sizeof(buf) - 1;
msgbuf[chars] = 0;
}
@@ -61,7 +65,8 @@ char ZLIB_INTERNAL *gz_strwinerror (error)
wcstombs(buf, msgbuf, chars + 1);
LocalFree(msgbuf);
}
else {
else
{
sprintf(buf, "unknown win32 error (%ld)", error);
}
@@ -80,11 +85,14 @@ local void gz_reset(state)
#endif
{
state->x.have = 0; /* no output data available */
if (state->mode == GZ_READ) { /* for reading ... */
if (state->mode == GZ_READ) /* for reading ... */
{
state->eof = 0; /* not at end of file */
state->past = 0; /* have not read past end yet */
state->how = LOOK; /* look for gzip header */
}
state->seek = 0; /* no seek request pending */
gz_error(state, Z_OK, NULL); /* clear error */
state->x.pos = 0; /* no uncompressed data yet */
@@ -117,8 +125,10 @@ local gzFile gz_open(path, fd, mode)
/* allocate gzFile structure to return */
state = malloc(sizeof(gz_state));
if (state == NULL)
return NULL;
state->size = 0; /* no buffers allocated yet */
state->want = GZBUFSIZE; /* requested buffer size */
state->msg = NULL; /* no error message yet */
@@ -128,87 +138,115 @@ local gzFile gz_open(path, fd, mode)
state->level = Z_DEFAULT_COMPRESSION;
state->strategy = Z_DEFAULT_STRATEGY;
state->direct = 0;
while (*mode) {
while (*mode)
{
if (*mode >= '0' && *mode <= '9')
state->level = *mode - '0';
else
switch (*mode) {
switch (*mode)
{
case 'r':
state->mode = GZ_READ;
break;
#ifndef NO_GZCOMPRESS
case 'w':
state->mode = GZ_WRITE;
break;
case 'a':
state->mode = GZ_APPEND;
break;
#endif
case '+': /* can't read and write at the same time */
free(state);
return NULL;
case 'b': /* ignore -- will request binary anyway */
break;
#ifdef O_CLOEXEC
case 'e':
cloexec = 1;
break;
#endif
#ifdef O_EXCL
case 'x':
exclusive = 1;
break;
#endif
case 'f':
state->strategy = Z_FILTERED;
break;
case 'h':
state->strategy = Z_HUFFMAN_ONLY;
break;
case 'R':
state->strategy = Z_RLE;
break;
case 'F':
state->strategy = Z_FIXED;
case 'T':
state->direct = 1;
default: /* could consider as an error, but just ignore */
;
}
mode++;
}
/* must provide an "r", "w", or "a" */
if (state->mode == GZ_NONE) {
if (state->mode == GZ_NONE)
{
free(state);
return NULL;
}
/* can't force transparent read */
if (state->mode == GZ_READ) {
if (state->direct) {
if (state->mode == GZ_READ)
{
if (state->direct)
{
free(state);
return NULL;
}
state->direct = 1; /* for empty file */
}
/* save the path name for error messages */
#ifdef _WIN32
if (fd == -2) {
if (fd == -2)
{
len = wcstombs(NULL, path, 0);
if (len == (size_t) - 1)
len = 0;
}
else
#endif
len = strlen(path);
state->path = malloc(len + 1);
if (state->path == NULL) {
if (state->path == NULL)
{
free(state);
return NULL;
}
#ifdef _WIN32
if (fd == -2)
if (len)
wcstombs(state->path, path, len + 1);
@@ -245,17 +283,22 @@ local gzFile gz_open(path, fd, mode)
fd == -2 ? _wopen(path, oflag, 0666) :
#endif
open(path, oflag, 0666));
if (state->fd == -1) {
if (state->fd == -1)
{
free(state->path);
free(state);
return NULL;
}
if (state->mode == GZ_APPEND)
state->mode = GZ_WRITE; /* simplify later checks */
/* save the current position for rewinding (only if reading) */
if (state->mode == GZ_READ) {
if (state->mode == GZ_READ)
{
state->start = LSEEK(state->fd, 0, SEEK_CUR);
if (state->start == -1) state->start = 0;
}
@@ -304,6 +347,7 @@ gzFile ZEXPORT gzdopen(fd, mode)
if (fd == -1 || (path = malloc(7 + 3 * sizeof(int))) == NULL)
return NULL;
sprintf(path, "<fd:%d>", fd); /* for debugging */
gz = gz_open(path, fd, mode);
free(path);
@@ -336,7 +380,9 @@ int ZEXPORT gzbuffer(file, size)
/* get internal structure and check integrity */
if (file == NULL)
return -1;
state = (gz_statep)file;
if (state->mode != GZ_READ && state->mode != GZ_WRITE)
return -1;
@@ -347,6 +393,7 @@ int ZEXPORT gzbuffer(file, size)
/* check and set requested size */
if (size < 2)
size = 2; /* need two bytes to check magic header */
state->want = size;
return 0;
}
@@ -364,6 +411,7 @@ int ZEXPORT gzrewind(file)
/* get internal structure */
if (file == NULL)
return -1;
state = (gz_statep)file;
/* check that we're reading and that there's no error */
@@ -374,6 +422,7 @@ int ZEXPORT gzrewind(file)
/* back up and start over */
if (LSEEK(state->fd, state->start, SEEK_SET) == -1)
return -1;
gz_reset(state);
return 0;
}
@@ -395,7 +444,9 @@ z_off64_t ZEXPORT gzseek64(file, offset, whence)
/* get internal structure and check integrity */
if (file == NULL)
return -1;
state = (gz_statep)file;
if (state->mode != GZ_READ && state->mode != GZ_WRITE)
return -1;
@@ -412,14 +463,18 @@ z_off64_t ZEXPORT gzseek64(file, offset, whence)
offset -= state->x.pos;
else if (state->seek)
offset += state->skip;
state->seek = 0;
/* if within raw area while reading, just go there */
if (state->mode == GZ_READ && state->how == COPY &&
state->x.pos + offset >= 0) {
state->x.pos + offset >= 0)
{
ret = LSEEK(state->fd, offset - state->x.have, SEEK_CUR);
if (ret == -1)
return -1;
state->x.have = 0;
state->eof = 0;
state->past = 0;
@@ -431,18 +486,23 @@ z_off64_t ZEXPORT gzseek64(file, offset, whence)
}
/* calculate skip amount, rewinding if needed for back seek when reading */
if (offset < 0) {
if (offset < 0)
{
if (state->mode != GZ_READ) /* writing -- can't go backwards */
return -1;
offset += state->x.pos;
if (offset < 0) /* before start of file! */
return -1;
if (gzrewind(file) == -1) /* rewind, then skip to offset */
return -1;
}
/* if reading, skip what's in output buffer (one less gzgetc() check) */
if (state->mode == GZ_READ) {
if (state->mode == GZ_READ)
{
n = GT_OFF(state->x.have) || (z_off64_t)state->x.have > offset ?
(unsigned)offset : state->x.have;
state->x.have -= n;
@@ -452,10 +512,12 @@ z_off64_t ZEXPORT gzseek64(file, offset, whence)
}
/* request skip (if not zero) */
if (offset) {
if (offset)
{
state->seek = 1;
state->skip = offset;
}
return state->x.pos + offset;
}
@@ -488,7 +550,9 @@ z_off64_t ZEXPORT gztell64(file)
/* get internal structure and check integrity */
if (file == NULL)
return -1;
state = (gz_statep)file;
if (state->mode != GZ_READ && state->mode != GZ_WRITE)
return -1;
@@ -524,16 +588,21 @@ z_off64_t ZEXPORT gzoffset64(file)
/* get internal structure and check integrity */
if (file == NULL)
return -1;
state = (gz_statep)file;
if (state->mode != GZ_READ && state->mode != GZ_WRITE)
return -1;
/* compute and return effective offset in file */
offset = LSEEK(state->fd, 0, SEEK_CUR);
if (offset == -1)
return -1;
if (state->mode == GZ_READ) /* reading */
offset -= state->strm.avail_in; /* don't count buffered input */
return offset;
}
@@ -564,7 +633,9 @@ int ZEXPORT gzeof(file)
/* get internal structure and check integrity */
if (file == NULL)
return 0;
state = (gz_statep)file;
if (state->mode != GZ_READ && state->mode != GZ_WRITE)
return 0;
@@ -586,13 +657,16 @@ const char * ZEXPORT gzerror(file, errnum)
/* get internal structure and check integrity */
if (file == NULL)
return NULL;
state = (gz_statep)file;
if (state->mode != GZ_READ && state->mode != GZ_WRITE)
return NULL;
/* return error information */
if (errnum != NULL)
*errnum = state->err;
return state->msg == NULL ? "" : state->msg;
}
@@ -609,15 +683,19 @@ void ZEXPORT gzclearerr(file)
/* get internal structure and check integrity */
if (file == NULL)
return;
state = (gz_statep)file;
if (state->mode != GZ_READ && state->mode != GZ_WRITE)
return;
/* clear error and end-of-file */
if (state->mode == GZ_READ) {
if (state->mode == GZ_READ)
{
state->eof = 0;
state->past = 0;
}
gz_error(state, Z_OK, NULL);
}
@@ -637,9 +715,11 @@ void ZLIB_INTERNAL gz_error(state, err, msg)
#endif
{
/* free previously allocated message and clear */
if (state->msg != NULL) {
if (state->msg != NULL)
{
if (state->err != Z_MEM_ERROR)
free(state->msg);
state->msg = NULL;
}
@@ -649,21 +729,25 @@ void ZLIB_INTERNAL gz_error(state, err, msg)
/* set error code, and if no message, then done */
state->err = err;
if (msg == NULL)
return;
/* for an out of memory error, save as static string */
if (err == Z_MEM_ERROR) {
if (err == Z_MEM_ERROR)
{
state->msg = (char*)msg;
return;
}
/* construct error message with path */
if ((state->msg = malloc(strlen(state->path) + strlen(msg) + 3)) == NULL) {
if ((state->msg = malloc(strlen(state->path) + strlen(msg) + 3)) == NULL)
{
state->err = Z_MEM_ERROR;
state->msg = (char*)"out of memory";
return;
}
strcpy(state->msg, state->path);
strcat(state->msg, ": ");
strcat(state->msg, msg);
@@ -680,11 +764,15 @@ unsigned ZLIB_INTERNAL gz_intmax()
unsigned p, q;
p = 1;
do {
do
{
q = p;
p <<= 1;
p++;
} while (p > q);
}
while (p > q);
return q >> 1;
}
#endif

186
extern/zlib/gzread.c vendored
View File

@@ -30,18 +30,27 @@ local int gz_load(state, buf, len, have)
int ret;
*have = 0;
do {
do
{
ret = read(state->fd, buf + *have, len - *have);
if (ret <= 0)
break;
*have += ret;
} while (*have < len);
if (ret < 0) {
}
while (*have < len);
if (ret < 0)
{
gz_error(state, Z_ERRNO, zstrerror());
return -1;
}
if (ret == 0)
state->eof = 1;
return 0;
}
@@ -64,20 +73,29 @@ local int gz_avail(state)
if (state->err != Z_OK && state->err != Z_BUF_ERROR)
return -1;
if (state->eof == 0) {
if (strm->avail_in) { /* copy what's there to the start */
if (state->eof == 0)
{
if (strm->avail_in) /* copy what's there to the start */
{
unsigned char* p = state->in, *q = strm->next_in;
unsigned n = strm->avail_in;
do {
do
{
*p++ = *q++;
} while (--n);
}
while (--n);
}
if (gz_load(state, state->in + strm->avail_in,
state->size - strm->avail_in, &got) == -1)
return -1;
strm->avail_in += got;
strm->next_in = state->in;
}
return 0;
}
@@ -100,18 +118,24 @@ local int gz_look(state)
z_streamp strm = &(state->strm);
/* allocate read buffers and inflate memory */
if (state->size == 0) {
if (state->size == 0)
{
/* allocate buffers */
state->in = malloc(state->want);
state->out = malloc(state->want << 1);
if (state->in == NULL || state->out == NULL) {
if (state->in == NULL || state->out == NULL)
{
if (state->out != NULL)
free(state->out);
if (state->in != NULL)
free(state->in);
gz_error(state, Z_MEM_ERROR, "out of memory");
return -1;
}
state->size = state->want;
/* allocate inflate memory */
@@ -120,7 +144,9 @@ local int gz_look(state)
state->strm.opaque = Z_NULL;
state->strm.avail_in = 0;
state->strm.next_in = Z_NULL;
if (inflateInit2(&(state->strm), 15 + 16) != Z_OK) { /* gunzip */
if (inflateInit2(&(state->strm), 15 + 16) != Z_OK) /* gunzip */
{
free(state->out);
free(state->in);
state->size = 0;
@@ -130,9 +156,11 @@ local int gz_look(state)
}
/* get at least the magic bytes in the input buffer */
if (strm->avail_in < 2) {
if (strm->avail_in < 2)
{
if (gz_avail(state) == -1)
return -1;
if (strm->avail_in == 0)
return 0;
}
@@ -145,7 +173,8 @@ local int gz_look(state)
the header will be written in a single operation, so that reading a
single byte is sufficient indication that it is not a gzip file) */
if (strm->avail_in > 1 &&
strm->next_in[0] == 31 && strm->next_in[1] == 139) {
strm->next_in[0] == 31 && strm->next_in[1] == 139)
{
inflateReset(strm);
state->how = GZIP;
state->direct = 0;
@@ -154,7 +183,8 @@ local int gz_look(state)
/* no gzip header -- if we were decoding gzip before, then this is trailing
garbage. Ignore the trailing garbage and finish. */
if (state->direct == 0) {
if (state->direct == 0)
{
strm->avail_in = 0;
state->eof = 1;
state->x.have = 0;
@@ -165,11 +195,14 @@ local int gz_look(state)
the output buffer is larger than the input buffer, which also assures
space for gzungetc() */
state->x.next = state->out;
if (strm->avail_in) {
if (strm->avail_in)
{
memcpy(state->x.next, strm->next_in, strm->avail_in);
state->x.have = strm->avail_in;
strm->avail_in = 0;
}
state->how = COPY;
state->direct = 1;
return 0;
@@ -193,32 +226,43 @@ local int gz_decomp(state)
/* fill output buffer up to end of deflate stream */
had = strm->avail_out;
do {
do
{
/* get more input for inflate() */
if (strm->avail_in == 0 && gz_avail(state) == -1)
return -1;
if (strm->avail_in == 0) {
if (strm->avail_in == 0)
{
gz_error(state, Z_BUF_ERROR, "unexpected end of file");
break;
}
/* decompress and handle errors */
ret = inflate(strm, Z_NO_FLUSH);
if (ret == Z_STREAM_ERROR || ret == Z_NEED_DICT) {
if (ret == Z_STREAM_ERROR || ret == Z_NEED_DICT)
{
gz_error(state, Z_STREAM_ERROR,
"internal error: inflate stream corrupt");
return -1;
}
if (ret == Z_MEM_ERROR) {
if (ret == Z_MEM_ERROR)
{
gz_error(state, Z_MEM_ERROR, "out of memory");
return -1;
}
if (ret == Z_DATA_ERROR) { /* deflate stream invalid */
if (ret == Z_DATA_ERROR) /* deflate stream invalid */
{
gz_error(state, Z_DATA_ERROR,
strm->msg == NULL ? "compressed data error" : strm->msg);
return -1;
}
} while (strm->avail_out && ret != Z_STREAM_END);
}
while (strm->avail_out && ret != Z_STREAM_END);
/* update available output */
state->x.have = had - strm->avail_out;
@@ -247,27 +291,37 @@ local int gz_fetch(state)
{
z_streamp strm = &(state->strm);
do {
switch(state->how) {
do
{
switch (state->how)
{
case LOOK: /* -> LOOK, COPY (only if never GZIP), or GZIP */
if (gz_look(state) == -1)
return -1;
if (state->how == LOOK)
return 0;
break;
case COPY: /* -> COPY */
if (gz_load(state, state->out, state->size << 1, &(state->x.have))
== -1)
return -1;
state->x.next = state->out;
return 0;
case GZIP: /* -> GZIP or LOOK (if end of gzip stream) */
strm->avail_out = state->size << 1;
strm->next_out = state->out;
if (gz_decomp(state) == -1)
return -1;
}
} while (state->x.have == 0 && (!state->eof || strm->avail_in));
}
while (state->x.have == 0 && (!state->eof || strm->avail_in));
return 0;
}
@@ -284,8 +338,10 @@ local int gz_skip(state, len)
/* skip over len bytes or reach end-of-file, whichever comes first */
while (len)
/* skip over whatever is in output buffer */
if (state->x.have) {
if (state->x.have)
{
n = GT_OFF(state->x.have) || (z_off64_t)state->x.have > len ?
(unsigned)len : state->x.have;
state->x.have -= n;
@@ -299,11 +355,13 @@ local int gz_skip(state, len)
break;
/* need more data to skip -- load up output buffer */
else {
else
{
/* get more output, looking for header if required */
if (gz_fetch(state) == -1)
return -1;
}
return 0;
}
@@ -324,6 +382,7 @@ int ZEXPORT gzread(file, buf, len)
/* get internal structure */
if (file == NULL)
return -1;
state = (gz_statep)file;
strm = &(state->strm);
@@ -334,7 +393,8 @@ int ZEXPORT gzread(file, buf, len)
/* since an int is returned, make sure len fits in one, otherwise return
with an error (this avoids the flaw in the interface) */
if ((int)len < 0) {
if ((int)len < 0)
{
gz_error(state, Z_DATA_ERROR, "requested length does not fit in int");
return -1;
}
@@ -344,17 +404,22 @@ int ZEXPORT gzread(file, buf, len)
return 0;
/* process a skip request */
if (state->seek) {
if (state->seek)
{
state->seek = 0;
if (gz_skip(state, state->skip) == -1)
return -1;
}
/* get len bytes to buf, or less than len if at the end */
got = 0;
do {
do
{
/* first just try copying data from the output buffer */
if (state->x.have) {
if (state->x.have)
{
n = state->x.have > len ? len : state->x.have;
memcpy(buf, state->x.next, n);
state->x.next += n;
@@ -362,34 +427,41 @@ int ZEXPORT gzread(file, buf, len)
}
/* output buffer empty -- return if we're at the end of the input */
else if (state->eof && strm->avail_in == 0) {
else if (state->eof && strm->avail_in == 0)
{
state->past = 1; /* tried to read past end */
break;
}
/* need output data -- for small len or new stream load up our output
buffer */
else if (state->how == LOOK || len < (state->size << 1)) {
else if (state->how == LOOK || len < (state->size << 1))
{
/* get more output, looking for header if required */
if (gz_fetch(state) == -1)
return -1;
continue; /* no progress yet -- go back to copy above */
/* the copy above assures that we will leave with space in the
output buffer, allowing at least one gzungetc() to succeed */
}
/* large len -- read directly into user buffer */
else if (state->how == COPY) { /* read directly */
else if (state->how == COPY) /* read directly */
{
if (gz_load(state, buf, len, &n) == -1)
return -1;
}
/* large len -- decompress directly into user buffer */
else { /* state->how == GZIP */
else /* state->how == GZIP */
{
strm->avail_out = len;
strm->next_out = buf;
if (gz_decomp(state) == -1)
return -1;
n = state->x.have;
state->x.have = 0;
}
@@ -399,7 +471,8 @@ int ZEXPORT gzread(file, buf, len)
buf = (char*)buf + n;
got += n;
state->x.pos += n;
} while (len);
}
while (len);
/* return number of bytes read into user buffer (will fit in int) */
return (int)got;
@@ -421,6 +494,7 @@ int ZEXPORT gzgetc(file)
/* get internal structure */
if (file == NULL)
return -1;
state = (gz_statep)file;
/* check that we're reading and that there's no (serious) error */
@@ -429,7 +503,8 @@ int ZEXPORT gzgetc(file)
return -1;
/* try output buffer (no need to check for skip request) */
if (state->x.have) {
if (state->x.have)
{
state->x.have--;
state->x.pos++;
return *(state->x.next)++;
@@ -464,6 +539,7 @@ int ZEXPORT gzungetc(c, file)
/* get internal structure */
if (file == NULL)
return -1;
state = (gz_statep)file;
/* check that we're reading and that there's no (serious) error */
@@ -472,8 +548,10 @@ int ZEXPORT gzungetc(c, file)
return -1;
/* process a skip request */
if (state->seek) {
if (state->seek)
{
state->seek = 0;
if (gz_skip(state, state->skip) == -1)
return -1;
}
@@ -483,7 +561,8 @@ int ZEXPORT gzungetc(c, file)
return -1;
/* if output buffer empty, put byte at end (allows more pushing) */
if (state->x.have == 0) {
if (state->x.have == 0)
{
state->x.have = 1;
state->x.next = state->out + (state->size << 1) - 1;
state->x.next[0] = c;
@@ -493,19 +572,24 @@ int ZEXPORT gzungetc(c, file)
}
/* if no room, give up (must have already done a gzungetc()) */
if (state->x.have == (state->size << 1)) {
if (state->x.have == (state->size << 1))
{
gz_error(state, Z_DATA_ERROR, "out of room to push characters");
return -1;
}
/* slide output data if needed and insert byte before existing data */
if (state->x.next == state->out) {
if (state->x.next == state->out)
{
unsigned char* src = state->out + state->x.have;
unsigned char* dest = state->out + (state->size << 1);
while (src > state->out)
*--dest = *--src;
state->x.next = dest;
}
state->x.have++;
state->x.next--;
state->x.next[0] = c;
@@ -532,6 +616,7 @@ char * ZEXPORT gzgets(file, buf, len)
/* check parameters and get internal structure */
if (file == NULL || buf == NULL || len < 1)
return NULL;
state = (gz_statep)file;
/* check that we're reading and that there's no (serious) error */
@@ -540,8 +625,10 @@ char * ZEXPORT gzgets(file, buf, len)
return NULL;
/* process a skip request */
if (state->seek) {
if (state->seek)
{
state->seek = 0;
if (gz_skip(state, state->skip) == -1)
return NULL;
}
@@ -551,11 +638,15 @@ char * ZEXPORT gzgets(file, buf, len)
the contents, let the user worry about that) */
str = buf;
left = (unsigned)len - 1;
if (left) do {
if (left) do
{
/* assure that something is in the output buffer */
if (state->x.have == 0 && gz_fetch(state) == -1)
return NULL; /* error */
if (state->x.have == 0) { /* end of file */
if (state->x.have == 0) /* end of file */
{
state->past = 1; /* read past end */
break; /* return what we have */
}
@@ -563,6 +654,7 @@ char * ZEXPORT gzgets(file, buf, len)
/* look for end-of-line in current output buffer */
n = state->x.have > left ? left : state->x.have;
eol = memchr(state->x.next, '\n', n);
if (eol != NULL)
n = (unsigned)(eol - state->x.next) + 1;
@@ -573,11 +665,13 @@ char * ZEXPORT gzgets(file, buf, len)
state->x.pos += n;
left -= n;
buf += n;
} while (left && eol == NULL);
}
while (left && eol == NULL);
/* return terminated string, or if nothing, end of file */
if (buf == str)
return NULL;
buf[0] = 0;
return str;
}
@@ -595,6 +689,7 @@ int ZEXPORT gzdirect(file)
/* get internal structure */
if (file == NULL)
return 0;
state = (gz_statep)file;
/* if the state is not known, but we can find out, then do so (this is
@@ -620,6 +715,7 @@ int ZEXPORT gzclose_r(file)
/* get internal structure */
if (file == NULL)
return Z_STREAM_ERROR;
state = (gz_statep)file;
/* check that we're reading */
@@ -627,11 +723,13 @@ int ZEXPORT gzclose_r(file)
return Z_STREAM_ERROR;
/* free memory and close file */
if (state->size) {
if (state->size)
{
inflateEnd(&(state->strm));
free(state->out);
free(state->in);
}
err = state->err == Z_BUF_ERROR ? Z_BUF_ERROR : Z_OK;
gz_error(state, Z_OK, NULL);
free(state->path);

144
extern/zlib/gzwrite.c vendored
View File

@@ -24,16 +24,21 @@ local int gz_init(state)
/* allocate input buffer */
state->in = malloc(state->want);
if (state->in == NULL) {
if (state->in == NULL)
{
gz_error(state, Z_MEM_ERROR, "out of memory");
return -1;
}
/* only need output buffer and deflate state if compressing */
if (!state->direct) {
if (!state->direct)
{
/* allocate output buffer */
state->out = malloc(state->want);
if (state->out == NULL) {
if (state->out == NULL)
{
free(state->in);
gz_error(state, Z_MEM_ERROR, "out of memory");
return -1;
@@ -45,7 +50,9 @@ local int gz_init(state)
strm->opaque = Z_NULL;
ret = deflateInit2(strm, state->level, Z_DEFLATED,
MAX_WBITS + 16, DEF_MEM_LEVEL, state->strategy);
if (ret != Z_OK) {
if (ret != Z_OK)
{
free(state->out);
free(state->in);
gz_error(state, Z_MEM_ERROR, "out of memory");
@@ -57,11 +64,13 @@ local int gz_init(state)
state->size = state->want;
/* initialize write buffer if compressing */
if (!state->direct) {
if (!state->direct)
{
strm->avail_out = state->size;
strm->next_out = state->out;
state->x.next = strm->next_out;
}
return 0;
}
@@ -88,46 +97,62 @@ local int gz_comp(state, flush)
return -1;
/* write directly if requested */
if (state->direct) {
if (state->direct)
{
got = write(state->fd, strm->next_in, strm->avail_in);
if (got < 0 || (unsigned)got != strm->avail_in) {
if (got < 0 || (unsigned)got != strm->avail_in)
{
gz_error(state, Z_ERRNO, zstrerror());
return -1;
}
strm->avail_in = 0;
return 0;
}
/* run deflate() on provided input until it produces no more output */
ret = Z_OK;
do {
do
{
/* write out current buffer contents if full, or if flushing, but if
doing Z_FINISH then don't write until we get to Z_STREAM_END */
if (strm->avail_out == 0 || (flush != Z_NO_FLUSH &&
(flush != Z_FINISH || ret == Z_STREAM_END))) {
(flush != Z_FINISH || ret == Z_STREAM_END)))
{
have = (unsigned)(strm->next_out - state->x.next);
if (have && ((got = write(state->fd, state->x.next, have)) < 0 ||
(unsigned)got != have)) {
(unsigned)got != have))
{
gz_error(state, Z_ERRNO, zstrerror());
return -1;
}
if (strm->avail_out == 0) {
if (strm->avail_out == 0)
{
strm->avail_out = state->size;
strm->next_out = state->out;
}
state->x.next = strm->next_out;
}
/* compress */
have = strm->avail_out;
ret = deflate(strm, flush);
if (ret == Z_STREAM_ERROR) {
if (ret == Z_STREAM_ERROR)
{
gz_error(state, Z_STREAM_ERROR,
"internal error: deflate stream corrupt");
return -1;
}
have -= strm->avail_out;
} while (have);
}
while (have);
/* if that completed a deflate stream, allow another to start */
if (flush == Z_FINISH)
@@ -156,20 +181,28 @@ local int gz_zero(state, len)
/* compress len zeros (len guaranteed > 0) */
first = 1;
while (len) {
while (len)
{
n = GT_OFF(state->size) || (z_off64_t)state->size > len ?
(unsigned)len : state->size;
if (first) {
if (first)
{
memset(state->in, 0, n);
first = 0;
}
strm->avail_in = n;
strm->next_in = state->in;
state->x.pos += n;
if (gz_comp(state, Z_NO_FLUSH) == -1)
return -1;
len -= n;
}
return 0;
}
@@ -191,6 +224,7 @@ int ZEXPORT gzwrite(file, buf, len)
/* get internal structure */
if (file == NULL)
return 0;
state = (gz_statep)file;
strm = &(state->strm);
@@ -200,7 +234,8 @@ int ZEXPORT gzwrite(file, buf, len)
/* since an int is returned, make sure len fits in one, otherwise return
with an error (this avoids the flaw in the interface) */
if ((int)len < 0) {
if ((int)len < 0)
{
gz_error(state, Z_DATA_ERROR, "requested length does not fit in int");
return 0;
}
@@ -214,31 +249,41 @@ int ZEXPORT gzwrite(file, buf, len)
return 0;
/* check for seek request */
if (state->seek) {
if (state->seek)
{
state->seek = 0;
if (gz_zero(state, state->skip) == -1)
return 0;
}
/* for small len, copy to input buffer, otherwise compress directly */
if (len < state->size) {
if (len < state->size)
{
/* copy to input buffer, compress when full */
do {
do
{
if (strm->avail_in == 0)
strm->next_in = state->in;
n = state->size - strm->avail_in;
if (n > len)
n = len;
memcpy(strm->next_in + strm->avail_in, buf, n);
strm->avail_in += n;
state->x.pos += n;
buf = (char*)buf + n;
len -= n;
if (len && gz_comp(state, Z_NO_FLUSH) == -1)
return 0;
} while (len);
}
else {
while (len);
}
else
{
/* consume whatever's left in the input buffer */
if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1)
return 0;
@@ -247,6 +292,7 @@ int ZEXPORT gzwrite(file, buf, len)
strm->avail_in = len;
strm->next_in = (voidp)buf;
state->x.pos += len;
if (gz_comp(state, Z_NO_FLUSH) == -1)
return 0;
}
@@ -271,6 +317,7 @@ int ZEXPORT gzputc(file, c)
/* get internal structure */
if (file == NULL)
return -1;
state = (gz_statep)file;
strm = &(state->strm);
@@ -279,17 +326,21 @@ int ZEXPORT gzputc(file, c)
return -1;
/* check for seek request */
if (state->seek) {
if (state->seek)
{
state->seek = 0;
if (gz_zero(state, state->skip) == -1)
return -1;
}
/* try writing to input buffer for speed (state->size == 0 if buffer not
initialized) */
if (strm->avail_in < state->size) {
if (strm->avail_in < state->size)
{
if (strm->avail_in == 0)
strm->next_in = state->in;
strm->next_in[strm->avail_in++] = c;
state->x.pos++;
return c & 0xff;
@@ -297,8 +348,10 @@ int ZEXPORT gzputc(file, c)
/* no room in buffer or not initialized, use gz_write() */
buf[0] = c;
if (gzwrite(file, buf, 1) != 1)
return -1;
return c & 0xff;
}
@@ -334,6 +387,7 @@ int ZEXPORTVA gzprintf (gzFile file, const char *format, ...)
/* get internal structure */
if (file == NULL)
return -1;
state = (gz_statep)file;
strm = &(state->strm);
@@ -346,8 +400,10 @@ int ZEXPORTVA gzprintf (gzFile file, const char *format, ...)
return 0;
/* check for seek request */
if (state->seek) {
if (state->seek)
{
state->seek = 0;
if (gz_zero(state, state->skip) == -1)
return 0;
}
@@ -364,8 +420,10 @@ int ZEXPORTVA gzprintf (gzFile file, const char *format, ...)
# ifdef HAS_vsprintf_void
(void)vsprintf((char*)(state->in), format, va);
va_end(va);
for (len = 0; len < size; len++)
if (state->in[len] == 0) break;
# else
len = vsprintf((char*)(state->in), format, va);
va_end(va);
@@ -409,6 +467,7 @@ int ZEXPORTVA gzprintf (file, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10,
/* get internal structure */
if (file == NULL)
return -1;
state = (gz_statep)file;
strm = &(state->strm);
@@ -425,8 +484,10 @@ int ZEXPORTVA gzprintf (file, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10,
return 0;
/* check for seek request */
if (state->seek) {
if (state->seek)
{
state->seek = 0;
if (gz_zero(state, state->skip) == -1)
return 0;
}
@@ -442,8 +503,10 @@ int ZEXPORTVA gzprintf (file, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10,
# ifdef HAS_sprintf_void
sprintf((char*)(state->in), format, a1, a2, a3, a4, a5, a6, a7, a8,
a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
for (len = 0; len < size; len++)
if (state->in[len] == 0) break;
# else
len = sprintf((char*)(state->in), format, a1, a2, a3, a4, a5, a6, a7, a8,
a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
@@ -487,6 +550,7 @@ int ZEXPORT gzflush(file, flush)
/* get internal structure */
if (file == NULL)
return -1;
state = (gz_statep)file;
/* check that we're writing and that there's no error */
@@ -498,8 +562,10 @@ int ZEXPORT gzflush(file, flush)
return Z_STREAM_ERROR;
/* check for seek request */
if (state->seek) {
if (state->seek)
{
state->seek = 0;
if (gz_zero(state, state->skip) == -1)
return -1;
}
@@ -525,6 +591,7 @@ int ZEXPORT gzsetparams(file, level, strategy)
/* get internal structure */
if (file == NULL)
return Z_STREAM_ERROR;
state = (gz_statep)file;
strm = &(state->strm);
@@ -537,19 +604,24 @@ int ZEXPORT gzsetparams(file, level, strategy)
return Z_OK;
/* check for seek request */
if (state->seek) {
if (state->seek)
{
state->seek = 0;
if (gz_zero(state, state->skip) == -1)
return -1;
}
/* change compression parameters for subsequent input */
if (state->size) {
if (state->size)
{
/* flush previous input with previous parameters before changing */
if (strm->avail_in && gz_comp(state, Z_PARTIAL_FLUSH) == -1)
return state->err;
deflateParams(strm, level, strategy);
}
state->level = level;
state->strategy = strategy;
return Z_OK;
@@ -569,6 +641,7 @@ int ZEXPORT gzclose_w(file)
/* get internal structure */
if (file == NULL)
return Z_STREAM_ERROR;
state = (gz_statep)file;
/* check that we're writing */
@@ -576,26 +649,35 @@ int ZEXPORT gzclose_w(file)
return Z_STREAM_ERROR;
/* check for seek request */
if (state->seek) {
if (state->seek)
{
state->seek = 0;
if (gz_zero(state, state->skip) == -1)
ret = state->err;
}
/* flush, free memory, and close file */
if (state->size) {
if (state->size)
{
if (gz_comp(state, Z_FINISH) == -1)
ret = state->err;
if (!state->direct) {
if (!state->direct)
{
(void)deflateEnd(&(state->strm));
free(state->out);
}
free(state->in);
}
gz_error(state, Z_OK, NULL);
free(state->path);
if (close(state->fd) == -1)
ret = Z_ERRNO;
free(state);
return ret;
}

211
extern/zlib/infback.c vendored
View File

@@ -58,11 +58,15 @@ int stream_size;
if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
stream_size != (int)(sizeof(z_stream)))
return Z_VERSION_ERROR;
if (strm == Z_NULL || window == Z_NULL ||
windowBits < 8 || windowBits > 15)
return Z_STREAM_ERROR;
strm->msg = Z_NULL; /* in case we return an error */
if (strm->zalloc == (alloc_func)0) {
if (strm->zalloc == (alloc_func)0)
{
#ifdef Z_SOLO
return Z_STREAM_ERROR;
#else
@@ -70,15 +74,19 @@ int stream_size;
strm->opaque = (voidpf)0;
#endif
}
if (strm->zfree == (free_func)0)
#ifdef Z_SOLO
return Z_STREAM_ERROR;
#else
strm->zfree = zcfree;
#endif
state = (struct inflate_state FAR*)ZALLOC(strm, 1,
sizeof(struct inflate_state));
if (state == Z_NULL) return Z_MEM_ERROR;
Tracev((stderr, "inflate: allocated\n"));
strm->state = (struct internal_state FAR*)state;
state->dmax = 32768U;
@@ -113,16 +121,22 @@ struct inflate_state FAR *state;
static code fixed[544];
/* build fixed huffman tables if first call (may not be thread safe) */
if (virgin) {
if (virgin)
{
unsigned sym, bits;
static code* next;
/* literal/length table */
sym = 0;
while (sym < 144) state->lens[sym++] = 8;
while (sym < 256) state->lens[sym++] = 9;
while (sym < 280) state->lens[sym++] = 7;
while (sym < 288) state->lens[sym++] = 8;
next = fixed;
lenfix = next;
bits = 9;
@@ -130,7 +144,9 @@ struct inflate_state FAR *state;
/* distance table */
sym = 0;
while (sym < 32) state->lens[sym++] = 5;
distfix = next;
bits = 5;
inflate_table(DISTS, state->lens, 32, &(next), &(bits), state->work);
@@ -138,6 +154,7 @@ struct inflate_state FAR *state;
/* do this just once */
virgin = 0;
}
#else /* !BUILDFIXED */
# include "inffixed.h"
#endif /* BUILDFIXED */
@@ -301,6 +318,7 @@ void FAR *out_desc;
/* Check that the strm exists and that the state was initialized */
if (strm == Z_NULL || strm->state == Z_NULL)
return Z_STREAM_ERROR;
state = (struct inflate_state FAR*)strm->state;
/* Reset the state */
@@ -317,38 +335,48 @@ void FAR *out_desc;
/* Inflate until end of block marked as last */
for (;;)
switch (state->mode) {
switch (state->mode)
{
case TYPE:
/* determine and dispatch block type */
if (state->last) {
if (state->last)
{
BYTEBITS();
state->mode = DONE;
break;
}
NEEDBITS(3);
state->last = BITS(1);
DROPBITS(1);
switch (BITS(2)) {
switch (BITS(2))
{
case 0: /* stored block */
Tracev((stderr, "inflate: stored block%s\n",
state->last ? " (last)" : ""));
state->mode = STORED;
break;
case 1: /* fixed block */
fixedtables(state);
Tracev((stderr, "inflate: fixed codes block%s\n",
state->last ? " (last)" : ""));
state->mode = LEN; /* decode codes */
break;
case 2: /* dynamic block */
Tracev((stderr, "inflate: dynamic codes block%s\n",
state->last ? " (last)" : ""));
state->mode = TABLE;
break;
case 3:
strm->msg = (char*)"invalid block type";
state->mode = BAD;
}
DROPBITS(2);
break;
@@ -356,23 +384,30 @@ void FAR *out_desc;
/* get and verify stored block length */
BYTEBITS(); /* go to byte boundary */
NEEDBITS(32);
if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) {
if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff))
{
strm->msg = (char*)"invalid stored block lengths";
state->mode = BAD;
break;
}
state->length = (unsigned)hold & 0xffff;
Tracev((stderr, "inflate: stored length %u\n",
state->length));
INITBITS();
/* copy stored block from input to output */
while (state->length != 0) {
while (state->length != 0)
{
copy = state->length;
PULL();
ROOM();
if (copy > have) copy = have;
if (copy > left) copy = left;
zmemcpy(put, next, copy);
have -= copy;
next += copy;
@@ -380,6 +415,7 @@ void FAR *out_desc;
put += copy;
state->length -= copy;
}
Tracev((stderr, "inflate: stored end\n"));
state->mode = TYPE;
break;
@@ -394,79 +430,106 @@ void FAR *out_desc;
state->ncode = BITS(4) + 4;
DROPBITS(4);
#ifndef PKZIP_BUG_WORKAROUND
if (state->nlen > 286 || state->ndist > 30) {
if (state->nlen > 286 || state->ndist > 30)
{
strm->msg = (char*)"too many length or distance symbols";
state->mode = BAD;
break;
}
#endif
Tracev((stderr, "inflate: table sizes ok\n"));
/* get code length code lengths (not a typo) */
state->have = 0;
while (state->have < state->ncode) {
while (state->have < state->ncode)
{
NEEDBITS(3);
state->lens[order[state->have++]] = (unsigned short)BITS(3);
DROPBITS(3);
}
while (state->have < 19)
state->lens[order[state->have++]] = 0;
state->next = state->codes;
state->lencode = (code const FAR*)(state->next);
state->lenbits = 7;
ret = inflate_table(CODES, state->lens, 19, &(state->next),
&(state->lenbits), state->work);
if (ret) {
if (ret)
{
strm->msg = (char*)"invalid code lengths set";
state->mode = BAD;
break;
}
Tracev((stderr, "inflate: code lengths ok\n"));
/* get length and distance code code lengths */
state->have = 0;
while (state->have < state->nlen + state->ndist) {
for (;;) {
while (state->have < state->nlen + state->ndist)
{
for (;;)
{
here = state->lencode[BITS(state->lenbits)];
if ((unsigned)(here.bits) <= bits) break;
PULLBYTE();
}
if (here.val < 16) {
if (here.val < 16)
{
DROPBITS(here.bits);
state->lens[state->have++] = here.val;
}
else {
if (here.val == 16) {
else
{
if (here.val == 16)
{
NEEDBITS(here.bits + 2);
DROPBITS(here.bits);
if (state->have == 0) {
if (state->have == 0)
{
strm->msg = (char*)"invalid bit length repeat";
state->mode = BAD;
break;
}
len = (unsigned)(state->lens[state->have - 1]);
copy = 3 + BITS(2);
DROPBITS(2);
}
else if (here.val == 17) {
else if (here.val == 17)
{
NEEDBITS(here.bits + 3);
DROPBITS(here.bits);
len = 0;
copy = 3 + BITS(3);
DROPBITS(3);
}
else {
else
{
NEEDBITS(here.bits + 7);
DROPBITS(here.bits);
len = 0;
copy = 11 + BITS(7);
DROPBITS(7);
}
if (state->have + copy > state->nlen + state->ndist) {
if (state->have + copy > state->nlen + state->ndist)
{
strm->msg = (char*)"invalid bit length repeat";
state->mode = BAD;
break;
}
while (copy--)
state->lens[state->have++] = (unsigned short)len;
}
@@ -476,7 +539,8 @@ void FAR *out_desc;
if (state->mode == BAD) break;
/* check for end-of-block code (better have one) */
if (state->lens[256] == 0) {
if (state->lens[256] == 0)
{
strm->msg = (char*)"invalid code -- missing end-of-block";
state->mode = BAD;
break;
@@ -490,55 +554,77 @@ void FAR *out_desc;
state->lenbits = 9;
ret = inflate_table(LENS, state->lens, state->nlen, &(state->next),
&(state->lenbits), state->work);
if (ret) {
if (ret)
{
strm->msg = (char*)"invalid literal/lengths set";
state->mode = BAD;
break;
}
state->distcode = (code const FAR*)(state->next);
state->distbits = 6;
ret = inflate_table(DISTS, state->lens + state->nlen, state->ndist,
&(state->next), &(state->distbits), state->work);
if (ret) {
if (ret)
{
strm->msg = (char*)"invalid distances set";
state->mode = BAD;
break;
}
Tracev((stderr, "inflate: codes ok\n"));
state->mode = LEN;
case LEN:
/* use inflate_fast() if we have enough input and output */
if (have >= 6 && left >= 258) {
if (have >= 6 && left >= 258)
{
RESTORE();
if (state->whave < state->wsize)
state->whave = state->wsize - left;
inflate_fast(strm, state->wsize);
LOAD();
break;
}
/* get a literal, length, or end-of-block code */
for (;;) {
for (;;)
{
here = state->lencode[BITS(state->lenbits)];
if ((unsigned)(here.bits) <= bits) break;
PULLBYTE();
}
if (here.op && (here.op & 0xf0) == 0) {
if (here.op && (here.op & 0xf0) == 0)
{
last = here;
for (;;) {
for (;;)
{
here = state->lencode[last.val +
(BITS(last.bits + last.op) >> last.bits)];
if ((unsigned)(last.bits + here.bits) <= bits) break;
PULLBYTE();
}
DROPBITS(last.bits);
}
DROPBITS(here.bits);
state->length = (unsigned)here.val;
/* process literal */
if (here.op == 0) {
if (here.op == 0)
{
Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
"inflate: literal '%c'\n" :
"inflate: literal 0x%02x\n", here.val));
@@ -550,14 +636,16 @@ void FAR *out_desc;
}
/* process end of block */
if (here.op & 32) {
if (here.op & 32)
{
Tracevv((stderr, "inflate: end of block\n"));
state->mode = TYPE;
break;
}
/* invalid code */
if (here.op & 64) {
if (here.op & 64)
{
strm->msg = (char*)"invalid literal/length code";
state->mode = BAD;
break;
@@ -565,80 +653,116 @@ void FAR *out_desc;
/* length code -- get extra bits, if any */
state->extra = (unsigned)(here.op) & 15;
if (state->extra != 0) {
if (state->extra != 0)
{
NEEDBITS(state->extra);
state->length += BITS(state->extra);
DROPBITS(state->extra);
}
Tracevv((stderr, "inflate: length %u\n", state->length));
/* get distance code */
for (;;) {
for (;;)
{
here = state->distcode[BITS(state->distbits)];
if ((unsigned)(here.bits) <= bits) break;
PULLBYTE();
}
if ((here.op & 0xf0) == 0) {
if ((here.op & 0xf0) == 0)
{
last = here;
for (;;) {
for (;;)
{
here = state->distcode[last.val +
(BITS(last.bits + last.op) >> last.bits)];
if ((unsigned)(last.bits + here.bits) <= bits) break;
PULLBYTE();
}
DROPBITS(last.bits);
}
DROPBITS(here.bits);
if (here.op & 64) {
if (here.op & 64)
{
strm->msg = (char*)"invalid distance code";
state->mode = BAD;
break;
}
state->offset = (unsigned)here.val;
/* get distance extra bits, if any */
state->extra = (unsigned)(here.op) & 15;
if (state->extra != 0) {
if (state->extra != 0)
{
NEEDBITS(state->extra);
state->offset += BITS(state->extra);
DROPBITS(state->extra);
}
if (state->offset > state->wsize - (state->whave < state->wsize ?
left : 0)) {
left : 0))
{
strm->msg = (char*)"invalid distance too far back";
state->mode = BAD;
break;
}
Tracevv((stderr, "inflate: distance %u\n", state->offset));
/* copy match from window to output */
do {
do
{
ROOM();
copy = state->wsize - state->offset;
if (copy < left) {
if (copy < left)
{
from = put + copy;
copy = left - copy;
}
else {
else
{
from = put - state->offset;
copy = left;
}
if (copy > state->length) copy = state->length;
state->length -= copy;
left -= copy;
do {
do
{
*put++ = *from++;
} while (--copy);
} while (state->length != 0);
}
while (--copy);
}
while (state->length != 0);
break;
case DONE:
/* inflate stream terminated properly -- write leftover output */
ret = Z_STREAM_END;
if (left < state->wsize) {
if (left < state->wsize)
{
if (out(out_desc, state->window, state->wsize - left))
ret = Z_BUF_ERROR;
}
goto inf_leave;
case BAD:
@@ -666,6 +790,7 @@ z_streamp strm;
{
if (strm == Z_NULL || strm->state == Z_NULL || strm->zfree == (free_func)0)
return Z_STREAM_ERROR;
ZFREE(strm, strm->state);
strm->state = Z_NULL;
Tracev((stderr, "inflate: end\n"));

191
extern/zlib/inffast.c vendored
View File

@@ -121,194 +121,287 @@ unsigned start; /* inflate()'s starting value for strm->avail_out */
/* decode literals and length/distances until end-of-block or not enough
input data or output space */
do {
if (bits < 15) {
do
{
if (bits < 15)
{
hold += (unsigned long)(PUP(in)) << bits;
bits += 8;
hold += (unsigned long)(PUP(in)) << bits;
bits += 8;
}
here = lcode[hold & lmask];
dolen:
op = (unsigned)(here.bits);
hold >>= op;
bits -= op;
op = (unsigned)(here.op);
if (op == 0) { /* literal */
if (op == 0) /* literal */
{
Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
"inflate: literal '%c'\n" :
"inflate: literal 0x%02x\n", here.val));
PUP(out) = (unsigned char)(here.val);
}
else if (op & 16) { /* length base */
else if (op & 16) /* length base */
{
len = (unsigned)(here.val);
op &= 15; /* number of extra bits */
if (op) {
if (bits < op) {
if (op)
{
if (bits < op)
{
hold += (unsigned long)(PUP(in)) << bits;
bits += 8;
}
len += (unsigned)hold & ((1U << op) - 1);
hold >>= op;
bits -= op;
}
Tracevv((stderr, "inflate: length %u\n", len));
if (bits < 15) {
if (bits < 15)
{
hold += (unsigned long)(PUP(in)) << bits;
bits += 8;
hold += (unsigned long)(PUP(in)) << bits;
bits += 8;
}
here = dcode[hold & dmask];
dodist:
op = (unsigned)(here.bits);
hold >>= op;
bits -= op;
op = (unsigned)(here.op);
if (op & 16) { /* distance base */
if (op & 16) /* distance base */
{
dist = (unsigned)(here.val);
op &= 15; /* number of extra bits */
if (bits < op) {
if (bits < op)
{
hold += (unsigned long)(PUP(in)) << bits;
bits += 8;
if (bits < op) {
if (bits < op)
{
hold += (unsigned long)(PUP(in)) << bits;
bits += 8;
}
}
dist += (unsigned)hold & ((1U << op) - 1);
#ifdef INFLATE_STRICT
if (dist > dmax) {
if (dist > dmax)
{
strm->msg = (char*)"invalid distance too far back";
state->mode = BAD;
break;
}
#endif
hold >>= op;
bits -= op;
Tracevv((stderr, "inflate: distance %u\n", dist));
op = (unsigned)(out - beg); /* max distance in output */
if (dist > op) { /* see if copy from window */
if (dist > op) /* see if copy from window */
{
op = dist - op; /* distance back in window */
if (op > whave) {
if (state->sane) {
if (op > whave)
{
if (state->sane)
{
strm->msg =
(char*)"invalid distance too far back";
state->mode = BAD;
break;
}
#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
if (len <= op - whave) {
do {
if (len <= op - whave)
{
do
{
PUP(out) = 0;
} while (--len);
}
while (--len);
continue;
}
len -= op - whave;
do {
do
{
PUP(out) = 0;
} while (--op > whave);
if (op == 0) {
}
while (--op > whave);
if (op == 0)
{
from = out - dist;
do {
do
{
PUP(out) = PUP(from);
} while (--len);
}
while (--len);
continue;
}
#endif
}
from = window - OFF;
if (wnext == 0) { /* very common case */
if (wnext == 0) /* very common case */
{
from += wsize - op;
if (op < len) { /* some from window */
if (op < len) /* some from window */
{
len -= op;
do {
do
{
PUP(out) = PUP(from);
} while (--op);
}
while (--op);
from = out - dist; /* rest from output */
}
}
else if (wnext < op) { /* wrap around window */
else if (wnext < op) /* wrap around window */
{
from += wsize + wnext - op;
op -= wnext;
if (op < len) { /* some from end of window */
if (op < len) /* some from end of window */
{
len -= op;
do {
do
{
PUP(out) = PUP(from);
} while (--op);
}
while (--op);
from = window - OFF;
if (wnext < len) { /* some from start of window */
if (wnext < len) /* some from start of window */
{
op = wnext;
len -= op;
do {
do
{
PUP(out) = PUP(from);
} while (--op);
}
while (--op);
from = out - dist; /* rest from output */
}
}
}
else { /* contiguous in window */
else /* contiguous in window */
{
from += wnext - op;
if (op < len) { /* some from window */
if (op < len) /* some from window */
{
len -= op;
do {
do
{
PUP(out) = PUP(from);
} while (--op);
}
while (--op);
from = out - dist; /* rest from output */
}
}
while (len > 2) {
while (len > 2)
{
PUP(out) = PUP(from);
PUP(out) = PUP(from);
PUP(out) = PUP(from);
len -= 3;
}
if (len) {
if (len)
{
PUP(out) = PUP(from);
if (len > 1)
PUP(out) = PUP(from);
}
}
else {
else
{
from = out - dist; /* copy direct from output */
do { /* minimum length is three */
do /* minimum length is three */
{
PUP(out) = PUP(from);
PUP(out) = PUP(from);
PUP(out) = PUP(from);
len -= 3;
} while (len > 2);
if (len) {
}
while (len > 2);
if (len)
{
PUP(out) = PUP(from);
if (len > 1)
PUP(out) = PUP(from);
}
}
}
else if ((op & 64) == 0) { /* 2nd level distance code */
else if ((op & 64) == 0) /* 2nd level distance code */
{
here = dcode[here.val + (hold & ((1U << op) - 1))];
goto dodist;
}
else {
else
{
strm->msg = (char*)"invalid distance code";
state->mode = BAD;
break;
}
}
else if ((op & 64) == 0) { /* 2nd level length code */
else if ((op & 64) == 0) /* 2nd level length code */
{
here = lcode[here.val + (hold & ((1U << op) - 1))];
goto dolen;
}
else if (op & 32) { /* end-of-block */
else if (op & 32) /* end-of-block */
{
Tracevv((stderr, "inflate: end of block\n"));
state->mode = TYPE;
break;
}
else {
else
{
strm->msg = (char*)"invalid literal/length code";
state->mode = BAD;
break;
}
} while (in < last && out < end);
}
while (in < last && out < end);
/* return unused bytes (on entry, bits < 8, so in won't go too far back) */
len = bits >> 3;

View File

@@ -7,7 +7,8 @@
subject to change. Applications should only use zlib.h.
*/
static const code lenfix[512] = {
static const code lenfix[512] =
{
{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}, {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}, {0, 8, 24}, {0, 9, 144}, {19, 7, 59},
@@ -84,7 +85,8 @@
{0, 9, 255}
};
static const code distfix[32] = {
static const code distfix[32] =
{
{16, 5, 1}, {23, 5, 257}, {19, 5, 17}, {27, 5, 4097}, {17, 5, 5}, {25, 5, 1025},
{21, 5, 65}, {29, 5, 16385}, {16, 5, 3}, {24, 5, 513}, {20, 5, 33}, {28, 5, 8193},
{18, 5, 9}, {26, 5, 2049}, {22, 5, 129}, {64, 5, 0}, {16, 5, 2}, {23, 5, 385},

474
extern/zlib/inflate.c vendored

File diff suppressed because it is too large Load Diff

View File

@@ -17,7 +17,8 @@
#endif
/* Possible inflate modes between inflate() calls */
typedef enum {
typedef enum
{
HEAD, /* i: waiting for magic header */
FLAGS, /* i: waiting for method and flags (gzip) */
TIME, /* i: waiting for modification time (gzip) */
@@ -78,7 +79,8 @@ typedef enum {
*/
/* state maintained between inflate() calls. Approximately 10K bytes. */
struct inflate_state {
struct inflate_state
{
inflate_mode mode; /* current inflate mode */
int last; /* true if processing last block */
int wrap; /* bit 0 true for zlib, bit 1 true for gzip */

View File

@@ -74,20 +74,28 @@ unsigned short FAR *work;
int end; /* use base and extra for symbol > end */
unsigned short count[MAXBITS + 1]; /* number of codes of each length */
unsigned short offs[MAXBITS + 1]; /* offsets in table for each length */
static const unsigned short lbase[31] = { /* Length codes 257..285 base */
static const unsigned short lbase[31] = /* Length codes 257..285 base */
{
3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
static const unsigned short lext[31] = { /* Length codes 257..285 extra */
35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0
};
static const unsigned short lext[31] = /* Length codes 257..285 extra */
{
16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18, 18,
19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 16, 78, 68};
static const unsigned short dbase[32] = { /* Distance codes 0..29 base */
19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 16, 78, 68
};
static const unsigned short dbase[32] = /* Distance codes 0..29 base */
{
1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
8193, 12289, 16385, 24577, 0, 0};
static const unsigned short dext[32] = { /* Distance codes 0..29 extra */
8193, 12289, 16385, 24577, 0, 0
};
static const unsigned short dext[32] = /* Distance codes 0..29 extra */
{
16, 16, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22,
23, 23, 24, 24, 25, 25, 26, 26, 27, 27,
28, 28, 29, 29, 64, 64};
28, 28, 29, 29, 64, 64
};
/*
Process a set of code lengths to create a canonical Huffman code. The
@@ -123,15 +131,20 @@ unsigned short FAR *work;
/* accumulate lengths for codes (assumes lens[] all in 0..MAXBITS) */
for (len = 0; len <= MAXBITS; len++)
count[len] = 0;
for (sym = 0; sym < codes; sym++)
count[lens[sym]]++;
/* bound code lengths, force root to be within code lengths */
root = *bits;
for (max = MAXBITS; max >= 1; max--)
if (count[max] != 0) break;
if (root > max) root = max;
if (max == 0) { /* no symbols to code at all */
if (max == 0) /* no symbols to code at all */
{
here.op = (unsigned char)64; /* invalid code marker */
here.bits = (unsigned char)1;
here.val = (unsigned short)0;
@@ -140,22 +153,29 @@ unsigned short FAR *work;
*bits = 1;
return 0; /* no symbols, but wait for decoding to report error */
}
for (min = 1; min < max; min++)
if (count[min] != 0) break;
if (root < min) root = min;
/* check for an over-subscribed or incomplete set of lengths */
left = 1;
for (len = 1; len <= MAXBITS; len++) {
for (len = 1; len <= MAXBITS; len++)
{
left <<= 1;
left -= count[len];
if (left < 0) return -1; /* over-subscribed */
}
if (left > 0 && (type == CODES || max != 1))
return -1; /* incomplete set */
/* generate offsets into symbol table for each length for sorting */
offs[1] = 0;
for (len = 1; len < MAXBITS; len++)
offs[len + 1] = offs[len] + count[len];
@@ -195,11 +215,13 @@ unsigned short FAR *work;
*/
/* set up for code type */
switch (type) {
switch (type)
{
case CODES:
base = extra = work; /* dummy value--not used */
end = 19;
break;
case LENS:
base = lbase;
base -= 257;
@@ -207,6 +229,7 @@ unsigned short FAR *work;
extra -= 257;
end = 256;
break;
default: /* DISTS */
base = dbase;
extra = dext;
@@ -230,18 +253,23 @@ unsigned short FAR *work;
return 1;
/* process all codes and make table entries */
for (;;) {
for (;;)
{
/* create table entry */
here.bits = (unsigned char)(len - drop);
if ((int)(work[sym]) < end) {
if ((int)(work[sym]) < end)
{
here.op = (unsigned char)0;
here.val = work[sym];
}
else if ((int)(work[sym]) > end) {
else if ((int)(work[sym]) > end)
{
here.op = (unsigned char)(extra[work[sym]]);
here.val = base[work[sym]];
}
else {
else
{
here.op = (unsigned char)(32 + 64); /* end of block */
here.val = 0;
}
@@ -250,16 +278,22 @@ unsigned short FAR *work;
incr = 1U << (len - drop);
fill = 1U << curr;
min = fill; /* save offset to next table */
do {
do
{
fill -= incr;
next[(huff >> drop) + fill] = here;
} while (fill != 0);
}
while (fill != 0);
/* backwards increment the len-bit code huff */
incr = 1U << (len - 1);
while (huff & incr)
incr >>= 1;
if (incr != 0) {
if (incr != 0)
{
huff &= incr - 1;
huff += incr;
}
@@ -268,13 +302,17 @@ unsigned short FAR *work;
/* go to next symbol, update count, len */
sym++;
if (--(count[len]) == 0) {
if (--(count[len]) == 0)
{
if (len == max) break;
len = lens[work[sym]];
}
/* create new sub-table if needed */
if (len > root && (huff & mask) != low) {
if (len > root && (huff & mask) != low)
{
/* if first time, transition to sub-tables */
if (drop == 0)
drop = root;
@@ -285,15 +323,20 @@ unsigned short FAR *work;
/* determine length of next table */
curr = len - drop;
left = (int)(1 << curr);
while (curr + drop < max) {
while (curr + drop < max)
{
left -= count[curr + drop];
if (left <= 0) break;
curr++;
left <<= 1;
}
/* check for enough space */
used += 1U << curr;
if ((type == LENS && used >= ENOUGH_LENS) ||
(type == DISTS && used >= ENOUGH_DISTS))
return 1;
@@ -309,7 +352,8 @@ unsigned short FAR *work;
/* fill in remaining table entry if code is incomplete (guaranteed to have
at most one remaining entry, since if the code is incomplete, the
maximum code length that was allowed to get this far is one bit) */
if (huff != 0) {
if (huff != 0)
{
here.op = (unsigned char)64; /* invalid code marker */
here.bits = (unsigned char)(len - drop);
here.val = (unsigned short)0;

View File

@@ -21,7 +21,8 @@
of the bit buffer. val is the actual byte to output in the case
of a literal, the base length or distance, or the offset from
the current table to the next table. Each entry is four bytes. */
typedef struct {
typedef struct
{
unsigned char op; /* operation, extra bits, table bits */
unsigned char bits; /* bits in this part of the code */
unsigned short val; /* offset in table or code value */
@@ -51,7 +52,8 @@ typedef struct {
#define ENOUGH (ENOUGH_LENS+ENOUGH_DISTS)
/* Type of code to build for inflate_table() */
typedef enum {
typedef enum
{
CODES,
LENS,
DISTS

View File

@@ -100,10 +100,13 @@ void test_compress(compr, comprLen, uncompr, uncomprLen)
err = uncompress(uncompr, &uncomprLen, compr, comprLen);
CHECK_ERR(err, "uncompress");
if (strcmp((char*)uncompr, hello)) {
if (strcmp((char*)uncompr, hello))
{
fprintf(stderr, "bad uncompress\n");
exit(1);
} else {
}
else
{
printf("uncompress(): %s\n", (char*)uncompr);
}
}
@@ -125,66 +128,92 @@ void test_gzio(fname, uncompr, uncomprLen)
z_off_t pos;
file = gzopen(fname, "wb");
if (file == NULL) {
if (file == NULL)
{
fprintf(stderr, "gzopen error\n");
exit(1);
}
gzputc(file, 'h');
if (gzputs(file, "ello") != 4) {
if (gzputs(file, "ello") != 4)
{
fprintf(stderr, "gzputs err: %s\n", gzerror(file, &err));
exit(1);
}
if (gzprintf(file, ", %s!", "hello") != 8) {
if (gzprintf(file, ", %s!", "hello") != 8)
{
fprintf(stderr, "gzprintf err: %s\n", gzerror(file, &err));
exit(1);
}
gzseek(file, 1L, SEEK_CUR); /* add one zero byte */
gzclose(file);
file = gzopen(fname, "rb");
if (file == NULL) {
if (file == NULL)
{
fprintf(stderr, "gzopen error\n");
exit(1);
}
strcpy((char*)uncompr, "garbage");
if (gzread(file, uncompr, (unsigned)uncomprLen) != len) {
if (gzread(file, uncompr, (unsigned)uncomprLen) != len)
{
fprintf(stderr, "gzread err: %s\n", gzerror(file, &err));
exit(1);
}
if (strcmp((char*)uncompr, hello)) {
if (strcmp((char*)uncompr, hello))
{
fprintf(stderr, "bad gzread: %s\n", (char*)uncompr);
exit(1);
} else {
}
else
{
printf("gzread(): %s\n", (char*)uncompr);
}
pos = gzseek(file, -8L, SEEK_CUR);
if (pos != 6 || gztell(file) != pos) {
if (pos != 6 || gztell(file) != pos)
{
fprintf(stderr, "gzseek error, pos=%ld, gztell=%ld\n",
(long)pos, (long)gztell(file));
exit(1);
}
if (gzgetc(file) != ' ') {
if (gzgetc(file) != ' ')
{
fprintf(stderr, "gzgetc error\n");
exit(1);
}
if (gzungetc(' ', file) != ' ') {
if (gzungetc(' ', file) != ' ')
{
fprintf(stderr, "gzungetc error\n");
exit(1);
}
gzgets(file, (char*)uncompr, (int)uncomprLen);
if (strlen((char*)uncompr) != 7) { /* " hello!" */
if (strlen((char*)uncompr) != 7) /* " hello!" */
{
fprintf(stderr, "gzgets err after gzseek: %s\n", gzerror(file, &err));
exit(1);
}
if (strcmp((char*)uncompr, hello + 6)) {
if (strcmp((char*)uncompr, hello + 6))
{
fprintf(stderr, "bad gzgets after gzseek\n");
exit(1);
} else {
}
else
{
printf("gzgets() after gzseek: %s\n", (char*)uncompr);
}
@@ -215,16 +244,21 @@ void test_deflate(compr, comprLen)
c_stream.next_in = (Bytef*)hello;
c_stream.next_out = compr;
while (c_stream.total_in != len && c_stream.total_out < comprLen) {
while (c_stream.total_in != len && c_stream.total_out < comprLen)
{
c_stream.avail_in = c_stream.avail_out = 1; /* force small buffers */
err = deflate(&c_stream, Z_NO_FLUSH);
CHECK_ERR(err, "deflate");
}
/* Finish the stream, still forcing small buffers: */
for (;;) {
for (;;)
{
c_stream.avail_out = 1;
err = deflate(&c_stream, Z_FINISH);
if (err == Z_STREAM_END) break;
CHECK_ERR(err, "deflate");
}
@@ -255,20 +289,26 @@ void test_inflate(compr, comprLen, uncompr, uncomprLen)
err = inflateInit(&d_stream);
CHECK_ERR(err, "inflateInit");
while (d_stream.total_out < uncomprLen && d_stream.total_in < comprLen) {
while (d_stream.total_out < uncomprLen && d_stream.total_in < comprLen)
{
d_stream.avail_in = d_stream.avail_out = 1; /* force small buffers */
err = inflate(&d_stream, Z_NO_FLUSH);
if (err == Z_STREAM_END) break;
CHECK_ERR(err, "inflate");
}
err = inflateEnd(&d_stream);
CHECK_ERR(err, "inflateEnd");
if (strcmp((char*)uncompr, hello)) {
if (strcmp((char*)uncompr, hello))
{
fprintf(stderr, "bad inflate\n");
exit(1);
} else {
}
else
{
printf("inflate(): %s\n", (char*)uncompr);
}
}
@@ -300,7 +340,9 @@ void test_large_deflate(compr, comprLen, uncompr, uncomprLen)
c_stream.avail_in = (uInt)uncomprLen;
err = deflate(&c_stream, Z_NO_FLUSH);
CHECK_ERR(err, "deflate");
if (c_stream.avail_in != 0) {
if (c_stream.avail_in != 0)
{
fprintf(stderr, "deflate not greedy\n");
exit(1);
}
@@ -320,10 +362,13 @@ void test_large_deflate(compr, comprLen, uncompr, uncomprLen)
CHECK_ERR(err, "deflate");
err = deflate(&c_stream, Z_FINISH);
if (err != Z_STREAM_END) {
if (err != Z_STREAM_END)
{
fprintf(stderr, "deflate should report Z_STREAM_END\n");
exit(1);
}
err = deflateEnd(&c_stream);
CHECK_ERR(err, "deflateEnd");
}
@@ -350,21 +395,27 @@ void test_large_inflate(compr, comprLen, uncompr, uncomprLen)
err = inflateInit(&d_stream);
CHECK_ERR(err, "inflateInit");
for (;;) {
for (;;)
{
d_stream.next_out = uncompr; /* discard the output */
d_stream.avail_out = (uInt)uncomprLen;
err = inflate(&d_stream, Z_NO_FLUSH);
if (err == Z_STREAM_END) break;
CHECK_ERR(err, "large inflate");
}
err = inflateEnd(&d_stream);
CHECK_ERR(err, "inflateEnd");
if (d_stream.total_out != 2*uncomprLen + comprLen/2) {
if (d_stream.total_out != 2 * uncomprLen + comprLen / 2)
{
fprintf(stderr, "bad large inflate: %ld\n", d_stream.total_out);
exit(1);
} else {
}
else
{
printf("large_inflate(): OK\n");
}
}
@@ -398,9 +449,12 @@ void test_flush(compr, comprLen)
c_stream.avail_in = len - 3;
err = deflate(&c_stream, Z_FINISH);
if (err != Z_STREAM_END) {
if (err != Z_STREAM_END)
{
CHECK_ERR(err, "deflate");
}
err = deflateEnd(&c_stream);
CHECK_ERR(err, "deflateEnd");
@@ -440,11 +494,14 @@ void test_sync(compr, comprLen, uncompr, uncomprLen)
CHECK_ERR(err, "inflateSync");
err = inflate(&d_stream, Z_FINISH);
if (err != Z_DATA_ERROR) {
if (err != Z_DATA_ERROR)
{
fprintf(stderr, "inflate should report DATA_ERROR\n");
/* Because of incorrect adler32 */
exit(1);
}
err = inflateEnd(&d_stream);
CHECK_ERR(err, "inflateEnd");
@@ -480,10 +537,13 @@ void test_dict_deflate(compr, comprLen)
c_stream.avail_in = (uInt)strlen(hello) + 1;
err = deflate(&c_stream, Z_FINISH);
if (err != Z_STREAM_END) {
if (err != Z_STREAM_END)
{
fprintf(stderr, "deflate should report Z_STREAM_END\n");
exit(1);
}
err = deflateEnd(&c_stream);
CHECK_ERR(err, "deflateEnd");
}
@@ -513,27 +573,37 @@ void test_dict_inflate(compr, comprLen, uncompr, uncomprLen)
d_stream.next_out = uncompr;
d_stream.avail_out = (uInt)uncomprLen;
for (;;) {
for (;;)
{
err = inflate(&d_stream, Z_NO_FLUSH);
if (err == Z_STREAM_END) break;
if (err == Z_NEED_DICT) {
if (d_stream.adler != dictId) {
if (err == Z_NEED_DICT)
{
if (d_stream.adler != dictId)
{
fprintf(stderr, "unexpected dictionary");
exit(1);
}
err = inflateSetDictionary(&d_stream, (const Bytef*)dictionary,
(int)sizeof(dictionary));
}
CHECK_ERR(err, "inflate with dict");
}
err = inflateEnd(&d_stream);
CHECK_ERR(err, "inflateEnd");
if (strcmp((char*)uncompr, hello)) {
if (strcmp((char*)uncompr, hello))
{
fprintf(stderr, "bad inflate with dict\n");
exit(1);
} else {
}
else
{
printf("inflate with dictionary: %s\n", (char*)uncompr);
}
}
@@ -551,11 +621,14 @@ int main(argc, argv)
uLong uncomprLen = comprLen;
static const char* myVersion = ZLIB_VERSION;
if (zlibVersion()[0] != myVersion[0]) {
if (zlibVersion()[0] != myVersion[0])
{
fprintf(stderr, "incompatible zlib version\n");
exit(1);
} else if (strcmp(zlibVersion(), ZLIB_VERSION) != 0) {
}
else if (strcmp(zlibVersion(), ZLIB_VERSION) != 0)
{
fprintf(stderr, "warning: different zlib version\n");
}
@@ -564,10 +637,12 @@ int main(argc, argv)
compr = (Byte*)calloc((uInt)comprLen, 1);
uncompr = (Byte*)calloc((uInt)uncomprLen, 1);
/* compr and uncompr are cleared to avoid reading uninitialized
* data and to ensure that uncompr compresses well.
*/
if (compr == Z_NULL || uncompr == Z_NULL) {
if (compr == Z_NULL || uncompr == Z_NULL)
{
printf("out of memory\n");
exit(1);
}

View File

@@ -53,14 +53,16 @@
*/
/* these items are strung together in a linked list, one for each allocation */
struct mem_item {
struct mem_item
{
void* ptr; /* pointer to allocated memory */
size_t size; /* requested size of allocation */
struct mem_item* next; /* pointer to next item in list, or NULL */
};
/* this structure is at the root of the linked list, and tracks statistics */
struct mem_zone {
struct mem_zone
{
struct mem_item* first; /* pointer to first item in list, or NULL */
size_t total, highwater; /* total allocations, and largest total */
size_t limit; /* memory allocation limit, or 0 if no limit */
@@ -82,16 +84,21 @@ local void *mem_alloc(void *mem, unsigned count, unsigned size)
/* perform allocation using the standard library, fill memory with a
non-zero value to make sure that the code isn't depending on zeros */
ptr = malloc(len);
if (ptr == NULL)
return NULL;
memset(ptr, 0xa5, len);
/* create a new item for the list */
item = malloc(sizeof(struct mem_item));
if (item == NULL) {
if (item == NULL)
{
free(ptr);
return NULL;
}
item->ptr = ptr;
item->size = len;
@@ -101,6 +108,7 @@ local void *mem_alloc(void *mem, unsigned count, unsigned size)
/* update the statistics */
zone->total += item->size;
if (zone->total > zone->highwater)
zone->highwater = zone->total;
@@ -115,7 +123,8 @@ local void mem_free(void *mem, void *ptr)
struct mem_zone* zone = mem;
/* if no zone, just do a free */
if (zone == NULL) {
if (zone == NULL)
{
free(ptr);
return;
}
@@ -123,15 +132,22 @@ local void mem_free(void *mem, void *ptr)
/* point next to the item that matches ptr, or NULL if not found -- remove
the item from the linked list if found */
next = zone->first;
if (next) {
if (next)
{
if (next->ptr == ptr)
zone->first = next->next; /* first one is it, remove from list */
else {
do { /* search the linked list */
else
{
do /* search the linked list */
{
item = next;
next = item->next;
} while (next != NULL && next->ptr != ptr);
if (next) { /* if found, remove from linked list */
}
while (next != NULL && next->ptr != ptr);
if (next) /* if found, remove from linked list */
{
item->next = next->next;
zone->notlifo++; /* not a LIFO free */
}
@@ -140,7 +156,8 @@ local void mem_free(void *mem, void *ptr)
}
/* if found, update the statistics and free the item */
if (next) {
if (next)
{
zone->total -= next->size;
free(next);
}
@@ -208,7 +225,9 @@ local void mem_done(z_stream *strm, char *prefix)
/* free leftover allocations and item structures, if any */
item = zone->first;
while (item != NULL) {
while (item != NULL)
{
free(item->ptr);
next = item->next;
free(item);
@@ -220,8 +239,10 @@ local void mem_done(z_stream *strm, char *prefix)
if (count || zone->total)
fprintf(stderr, "** %s: %lu bytes in %d blocks not freed\n",
prefix, zone->total, count);
if (zone->notlifo)
fprintf(stderr, "** %s: %d frees not LIFO\n", prefix, zone->notlifo);
if (zone->rogue)
fprintf(stderr, "** %s: %d frees not recognized\n",
prefix, zone->rogue);
@@ -248,11 +269,15 @@ local unsigned char *h2b(const char *hex, unsigned *len)
unsigned next, val;
in = malloc((strlen(hex) + 1) >> 1);
if (in == NULL)
return NULL;
next = 0;
val = 1;
do {
do
{
if (*hex >= '0' && *hex <= '9')
val = (val << 4) + *hex - '0';
else if (*hex >= 'A' && *hex <= 'F')
@@ -261,13 +286,18 @@ local unsigned char *h2b(const char *hex, unsigned *len)
val = (val << 4) + *hex - 'a' + 10;
else if (val != 1 && val < 32) /* one digit followed by delimiter */
val += 240; /* make it look like two digits */
if (val > 255) { /* have two digits */
if (val > 255) /* have two digits */
{
in[next++] = val & 0xff; /* save the decoded byte */
val = 1; /* start over */
}
} while (*hex++); /* go through the loop with the terminating null */
}
while (*hex++); /* go through the loop with the terminating null */
if (len != NULL)
*len = next;
in = reallocf(in, next);
return in;
}
@@ -294,33 +324,50 @@ local void inf(char *hex, char *what, unsigned step, int win, unsigned len,
strm.avail_in = 0;
strm.next_in = Z_NULL;
ret = inflateInit2(&strm, win);
if (ret != Z_OK) {
if (ret != Z_OK)
{
mem_done(&strm, what);
return;
}
out = malloc(len); assert(out != NULL);
if (win == 47) {
out = malloc(len);
assert(out != NULL);
if (win == 47)
{
head.extra = out;
head.extra_max = len;
head.name = out;
head.name_max = len;
head.comment = out;
head.comm_max = len;
ret = inflateGetHeader(&strm, &head); assert(ret == Z_OK);
ret = inflateGetHeader(&strm, &head);
assert(ret == Z_OK);
}
in = h2b(hex, &have); assert(in != NULL);
in = h2b(hex, &have);
assert(in != NULL);
if (step == 0 || step > have)
step = have;
strm.avail_in = step;
have -= step;
strm.next_in = in;
do {
do
{
strm.avail_out = len;
strm.next_out = out;
ret = inflate(&strm, Z_NO_FLUSH); assert(err == 9 || ret == err);
ret = inflate(&strm, Z_NO_FLUSH);
assert(err == 9 || ret == err);
if (ret != Z_OK && ret != Z_BUF_ERROR && ret != Z_NEED_DICT)
break;
if (ret == Z_NEED_DICT) {
if (ret == Z_NEED_DICT)
{
ret = inflateSetDictionary(&strm, in, 1);
assert(ret == Z_DATA_ERROR);
mem_limit(&strm, 1);
@@ -330,19 +377,27 @@ local void inf(char *hex, char *what, unsigned step, int win, unsigned len,
((struct inflate_state*)strm.state)->mode = DICT;
ret = inflateSetDictionary(&strm, out, 0);
assert(ret == Z_OK);
ret = inflate(&strm, Z_NO_FLUSH); assert(ret == Z_BUF_ERROR);
ret = inflate(&strm, Z_NO_FLUSH);
assert(ret == Z_BUF_ERROR);
}
ret = inflateCopy(&copy, &strm); assert(ret == Z_OK);
ret = inflateEnd(&copy); assert(ret == Z_OK);
ret = inflateCopy(&copy, &strm);
assert(ret == Z_OK);
ret = inflateEnd(&copy);
assert(ret == Z_OK);
err = 9; /* don't care next time around */
have += strm.avail_in;
strm.avail_in = step > have ? have : step;
have -= strm.avail_in;
} while (strm.avail_in);
}
while (strm.avail_in);
free(in);
free(out);
ret = inflateReset2(&strm, -8); assert(ret == Z_OK);
ret = inflateEnd(&strm); assert(ret == Z_OK);
ret = inflateReset2(&strm, -8);
assert(ret == Z_OK);
ret = inflateEnd(&strm);
assert(ret == Z_OK);
mem_done(&strm, what);
}
@@ -355,13 +410,17 @@ local void cover_support(void)
mem_setup(&strm);
strm.avail_in = 0;
strm.next_in = Z_NULL;
ret = inflateInit(&strm); assert(ret == Z_OK);
ret = inflateInit(&strm);
assert(ret == Z_OK);
mem_used(&strm, "inflate init");
ret = inflatePrime(&strm, 5, 31); assert(ret == Z_OK);
ret = inflatePrime(&strm, -1, 0); assert(ret == Z_OK);
ret = inflatePrime(&strm, 5, 31);
assert(ret == Z_OK);
ret = inflatePrime(&strm, -1, 0);
assert(ret == Z_OK);
ret = inflateSetDictionary(&strm, Z_NULL, 0);
assert(ret == Z_STREAM_ERROR);
ret = inflateEnd(&strm); assert(ret == Z_OK);
ret = inflateEnd(&strm);
assert(ret == Z_OK);
mem_done(&strm, "prime");
inf("63 0", "force window allocation", 0, -15, 1, Z_OK);
@@ -379,8 +438,10 @@ local void cover_support(void)
strm.avail_in = 0;
strm.next_in = Z_NULL;
ret = inflateInit(&strm); assert(ret == Z_OK);
ret = inflateEnd(&strm); assert(ret == Z_OK);
ret = inflateInit(&strm);
assert(ret == Z_OK);
ret = inflateEnd(&strm);
assert(ret == Z_OK);
fputs("inflate built-in memory routines\n", stderr);
}
@@ -391,9 +452,12 @@ local void cover_wrap(void)
z_stream strm, copy;
unsigned char dict[257];
ret = inflate(Z_NULL, 0); assert(ret == Z_STREAM_ERROR);
ret = inflateEnd(Z_NULL); assert(ret == Z_STREAM_ERROR);
ret = inflateCopy(Z_NULL, Z_NULL); assert(ret == Z_STREAM_ERROR);
ret = inflate(Z_NULL, 0);
assert(ret == Z_STREAM_ERROR);
ret = inflateEnd(Z_NULL);
assert(ret == Z_STREAM_ERROR);
ret = inflateCopy(Z_NULL, Z_NULL);
assert(ret == Z_STREAM_ERROR);
fputs("inflate bad parameters\n", stderr);
inf("1f 8b 0 0", "bad gzip method", 0, 31, 0, Z_DATA_ERROR);
@@ -419,27 +483,36 @@ local void cover_wrap(void)
strm.avail_out = 1;
strm.next_out = (void*)&ret;
mem_limit(&strm, 1);
ret = inflate(&strm, Z_NO_FLUSH); assert(ret == Z_MEM_ERROR);
ret = inflate(&strm, Z_NO_FLUSH); assert(ret == Z_MEM_ERROR);
ret = inflate(&strm, Z_NO_FLUSH);
assert(ret == Z_MEM_ERROR);
ret = inflate(&strm, Z_NO_FLUSH);
assert(ret == Z_MEM_ERROR);
mem_limit(&strm, 0);
memset(dict, 0, 257);
ret = inflateSetDictionary(&strm, dict, 257);
assert(ret == Z_OK);
mem_limit(&strm, (sizeof(struct inflate_state) << 1) + 256);
ret = inflatePrime(&strm, 16, 0); assert(ret == Z_OK);
ret = inflatePrime(&strm, 16, 0);
assert(ret == Z_OK);
strm.avail_in = 2;
strm.next_in = (void*)"\x80";
ret = inflateSync(&strm); assert(ret == Z_DATA_ERROR);
ret = inflate(&strm, Z_NO_FLUSH); assert(ret == Z_STREAM_ERROR);
ret = inflateSync(&strm);
assert(ret == Z_DATA_ERROR);
ret = inflate(&strm, Z_NO_FLUSH);
assert(ret == Z_STREAM_ERROR);
strm.avail_in = 4;
strm.next_in = (void*)"\0\0\xff\xff";
ret = inflateSync(&strm); assert(ret == Z_OK);
ret = inflateSync(&strm);
assert(ret == Z_OK);
(void)inflateSyncPoint(&strm);
ret = inflateCopy(&copy, &strm); assert(ret == Z_MEM_ERROR);
ret = inflateCopy(&copy, &strm);
assert(ret == Z_MEM_ERROR);
mem_limit(&strm, 0);
ret = inflateUndermine(&strm, 1); assert(ret == Z_DATA_ERROR);
ret = inflateUndermine(&strm, 1);
assert(ret == Z_DATA_ERROR);
(void)inflateMark(&strm);
ret = inflateEnd(&strm); assert(ret == Z_OK);
ret = inflateEnd(&strm);
assert(ret == Z_OK);
mem_done(&strm, "miscellaneous, force memory errors");
}
@@ -450,13 +523,17 @@ local unsigned pull(void *desc, unsigned char **buf)
static unsigned char dat[] = {0x63, 0, 2, 0};
struct inflate_state* state;
if (desc == Z_NULL) {
if (desc == Z_NULL)
{
next = 0;
return 0; /* no input (already provided at next_in) */
}
state = (void*)((z_stream*)desc)->state;
if (state != Z_NULL)
state->mode = SYNC; /* force an otherwise impossible situation */
return next < sizeof(dat) ? (*buf = dat + next++, 1) : 0;
}
@@ -475,14 +552,17 @@ local void cover_back(void)
ret = inflateBackInit_(Z_NULL, 0, win, 0, 0);
assert(ret == Z_VERSION_ERROR);
ret = inflateBackInit(Z_NULL, 0, win); assert(ret == Z_STREAM_ERROR);
ret = inflateBackInit(Z_NULL, 0, win);
assert(ret == Z_STREAM_ERROR);
ret = inflateBack(Z_NULL, Z_NULL, Z_NULL, Z_NULL, Z_NULL);
assert(ret == Z_STREAM_ERROR);
ret = inflateBackEnd(Z_NULL); assert(ret == Z_STREAM_ERROR);
ret = inflateBackEnd(Z_NULL);
assert(ret == Z_STREAM_ERROR);
fputs("inflateBack bad parameters\n", stderr);
mem_setup(&strm);
ret = inflateBackInit(&strm, 15, win); assert(ret == Z_OK);
ret = inflateBackInit(&strm, 15, win);
assert(ret == Z_OK);
strm.avail_in = 2;
strm.next_in = (void*)"\x03";
ret = inflateBack(&strm, pull, Z_NULL, push, Z_NULL);
@@ -495,15 +575,19 @@ local void cover_back(void)
/* force mode error by mucking with state */
ret = inflateBack(&strm, pull, &strm, push, Z_NULL);
assert(ret == Z_STREAM_ERROR);
ret = inflateBackEnd(&strm); assert(ret == Z_OK);
ret = inflateBackEnd(&strm);
assert(ret == Z_OK);
mem_done(&strm, "inflateBack bad state");
ret = inflateBackInit(&strm, 15, win); assert(ret == Z_OK);
ret = inflateBackEnd(&strm); assert(ret == Z_OK);
ret = inflateBackInit(&strm, 15, win);
assert(ret == Z_OK);
ret = inflateBackEnd(&strm);
assert(ret == Z_OK);
fputs("inflateBack built-in memory routines\n", stderr);
}
/* do a raw inflate of data in hexadecimal with both inflate and inflateBack */
local int try(char* hex, char* id, int err)
{
int ret;
@@ -535,23 +619,31 @@ local int try(char *hex, char *id, int err)
assert(ret == Z_OK);
strm.avail_in = len;
strm.next_in = in;
do {
do
{
strm.avail_out = size;
strm.next_out = out;
ret = inflate(&strm, Z_TREES);
assert(ret != Z_STREAM_ERROR && ret != Z_MEM_ERROR);
if (ret == Z_DATA_ERROR || ret == Z_NEED_DICT)
break;
} while (strm.avail_in || strm.avail_out == 0);
if (err) {
}
while (strm.avail_in || strm.avail_out == 0);
if (err)
{
assert(ret == Z_DATA_ERROR);
assert(strcmp(id, strm.msg) == 0);
}
inflateEnd(&strm);
mem_done(&strm, prefix);
/* then with inflateBack */
if (err >= 0) {
if (err >= 0)
{
strcpy(prefix, id);
strcat(prefix, "-back");
mem_setup(&strm);
@@ -561,10 +653,13 @@ local int try(char *hex, char *id, int err)
strm.next_in = in;
ret = inflateBack(&strm, pull, Z_NULL, push, Z_NULL);
assert(ret != Z_STREAM_ERROR);
if (err) {
if (err)
{
assert(ret == Z_DATA_ERROR);
assert(strcmp(id, strm.msg) == 0);
}
inflateBackEnd(&strm);
mem_done(&strm, prefix);
}
@@ -581,33 +676,53 @@ local int try(char *hex, char *id, int err)
local void cover_inflate(void)
{
try("0 0 0 0 0", "invalid stored block lengths", 1);
try("3 0", "fixed", 0);
try("6", "invalid block type", 1);
try("1 1 0 fe ff 0", "stored", 0);
try("fc 0 0", "too many length or distance symbols", 1);
try("4 0 fe ff", "invalid code lengths set", 1);
try("4 0 24 49 0", "invalid bit length repeat", 1);
try("4 0 24 e9 ff ff", "invalid bit length repeat", 1);
try("4 0 24 e9 ff 6d", "invalid code -- missing end-of-block", 1);
try("4 80 49 92 24 49 92 24 71 ff ff 93 11 0",
"invalid literal/lengths set", 1);
try("4 80 49 92 24 49 92 24 f b4 ff ff c3 84", "invalid distances set", 1);
try("4 c0 81 8 0 0 0 0 20 7f eb b 0 0", "invalid literal/length code", 1);
try("2 7e ff ff", "invalid distance code", 1);
try("c c0 81 0 0 0 0 0 90 ff 6b 4 0", "invalid distance too far back", 1);
/* also trailer mismatch just in inflate() */
try("1f 8b 8 0 0 0 0 0 0 0 3 0 0 0 0 1", "incorrect data check", -1);
try("1f 8b 8 0 0 0 0 0 0 0 3 0 0 0 0 0 0 0 0 1",
"incorrect length check", -1);
try("5 c0 21 d 0 0 0 80 b0 fe 6d 2f 91 6c", "pull 17", 0);
try("5 e0 81 91 24 cb b2 2c 49 e2 f 2e 8b 9a 47 56 9f fb fe ec d2 ff 1f",
"long code", 0);
try("ed c0 1 1 0 0 0 40 20 ff 57 1b 42 2c 4f", "length extra", 0);
try("ed cf c1 b1 2c 47 10 c4 30 fa 6f 35 1d 1 82 59 3d fb be 2e 2a fc f c",
"long distance and extra", 0);
try("ed c0 81 0 0 0 0 80 a0 fd a9 17 a9 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 "
"0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 6", "window end", 0);
inf("2 8 20 80 0 3 0", "inflate_fast TYPE return", 0, -15, 258,
Z_STREAM_END);
inf("63 18 5 40 c 0", "window wrap", 3, -8, 300, Z_OK);
@@ -625,6 +740,7 @@ local void cover_trees(void)
enough errors, since zlib insures that enough is always enough */
for (bits = 0; bits < 15; bits++)
lens[bits] = (unsigned short)(bits + 1);
lens[15] = 15;
next = table;
bits = 15;

View File

@@ -88,15 +88,19 @@ static char *strwinerror (error)
(LPVOID)&msgbuf,
0,
NULL);
if (chars != 0) {
if (chars != 0)
{
/* If there is an \r\n appended, zap it. */
if (chars >= 2
&& msgbuf[chars - 2] == '\r' && msgbuf[chars - 1] == '\n') {
&& msgbuf[chars - 2] == '\r' && msgbuf[chars - 1] == '\n')
{
chars -= 2;
msgbuf[chars] = 0;
}
if (chars > sizeof (buf) - 1) {
if (chars > sizeof(buf) - 1)
{
chars = sizeof(buf) - 1;
msgbuf[chars] = 0;
}
@@ -104,7 +108,8 @@ static char *strwinerror (error)
wcstombs(buf, msgbuf, chars + 1);
LocalFree(msgbuf);
}
else {
else
{
sprintf(buf, "unknown win32 error (%ld)", error);
}
@@ -163,7 +168,8 @@ void myfree(q, p)
free(p);
}
typedef struct gzFile_s {
typedef struct gzFile_s
{
FILE* file;
int write;
int err;
@@ -198,30 +204,40 @@ gzFile gz_open(path, fd, mode)
int ret;
gz = malloc(sizeof(struct gzFile_s));
if (gz == NULL)
return NULL;
gz->write = strchr(mode, 'w') != NULL;
gz->strm.zalloc = myalloc;
gz->strm.zfree = myfree;
gz->strm.opaque = Z_NULL;
if (gz->write)
ret = deflateInit2(&(gz->strm), -1, 8, 15 + 16, 8, 0);
else {
else
{
gz->strm.next_in = 0;
gz->strm.avail_in = Z_NULL;
ret = inflateInit2(&(gz->strm), 15 + 16);
}
if (ret != Z_OK) {
if (ret != Z_OK)
{
free(gz);
return NULL;
}
gz->file = path == NULL ? fdopen(fd, gz->write ? "wb" : "rb") :
fopen(path, gz->write ? "wb" : "rb");
if (gz->file == NULL) {
if (gz->file == NULL)
{
gz->write ? deflateEnd(&(gz->strm)) : inflateEnd(&(gz->strm));
free(gz);
return NULL;
}
gz->err = 0;
gz->msg = "";
return gz;
@@ -239,15 +255,20 @@ int gzwrite(gz, buf, len)
if (gz == NULL || !gz->write)
return 0;
strm = &(gz->strm);
strm->next_in = (void*)buf;
strm->avail_in = len;
do {
do
{
strm->next_out = out;
strm->avail_out = BUFLEN;
(void)deflate(strm, Z_NO_FLUSH);
fwrite(out, 1, BUFLEN - strm->avail_out, gz->file);
} while (strm->avail_out == 0);
}
while (strm->avail_out == 0);
return len;
}
@@ -265,26 +286,37 @@ int gzread(gz, buf, len)
if (gz == NULL || gz->write)
return 0;
if (gz->err)
return 0;
strm = &(gz->strm);
strm->next_out = (void*)buf;
strm->avail_out = len;
do {
do
{
got = fread(in, 1, 1, gz->file);
if (got == 0)
break;
strm->next_in = in;
strm->avail_in = 1;
ret = inflate(strm, Z_NO_FLUSH);
if (ret == Z_DATA_ERROR) {
if (ret == Z_DATA_ERROR)
{
gz->err = Z_DATA_ERROR;
gz->msg = strm->msg;
return 0;
}
if (ret == Z_STREAM_END)
inflateReset(strm);
} while (strm->avail_out);
}
while (strm->avail_out);
return len - strm->avail_out;
}
@@ -298,20 +330,28 @@ int gzclose(gz)
if (gz == NULL)
return Z_STREAM_ERROR;
strm = &(gz->strm);
if (gz->write) {
if (gz->write)
{
strm->next_in = Z_NULL;
strm->avail_in = 0;
do {
do
{
strm->next_out = out;
strm->avail_out = BUFLEN;
(void)deflate(strm, Z_FINISH);
fwrite(out, 1, BUFLEN - strm->avail_out, gz->file);
} while (strm->avail_out == 0);
}
while (strm->avail_out == 0);
deflateEnd(strm);
}
else
inflateEnd(strm);
fclose(gz->file);
free(gz);
return Z_OK;
@@ -364,22 +404,31 @@ void gz_compress(in, out)
int err;
#ifdef USE_MMAP
/* Try first compressing with mmap. If mmap fails (minigzip used in a
* pipe), use the normal fread loop.
*/
if (gz_compress_mmap(in, out) == Z_OK) return;
#endif
for (;;) {
for (;;)
{
len = (int)fread(buf, 1, sizeof(buf), in);
if (ferror(in)) {
if (ferror(in))
{
perror("fread");
exit(1);
}
if (len == 0) break;
if (gzwrite(out, buf, (unsigned)len) != len) error(gzerror(out, &err));
}
fclose(in);
if (gzclose(out) != Z_OK) error("failed gzclose");
}
@@ -401,11 +450,14 @@ int gz_compress_mmap(in, out)
/* Determine the size of the file, needed for mmap: */
if (fstat(ifd, &sb) < 0) return Z_ERRNO;
buf_len = sb.st_size;
if (buf_len <= 0) return Z_ERRNO;
/* Now do the actual mmap: */
buf = mmap((caddr_t) 0, buf_len, PROT_READ, MAP_SHARED, ifd, (off_t)0);
if (buf == (caddr_t)(-1)) return Z_ERRNO;
/* Compress the whole file at once: */
@@ -415,7 +467,9 @@ int gz_compress_mmap(in, out)
munmap(buf, buf_len);
fclose(in);
if (gzclose(out) != Z_OK) error("failed gzclose");
return Z_OK;
}
#endif /* USE_MMAP */
@@ -431,15 +485,20 @@ void gz_uncompress(in, out)
int len;
int err;
for (;;) {
for (;;)
{
len = gzread(in, buf, sizeof(buf));
if (len < 0) error(gzerror(in, &err));
if (len == 0) break;
if ((int)fwrite(buf, 1, (unsigned)len, out) != len) {
if ((int)fwrite(buf, 1, (unsigned)len, out) != len)
{
error("failed fwrite");
}
}
if (fclose(out)) error("failed fclose");
if (gzclose(in) != Z_OK) error("failed gzclose");
@@ -458,7 +517,8 @@ void file_compress(file, mode)
FILE* in;
gzFile out;
if (strlen(file) + strlen(GZ_SUFFIX) >= sizeof(outfile)) {
if (strlen(file) + strlen(GZ_SUFFIX) >= sizeof(outfile))
{
fprintf(stderr, "%s: filename too long\n", prog);
exit(1);
}
@@ -467,15 +527,21 @@ void file_compress(file, mode)
strcat(outfile, GZ_SUFFIX);
in = fopen(file, "rb");
if (in == NULL) {
if (in == NULL)
{
perror(file);
exit(1);
}
out = gzopen(outfile, mode);
if (out == NULL) {
if (out == NULL)
{
fprintf(stderr, "%s: can't gzopen %s\n", prog, outfile);
exit(1);
}
gz_compress(in, out);
unlink(file);
@@ -494,29 +560,39 @@ void file_uncompress(file)
gzFile in;
size_t len = strlen(file);
if (len + strlen(GZ_SUFFIX) >= sizeof(buf)) {
if (len + strlen(GZ_SUFFIX) >= sizeof(buf))
{
fprintf(stderr, "%s: filename too long\n", prog);
exit(1);
}
strcpy(buf, file);
if (len > SUFFIX_LEN && strcmp(file+len-SUFFIX_LEN, GZ_SUFFIX) == 0) {
if (len > SUFFIX_LEN && strcmp(file + len - SUFFIX_LEN, GZ_SUFFIX) == 0)
{
infile = file;
outfile = buf;
outfile[len - 3] = '\0';
} else {
}
else
{
outfile = file;
infile = buf;
strcat(infile, GZ_SUFFIX);
}
in = gzopen(infile, "rb");
if (in == NULL) {
if (in == NULL)
{
fprintf(stderr, "%s: can't gzopen %s\n", prog, infile);
exit(1);
}
out = fopen(outfile, "wb");
if (out == NULL) {
if (out == NULL)
{
perror(file);
exit(1);
}
@@ -550,10 +626,12 @@ int main(argc, argv)
prog = argv[0];
bname = strrchr(argv[0], '/');
if (bname)
bname++;
else
bname = argv[0];
argc--, argv++;
if (!strcmp(bname, "gunzip"))
@@ -561,7 +639,8 @@ int main(argc, argv)
else if (!strcmp(bname, "zcat"))
copyout = uncompr = 1;
while (argc > 0) {
while (argc > 0)
{
if (strcmp(*argv, "-c") == 0)
copyout = 1;
else if (strcmp(*argv, "-d") == 0)
@@ -577,55 +656,88 @@ int main(argc, argv)
outmode[2] = (*argv)[1];
else
break;
argc--, argv++;
}
if (outmode[3] == ' ')
outmode[3] = 0;
if (argc == 0) {
if (argc == 0)
{
SET_BINARY_MODE(stdin);
SET_BINARY_MODE(stdout);
if (uncompr) {
if (uncompr)
{
file = gzdopen(fileno(stdin), "rb");
if (file == NULL) error("can't gzdopen stdin");
gz_uncompress(file, stdout);
} else {
}
else
{
file = gzdopen(fileno(stdout), outmode);
if (file == NULL) error("can't gzdopen stdout");
gz_compress(stdin, file);
}
} else {
if (copyout) {
}
else
{
if (copyout)
{
SET_BINARY_MODE(stdout);
}
do {
if (uncompr) {
if (copyout) {
do
{
if (uncompr)
{
if (copyout)
{
file = gzopen(*argv, "rb");
if (file == NULL)
fprintf(stderr, "%s: can't gzopen %s\n", prog, *argv);
else
gz_uncompress(file, stdout);
} else {
}
else
{
file_uncompress(*argv);
}
} else {
if (copyout) {
}
else
{
if (copyout)
{
FILE* in = fopen(*argv, "rb");
if (in == NULL) {
if (in == NULL)
{
perror(*argv);
} else {
}
else
{
file = gzdopen(fileno(stdout), outmode);
if (file == NULL) error("can't gzdopen stdout");
gz_compress(in, file);
}
} else {
}
else
{
file_compress(*argv, outmode);
}
}
} while (argv++, --argc);
}
while (argv++, --argc);
}
return 0;
}

379
extern/zlib/trees.c vendored
View File

@@ -114,7 +114,8 @@ local int base_dist[D_CODES];
# include "trees.h"
#endif /* GEN_TREES_H */
struct static_tree_desc_s {
struct static_tree_desc_s
{
const ct_data* static_tree; /* static tree or NULL */
const intf* extra_bits; /* extra bits for each code or NULL */
int extra_base; /* base index for extra_bits */
@@ -198,12 +199,15 @@ local void send_bits(s, value, length)
* (16 - bi_valid) bits from value, leaving (width - (16-bi_valid))
* unused bits in value.
*/
if (s->bi_valid > (int)Buf_size - length) {
if (s->bi_valid > (int)Buf_size - length)
{
s->bi_buf |= (ush)value << s->bi_valid;
put_short(s, s->bi_buf);
s->bi_buf = (ush)value >> (Buf_size - s->bi_valid);
s->bi_valid += length - Buf_size;
} else {
}
else
{
s->bi_buf |= (ush)value << s->bi_valid;
s->bi_valid += length;
}
@@ -256,12 +260,17 @@ local void tr_static_init()
/* Initialize the mapping length (0..255) -> length code (0..28) */
length = 0;
for (code = 0; code < LENGTH_CODES-1; code++) {
for (code = 0; code < LENGTH_CODES - 1; code++)
{
base_length[code] = length;
for (n = 0; n < (1<<extra_lbits[code]); n++) {
for (n = 0; n < (1 << extra_lbits[code]); n++)
{
_length_code[length++] = (uch)code;
}
}
Assert(length == 256, "tr_static_init: length != 256");
/* Note that the length 255 (match length 258) can be represented
* in two different ways: code 284 + 5 bits or code 285, so we
@@ -271,29 +280,45 @@ local void tr_static_init()
/* Initialize the mapping dist (0..32K) -> dist code (0..29) */
dist = 0;
for (code = 0 ; code < 16; code++) {
for (code = 0 ; code < 16; code++)
{
base_dist[code] = dist;
for (n = 0; n < (1<<extra_dbits[code]); n++) {
for (n = 0; n < (1 << extra_dbits[code]); n++)
{
_dist_code[dist++] = (uch)code;
}
}
Assert(dist == 256, "tr_static_init: dist != 256");
dist >>= 7; /* from now on, all distances are divided by 128 */
for ( ; code < D_CODES; code++) {
for (; code < D_CODES; code++)
{
base_dist[code] = dist << 7;
for (n = 0; n < (1<<(extra_dbits[code]-7)); n++) {
for (n = 0; n < (1 << (extra_dbits[code] - 7)); n++)
{
_dist_code[256 + dist++] = (uch)code;
}
}
Assert(dist == 256, "tr_static_init: 256+dist != 512");
/* Construct the codes of the static literal tree */
for (bits = 0; bits <= MAX_BITS; bits++) bl_count[bits] = 0;
n = 0;
while (n <= 143) static_ltree[n++].Len = 8, bl_count[8]++;
while (n <= 255) static_ltree[n++].Len = 9, bl_count[9]++;
while (n <= 279) static_ltree[n++].Len = 7, bl_count[7]++;
while (n <= 287) static_ltree[n++].Len = 8, bl_count[8]++;
/* Codes 286 and 287 do not exist, but we must include them in the
* tree construction to get a canonical Huffman tree (longest code
* all ones)
@@ -301,10 +326,12 @@ local void tr_static_init()
gen_codes((ct_data*)static_ltree, L_CODES + 1, bl_count);
/* The static distance tree is trivial: */
for (n = 0; n < D_CODES; n++) {
for (n = 0; n < D_CODES; n++)
{
static_dtree[n].Len = 5;
static_dtree[n].Code = bi_reverse((unsigned)n, 5);
}
static_init_done = 1;
# ifdef GEN_TREES_H
@@ -335,38 +362,50 @@ void gen_trees_header()
"/* header created automatically with -DGEN_TREES_H */\n\n");
fprintf(header, "local const ct_data static_ltree[L_CODES+2] = {\n");
for (i = 0; i < L_CODES+2; i++) {
for (i = 0; i < L_CODES + 2; i++)
{
fprintf(header, "{{%3u},{%3u}}%s", static_ltree[i].Code,
static_ltree[i].Len, SEPARATOR(i, L_CODES + 1, 5));
}
fprintf(header, "local const ct_data static_dtree[D_CODES] = {\n");
for (i = 0; i < D_CODES; i++) {
for (i = 0; i < D_CODES; i++)
{
fprintf(header, "{{%2u},{%2u}}%s", static_dtree[i].Code,
static_dtree[i].Len, SEPARATOR(i, D_CODES - 1, 5));
}
fprintf(header, "const uch ZLIB_INTERNAL _dist_code[DIST_CODE_LEN] = {\n");
for (i = 0; i < DIST_CODE_LEN; i++) {
for (i = 0; i < DIST_CODE_LEN; i++)
{
fprintf(header, "%2u%s", _dist_code[i],
SEPARATOR(i, DIST_CODE_LEN - 1, 20));
}
fprintf(header,
"const uch ZLIB_INTERNAL _length_code[MAX_MATCH-MIN_MATCH+1]= {\n");
for (i = 0; i < MAX_MATCH-MIN_MATCH+1; i++) {
for (i = 0; i < MAX_MATCH - MIN_MATCH + 1; i++)
{
fprintf(header, "%2u%s", _length_code[i],
SEPARATOR(i, MAX_MATCH - MIN_MATCH, 20));
}
fprintf(header, "local const int base_length[LENGTH_CODES] = {\n");
for (i = 0; i < LENGTH_CODES; i++) {
for (i = 0; i < LENGTH_CODES; i++)
{
fprintf(header, "%1u%s", base_length[i],
SEPARATOR(i, LENGTH_CODES - 1, 20));
}
fprintf(header, "local const int base_dist[D_CODES] = {\n");
for (i = 0; i < D_CODES; i++) {
for (i = 0; i < D_CODES; i++)
{
fprintf(header, "%5u%s", base_dist[i],
SEPARATOR(i, D_CODES - 1, 10));
}
@@ -421,7 +460,9 @@ local void init_block(s)
/* Initialize the trees. */
for (n = 0; n < L_CODES; n++) s->dyn_ltree[n].Freq = 0;
for (n = 0; n < D_CODES; n++) s->dyn_dtree[n].Freq = 0;
for (n = 0; n < BL_CODES; n++) s->bl_tree[n].Freq = 0;
s->dyn_ltree[END_BLOCK].Freq = 1;
@@ -469,21 +510,27 @@ local void pqdownheap(s, tree, k)
{
int v = s->heap[k];
int j = k << 1; /* left son of k */
while (j <= s->heap_len) {
while (j <= s->heap_len)
{
/* Set j to the smallest of the two sons: */
if (j < s->heap_len &&
smaller(tree, s->heap[j+1], s->heap[j], s->depth)) {
smaller(tree, s->heap[j + 1], s->heap[j], s->depth))
{
j++;
}
/* Exit if v is smaller than both sons */
if (smaller(tree, v, s->heap[j], s->depth)) break;
/* Exchange v with the smallest son */
s->heap[k] = s->heap[j]; k = j;
s->heap[k] = s->heap[j];
k = j;
/* And continue down the tree, setting j to the left son of k */
j <<= 1;
}
s->heap[k] = v;
}
@@ -525,10 +572,13 @@ local void gen_bitlen(s, desc)
*/
tree[s->heap[s->heap_max]].Len = 0; /* root of the heap */
for (h = s->heap_max+1; h < HEAP_SIZE; h++) {
for (h = s->heap_max + 1; h < HEAP_SIZE; h++)
{
n = s->heap[h];
bits = tree[tree[n].Dad].Len + 1;
if (bits > max_length) bits = max_length, overflow++;
tree[n].Len = (ush)bits;
/* We overwrite tree[n].Dad which is no longer needed */
@@ -536,20 +586,27 @@ local void gen_bitlen(s, desc)
s->bl_count[bits]++;
xbits = 0;
if (n >= base) xbits = extra[n - base];
f = tree[n].Freq;
s->opt_len += (ulg)f * (bits + xbits);
if (stree) s->static_len += (ulg)f * (stree[n].Len + xbits);
}
if (overflow == 0) return;
Trace((stderr, "\nbit length overflow\n"));
/* This happens for example on obj2 and pic of the Calgary corpus */
/* Find the first bit length which could increase: */
do {
do
{
bits = max_length - 1;
while (s->bl_count[bits] == 0) bits--;
s->bl_count[bits]--; /* move one leaf down the tree */
s->bl_count[bits + 1] += 2; /* move one overflow item as its brother */
s->bl_count[max_length]--;
@@ -557,24 +614,32 @@ local void gen_bitlen(s, desc)
* but this does not affect bl_count[max_length]
*/
overflow -= 2;
} while (overflow > 0);
}
while (overflow > 0);
/* Now recompute all bit lengths, scanning in increasing frequency.
* h is still equal to HEAP_SIZE. (It is simpler to reconstruct all
* lengths instead of fixing only the wrong ones. This idea is taken
* from 'ar' written by Haruhiko Okumura.)
*/
for (bits = max_length; bits != 0; bits--) {
for (bits = max_length; bits != 0; bits--)
{
n = s->bl_count[bits];
while (n != 0) {
while (n != 0)
{
m = s->heap[--h];
if (m > max_code) continue;
if ((unsigned) tree[m].Len != (unsigned) bits) {
if ((unsigned) tree[m].Len != (unsigned) bits)
{
Trace((stderr, "code %d bits %d->%d\n", m, tree[m].Len, bits));
s->opt_len += ((long)bits - (long)tree[m].Len)
* (long)tree[m].Freq;
tree[m].Len = (ush)bits;
}
n--;
}
}
@@ -605,9 +670,11 @@ local void gen_codes (tree, max_code, bl_count)
/* The distribution counts are first used to generate the code values
* without bit reversal.
*/
for (bits = 1; bits <= MAX_BITS; bits++) {
for (bits = 1; bits <= MAX_BITS; bits++)
{
next_code[bits] = code = (code + bl_count[bits - 1]) << 1;
}
/* Check that the bit counts in bl_count are consistent. The last code
* must be all ones.
*/
@@ -615,13 +682,16 @@ local void gen_codes (tree, max_code, bl_count)
"inconsistent bit counts");
Tracev((stderr, "\ngen_codes: max_code %d ", max_code));
for (n = 0; n <= max_code; n++) {
for (n = 0; n <= max_code; n++)
{
#ifdef WIN32
ush len = tree[n].Len;
#else
int len = tree[n].Len;
#endif
if (len == 0) continue;
/* Now reverse the bits */
#ifdef WIN32
tree[n].Code = (ush) bi_reverse(next_code[len]++, (int) len);
@@ -663,11 +733,15 @@ local void build_tree(s, desc)
*/
s->heap_len = 0, s->heap_max = HEAP_SIZE;
for (n = 0; n < elems; n++) {
if (tree[n].Freq != 0) {
for (n = 0; n < elems; n++)
{
if (tree[n].Freq != 0)
{
s->heap[++(s->heap_len)] = max_code = n;
s->depth[n] = 0;
} else {
}
else
{
tree[n].Len = 0;
}
}
@@ -677,13 +751,18 @@ local void build_tree(s, desc)
* possible code. So to avoid special checks later on we force at least
* two codes of non zero frequency.
*/
while (s->heap_len < 2) {
while (s->heap_len < 2)
{
node = s->heap[++(s->heap_len)] = (max_code < 2 ? ++max_code : 0);
tree[node].Freq = 1;
s->depth[node] = 0;
s->opt_len--; if (stree) s->static_len -= stree[node].Len;
s->opt_len--;
if (stree) s->static_len -= stree[node].Len;
/* node is 0 or 1 so it does not have extra bits */
}
desc->max_code = max_code;
/* The elements heap[heap_len/2+1 .. heap_len] are leaves of the tree,
@@ -695,7 +774,9 @@ local void build_tree(s, desc)
* frequent nodes.
*/
node = elems; /* next internal node of the tree */
do {
do
{
pqremove(s, tree, n); /* n = node of least frequency */
m = s->heap[SMALLEST]; /* m = node of next least frequency */
@@ -708,16 +789,20 @@ local void build_tree(s, desc)
s->depth[n] : s->depth[m]) + 1);
tree[n].Dad = tree[m].Dad = (ush)node;
#ifdef DUMP_BL_TREE
if (tree == s->bl_tree) {
if (tree == s->bl_tree)
{
fprintf(stderr, "\nnode %d(%d), sons %d(%d) %d(%d)",
node, tree[node].Freq, n, tree[n].Freq, m, tree[m].Freq);
}
#endif
/* and insert the new node in the heap */
s->heap[SMALLEST] = node++;
pqdownheap(s, tree, SMALLEST);
} while (s->heap_len >= 2);
}
while (s->heap_len >= 2);
s->heap[--(s->heap_max)] = s->heap[SMALLEST];
@@ -759,28 +844,50 @@ local void scan_tree (s, tree, max_code)
#endif
if (nextlen == 0) max_count = 138, min_count = 3;
tree[max_code + 1].Len = (ush)0xffff; /* guard */
for (n = 0; n <= max_code; n++) {
curlen = nextlen; nextlen = tree[n+1].Len;
if (++count < max_count && curlen == nextlen) {
for (n = 0; n <= max_code; n++)
{
curlen = nextlen;
nextlen = tree[n + 1].Len;
if (++count < max_count && curlen == nextlen)
{
continue;
} else if (count < min_count) {
}
else if (count < min_count)
{
s->bl_tree[curlen].Freq += count;
} else if (curlen != 0) {
}
else if (curlen != 0)
{
if (curlen != prevlen) s->bl_tree[curlen].Freq++;
s->bl_tree[REP_3_6].Freq++;
} else if (count <= 10) {
}
else if (count <= 10)
{
s->bl_tree[REPZ_3_10].Freq++;
} else {
}
else
{
s->bl_tree[REPZ_11_138].Freq++;
}
count = 0; prevlen = curlen;
if (nextlen == 0) {
count = 0;
prevlen = curlen;
if (nextlen == 0)
{
max_count = 138, min_count = 3;
} else if (curlen == nextlen) {
}
else if (curlen == nextlen)
{
max_count = 6, min_count = 3;
} else {
}
else
{
max_count = 7, min_count = 4;
}
}
@@ -810,32 +917,59 @@ local void send_tree (s, tree, max_code)
/* tree[max_code+1].Len = -1; */ /* guard already set */
if (nextlen == 0) max_count = 138, min_count = 3;
for (n = 0; n <= max_code; n++) {
curlen = nextlen; nextlen = tree[n+1].Len;
if (++count < max_count && curlen == nextlen) {
for (n = 0; n <= max_code; n++)
{
curlen = nextlen;
nextlen = tree[n + 1].Len;
if (++count < max_count && curlen == nextlen)
{
continue;
} else if (count < min_count) {
do { send_code(s, curlen, s->bl_tree); } while (--count != 0);
} else if (curlen != 0) {
if (curlen != prevlen) {
send_code(s, curlen, s->bl_tree); count--;
}
else if (count < min_count)
{
do { send_code(s, curlen, s->bl_tree); }
while (--count != 0);
}
else if (curlen != 0)
{
if (curlen != prevlen)
{
send_code(s, curlen, s->bl_tree);
count--;
}
Assert(count >= 3 && count <= 6, " 3_6?");
send_code(s, REP_3_6, s->bl_tree); send_bits(s, count-3, 2);
send_code(s, REP_3_6, s->bl_tree);
send_bits(s, count - 3, 2);
} else if (count <= 10) {
send_code(s, REPZ_3_10, s->bl_tree); send_bits(s, count-3, 3);
} else {
send_code(s, REPZ_11_138, s->bl_tree); send_bits(s, count-11, 7);
}
count = 0; prevlen = curlen;
if (nextlen == 0) {
else if (count <= 10)
{
send_code(s, REPZ_3_10, s->bl_tree);
send_bits(s, count - 3, 3);
}
else
{
send_code(s, REPZ_11_138, s->bl_tree);
send_bits(s, count - 11, 7);
}
count = 0;
prevlen = curlen;
if (nextlen == 0)
{
max_count = 138, min_count = 3;
} else if (curlen == nextlen) {
}
else if (curlen == nextlen)
{
max_count = 6, min_count = 3;
} else {
}
else
{
max_count = 7, min_count = 4;
}
}
@@ -868,9 +1002,11 @@ local int build_bl_tree(s)
* requires that at least 4 bit length codes be sent. (appnote.txt says
* 3 but the actual value used is 4.)
*/
for (max_blindex = BL_CODES-1; max_blindex >= 3; max_blindex--) {
for (max_blindex = BL_CODES - 1; max_blindex >= 3; max_blindex--)
{
if (s->bl_tree[bl_order[max_blindex]].Len != 0) break;
}
/* Update opt_len to include the bit length tree and counts */
s->opt_len += 3 * (max_blindex + 1) + 5 + 5 + 4;
Tracev((stderr, "\ndyn trees: dyn %ld, stat %ld",
@@ -901,10 +1037,13 @@ local void send_all_trees(s, lcodes, dcodes, blcodes)
send_bits(s, lcodes - 257, 5); /* not +255 as stated in appnote.txt */
send_bits(s, dcodes - 1, 5);
send_bits(s, blcodes - 4, 4); /* not -3 as stated in appnote.txt */
for (rank = 0; rank < blcodes; rank++) {
for (rank = 0; rank < blcodes; rank++)
{
Tracev((stderr, "\nbl code %2d ", bl_order[rank]));
send_bits(s, s->bl_tree[bl_order[rank]].Len, 3);
}
Tracev((stderr, "\nbl tree: sent %ld", s->bits_sent));
send_tree(s, (ct_data*)s->dyn_ltree, lcodes - 1); /* literal tree */
@@ -985,7 +1124,8 @@ void ZLIB_INTERNAL _tr_flush_block(s, buf, stored_len, last)
int max_blindex = 0; /* index of last bit length code of non zero freq */
/* Build the Huffman trees unless a stored block is forced */
if (s->level > 0) {
if (s->level > 0)
{
/* Check if the file is binary or text */
if (s->strm->data_type == Z_UNKNOWN)
@@ -1018,15 +1158,21 @@ void ZLIB_INTERNAL _tr_flush_block(s, buf, stored_len, last)
if (static_lenb <= opt_lenb) opt_lenb = static_lenb;
} else {
}
else
{
Assert(buf != (char*)0, "lost buf");
opt_lenb = static_lenb = stored_len + 5; /* force a stored block */
}
#ifdef FORCE_STORED
if (buf != (char*)0) { /* force stored block */
if (buf != (char*)0) /* force stored block */
{
#else
if (stored_len+4 <= opt_lenb && buf != (char*)0) {
if (stored_len + 4 <= opt_lenb && buf != (char*)0)
{
/* 4: two words for the lengths */
#endif
/* The test buf != NULL is only necessary if LIT_BUFSIZE > WSIZE.
@@ -1038,16 +1184,22 @@ void ZLIB_INTERNAL _tr_flush_block(s, buf, stored_len, last)
_tr_stored_block(s, buf, stored_len, last);
#ifdef FORCE_STATIC
} else if (static_lenb >= 0) { /* force static trees */
}
else if (static_lenb >= 0) /* force static trees */
{
#else
} else if (s->strategy == Z_FIXED || static_lenb == opt_lenb) {
}
else if (s->strategy == Z_FIXED || static_lenb == opt_lenb)
{
#endif
send_bits(s, (STATIC_TREES << 1) + last, 3);
compress_block(s, (ct_data*)static_ltree, (ct_data*)static_dtree);
#ifdef DEBUG
s->compressed_len += 3 + s->static_len;
#endif
} else {
}
else
{
send_bits(s, (DYN_TREES << 1) + last, 3);
send_all_trees(s, s->l_desc.max_code + 1, s->d_desc.max_code + 1,
max_blindex + 1);
@@ -1056,18 +1208,21 @@ void ZLIB_INTERNAL _tr_flush_block(s, buf, stored_len, last)
s->compressed_len += 3 + s->opt_len;
#endif
}
Assert(s->compressed_len == s->bits_sent, "bad compressed size");
/* The above check is made mod 2^32, for files larger than 512 MB
* and uLong implemented on 32 bits.
*/
init_block(s);
if (last) {
if (last)
{
bi_windup(s);
#ifdef DEBUG
s->compressed_len += 7; /* align on byte boundary */
#endif
}
Tracev((stderr, "\ncomprlen %lu(%lu) ", s->compressed_len >> 3,
s->compressed_len - 7 * last));
}
@@ -1087,10 +1242,14 @@ int ZLIB_INTERNAL _tr_tally (s, dist, lc)
{
s->d_buf[s->last_lit] = (ush)dist;
s->l_buf[s->last_lit++] = (uch)lc;
if (dist == 0) {
if (dist == 0)
{
/* lc is the unmatched char */
s->dyn_ltree[lc].Freq++;
} else {
}
else
{
s->matches++;
/* Here, lc is the match length - MIN_MATCH */
dist--; /* dist = match distance - 1 */
@@ -1103,22 +1262,29 @@ int ZLIB_INTERNAL _tr_tally (s, dist, lc)
}
#ifdef TRUNCATE_BLOCK
/* Try to guess if it is profitable to stop the current block here */
if ((s->last_lit & 0x1fff) == 0 && s->level > 2) {
if ((s->last_lit & 0x1fff) == 0 && s->level > 2)
{
/* Compute an upper bound for the compressed length */
ulg out_length = (ulg)s->last_lit * 8L;
ulg in_length = (ulg)((long)s->strstart - s->block_start);
int dcode;
for (dcode = 0; dcode < D_CODES; dcode++) {
for (dcode = 0; dcode < D_CODES; dcode++)
{
out_length += (ulg)s->dyn_dtree[dcode].Freq *
(5L + extra_dbits[dcode]);
}
out_length >>= 3;
Tracev((stderr, "\nlast_lit %u, in %ld, out ~%ld(%ld%%) ",
s->last_lit, in_length, out_length,
100L - out_length * 100L / in_length));
if (s->matches < s->last_lit / 2 && out_length < in_length / 2) return 1;
}
#endif
return (s->last_lit == s->lit_bufsize - 1);
/* We avoid equality with lit_bufsize because of wraparound at 64K
@@ -1145,28 +1311,38 @@ local void compress_block(s, ltree, dtree)
unsigned code; /* the code to send */
int extra; /* number of extra bits to send */
if (s->last_lit != 0) do {
if (s->last_lit != 0) do
{
dist = s->d_buf[lx];
lc = s->l_buf[lx++];
if (dist == 0) {
if (dist == 0)
{
send_code(s, lc, ltree); /* send a literal byte */
Tracecv(isgraph(lc), (stderr, " '%c' ", lc));
} else {
}
else
{
/* Here, lc is the match length - MIN_MATCH */
code = _length_code[lc];
send_code(s, code + LITERALS + 1, ltree); /* send the length code */
extra = extra_lbits[code];
if (extra != 0) {
if (extra != 0)
{
lc -= base_length[code];
send_bits(s, lc, extra); /* send the extra length bits */
}
dist--; /* dist is now the match distance - 1 */
code = d_code(dist);
Assert(code < D_CODES, "bad d_code");
send_code(s, code, dtree); /* send the distance code */
extra = extra_dbits[code];
if (extra != 0) {
if (extra != 0)
{
dist -= base_dist[code];
send_bits(s, dist, extra); /* send the extra distance bits */
}
@@ -1176,7 +1352,8 @@ local void compress_block(s, ltree, dtree)
Assert((uInt)(s->pending) < s->lit_bufsize + 2 * lx,
"pendingBuf overflow");
} while (lx < s->last_lit);
}
while (lx < s->last_lit);
send_code(s, END_BLOCK, ltree);
}
@@ -1217,6 +1394,7 @@ local int detect_data_type(s)
if (s->dyn_ltree[9].Freq != 0 || s->dyn_ltree[10].Freq != 0
|| s->dyn_ltree[13].Freq != 0)
return Z_TEXT;
for (n = 32; n < LITERALS; n++)
if (s->dyn_ltree[n].Freq != 0)
return Z_TEXT;
@@ -1241,10 +1419,14 @@ local unsigned bi_reverse(code, len)
#endif
{
register unsigned res = 0;
do {
do
{
res |= code & 1;
code >>= 1, res <<= 1;
} while (--len > 0);
}
while (--len > 0);
return res >> 1;
}
@@ -1258,11 +1440,14 @@ local void bi_flush(s)
deflate_state* s;
#endif
{
if (s->bi_valid == 16) {
if (s->bi_valid == 16)
{
put_short(s, s->bi_buf);
s->bi_buf = 0;
s->bi_valid = 0;
} else if (s->bi_valid >= 8) {
}
else if (s->bi_valid >= 8)
{
put_byte(s, (Byte)s->bi_buf);
s->bi_buf >>= 8;
s->bi_valid -= 8;
@@ -1279,11 +1464,15 @@ local void bi_windup(s)
deflate_state* s;
#endif
{
if (s->bi_valid > 8) {
if (s->bi_valid > 8)
{
put_short(s, s->bi_buf);
} else if (s->bi_valid > 0) {
}
else if (s->bi_valid > 0)
{
put_byte(s, (Byte)s->bi_buf);
}
s->bi_buf = 0;
s->bi_valid = 0;
#ifdef DEBUG
@@ -1307,17 +1496,21 @@ local void copy_block(s, buf, len, header)
{
bi_windup(s); /* align on byte boundary */
if (header) {
if (header)
{
put_short(s, (ush)len);
put_short(s, (ush)~len);
#ifdef DEBUG
s->bits_sent += 2 * 16;
#endif
}
#ifdef DEBUG
s->bits_sent += (ulg)len << 3;
#endif
while (len--) {
while (len--)
{
put_byte(s, *buf++);
}
}

18
extern/zlib/trees.h vendored
View File

@@ -1,6 +1,7 @@
/* header created automatically with -DGEN_TREES_H */
local const ct_data static_ltree[L_CODES+2] = {
local const ct_data static_ltree[L_CODES + 2] =
{
{{ 12}, { 8}}, {{140}, { 8}}, {{ 76}, { 8}}, {{204}, { 8}}, {{ 44}, { 8}},
{{172}, { 8}}, {{108}, { 8}}, {{236}, { 8}}, {{ 28}, { 8}}, {{156}, { 8}},
{{ 92}, { 8}}, {{220}, { 8}}, {{ 60}, { 8}}, {{188}, { 8}}, {{124}, { 8}},
@@ -61,7 +62,8 @@ local const ct_data static_ltree[L_CODES+2] = {
{{163}, { 8}}, {{ 99}, { 8}}, {{227}, { 8}}
};
local const ct_data static_dtree[D_CODES] = {
local const ct_data static_dtree[D_CODES] =
{
{{ 0}, { 5}}, {{16}, { 5}}, {{ 8}, { 5}}, {{24}, { 5}}, {{ 4}, { 5}},
{{20}, { 5}}, {{12}, { 5}}, {{28}, { 5}}, {{ 2}, { 5}}, {{18}, { 5}},
{{10}, { 5}}, {{26}, { 5}}, {{ 6}, { 5}}, {{22}, { 5}}, {{14}, { 5}},
@@ -70,7 +72,8 @@ local const ct_data static_dtree[D_CODES] = {
{{19}, { 5}}, {{11}, { 5}}, {{27}, { 5}}, {{ 7}, { 5}}, {{23}, { 5}}
};
const uch ZLIB_INTERNAL _dist_code[DIST_CODE_LEN] = {
const uch ZLIB_INTERNAL _dist_code[DIST_CODE_LEN] =
{
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, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
@@ -99,7 +102,8 @@ const uch ZLIB_INTERNAL _dist_code[DIST_CODE_LEN] = {
29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29
};
const uch ZLIB_INTERNAL _length_code[MAX_MATCH-MIN_MATCH+1]= {
const uch ZLIB_INTERNAL _length_code[MAX_MATCH - MIN_MATCH + 1] =
{
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, 16, 16, 16,
17, 17, 17, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18, 18, 18, 18, 19, 19, 19, 19,
@@ -115,12 +119,14 @@ const uch ZLIB_INTERNAL _length_code[MAX_MATCH-MIN_MATCH+1]= {
27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 28
};
local const int base_length[LENGTH_CODES] = {
local const int base_length[LENGTH_CODES] =
{
0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16, 20, 24, 28, 32, 40, 48, 56,
64, 80, 96, 112, 128, 160, 192, 224, 0
};
local const int base_dist[D_CODES] = {
local const int base_dist[D_CODES] =
{
0, 1, 2, 3, 4, 6, 8, 12, 16, 24,
32, 48, 64, 96, 128, 192, 256, 384, 512, 768,
1024, 1536, 2048, 3072, 4096, 6144, 8192, 12288, 16384, 24576

10
extern/zlib/uncompr.c vendored
View File

@@ -36,26 +36,34 @@ int ZEXPORT uncompress (dest, destLen, source, sourceLen)
stream.next_in = (Bytef*)source;
stream.avail_in = (uInt)sourceLen;
/* Check for source > 64K on 16-bit machine: */
if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR;
stream.next_out = dest;
stream.avail_out = (uInt) * destLen;
if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR;
stream.zalloc = (alloc_func)0;
stream.zfree = (free_func)0;
err = inflateInit(&stream);
if (err != Z_OK) return err;
err = inflate(&stream, Z_FINISH);
if (err != Z_STREAM_END) {
if (err != Z_STREAM_END)
{
inflateEnd(&stream);
if (err == Z_NEED_DICT || (err == Z_BUF_ERROR && stream.avail_in == 0))
return Z_DATA_ERROR;
return err;
}
*destLen = stream.total_out;
err = inflateEnd(&stream);

9
extern/zlib/zlib.h vendored
View File

@@ -107,7 +107,8 @@ typedef void (*free_func) OF((voidpf opaque, voidpf address));
struct internal_state;
typedef struct z_stream_s {
typedef struct z_stream_s
{
z_const Bytef* next_in; /* next input byte */
uInt avail_in; /* number of bytes available at next_in */
uLong total_in; /* total number of input bytes read so far */
@@ -134,7 +135,8 @@ typedef z_stream FAR *z_streamp;
gzip header information passed to and from zlib routines. See RFC 1952
for more details on the meanings of these fields.
*/
typedef struct gz_header_s {
typedef struct gz_header_s
{
int text; /* true if compressed data believed to be text */
uLong time; /* modification time */
int xflags; /* extra flags (not used when writing a gzip file) */
@@ -1683,7 +1685,8 @@ ZEXTERN int ZEXPORT inflateBackInit_ OF((z_streamp strm, int windowBits,
* behavior could change in the future, perhaps even capriciously. They can
* only be used by the gzgetc() macro. You have been warned.
*/
struct gzFile_s {
struct gzFile_s
{
unsigned have;
unsigned char* next;
z_off64_t pos;

31
extern/zlib/zlib.pri vendored
View File

@@ -1,31 +0,0 @@
SOURCES += \
$$PWD/adler32.c \
$$PWD/compress.c \
$$PWD/crc32.c \
$$PWD/deflate.c \
$$PWD/gzclose.c \
$$PWD/gzlib.c \
$$PWD/gzread.c \
$$PWD/gzwrite.c \
$$PWD/infback.c \
$$PWD/inffast.c \
$$PWD/inflate.c \
$$PWD/inftrees.c \
$$PWD/trees.c \
$$PWD/uncompr.c \
$$PWD/zutil.c
HEADERS += \
$$PWD/crc32.h \
$$PWD/deflate.h \
$$PWD/gzguts.h \
$$PWD/inffast.h \
$$PWD/inffixed.h \
$$PWD/inflate.h \
$$PWD/inftrees.h \
$$PWD/trees.h \
$$PWD/zconf.h \
$$PWD/zlib.h \
$$PWD/zutil.h
INCLUDEPATH += $$PWD

140
extern/zlib/zutil.c vendored
View File

@@ -14,7 +14,8 @@
struct internal_state {int dummy;}; /* for buggy compilers */
#endif
const char * const z_errmsg[10] = {
const char* const z_errmsg[10] =
{
"need dictionary", /* Z_NEED_DICT 2 */
"stream end", /* Z_STREAM_END 1 */
"", /* Z_OK 0 */
@@ -24,7 +25,8 @@ const char * const z_errmsg[10] = {
"insufficient memory", /* Z_MEM_ERROR (-4) */
"buffer error", /* Z_BUF_ERROR (-5) */
"incompatible version",/* Z_VERSION_ERROR (-6) */
""};
""
};
const char* ZEXPORT zlibVersion()
@@ -37,30 +39,75 @@ uLong ZEXPORT zlibCompileFlags()
uLong flags;
flags = 0;
switch ((int)(sizeof(uInt))) {
case 2: break;
case 4: flags += 1; break;
case 8: flags += 2; break;
default: flags += 3;
switch ((int)(sizeof(uInt)))
{
case 2:
break;
case 4:
flags += 1;
break;
case 8:
flags += 2;
break;
default:
flags += 3;
}
switch ((int)(sizeof(uLong))) {
case 2: break;
case 4: flags += 1 << 2; break;
case 8: flags += 2 << 2; break;
default: flags += 3 << 2;
switch ((int)(sizeof(uLong)))
{
case 2:
break;
case 4:
flags += 1 << 2;
break;
case 8:
flags += 2 << 2;
break;
default:
flags += 3 << 2;
}
switch ((int)(sizeof(voidpf))) {
case 2: break;
case 4: flags += 1 << 4; break;
case 8: flags += 2 << 4; break;
default: flags += 3 << 4;
switch ((int)(sizeof(voidpf)))
{
case 2:
break;
case 4:
flags += 1 << 4;
break;
case 8:
flags += 2 << 4;
break;
default:
flags += 3 << 4;
}
switch ((int)(sizeof(z_off_t))) {
case 2: break;
case 4: flags += 1 << 6; break;
case 8: flags += 2 << 6; break;
default: flags += 3 << 6;
switch ((int)(sizeof(z_off_t)))
{
case 2:
break;
case 4:
flags += 1 << 6;
break;
case 8:
flags += 2 << 6;
break;
default:
flags += 3 << 6;
}
#ifdef DEBUG
flags += 1 << 8;
#endif
@@ -159,9 +206,12 @@ void ZLIB_INTERNAL zmemcpy(dest, source, len)
uInt len;
{
if (len == 0) return;
do {
do
{
*dest++ = *source++; /* ??? to be unrolled */
} while (--len != 0);
}
while (--len != 0);
}
int ZLIB_INTERNAL zmemcmp(s1, s2, len)
@@ -171,9 +221,11 @@ int ZLIB_INTERNAL zmemcmp(s1, s2, len)
{
uInt j;
for (j = 0; j < len; j++) {
for (j = 0; j < len; j++)
{
if (s1[j] != s2[j]) return 2 * (s1[j] > s2[j]) - 1;
}
return 0;
}
@@ -182,9 +234,12 @@ void ZLIB_INTERNAL zmemzero(dest, len)
uInt len;
{
if (len == 0) return;
do {
do
{
*dest++ = 0; /* ??? to be unrolled */
} while (--len != 0);
}
while (--len != 0);
}
#endif
@@ -208,7 +263,8 @@ void ZLIB_INTERNAL zmemzero(dest, len)
local int next_ptr = 0;
typedef struct ptr_table_s {
typedef struct ptr_table_s
{
voidpf org_ptr;
voidpf new_ptr;
} ptr_table;
@@ -229,13 +285,19 @@ voidpf ZLIB_INTERNAL zcalloc (voidpf opaque, unsigned items, unsigned size)
/* If we allocate less than 65520 bytes, we assume that farmalloc
* will return a usable pointer which doesn't have to be normalized.
*/
if (bsize < 65520L) {
if (bsize < 65520L)
{
buf = farmalloc(bsize);
if (*(ush*)&buf != 0) return buf;
} else {
}
else
{
buf = farmalloc(bsize + 16L);
}
if (buf == NULL || next_ptr >= MAX_PTR) return NULL;
table[next_ptr].org_ptr = buf;
/* Normalize the pointer to seg:0 */
@@ -248,21 +310,29 @@ voidpf ZLIB_INTERNAL zcalloc (voidpf opaque, unsigned items, unsigned size)
void ZLIB_INTERNAL zcfree(voidpf opaque, voidpf ptr)
{
int n;
if (*(ush*)&ptr != 0) { /* object < 64K */
if (*(ush*)&ptr != 0) /* object < 64K */
{
farfree(ptr);
return;
}
/* Find the original pointer */
for (n = 0; n < next_ptr; n++) {
for (n = 0; n < next_ptr; n++)
{
if (ptr != table[n].new_ptr) continue;
farfree(table[n].org_ptr);
while (++n < next_ptr) {
while (++n < next_ptr)
{
table[n - 1] = table[n];
}
next_ptr--;
return;
}
ptr = opaque; /* just to make some compilers happy */
Assert(0, "zcfree: ptr not found");
}
@@ -283,12 +353,14 @@ void ZLIB_INTERNAL zcfree (voidpf opaque, voidpf ptr)
voidpf ZLIB_INTERNAL zcalloc(voidpf opaque, uInt items, uInt size)
{
if (opaque) opaque = 0; /* to make compiler happy */
return _halloc((long)items, size);
}
void ZLIB_INTERNAL zcfree(voidpf opaque, voidpf ptr)
{
if (opaque) opaque = 0; /* to make compiler happy */
_hfree(ptr);
}
@@ -315,6 +387,7 @@ voidpf ZLIB_INTERNAL zcalloc (opaque, items, size)
#endif
{
if (opaque) items += size - size; /* make compiler happy */
return sizeof(uInt) > 2 ? (voidpf)malloc(items * size) :
(voidpf)calloc(items, size);
}
@@ -328,6 +401,7 @@ void ZLIB_INTERNAL zcfree (opaque, ptr)
#endif
{
free(ptr);
if (opaque) return; /* make compiler happy */
}

View File

@@ -35,9 +35,9 @@ namespace io
* all work is done using a memory buffer, and not read directly from the disk.
* \sa BinaryReader
*/
class ALTTPFileReader : protected MemoryReader
class ALTTPFileReader : protected MemoryCopyReader
{
MEMORYREADER_BASE();
MEMORYCOPYREADER_BASE();
public:
/*! \brief This constructor takes an existing buffer to read from.

View File

@@ -34,9 +34,9 @@ namespace io
* all work is done using a memory buffer, and not written directly to the disk.
* \sa BinaryReader
*/
class ALTTPFileWriter : protected MemoryWriter
class ALTTPFileWriter : protected MemoryCopyWriter
{
MEMORYWRITER_BASE();
MEMORYCOPYWRITER_BASE();
public:
/*! \brief This constructor takes an existing buffer to write to.

View File

@@ -337,7 +337,8 @@ public:
* \brief setCrystals
* \param val
*/
void setCrystals(ALTTPCrystals val);\
void setCrystals(ALTTPCrystals val);
\
/*!
* \brief crystals

View File

@@ -1,18 +1,3 @@
// This file is part of libAthena.
//
// libAthena is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// libAthena is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with libAthena. If not, see <http://www.gnu.org/licenses/>
#ifndef CHECKSUMS_HPP
#define CHECKSUMS_HPP

View File

@@ -1,18 +1,3 @@
// This file is part of libAthena.
//
// libAthena is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// libAthena is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with libAthena. If not, see <http://www.gnu.org/licenses/>
#ifndef COMPRESSION_HPP
#define COMPRESSION_HPP

163
include/Athena/DNA.hpp Normal file
View File

@@ -0,0 +1,163 @@
#ifndef DNA_HPP
#define DNA_HPP
/* BIG FAT WARNING!!!
*
* The type-structure of this file is expected to remain consistent for 'atdna'
* Any changes to the types or namespacing must be reflected in 'atdna/main.cpp'
*/
#include "Global.hpp"
#include "IStreamReader.hpp"
#include "IStreamWriter.hpp"
#include <vector>
#include <memory>
namespace Athena
{
namespace io
{
/* forward-declaration dance for recursively-derived types */
template <size_t sizeVar, Endian VE>
struct Buffer;
template <atInt32 sizeVar, Endian VE>
struct String;
template <atInt32 sizeVar, Endian VE>
struct WString;
template <atInt32 sizeVar, Endian VE>
struct WStringAsString;
/**
* @brief Base DNA class used against 'atdna'
*
* Athena bundles a build-tool called 'atdna'. This tool functions
* just like the 'clang' compiler, except it emits a full .cpp implementation
* with all read/write calls necessary to marshal the DNA structure to/from
* a streamed medium
*/
template <Endian DNAE>
struct DNA
{
virtual void read(IStreamReader&)=0;
virtual void write(IStreamWriter&) const=0;
template <typename T, Endian VE = DNAE>
using Value = T;
template <typename T, size_t cntVar, Endian VE = DNAE>
using Vector = std::vector<T>;
template <size_t sizeVar>
using Buffer = struct Buffer<sizeVar, DNAE>;
template <atInt32 sizeVar = -1>
using String = struct String<sizeVar, DNAE>;
template <atInt32 sizeVar = -1, Endian VE = DNAE>
using WString = struct WString<sizeVar, VE>;
template <atInt32 sizeVar = -1>
using WStringAsString = struct WStringAsString<sizeVar, DNAE>;
template <off_t offset, SeekOrigin direction>
struct Seek {};
template <size_t align>
struct Align {};
struct Delete {};
};
/* Concrete DNA types */
template <size_t sizeVar, Endian VE>
struct Buffer : public DNA<VE>, public std::unique_ptr<atUint8[]>
{
typename DNA<VE>::Delete expl;
inline void read(IStreamReader& reader)
{
reset(new atUint8[sizeVar]);
reader.readUBytesToBuf(get(), sizeVar);
}
inline void write(IStreamWriter& writer) const
{
writer.writeUBytes(get(), sizeVar);
}
};
template <atInt32 sizeVar, Endian VE>
struct String : public DNA<VE>, public std::string
{
typename DNA<VE>::Delete expl;
inline void read(IStreamReader& reader)
{*this = reader.readString(sizeVar);}
inline void write(IStreamWriter& writer) const
{writer.writeString(*this, sizeVar);}
inline std::string& operator=(const std::string& __str)
{return this->assign(__str);}
inline std::string& operator=(std::string&& __str)
{this->swap(__str); return *this;}
};
template <atInt32 sizeVar, Endian VE>
struct WString : public DNA<VE>, public std::wstring
{
typename DNA<VE>::Delete expl;
inline void read(IStreamReader& reader)
{
reader.setEndian(VE);
*this = reader.readWString(sizeVar);
}
inline void write(IStreamWriter& writer) const
{
writer.setEndian(VE);
writer.writeWString(*this, sizeVar);
}
inline std::wstring& operator=(const std::wstring& __str)
{return this->assign(__str);}
inline std::wstring& operator=(std::wstring&& __str)
{this->swap(__str); return *this;}
};
template <atInt32 sizeVar, Endian VE>
struct WStringAsString : public DNA<VE>, public std::string
{
typename DNA<VE>::Delete expl;
inline void read(IStreamReader& reader)
{*this = reader.readWStringAsString(sizeVar);}
inline void write(IStreamWriter& writer) const
{writer.writeStringAsWString(*this, sizeVar);}
inline std::string& operator=(const std::string& __str)
{return this->assign(__str);}
inline std::string& operator=(std::string&& __str)
{this->swap(__str); return *this;}
};
/** Macro to automatically declare read/write methods in subclasses */
#define DECL_DNA \
void read(Athena::io::IStreamReader&); \
void write(Athena::io::IStreamWriter&) const; \
/** Macro to automatically declare read/write methods and prevent outputting implementation */
#define DECL_EXPLICIT_DNA \
void read(Athena::io::IStreamReader&); \
void write(Athena::io::IStreamWriter&) const; \
Delete __dna_delete;
/** Macro to supply count variable to atdna and mute it for other compilers */
#ifdef __clang__
#define DNA_COUNT(cnt) sizeof(cnt)
#else
#define DNA_COUNT(cnt) 0
#endif
}
}
#endif // DNA_HPP

38
include/Athena/Dir.hpp Normal file
View File

@@ -0,0 +1,38 @@
#ifndef DIR_HPP
#define DIR_HPP
#include "Athena/FileInfo.hpp"
#include <stdio.h>
#if _WIN32
typedef int mode_t;
#endif
namespace Athena
{
class Dir
{
public:
explicit Dir(const std::string& path);
std::string absolutePath() const;
static inline std::string absolutePath(const std::string& path)
{ return Dir(path).absolutePath(); }
bool isDir() const;
static bool isDir(const std::string dir)
{ return Dir(dir).isDir(); }
std::vector<FileInfo> files() const;
bool cd(const std::string& path);
bool rm(const std::string& path);
bool touch();
static bool mkdir(const std::string& dir, mode_t mode = 0755);
static bool mkpath(const std::string& path, mode_t mode = 0755);
private:
std::string m_path;
};
}
#endif // DIR_HPP

View File

@@ -1,18 +1,3 @@
// This file is part of libAthena.
//
// libAthena is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// libAthena is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with libAthena. If not, see <http://www.gnu.org/licenses/>
#ifndef EXCEPTION_HPP
#define EXCEPTION_HPP
@@ -45,7 +30,8 @@ public:
m_message(message),
m_file(file),
m_function(function),
m_line(line)
m_line(line),
m_exceptionName("Exception")
{
}
@@ -54,7 +40,7 @@ public:
*/
inline std::string message() const
{
return m_message;
return m_exceptionName + (m_message.empty() ? "" : ": " + m_message);
}
inline std::string file() const
@@ -74,27 +60,56 @@ public:
inline std::string formattedMessage() const
{
return Athena::utility::sprintf("%s : %s (%i) %s", m_file.c_str(), m_function.c_str(), m_line, m_message.c_str());
return Athena::utility::sprintf("%s : %s (%i) %s", m_file.c_str(), m_function.c_str(), m_line, message().c_str());
}
protected:
std::string m_message; //!< The error message string
std::string m_file;
std::string m_function;
int m_line;
std::string m_exceptionName;
};
} // error
} // Athena
#ifdef _MSC_VER
#define THROW_EXCEPTION(args,...) \
do { \
std::string msg = Athena::utility::sprintf(args, __VA_ARGS__); \
if (atGetExceptionHandler()) {atGetExceptionHandler()(__FILE__, AT_PRETTY_FUNCTION, __LINE__, __VA_ARGS__); return; \
} else { std::string msg = Athena::utility::sprintf(__VA_ARGS__); \
throw Athena::error::Exception(std::string("Exception: ")+msg, __FILE__, AT_PRETTY_FUNCTION, __LINE__); \
} \
} while(0)
#elif defined(__GNUC__)
#define THROW_EXCEPTION(args...) \
do { \
if (atGetExceptionHandler()) { atGetExceptionHandler()(__FILE__, AT_PRETTY_FUNCTION, __LINE__, args); return; \
} else { \
std::string msg = Athena::utility::sprintf(args); \
throw Athena::error::Exception(msg, __FILE__, AT_PRETTY_FUNCTION, __LINE__); \
} \
} while(0)
#endif
#ifdef _MSC_VER
#define THROW_EXCEPTION_RETURN(ret, args,...) \
do { \
if (atGetExceptionHandler()) \
{ \
atGetExceptionHandler()(__FILE__, AT_PRETTY_FUNCTION, __LINE__, __VA_ARGS__); \
return ret; \
} else { \
std::string msg = Athena::utility::sprintf(__VA_ARGS__); \
throw Athena::error::Exception(std::string("Exception: ")+msg, __FILE__, AT_PRETTY_FUNCTION, __LINE__); \
} \
} while(0)
#elif defined(__GNUC__)
#define THROW_EXCEPTION_RETURN(ret, args...) \
do { \
if (atGetExceptionHandler()) { atGetExceptionHandler()(__FILE__, AT_PRETTY_FUNCTION, __LINE__, args); return ret; \
} else { \
std::string msg = Athena::utility::sprintf(args); \
throw Athena::error::Exception(msg, __FILE__, AT_PRETTY_FUNCTION, __LINE__); \
} \
} while(0)
#endif

View File

@@ -0,0 +1,59 @@
#ifndef FILEINFO_HPP
#define FILEINFO_HPP
#include <string>
#include "Athena/Global.hpp"
namespace Athena
{
class FileInfo
{
public:
explicit FileInfo(const std::string& path = std::string());
std::string absolutePath() const;
static inline std::string absolutePath(const std::string& lnk)
{ return FileInfo(lnk).absolutePath(); }
std::string absoluteFilePath() const;
static inline std::string absoluteFilePath(const std::string& path)
{ return FileInfo(path).absoluteFilePath(); }
std::string filename() const;
static inline std::string filename(const std::string path)
{ return FileInfo(path).filename(); }
std::string path() const;
static inline std::string path(const std::string path)
{ return FileInfo(path).path(); }
std::string extension() const;
static inline std::string extension(const std::string path)
{ return FileInfo(path).extension(); }
atUint64 size() const;
static inline atUint64 size(const std::string path)
{ return FileInfo(path).size(); }
bool exists() const;
static inline bool exists(const std::string& path)
{ return FileInfo(path).exists(); }
bool isLink() const;
static inline bool isLink(const std::string& lnk)
{ return FileInfo(lnk).isLink(); }
bool isFile() const;
static inline bool isFile(const std::string& path)
{ return FileInfo(path).isFile(); }
bool touch() const;
static inline bool touch(const std::string& path)
{ return FileInfo(path).touch(); }
private:
std::string m_path;
};
}
#endif // FILEINFO_HPP

View File

@@ -1,18 +1,3 @@
// This file is part of libAthena.
//
// libAthena is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// libAthena is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with libAthena. If not, see <http://www.gnu.org/licenses/>
#ifndef FILENOTFOUNDEXCEPTION_HPP
#define FILENOTFOUNDEXCEPTION_HPP
@@ -39,7 +24,9 @@ public:
inline FileNotFoundException(const std::string& filename, const std::string& file, const std::string& function, const int line) :
Exception(std::string("FileNotFoundException: Could not find file \"") + filename + std::string("\", please check that it exists."), file, function, line),
m_filename(filename)
{}
{
m_exceptionName = "FileNotFoundException";
}
/*! \brief Returns the path of the offending file.
* \return std::string The filename of the file including the path.
@@ -51,7 +38,31 @@ private:
} // error
} // Athena
#ifndef THROW_FILE_NOT_FOUND_EXCEPTION
#define THROW_FILE_NOT_FOUND_EXCEPTION(msg) \
do { throw Athena::error::FileNotFoundException(msg, __FILE__, AT_PRETTY_FUNCTION, __LINE__); } while(0)
do { \
if (atGetExceptionHandler()) \
{ \
atGetExceptionHandler()(__FILE__, AT_PRETTY_FUNCTION, __LINE__, msg); \
return; \
} \
else \
throw Athena::error::FileNotFoundException(msg, __FILE__, AT_PRETTY_FUNCTION, __LINE__); \
} while(0)
#endif
#ifndef THROW_FILE_NOT_FOUND_EXCEPTION_RETURN
#define THROW_FILE_NOT_FOUND_EXCEPTION_RETURN(ret, msg) \
do { \
if (atGetExceptionHandler()) \
{ \
atGetExceptionHandler()(__FILE__, AT_PRETTY_FUNCTION, __LINE__, msg); \
return ret; \
} \
else \
throw Athena::error::FileNotFoundException(msg, __FILE__, AT_PRETTY_FUNCTION, __LINE__); \
} while(0)
#endif
#endif // FILENOTFOUNDEXCEPTION_HPP

View File

@@ -1,24 +1,10 @@
// This file is part of libAthena.
//
// libAthena is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// libAthena is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with libAthena. If not, see <http://www.gnu.org/licenses/>
#ifndef FILESTREAM_HPP
#define FILESTREAM_HPP
#include "Athena/IStreamReader.hpp"
#include <string>
#include <cstdio>
#include <memory>
#include <stdio.h>
#include "Athena/IStreamReader.hpp"
namespace Athena
{
@@ -27,51 +13,29 @@ namespace io
class FileReader : public IStreamReader
{
public:
FileReader(const std::string& filename);
FileReader(const std::string& filename, atInt32 cacheSize = (32 * 1024));
virtual ~FileReader();
std::string filename() const;
inline const std::string& filename() const
{return m_filename;}
void setEndian(Endian endian);
Endian endian() const;
bool isBigEndian() const;
bool isLittleEndian() const;
void open();
void close();
bool isOpen() const;
inline bool isOpen() const
{return m_fileHandle != NULL;}
bool save();
void seek(atInt64 pos, SeekOrigin origin = SeekOrigin::Current);
inline void seekAlign32() {seek(ROUND_UP_32(position()), SeekOrigin::Begin);}
bool atEnd() const;
atUint64 position() const;
atUint64 length() const;
void seekBit(int);
bool readBit();
atUint8 readUByte();
atInt8 readByte();
atUint8* readUBytes(atUint64 len);
atInt8* readBytes(atUint64 len);
atUint64 readBytesToBuf(void* buf, atUint64 len) {return readUBytesToBuf(buf, len);}
atUint64 readUBytesToBuf(void* buf, atUint64 len);
atUint16 readUint16();
atInt16 readInt16();
atUint32 readUint32();
atInt32 readInt32();
atUint64 readUint64();
atInt64 readInt64();
double readDouble();
float readFloat();
bool readBool();
std::string readString(atInt32 maxlen = -1);
std::string readUnicode(atInt32 maxlen = -1);
void setCacheSize(const atInt32 blockSize);
protected:
std::string m_filename;
FILE* m_fileHandle;
Endian m_endian;
atUint8 m_currentByte;
atUint8 m_bitShift;
bool m_bitValid;
std::unique_ptr<atUint8[]> m_cacheData;
atInt32 m_blockSize;
atInt32 m_curBlock;
atUint64 m_offset;
};
} // io
} // Athena

View File

@@ -1,22 +1,8 @@
// This file is part of libAthena.
//
// libAthena is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// libAthena is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with libAthena. If not, see <http://www.gnu.org/licenses/>
#ifndef FILEWRITER_HPP
#define FILEWRITER_HPP
#include "Athena/IStreamWriter.hpp"
#include <stdio.h>
namespace Athena
{
@@ -25,50 +11,23 @@ namespace io
class FileWriter : public IStreamWriter
{
public:
FileWriter(const std::string& filename);
FileWriter(const std::string& filename, bool overwrite = true);
virtual ~FileWriter();
void setEndian(Endian endian);
Endian endian() const;
bool isBigEndian() const;
bool isLittleEndian() const;
void open();
void open(bool overwrite = true);
void close();
bool isOpen() const;
bool save();
inline bool isOpen() const
{return m_fileHandle != NULL;}
void seek(atInt64 pos, SeekOrigin origin = SeekOrigin::Current);
inline void seekAlign32() {seek(ROUND_UP_32(position()), SeekOrigin::Begin);}
bool atEnd() const;
atUint64 position() const;
atUint64 length() const;
void writeUBytes(const atUint8* data, atUint64 len);
void writeBit (bool val);
void seekBit (int bit);
void writeUByte (atUint8 val);
void writeByte (atInt8 val);
void writeUBytes(atUint8* data, atUint64 len);
void writeBytes (atInt8* data, atUint64 len);
void writeUint16(atUint16 val);
void writeInt16 (atInt16 val);
void writeUint32(atUint32 val);
void writeInt32 (atInt32 val);
void writeUint64(atUint64 val);
void writeInt64 (atInt64 val);
void writeDouble(double val);
void writeFloat (float val);
void writeBool (bool val);
void writeString(const std::string& val);
void writeUnicode(const std::string& str);
void fill(atInt8 byte, atUint64 len);
void fill(atUint8 byte, atUint64 len);
private:
std::string m_filename;
FILE* m_fileHandle;
Endian m_endian;
atUint8 m_currentByte;
atUint64 m_bytePosition;
atUint8 m_bitShift;
bool m_bitValid;
};
}
} // Athena

View File

@@ -1,27 +1,27 @@
// This file is part of libAthena.
//
// libAthena is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// libAthena is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with libAthena. If not, see <http://www.gnu.org/licenses/>
#ifndef GLOBAL_HPP
#define GLOBAL_HPP
#include <iostream>
#include "Athena/Types.hpp"
#include "Athena/Utility.hpp"
#include <iostream>
#ifdef _MSC_VER
#pragma warning(disable : 4996)
#include <sys/stat.h>
#if !defined(S_ISREG) && defined(S_IFMT) && defined(S_IFREG)
#define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
#endif
#if !defined(S_ISDIR) && defined(S_IFMT) && defined(S_IFDIR)
#define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
#endif
#if !defined(S_ISLNK)
#define S_ISLNK(m) 0
#endif
#endif
#ifndef AT_PRETTY_FUNCTION
@@ -40,6 +40,17 @@
# endif
#endif
#ifdef GEKKO
#include "gekko_support.h"
typedef struct stat stat64_t;
#define stat64 stat
#elif _WIN32
typedef struct _stat64 stat64_t;
#else
typedef struct stat64 stat64_t;
#endif
#ifndef aDebug
#define aDebug() \
std::cout << __FILE__ << "(" << __LINE__ << ") " << AT_PRETTY_FUNCTION << ": "
@@ -64,14 +75,14 @@
namespace Athena
{
enum class SeekOrigin
enum SeekOrigin
{
Begin,
Current,
End
};
enum class Endian
enum Endian
{
LittleEndian,
BigEndian
@@ -107,6 +118,11 @@ typedef Vector2D<float> Vector2Df;
#endif // ATHENA_NO_SAKURA
} // Athena
typedef void (*atEXCEPTION_HANDLER)(const std::string& file, const std::string& function, int line, const std::string&, ...);
atEXCEPTION_HANDLER atGetExceptionHandler();
void atSetExceptionHandler(atEXCEPTION_HANDLER func);
std::ostream& operator<<(std::ostream& os, const Athena::SeekOrigin& origin);
std::ostream& operator<<(std::ostream& os, const Athena::Endian& endian);
#endif // GLOBAL_HPP

View File

@@ -1,18 +1,3 @@
// This file is part of libAthena.
//
// libAthena is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// libAthena is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with libAthena. If not, see <http://www.gnu.org/licenses/>
#ifndef IOEXCEPTION_HPP
#define IOEXCEPTION_HPP
@@ -41,7 +26,9 @@ public:
*/
inline IOException(const std::string& message, const std::string& file, const std::string& function, const int line) :
Exception(message, file, function, line)
{}
{
m_exceptionName = "IOException";
}
};
} // error
@@ -50,14 +37,38 @@ public:
#ifdef _MSC_VER
#define THROW_IO_EXCEPTION(args, ...) \
do { \
if (atGetExceptionHandler()) {atGetExceptionHandler()(__FILE__, AT_PRETTY_FUNCTION, __LINE__, args, __VA_ARGS__); return; \
} else { \
std::string msg = Athena::utility::sprintf(args, __VA_ARGS__); \
throw Athena::error::IOException(std::string("IOException: ") + msg, __FILE__, AT_PRETTY_FUNCTION, __LINE__); \
} \
} while (0)
#elif defined(__GNUC__)
#define THROW_IO_EXCEPTION(args...) \
do { \
std::string msg = Athena::utility::sprintf(args); \
if (atGetExceptionHandler()) {atGetExceptionHandler()(__FILE__, AT_PRETTY_FUNCTION, __LINE__, args); return; \
} else { std::string msg = Athena::utility::sprintf(args); \
throw Athena::error::IOException(msg, __FILE__, AT_PRETTY_FUNCTION, __LINE__); \
} \
} while(0)
#endif
#ifdef _MSC_VER
#define THROW_IO_EXCEPTION_RETURN(ret, args, ...) \
do { \
if (atGetExceptionHandler()) {atGetExceptionHandler()(__FILE__, AT_PRETTY_FUNCTION, __LINE__, args, __VA_ARGS__); return ret; \
} else { \
std::string msg = Athena::utility::sprintf(args, __VA_ARGS__); \
throw Athena::error::IOException(std::string("IOException: ") + msg, __FILE__, AT_PRETTY_FUNCTION, __LINE__); \
} \
} while (0)
#elif defined(__GNUC__)
#define THROW_IO_EXCEPTION_RETURN(ret, args...) \
do { \
if (atGetExceptionHandler()) {atGetExceptionHandler()(__FILE__, AT_PRETTY_FUNCTION, __LINE__, args); return ret; \
} else { std::string msg = Athena::utility::sprintf(args); \
throw Athena::error::IOException(msg, __FILE__, AT_PRETTY_FUNCTION, __LINE__); \
} \
} while(0)
#endif

View File

@@ -1,18 +1,3 @@
// This file is part of libAthena.
//
// libAthena is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// libAthena is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with libAthena. If not, see <http://www.gnu.org/licenses/>
#ifndef STREAM_HPP
#define STREAM_HPP
@@ -34,12 +19,10 @@ public:
virtual Endian endian() const = 0;
virtual bool isBigEndian() const = 0;
virtual bool isLittleEndian()const = 0;
virtual bool isOpen() const= 0;
virtual void seek(atInt64, SeekOrigin) = 0;
virtual bool atEnd() const = 0;
virtual atUint64 position() const = 0;
virtual atUint64 length() const = 0;
virtual void seekBit (int)=0;
};
}
}

View File

@@ -1,6 +1,8 @@
#ifndef ISTREAMREADER_HPP
#define ISTREAMREADER_HPP
#include <locale>
#include <codecvt>
#include "IStream.hpp"
namespace Athena
@@ -11,35 +13,375 @@ class IStreamReader : public IStream
{
public:
virtual ~IStreamReader() {}
virtual void setEndian(Endian) = 0;
virtual Endian endian() const= 0;
virtual bool isBigEndian() const= 0;
virtual bool isLittleEndian()const= 0;
virtual bool isOpen() const= 0;
virtual void seek(atInt64, SeekOrigin)=0;
virtual void seekAlign32()=0;
virtual bool atEnd() const= 0;
/*! \brief Sets the Endianness of the stream
*
* \param endian The Endianness to set \sa Endian
*/
inline void setEndian(Endian endian)
{m_endian = endian;}
/*! \brief Returns the current Endianness of the stream
*
* \return Endian The current Stream Endianness
*/
inline Endian endian() const
{return m_endian;}
/*! \brief Returns whether the stream is BigEndian
*
* \return bool True for BigEndian; False for LittleEndian
*/
inline bool isBigEndian() const
{return (m_endian == Endian::BigEndian);}
/*! \brief Returns whether the stream is LittleEndian
*
* \return bool True for LittleEndian; False for BigEndian
*/
inline bool isLittleEndian()const
{return (m_endian == Endian::LittleEndian);}
/*! \brief Sets the buffers position relative to the specified position.<br />
* It seeks relative to the current position by default.
* \param position where in the buffer to seek
* \param origin The Origin to seek \sa SeekOrigin
*/
virtual void seek(atInt64 pos, SeekOrigin origin = SeekOrigin::Current)=0;
/*! \brief Sets the buffers position relative to the next 32-byte aligned position.<br />
*/
inline void seekAlign32() {seek(ROUND_UP_32(position()), SeekOrigin::Begin);}
/*! \brief Returns whether or not the stream is at the end.
*
* \return bool True if at end; False otherwise.
*/
inline bool atEnd() const
{return position() >= length();}
/*! \brief Returns the current position in the stream.
*
* \return Int64 The current position in the stream.
*/
virtual atUint64 position() const=0;
/*! \brief Returns whether or not the stream is at the end.
*
* \return bool True if at end; False otherwise.
*/
virtual atUint64 length() const=0;
virtual void seekBit (int)=0;
virtual bool readBit()=0;
virtual atUint8 readUByte()=0;
virtual atInt8 readByte()=0;
virtual atUint8* readUBytes(atUint64)=0;
virtual atInt8* readBytes(atUint64)=0;
virtual atUint64 readUBytesToBuf(void*, atUint64)=0;
virtual atUint64 readBytesToBuf(void*, atUint64)=0;
virtual atUint16 readUint16()=0;
virtual atInt16 readInt16()=0;
virtual atUint32 readUint32()=0;
virtual atInt32 readInt32()=0;
virtual atUint64 readUint64()=0;
virtual atInt64 readInt64()=0;
virtual double readDouble()=0;
virtual float readFloat()=0;
virtual bool readBool()=0;
virtual std::string readUnicode(atInt32=-1)=0;
virtual std::string readString(atInt32=-1)=0;
/*! \brief Reads a byte at the current position and advances the current position
*
* \return Int8 The value at the current position
*/
inline atInt8 readByte() {atInt8 val; readUBytesToBuf(&val, 1); return val;}
/*! \brief Reads a byte at the current position and advances the current position
*
* \return Uint8 The value at the current position
*/
inline atUint8 readUByte() {return readByte();}
/*! \brief Reads a byte at the current position and advances the current position.
*
* \return Uint8* The buffer at the current position from the given length.
*/
inline atInt8* readBytes(atUint64 length)
{atInt8* buf = new atInt8[length]; readUBytesToBuf(buf, length); return buf;}
/*! \brief Reads a byte at the current position and advances the current position.
*
* \return Int8* The buffer at the current position from the given length.
*/
inline atUint8* readUBytes(atUint64 length) {return (atUint8*)readBytes(length);}
inline atUint64 readBytesToBuf(void* buf, atUint64 len) {return readUBytesToBuf(buf, len);}
virtual atUint64 readUBytesToBuf(void* buf, atUint64 len)=0;
/*! \brief Reads a Int16 and swaps to proper endianness depending on platform
* and Stream settings, and advances the current position
*
* \sa Endian
*
* \return Int16 The value at the current address
* \throw IOException when address is out of range
*/
inline atInt16 readInt16()
{
atInt16 val;
readUBytesToBuf(&val, 2);
return m_endian == BigEndian ? utility::BigInt16(val) : utility::LittleInt16(val);
}
/*! \brief Reads a Uint16 and swaps to proper endianness depending on platform
* and Stream settings, and advances the current position
*
* \sa Endian
*
* \return Uint16 The value at the current address
* \throw IOException when address is out of range
*/
inline atUint16 readUint16()
{return readInt16();}
/*! \brief Reads a Int32 and swaps to proper endianness depending on platform
* and Stream settings, and advances the current position
*
* \sa Endian
*
* \return Int32 The value at the current address
* \throw IOException when address is out of range
*/
inline atInt32 readInt32()
{
atInt32 val;
readUBytesToBuf(&val, 4);
return m_endian == BigEndian ? utility::BigInt32(val) : utility::LittleInt32(val);
}
/*! \brief Reads a Uint32 and swaps to proper endianness depending on platform
* and Stream settings, and advances the current position
*
* \sa Endian
*
* \return Uint32 The value at the current address
* \throw IOException when address is out of range
*/
inline atUint32 readUint32()
{return readInt32();}
/*! \brief Reads a Int64 and swaps to proper endianness depending on platform
* and Stream settings, and advances the current position
*
* \sa Endian
*
* \return Int64 The value at the current address
* \throw IOException when address is out of range
*/
inline atInt64 readInt64()
{
atInt64 val;
readUBytesToBuf(&val, 8);
return m_endian == BigEndian ? utility::BigInt64(val) : utility::LittleInt64(val);
}
/*! \brief Reads a Uint64 and swaps to proper endianness depending on platform
* and Stream settings, and advances the current position
*
* \sa Endian
*
* \return Uint64 The value at the current address
* \throw IOException when address is out of range
*/
inline atUint64 readUint64()
{return readInt64();}
/*! \brief Reads a float and swaps to proper endianness depending on platform
* and Stream settings, and advances the current position
*
* \sa Endian
*
* \return float The value at the current address
* \throw IOException when address is out of range
*/
inline float readFloat()
{
float val;
readUBytesToBuf(&val, 4);
return m_endian == BigEndian ? utility::BigFloat(val) : utility::LittleFloat(val);
}
/*! \brief Reads a double and swaps to proper endianness depending on platform
* and Stream settings, and advances the current position
*
* \sa Endian
*
* \return double The value at the current address
* \throw IOException when address is out of range
*/
inline double readDouble()
{
double val;
readUBytesToBuf(&val, 8);
return m_endian == BigEndian ? utility::BigDouble(val) : utility::LittleDouble(val);
}
/*! \brief Reads a bool and advances the current position
*
* \return bool The value at the current address
* \throw IOException when address is out of range
*/
inline bool readBool()
{
atUint8 val;
readUBytesToBuf(&val, 1);
return val != 0;
}
/*! \brief Reads an atVec2f (8 bytes) and advances the current position
*
* \return atVec2f The value at the current address
* \throw IOException when address is out of range
*/
inline atVec2f readVec2f()
{
atVec2f val;
readUBytesToBuf(&val, 8);
if (m_endian == BigEndian)
{
utility::BigFloat(val.vec[0]);
utility::BigFloat(val.vec[1]);
}
else
{
utility::LittleFloat(val.vec[0]);
utility::LittleFloat(val.vec[1]);
}
return val;
}
/*! \brief Reads an atVec3f (12 bytes) and advances the current position
*
* \return atVec3f The value at the current address
* \throw IOException when address is out of range
*/
inline atVec3f readVec3f()
{
atVec3f val;
readUBytesToBuf(&val, 12);
if (m_endian == BigEndian)
{
utility::BigFloat(val.vec[0]);
utility::BigFloat(val.vec[1]);
utility::BigFloat(val.vec[2]);
}
else
{
utility::LittleFloat(val.vec[0]);
utility::LittleFloat(val.vec[1]);
utility::LittleFloat(val.vec[2]);
}
return val;
}
/*! \brief Reads an atVec4f (16 bytes) and advances the current position
*
* \return atVec4f The value at the current address
* \throw IOException when address is out of range
*/
inline atVec4f readVec4f()
{
atVec4f val;
readUBytesToBuf(&val, 16);
if (m_endian == BigEndian)
{
utility::BigFloat(val.vec[0]);
utility::BigFloat(val.vec[1]);
utility::BigFloat(val.vec[2]);
utility::BigFloat(val.vec[3]);
}
else
{
utility::LittleFloat(val.vec[0]);
utility::LittleFloat(val.vec[1]);
utility::LittleFloat(val.vec[2]);
utility::LittleFloat(val.vec[3]);
}
return val;
}
/*! \brief Reads a wide-char string, converts to UTF8 and advances the position in the file
*
* \param fixedLen If non-negative, this is a fixed-length string read
* \return std::string The value at the current address
* \throw IOException when address is out of range
*/
inline std::string readWStringAsString(atInt32 fixedLen = -1)
{
std::wstring tmp;
atUint16 chr = readUint16();
atInt32 i;
for (i = 0 ;; ++i)
{
if (fixedLen >= 0 && i >= fixedLen - 1)
break;
if (!chr)
break;
tmp.push_back(chr);
chr = readUint16();
}
if (fixedLen >= 0 && i < fixedLen)
seek(fixedLen - i);
std::wstring_convert<std::codecvt_utf8<wchar_t>> conv;
return conv.to_bytes(tmp);
}
/*! \brief Reads a string and advances the position in the file
*
* \param fixedLen If non-negative, this is a fixed-length string read
* \return std::string The value at the current address
* \throw IOException when address is out of range
*/
inline std::string readString(atInt32 fixedLen = -1)
{
std::string ret;
atUint8 chr = readByte();
atInt32 i;
for (i = 1 ; chr != 0 ; ++i)
{
ret += chr;
if (fixedLen >= 0 && i >= fixedLen)
break;
chr = readByte();
}
if (fixedLen >= 0 && i < fixedLen)
seek(fixedLen - i);
return ret;
}
/*! \brief Reads a wstring and advances the position in the file
*
* \param fixedLen If non-negative, this is a fixed-length string read
* \return std::wstring The value at the current address
* \throw IOException when address is out of range
*/
inline std::wstring readWString(atInt32 fixedLen = -1)
{
std::wstring ret;
atUint16 chr = readUint16();
atInt32 i;
for (i = 1 ; chr != 0 ; ++i)
{
ret += chr;
if (fixedLen >= 0 && i >= fixedLen)
break;
chr = readUint16();
}
if (fixedLen >= 0 && i < fixedLen)
seek(fixedLen - i);
return ret;
}
protected:
Endian m_endian;
};
}
}

Some files were not shown because too many files have changed in this diff Show More