├── .gitignore ├── CMakeLists.txt ├── CompressedTensor.lua ├── README.md ├── generic └── torchzlib.c ├── init.lua ├── rocks └── torchzlib-1.0-1.rockspec ├── test ├── .gitignore └── test.lua ├── torchzlib.c └── zlib ├── CMakeLists.txt ├── ChangeLog ├── FAQ ├── INDEX ├── README ├── adler32.c ├── compress.c ├── crc32.c ├── crc32.h ├── deflate.c ├── deflate.h ├── gzclose.c ├── gzguts.h ├── gzlib.c ├── gzread.c ├── gzwrite.c ├── infback.c ├── inffast.c ├── inffast.h ├── inffixed.h ├── inflate.c ├── inflate.h ├── inftrees.c ├── inftrees.h ├── trees.c ├── trees.h ├── uncompr.c ├── zconf.h.cmakein ├── zlib.def ├── zlib.h ├── zlib1.rc ├── zutil.c └── zutil.h /.gitignore: -------------------------------------------------------------------------------- 1 | build 2 | 3 | tmp.bin 4 | *.swp 5 | image.bin 6 | image.bin.gz 7 | image.filtered.zlib 8 | image.png 9 | image.zlib 10 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | CMAKE_MINIMUM_REQUIRED(VERSION 2.6 FATAL_ERROR) 2 | CMAKE_POLICY(VERSION 2.6) 3 | 4 | FIND_PACKAGE(Torch REQUIRED) 5 | 6 | ADD_SUBDIRECTORY(zlib) 7 | 8 | INCLUDE_DIRECTORIES(${CMAKE_BINARY_DIR}/zlib) 9 | 10 | SET(src torchzlib.c) 11 | 12 | FILE(GLOB luasrc *.lua) 13 | SET(luasrc init.lua ${luasrc}) 14 | 15 | ADD_TORCH_PACKAGE(torchzlib "${src}" "${luasrc}" "Torch zlib utility library") 16 | TARGET_LINK_LIBRARIES(torchzlib luaT TH zlib) 17 | INSTALL(FILES "README.md" DESTINATION "${Torch_INSTALL_LUA_PATH_SUBDIR}/torchzlib") 18 | -------------------------------------------------------------------------------- /CompressedTensor.lua: -------------------------------------------------------------------------------- 1 | -- This class is just a wrapper around the compressed ByteTensor which makes 2 | -- the API a little less painful. 3 | 4 | local CompressedTensor = torch.class("torch.CompressedTensor") 5 | 6 | function CompressedTensor:__init(tensor, quality, apply_sub_filter) 7 | assert(torch.typename(tensor):find('torch%..+Tensor'), 8 | 'tensor input must be a torch tensor') 9 | self.apply_sub_filter = apply_sub_filter or false 10 | self.src_type = torch.typename(tensor) 11 | self.data = torch.ByteTensor() 12 | self.size = torch.LongStorage(tensor:size():size()) -- Size of storage 13 | self.size:copy(tensor:size()) 14 | self.quality = quality or 1 -- 0, 1, 2 (for low to high compression) 15 | -- TODO: the src_type string above is at least a few 10s of bytes overhead 16 | -- --> change to an enum value 17 | 18 | local filt_tensor = tensor 19 | if self.apply_sub_filter then 20 | -- PNG sub filter, assume last dimension is the X dim (ie width): 21 | -- clone so we don't destroy the original tensor and so we can calculate 22 | -- the difference image in parallel 23 | filt_tensor = tensor:clone() 24 | local dim = filt_tensor:dim() 25 | local sz = filt_tensor:size(dim) 26 | filt_tensor:narrow(dim, 2, sz-1):add(-1, tensor:narrow(dim, 1, sz-1)) 27 | end 28 | 29 | filt_tensor = filt_tensor:contiguous() 30 | 31 | -- Compress the tensor 32 | tensor.torchzlib.compress(filt_tensor, self.data, self.quality) 33 | end 34 | 35 | function CompressedTensor:decompress() 36 | local return_tensor = torch.Tensor():type(self.src_type):resize(self.size) 37 | return_tensor.torchzlib.decompress(self.data, return_tensor) 38 | 39 | if self.apply_sub_filter then 40 | local dim = return_tensor:dim() 41 | local sz = return_tensor:size(dim) 42 | for i = 2, sz do 43 | -- Here, order matters so we need to loop... 44 | return_tensor:narrow(dim, i, 1):add(return_tensor:narrow(dim, i-1, 1)) 45 | end 46 | end 47 | 48 | return return_tensor 49 | end 50 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | torchzlib 2 | ========= 3 | 4 | A utility library for zlib compression / decompression of Torch7 tensors. 5 | 6 | Installation 7 | ------------ 8 | 9 | After cloning the library, do a local luarocks build: 10 | 11 | ``` 12 | git clone git@github.com:jonathantompson/torchzlib.git 13 | cd torchzlib 14 | luarocks make rocks/torchzlib-1.0-1.rockspec 15 | ``` 16 | 17 | API / Usage 18 | ------------ 19 | 20 | The main (and only) API entry point is a new class ```torch.CompressedTensor```. This is a super simple class that creates a compressed ByteTensor of an input tensor (using zlib ```deflate```) and has a single ```decompress()``` method to return the original data. 21 | 22 | The constructor signature is: 23 | 24 | ``` lua 25 | torch.CompressedTensor(tensor, 26 | quality, -- optional: default 1 27 | apply_sub_filter) -- optional: default false 28 | ``` 29 | 30 | Where ```tensor``` is the tensor to be compressed, ```quality``` is a integer value in [0, 1, 2] and controls the amount of compression and ```apply_sub_filter``` is a boolean and indicates whether a PNG subfilter should be applied to each scanline before each compression (and inverse applied on decompression). Note: if the tensor is a float type, then the sub-filter will result in lossy compression. 31 | 32 | As per the zlib library, all compression is lossless. 33 | 34 | Usage: 35 | 36 | ```lua 37 | require 'torchzlib' 38 | 39 | data = torch.rand(5,5):double() -- Can be any other tensor 40 | data_compressed = torch.CompressedTensor(data) -- Compress using default compression level (1) 41 | 42 | -- Alternatively: 43 | data_compressed = torch.CompressedTensor(data, 0) -- Compress using fast compression (low compression ratio) 44 | data_compressed = torch.CompressedTensor(data, 2) -- Compress using slow compression (high compression ratio) 45 | 46 | -- Do whatever you want in here (including saving and loading data_compressed to file) 47 | 48 | data_decompressed = data_compressed:decompress() 49 | ``` 50 | 51 | To compare libpng vs torchzlib compression ratios: 52 | 53 | ```lua 54 | require 'torchzlib' 55 | require 'image' 56 | 57 | im = image.lena():mul(255):clamp(0,255):byte() 58 | torch.save('image.bin', im) -- Baseline 59 | image.savePNG('image.png', im) -- PNG compression 60 | torch.save('image.zlib', torch.CompressedTensor(im, 2)) -- zlib compression 61 | torch.save('image.filtered.zlib', torch.CompressedTensor(im, 2, true)) 62 | os.execute('gzip -c image.bin > image.bin.gz') -- zlib compression from the command line (Linux) 63 | ``` 64 | 65 | Note: png is not just zlib. It's also uses a preprocessing step (called delta filtering) that enables extremely high compression ratios on images once the output is passed through DEFLATE. In the above example ```image.bin.gz``` should be approximately the same size as ```image.zlib```. 66 | -------------------------------------------------------------------------------- /generic/torchzlib.c: -------------------------------------------------------------------------------- 1 | #ifndef TH_GENERIC_FILE 2 | #define TH_GENERIC_FILE "generic/torchzlib.c" 3 | #else 4 | 5 | #undef MAX 6 | #define MAX(a,b) ( ((a)>(b)) ? (a) : (b) ) 7 | 8 | #undef MIN 9 | #define MIN(a,b) ( ((a)<(b)) ? (a) : (b) ) 10 | 11 | #undef TAPI 12 | #define TAPI __declspec(dllimport) 13 | 14 | #ifndef M_PI 15 | #define M_PI 3.14159265358979323846 16 | #endif 17 | 18 | #include 19 | #include "zlib/zlib.h" 20 | 21 | static int torchzlib_(Main_compress)(lua_State *L) { 22 | THTensor *Tsrc = luaT_checkudata(L, 1, torch_Tensor); 23 | real *src_data = THTensor_(data)(Tsrc); 24 | int64_t src_data_size = (int64_t)THTensor_(nElement)(Tsrc) * (int64_t)sizeof(src_data[0]); 25 | 26 | THByteTensor* tensor_dest = luaT_checkudata(L, 2, "torch.ByteTensor"); 27 | int quality = luaL_checkint(L, 3); 28 | 29 | z_stream strm; 30 | strm.zalloc = Z_NULL; 31 | strm.zfree = Z_NULL; 32 | strm.next_in = (uint8_t *)(src_data); 33 | strm.avail_in = src_data_size; 34 | 35 | int compression; 36 | switch (quality) { 37 | case 0: 38 | compression = Z_BEST_SPEED; 39 | break; 40 | case 1: 41 | compression = Z_DEFAULT_COMPRESSION; 42 | break; 43 | case 2: 44 | compression = Z_BEST_COMPRESSION; 45 | break; 46 | default: 47 | luaL_error(L, "quality must be 0, 1, or 2"); 48 | } 49 | 50 | if (deflateInit(&strm, compression) != Z_OK) { 51 | luaL_error(L, "cannot open zlib deflate stream"); 52 | } 53 | 54 | /* Allocate the maximum possible compressed array (actually larger than the input). We COULD compress and decompress in chunks, but then we need an STL like Vector implementation in C and the worst case is that we would be allocating this much anyway. So just compress into a large temp buffer (that is likely too big) than store only the compressed size. */ 55 | 56 | int temp_buffer_size = deflateBound(&strm, src_data_size); 57 | uint8_t* temp_buffer = (uint8_t*)malloc(temp_buffer_size); 58 | if (temp_buffer == NULL) { 59 | luaL_error(L, "Could not malloc temporary memory"); 60 | } 61 | 62 | strm.next_out = temp_buffer; 63 | strm.avail_out = temp_buffer_size; 64 | 65 | int res = deflate(&strm, Z_FINISH); 66 | 67 | if (res != Z_STREAM_END) { 68 | luaL_error(L, "zlib deflate failed! Error code: %d", res); 69 | } 70 | 71 | if (strm.avail_in != 0) { 72 | luaL_error(L, "zlib deflate failed! Bytes left in the input stream: %d", strm.avail_in); 73 | } 74 | 75 | int64_t nret_size = strm.total_out; 76 | deflateEnd(&strm); 77 | 78 | THByteTensor_resize1d(tensor_dest, nret_size); 79 | unsigned char* tensor_dest_data = THByteTensor_data(tensor_dest); 80 | memcpy(tensor_dest_data, temp_buffer, nret_size); 81 | 82 | free(temp_buffer); 83 | 84 | return 0; 85 | } 86 | 87 | static int torchzlib_(Main_decompress)(lua_State *L) { 88 | THTensor *Tdst = luaT_checkudata(L, 2, torch_Tensor); 89 | real *dst_data = THTensor_(data)(Tdst); 90 | /* Here I assume that the correct output size has already been allocated. We will handle this on the lua end */ 91 | int64_t dst_data_size = (int64_t)THTensor_(nElement)(Tdst) * (int64_t)sizeof(dst_data[0]); 92 | 93 | THByteTensor* tensor_src = luaT_checkudata(L, 1, "torch.ByteTensor"); 94 | int64_t src_data_size = (int64_t)THByteTensor_nElement(tensor_src); 95 | unsigned char* src_data = THByteTensor_data(tensor_src); 96 | 97 | z_stream strm ={0}; 98 | strm.total_in = strm.avail_in = src_data_size; 99 | strm.total_out = strm.avail_out = dst_data_size; 100 | strm.next_in = src_data; 101 | strm.next_out= (uint8_t*)dst_data; 102 | 103 | if (inflateInit(&strm) != Z_OK) { 104 | luaL_error(L, "cannot open zlib inflate stream"); 105 | } 106 | 107 | int res = inflate(&strm, Z_FINISH); 108 | 109 | if (res != Z_STREAM_END) { 110 | luaL_error(L, "zlib deflate failed! Error code: %d", res); 111 | } 112 | 113 | if (strm.avail_in != 0) { 114 | luaL_error(L, "zlib inflate failed! Bytes left in the input stream: %d", strm.avail_in); 115 | } 116 | 117 | if (strm.total_out != dst_data_size) { 118 | luaL_error(L, "zlib inflate failed! Incorrect return size: %d (expecting %d)", strm.total_out, dst_data_size); 119 | } 120 | 121 | inflateEnd(&strm); 122 | 123 | return 0; 124 | } 125 | 126 | static const struct luaL_Reg torchzlib_(Main__) [] = { 127 | {"compress", torchzlib_(Main_compress)}, 128 | {"decompress", torchzlib_(Main_decompress)}, 129 | {NULL, NULL} 130 | }; 131 | 132 | void torchzlib_(Main_init)(lua_State *L) 133 | { 134 | luaT_pushmetatable(L, torch_Tensor); 135 | luaT_registeratname(L, torchzlib_(Main__), "torchzlib"); 136 | } 137 | 138 | #endif 139 | -------------------------------------------------------------------------------- /init.lua: -------------------------------------------------------------------------------- 1 | require "torch" 2 | 3 | include('CompressedTensor.lua') 4 | 5 | require "libtorchzlib" 6 | -------------------------------------------------------------------------------- /rocks/torchzlib-1.0-1.rockspec: -------------------------------------------------------------------------------- 1 | package = "torchzlib" 2 | version = "1.0-1" 3 | source = { 4 | url = "git://github.com/jonathantompson/torchzlib.git" 5 | } 6 | description = { 7 | summary = "A utility library for zlib compression / decompression of tensors", 8 | detailed = [[ 9 | ]], 10 | homepage = "...", 11 | license = "None" 12 | } 13 | dependencies = { 14 | } 15 | 16 | build = { 17 | type = "command", 18 | build_command = [[ 19 | cmake -E make_directory build && cd build && cmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_PREFIX_PATH="$(LUA_BINDIR)/.." -DCMAKE_INSTALL_PREFIX="$(PREFIX)" && $(MAKE) 20 | ]], 21 | install_command = "cd build && $(MAKE) install" 22 | } 23 | -------------------------------------------------------------------------------- /test/.gitignore: -------------------------------------------------------------------------------- 1 | 2 | tmp.bin -------------------------------------------------------------------------------- /test/test.lua: -------------------------------------------------------------------------------- 1 | require 'torchzlib' 2 | 3 | test_types = {'torch.ByteTensor', 'torch.CharTensor', 'torch.ShortTensor', 'torch.IntTensor', 'torch.LongTensor', 'torch.FloatTensor', 'torch.DoubleTensor'} 4 | 5 | for _, cur_type in pairs(test_types) do 6 | for apply_sub_filter = 0, 1 do 7 | for quality = 0, 2 do 8 | 9 | local data = torch.rand(4,1,125,126):mul(255):clamp(0,255):type(cur_type) 10 | local data_compressed = torch.CompressedTensor(data, quality, apply_sub_filter == 1) 11 | 12 | -- Make sure data integrety is maintained when serializing to / from disk 13 | torch.save('tmp.bin', data_compressed) 14 | data_compressed = torch.load('tmp.bin') 15 | 16 | local data_decompressed = data_compressed:decompress() 17 | assert(torch.type(data_decompressed) == cur_type) 18 | 19 | local err = data_decompressed - data 20 | if apply_sub_filter == 0 then 21 | assert(err:max() == 0 and err:min() == 0, -- Lossless! It should be exactly the same 22 | 'failed on '..cur_type..', quality='..quality..', apply_sub_filter='..apply_sub_filter) 23 | else 24 | assert(err:max() < 1e-3 and err:min() > -1e-3, -- Filter will introduce floating point error 25 | 'failed on '..cur_type..', quality='..quality..', apply_sub_filter='..apply_sub_filter) 26 | end 27 | end 28 | end 29 | end 30 | 31 | 32 | 33 | print('All tests passed!') 34 | -------------------------------------------------------------------------------- /torchzlib.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | 5 | #define torch_(NAME) TH_CONCAT_3(torch_, Real, NAME) 6 | #define torch_Tensor TH_CONCAT_STRING_3(torch., Real, Tensor) 7 | #define torchzlib_(NAME) TH_CONCAT_3(torchzlib_, Real, NAME) 8 | 9 | #ifdef max 10 | #undef max 11 | #endif 12 | #define max( a, b ) ( ((a) > (b)) ? (a) : (b) ) 13 | 14 | #ifdef min 15 | #undef min 16 | #endif 17 | #define min( a, b ) ( ((a) < (b)) ? (a) : (b) ) 18 | 19 | #include "generic/torchzlib.c" 20 | #include "THGenerateAllTypes.h" 21 | 22 | DLL_EXPORT int luaopen_libtorchzlib(lua_State *L) 23 | { 24 | torchzlib_ByteMain_init(L); 25 | torchzlib_CharMain_init(L); 26 | torchzlib_ShortMain_init(L); 27 | torchzlib_IntMain_init(L); 28 | torchzlib_LongMain_init(L); 29 | torchzlib_FloatMain_init(L); 30 | torchzlib_DoubleMain_init(L); 31 | 32 | luaL_register(L, "torchzlib.byte", torchzlib_ByteMain__); 33 | luaL_register(L, "torchzlib.char", torchzlib_CharMain__); 34 | luaL_register(L, "torchzlib.short", torchzlib_ShortMain__); 35 | luaL_register(L, "torchzlib.int", torchzlib_IntMain__); 36 | luaL_register(L, "torchzlib.long", torchzlib_LongMain__); 37 | luaL_register(L, "torchzlib.float", torchzlib_FloatMain__); 38 | luaL_register(L, "torchzlib.double", torchzlib_DoubleMain__); 39 | 40 | return 1; 41 | } 42 | -------------------------------------------------------------------------------- /zlib/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # -*- cmake -*- 2 | 3 | set(VERSION "1.2.8") 4 | 5 | include(CheckTypeSize) 6 | include(CheckFunctionExists) 7 | include(CheckIncludeFile) 8 | include(CheckCSourceCompiles) 9 | 10 | enable_testing() 11 | 12 | check_include_file(sys/types.h HAVE_SYS_TYPES_H) 13 | check_include_file(stdint.h HAVE_STDINT_H) 14 | check_include_file(stddef.h HAVE_STDDEF_H) 15 | 16 | 17 | set(CMAKE_REQUIRED_DEFINITIONS -D_LARGEFILE64_SOURCE=1) 18 | check_type_size(off64_t OFF64_T) 19 | if(HAVE_OFF64_T) 20 | add_definitions(-D_LARGEFILE64_SOURCE=1) 21 | endif() 22 | set(CMAKE_REQUIRED_DEFINITIONS) # clear variable 23 | 24 | check_function_exists(fseeko HAVE_FSEEKO) 25 | if(NOT HAVE_FSEEKO) 26 | add_definitions(-DNO_FSEEKO) 27 | endif() 28 | 29 | check_include_file(unistd.h Z_HAVE_UNISTD_H) 30 | 31 | if(MSVC) 32 | set(CMAKE_DEBUG_POSTFIX "d") 33 | add_definitions(-D_CRT_SECURE_NO_DEPRECATE) 34 | add_definitions(-D_CRT_NONSTDC_NO_DEPRECATE) 35 | endif() 36 | 37 | configure_file( ${CMAKE_CURRENT_SOURCE_DIR}/zconf.h.cmakein 38 | ${CMAKE_CURRENT_BINARY_DIR}/zconf.h @ONLY) 39 | 40 | include_directories(${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_SOURCE_DIR}) 41 | 42 | 43 | 44 | SET(src 45 | adler32.c 46 | crc32.c 47 | infback.c 48 | inflate.c 49 | uncompr.c 50 | compress.c 51 | deflate.c 52 | gzclose.c 53 | gzlib.c 54 | gzread.c 55 | gzwrite.c 56 | inffast.c 57 | inftrees.c 58 | trees.c 59 | zutil.c) 60 | 61 | ADD_LIBRARY(zlib SHARED ${src}) 62 | 63 | IF(WIN32) 64 | ADD_DEFINITIONS(-DZLIB_DLL) 65 | ENDIF(WIN32) 66 | 67 | # To compile examples 68 | # ADD_EXECUTABLE(example example.c) 69 | # TARGET_LINK_LIBRARIES(example zlib) 70 | # 71 | # ADDZZ_EXECUTABLE(minigzip minigzip.c) 72 | # TARGET_LINK_LIBRARIES(minigzip zlib) 73 | 74 | INSTALL(TARGETS zlib 75 | LIBRARY DESTINATION "${Torch_INSTALL_LIB_SUBDIR}" 76 | ARCHIVE DESTINATION "${Torch_INSTALL_LIB_SUBDIR}" 77 | RUNTIME DESTINATION "${Torch_INSTALL_BIN_SUBDIR}") 78 | 79 | INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/zconf.h zlib.h 80 | DESTINATION "${Torch_INSTALL_INCLUDE_SUBDIR}") 81 | 82 | -------------------------------------------------------------------------------- /zlib/ChangeLog: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jonathantompson/torchzlib/f78b61faab5d9cc09d18022d4b4be482c17805ca/zlib/ChangeLog -------------------------------------------------------------------------------- /zlib/FAQ: -------------------------------------------------------------------------------- 1 | 2 | Frequently Asked Questions about zlib 3 | 4 | 5 | If your question is not there, please check the zlib home page 6 | http://zlib.net/ which may have more recent information. 7 | The lastest zlib FAQ is at http://zlib.net/zlib_faq.html 8 | 9 | 10 | 1. Is zlib Y2K-compliant? 11 | 12 | Yes. zlib doesn't handle dates. 13 | 14 | 2. Where can I get a Windows DLL version? 15 | 16 | The zlib sources can be compiled without change to produce a DLL. See the 17 | file win32/DLL_FAQ.txt in the zlib distribution. Pointers to the 18 | precompiled DLL are found in the zlib web site at http://zlib.net/ . 19 | 20 | 3. Where can I get a Visual Basic interface to zlib? 21 | 22 | See 23 | * http://marknelson.us/1997/01/01/zlib-engine/ 24 | * win32/DLL_FAQ.txt in the zlib distribution 25 | 26 | 4. compress() returns Z_BUF_ERROR. 27 | 28 | Make sure that before the call of compress(), the length of the compressed 29 | buffer is equal to the available size of the compressed buffer and not 30 | zero. For Visual Basic, check that this parameter is passed by reference 31 | ("as any"), not by value ("as long"). 32 | 33 | 5. deflate() or inflate() returns Z_BUF_ERROR. 34 | 35 | Before making the call, make sure that avail_in and avail_out are not zero. 36 | When setting the parameter flush equal to Z_FINISH, also make sure that 37 | avail_out is big enough to allow processing all pending input. Note that a 38 | Z_BUF_ERROR is not fatal--another call to deflate() or inflate() can be 39 | made with more input or output space. A Z_BUF_ERROR may in fact be 40 | unavoidable depending on how the functions are used, since it is not 41 | possible to tell whether or not there is more output pending when 42 | strm.avail_out returns with zero. See http://zlib.net/zlib_how.html for a 43 | heavily annotated example. 44 | 45 | 6. Where's the zlib documentation (man pages, etc.)? 46 | 47 | It's in zlib.h . Examples of zlib usage are in the files test/example.c 48 | and test/minigzip.c, with more in examples/ . 49 | 50 | 7. Why don't you use GNU autoconf or libtool or ...? 51 | 52 | Because we would like to keep zlib as a very small and simple package. 53 | zlib is rather portable and doesn't need much configuration. 54 | 55 | 8. I found a bug in zlib. 56 | 57 | Most of the time, such problems are due to an incorrect usage of zlib. 58 | Please try to reproduce the problem with a small program and send the 59 | corresponding source to us at zlib@gzip.org . Do not send multi-megabyte 60 | data files without prior agreement. 61 | 62 | 9. Why do I get "undefined reference to gzputc"? 63 | 64 | If "make test" produces something like 65 | 66 | example.o(.text+0x154): undefined reference to `gzputc' 67 | 68 | check that you don't have old files libz.* in /usr/lib, /usr/local/lib or 69 | /usr/X11R6/lib. Remove any old versions, then do "make install". 70 | 71 | 10. I need a Delphi interface to zlib. 72 | 73 | See the contrib/delphi directory in the zlib distribution. 74 | 75 | 11. Can zlib handle .zip archives? 76 | 77 | Not by itself, no. See the directory contrib/minizip in the zlib 78 | distribution. 79 | 80 | 12. Can zlib handle .Z files? 81 | 82 | No, sorry. You have to spawn an uncompress or gunzip subprocess, or adapt 83 | the code of uncompress on your own. 84 | 85 | 13. How can I make a Unix shared library? 86 | 87 | By default a shared (and a static) library is built for Unix. So: 88 | 89 | make distclean 90 | ./configure 91 | make 92 | 93 | 14. How do I install a shared zlib library on Unix? 94 | 95 | After the above, then: 96 | 97 | make install 98 | 99 | However, many flavors of Unix come with a shared zlib already installed. 100 | Before going to the trouble of compiling a shared version of zlib and 101 | trying to install it, you may want to check if it's already there! If you 102 | can #include , it's there. The -lz option will probably link to 103 | it. You can check the version at the top of zlib.h or with the 104 | ZLIB_VERSION symbol defined in zlib.h . 105 | 106 | 15. I have a question about OttoPDF. 107 | 108 | We are not the authors of OttoPDF. The real author is on the OttoPDF web 109 | site: Joel Hainley, jhainley@myndkryme.com. 110 | 111 | 16. Can zlib decode Flate data in an Adobe PDF file? 112 | 113 | Yes. See http://www.pdflib.com/ . To modify PDF forms, see 114 | http://sourceforge.net/projects/acroformtool/ . 115 | 116 | 17. Why am I getting this "register_frame_info not found" error on Solaris? 117 | 118 | After installing zlib 1.1.4 on Solaris 2.6, running applications using zlib 119 | generates an error such as: 120 | 121 | ld.so.1: rpm: fatal: relocation error: file /usr/local/lib/libz.so: 122 | symbol __register_frame_info: referenced symbol not found 123 | 124 | The symbol __register_frame_info is not part of zlib, it is generated by 125 | the C compiler (cc or gcc). You must recompile applications using zlib 126 | which have this problem. This problem is specific to Solaris. See 127 | http://www.sunfreeware.com for Solaris versions of zlib and applications 128 | using zlib. 129 | 130 | 18. Why does gzip give an error on a file I make with compress/deflate? 131 | 132 | The compress and deflate functions produce data in the zlib format, which 133 | is different and incompatible with the gzip format. The gz* functions in 134 | zlib on the other hand use the gzip format. Both the zlib and gzip formats 135 | use the same compressed data format internally, but have different headers 136 | and trailers around the compressed data. 137 | 138 | 19. Ok, so why are there two different formats? 139 | 140 | The gzip format was designed to retain the directory information about a 141 | single file, such as the name and last modification date. The zlib format 142 | on the other hand was designed for in-memory and communication channel 143 | applications, and has a much more compact header and trailer and uses a 144 | faster integrity check than gzip. 145 | 146 | 20. Well that's nice, but how do I make a gzip file in memory? 147 | 148 | You can request that deflate write the gzip format instead of the zlib 149 | format using deflateInit2(). You can also request that inflate decode the 150 | gzip format using inflateInit2(). Read zlib.h for more details. 151 | 152 | 21. Is zlib thread-safe? 153 | 154 | Yes. However any library routines that zlib uses and any application- 155 | provided memory allocation routines must also be thread-safe. zlib's gz* 156 | functions use stdio library routines, and most of zlib's functions use the 157 | library memory allocation routines by default. zlib's *Init* functions 158 | allow for the application to provide custom memory allocation routines. 159 | 160 | Of course, you should only operate on any given zlib or gzip stream from a 161 | single thread at a time. 162 | 163 | 22. Can I use zlib in my commercial application? 164 | 165 | Yes. Please read the license in zlib.h. 166 | 167 | 23. Is zlib under the GNU license? 168 | 169 | No. Please read the license in zlib.h. 170 | 171 | 24. The license says that altered source versions must be "plainly marked". So 172 | what exactly do I need to do to meet that requirement? 173 | 174 | You need to change the ZLIB_VERSION and ZLIB_VERNUM #defines in zlib.h. In 175 | particular, the final version number needs to be changed to "f", and an 176 | identification string should be appended to ZLIB_VERSION. Version numbers 177 | x.x.x.f are reserved for modifications to zlib by others than the zlib 178 | maintainers. For example, if the version of the base zlib you are altering 179 | is "1.2.3.4", then in zlib.h you should change ZLIB_VERNUM to 0x123f, and 180 | ZLIB_VERSION to something like "1.2.3.f-zachary-mods-v3". You can also 181 | update the version strings in deflate.c and inftrees.c. 182 | 183 | For altered source distributions, you should also note the origin and 184 | nature of the changes in zlib.h, as well as in ChangeLog and README, along 185 | with the dates of the alterations. The origin should include at least your 186 | name (or your company's name), and an email address to contact for help or 187 | issues with the library. 188 | 189 | Note that distributing a compiled zlib library along with zlib.h and 190 | zconf.h is also a source distribution, and so you should change 191 | ZLIB_VERSION and ZLIB_VERNUM and note the origin and nature of the changes 192 | in zlib.h as you would for a full source distribution. 193 | 194 | 25. Will zlib work on a big-endian or little-endian architecture, and can I 195 | exchange compressed data between them? 196 | 197 | Yes and yes. 198 | 199 | 26. Will zlib work on a 64-bit machine? 200 | 201 | Yes. It has been tested on 64-bit machines, and has no dependence on any 202 | data types being limited to 32-bits in length. If you have any 203 | difficulties, please provide a complete problem report to zlib@gzip.org 204 | 205 | 27. Will zlib decompress data from the PKWare Data Compression Library? 206 | 207 | No. The PKWare DCL uses a completely different compressed data format than 208 | does PKZIP and zlib. However, you can look in zlib's contrib/blast 209 | directory for a possible solution to your problem. 210 | 211 | 28. Can I access data randomly in a compressed stream? 212 | 213 | No, not without some preparation. If when compressing you periodically use 214 | Z_FULL_FLUSH, carefully write all the pending data at those points, and 215 | keep an index of those locations, then you can start decompression at those 216 | points. You have to be careful to not use Z_FULL_FLUSH too often, since it 217 | can significantly degrade compression. Alternatively, you can scan a 218 | deflate stream once to generate an index, and then use that index for 219 | random access. See examples/zran.c . 220 | 221 | 29. Does zlib work on MVS, OS/390, CICS, etc.? 222 | 223 | It has in the past, but we have not heard of any recent evidence. There 224 | were working ports of zlib 1.1.4 to MVS, but those links no longer work. 225 | If you know of recent, successful applications of zlib on these operating 226 | systems, please let us know. Thanks. 227 | 228 | 30. Is there some simpler, easier to read version of inflate I can look at to 229 | understand the deflate format? 230 | 231 | First off, you should read RFC 1951. Second, yes. Look in zlib's 232 | contrib/puff directory. 233 | 234 | 31. Does zlib infringe on any patents? 235 | 236 | As far as we know, no. In fact, that was originally the whole point behind 237 | zlib. Look here for some more information: 238 | 239 | http://www.gzip.org/#faq11 240 | 241 | 32. Can zlib work with greater than 4 GB of data? 242 | 243 | Yes. inflate() and deflate() will process any amount of data correctly. 244 | Each call of inflate() or deflate() is limited to input and output chunks 245 | of the maximum value that can be stored in the compiler's "unsigned int" 246 | type, but there is no limit to the number of chunks. Note however that the 247 | strm.total_in and strm_total_out counters may be limited to 4 GB. These 248 | counters are provided as a convenience and are not used internally by 249 | inflate() or deflate(). The application can easily set up its own counters 250 | updated after each call of inflate() or deflate() to count beyond 4 GB. 251 | compress() and uncompress() may be limited to 4 GB, since they operate in a 252 | single call. gzseek() and gztell() may be limited to 4 GB depending on how 253 | zlib is compiled. See the zlibCompileFlags() function in zlib.h. 254 | 255 | The word "may" appears several times above since there is a 4 GB limit only 256 | if the compiler's "long" type is 32 bits. If the compiler's "long" type is 257 | 64 bits, then the limit is 16 exabytes. 258 | 259 | 33. Does zlib have any security vulnerabilities? 260 | 261 | The only one that we are aware of is potentially in gzprintf(). If zlib is 262 | compiled to use sprintf() or vsprintf(), then there is no protection 263 | against a buffer overflow of an 8K string space (or other value as set by 264 | gzbuffer()), other than the caller of gzprintf() assuring that the output 265 | will not exceed 8K. On the other hand, if zlib is compiled to use 266 | snprintf() or vsnprintf(), which should normally be the case, then there is 267 | no vulnerability. The ./configure script will display warnings if an 268 | insecure variation of sprintf() will be used by gzprintf(). Also the 269 | zlibCompileFlags() function will return information on what variant of 270 | sprintf() is used by gzprintf(). 271 | 272 | If you don't have snprintf() or vsnprintf() and would like one, you can 273 | find a portable implementation here: 274 | 275 | http://www.ijs.si/software/snprintf/ 276 | 277 | Note that you should be using the most recent version of zlib. Versions 278 | 1.1.3 and before were subject to a double-free vulnerability, and versions 279 | 1.2.1 and 1.2.2 were subject to an access exception when decompressing 280 | invalid compressed data. 281 | 282 | 34. Is there a Java version of zlib? 283 | 284 | Probably what you want is to use zlib in Java. zlib is already included 285 | as part of the Java SDK in the java.util.zip package. If you really want 286 | a version of zlib written in the Java language, look on the zlib home 287 | page for links: http://zlib.net/ . 288 | 289 | 35. I get this or that compiler or source-code scanner warning when I crank it 290 | up to maximally-pedantic. Can't you guys write proper code? 291 | 292 | Many years ago, we gave up attempting to avoid warnings on every compiler 293 | in the universe. It just got to be a waste of time, and some compilers 294 | were downright silly as well as contradicted each other. So now, we simply 295 | make sure that the code always works. 296 | 297 | 36. Valgrind (or some similar memory access checker) says that deflate is 298 | performing a conditional jump that depends on an uninitialized value. 299 | Isn't that a bug? 300 | 301 | No. That is intentional for performance reasons, and the output of deflate 302 | is not affected. This only started showing up recently since zlib 1.2.x 303 | uses malloc() by default for allocations, whereas earlier versions used 304 | calloc(), which zeros out the allocated memory. Even though the code was 305 | correct, versions 1.2.4 and later was changed to not stimulate these 306 | checkers. 307 | 308 | 37. Will zlib read the (insert any ancient or arcane format here) compressed 309 | data format? 310 | 311 | Probably not. Look in the comp.compression FAQ for pointers to various 312 | formats and associated software. 313 | 314 | 38. How can I encrypt/decrypt zip files with zlib? 315 | 316 | zlib doesn't support encryption. The original PKZIP encryption is very 317 | weak and can be broken with freely available programs. To get strong 318 | encryption, use GnuPG, http://www.gnupg.org/ , which already includes zlib 319 | compression. For PKZIP compatible "encryption", look at 320 | http://www.info-zip.org/ 321 | 322 | 39. What's the difference between the "gzip" and "deflate" HTTP 1.1 encodings? 323 | 324 | "gzip" is the gzip format, and "deflate" is the zlib format. They should 325 | probably have called the second one "zlib" instead to avoid confusion with 326 | the raw deflate compressed data format. While the HTTP 1.1 RFC 2616 327 | correctly points to the zlib specification in RFC 1950 for the "deflate" 328 | transfer encoding, there have been reports of servers and browsers that 329 | incorrectly produce or expect raw deflate data per the deflate 330 | specification in RFC 1951, most notably Microsoft. So even though the 331 | "deflate" transfer encoding using the zlib format would be the more 332 | efficient approach (and in fact exactly what the zlib format was designed 333 | for), using the "gzip" transfer encoding is probably more reliable due to 334 | an unfortunate choice of name on the part of the HTTP 1.1 authors. 335 | 336 | Bottom line: use the gzip format for HTTP 1.1 encoding. 337 | 338 | 40. Does zlib support the new "Deflate64" format introduced by PKWare? 339 | 340 | No. PKWare has apparently decided to keep that format proprietary, since 341 | they have not documented it as they have previous compression formats. In 342 | any case, the compression improvements are so modest compared to other more 343 | modern approaches, that it's not worth the effort to implement. 344 | 345 | 41. I'm having a problem with the zip functions in zlib, can you help? 346 | 347 | There are no zip functions in zlib. You are probably using minizip by 348 | Giles Vollant, which is found in the contrib directory of zlib. It is not 349 | part of zlib. In fact none of the stuff in contrib is part of zlib. The 350 | files in there are not supported by the zlib authors. You need to contact 351 | the authors of the respective contribution for help. 352 | 353 | 42. The match.asm code in contrib is under the GNU General Public License. 354 | Since it's part of zlib, doesn't that mean that all of zlib falls under the 355 | GNU GPL? 356 | 357 | No. The files in contrib are not part of zlib. They were contributed by 358 | other authors and are provided as a convenience to the user within the zlib 359 | distribution. Each item in contrib has its own license. 360 | 361 | 43. Is zlib subject to export controls? What is its ECCN? 362 | 363 | zlib is not subject to export controls, and so is classified as EAR99. 364 | 365 | 44. Can you please sign these lengthy legal documents and fax them back to us 366 | so that we can use your software in our product? 367 | 368 | No. Go away. Shoo. 369 | -------------------------------------------------------------------------------- /zlib/INDEX: -------------------------------------------------------------------------------- 1 | CMakeLists.txt cmake build file 2 | ChangeLog history of changes 3 | FAQ Frequently Asked Questions about zlib 4 | INDEX this file 5 | Makefile dummy Makefile that tells you to ./configure 6 | Makefile.in template for Unix Makefile 7 | README guess what 8 | configure configure script for Unix 9 | make_vms.com makefile for VMS 10 | test/example.c zlib usages examples for build testing 11 | test/minigzip.c minimal gzip-like functionality for build testing 12 | test/infcover.c inf*.c code coverage for build coverage testing 13 | treebuild.xml XML description of source file dependencies 14 | zconf.h.cmakein zconf.h template for cmake 15 | zconf.h.in zconf.h template for configure 16 | zlib.3 Man page for zlib 17 | zlib.3.pdf Man page in PDF format 18 | zlib.map Linux symbol information 19 | zlib.pc.in Template for pkg-config descriptor 20 | zlib.pc.cmakein zlib.pc template for cmake 21 | zlib2ansi perl script to convert source files for C++ compilation 22 | 23 | amiga/ makefiles for Amiga SAS C 24 | as400/ makefiles for AS/400 25 | doc/ documentation for formats and algorithms 26 | msdos/ makefiles for MSDOS 27 | nintendods/ makefile for Nintendo DS 28 | old/ makefiles for various architectures and zlib documentation 29 | files that have not yet been updated for zlib 1.2.x 30 | qnx/ makefiles for QNX 31 | watcom/ makefiles for OpenWatcom 32 | win32/ makefiles for Windows 33 | 34 | zlib public header files (required for library use): 35 | zconf.h 36 | zlib.h 37 | 38 | private source files used to build the zlib library: 39 | adler32.c 40 | compress.c 41 | crc32.c 42 | crc32.h 43 | deflate.c 44 | deflate.h 45 | gzclose.c 46 | gzguts.h 47 | gzlib.c 48 | gzread.c 49 | gzwrite.c 50 | infback.c 51 | inffast.c 52 | inffast.h 53 | inffixed.h 54 | inflate.c 55 | inflate.h 56 | inftrees.c 57 | inftrees.h 58 | trees.c 59 | trees.h 60 | uncompr.c 61 | zutil.c 62 | zutil.h 63 | 64 | source files for sample programs 65 | See examples/README.examples 66 | 67 | unsupported contributions by third parties 68 | See contrib/README.contrib 69 | -------------------------------------------------------------------------------- /zlib/README: -------------------------------------------------------------------------------- 1 | ZLIB DATA COMPRESSION LIBRARY 2 | 3 | zlib 1.2.8 is a general purpose data compression library. All the code is 4 | thread safe. The data format used by the zlib library is described by RFCs 5 | (Request for Comments) 1950 to 1952 in the files 6 | http://tools.ietf.org/html/rfc1950 (zlib format), rfc1951 (deflate format) and 7 | rfc1952 (gzip format). 8 | 9 | All functions of the compression library are documented in the file zlib.h 10 | (volunteer to write man pages welcome, contact zlib@gzip.org). A usage example 11 | of the library is given in the file test/example.c which also tests that 12 | the library is working correctly. Another example is given in the file 13 | test/minigzip.c. The compression library itself is composed of all source 14 | files in the root directory. 15 | 16 | To compile all files and run the test program, follow the instructions given at 17 | the top of Makefile.in. In short "./configure; make test", and if that goes 18 | well, "make install" should work for most flavors of Unix. For Windows, use 19 | one of the special makefiles in win32/ or contrib/vstudio/ . For VMS, use 20 | make_vms.com. 21 | 22 | Questions about zlib should be sent to , or to Gilles Vollant 23 | for the Windows DLL version. The zlib home page is 24 | http://zlib.net/ . Before reporting a problem, please check this site to 25 | verify that you have the latest version of zlib; otherwise get the latest 26 | version and check whether the problem still exists or not. 27 | 28 | PLEASE read the zlib FAQ http://zlib.net/zlib_faq.html before asking for help. 29 | 30 | Mark Nelson wrote an article about zlib for the Jan. 1997 31 | issue of Dr. Dobb's Journal; a copy of the article is available at 32 | http://marknelson.us/1997/01/01/zlib-engine/ . 33 | 34 | The changes made in version 1.2.8 are documented in the file ChangeLog. 35 | 36 | Unsupported third party contributions are provided in directory contrib/ . 37 | 38 | zlib is available in Java using the java.util.zip package, documented at 39 | http://java.sun.com/developer/technicalArticles/Programming/compression/ . 40 | 41 | A Perl interface to zlib written by Paul Marquess is available 42 | at CPAN (Comprehensive Perl Archive Network) sites, including 43 | http://search.cpan.org/~pmqs/IO-Compress-Zlib/ . 44 | 45 | A Python interface to zlib written by A.M. Kuchling is 46 | available in Python 1.5 and later versions, see 47 | http://docs.python.org/library/zlib.html . 48 | 49 | zlib is built into tcl: http://wiki.tcl.tk/4610 . 50 | 51 | An experimental package to read and write files in .zip format, written on top 52 | of zlib by Gilles Vollant , is available in the 53 | contrib/minizip directory of zlib. 54 | 55 | 56 | Notes for some targets: 57 | 58 | - For Windows DLL versions, please see win32/DLL_FAQ.txt 59 | 60 | - For 64-bit Irix, deflate.c must be compiled without any optimization. With 61 | -O, one libpng test fails. The test works in 32 bit mode (with the -n32 62 | compiler flag). The compiler bug has been reported to SGI. 63 | 64 | - zlib doesn't work with gcc 2.6.3 on a DEC 3000/300LX under OSF/1 2.1 it works 65 | when compiled with cc. 66 | 67 | - On Digital Unix 4.0D (formely OSF/1) on AlphaServer, the cc option -std1 is 68 | necessary to get gzprintf working correctly. This is done by configure. 69 | 70 | - zlib doesn't work on HP-UX 9.05 with some versions of /bin/cc. It works with 71 | other compilers. Use "make test" to check your compiler. 72 | 73 | - gzdopen is not supported on RISCOS or BEOS. 74 | 75 | - For PalmOs, see http://palmzlib.sourceforge.net/ 76 | 77 | 78 | Acknowledgments: 79 | 80 | The deflate format used by zlib was defined by Phil Katz. The deflate and 81 | zlib specifications were written by L. Peter Deutsch. Thanks to all the 82 | people who reported problems and suggested various improvements in zlib; they 83 | are too numerous to cite here. 84 | 85 | Copyright notice: 86 | 87 | (C) 1995-2013 Jean-loup Gailly and Mark Adler 88 | 89 | This software is provided 'as-is', without any express or implied 90 | warranty. In no event will the authors be held liable for any damages 91 | arising from the use of this software. 92 | 93 | Permission is granted to anyone to use this software for any purpose, 94 | including commercial applications, and to alter it and redistribute it 95 | freely, subject to the following restrictions: 96 | 97 | 1. The origin of this software must not be misrepresented; you must not 98 | claim that you wrote the original software. If you use this software 99 | in a product, an acknowledgment in the product documentation would be 100 | appreciated but is not required. 101 | 2. Altered source versions must be plainly marked as such, and must not be 102 | misrepresented as being the original software. 103 | 3. This notice may not be removed or altered from any source distribution. 104 | 105 | Jean-loup Gailly Mark Adler 106 | jloup@gzip.org madler@alumni.caltech.edu 107 | 108 | If you use the zlib library in a product, we would appreciate *not* receiving 109 | lengthy legal documents to sign. The sources are provided for free but without 110 | warranty of any kind. The library has been entirely written by Jean-loup 111 | Gailly and Mark Adler; it does not include third-party code. 112 | 113 | If you redistribute modified sources, we would appreciate that you include in 114 | the file ChangeLog history information documenting your changes. Please read 115 | the FAQ for more information on the distribution of modified source versions. 116 | -------------------------------------------------------------------------------- /zlib/adler32.c: -------------------------------------------------------------------------------- 1 | /* adler32.c -- compute the Adler-32 checksum of a data stream 2 | * Copyright (C) 1995-2011 Mark Adler 3 | * For conditions of distribution and use, see copyright notice in zlib.h 4 | */ 5 | 6 | /* @(#) $Id$ */ 7 | 8 | #include "zutil.h" 9 | 10 | #define local static 11 | 12 | local uLong adler32_combine_ OF((uLong adler1, uLong adler2, z_off64_t len2)); 13 | 14 | #define BASE 65521 /* largest prime smaller than 65536 */ 15 | #define NMAX 5552 16 | /* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */ 17 | 18 | #define DO1(buf,i) {adler += (buf)[i]; sum2 += adler;} 19 | #define DO2(buf,i) DO1(buf,i); DO1(buf,i+1); 20 | #define DO4(buf,i) DO2(buf,i); DO2(buf,i+2); 21 | #define DO8(buf,i) DO4(buf,i); DO4(buf,i+4); 22 | #define DO16(buf) DO8(buf,0); DO8(buf,8); 23 | 24 | /* use NO_DIVIDE if your processor does not do division in hardware -- 25 | try it both ways to see which is faster */ 26 | #ifdef NO_DIVIDE 27 | /* note that this assumes BASE is 65521, where 65536 % 65521 == 15 28 | (thank you to John Reiser for pointing this out) */ 29 | # define CHOP(a) \ 30 | do { \ 31 | unsigned long tmp = a >> 16; \ 32 | a &= 0xffffUL; \ 33 | a += (tmp << 4) - tmp; \ 34 | } while (0) 35 | # define MOD28(a) \ 36 | do { \ 37 | CHOP(a); \ 38 | if (a >= BASE) a -= BASE; \ 39 | } while (0) 40 | # define MOD(a) \ 41 | do { \ 42 | CHOP(a); \ 43 | MOD28(a); \ 44 | } while (0) 45 | # define MOD63(a) \ 46 | do { /* this assumes a is not negative */ \ 47 | z_off64_t tmp = a >> 32; \ 48 | a &= 0xffffffffL; \ 49 | a += (tmp << 8) - (tmp << 5) + tmp; \ 50 | tmp = a >> 16; \ 51 | a &= 0xffffL; \ 52 | a += (tmp << 4) - tmp; \ 53 | tmp = a >> 16; \ 54 | a &= 0xffffL; \ 55 | a += (tmp << 4) - tmp; \ 56 | if (a >= BASE) a -= BASE; \ 57 | } while (0) 58 | #else 59 | # define MOD(a) a %= BASE 60 | # define MOD28(a) a %= BASE 61 | # define MOD63(a) a %= BASE 62 | #endif 63 | 64 | /* ========================================================================= */ 65 | uLong ZEXPORT adler32(adler, buf, len) 66 | uLong adler; 67 | const Bytef *buf; 68 | uInt len; 69 | { 70 | unsigned long sum2; 71 | unsigned n; 72 | 73 | /* split Adler-32 into component sums */ 74 | sum2 = (adler >> 16) & 0xffff; 75 | adler &= 0xffff; 76 | 77 | /* in case user likes doing a byte at a time, keep it fast */ 78 | if (len == 1) { 79 | adler += buf[0]; 80 | if (adler >= BASE) 81 | adler -= BASE; 82 | sum2 += adler; 83 | if (sum2 >= BASE) 84 | sum2 -= BASE; 85 | return adler | (sum2 << 16); 86 | } 87 | 88 | /* initial Adler-32 value (deferred check for len == 1 speed) */ 89 | if (buf == Z_NULL) 90 | return 1L; 91 | 92 | /* in case short lengths are provided, keep it somewhat fast */ 93 | if (len < 16) { 94 | while (len--) { 95 | adler += *buf++; 96 | sum2 += adler; 97 | } 98 | if (adler >= BASE) 99 | adler -= BASE; 100 | MOD28(sum2); /* only added so many BASE's */ 101 | return adler | (sum2 << 16); 102 | } 103 | 104 | /* do length NMAX blocks -- requires just one modulo operation */ 105 | while (len >= NMAX) { 106 | len -= NMAX; 107 | n = NMAX / 16; /* NMAX is divisible by 16 */ 108 | do { 109 | DO16(buf); /* 16 sums unrolled */ 110 | buf += 16; 111 | } while (--n); 112 | MOD(adler); 113 | MOD(sum2); 114 | } 115 | 116 | /* do remaining bytes (less than NMAX, still just one modulo) */ 117 | if (len) { /* avoid modulos if none remaining */ 118 | while (len >= 16) { 119 | len -= 16; 120 | DO16(buf); 121 | buf += 16; 122 | } 123 | while (len--) { 124 | adler += *buf++; 125 | sum2 += adler; 126 | } 127 | MOD(adler); 128 | MOD(sum2); 129 | } 130 | 131 | /* return recombined sums */ 132 | return adler | (sum2 << 16); 133 | } 134 | 135 | /* ========================================================================= */ 136 | local uLong adler32_combine_(adler1, adler2, len2) 137 | uLong adler1; 138 | uLong adler2; 139 | z_off64_t len2; 140 | { 141 | unsigned long sum1; 142 | unsigned long sum2; 143 | unsigned rem; 144 | 145 | /* for negative len, return invalid adler32 as a clue for debugging */ 146 | if (len2 < 0) 147 | return 0xffffffffUL; 148 | 149 | /* the derivation of this formula is left as an exercise for the reader */ 150 | MOD63(len2); /* assumes len2 >= 0 */ 151 | rem = (unsigned)len2; 152 | sum1 = adler1 & 0xffff; 153 | sum2 = rem * sum1; 154 | MOD(sum2); 155 | sum1 += (adler2 & 0xffff) + BASE - 1; 156 | sum2 += ((adler1 >> 16) & 0xffff) + ((adler2 >> 16) & 0xffff) + BASE - rem; 157 | if (sum1 >= BASE) sum1 -= BASE; 158 | if (sum1 >= BASE) sum1 -= BASE; 159 | if (sum2 >= (BASE << 1)) sum2 -= (BASE << 1); 160 | if (sum2 >= BASE) sum2 -= BASE; 161 | return sum1 | (sum2 << 16); 162 | } 163 | 164 | /* ========================================================================= */ 165 | uLong ZEXPORT adler32_combine(adler1, adler2, len2) 166 | uLong adler1; 167 | uLong adler2; 168 | z_off_t len2; 169 | { 170 | return adler32_combine_(adler1, adler2, len2); 171 | } 172 | 173 | uLong ZEXPORT adler32_combine64(adler1, adler2, len2) 174 | uLong adler1; 175 | uLong adler2; 176 | z_off64_t len2; 177 | { 178 | return adler32_combine_(adler1, adler2, len2); 179 | } 180 | -------------------------------------------------------------------------------- /zlib/compress.c: -------------------------------------------------------------------------------- 1 | /* compress.c -- compress a memory buffer 2 | * Copyright (C) 1995-2005 Jean-loup Gailly. 3 | * For conditions of distribution and use, see copyright notice in zlib.h 4 | */ 5 | 6 | /* @(#) $Id$ */ 7 | 8 | #define ZLIB_INTERNAL 9 | #include "zlib.h" 10 | 11 | /* =========================================================================== 12 | Compresses the source buffer into the destination buffer. The level 13 | parameter has the same meaning as in deflateInit. sourceLen is the byte 14 | length of the source buffer. Upon entry, destLen is the total size of the 15 | destination buffer, which must be at least 0.1% larger than sourceLen plus 16 | 12 bytes. Upon exit, destLen is the actual size of the compressed buffer. 17 | 18 | compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough 19 | memory, Z_BUF_ERROR if there was not enough room in the output buffer, 20 | Z_STREAM_ERROR if the level parameter is invalid. 21 | */ 22 | int ZEXPORT compress2 (dest, destLen, source, sourceLen, level) 23 | Bytef *dest; 24 | uLongf *destLen; 25 | const Bytef *source; 26 | uLong sourceLen; 27 | int level; 28 | { 29 | z_stream stream; 30 | int err; 31 | 32 | stream.next_in = (z_const Bytef *)source; 33 | stream.avail_in = (uInt)sourceLen; 34 | #ifdef MAXSEG_64K 35 | /* Check for source > 64K on 16-bit machine: */ 36 | if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR; 37 | #endif 38 | stream.next_out = dest; 39 | stream.avail_out = (uInt)*destLen; 40 | if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR; 41 | 42 | stream.zalloc = (alloc_func)0; 43 | stream.zfree = (free_func)0; 44 | stream.opaque = (voidpf)0; 45 | 46 | err = deflateInit(&stream, level); 47 | if (err != Z_OK) return err; 48 | 49 | err = deflate(&stream, Z_FINISH); 50 | if (err != Z_STREAM_END) { 51 | deflateEnd(&stream); 52 | return err == Z_OK ? Z_BUF_ERROR : err; 53 | } 54 | *destLen = stream.total_out; 55 | 56 | err = deflateEnd(&stream); 57 | return err; 58 | } 59 | 60 | /* =========================================================================== 61 | */ 62 | int ZEXPORT compress (dest, destLen, source, sourceLen) 63 | Bytef *dest; 64 | uLongf *destLen; 65 | const Bytef *source; 66 | uLong sourceLen; 67 | { 68 | return compress2(dest, destLen, source, sourceLen, Z_DEFAULT_COMPRESSION); 69 | } 70 | 71 | /* =========================================================================== 72 | If the default memLevel or windowBits for deflateInit() is changed, then 73 | this function needs to be updated. 74 | */ 75 | uLong ZEXPORT compressBound (sourceLen) 76 | uLong sourceLen; 77 | { 78 | return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) + 79 | (sourceLen >> 25) + 13; 80 | } 81 | -------------------------------------------------------------------------------- /zlib/crc32.c: -------------------------------------------------------------------------------- 1 | /* crc32.c -- compute the CRC-32 of a data stream 2 | * Copyright (C) 1995-2006, 2010, 2011, 2012 Mark Adler 3 | * For conditions of distribution and use, see copyright notice in zlib.h 4 | * 5 | * Thanks to Rodney Brown for his contribution of faster 6 | * CRC methods: exclusive-oring 32 bits of data at a time, and pre-computing 7 | * tables for updating the shift register in one step with three exclusive-ors 8 | * instead of four steps with four exclusive-ors. This results in about a 9 | * factor of two increase in speed on a Power PC G4 (PPC7455) using gcc -O3. 10 | */ 11 | 12 | /* @(#) $Id$ */ 13 | 14 | /* 15 | Note on the use of DYNAMIC_CRC_TABLE: there is no mutex or semaphore 16 | protection on the static variables used to control the first-use generation 17 | of the crc tables. Therefore, if you #define DYNAMIC_CRC_TABLE, you should 18 | first call get_crc_table() to initialize the tables before allowing more than 19 | one thread to use crc32(). 20 | 21 | DYNAMIC_CRC_TABLE and MAKECRCH can be #defined to write out crc32.h. 22 | */ 23 | 24 | #ifdef MAKECRCH 25 | # include 26 | # ifndef DYNAMIC_CRC_TABLE 27 | # define DYNAMIC_CRC_TABLE 28 | # endif /* !DYNAMIC_CRC_TABLE */ 29 | #endif /* MAKECRCH */ 30 | 31 | #include "zutil.h" /* for STDC and FAR definitions */ 32 | 33 | #define local static 34 | 35 | /* Definitions for doing the crc four data bytes at a time. */ 36 | #if !defined(NOBYFOUR) && defined(Z_U4) 37 | # define BYFOUR 38 | #endif 39 | #ifdef BYFOUR 40 | local unsigned long crc32_little OF((unsigned long, 41 | const unsigned char FAR *, unsigned)); 42 | local unsigned long crc32_big OF((unsigned long, 43 | const unsigned char FAR *, unsigned)); 44 | # define TBLS 8 45 | #else 46 | # define TBLS 1 47 | #endif /* BYFOUR */ 48 | 49 | /* Local functions for crc concatenation */ 50 | local unsigned long gf2_matrix_times OF((unsigned long *mat, 51 | unsigned long vec)); 52 | local void gf2_matrix_square OF((unsigned long *square, unsigned long *mat)); 53 | local uLong crc32_combine_ OF((uLong crc1, uLong crc2, z_off64_t len2)); 54 | 55 | 56 | #ifdef DYNAMIC_CRC_TABLE 57 | 58 | local volatile int crc_table_empty = 1; 59 | local z_crc_t FAR crc_table[TBLS][256]; 60 | local void make_crc_table OF((void)); 61 | #ifdef MAKECRCH 62 | local void write_table OF((FILE *, const z_crc_t FAR *)); 63 | #endif /* MAKECRCH */ 64 | /* 65 | Generate tables for a byte-wise 32-bit CRC calculation on the polynomial: 66 | x^32+x^26+x^23+x^22+x^16+x^12+x^11+x^10+x^8+x^7+x^5+x^4+x^2+x+1. 67 | 68 | Polynomials over GF(2) are represented in binary, one bit per coefficient, 69 | with the lowest powers in the most significant bit. Then adding polynomials 70 | is just exclusive-or, and multiplying a polynomial by x is a right shift by 71 | one. If we call the above polynomial p, and represent a byte as the 72 | polynomial q, also with the lowest power in the most significant bit (so the 73 | byte 0xb1 is the polynomial x^7+x^3+x+1), then the CRC is (q*x^32) mod p, 74 | where a mod b means the remainder after dividing a by b. 75 | 76 | This calculation is done using the shift-register method of multiplying and 77 | taking the remainder. The register is initialized to zero, and for each 78 | incoming bit, x^32 is added mod p to the register if the bit is a one (where 79 | x^32 mod p is p+x^32 = x^26+...+1), and the register is multiplied mod p by 80 | x (which is shifting right by one and adding x^32 mod p if the bit shifted 81 | out is a one). We start with the highest power (least significant bit) of 82 | q and repeat for all eight bits of q. 83 | 84 | The first table is simply the CRC of all possible eight bit values. This is 85 | all the information needed to generate CRCs on data a byte at a time for all 86 | combinations of CRC register values and incoming bytes. The remaining tables 87 | allow for word-at-a-time CRC calculation for both big-endian and little- 88 | endian machines, where a word is four bytes. 89 | */ 90 | local void make_crc_table() 91 | { 92 | z_crc_t c; 93 | int n, k; 94 | z_crc_t poly; /* polynomial exclusive-or pattern */ 95 | /* terms of polynomial defining this crc (except x^32): */ 96 | static volatile int first = 1; /* flag to limit concurrent making */ 97 | static const unsigned char p[] = {0,1,2,4,5,7,8,10,11,12,16,22,23,26}; 98 | 99 | /* See if another task is already doing this (not thread-safe, but better 100 | than nothing -- significantly reduces duration of vulnerability in 101 | case the advice about DYNAMIC_CRC_TABLE is ignored) */ 102 | if (first) { 103 | first = 0; 104 | 105 | /* make exclusive-or pattern from polynomial (0xedb88320UL) */ 106 | poly = 0; 107 | for (n = 0; n < (int)(sizeof(p)/sizeof(unsigned char)); n++) 108 | poly |= (z_crc_t)1 << (31 - p[n]); 109 | 110 | /* generate a crc for every 8-bit value */ 111 | for (n = 0; n < 256; n++) { 112 | c = (z_crc_t)n; 113 | for (k = 0; k < 8; k++) 114 | c = c & 1 ? poly ^ (c >> 1) : c >> 1; 115 | crc_table[0][n] = c; 116 | } 117 | 118 | #ifdef BYFOUR 119 | /* generate crc for each value followed by one, two, and three zeros, 120 | and then the byte reversal of those as well as the first table */ 121 | for (n = 0; n < 256; n++) { 122 | c = crc_table[0][n]; 123 | crc_table[4][n] = ZSWAP32(c); 124 | for (k = 1; k < 4; k++) { 125 | c = crc_table[0][c & 0xff] ^ (c >> 8); 126 | crc_table[k][n] = c; 127 | crc_table[k + 4][n] = ZSWAP32(c); 128 | } 129 | } 130 | #endif /* BYFOUR */ 131 | 132 | crc_table_empty = 0; 133 | } 134 | else { /* not first */ 135 | /* wait for the other guy to finish (not efficient, but rare) */ 136 | while (crc_table_empty) 137 | ; 138 | } 139 | 140 | #ifdef MAKECRCH 141 | /* write out CRC tables to crc32.h */ 142 | { 143 | FILE *out; 144 | 145 | out = fopen("crc32.h", "w"); 146 | if (out == NULL) return; 147 | fprintf(out, "/* crc32.h -- tables for rapid CRC calculation\n"); 148 | fprintf(out, " * Generated automatically by crc32.c\n */\n\n"); 149 | fprintf(out, "local const z_crc_t FAR "); 150 | fprintf(out, "crc_table[TBLS][256] =\n{\n {\n"); 151 | write_table(out, crc_table[0]); 152 | # ifdef BYFOUR 153 | fprintf(out, "#ifdef BYFOUR\n"); 154 | for (k = 1; k < 8; k++) { 155 | fprintf(out, " },\n {\n"); 156 | write_table(out, crc_table[k]); 157 | } 158 | fprintf(out, "#endif\n"); 159 | # endif /* BYFOUR */ 160 | fprintf(out, " }\n};\n"); 161 | fclose(out); 162 | } 163 | #endif /* MAKECRCH */ 164 | } 165 | 166 | #ifdef MAKECRCH 167 | local void write_table(out, table) 168 | FILE *out; 169 | const z_crc_t FAR *table; 170 | { 171 | int n; 172 | 173 | for (n = 0; n < 256; n++) 174 | fprintf(out, "%s0x%08lxUL%s", n % 5 ? "" : " ", 175 | (unsigned long)(table[n]), 176 | n == 255 ? "\n" : (n % 5 == 4 ? ",\n" : ", ")); 177 | } 178 | #endif /* MAKECRCH */ 179 | 180 | #else /* !DYNAMIC_CRC_TABLE */ 181 | /* ======================================================================== 182 | * Tables of CRC-32s of all single-byte values, made by make_crc_table(). 183 | */ 184 | #include "crc32.h" 185 | #endif /* DYNAMIC_CRC_TABLE */ 186 | 187 | /* ========================================================================= 188 | * This function can be used by asm versions of crc32() 189 | */ 190 | const z_crc_t FAR * ZEXPORT get_crc_table() 191 | { 192 | #ifdef DYNAMIC_CRC_TABLE 193 | if (crc_table_empty) 194 | make_crc_table(); 195 | #endif /* DYNAMIC_CRC_TABLE */ 196 | return (const z_crc_t FAR *)crc_table; 197 | } 198 | 199 | /* ========================================================================= */ 200 | #define DO1 crc = crc_table[0][((int)crc ^ (*buf++)) & 0xff] ^ (crc >> 8) 201 | #define DO8 DO1; DO1; DO1; DO1; DO1; DO1; DO1; DO1 202 | 203 | /* ========================================================================= */ 204 | unsigned long ZEXPORT crc32(crc, buf, len) 205 | unsigned long crc; 206 | const unsigned char FAR *buf; 207 | uInt len; 208 | { 209 | if (buf == Z_NULL) return 0UL; 210 | 211 | #ifdef DYNAMIC_CRC_TABLE 212 | if (crc_table_empty) 213 | make_crc_table(); 214 | #endif /* DYNAMIC_CRC_TABLE */ 215 | 216 | #ifdef BYFOUR 217 | if (sizeof(void *) == sizeof(ptrdiff_t)) { 218 | z_crc_t endian; 219 | 220 | endian = 1; 221 | if (*((unsigned char *)(&endian))) 222 | return crc32_little(crc, buf, len); 223 | else 224 | return crc32_big(crc, buf, len); 225 | } 226 | #endif /* BYFOUR */ 227 | crc = crc ^ 0xffffffffUL; 228 | while (len >= 8) { 229 | DO8; 230 | len -= 8; 231 | } 232 | if (len) do { 233 | DO1; 234 | } while (--len); 235 | return crc ^ 0xffffffffUL; 236 | } 237 | 238 | #ifdef BYFOUR 239 | 240 | /* ========================================================================= */ 241 | #define DOLIT4 c ^= *buf4++; \ 242 | c = crc_table[3][c & 0xff] ^ crc_table[2][(c >> 8) & 0xff] ^ \ 243 | crc_table[1][(c >> 16) & 0xff] ^ crc_table[0][c >> 24] 244 | #define DOLIT32 DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4 245 | 246 | /* ========================================================================= */ 247 | local unsigned long crc32_little(crc, buf, len) 248 | unsigned long crc; 249 | const unsigned char FAR *buf; 250 | unsigned len; 251 | { 252 | register z_crc_t c; 253 | register const z_crc_t FAR *buf4; 254 | 255 | c = (z_crc_t)crc; 256 | c = ~c; 257 | while (len && ((ptrdiff_t)buf & 3)) { 258 | c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8); 259 | len--; 260 | } 261 | 262 | buf4 = (const z_crc_t FAR *)(const void FAR *)buf; 263 | while (len >= 32) { 264 | DOLIT32; 265 | len -= 32; 266 | } 267 | while (len >= 4) { 268 | DOLIT4; 269 | len -= 4; 270 | } 271 | buf = (const unsigned char FAR *)buf4; 272 | 273 | if (len) do { 274 | c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8); 275 | } while (--len); 276 | c = ~c; 277 | return (unsigned long)c; 278 | } 279 | 280 | /* ========================================================================= */ 281 | #define DOBIG4 c ^= *++buf4; \ 282 | c = crc_table[4][c & 0xff] ^ crc_table[5][(c >> 8) & 0xff] ^ \ 283 | crc_table[6][(c >> 16) & 0xff] ^ crc_table[7][c >> 24] 284 | #define DOBIG32 DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4 285 | 286 | /* ========================================================================= */ 287 | local unsigned long crc32_big(crc, buf, len) 288 | unsigned long crc; 289 | const unsigned char FAR *buf; 290 | unsigned len; 291 | { 292 | register z_crc_t c; 293 | register const z_crc_t FAR *buf4; 294 | 295 | c = ZSWAP32((z_crc_t)crc); 296 | c = ~c; 297 | while (len && ((ptrdiff_t)buf & 3)) { 298 | c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8); 299 | len--; 300 | } 301 | 302 | buf4 = (const z_crc_t FAR *)(const void FAR *)buf; 303 | buf4--; 304 | while (len >= 32) { 305 | DOBIG32; 306 | len -= 32; 307 | } 308 | while (len >= 4) { 309 | DOBIG4; 310 | len -= 4; 311 | } 312 | buf4++; 313 | buf = (const unsigned char FAR *)buf4; 314 | 315 | if (len) do { 316 | c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8); 317 | } while (--len); 318 | c = ~c; 319 | return (unsigned long)(ZSWAP32(c)); 320 | } 321 | 322 | #endif /* BYFOUR */ 323 | 324 | #define GF2_DIM 32 /* dimension of GF(2) vectors (length of CRC) */ 325 | 326 | /* ========================================================================= */ 327 | local unsigned long gf2_matrix_times(mat, vec) 328 | unsigned long *mat; 329 | unsigned long vec; 330 | { 331 | unsigned long sum; 332 | 333 | sum = 0; 334 | while (vec) { 335 | if (vec & 1) 336 | sum ^= *mat; 337 | vec >>= 1; 338 | mat++; 339 | } 340 | return sum; 341 | } 342 | 343 | /* ========================================================================= */ 344 | local void gf2_matrix_square(square, mat) 345 | unsigned long *square; 346 | unsigned long *mat; 347 | { 348 | int n; 349 | 350 | for (n = 0; n < GF2_DIM; n++) 351 | square[n] = gf2_matrix_times(mat, mat[n]); 352 | } 353 | 354 | /* ========================================================================= */ 355 | local uLong crc32_combine_(crc1, crc2, len2) 356 | uLong crc1; 357 | uLong crc2; 358 | z_off64_t len2; 359 | { 360 | int n; 361 | unsigned long row; 362 | unsigned long even[GF2_DIM]; /* even-power-of-two zeros operator */ 363 | unsigned long odd[GF2_DIM]; /* odd-power-of-two zeros operator */ 364 | 365 | /* degenerate case (also disallow negative lengths) */ 366 | if (len2 <= 0) 367 | return crc1; 368 | 369 | /* put operator for one zero bit in odd */ 370 | odd[0] = 0xedb88320UL; /* CRC-32 polynomial */ 371 | row = 1; 372 | for (n = 1; n < GF2_DIM; n++) { 373 | odd[n] = row; 374 | row <<= 1; 375 | } 376 | 377 | /* put operator for two zero bits in even */ 378 | gf2_matrix_square(even, odd); 379 | 380 | /* put operator for four zero bits in odd */ 381 | gf2_matrix_square(odd, even); 382 | 383 | /* apply len2 zeros to crc1 (first square will put the operator for one 384 | zero byte, eight zero bits, in even) */ 385 | do { 386 | /* apply zeros operator for this bit of len2 */ 387 | gf2_matrix_square(even, odd); 388 | if (len2 & 1) 389 | crc1 = gf2_matrix_times(even, crc1); 390 | len2 >>= 1; 391 | 392 | /* if no more bits set, then done */ 393 | if (len2 == 0) 394 | break; 395 | 396 | /* another iteration of the loop with odd and even swapped */ 397 | gf2_matrix_square(odd, even); 398 | if (len2 & 1) 399 | crc1 = gf2_matrix_times(odd, crc1); 400 | len2 >>= 1; 401 | 402 | /* if no more bits set, then done */ 403 | } while (len2 != 0); 404 | 405 | /* return combined crc */ 406 | crc1 ^= crc2; 407 | return crc1; 408 | } 409 | 410 | /* ========================================================================= */ 411 | uLong ZEXPORT crc32_combine(crc1, crc2, len2) 412 | uLong crc1; 413 | uLong crc2; 414 | z_off_t len2; 415 | { 416 | return crc32_combine_(crc1, crc2, len2); 417 | } 418 | 419 | uLong ZEXPORT crc32_combine64(crc1, crc2, len2) 420 | uLong crc1; 421 | uLong crc2; 422 | z_off64_t len2; 423 | { 424 | return crc32_combine_(crc1, crc2, len2); 425 | } 426 | -------------------------------------------------------------------------------- /zlib/deflate.h: -------------------------------------------------------------------------------- 1 | /* deflate.h -- internal compression state 2 | * Copyright (C) 1995-2012 Jean-loup Gailly 3 | * For conditions of distribution and use, see copyright notice in zlib.h 4 | */ 5 | 6 | /* WARNING: this file should *not* be used by applications. It is 7 | part of the implementation of the compression library and is 8 | subject to change. Applications should only use zlib.h. 9 | */ 10 | 11 | /* @(#) $Id$ */ 12 | 13 | #ifndef DEFLATE_H 14 | #define DEFLATE_H 15 | 16 | #include "zutil.h" 17 | 18 | /* define NO_GZIP when compiling if you want to disable gzip header and 19 | trailer creation by deflate(). NO_GZIP would be used to avoid linking in 20 | the crc code when it is not needed. For shared libraries, gzip encoding 21 | should be left enabled. */ 22 | #ifndef NO_GZIP 23 | # define GZIP 24 | #endif 25 | 26 | /* =========================================================================== 27 | * Internal compression state. 28 | */ 29 | 30 | #define LENGTH_CODES 29 31 | /* number of length codes, not counting the special END_BLOCK code */ 32 | 33 | #define LITERALS 256 34 | /* number of literal bytes 0..255 */ 35 | 36 | #define L_CODES (LITERALS+1+LENGTH_CODES) 37 | /* number of Literal or Length codes, including the END_BLOCK code */ 38 | 39 | #define D_CODES 30 40 | /* number of distance codes */ 41 | 42 | #define BL_CODES 19 43 | /* number of codes used to transfer the bit lengths */ 44 | 45 | #define HEAP_SIZE (2*L_CODES+1) 46 | /* maximum heap size */ 47 | 48 | #define MAX_BITS 15 49 | /* All codes must not exceed MAX_BITS bits */ 50 | 51 | #define Buf_size 16 52 | /* size of bit buffer in bi_buf */ 53 | 54 | #define INIT_STATE 42 55 | #define EXTRA_STATE 69 56 | #define NAME_STATE 73 57 | #define COMMENT_STATE 91 58 | #define HCRC_STATE 103 59 | #define BUSY_STATE 113 60 | #define FINISH_STATE 666 61 | /* Stream status */ 62 | 63 | 64 | /* Data structure describing a single value and its code string. */ 65 | typedef struct ct_data_s { 66 | union { 67 | ush freq; /* frequency count */ 68 | ush code; /* bit string */ 69 | } fc; 70 | union { 71 | ush dad; /* father node in Huffman tree */ 72 | ush len; /* length of bit string */ 73 | } dl; 74 | } FAR ct_data; 75 | 76 | #define Freq fc.freq 77 | #define Code fc.code 78 | #define Dad dl.dad 79 | #define Len dl.len 80 | 81 | typedef struct static_tree_desc_s static_tree_desc; 82 | 83 | typedef struct tree_desc_s { 84 | ct_data *dyn_tree; /* the dynamic tree */ 85 | int max_code; /* largest code with non zero frequency */ 86 | static_tree_desc *stat_desc; /* the corresponding static tree */ 87 | } FAR tree_desc; 88 | 89 | typedef ush Pos; 90 | typedef Pos FAR Posf; 91 | typedef unsigned IPos; 92 | 93 | /* A Pos is an index in the character window. We use short instead of int to 94 | * save space in the various tables. IPos is used only for parameter passing. 95 | */ 96 | 97 | typedef struct internal_state { 98 | z_streamp strm; /* pointer back to this zlib stream */ 99 | int status; /* as the name implies */ 100 | Bytef *pending_buf; /* output still pending */ 101 | ulg pending_buf_size; /* size of pending_buf */ 102 | Bytef *pending_out; /* next pending byte to output to the stream */ 103 | uInt pending; /* nb of bytes in the pending buffer */ 104 | int wrap; /* bit 0 true for zlib, bit 1 true for gzip */ 105 | gz_headerp gzhead; /* gzip header information to write */ 106 | uInt gzindex; /* where in extra, name, or comment */ 107 | Byte method; /* can only be DEFLATED */ 108 | int last_flush; /* value of flush param for previous deflate call */ 109 | 110 | /* used by deflate.c: */ 111 | 112 | uInt w_size; /* LZ77 window size (32K by default) */ 113 | uInt w_bits; /* log2(w_size) (8..16) */ 114 | uInt w_mask; /* w_size - 1 */ 115 | 116 | Bytef *window; 117 | /* Sliding window. Input bytes are read into the second half of the window, 118 | * and move to the first half later to keep a dictionary of at least wSize 119 | * bytes. With this organization, matches are limited to a distance of 120 | * wSize-MAX_MATCH bytes, but this ensures that IO is always 121 | * performed with a length multiple of the block size. Also, it limits 122 | * the window size to 64K, which is quite useful on MSDOS. 123 | * To do: use the user input buffer as sliding window. 124 | */ 125 | 126 | ulg window_size; 127 | /* Actual size of window: 2*wSize, except when the user input buffer 128 | * is directly used as sliding window. 129 | */ 130 | 131 | Posf *prev; 132 | /* Link to older string with same hash index. To limit the size of this 133 | * array to 64K, this link is maintained only for the last 32K strings. 134 | * An index in this array is thus a window index modulo 32K. 135 | */ 136 | 137 | Posf *head; /* Heads of the hash chains or NIL. */ 138 | 139 | uInt ins_h; /* hash index of string to be inserted */ 140 | uInt hash_size; /* number of elements in hash table */ 141 | uInt hash_bits; /* log2(hash_size) */ 142 | uInt hash_mask; /* hash_size-1 */ 143 | 144 | uInt hash_shift; 145 | /* Number of bits by which ins_h must be shifted at each input 146 | * step. It must be such that after MIN_MATCH steps, the oldest 147 | * byte no longer takes part in the hash key, that is: 148 | * hash_shift * MIN_MATCH >= hash_bits 149 | */ 150 | 151 | long block_start; 152 | /* Window position at the beginning of the current output block. Gets 153 | * negative when the window is moved backwards. 154 | */ 155 | 156 | uInt match_length; /* length of best match */ 157 | IPos prev_match; /* previous match */ 158 | int match_available; /* set if previous match exists */ 159 | uInt strstart; /* start of string to insert */ 160 | uInt match_start; /* start of matching string */ 161 | uInt lookahead; /* number of valid bytes ahead in window */ 162 | 163 | uInt prev_length; 164 | /* Length of the best match at previous step. Matches not greater than this 165 | * are discarded. This is used in the lazy match evaluation. 166 | */ 167 | 168 | uInt max_chain_length; 169 | /* To speed up deflation, hash chains are never searched beyond this 170 | * length. A higher limit improves compression ratio but degrades the 171 | * speed. 172 | */ 173 | 174 | uInt max_lazy_match; 175 | /* Attempt to find a better match only when the current match is strictly 176 | * smaller than this value. This mechanism is used only for compression 177 | * levels >= 4. 178 | */ 179 | # define max_insert_length max_lazy_match 180 | /* Insert new strings in the hash table only if the match length is not 181 | * greater than this length. This saves time but degrades compression. 182 | * max_insert_length is used only for compression levels <= 3. 183 | */ 184 | 185 | int level; /* compression level (1..9) */ 186 | int strategy; /* favor or force Huffman coding*/ 187 | 188 | uInt good_match; 189 | /* Use a faster search when the previous match is longer than this */ 190 | 191 | int nice_match; /* Stop searching when current match exceeds this */ 192 | 193 | /* used by trees.c: */ 194 | /* Didn't use ct_data typedef below to suppress compiler warning */ 195 | struct ct_data_s dyn_ltree[HEAP_SIZE]; /* literal and length tree */ 196 | struct ct_data_s dyn_dtree[2*D_CODES+1]; /* distance tree */ 197 | struct ct_data_s bl_tree[2*BL_CODES+1]; /* Huffman tree for bit lengths */ 198 | 199 | struct tree_desc_s l_desc; /* desc. for literal tree */ 200 | struct tree_desc_s d_desc; /* desc. for distance tree */ 201 | struct tree_desc_s bl_desc; /* desc. for bit length tree */ 202 | 203 | ush bl_count[MAX_BITS+1]; 204 | /* number of codes at each bit length for an optimal tree */ 205 | 206 | int heap[2*L_CODES+1]; /* heap used to build the Huffman trees */ 207 | int heap_len; /* number of elements in the heap */ 208 | int heap_max; /* element of largest frequency */ 209 | /* The sons of heap[n] are heap[2*n] and heap[2*n+1]. heap[0] is not used. 210 | * The same heap array is used to build all trees. 211 | */ 212 | 213 | uch depth[2*L_CODES+1]; 214 | /* Depth of each subtree used as tie breaker for trees of equal frequency 215 | */ 216 | 217 | uchf *l_buf; /* buffer for literals or lengths */ 218 | 219 | uInt lit_bufsize; 220 | /* Size of match buffer for literals/lengths. There are 4 reasons for 221 | * limiting lit_bufsize to 64K: 222 | * - frequencies can be kept in 16 bit counters 223 | * - if compression is not successful for the first block, all input 224 | * data is still in the window so we can still emit a stored block even 225 | * when input comes from standard input. (This can also be done for 226 | * all blocks if lit_bufsize is not greater than 32K.) 227 | * - if compression is not successful for a file smaller than 64K, we can 228 | * even emit a stored file instead of a stored block (saving 5 bytes). 229 | * This is applicable only for zip (not gzip or zlib). 230 | * - creating new Huffman trees less frequently may not provide fast 231 | * adaptation to changes in the input data statistics. (Take for 232 | * example a binary file with poorly compressible code followed by 233 | * a highly compressible string table.) Smaller buffer sizes give 234 | * fast adaptation but have of course the overhead of transmitting 235 | * trees more frequently. 236 | * - I can't count above 4 237 | */ 238 | 239 | uInt last_lit; /* running index in l_buf */ 240 | 241 | ushf *d_buf; 242 | /* Buffer for distances. To simplify the code, d_buf and l_buf have 243 | * the same number of elements. To use different lengths, an extra flag 244 | * array would be necessary. 245 | */ 246 | 247 | ulg opt_len; /* bit length of current block with optimal trees */ 248 | ulg static_len; /* bit length of current block with static trees */ 249 | uInt matches; /* number of string matches in current block */ 250 | uInt insert; /* bytes at end of window left to insert */ 251 | 252 | #ifdef DEBUG 253 | ulg compressed_len; /* total bit length of compressed file mod 2^32 */ 254 | ulg bits_sent; /* bit length of compressed data sent mod 2^32 */ 255 | #endif 256 | 257 | ush bi_buf; 258 | /* Output buffer. bits are inserted starting at the bottom (least 259 | * significant bits). 260 | */ 261 | int bi_valid; 262 | /* Number of valid bits in bi_buf. All bits above the last valid bit 263 | * are always zero. 264 | */ 265 | 266 | ulg high_water; 267 | /* High water mark offset in window for initialized bytes -- bytes above 268 | * this are set to zero in order to avoid memory check warnings when 269 | * longest match routines access bytes past the input. This is then 270 | * updated to the new high water mark. 271 | */ 272 | 273 | } FAR deflate_state; 274 | 275 | /* Output a byte on the stream. 276 | * IN assertion: there is enough room in pending_buf. 277 | */ 278 | #define put_byte(s, c) {s->pending_buf[s->pending++] = (c);} 279 | 280 | 281 | #define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1) 282 | /* Minimum amount of lookahead, except at the end of the input file. 283 | * See deflate.c for comments about the MIN_MATCH+1. 284 | */ 285 | 286 | #define MAX_DIST(s) ((s)->w_size-MIN_LOOKAHEAD) 287 | /* In order to simplify the code, particularly on 16 bit machines, match 288 | * distances are limited to MAX_DIST instead of WSIZE. 289 | */ 290 | 291 | #define WIN_INIT MAX_MATCH 292 | /* Number of bytes after end of data in window to initialize in order to avoid 293 | memory checker errors from longest match routines */ 294 | 295 | /* in trees.c */ 296 | void ZLIB_INTERNAL _tr_init OF((deflate_state *s)); 297 | int ZLIB_INTERNAL _tr_tally OF((deflate_state *s, unsigned dist, unsigned lc)); 298 | void ZLIB_INTERNAL _tr_flush_block OF((deflate_state *s, charf *buf, 299 | ulg stored_len, int last)); 300 | void ZLIB_INTERNAL _tr_flush_bits OF((deflate_state *s)); 301 | void ZLIB_INTERNAL _tr_align OF((deflate_state *s)); 302 | void ZLIB_INTERNAL _tr_stored_block OF((deflate_state *s, charf *buf, 303 | ulg stored_len, int last)); 304 | 305 | #define d_code(dist) \ 306 | ((dist) < 256 ? _dist_code[dist] : _dist_code[256+((dist)>>7)]) 307 | /* Mapping from a distance to a distance code. dist is the distance - 1 and 308 | * must not have side effects. _dist_code[256] and _dist_code[257] are never 309 | * used. 310 | */ 311 | 312 | #ifndef DEBUG 313 | /* Inline versions of _tr_tally for speed: */ 314 | 315 | #if defined(GEN_TREES_H) || !defined(STDC) 316 | extern uch ZLIB_INTERNAL _length_code[]; 317 | extern uch ZLIB_INTERNAL _dist_code[]; 318 | #else 319 | extern const uch ZLIB_INTERNAL _length_code[]; 320 | extern const uch ZLIB_INTERNAL _dist_code[]; 321 | #endif 322 | 323 | # define _tr_tally_lit(s, c, flush) \ 324 | { uch cc = (c); \ 325 | s->d_buf[s->last_lit] = 0; \ 326 | s->l_buf[s->last_lit++] = cc; \ 327 | s->dyn_ltree[cc].Freq++; \ 328 | flush = (s->last_lit == s->lit_bufsize-1); \ 329 | } 330 | # define _tr_tally_dist(s, distance, length, flush) \ 331 | { uch len = (length); \ 332 | ush dist = (distance); \ 333 | s->d_buf[s->last_lit] = dist; \ 334 | s->l_buf[s->last_lit++] = len; \ 335 | dist--; \ 336 | s->dyn_ltree[_length_code[len]+LITERALS+1].Freq++; \ 337 | s->dyn_dtree[d_code(dist)].Freq++; \ 338 | flush = (s->last_lit == s->lit_bufsize-1); \ 339 | } 340 | #else 341 | # define _tr_tally_lit(s, c, flush) flush = _tr_tally(s, 0, c) 342 | # define _tr_tally_dist(s, distance, length, flush) \ 343 | flush = _tr_tally(s, distance, length) 344 | #endif 345 | 346 | #endif /* DEFLATE_H */ 347 | -------------------------------------------------------------------------------- /zlib/gzclose.c: -------------------------------------------------------------------------------- 1 | /* gzclose.c -- zlib gzclose() function 2 | * Copyright (C) 2004, 2010 Mark Adler 3 | * For conditions of distribution and use, see copyright notice in zlib.h 4 | */ 5 | 6 | #include "gzguts.h" 7 | 8 | /* gzclose() is in a separate file so that it is linked in only if it is used. 9 | That way the other gzclose functions can be used instead to avoid linking in 10 | unneeded compression or decompression routines. */ 11 | int ZEXPORT gzclose(file) 12 | gzFile file; 13 | { 14 | #ifndef NO_GZCOMPRESS 15 | gz_statep state; 16 | 17 | if (file == NULL) 18 | return Z_STREAM_ERROR; 19 | state = (gz_statep)file; 20 | 21 | return state->mode == GZ_READ ? gzclose_r(file) : gzclose_w(file); 22 | #else 23 | return gzclose_r(file); 24 | #endif 25 | } 26 | -------------------------------------------------------------------------------- /zlib/gzguts.h: -------------------------------------------------------------------------------- 1 | /* gzguts.h -- zlib internal header definitions for gz* operations 2 | * Copyright (C) 2004, 2005, 2010, 2011, 2012, 2013 Mark Adler 3 | * For conditions of distribution and use, see copyright notice in zlib.h 4 | */ 5 | 6 | #ifdef _LARGEFILE64_SOURCE 7 | # ifndef _LARGEFILE_SOURCE 8 | # define _LARGEFILE_SOURCE 1 9 | # endif 10 | # ifdef _FILE_OFFSET_BITS 11 | # undef _FILE_OFFSET_BITS 12 | # endif 13 | #endif 14 | 15 | #ifdef HAVE_HIDDEN 16 | # define ZLIB_INTERNAL __attribute__((visibility ("hidden"))) 17 | #else 18 | # define ZLIB_INTERNAL 19 | #endif 20 | 21 | #include 22 | #include "zlib.h" 23 | #ifdef STDC 24 | # include 25 | # include 26 | # include 27 | #endif 28 | #include 29 | 30 | #ifdef _WIN32 31 | # include 32 | #endif 33 | 34 | #if defined(__TURBOC__) || defined(_MSC_VER) || defined(_WIN32) 35 | # include 36 | #endif 37 | 38 | #ifdef WINAPI_FAMILY 39 | # define open _open 40 | # define read _read 41 | # define write _write 42 | # define close _close 43 | #endif 44 | 45 | #ifdef NO_DEFLATE /* for compatibility with old definition */ 46 | # define NO_GZCOMPRESS 47 | #endif 48 | 49 | #if defined(STDC99) || (defined(__TURBOC__) && __TURBOC__ >= 0x550) 50 | # ifndef HAVE_VSNPRINTF 51 | # define HAVE_VSNPRINTF 52 | # endif 53 | #endif 54 | 55 | #if defined(__CYGWIN__) 56 | # ifndef HAVE_VSNPRINTF 57 | # define HAVE_VSNPRINTF 58 | # endif 59 | #endif 60 | 61 | #if defined(MSDOS) && defined(__BORLANDC__) && (BORLANDC > 0x410) 62 | # ifndef HAVE_VSNPRINTF 63 | # define HAVE_VSNPRINTF 64 | # endif 65 | #endif 66 | 67 | #ifndef HAVE_VSNPRINTF 68 | # ifdef MSDOS 69 | /* vsnprintf may exist on some MS-DOS compilers (DJGPP?), 70 | but for now we just assume it doesn't. */ 71 | # define NO_vsnprintf 72 | # endif 73 | # ifdef __TURBOC__ 74 | # define NO_vsnprintf 75 | # endif 76 | # ifdef WIN32 77 | /* In Win32, vsnprintf is available as the "non-ANSI" _vsnprintf. */ 78 | # if !defined(vsnprintf) && !defined(NO_vsnprintf) 79 | # if !defined(_MSC_VER) || ( defined(_MSC_VER) && _MSC_VER < 1500 ) 80 | # define vsnprintf _vsnprintf 81 | # endif 82 | # endif 83 | # endif 84 | # ifdef __SASC 85 | # define NO_vsnprintf 86 | # endif 87 | # ifdef VMS 88 | # define NO_vsnprintf 89 | # endif 90 | # ifdef __OS400__ 91 | # define NO_vsnprintf 92 | # endif 93 | # ifdef __MVS__ 94 | # define NO_vsnprintf 95 | # endif 96 | #endif 97 | 98 | /* unlike snprintf (which is required in C99, yet still not supported by 99 | Microsoft more than a decade later!), _snprintf does not guarantee null 100 | termination of the result -- however this is only used in gzlib.c where 101 | the result is assured to fit in the space provided */ 102 | #ifdef _MSC_VER 103 | # define snprintf _snprintf 104 | #endif 105 | 106 | #ifndef local 107 | # define local static 108 | #endif 109 | /* compile with -Dlocal if your debugger can't find static symbols */ 110 | 111 | /* gz* functions always use library allocation functions */ 112 | #ifndef STDC 113 | extern voidp malloc OF((uInt size)); 114 | extern void free OF((voidpf ptr)); 115 | #endif 116 | 117 | /* get errno and strerror definition */ 118 | #if defined UNDER_CE 119 | # include 120 | # define zstrerror() gz_strwinerror((DWORD)GetLastError()) 121 | #else 122 | # ifndef NO_STRERROR 123 | # include 124 | # define zstrerror() strerror(errno) 125 | # else 126 | # define zstrerror() "stdio error (consult errno)" 127 | # endif 128 | #endif 129 | 130 | /* provide prototypes for these when building zlib without LFS */ 131 | #if !defined(_LARGEFILE64_SOURCE) || _LFS64_LARGEFILE-0 == 0 132 | ZEXTERN gzFile ZEXPORT gzopen64 OF((const char *, const char *)); 133 | ZEXTERN z_off64_t ZEXPORT gzseek64 OF((gzFile, z_off64_t, int)); 134 | ZEXTERN z_off64_t ZEXPORT gztell64 OF((gzFile)); 135 | ZEXTERN z_off64_t ZEXPORT gzoffset64 OF((gzFile)); 136 | #endif 137 | 138 | /* default memLevel */ 139 | #if MAX_MEM_LEVEL >= 8 140 | # define DEF_MEM_LEVEL 8 141 | #else 142 | # define DEF_MEM_LEVEL MAX_MEM_LEVEL 143 | #endif 144 | 145 | /* default i/o buffer size -- double this for output when reading (this and 146 | twice this must be able to fit in an unsigned type) */ 147 | #define GZBUFSIZE 8192 148 | 149 | /* gzip modes, also provide a little integrity check on the passed structure */ 150 | #define GZ_NONE 0 151 | #define GZ_READ 7247 152 | #define GZ_WRITE 31153 153 | #define GZ_APPEND 1 /* mode set to GZ_WRITE after the file is opened */ 154 | 155 | /* values for gz_state how */ 156 | #define LOOK 0 /* look for a gzip header */ 157 | #define COPY 1 /* copy input directly */ 158 | #define GZIP 2 /* decompress a gzip stream */ 159 | 160 | /* internal gzip file state data structure */ 161 | typedef struct { 162 | /* exposed contents for gzgetc() macro */ 163 | struct gzFile_s x; /* "x" for exposed */ 164 | /* x.have: number of bytes available at x.next */ 165 | /* x.next: next output data to deliver or write */ 166 | /* x.pos: current position in uncompressed data */ 167 | /* used for both reading and writing */ 168 | int mode; /* see gzip modes above */ 169 | int fd; /* file descriptor */ 170 | char *path; /* path or fd for error messages */ 171 | unsigned size; /* buffer size, zero if not allocated yet */ 172 | unsigned want; /* requested buffer size, default is GZBUFSIZE */ 173 | unsigned char *in; /* input buffer */ 174 | unsigned char *out; /* output buffer (double-sized when reading) */ 175 | int direct; /* 0 if processing gzip, 1 if transparent */ 176 | /* just for reading */ 177 | int how; /* 0: get header, 1: copy, 2: decompress */ 178 | z_off64_t start; /* where the gzip data started, for rewinding */ 179 | int eof; /* true if end of input file reached */ 180 | int past; /* true if read requested past end */ 181 | /* just for writing */ 182 | int level; /* compression level */ 183 | int strategy; /* compression strategy */ 184 | /* seek request */ 185 | z_off64_t skip; /* amount to skip (already rewound if backwards) */ 186 | int seek; /* true if seek request pending */ 187 | /* error information */ 188 | int err; /* error code */ 189 | char *msg; /* error message */ 190 | /* zlib inflate or deflate stream */ 191 | z_stream strm; /* stream structure in-place (not a pointer) */ 192 | } gz_state; 193 | typedef gz_state FAR *gz_statep; 194 | 195 | /* shared functions */ 196 | void ZLIB_INTERNAL gz_error OF((gz_statep, int, const char *)); 197 | #if defined UNDER_CE 198 | char ZLIB_INTERNAL *gz_strwinerror OF((DWORD error)); 199 | #endif 200 | 201 | /* GT_OFF(x), where x is an unsigned value, is true if x > maximum z_off64_t 202 | value -- needed when comparing unsigned to z_off64_t, which is signed 203 | (possible z_off64_t types off_t, off64_t, and long are all signed) */ 204 | #ifdef INT_MAX 205 | # define GT_OFF(x) (sizeof(int) == sizeof(z_off64_t) && (x) > INT_MAX) 206 | #else 207 | unsigned ZLIB_INTERNAL gz_intmax OF((void)); 208 | # define GT_OFF(x) (sizeof(int) == sizeof(z_off64_t) && (x) > gz_intmax()) 209 | #endif 210 | -------------------------------------------------------------------------------- /zlib/gzlib.c: -------------------------------------------------------------------------------- 1 | /* gzlib.c -- zlib functions common to reading and writing gzip files 2 | * Copyright (C) 2004, 2010, 2011, 2012, 2013 Mark Adler 3 | * For conditions of distribution and use, see copyright notice in zlib.h 4 | */ 5 | 6 | #include "gzguts.h" 7 | 8 | #if defined(_WIN32) && !defined(__BORLANDC__) 9 | # define LSEEK _lseeki64 10 | #else 11 | #if defined(_LARGEFILE64_SOURCE) && _LFS64_LARGEFILE-0 12 | # define LSEEK lseek64 13 | #else 14 | # define LSEEK lseek 15 | #endif 16 | #endif 17 | 18 | /* Local functions */ 19 | local void gz_reset OF((gz_statep)); 20 | local gzFile gz_open OF((const void *, int, const char *)); 21 | 22 | #if defined UNDER_CE 23 | 24 | /* Map the Windows error number in ERROR to a locale-dependent error message 25 | string and return a pointer to it. Typically, the values for ERROR come 26 | from GetLastError. 27 | 28 | The string pointed to shall not be modified by the application, but may be 29 | overwritten by a subsequent call to gz_strwinerror 30 | 31 | The gz_strwinerror function does not change the current setting of 32 | GetLastError. */ 33 | char ZLIB_INTERNAL *gz_strwinerror (error) 34 | DWORD error; 35 | { 36 | static char buf[1024]; 37 | 38 | wchar_t *msgbuf; 39 | DWORD lasterr = GetLastError(); 40 | DWORD chars = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM 41 | | FORMAT_MESSAGE_ALLOCATE_BUFFER, 42 | NULL, 43 | error, 44 | 0, /* Default language */ 45 | (LPVOID)&msgbuf, 46 | 0, 47 | NULL); 48 | if (chars != 0) { 49 | /* If there is an \r\n appended, zap it. */ 50 | if (chars >= 2 51 | && msgbuf[chars - 2] == '\r' && msgbuf[chars - 1] == '\n') { 52 | chars -= 2; 53 | msgbuf[chars] = 0; 54 | } 55 | 56 | if (chars > sizeof (buf) - 1) { 57 | chars = sizeof (buf) - 1; 58 | msgbuf[chars] = 0; 59 | } 60 | 61 | wcstombs(buf, msgbuf, chars + 1); 62 | LocalFree(msgbuf); 63 | } 64 | else { 65 | sprintf(buf, "unknown win32 error (%ld)", error); 66 | } 67 | 68 | SetLastError(lasterr); 69 | return buf; 70 | } 71 | 72 | #endif /* UNDER_CE */ 73 | 74 | /* Reset gzip file state */ 75 | local void gz_reset(state) 76 | gz_statep state; 77 | { 78 | state->x.have = 0; /* no output data available */ 79 | if (state->mode == GZ_READ) { /* for reading ... */ 80 | state->eof = 0; /* not at end of file */ 81 | state->past = 0; /* have not read past end yet */ 82 | state->how = LOOK; /* look for gzip header */ 83 | } 84 | state->seek = 0; /* no seek request pending */ 85 | gz_error(state, Z_OK, NULL); /* clear error */ 86 | state->x.pos = 0; /* no uncompressed data yet */ 87 | state->strm.avail_in = 0; /* no input data yet */ 88 | } 89 | 90 | /* Open a gzip file either by name or file descriptor. */ 91 | local gzFile gz_open(path, fd, mode) 92 | const void *path; 93 | int fd; 94 | const char *mode; 95 | { 96 | gz_statep state; 97 | size_t len; 98 | int oflag; 99 | #ifdef O_CLOEXEC 100 | int cloexec = 0; 101 | #endif 102 | #ifdef O_EXCL 103 | int exclusive = 0; 104 | #endif 105 | 106 | /* check input */ 107 | if (path == NULL) 108 | return NULL; 109 | 110 | /* allocate gzFile structure to return */ 111 | state = (gz_statep)malloc(sizeof(gz_state)); 112 | if (state == NULL) 113 | return NULL; 114 | state->size = 0; /* no buffers allocated yet */ 115 | state->want = GZBUFSIZE; /* requested buffer size */ 116 | state->msg = NULL; /* no error message yet */ 117 | 118 | /* interpret mode */ 119 | state->mode = GZ_NONE; 120 | state->level = Z_DEFAULT_COMPRESSION; 121 | state->strategy = Z_DEFAULT_STRATEGY; 122 | state->direct = 0; 123 | while (*mode) { 124 | if (*mode >= '0' && *mode <= '9') 125 | state->level = *mode - '0'; 126 | else 127 | switch (*mode) { 128 | case 'r': 129 | state->mode = GZ_READ; 130 | break; 131 | #ifndef NO_GZCOMPRESS 132 | case 'w': 133 | state->mode = GZ_WRITE; 134 | break; 135 | case 'a': 136 | state->mode = GZ_APPEND; 137 | break; 138 | #endif 139 | case '+': /* can't read and write at the same time */ 140 | free(state); 141 | return NULL; 142 | case 'b': /* ignore -- will request binary anyway */ 143 | break; 144 | #ifdef O_CLOEXEC 145 | case 'e': 146 | cloexec = 1; 147 | break; 148 | #endif 149 | #ifdef O_EXCL 150 | case 'x': 151 | exclusive = 1; 152 | break; 153 | #endif 154 | case 'f': 155 | state->strategy = Z_FILTERED; 156 | break; 157 | case 'h': 158 | state->strategy = Z_HUFFMAN_ONLY; 159 | break; 160 | case 'R': 161 | state->strategy = Z_RLE; 162 | break; 163 | case 'F': 164 | state->strategy = Z_FIXED; 165 | break; 166 | case 'T': 167 | state->direct = 1; 168 | break; 169 | default: /* could consider as an error, but just ignore */ 170 | ; 171 | } 172 | mode++; 173 | } 174 | 175 | /* must provide an "r", "w", or "a" */ 176 | if (state->mode == GZ_NONE) { 177 | free(state); 178 | return NULL; 179 | } 180 | 181 | /* can't force transparent read */ 182 | if (state->mode == GZ_READ) { 183 | if (state->direct) { 184 | free(state); 185 | return NULL; 186 | } 187 | state->direct = 1; /* for empty file */ 188 | } 189 | 190 | /* save the path name for error messages */ 191 | #ifdef _WIN32 192 | if (fd == -2) { 193 | len = wcstombs(NULL, path, 0); 194 | if (len == (size_t)-1) 195 | len = 0; 196 | } 197 | else 198 | #endif 199 | len = strlen((const char *)path); 200 | state->path = (char *)malloc(len + 1); 201 | if (state->path == NULL) { 202 | free(state); 203 | return NULL; 204 | } 205 | #ifdef _WIN32 206 | if (fd == -2) 207 | if (len) 208 | wcstombs(state->path, path, len + 1); 209 | else 210 | *(state->path) = 0; 211 | else 212 | #endif 213 | #if !defined(NO_snprintf) && !defined(NO_vsnprintf) 214 | snprintf(state->path, len + 1, "%s", (const char *)path); 215 | #else 216 | strcpy(state->path, path); 217 | #endif 218 | 219 | /* compute the flags for open() */ 220 | oflag = 221 | #ifdef O_LARGEFILE 222 | O_LARGEFILE | 223 | #endif 224 | #ifdef O_BINARY 225 | O_BINARY | 226 | #endif 227 | #ifdef O_CLOEXEC 228 | (cloexec ? O_CLOEXEC : 0) | 229 | #endif 230 | (state->mode == GZ_READ ? 231 | O_RDONLY : 232 | (O_WRONLY | O_CREAT | 233 | #ifdef O_EXCL 234 | (exclusive ? O_EXCL : 0) | 235 | #endif 236 | (state->mode == GZ_WRITE ? 237 | O_TRUNC : 238 | O_APPEND))); 239 | 240 | /* open the file with the appropriate flags (or just use fd) */ 241 | state->fd = fd > -1 ? fd : ( 242 | #ifdef _WIN32 243 | fd == -2 ? _wopen(path, oflag, 0666) : 244 | #endif 245 | open((const char *)path, oflag, 0666)); 246 | if (state->fd == -1) { 247 | free(state->path); 248 | free(state); 249 | return NULL; 250 | } 251 | if (state->mode == GZ_APPEND) 252 | state->mode = GZ_WRITE; /* simplify later checks */ 253 | 254 | /* save the current position for rewinding (only if reading) */ 255 | if (state->mode == GZ_READ) { 256 | state->start = LSEEK(state->fd, 0, SEEK_CUR); 257 | if (state->start == -1) state->start = 0; 258 | } 259 | 260 | /* initialize stream */ 261 | gz_reset(state); 262 | 263 | /* return stream */ 264 | return (gzFile)state; 265 | } 266 | 267 | /* -- see zlib.h -- */ 268 | gzFile ZEXPORT gzopen(path, mode) 269 | const char *path; 270 | const char *mode; 271 | { 272 | return gz_open(path, -1, mode); 273 | } 274 | 275 | /* -- see zlib.h -- */ 276 | gzFile ZEXPORT gzopen64(path, mode) 277 | const char *path; 278 | const char *mode; 279 | { 280 | return gz_open(path, -1, mode); 281 | } 282 | 283 | /* -- see zlib.h -- */ 284 | gzFile ZEXPORT gzdopen(fd, mode) 285 | int fd; 286 | const char *mode; 287 | { 288 | char *path; /* identifier for error messages */ 289 | gzFile gz; 290 | 291 | if (fd == -1 || (path = (char *)malloc(7 + 3 * sizeof(int))) == NULL) 292 | return NULL; 293 | #if !defined(NO_snprintf) && !defined(NO_vsnprintf) 294 | snprintf(path, 7 + 3 * sizeof(int), "", fd); /* for debugging */ 295 | #else 296 | sprintf(path, "", fd); /* for debugging */ 297 | #endif 298 | gz = gz_open(path, fd, mode); 299 | free(path); 300 | return gz; 301 | } 302 | 303 | /* -- see zlib.h -- */ 304 | #ifdef _WIN32 305 | gzFile ZEXPORT gzopen_w(path, mode) 306 | const wchar_t *path; 307 | const char *mode; 308 | { 309 | return gz_open(path, -2, mode); 310 | } 311 | #endif 312 | 313 | /* -- see zlib.h -- */ 314 | int ZEXPORT gzbuffer(file, size) 315 | gzFile file; 316 | unsigned size; 317 | { 318 | gz_statep state; 319 | 320 | /* get internal structure and check integrity */ 321 | if (file == NULL) 322 | return -1; 323 | state = (gz_statep)file; 324 | if (state->mode != GZ_READ && state->mode != GZ_WRITE) 325 | return -1; 326 | 327 | /* make sure we haven't already allocated memory */ 328 | if (state->size != 0) 329 | return -1; 330 | 331 | /* check and set requested size */ 332 | if (size < 2) 333 | size = 2; /* need two bytes to check magic header */ 334 | state->want = size; 335 | return 0; 336 | } 337 | 338 | /* -- see zlib.h -- */ 339 | int ZEXPORT gzrewind(file) 340 | gzFile file; 341 | { 342 | gz_statep state; 343 | 344 | /* get internal structure */ 345 | if (file == NULL) 346 | return -1; 347 | state = (gz_statep)file; 348 | 349 | /* check that we're reading and that there's no error */ 350 | if (state->mode != GZ_READ || 351 | (state->err != Z_OK && state->err != Z_BUF_ERROR)) 352 | return -1; 353 | 354 | /* back up and start over */ 355 | if (LSEEK(state->fd, state->start, SEEK_SET) == -1) 356 | return -1; 357 | gz_reset(state); 358 | return 0; 359 | } 360 | 361 | /* -- see zlib.h -- */ 362 | z_off64_t ZEXPORT gzseek64(file, offset, whence) 363 | gzFile file; 364 | z_off64_t offset; 365 | int whence; 366 | { 367 | unsigned n; 368 | z_off64_t ret; 369 | gz_statep state; 370 | 371 | /* get internal structure and check integrity */ 372 | if (file == NULL) 373 | return -1; 374 | state = (gz_statep)file; 375 | if (state->mode != GZ_READ && state->mode != GZ_WRITE) 376 | return -1; 377 | 378 | /* check that there's no error */ 379 | if (state->err != Z_OK && state->err != Z_BUF_ERROR) 380 | return -1; 381 | 382 | /* can only seek from start or relative to current position */ 383 | if (whence != SEEK_SET && whence != SEEK_CUR) 384 | return -1; 385 | 386 | /* normalize offset to a SEEK_CUR specification */ 387 | if (whence == SEEK_SET) 388 | offset -= state->x.pos; 389 | else if (state->seek) 390 | offset += state->skip; 391 | state->seek = 0; 392 | 393 | /* if within raw area while reading, just go there */ 394 | if (state->mode == GZ_READ && state->how == COPY && 395 | state->x.pos + offset >= 0) { 396 | ret = LSEEK(state->fd, offset - state->x.have, SEEK_CUR); 397 | if (ret == -1) 398 | return -1; 399 | state->x.have = 0; 400 | state->eof = 0; 401 | state->past = 0; 402 | state->seek = 0; 403 | gz_error(state, Z_OK, NULL); 404 | state->strm.avail_in = 0; 405 | state->x.pos += offset; 406 | return state->x.pos; 407 | } 408 | 409 | /* calculate skip amount, rewinding if needed for back seek when reading */ 410 | if (offset < 0) { 411 | if (state->mode != GZ_READ) /* writing -- can't go backwards */ 412 | return -1; 413 | offset += state->x.pos; 414 | if (offset < 0) /* before start of file! */ 415 | return -1; 416 | if (gzrewind(file) == -1) /* rewind, then skip to offset */ 417 | return -1; 418 | } 419 | 420 | /* if reading, skip what's in output buffer (one less gzgetc() check) */ 421 | if (state->mode == GZ_READ) { 422 | n = GT_OFF(state->x.have) || (z_off64_t)state->x.have > offset ? 423 | (unsigned)offset : state->x.have; 424 | state->x.have -= n; 425 | state->x.next += n; 426 | state->x.pos += n; 427 | offset -= n; 428 | } 429 | 430 | /* request skip (if not zero) */ 431 | if (offset) { 432 | state->seek = 1; 433 | state->skip = offset; 434 | } 435 | return state->x.pos + offset; 436 | } 437 | 438 | /* -- see zlib.h -- */ 439 | z_off_t ZEXPORT gzseek(file, offset, whence) 440 | gzFile file; 441 | z_off_t offset; 442 | int whence; 443 | { 444 | z_off64_t ret; 445 | 446 | ret = gzseek64(file, (z_off64_t)offset, whence); 447 | return ret == (z_off_t)ret ? (z_off_t)ret : -1; 448 | } 449 | 450 | /* -- see zlib.h -- */ 451 | z_off64_t ZEXPORT gztell64(file) 452 | gzFile file; 453 | { 454 | gz_statep state; 455 | 456 | /* get internal structure and check integrity */ 457 | if (file == NULL) 458 | return -1; 459 | state = (gz_statep)file; 460 | if (state->mode != GZ_READ && state->mode != GZ_WRITE) 461 | return -1; 462 | 463 | /* return position */ 464 | return state->x.pos + (state->seek ? state->skip : 0); 465 | } 466 | 467 | /* -- see zlib.h -- */ 468 | z_off_t ZEXPORT gztell(file) 469 | gzFile file; 470 | { 471 | z_off64_t ret; 472 | 473 | ret = gztell64(file); 474 | return ret == (z_off_t)ret ? (z_off_t)ret : -1; 475 | } 476 | 477 | /* -- see zlib.h -- */ 478 | z_off64_t ZEXPORT gzoffset64(file) 479 | gzFile file; 480 | { 481 | z_off64_t offset; 482 | gz_statep state; 483 | 484 | /* get internal structure and check integrity */ 485 | if (file == NULL) 486 | return -1; 487 | state = (gz_statep)file; 488 | if (state->mode != GZ_READ && state->mode != GZ_WRITE) 489 | return -1; 490 | 491 | /* compute and return effective offset in file */ 492 | offset = LSEEK(state->fd, 0, SEEK_CUR); 493 | if (offset == -1) 494 | return -1; 495 | if (state->mode == GZ_READ) /* reading */ 496 | offset -= state->strm.avail_in; /* don't count buffered input */ 497 | return offset; 498 | } 499 | 500 | /* -- see zlib.h -- */ 501 | z_off_t ZEXPORT gzoffset(file) 502 | gzFile file; 503 | { 504 | z_off64_t ret; 505 | 506 | ret = gzoffset64(file); 507 | return ret == (z_off_t)ret ? (z_off_t)ret : -1; 508 | } 509 | 510 | /* -- see zlib.h -- */ 511 | int ZEXPORT gzeof(file) 512 | gzFile file; 513 | { 514 | gz_statep state; 515 | 516 | /* get internal structure and check integrity */ 517 | if (file == NULL) 518 | return 0; 519 | state = (gz_statep)file; 520 | if (state->mode != GZ_READ && state->mode != GZ_WRITE) 521 | return 0; 522 | 523 | /* return end-of-file state */ 524 | return state->mode == GZ_READ ? state->past : 0; 525 | } 526 | 527 | /* -- see zlib.h -- */ 528 | const char * ZEXPORT gzerror(file, errnum) 529 | gzFile file; 530 | int *errnum; 531 | { 532 | gz_statep state; 533 | 534 | /* get internal structure and check integrity */ 535 | if (file == NULL) 536 | return NULL; 537 | state = (gz_statep)file; 538 | if (state->mode != GZ_READ && state->mode != GZ_WRITE) 539 | return NULL; 540 | 541 | /* return error information */ 542 | if (errnum != NULL) 543 | *errnum = state->err; 544 | return state->err == Z_MEM_ERROR ? "out of memory" : 545 | (state->msg == NULL ? "" : state->msg); 546 | } 547 | 548 | /* -- see zlib.h -- */ 549 | void ZEXPORT gzclearerr(file) 550 | gzFile file; 551 | { 552 | gz_statep state; 553 | 554 | /* get internal structure and check integrity */ 555 | if (file == NULL) 556 | return; 557 | state = (gz_statep)file; 558 | if (state->mode != GZ_READ && state->mode != GZ_WRITE) 559 | return; 560 | 561 | /* clear error and end-of-file */ 562 | if (state->mode == GZ_READ) { 563 | state->eof = 0; 564 | state->past = 0; 565 | } 566 | gz_error(state, Z_OK, NULL); 567 | } 568 | 569 | /* Create an error message in allocated memory and set state->err and 570 | state->msg accordingly. Free any previous error message already there. Do 571 | not try to free or allocate space if the error is Z_MEM_ERROR (out of 572 | memory). Simply save the error message as a static string. If there is an 573 | allocation failure constructing the error message, then convert the error to 574 | out of memory. */ 575 | void ZLIB_INTERNAL gz_error(state, err, msg) 576 | gz_statep state; 577 | int err; 578 | const char *msg; 579 | { 580 | /* free previously allocated message and clear */ 581 | if (state->msg != NULL) { 582 | if (state->err != Z_MEM_ERROR) 583 | free(state->msg); 584 | state->msg = NULL; 585 | } 586 | 587 | /* if fatal, set state->x.have to 0 so that the gzgetc() macro fails */ 588 | if (err != Z_OK && err != Z_BUF_ERROR) 589 | state->x.have = 0; 590 | 591 | /* set error code, and if no message, then done */ 592 | state->err = err; 593 | if (msg == NULL) 594 | return; 595 | 596 | /* for an out of memory error, return literal string when requested */ 597 | if (err == Z_MEM_ERROR) 598 | return; 599 | 600 | /* construct error message with path */ 601 | if ((state->msg = (char *)malloc(strlen(state->path) + strlen(msg) + 3)) == 602 | NULL) { 603 | state->err = Z_MEM_ERROR; 604 | return; 605 | } 606 | #if !defined(NO_snprintf) && !defined(NO_vsnprintf) 607 | snprintf(state->msg, strlen(state->path) + strlen(msg) + 3, 608 | "%s%s%s", state->path, ": ", msg); 609 | #else 610 | strcpy(state->msg, state->path); 611 | strcat(state->msg, ": "); 612 | strcat(state->msg, msg); 613 | #endif 614 | return; 615 | } 616 | 617 | #ifndef INT_MAX 618 | /* portably return maximum value for an int (when limits.h presumed not 619 | available) -- we need to do this to cover cases where 2's complement not 620 | used, since C standard permits 1's complement and sign-bit representations, 621 | otherwise we could just use ((unsigned)-1) >> 1 */ 622 | unsigned ZLIB_INTERNAL gz_intmax() 623 | { 624 | unsigned p, q; 625 | 626 | p = 1; 627 | do { 628 | q = p; 629 | p <<= 1; 630 | p++; 631 | } while (p > q); 632 | return q >> 1; 633 | } 634 | #endif 635 | -------------------------------------------------------------------------------- /zlib/gzwrite.c: -------------------------------------------------------------------------------- 1 | /* gzwrite.c -- zlib functions for writing gzip files 2 | * Copyright (C) 2004, 2005, 2010, 2011, 2012, 2013 Mark Adler 3 | * For conditions of distribution and use, see copyright notice in zlib.h 4 | */ 5 | 6 | #include "gzguts.h" 7 | 8 | /* Local functions */ 9 | local int gz_init OF((gz_statep)); 10 | local int gz_comp OF((gz_statep, int)); 11 | local int gz_zero OF((gz_statep, z_off64_t)); 12 | 13 | /* Initialize state for writing a gzip file. Mark initialization by setting 14 | state->size to non-zero. Return -1 on failure or 0 on success. */ 15 | local int gz_init(state) 16 | gz_statep state; 17 | { 18 | int ret; 19 | z_streamp strm = &(state->strm); 20 | 21 | /* allocate input buffer */ 22 | state->in = (unsigned char *)malloc(state->want); 23 | if (state->in == NULL) { 24 | gz_error(state, Z_MEM_ERROR, "out of memory"); 25 | return -1; 26 | } 27 | 28 | /* only need output buffer and deflate state if compressing */ 29 | if (!state->direct) { 30 | /* allocate output buffer */ 31 | state->out = (unsigned char *)malloc(state->want); 32 | if (state->out == NULL) { 33 | free(state->in); 34 | gz_error(state, Z_MEM_ERROR, "out of memory"); 35 | return -1; 36 | } 37 | 38 | /* allocate deflate memory, set up for gzip compression */ 39 | strm->zalloc = Z_NULL; 40 | strm->zfree = Z_NULL; 41 | strm->opaque = Z_NULL; 42 | ret = deflateInit2(strm, state->level, Z_DEFLATED, 43 | MAX_WBITS + 16, DEF_MEM_LEVEL, state->strategy); 44 | if (ret != Z_OK) { 45 | free(state->out); 46 | free(state->in); 47 | gz_error(state, Z_MEM_ERROR, "out of memory"); 48 | return -1; 49 | } 50 | } 51 | 52 | /* mark state as initialized */ 53 | state->size = state->want; 54 | 55 | /* initialize write buffer if compressing */ 56 | if (!state->direct) { 57 | strm->avail_out = state->size; 58 | strm->next_out = state->out; 59 | state->x.next = strm->next_out; 60 | } 61 | return 0; 62 | } 63 | 64 | /* Compress whatever is at avail_in and next_in and write to the output file. 65 | Return -1 if there is an error writing to the output file, otherwise 0. 66 | flush is assumed to be a valid deflate() flush value. If flush is Z_FINISH, 67 | then the deflate() state is reset to start a new gzip stream. If gz->direct 68 | is true, then simply write to the output file without compressing, and 69 | ignore flush. */ 70 | local int gz_comp(state, flush) 71 | gz_statep state; 72 | int flush; 73 | { 74 | int ret, got; 75 | unsigned have; 76 | z_streamp strm = &(state->strm); 77 | 78 | /* allocate memory if this is the first time through */ 79 | if (state->size == 0 && gz_init(state) == -1) 80 | return -1; 81 | 82 | /* write directly if requested */ 83 | if (state->direct) { 84 | got = write(state->fd, strm->next_in, strm->avail_in); 85 | if (got < 0 || (unsigned)got != strm->avail_in) { 86 | gz_error(state, Z_ERRNO, zstrerror()); 87 | return -1; 88 | } 89 | strm->avail_in = 0; 90 | return 0; 91 | } 92 | 93 | /* run deflate() on provided input until it produces no more output */ 94 | ret = Z_OK; 95 | do { 96 | /* write out current buffer contents if full, or if flushing, but if 97 | doing Z_FINISH then don't write until we get to Z_STREAM_END */ 98 | if (strm->avail_out == 0 || (flush != Z_NO_FLUSH && 99 | (flush != Z_FINISH || ret == Z_STREAM_END))) { 100 | have = (unsigned)(strm->next_out - state->x.next); 101 | if (have && ((got = write(state->fd, state->x.next, have)) < 0 || 102 | (unsigned)got != have)) { 103 | gz_error(state, Z_ERRNO, zstrerror()); 104 | return -1; 105 | } 106 | if (strm->avail_out == 0) { 107 | strm->avail_out = state->size; 108 | strm->next_out = state->out; 109 | } 110 | state->x.next = strm->next_out; 111 | } 112 | 113 | /* compress */ 114 | have = strm->avail_out; 115 | ret = deflate(strm, flush); 116 | if (ret == Z_STREAM_ERROR) { 117 | gz_error(state, Z_STREAM_ERROR, 118 | "internal error: deflate stream corrupt"); 119 | return -1; 120 | } 121 | have -= strm->avail_out; 122 | } while (have); 123 | 124 | /* if that completed a deflate stream, allow another to start */ 125 | if (flush == Z_FINISH) 126 | deflateReset(strm); 127 | 128 | /* all done, no errors */ 129 | return 0; 130 | } 131 | 132 | /* Compress len zeros to output. Return -1 on error, 0 on success. */ 133 | local int gz_zero(state, len) 134 | gz_statep state; 135 | z_off64_t len; 136 | { 137 | int first; 138 | unsigned n; 139 | z_streamp strm = &(state->strm); 140 | 141 | /* consume whatever's left in the input buffer */ 142 | if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1) 143 | return -1; 144 | 145 | /* compress len zeros (len guaranteed > 0) */ 146 | first = 1; 147 | while (len) { 148 | n = GT_OFF(state->size) || (z_off64_t)state->size > len ? 149 | (unsigned)len : state->size; 150 | if (first) { 151 | memset(state->in, 0, n); 152 | first = 0; 153 | } 154 | strm->avail_in = n; 155 | strm->next_in = state->in; 156 | state->x.pos += n; 157 | if (gz_comp(state, Z_NO_FLUSH) == -1) 158 | return -1; 159 | len -= n; 160 | } 161 | return 0; 162 | } 163 | 164 | /* -- see zlib.h -- */ 165 | int ZEXPORT gzwrite(file, buf, len) 166 | gzFile file; 167 | voidpc buf; 168 | unsigned len; 169 | { 170 | unsigned put = len; 171 | gz_statep state; 172 | z_streamp strm; 173 | 174 | /* get internal structure */ 175 | if (file == NULL) 176 | return 0; 177 | state = (gz_statep)file; 178 | strm = &(state->strm); 179 | 180 | /* check that we're writing and that there's no error */ 181 | if (state->mode != GZ_WRITE || state->err != Z_OK) 182 | return 0; 183 | 184 | /* since an int is returned, make sure len fits in one, otherwise return 185 | with an error (this avoids the flaw in the interface) */ 186 | if ((int)len < 0) { 187 | gz_error(state, Z_DATA_ERROR, "requested length does not fit in int"); 188 | return 0; 189 | } 190 | 191 | /* if len is zero, avoid unnecessary operations */ 192 | if (len == 0) 193 | return 0; 194 | 195 | /* allocate memory if this is the first time through */ 196 | if (state->size == 0 && gz_init(state) == -1) 197 | return 0; 198 | 199 | /* check for seek request */ 200 | if (state->seek) { 201 | state->seek = 0; 202 | if (gz_zero(state, state->skip) == -1) 203 | return 0; 204 | } 205 | 206 | /* for small len, copy to input buffer, otherwise compress directly */ 207 | if (len < state->size) { 208 | /* copy to input buffer, compress when full */ 209 | do { 210 | unsigned have, copy; 211 | 212 | if (strm->avail_in == 0) 213 | strm->next_in = state->in; 214 | have = (unsigned)((strm->next_in + strm->avail_in) - state->in); 215 | copy = state->size - have; 216 | if (copy > len) 217 | copy = len; 218 | memcpy(state->in + have, buf, copy); 219 | strm->avail_in += copy; 220 | state->x.pos += copy; 221 | buf = (const char *)buf + copy; 222 | len -= copy; 223 | if (len && gz_comp(state, Z_NO_FLUSH) == -1) 224 | return 0; 225 | } while (len); 226 | } 227 | else { 228 | /* consume whatever's left in the input buffer */ 229 | if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1) 230 | return 0; 231 | 232 | /* directly compress user buffer to file */ 233 | strm->avail_in = len; 234 | strm->next_in = (z_const Bytef *)buf; 235 | state->x.pos += len; 236 | if (gz_comp(state, Z_NO_FLUSH) == -1) 237 | return 0; 238 | } 239 | 240 | /* input was all buffered or compressed (put will fit in int) */ 241 | return (int)put; 242 | } 243 | 244 | /* -- see zlib.h -- */ 245 | int ZEXPORT gzputc(file, c) 246 | gzFile file; 247 | int c; 248 | { 249 | unsigned have; 250 | unsigned char buf[1]; 251 | gz_statep state; 252 | z_streamp strm; 253 | 254 | /* get internal structure */ 255 | if (file == NULL) 256 | return -1; 257 | state = (gz_statep)file; 258 | strm = &(state->strm); 259 | 260 | /* check that we're writing and that there's no error */ 261 | if (state->mode != GZ_WRITE || state->err != Z_OK) 262 | return -1; 263 | 264 | /* check for seek request */ 265 | if (state->seek) { 266 | state->seek = 0; 267 | if (gz_zero(state, state->skip) == -1) 268 | return -1; 269 | } 270 | 271 | /* try writing to input buffer for speed (state->size == 0 if buffer not 272 | initialized) */ 273 | if (state->size) { 274 | if (strm->avail_in == 0) 275 | strm->next_in = state->in; 276 | have = (unsigned)((strm->next_in + strm->avail_in) - state->in); 277 | if (have < state->size) { 278 | state->in[have] = c; 279 | strm->avail_in++; 280 | state->x.pos++; 281 | return c & 0xff; 282 | } 283 | } 284 | 285 | /* no room in buffer or not initialized, use gz_write() */ 286 | buf[0] = c; 287 | if (gzwrite(file, buf, 1) != 1) 288 | return -1; 289 | return c & 0xff; 290 | } 291 | 292 | /* -- see zlib.h -- */ 293 | int ZEXPORT gzputs(file, str) 294 | gzFile file; 295 | const char *str; 296 | { 297 | int ret; 298 | unsigned len; 299 | 300 | /* write string */ 301 | len = (unsigned)strlen(str); 302 | ret = gzwrite(file, str, len); 303 | return ret == 0 && len != 0 ? -1 : ret; 304 | } 305 | 306 | #if defined(STDC) || defined(Z_HAVE_STDARG_H) 307 | #include 308 | 309 | /* -- see zlib.h -- */ 310 | int ZEXPORTVA gzvprintf(gzFile file, const char *format, va_list va) 311 | { 312 | int size, len; 313 | gz_statep state; 314 | z_streamp strm; 315 | 316 | /* get internal structure */ 317 | if (file == NULL) 318 | return -1; 319 | state = (gz_statep)file; 320 | strm = &(state->strm); 321 | 322 | /* check that we're writing and that there's no error */ 323 | if (state->mode != GZ_WRITE || state->err != Z_OK) 324 | return 0; 325 | 326 | /* make sure we have some buffer space */ 327 | if (state->size == 0 && gz_init(state) == -1) 328 | return 0; 329 | 330 | /* check for seek request */ 331 | if (state->seek) { 332 | state->seek = 0; 333 | if (gz_zero(state, state->skip) == -1) 334 | return 0; 335 | } 336 | 337 | /* consume whatever's left in the input buffer */ 338 | if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1) 339 | return 0; 340 | 341 | /* do the printf() into the input buffer, put length in len */ 342 | size = (int)(state->size); 343 | state->in[size - 1] = 0; 344 | #ifdef NO_vsnprintf 345 | # ifdef HAS_vsprintf_void 346 | (void)vsprintf((char *)(state->in), format, va); 347 | for (len = 0; len < size; len++) 348 | if (state->in[len] == 0) break; 349 | # else 350 | len = vsprintf((char *)(state->in), format, va); 351 | # endif 352 | #else 353 | # ifdef HAS_vsnprintf_void 354 | (void)vsnprintf((char *)(state->in), size, format, va); 355 | len = strlen((char *)(state->in)); 356 | # else 357 | len = vsnprintf((char *)(state->in), size, format, va); 358 | # endif 359 | #endif 360 | 361 | /* check that printf() results fit in buffer */ 362 | if (len <= 0 || len >= (int)size || state->in[size - 1] != 0) 363 | return 0; 364 | 365 | /* update buffer and position, defer compression until needed */ 366 | strm->avail_in = (unsigned)len; 367 | strm->next_in = state->in; 368 | state->x.pos += len; 369 | return len; 370 | } 371 | 372 | int ZEXPORTVA gzprintf(gzFile file, const char *format, ...) 373 | { 374 | va_list va; 375 | int ret; 376 | 377 | va_start(va, format); 378 | ret = gzvprintf(file, format, va); 379 | va_end(va); 380 | return ret; 381 | } 382 | 383 | #else /* !STDC && !Z_HAVE_STDARG_H */ 384 | 385 | /* -- see zlib.h -- */ 386 | int ZEXPORTVA gzprintf (file, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, 387 | a11, a12, a13, a14, a15, a16, a17, a18, a19, a20) 388 | gzFile file; 389 | const char *format; 390 | int a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, 391 | a11, a12, a13, a14, a15, a16, a17, a18, a19, a20; 392 | { 393 | int size, len; 394 | gz_statep state; 395 | z_streamp strm; 396 | 397 | /* get internal structure */ 398 | if (file == NULL) 399 | return -1; 400 | state = (gz_statep)file; 401 | strm = &(state->strm); 402 | 403 | /* check that can really pass pointer in ints */ 404 | if (sizeof(int) != sizeof(void *)) 405 | return 0; 406 | 407 | /* check that we're writing and that there's no error */ 408 | if (state->mode != GZ_WRITE || state->err != Z_OK) 409 | return 0; 410 | 411 | /* make sure we have some buffer space */ 412 | if (state->size == 0 && gz_init(state) == -1) 413 | return 0; 414 | 415 | /* check for seek request */ 416 | if (state->seek) { 417 | state->seek = 0; 418 | if (gz_zero(state, state->skip) == -1) 419 | return 0; 420 | } 421 | 422 | /* consume whatever's left in the input buffer */ 423 | if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1) 424 | return 0; 425 | 426 | /* do the printf() into the input buffer, put length in len */ 427 | size = (int)(state->size); 428 | state->in[size - 1] = 0; 429 | #ifdef NO_snprintf 430 | # ifdef HAS_sprintf_void 431 | sprintf((char *)(state->in), format, a1, a2, a3, a4, a5, a6, a7, a8, 432 | a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20); 433 | for (len = 0; len < size; len++) 434 | if (state->in[len] == 0) break; 435 | # else 436 | len = sprintf((char *)(state->in), format, a1, a2, a3, a4, a5, a6, a7, a8, 437 | a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20); 438 | # endif 439 | #else 440 | # ifdef HAS_snprintf_void 441 | snprintf((char *)(state->in), size, format, a1, a2, a3, a4, a5, a6, a7, a8, 442 | a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20); 443 | len = strlen((char *)(state->in)); 444 | # else 445 | len = snprintf((char *)(state->in), size, format, a1, a2, a3, a4, a5, a6, 446 | a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, 447 | a19, a20); 448 | # endif 449 | #endif 450 | 451 | /* check that printf() results fit in buffer */ 452 | if (len <= 0 || len >= (int)size || state->in[size - 1] != 0) 453 | return 0; 454 | 455 | /* update buffer and position, defer compression until needed */ 456 | strm->avail_in = (unsigned)len; 457 | strm->next_in = state->in; 458 | state->x.pos += len; 459 | return len; 460 | } 461 | 462 | #endif 463 | 464 | /* -- see zlib.h -- */ 465 | int ZEXPORT gzflush(file, flush) 466 | gzFile file; 467 | int flush; 468 | { 469 | gz_statep state; 470 | 471 | /* get internal structure */ 472 | if (file == NULL) 473 | return -1; 474 | state = (gz_statep)file; 475 | 476 | /* check that we're writing and that there's no error */ 477 | if (state->mode != GZ_WRITE || state->err != Z_OK) 478 | return Z_STREAM_ERROR; 479 | 480 | /* check flush parameter */ 481 | if (flush < 0 || flush > Z_FINISH) 482 | return Z_STREAM_ERROR; 483 | 484 | /* check for seek request */ 485 | if (state->seek) { 486 | state->seek = 0; 487 | if (gz_zero(state, state->skip) == -1) 488 | return -1; 489 | } 490 | 491 | /* compress remaining data with requested flush */ 492 | gz_comp(state, flush); 493 | return state->err; 494 | } 495 | 496 | /* -- see zlib.h -- */ 497 | int ZEXPORT gzsetparams(file, level, strategy) 498 | gzFile file; 499 | int level; 500 | int strategy; 501 | { 502 | gz_statep state; 503 | z_streamp strm; 504 | 505 | /* get internal structure */ 506 | if (file == NULL) 507 | return Z_STREAM_ERROR; 508 | state = (gz_statep)file; 509 | strm = &(state->strm); 510 | 511 | /* check that we're writing and that there's no error */ 512 | if (state->mode != GZ_WRITE || state->err != Z_OK) 513 | return Z_STREAM_ERROR; 514 | 515 | /* if no change is requested, then do nothing */ 516 | if (level == state->level && strategy == state->strategy) 517 | return Z_OK; 518 | 519 | /* check for seek request */ 520 | if (state->seek) { 521 | state->seek = 0; 522 | if (gz_zero(state, state->skip) == -1) 523 | return -1; 524 | } 525 | 526 | /* change compression parameters for subsequent input */ 527 | if (state->size) { 528 | /* flush previous input with previous parameters before changing */ 529 | if (strm->avail_in && gz_comp(state, Z_PARTIAL_FLUSH) == -1) 530 | return state->err; 531 | deflateParams(strm, level, strategy); 532 | } 533 | state->level = level; 534 | state->strategy = strategy; 535 | return Z_OK; 536 | } 537 | 538 | /* -- see zlib.h -- */ 539 | int ZEXPORT gzclose_w(file) 540 | gzFile file; 541 | { 542 | int ret = Z_OK; 543 | gz_statep state; 544 | 545 | /* get internal structure */ 546 | if (file == NULL) 547 | return Z_STREAM_ERROR; 548 | state = (gz_statep)file; 549 | 550 | /* check that we're writing */ 551 | if (state->mode != GZ_WRITE) 552 | return Z_STREAM_ERROR; 553 | 554 | /* check for seek request */ 555 | if (state->seek) { 556 | state->seek = 0; 557 | if (gz_zero(state, state->skip) == -1) 558 | ret = state->err; 559 | } 560 | 561 | /* flush, free memory, and close file */ 562 | if (gz_comp(state, Z_FINISH) == -1) 563 | ret = state->err; 564 | if (state->size) { 565 | if (!state->direct) { 566 | (void)deflateEnd(&(state->strm)); 567 | free(state->out); 568 | } 569 | free(state->in); 570 | } 571 | gz_error(state, Z_OK, NULL); 572 | free(state->path); 573 | if (close(state->fd) == -1) 574 | ret = Z_ERRNO; 575 | free(state); 576 | return ret; 577 | } 578 | -------------------------------------------------------------------------------- /zlib/inffast.c: -------------------------------------------------------------------------------- 1 | /* inffast.c -- fast decoding 2 | * Copyright (C) 1995-2008, 2010, 2013 Mark Adler 3 | * For conditions of distribution and use, see copyright notice in zlib.h 4 | */ 5 | 6 | #include "zutil.h" 7 | #include "inftrees.h" 8 | #include "inflate.h" 9 | #include "inffast.h" 10 | 11 | #ifndef ASMINF 12 | 13 | /* Allow machine dependent optimization for post-increment or pre-increment. 14 | Based on testing to date, 15 | Pre-increment preferred for: 16 | - PowerPC G3 (Adler) 17 | - MIPS R5000 (Randers-Pehrson) 18 | Post-increment preferred for: 19 | - none 20 | No measurable difference: 21 | - Pentium III (Anderson) 22 | - M68060 (Nikl) 23 | */ 24 | #ifdef POSTINC 25 | # define OFF 0 26 | # define PUP(a) *(a)++ 27 | #else 28 | # define OFF 1 29 | # define PUP(a) *++(a) 30 | #endif 31 | 32 | /* 33 | Decode literal, length, and distance codes and write out the resulting 34 | literal and match bytes until either not enough input or output is 35 | available, an end-of-block is encountered, or a data error is encountered. 36 | When large enough input and output buffers are supplied to inflate(), for 37 | example, a 16K input buffer and a 64K output buffer, more than 95% of the 38 | inflate execution time is spent in this routine. 39 | 40 | Entry assumptions: 41 | 42 | state->mode == LEN 43 | strm->avail_in >= 6 44 | strm->avail_out >= 258 45 | start >= strm->avail_out 46 | state->bits < 8 47 | 48 | On return, state->mode is one of: 49 | 50 | LEN -- ran out of enough output space or enough available input 51 | TYPE -- reached end of block code, inflate() to interpret next block 52 | BAD -- error in block data 53 | 54 | Notes: 55 | 56 | - The maximum input bits used by a length/distance pair is 15 bits for the 57 | length code, 5 bits for the length extra, 15 bits for the distance code, 58 | and 13 bits for the distance extra. This totals 48 bits, or six bytes. 59 | Therefore if strm->avail_in >= 6, then there is enough input to avoid 60 | checking for available input while decoding. 61 | 62 | - The maximum bytes that a single length/distance pair can output is 258 63 | bytes, which is the maximum length that can be coded. inflate_fast() 64 | requires strm->avail_out >= 258 for each loop to avoid checking for 65 | output space. 66 | */ 67 | void ZLIB_INTERNAL inflate_fast(strm, start) 68 | z_streamp strm; 69 | unsigned start; /* inflate()'s starting value for strm->avail_out */ 70 | { 71 | struct inflate_state FAR *state; 72 | z_const unsigned char FAR *in; /* local strm->next_in */ 73 | z_const unsigned char FAR *last; /* have enough input while in < last */ 74 | unsigned char FAR *out; /* local strm->next_out */ 75 | unsigned char FAR *beg; /* inflate()'s initial strm->next_out */ 76 | unsigned char FAR *end; /* while out < end, enough space available */ 77 | #ifdef INFLATE_STRICT 78 | unsigned dmax; /* maximum distance from zlib header */ 79 | #endif 80 | unsigned wsize; /* window size or zero if not using window */ 81 | unsigned whave; /* valid bytes in the window */ 82 | unsigned wnext; /* window write index */ 83 | unsigned char FAR *window; /* allocated sliding window, if wsize != 0 */ 84 | unsigned long hold; /* local strm->hold */ 85 | unsigned bits; /* local strm->bits */ 86 | code const FAR *lcode; /* local strm->lencode */ 87 | code const FAR *dcode; /* local strm->distcode */ 88 | unsigned lmask; /* mask for first level of length codes */ 89 | unsigned dmask; /* mask for first level of distance codes */ 90 | code here; /* retrieved table entry */ 91 | unsigned op; /* code bits, operation, extra bits, or */ 92 | /* window position, window bytes to copy */ 93 | unsigned len; /* match length, unused bytes */ 94 | unsigned dist; /* match distance */ 95 | unsigned char FAR *from; /* where to copy match from */ 96 | 97 | /* copy state to local variables */ 98 | state = (struct inflate_state FAR *)strm->state; 99 | in = strm->next_in - OFF; 100 | last = in + (strm->avail_in - 5); 101 | out = strm->next_out - OFF; 102 | beg = out - (start - strm->avail_out); 103 | end = out + (strm->avail_out - 257); 104 | #ifdef INFLATE_STRICT 105 | dmax = state->dmax; 106 | #endif 107 | wsize = state->wsize; 108 | whave = state->whave; 109 | wnext = state->wnext; 110 | window = state->window; 111 | hold = state->hold; 112 | bits = state->bits; 113 | lcode = state->lencode; 114 | dcode = state->distcode; 115 | lmask = (1U << state->lenbits) - 1; 116 | dmask = (1U << state->distbits) - 1; 117 | 118 | /* decode literals and length/distances until end-of-block or not enough 119 | input data or output space */ 120 | do { 121 | if (bits < 15) { 122 | hold += (unsigned long)(PUP(in)) << bits; 123 | bits += 8; 124 | hold += (unsigned long)(PUP(in)) << bits; 125 | bits += 8; 126 | } 127 | here = lcode[hold & lmask]; 128 | dolen: 129 | op = (unsigned)(here.bits); 130 | hold >>= op; 131 | bits -= op; 132 | op = (unsigned)(here.op); 133 | if (op == 0) { /* literal */ 134 | Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ? 135 | "inflate: literal '%c'\n" : 136 | "inflate: literal 0x%02x\n", here.val)); 137 | PUP(out) = (unsigned char)(here.val); 138 | } 139 | else if (op & 16) { /* length base */ 140 | len = (unsigned)(here.val); 141 | op &= 15; /* number of extra bits */ 142 | if (op) { 143 | if (bits < op) { 144 | hold += (unsigned long)(PUP(in)) << bits; 145 | bits += 8; 146 | } 147 | len += (unsigned)hold & ((1U << op) - 1); 148 | hold >>= op; 149 | bits -= op; 150 | } 151 | Tracevv((stderr, "inflate: length %u\n", len)); 152 | if (bits < 15) { 153 | hold += (unsigned long)(PUP(in)) << bits; 154 | bits += 8; 155 | hold += (unsigned long)(PUP(in)) << bits; 156 | bits += 8; 157 | } 158 | here = dcode[hold & dmask]; 159 | dodist: 160 | op = (unsigned)(here.bits); 161 | hold >>= op; 162 | bits -= op; 163 | op = (unsigned)(here.op); 164 | if (op & 16) { /* distance base */ 165 | dist = (unsigned)(here.val); 166 | op &= 15; /* number of extra bits */ 167 | if (bits < op) { 168 | hold += (unsigned long)(PUP(in)) << bits; 169 | bits += 8; 170 | if (bits < op) { 171 | hold += (unsigned long)(PUP(in)) << bits; 172 | bits += 8; 173 | } 174 | } 175 | dist += (unsigned)hold & ((1U << op) - 1); 176 | #ifdef INFLATE_STRICT 177 | if (dist > dmax) { 178 | strm->msg = (char *)"invalid distance too far back"; 179 | state->mode = BAD; 180 | break; 181 | } 182 | #endif 183 | hold >>= op; 184 | bits -= op; 185 | Tracevv((stderr, "inflate: distance %u\n", dist)); 186 | op = (unsigned)(out - beg); /* max distance in output */ 187 | if (dist > op) { /* see if copy from window */ 188 | op = dist - op; /* distance back in window */ 189 | if (op > whave) { 190 | if (state->sane) { 191 | strm->msg = 192 | (char *)"invalid distance too far back"; 193 | state->mode = BAD; 194 | break; 195 | } 196 | #ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR 197 | if (len <= op - whave) { 198 | do { 199 | PUP(out) = 0; 200 | } while (--len); 201 | continue; 202 | } 203 | len -= op - whave; 204 | do { 205 | PUP(out) = 0; 206 | } while (--op > whave); 207 | if (op == 0) { 208 | from = out - dist; 209 | do { 210 | PUP(out) = PUP(from); 211 | } while (--len); 212 | continue; 213 | } 214 | #endif 215 | } 216 | from = window - OFF; 217 | if (wnext == 0) { /* very common case */ 218 | from += wsize - op; 219 | if (op < len) { /* some from window */ 220 | len -= op; 221 | do { 222 | PUP(out) = PUP(from); 223 | } while (--op); 224 | from = out - dist; /* rest from output */ 225 | } 226 | } 227 | else if (wnext < op) { /* wrap around window */ 228 | from += wsize + wnext - op; 229 | op -= wnext; 230 | if (op < len) { /* some from end of window */ 231 | len -= op; 232 | do { 233 | PUP(out) = PUP(from); 234 | } while (--op); 235 | from = window - OFF; 236 | if (wnext < len) { /* some from start of window */ 237 | op = wnext; 238 | len -= op; 239 | do { 240 | PUP(out) = PUP(from); 241 | } while (--op); 242 | from = out - dist; /* rest from output */ 243 | } 244 | } 245 | } 246 | else { /* contiguous in window */ 247 | from += wnext - op; 248 | if (op < len) { /* some from window */ 249 | len -= op; 250 | do { 251 | PUP(out) = PUP(from); 252 | } while (--op); 253 | from = out - dist; /* rest from output */ 254 | } 255 | } 256 | while (len > 2) { 257 | PUP(out) = PUP(from); 258 | PUP(out) = PUP(from); 259 | PUP(out) = PUP(from); 260 | len -= 3; 261 | } 262 | if (len) { 263 | PUP(out) = PUP(from); 264 | if (len > 1) 265 | PUP(out) = PUP(from); 266 | } 267 | } 268 | else { 269 | from = out - dist; /* copy direct from output */ 270 | do { /* minimum length is three */ 271 | PUP(out) = PUP(from); 272 | PUP(out) = PUP(from); 273 | PUP(out) = PUP(from); 274 | len -= 3; 275 | } while (len > 2); 276 | if (len) { 277 | PUP(out) = PUP(from); 278 | if (len > 1) 279 | PUP(out) = PUP(from); 280 | } 281 | } 282 | } 283 | else if ((op & 64) == 0) { /* 2nd level distance code */ 284 | here = dcode[here.val + (hold & ((1U << op) - 1))]; 285 | goto dodist; 286 | } 287 | else { 288 | strm->msg = (char *)"invalid distance code"; 289 | state->mode = BAD; 290 | break; 291 | } 292 | } 293 | else if ((op & 64) == 0) { /* 2nd level length code */ 294 | here = lcode[here.val + (hold & ((1U << op) - 1))]; 295 | goto dolen; 296 | } 297 | else if (op & 32) { /* end-of-block */ 298 | Tracevv((stderr, "inflate: end of block\n")); 299 | state->mode = TYPE; 300 | break; 301 | } 302 | else { 303 | strm->msg = (char *)"invalid literal/length code"; 304 | state->mode = BAD; 305 | break; 306 | } 307 | } while (in < last && out < end); 308 | 309 | /* return unused bytes (on entry, bits < 8, so in won't go too far back) */ 310 | len = bits >> 3; 311 | in -= len; 312 | bits -= len << 3; 313 | hold &= (1U << bits) - 1; 314 | 315 | /* update state and return */ 316 | strm->next_in = in + OFF; 317 | strm->next_out = out + OFF; 318 | strm->avail_in = (unsigned)(in < last ? 5 + (last - in) : 5 - (in - last)); 319 | strm->avail_out = (unsigned)(out < end ? 320 | 257 + (end - out) : 257 - (out - end)); 321 | state->hold = hold; 322 | state->bits = bits; 323 | return; 324 | } 325 | 326 | /* 327 | inflate_fast() speedups that turned out slower (on a PowerPC G3 750CXe): 328 | - Using bit fields for code structure 329 | - Different op definition to avoid & for extra bits (do & for table bits) 330 | - Three separate decoding do-loops for direct, window, and wnext == 0 331 | - Special case for distance > 1 copies to do overlapped load and store copy 332 | - Explicit branch predictions (based on measured branch probabilities) 333 | - Deferring match copy and interspersed it with decoding subsequent codes 334 | - Swapping literal/length else 335 | - Swapping window/direct else 336 | - Larger unrolled copy loops (three is about right) 337 | - Moving len -= 3 statement into middle of loop 338 | */ 339 | 340 | #endif /* !ASMINF */ 341 | -------------------------------------------------------------------------------- /zlib/inffast.h: -------------------------------------------------------------------------------- 1 | /* inffast.h -- header to use inffast.c 2 | * Copyright (C) 1995-2003, 2010 Mark Adler 3 | * For conditions of distribution and use, see copyright notice in zlib.h 4 | */ 5 | 6 | /* WARNING: this file should *not* be used by applications. It is 7 | part of the implementation of the compression library and is 8 | subject to change. Applications should only use zlib.h. 9 | */ 10 | 11 | void ZLIB_INTERNAL inflate_fast OF((z_streamp strm, unsigned start)); 12 | -------------------------------------------------------------------------------- /zlib/inffixed.h: -------------------------------------------------------------------------------- 1 | /* inffixed.h -- table for decoding fixed codes 2 | * Generated automatically by makefixed(). 3 | */ 4 | 5 | /* WARNING: this file should *not* be used by applications. 6 | It is part of the implementation of this library and is 7 | subject to change. Applications should only use zlib.h. 8 | */ 9 | 10 | static const code lenfix[512] = { 11 | {96,7,0},{0,8,80},{0,8,16},{20,8,115},{18,7,31},{0,8,112},{0,8,48}, 12 | {0,9,192},{16,7,10},{0,8,96},{0,8,32},{0,9,160},{0,8,0},{0,8,128}, 13 | {0,8,64},{0,9,224},{16,7,6},{0,8,88},{0,8,24},{0,9,144},{19,7,59}, 14 | {0,8,120},{0,8,56},{0,9,208},{17,7,17},{0,8,104},{0,8,40},{0,9,176}, 15 | {0,8,8},{0,8,136},{0,8,72},{0,9,240},{16,7,4},{0,8,84},{0,8,20}, 16 | {21,8,227},{19,7,43},{0,8,116},{0,8,52},{0,9,200},{17,7,13},{0,8,100}, 17 | {0,8,36},{0,9,168},{0,8,4},{0,8,132},{0,8,68},{0,9,232},{16,7,8}, 18 | {0,8,92},{0,8,28},{0,9,152},{20,7,83},{0,8,124},{0,8,60},{0,9,216}, 19 | {18,7,23},{0,8,108},{0,8,44},{0,9,184},{0,8,12},{0,8,140},{0,8,76}, 20 | {0,9,248},{16,7,3},{0,8,82},{0,8,18},{21,8,163},{19,7,35},{0,8,114}, 21 | {0,8,50},{0,9,196},{17,7,11},{0,8,98},{0,8,34},{0,9,164},{0,8,2}, 22 | {0,8,130},{0,8,66},{0,9,228},{16,7,7},{0,8,90},{0,8,26},{0,9,148}, 23 | {20,7,67},{0,8,122},{0,8,58},{0,9,212},{18,7,19},{0,8,106},{0,8,42}, 24 | {0,9,180},{0,8,10},{0,8,138},{0,8,74},{0,9,244},{16,7,5},{0,8,86}, 25 | {0,8,22},{64,8,0},{19,7,51},{0,8,118},{0,8,54},{0,9,204},{17,7,15}, 26 | {0,8,102},{0,8,38},{0,9,172},{0,8,6},{0,8,134},{0,8,70},{0,9,236}, 27 | {16,7,9},{0,8,94},{0,8,30},{0,9,156},{20,7,99},{0,8,126},{0,8,62}, 28 | {0,9,220},{18,7,27},{0,8,110},{0,8,46},{0,9,188},{0,8,14},{0,8,142}, 29 | {0,8,78},{0,9,252},{96,7,0},{0,8,81},{0,8,17},{21,8,131},{18,7,31}, 30 | {0,8,113},{0,8,49},{0,9,194},{16,7,10},{0,8,97},{0,8,33},{0,9,162}, 31 | {0,8,1},{0,8,129},{0,8,65},{0,9,226},{16,7,6},{0,8,89},{0,8,25}, 32 | {0,9,146},{19,7,59},{0,8,121},{0,8,57},{0,9,210},{17,7,17},{0,8,105}, 33 | {0,8,41},{0,9,178},{0,8,9},{0,8,137},{0,8,73},{0,9,242},{16,7,4}, 34 | {0,8,85},{0,8,21},{16,8,258},{19,7,43},{0,8,117},{0,8,53},{0,9,202}, 35 | {17,7,13},{0,8,101},{0,8,37},{0,9,170},{0,8,5},{0,8,133},{0,8,69}, 36 | {0,9,234},{16,7,8},{0,8,93},{0,8,29},{0,9,154},{20,7,83},{0,8,125}, 37 | {0,8,61},{0,9,218},{18,7,23},{0,8,109},{0,8,45},{0,9,186},{0,8,13}, 38 | {0,8,141},{0,8,77},{0,9,250},{16,7,3},{0,8,83},{0,8,19},{21,8,195}, 39 | {19,7,35},{0,8,115},{0,8,51},{0,9,198},{17,7,11},{0,8,99},{0,8,35}, 40 | {0,9,166},{0,8,3},{0,8,131},{0,8,67},{0,9,230},{16,7,7},{0,8,91}, 41 | {0,8,27},{0,9,150},{20,7,67},{0,8,123},{0,8,59},{0,9,214},{18,7,19}, 42 | {0,8,107},{0,8,43},{0,9,182},{0,8,11},{0,8,139},{0,8,75},{0,9,246}, 43 | {16,7,5},{0,8,87},{0,8,23},{64,8,0},{19,7,51},{0,8,119},{0,8,55}, 44 | {0,9,206},{17,7,15},{0,8,103},{0,8,39},{0,9,174},{0,8,7},{0,8,135}, 45 | {0,8,71},{0,9,238},{16,7,9},{0,8,95},{0,8,31},{0,9,158},{20,7,99}, 46 | {0,8,127},{0,8,63},{0,9,222},{18,7,27},{0,8,111},{0,8,47},{0,9,190}, 47 | {0,8,15},{0,8,143},{0,8,79},{0,9,254},{96,7,0},{0,8,80},{0,8,16}, 48 | {20,8,115},{18,7,31},{0,8,112},{0,8,48},{0,9,193},{16,7,10},{0,8,96}, 49 | {0,8,32},{0,9,161},{0,8,0},{0,8,128},{0,8,64},{0,9,225},{16,7,6}, 50 | {0,8,88},{0,8,24},{0,9,145},{19,7,59},{0,8,120},{0,8,56},{0,9,209}, 51 | {17,7,17},{0,8,104},{0,8,40},{0,9,177},{0,8,8},{0,8,136},{0,8,72}, 52 | {0,9,241},{16,7,4},{0,8,84},{0,8,20},{21,8,227},{19,7,43},{0,8,116}, 53 | {0,8,52},{0,9,201},{17,7,13},{0,8,100},{0,8,36},{0,9,169},{0,8,4}, 54 | {0,8,132},{0,8,68},{0,9,233},{16,7,8},{0,8,92},{0,8,28},{0,9,153}, 55 | {20,7,83},{0,8,124},{0,8,60},{0,9,217},{18,7,23},{0,8,108},{0,8,44}, 56 | {0,9,185},{0,8,12},{0,8,140},{0,8,76},{0,9,249},{16,7,3},{0,8,82}, 57 | {0,8,18},{21,8,163},{19,7,35},{0,8,114},{0,8,50},{0,9,197},{17,7,11}, 58 | {0,8,98},{0,8,34},{0,9,165},{0,8,2},{0,8,130},{0,8,66},{0,9,229}, 59 | {16,7,7},{0,8,90},{0,8,26},{0,9,149},{20,7,67},{0,8,122},{0,8,58}, 60 | {0,9,213},{18,7,19},{0,8,106},{0,8,42},{0,9,181},{0,8,10},{0,8,138}, 61 | {0,8,74},{0,9,245},{16,7,5},{0,8,86},{0,8,22},{64,8,0},{19,7,51}, 62 | {0,8,118},{0,8,54},{0,9,205},{17,7,15},{0,8,102},{0,8,38},{0,9,173}, 63 | {0,8,6},{0,8,134},{0,8,70},{0,9,237},{16,7,9},{0,8,94},{0,8,30}, 64 | {0,9,157},{20,7,99},{0,8,126},{0,8,62},{0,9,221},{18,7,27},{0,8,110}, 65 | {0,8,46},{0,9,189},{0,8,14},{0,8,142},{0,8,78},{0,9,253},{96,7,0}, 66 | {0,8,81},{0,8,17},{21,8,131},{18,7,31},{0,8,113},{0,8,49},{0,9,195}, 67 | {16,7,10},{0,8,97},{0,8,33},{0,9,163},{0,8,1},{0,8,129},{0,8,65}, 68 | {0,9,227},{16,7,6},{0,8,89},{0,8,25},{0,9,147},{19,7,59},{0,8,121}, 69 | {0,8,57},{0,9,211},{17,7,17},{0,8,105},{0,8,41},{0,9,179},{0,8,9}, 70 | {0,8,137},{0,8,73},{0,9,243},{16,7,4},{0,8,85},{0,8,21},{16,8,258}, 71 | {19,7,43},{0,8,117},{0,8,53},{0,9,203},{17,7,13},{0,8,101},{0,8,37}, 72 | {0,9,171},{0,8,5},{0,8,133},{0,8,69},{0,9,235},{16,7,8},{0,8,93}, 73 | {0,8,29},{0,9,155},{20,7,83},{0,8,125},{0,8,61},{0,9,219},{18,7,23}, 74 | {0,8,109},{0,8,45},{0,9,187},{0,8,13},{0,8,141},{0,8,77},{0,9,251}, 75 | {16,7,3},{0,8,83},{0,8,19},{21,8,195},{19,7,35},{0,8,115},{0,8,51}, 76 | {0,9,199},{17,7,11},{0,8,99},{0,8,35},{0,9,167},{0,8,3},{0,8,131}, 77 | {0,8,67},{0,9,231},{16,7,7},{0,8,91},{0,8,27},{0,9,151},{20,7,67}, 78 | {0,8,123},{0,8,59},{0,9,215},{18,7,19},{0,8,107},{0,8,43},{0,9,183}, 79 | {0,8,11},{0,8,139},{0,8,75},{0,9,247},{16,7,5},{0,8,87},{0,8,23}, 80 | {64,8,0},{19,7,51},{0,8,119},{0,8,55},{0,9,207},{17,7,15},{0,8,103}, 81 | {0,8,39},{0,9,175},{0,8,7},{0,8,135},{0,8,71},{0,9,239},{16,7,9}, 82 | {0,8,95},{0,8,31},{0,9,159},{20,7,99},{0,8,127},{0,8,63},{0,9,223}, 83 | {18,7,27},{0,8,111},{0,8,47},{0,9,191},{0,8,15},{0,8,143},{0,8,79}, 84 | {0,9,255} 85 | }; 86 | 87 | static const code distfix[32] = { 88 | {16,5,1},{23,5,257},{19,5,17},{27,5,4097},{17,5,5},{25,5,1025}, 89 | {21,5,65},{29,5,16385},{16,5,3},{24,5,513},{20,5,33},{28,5,8193}, 90 | {18,5,9},{26,5,2049},{22,5,129},{64,5,0},{16,5,2},{23,5,385}, 91 | {19,5,25},{27,5,6145},{17,5,7},{25,5,1537},{21,5,97},{29,5,24577}, 92 | {16,5,4},{24,5,769},{20,5,49},{28,5,12289},{18,5,13},{26,5,3073}, 93 | {22,5,193},{64,5,0} 94 | }; 95 | -------------------------------------------------------------------------------- /zlib/inflate.h: -------------------------------------------------------------------------------- 1 | /* inflate.h -- internal inflate state definition 2 | * Copyright (C) 1995-2009 Mark Adler 3 | * For conditions of distribution and use, see copyright notice in zlib.h 4 | */ 5 | 6 | /* WARNING: this file should *not* be used by applications. It is 7 | part of the implementation of the compression library and is 8 | subject to change. Applications should only use zlib.h. 9 | */ 10 | 11 | /* define NO_GZIP when compiling if you want to disable gzip header and 12 | trailer decoding by inflate(). NO_GZIP would be used to avoid linking in 13 | the crc code when it is not needed. For shared libraries, gzip decoding 14 | should be left enabled. */ 15 | #ifndef NO_GZIP 16 | # define GUNZIP 17 | #endif 18 | 19 | /* Possible inflate modes between inflate() calls */ 20 | typedef enum { 21 | HEAD, /* i: waiting for magic header */ 22 | FLAGS, /* i: waiting for method and flags (gzip) */ 23 | TIME, /* i: waiting for modification time (gzip) */ 24 | OS, /* i: waiting for extra flags and operating system (gzip) */ 25 | EXLEN, /* i: waiting for extra length (gzip) */ 26 | EXTRA, /* i: waiting for extra bytes (gzip) */ 27 | NAME, /* i: waiting for end of file name (gzip) */ 28 | COMMENT, /* i: waiting for end of comment (gzip) */ 29 | HCRC, /* i: waiting for header crc (gzip) */ 30 | DICTID, /* i: waiting for dictionary check value */ 31 | DICT, /* waiting for inflateSetDictionary() call */ 32 | TYPE, /* i: waiting for type bits, including last-flag bit */ 33 | TYPEDO, /* i: same, but skip check to exit inflate on new block */ 34 | STORED, /* i: waiting for stored size (length and complement) */ 35 | COPY_, /* i/o: same as COPY below, but only first time in */ 36 | COPY, /* i/o: waiting for input or output to copy stored block */ 37 | TABLE, /* i: waiting for dynamic block table lengths */ 38 | LENLENS, /* i: waiting for code length code lengths */ 39 | CODELENS, /* i: waiting for length/lit and distance code lengths */ 40 | LEN_, /* i: same as LEN below, but only first time in */ 41 | LEN, /* i: waiting for length/lit/eob code */ 42 | LENEXT, /* i: waiting for length extra bits */ 43 | DIST, /* i: waiting for distance code */ 44 | DISTEXT, /* i: waiting for distance extra bits */ 45 | MATCH, /* o: waiting for output space to copy string */ 46 | LIT, /* o: waiting for output space to write literal */ 47 | CHECK, /* i: waiting for 32-bit check value */ 48 | LENGTH, /* i: waiting for 32-bit length (gzip) */ 49 | DONE, /* finished check, done -- remain here until reset */ 50 | BAD, /* got a data error -- remain here until reset */ 51 | MEM, /* got an inflate() memory error -- remain here until reset */ 52 | SYNC /* looking for synchronization bytes to restart inflate() */ 53 | } inflate_mode; 54 | 55 | /* 56 | State transitions between above modes - 57 | 58 | (most modes can go to BAD or MEM on error -- not shown for clarity) 59 | 60 | Process header: 61 | HEAD -> (gzip) or (zlib) or (raw) 62 | (gzip) -> FLAGS -> TIME -> OS -> EXLEN -> EXTRA -> NAME -> COMMENT -> 63 | HCRC -> TYPE 64 | (zlib) -> DICTID or TYPE 65 | DICTID -> DICT -> TYPE 66 | (raw) -> TYPEDO 67 | Read deflate blocks: 68 | TYPE -> TYPEDO -> STORED or TABLE or LEN_ or CHECK 69 | STORED -> COPY_ -> COPY -> TYPE 70 | TABLE -> LENLENS -> CODELENS -> LEN_ 71 | LEN_ -> LEN 72 | Read deflate codes in fixed or dynamic block: 73 | LEN -> LENEXT or LIT or TYPE 74 | LENEXT -> DIST -> DISTEXT -> MATCH -> LEN 75 | LIT -> LEN 76 | Process trailer: 77 | CHECK -> LENGTH -> DONE 78 | */ 79 | 80 | /* state maintained between inflate() calls. Approximately 10K bytes. */ 81 | struct inflate_state { 82 | inflate_mode mode; /* current inflate mode */ 83 | int last; /* true if processing last block */ 84 | int wrap; /* bit 0 true for zlib, bit 1 true for gzip */ 85 | int havedict; /* true if dictionary provided */ 86 | int flags; /* gzip header method and flags (0 if zlib) */ 87 | unsigned dmax; /* zlib header max distance (INFLATE_STRICT) */ 88 | unsigned long check; /* protected copy of check value */ 89 | unsigned long total; /* protected copy of output count */ 90 | gz_headerp head; /* where to save gzip header information */ 91 | /* sliding window */ 92 | unsigned wbits; /* log base 2 of requested window size */ 93 | unsigned wsize; /* window size or zero if not using window */ 94 | unsigned whave; /* valid bytes in the window */ 95 | unsigned wnext; /* window write index */ 96 | unsigned char FAR *window; /* allocated sliding window, if needed */ 97 | /* bit accumulator */ 98 | unsigned long hold; /* input bit accumulator */ 99 | unsigned bits; /* number of bits in "in" */ 100 | /* for string and stored block copying */ 101 | unsigned length; /* literal or length of data to copy */ 102 | unsigned offset; /* distance back to copy string from */ 103 | /* for table and code decoding */ 104 | unsigned extra; /* extra bits needed */ 105 | /* fixed and dynamic code tables */ 106 | code const FAR *lencode; /* starting table for length/literal codes */ 107 | code const FAR *distcode; /* starting table for distance codes */ 108 | unsigned lenbits; /* index bits for lencode */ 109 | unsigned distbits; /* index bits for distcode */ 110 | /* dynamic table building */ 111 | unsigned ncode; /* number of code length code lengths */ 112 | unsigned nlen; /* number of length code lengths */ 113 | unsigned ndist; /* number of distance code lengths */ 114 | unsigned have; /* number of code lengths in lens[] */ 115 | code FAR *next; /* next available space in codes[] */ 116 | unsigned short lens[320]; /* temporary storage for code lengths */ 117 | unsigned short work[288]; /* work area for code table building */ 118 | code codes[ENOUGH]; /* space for code tables */ 119 | int sane; /* if false, allow invalid distance too far */ 120 | int back; /* bits back of last unprocessed length/lit */ 121 | unsigned was; /* initial length of match */ 122 | }; 123 | -------------------------------------------------------------------------------- /zlib/inftrees.c: -------------------------------------------------------------------------------- 1 | /* inftrees.c -- generate Huffman trees for efficient decoding 2 | * Copyright (C) 1995-2013 Mark Adler 3 | * For conditions of distribution and use, see copyright notice in zlib.h 4 | */ 5 | 6 | #include "zutil.h" 7 | #include "inftrees.h" 8 | 9 | #define MAXBITS 15 10 | 11 | const char inflate_copyright[] = 12 | " inflate 1.2.8 Copyright 1995-2013 Mark Adler "; 13 | /* 14 | If you use the zlib library in a product, an acknowledgment is welcome 15 | in the documentation of your product. If for some reason you cannot 16 | include such an acknowledgment, I would appreciate that you keep this 17 | copyright string in the executable of your product. 18 | */ 19 | 20 | /* 21 | Build a set of tables to decode the provided canonical Huffman code. 22 | The code lengths are lens[0..codes-1]. The result starts at *table, 23 | whose indices are 0..2^bits-1. work is a writable array of at least 24 | lens shorts, which is used as a work area. type is the type of code 25 | to be generated, CODES, LENS, or DISTS. On return, zero is success, 26 | -1 is an invalid code, and +1 means that ENOUGH isn't enough. table 27 | on return points to the next available entry's address. bits is the 28 | requested root table index bits, and on return it is the actual root 29 | table index bits. It will differ if the request is greater than the 30 | longest code or if it is less than the shortest code. 31 | */ 32 | int ZLIB_INTERNAL inflate_table(type, lens, codes, table, bits, work) 33 | codetype type; 34 | unsigned short FAR *lens; 35 | unsigned codes; 36 | code FAR * FAR *table; 37 | unsigned FAR *bits; 38 | unsigned short FAR *work; 39 | { 40 | unsigned len; /* a code's length in bits */ 41 | unsigned sym; /* index of code symbols */ 42 | unsigned min, max; /* minimum and maximum code lengths */ 43 | unsigned root; /* number of index bits for root table */ 44 | unsigned curr; /* number of index bits for current table */ 45 | unsigned drop; /* code bits to drop for sub-table */ 46 | int left; /* number of prefix codes available */ 47 | unsigned used; /* code entries in table used */ 48 | unsigned huff; /* Huffman code */ 49 | unsigned incr; /* for incrementing code, index */ 50 | unsigned fill; /* index for replicating entries */ 51 | unsigned low; /* low bits for current root entry */ 52 | unsigned mask; /* mask for low root bits */ 53 | code here; /* table entry for duplication */ 54 | code FAR *next; /* next available space in table */ 55 | const unsigned short FAR *base; /* base value table to use */ 56 | const unsigned short FAR *extra; /* extra bits table to use */ 57 | int end; /* use base and extra for symbol > end */ 58 | unsigned short count[MAXBITS+1]; /* number of codes of each length */ 59 | unsigned short offs[MAXBITS+1]; /* offsets in table for each length */ 60 | static const unsigned short lbase[31] = { /* Length codes 257..285 base */ 61 | 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 62 | 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0}; 63 | static const unsigned short lext[31] = { /* Length codes 257..285 extra */ 64 | 16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18, 18, 65 | 19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 16, 72, 78}; 66 | static const unsigned short dbase[32] = { /* Distance codes 0..29 base */ 67 | 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 68 | 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 69 | 8193, 12289, 16385, 24577, 0, 0}; 70 | static const unsigned short dext[32] = { /* Distance codes 0..29 extra */ 71 | 16, 16, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 72 | 23, 23, 24, 24, 25, 25, 26, 26, 27, 27, 73 | 28, 28, 29, 29, 64, 64}; 74 | 75 | /* 76 | Process a set of code lengths to create a canonical Huffman code. The 77 | code lengths are lens[0..codes-1]. Each length corresponds to the 78 | symbols 0..codes-1. The Huffman code is generated by first sorting the 79 | symbols by length from short to long, and retaining the symbol order 80 | for codes with equal lengths. Then the code starts with all zero bits 81 | for the first code of the shortest length, and the codes are integer 82 | increments for the same length, and zeros are appended as the length 83 | increases. For the deflate format, these bits are stored backwards 84 | from their more natural integer increment ordering, and so when the 85 | decoding tables are built in the large loop below, the integer codes 86 | are incremented backwards. 87 | 88 | This routine assumes, but does not check, that all of the entries in 89 | lens[] are in the range 0..MAXBITS. The caller must assure this. 90 | 1..MAXBITS is interpreted as that code length. zero means that that 91 | symbol does not occur in this code. 92 | 93 | The codes are sorted by computing a count of codes for each length, 94 | creating from that a table of starting indices for each length in the 95 | sorted table, and then entering the symbols in order in the sorted 96 | table. The sorted table is work[], with that space being provided by 97 | the caller. 98 | 99 | The length counts are used for other purposes as well, i.e. finding 100 | the minimum and maximum length codes, determining if there are any 101 | codes at all, checking for a valid set of lengths, and looking ahead 102 | at length counts to determine sub-table sizes when building the 103 | decoding tables. 104 | */ 105 | 106 | /* accumulate lengths for codes (assumes lens[] all in 0..MAXBITS) */ 107 | for (len = 0; len <= MAXBITS; len++) 108 | count[len] = 0; 109 | for (sym = 0; sym < codes; sym++) 110 | count[lens[sym]]++; 111 | 112 | /* bound code lengths, force root to be within code lengths */ 113 | root = *bits; 114 | for (max = MAXBITS; max >= 1; max--) 115 | if (count[max] != 0) break; 116 | if (root > max) root = max; 117 | if (max == 0) { /* no symbols to code at all */ 118 | here.op = (unsigned char)64; /* invalid code marker */ 119 | here.bits = (unsigned char)1; 120 | here.val = (unsigned short)0; 121 | *(*table)++ = here; /* make a table to force an error */ 122 | *(*table)++ = here; 123 | *bits = 1; 124 | return 0; /* no symbols, but wait for decoding to report error */ 125 | } 126 | for (min = 1; min < max; min++) 127 | if (count[min] != 0) break; 128 | if (root < min) root = min; 129 | 130 | /* check for an over-subscribed or incomplete set of lengths */ 131 | left = 1; 132 | for (len = 1; len <= MAXBITS; len++) { 133 | left <<= 1; 134 | left -= count[len]; 135 | if (left < 0) return -1; /* over-subscribed */ 136 | } 137 | if (left > 0 && (type == CODES || max != 1)) 138 | return -1; /* incomplete set */ 139 | 140 | /* generate offsets into symbol table for each length for sorting */ 141 | offs[1] = 0; 142 | for (len = 1; len < MAXBITS; len++) 143 | offs[len + 1] = offs[len] + count[len]; 144 | 145 | /* sort symbols by length, by symbol order within each length */ 146 | for (sym = 0; sym < codes; sym++) 147 | if (lens[sym] != 0) work[offs[lens[sym]]++] = (unsigned short)sym; 148 | 149 | /* 150 | Create and fill in decoding tables. In this loop, the table being 151 | filled is at next and has curr index bits. The code being used is huff 152 | with length len. That code is converted to an index by dropping drop 153 | bits off of the bottom. For codes where len is less than drop + curr, 154 | those top drop + curr - len bits are incremented through all values to 155 | fill the table with replicated entries. 156 | 157 | root is the number of index bits for the root table. When len exceeds 158 | root, sub-tables are created pointed to by the root entry with an index 159 | of the low root bits of huff. This is saved in low to check for when a 160 | new sub-table should be started. drop is zero when the root table is 161 | being filled, and drop is root when sub-tables are being filled. 162 | 163 | When a new sub-table is needed, it is necessary to look ahead in the 164 | code lengths to determine what size sub-table is needed. The length 165 | counts are used for this, and so count[] is decremented as codes are 166 | entered in the tables. 167 | 168 | used keeps track of how many table entries have been allocated from the 169 | provided *table space. It is checked for LENS and DIST tables against 170 | the constants ENOUGH_LENS and ENOUGH_DISTS to guard against changes in 171 | the initial root table size constants. See the comments in inftrees.h 172 | for more information. 173 | 174 | sym increments through all symbols, and the loop terminates when 175 | all codes of length max, i.e. all codes, have been processed. This 176 | routine permits incomplete codes, so another loop after this one fills 177 | in the rest of the decoding tables with invalid code markers. 178 | */ 179 | 180 | /* set up for code type */ 181 | switch (type) { 182 | case CODES: 183 | base = extra = work; /* dummy value--not used */ 184 | end = 19; 185 | break; 186 | case LENS: 187 | base = lbase; 188 | base -= 257; 189 | extra = lext; 190 | extra -= 257; 191 | end = 256; 192 | break; 193 | default: /* DISTS */ 194 | base = dbase; 195 | extra = dext; 196 | end = -1; 197 | } 198 | 199 | /* initialize state for loop */ 200 | huff = 0; /* starting code */ 201 | sym = 0; /* starting code symbol */ 202 | len = min; /* starting code length */ 203 | next = *table; /* current table to fill in */ 204 | curr = root; /* current table index bits */ 205 | drop = 0; /* current bits to drop from code for index */ 206 | low = (unsigned)(-1); /* trigger new sub-table when len > root */ 207 | used = 1U << root; /* use root table entries */ 208 | mask = used - 1; /* mask for comparing low */ 209 | 210 | /* check available table space */ 211 | if ((type == LENS && used > ENOUGH_LENS) || 212 | (type == DISTS && used > ENOUGH_DISTS)) 213 | return 1; 214 | 215 | /* process all codes and make table entries */ 216 | for (;;) { 217 | /* create table entry */ 218 | here.bits = (unsigned char)(len - drop); 219 | if ((int)(work[sym]) < end) { 220 | here.op = (unsigned char)0; 221 | here.val = work[sym]; 222 | } 223 | else if ((int)(work[sym]) > end) { 224 | here.op = (unsigned char)(extra[work[sym]]); 225 | here.val = base[work[sym]]; 226 | } 227 | else { 228 | here.op = (unsigned char)(32 + 64); /* end of block */ 229 | here.val = 0; 230 | } 231 | 232 | /* replicate for those indices with low len bits equal to huff */ 233 | incr = 1U << (len - drop); 234 | fill = 1U << curr; 235 | min = fill; /* save offset to next table */ 236 | do { 237 | fill -= incr; 238 | next[(huff >> drop) + fill] = here; 239 | } while (fill != 0); 240 | 241 | /* backwards increment the len-bit code huff */ 242 | incr = 1U << (len - 1); 243 | while (huff & incr) 244 | incr >>= 1; 245 | if (incr != 0) { 246 | huff &= incr - 1; 247 | huff += incr; 248 | } 249 | else 250 | huff = 0; 251 | 252 | /* go to next symbol, update count, len */ 253 | sym++; 254 | if (--(count[len]) == 0) { 255 | if (len == max) break; 256 | len = lens[work[sym]]; 257 | } 258 | 259 | /* create new sub-table if needed */ 260 | if (len > root && (huff & mask) != low) { 261 | /* if first time, transition to sub-tables */ 262 | if (drop == 0) 263 | drop = root; 264 | 265 | /* increment past last table */ 266 | next += min; /* here min is 1 << curr */ 267 | 268 | /* determine length of next table */ 269 | curr = len - drop; 270 | left = (int)(1 << curr); 271 | while (curr + drop < max) { 272 | left -= count[curr + drop]; 273 | if (left <= 0) break; 274 | curr++; 275 | left <<= 1; 276 | } 277 | 278 | /* check for enough space */ 279 | used += 1U << curr; 280 | if ((type == LENS && used > ENOUGH_LENS) || 281 | (type == DISTS && used > ENOUGH_DISTS)) 282 | return 1; 283 | 284 | /* point entry in root table to sub-table */ 285 | low = huff & mask; 286 | (*table)[low].op = (unsigned char)curr; 287 | (*table)[low].bits = (unsigned char)root; 288 | (*table)[low].val = (unsigned short)(next - *table); 289 | } 290 | } 291 | 292 | /* fill in remaining table entry if code is incomplete (guaranteed to have 293 | at most one remaining entry, since if the code is incomplete, the 294 | maximum code length that was allowed to get this far is one bit) */ 295 | if (huff != 0) { 296 | here.op = (unsigned char)64; /* invalid code marker */ 297 | here.bits = (unsigned char)(len - drop); 298 | here.val = (unsigned short)0; 299 | next[huff] = here; 300 | } 301 | 302 | /* set return parameters */ 303 | *table += used; 304 | *bits = root; 305 | return 0; 306 | } 307 | -------------------------------------------------------------------------------- /zlib/inftrees.h: -------------------------------------------------------------------------------- 1 | /* inftrees.h -- header to use inftrees.c 2 | * Copyright (C) 1995-2005, 2010 Mark Adler 3 | * For conditions of distribution and use, see copyright notice in zlib.h 4 | */ 5 | 6 | /* WARNING: this file should *not* be used by applications. It is 7 | part of the implementation of the compression library and is 8 | subject to change. Applications should only use zlib.h. 9 | */ 10 | 11 | /* Structure for decoding tables. Each entry provides either the 12 | information needed to do the operation requested by the code that 13 | indexed that table entry, or it provides a pointer to another 14 | table that indexes more bits of the code. op indicates whether 15 | the entry is a pointer to another table, a literal, a length or 16 | distance, an end-of-block, or an invalid code. For a table 17 | pointer, the low four bits of op is the number of index bits of 18 | that table. For a length or distance, the low four bits of op 19 | is the number of extra bits to get after the code. bits is 20 | the number of bits in this code or part of the code to drop off 21 | of the bit buffer. val is the actual byte to output in the case 22 | of a literal, the base length or distance, or the offset from 23 | the current table to the next table. Each entry is four bytes. */ 24 | typedef struct { 25 | unsigned char op; /* operation, extra bits, table bits */ 26 | unsigned char bits; /* bits in this part of the code */ 27 | unsigned short val; /* offset in table or code value */ 28 | } code; 29 | 30 | /* op values as set by inflate_table(): 31 | 00000000 - literal 32 | 0000tttt - table link, tttt != 0 is the number of table index bits 33 | 0001eeee - length or distance, eeee is the number of extra bits 34 | 01100000 - end of block 35 | 01000000 - invalid code 36 | */ 37 | 38 | /* Maximum size of the dynamic table. The maximum number of code structures is 39 | 1444, which is the sum of 852 for literal/length codes and 592 for distance 40 | codes. These values were found by exhaustive searches using the program 41 | examples/enough.c found in the zlib distribtution. The arguments to that 42 | program are the number of symbols, the initial root table size, and the 43 | maximum bit length of a code. "enough 286 9 15" for literal/length codes 44 | returns returns 852, and "enough 30 6 15" for distance codes returns 592. 45 | The initial root table size (9 or 6) is found in the fifth argument of the 46 | inflate_table() calls in inflate.c and infback.c. If the root table size is 47 | changed, then these maximum sizes would be need to be recalculated and 48 | updated. */ 49 | #define ENOUGH_LENS 852 50 | #define ENOUGH_DISTS 592 51 | #define ENOUGH (ENOUGH_LENS+ENOUGH_DISTS) 52 | 53 | /* Type of code to build for inflate_table() */ 54 | typedef enum { 55 | CODES, 56 | LENS, 57 | DISTS 58 | } codetype; 59 | 60 | int ZLIB_INTERNAL inflate_table OF((codetype type, unsigned short FAR *lens, 61 | unsigned codes, code FAR * FAR *table, 62 | unsigned FAR *bits, unsigned short FAR *work)); 63 | -------------------------------------------------------------------------------- /zlib/trees.h: -------------------------------------------------------------------------------- 1 | /* header created automatically with -DGEN_TREES_H */ 2 | 3 | local const ct_data static_ltree[L_CODES+2] = { 4 | {{ 12},{ 8}}, {{140},{ 8}}, {{ 76},{ 8}}, {{204},{ 8}}, {{ 44},{ 8}}, 5 | {{172},{ 8}}, {{108},{ 8}}, {{236},{ 8}}, {{ 28},{ 8}}, {{156},{ 8}}, 6 | {{ 92},{ 8}}, {{220},{ 8}}, {{ 60},{ 8}}, {{188},{ 8}}, {{124},{ 8}}, 7 | {{252},{ 8}}, {{ 2},{ 8}}, {{130},{ 8}}, {{ 66},{ 8}}, {{194},{ 8}}, 8 | {{ 34},{ 8}}, {{162},{ 8}}, {{ 98},{ 8}}, {{226},{ 8}}, {{ 18},{ 8}}, 9 | {{146},{ 8}}, {{ 82},{ 8}}, {{210},{ 8}}, {{ 50},{ 8}}, {{178},{ 8}}, 10 | {{114},{ 8}}, {{242},{ 8}}, {{ 10},{ 8}}, {{138},{ 8}}, {{ 74},{ 8}}, 11 | {{202},{ 8}}, {{ 42},{ 8}}, {{170},{ 8}}, {{106},{ 8}}, {{234},{ 8}}, 12 | {{ 26},{ 8}}, {{154},{ 8}}, {{ 90},{ 8}}, {{218},{ 8}}, {{ 58},{ 8}}, 13 | {{186},{ 8}}, {{122},{ 8}}, {{250},{ 8}}, {{ 6},{ 8}}, {{134},{ 8}}, 14 | {{ 70},{ 8}}, {{198},{ 8}}, {{ 38},{ 8}}, {{166},{ 8}}, {{102},{ 8}}, 15 | {{230},{ 8}}, {{ 22},{ 8}}, {{150},{ 8}}, {{ 86},{ 8}}, {{214},{ 8}}, 16 | {{ 54},{ 8}}, {{182},{ 8}}, {{118},{ 8}}, {{246},{ 8}}, {{ 14},{ 8}}, 17 | {{142},{ 8}}, {{ 78},{ 8}}, {{206},{ 8}}, {{ 46},{ 8}}, {{174},{ 8}}, 18 | {{110},{ 8}}, {{238},{ 8}}, {{ 30},{ 8}}, {{158},{ 8}}, {{ 94},{ 8}}, 19 | {{222},{ 8}}, {{ 62},{ 8}}, {{190},{ 8}}, {{126},{ 8}}, {{254},{ 8}}, 20 | {{ 1},{ 8}}, {{129},{ 8}}, {{ 65},{ 8}}, {{193},{ 8}}, {{ 33},{ 8}}, 21 | {{161},{ 8}}, {{ 97},{ 8}}, {{225},{ 8}}, {{ 17},{ 8}}, {{145},{ 8}}, 22 | {{ 81},{ 8}}, {{209},{ 8}}, {{ 49},{ 8}}, {{177},{ 8}}, {{113},{ 8}}, 23 | {{241},{ 8}}, {{ 9},{ 8}}, {{137},{ 8}}, {{ 73},{ 8}}, {{201},{ 8}}, 24 | {{ 41},{ 8}}, {{169},{ 8}}, {{105},{ 8}}, {{233},{ 8}}, {{ 25},{ 8}}, 25 | {{153},{ 8}}, {{ 89},{ 8}}, {{217},{ 8}}, {{ 57},{ 8}}, {{185},{ 8}}, 26 | {{121},{ 8}}, {{249},{ 8}}, {{ 5},{ 8}}, {{133},{ 8}}, {{ 69},{ 8}}, 27 | {{197},{ 8}}, {{ 37},{ 8}}, {{165},{ 8}}, {{101},{ 8}}, {{229},{ 8}}, 28 | {{ 21},{ 8}}, {{149},{ 8}}, {{ 85},{ 8}}, {{213},{ 8}}, {{ 53},{ 8}}, 29 | {{181},{ 8}}, {{117},{ 8}}, {{245},{ 8}}, {{ 13},{ 8}}, {{141},{ 8}}, 30 | {{ 77},{ 8}}, {{205},{ 8}}, {{ 45},{ 8}}, {{173},{ 8}}, {{109},{ 8}}, 31 | {{237},{ 8}}, {{ 29},{ 8}}, {{157},{ 8}}, {{ 93},{ 8}}, {{221},{ 8}}, 32 | {{ 61},{ 8}}, {{189},{ 8}}, {{125},{ 8}}, {{253},{ 8}}, {{ 19},{ 9}}, 33 | {{275},{ 9}}, {{147},{ 9}}, {{403},{ 9}}, {{ 83},{ 9}}, {{339},{ 9}}, 34 | {{211},{ 9}}, {{467},{ 9}}, {{ 51},{ 9}}, {{307},{ 9}}, {{179},{ 9}}, 35 | {{435},{ 9}}, {{115},{ 9}}, {{371},{ 9}}, {{243},{ 9}}, {{499},{ 9}}, 36 | {{ 11},{ 9}}, {{267},{ 9}}, {{139},{ 9}}, {{395},{ 9}}, {{ 75},{ 9}}, 37 | {{331},{ 9}}, {{203},{ 9}}, {{459},{ 9}}, {{ 43},{ 9}}, {{299},{ 9}}, 38 | {{171},{ 9}}, {{427},{ 9}}, {{107},{ 9}}, {{363},{ 9}}, {{235},{ 9}}, 39 | {{491},{ 9}}, {{ 27},{ 9}}, {{283},{ 9}}, {{155},{ 9}}, {{411},{ 9}}, 40 | {{ 91},{ 9}}, {{347},{ 9}}, {{219},{ 9}}, {{475},{ 9}}, {{ 59},{ 9}}, 41 | {{315},{ 9}}, {{187},{ 9}}, {{443},{ 9}}, {{123},{ 9}}, {{379},{ 9}}, 42 | {{251},{ 9}}, {{507},{ 9}}, {{ 7},{ 9}}, {{263},{ 9}}, {{135},{ 9}}, 43 | {{391},{ 9}}, {{ 71},{ 9}}, {{327},{ 9}}, {{199},{ 9}}, {{455},{ 9}}, 44 | {{ 39},{ 9}}, {{295},{ 9}}, {{167},{ 9}}, {{423},{ 9}}, {{103},{ 9}}, 45 | {{359},{ 9}}, {{231},{ 9}}, {{487},{ 9}}, {{ 23},{ 9}}, {{279},{ 9}}, 46 | {{151},{ 9}}, {{407},{ 9}}, {{ 87},{ 9}}, {{343},{ 9}}, {{215},{ 9}}, 47 | {{471},{ 9}}, {{ 55},{ 9}}, {{311},{ 9}}, {{183},{ 9}}, {{439},{ 9}}, 48 | {{119},{ 9}}, {{375},{ 9}}, {{247},{ 9}}, {{503},{ 9}}, {{ 15},{ 9}}, 49 | {{271},{ 9}}, {{143},{ 9}}, {{399},{ 9}}, {{ 79},{ 9}}, {{335},{ 9}}, 50 | {{207},{ 9}}, {{463},{ 9}}, {{ 47},{ 9}}, {{303},{ 9}}, {{175},{ 9}}, 51 | {{431},{ 9}}, {{111},{ 9}}, {{367},{ 9}}, {{239},{ 9}}, {{495},{ 9}}, 52 | {{ 31},{ 9}}, {{287},{ 9}}, {{159},{ 9}}, {{415},{ 9}}, {{ 95},{ 9}}, 53 | {{351},{ 9}}, {{223},{ 9}}, {{479},{ 9}}, {{ 63},{ 9}}, {{319},{ 9}}, 54 | {{191},{ 9}}, {{447},{ 9}}, {{127},{ 9}}, {{383},{ 9}}, {{255},{ 9}}, 55 | {{511},{ 9}}, {{ 0},{ 7}}, {{ 64},{ 7}}, {{ 32},{ 7}}, {{ 96},{ 7}}, 56 | {{ 16},{ 7}}, {{ 80},{ 7}}, {{ 48},{ 7}}, {{112},{ 7}}, {{ 8},{ 7}}, 57 | {{ 72},{ 7}}, {{ 40},{ 7}}, {{104},{ 7}}, {{ 24},{ 7}}, {{ 88},{ 7}}, 58 | {{ 56},{ 7}}, {{120},{ 7}}, {{ 4},{ 7}}, {{ 68},{ 7}}, {{ 36},{ 7}}, 59 | {{100},{ 7}}, {{ 20},{ 7}}, {{ 84},{ 7}}, {{ 52},{ 7}}, {{116},{ 7}}, 60 | {{ 3},{ 8}}, {{131},{ 8}}, {{ 67},{ 8}}, {{195},{ 8}}, {{ 35},{ 8}}, 61 | {{163},{ 8}}, {{ 99},{ 8}}, {{227},{ 8}} 62 | }; 63 | 64 | local const ct_data static_dtree[D_CODES] = { 65 | {{ 0},{ 5}}, {{16},{ 5}}, {{ 8},{ 5}}, {{24},{ 5}}, {{ 4},{ 5}}, 66 | {{20},{ 5}}, {{12},{ 5}}, {{28},{ 5}}, {{ 2},{ 5}}, {{18},{ 5}}, 67 | {{10},{ 5}}, {{26},{ 5}}, {{ 6},{ 5}}, {{22},{ 5}}, {{14},{ 5}}, 68 | {{30},{ 5}}, {{ 1},{ 5}}, {{17},{ 5}}, {{ 9},{ 5}}, {{25},{ 5}}, 69 | {{ 5},{ 5}}, {{21},{ 5}}, {{13},{ 5}}, {{29},{ 5}}, {{ 3},{ 5}}, 70 | {{19},{ 5}}, {{11},{ 5}}, {{27},{ 5}}, {{ 7},{ 5}}, {{23},{ 5}} 71 | }; 72 | 73 | const uch ZLIB_INTERNAL _dist_code[DIST_CODE_LEN] = { 74 | 0, 1, 2, 3, 4, 4, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 75 | 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 76 | 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 77 | 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 78 | 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13, 79 | 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 80 | 13, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 81 | 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 82 | 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 83 | 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15, 15, 15, 84 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 85 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 86 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 0, 0, 16, 17, 87 | 18, 18, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 22, 22, 22, 22, 22, 22, 22, 22, 88 | 23, 23, 23, 23, 23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 89 | 24, 24, 24, 24, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 90 | 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 91 | 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 27, 27, 27, 27, 27, 27, 27, 27, 92 | 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 93 | 27, 27, 27, 27, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 94 | 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 95 | 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 96 | 28, 28, 28, 28, 28, 28, 28, 28, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 97 | 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 98 | 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 99 | 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29 100 | }; 101 | 102 | const uch ZLIB_INTERNAL _length_code[MAX_MATCH-MIN_MATCH+1]= { 103 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 12, 12, 104 | 13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 15, 16, 16, 16, 16, 16, 16, 16, 16, 105 | 17, 17, 17, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18, 18, 18, 18, 19, 19, 19, 19, 106 | 19, 19, 19, 19, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 107 | 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 22, 22, 22, 22, 108 | 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 23, 23, 23, 23, 23, 23, 23, 23, 109 | 23, 23, 23, 23, 23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 110 | 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 111 | 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 112 | 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 26, 26, 26, 26, 26, 26, 26, 26, 113 | 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 114 | 26, 26, 26, 26, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 115 | 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 28 116 | }; 117 | 118 | local const int base_length[LENGTH_CODES] = { 119 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16, 20, 24, 28, 32, 40, 48, 56, 120 | 64, 80, 96, 112, 128, 160, 192, 224, 0 121 | }; 122 | 123 | local const int base_dist[D_CODES] = { 124 | 0, 1, 2, 3, 4, 6, 8, 12, 16, 24, 125 | 32, 48, 64, 96, 128, 192, 256, 384, 512, 768, 126 | 1024, 1536, 2048, 3072, 4096, 6144, 8192, 12288, 16384, 24576 127 | }; 128 | 129 | -------------------------------------------------------------------------------- /zlib/uncompr.c: -------------------------------------------------------------------------------- 1 | /* uncompr.c -- decompress a memory buffer 2 | * Copyright (C) 1995-2003, 2010 Jean-loup Gailly. 3 | * For conditions of distribution and use, see copyright notice in zlib.h 4 | */ 5 | 6 | /* @(#) $Id$ */ 7 | 8 | #define ZLIB_INTERNAL 9 | #include "zlib.h" 10 | 11 | /* =========================================================================== 12 | Decompresses the source buffer into the destination buffer. sourceLen is 13 | the byte length of the source buffer. Upon entry, destLen is the total 14 | size of the destination buffer, which must be large enough to hold the 15 | entire uncompressed data. (The size of the uncompressed data must have 16 | been saved previously by the compressor and transmitted to the decompressor 17 | by some mechanism outside the scope of this compression library.) 18 | Upon exit, destLen is the actual size of the compressed buffer. 19 | 20 | uncompress returns Z_OK if success, Z_MEM_ERROR if there was not 21 | enough memory, Z_BUF_ERROR if there was not enough room in the output 22 | buffer, or Z_DATA_ERROR if the input data was corrupted. 23 | */ 24 | int ZEXPORT uncompress (dest, destLen, source, sourceLen) 25 | Bytef *dest; 26 | uLongf *destLen; 27 | const Bytef *source; 28 | uLong sourceLen; 29 | { 30 | z_stream stream; 31 | int err; 32 | 33 | stream.next_in = (z_const Bytef *)source; 34 | stream.avail_in = (uInt)sourceLen; 35 | /* Check for source > 64K on 16-bit machine: */ 36 | if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR; 37 | 38 | stream.next_out = dest; 39 | stream.avail_out = (uInt)*destLen; 40 | if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR; 41 | 42 | stream.zalloc = (alloc_func)0; 43 | stream.zfree = (free_func)0; 44 | 45 | err = inflateInit(&stream); 46 | if (err != Z_OK) return err; 47 | 48 | err = inflate(&stream, Z_FINISH); 49 | if (err != Z_STREAM_END) { 50 | inflateEnd(&stream); 51 | if (err == Z_NEED_DICT || (err == Z_BUF_ERROR && stream.avail_in == 0)) 52 | return Z_DATA_ERROR; 53 | return err; 54 | } 55 | *destLen = stream.total_out; 56 | 57 | err = inflateEnd(&stream); 58 | return err; 59 | } 60 | -------------------------------------------------------------------------------- /zlib/zconf.h.cmakein: -------------------------------------------------------------------------------- 1 | /* zconf.h -- configuration of the zlib compression library 2 | * Copyright (C) 1995-2013 Jean-loup Gailly. 3 | * For conditions of distribution and use, see copyright notice in zlib.h 4 | */ 5 | 6 | /* @(#) $Id$ */ 7 | 8 | #ifndef ZCONF_H 9 | #define ZCONF_H 10 | #cmakedefine Z_PREFIX 11 | #cmakedefine Z_HAVE_UNISTD_H 12 | 13 | /* 14 | * If you *really* need a unique prefix for all types and library functions, 15 | * compile with -DZ_PREFIX. The "standard" zlib should be compiled without it. 16 | * Even better than compiling with -DZ_PREFIX would be to use configure to set 17 | * this permanently in zconf.h using "./configure --zprefix". 18 | */ 19 | #ifdef Z_PREFIX /* may be set to #if 1 by ./configure */ 20 | # define Z_PREFIX_SET 21 | 22 | /* all linked symbols */ 23 | # define _dist_code z__dist_code 24 | # define _length_code z__length_code 25 | # define _tr_align z__tr_align 26 | # define _tr_flush_bits z__tr_flush_bits 27 | # define _tr_flush_block z__tr_flush_block 28 | # define _tr_init z__tr_init 29 | # define _tr_stored_block z__tr_stored_block 30 | # define _tr_tally z__tr_tally 31 | # define adler32 z_adler32 32 | # define adler32_combine z_adler32_combine 33 | # define adler32_combine64 z_adler32_combine64 34 | # ifndef Z_SOLO 35 | # define compress z_compress 36 | # define compress2 z_compress2 37 | # define compressBound z_compressBound 38 | # endif 39 | # define crc32 z_crc32 40 | # define crc32_combine z_crc32_combine 41 | # define crc32_combine64 z_crc32_combine64 42 | # define deflate z_deflate 43 | # define deflateBound z_deflateBound 44 | # define deflateCopy z_deflateCopy 45 | # define deflateEnd z_deflateEnd 46 | # define deflateInit2_ z_deflateInit2_ 47 | # define deflateInit_ z_deflateInit_ 48 | # define deflateParams z_deflateParams 49 | # define deflatePending z_deflatePending 50 | # define deflatePrime z_deflatePrime 51 | # define deflateReset z_deflateReset 52 | # define deflateResetKeep z_deflateResetKeep 53 | # define deflateSetDictionary z_deflateSetDictionary 54 | # define deflateSetHeader z_deflateSetHeader 55 | # define deflateTune z_deflateTune 56 | # define deflate_copyright z_deflate_copyright 57 | # define get_crc_table z_get_crc_table 58 | # ifndef Z_SOLO 59 | # define gz_error z_gz_error 60 | # define gz_intmax z_gz_intmax 61 | # define gz_strwinerror z_gz_strwinerror 62 | # define gzbuffer z_gzbuffer 63 | # define gzclearerr z_gzclearerr 64 | # define gzclose z_gzclose 65 | # define gzclose_r z_gzclose_r 66 | # define gzclose_w z_gzclose_w 67 | # define gzdirect z_gzdirect 68 | # define gzdopen z_gzdopen 69 | # define gzeof z_gzeof 70 | # define gzerror z_gzerror 71 | # define gzflush z_gzflush 72 | # define gzgetc z_gzgetc 73 | # define gzgetc_ z_gzgetc_ 74 | # define gzgets z_gzgets 75 | # define gzoffset z_gzoffset 76 | # define gzoffset64 z_gzoffset64 77 | # define gzopen z_gzopen 78 | # define gzopen64 z_gzopen64 79 | # ifdef _WIN32 80 | # define gzopen_w z_gzopen_w 81 | # endif 82 | # define gzprintf z_gzprintf 83 | # define gzvprintf z_gzvprintf 84 | # define gzputc z_gzputc 85 | # define gzputs z_gzputs 86 | # define gzread z_gzread 87 | # define gzrewind z_gzrewind 88 | # define gzseek z_gzseek 89 | # define gzseek64 z_gzseek64 90 | # define gzsetparams z_gzsetparams 91 | # define gztell z_gztell 92 | # define gztell64 z_gztell64 93 | # define gzungetc z_gzungetc 94 | # define gzwrite z_gzwrite 95 | # endif 96 | # define inflate z_inflate 97 | # define inflateBack z_inflateBack 98 | # define inflateBackEnd z_inflateBackEnd 99 | # define inflateBackInit_ z_inflateBackInit_ 100 | # define inflateCopy z_inflateCopy 101 | # define inflateEnd z_inflateEnd 102 | # define inflateGetHeader z_inflateGetHeader 103 | # define inflateInit2_ z_inflateInit2_ 104 | # define inflateInit_ z_inflateInit_ 105 | # define inflateMark z_inflateMark 106 | # define inflatePrime z_inflatePrime 107 | # define inflateReset z_inflateReset 108 | # define inflateReset2 z_inflateReset2 109 | # define inflateSetDictionary z_inflateSetDictionary 110 | # define inflateGetDictionary z_inflateGetDictionary 111 | # define inflateSync z_inflateSync 112 | # define inflateSyncPoint z_inflateSyncPoint 113 | # define inflateUndermine z_inflateUndermine 114 | # define inflateResetKeep z_inflateResetKeep 115 | # define inflate_copyright z_inflate_copyright 116 | # define inflate_fast z_inflate_fast 117 | # define inflate_table z_inflate_table 118 | # ifndef Z_SOLO 119 | # define uncompress z_uncompress 120 | # endif 121 | # define zError z_zError 122 | # ifndef Z_SOLO 123 | # define zcalloc z_zcalloc 124 | # define zcfree z_zcfree 125 | # endif 126 | # define zlibCompileFlags z_zlibCompileFlags 127 | # define zlibVersion z_zlibVersion 128 | 129 | /* all zlib typedefs in zlib.h and zconf.h */ 130 | # define Byte z_Byte 131 | # define Bytef z_Bytef 132 | # define alloc_func z_alloc_func 133 | # define charf z_charf 134 | # define free_func z_free_func 135 | # ifndef Z_SOLO 136 | # define gzFile z_gzFile 137 | # endif 138 | # define gz_header z_gz_header 139 | # define gz_headerp z_gz_headerp 140 | # define in_func z_in_func 141 | # define intf z_intf 142 | # define out_func z_out_func 143 | # define uInt z_uInt 144 | # define uIntf z_uIntf 145 | # define uLong z_uLong 146 | # define uLongf z_uLongf 147 | # define voidp z_voidp 148 | # define voidpc z_voidpc 149 | # define voidpf z_voidpf 150 | 151 | /* all zlib structs in zlib.h and zconf.h */ 152 | # define gz_header_s z_gz_header_s 153 | # define internal_state z_internal_state 154 | 155 | #endif 156 | 157 | #if defined(__MSDOS__) && !defined(MSDOS) 158 | # define MSDOS 159 | #endif 160 | #if (defined(OS_2) || defined(__OS2__)) && !defined(OS2) 161 | # define OS2 162 | #endif 163 | #if defined(_WINDOWS) && !defined(WINDOWS) 164 | # define WINDOWS 165 | #endif 166 | #if defined(_WIN32) || defined(_WIN32_WCE) || defined(__WIN32__) 167 | # ifndef WIN32 168 | # define WIN32 169 | # endif 170 | #endif 171 | #if (defined(MSDOS) || defined(OS2) || defined(WINDOWS)) && !defined(WIN32) 172 | # if !defined(__GNUC__) && !defined(__FLAT__) && !defined(__386__) 173 | # ifndef SYS16BIT 174 | # define SYS16BIT 175 | # endif 176 | # endif 177 | #endif 178 | 179 | /* 180 | * Compile with -DMAXSEG_64K if the alloc function cannot allocate more 181 | * than 64k bytes at a time (needed on systems with 16-bit int). 182 | */ 183 | #ifdef SYS16BIT 184 | # define MAXSEG_64K 185 | #endif 186 | #ifdef MSDOS 187 | # define UNALIGNED_OK 188 | #endif 189 | 190 | #ifdef __STDC_VERSION__ 191 | # ifndef STDC 192 | # define STDC 193 | # endif 194 | # if __STDC_VERSION__ >= 199901L 195 | # ifndef STDC99 196 | # define STDC99 197 | # endif 198 | # endif 199 | #endif 200 | #if !defined(STDC) && (defined(__STDC__) || defined(__cplusplus)) 201 | # define STDC 202 | #endif 203 | #if !defined(STDC) && (defined(__GNUC__) || defined(__BORLANDC__)) 204 | # define STDC 205 | #endif 206 | #if !defined(STDC) && (defined(MSDOS) || defined(WINDOWS) || defined(WIN32)) 207 | # define STDC 208 | #endif 209 | #if !defined(STDC) && (defined(OS2) || defined(__HOS_AIX__)) 210 | # define STDC 211 | #endif 212 | 213 | #if defined(__OS400__) && !defined(STDC) /* iSeries (formerly AS/400). */ 214 | # define STDC 215 | #endif 216 | 217 | #ifndef STDC 218 | # ifndef const /* cannot use !defined(STDC) && !defined(const) on Mac */ 219 | # define const /* note: need a more gentle solution here */ 220 | # endif 221 | #endif 222 | 223 | #if defined(ZLIB_CONST) && !defined(z_const) 224 | # define z_const const 225 | #else 226 | # define z_const 227 | #endif 228 | 229 | /* Some Mac compilers merge all .h files incorrectly: */ 230 | #if defined(__MWERKS__)||defined(applec)||defined(THINK_C)||defined(__SC__) 231 | # define NO_DUMMY_DECL 232 | #endif 233 | 234 | /* Maximum value for memLevel in deflateInit2 */ 235 | #ifndef MAX_MEM_LEVEL 236 | # ifdef MAXSEG_64K 237 | # define MAX_MEM_LEVEL 8 238 | # else 239 | # define MAX_MEM_LEVEL 9 240 | # endif 241 | #endif 242 | 243 | /* Maximum value for windowBits in deflateInit2 and inflateInit2. 244 | * WARNING: reducing MAX_WBITS makes minigzip unable to extract .gz files 245 | * created by gzip. (Files created by minigzip can still be extracted by 246 | * gzip.) 247 | */ 248 | #ifndef MAX_WBITS 249 | # define MAX_WBITS 15 /* 32K LZ77 window */ 250 | #endif 251 | 252 | /* The memory requirements for deflate are (in bytes): 253 | (1 << (windowBits+2)) + (1 << (memLevel+9)) 254 | that is: 128K for windowBits=15 + 128K for memLevel = 8 (default values) 255 | plus a few kilobytes for small objects. For example, if you want to reduce 256 | the default memory requirements from 256K to 128K, compile with 257 | make CFLAGS="-O -DMAX_WBITS=14 -DMAX_MEM_LEVEL=7" 258 | Of course this will generally degrade compression (there's no free lunch). 259 | 260 | The memory requirements for inflate are (in bytes) 1 << windowBits 261 | that is, 32K for windowBits=15 (default value) plus a few kilobytes 262 | for small objects. 263 | */ 264 | 265 | /* Type declarations */ 266 | 267 | #ifndef OF /* function prototypes */ 268 | # ifdef STDC 269 | # define OF(args) args 270 | # else 271 | # define OF(args) () 272 | # endif 273 | #endif 274 | 275 | #ifndef Z_ARG /* function prototypes for stdarg */ 276 | # if defined(STDC) || defined(Z_HAVE_STDARG_H) 277 | # define Z_ARG(args) args 278 | # else 279 | # define Z_ARG(args) () 280 | # endif 281 | #endif 282 | 283 | /* The following definitions for FAR are needed only for MSDOS mixed 284 | * model programming (small or medium model with some far allocations). 285 | * This was tested only with MSC; for other MSDOS compilers you may have 286 | * to define NO_MEMCPY in zutil.h. If you don't need the mixed model, 287 | * just define FAR to be empty. 288 | */ 289 | #ifdef SYS16BIT 290 | # if defined(M_I86SM) || defined(M_I86MM) 291 | /* MSC small or medium model */ 292 | # define SMALL_MEDIUM 293 | # ifdef _MSC_VER 294 | # define FAR _far 295 | # else 296 | # define FAR far 297 | # endif 298 | # endif 299 | # if (defined(__SMALL__) || defined(__MEDIUM__)) 300 | /* Turbo C small or medium model */ 301 | # define SMALL_MEDIUM 302 | # ifdef __BORLANDC__ 303 | # define FAR _far 304 | # else 305 | # define FAR far 306 | # endif 307 | # endif 308 | #endif 309 | 310 | #if defined(WINDOWS) || defined(WIN32) 311 | /* If building or using zlib as a DLL, define ZLIB_DLL. 312 | * This is not mandatory, but it offers a little performance increase. 313 | */ 314 | # ifdef ZLIB_DLL 315 | # if defined(WIN32) && (!defined(__BORLANDC__) || (__BORLANDC__ >= 0x500)) 316 | # ifdef ZLIB_INTERNAL 317 | # define ZEXTERN extern __declspec(dllexport) 318 | # else 319 | # define ZEXTERN extern __declspec(dllimport) 320 | # endif 321 | # endif 322 | # endif /* ZLIB_DLL */ 323 | /* If building or using zlib with the WINAPI/WINAPIV calling convention, 324 | * define ZLIB_WINAPI. 325 | * Caution: the standard ZLIB1.DLL is NOT compiled using ZLIB_WINAPI. 326 | */ 327 | # ifdef ZLIB_WINAPI 328 | # ifdef FAR 329 | # undef FAR 330 | # endif 331 | # include 332 | /* No need for _export, use ZLIB.DEF instead. */ 333 | /* For complete Windows compatibility, use WINAPI, not __stdcall. */ 334 | # define ZEXPORT WINAPI 335 | # ifdef WIN32 336 | # define ZEXPORTVA WINAPIV 337 | # else 338 | # define ZEXPORTVA FAR CDECL 339 | # endif 340 | # endif 341 | #endif 342 | 343 | #if defined (__BEOS__) 344 | # ifdef ZLIB_DLL 345 | # ifdef ZLIB_INTERNAL 346 | # define ZEXPORT __declspec(dllexport) 347 | # define ZEXPORTVA __declspec(dllexport) 348 | # else 349 | # define ZEXPORT __declspec(dllimport) 350 | # define ZEXPORTVA __declspec(dllimport) 351 | # endif 352 | # endif 353 | #endif 354 | 355 | #ifndef ZEXTERN 356 | # define ZEXTERN extern 357 | #endif 358 | #ifndef ZEXPORT 359 | # define ZEXPORT 360 | #endif 361 | #ifndef ZEXPORTVA 362 | # define ZEXPORTVA 363 | #endif 364 | 365 | #ifndef FAR 366 | # define FAR 367 | #endif 368 | 369 | #if !defined(__MACTYPES__) 370 | typedef unsigned char Byte; /* 8 bits */ 371 | #endif 372 | typedef unsigned int uInt; /* 16 bits or more */ 373 | typedef unsigned long uLong; /* 32 bits or more */ 374 | 375 | #ifdef SMALL_MEDIUM 376 | /* Borland C/C++ and some old MSC versions ignore FAR inside typedef */ 377 | # define Bytef Byte FAR 378 | #else 379 | typedef Byte FAR Bytef; 380 | #endif 381 | typedef char FAR charf; 382 | typedef int FAR intf; 383 | typedef uInt FAR uIntf; 384 | typedef uLong FAR uLongf; 385 | 386 | #ifdef STDC 387 | typedef void const *voidpc; 388 | typedef void FAR *voidpf; 389 | typedef void *voidp; 390 | #else 391 | typedef Byte const *voidpc; 392 | typedef Byte FAR *voidpf; 393 | typedef Byte *voidp; 394 | #endif 395 | 396 | #if !defined(Z_U4) && !defined(Z_SOLO) && defined(STDC) 397 | # include 398 | # if (UINT_MAX == 0xffffffffUL) 399 | # define Z_U4 unsigned 400 | # elif (ULONG_MAX == 0xffffffffUL) 401 | # define Z_U4 unsigned long 402 | # elif (USHRT_MAX == 0xffffffffUL) 403 | # define Z_U4 unsigned short 404 | # endif 405 | #endif 406 | 407 | #ifdef Z_U4 408 | typedef Z_U4 z_crc_t; 409 | #else 410 | typedef unsigned long z_crc_t; 411 | #endif 412 | 413 | #ifdef HAVE_UNISTD_H /* may be set to #if 1 by ./configure */ 414 | # define Z_HAVE_UNISTD_H 415 | #endif 416 | 417 | #ifdef HAVE_STDARG_H /* may be set to #if 1 by ./configure */ 418 | # define Z_HAVE_STDARG_H 419 | #endif 420 | 421 | #ifdef STDC 422 | # ifndef Z_SOLO 423 | # include /* for off_t */ 424 | # endif 425 | #endif 426 | 427 | #if defined(STDC) || defined(Z_HAVE_STDARG_H) 428 | # ifndef Z_SOLO 429 | # include /* for va_list */ 430 | # endif 431 | #endif 432 | 433 | #ifdef _WIN32 434 | # ifndef Z_SOLO 435 | # include /* for wchar_t */ 436 | # endif 437 | #endif 438 | 439 | /* a little trick to accommodate both "#define _LARGEFILE64_SOURCE" and 440 | * "#define _LARGEFILE64_SOURCE 1" as requesting 64-bit operations, (even 441 | * though the former does not conform to the LFS document), but considering 442 | * both "#undef _LARGEFILE64_SOURCE" and "#define _LARGEFILE64_SOURCE 0" as 443 | * equivalently requesting no 64-bit operations 444 | */ 445 | #if defined(_LARGEFILE64_SOURCE) && -_LARGEFILE64_SOURCE - -1 == 1 446 | # undef _LARGEFILE64_SOURCE 447 | #endif 448 | 449 | #if defined(__WATCOMC__) && !defined(Z_HAVE_UNISTD_H) 450 | # define Z_HAVE_UNISTD_H 451 | #endif 452 | #ifndef Z_SOLO 453 | # if defined(Z_HAVE_UNISTD_H) || defined(_LARGEFILE64_SOURCE) 454 | # include /* for SEEK_*, off_t, and _LFS64_LARGEFILE */ 455 | # ifdef VMS 456 | # include /* for off_t */ 457 | # endif 458 | # ifndef z_off_t 459 | # define z_off_t off_t 460 | # endif 461 | # endif 462 | #endif 463 | 464 | #if defined(_LFS64_LARGEFILE) && _LFS64_LARGEFILE-0 465 | # define Z_LFS64 466 | #endif 467 | 468 | #if defined(_LARGEFILE64_SOURCE) && defined(Z_LFS64) 469 | # define Z_LARGE64 470 | #endif 471 | 472 | #if defined(_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS-0 == 64 && defined(Z_LFS64) 473 | # define Z_WANT64 474 | #endif 475 | 476 | #if !defined(SEEK_SET) && !defined(Z_SOLO) 477 | # define SEEK_SET 0 /* Seek from beginning of file. */ 478 | # define SEEK_CUR 1 /* Seek from current position. */ 479 | # define SEEK_END 2 /* Set file pointer to EOF plus "offset" */ 480 | #endif 481 | 482 | #ifndef z_off_t 483 | # define z_off_t long 484 | #endif 485 | 486 | #if !defined(_WIN32) && defined(Z_LARGE64) 487 | # define z_off64_t off64_t 488 | #else 489 | # if defined(_WIN32) && !defined(__GNUC__) && !defined(Z_SOLO) 490 | # define z_off64_t __int64 491 | # else 492 | # define z_off64_t z_off_t 493 | # endif 494 | #endif 495 | 496 | /* MVS linker does not support external names larger than 8 bytes */ 497 | #if defined(__MVS__) 498 | #pragma map(deflateInit_,"DEIN") 499 | #pragma map(deflateInit2_,"DEIN2") 500 | #pragma map(deflateEnd,"DEEND") 501 | #pragma map(deflateBound,"DEBND") 502 | #pragma map(inflateInit_,"ININ") 503 | #pragma map(inflateInit2_,"ININ2") 504 | #pragma map(inflateEnd,"INEND") 505 | #pragma map(inflateSync,"INSY") 506 | #pragma map(inflateSetDictionary,"INSEDI") 507 | #pragma map(compressBound,"CMBND") 508 | #pragma map(inflate_table,"INTABL") 509 | #pragma map(inflate_fast,"INFA") 510 | #pragma map(inflate_copyright,"INCOPY") 511 | #endif 512 | 513 | #endif /* ZCONF_H */ 514 | -------------------------------------------------------------------------------- /zlib/zlib.def: -------------------------------------------------------------------------------- 1 | ; zlib data compression library 2 | EXPORTS 3 | ; basic functions 4 | zlibVersion 5 | deflate 6 | deflateEnd 7 | inflate 8 | inflateEnd 9 | ; advanced functions 10 | deflateSetDictionary 11 | deflateCopy 12 | deflateReset 13 | deflateParams 14 | deflateTune 15 | deflateBound 16 | deflatePending 17 | deflatePrime 18 | deflateSetHeader 19 | inflateSetDictionary 20 | inflateGetDictionary 21 | inflateSync 22 | inflateCopy 23 | inflateReset 24 | inflateReset2 25 | inflatePrime 26 | inflateMark 27 | inflateGetHeader 28 | inflateBack 29 | inflateBackEnd 30 | zlibCompileFlags 31 | ; utility functions 32 | compress 33 | compress2 34 | compressBound 35 | uncompress 36 | gzopen 37 | gzdopen 38 | gzbuffer 39 | gzsetparams 40 | gzread 41 | gzwrite 42 | gzprintf 43 | gzvprintf 44 | gzputs 45 | gzgets 46 | gzputc 47 | gzgetc 48 | gzungetc 49 | gzflush 50 | gzseek 51 | gzrewind 52 | gztell 53 | gzoffset 54 | gzeof 55 | gzdirect 56 | gzclose 57 | gzclose_r 58 | gzclose_w 59 | gzerror 60 | gzclearerr 61 | ; large file functions 62 | gzopen64 63 | gzseek64 64 | gztell64 65 | gzoffset64 66 | adler32_combine64 67 | crc32_combine64 68 | ; checksum functions 69 | adler32 70 | crc32 71 | adler32_combine 72 | crc32_combine 73 | ; various hacks, don't look :) 74 | deflateInit_ 75 | deflateInit2_ 76 | inflateInit_ 77 | inflateInit2_ 78 | inflateBackInit_ 79 | gzgetc_ 80 | zError 81 | inflateSyncPoint 82 | get_crc_table 83 | inflateUndermine 84 | inflateResetKeep 85 | deflateResetKeep 86 | gzopen_w 87 | -------------------------------------------------------------------------------- /zlib/zlib1.rc: -------------------------------------------------------------------------------- 1 | #include 2 | #include "../zlib.h" 3 | 4 | #ifdef GCC_WINDRES 5 | VS_VERSION_INFO VERSIONINFO 6 | #else 7 | VS_VERSION_INFO VERSIONINFO MOVEABLE IMPURE LOADONCALL DISCARDABLE 8 | #endif 9 | FILEVERSION ZLIB_VER_MAJOR,ZLIB_VER_MINOR,ZLIB_VER_REVISION,0 10 | PRODUCTVERSION ZLIB_VER_MAJOR,ZLIB_VER_MINOR,ZLIB_VER_REVISION,0 11 | FILEFLAGSMASK VS_FFI_FILEFLAGSMASK 12 | #ifdef _DEBUG 13 | FILEFLAGS 1 14 | #else 15 | FILEFLAGS 0 16 | #endif 17 | FILEOS VOS__WINDOWS32 18 | FILETYPE VFT_DLL 19 | FILESUBTYPE 0 // not used 20 | BEGIN 21 | BLOCK "StringFileInfo" 22 | BEGIN 23 | BLOCK "040904E4" 24 | //language ID = U.S. English, char set = Windows, Multilingual 25 | BEGIN 26 | VALUE "FileDescription", "zlib data compression library\0" 27 | VALUE "FileVersion", ZLIB_VERSION "\0" 28 | VALUE "InternalName", "zlib1.dll\0" 29 | VALUE "LegalCopyright", "(C) 1995-2013 Jean-loup Gailly & Mark Adler\0" 30 | VALUE "OriginalFilename", "zlib1.dll\0" 31 | VALUE "ProductName", "zlib\0" 32 | VALUE "ProductVersion", ZLIB_VERSION "\0" 33 | VALUE "Comments", "For more information visit http://www.zlib.net/\0" 34 | END 35 | END 36 | BLOCK "VarFileInfo" 37 | BEGIN 38 | VALUE "Translation", 0x0409, 1252 39 | END 40 | END 41 | -------------------------------------------------------------------------------- /zlib/zutil.c: -------------------------------------------------------------------------------- 1 | /* zutil.c -- target dependent utility functions for the compression library 2 | * Copyright (C) 1995-2005, 2010, 2011, 2012 Jean-loup Gailly. 3 | * For conditions of distribution and use, see copyright notice in zlib.h 4 | */ 5 | 6 | /* @(#) $Id$ */ 7 | 8 | #include "zutil.h" 9 | #ifndef Z_SOLO 10 | # include "gzguts.h" 11 | #endif 12 | 13 | #ifndef NO_DUMMY_DECL 14 | struct internal_state {int dummy;}; /* for buggy compilers */ 15 | #endif 16 | 17 | z_const char * const z_errmsg[10] = { 18 | "need dictionary", /* Z_NEED_DICT 2 */ 19 | "stream end", /* Z_STREAM_END 1 */ 20 | "", /* Z_OK 0 */ 21 | "file error", /* Z_ERRNO (-1) */ 22 | "stream error", /* Z_STREAM_ERROR (-2) */ 23 | "data error", /* Z_DATA_ERROR (-3) */ 24 | "insufficient memory", /* Z_MEM_ERROR (-4) */ 25 | "buffer error", /* Z_BUF_ERROR (-5) */ 26 | "incompatible version",/* Z_VERSION_ERROR (-6) */ 27 | ""}; 28 | 29 | 30 | const char * ZEXPORT zlibVersion() 31 | { 32 | return ZLIB_VERSION; 33 | } 34 | 35 | uLong ZEXPORT zlibCompileFlags() 36 | { 37 | uLong flags; 38 | 39 | flags = 0; 40 | switch ((int)(sizeof(uInt))) { 41 | case 2: break; 42 | case 4: flags += 1; break; 43 | case 8: flags += 2; break; 44 | default: flags += 3; 45 | } 46 | switch ((int)(sizeof(uLong))) { 47 | case 2: break; 48 | case 4: flags += 1 << 2; break; 49 | case 8: flags += 2 << 2; break; 50 | default: flags += 3 << 2; 51 | } 52 | switch ((int)(sizeof(voidpf))) { 53 | case 2: break; 54 | case 4: flags += 1 << 4; break; 55 | case 8: flags += 2 << 4; break; 56 | default: flags += 3 << 4; 57 | } 58 | switch ((int)(sizeof(z_off_t))) { 59 | case 2: break; 60 | case 4: flags += 1 << 6; break; 61 | case 8: flags += 2 << 6; break; 62 | default: flags += 3 << 6; 63 | } 64 | #ifdef DEBUG 65 | flags += 1 << 8; 66 | #endif 67 | #if defined(ASMV) || defined(ASMINF) 68 | flags += 1 << 9; 69 | #endif 70 | #ifdef ZLIB_WINAPI 71 | flags += 1 << 10; 72 | #endif 73 | #ifdef BUILDFIXED 74 | flags += 1 << 12; 75 | #endif 76 | #ifdef DYNAMIC_CRC_TABLE 77 | flags += 1 << 13; 78 | #endif 79 | #ifdef NO_GZCOMPRESS 80 | flags += 1L << 16; 81 | #endif 82 | #ifdef NO_GZIP 83 | flags += 1L << 17; 84 | #endif 85 | #ifdef PKZIP_BUG_WORKAROUND 86 | flags += 1L << 20; 87 | #endif 88 | #ifdef FASTEST 89 | flags += 1L << 21; 90 | #endif 91 | #if defined(STDC) || defined(Z_HAVE_STDARG_H) 92 | # ifdef NO_vsnprintf 93 | flags += 1L << 25; 94 | # ifdef HAS_vsprintf_void 95 | flags += 1L << 26; 96 | # endif 97 | # else 98 | # ifdef HAS_vsnprintf_void 99 | flags += 1L << 26; 100 | # endif 101 | # endif 102 | #else 103 | flags += 1L << 24; 104 | # ifdef NO_snprintf 105 | flags += 1L << 25; 106 | # ifdef HAS_sprintf_void 107 | flags += 1L << 26; 108 | # endif 109 | # else 110 | # ifdef HAS_snprintf_void 111 | flags += 1L << 26; 112 | # endif 113 | # endif 114 | #endif 115 | return flags; 116 | } 117 | 118 | #ifdef DEBUG 119 | 120 | # ifndef verbose 121 | # define verbose 0 122 | # endif 123 | int ZLIB_INTERNAL z_verbose = verbose; 124 | 125 | void ZLIB_INTERNAL z_error (m) 126 | char *m; 127 | { 128 | fprintf(stderr, "%s\n", m); 129 | exit(1); 130 | } 131 | #endif 132 | 133 | /* exported to allow conversion of error code to string for compress() and 134 | * uncompress() 135 | */ 136 | const char * ZEXPORT zError(err) 137 | int err; 138 | { 139 | return ERR_MSG(err); 140 | } 141 | 142 | #if defined(_WIN32_WCE) 143 | /* The Microsoft C Run-Time Library for Windows CE doesn't have 144 | * errno. We define it as a global variable to simplify porting. 145 | * Its value is always 0 and should not be used. 146 | */ 147 | int errno = 0; 148 | #endif 149 | 150 | #ifndef HAVE_MEMCPY 151 | 152 | void ZLIB_INTERNAL zmemcpy(dest, source, len) 153 | Bytef* dest; 154 | const Bytef* source; 155 | uInt len; 156 | { 157 | if (len == 0) return; 158 | do { 159 | *dest++ = *source++; /* ??? to be unrolled */ 160 | } while (--len != 0); 161 | } 162 | 163 | int ZLIB_INTERNAL zmemcmp(s1, s2, len) 164 | const Bytef* s1; 165 | const Bytef* s2; 166 | uInt len; 167 | { 168 | uInt j; 169 | 170 | for (j = 0; j < len; j++) { 171 | if (s1[j] != s2[j]) return 2*(s1[j] > s2[j])-1; 172 | } 173 | return 0; 174 | } 175 | 176 | void ZLIB_INTERNAL zmemzero(dest, len) 177 | Bytef* dest; 178 | uInt len; 179 | { 180 | if (len == 0) return; 181 | do { 182 | *dest++ = 0; /* ??? to be unrolled */ 183 | } while (--len != 0); 184 | } 185 | #endif 186 | 187 | #ifndef Z_SOLO 188 | 189 | #ifdef SYS16BIT 190 | 191 | #ifdef __TURBOC__ 192 | /* Turbo C in 16-bit mode */ 193 | 194 | # define MY_ZCALLOC 195 | 196 | /* Turbo C malloc() does not allow dynamic allocation of 64K bytes 197 | * and farmalloc(64K) returns a pointer with an offset of 8, so we 198 | * must fix the pointer. Warning: the pointer must be put back to its 199 | * original form in order to free it, use zcfree(). 200 | */ 201 | 202 | #define MAX_PTR 10 203 | /* 10*64K = 640K */ 204 | 205 | local int next_ptr = 0; 206 | 207 | typedef struct ptr_table_s { 208 | voidpf org_ptr; 209 | voidpf new_ptr; 210 | } ptr_table; 211 | 212 | local ptr_table table[MAX_PTR]; 213 | /* This table is used to remember the original form of pointers 214 | * to large buffers (64K). Such pointers are normalized with a zero offset. 215 | * Since MSDOS is not a preemptive multitasking OS, this table is not 216 | * protected from concurrent access. This hack doesn't work anyway on 217 | * a protected system like OS/2. Use Microsoft C instead. 218 | */ 219 | 220 | voidpf ZLIB_INTERNAL zcalloc (voidpf opaque, unsigned items, unsigned size) 221 | { 222 | voidpf buf = opaque; /* just to make some compilers happy */ 223 | ulg bsize = (ulg)items*size; 224 | 225 | /* If we allocate less than 65520 bytes, we assume that farmalloc 226 | * will return a usable pointer which doesn't have to be normalized. 227 | */ 228 | if (bsize < 65520L) { 229 | buf = farmalloc(bsize); 230 | if (*(ush*)&buf != 0) return buf; 231 | } else { 232 | buf = farmalloc(bsize + 16L); 233 | } 234 | if (buf == NULL || next_ptr >= MAX_PTR) return NULL; 235 | table[next_ptr].org_ptr = buf; 236 | 237 | /* Normalize the pointer to seg:0 */ 238 | *((ush*)&buf+1) += ((ush)((uch*)buf-0) + 15) >> 4; 239 | *(ush*)&buf = 0; 240 | table[next_ptr++].new_ptr = buf; 241 | return buf; 242 | } 243 | 244 | void ZLIB_INTERNAL zcfree (voidpf opaque, voidpf ptr) 245 | { 246 | int n; 247 | if (*(ush*)&ptr != 0) { /* object < 64K */ 248 | farfree(ptr); 249 | return; 250 | } 251 | /* Find the original pointer */ 252 | for (n = 0; n < next_ptr; n++) { 253 | if (ptr != table[n].new_ptr) continue; 254 | 255 | farfree(table[n].org_ptr); 256 | while (++n < next_ptr) { 257 | table[n-1] = table[n]; 258 | } 259 | next_ptr--; 260 | return; 261 | } 262 | ptr = opaque; /* just to make some compilers happy */ 263 | Assert(0, "zcfree: ptr not found"); 264 | } 265 | 266 | #endif /* __TURBOC__ */ 267 | 268 | 269 | #ifdef M_I86 270 | /* Microsoft C in 16-bit mode */ 271 | 272 | # define MY_ZCALLOC 273 | 274 | #if (!defined(_MSC_VER) || (_MSC_VER <= 600)) 275 | # define _halloc halloc 276 | # define _hfree hfree 277 | #endif 278 | 279 | voidpf ZLIB_INTERNAL zcalloc (voidpf opaque, uInt items, uInt size) 280 | { 281 | if (opaque) opaque = 0; /* to make compiler happy */ 282 | return _halloc((long)items, size); 283 | } 284 | 285 | void ZLIB_INTERNAL zcfree (voidpf opaque, voidpf ptr) 286 | { 287 | if (opaque) opaque = 0; /* to make compiler happy */ 288 | _hfree(ptr); 289 | } 290 | 291 | #endif /* M_I86 */ 292 | 293 | #endif /* SYS16BIT */ 294 | 295 | 296 | #ifndef MY_ZCALLOC /* Any system without a special alloc function */ 297 | 298 | #ifndef STDC 299 | extern voidp malloc OF((uInt size)); 300 | extern voidp calloc OF((uInt items, uInt size)); 301 | extern void free OF((voidpf ptr)); 302 | #endif 303 | 304 | voidpf ZLIB_INTERNAL zcalloc (opaque, items, size) 305 | voidpf opaque; 306 | unsigned items; 307 | unsigned size; 308 | { 309 | if (opaque) items += size - size; /* make compiler happy */ 310 | return sizeof(uInt) > 2 ? (voidpf)malloc(items * size) : 311 | (voidpf)calloc(items, size); 312 | } 313 | 314 | void ZLIB_INTERNAL zcfree (opaque, ptr) 315 | voidpf opaque; 316 | voidpf ptr; 317 | { 318 | free(ptr); 319 | if (opaque) return; /* make compiler happy */ 320 | } 321 | 322 | #endif /* MY_ZCALLOC */ 323 | 324 | #endif /* !Z_SOLO */ 325 | -------------------------------------------------------------------------------- /zlib/zutil.h: -------------------------------------------------------------------------------- 1 | /* zutil.h -- internal interface and configuration of the compression library 2 | * Copyright (C) 1995-2013 Jean-loup Gailly. 3 | * For conditions of distribution and use, see copyright notice in zlib.h 4 | */ 5 | 6 | /* WARNING: this file should *not* be used by applications. It is 7 | part of the implementation of the compression library and is 8 | subject to change. Applications should only use zlib.h. 9 | */ 10 | 11 | /* @(#) $Id$ */ 12 | 13 | #ifndef ZUTIL_H 14 | #define ZUTIL_H 15 | 16 | #ifdef HAVE_HIDDEN 17 | # define ZLIB_INTERNAL __attribute__((visibility ("hidden"))) 18 | #else 19 | # define ZLIB_INTERNAL 20 | #endif 21 | 22 | #include "zlib.h" 23 | 24 | #if defined(STDC) && !defined(Z_SOLO) 25 | # if !(defined(_WIN32_WCE) && defined(_MSC_VER)) 26 | # include 27 | # endif 28 | # include 29 | # include 30 | #endif 31 | 32 | #ifdef Z_SOLO 33 | typedef long ptrdiff_t; /* guess -- will be caught if guess is wrong */ 34 | #endif 35 | 36 | #ifndef local 37 | # define local static 38 | #endif 39 | /* compile with -Dlocal if your debugger can't find static symbols */ 40 | 41 | typedef unsigned char uch; 42 | typedef uch FAR uchf; 43 | typedef unsigned short ush; 44 | typedef ush FAR ushf; 45 | typedef unsigned long ulg; 46 | 47 | extern z_const char * const z_errmsg[10]; /* indexed by 2-zlib_error */ 48 | /* (size given to avoid silly warnings with Visual C++) */ 49 | 50 | #define ERR_MSG(err) z_errmsg[Z_NEED_DICT-(err)] 51 | 52 | #define ERR_RETURN(strm,err) \ 53 | return (strm->msg = ERR_MSG(err), (err)) 54 | /* To be used only when the state is known to be valid */ 55 | 56 | /* common constants */ 57 | 58 | #ifndef DEF_WBITS 59 | # define DEF_WBITS MAX_WBITS 60 | #endif 61 | /* default windowBits for decompression. MAX_WBITS is for compression only */ 62 | 63 | #if MAX_MEM_LEVEL >= 8 64 | # define DEF_MEM_LEVEL 8 65 | #else 66 | # define DEF_MEM_LEVEL MAX_MEM_LEVEL 67 | #endif 68 | /* default memLevel */ 69 | 70 | #define STORED_BLOCK 0 71 | #define STATIC_TREES 1 72 | #define DYN_TREES 2 73 | /* The three kinds of block type */ 74 | 75 | #define MIN_MATCH 3 76 | #define MAX_MATCH 258 77 | /* The minimum and maximum match lengths */ 78 | 79 | #define PRESET_DICT 0x20 /* preset dictionary flag in zlib header */ 80 | 81 | /* target dependencies */ 82 | 83 | #if defined(MSDOS) || (defined(WINDOWS) && !defined(WIN32)) 84 | # define OS_CODE 0x00 85 | # ifndef Z_SOLO 86 | # if defined(__TURBOC__) || defined(__BORLANDC__) 87 | # if (__STDC__ == 1) && (defined(__LARGE__) || defined(__COMPACT__)) 88 | /* Allow compilation with ANSI keywords only enabled */ 89 | void _Cdecl farfree( void *block ); 90 | void *_Cdecl farmalloc( unsigned long nbytes ); 91 | # else 92 | # include 93 | # endif 94 | # else /* MSC or DJGPP */ 95 | # include 96 | # endif 97 | # endif 98 | #endif 99 | 100 | #ifdef AMIGA 101 | # define OS_CODE 0x01 102 | #endif 103 | 104 | #if defined(VAXC) || defined(VMS) 105 | # define OS_CODE 0x02 106 | # define F_OPEN(name, mode) \ 107 | fopen((name), (mode), "mbc=60", "ctx=stm", "rfm=fix", "mrs=512") 108 | #endif 109 | 110 | #if defined(ATARI) || defined(atarist) 111 | # define OS_CODE 0x05 112 | #endif 113 | 114 | #ifdef OS2 115 | # define OS_CODE 0x06 116 | # if defined(M_I86) && !defined(Z_SOLO) 117 | # include 118 | # endif 119 | #endif 120 | 121 | #if defined(MACOS) || defined(TARGET_OS_MAC) 122 | # define OS_CODE 0x07 123 | # ifndef Z_SOLO 124 | # if defined(__MWERKS__) && __dest_os != __be_os && __dest_os != __win32_os 125 | # include /* for fdopen */ 126 | # else 127 | # ifndef fdopen 128 | # define fdopen(fd,mode) NULL /* No fdopen() */ 129 | # endif 130 | # endif 131 | # endif 132 | #endif 133 | 134 | #ifdef TOPS20 135 | # define OS_CODE 0x0a 136 | #endif 137 | 138 | #ifdef WIN32 139 | # ifndef __CYGWIN__ /* Cygwin is Unix, not Win32 */ 140 | # define OS_CODE 0x0b 141 | # endif 142 | #endif 143 | 144 | #ifdef __50SERIES /* Prime/PRIMOS */ 145 | # define OS_CODE 0x0f 146 | #endif 147 | 148 | #if defined(_BEOS_) || defined(RISCOS) 149 | # define fdopen(fd,mode) NULL /* No fdopen() */ 150 | #endif 151 | 152 | #if (defined(_MSC_VER) && (_MSC_VER > 600)) && !defined __INTERIX 153 | # if defined(_WIN32_WCE) 154 | # define fdopen(fd,mode) NULL /* No fdopen() */ 155 | # ifndef _PTRDIFF_T_DEFINED 156 | typedef int ptrdiff_t; 157 | # define _PTRDIFF_T_DEFINED 158 | # endif 159 | # else 160 | # define fdopen(fd,type) _fdopen(fd,type) 161 | # endif 162 | #endif 163 | 164 | #if defined(__BORLANDC__) && !defined(MSDOS) 165 | #pragma warn -8004 166 | #pragma warn -8008 167 | #pragma warn -8066 168 | #endif 169 | 170 | /* provide prototypes for these when building zlib without LFS */ 171 | #if !defined(_WIN32) && \ 172 | (!defined(_LARGEFILE64_SOURCE) || _LFS64_LARGEFILE-0 == 0) 173 | ZEXTERN uLong ZEXPORT adler32_combine64 OF((uLong, uLong, z_off_t)); 174 | ZEXTERN uLong ZEXPORT crc32_combine64 OF((uLong, uLong, z_off_t)); 175 | #endif 176 | 177 | /* common defaults */ 178 | 179 | #ifndef OS_CODE 180 | # define OS_CODE 0x03 /* assume Unix */ 181 | #endif 182 | 183 | #ifndef F_OPEN 184 | # define F_OPEN(name, mode) fopen((name), (mode)) 185 | #endif 186 | 187 | /* functions */ 188 | 189 | #if defined(pyr) || defined(Z_SOLO) 190 | # define NO_MEMCPY 191 | #endif 192 | #if defined(SMALL_MEDIUM) && !defined(_MSC_VER) && !defined(__SC__) 193 | /* Use our own functions for small and medium model with MSC <= 5.0. 194 | * You may have to use the same strategy for Borland C (untested). 195 | * The __SC__ check is for Symantec. 196 | */ 197 | # define NO_MEMCPY 198 | #endif 199 | #if defined(STDC) && !defined(HAVE_MEMCPY) && !defined(NO_MEMCPY) 200 | # define HAVE_MEMCPY 201 | #endif 202 | #ifdef HAVE_MEMCPY 203 | # ifdef SMALL_MEDIUM /* MSDOS small or medium model */ 204 | # define zmemcpy _fmemcpy 205 | # define zmemcmp _fmemcmp 206 | # define zmemzero(dest, len) _fmemset(dest, 0, len) 207 | # else 208 | # define zmemcpy memcpy 209 | # define zmemcmp memcmp 210 | # define zmemzero(dest, len) memset(dest, 0, len) 211 | # endif 212 | #else 213 | void ZLIB_INTERNAL zmemcpy OF((Bytef* dest, const Bytef* source, uInt len)); 214 | int ZLIB_INTERNAL zmemcmp OF((const Bytef* s1, const Bytef* s2, uInt len)); 215 | void ZLIB_INTERNAL zmemzero OF((Bytef* dest, uInt len)); 216 | #endif 217 | 218 | /* Diagnostic functions */ 219 | #ifdef DEBUG 220 | # include 221 | extern int ZLIB_INTERNAL z_verbose; 222 | extern void ZLIB_INTERNAL z_error OF((char *m)); 223 | # define Assert(cond,msg) {if(!(cond)) z_error(msg);} 224 | # define Trace(x) {if (z_verbose>=0) fprintf x ;} 225 | # define Tracev(x) {if (z_verbose>0) fprintf x ;} 226 | # define Tracevv(x) {if (z_verbose>1) fprintf x ;} 227 | # define Tracec(c,x) {if (z_verbose>0 && (c)) fprintf x ;} 228 | # define Tracecv(c,x) {if (z_verbose>1 && (c)) fprintf x ;} 229 | #else 230 | # define Assert(cond,msg) 231 | # define Trace(x) 232 | # define Tracev(x) 233 | # define Tracevv(x) 234 | # define Tracec(c,x) 235 | # define Tracecv(c,x) 236 | #endif 237 | 238 | #ifndef Z_SOLO 239 | voidpf ZLIB_INTERNAL zcalloc OF((voidpf opaque, unsigned items, 240 | unsigned size)); 241 | void ZLIB_INTERNAL zcfree OF((voidpf opaque, voidpf ptr)); 242 | #endif 243 | 244 | #define ZALLOC(strm, items, size) \ 245 | (*((strm)->zalloc))((strm)->opaque, (items), (size)) 246 | #define ZFREE(strm, addr) (*((strm)->zfree))((strm)->opaque, (voidpf)(addr)) 247 | #define TRY_FREE(s, p) {if (p) ZFREE(s, p);} 248 | 249 | /* Reverse the bytes in a 32-bit value */ 250 | #define ZSWAP32(q) ((((q) >> 24) & 0xff) + (((q) >> 8) & 0xff00) + \ 251 | (((q) & 0xff00) << 8) + (((q) & 0xff) << 24)) 252 | 253 | #endif /* ZUTIL_H */ 254 | --------------------------------------------------------------------------------