├── zlib ├── ChangeLog ├── inffast.h ├── INDEX ├── uncompr.c ├── inftrees.h ├── compress.c ├── zlib.3 ├── adler32.c ├── README ├── inflate.h ├── inffixed.h ├── zutil.h ├── zutil.c ├── trees.h ├── minigzip.c ├── algorithm.txt ├── zconf.h ├── zconf.in.h ├── deflate.h ├── inffast.c ├── crc32.c └── inftrees.c ├── examples ├── make_new.c ├── Makefile ├── compress_all.c └── extract_all.c ├── README.md ├── src ├── main.c ├── zlib.c ├── euc_kr.c ├── hash_tables.c └── test.c ├── doc ├── grf_magic ├── rat.txt └── README ├── CMakeLists.txt ├── includes ├── hash_tables.h └── grf.h ├── grfbuilder ├── main.cpp └── MainWindow.h └── Makefile /zlib/ChangeLog: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MagicalTux/grf/HEAD/zlib/ChangeLog -------------------------------------------------------------------------------- /examples/make_new.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MagicalTux/grf/HEAD/examples/make_new.c -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # libgrf and GrfBuilder 2 | 3 | Tool made a long time ago to manipulate Gravity Ragnarok Online(tm)'s data files. 4 | 5 | ## Building 6 | 7 | cmake -B build -DCMAKE_BUILD_TYPE=Release 8 | cmake --build build 9 | 10 | 11 | -------------------------------------------------------------------------------- /zlib/inffast.h: -------------------------------------------------------------------------------- 1 | /* inffast.h -- header to use inffast.c 2 | * Copyright (C) 1995-2003 Mark Adler 3 | * For conditions of distribution and use, see copyright notice in zlib.h 4 | */ 5 | 6 | /* WARNING: this file should *not* be used by applications. It is 7 | part of the implementation of the compression library and is 8 | subject to change. Applications should only use zlib.h. 9 | */ 10 | 11 | void inflate_fast OF((z_streamp strm, unsigned start)); 12 | -------------------------------------------------------------------------------- /examples/Makefile: -------------------------------------------------------------------------------- 1 | #!/bin/make 2 | 3 | CC=$(shell which gcc32) 4 | ifeq ($(CC),) 5 | CC=gcc 6 | endif 7 | 8 | SOURCES=$(wildcard *.c) 9 | OBJECTS=$(patsubst %.c,%.o,$(SOURCES)) 10 | TARGETS=$(patsubst %.o,%.exe,$(OBJECTS)) 11 | CFLAGS=-I../includes -ggdb -pipe -Wall -pedantic 12 | 13 | all: $(TARGETS) 14 | 15 | %.o: %.c 16 | $(CC) $(CFLAGS) -c -o $@ $^ 17 | 18 | %.exe: %.o 19 | $(CC) -o $@ $^ -L.. -lgrf 20 | 21 | clean: 22 | $(RM) $(TARGETS) $(OBJECTS) 23 | 24 | -------------------------------------------------------------------------------- /src/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | GRFEXPORT uint32_t grf_version(void) { 7 | return (VERSION_MAJOR << 16) | (VERSION_MINOR << 8) | VERSION_REVISION; 8 | } 9 | 10 | GRFEXPORT char *grf_versionstring(void) { 11 | static char vstr[128]; 12 | snprintf((char *)&vstr, 128, VERSION_STRING, VERSION_MAJOR, VERSION_MINOR, VERSION_REVISION, zlibVersion()); 13 | return vstr; 14 | } 15 | 16 | GRFEXPORT char *grf_versionstring_r(char *vstr, size_t buflen) { 17 | snprintf(vstr, buflen, VERSION_STRING, VERSION_MAJOR, VERSION_MINOR, VERSION_REVISION, zlibVersion()); 18 | return vstr; 19 | } 20 | 21 | -------------------------------------------------------------------------------- /doc/grf_magic: -------------------------------------------------------------------------------- 1 | # Gravity Ragnarok File 2 | # This is in fact a file container (compressed using zlib) for game files 3 | # There are 3 known versions of those files, which are 0x120 0x130 and 0x200 4 | # Difference in 0x200 files: the file index is now unencrypted, but also compressed 5 | # 6 | # filecount = filecount - seed - 7 7 | # Some programs (grf factory?) tend to use value 15724 as seed... 8 | 0 string Master\ of\ Magic\0 Gravity GRF file 9 | > 16 string \0\0\0\0\0\0\0\0\0\0\0\0\0\0 without encryption 10 | > 16 string !\0\0\0\0\0\0\0\0\0\0\0\0\0\0 11 | >> 16 string !\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e (corrupted) 12 | > 42 leshort x (version 0x%x) 13 | > 34 lelong 0 14 | >> 38 lelong-7 x containing %u files 15 | > 34 lelong >0 16 | >> 38 lelong-7 x containing (%u - 17 | >> 34 lelong x %u) files 18 | -------------------------------------------------------------------------------- /doc/rat.txt: -------------------------------------------------------------------------------- 1 | RAT files 2 | 3 | Directory entry: 4 | d + (namelen).b + name + \00 5 | 6 | File entry: 7 | f + (namelen).b + \00 + (filelenle).l + file 8 | 9 | End entry: 10 | e + \04 + end + \00 11 | 12 | Example file: 13 | 00000000 64 07 66 6f 6f 62 61 72 00 64 0c 66 6f 6f 62 61 |d.foobar.d.fooba| 14 | 00000010 72 5c 74 65 73 74 00 66 17 66 6f 6f 62 61 72 5c |r\test.f.foobar\| 15 | 00000020 74 65 73 74 5c 62 6c 61 62 6c 61 2e 74 78 74 00 |test\blabla.txt.| 16 | 00000030 0a 00 00 00 66 6f 6f 62 61 72 62 61 7a 0a 66 18 |....foobarbaz.f.| 17 | 00000040 66 6f 6f 62 61 72 5c 74 65 73 74 5c 62 6c 61 62 |foobar\test\blab| 18 | 00000050 6c 61 32 2e 74 78 74 00 0a 00 00 00 66 6f 6f 62 |la2.txt.....foob| 19 | 00000060 61 72 62 61 7a 0a 66 18 66 6f 6f 62 61 72 5c 74 |arbaz.f.foobar\t| 20 | 00000070 65 73 74 5c 62 6c 61 62 6c 61 33 2e 74 78 74 00 |est\blabla3.txt.| 21 | 00000080 0a 00 00 00 66 6f 6f 62 61 72 62 61 7a 0a 66 0f |....foobarbaz.f.| 22 | 00000090 66 6f 6f 62 61 72 5c 74 73 74 2e 74 78 74 00 0a |foobar\tst.txt..| 23 | 000000a0 00 00 00 66 6f 6f 62 61 72 62 61 7a 0a 65 04 65 |...foobarbaz.e.e| 24 | 000000b0 6e 64 00 |nd.| 25 | 26 | -------------------------------------------------------------------------------- /doc/README: -------------------------------------------------------------------------------- 1 | GRF library 2 | =========== 3 | http://www.grfbuilder.com/ 4 | 5 | This library is NOT opensource, but is free to use. 6 | 7 | You can submit bugs by mailing me (magicaltux@ookoo.org). 8 | 9 | If you use my library, you can drop me an email, it'll give me more motivation 10 | and will allow you to have more updates :) 11 | 12 | Running GrfBuilder on Linux without installing the GRF lib 13 | ========================================================== 14 | 15 | By default, the Linux system will try to load the GRF library from system's 16 | library path (/lib, /usr/lib, etc). You can override this by using the 17 | LD_LIBRARY_PATH environnement variable. 18 | Running GrfBuilder would be done by typing : 19 | 20 | $ LD_LIBRARY_PATH=. ./grfbuilder.bin 21 | 22 | Audio support on Linux with Qt 23 | ============================== 24 | 25 | You need to compile your Qt library and include NAS support. 26 | 27 | Gentoo: 28 | echo "x11-libs/qt nas" >>/etc/portage/package.use 29 | 30 | Configure: add option -system-nas-sound 31 | 32 | Changelog 33 | ========= 34 | 35 | Version 0.1.30 36 | -------------- 37 | 38 | * Fixed grf_merge() going in infinite loop if some requirements are met (thanks to Atikae) 39 | * Fixed use of _O_BINARY mode on Win32 (thanks to Jogger) 40 | 41 | -------------------------------------------------------------------------------- /examples/compress_all.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include 8 | 9 | /* open directory "data" in current context, and recursively add all files */ 10 | 11 | void recurse_scan_add(void *grf, char *dname) { 12 | DIR *d; 13 | char name[NAME_MAX]; 14 | struct stat s; 15 | 16 | printf("opendir([%p]%s)\n", dname, dname); 17 | d = opendir(dname); 18 | if (d == NULL) return; /* argh */ 19 | while(1) { 20 | struct dirent *de = readdir(d); 21 | if (de == NULL) break; 22 | if (de->d_name[0]=='.') continue; /* skip "invisible" files */ 23 | sprintf(name, "%s/%s", dname, de->d_name); 24 | stat(name, &s); 25 | if (S_ISDIR(s.st_mode)) { 26 | recurse_scan_add(grf, name); 27 | } else if (S_ISREG(s.st_mode)) { 28 | grf_file_add_path(grf, name, name); 29 | } 30 | } 31 | closedir(d); 32 | } 33 | 34 | int main(int argc, char *argv[]) { 35 | void *grf; 36 | if (argc != 2) { 37 | fprintf(stderr, "Arguments: %s out_file\n", argv[0]); 38 | return 1; 39 | } 40 | grf = grf_new(argv[1], true); 41 | if (grf == NULL) { 42 | fprintf(stderr, "Could not write to %s\n", argv[1]); 43 | return 2; 44 | } 45 | recurse_scan_add(grf, "data"); 46 | grf_free(grf); 47 | return 0; 48 | } 49 | 50 | -------------------------------------------------------------------------------- /src/zlib.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int zlib_buffer_inflate(void *dest, int destlen, void *src, int srclen) { 5 | z_stream stream; 6 | int err; 7 | 8 | stream.next_in = src; 9 | stream.avail_in = srclen; 10 | 11 | stream.next_out = dest; 12 | stream.avail_out = destlen; 13 | 14 | stream.zalloc = (alloc_func)0; 15 | stream.zfree = (free_func)0; 16 | 17 | err = inflateInit(&stream); 18 | if (err != Z_OK) return 0; 19 | 20 | err = inflate(&stream, Z_FINISH); 21 | if (err != Z_STREAM_END) { 22 | inflateEnd(&stream); 23 | return 0; 24 | } 25 | 26 | err = inflateEnd(&stream); 27 | if (err != Z_OK) return 0; 28 | return stream.total_out; 29 | } 30 | 31 | int zlib_buffer_deflate(void *dest, int destlen, void *src, int srclen, int level) { 32 | z_stream stream; 33 | int err; 34 | 35 | stream.next_in = src; 36 | stream.avail_in = srclen; 37 | 38 | stream.next_out = dest; 39 | stream.avail_out = destlen; 40 | 41 | stream.zalloc = (alloc_func)0; 42 | stream.zfree = (free_func)0; 43 | 44 | err = deflateInit(&stream, level); 45 | if (err != Z_OK) return 0; 46 | 47 | err = deflate(&stream, Z_FINISH); 48 | if (err != Z_STREAM_END) { 49 | deflateEnd(&stream); 50 | return 0; 51 | } 52 | 53 | err = deflateEnd(&stream); 54 | if (err != Z_OK) return 0; 55 | return stream.total_out; 56 | } 57 | 58 | -------------------------------------------------------------------------------- /zlib/INDEX: -------------------------------------------------------------------------------- 1 | ChangeLog history of changes 2 | FAQ Frequently Asked Questions about zlib 3 | INDEX this file 4 | Makefile makefile for Unix (generated by configure) 5 | Makefile.in makefile for Unix (template for configure) 6 | README guess what 7 | algorithm.txt description of the (de)compression algorithm 8 | configure configure script for Unix 9 | zconf.in.h template for zconf.h (used by configure) 10 | 11 | amiga/ makefiles for Amiga SAS C 12 | as400/ makefiles for IBM AS/400 13 | msdos/ makefiles for MSDOS 14 | old/ makefiles for various architectures and zlib documentation 15 | files that have not yet been updated for zlib 1.2.x 16 | projects/ projects for various Integrated Development Environments 17 | qnx/ makefiles for QNX 18 | win32/ makefiles for Windows 19 | 20 | zlib public header files (must be kept): 21 | zconf.h 22 | zlib.h 23 | 24 | private source files used to build the zlib library: 25 | adler32.c 26 | compress.c 27 | crc32.c 28 | crc32.h 29 | deflate.c 30 | deflate.h 31 | gzio.c 32 | infback.c 33 | inffast.c 34 | inffast.h 35 | inffixed.h 36 | inflate.c 37 | inflate.h 38 | inftrees.c 39 | inftrees.h 40 | trees.c 41 | trees.h 42 | uncompr.c 43 | zutil.c 44 | zutil.h 45 | 46 | source files for sample programs: 47 | example.c 48 | minigzip.c 49 | 50 | unsupported contribution by third parties 51 | See contrib/README.contrib 52 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.10) 2 | 3 | # Set the project name and version (adjust as needed). 4 | project(grf VERSION 0.1.30 LANGUAGES C) 5 | 6 | # Enable an option to choose between building a static or shared library. 7 | option(BUILD_SHARED_LIB "Build as a shared library instead of static" ON) 8 | option(BUILD_TESTS "Build the test executable" ON) 9 | 10 | # Find ZLIB library - this makes ZLIB::ZLIB available. 11 | find_package(ZLIB REQUIRED) 12 | 13 | # List out all source files from the src directory. 14 | set(SRC_FILES 15 | "${CMAKE_CURRENT_SOURCE_DIR}/src/euc_kr.c" 16 | "${CMAKE_CURRENT_SOURCE_DIR}/src/grf.c" 17 | "${CMAKE_CURRENT_SOURCE_DIR}/src/hash_tables.c" 18 | "${CMAKE_CURRENT_SOURCE_DIR}/src/main.c" 19 | "${CMAKE_CURRENT_SOURCE_DIR}/src/zlib.c" 20 | ) 21 | 22 | # Decide how the library will be built (STATIC or SHARED). 23 | if(BUILD_SHARED_LIB) 24 | add_library(grf SHARED ${SRC_FILES}) 25 | else() 26 | add_library(grf STATIC ${SRC_FILES}) 27 | endif() 28 | 29 | # Optionally set a C standard (e.g., C99). 30 | # You can remove or update this to whatever standard you need. 31 | set_property(TARGET grf PROPERTY C_STANDARD 99) 32 | 33 | # Link against zlib. 34 | target_link_libraries(grf PUBLIC ZLIB::ZLIB) 35 | 36 | # If you have library headers you want other projects to use, specify them here: 37 | target_include_directories(grf PUBLIC 38 | "${CMAKE_CURRENT_SOURCE_DIR}/includes" 39 | ) 40 | 41 | if(BUILD_TESTS) 42 | # We use CMake's testing features. 43 | include(CTest) 44 | enable_testing() 45 | 46 | # Create an executable from test.c 47 | add_executable(grf_test "${CMAKE_CURRENT_SOURCE_DIR}/src/test.c") 48 | 49 | # Link the test executable against our library 50 | target_link_libraries(grf_test PRIVATE grf) 51 | 52 | # Optionally, add a test to be run with "ctest" command 53 | add_test(NAME run_grf_test COMMAND grf_test) 54 | endif() 55 | 56 | # If you have headers to install as well, list them here: 57 | install(FILES "${CMAKE_CURRENT_SOURCE_DIR}/includes/libgrf.h" DESTINATION include) 58 | 59 | -------------------------------------------------------------------------------- /includes/hash_tables.h: -------------------------------------------------------------------------------- 1 | /* hash_tables.h : headers for hash tables 2 | * $Id: hash_tables.h 2 2005-07-24 23:28:07Z magicaltux $ 3 | * 4 | * Kumo Internet Relay Chat Daemon 5 | * Copyright (C) 2004 Robert Karpeles 6 | * 7 | * This program is free software; you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation; either version 1, or (at your option) 10 | * any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program; if not, write to the Free Software 19 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 20 | */ 21 | 22 | /* We will store linked lists in our hash tables, so first make the linked list typedef 23 | */ 24 | 25 | #ifndef _HASH_TABLES_H 26 | #define _HASH_TABLES_H 27 | 28 | typedef struct _list_element { 29 | char *string; 30 | void *pointer; /* pointer to the value of this string */ 31 | struct _list_element *next; 32 | } list_element; 33 | 34 | /* and the base hashtable structure with a dynamic array 35 | */ 36 | 37 | typedef struct _hash_table { 38 | unsigned long int size; /* store hash table size for future references */ 39 | struct _list_element **table; 40 | void (*free_func)(void *); 41 | unsigned int count; 42 | } hash_table; 43 | 44 | /* Our "exported" functions 45 | */ 46 | 47 | hash_table *hash_create_table(unsigned long, void *func); 48 | list_element *hash_lookup_raw(hash_table *, const char *); 49 | void *hash_lookup(hash_table *, const char *); 50 | int hash_set_element(hash_table *, char *, void *, int); 51 | int hash_add_element(hash_table *, char *, void *); 52 | int hash_del_element(hash_table *, char *); 53 | int hash_remove_element(hash_table *, char *); 54 | void hash_free_table(hash_table *); 55 | list_element **hash_foreach(hash_table *); 56 | void **hash_foreach_val(hash_table *); 57 | 58 | #endif 59 | 60 | -------------------------------------------------------------------------------- /examples/extract_all.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include /* free() */ 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | #include 12 | 13 | void do_mkdir(char *name) { 14 | char *n = strdup(name); 15 | char *b = strdup(dirname(n)); 16 | struct stat s; 17 | memset(&s, 0, sizeof(s)); 18 | free(n); 19 | stat(b, &s); 20 | if ((s.st_mode & S_IFDIR) == S_IFDIR) { free(b); return; } /* already good */ 21 | if (mkdir(b, 0755) == 0) { 22 | free(b); 23 | return; 24 | } 25 | do_mkdir(b); 26 | if (mkdir(b, 0755) != 0) { 27 | printf("Failed to create %s\n", b); 28 | exit(1); 29 | } 30 | free(b); 31 | } 32 | 33 | int main(int argc, char *argv[]) { 34 | void *grf; 35 | void *cur_file; 36 | int i; 37 | char *name; 38 | size_t len, size; 39 | void *ptr; 40 | FILE *f; 41 | if (argc != 2) { 42 | fprintf(stderr, "Call: %s /path/to/file.grf\n", argv[0]); 43 | return 1; 44 | } 45 | printf("Trying to open `%s'...\n", argv[1]); 46 | grf = grf_load(argv[1], false); /* readonly */ 47 | if (grf==NULL) { 48 | printf("Failed! Please check that `%s' is a valid Gravity Ragnarok File.\n", argv[1]); 49 | return 1; 50 | } 51 | /* get files list */ 52 | cur_file = grf_get_file_first(grf); 53 | while(cur_file != NULL) { 54 | name = strdup(grf_file_get_filename(cur_file)); 55 | len = strlen(name); 56 | size = grf_file_get_size(cur_file); 57 | for(i=0;i 64K on 16-bit machine: */ 38 | if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR; 39 | 40 | stream.next_out = dest; 41 | stream.avail_out = (uInt)*destLen; 42 | if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR; 43 | 44 | stream.zalloc = (alloc_func)0; 45 | stream.zfree = (free_func)0; 46 | 47 | err = inflateInit(&stream); 48 | if (err != Z_OK) return err; 49 | 50 | err = inflate(&stream, Z_FINISH); 51 | if (err != Z_STREAM_END) { 52 | inflateEnd(&stream); 53 | if (err == Z_NEED_DICT || (err == Z_BUF_ERROR && stream.avail_in == 0)) 54 | return Z_DATA_ERROR; 55 | return err; 56 | } 57 | *destLen = stream.total_out; 58 | 59 | err = inflateEnd(&stream); 60 | return err; 61 | } 62 | -------------------------------------------------------------------------------- /zlib/inftrees.h: -------------------------------------------------------------------------------- 1 | /* inftrees.h -- header to use inftrees.c 2 | * Copyright (C) 1995-2005 Mark Adler 3 | * For conditions of distribution and use, see copyright notice in zlib.h 4 | */ 5 | 6 | /* WARNING: this file should *not* be used by applications. It is 7 | part of the implementation of the compression library and is 8 | subject to change. Applications should only use zlib.h. 9 | */ 10 | 11 | /* Structure for decoding tables. Each entry provides either the 12 | information needed to do the operation requested by the code that 13 | indexed that table entry, or it provides a pointer to another 14 | table that indexes more bits of the code. op indicates whether 15 | the entry is a pointer to another table, a literal, a length or 16 | distance, an end-of-block, or an invalid code. For a table 17 | pointer, the low four bits of op is the number of index bits of 18 | that table. For a length or distance, the low four bits of op 19 | is the number of extra bits to get after the code. bits is 20 | the number of bits in this code or part of the code to drop off 21 | of the bit buffer. val is the actual byte to output in the case 22 | of a literal, the base length or distance, or the offset from 23 | the current table to the next table. Each entry is four bytes. */ 24 | typedef struct { 25 | unsigned char op; /* operation, extra bits, table bits */ 26 | unsigned char bits; /* bits in this part of the code */ 27 | unsigned short val; /* offset in table or code value */ 28 | } code; 29 | 30 | /* op values as set by inflate_table(): 31 | 00000000 - literal 32 | 0000tttt - table link, tttt != 0 is the number of table index bits 33 | 0001eeee - length or distance, eeee is the number of extra bits 34 | 01100000 - end of block 35 | 01000000 - invalid code 36 | */ 37 | 38 | /* Maximum size of dynamic tree. The maximum found in a long but non- 39 | exhaustive search was 1444 code structures (852 for length/literals 40 | and 592 for distances, the latter actually the result of an 41 | exhaustive search). The true maximum is not known, but the value 42 | below is more than safe. */ 43 | #define ENOUGH 2048 44 | #define MAXD 592 45 | 46 | /* Type of code to build for inftable() */ 47 | typedef enum { 48 | CODES, 49 | LENS, 50 | DISTS 51 | } codetype; 52 | 53 | extern int inflate_table OF((codetype type, unsigned short FAR *lens, 54 | unsigned codes, code FAR * FAR *table, 55 | unsigned FAR *bits, unsigned short FAR *work)); 56 | -------------------------------------------------------------------------------- /zlib/compress.c: -------------------------------------------------------------------------------- 1 | /* compress.c -- compress a memory buffer 2 | * Copyright (C) 1995-2003 Jean-loup Gailly. 3 | * For conditions of distribution and use, see copyright notice in zlib.h 4 | */ 5 | 6 | /* @(#) $Id$ */ 7 | 8 | #define ZLIB_INTERNAL 9 | #include "zlib.h" 10 | 11 | /* =========================================================================== 12 | Compresses the source buffer into the destination buffer. The level 13 | parameter has the same meaning as in deflateInit. sourceLen is the byte 14 | length of the source buffer. Upon entry, destLen is the total size of the 15 | destination buffer, which must be at least 0.1% larger than sourceLen plus 16 | 12 bytes. Upon exit, destLen is the actual size of the compressed buffer. 17 | 18 | compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough 19 | memory, Z_BUF_ERROR if there was not enough room in the output buffer, 20 | Z_STREAM_ERROR if the level parameter is invalid. 21 | */ 22 | int ZEXPORT compress2 (dest, destLen, source, sourceLen, level) 23 | Bytef *dest; 24 | uLongf *destLen; 25 | const Bytef *source; 26 | uLong sourceLen; 27 | int level; 28 | { 29 | z_stream stream; 30 | int err; 31 | 32 | stream.next_in = (Bytef*)source; 33 | stream.avail_in = (uInt)sourceLen; 34 | #ifdef MAXSEG_64K 35 | /* Check for source > 64K on 16-bit machine: */ 36 | if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR; 37 | #endif 38 | stream.next_out = dest; 39 | stream.avail_out = (uInt)*destLen; 40 | if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR; 41 | 42 | stream.zalloc = (alloc_func)0; 43 | stream.zfree = (free_func)0; 44 | stream.opaque = (voidpf)0; 45 | 46 | err = deflateInit(&stream, level); 47 | if (err != Z_OK) return err; 48 | 49 | err = deflate(&stream, Z_FINISH); 50 | if (err != Z_STREAM_END) { 51 | deflateEnd(&stream); 52 | return err == Z_OK ? Z_BUF_ERROR : err; 53 | } 54 | *destLen = stream.total_out; 55 | 56 | err = deflateEnd(&stream); 57 | return err; 58 | } 59 | 60 | /* =========================================================================== 61 | */ 62 | int ZEXPORT compress (dest, destLen, source, sourceLen) 63 | Bytef *dest; 64 | uLongf *destLen; 65 | const Bytef *source; 66 | uLong sourceLen; 67 | { 68 | return compress2(dest, destLen, source, sourceLen, Z_DEFAULT_COMPRESSION); 69 | } 70 | 71 | /* =========================================================================== 72 | If the default memLevel or windowBits for deflateInit() is changed, then 73 | this function needs to be updated. 74 | */ 75 | uLong ZEXPORT compressBound (sourceLen) 76 | uLong sourceLen; 77 | { 78 | return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) + 11; 79 | } 80 | -------------------------------------------------------------------------------- /grfbuilder/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "MainWindow.h" 5 | #include 6 | 7 | #include 8 | 9 | int main(int argc, char *argv[]) { 10 | QApplication app(argc, argv); 11 | 12 | QCoreApplication::setOrganizationName("GrfBuilder"); 13 | QCoreApplication::setOrganizationDomain("grfbuilder.com"); 14 | QCoreApplication::setApplicationName("GrfBuilder"); 15 | if (grf_version() < (0x11c)) { // 0.1.28 16 | QMessageBox::warning(NULL, "GrfBuilder", "I need at least libgrf v0.1.28 to run. Make sure you have the last one installed.", QMessageBox::Cancel, QMessageBox::Cancel); 17 | return 1; 18 | } 19 | 20 | QString locale = QLocale::system().name(); 21 | 22 | MainWindow MW; 23 | if (MW.translator.load(QString("grfbuilder_") + locale)) { 24 | app.installTranslator(&MW.translator); 25 | } else { 26 | printf("Locale initialisation failed!\n"); 27 | } 28 | 29 | MW.RetranslateStrings(); 30 | MW.show(); 31 | return app.exec(); 32 | } 33 | 34 | /* 35 | 36 | QTreeWidgetItem *__item = new QTreeWidgetItem(view_allfiles); 37 | __item->setText(0, QApplication::translate("main_window", "15156", 0, QApplication::UnicodeUTF8)); 38 | __item->setText(1, QApplication::translate("main_window", "165645", 0, QApplication::UnicodeUTF8)); 39 | __item->setText(2, QApplication::translate("main_window", "0", 0, QApplication::UnicodeUTF8)); 40 | __item->setText(3, QApplication::translate("main_window", "data\\prontera.gat", 0, QApplication::UnicodeUTF8)); 41 | tab_sel->setTabText(tab_sel->indexOf(grf_allfiles), QApplication::translate("main_window", "All files", 0, QApplication::UnicodeUTF8)); 42 | view_filestree->headerItem()->setText(0, QApplication::translate("main_window", "Filename", 0, QApplication::UnicodeUTF8)); 43 | view_filestree->headerItem()->setText(1, QApplication::translate("main_window", "File size", 0, QApplication::UnicodeUTF8)); 44 | view_filestree->clear(); 45 | 46 | QTreeWidgetItem *__item1 = new QTreeWidgetItem(view_filestree); 47 | __item1->setText(0, QApplication::translate("main_window", "data", 0, QApplication::UnicodeUTF8)); 48 | __item1->setText(1, QApplication::translate("main_window", "", 0, QApplication::UnicodeUTF8)); 49 | 50 | QTreeWidgetItem *__item2 = new QTreeWidgetItem(__item1); 51 | __item2->setText(0, QApplication::translate("main_window", "prontera.gat", 0, QApplication::UnicodeUTF8)); 52 | __item2->setText(1, QApplication::translate("main_window", "156465", 0, QApplication::UnicodeUTF8)); 53 | 54 | QTreeWidgetItem *__item3 = new QTreeWidgetItem(__item1); 55 | __item3->setText(0, QApplication::translate("main_window", "palette", 0, QApplication::UnicodeUTF8)); 56 | __item3->setText(1, QApplication::translate("main_window", "", 0, QApplication::UnicodeUTF8)); 57 | 58 | QTreeWidgetItem *__item4 = new QTreeWidgetItem(__item3); 59 | __item4->setText(0, QApplication::translate("main_window", "\355\225\234\352\265\255\354\226\264", 0, QApplication::UnicodeUTF8)); 60 | __item4->setText(1, QApplication::translate("main_window", "", 0, QApplication::UnicodeUTF8)); 61 | tab_sel->setTabText(tab_sel->indexOf(grf_treeview), QApplication::translate("main_window", "Tree view", 0, QApplication::UnicodeUTF8)); 62 | 63 | */ 64 | -------------------------------------------------------------------------------- /includes/grf.h: -------------------------------------------------------------------------------- 1 | /* grf.h : main grf include 2 | * 3 | * Private definitions 4 | */ 5 | 6 | #ifndef __GRF_H_INCLUDED 7 | #define __GRF_H_INCLUDED 8 | 9 | #define _LARGEFILE_SOURCE 10 | 11 | #ifdef __C99 12 | #error test 13 | #endif 14 | 15 | #include 16 | #include 17 | #include 18 | #include "hash_tables.h" 19 | 20 | #ifndef GRF_NO_EXPORT 21 | #ifdef __WIN32 22 | #define GRFEXPORT __declspec(dllexport) 23 | #else 24 | #define GRFEXPORT extern 25 | #endif 26 | #else 27 | #define GRFEXPORT 28 | #endif 29 | 30 | #define VERSION_MAJOR 0 31 | #define VERSION_MINOR 1 32 | #define VERSION_REVISION 31 33 | 34 | #ifdef __WIN32 35 | #define VERSION_TYPE "Win32" 36 | #else 37 | #define VERSION_TYPE "Unix" 38 | #endif 39 | 40 | #ifdef __USE_LARGEFILE 41 | #define VERSION_EXTRA ", LargeFile" 42 | #else 43 | #define VERSION_EXTRA 44 | #endif 45 | 46 | #ifdef __DEBUG 47 | #define VERSION_EXTRA2 ", Debug" 48 | #else 49 | #define VERSION_EXTRA2 50 | #endif 51 | 52 | #define VERSION_STRING "Gravity Ragnarok Files lib v%d.%d.%d (" VERSION_TYPE VERSION_EXTRA VERSION_EXTRA2 ") by MagicalTux \nBundled zlib version %s" 53 | 54 | typedef struct grf_handler *grf_handle; 55 | typedef struct grf_node *grf_node; 56 | typedef struct grf_treenode *grf_treenode; 57 | #define __LIBGRF_HAS_TYPEDEF 58 | 59 | struct grf_node { 60 | struct grf_node *prev, *next; 61 | struct grf_handler *parent; 62 | struct grf_treenode *tree_parent; 63 | char *filename, flags; 64 | uint32_t size, len, len_aligned, pos, id; 65 | int cycle; 66 | }; 67 | 68 | struct grf_treenode { 69 | bool is_dir; 70 | char *name; 71 | hash_table *subdir; 72 | struct grf_node *ptr; 73 | struct grf_treenode *parent; 74 | }; 75 | 76 | struct grf_handler { 77 | uint32_t filecount, table_offset, table_size, wasted_space; 78 | uint32_t version; 79 | int fd; 80 | int compression_level; 81 | bool need_save, write_mode; 82 | struct grf_node *first_node; 83 | hash_table *fast_table; 84 | struct grf_treenode *root; 85 | bool (* callback)(void *, grf_handle, int, int, const char *); 86 | void *callback_etc; 87 | struct grf_node **node_table; 88 | }; 89 | 90 | #define GRF_HEADER_SIZE 0x2e /* sizeof(grf_header) */ 91 | #define GRF_HEADER_MAGIC "Master of Magic" 92 | #define GRF_FILE_OUTPUT_VERISON 0x200 93 | #define GRF_HASH_TABLE_SIZE 128 94 | #define GRF_TREE_HASH_SIZE 32 95 | 96 | /* values specific to all directories */ 97 | #define GRF_DIRECTORY_LEN 1094 98 | #define GRF_DIRECTORY_LEN_ALIGNED 1812 99 | #define GRF_DIRECTORY_SIZE 1372 100 | #define GRF_DIRECTORY_OFFSET 1418 101 | 102 | // should we use pragma pack ? 103 | struct grf_header { 104 | char header_magic[16]; // "Master of Magic" + 0x00 105 | char header_key[14]; // 0x01 -> 0x0e, or 0x00 -> 0x00 (no crypt) 106 | uint32_t offset __attribute__ ((__packed__)); // offset of file table 107 | uint32_t seed __attribute__ ((__packed__)); 108 | uint32_t filecount __attribute__ ((__packed__)); // Real filecount = filecount - seed - 7 109 | uint32_t version __attribute__ ((__packed__)); // 0x102 0x103 0x200 0xCACA 110 | }; 111 | 112 | struct grf_table_entry_data { 113 | // file name (char + 0x00) 114 | uint32_t len __attribute__ ((__packed__)); // packed len 115 | uint32_t len_aligned __attribute__ ((__packed__)); // same, but with the alignment (?) 116 | uint32_t size __attribute__ ((__packed__)); // real file size (unpacked) 117 | uint8_t flags; // file flags 118 | uint32_t pos __attribute__ ((__packed__)); // position in the grf 119 | }; 120 | 121 | int zlib_buffer_inflate(void *, int, void *, int); /* private: zlib.c */ 122 | int zlib_buffer_deflate(void *, int, void *, int, int); /* private: zlib.c */ 123 | 124 | #define MAX(a,b) ((a>b)?a:b) 125 | 126 | #include "libgrf.h" 127 | 128 | #endif /* __GRF_H_INCLUDED */ 129 | 130 | -------------------------------------------------------------------------------- /grfbuilder/MainWindow.h: -------------------------------------------------------------------------------- 1 | #ifndef __QTWIN_H_FILE 2 | #define __QTWIN_H_FILE 3 | 4 | #define GRFBUILDER_VERSION_MAJOR 0 5 | #define GRFBUILDER_VERSION_MINOR 1 6 | #define GRFBUILDER_VERSION_REVISION 29 7 | 8 | #define L_TRADITIONAL_CHINESE_NAME "\xe7\xb9\x81\xe9\xab\x94\xe4\xb8\xad\xe6\x96\x87 (Trad. Chinese)" 9 | #define L_TRADITIONAL_CHINESE_LOC "zh_TW" 10 | #define L_SIMPLIFIED_CHINESE_NAME "\xe7\xb0\xa1\xe9\xab\x94\xe4\xb8\xad\xe6\x96\x87 (Simp. Chinese)" 11 | #define L_SIMPLIFIED_CHINESE_LOC "zh_CN" 12 | #define L_ENGLISH_NAME "English" 13 | #define L_ENGLISH_LOC "en" 14 | #define L_FRENCH_NAME "Fran\303\247ais" 15 | #define L_FRENCH_LOC "fr" 16 | #define L_GERMAN_NAME "Deutsch" 17 | #define L_GERMAN_LOC "de" 18 | #define L_SPANISH_NAME "Espa\xc3\xb1ol" 19 | #define L_SPANISH_LOC "es" 20 | 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | 27 | #include "ui_MainWindow.h" 28 | 29 | class MainWindow : public QMainWindow { 30 | Q_OBJECT 31 | 32 | public: 33 | MainWindow(QWidget *parent = 0); 34 | bool progress_callback(void *, int pos, int max); 35 | bool repack_progress_callback(void *grf, int pos, int max, const char *filename, QProgressDialog *prog); 36 | bool merge_progress_callback(void *grf, int pos, int max, const char *filename, QProgressDialog *prog); 37 | QTranslator translator; 38 | void RetranslateStrings(); 39 | 40 | protected: 41 | void closeEvent(QCloseEvent *); 42 | 43 | private slots: 44 | void on_btn_new_clicked(); 45 | void on_btn_open_clicked(); 46 | void on_btn_close_clicked(); 47 | void on_btn_extract_clicked(); 48 | void on_btn_delete_clicked(); 49 | void on_btn_extractall_clicked(); 50 | void on_btn_repack_clicked(); 51 | void on_btn_mergegrf_clicked(); 52 | void on_btn_mergedir_clicked(); 53 | void on_listFilter_currentIndexChanged(QString); 54 | 55 | void on_view_allfiles_customContextMenuRequested(const QPoint); 56 | void on_viewSearch_customContextMenuRequested(const QPoint); 57 | 58 | void on_tab_sel_currentChanged(int); 59 | 60 | void on_view_allfiles_doubleClicked(const QModelIndex); 61 | void on_view_filestree_doubleClicked(const QModelIndex); 62 | void on_viewSearch_doubleClicked(const QModelIndex); 63 | 64 | // locales 65 | void myLocaleChange(); 66 | // menu 67 | void on_action_New_triggered(); 68 | void on_action_Open_triggered(); 69 | void on_action_Extract_triggered(); 70 | void on_actionDelete_triggered(); 71 | void on_action_Extract_All_triggered(); 72 | void on_actionRepack_triggered(); 73 | void on_action_Merge_GRF_triggered(); 74 | void on_actionMerge_Dir_triggered(); 75 | void on_action_Close_triggered(); 76 | void on_action_Quit_triggered(); 77 | void on_actionAbout_triggered(); 78 | // Recent files 79 | void myOpenRecent(); 80 | // compression 81 | void on_actionC0_triggered(); 82 | void on_actionC1_triggered(); 83 | void on_actionC2_triggered(); 84 | void on_actionC3_triggered(); 85 | void on_actionC4_triggered(); 86 | void on_actionC5_triggered(); 87 | void on_actionC6_triggered(); 88 | void on_actionC7_triggered(); 89 | void on_actionC8_triggered(); 90 | void on_actionC9_triggered(); 91 | 92 | void on_actionMove_files_triggered(); 93 | void on_actionDecrypt_triggered(); 94 | void on_actionRecompress_triggered(); 95 | 96 | void on_actionUnicode_triggered(); 97 | void on_actionStandard_triggered(); 98 | 99 | private: 100 | Ui::MainWindow ui; 101 | QSettings *settings; 102 | QFile grf_file; 103 | QDialog *image_viewer; 104 | QString last_search; 105 | QMenu *recent_files_menu; 106 | int repack_type; 107 | int compression_level; 108 | unsigned int fillFilesTree(void *, QTreeWidget *); 109 | unsigned int fillFilesTree(void *, QTreeWidgetItem *); 110 | void *grf; 111 | bool grf_has_tree; 112 | void do_mkdir(QString *); 113 | void do_display_wav(void *); 114 | void DoUpdateFilter(QString); 115 | void doOpenFileById(int); 116 | QString showSizeAsString(unsigned int); 117 | void setCompressionLevel(int); 118 | void setRepackType(int); 119 | void RefreshAfterLoad(); 120 | bool do_recurse_dirscan(QList *, QString, QString); 121 | QList myTreeViewRecuriveSearch(QList*); 122 | void refreshRecentList(); 123 | void setRecentFile(QString); 124 | void doLoadFile(QString); 125 | }; 126 | 127 | #endif 128 | 129 | -------------------------------------------------------------------------------- /zlib/zlib.3: -------------------------------------------------------------------------------- 1 | .TH ZLIB 3 "18 July 2005" 2 | .SH NAME 3 | zlib \- compression/decompression library 4 | .SH SYNOPSIS 5 | [see 6 | .I zlib.h 7 | for full description] 8 | .SH DESCRIPTION 9 | The 10 | .I zlib 11 | library is a general purpose data compression library. 12 | The code is thread safe. 13 | It provides in-memory compression and decompression functions, 14 | including integrity checks of the uncompressed data. 15 | This version of the library supports only one compression method (deflation) 16 | but other algorithms will be added later 17 | and will have the same stream interface. 18 | .LP 19 | Compression can be done in a single step if the buffers are large enough 20 | (for example if an input file is mmap'ed), 21 | or can be done by repeated calls of the compression function. 22 | In the latter case, 23 | the application must provide more input and/or consume the output 24 | (providing more output space) before each call. 25 | .LP 26 | The library also supports reading and writing files in 27 | .IR gzip (1) 28 | (.gz) format 29 | with an interface similar to that of stdio. 30 | .LP 31 | The library does not install any signal handler. 32 | The decoder checks the consistency of the compressed data, 33 | so the library should never crash even in case of corrupted input. 34 | .LP 35 | All functions of the compression library are documented in the file 36 | .IR zlib.h . 37 | The distribution source includes examples of use of the library 38 | in the files 39 | .I example.c 40 | and 41 | .IR minigzip.c . 42 | .LP 43 | Changes to this version are documented in the file 44 | .I ChangeLog 45 | that accompanies the source, 46 | and are concerned primarily with bug fixes and portability enhancements. 47 | .LP 48 | A Java implementation of 49 | .I zlib 50 | is available in the Java Development Kit 1.1: 51 | .IP 52 | http://www.javasoft.com/products/JDK/1.1/docs/api/Package-java.util.zip.html 53 | .LP 54 | A Perl interface to 55 | .IR zlib , 56 | written by Paul Marquess (pmqs@cpan.org), 57 | is available at CPAN (Comprehensive Perl Archive Network) sites, 58 | including: 59 | .IP 60 | http://www.cpan.org/modules/by-module/Compress/ 61 | .LP 62 | A Python interface to 63 | .IR zlib , 64 | written by A.M. Kuchling (amk@magnet.com), 65 | is available in Python 1.5 and later versions: 66 | .IP 67 | http://www.python.org/doc/lib/module-zlib.html 68 | .LP 69 | A 70 | .I zlib 71 | binding for 72 | .IR tcl (1), 73 | written by Andreas Kupries (a.kupries@westend.com), 74 | is availlable at: 75 | .IP 76 | http://www.westend.com/~kupries/doc/trf/man/man.html 77 | .LP 78 | An experimental package to read and write files in .zip format, 79 | written on top of 80 | .I zlib 81 | by Gilles Vollant (info@winimage.com), 82 | is available at: 83 | .IP 84 | http://www.winimage.com/zLibDll/unzip.html 85 | and also in the 86 | .I contrib/minizip 87 | directory of the main 88 | .I zlib 89 | web site. 90 | .SH "SEE ALSO" 91 | The 92 | .I zlib 93 | web site can be found at either of these locations: 94 | .IP 95 | http://www.zlib.org 96 | .br 97 | http://www.gzip.org/zlib/ 98 | .LP 99 | The data format used by the zlib library is described by RFC 100 | (Request for Comments) 1950 to 1952 in the files: 101 | .IP 102 | http://www.ietf.org/rfc/rfc1950.txt (concerning zlib format) 103 | .br 104 | http://www.ietf.org/rfc/rfc1951.txt (concerning deflate format) 105 | .br 106 | http://www.ietf.org/rfc/rfc1952.txt (concerning gzip format) 107 | .LP 108 | These documents are also available in other formats from: 109 | .IP 110 | ftp://ftp.uu.net/graphics/png/documents/zlib/zdoc-index.html 111 | .LP 112 | Mark Nelson (markn@ieee.org) wrote an article about 113 | .I zlib 114 | for the Jan. 1997 issue of Dr. Dobb's Journal; 115 | a copy of the article is available at: 116 | .IP 117 | http://dogma.net/markn/articles/zlibtool/zlibtool.htm 118 | .SH "REPORTING PROBLEMS" 119 | Before reporting a problem, 120 | please check the 121 | .I zlib 122 | web site to verify that you have the latest version of 123 | .IR zlib ; 124 | otherwise, 125 | obtain the latest version and see if the problem still exists. 126 | Please read the 127 | .I zlib 128 | FAQ at: 129 | .IP 130 | http://www.gzip.org/zlib/zlib_faq.html 131 | .LP 132 | before asking for help. 133 | Send questions and/or comments to zlib@gzip.org, 134 | or (for the Windows DLL version) to Gilles Vollant (info@winimage.com). 135 | .SH AUTHORS 136 | Version 1.2.3 137 | Copyright (C) 1995-2005 Jean-loup Gailly (jloup@gzip.org) 138 | and Mark Adler (madler@alumni.caltech.edu). 139 | .LP 140 | This software is provided "as-is," 141 | without any express or implied warranty. 142 | In no event will the authors be held liable for any damages 143 | arising from the use of this software. 144 | See the distribution directory with respect to requirements 145 | governing redistribution. 146 | The deflate format used by 147 | .I zlib 148 | was defined by Phil Katz. 149 | The deflate and 150 | .I zlib 151 | specifications were written by L. Peter Deutsch. 152 | Thanks to all the people who reported problems and suggested various 153 | improvements in 154 | .IR zlib ; 155 | who are too numerous to cite here. 156 | .LP 157 | UNIX manual page by R. P. C. Rodgers, 158 | U.S. National Library of Medicine (rodgers@nlm.nih.gov). 159 | .\" end of man page 160 | -------------------------------------------------------------------------------- /zlib/adler32.c: -------------------------------------------------------------------------------- 1 | /* adler32.c -- compute the Adler-32 checksum of a data stream 2 | * Copyright (C) 1995-2004 Mark Adler 3 | * For conditions of distribution and use, see copyright notice in zlib.h 4 | */ 5 | 6 | /* @(#) $Id$ */ 7 | 8 | #define ZLIB_INTERNAL 9 | #include "zlib.h" 10 | 11 | #define BASE 65521UL /* largest prime smaller than 65536 */ 12 | #define NMAX 5552 13 | /* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */ 14 | 15 | #define DO1(buf,i) {adler += (buf)[i]; sum2 += adler;} 16 | #define DO2(buf,i) DO1(buf,i); DO1(buf,i+1); 17 | #define DO4(buf,i) DO2(buf,i); DO2(buf,i+2); 18 | #define DO8(buf,i) DO4(buf,i); DO4(buf,i+4); 19 | #define DO16(buf) DO8(buf,0); DO8(buf,8); 20 | 21 | /* use NO_DIVIDE if your processor does not do division in hardware */ 22 | #ifdef NO_DIVIDE 23 | # define MOD(a) \ 24 | do { \ 25 | if (a >= (BASE << 16)) a -= (BASE << 16); \ 26 | if (a >= (BASE << 15)) a -= (BASE << 15); \ 27 | if (a >= (BASE << 14)) a -= (BASE << 14); \ 28 | if (a >= (BASE << 13)) a -= (BASE << 13); \ 29 | if (a >= (BASE << 12)) a -= (BASE << 12); \ 30 | if (a >= (BASE << 11)) a -= (BASE << 11); \ 31 | if (a >= (BASE << 10)) a -= (BASE << 10); \ 32 | if (a >= (BASE << 9)) a -= (BASE << 9); \ 33 | if (a >= (BASE << 8)) a -= (BASE << 8); \ 34 | if (a >= (BASE << 7)) a -= (BASE << 7); \ 35 | if (a >= (BASE << 6)) a -= (BASE << 6); \ 36 | if (a >= (BASE << 5)) a -= (BASE << 5); \ 37 | if (a >= (BASE << 4)) a -= (BASE << 4); \ 38 | if (a >= (BASE << 3)) a -= (BASE << 3); \ 39 | if (a >= (BASE << 2)) a -= (BASE << 2); \ 40 | if (a >= (BASE << 1)) a -= (BASE << 1); \ 41 | if (a >= BASE) a -= BASE; \ 42 | } while (0) 43 | # define MOD4(a) \ 44 | do { \ 45 | if (a >= (BASE << 4)) a -= (BASE << 4); \ 46 | if (a >= (BASE << 3)) a -= (BASE << 3); \ 47 | if (a >= (BASE << 2)) a -= (BASE << 2); \ 48 | if (a >= (BASE << 1)) a -= (BASE << 1); \ 49 | if (a >= BASE) a -= BASE; \ 50 | } while (0) 51 | #else 52 | # define MOD(a) a %= BASE 53 | # define MOD4(a) a %= BASE 54 | #endif 55 | 56 | /* ========================================================================= */ 57 | uLong ZEXPORT adler32(adler, buf, len) 58 | uLong adler; 59 | const Bytef *buf; 60 | uInt len; 61 | { 62 | unsigned long sum2; 63 | unsigned n; 64 | 65 | /* split Adler-32 into component sums */ 66 | sum2 = (adler >> 16) & 0xffff; 67 | adler &= 0xffff; 68 | 69 | /* in case user likes doing a byte at a time, keep it fast */ 70 | if (len == 1) { 71 | adler += buf[0]; 72 | if (adler >= BASE) 73 | adler -= BASE; 74 | sum2 += adler; 75 | if (sum2 >= BASE) 76 | sum2 -= BASE; 77 | return adler | (sum2 << 16); 78 | } 79 | 80 | /* initial Adler-32 value (deferred check for len == 1 speed) */ 81 | if (buf == Z_NULL) 82 | return 1L; 83 | 84 | /* in case short lengths are provided, keep it somewhat fast */ 85 | if (len < 16) { 86 | while (len--) { 87 | adler += *buf++; 88 | sum2 += adler; 89 | } 90 | if (adler >= BASE) 91 | adler -= BASE; 92 | MOD4(sum2); /* only added so many BASE's */ 93 | return adler | (sum2 << 16); 94 | } 95 | 96 | /* do length NMAX blocks -- requires just one modulo operation */ 97 | while (len >= NMAX) { 98 | len -= NMAX; 99 | n = NMAX / 16; /* NMAX is divisible by 16 */ 100 | do { 101 | DO16(buf); /* 16 sums unrolled */ 102 | buf += 16; 103 | } while (--n); 104 | MOD(adler); 105 | MOD(sum2); 106 | } 107 | 108 | /* do remaining bytes (less than NMAX, still just one modulo) */ 109 | if (len) { /* avoid modulos if none remaining */ 110 | while (len >= 16) { 111 | len -= 16; 112 | DO16(buf); 113 | buf += 16; 114 | } 115 | while (len--) { 116 | adler += *buf++; 117 | sum2 += adler; 118 | } 119 | MOD(adler); 120 | MOD(sum2); 121 | } 122 | 123 | /* return recombined sums */ 124 | return adler | (sum2 << 16); 125 | } 126 | 127 | /* ========================================================================= */ 128 | uLong ZEXPORT adler32_combine(adler1, adler2, len2) 129 | uLong adler1; 130 | uLong adler2; 131 | z_off_t len2; 132 | { 133 | unsigned long sum1; 134 | unsigned long sum2; 135 | unsigned rem; 136 | 137 | /* the derivation of this formula is left as an exercise for the reader */ 138 | rem = (unsigned)(len2 % BASE); 139 | sum1 = adler1 & 0xffff; 140 | sum2 = rem * sum1; 141 | MOD(sum2); 142 | sum1 += (adler2 & 0xffff) + BASE - 1; 143 | sum2 += ((adler1 >> 16) & 0xffff) + ((adler2 >> 16) & 0xffff) + BASE - rem; 144 | if (sum1 > BASE) sum1 -= BASE; 145 | if (sum1 > BASE) sum1 -= BASE; 146 | if (sum2 > (BASE << 1)) sum2 -= (BASE << 1); 147 | if (sum2 > BASE) sum2 -= BASE; 148 | return sum1 | (sum2 << 16); 149 | } 150 | -------------------------------------------------------------------------------- /zlib/README: -------------------------------------------------------------------------------- 1 | ZLIB DATA COMPRESSION LIBRARY 2 | 3 | zlib 1.2.3 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://www.ietf.org/rfc/rfc1950.txt (zlib format), rfc1951.txt (deflate format) 7 | and rfc1952.txt (gzip format). These documents are also available in other 8 | formats from ftp://ftp.uu.net/graphics/png/documents/zlib/zdoc-index.html 9 | 10 | All functions of the compression library are documented in the file zlib.h 11 | (volunteer to write man pages welcome, contact zlib@gzip.org). A usage example 12 | of the library is given in the file example.c which also tests that the library 13 | is working correctly. Another example is given in the file minigzip.c. The 14 | compression library itself is composed of all source files except example.c and 15 | minigzip.c. 16 | 17 | To compile all files and run the test program, follow the instructions given at 18 | the top of Makefile. In short "make test; make install" should work for most 19 | machines. For Unix: "./configure; make test; make install". For MSDOS, use one 20 | of the special makefiles such as Makefile.msc. For VMS, use 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://www.zlib.org or http://www.gzip.org/zlib/ Before reporting a problem, 25 | please check this site to verify that you have the latest version of zlib; 26 | otherwise get the latest version and check whether the problem still exists or 27 | not. 28 | 29 | PLEASE read the zlib FAQ http://www.gzip.org/zlib/zlib_faq.html before asking 30 | for help. 31 | 32 | Mark Nelson wrote an article about zlib for the Jan. 1997 33 | issue of Dr. Dobb's Journal; a copy of the article is available in 34 | http://dogma.net/markn/articles/zlibtool/zlibtool.htm 35 | 36 | The changes made in version 1.2.3 are documented in the file ChangeLog. 37 | 38 | Unsupported third party contributions are provided in directory "contrib". 39 | 40 | A Java implementation of zlib is available in the Java Development Kit 41 | http://java.sun.com/j2se/1.4.2/docs/api/java/util/zip/package-summary.html 42 | See the zlib home page http://www.zlib.org for details. 43 | 44 | A Perl interface to zlib written by Paul Marquess is in the 45 | CPAN (Comprehensive Perl Archive Network) sites 46 | http://www.cpan.org/modules/by-module/Compress/ 47 | 48 | A Python interface to zlib written by A.M. Kuchling is 49 | available in Python 1.5 and later versions, see 50 | http://www.python.org/doc/lib/module-zlib.html 51 | 52 | A zlib binding for TCL written by Andreas Kupries is 53 | availlable at http://www.oche.de/~akupries/soft/trf/trf_zip.html 54 | 55 | An experimental package to read and write files in .zip format, written on top 56 | of zlib by Gilles Vollant , is available in the 57 | contrib/minizip directory of zlib. 58 | 59 | 60 | Notes for some targets: 61 | 62 | - For Windows DLL versions, please see win32/DLL_FAQ.txt 63 | 64 | - For 64-bit Irix, deflate.c must be compiled without any optimization. With 65 | -O, one libpng test fails. The test works in 32 bit mode (with the -n32 66 | compiler flag). The compiler bug has been reported to SGI. 67 | 68 | - zlib doesn't work with gcc 2.6.3 on a DEC 3000/300LX under OSF/1 2.1 it works 69 | when compiled with cc. 70 | 71 | - On Digital Unix 4.0D (formely OSF/1) on AlphaServer, the cc option -std1 is 72 | necessary to get gzprintf working correctly. This is done by configure. 73 | 74 | - zlib doesn't work on HP-UX 9.05 with some versions of /bin/cc. It works with 75 | other compilers. Use "make test" to check your compiler. 76 | 77 | - gzdopen is not supported on RISCOS, BEOS and by some Mac compilers. 78 | 79 | - For PalmOs, see http://palmzlib.sourceforge.net/ 80 | 81 | - When building a shared, i.e. dynamic library on Mac OS X, the library must be 82 | installed before testing (do "make install" before "make test"), since the 83 | library location is specified in the library. 84 | 85 | 86 | Acknowledgments: 87 | 88 | The deflate format used by zlib was defined by Phil Katz. The deflate 89 | and zlib specifications were written by L. Peter Deutsch. Thanks to all the 90 | people who reported problems and suggested various improvements in zlib; 91 | they are too numerous to cite here. 92 | 93 | Copyright notice: 94 | 95 | (C) 1995-2004 Jean-loup Gailly and Mark Adler 96 | 97 | This software is provided 'as-is', without any express or implied 98 | warranty. In no event will the authors be held liable for any damages 99 | arising from the use of this software. 100 | 101 | Permission is granted to anyone to use this software for any purpose, 102 | including commercial applications, and to alter it and redistribute it 103 | freely, subject to the following restrictions: 104 | 105 | 1. The origin of this software must not be misrepresented; you must not 106 | claim that you wrote the original software. If you use this software 107 | in a product, an acknowledgment in the product documentation would be 108 | appreciated but is not required. 109 | 2. Altered source versions must be plainly marked as such, and must not be 110 | misrepresented as being the original software. 111 | 3. This notice may not be removed or altered from any source distribution. 112 | 113 | Jean-loup Gailly Mark Adler 114 | jloup@gzip.org madler@alumni.caltech.edu 115 | 116 | If you use the zlib library in a product, we would appreciate *not* 117 | receiving lengthy legal documents to sign. The sources are provided 118 | for free but without warranty of any kind. The library has been 119 | entirely written by Jean-loup Gailly and Mark Adler; it does not 120 | include third-party code. 121 | 122 | If you redistribute modified sources, we would appreciate that you include 123 | in the file ChangeLog history information documenting your changes. Please 124 | read the FAQ for more information on the distribution of modified source 125 | versions. 126 | -------------------------------------------------------------------------------- /zlib/inflate.h: -------------------------------------------------------------------------------- 1 | /* inflate.h -- internal inflate state definition 2 | * Copyright (C) 1995-2004 Mark Adler 3 | * For conditions of distribution and use, see copyright notice in zlib.h 4 | */ 5 | 6 | /* WARNING: this file should *not* be used by applications. It is 7 | part of the implementation of the compression library and is 8 | subject to change. Applications should only use zlib.h. 9 | */ 10 | 11 | /* define NO_GZIP when compiling if you want to disable gzip header and 12 | trailer decoding by inflate(). NO_GZIP would be used to avoid linking in 13 | the crc code when it is not needed. For shared libraries, gzip decoding 14 | should be left enabled. */ 15 | #ifndef NO_GZIP 16 | # define GUNZIP 17 | #endif 18 | 19 | /* Possible inflate modes between inflate() calls */ 20 | typedef enum { 21 | HEAD, /* i: waiting for magic header */ 22 | FLAGS, /* i: waiting for method and flags (gzip) */ 23 | TIME, /* i: waiting for modification time (gzip) */ 24 | OS, /* i: waiting for extra flags and operating system (gzip) */ 25 | EXLEN, /* i: waiting for extra length (gzip) */ 26 | EXTRA, /* i: waiting for extra bytes (gzip) */ 27 | NAME, /* i: waiting for end of file name (gzip) */ 28 | COMMENT, /* i: waiting for end of comment (gzip) */ 29 | HCRC, /* i: waiting for header crc (gzip) */ 30 | DICTID, /* i: waiting for dictionary check value */ 31 | DICT, /* waiting for inflateSetDictionary() call */ 32 | TYPE, /* i: waiting for type bits, including last-flag bit */ 33 | TYPEDO, /* i: same, but skip check to exit inflate on new block */ 34 | STORED, /* i: waiting for stored size (length and complement) */ 35 | COPY, /* i/o: waiting for input or output to copy stored block */ 36 | TABLE, /* i: waiting for dynamic block table lengths */ 37 | LENLENS, /* i: waiting for code length code lengths */ 38 | CODELENS, /* i: waiting for length/lit and distance code lengths */ 39 | LEN, /* i: waiting for length/lit code */ 40 | LENEXT, /* i: waiting for length extra bits */ 41 | DIST, /* i: waiting for distance code */ 42 | DISTEXT, /* i: waiting for distance extra bits */ 43 | MATCH, /* o: waiting for output space to copy string */ 44 | LIT, /* o: waiting for output space to write literal */ 45 | CHECK, /* i: waiting for 32-bit check value */ 46 | LENGTH, /* i: waiting for 32-bit length (gzip) */ 47 | DONE, /* finished check, done -- remain here until reset */ 48 | BAD, /* got a data error -- remain here until reset */ 49 | MEM, /* got an inflate() memory error -- remain here until reset */ 50 | SYNC /* looking for synchronization bytes to restart inflate() */ 51 | } inflate_mode; 52 | 53 | /* 54 | State transitions between above modes - 55 | 56 | (most modes can go to the BAD or MEM mode -- not shown for clarity) 57 | 58 | Process header: 59 | HEAD -> (gzip) or (zlib) 60 | (gzip) -> FLAGS -> TIME -> OS -> EXLEN -> EXTRA -> NAME 61 | NAME -> COMMENT -> HCRC -> TYPE 62 | (zlib) -> DICTID or TYPE 63 | DICTID -> DICT -> TYPE 64 | Read deflate blocks: 65 | TYPE -> STORED or TABLE or LEN or CHECK 66 | STORED -> COPY -> TYPE 67 | TABLE -> LENLENS -> CODELENS -> LEN 68 | Read deflate codes: 69 | LEN -> LENEXT or LIT or TYPE 70 | LENEXT -> DIST -> DISTEXT -> MATCH -> LEN 71 | LIT -> LEN 72 | Process trailer: 73 | CHECK -> LENGTH -> DONE 74 | */ 75 | 76 | /* state maintained between inflate() calls. Approximately 7K bytes. */ 77 | struct inflate_state { 78 | inflate_mode mode; /* current inflate mode */ 79 | int last; /* true if processing last block */ 80 | int wrap; /* bit 0 true for zlib, bit 1 true for gzip */ 81 | int havedict; /* true if dictionary provided */ 82 | int flags; /* gzip header method and flags (0 if zlib) */ 83 | unsigned dmax; /* zlib header max distance (INFLATE_STRICT) */ 84 | unsigned long check; /* protected copy of check value */ 85 | unsigned long total; /* protected copy of output count */ 86 | gz_headerp head; /* where to save gzip header information */ 87 | /* sliding window */ 88 | unsigned wbits; /* log base 2 of requested window size */ 89 | unsigned wsize; /* window size or zero if not using window */ 90 | unsigned whave; /* valid bytes in the window */ 91 | unsigned write; /* window write index */ 92 | unsigned char FAR *window; /* allocated sliding window, if needed */ 93 | /* bit accumulator */ 94 | unsigned long hold; /* input bit accumulator */ 95 | unsigned bits; /* number of bits in "in" */ 96 | /* for string and stored block copying */ 97 | unsigned length; /* literal or length of data to copy */ 98 | unsigned offset; /* distance back to copy string from */ 99 | /* for table and code decoding */ 100 | unsigned extra; /* extra bits needed */ 101 | /* fixed and dynamic code tables */ 102 | code const FAR *lencode; /* starting table for length/literal codes */ 103 | code const FAR *distcode; /* starting table for distance codes */ 104 | unsigned lenbits; /* index bits for lencode */ 105 | unsigned distbits; /* index bits for distcode */ 106 | /* dynamic table building */ 107 | unsigned ncode; /* number of code length code lengths */ 108 | unsigned nlen; /* number of length code lengths */ 109 | unsigned ndist; /* number of distance code lengths */ 110 | unsigned have; /* number of code lengths in lens[] */ 111 | code FAR *next; /* next available space in codes[] */ 112 | unsigned short lens[320]; /* temporary storage for code lengths */ 113 | unsigned short work[288]; /* work area for code table building */ 114 | code codes[ENOUGH]; /* space for code tables */ 115 | }; 116 | -------------------------------------------------------------------------------- /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. It 6 | is part of the implementation of the compression library and 7 | is subject to change. Applications should only use zlib.h. 8 | */ 9 | 10 | static const code lenfix[512] = { 11 | {96,7,0},{0,8,80},{0,8,16},{20,8,115},{18,7,31},{0,8,112},{0,8,48}, 12 | {0,9,192},{16,7,10},{0,8,96},{0,8,32},{0,9,160},{0,8,0},{0,8,128}, 13 | {0,8,64},{0,9,224},{16,7,6},{0,8,88},{0,8,24},{0,9,144},{19,7,59}, 14 | {0,8,120},{0,8,56},{0,9,208},{17,7,17},{0,8,104},{0,8,40},{0,9,176}, 15 | {0,8,8},{0,8,136},{0,8,72},{0,9,240},{16,7,4},{0,8,84},{0,8,20}, 16 | {21,8,227},{19,7,43},{0,8,116},{0,8,52},{0,9,200},{17,7,13},{0,8,100}, 17 | {0,8,36},{0,9,168},{0,8,4},{0,8,132},{0,8,68},{0,9,232},{16,7,8}, 18 | {0,8,92},{0,8,28},{0,9,152},{20,7,83},{0,8,124},{0,8,60},{0,9,216}, 19 | {18,7,23},{0,8,108},{0,8,44},{0,9,184},{0,8,12},{0,8,140},{0,8,76}, 20 | {0,9,248},{16,7,3},{0,8,82},{0,8,18},{21,8,163},{19,7,35},{0,8,114}, 21 | {0,8,50},{0,9,196},{17,7,11},{0,8,98},{0,8,34},{0,9,164},{0,8,2}, 22 | {0,8,130},{0,8,66},{0,9,228},{16,7,7},{0,8,90},{0,8,26},{0,9,148}, 23 | {20,7,67},{0,8,122},{0,8,58},{0,9,212},{18,7,19},{0,8,106},{0,8,42}, 24 | {0,9,180},{0,8,10},{0,8,138},{0,8,74},{0,9,244},{16,7,5},{0,8,86}, 25 | {0,8,22},{64,8,0},{19,7,51},{0,8,118},{0,8,54},{0,9,204},{17,7,15}, 26 | {0,8,102},{0,8,38},{0,9,172},{0,8,6},{0,8,134},{0,8,70},{0,9,236}, 27 | {16,7,9},{0,8,94},{0,8,30},{0,9,156},{20,7,99},{0,8,126},{0,8,62}, 28 | {0,9,220},{18,7,27},{0,8,110},{0,8,46},{0,9,188},{0,8,14},{0,8,142}, 29 | {0,8,78},{0,9,252},{96,7,0},{0,8,81},{0,8,17},{21,8,131},{18,7,31}, 30 | {0,8,113},{0,8,49},{0,9,194},{16,7,10},{0,8,97},{0,8,33},{0,9,162}, 31 | {0,8,1},{0,8,129},{0,8,65},{0,9,226},{16,7,6},{0,8,89},{0,8,25}, 32 | {0,9,146},{19,7,59},{0,8,121},{0,8,57},{0,9,210},{17,7,17},{0,8,105}, 33 | {0,8,41},{0,9,178},{0,8,9},{0,8,137},{0,8,73},{0,9,242},{16,7,4}, 34 | {0,8,85},{0,8,21},{16,8,258},{19,7,43},{0,8,117},{0,8,53},{0,9,202}, 35 | {17,7,13},{0,8,101},{0,8,37},{0,9,170},{0,8,5},{0,8,133},{0,8,69}, 36 | {0,9,234},{16,7,8},{0,8,93},{0,8,29},{0,9,154},{20,7,83},{0,8,125}, 37 | {0,8,61},{0,9,218},{18,7,23},{0,8,109},{0,8,45},{0,9,186},{0,8,13}, 38 | {0,8,141},{0,8,77},{0,9,250},{16,7,3},{0,8,83},{0,8,19},{21,8,195}, 39 | {19,7,35},{0,8,115},{0,8,51},{0,9,198},{17,7,11},{0,8,99},{0,8,35}, 40 | {0,9,166},{0,8,3},{0,8,131},{0,8,67},{0,9,230},{16,7,7},{0,8,91}, 41 | {0,8,27},{0,9,150},{20,7,67},{0,8,123},{0,8,59},{0,9,214},{18,7,19}, 42 | {0,8,107},{0,8,43},{0,9,182},{0,8,11},{0,8,139},{0,8,75},{0,9,246}, 43 | {16,7,5},{0,8,87},{0,8,23},{64,8,0},{19,7,51},{0,8,119},{0,8,55}, 44 | {0,9,206},{17,7,15},{0,8,103},{0,8,39},{0,9,174},{0,8,7},{0,8,135}, 45 | {0,8,71},{0,9,238},{16,7,9},{0,8,95},{0,8,31},{0,9,158},{20,7,99}, 46 | {0,8,127},{0,8,63},{0,9,222},{18,7,27},{0,8,111},{0,8,47},{0,9,190}, 47 | {0,8,15},{0,8,143},{0,8,79},{0,9,254},{96,7,0},{0,8,80},{0,8,16}, 48 | {20,8,115},{18,7,31},{0,8,112},{0,8,48},{0,9,193},{16,7,10},{0,8,96}, 49 | {0,8,32},{0,9,161},{0,8,0},{0,8,128},{0,8,64},{0,9,225},{16,7,6}, 50 | {0,8,88},{0,8,24},{0,9,145},{19,7,59},{0,8,120},{0,8,56},{0,9,209}, 51 | {17,7,17},{0,8,104},{0,8,40},{0,9,177},{0,8,8},{0,8,136},{0,8,72}, 52 | {0,9,241},{16,7,4},{0,8,84},{0,8,20},{21,8,227},{19,7,43},{0,8,116}, 53 | {0,8,52},{0,9,201},{17,7,13},{0,8,100},{0,8,36},{0,9,169},{0,8,4}, 54 | {0,8,132},{0,8,68},{0,9,233},{16,7,8},{0,8,92},{0,8,28},{0,9,153}, 55 | {20,7,83},{0,8,124},{0,8,60},{0,9,217},{18,7,23},{0,8,108},{0,8,44}, 56 | {0,9,185},{0,8,12},{0,8,140},{0,8,76},{0,9,249},{16,7,3},{0,8,82}, 57 | {0,8,18},{21,8,163},{19,7,35},{0,8,114},{0,8,50},{0,9,197},{17,7,11}, 58 | {0,8,98},{0,8,34},{0,9,165},{0,8,2},{0,8,130},{0,8,66},{0,9,229}, 59 | {16,7,7},{0,8,90},{0,8,26},{0,9,149},{20,7,67},{0,8,122},{0,8,58}, 60 | {0,9,213},{18,7,19},{0,8,106},{0,8,42},{0,9,181},{0,8,10},{0,8,138}, 61 | {0,8,74},{0,9,245},{16,7,5},{0,8,86},{0,8,22},{64,8,0},{19,7,51}, 62 | {0,8,118},{0,8,54},{0,9,205},{17,7,15},{0,8,102},{0,8,38},{0,9,173}, 63 | {0,8,6},{0,8,134},{0,8,70},{0,9,237},{16,7,9},{0,8,94},{0,8,30}, 64 | {0,9,157},{20,7,99},{0,8,126},{0,8,62},{0,9,221},{18,7,27},{0,8,110}, 65 | {0,8,46},{0,9,189},{0,8,14},{0,8,142},{0,8,78},{0,9,253},{96,7,0}, 66 | {0,8,81},{0,8,17},{21,8,131},{18,7,31},{0,8,113},{0,8,49},{0,9,195}, 67 | {16,7,10},{0,8,97},{0,8,33},{0,9,163},{0,8,1},{0,8,129},{0,8,65}, 68 | {0,9,227},{16,7,6},{0,8,89},{0,8,25},{0,9,147},{19,7,59},{0,8,121}, 69 | {0,8,57},{0,9,211},{17,7,17},{0,8,105},{0,8,41},{0,9,179},{0,8,9}, 70 | {0,8,137},{0,8,73},{0,9,243},{16,7,4},{0,8,85},{0,8,21},{16,8,258}, 71 | {19,7,43},{0,8,117},{0,8,53},{0,9,203},{17,7,13},{0,8,101},{0,8,37}, 72 | {0,9,171},{0,8,5},{0,8,133},{0,8,69},{0,9,235},{16,7,8},{0,8,93}, 73 | {0,8,29},{0,9,155},{20,7,83},{0,8,125},{0,8,61},{0,9,219},{18,7,23}, 74 | {0,8,109},{0,8,45},{0,9,187},{0,8,13},{0,8,141},{0,8,77},{0,9,251}, 75 | {16,7,3},{0,8,83},{0,8,19},{21,8,195},{19,7,35},{0,8,115},{0,8,51}, 76 | {0,9,199},{17,7,11},{0,8,99},{0,8,35},{0,9,167},{0,8,3},{0,8,131}, 77 | {0,8,67},{0,9,231},{16,7,7},{0,8,91},{0,8,27},{0,9,151},{20,7,67}, 78 | {0,8,123},{0,8,59},{0,9,215},{18,7,19},{0,8,107},{0,8,43},{0,9,183}, 79 | {0,8,11},{0,8,139},{0,8,75},{0,9,247},{16,7,5},{0,8,87},{0,8,23}, 80 | {64,8,0},{19,7,51},{0,8,119},{0,8,55},{0,9,207},{17,7,15},{0,8,103}, 81 | {0,8,39},{0,9,175},{0,8,7},{0,8,135},{0,8,71},{0,9,239},{16,7,9}, 82 | {0,8,95},{0,8,31},{0,9,159},{20,7,99},{0,8,127},{0,8,63},{0,9,223}, 83 | {18,7,27},{0,8,111},{0,8,47},{0,9,191},{0,8,15},{0,8,143},{0,8,79}, 84 | {0,9,255} 85 | }; 86 | 87 | static const code distfix[32] = { 88 | {16,5,1},{23,5,257},{19,5,17},{27,5,4097},{17,5,5},{25,5,1025}, 89 | {21,5,65},{29,5,16385},{16,5,3},{24,5,513},{20,5,33},{28,5,8193}, 90 | {18,5,9},{26,5,2049},{22,5,129},{64,5,0},{16,5,2},{23,5,385}, 91 | {19,5,25},{27,5,6145},{17,5,7},{25,5,1537},{21,5,97},{29,5,24577}, 92 | {16,5,4},{24,5,769},{20,5,49},{28,5,12289},{18,5,13},{26,5,3073}, 93 | {22,5,193},{64,5,0} 94 | }; 95 | -------------------------------------------------------------------------------- /src/euc_kr.c: -------------------------------------------------------------------------------- 1 | /* euc_kr.c 2 | * EUC-KR handling functions 3 | */ 4 | 5 | /* C99 */ 6 | #include 7 | #include 8 | 9 | #include 10 | 11 | #include "unicode_table_uhc.h" 12 | #include 13 | 14 | static uint32_t euc_kr_strlen(const uint8_t *euckr) { 15 | uint32_t c=0; 16 | const uint8_t *t=euckr; 17 | while(*t) { 18 | uint8_t x=*t; t++; 19 | c++; 20 | if (x<0x80) continue; /* latin */ 21 | if (x>0xa0 && x<0xff && x!=0xc9) { 22 | if (*t == 0) return 0; /* ERG */ 23 | t++; 24 | c+=2; /* total size of a korean UTF8 char is 3 bytes */ 25 | continue; 26 | } 27 | c+=5; // max length, as we don't know what will happen 28 | // This case wasn't supposed to happen as we were expecting to get a 100% EUC-KR 29 | // file, however we got it anyway. Life is unpredictable. 30 | //return 0; /* should not happen -> throw fatal */ 31 | } 32 | return c; 33 | } 34 | 35 | static uint32_t utf8_strlen(const uint8_t *utf8) { 36 | uint32_t c=0; 37 | const uint8_t *t = utf8; 38 | while(*t) { 39 | uint8_t x=*t; t++; c++; 40 | uint8_t l=0; 41 | if ((x & 0x80) == 0) continue; // ASCII range 42 | if ((x & 0xe0) == 0xc0) { // 2chars utf8 43 | l=1; 44 | } else if ((x & 0xf0) == 0xe0) { // 3chars utf8 45 | l=2; 46 | } else if ((x & 0xf8) == 0xf0) { // 4chars utf8 47 | l=3; 48 | } else if ((x & 0xfc) == 0xf8) { // 5chars utf8 (illegal) 49 | l=4; 50 | } else if ((x & 0xfe) == 0xfc) { // 6chars utf8 (illegal) 51 | l=5; 52 | } 53 | if (l==0) return 0; /* ILLEGAL */ 54 | for (int i=0;i= 0x70000000) || (c <= 0)) { /* MBFL_WCSGROUP_UCS4MAX */ 64 | // illegal character 65 | return; 66 | } 67 | if (c < 0x80) { /* 1 char */ 68 | **r = c; 69 | (*r)++; 70 | return; 71 | } 72 | if (c < 0x800) { /* 2 chars */ 73 | **r = ((c >> 6) & 0x1f) | 0xc0; (*r)++; 74 | **r = (c & 0x3f) | 0x80; (*r)++; 75 | return; 76 | } 77 | if (c < 0x10000) { /* 3 chars */ 78 | **r = ((c >> 12) & 0x0f) | 0xe0; (*r)++; 79 | **r = ((c >> 6) & 0x3f) | 0x80; (*r)++; 80 | **r = (c & 0x3f) | 0x80; (*r)++; 81 | return; 82 | } 83 | if (c < 0x200000) { /* 4 chars */ 84 | **r = ((c >> 18) & 0x07) | 0xf0; (*r)++; 85 | **r = ((c >> 12) & 0x3f) | 0x80; (*r)++; 86 | **r = ((c >> 6) & 0x3f) | 0x80; (*r)++; 87 | **r = (c & 0x3f) | 0x80; (*r)++; 88 | return; 89 | } 90 | if (c < 0x4000000) { /* 5 chars */ 91 | **r = ((c >> 24) & 0x03) | 0xf8; (*r)++; 92 | **r = ((c >> 18) & 0x3f) | 0x80; (*r)++; 93 | **r = ((c >> 12) & 0x3f) | 0x80; (*r)++; 94 | **r = ((c >> 6) & 0x3f) | 0x80; (*r)++; 95 | **r = (c & 0x3f) | 0x80; (*r)++; 96 | return; 97 | } 98 | /* 6 chars */ 99 | **r = ((c >> 30) & 0x01) | 0xfc; (*r)++; 100 | **r = ((c >> 24) & 0x3f) | 0x80; (*r)++; 101 | **r = ((c >> 18) & 0x3f) | 0x80; (*r)++; 102 | **r = ((c >> 12) & 0x3f) | 0x80; (*r)++; 103 | **r = ((c >> 6) & 0x3f) | 0x80; (*r)++; 104 | **r = (c & 0x3f) | 0x80; (*r)++; 105 | } 106 | 107 | static bool euc_kr_append_from_wchar(uint8_t **r, int c) { 108 | int s=0, c1, c2; 109 | if (c >= ucs_a1_uhc_table_min && c < ucs_a1_uhc_table_max) { 110 | s = ucs_a1_uhc_table[c - ucs_a1_uhc_table_min]; 111 | } else if (c >= ucs_a2_uhc_table_min && c < ucs_a2_uhc_table_max) { 112 | s = ucs_a2_uhc_table[c - ucs_a2_uhc_table_min]; 113 | } else if (c >= ucs_a3_uhc_table_min && c < ucs_a3_uhc_table_max) { 114 | s = ucs_a3_uhc_table[c - ucs_a3_uhc_table_min]; 115 | } else if (c >= ucs_i_uhc_table_min && c < ucs_i_uhc_table_max) { 116 | s = ucs_i_uhc_table[c - ucs_i_uhc_table_min]; 117 | } else if (c >= ucs_s_uhc_table_min && c < ucs_s_uhc_table_max) { 118 | s = ucs_s_uhc_table[c - ucs_s_uhc_table_min]; 119 | } else if (c >= ucs_r1_uhc_table_min && c < ucs_r1_uhc_table_max) { 120 | s = ucs_r1_uhc_table[c - ucs_r1_uhc_table_min]; 121 | } else if (c >= ucs_r2_uhc_table_min && c < ucs_r2_uhc_table_max) { 122 | s = ucs_r2_uhc_table[c - ucs_r2_uhc_table_min]; 123 | } 124 | 125 | c1 = (s >> 8) & 0xff; 126 | c2 = s & 0xff; 127 | /* exclude UHC extension area */ 128 | if (c1 < 0xa1 || c2 < 0xa1){ 129 | s = c; 130 | } 131 | 132 | if (s <= 0) { 133 | return false; /* error */ 134 | } 135 | if (s < 0x80) { /* latin */ 136 | **r = s; (*r)++; 137 | return true; 138 | } 139 | **r = ((s>>8)&0xff); (*r)++; 140 | **r = (s&0xff); (*r)++; 141 | return true; 142 | } 143 | 144 | GRFEXPORT char *utf8_to_euc_kr_r(const char *orig, uint8_t *res) { 145 | int c; 146 | uint32_t flen = utf8_strlen((const uint8_t *)orig); 147 | uint8_t *t = (uint8_t *)orig; 148 | uint8_t *r = res; 149 | if (flen == 0) return NULL; 150 | while (*t) { 151 | uint8_t x = *t; 152 | uint8_t l = 0, p; 153 | t++; 154 | if (x<0x80) { 155 | // easy one, that's the same char in UTF-8 156 | *r = x; r++; 157 | continue; 158 | } 159 | if ((x & 0xe0) == 0xc0) { // 2chars utf8 160 | l=1; p=0x1f; 161 | } else if ((x & 0xf0) == 0xe0) { // 3chars utf8 162 | l=2; p=0xf; 163 | } else if ((x & 0xf8) == 0xf0) { // 4chars utf8 164 | l=3; p=0x7; 165 | } else if ((x & 0xfc) == 0xf8) { // 5chars utf8 (illegal) 166 | l=4; p=0x3; 167 | } else if ((x & 0xfe) == 0xfc) { // 6chars utf8 (illegal) 168 | l=5; p=0x1; 169 | } 170 | if (l==0) return NULL; /* ILLEGAL */ 171 | c = x & p; 172 | for(int i=0;i= 0xff || x == 0xc9) { 196 | /* unexpected: skip character and hope tomorrow will be a better day */ 197 | t++; continue; 198 | // return NULL; 199 | } 200 | t++; 201 | x2 = *t; t++; 202 | flag = 0; 203 | if (x >= 0xa1 && x <= 0xc6) { 204 | flag = 1; 205 | } else if (x >= 0xc7 && x <= 0xfe && x != 0xc9) { 206 | flag = 2; 207 | } 208 | if (flag > 0 && x2 >= 0xa1 && x2 <= 0xfe) { 209 | if (flag == 1) { /* 1st: 0xa1..0xc6, 2nd: 0x41..0x7a, 0x81..0xfe */ 210 | c = (x - 0xa1)*190 + (x2 - 0x41); 211 | if (c >= 0 && c < uhc2_ucs_table_size) { 212 | c = uhc2_ucs_table[c]; 213 | } else { 214 | c = 0; 215 | } 216 | } else { /* 1st: 0xc7..0xc8,0xca..0xfe, 2nd: 0xa1..0xfe */ 217 | c = (x - 0xc7)*94 + (x2 - 0xa1); 218 | if (c >= 0 && c < uhc3_ucs_table_size) { 219 | c = uhc3_ucs_table[c]; 220 | } else { 221 | c = 0; 222 | } 223 | } 224 | if (c <= 0) { 225 | c = (x << 8) | x2; 226 | c &= 0xffff; /* MBFL_WCSPLANE_MASK */ 227 | c |= 0x70f10000; /* MBFL_WCSPLANE_KSC5601: 2121h - 7E7Eh */ 228 | } 229 | } else if ((x2 < 0x21) || x2 == 0x7f) { /* CTLs */ 230 | continue; 231 | } else { 232 | c = (x << 8) | x2; 233 | c &= 0xffffff; /* MBFL_WCSGROUP_MASK */ 234 | c |= 0x78000000; /* MBFL_WCSGROUP_THROUGH: 000000h - FFFFFFh */ 235 | } 236 | utf8_append_from_wchar(&r, c); 237 | } 238 | *r = 0; 239 | return (char *)res; 240 | } 241 | 242 | GRFEXPORT char *euc_kr_to_utf8(const char *orig) { 243 | static uint8_t *res[4096]; // should be enough 244 | return euc_kr_to_utf8_r(orig, (uint8_t *)&res); 245 | } 246 | 247 | GRFEXPORT char *utf8_to_euc_kr(const char *orig) { 248 | static uint8_t *res[4096]; // should be enough 249 | return utf8_to_euc_kr_r(orig, (uint8_t *)&res); 250 | } 251 | 252 | -------------------------------------------------------------------------------- /zlib/zutil.h: -------------------------------------------------------------------------------- 1 | /* zutil.h -- internal interface and configuration of the compression library 2 | * Copyright (C) 1995-2005 Jean-loup Gailly. 3 | * For conditions of distribution and use, see copyright notice in zlib.h 4 | */ 5 | 6 | /* WARNING: this file should *not* be used by applications. It is 7 | part of the implementation of the compression library and is 8 | subject to change. Applications should only use zlib.h. 9 | */ 10 | 11 | /* @(#) $Id$ */ 12 | 13 | #ifndef ZUTIL_H 14 | #define ZUTIL_H 15 | 16 | #define ZLIB_INTERNAL 17 | #include "zlib.h" 18 | 19 | #ifdef STDC 20 | # ifndef _WIN32_WCE 21 | # include 22 | # endif 23 | # include 24 | # include 25 | #endif 26 | #ifdef NO_ERRNO_H 27 | # ifdef _WIN32_WCE 28 | /* The Microsoft C Run-Time Library for Windows CE doesn't have 29 | * errno. We define it as a global variable to simplify porting. 30 | * Its value is always 0 and should not be used. We rename it to 31 | * avoid conflict with other libraries that use the same workaround. 32 | */ 33 | # define errno z_errno 34 | # endif 35 | extern int errno; 36 | #else 37 | # ifndef _WIN32_WCE 38 | # include 39 | # endif 40 | #endif 41 | 42 | #ifndef local 43 | # define local static 44 | #endif 45 | /* compile with -Dlocal if your debugger can't find static symbols */ 46 | 47 | typedef unsigned char uch; 48 | typedef uch FAR uchf; 49 | typedef unsigned short ush; 50 | typedef ush FAR ushf; 51 | typedef unsigned long ulg; 52 | 53 | extern const char * const z_errmsg[10]; /* indexed by 2-zlib_error */ 54 | /* (size given to avoid silly warnings with Visual C++) */ 55 | 56 | #define ERR_MSG(err) z_errmsg[Z_NEED_DICT-(err)] 57 | 58 | #define ERR_RETURN(strm,err) \ 59 | return (strm->msg = (char*)ERR_MSG(err), (err)) 60 | /* To be used only when the state is known to be valid */ 61 | 62 | /* common constants */ 63 | 64 | #ifndef DEF_WBITS 65 | # define DEF_WBITS MAX_WBITS 66 | #endif 67 | /* default windowBits for decompression. MAX_WBITS is for compression only */ 68 | 69 | #if MAX_MEM_LEVEL >= 8 70 | # define DEF_MEM_LEVEL 8 71 | #else 72 | # define DEF_MEM_LEVEL MAX_MEM_LEVEL 73 | #endif 74 | /* default memLevel */ 75 | 76 | #define STORED_BLOCK 0 77 | #define STATIC_TREES 1 78 | #define DYN_TREES 2 79 | /* The three kinds of block type */ 80 | 81 | #define MIN_MATCH 3 82 | #define MAX_MATCH 258 83 | /* The minimum and maximum match lengths */ 84 | 85 | #define PRESET_DICT 0x20 /* preset dictionary flag in zlib header */ 86 | 87 | /* target dependencies */ 88 | 89 | #if defined(MSDOS) || (defined(WINDOWS) && !defined(WIN32)) 90 | # define OS_CODE 0x00 91 | # if defined(__TURBOC__) || defined(__BORLANDC__) 92 | # if(__STDC__ == 1) && (defined(__LARGE__) || defined(__COMPACT__)) 93 | /* Allow compilation with ANSI keywords only enabled */ 94 | void _Cdecl farfree( void *block ); 95 | void *_Cdecl farmalloc( unsigned long nbytes ); 96 | # else 97 | # include 98 | # endif 99 | # else /* MSC or DJGPP */ 100 | # include 101 | # endif 102 | #endif 103 | 104 | #ifdef AMIGA 105 | # define OS_CODE 0x01 106 | #endif 107 | 108 | #if defined(VAXC) || defined(VMS) 109 | # define OS_CODE 0x02 110 | # define F_OPEN(name, mode) \ 111 | fopen((name), (mode), "mbc=60", "ctx=stm", "rfm=fix", "mrs=512") 112 | #endif 113 | 114 | #if defined(ATARI) || defined(atarist) 115 | # define OS_CODE 0x05 116 | #endif 117 | 118 | #ifdef OS2 119 | # define OS_CODE 0x06 120 | # ifdef M_I86 121 | #include 122 | # endif 123 | #endif 124 | 125 | #if defined(MACOS) || defined(TARGET_OS_MAC) 126 | # define OS_CODE 0x07 127 | # if defined(__MWERKS__) && __dest_os != __be_os && __dest_os != __win32_os 128 | # include /* for fdopen */ 129 | # else 130 | # ifndef fdopen 131 | # define fdopen(fd,mode) NULL /* No fdopen() */ 132 | # endif 133 | # endif 134 | #endif 135 | 136 | #ifdef TOPS20 137 | # define OS_CODE 0x0a 138 | #endif 139 | 140 | #ifdef WIN32 141 | # ifndef __CYGWIN__ /* Cygwin is Unix, not Win32 */ 142 | # define OS_CODE 0x0b 143 | # endif 144 | #endif 145 | 146 | #ifdef __50SERIES /* Prime/PRIMOS */ 147 | # define OS_CODE 0x0f 148 | #endif 149 | 150 | #if defined(_BEOS_) || defined(RISCOS) 151 | # define fdopen(fd,mode) NULL /* No fdopen() */ 152 | #endif 153 | 154 | #if (defined(_MSC_VER) && (_MSC_VER > 600)) 155 | # if defined(_WIN32_WCE) 156 | # define fdopen(fd,mode) NULL /* No fdopen() */ 157 | # ifndef _PTRDIFF_T_DEFINED 158 | typedef int ptrdiff_t; 159 | # define _PTRDIFF_T_DEFINED 160 | # endif 161 | # else 162 | # define fdopen(fd,type) _fdopen(fd,type) 163 | # endif 164 | #endif 165 | 166 | /* common defaults */ 167 | 168 | #ifndef OS_CODE 169 | # define OS_CODE 0x03 /* assume Unix */ 170 | #endif 171 | 172 | #ifndef F_OPEN 173 | # define F_OPEN(name, mode) fopen((name), (mode)) 174 | #endif 175 | 176 | /* functions */ 177 | 178 | #if defined(STDC99) || (defined(__TURBOC__) && __TURBOC__ >= 0x550) 179 | # ifndef HAVE_VSNPRINTF 180 | # define HAVE_VSNPRINTF 181 | # endif 182 | #endif 183 | #if defined(__CYGWIN__) 184 | # ifndef HAVE_VSNPRINTF 185 | # define HAVE_VSNPRINTF 186 | # endif 187 | #endif 188 | #ifndef HAVE_VSNPRINTF 189 | # ifdef MSDOS 190 | /* vsnprintf may exist on some MS-DOS compilers (DJGPP?), 191 | but for now we just assume it doesn't. */ 192 | # define NO_vsnprintf 193 | # endif 194 | # ifdef __TURBOC__ 195 | # define NO_vsnprintf 196 | # endif 197 | # ifdef WIN32 198 | /* In Win32, vsnprintf is available as the "non-ANSI" _vsnprintf. */ 199 | # if !defined(vsnprintf) && !defined(NO_vsnprintf) 200 | # define vsnprintf _vsnprintf 201 | # endif 202 | # endif 203 | # ifdef __SASC 204 | # define NO_vsnprintf 205 | # endif 206 | #endif 207 | #ifdef VMS 208 | # define NO_vsnprintf 209 | #endif 210 | 211 | #if defined(pyr) 212 | # define NO_MEMCPY 213 | #endif 214 | #if defined(SMALL_MEDIUM) && !defined(_MSC_VER) && !defined(__SC__) 215 | /* Use our own functions for small and medium model with MSC <= 5.0. 216 | * You may have to use the same strategy for Borland C (untested). 217 | * The __SC__ check is for Symantec. 218 | */ 219 | # define NO_MEMCPY 220 | #endif 221 | #if defined(STDC) && !defined(HAVE_MEMCPY) && !defined(NO_MEMCPY) 222 | # define HAVE_MEMCPY 223 | #endif 224 | #ifdef HAVE_MEMCPY 225 | # ifdef SMALL_MEDIUM /* MSDOS small or medium model */ 226 | # define zmemcpy _fmemcpy 227 | # define zmemcmp _fmemcmp 228 | # define zmemzero(dest, len) _fmemset(dest, 0, len) 229 | # else 230 | # define zmemcpy memcpy 231 | # define zmemcmp memcmp 232 | # define zmemzero(dest, len) memset(dest, 0, len) 233 | # endif 234 | #else 235 | extern void zmemcpy OF((Bytef* dest, const Bytef* source, uInt len)); 236 | extern int zmemcmp OF((const Bytef* s1, const Bytef* s2, uInt len)); 237 | extern void zmemzero OF((Bytef* dest, uInt len)); 238 | #endif 239 | 240 | /* Diagnostic functions */ 241 | #ifdef DEBUG 242 | # include 243 | extern int z_verbose; 244 | extern void z_error OF((char *m)); 245 | # define Assert(cond,msg) {if(!(cond)) z_error(msg);} 246 | # define Trace(x) {if (z_verbose>=0) fprintf x ;} 247 | # define Tracev(x) {if (z_verbose>0) fprintf x ;} 248 | # define Tracevv(x) {if (z_verbose>1) fprintf x ;} 249 | # define Tracec(c,x) {if (z_verbose>0 && (c)) fprintf x ;} 250 | # define Tracecv(c,x) {if (z_verbose>1 && (c)) fprintf x ;} 251 | #else 252 | # define Assert(cond,msg) 253 | # define Trace(x) 254 | # define Tracev(x) 255 | # define Tracevv(x) 256 | # define Tracec(c,x) 257 | # define Tracecv(c,x) 258 | #endif 259 | 260 | 261 | voidpf zcalloc OF((voidpf opaque, unsigned items, unsigned size)); 262 | void zcfree OF((voidpf opaque, voidpf ptr)); 263 | 264 | #define ZALLOC(strm, items, size) \ 265 | (*((strm)->zalloc))((strm)->opaque, (items), (size)) 266 | #define ZFREE(strm, addr) (*((strm)->zfree))((strm)->opaque, (voidpf)(addr)) 267 | #define TRY_FREE(s, p) {if (p) ZFREE(s, p);} 268 | 269 | #endif /* ZUTIL_H */ 270 | -------------------------------------------------------------------------------- /src/hash_tables.c: -------------------------------------------------------------------------------- 1 | /* hash_tables.c : Hash tables manager 2 | * $Id: hash_tables.c 2 2005-07-24 23:28:07Z magicaltux $ 3 | * 4 | */ 5 | 6 | /* System includes */ 7 | #include 8 | #include 9 | 10 | /* Local includes */ 11 | #include 12 | #include 13 | 14 | /* Our hash function 15 | * It is based on the published UNIX ELF hash algorythm used to find object files 16 | * This version is less efficient but fits our needs 17 | */ 18 | unsigned long hash_calc(char *name, unsigned long limit) { 19 | unsigned long h = 0, g; 20 | while(*name) { 21 | h = (h << 4) + *(name++); 22 | /* gcc3 will display a warning if we don't put the additional parentheses */ 23 | if ( (g = (h & 0xF0000000)) ) h ^= (g >> 24); 24 | h &= ~g; 25 | } 26 | h = h % limit; 27 | return h; 28 | } 29 | 30 | /* a little function to get lowercased string 31 | */ 32 | char *strduptolower(const char *str) { 33 | char *res,*tmp; 34 | res=(char *)strdup(str); 35 | tmp=res-1; 36 | while(*(++tmp)) { 37 | if ((*tmp>='A') && (*tmp<='Z')) *tmp+=32; 38 | if (*tmp=='\\') *tmp='/'; 39 | } 40 | return res; 41 | } 42 | 43 | hash_table *hash_create_table(unsigned long size, void *func) { 44 | hash_table *new_table; 45 | 46 | if (size<1) return NULL; /* illegal table size */ 47 | 48 | /* Attempt to malloc some memory */ 49 | if ((new_table = calloc(1, sizeof(hash_table))) == NULL) { 50 | return NULL; 51 | } 52 | new_table->free_func = func; 53 | 54 | /* get some memory for our dynamic array */ 55 | if ((new_table->table = calloc(1, sizeof(list_element)*size)) == NULL) { 56 | free(new_table); 57 | return NULL; 58 | } 59 | 60 | new_table->size=size; 61 | 62 | return new_table; 63 | } 64 | 65 | list_element *hash_lookup_raw(hash_table *table, const char *string) { 66 | list_element *element; 67 | unsigned long hash_val; 68 | char *str; 69 | 70 | str=strduptolower(string); 71 | hash_val=hash_calc(str, table->size); 72 | for(element=table->table[hash_val]; element != NULL; element=element->next) { 73 | if (strcmp(str, element->string)==0) { 74 | free(str); 75 | return element; 76 | } 77 | } 78 | free(str); 79 | 80 | return NULL; 81 | } 82 | 83 | void *hash_lookup(hash_table *table, const char *string) { 84 | list_element *element; 85 | element=hash_lookup_raw(table,string); 86 | 87 | if (element == NULL) return NULL; 88 | 89 | return element->pointer; 90 | } 91 | 92 | int hash_set_element(hash_table *table, char *string, void *pointer, int delold) { 93 | list_element *current_element; 94 | 95 | if (table == NULL) return 1; 96 | 97 | current_element = hash_lookup_raw(table, string); 98 | if (current_element == NULL) return 2; 99 | 100 | if ( (delold != 0 ) && (table->free_func != NULL)) (table->free_func)(current_element->pointer); 101 | current_element->pointer = pointer; 102 | return 0; 103 | } 104 | 105 | int hash_add_element(hash_table *table, char *string, void *pointer) { 106 | list_element *new_element; 107 | list_element *current_element; 108 | unsigned long hashval; 109 | char *str; 110 | 111 | str=strduptolower(string); 112 | hashval=hash_calc(str, table->size); 113 | current_element=hash_lookup_raw(table, string); 114 | if (current_element != NULL) { 115 | free(str); 116 | return 2; /* already present in hash table */ 117 | } 118 | 119 | if ((new_element = malloc(sizeof(list_element))) == NULL) { 120 | free(str); 121 | return 1; 122 | } 123 | 124 | new_element->string = str; 125 | new_element->next = table->table[hashval]; 126 | new_element->pointer = pointer; 127 | table->table[hashval]=new_element; 128 | table->count += 1; 129 | 130 | return 0; 131 | } 132 | 133 | int hash_del_element(hash_table *table, char *string) { 134 | list_element *old_element; 135 | list_element *current_element, *prev; 136 | unsigned long hashval; 137 | char *str; 138 | 139 | str=strduptolower(string); 140 | hashval=hash_calc(str, table->size); 141 | old_element=hash_lookup_raw(table, str); 142 | free(str); 143 | if (old_element == NULL) return 1; /* not found in table */ 144 | if (table->free_func != NULL) (table->free_func)(old_element->pointer); 145 | 146 | prev=NULL; 147 | for(current_element=table->table[hashval]; current_element != NULL; current_element=current_element->next) { 148 | if (current_element==old_element) { 149 | if (prev==NULL) { 150 | table->table[hashval] = current_element->next; 151 | table->count -= 1; 152 | free(current_element->string); 153 | free(current_element); 154 | return 0; 155 | } 156 | prev->next=current_element->next; 157 | table->count -= 1; 158 | free(current_element->string); 159 | free(current_element); 160 | return 0; 161 | } 162 | prev=current_element; 163 | } 164 | return 2; /* not found ! */ 165 | } 166 | 167 | int hash_remove_element(hash_table *table, char *string) { 168 | /* same as previous, but do not free the element */ 169 | list_element *old_element; 170 | list_element *current_element, *prev; 171 | unsigned long hashval; 172 | char *str; 173 | 174 | str=strduptolower(string); 175 | hashval=hash_calc(str, table->size); 176 | old_element=hash_lookup_raw(table, str); 177 | free(str); 178 | if (old_element == NULL) return 1; /* not found in table */ 179 | 180 | prev=NULL; 181 | for(current_element=table->table[hashval]; current_element != NULL; current_element=current_element->next) { 182 | if (current_element==old_element) { 183 | if (prev==NULL) { 184 | table->table[hashval] = current_element->next; 185 | table->count -= 1; 186 | free(current_element->string); 187 | free(current_element); 188 | return 0; 189 | } 190 | prev->next=current_element->next; 191 | table->count -= 1; 192 | free(current_element->string); 193 | free(current_element); 194 | return 0; 195 | } 196 | prev=current_element; 197 | } 198 | return 2; /* not found ! */ 199 | } 200 | 201 | void hash_free_table(hash_table *table) { 202 | int i; 203 | list_element *cur,*prev; 204 | 205 | if (table == NULL) return; 206 | 207 | for(i=0;isize; i++) { 208 | cur=table->table[i]; 209 | while(cur!=NULL) { 210 | if (table->free_func != NULL) (table->free_func)(cur->pointer); 211 | prev=cur; 212 | cur=cur->next; 213 | free(prev->string); 214 | free(prev); 215 | } 216 | } 217 | 218 | free(table->table); 219 | free(table); 220 | } 221 | 222 | list_element **hash_foreach(hash_table *table) { 223 | /* will return an array of pointers with every entry of the hash table list */ 224 | list_element **result, *cur; 225 | int num_entries=0,num_alloc=0, i; 226 | result = NULL; 227 | 228 | if (table == NULL) return NULL; 229 | 230 | for(i=0;isize; i++) { 231 | cur=table->table[i]; 232 | while(cur != NULL) { 233 | num_entries++; 234 | if (num_entries > num_alloc) { 235 | num_alloc+=512; 236 | if (result == NULL) { 237 | result = calloc(num_alloc, sizeof(void *)); 238 | } else { 239 | result = realloc(result, sizeof(void *) * num_alloc); 240 | } 241 | } 242 | result[num_entries-1]=cur; 243 | cur=cur->next; 244 | } 245 | } 246 | if (result != NULL) { 247 | result = realloc(result, sizeof(void *) * (num_entries+1)); 248 | result[num_entries]=NULL; 249 | } 250 | return result; 251 | } 252 | 253 | void **hash_foreach_val(hash_table *table) { 254 | /* will return an array of pointers with every pointer in the list */ 255 | void **result; 256 | list_element *cur; 257 | int num_entries=0,num_alloc=0, i; 258 | result = NULL; 259 | 260 | if (table == NULL) return NULL; 261 | 262 | for(i=0;isize; i++) { 263 | cur=table->table[i]; 264 | while(cur != NULL) { 265 | num_entries++; 266 | if (num_entries > num_alloc) { 267 | num_alloc+=512; 268 | if (result == NULL) { 269 | result = calloc(num_alloc, sizeof(void *)); 270 | } else { 271 | result = realloc(result, sizeof(void *) * num_alloc); 272 | } 273 | } 274 | result[num_entries-1]=cur->pointer; 275 | cur=cur->next; 276 | } 277 | } 278 | if (result != NULL) { 279 | result = realloc(result, sizeof(void *) * (num_entries+1)); 280 | result[num_entries]=NULL; 281 | } 282 | return result; 283 | } 284 | 285 | -------------------------------------------------------------------------------- /zlib/zutil.c: -------------------------------------------------------------------------------- 1 | /* zutil.c -- target dependent utility functions for the compression library 2 | * Copyright (C) 1995-2005 Jean-loup Gailly. 3 | * For conditions of distribution and use, see copyright notice in zlib.h 4 | */ 5 | 6 | /* @(#) $Id$ */ 7 | 8 | #include "zutil.h" 9 | 10 | #ifndef NO_DUMMY_DECL 11 | struct internal_state {int dummy;}; /* for buggy compilers */ 12 | #endif 13 | 14 | const char * const z_errmsg[10] = { 15 | "need dictionary", /* Z_NEED_DICT 2 */ 16 | "stream end", /* Z_STREAM_END 1 */ 17 | "", /* Z_OK 0 */ 18 | "file error", /* Z_ERRNO (-1) */ 19 | "stream error", /* Z_STREAM_ERROR (-2) */ 20 | "data error", /* Z_DATA_ERROR (-3) */ 21 | "insufficient memory", /* Z_MEM_ERROR (-4) */ 22 | "buffer error", /* Z_BUF_ERROR (-5) */ 23 | "incompatible version",/* Z_VERSION_ERROR (-6) */ 24 | ""}; 25 | 26 | 27 | const char * ZEXPORT zlibVersion() 28 | { 29 | return ZLIB_VERSION; 30 | } 31 | 32 | uLong ZEXPORT zlibCompileFlags() 33 | { 34 | uLong flags; 35 | 36 | flags = 0; 37 | switch (sizeof(uInt)) { 38 | case 2: break; 39 | case 4: flags += 1; break; 40 | case 8: flags += 2; break; 41 | default: flags += 3; 42 | } 43 | switch (sizeof(uLong)) { 44 | case 2: break; 45 | case 4: flags += 1 << 2; break; 46 | case 8: flags += 2 << 2; break; 47 | default: flags += 3 << 2; 48 | } 49 | switch (sizeof(voidpf)) { 50 | case 2: break; 51 | case 4: flags += 1 << 4; break; 52 | case 8: flags += 2 << 4; break; 53 | default: flags += 3 << 4; 54 | } 55 | switch (sizeof(z_off_t)) { 56 | case 2: break; 57 | case 4: flags += 1 << 6; break; 58 | case 8: flags += 2 << 6; break; 59 | default: flags += 3 << 6; 60 | } 61 | #ifdef DEBUG 62 | flags += 1 << 8; 63 | #endif 64 | #if defined(ASMV) || defined(ASMINF) 65 | flags += 1 << 9; 66 | #endif 67 | #ifdef ZLIB_WINAPI 68 | flags += 1 << 10; 69 | #endif 70 | #ifdef BUILDFIXED 71 | flags += 1 << 12; 72 | #endif 73 | #ifdef DYNAMIC_CRC_TABLE 74 | flags += 1 << 13; 75 | #endif 76 | #ifdef NO_GZCOMPRESS 77 | flags += 1L << 16; 78 | #endif 79 | #ifdef NO_GZIP 80 | flags += 1L << 17; 81 | #endif 82 | #ifdef PKZIP_BUG_WORKAROUND 83 | flags += 1L << 20; 84 | #endif 85 | #ifdef FASTEST 86 | flags += 1L << 21; 87 | #endif 88 | #ifdef STDC 89 | # ifdef NO_vsnprintf 90 | flags += 1L << 25; 91 | # ifdef HAS_vsprintf_void 92 | flags += 1L << 26; 93 | # endif 94 | # else 95 | # ifdef HAS_vsnprintf_void 96 | flags += 1L << 26; 97 | # endif 98 | # endif 99 | #else 100 | flags += 1L << 24; 101 | # ifdef NO_snprintf 102 | flags += 1L << 25; 103 | # ifdef HAS_sprintf_void 104 | flags += 1L << 26; 105 | # endif 106 | # else 107 | # ifdef HAS_snprintf_void 108 | flags += 1L << 26; 109 | # endif 110 | # endif 111 | #endif 112 | return flags; 113 | } 114 | 115 | #ifdef DEBUG 116 | 117 | # ifndef verbose 118 | # define verbose 0 119 | # endif 120 | int z_verbose = verbose; 121 | 122 | void z_error (m) 123 | char *m; 124 | { 125 | fprintf(stderr, "%s\n", m); 126 | exit(1); 127 | } 128 | #endif 129 | 130 | /* exported to allow conversion of error code to string for compress() and 131 | * uncompress() 132 | */ 133 | const char * ZEXPORT zError(err) 134 | int err; 135 | { 136 | return ERR_MSG(err); 137 | } 138 | 139 | #if defined(_WIN32_WCE) 140 | /* The Microsoft C Run-Time Library for Windows CE doesn't have 141 | * errno. We define it as a global variable to simplify porting. 142 | * Its value is always 0 and should not be used. 143 | */ 144 | int errno = 0; 145 | #endif 146 | 147 | #ifndef HAVE_MEMCPY 148 | 149 | void zmemcpy(dest, source, len) 150 | Bytef* dest; 151 | const Bytef* source; 152 | uInt len; 153 | { 154 | if (len == 0) return; 155 | do { 156 | *dest++ = *source++; /* ??? to be unrolled */ 157 | } while (--len != 0); 158 | } 159 | 160 | int zmemcmp(s1, s2, len) 161 | const Bytef* s1; 162 | const Bytef* s2; 163 | uInt len; 164 | { 165 | uInt j; 166 | 167 | for (j = 0; j < len; j++) { 168 | if (s1[j] != s2[j]) return 2*(s1[j] > s2[j])-1; 169 | } 170 | return 0; 171 | } 172 | 173 | void zmemzero(dest, len) 174 | Bytef* dest; 175 | uInt len; 176 | { 177 | if (len == 0) return; 178 | do { 179 | *dest++ = 0; /* ??? to be unrolled */ 180 | } while (--len != 0); 181 | } 182 | #endif 183 | 184 | 185 | #ifdef SYS16BIT 186 | 187 | #ifdef __TURBOC__ 188 | /* Turbo C in 16-bit mode */ 189 | 190 | # define MY_ZCALLOC 191 | 192 | /* Turbo C malloc() does not allow dynamic allocation of 64K bytes 193 | * and farmalloc(64K) returns a pointer with an offset of 8, so we 194 | * must fix the pointer. Warning: the pointer must be put back to its 195 | * original form in order to free it, use zcfree(). 196 | */ 197 | 198 | #define MAX_PTR 10 199 | /* 10*64K = 640K */ 200 | 201 | local int next_ptr = 0; 202 | 203 | typedef struct ptr_table_s { 204 | voidpf org_ptr; 205 | voidpf new_ptr; 206 | } ptr_table; 207 | 208 | local ptr_table table[MAX_PTR]; 209 | /* This table is used to remember the original form of pointers 210 | * to large buffers (64K). Such pointers are normalized with a zero offset. 211 | * Since MSDOS is not a preemptive multitasking OS, this table is not 212 | * protected from concurrent access. This hack doesn't work anyway on 213 | * a protected system like OS/2. Use Microsoft C instead. 214 | */ 215 | 216 | voidpf zcalloc (voidpf opaque, unsigned items, unsigned size) 217 | { 218 | voidpf buf = opaque; /* just to make some compilers happy */ 219 | ulg bsize = (ulg)items*size; 220 | 221 | /* If we allocate less than 65520 bytes, we assume that farmalloc 222 | * will return a usable pointer which doesn't have to be normalized. 223 | */ 224 | if (bsize < 65520L) { 225 | buf = farmalloc(bsize); 226 | if (*(ush*)&buf != 0) return buf; 227 | } else { 228 | buf = farmalloc(bsize + 16L); 229 | } 230 | if (buf == NULL || next_ptr >= MAX_PTR) return NULL; 231 | table[next_ptr].org_ptr = buf; 232 | 233 | /* Normalize the pointer to seg:0 */ 234 | *((ush*)&buf+1) += ((ush)((uch*)buf-0) + 15) >> 4; 235 | *(ush*)&buf = 0; 236 | table[next_ptr++].new_ptr = buf; 237 | return buf; 238 | } 239 | 240 | void zcfree (voidpf opaque, voidpf ptr) 241 | { 242 | int n; 243 | if (*(ush*)&ptr != 0) { /* object < 64K */ 244 | farfree(ptr); 245 | return; 246 | } 247 | /* Find the original pointer */ 248 | for (n = 0; n < next_ptr; n++) { 249 | if (ptr != table[n].new_ptr) continue; 250 | 251 | farfree(table[n].org_ptr); 252 | while (++n < next_ptr) { 253 | table[n-1] = table[n]; 254 | } 255 | next_ptr--; 256 | return; 257 | } 258 | ptr = opaque; /* just to make some compilers happy */ 259 | Assert(0, "zcfree: ptr not found"); 260 | } 261 | 262 | #endif /* __TURBOC__ */ 263 | 264 | 265 | #ifdef M_I86 266 | /* Microsoft C in 16-bit mode */ 267 | 268 | # define MY_ZCALLOC 269 | 270 | #if (!defined(_MSC_VER) || (_MSC_VER <= 600)) 271 | # define _halloc halloc 272 | # define _hfree hfree 273 | #endif 274 | 275 | voidpf zcalloc (voidpf opaque, unsigned items, unsigned size) 276 | { 277 | if (opaque) opaque = 0; /* to make compiler happy */ 278 | return _halloc((long)items, size); 279 | } 280 | 281 | void zcfree (voidpf opaque, voidpf ptr) 282 | { 283 | if (opaque) opaque = 0; /* to make compiler happy */ 284 | _hfree(ptr); 285 | } 286 | 287 | #endif /* M_I86 */ 288 | 289 | #endif /* SYS16BIT */ 290 | 291 | 292 | #ifndef MY_ZCALLOC /* Any system without a special alloc function */ 293 | 294 | #ifndef STDC 295 | extern voidp malloc OF((uInt size)); 296 | extern voidp calloc OF((uInt items, uInt size)); 297 | extern void free OF((voidpf ptr)); 298 | #endif 299 | 300 | voidpf zcalloc (opaque, items, size) 301 | voidpf opaque; 302 | unsigned items; 303 | unsigned size; 304 | { 305 | if (opaque) items += size - size; /* make compiler happy */ 306 | return sizeof(uInt) > 2 ? (voidpf)malloc(items * size) : 307 | (voidpf)calloc(items, size); 308 | } 309 | 310 | void zcfree (opaque, ptr) 311 | voidpf opaque; 312 | voidpf ptr; 313 | { 314 | free(ptr); 315 | if (opaque) return; /* make compiler happy */ 316 | } 317 | 318 | #endif /* MY_ZCALLOC */ 319 | -------------------------------------------------------------------------------- /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 _dist_code[DIST_CODE_LEN] = { 74 | 0, 1, 2, 3, 4, 4, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 75 | 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 76 | 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 77 | 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 78 | 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13, 79 | 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 80 | 13, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 81 | 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 82 | 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 83 | 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15, 15, 15, 84 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 85 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 86 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 0, 0, 16, 17, 87 | 18, 18, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 22, 22, 22, 22, 22, 22, 22, 22, 88 | 23, 23, 23, 23, 23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 89 | 24, 24, 24, 24, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 90 | 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 91 | 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 27, 27, 27, 27, 27, 27, 27, 27, 92 | 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 93 | 27, 27, 27, 27, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 94 | 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 95 | 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 96 | 28, 28, 28, 28, 28, 28, 28, 28, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 97 | 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 98 | 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 99 | 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29 100 | }; 101 | 102 | const uch _length_code[MAX_MATCH-MIN_MATCH+1]= { 103 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 12, 12, 104 | 13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 15, 16, 16, 16, 16, 16, 16, 16, 16, 105 | 17, 17, 17, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18, 18, 18, 18, 19, 19, 19, 19, 106 | 19, 19, 19, 19, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 107 | 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 22, 22, 22, 22, 108 | 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 23, 23, 23, 23, 23, 23, 23, 23, 109 | 23, 23, 23, 23, 23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 110 | 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 111 | 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 112 | 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 26, 26, 26, 26, 26, 26, 26, 26, 113 | 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 114 | 26, 26, 26, 26, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 115 | 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 28 116 | }; 117 | 118 | local const int base_length[LENGTH_CODES] = { 119 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16, 20, 24, 28, 32, 40, 48, 56, 120 | 64, 80, 96, 112, 128, 160, 192, 224, 0 121 | }; 122 | 123 | local const int base_dist[D_CODES] = { 124 | 0, 1, 2, 3, 4, 6, 8, 12, 16, 24, 125 | 32, 48, 64, 96, 128, 192, 256, 384, 512, 768, 126 | 1024, 1536, 2048, 3072, 4096, 6144, 8192, 12288, 16384, 24576 127 | }; 128 | 129 | -------------------------------------------------------------------------------- /src/test.c: -------------------------------------------------------------------------------- 1 | #define GRF_NO_EXPORT 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #ifndef __WIN32 10 | struct timeval tv; 11 | 12 | void timer_start() { 13 | gettimeofday((struct timeval *)&tv, NULL); 14 | } 15 | 16 | void timer_end(const char *reason) { 17 | struct timeval tv2; 18 | float diff; 19 | gettimeofday((struct timeval *)&tv2, NULL); 20 | diff = tv2.tv_usec - tv.tv_usec; 21 | diff = ((tv2.tv_sec - tv.tv_sec) * 1000) + (diff / 1000); 22 | printf(reason, diff); 23 | } 24 | #else 25 | #define timer_start() 26 | #define timer_end(x) 27 | #endif 28 | 29 | void test_grf_version() { 30 | uint32_t version=grf_version(); 31 | uint8_t major, minor, revision; 32 | major = (version >> 16) & 0xff; 33 | minor = (version >> 8) & 0xff; 34 | revision = version & 0xff; 35 | printf(" - test_version(): 0x%06x (%d.%d.%d)\n", version, major, minor, revision); 36 | if ( (major != VERSION_MAJOR) || (minor != VERSION_MINOR) || (revision != VERSION_REVISION)) { 37 | puts("Error: This test program was not compiled for this lib!"); 38 | exit(1); 39 | } 40 | } 41 | 42 | void test_new_handler() { 43 | void *handler, *handler2; 44 | void *f; 45 | 46 | if (sizeof(struct grf_header) != GRF_HEADER_SIZE) { 47 | printf("Your compiler didn't use the right size for the packed structure (%x!=%x) !", (uint32_t) sizeof(struct grf_header), GRF_HEADER_SIZE); 48 | exit(2); 49 | } 50 | 51 | handler = grf_new("test.grf", true); 52 | grf_set_compression_level(handler, 9); 53 | printf(" - test_new_handler(): New handler at %p.\n", handler); 54 | if (grf_save(handler) == true) { 55 | puts(" - test_new_handler(): Write file success !"); 56 | } else { 57 | perror("grf_save"); 58 | grf_free(handler); 59 | exit(3); 60 | } 61 | printf(" - test_new_handler(): Adding some files: "); 62 | // GRFEXPORT void *grf_file_add(void *tmphandler, char *filename, void *ptr, size_t size) 63 | printf("grf_test_linux[%p] ", grf_file_add_path(handler, "data/grf_test_linux", "grf_test_linux")); 64 | fflush(stdout); 65 | printf("libgrf64.so[%p] ", grf_file_add_path(handler, "data/libgrf64.so", "libgrf64.so")); 66 | fflush(stdout); 67 | printf("problemfix.gpf[%p] ", grf_file_add_path(handler, "data/problemfix.gpf", "grf/problemfix.gpf")); 68 | fflush(stdout); 69 | printf("Alpha.grf[%p] ", grf_file_add_path(handler, "data/Alpha.grf", "grf/Alpha.grf")); 70 | fflush(stdout); 71 | printf("Beta.grf[%p] ", grf_file_add_path(handler, "data/Beta.grf", "grf/Beta.grf")); 72 | fflush(stdout); 73 | printf("test.txt[%p] ", grf_file_add_path(handler, "data/test.txt", "doc/README")); 74 | fflush(stdout); 75 | char staticTxt[] = "Hello world, this is a text file!"; 76 | printf("static.txt[%p] ", grf_file_add(handler, "data/static.txt", staticTxt, sizeof(staticTxt))); 77 | printf("static2.txt[%p]\n", grf_file_add(handler, "data/static2.txt", staticTxt, sizeof(staticTxt))); 78 | grf_save(handler); 79 | // Rename test! 80 | printf(" - test_new_handler(): renaming data/static.txt to data/RENAMEstatic.txt\n"); 81 | grf_file_rename(grf_get_file(handler, "data/static.txt"), "data/RENAMEstatic.txt"); 82 | printf(" - test_new_handler(): [Rename test] file data/static.txt: %p\n", grf_get_file(handler, "data/static.txt")); 83 | printf(" - test_new_handler(): [Rename test] file data/RENAMEstatic.txt: %p\n", grf_get_file(handler, "data/RENAMEstatic.txt")); 84 | // close 85 | grf_free(handler); 86 | timer_start(); 87 | handler = grf_load("test.grf", true); 88 | printf(" - test_new_handler(): Re-loaded file at %p.\n", handler); 89 | timer_end(" - test_new_handler(): File loading took %fms\n"); 90 | if (handler == NULL) return; 91 | grf_create_tree(handler); 92 | printf(" - test_new_handler(): There are %d files in this GRF - %d byte(s) wasted.\n", grf_filecount(handler), grf_wasted_space(handler)); 93 | f = grf_get_file(handler, "data/RENAMEstatic.txt"); 94 | printf(" - test_new_handler(): file `%s' found at %p - deleting...\n", grf_file_get_filename(f), f); 95 | if (!grf_file_delete(f)) { 96 | printf(" - test_new_handler(): delete failed\n"); 97 | } else { 98 | printf(" - test_new_handler(): delete OK\n"); 99 | } 100 | printf(" - test_new_handler(): %d byte(s) wasted.\n", grf_wasted_space(handler)); 101 | grf_save(handler); 102 | grf_free(handler); 103 | timer_start(); 104 | handler = grf_load("test.grf", true); 105 | printf(" - test_new_handler(): Re-loaded file at %p.\n", handler); 106 | timer_end(" - test_new_handler(): File loading took %fms\n"); 107 | if (handler == NULL) return; 108 | printf(" - test_new_handler(): There are %d files in this GRF.\n", grf_filecount(handler)); 109 | printf(" - test_new_handler(): %d byte(s) wasted (might be slightly lower than before, as table was moved to an optimized place)\n", grf_wasted_space(handler)); 110 | printf(" - test_new_handler(): attempting to repack file...\n"); 111 | if (grf_repack(handler, GRF_REPACK_FAST)) { 112 | printf(" - test_new_handler(): repack returned TRUE\n"); 113 | } else { 114 | printf(" - test_new_handler(): repack FAILED\n"); 115 | } 116 | grf_save(handler); 117 | grf_free(handler); 118 | handler = grf_load("test.grf", true); 119 | printf(" - test_new_handler(): Re-loaded file at %p.\n", handler); 120 | if (handler == NULL) return; 121 | printf(" - test_new_handler(): There are %d files in this GRF, and %d byte(s) wasted\n", grf_filecount(handler), grf_wasted_space(handler)); 122 | 123 | printf(" - test_new_handler(): Creating second handler...\n"); 124 | handler2 = grf_new("test2.grf", true); 125 | printf(" - test_new_handler(): Merging files from test.grf to test2.grf\n"); 126 | grf_merge(handler2, handler, GRF_REPACK_FAST); 127 | grf_free(handler2); 128 | 129 | grf_free(handler); 130 | } 131 | 132 | void test_load_file() { 133 | void *handler, *fhandler; 134 | void *filec; 135 | grf_node *list; 136 | // char *fn = "/storage/win_d/Program Files/Gravity/fRO_II/data.grf"; 137 | // char *fn = "/storage/win_d/Program Files/Gravity/RO/data.grf"; 138 | // char *fn = "/storage/win_d/Program Files/Gravity/20060224_krodata.gpf"; 139 | char *fn = "grf/problemfix.gpf"; 140 | // char *fn = "grf/data.grf"; 141 | char *fn2 = "data/airplane.gnd"; 142 | int x; 143 | unsigned int y; 144 | 145 | // test 146 | #if 0 147 | handler=grf_load("103.grf", true); 148 | grf_set_compression_level(handler, 9); 149 | grf_save(handler); 150 | grf_free(handler); 151 | #endif 152 | printf(" - test_load_file(): Opening `%s` in read only mode...\n", fn); 153 | timer_start(); 154 | handler = grf_load(fn, false); 155 | printf(" - test_load_file(): Loaded file at %p.\n", handler); 156 | timer_end(" - test_load_file(): File loading took %fms\n"); 157 | if (handler == NULL) return; 158 | printf(" - test_load_file(): There are %d files in this GRF.\n", grf_filecount(handler)); 159 | printf(" - test_load_file(): %d byte(s) wasted.\n", grf_wasted_space(handler)); 160 | printf(" - grf_create_tree(): Building global tree...\n"); 161 | timer_start(); 162 | grf_create_tree(handler); 163 | timer_end(" - grf_create_tree(): Tree created in %fms\n"); 164 | printf(" - test_load_file(): Searching for file %s\n", fn2); 165 | timer_start(); 166 | fhandler = grf_get_file(handler, fn2); 167 | timer_end(" - test_load_file(): File search took %fms.\n"); 168 | printf(" - test_load_file(): File is at %p\n", fhandler); 169 | if (fhandler != NULL) { 170 | printf(" - test_load_file(): Real filename is `%s`\n", grf_file_get_filename(fhandler)); 171 | printf(" - test_load_file(): File size is %d bytes.\n", grf_file_get_size(fhandler)); 172 | filec = malloc(grf_file_get_size(fhandler)); 173 | printf(" - test_load_file(): Extracted %d bytes for this file.\n", grf_file_get_contents(fhandler, filec)); 174 | #if 0 175 | FILE *f=fopen("loading45.jpg", "w"); 176 | if (f == NULL) { 177 | printf(" - test_writing failed\n"); 178 | } else { 179 | printf(" - test_writing ok\n"); 180 | fwrite(filec, grf_file_get_size(fhandler), 1, f); 181 | fclose(f); 182 | } 183 | #endif 184 | free(filec); 185 | } 186 | printf(" - test_load_file(): checking chained list integrity\n"); 187 | fhandler = grf_get_file_first(handler); 188 | x = 0; 189 | y = 0; 190 | while(fhandler != NULL) { 191 | int y2 = grf_file_get_storage_pos(fhandler); 192 | if (y2 < y) { 193 | printf("SORT ERROR\n"); 194 | } 195 | y = y2; 196 | fhandler = grf_get_file_next(fhandler); 197 | x++; 198 | } 199 | printf(" - test_load_file(): chained list size is %d (expected result: %d)\n", x, grf_filecount(handler)); 200 | if (grf_filecount(handler)<=16) { 201 | // Attempt to list file content 202 | printf(" - test_load_file(): Getting files list\n"); 203 | timer_start(); 204 | list = grf_get_file_list(handler); 205 | timer_end(" - test_load_file(): Got files list in %fms.\n"); 206 | if (list == NULL) { 207 | printf(" - test_load_file(): No files found\n"); 208 | } else { 209 | for(int i=0;list[i]!=NULL;i++) { 210 | printf(" - test_load_file(): %s\n", grf_file_get_filename(list[i])); 211 | } 212 | free(list); 213 | } 214 | } 215 | grf_free(handler); 216 | } 217 | 218 | int main() { 219 | puts(grf_versionstring()); 220 | puts(" * Running library tests..."); 221 | test_grf_version(); 222 | test_new_handler(); 223 | test_load_file(); 224 | return 0; 225 | } 226 | 227 | -------------------------------------------------------------------------------- /zlib/minigzip.c: -------------------------------------------------------------------------------- 1 | /* minigzip.c -- simulate gzip using the zlib compression library 2 | * Copyright (C) 1995-2005 Jean-loup Gailly. 3 | * For conditions of distribution and use, see copyright notice in zlib.h 4 | */ 5 | 6 | /* 7 | * minigzip is a minimal implementation of the gzip utility. This is 8 | * only an example of using zlib and isn't meant to replace the 9 | * full-featured gzip. No attempt is made to deal with file systems 10 | * limiting names to 14 or 8+3 characters, etc... Error checking is 11 | * very limited. So use minigzip only for testing; use gzip for the 12 | * real thing. On MSDOS, use only on file names without extension 13 | * or in pipe mode. 14 | */ 15 | 16 | /* @(#) $Id$ */ 17 | 18 | #include 19 | #include "zlib.h" 20 | 21 | #ifdef STDC 22 | # include 23 | # include 24 | #endif 25 | 26 | #ifdef USE_MMAP 27 | # include 28 | # include 29 | # include 30 | #endif 31 | 32 | #if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(__CYGWIN__) 33 | # include 34 | # include 35 | # define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY) 36 | #else 37 | # define SET_BINARY_MODE(file) 38 | #endif 39 | 40 | #ifdef VMS 41 | # define unlink delete 42 | # define GZ_SUFFIX "-gz" 43 | #endif 44 | #ifdef RISCOS 45 | # define unlink remove 46 | # define GZ_SUFFIX "-gz" 47 | # define fileno(file) file->__file 48 | #endif 49 | #if defined(__MWERKS__) && __dest_os != __be_os && __dest_os != __win32_os 50 | # include /* for fileno */ 51 | #endif 52 | 53 | #ifndef WIN32 /* unlink already in stdio.h for WIN32 */ 54 | extern int unlink OF((const char *)); 55 | #endif 56 | 57 | #ifndef GZ_SUFFIX 58 | # define GZ_SUFFIX ".gz" 59 | #endif 60 | #define SUFFIX_LEN (sizeof(GZ_SUFFIX)-1) 61 | 62 | #define BUFLEN 16384 63 | #define MAX_NAME_LEN 1024 64 | 65 | #ifdef MAXSEG_64K 66 | # define local static 67 | /* Needed for systems with limitation on stack size. */ 68 | #else 69 | # define local 70 | #endif 71 | 72 | char *prog; 73 | 74 | void error OF((const char *msg)); 75 | void gz_compress OF((FILE *in, gzFile out)); 76 | #ifdef USE_MMAP 77 | int gz_compress_mmap OF((FILE *in, gzFile out)); 78 | #endif 79 | void gz_uncompress OF((gzFile in, FILE *out)); 80 | void file_compress OF((char *file, char *mode)); 81 | void file_uncompress OF((char *file)); 82 | int main OF((int argc, char *argv[])); 83 | 84 | /* =========================================================================== 85 | * Display error message and exit 86 | */ 87 | void error(msg) 88 | const char *msg; 89 | { 90 | fprintf(stderr, "%s: %s\n", prog, msg); 91 | exit(1); 92 | } 93 | 94 | /* =========================================================================== 95 | * Compress input to output then close both files. 96 | */ 97 | 98 | void gz_compress(in, out) 99 | FILE *in; 100 | gzFile out; 101 | { 102 | local char buf[BUFLEN]; 103 | int len; 104 | int err; 105 | 106 | #ifdef USE_MMAP 107 | /* Try first compressing with mmap. If mmap fails (minigzip used in a 108 | * pipe), use the normal fread loop. 109 | */ 110 | if (gz_compress_mmap(in, out) == Z_OK) return; 111 | #endif 112 | for (;;) { 113 | len = (int)fread(buf, 1, sizeof(buf), in); 114 | if (ferror(in)) { 115 | perror("fread"); 116 | exit(1); 117 | } 118 | if (len == 0) break; 119 | 120 | if (gzwrite(out, buf, (unsigned)len) != len) error(gzerror(out, &err)); 121 | } 122 | fclose(in); 123 | if (gzclose(out) != Z_OK) error("failed gzclose"); 124 | } 125 | 126 | #ifdef USE_MMAP /* MMAP version, Miguel Albrecht */ 127 | 128 | /* Try compressing the input file at once using mmap. Return Z_OK if 129 | * if success, Z_ERRNO otherwise. 130 | */ 131 | int gz_compress_mmap(in, out) 132 | FILE *in; 133 | gzFile out; 134 | { 135 | int len; 136 | int err; 137 | int ifd = fileno(in); 138 | caddr_t buf; /* mmap'ed buffer for the entire input file */ 139 | off_t buf_len; /* length of the input file */ 140 | struct stat sb; 141 | 142 | /* Determine the size of the file, needed for mmap: */ 143 | if (fstat(ifd, &sb) < 0) return Z_ERRNO; 144 | buf_len = sb.st_size; 145 | if (buf_len <= 0) return Z_ERRNO; 146 | 147 | /* Now do the actual mmap: */ 148 | buf = mmap((caddr_t) 0, buf_len, PROT_READ, MAP_SHARED, ifd, (off_t)0); 149 | if (buf == (caddr_t)(-1)) return Z_ERRNO; 150 | 151 | /* Compress the whole file at once: */ 152 | len = gzwrite(out, (char *)buf, (unsigned)buf_len); 153 | 154 | if (len != (int)buf_len) error(gzerror(out, &err)); 155 | 156 | munmap(buf, buf_len); 157 | fclose(in); 158 | if (gzclose(out) != Z_OK) error("failed gzclose"); 159 | return Z_OK; 160 | } 161 | #endif /* USE_MMAP */ 162 | 163 | /* =========================================================================== 164 | * Uncompress input to output then close both files. 165 | */ 166 | void gz_uncompress(in, out) 167 | gzFile in; 168 | FILE *out; 169 | { 170 | local char buf[BUFLEN]; 171 | int len; 172 | int err; 173 | 174 | for (;;) { 175 | len = gzread(in, buf, sizeof(buf)); 176 | if (len < 0) error (gzerror(in, &err)); 177 | if (len == 0) break; 178 | 179 | if ((int)fwrite(buf, 1, (unsigned)len, out) != len) { 180 | error("failed fwrite"); 181 | } 182 | } 183 | if (fclose(out)) error("failed fclose"); 184 | 185 | if (gzclose(in) != Z_OK) error("failed gzclose"); 186 | } 187 | 188 | 189 | /* =========================================================================== 190 | * Compress the given file: create a corresponding .gz file and remove the 191 | * original. 192 | */ 193 | void file_compress(file, mode) 194 | char *file; 195 | char *mode; 196 | { 197 | local char outfile[MAX_NAME_LEN]; 198 | FILE *in; 199 | gzFile out; 200 | 201 | strcpy(outfile, file); 202 | strcat(outfile, GZ_SUFFIX); 203 | 204 | in = fopen(file, "rb"); 205 | if (in == NULL) { 206 | perror(file); 207 | exit(1); 208 | } 209 | out = gzopen(outfile, mode); 210 | if (out == NULL) { 211 | fprintf(stderr, "%s: can't gzopen %s\n", prog, outfile); 212 | exit(1); 213 | } 214 | gz_compress(in, out); 215 | 216 | unlink(file); 217 | } 218 | 219 | 220 | /* =========================================================================== 221 | * Uncompress the given file and remove the original. 222 | */ 223 | void file_uncompress(file) 224 | char *file; 225 | { 226 | local char buf[MAX_NAME_LEN]; 227 | char *infile, *outfile; 228 | FILE *out; 229 | gzFile in; 230 | uInt len = (uInt)strlen(file); 231 | 232 | strcpy(buf, file); 233 | 234 | if (len > SUFFIX_LEN && strcmp(file+len-SUFFIX_LEN, GZ_SUFFIX) == 0) { 235 | infile = file; 236 | outfile = buf; 237 | outfile[len-3] = '\0'; 238 | } else { 239 | outfile = file; 240 | infile = buf; 241 | strcat(infile, GZ_SUFFIX); 242 | } 243 | in = gzopen(infile, "rb"); 244 | if (in == NULL) { 245 | fprintf(stderr, "%s: can't gzopen %s\n", prog, infile); 246 | exit(1); 247 | } 248 | out = fopen(outfile, "wb"); 249 | if (out == NULL) { 250 | perror(file); 251 | exit(1); 252 | } 253 | 254 | gz_uncompress(in, out); 255 | 256 | unlink(infile); 257 | } 258 | 259 | 260 | /* =========================================================================== 261 | * Usage: minigzip [-d] [-f] [-h] [-r] [-1 to -9] [files...] 262 | * -d : decompress 263 | * -f : compress with Z_FILTERED 264 | * -h : compress with Z_HUFFMAN_ONLY 265 | * -r : compress with Z_RLE 266 | * -1 to -9 : compression level 267 | */ 268 | 269 | int main(argc, argv) 270 | int argc; 271 | char *argv[]; 272 | { 273 | int uncompr = 0; 274 | gzFile file; 275 | char outmode[20]; 276 | 277 | strcpy(outmode, "wb6 "); 278 | 279 | prog = argv[0]; 280 | argc--, argv++; 281 | 282 | while (argc > 0) { 283 | if (strcmp(*argv, "-d") == 0) 284 | uncompr = 1; 285 | else if (strcmp(*argv, "-f") == 0) 286 | outmode[3] = 'f'; 287 | else if (strcmp(*argv, "-h") == 0) 288 | outmode[3] = 'h'; 289 | else if (strcmp(*argv, "-r") == 0) 290 | outmode[3] = 'R'; 291 | else if ((*argv)[0] == '-' && (*argv)[1] >= '1' && (*argv)[1] <= '9' && 292 | (*argv)[2] == 0) 293 | outmode[2] = (*argv)[1]; 294 | else 295 | break; 296 | argc--, argv++; 297 | } 298 | if (outmode[3] == ' ') 299 | outmode[3] = 0; 300 | if (argc == 0) { 301 | SET_BINARY_MODE(stdin); 302 | SET_BINARY_MODE(stdout); 303 | if (uncompr) { 304 | file = gzdopen(fileno(stdin), "rb"); 305 | if (file == NULL) error("can't gzdopen stdin"); 306 | gz_uncompress(file, stdout); 307 | } else { 308 | file = gzdopen(fileno(stdout), outmode); 309 | if (file == NULL) error("can't gzdopen stdout"); 310 | gz_compress(stdin, file); 311 | } 312 | } else { 313 | do { 314 | if (uncompr) { 315 | file_uncompress(*argv); 316 | } else { 317 | file_compress(*argv, outmode); 318 | } 319 | } while (argv++, --argc); 320 | } 321 | return 0; 322 | } 323 | -------------------------------------------------------------------------------- /zlib/algorithm.txt: -------------------------------------------------------------------------------- 1 | 1. Compression algorithm (deflate) 2 | 3 | The deflation algorithm used by gzip (also zip and zlib) is a variation of 4 | LZ77 (Lempel-Ziv 1977, see reference below). It finds duplicated strings in 5 | the input data. The second occurrence of a string is replaced by a 6 | pointer to the previous string, in the form of a pair (distance, 7 | length). Distances are limited to 32K bytes, and lengths are limited 8 | to 258 bytes. When a string does not occur anywhere in the previous 9 | 32K bytes, it is emitted as a sequence of literal bytes. (In this 10 | description, `string' must be taken as an arbitrary sequence of bytes, 11 | and is not restricted to printable characters.) 12 | 13 | Literals or match lengths are compressed with one Huffman tree, and 14 | match distances are compressed with another tree. The trees are stored 15 | in a compact form at the start of each block. The blocks can have any 16 | size (except that the compressed data for one block must fit in 17 | available memory). A block is terminated when deflate() determines that 18 | it would be useful to start another block with fresh trees. (This is 19 | somewhat similar to the behavior of LZW-based _compress_.) 20 | 21 | Duplicated strings are found using a hash table. All input strings of 22 | length 3 are inserted in the hash table. A hash index is computed for 23 | the next 3 bytes. If the hash chain for this index is not empty, all 24 | strings in the chain are compared with the current input string, and 25 | the longest match is selected. 26 | 27 | The hash chains are searched starting with the most recent strings, to 28 | favor small distances and thus take advantage of the Huffman encoding. 29 | The hash chains are singly linked. There are no deletions from the 30 | hash chains, the algorithm simply discards matches that are too old. 31 | 32 | To avoid a worst-case situation, very long hash chains are arbitrarily 33 | truncated at a certain length, determined by a runtime option (level 34 | parameter of deflateInit). So deflate() does not always find the longest 35 | possible match but generally finds a match which is long enough. 36 | 37 | deflate() also defers the selection of matches with a lazy evaluation 38 | mechanism. After a match of length N has been found, deflate() searches for 39 | a longer match at the next input byte. If a longer match is found, the 40 | previous match is truncated to a length of one (thus producing a single 41 | literal byte) and the process of lazy evaluation begins again. Otherwise, 42 | the original match is kept, and the next match search is attempted only N 43 | steps later. 44 | 45 | The lazy match evaluation is also subject to a runtime parameter. If 46 | the current match is long enough, deflate() reduces the search for a longer 47 | match, thus speeding up the whole process. If compression ratio is more 48 | important than speed, deflate() attempts a complete second search even if 49 | the first match is already long enough. 50 | 51 | The lazy match evaluation is not performed for the fastest compression 52 | modes (level parameter 1 to 3). For these fast modes, new strings 53 | are inserted in the hash table only when no match was found, or 54 | when the match is not too long. This degrades the compression ratio 55 | but saves time since there are both fewer insertions and fewer searches. 56 | 57 | 58 | 2. Decompression algorithm (inflate) 59 | 60 | 2.1 Introduction 61 | 62 | The key question is how to represent a Huffman code (or any prefix code) so 63 | that you can decode fast. The most important characteristic is that shorter 64 | codes are much more common than longer codes, so pay attention to decoding the 65 | short codes fast, and let the long codes take longer to decode. 66 | 67 | inflate() sets up a first level table that covers some number of bits of 68 | input less than the length of longest code. It gets that many bits from the 69 | stream, and looks it up in the table. The table will tell if the next 70 | code is that many bits or less and how many, and if it is, it will tell 71 | the value, else it will point to the next level table for which inflate() 72 | grabs more bits and tries to decode a longer code. 73 | 74 | How many bits to make the first lookup is a tradeoff between the time it 75 | takes to decode and the time it takes to build the table. If building the 76 | table took no time (and if you had infinite memory), then there would only 77 | be a first level table to cover all the way to the longest code. However, 78 | building the table ends up taking a lot longer for more bits since short 79 | codes are replicated many times in such a table. What inflate() does is 80 | simply to make the number of bits in the first table a variable, and then 81 | to set that variable for the maximum speed. 82 | 83 | For inflate, which has 286 possible codes for the literal/length tree, the size 84 | of the first table is nine bits. Also the distance trees have 30 possible 85 | values, and the size of the first table is six bits. Note that for each of 86 | those cases, the table ended up one bit longer than the ``average'' code 87 | length, i.e. the code length of an approximately flat code which would be a 88 | little more than eight bits for 286 symbols and a little less than five bits 89 | for 30 symbols. 90 | 91 | 92 | 2.2 More details on the inflate table lookup 93 | 94 | Ok, you want to know what this cleverly obfuscated inflate tree actually 95 | looks like. You are correct that it's not a Huffman tree. It is simply a 96 | lookup table for the first, let's say, nine bits of a Huffman symbol. The 97 | symbol could be as short as one bit or as long as 15 bits. If a particular 98 | symbol is shorter than nine bits, then that symbol's translation is duplicated 99 | in all those entries that start with that symbol's bits. For example, if the 100 | symbol is four bits, then it's duplicated 32 times in a nine-bit table. If a 101 | symbol is nine bits long, it appears in the table once. 102 | 103 | If the symbol is longer than nine bits, then that entry in the table points 104 | to another similar table for the remaining bits. Again, there are duplicated 105 | entries as needed. The idea is that most of the time the symbol will be short 106 | and there will only be one table look up. (That's whole idea behind data 107 | compression in the first place.) For the less frequent long symbols, there 108 | will be two lookups. If you had a compression method with really long 109 | symbols, you could have as many levels of lookups as is efficient. For 110 | inflate, two is enough. 111 | 112 | So a table entry either points to another table (in which case nine bits in 113 | the above example are gobbled), or it contains the translation for the symbol 114 | and the number of bits to gobble. Then you start again with the next 115 | ungobbled bit. 116 | 117 | You may wonder: why not just have one lookup table for how ever many bits the 118 | longest symbol is? The reason is that if you do that, you end up spending 119 | more time filling in duplicate symbol entries than you do actually decoding. 120 | At least for deflate's output that generates new trees every several 10's of 121 | kbytes. You can imagine that filling in a 2^15 entry table for a 15-bit code 122 | would take too long if you're only decoding several thousand symbols. At the 123 | other extreme, you could make a new table for every bit in the code. In fact, 124 | that's essentially a Huffman tree. But then you spend two much time 125 | traversing the tree while decoding, even for short symbols. 126 | 127 | So the number of bits for the first lookup table is a trade of the time to 128 | fill out the table vs. the time spent looking at the second level and above of 129 | the table. 130 | 131 | Here is an example, scaled down: 132 | 133 | The code being decoded, with 10 symbols, from 1 to 6 bits long: 134 | 135 | A: 0 136 | B: 10 137 | C: 1100 138 | D: 11010 139 | E: 11011 140 | F: 11100 141 | G: 11101 142 | H: 11110 143 | I: 111110 144 | J: 111111 145 | 146 | Let's make the first table three bits long (eight entries): 147 | 148 | 000: A,1 149 | 001: A,1 150 | 010: A,1 151 | 011: A,1 152 | 100: B,2 153 | 101: B,2 154 | 110: -> table X (gobble 3 bits) 155 | 111: -> table Y (gobble 3 bits) 156 | 157 | Each entry is what the bits decode as and how many bits that is, i.e. how 158 | many bits to gobble. Or the entry points to another table, with the number of 159 | bits to gobble implicit in the size of the table. 160 | 161 | Table X is two bits long since the longest code starting with 110 is five bits 162 | long: 163 | 164 | 00: C,1 165 | 01: C,1 166 | 10: D,2 167 | 11: E,2 168 | 169 | Table Y is three bits long since the longest code starting with 111 is six 170 | bits long: 171 | 172 | 000: F,2 173 | 001: F,2 174 | 010: G,2 175 | 011: G,2 176 | 100: H,2 177 | 101: H,2 178 | 110: I,3 179 | 111: J,3 180 | 181 | So what we have here are three tables with a total of 20 entries that had to 182 | be constructed. That's compared to 64 entries for a single table. Or 183 | compared to 16 entries for a Huffman tree (six two entry tables and one four 184 | entry table). Assuming that the code ideally represents the probability of 185 | the symbols, it takes on the average 1.25 lookups per symbol. That's compared 186 | to one lookup for the single table, or 1.66 lookups per symbol for the 187 | Huffman tree. 188 | 189 | There, I think that gives you a picture of what's going on. For inflate, the 190 | meaning of a particular symbol is often more than just a letter. It can be a 191 | byte (a "literal"), or it can be either a length or a distance which 192 | indicates a base value and a number of bits to fetch after the code that is 193 | added to the base value. Or it might be the special end-of-block code. The 194 | data structures created in inftrees.c try to encode all that information 195 | compactly in the tables. 196 | 197 | 198 | Jean-loup Gailly Mark Adler 199 | jloup@gzip.org madler@alumni.caltech.edu 200 | 201 | 202 | References: 203 | 204 | [LZ77] Ziv J., Lempel A., ``A Universal Algorithm for Sequential Data 205 | Compression,'' IEEE Transactions on Information Theory, Vol. 23, No. 3, 206 | pp. 337-343. 207 | 208 | ``DEFLATE Compressed Data Format Specification'' available in 209 | http://www.ietf.org/rfc/rfc1951.txt 210 | -------------------------------------------------------------------------------- /zlib/zconf.h: -------------------------------------------------------------------------------- 1 | /* zconf.h -- configuration of the zlib compression library 2 | * Copyright (C) 1995-2005 Jean-loup Gailly. 3 | * For conditions of distribution and use, see copyright notice in zlib.h 4 | */ 5 | 6 | /* @(#) $Id$ */ 7 | 8 | #ifndef ZCONF_H 9 | #define ZCONF_H 10 | 11 | /* 12 | * If you *really* need a unique prefix for all types and library functions, 13 | * compile with -DZ_PREFIX. The "standard" zlib should be compiled without it. 14 | */ 15 | #ifdef Z_PREFIX 16 | # define deflateInit_ z_deflateInit_ 17 | # define deflate z_deflate 18 | # define deflateEnd z_deflateEnd 19 | # define inflateInit_ z_inflateInit_ 20 | # define inflate z_inflate 21 | # define inflateEnd z_inflateEnd 22 | # define deflateInit2_ z_deflateInit2_ 23 | # define deflateSetDictionary z_deflateSetDictionary 24 | # define deflateCopy z_deflateCopy 25 | # define deflateReset z_deflateReset 26 | # define deflateParams z_deflateParams 27 | # define deflateBound z_deflateBound 28 | # define deflatePrime z_deflatePrime 29 | # define inflateInit2_ z_inflateInit2_ 30 | # define inflateSetDictionary z_inflateSetDictionary 31 | # define inflateSync z_inflateSync 32 | # define inflateSyncPoint z_inflateSyncPoint 33 | # define inflateCopy z_inflateCopy 34 | # define inflateReset z_inflateReset 35 | # define inflateBack z_inflateBack 36 | # define inflateBackEnd z_inflateBackEnd 37 | # define compress z_compress 38 | # define compress2 z_compress2 39 | # define compressBound z_compressBound 40 | # define uncompress z_uncompress 41 | # define adler32 z_adler32 42 | # define crc32 z_crc32 43 | # define get_crc_table z_get_crc_table 44 | # define zError z_zError 45 | 46 | # define alloc_func z_alloc_func 47 | # define free_func z_free_func 48 | # define in_func z_in_func 49 | # define out_func z_out_func 50 | # define Byte z_Byte 51 | # define uInt z_uInt 52 | # define uLong z_uLong 53 | # define Bytef z_Bytef 54 | # define charf z_charf 55 | # define intf z_intf 56 | # define uIntf z_uIntf 57 | # define uLongf z_uLongf 58 | # define voidpf z_voidpf 59 | # define voidp z_voidp 60 | #endif 61 | 62 | #if defined(__MSDOS__) && !defined(MSDOS) 63 | # define MSDOS 64 | #endif 65 | #if (defined(OS_2) || defined(__OS2__)) && !defined(OS2) 66 | # define OS2 67 | #endif 68 | #if defined(_WINDOWS) && !defined(WINDOWS) 69 | # define WINDOWS 70 | #endif 71 | #if defined(_WIN32) || defined(_WIN32_WCE) || defined(__WIN32__) 72 | # ifndef WIN32 73 | # define WIN32 74 | # endif 75 | #endif 76 | #if (defined(MSDOS) || defined(OS2) || defined(WINDOWS)) && !defined(WIN32) 77 | # if !defined(__GNUC__) && !defined(__FLAT__) && !defined(__386__) 78 | # ifndef SYS16BIT 79 | # define SYS16BIT 80 | # endif 81 | # endif 82 | #endif 83 | 84 | /* 85 | * Compile with -DMAXSEG_64K if the alloc function cannot allocate more 86 | * than 64k bytes at a time (needed on systems with 16-bit int). 87 | */ 88 | #ifdef SYS16BIT 89 | # define MAXSEG_64K 90 | #endif 91 | #ifdef MSDOS 92 | # define UNALIGNED_OK 93 | #endif 94 | 95 | #ifdef __STDC_VERSION__ 96 | # ifndef STDC 97 | # define STDC 98 | # endif 99 | # if __STDC_VERSION__ >= 199901L 100 | # ifndef STDC99 101 | # define STDC99 102 | # endif 103 | # endif 104 | #endif 105 | #if !defined(STDC) && (defined(__STDC__) || defined(__cplusplus)) 106 | # define STDC 107 | #endif 108 | #if !defined(STDC) && (defined(__GNUC__) || defined(__BORLANDC__)) 109 | # define STDC 110 | #endif 111 | #if !defined(STDC) && (defined(MSDOS) || defined(WINDOWS) || defined(WIN32)) 112 | # define STDC 113 | #endif 114 | #if !defined(STDC) && (defined(OS2) || defined(__HOS_AIX__)) 115 | # define STDC 116 | #endif 117 | 118 | #if defined(__OS400__) && !defined(STDC) /* iSeries (formerly AS/400). */ 119 | # define STDC 120 | #endif 121 | 122 | #ifndef STDC 123 | # ifndef const /* cannot use !defined(STDC) && !defined(const) on Mac */ 124 | # define const /* note: need a more gentle solution here */ 125 | # endif 126 | #endif 127 | 128 | /* Some Mac compilers merge all .h files incorrectly: */ 129 | #if defined(__MWERKS__)||defined(applec)||defined(THINK_C)||defined(__SC__) 130 | # define NO_DUMMY_DECL 131 | #endif 132 | 133 | /* Maximum value for memLevel in deflateInit2 */ 134 | #ifndef MAX_MEM_LEVEL 135 | # ifdef MAXSEG_64K 136 | # define MAX_MEM_LEVEL 8 137 | # else 138 | # define MAX_MEM_LEVEL 9 139 | # endif 140 | #endif 141 | 142 | /* Maximum value for windowBits in deflateInit2 and inflateInit2. 143 | * WARNING: reducing MAX_WBITS makes minigzip unable to extract .gz files 144 | * created by gzip. (Files created by minigzip can still be extracted by 145 | * gzip.) 146 | */ 147 | #ifndef MAX_WBITS 148 | # define MAX_WBITS 15 /* 32K LZ77 window */ 149 | #endif 150 | 151 | /* The memory requirements for deflate are (in bytes): 152 | (1 << (windowBits+2)) + (1 << (memLevel+9)) 153 | that is: 128K for windowBits=15 + 128K for memLevel = 8 (default values) 154 | plus a few kilobytes for small objects. For example, if you want to reduce 155 | the default memory requirements from 256K to 128K, compile with 156 | make CFLAGS="-O -DMAX_WBITS=14 -DMAX_MEM_LEVEL=7" 157 | Of course this will generally degrade compression (there's no free lunch). 158 | 159 | The memory requirements for inflate are (in bytes) 1 << windowBits 160 | that is, 32K for windowBits=15 (default value) plus a few kilobytes 161 | for small objects. 162 | */ 163 | 164 | /* Type declarations */ 165 | 166 | #ifndef OF /* function prototypes */ 167 | # ifdef STDC 168 | # define OF(args) args 169 | # else 170 | # define OF(args) () 171 | # endif 172 | #endif 173 | 174 | /* The following definitions for FAR are needed only for MSDOS mixed 175 | * model programming (small or medium model with some far allocations). 176 | * This was tested only with MSC; for other MSDOS compilers you may have 177 | * to define NO_MEMCPY in zutil.h. If you don't need the mixed model, 178 | * just define FAR to be empty. 179 | */ 180 | #ifdef SYS16BIT 181 | # if defined(M_I86SM) || defined(M_I86MM) 182 | /* MSC small or medium model */ 183 | # define SMALL_MEDIUM 184 | # ifdef _MSC_VER 185 | # define FAR _far 186 | # else 187 | # define FAR far 188 | # endif 189 | # endif 190 | # if (defined(__SMALL__) || defined(__MEDIUM__)) 191 | /* Turbo C small or medium model */ 192 | # define SMALL_MEDIUM 193 | # ifdef __BORLANDC__ 194 | # define FAR _far 195 | # else 196 | # define FAR far 197 | # endif 198 | # endif 199 | #endif 200 | 201 | #if defined(WINDOWS) || defined(WIN32) 202 | /* If building or using zlib as a DLL, define ZLIB_DLL. 203 | * This is not mandatory, but it offers a little performance increase. 204 | */ 205 | # ifdef ZLIB_DLL 206 | # if defined(WIN32) && (!defined(__BORLANDC__) || (__BORLANDC__ >= 0x500)) 207 | # ifdef ZLIB_INTERNAL 208 | # define ZEXTERN extern __declspec(dllexport) 209 | # else 210 | # define ZEXTERN extern __declspec(dllimport) 211 | # endif 212 | # endif 213 | # endif /* ZLIB_DLL */ 214 | /* If building or using zlib with the WINAPI/WINAPIV calling convention, 215 | * define ZLIB_WINAPI. 216 | * Caution: the standard ZLIB1.DLL is NOT compiled using ZLIB_WINAPI. 217 | */ 218 | # ifdef ZLIB_WINAPI 219 | # ifdef FAR 220 | # undef FAR 221 | # endif 222 | # include 223 | /* No need for _export, use ZLIB.DEF instead. */ 224 | /* For complete Windows compatibility, use WINAPI, not __stdcall. */ 225 | # define ZEXPORT WINAPI 226 | # ifdef WIN32 227 | # define ZEXPORTVA WINAPIV 228 | # else 229 | # define ZEXPORTVA FAR CDECL 230 | # endif 231 | # endif 232 | #endif 233 | 234 | #if defined (__BEOS__) 235 | # ifdef ZLIB_DLL 236 | # ifdef ZLIB_INTERNAL 237 | # define ZEXPORT __declspec(dllexport) 238 | # define ZEXPORTVA __declspec(dllexport) 239 | # else 240 | # define ZEXPORT __declspec(dllimport) 241 | # define ZEXPORTVA __declspec(dllimport) 242 | # endif 243 | # endif 244 | #endif 245 | 246 | #ifndef ZEXTERN 247 | # define ZEXTERN extern 248 | #endif 249 | #ifndef ZEXPORT 250 | # define ZEXPORT 251 | #endif 252 | #ifndef ZEXPORTVA 253 | # define ZEXPORTVA 254 | #endif 255 | 256 | #ifndef FAR 257 | # define FAR 258 | #endif 259 | 260 | #if !defined(__MACTYPES__) 261 | typedef unsigned char Byte; /* 8 bits */ 262 | #endif 263 | typedef unsigned int uInt; /* 16 bits or more */ 264 | typedef unsigned long uLong; /* 32 bits or more */ 265 | 266 | #ifdef SMALL_MEDIUM 267 | /* Borland C/C++ and some old MSC versions ignore FAR inside typedef */ 268 | # define Bytef Byte FAR 269 | #else 270 | typedef Byte FAR Bytef; 271 | #endif 272 | typedef char FAR charf; 273 | typedef int FAR intf; 274 | typedef uInt FAR uIntf; 275 | typedef uLong FAR uLongf; 276 | 277 | #ifdef STDC 278 | typedef void const *voidpc; 279 | typedef void FAR *voidpf; 280 | typedef void *voidp; 281 | #else 282 | typedef Byte const *voidpc; 283 | typedef Byte FAR *voidpf; 284 | typedef Byte *voidp; 285 | #endif 286 | 287 | #if 0 /* HAVE_UNISTD_H -- this line is updated by ./configure */ 288 | # include /* for off_t */ 289 | # include /* for SEEK_* and off_t */ 290 | # ifdef VMS 291 | # include /* for off_t */ 292 | # endif 293 | # define z_off_t off_t 294 | #endif 295 | #ifndef SEEK_SET 296 | # define SEEK_SET 0 /* Seek from beginning of file. */ 297 | # define SEEK_CUR 1 /* Seek from current position. */ 298 | # define SEEK_END 2 /* Set file pointer to EOF plus "offset" */ 299 | #endif 300 | #ifndef z_off_t 301 | # define z_off_t long 302 | #endif 303 | 304 | #if defined(__OS400__) 305 | # define NO_vsnprintf 306 | #endif 307 | 308 | #if defined(__MVS__) 309 | # define NO_vsnprintf 310 | # ifdef FAR 311 | # undef FAR 312 | # endif 313 | #endif 314 | 315 | /* MVS linker does not support external names larger than 8 bytes */ 316 | #if defined(__MVS__) 317 | # pragma map(deflateInit_,"DEIN") 318 | # pragma map(deflateInit2_,"DEIN2") 319 | # pragma map(deflateEnd,"DEEND") 320 | # pragma map(deflateBound,"DEBND") 321 | # pragma map(inflateInit_,"ININ") 322 | # pragma map(inflateInit2_,"ININ2") 323 | # pragma map(inflateEnd,"INEND") 324 | # pragma map(inflateSync,"INSY") 325 | # pragma map(inflateSetDictionary,"INSEDI") 326 | # pragma map(compressBound,"CMBND") 327 | # pragma map(inflate_table,"INTABL") 328 | # pragma map(inflate_fast,"INFA") 329 | # pragma map(inflate_copyright,"INCOPY") 330 | #endif 331 | 332 | #endif /* ZCONF_H */ 333 | -------------------------------------------------------------------------------- /zlib/zconf.in.h: -------------------------------------------------------------------------------- 1 | /* zconf.h -- configuration of the zlib compression library 2 | * Copyright (C) 1995-2005 Jean-loup Gailly. 3 | * For conditions of distribution and use, see copyright notice in zlib.h 4 | */ 5 | 6 | /* @(#) $Id$ */ 7 | 8 | #ifndef ZCONF_H 9 | #define ZCONF_H 10 | 11 | /* 12 | * If you *really* need a unique prefix for all types and library functions, 13 | * compile with -DZ_PREFIX. The "standard" zlib should be compiled without it. 14 | */ 15 | #ifdef Z_PREFIX 16 | # define deflateInit_ z_deflateInit_ 17 | # define deflate z_deflate 18 | # define deflateEnd z_deflateEnd 19 | # define inflateInit_ z_inflateInit_ 20 | # define inflate z_inflate 21 | # define inflateEnd z_inflateEnd 22 | # define deflateInit2_ z_deflateInit2_ 23 | # define deflateSetDictionary z_deflateSetDictionary 24 | # define deflateCopy z_deflateCopy 25 | # define deflateReset z_deflateReset 26 | # define deflateParams z_deflateParams 27 | # define deflateBound z_deflateBound 28 | # define deflatePrime z_deflatePrime 29 | # define inflateInit2_ z_inflateInit2_ 30 | # define inflateSetDictionary z_inflateSetDictionary 31 | # define inflateSync z_inflateSync 32 | # define inflateSyncPoint z_inflateSyncPoint 33 | # define inflateCopy z_inflateCopy 34 | # define inflateReset z_inflateReset 35 | # define inflateBack z_inflateBack 36 | # define inflateBackEnd z_inflateBackEnd 37 | # define compress z_compress 38 | # define compress2 z_compress2 39 | # define compressBound z_compressBound 40 | # define uncompress z_uncompress 41 | # define adler32 z_adler32 42 | # define crc32 z_crc32 43 | # define get_crc_table z_get_crc_table 44 | # define zError z_zError 45 | 46 | # define alloc_func z_alloc_func 47 | # define free_func z_free_func 48 | # define in_func z_in_func 49 | # define out_func z_out_func 50 | # define Byte z_Byte 51 | # define uInt z_uInt 52 | # define uLong z_uLong 53 | # define Bytef z_Bytef 54 | # define charf z_charf 55 | # define intf z_intf 56 | # define uIntf z_uIntf 57 | # define uLongf z_uLongf 58 | # define voidpf z_voidpf 59 | # define voidp z_voidp 60 | #endif 61 | 62 | #if defined(__MSDOS__) && !defined(MSDOS) 63 | # define MSDOS 64 | #endif 65 | #if (defined(OS_2) || defined(__OS2__)) && !defined(OS2) 66 | # define OS2 67 | #endif 68 | #if defined(_WINDOWS) && !defined(WINDOWS) 69 | # define WINDOWS 70 | #endif 71 | #if defined(_WIN32) || defined(_WIN32_WCE) || defined(__WIN32__) 72 | # ifndef WIN32 73 | # define WIN32 74 | # endif 75 | #endif 76 | #if (defined(MSDOS) || defined(OS2) || defined(WINDOWS)) && !defined(WIN32) 77 | # if !defined(__GNUC__) && !defined(__FLAT__) && !defined(__386__) 78 | # ifndef SYS16BIT 79 | # define SYS16BIT 80 | # endif 81 | # endif 82 | #endif 83 | 84 | /* 85 | * Compile with -DMAXSEG_64K if the alloc function cannot allocate more 86 | * than 64k bytes at a time (needed on systems with 16-bit int). 87 | */ 88 | #ifdef SYS16BIT 89 | # define MAXSEG_64K 90 | #endif 91 | #ifdef MSDOS 92 | # define UNALIGNED_OK 93 | #endif 94 | 95 | #ifdef __STDC_VERSION__ 96 | # ifndef STDC 97 | # define STDC 98 | # endif 99 | # if __STDC_VERSION__ >= 199901L 100 | # ifndef STDC99 101 | # define STDC99 102 | # endif 103 | # endif 104 | #endif 105 | #if !defined(STDC) && (defined(__STDC__) || defined(__cplusplus)) 106 | # define STDC 107 | #endif 108 | #if !defined(STDC) && (defined(__GNUC__) || defined(__BORLANDC__)) 109 | # define STDC 110 | #endif 111 | #if !defined(STDC) && (defined(MSDOS) || defined(WINDOWS) || defined(WIN32)) 112 | # define STDC 113 | #endif 114 | #if !defined(STDC) && (defined(OS2) || defined(__HOS_AIX__)) 115 | # define STDC 116 | #endif 117 | 118 | #if defined(__OS400__) && !defined(STDC) /* iSeries (formerly AS/400). */ 119 | # define STDC 120 | #endif 121 | 122 | #ifndef STDC 123 | # ifndef const /* cannot use !defined(STDC) && !defined(const) on Mac */ 124 | # define const /* note: need a more gentle solution here */ 125 | # endif 126 | #endif 127 | 128 | /* Some Mac compilers merge all .h files incorrectly: */ 129 | #if defined(__MWERKS__)||defined(applec)||defined(THINK_C)||defined(__SC__) 130 | # define NO_DUMMY_DECL 131 | #endif 132 | 133 | /* Maximum value for memLevel in deflateInit2 */ 134 | #ifndef MAX_MEM_LEVEL 135 | # ifdef MAXSEG_64K 136 | # define MAX_MEM_LEVEL 8 137 | # else 138 | # define MAX_MEM_LEVEL 9 139 | # endif 140 | #endif 141 | 142 | /* Maximum value for windowBits in deflateInit2 and inflateInit2. 143 | * WARNING: reducing MAX_WBITS makes minigzip unable to extract .gz files 144 | * created by gzip. (Files created by minigzip can still be extracted by 145 | * gzip.) 146 | */ 147 | #ifndef MAX_WBITS 148 | # define MAX_WBITS 15 /* 32K LZ77 window */ 149 | #endif 150 | 151 | /* The memory requirements for deflate are (in bytes): 152 | (1 << (windowBits+2)) + (1 << (memLevel+9)) 153 | that is: 128K for windowBits=15 + 128K for memLevel = 8 (default values) 154 | plus a few kilobytes for small objects. For example, if you want to reduce 155 | the default memory requirements from 256K to 128K, compile with 156 | make CFLAGS="-O -DMAX_WBITS=14 -DMAX_MEM_LEVEL=7" 157 | Of course this will generally degrade compression (there's no free lunch). 158 | 159 | The memory requirements for inflate are (in bytes) 1 << windowBits 160 | that is, 32K for windowBits=15 (default value) plus a few kilobytes 161 | for small objects. 162 | */ 163 | 164 | /* Type declarations */ 165 | 166 | #ifndef OF /* function prototypes */ 167 | # ifdef STDC 168 | # define OF(args) args 169 | # else 170 | # define OF(args) () 171 | # endif 172 | #endif 173 | 174 | /* The following definitions for FAR are needed only for MSDOS mixed 175 | * model programming (small or medium model with some far allocations). 176 | * This was tested only with MSC; for other MSDOS compilers you may have 177 | * to define NO_MEMCPY in zutil.h. If you don't need the mixed model, 178 | * just define FAR to be empty. 179 | */ 180 | #ifdef SYS16BIT 181 | # if defined(M_I86SM) || defined(M_I86MM) 182 | /* MSC small or medium model */ 183 | # define SMALL_MEDIUM 184 | # ifdef _MSC_VER 185 | # define FAR _far 186 | # else 187 | # define FAR far 188 | # endif 189 | # endif 190 | # if (defined(__SMALL__) || defined(__MEDIUM__)) 191 | /* Turbo C small or medium model */ 192 | # define SMALL_MEDIUM 193 | # ifdef __BORLANDC__ 194 | # define FAR _far 195 | # else 196 | # define FAR far 197 | # endif 198 | # endif 199 | #endif 200 | 201 | #if defined(WINDOWS) || defined(WIN32) 202 | /* If building or using zlib as a DLL, define ZLIB_DLL. 203 | * This is not mandatory, but it offers a little performance increase. 204 | */ 205 | # ifdef ZLIB_DLL 206 | # if defined(WIN32) && (!defined(__BORLANDC__) || (__BORLANDC__ >= 0x500)) 207 | # ifdef ZLIB_INTERNAL 208 | # define ZEXTERN extern __declspec(dllexport) 209 | # else 210 | # define ZEXTERN extern __declspec(dllimport) 211 | # endif 212 | # endif 213 | # endif /* ZLIB_DLL */ 214 | /* If building or using zlib with the WINAPI/WINAPIV calling convention, 215 | * define ZLIB_WINAPI. 216 | * Caution: the standard ZLIB1.DLL is NOT compiled using ZLIB_WINAPI. 217 | */ 218 | # ifdef ZLIB_WINAPI 219 | # ifdef FAR 220 | # undef FAR 221 | # endif 222 | # include 223 | /* No need for _export, use ZLIB.DEF instead. */ 224 | /* For complete Windows compatibility, use WINAPI, not __stdcall. */ 225 | # define ZEXPORT WINAPI 226 | # ifdef WIN32 227 | # define ZEXPORTVA WINAPIV 228 | # else 229 | # define ZEXPORTVA FAR CDECL 230 | # endif 231 | # endif 232 | #endif 233 | 234 | #if defined (__BEOS__) 235 | # ifdef ZLIB_DLL 236 | # ifdef ZLIB_INTERNAL 237 | # define ZEXPORT __declspec(dllexport) 238 | # define ZEXPORTVA __declspec(dllexport) 239 | # else 240 | # define ZEXPORT __declspec(dllimport) 241 | # define ZEXPORTVA __declspec(dllimport) 242 | # endif 243 | # endif 244 | #endif 245 | 246 | #ifndef ZEXTERN 247 | # define ZEXTERN extern 248 | #endif 249 | #ifndef ZEXPORT 250 | # define ZEXPORT 251 | #endif 252 | #ifndef ZEXPORTVA 253 | # define ZEXPORTVA 254 | #endif 255 | 256 | #ifndef FAR 257 | # define FAR 258 | #endif 259 | 260 | #if !defined(__MACTYPES__) 261 | typedef unsigned char Byte; /* 8 bits */ 262 | #endif 263 | typedef unsigned int uInt; /* 16 bits or more */ 264 | typedef unsigned long uLong; /* 32 bits or more */ 265 | 266 | #ifdef SMALL_MEDIUM 267 | /* Borland C/C++ and some old MSC versions ignore FAR inside typedef */ 268 | # define Bytef Byte FAR 269 | #else 270 | typedef Byte FAR Bytef; 271 | #endif 272 | typedef char FAR charf; 273 | typedef int FAR intf; 274 | typedef uInt FAR uIntf; 275 | typedef uLong FAR uLongf; 276 | 277 | #ifdef STDC 278 | typedef void const *voidpc; 279 | typedef void FAR *voidpf; 280 | typedef void *voidp; 281 | #else 282 | typedef Byte const *voidpc; 283 | typedef Byte FAR *voidpf; 284 | typedef Byte *voidp; 285 | #endif 286 | 287 | #if 0 /* HAVE_UNISTD_H -- this line is updated by ./configure */ 288 | # include /* for off_t */ 289 | # include /* for SEEK_* and off_t */ 290 | # ifdef VMS 291 | # include /* for off_t */ 292 | # endif 293 | # define z_off_t off_t 294 | #endif 295 | #ifndef SEEK_SET 296 | # define SEEK_SET 0 /* Seek from beginning of file. */ 297 | # define SEEK_CUR 1 /* Seek from current position. */ 298 | # define SEEK_END 2 /* Set file pointer to EOF plus "offset" */ 299 | #endif 300 | #ifndef z_off_t 301 | # define z_off_t long 302 | #endif 303 | 304 | #if defined(__OS400__) 305 | # define NO_vsnprintf 306 | #endif 307 | 308 | #if defined(__MVS__) 309 | # define NO_vsnprintf 310 | # ifdef FAR 311 | # undef FAR 312 | # endif 313 | #endif 314 | 315 | /* MVS linker does not support external names larger than 8 bytes */ 316 | #if defined(__MVS__) 317 | # pragma map(deflateInit_,"DEIN") 318 | # pragma map(deflateInit2_,"DEIN2") 319 | # pragma map(deflateEnd,"DEEND") 320 | # pragma map(deflateBound,"DEBND") 321 | # pragma map(inflateInit_,"ININ") 322 | # pragma map(inflateInit2_,"ININ2") 323 | # pragma map(inflateEnd,"INEND") 324 | # pragma map(inflateSync,"INSY") 325 | # pragma map(inflateSetDictionary,"INSEDI") 326 | # pragma map(compressBound,"CMBND") 327 | # pragma map(inflate_table,"INTABL") 328 | # pragma map(inflate_fast,"INFA") 329 | # pragma map(inflate_copyright,"INCOPY") 330 | #endif 331 | 332 | #endif /* ZCONF_H */ 333 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | #!make 2 | OBJECTS=main.o zlib.o hash_tables.o grf.o euc_kr.o 3 | OBJECTS32=$(patsubst %.o,%_32.o,$(OBJECTS)) 4 | OBJECTS64=$(patsubst %.o,%_64.o,$(OBJECTS)) 5 | GB_OBJECTS=main.o MainWindow.o moc_MainWindow.o 6 | GB_OBJECTS32=$(patsubst %.o,%_32.o,$(GB_OBJECTS)) 7 | GB_OBJECTS64=$(patsubst %.o,%_64.o,$(GB_OBJECTS)) 8 | DIST_FILES=libgrf-^.zip libgrf-^.tar.gz grfbuilder-^.zip grfbuilder-^.tar.gz 9 | TARGET=libgrf.so 10 | TARGET64=libgrf64.so 11 | TARGET_WIN=grf.dll 12 | GB_TARGET=grfbuilder.bin 13 | GB_TARGET64=grfbuilder_64.bin 14 | GB_TARGET_WIN=grfbuilder.exe 15 | GB_LOCALES=grfbuilder_fr.qm grfbuilder_de.qm grfbuilder_es.qm grfbuilder_zh_CN.qm grfbuilder_zh_TW.qm 16 | GB_REQ_DLL=mingwm10.dll QtCore4.dll QtGui4.dll 17 | BUILD=unknown 18 | LDFLAGS=--shared 19 | LDFLAGS_TEST= 20 | INCLUDES=-Iincludes -Izlib 21 | ifndef DEBUG 22 | DEBUG=yes 23 | endif 24 | ifeq ($(DEBUG),no) 25 | CFLAGS=-pipe -O3 -Wall --std=gnu99 26 | CXXFLAGS=-pipe -O3 -Wall 27 | else 28 | CFLAGS=-pipe -g -ggdb -O0 -Wall --std=gnu99 -D__DEBUG 29 | CXXFLAGS=-pipe -g -ggdb -O0 -Wall -D__DEBUG 30 | endif 31 | 32 | ZOBJS = adler32.o compress.o crc32.o gzio.o uncompr.o deflate.o trees.o \ 33 | zutil.o inflate.o infback.o inftrees.o inffast.o 34 | 35 | ZOBJS32=$(patsubst %.o,%_32.o,$(ZOBJS)) 36 | ZOBJS64=$(patsubst %.o,%_64.o,$(ZOBJS)) 37 | 38 | UNAME=$(shell uname -s | sed -e 's/_.*$$//') 39 | ifeq ($(UNAME),Linux) 40 | # ***** 41 | # *** Linux config 42 | # ***** 43 | CC=gcc -m32 44 | CC64=gcc 45 | CXX=g++ -m32 46 | CXX64=g++ 47 | STRIP=strip 48 | # /opt/xmingw/ for old gentoo, i586-mingw32msvc-gcc for debian 49 | # and mingw32-gcc for crossdev gentoo 50 | ifeq ($(shell which i586-mingw32msvc-gcc 2>/dev/null),) 51 | ifeq ($(shell which mingw32-gcc 2>/dev/null),) 52 | CC_WIN=/opt/xmingw/bin/i386-mingw32msvc-gcc 53 | CXX_WIN=/opt/xmingw/bin/i386-mingw32msvc-g++ 54 | STRIP_WIN=/opt/xmingw/bin/i386-mingw32msvc-strip 55 | else 56 | CC_WIN=mingw32-gcc 57 | CXX_WIN=mingw32-g++ 58 | STRIP_WIN=mingw32-strip 59 | endif 60 | else 61 | CC_WIN=i586-mingw32msvc-gcc 62 | CXX_WIN=i586-mingw32msvc-g++ 63 | STRIP_WIN=i586-mingw32msvc-strip 64 | endif 65 | BUILD=Linux 66 | WINFLAGS=-D__WIN32 -mwindows 67 | LINFLAGS=-fPIC -DPIC 68 | 69 | else 70 | ifeq ($(UNAME),CYGWIN) 71 | # ***** 72 | # *** Cygwin config 73 | # ***** 74 | CC=gcc 75 | CC_WIN=gcc 76 | STRIP=strip 77 | STRIP_WIN=strip 78 | BUILD=Cygwin 79 | WINFLAGS=-mno-cygwin -mwindows -D__WIN32 80 | LINFLAGS= 81 | endif 82 | endif 83 | QT_WIN=../qt/4.2.1 84 | QT_WIN_LIBS=-L$(QT_WIN)/lib -lQtGui4 -lQtCore4 85 | QT_WIN_INCLUDE=-I$(QT_WIN)/include -I$(QT_WIN)/include/QtCore -I$(QT_WIN)/include/QtGui -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB 86 | #g++ -c -pipe -O2 -Wall -W -D_REENTRANT -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt4/mkspecs/linux-g++ -I. -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtGui -I/usr/include/qt4 -I. -I. -I. -o main.o main.cpp 87 | #g++ -o grfbuilder main.o -L/usr/lib64/qt4 -lQtGui -L/usr/lib64 -L/usr/lib64/qt4 -lpng -lSM -lICE -lXi -lXrender -lXrandr -lXcursor -lXinerama -lfreetype -lfontconfig -lXext -lX11 -lQtCore -lz -lm -ldl -lpthread 88 | QT_LIN_LIBS=$(shell pkg-config --libs QtGui QtCore) 89 | QT_LIN_LIBS+=-lpthread 90 | QT_LIN_INCLUDE=-I/usr/share/qt4/mkspecs/linux-g++ $(shell pkg-config --cflags QtGui QtCore) 91 | QT_LIN_INCLUDE+=-D_REENTRANT -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB 92 | QT_LIN32_INCLUDE=$(patsubst -I%,-I/var/chroot/ia32%,$(QT_LIN_INCLUDE)) 93 | GCC_VERSION=$(shell $(CC) -dumpversion | awk -F. '{ print $$1 }') 94 | ifeq ($(GCC_VERSION),4) 95 | CFLAGS+=-Wno-attributes 96 | endif 97 | GCC_WIN_VERSION=$(shell $(CC_WIN) -dumpversion | awk -F. '{ print $$1 }') 98 | ifeq ($(GCC_WIN_VERSION),4) 99 | WINFLAGS+=-Wno-attributes 100 | endif 101 | 102 | GCC_VERSION=$(shell $(CC) -dumpversion) 103 | 104 | win32/%_32.o: src/%.c 105 | @echo -en " CC\t$< \015" 106 | @$(CC_WIN) $(CFLAGS) $(WINFLAGS) $(INCLUDES) -c -o $@ $< 107 | 108 | linux/%_32.o: src/%.c 109 | @echo -en " CC\t$< \015" 110 | @$(CC) $(CFLAGS) $(LINFLAGS) $(INCLUDES) -c -o $@ $< 111 | 112 | linux/%_64.o: src/%.c 113 | @echo -en " CC64\t$< \015" 114 | @$(CC64) $(CFLAGS) $(LINFLAGS) $(INCLUDES) -c -o $@ $< 115 | 116 | win32/%_32.o: zlib/%.c 117 | @echo -en " CC\t$< \015" 118 | @$(CC_WIN) $(CFLAGS) $(WINFLAGS) $(INCLUDES) -c -o $@ $< 119 | 120 | linux/%_64.o: zlib/%.c 121 | @echo -en " CC64\t$< \015" 122 | @$(CC64) $(CFLAGS) $(LINFLAGS) $(INCLUDES) -c -o $@ $< 123 | 124 | linux/%_32.o: zlib/%.c 125 | @echo -en " CC\t$< \015" 126 | @$(CC) $(CFLAGS) $(LINFLAGS) $(INCLUDES) -c -o $@ $< 127 | 128 | win32/gb_%_32.o: grfbuilder/%.cpp 129 | @echo -en " CXX\t$< \015" 130 | @$(CXX_WIN) $(CXXFLAGS) -Igrfbuilder $(WINFLAGS) $(QT_WIN_INCLUDE) $(INCLUDES) -c -o $@ $< 131 | 132 | linux/gb_%_32.o: grfbuilder/%.cpp 133 | @echo -en " CXX\t$< \015" 134 | @$(CXX) $(CXXFLAGS) -Igrfbuilder $(LINFLAGS) $(QT_LIN32_INCLUDE) $(INCLUDES) -c -o $@ $< 135 | 136 | linux/gb_%_64.o: grfbuilder/%.cpp 137 | @echo -en " CXX64\t$< \015" 138 | @$(CXX64) $(CXXFLAGS) -Igrfbuilder $(LINFLAGS) $(QT_LIN_INCLUDE) $(INCLUDES) -c -o $@ $< 139 | 140 | .PHONY: make_dirs test dist gb 141 | 142 | ifeq ($(BUILD),unknown) 143 | all: ;@echo "Unknown system $(UNAME) !" 144 | else 145 | all: make_dirs $(TARGET) $(TARGET) $(GB_TARGET) $(TARGET64) $(GB_TARGET64) grf_test_linux $(TARGET_WIN) grf_test_win.exe $(GB_TARGET_WIN) 146 | endif 147 | 148 | make_dirs: 149 | @mkdir win32 linux 2>/dev/null || true 150 | 151 | $(TARGET_WIN): $(patsubst %.o,win32/%.o,$(ZOBJS32) $(OBJECTS32)) 152 | @echo -e " LD\t$@ " 153 | @$(CC_WIN) $(CFLAGS) $(WINFLAGS) $(LDFLAGS) -Wl,--output-def,grf.def,--out-implib,libgrfdll.a -o $@ $^ 154 | ifeq ($(DEBUG),no) 155 | @echo -e " STRIP\t$@" 156 | @$(STRIP_WIN) $@ 157 | endif 158 | 159 | $(TARGET): $(patsubst %.o,linux/%.o,$(ZOBJS32) $(OBJECTS32)) 160 | @echo -e " LD\t$@ " 161 | @$(CC) $(CFLAGS) $(LINFLAGS) $(LDFLAGS) -o $@ $^ 162 | ifeq ($(DEBUG),no) 163 | @echo -e " STRIP\t$@" 164 | @$(STRIP) $@ 165 | endif 166 | 167 | $(TARGET64): $(patsubst %.o,linux/%.o,$(ZOBJS64) $(OBJECTS64)) 168 | @echo -e " LD\t$@ " 169 | @$(CC64) $(CFLAGS) $(LINFLAGS) $(LDFLAGS) -o $@ $^ 170 | ifeq ($(DEBUG),no) 171 | @echo -e " STRIP\t$@" 172 | @$(STRIP) $@ 173 | endif 174 | 175 | $(GB_TARGET): $(patsubst %.o,linux/gb_%.o,$(GB_OBJECTS32)) 176 | @echo -e " LD\t$@ " 177 | # @$(CXX) -nostdlib $(CXXFLAGS) $(LINFLAGS) $(QT_LIN32_INCLUDE) -o $@ $^ -L/var/chroot/ia32/lib -L/var/chroot/ia32/usr/lib -L/var/chroot/ia32/usr/lib/qt4 `echo "$(QT_LIN_LIBS)" | sed -e 's#-lpthread#/var/chroot/ia32/lib/libpthread.so.0#g'` -L. -lgrf /var/chroot/ia32/usr/lib/gcc/i686-pc-linux-gnu/4.1.1/libgcc.a /usr/lib/gcc/x86_64-pc-linux-gnu/4.1.1/32/libgcc.a /var/chroot/ia32/lib/libc.so.6 178 | # @$(CXX) -v $(CXXFLAGS) $(LINFLAGS) $(QT_LIN32_INCLUDE) -o $@ $^ -L/var/chroot/ia32/lib -L/var/chroot/ia32/usr/lib -L/var/chroot/ia32/usr/lib/qt4 `echo "$(QT_LIN_LIBS)" | sed -e 's#-lpthread#/var/chroot/ia32/lib/libpthread.so.0#g'` -L. -lgrf /var/chroot/ia32/usr/lib/gcc/i686-pc-linux-gnu/4.1.1/libgcc.a /usr/lib/gcc/x86_64-pc-linux-gnu/4.1.1/32/libgcc.a /var/chroot/ia32/lib/libc.so.6 179 | @$(CXX) -nostdlib $(CXXFLAGS) $(LINFLAGS) $(QT_LIN32_INCLUDE) -o $@ /usr/lib/gcc/x86_64-pc-linux-gnu/$(GCC_VERSION)/../../../../lib32/crt1.o /usr/lib/gcc/x86_64-pc-linux-gnu/$(GCC_VERSION)/../../../../lib32/crti.o /usr/lib/gcc/x86_64-pc-linux-gnu/$(GCC_VERSION)/32/crtbegin.o $^ -L/var/chroot/ia32/lib -L/var/chroot/ia32/usr/lib -L/var/chroot/ia32/usr/lib/qt4 `echo "$(QT_LIN_LIBS)" | sed -e 's#-lpthread#/var/chroot/ia32/lib/libpthread.so.0#g'` -L. -lgrf /var/chroot/ia32/usr/lib/gcc/i686-pc-linux-gnu/$(GCC_VERSION)/libgcc.a /usr/lib/gcc/x86_64-pc-linux-gnu/$(GCC_VERSION)/32/libgcc.a /usr/lib32/libc.so -L/usr/lib/gcc/x86_64-pc-linux-gnu/$(GCC_VERSION)/32 -lstdc++ -lm -lgcc_s -lgcc -lgcc_s /usr/lib32/libc.so -lgcc /usr/lib/gcc/x86_64-pc-linux-gnu/$(GCC_VERSION)/32/crtend.o /usr/lib/gcc/x86_64-pc-linux-gnu/$(GCC_VERSION)/../../../../lib32/crtn.o 180 | ifeq ($(DEBUG),no) 181 | @echo -e " STRIP\t$@" 182 | @$(STRIP) $@ 183 | endif 184 | 185 | $(GB_TARGET64): $(patsubst %.o,linux/gb_%.o,$(GB_OBJECTS64)) 186 | @echo -e " LD\t$@ " 187 | @$(CXX64) $(CXXFLAGS) $(LINFLAGS) $(QT_LIN_INCLUDE) -o $@ $^ $(QT_LIN_LIBS) -L. -lgrf64 188 | ifeq ($(DEBUG),no) 189 | @echo -e " STRIP\t$@" 190 | @$(STRIP) $@ 191 | endif 192 | 193 | $(GB_TARGET_WIN): $(patsubst %.o,win32/gb_%.o,$(GB_OBJECTS32)) 194 | @echo -e " LD\t$@ " 195 | @$(CXX_WIN) $(CXXFLAGS) $(WINFLAGS) $(QT_WIN_INCLUDE) -o $@ $^ $(QT_WIN_LIBS) -L. -lgrf 196 | ifeq ($(DEBUG),no) 197 | @echo -e " STRIP\t$@" 198 | @$(STRIP_WIN) $@ 199 | endif 200 | 201 | grf.def: $(TARGET_WIN) 202 | 203 | version.sh: includes/grf.h 204 | cat $< | grep "define VERSION" | grep -E "MAJOR|MINOR|REVISION" | sed -e 's/^#define //;s/ /=/' >$@ 205 | 206 | libgrf-%.zip: $(TARGET) $(TARGET64) $(TARGET_WIN) grf.def includes/libgrf.h $(wildcard examples/*) doc/README doc/grf_magic 207 | $(RM) $@ 208 | zip -9r $@ $^ -x .svn '*.o' data 209 | 210 | libgrf-%.tar.gz: $(TARGET) $(TARGET64) $(TARGET_WIN) grf.def includes/libgrf.h $(wildcard examples/*) doc/README doc/grf_magic 211 | tar -cvzf $@ --exclude '*.o' --exclude '.svn' --exclude 'data' $^ 212 | 213 | grfbuilder-%.zip: $(TARGET_WIN) doc/README QtCore4.dll QtGui4.dll mingwm10.dll $(GB_TARGET_WIN) $(GB_LOCALES) 214 | $(RM) $@ 215 | zip -9r $@ $^ -x .svn '*.o' data 216 | 217 | grfbuilder-%.tar.gz: $(TARGET) $(TARGET64) doc/README $(GB_TARGET) $(GB_TARGET64) $(GB_LOCALES) 218 | tar -cvzf $@ --exclude '*.o' --exclude '.svn' --exclude 'data' $^ 219 | 220 | dist: make_dirs version.sh 221 | . version.sh; for foo in $(subst ^,$$VERSION_MAJOR.$$VERSION_MINOR.$$VERSION_REVISION,$(DIST_FILES)); do $(MAKE) -C . "$$foo" DEBUG=no; done 222 | 223 | grf_test_win.exe: win32/test_32.o $(TARGET_WIN) 224 | @echo -e " LD\t$@ " 225 | @$(CC_WIN) $(CFLAGS) $(WINFLAGS) $(LDFLAGS_TEST) -o $@ $< -L. -lgrf 226 | 227 | grf_test_linux: linux/test_64.o $(TARGET64) 228 | @echo -e " LD\t$@ " 229 | @$(CC64) $(CFLAGS) $(LINFLAGS) $(LDFLAGS_TEST) -o $@ $< -L. -lgrf64 230 | 231 | ifeq ($(UNAME),Linux) 232 | test: make_dirs grf_test_linux 233 | @LD_LIBRARY_PATH="." ./grf_test_linux 234 | 235 | leak: make_dirs grf_test_linux 236 | @LD_LIBRARY_PATH="." valgrind --show-reachable=yes --leak-check=full ./grf_test_linux 237 | 238 | gdb: make_dirs grf_test_linux 239 | @LD_LIBRARY_PATH="." gdb ./grf_test_linux 240 | 241 | gb: make_dirs libgrf64.so grfbuilder_64.bin $(GB_LOCALES) 242 | @LD_LIBRARY_PATH="." ./grfbuilder_64.bin 243 | 244 | else 245 | ifeq ($(UNAME),CYGWIN) 246 | test: make_dirs grf_test_win.exe 247 | ./grf_test_win.exe 248 | else 249 | test: ;@echo "No test available for your platform ($(UNAME))." 250 | endif 251 | endif 252 | 253 | ## SPECIFIC RULES 254 | grfbuilder/moc_MainWindow.cpp: grfbuilder/MainWindow.h 255 | @moc $(QT_LIN_INCLUDE) $< -o $@ 256 | grfbuilder/main.cpp: grfbuilder/MainWindow.h grfbuilder/ui_MainWindow.h 257 | grfbuilder/MainWindow.cpp: grfbuilder/MainWindow.h grfbuilder/ui_MainWindow.h 258 | grfbuilder/MainWindow.h: grfbuilder/ui_MainWindow.h 259 | grfbuilder/ui_MainWindow.h: grfbuilder/MainWindow.ui 260 | @uic $< >$@ 261 | grfbuilder/grfbuilder_%.qm: grfbuilder/locales/grfbuilder_%.ts grfbuilder/locales/grfbuilder_empty.ts 262 | @lrelease $< -qm $@ 263 | grfbuilder/locales/grfbuilder_%.ts: $(wildcard grfbuilder/*.cpp grfbuilder/*.h) grfbuilder/ui_MainWindow.h 264 | @lupdate $^ -ts $@ 265 | grfbuilder_%.qm: grfbuilder/grfbuilder_%.qm 266 | cp $< $@ 267 | 268 | %.dll: $(QT_WIN)/bin/%.dll 269 | cp $< $@ 270 | 271 | clean: 272 | $(RM) -r linux $(TARGET) $(TARGET64) win32 $(TARGET_WIN) $(GB_TARGET) $(GB_TARGET64) $(GB_TARGET_WIN) grf_test_win.exe grf_test_linux version.sh 273 | $(RM) grfbuilder/ui_MainWindow.h grfbuilder/moc_MainWindow.cpp grfbuilder/grfbuilder_fr.qm grfbuilder_fr.qm 274 | $(RM) mingwm10.dll QtCore4.dll QtGui4.dll $(GB_LOCALES) $(patsubst %,grfbuilder/%,$(GB_LOCALES)) 275 | $(RM) $(subst ^,*.*.*,$(DIST_FILES)) 276 | 277 | -------------------------------------------------------------------------------- /zlib/deflate.h: -------------------------------------------------------------------------------- 1 | /* deflate.h -- internal compression state 2 | * Copyright (C) 1995-2004 Jean-loup Gailly 3 | * For conditions of distribution and use, see copyright notice in zlib.h 4 | */ 5 | 6 | /* WARNING: this file should *not* be used by applications. It is 7 | part of the implementation of the compression library and is 8 | subject to change. Applications should only use zlib.h. 9 | */ 10 | 11 | /* @(#) $Id$ */ 12 | 13 | #ifndef DEFLATE_H 14 | #define DEFLATE_H 15 | 16 | #include "zutil.h" 17 | 18 | /* define NO_GZIP when compiling if you want to disable gzip header and 19 | trailer creation by deflate(). NO_GZIP would be used to avoid linking in 20 | the crc code when it is not needed. For shared libraries, gzip encoding 21 | should be left enabled. */ 22 | #ifndef NO_GZIP 23 | # define GZIP 24 | #endif 25 | 26 | /* =========================================================================== 27 | * Internal compression state. 28 | */ 29 | 30 | #define LENGTH_CODES 29 31 | /* number of length codes, not counting the special END_BLOCK code */ 32 | 33 | #define LITERALS 256 34 | /* number of literal bytes 0..255 */ 35 | 36 | #define L_CODES (LITERALS+1+LENGTH_CODES) 37 | /* number of Literal or Length codes, including the END_BLOCK code */ 38 | 39 | #define D_CODES 30 40 | /* number of distance codes */ 41 | 42 | #define BL_CODES 19 43 | /* number of codes used to transfer the bit lengths */ 44 | 45 | #define HEAP_SIZE (2*L_CODES+1) 46 | /* maximum heap size */ 47 | 48 | #define MAX_BITS 15 49 | /* All codes must not exceed MAX_BITS bits */ 50 | 51 | #define INIT_STATE 42 52 | #define EXTRA_STATE 69 53 | #define NAME_STATE 73 54 | #define COMMENT_STATE 91 55 | #define HCRC_STATE 103 56 | #define BUSY_STATE 113 57 | #define FINISH_STATE 666 58 | /* Stream status */ 59 | 60 | 61 | /* Data structure describing a single value and its code string. */ 62 | typedef struct ct_data_s { 63 | union { 64 | ush freq; /* frequency count */ 65 | ush code; /* bit string */ 66 | } fc; 67 | union { 68 | ush dad; /* father node in Huffman tree */ 69 | ush len; /* length of bit string */ 70 | } dl; 71 | } FAR ct_data; 72 | 73 | #define Freq fc.freq 74 | #define Code fc.code 75 | #define Dad dl.dad 76 | #define Len dl.len 77 | 78 | typedef struct static_tree_desc_s static_tree_desc; 79 | 80 | typedef struct tree_desc_s { 81 | ct_data *dyn_tree; /* the dynamic tree */ 82 | int max_code; /* largest code with non zero frequency */ 83 | static_tree_desc *stat_desc; /* the corresponding static tree */ 84 | } FAR tree_desc; 85 | 86 | typedef ush Pos; 87 | typedef Pos FAR Posf; 88 | typedef unsigned IPos; 89 | 90 | /* A Pos is an index in the character window. We use short instead of int to 91 | * save space in the various tables. IPos is used only for parameter passing. 92 | */ 93 | 94 | typedef struct internal_state { 95 | z_streamp strm; /* pointer back to this zlib stream */ 96 | int status; /* as the name implies */ 97 | Bytef *pending_buf; /* output still pending */ 98 | ulg pending_buf_size; /* size of pending_buf */ 99 | Bytef *pending_out; /* next pending byte to output to the stream */ 100 | uInt pending; /* nb of bytes in the pending buffer */ 101 | int wrap; /* bit 0 true for zlib, bit 1 true for gzip */ 102 | gz_headerp gzhead; /* gzip header information to write */ 103 | uInt gzindex; /* where in extra, name, or comment */ 104 | Byte method; /* STORED (for zip only) or DEFLATED */ 105 | int last_flush; /* value of flush param for previous deflate call */ 106 | 107 | /* used by deflate.c: */ 108 | 109 | uInt w_size; /* LZ77 window size (32K by default) */ 110 | uInt w_bits; /* log2(w_size) (8..16) */ 111 | uInt w_mask; /* w_size - 1 */ 112 | 113 | Bytef *window; 114 | /* Sliding window. Input bytes are read into the second half of the window, 115 | * and move to the first half later to keep a dictionary of at least wSize 116 | * bytes. With this organization, matches are limited to a distance of 117 | * wSize-MAX_MATCH bytes, but this ensures that IO is always 118 | * performed with a length multiple of the block size. Also, it limits 119 | * the window size to 64K, which is quite useful on MSDOS. 120 | * To do: use the user input buffer as sliding window. 121 | */ 122 | 123 | ulg window_size; 124 | /* Actual size of window: 2*wSize, except when the user input buffer 125 | * is directly used as sliding window. 126 | */ 127 | 128 | Posf *prev; 129 | /* Link to older string with same hash index. To limit the size of this 130 | * array to 64K, this link is maintained only for the last 32K strings. 131 | * An index in this array is thus a window index modulo 32K. 132 | */ 133 | 134 | Posf *head; /* Heads of the hash chains or NIL. */ 135 | 136 | uInt ins_h; /* hash index of string to be inserted */ 137 | uInt hash_size; /* number of elements in hash table */ 138 | uInt hash_bits; /* log2(hash_size) */ 139 | uInt hash_mask; /* hash_size-1 */ 140 | 141 | uInt hash_shift; 142 | /* Number of bits by which ins_h must be shifted at each input 143 | * step. It must be such that after MIN_MATCH steps, the oldest 144 | * byte no longer takes part in the hash key, that is: 145 | * hash_shift * MIN_MATCH >= hash_bits 146 | */ 147 | 148 | long block_start; 149 | /* Window position at the beginning of the current output block. Gets 150 | * negative when the window is moved backwards. 151 | */ 152 | 153 | uInt match_length; /* length of best match */ 154 | IPos prev_match; /* previous match */ 155 | int match_available; /* set if previous match exists */ 156 | uInt strstart; /* start of string to insert */ 157 | uInt match_start; /* start of matching string */ 158 | uInt lookahead; /* number of valid bytes ahead in window */ 159 | 160 | uInt prev_length; 161 | /* Length of the best match at previous step. Matches not greater than this 162 | * are discarded. This is used in the lazy match evaluation. 163 | */ 164 | 165 | uInt max_chain_length; 166 | /* To speed up deflation, hash chains are never searched beyond this 167 | * length. A higher limit improves compression ratio but degrades the 168 | * speed. 169 | */ 170 | 171 | uInt max_lazy_match; 172 | /* Attempt to find a better match only when the current match is strictly 173 | * smaller than this value. This mechanism is used only for compression 174 | * levels >= 4. 175 | */ 176 | # define max_insert_length max_lazy_match 177 | /* Insert new strings in the hash table only if the match length is not 178 | * greater than this length. This saves time but degrades compression. 179 | * max_insert_length is used only for compression levels <= 3. 180 | */ 181 | 182 | int level; /* compression level (1..9) */ 183 | int strategy; /* favor or force Huffman coding*/ 184 | 185 | uInt good_match; 186 | /* Use a faster search when the previous match is longer than this */ 187 | 188 | int nice_match; /* Stop searching when current match exceeds this */ 189 | 190 | /* used by trees.c: */ 191 | /* Didn't use ct_data typedef below to supress compiler warning */ 192 | struct ct_data_s dyn_ltree[HEAP_SIZE]; /* literal and length tree */ 193 | struct ct_data_s dyn_dtree[2*D_CODES+1]; /* distance tree */ 194 | struct ct_data_s bl_tree[2*BL_CODES+1]; /* Huffman tree for bit lengths */ 195 | 196 | struct tree_desc_s l_desc; /* desc. for literal tree */ 197 | struct tree_desc_s d_desc; /* desc. for distance tree */ 198 | struct tree_desc_s bl_desc; /* desc. for bit length tree */ 199 | 200 | ush bl_count[MAX_BITS+1]; 201 | /* number of codes at each bit length for an optimal tree */ 202 | 203 | int heap[2*L_CODES+1]; /* heap used to build the Huffman trees */ 204 | int heap_len; /* number of elements in the heap */ 205 | int heap_max; /* element of largest frequency */ 206 | /* The sons of heap[n] are heap[2*n] and heap[2*n+1]. heap[0] is not used. 207 | * The same heap array is used to build all trees. 208 | */ 209 | 210 | uch depth[2*L_CODES+1]; 211 | /* Depth of each subtree used as tie breaker for trees of equal frequency 212 | */ 213 | 214 | uchf *l_buf; /* buffer for literals or lengths */ 215 | 216 | uInt lit_bufsize; 217 | /* Size of match buffer for literals/lengths. There are 4 reasons for 218 | * limiting lit_bufsize to 64K: 219 | * - frequencies can be kept in 16 bit counters 220 | * - if compression is not successful for the first block, all input 221 | * data is still in the window so we can still emit a stored block even 222 | * when input comes from standard input. (This can also be done for 223 | * all blocks if lit_bufsize is not greater than 32K.) 224 | * - if compression is not successful for a file smaller than 64K, we can 225 | * even emit a stored file instead of a stored block (saving 5 bytes). 226 | * This is applicable only for zip (not gzip or zlib). 227 | * - creating new Huffman trees less frequently may not provide fast 228 | * adaptation to changes in the input data statistics. (Take for 229 | * example a binary file with poorly compressible code followed by 230 | * a highly compressible string table.) Smaller buffer sizes give 231 | * fast adaptation but have of course the overhead of transmitting 232 | * trees more frequently. 233 | * - I can't count above 4 234 | */ 235 | 236 | uInt last_lit; /* running index in l_buf */ 237 | 238 | ushf *d_buf; 239 | /* Buffer for distances. To simplify the code, d_buf and l_buf have 240 | * the same number of elements. To use different lengths, an extra flag 241 | * array would be necessary. 242 | */ 243 | 244 | ulg opt_len; /* bit length of current block with optimal trees */ 245 | ulg static_len; /* bit length of current block with static trees */ 246 | uInt matches; /* number of string matches in current block */ 247 | int last_eob_len; /* bit length of EOB code for last block */ 248 | 249 | #ifdef DEBUG 250 | ulg compressed_len; /* total bit length of compressed file mod 2^32 */ 251 | ulg bits_sent; /* bit length of compressed data sent mod 2^32 */ 252 | #endif 253 | 254 | ush bi_buf; 255 | /* Output buffer. bits are inserted starting at the bottom (least 256 | * significant bits). 257 | */ 258 | int bi_valid; 259 | /* Number of valid bits in bi_buf. All bits above the last valid bit 260 | * are always zero. 261 | */ 262 | 263 | } FAR deflate_state; 264 | 265 | /* Output a byte on the stream. 266 | * IN assertion: there is enough room in pending_buf. 267 | */ 268 | #define put_byte(s, c) {s->pending_buf[s->pending++] = (c);} 269 | 270 | 271 | #define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1) 272 | /* Minimum amount of lookahead, except at the end of the input file. 273 | * See deflate.c for comments about the MIN_MATCH+1. 274 | */ 275 | 276 | #define MAX_DIST(s) ((s)->w_size-MIN_LOOKAHEAD) 277 | /* In order to simplify the code, particularly on 16 bit machines, match 278 | * distances are limited to MAX_DIST instead of WSIZE. 279 | */ 280 | 281 | /* in trees.c */ 282 | void _tr_init OF((deflate_state *s)); 283 | int _tr_tally OF((deflate_state *s, unsigned dist, unsigned lc)); 284 | void _tr_flush_block OF((deflate_state *s, charf *buf, ulg stored_len, 285 | int eof)); 286 | void _tr_align OF((deflate_state *s)); 287 | void _tr_stored_block OF((deflate_state *s, charf *buf, ulg stored_len, 288 | int eof)); 289 | 290 | #define d_code(dist) \ 291 | ((dist) < 256 ? _dist_code[dist] : _dist_code[256+((dist)>>7)]) 292 | /* Mapping from a distance to a distance code. dist is the distance - 1 and 293 | * must not have side effects. _dist_code[256] and _dist_code[257] are never 294 | * used. 295 | */ 296 | 297 | #ifndef DEBUG 298 | /* Inline versions of _tr_tally for speed: */ 299 | 300 | #if defined(GEN_TREES_H) || !defined(STDC) 301 | extern uch _length_code[]; 302 | extern uch _dist_code[]; 303 | #else 304 | extern const uch _length_code[]; 305 | extern const uch _dist_code[]; 306 | #endif 307 | 308 | # define _tr_tally_lit(s, c, flush) \ 309 | { uch cc = (c); \ 310 | s->d_buf[s->last_lit] = 0; \ 311 | s->l_buf[s->last_lit++] = cc; \ 312 | s->dyn_ltree[cc].Freq++; \ 313 | flush = (s->last_lit == s->lit_bufsize-1); \ 314 | } 315 | # define _tr_tally_dist(s, distance, length, flush) \ 316 | { uch len = (length); \ 317 | ush dist = (distance); \ 318 | s->d_buf[s->last_lit] = dist; \ 319 | s->l_buf[s->last_lit++] = len; \ 320 | dist--; \ 321 | s->dyn_ltree[_length_code[len]+LITERALS+1].Freq++; \ 322 | s->dyn_dtree[d_code(dist)].Freq++; \ 323 | flush = (s->last_lit == s->lit_bufsize-1); \ 324 | } 325 | #else 326 | # define _tr_tally_lit(s, c, flush) flush = _tr_tally(s, 0, c) 327 | # define _tr_tally_dist(s, distance, length, flush) \ 328 | flush = _tr_tally(s, distance, length) 329 | #endif 330 | 331 | #endif /* DEFLATE_H */ 332 | -------------------------------------------------------------------------------- /zlib/inffast.c: -------------------------------------------------------------------------------- 1 | /* inffast.c -- fast decoding 2 | * Copyright (C) 1995-2004 Mark Adler 3 | * For conditions of distribution and use, see copyright notice in zlib.h 4 | */ 5 | 6 | #include "zutil.h" 7 | #include "inftrees.h" 8 | #include "inflate.h" 9 | #include "inffast.h" 10 | 11 | #ifndef ASMINF 12 | 13 | /* Allow machine dependent optimization for post-increment or pre-increment. 14 | Based on testing to date, 15 | Pre-increment preferred for: 16 | - PowerPC G3 (Adler) 17 | - MIPS R5000 (Randers-Pehrson) 18 | Post-increment preferred for: 19 | - none 20 | No measurable difference: 21 | - Pentium III (Anderson) 22 | - M68060 (Nikl) 23 | */ 24 | #ifdef POSTINC 25 | # define OFF 0 26 | # define PUP(a) *(a)++ 27 | #else 28 | # define OFF 1 29 | # define PUP(a) *++(a) 30 | #endif 31 | 32 | /* 33 | Decode literal, length, and distance codes and write out the resulting 34 | literal and match bytes until either not enough input or output is 35 | available, an end-of-block is encountered, or a data error is encountered. 36 | When large enough input and output buffers are supplied to inflate(), for 37 | example, a 16K input buffer and a 64K output buffer, more than 95% of the 38 | inflate execution time is spent in this routine. 39 | 40 | Entry assumptions: 41 | 42 | state->mode == LEN 43 | strm->avail_in >= 6 44 | strm->avail_out >= 258 45 | start >= strm->avail_out 46 | state->bits < 8 47 | 48 | On return, state->mode is one of: 49 | 50 | LEN -- ran out of enough output space or enough available input 51 | TYPE -- reached end of block code, inflate() to interpret next block 52 | BAD -- error in block data 53 | 54 | Notes: 55 | 56 | - The maximum input bits used by a length/distance pair is 15 bits for the 57 | length code, 5 bits for the length extra, 15 bits for the distance code, 58 | and 13 bits for the distance extra. This totals 48 bits, or six bytes. 59 | Therefore if strm->avail_in >= 6, then there is enough input to avoid 60 | checking for available input while decoding. 61 | 62 | - The maximum bytes that a single length/distance pair can output is 258 63 | bytes, which is the maximum length that can be coded. inflate_fast() 64 | requires strm->avail_out >= 258 for each loop to avoid checking for 65 | output space. 66 | */ 67 | void inflate_fast(strm, start) 68 | z_streamp strm; 69 | unsigned start; /* inflate()'s starting value for strm->avail_out */ 70 | { 71 | struct inflate_state FAR *state; 72 | unsigned char FAR *in; /* local strm->next_in */ 73 | unsigned char FAR *last; /* while in < last, enough input available */ 74 | unsigned char FAR *out; /* local strm->next_out */ 75 | unsigned char FAR *beg; /* inflate()'s initial strm->next_out */ 76 | unsigned char FAR *end; /* while out < end, enough space available */ 77 | #ifdef INFLATE_STRICT 78 | unsigned dmax; /* maximum distance from zlib header */ 79 | #endif 80 | unsigned wsize; /* window size or zero if not using window */ 81 | unsigned whave; /* valid bytes in the window */ 82 | unsigned write; /* window write index */ 83 | unsigned char FAR *window; /* allocated sliding window, if wsize != 0 */ 84 | unsigned long hold; /* local strm->hold */ 85 | unsigned bits; /* local strm->bits */ 86 | code const FAR *lcode; /* local strm->lencode */ 87 | code const FAR *dcode; /* local strm->distcode */ 88 | unsigned lmask; /* mask for first level of length codes */ 89 | unsigned dmask; /* mask for first level of distance codes */ 90 | code this; /* retrieved table entry */ 91 | unsigned op; /* code bits, operation, extra bits, or */ 92 | /* window position, window bytes to copy */ 93 | unsigned len; /* match length, unused bytes */ 94 | unsigned dist; /* match distance */ 95 | unsigned char FAR *from; /* where to copy match from */ 96 | 97 | /* copy state to local variables */ 98 | state = (struct inflate_state FAR *)strm->state; 99 | in = strm->next_in - OFF; 100 | last = in + (strm->avail_in - 5); 101 | out = strm->next_out - OFF; 102 | beg = out - (start - strm->avail_out); 103 | end = out + (strm->avail_out - 257); 104 | #ifdef INFLATE_STRICT 105 | dmax = state->dmax; 106 | #endif 107 | wsize = state->wsize; 108 | whave = state->whave; 109 | write = state->write; 110 | window = state->window; 111 | hold = state->hold; 112 | bits = state->bits; 113 | lcode = state->lencode; 114 | dcode = state->distcode; 115 | lmask = (1U << state->lenbits) - 1; 116 | dmask = (1U << state->distbits) - 1; 117 | 118 | /* decode literals and length/distances until end-of-block or not enough 119 | input data or output space */ 120 | do { 121 | if (bits < 15) { 122 | hold += (unsigned long)(PUP(in)) << bits; 123 | bits += 8; 124 | hold += (unsigned long)(PUP(in)) << bits; 125 | bits += 8; 126 | } 127 | this = lcode[hold & lmask]; 128 | dolen: 129 | op = (unsigned)(this.bits); 130 | hold >>= op; 131 | bits -= op; 132 | op = (unsigned)(this.op); 133 | if (op == 0) { /* literal */ 134 | Tracevv((stderr, this.val >= 0x20 && this.val < 0x7f ? 135 | "inflate: literal '%c'\n" : 136 | "inflate: literal 0x%02x\n", this.val)); 137 | PUP(out) = (unsigned char)(this.val); 138 | } 139 | else if (op & 16) { /* length base */ 140 | len = (unsigned)(this.val); 141 | op &= 15; /* number of extra bits */ 142 | if (op) { 143 | if (bits < op) { 144 | hold += (unsigned long)(PUP(in)) << bits; 145 | bits += 8; 146 | } 147 | len += (unsigned)hold & ((1U << op) - 1); 148 | hold >>= op; 149 | bits -= op; 150 | } 151 | Tracevv((stderr, "inflate: length %u\n", len)); 152 | if (bits < 15) { 153 | hold += (unsigned long)(PUP(in)) << bits; 154 | bits += 8; 155 | hold += (unsigned long)(PUP(in)) << bits; 156 | bits += 8; 157 | } 158 | this = dcode[hold & dmask]; 159 | dodist: 160 | op = (unsigned)(this.bits); 161 | hold >>= op; 162 | bits -= op; 163 | op = (unsigned)(this.op); 164 | if (op & 16) { /* distance base */ 165 | dist = (unsigned)(this.val); 166 | op &= 15; /* number of extra bits */ 167 | if (bits < op) { 168 | hold += (unsigned long)(PUP(in)) << bits; 169 | bits += 8; 170 | if (bits < op) { 171 | hold += (unsigned long)(PUP(in)) << bits; 172 | bits += 8; 173 | } 174 | } 175 | dist += (unsigned)hold & ((1U << op) - 1); 176 | #ifdef INFLATE_STRICT 177 | if (dist > dmax) { 178 | strm->msg = (char *)"invalid distance too far back"; 179 | state->mode = BAD; 180 | break; 181 | } 182 | #endif 183 | hold >>= op; 184 | bits -= op; 185 | Tracevv((stderr, "inflate: distance %u\n", dist)); 186 | op = (unsigned)(out - beg); /* max distance in output */ 187 | if (dist > op) { /* see if copy from window */ 188 | op = dist - op; /* distance back in window */ 189 | if (op > whave) { 190 | strm->msg = (char *)"invalid distance too far back"; 191 | state->mode = BAD; 192 | break; 193 | } 194 | from = window - OFF; 195 | if (write == 0) { /* very common case */ 196 | from += wsize - op; 197 | if (op < len) { /* some from window */ 198 | len -= op; 199 | do { 200 | PUP(out) = PUP(from); 201 | } while (--op); 202 | from = out - dist; /* rest from output */ 203 | } 204 | } 205 | else if (write < op) { /* wrap around window */ 206 | from += wsize + write - op; 207 | op -= write; 208 | if (op < len) { /* some from end of window */ 209 | len -= op; 210 | do { 211 | PUP(out) = PUP(from); 212 | } while (--op); 213 | from = window - OFF; 214 | if (write < len) { /* some from start of window */ 215 | op = write; 216 | len -= op; 217 | do { 218 | PUP(out) = PUP(from); 219 | } while (--op); 220 | from = out - dist; /* rest from output */ 221 | } 222 | } 223 | } 224 | else { /* contiguous in window */ 225 | from += write - op; 226 | if (op < len) { /* some from window */ 227 | len -= op; 228 | do { 229 | PUP(out) = PUP(from); 230 | } while (--op); 231 | from = out - dist; /* rest from output */ 232 | } 233 | } 234 | while (len > 2) { 235 | PUP(out) = PUP(from); 236 | PUP(out) = PUP(from); 237 | PUP(out) = PUP(from); 238 | len -= 3; 239 | } 240 | if (len) { 241 | PUP(out) = PUP(from); 242 | if (len > 1) 243 | PUP(out) = PUP(from); 244 | } 245 | } 246 | else { 247 | from = out - dist; /* copy direct from output */ 248 | do { /* minimum length is three */ 249 | PUP(out) = PUP(from); 250 | PUP(out) = PUP(from); 251 | PUP(out) = PUP(from); 252 | len -= 3; 253 | } while (len > 2); 254 | if (len) { 255 | PUP(out) = PUP(from); 256 | if (len > 1) 257 | PUP(out) = PUP(from); 258 | } 259 | } 260 | } 261 | else if ((op & 64) == 0) { /* 2nd level distance code */ 262 | this = dcode[this.val + (hold & ((1U << op) - 1))]; 263 | goto dodist; 264 | } 265 | else { 266 | strm->msg = (char *)"invalid distance code"; 267 | state->mode = BAD; 268 | break; 269 | } 270 | } 271 | else if ((op & 64) == 0) { /* 2nd level length code */ 272 | this = lcode[this.val + (hold & ((1U << op) - 1))]; 273 | goto dolen; 274 | } 275 | else if (op & 32) { /* end-of-block */ 276 | Tracevv((stderr, "inflate: end of block\n")); 277 | state->mode = TYPE; 278 | break; 279 | } 280 | else { 281 | strm->msg = (char *)"invalid literal/length code"; 282 | state->mode = BAD; 283 | break; 284 | } 285 | } while (in < last && out < end); 286 | 287 | /* return unused bytes (on entry, bits < 8, so in won't go too far back) */ 288 | len = bits >> 3; 289 | in -= len; 290 | bits -= len << 3; 291 | hold &= (1U << bits) - 1; 292 | 293 | /* update state and return */ 294 | strm->next_in = in + OFF; 295 | strm->next_out = out + OFF; 296 | strm->avail_in = (unsigned)(in < last ? 5 + (last - in) : 5 - (in - last)); 297 | strm->avail_out = (unsigned)(out < end ? 298 | 257 + (end - out) : 257 - (out - end)); 299 | state->hold = hold; 300 | state->bits = bits; 301 | return; 302 | } 303 | 304 | /* 305 | inflate_fast() speedups that turned out slower (on a PowerPC G3 750CXe): 306 | - Using bit fields for code structure 307 | - Different op definition to avoid & for extra bits (do & for table bits) 308 | - Three separate decoding do-loops for direct, window, and write == 0 309 | - Special case for distance > 1 copies to do overlapped load and store copy 310 | - Explicit branch predictions (based on measured branch probabilities) 311 | - Deferring match copy and interspersed it with decoding subsequent codes 312 | - Swapping literal/length else 313 | - Swapping window/direct else 314 | - Larger unrolled copy loops (three is about right) 315 | - Moving len -= 3 statement into middle of loop 316 | */ 317 | 318 | #endif /* !ASMINF */ 319 | -------------------------------------------------------------------------------- /zlib/crc32.c: -------------------------------------------------------------------------------- 1 | /* crc32.c -- compute the CRC-32 of a data stream 2 | * Copyright (C) 1995-2005 Mark Adler 3 | * For conditions of distribution and use, see copyright notice in zlib.h 4 | * 5 | * Thanks to Rodney Brown for his contribution of faster 6 | * CRC methods: exclusive-oring 32 bits of data at a time, and pre-computing 7 | * tables for updating the shift register in one step with three exclusive-ors 8 | * instead of four steps with four exclusive-ors. This results in about a 9 | * factor of two increase in speed on a Power PC G4 (PPC7455) using gcc -O3. 10 | */ 11 | 12 | /* @(#) $Id$ */ 13 | 14 | /* 15 | Note on the use of DYNAMIC_CRC_TABLE: there is no mutex or semaphore 16 | protection on the static variables used to control the first-use generation 17 | of the crc tables. Therefore, if you #define DYNAMIC_CRC_TABLE, you should 18 | first call get_crc_table() to initialize the tables before allowing more than 19 | one thread to use crc32(). 20 | */ 21 | 22 | #ifdef MAKECRCH 23 | # include 24 | # ifndef DYNAMIC_CRC_TABLE 25 | # define DYNAMIC_CRC_TABLE 26 | # endif /* !DYNAMIC_CRC_TABLE */ 27 | #endif /* MAKECRCH */ 28 | 29 | #include "zutil.h" /* for STDC and FAR definitions */ 30 | 31 | #define local static 32 | 33 | /* Find a four-byte integer type for crc32_little() and crc32_big(). */ 34 | #ifndef NOBYFOUR 35 | # ifdef STDC /* need ANSI C limits.h to determine sizes */ 36 | # include 37 | # define BYFOUR 38 | # if (UINT_MAX == 0xffffffffUL) 39 | typedef unsigned int u4; 40 | # else 41 | # if (ULONG_MAX == 0xffffffffUL) 42 | typedef unsigned long u4; 43 | # else 44 | # if (USHRT_MAX == 0xffffffffUL) 45 | typedef unsigned short u4; 46 | # else 47 | # undef BYFOUR /* can't find a four-byte integer type! */ 48 | # endif 49 | # endif 50 | # endif 51 | # endif /* STDC */ 52 | #endif /* !NOBYFOUR */ 53 | 54 | /* Definitions for doing the crc four data bytes at a time. */ 55 | #ifdef BYFOUR 56 | # define REV(w) (((w)>>24)+(((w)>>8)&0xff00)+ \ 57 | (((w)&0xff00)<<8)+(((w)&0xff)<<24)) 58 | local unsigned long crc32_little OF((unsigned long, 59 | const unsigned char FAR *, unsigned)); 60 | local unsigned long crc32_big OF((unsigned long, 61 | const unsigned char FAR *, unsigned)); 62 | # define TBLS 8 63 | #else 64 | # define TBLS 1 65 | #endif /* BYFOUR */ 66 | 67 | /* Local functions for crc concatenation */ 68 | local unsigned long gf2_matrix_times OF((unsigned long *mat, 69 | unsigned long vec)); 70 | local void gf2_matrix_square OF((unsigned long *square, unsigned long *mat)); 71 | 72 | #ifdef DYNAMIC_CRC_TABLE 73 | 74 | local volatile int crc_table_empty = 1; 75 | local unsigned long FAR crc_table[TBLS][256]; 76 | local void make_crc_table OF((void)); 77 | #ifdef MAKECRCH 78 | local void write_table OF((FILE *, const unsigned long FAR *)); 79 | #endif /* MAKECRCH */ 80 | /* 81 | Generate tables for a byte-wise 32-bit CRC calculation on the polynomial: 82 | x^32+x^26+x^23+x^22+x^16+x^12+x^11+x^10+x^8+x^7+x^5+x^4+x^2+x+1. 83 | 84 | Polynomials over GF(2) are represented in binary, one bit per coefficient, 85 | with the lowest powers in the most significant bit. Then adding polynomials 86 | is just exclusive-or, and multiplying a polynomial by x is a right shift by 87 | one. If we call the above polynomial p, and represent a byte as the 88 | polynomial q, also with the lowest power in the most significant bit (so the 89 | byte 0xb1 is the polynomial x^7+x^3+x+1), then the CRC is (q*x^32) mod p, 90 | where a mod b means the remainder after dividing a by b. 91 | 92 | This calculation is done using the shift-register method of multiplying and 93 | taking the remainder. The register is initialized to zero, and for each 94 | incoming bit, x^32 is added mod p to the register if the bit is a one (where 95 | x^32 mod p is p+x^32 = x^26+...+1), and the register is multiplied mod p by 96 | x (which is shifting right by one and adding x^32 mod p if the bit shifted 97 | out is a one). We start with the highest power (least significant bit) of 98 | q and repeat for all eight bits of q. 99 | 100 | The first table is simply the CRC of all possible eight bit values. This is 101 | all the information needed to generate CRCs on data a byte at a time for all 102 | combinations of CRC register values and incoming bytes. The remaining tables 103 | allow for word-at-a-time CRC calculation for both big-endian and little- 104 | endian machines, where a word is four bytes. 105 | */ 106 | local void make_crc_table() 107 | { 108 | unsigned long c; 109 | int n, k; 110 | unsigned long poly; /* polynomial exclusive-or pattern */ 111 | /* terms of polynomial defining this crc (except x^32): */ 112 | static volatile int first = 1; /* flag to limit concurrent making */ 113 | static const unsigned char p[] = {0,1,2,4,5,7,8,10,11,12,16,22,23,26}; 114 | 115 | /* See if another task is already doing this (not thread-safe, but better 116 | than nothing -- significantly reduces duration of vulnerability in 117 | case the advice about DYNAMIC_CRC_TABLE is ignored) */ 118 | if (first) { 119 | first = 0; 120 | 121 | /* make exclusive-or pattern from polynomial (0xedb88320UL) */ 122 | poly = 0UL; 123 | for (n = 0; n < sizeof(p)/sizeof(unsigned char); n++) 124 | poly |= 1UL << (31 - p[n]); 125 | 126 | /* generate a crc for every 8-bit value */ 127 | for (n = 0; n < 256; n++) { 128 | c = (unsigned long)n; 129 | for (k = 0; k < 8; k++) 130 | c = c & 1 ? poly ^ (c >> 1) : c >> 1; 131 | crc_table[0][n] = c; 132 | } 133 | 134 | #ifdef BYFOUR 135 | /* generate crc for each value followed by one, two, and three zeros, 136 | and then the byte reversal of those as well as the first table */ 137 | for (n = 0; n < 256; n++) { 138 | c = crc_table[0][n]; 139 | crc_table[4][n] = REV(c); 140 | for (k = 1; k < 4; k++) { 141 | c = crc_table[0][c & 0xff] ^ (c >> 8); 142 | crc_table[k][n] = c; 143 | crc_table[k + 4][n] = REV(c); 144 | } 145 | } 146 | #endif /* BYFOUR */ 147 | 148 | crc_table_empty = 0; 149 | } 150 | else { /* not first */ 151 | /* wait for the other guy to finish (not efficient, but rare) */ 152 | while (crc_table_empty) 153 | ; 154 | } 155 | 156 | #ifdef MAKECRCH 157 | /* write out CRC tables to crc32.h */ 158 | { 159 | FILE *out; 160 | 161 | out = fopen("crc32.h", "w"); 162 | if (out == NULL) return; 163 | fprintf(out, "/* crc32.h -- tables for rapid CRC calculation\n"); 164 | fprintf(out, " * Generated automatically by crc32.c\n */\n\n"); 165 | fprintf(out, "local const unsigned long FAR "); 166 | fprintf(out, "crc_table[TBLS][256] =\n{\n {\n"); 167 | write_table(out, crc_table[0]); 168 | # ifdef BYFOUR 169 | fprintf(out, "#ifdef BYFOUR\n"); 170 | for (k = 1; k < 8; k++) { 171 | fprintf(out, " },\n {\n"); 172 | write_table(out, crc_table[k]); 173 | } 174 | fprintf(out, "#endif\n"); 175 | # endif /* BYFOUR */ 176 | fprintf(out, " }\n};\n"); 177 | fclose(out); 178 | } 179 | #endif /* MAKECRCH */ 180 | } 181 | 182 | #ifdef MAKECRCH 183 | local void write_table(out, table) 184 | FILE *out; 185 | const unsigned long FAR *table; 186 | { 187 | int n; 188 | 189 | for (n = 0; n < 256; n++) 190 | fprintf(out, "%s0x%08lxUL%s", n % 5 ? "" : " ", table[n], 191 | n == 255 ? "\n" : (n % 5 == 4 ? ",\n" : ", ")); 192 | } 193 | #endif /* MAKECRCH */ 194 | 195 | #else /* !DYNAMIC_CRC_TABLE */ 196 | /* ======================================================================== 197 | * Tables of CRC-32s of all single-byte values, made by make_crc_table(). 198 | */ 199 | #include "crc32.h" 200 | #endif /* DYNAMIC_CRC_TABLE */ 201 | 202 | /* ========================================================================= 203 | * This function can be used by asm versions of crc32() 204 | */ 205 | const unsigned long FAR * ZEXPORT get_crc_table() 206 | { 207 | #ifdef DYNAMIC_CRC_TABLE 208 | if (crc_table_empty) 209 | make_crc_table(); 210 | #endif /* DYNAMIC_CRC_TABLE */ 211 | return (const unsigned long FAR *)crc_table; 212 | } 213 | 214 | /* ========================================================================= */ 215 | #define DO1 crc = crc_table[0][((int)crc ^ (*buf++)) & 0xff] ^ (crc >> 8) 216 | #define DO8 DO1; DO1; DO1; DO1; DO1; DO1; DO1; DO1 217 | 218 | /* ========================================================================= */ 219 | unsigned long ZEXPORT crc32(crc, buf, len) 220 | unsigned long crc; 221 | const unsigned char FAR *buf; 222 | unsigned len; 223 | { 224 | if (buf == Z_NULL) return 0UL; 225 | 226 | #ifdef DYNAMIC_CRC_TABLE 227 | if (crc_table_empty) 228 | make_crc_table(); 229 | #endif /* DYNAMIC_CRC_TABLE */ 230 | 231 | #ifdef BYFOUR 232 | if (sizeof(void *) == sizeof(ptrdiff_t)) { 233 | u4 endian; 234 | 235 | endian = 1; 236 | if (*((unsigned char *)(&endian))) 237 | return crc32_little(crc, buf, len); 238 | else 239 | return crc32_big(crc, buf, len); 240 | } 241 | #endif /* BYFOUR */ 242 | crc = crc ^ 0xffffffffUL; 243 | while (len >= 8) { 244 | DO8; 245 | len -= 8; 246 | } 247 | if (len) do { 248 | DO1; 249 | } while (--len); 250 | return crc ^ 0xffffffffUL; 251 | } 252 | 253 | #ifdef BYFOUR 254 | 255 | /* ========================================================================= */ 256 | #define DOLIT4 c ^= *buf4++; \ 257 | c = crc_table[3][c & 0xff] ^ crc_table[2][(c >> 8) & 0xff] ^ \ 258 | crc_table[1][(c >> 16) & 0xff] ^ crc_table[0][c >> 24] 259 | #define DOLIT32 DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4 260 | 261 | /* ========================================================================= */ 262 | local unsigned long crc32_little(crc, buf, len) 263 | unsigned long crc; 264 | const unsigned char FAR *buf; 265 | unsigned len; 266 | { 267 | register u4 c; 268 | register const u4 FAR *buf4; 269 | 270 | c = (u4)crc; 271 | c = ~c; 272 | while (len && ((ptrdiff_t)buf & 3)) { 273 | c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8); 274 | len--; 275 | } 276 | 277 | buf4 = (const u4 FAR *)(const void FAR *)buf; 278 | while (len >= 32) { 279 | DOLIT32; 280 | len -= 32; 281 | } 282 | while (len >= 4) { 283 | DOLIT4; 284 | len -= 4; 285 | } 286 | buf = (const unsigned char FAR *)buf4; 287 | 288 | if (len) do { 289 | c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8); 290 | } while (--len); 291 | c = ~c; 292 | return (unsigned long)c; 293 | } 294 | 295 | /* ========================================================================= */ 296 | #define DOBIG4 c ^= *++buf4; \ 297 | c = crc_table[4][c & 0xff] ^ crc_table[5][(c >> 8) & 0xff] ^ \ 298 | crc_table[6][(c >> 16) & 0xff] ^ crc_table[7][c >> 24] 299 | #define DOBIG32 DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4 300 | 301 | /* ========================================================================= */ 302 | local unsigned long crc32_big(crc, buf, len) 303 | unsigned long crc; 304 | const unsigned char FAR *buf; 305 | unsigned len; 306 | { 307 | register u4 c; 308 | register const u4 FAR *buf4; 309 | 310 | c = REV((u4)crc); 311 | c = ~c; 312 | while (len && ((ptrdiff_t)buf & 3)) { 313 | c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8); 314 | len--; 315 | } 316 | 317 | buf4 = (const u4 FAR *)(const void FAR *)buf; 318 | buf4--; 319 | while (len >= 32) { 320 | DOBIG32; 321 | len -= 32; 322 | } 323 | while (len >= 4) { 324 | DOBIG4; 325 | len -= 4; 326 | } 327 | buf4++; 328 | buf = (const unsigned char FAR *)buf4; 329 | 330 | if (len) do { 331 | c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8); 332 | } while (--len); 333 | c = ~c; 334 | return (unsigned long)(REV(c)); 335 | } 336 | 337 | #endif /* BYFOUR */ 338 | 339 | #define GF2_DIM 32 /* dimension of GF(2) vectors (length of CRC) */ 340 | 341 | /* ========================================================================= */ 342 | local unsigned long gf2_matrix_times(mat, vec) 343 | unsigned long *mat; 344 | unsigned long vec; 345 | { 346 | unsigned long sum; 347 | 348 | sum = 0; 349 | while (vec) { 350 | if (vec & 1) 351 | sum ^= *mat; 352 | vec >>= 1; 353 | mat++; 354 | } 355 | return sum; 356 | } 357 | 358 | /* ========================================================================= */ 359 | local void gf2_matrix_square(square, mat) 360 | unsigned long *square; 361 | unsigned long *mat; 362 | { 363 | int n; 364 | 365 | for (n = 0; n < GF2_DIM; n++) 366 | square[n] = gf2_matrix_times(mat, mat[n]); 367 | } 368 | 369 | /* ========================================================================= */ 370 | uLong ZEXPORT crc32_combine(crc1, crc2, len2) 371 | uLong crc1; 372 | uLong crc2; 373 | z_off_t len2; 374 | { 375 | int n; 376 | unsigned long row; 377 | unsigned long even[GF2_DIM]; /* even-power-of-two zeros operator */ 378 | unsigned long odd[GF2_DIM]; /* odd-power-of-two zeros operator */ 379 | 380 | /* degenerate case */ 381 | if (len2 == 0) 382 | return crc1; 383 | 384 | /* put operator for one zero bit in odd */ 385 | odd[0] = 0xedb88320L; /* CRC-32 polynomial */ 386 | row = 1; 387 | for (n = 1; n < GF2_DIM; n++) { 388 | odd[n] = row; 389 | row <<= 1; 390 | } 391 | 392 | /* put operator for two zero bits in even */ 393 | gf2_matrix_square(even, odd); 394 | 395 | /* put operator for four zero bits in odd */ 396 | gf2_matrix_square(odd, even); 397 | 398 | /* apply len2 zeros to crc1 (first square will put the operator for one 399 | zero byte, eight zero bits, in even) */ 400 | do { 401 | /* apply zeros operator for this bit of len2 */ 402 | gf2_matrix_square(even, odd); 403 | if (len2 & 1) 404 | crc1 = gf2_matrix_times(even, crc1); 405 | len2 >>= 1; 406 | 407 | /* if no more bits set, then done */ 408 | if (len2 == 0) 409 | break; 410 | 411 | /* another iteration of the loop with odd and even swapped */ 412 | gf2_matrix_square(odd, even); 413 | if (len2 & 1) 414 | crc1 = gf2_matrix_times(odd, crc1); 415 | len2 >>= 1; 416 | 417 | /* if no more bits set, then done */ 418 | } while (len2 != 0); 419 | 420 | /* return combined crc */ 421 | crc1 ^= crc2; 422 | return crc1; 423 | } 424 | -------------------------------------------------------------------------------- /zlib/inftrees.c: -------------------------------------------------------------------------------- 1 | /* inftrees.c -- generate Huffman trees for efficient decoding 2 | * Copyright (C) 1995-2005 Mark Adler 3 | * For conditions of distribution and use, see copyright notice in zlib.h 4 | */ 5 | 6 | #include "zutil.h" 7 | #include "inftrees.h" 8 | 9 | #define MAXBITS 15 10 | 11 | const char inflate_copyright[] = 12 | " inflate 1.2.3 Copyright 1995-2005 Mark Adler "; 13 | /* 14 | If you use the zlib library in a product, an acknowledgment is welcome 15 | in the documentation of your product. If for some reason you cannot 16 | include such an acknowledgment, I would appreciate that you keep this 17 | copyright string in the executable of your product. 18 | */ 19 | 20 | /* 21 | Build a set of tables to decode the provided canonical Huffman code. 22 | The code lengths are lens[0..codes-1]. The result starts at *table, 23 | whose indices are 0..2^bits-1. work is a writable array of at least 24 | lens shorts, which is used as a work area. type is the type of code 25 | to be generated, CODES, LENS, or DISTS. On return, zero is success, 26 | -1 is an invalid code, and +1 means that ENOUGH isn't enough. table 27 | on return points to the next available entry's address. bits is the 28 | requested root table index bits, and on return it is the actual root 29 | table index bits. It will differ if the request is greater than the 30 | longest code or if it is less than the shortest code. 31 | */ 32 | int inflate_table(type, lens, codes, table, bits, work) 33 | codetype type; 34 | unsigned short FAR *lens; 35 | unsigned codes; 36 | code FAR * FAR *table; 37 | unsigned FAR *bits; 38 | unsigned short FAR *work; 39 | { 40 | unsigned len; /* a code's length in bits */ 41 | unsigned sym; /* index of code symbols */ 42 | unsigned min, max; /* minimum and maximum code lengths */ 43 | unsigned root; /* number of index bits for root table */ 44 | unsigned curr; /* number of index bits for current table */ 45 | unsigned drop; /* code bits to drop for sub-table */ 46 | int left; /* number of prefix codes available */ 47 | unsigned used; /* code entries in table used */ 48 | unsigned huff; /* Huffman code */ 49 | unsigned incr; /* for incrementing code, index */ 50 | unsigned fill; /* index for replicating entries */ 51 | unsigned low; /* low bits for current root entry */ 52 | unsigned mask; /* mask for low root bits */ 53 | code this; /* table entry for duplication */ 54 | code FAR *next; /* next available space in table */ 55 | const unsigned short FAR *base; /* base value table to use */ 56 | const unsigned short FAR *extra; /* extra bits table to use */ 57 | int end; /* use base and extra for symbol > end */ 58 | unsigned short count[MAXBITS+1]; /* number of codes of each length */ 59 | unsigned short offs[MAXBITS+1]; /* offsets in table for each length */ 60 | static const unsigned short lbase[31] = { /* Length codes 257..285 base */ 61 | 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 62 | 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0}; 63 | static const unsigned short lext[31] = { /* Length codes 257..285 extra */ 64 | 16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18, 18, 65 | 19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 16, 201, 196}; 66 | static const unsigned short dbase[32] = { /* Distance codes 0..29 base */ 67 | 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 68 | 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 69 | 8193, 12289, 16385, 24577, 0, 0}; 70 | static const unsigned short dext[32] = { /* Distance codes 0..29 extra */ 71 | 16, 16, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 72 | 23, 23, 24, 24, 25, 25, 26, 26, 27, 27, 73 | 28, 28, 29, 29, 64, 64}; 74 | 75 | /* 76 | Process a set of code lengths to create a canonical Huffman code. The 77 | code lengths are lens[0..codes-1]. Each length corresponds to the 78 | symbols 0..codes-1. The Huffman code is generated by first sorting the 79 | symbols by length from short to long, and retaining the symbol order 80 | for codes with equal lengths. Then the code starts with all zero bits 81 | for the first code of the shortest length, and the codes are integer 82 | increments for the same length, and zeros are appended as the length 83 | increases. For the deflate format, these bits are stored backwards 84 | from their more natural integer increment ordering, and so when the 85 | decoding tables are built in the large loop below, the integer codes 86 | are incremented backwards. 87 | 88 | This routine assumes, but does not check, that all of the entries in 89 | lens[] are in the range 0..MAXBITS. The caller must assure this. 90 | 1..MAXBITS is interpreted as that code length. zero means that that 91 | symbol does not occur in this code. 92 | 93 | The codes are sorted by computing a count of codes for each length, 94 | creating from that a table of starting indices for each length in the 95 | sorted table, and then entering the symbols in order in the sorted 96 | table. The sorted table is work[], with that space being provided by 97 | the caller. 98 | 99 | The length counts are used for other purposes as well, i.e. finding 100 | the minimum and maximum length codes, determining if there are any 101 | codes at all, checking for a valid set of lengths, and looking ahead 102 | at length counts to determine sub-table sizes when building the 103 | decoding tables. 104 | */ 105 | 106 | /* accumulate lengths for codes (assumes lens[] all in 0..MAXBITS) */ 107 | for (len = 0; len <= MAXBITS; len++) 108 | count[len] = 0; 109 | for (sym = 0; sym < codes; sym++) 110 | count[lens[sym]]++; 111 | 112 | /* bound code lengths, force root to be within code lengths */ 113 | root = *bits; 114 | for (max = MAXBITS; max >= 1; max--) 115 | if (count[max] != 0) break; 116 | if (root > max) root = max; 117 | if (max == 0) { /* no symbols to code at all */ 118 | this.op = (unsigned char)64; /* invalid code marker */ 119 | this.bits = (unsigned char)1; 120 | this.val = (unsigned short)0; 121 | *(*table)++ = this; /* make a table to force an error */ 122 | *(*table)++ = this; 123 | *bits = 1; 124 | return 0; /* no symbols, but wait for decoding to report error */ 125 | } 126 | for (min = 1; min <= MAXBITS; min++) 127 | if (count[min] != 0) break; 128 | if (root < min) root = min; 129 | 130 | /* check for an over-subscribed or incomplete set of lengths */ 131 | left = 1; 132 | for (len = 1; len <= MAXBITS; len++) { 133 | left <<= 1; 134 | left -= count[len]; 135 | if (left < 0) return -1; /* over-subscribed */ 136 | } 137 | if (left > 0 && (type == CODES || max != 1)) 138 | return -1; /* incomplete set */ 139 | 140 | /* generate offsets into symbol table for each length for sorting */ 141 | offs[1] = 0; 142 | for (len = 1; len < MAXBITS; len++) 143 | offs[len + 1] = offs[len] + count[len]; 144 | 145 | /* sort symbols by length, by symbol order within each length */ 146 | for (sym = 0; sym < codes; sym++) 147 | if (lens[sym] != 0) work[offs[lens[sym]]++] = (unsigned short)sym; 148 | 149 | /* 150 | Create and fill in decoding tables. In this loop, the table being 151 | filled is at next and has curr index bits. The code being used is huff 152 | with length len. That code is converted to an index by dropping drop 153 | bits off of the bottom. For codes where len is less than drop + curr, 154 | those top drop + curr - len bits are incremented through all values to 155 | fill the table with replicated entries. 156 | 157 | root is the number of index bits for the root table. When len exceeds 158 | root, sub-tables are created pointed to by the root entry with an index 159 | of the low root bits of huff. This is saved in low to check for when a 160 | new sub-table should be started. drop is zero when the root table is 161 | being filled, and drop is root when sub-tables are being filled. 162 | 163 | When a new sub-table is needed, it is necessary to look ahead in the 164 | code lengths to determine what size sub-table is needed. The length 165 | counts are used for this, and so count[] is decremented as codes are 166 | entered in the tables. 167 | 168 | used keeps track of how many table entries have been allocated from the 169 | provided *table space. It is checked when a LENS table is being made 170 | against the space in *table, ENOUGH, minus the maximum space needed by 171 | the worst case distance code, MAXD. This should never happen, but the 172 | sufficiency of ENOUGH has not been proven exhaustively, hence the check. 173 | This assumes that when type == LENS, bits == 9. 174 | 175 | sym increments through all symbols, and the loop terminates when 176 | all codes of length max, i.e. all codes, have been processed. This 177 | routine permits incomplete codes, so another loop after this one fills 178 | in the rest of the decoding tables with invalid code markers. 179 | */ 180 | 181 | /* set up for code type */ 182 | switch (type) { 183 | case CODES: 184 | base = extra = work; /* dummy value--not used */ 185 | end = 19; 186 | break; 187 | case LENS: 188 | base = lbase; 189 | base -= 257; 190 | extra = lext; 191 | extra -= 257; 192 | end = 256; 193 | break; 194 | default: /* DISTS */ 195 | base = dbase; 196 | extra = dext; 197 | end = -1; 198 | } 199 | 200 | /* initialize state for loop */ 201 | huff = 0; /* starting code */ 202 | sym = 0; /* starting code symbol */ 203 | len = min; /* starting code length */ 204 | next = *table; /* current table to fill in */ 205 | curr = root; /* current table index bits */ 206 | drop = 0; /* current bits to drop from code for index */ 207 | low = (unsigned)(-1); /* trigger new sub-table when len > root */ 208 | used = 1U << root; /* use root table entries */ 209 | mask = used - 1; /* mask for comparing low */ 210 | 211 | /* check available table space */ 212 | if (type == LENS && used >= ENOUGH - MAXD) 213 | return 1; 214 | 215 | /* process all codes and make table entries */ 216 | for (;;) { 217 | /* create table entry */ 218 | this.bits = (unsigned char)(len - drop); 219 | if ((int)(work[sym]) < end) { 220 | this.op = (unsigned char)0; 221 | this.val = work[sym]; 222 | } 223 | else if ((int)(work[sym]) > end) { 224 | this.op = (unsigned char)(extra[work[sym]]); 225 | this.val = base[work[sym]]; 226 | } 227 | else { 228 | this.op = (unsigned char)(32 + 64); /* end of block */ 229 | this.val = 0; 230 | } 231 | 232 | /* replicate for those indices with low len bits equal to huff */ 233 | incr = 1U << (len - drop); 234 | fill = 1U << curr; 235 | min = fill; /* save offset to next table */ 236 | do { 237 | fill -= incr; 238 | next[(huff >> drop) + fill] = this; 239 | } while (fill != 0); 240 | 241 | /* backwards increment the len-bit code huff */ 242 | incr = 1U << (len - 1); 243 | while (huff & incr) 244 | incr >>= 1; 245 | if (incr != 0) { 246 | huff &= incr - 1; 247 | huff += incr; 248 | } 249 | else 250 | huff = 0; 251 | 252 | /* go to next symbol, update count, len */ 253 | sym++; 254 | if (--(count[len]) == 0) { 255 | if (len == max) break; 256 | len = lens[work[sym]]; 257 | } 258 | 259 | /* create new sub-table if needed */ 260 | if (len > root && (huff & mask) != low) { 261 | /* if first time, transition to sub-tables */ 262 | if (drop == 0) 263 | drop = root; 264 | 265 | /* increment past last table */ 266 | next += min; /* here min is 1 << curr */ 267 | 268 | /* determine length of next table */ 269 | curr = len - drop; 270 | left = (int)(1 << curr); 271 | while (curr + drop < max) { 272 | left -= count[curr + drop]; 273 | if (left <= 0) break; 274 | curr++; 275 | left <<= 1; 276 | } 277 | 278 | /* check for enough space */ 279 | used += 1U << curr; 280 | if (type == LENS && used >= ENOUGH - MAXD) 281 | return 1; 282 | 283 | /* point entry in root table to sub-table */ 284 | low = huff & mask; 285 | (*table)[low].op = (unsigned char)curr; 286 | (*table)[low].bits = (unsigned char)root; 287 | (*table)[low].val = (unsigned short)(next - *table); 288 | } 289 | } 290 | 291 | /* 292 | Fill in rest of table for incomplete codes. This loop is similar to the 293 | loop above in incrementing huff for table indices. It is assumed that 294 | len is equal to curr + drop, so there is no loop needed to increment 295 | through high index bits. When the current sub-table is filled, the loop 296 | drops back to the root table to fill in any remaining entries there. 297 | */ 298 | this.op = (unsigned char)64; /* invalid code marker */ 299 | this.bits = (unsigned char)(len - drop); 300 | this.val = (unsigned short)0; 301 | while (huff != 0) { 302 | /* when done with sub-table, drop back to root table */ 303 | if (drop != 0 && (huff & mask) != low) { 304 | drop = 0; 305 | len = root; 306 | next = *table; 307 | this.bits = (unsigned char)len; 308 | } 309 | 310 | /* put invalid code marker in table */ 311 | next[huff >> drop] = this; 312 | 313 | /* backwards increment the len-bit code huff */ 314 | incr = 1U << (len - 1); 315 | while (huff & incr) 316 | incr >>= 1; 317 | if (incr != 0) { 318 | huff &= incr - 1; 319 | huff += incr; 320 | } 321 | else 322 | huff = 0; 323 | } 324 | 325 | /* set return parameters */ 326 | *table += used; 327 | *bits = root; 328 | return 0; 329 | } 330 | --------------------------------------------------------------------------------