mirror of
https://github.com/Proxmark/proxmark3.git
synced 2025-07-16 02:03:00 -07:00
Merge pull request #109 from pwpiwi/image_shrink
Compress FPGA configs and initialized data
This commit is contained in:
commit
2da2e92837
35 changed files with 12052 additions and 281 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -12,6 +12,7 @@
|
||||||
*.bin
|
*.bin
|
||||||
*.dll
|
*.dll
|
||||||
*.moc.cpp
|
*.moc.cpp
|
||||||
|
*.z
|
||||||
*.exe
|
*.exe
|
||||||
proxmark
|
proxmark
|
||||||
proxmark3
|
proxmark3
|
||||||
|
|
19
Makefile
19
Makefile
|
@ -1,9 +1,8 @@
|
||||||
include common/Makefile.common
|
include common/Makefile.common
|
||||||
|
|
||||||
GZIP=gzip
|
|
||||||
FLASH_PORT=/dev/ttyACM0
|
FLASH_PORT=/dev/ttyACM0
|
||||||
|
|
||||||
all clean: %: bootrom/% armsrc/% client/% recovery/%
|
all clean: %: client/% bootrom/% armsrc/% recovery/%
|
||||||
|
|
||||||
bootrom/%: FORCE
|
bootrom/%: FORCE
|
||||||
$(MAKE) -C bootrom $(patsubst bootrom/%,%,$@)
|
$(MAKE) -C bootrom $(patsubst bootrom/%,%,$@)
|
||||||
|
@ -15,8 +14,8 @@ recovery/%: FORCE
|
||||||
$(MAKE) -C recovery $(patsubst recovery/%,%,$@)
|
$(MAKE) -C recovery $(patsubst recovery/%,%,$@)
|
||||||
FORCE: # Dummy target to force remake in the subdirectories, even if files exist (this Makefile doesn't know about the prerequisites)
|
FORCE: # Dummy target to force remake in the subdirectories, even if files exist (this Makefile doesn't know about the prerequisites)
|
||||||
|
|
||||||
|
.PHONY: all clean help _test flash-bootrom flash-os flash-all FORCE
|
||||||
|
|
||||||
.PHONY: all clean help _test flash-bootrom flash-os flash-fpga flash-both flash-all FORCE
|
|
||||||
help:
|
help:
|
||||||
@echo Multi-OS Makefile, you are running on $(DETECTED_OS)
|
@echo Multi-OS Makefile, you are running on $(DETECTED_OS)
|
||||||
@echo Possible targets:
|
@echo Possible targets:
|
||||||
|
@ -24,9 +23,7 @@ help:
|
||||||
@echo + client - Make only the OS-specific host directory
|
@echo + client - Make only the OS-specific host directory
|
||||||
@echo + flash-bootrom - Make bootrom and flash it
|
@echo + flash-bootrom - Make bootrom and flash it
|
||||||
@echo + flash-os - Make armsrc and flash os (includes fpga)
|
@echo + flash-os - Make armsrc and flash os (includes fpga)
|
||||||
@echo + flash-fpga - (Deprecated:) Make armsrc and flash fpga
|
@echo + flash-all - Make bootrom and armsrc and flash bootrom and os image
|
||||||
@echo + flash-both - Make armsrc and flash os and fpga image
|
|
||||||
@echo + flash-all - Make bootrom and armsrc and flash bootrom, os and fpga image
|
|
||||||
@echo + clean - Clean in bootrom, armsrc and the OS-specific host directory
|
@echo + clean - Clean in bootrom, armsrc and the OS-specific host directory
|
||||||
|
|
||||||
client: client/all
|
client: client/all
|
||||||
|
@ -34,16 +31,10 @@ client: client/all
|
||||||
flash-bootrom: bootrom/obj/bootrom.elf $(FLASH_TOOL)
|
flash-bootrom: bootrom/obj/bootrom.elf $(FLASH_TOOL)
|
||||||
$(FLASH_TOOL) $(FLASH_PORT) -b $(subst /,$(PATHSEP),$<)
|
$(FLASH_TOOL) $(FLASH_PORT) -b $(subst /,$(PATHSEP),$<)
|
||||||
|
|
||||||
flash-os: armsrc/obj/osimage.elf $(FLASH_TOOL)
|
flash-os: armsrc/obj/fullimage.elf $(FLASH_TOOL)
|
||||||
$(FLASH_TOOL) $(FLASH_PORT) $(subst /,$(PATHSEP),$<)
|
$(FLASH_TOOL) $(FLASH_PORT) $(subst /,$(PATHSEP),$<)
|
||||||
|
|
||||||
#flash-fpga: armsrc/obj/fpgaimage.elf $(FLASH_TOOL)
|
flash-all: bootrom/obj/bootrom.elf armsrc/obj/fullimage.elf $(FLASH_TOOL)
|
||||||
# $(FLASH_TOOL) $(FLASH_PORT) $(subst /,$(PATHSEP),$<)
|
|
||||||
|
|
||||||
flash-both: armsrc/obj/osimage.elf $(FLASH_TOOL)
|
|
||||||
$(FLASH_TOOL) $(FLASH_PORT) $(subst /,$(PATHSEP),$(filter-out $(FLASH_TOOL),$^))
|
|
||||||
|
|
||||||
flash-all: bootrom/obj/bootrom.elf armsrc/obj/osimage.elf $(FLASH_TOOL)
|
|
||||||
$(FLASH_TOOL) $(FLASH_PORT) -b $(subst /,$(PATHSEP),$(filter-out $(FLASH_TOOL),$^))
|
$(FLASH_TOOL) $(FLASH_PORT) -b $(subst /,$(PATHSEP),$(filter-out $(FLASH_TOOL),$^))
|
||||||
|
|
||||||
newtarbin:
|
newtarbin:
|
||||||
|
|
|
@ -10,7 +10,8 @@ APP_INCLUDES = apps.h
|
||||||
|
|
||||||
#remove one of the following defines and comment out the relevant line
|
#remove one of the following defines and comment out the relevant line
|
||||||
#in the next section to remove that particular feature from compilation
|
#in the next section to remove that particular feature from compilation
|
||||||
APP_CFLAGS = -DWITH_LF -DWITH_ISO15693 -DWITH_ISO14443a -DWITH_ISO14443b -DWITH_ICLASS -DWITH_LEGICRF -DWITH_HITAG -DWITH_CRC -DON_DEVICE -fno-strict-aliasing -ffunction-sections -fdata-sections
|
APP_CFLAGS = -DWITH_LF -DWITH_ISO15693 -DWITH_ISO14443a -DWITH_ISO14443b -DWITH_ICLASS -DWITH_LEGICRF -DWITH_HITAG -DWITH_CRC -DON_DEVICE \
|
||||||
|
-fno-strict-aliasing -ffunction-sections -fdata-sections
|
||||||
#-DWITH_LCD
|
#-DWITH_LCD
|
||||||
|
|
||||||
#SRC_LCD = fonts.c LCD.c
|
#SRC_LCD = fonts.c LCD.c
|
||||||
|
@ -19,12 +20,28 @@ SRC_ISO15693 = iso15693.c iso15693tools.c
|
||||||
SRC_ISO14443a = epa.c iso14443a.c mifareutil.c mifarecmd.c mifaresniff.c
|
SRC_ISO14443a = epa.c iso14443a.c mifareutil.c mifarecmd.c mifaresniff.c
|
||||||
SRC_ISO14443b = iso14443b.c
|
SRC_ISO14443b = iso14443b.c
|
||||||
SRC_CRAPTO1 = crapto1.c crypto1.c des.c aes.c
|
SRC_CRAPTO1 = crapto1.c crypto1.c des.c aes.c
|
||||||
SRC_CRC = iso14443crc.c crc.c crc16.c crc32.c
|
SRC_CRC = iso14443crc.c crc.c crc16.c crc32.c
|
||||||
|
|
||||||
|
#the FPGA bitstream files. Note: order matters!
|
||||||
|
FPGA_BITSTREAMS = fpga_lf.bit fpga_hf.bit
|
||||||
|
|
||||||
|
#the zlib source files required for decompressing the fpga config at run time
|
||||||
|
SRC_ZLIB = inflate.c inffast.c inftrees.c adler32.c zutil.c
|
||||||
|
#additional defines required to compile zlib
|
||||||
|
ZLIB_CFLAGS = -DZ_SOLO -DZ_PREFIX -DNO_GZIP -DZLIB_PM3_TUNED
|
||||||
|
APP_CFLAGS += $(ZLIB_CFLAGS)
|
||||||
|
# zlib includes:
|
||||||
|
APP_CFLAGS += -I../zlib
|
||||||
|
|
||||||
|
# stdint.h provided locally until GCC 4.5 becomes C99 compliant
|
||||||
|
APP_CFLAGS += -I.
|
||||||
|
|
||||||
|
# Compile these in thumb mode (small size)
|
||||||
THUMBSRC = start.c \
|
THUMBSRC = start.c \
|
||||||
$(SRC_LCD) \
|
$(SRC_LCD) \
|
||||||
$(SRC_ISO15693) \
|
$(SRC_ISO15693) \
|
||||||
$(SRC_LF) \
|
$(SRC_LF) \
|
||||||
|
$(SRC_ZLIB) \
|
||||||
appmain.c \
|
appmain.c \
|
||||||
printf.c \
|
printf.c \
|
||||||
util.c \
|
util.c \
|
||||||
|
@ -45,50 +62,61 @@ ARMSRC = fpgaloader.c \
|
||||||
BigBuf.c \
|
BigBuf.c \
|
||||||
optimized_cipher.c
|
optimized_cipher.c
|
||||||
|
|
||||||
# stdint.h provided locally until GCC 4.5 becomes C99 compliant
|
|
||||||
APP_CFLAGS += -I.
|
|
||||||
|
|
||||||
# Do not move this inclusion before the definition of {THUMB,ASM,ARM}SRC
|
# Do not move this inclusion before the definition of {THUMB,ASM,ARM}SRC
|
||||||
include ../common/Makefile.common
|
include ../common/Makefile.common
|
||||||
|
|
||||||
OBJS = $(OBJDIR)/osimage.s19
|
OBJS = $(OBJDIR)/fullimage.s19
|
||||||
#$(OBJDIR)/fpgaimage.s19
|
FPGA_COMPRESSOR = ../client/fpga_compress
|
||||||
|
|
||||||
all: $(OBJS)
|
all: $(OBJS)
|
||||||
|
|
||||||
$(OBJDIR)/fpga_lf.o: fpga_lf.bit
|
$(OBJDIR)/fpga_all.o: $(OBJDIR)/fpga_all.bit.z
|
||||||
$(OBJCOPY) -O elf32-littlearm -I binary -B arm --redefine-sym _binary____fpga_fpga_lf_bit_start=_binary_fpga_lf_bit_start --redefine-sym _binary____fpga_fpga_lf_bit_end=_binary_fpga_lf_bit_end --prefix-sections=fpga_lf_bit $^ $@
|
$(OBJCOPY) -O elf32-littlearm -I binary -B arm --prefix-sections=fpga_all_bit $^ $@
|
||||||
|
|
||||||
$(OBJDIR)/fpga_hf.o: fpga_hf.bit
|
$(OBJDIR)/fpga_all.bit.z: $(FPGA_BITSTREAMS) $(FPGA_COMPRESSOR)
|
||||||
$(OBJCOPY) -O elf32-littlearm -I binary -B arm --redefine-sym _binary____fpga_fpga_hf_bit_start=_binary_fpga_hf_bit_start --redefine-sym _binary____fpga_fpga_hf_bit_end=_binary_fpga_hf_bit_end --prefix-sections=fpga_hf_bit $^ $@
|
$(FPGA_COMPRESSOR) $(filter %.bit,$^) $@
|
||||||
|
|
||||||
$(OBJDIR)/fullimage.elf: $(VERSIONOBJ) $(OBJDIR)/fpga_lf.o $(OBJDIR)/fpga_hf.o $(THUMBOBJ) $(ARMOBJ)
|
$(FPGA_COMPRESSOR):
|
||||||
|
make -C ../client $(notdir $(FPGA_COMPRESSOR))
|
||||||
|
|
||||||
|
$(OBJDIR)/fullimage.stage1.elf: $(VERSIONOBJ) $(OBJDIR)/fpga_all.o $(THUMBOBJ) $(ARMOBJ)
|
||||||
$(CC) $(LDFLAGS) -Wl,-T,ldscript,-Map,$(patsubst %.elf,%.map,$@) -o $@ $^ $(LIBS)
|
$(CC) $(LDFLAGS) -Wl,-T,ldscript,-Map,$(patsubst %.elf,%.map,$@) -o $@ $^ $(LIBS)
|
||||||
|
|
||||||
#$(OBJDIR)/fpgaimage.elf: $(OBJDIR)/fullimage.elf
|
$(OBJDIR)/fullimage.nodata.bin: $(OBJDIR)/fullimage.stage1.elf
|
||||||
# $(OBJCOPY) -F elf32-littlearm --only-section .fpgaimage $^ $@
|
$(OBJCOPY) -O binary -I elf32-littlearm --remove-section .data $^ $@
|
||||||
|
|
||||||
|
$(OBJDIR)/fullimage.nodata.o: $(OBJDIR)/fullimage.nodata.bin
|
||||||
|
$(OBJCOPY) -O elf32-littlearm -I binary -B arm --rename-section .data=stage1_image $^ $@
|
||||||
|
|
||||||
$(OBJDIR)/osimage.elf: $(OBJDIR)/fullimage.elf
|
$(OBJDIR)/fullimage.data.bin: $(OBJDIR)/fullimage.stage1.elf
|
||||||
$(OBJCOPY) -F elf32-littlearm $^ $@
|
$(OBJCOPY) -O binary -I elf32-littlearm --only-section .data $^ $@
|
||||||
|
|
||||||
|
$(OBJDIR)/fullimage.data.bin.z: $(OBJDIR)/fullimage.data.bin $(FPGA_COMPRESSOR)
|
||||||
|
$(FPGA_COMPRESSOR) $(filter %.bin,$^) $@
|
||||||
|
|
||||||
|
$(OBJDIR)/fullimage.data.o: $(OBJDIR)/fullimage.data.bin.z
|
||||||
|
$(OBJCOPY) -O elf32-littlearm -I binary -B arm --rename-section .data=compressed_data $^ $@
|
||||||
|
|
||||||
|
$(OBJDIR)/fullimage.elf: $(OBJDIR)/fullimage.nodata.o $(OBJDIR)/fullimage.data.o
|
||||||
|
$(CC) $(LDFLAGS) -Wl,-T,ldscript,-Map,$(patsubst %.elf,%.map,$@) -o $@ $^
|
||||||
|
|
||||||
tarbin: $(OBJS)
|
tarbin: $(OBJS)
|
||||||
$(TAR) $(TARFLAGS) ../proxmark3-$(platform)-bin.tar $(OBJS:%=armsrc/%) $(OBJS:%.s19=armsrc/%.elf)
|
$(TAR) $(TARFLAGS) ../proxmark3-$(platform)-bin.tar $(OBJS:%=armsrc/%) $(OBJS:%.s19=armsrc/%.elf)
|
||||||
|
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
$(DELETE) $(OBJDIR)$(PATHSEP)*.o
|
$(DELETE) $(OBJDIR)$(PATHSEP)*.o
|
||||||
$(DELETE) $(OBJDIR)$(PATHSEP)*.elf
|
$(DELETE) $(OBJDIR)$(PATHSEP)*.elf
|
||||||
$(DELETE) $(OBJDIR)$(PATHSEP)*.s19
|
$(DELETE) $(OBJDIR)$(PATHSEP)*.s19
|
||||||
$(DELETE) $(OBJDIR)$(PATHSEP)*.map
|
$(DELETE) $(OBJDIR)$(PATHSEP)*.map
|
||||||
$(DELETE) $(OBJDIR)$(PATHSEP)*.d
|
$(DELETE) $(OBJDIR)$(PATHSEP)*.d
|
||||||
|
$(DELETE) $(OBJDIR)$(PATHSEP)*.z
|
||||||
|
$(DELETE) $(OBJDIR)$(PATHSEP)*.bin
|
||||||
$(DELETE) version.c
|
$(DELETE) version.c
|
||||||
|
|
||||||
.PHONY: all clean help
|
.PHONY: all clean help
|
||||||
help:
|
help:
|
||||||
@echo Multi-OS Makefile, you are running on $(DETECTED_OS)
|
@echo Multi-OS Makefile, you are running on $(DETECTED_OS)
|
||||||
@echo Possible targets:
|
@echo Possible targets:
|
||||||
@echo + all - Make both:
|
@echo + all - Build the full image $(OBJDIR)/fullimage.s19
|
||||||
@echo + $(OBJDIR)/osimage.s19 - The OS image
|
@echo + clean - Clean $(OBJDIR)
|
||||||
@echo + $(OBJDIR)/fpgaimage.s19 - The FPGA image
|
|
||||||
@echo + clean - Clean $(OBJDIR)
|
|
||||||
|
|
||||||
|
|
|
@ -261,11 +261,11 @@ void ReadMem(int addr)
|
||||||
/* osimage version information is linked in */
|
/* osimage version information is linked in */
|
||||||
extern struct version_information version_information;
|
extern struct version_information version_information;
|
||||||
/* bootrom version information is pointed to from _bootphase1_version_pointer */
|
/* bootrom version information is pointed to from _bootphase1_version_pointer */
|
||||||
extern char *_bootphase1_version_pointer, _flash_start, _flash_end;
|
extern char *_bootphase1_version_pointer, _flash_start, _flash_end, _bootrom_start, _bootrom_end, __data_src_start__;
|
||||||
void SendVersion(void)
|
void SendVersion(void)
|
||||||
{
|
{
|
||||||
char temp[512]; /* Limited data payload in USB packets */
|
char temp[USB_CMD_DATA_SIZE]; /* Limited data payload in USB packets */
|
||||||
DbpString("Prox/RFID mark3 RFID instrument");
|
char VersionString[USB_CMD_DATA_SIZE] = { '\0' };
|
||||||
|
|
||||||
/* Try to find the bootrom version information. Expect to find a pointer at
|
/* Try to find the bootrom version information. Expect to find a pointer at
|
||||||
* symbol _bootphase1_version_pointer, perform slight sanity checks on the
|
* symbol _bootphase1_version_pointer, perform slight sanity checks on the
|
||||||
|
@ -273,19 +273,24 @@ void SendVersion(void)
|
||||||
*/
|
*/
|
||||||
char *bootrom_version = *(char**)&_bootphase1_version_pointer;
|
char *bootrom_version = *(char**)&_bootphase1_version_pointer;
|
||||||
if( bootrom_version < &_flash_start || bootrom_version >= &_flash_end ) {
|
if( bootrom_version < &_flash_start || bootrom_version >= &_flash_end ) {
|
||||||
DbpString("bootrom version information appears invalid");
|
strcat(VersionString, "bootrom version information appears invalid\n");
|
||||||
} else {
|
} else {
|
||||||
FormatVersionInformation(temp, sizeof(temp), "bootrom: ", bootrom_version);
|
FormatVersionInformation(temp, sizeof(temp), "bootrom: ", bootrom_version);
|
||||||
DbpString(temp);
|
strncat(VersionString, temp, sizeof(VersionString) - strlen(VersionString) - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
FormatVersionInformation(temp, sizeof(temp), "os: ", &version_information);
|
FormatVersionInformation(temp, sizeof(temp), "os: ", &version_information);
|
||||||
DbpString(temp);
|
strncat(VersionString, temp, sizeof(VersionString) - strlen(VersionString) - 1);
|
||||||
|
|
||||||
FpgaGatherVersion(temp, sizeof(temp));
|
FpgaGatherVersion(FPGA_BITSTREAM_LF, temp, sizeof(temp));
|
||||||
DbpString(temp);
|
strncat(VersionString, temp, sizeof(VersionString) - strlen(VersionString) - 1);
|
||||||
// Send Chip ID
|
FpgaGatherVersion(FPGA_BITSTREAM_HF, temp, sizeof(temp));
|
||||||
cmd_send(CMD_ACK,*(AT91C_DBGU_CIDR),0,0,NULL,0);
|
strncat(VersionString, temp, sizeof(VersionString) - strlen(VersionString) - 1);
|
||||||
|
|
||||||
|
// Send Chip ID and used flash memory
|
||||||
|
uint32_t text_and_rodata_section_size = (uint32_t)&__data_src_start__ - (uint32_t)&_flash_start;
|
||||||
|
uint32_t compressed_data_section_size = common_area.arg1;
|
||||||
|
cmd_send(CMD_ACK, *(AT91C_DBGU_CIDR), text_and_rodata_section_size + compressed_data_section_size, 0, VersionString, strlen(VersionString));
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef WITH_LF
|
#ifdef WITH_LF
|
||||||
|
|
|
@ -19,6 +19,7 @@
|
||||||
#include "mifare.h"
|
#include "mifare.h"
|
||||||
#include "../common/crc32.h"
|
#include "../common/crc32.h"
|
||||||
#include "BigBuf.h"
|
#include "BigBuf.h"
|
||||||
|
#include "fpgaloader.h"
|
||||||
|
|
||||||
extern const uint8_t OddByteParity[256];
|
extern const uint8_t OddByteParity[256];
|
||||||
extern int rsamples; // = 0;
|
extern int rsamples; // = 0;
|
||||||
|
@ -50,60 +51,6 @@ void ListenReaderField(int limit);
|
||||||
extern int ToSendMax;
|
extern int ToSendMax;
|
||||||
extern uint8_t ToSend[];
|
extern uint8_t ToSend[];
|
||||||
|
|
||||||
/// fpga.h
|
|
||||||
void FpgaSendCommand(uint16_t cmd, uint16_t v);
|
|
||||||
void FpgaWriteConfWord(uint8_t v);
|
|
||||||
void FpgaDownloadAndGo(int bitstream_version);
|
|
||||||
int FpgaGatherBitstreamVersion();
|
|
||||||
void FpgaGatherVersion(char *dst, int len);
|
|
||||||
void FpgaSetupSsc(void);
|
|
||||||
void SetupSpi(int mode);
|
|
||||||
bool FpgaSetupSscDma(uint8_t *buf, int len);
|
|
||||||
#define FpgaDisableSscDma(void) AT91C_BASE_PDC_SSC->PDC_PTCR = AT91C_PDC_RXTDIS;
|
|
||||||
#define FpgaEnableSscDma(void) AT91C_BASE_PDC_SSC->PDC_PTCR = AT91C_PDC_RXTEN;
|
|
||||||
void SetAdcMuxFor(uint32_t whichGpio);
|
|
||||||
|
|
||||||
// Definitions for the FPGA commands.
|
|
||||||
#define FPGA_CMD_SET_CONFREG (1<<12)
|
|
||||||
#define FPGA_CMD_SET_DIVISOR (2<<12)
|
|
||||||
#define FPGA_CMD_SET_USER_BYTE1 (3<<12)
|
|
||||||
// Definitions for the FPGA configuration word.
|
|
||||||
// LF
|
|
||||||
#define FPGA_MAJOR_MODE_LF_ADC (0<<5)
|
|
||||||
#define FPGA_MAJOR_MODE_LF_EDGE_DETECT (1<<5)
|
|
||||||
#define FPGA_MAJOR_MODE_LF_PASSTHRU (2<<5)
|
|
||||||
// HF
|
|
||||||
#define FPGA_MAJOR_MODE_HF_READER_TX (0<<5)
|
|
||||||
#define FPGA_MAJOR_MODE_HF_READER_RX_XCORR (1<<5)
|
|
||||||
#define FPGA_MAJOR_MODE_HF_SIMULATOR (2<<5)
|
|
||||||
#define FPGA_MAJOR_MODE_HF_ISO14443A (3<<5)
|
|
||||||
// BOTH
|
|
||||||
#define FPGA_MAJOR_MODE_OFF (7<<5)
|
|
||||||
// Options for LF_ADC
|
|
||||||
#define FPGA_LF_ADC_READER_FIELD (1<<0)
|
|
||||||
// Options for LF_EDGE_DETECT
|
|
||||||
#define FPGA_CMD_SET_EDGE_DETECT_THRESHOLD FPGA_CMD_SET_USER_BYTE1
|
|
||||||
#define FPGA_LF_EDGE_DETECT_READER_FIELD (1<<0)
|
|
||||||
#define FPGA_LF_EDGE_DETECT_TOGGLE_MODE (1<<1)
|
|
||||||
// Options for the HF reader, tx to tag
|
|
||||||
#define FPGA_HF_READER_TX_SHALLOW_MOD (1<<0)
|
|
||||||
// Options for the HF reader, correlating against rx from tag
|
|
||||||
#define FPGA_HF_READER_RX_XCORR_848_KHZ (1<<0)
|
|
||||||
#define FPGA_HF_READER_RX_XCORR_SNOOP (1<<1)
|
|
||||||
#define FPGA_HF_READER_RX_XCORR_QUARTER_FREQ (1<<2)
|
|
||||||
// Options for the HF simulated tag, how to modulate
|
|
||||||
#define FPGA_HF_SIMULATOR_NO_MODULATION (0<<0)
|
|
||||||
#define FPGA_HF_SIMULATOR_MODULATE_BPSK (1<<0)
|
|
||||||
#define FPGA_HF_SIMULATOR_MODULATE_212K (2<<0)
|
|
||||||
#define FPGA_HF_SIMULATOR_MODULATE_424K (4<<0)
|
|
||||||
#define FPGA_HF_SIMULATOR_MODULATE_424K_8BIT 0x5//101
|
|
||||||
|
|
||||||
// Options for ISO14443A
|
|
||||||
#define FPGA_HF_ISO14443A_SNIFFER (0<<0)
|
|
||||||
#define FPGA_HF_ISO14443A_TAGSIM_LISTEN (1<<0)
|
|
||||||
#define FPGA_HF_ISO14443A_TAGSIM_MOD (2<<0)
|
|
||||||
#define FPGA_HF_ISO14443A_READER_LISTEN (3<<0)
|
|
||||||
#define FPGA_HF_ISO14443A_READER_MOD (4<<0)
|
|
||||||
|
|
||||||
/// lfops.h
|
/// lfops.h
|
||||||
extern uint8_t decimation;
|
extern uint8_t decimation;
|
||||||
|
|
|
@ -9,10 +9,32 @@
|
||||||
// Routines to load the FPGA image, and then to configure the FPGA's major
|
// Routines to load the FPGA image, and then to configure the FPGA's major
|
||||||
// mode once it is configured.
|
// mode once it is configured.
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include "fpgaloader.h"
|
||||||
#include "proxmark3.h"
|
#include "proxmark3.h"
|
||||||
#include "apps.h"
|
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
#include "string.h"
|
#include "string.h"
|
||||||
|
#include "BigBuf.h"
|
||||||
|
#include "zlib.h"
|
||||||
|
|
||||||
|
extern void Dbprintf(const char *fmt, ...);
|
||||||
|
|
||||||
|
// remember which version of the bitstream we have already downloaded to the FPGA
|
||||||
|
static int downloaded_bitstream = FPGA_BITSTREAM_ERR;
|
||||||
|
|
||||||
|
// this is where the bitstreams are located in memory:
|
||||||
|
extern uint8_t _binary_obj_fpga_all_bit_z_start, _binary_obj_fpga_all_bit_z_end;
|
||||||
|
|
||||||
|
static uint8_t *fpga_image_ptr = NULL;
|
||||||
|
static uint32_t uncompressed_bytes_cnt;
|
||||||
|
|
||||||
|
static const uint8_t _bitparse_fixed_header[] = {0x00, 0x09, 0x0f, 0xf0, 0x0f, 0xf0, 0x0f, 0xf0, 0x0f, 0xf0, 0x00, 0x00, 0x01};
|
||||||
|
#define FPGA_BITSTREAM_FIXED_HEADER_SIZE sizeof(_bitparse_fixed_header)
|
||||||
|
#define OUTPUT_BUFFER_LEN 80
|
||||||
|
#define FPGA_INTERLEAVE_SIZE 288
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
// Set up the Serial Peripheral Interface as master
|
// Set up the Serial Peripheral Interface as master
|
||||||
|
@ -150,6 +172,94 @@ bool FpgaSetupSscDma(uint8_t *buf, int len)
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//----------------------------------------------------------------------------
|
||||||
|
// Uncompress (inflate) the FPGA data. Returns one decompressed byte with
|
||||||
|
// each call.
|
||||||
|
//----------------------------------------------------------------------------
|
||||||
|
static int get_from_fpga_combined_stream(z_streamp compressed_fpga_stream, uint8_t *output_buffer)
|
||||||
|
{
|
||||||
|
if (fpga_image_ptr == compressed_fpga_stream->next_out) { // need more data
|
||||||
|
compressed_fpga_stream->next_out = output_buffer;
|
||||||
|
compressed_fpga_stream->avail_out = OUTPUT_BUFFER_LEN;
|
||||||
|
fpga_image_ptr = output_buffer;
|
||||||
|
int res = inflate(compressed_fpga_stream, Z_SYNC_FLUSH);
|
||||||
|
if (res != Z_OK) {
|
||||||
|
Dbprintf("inflate returned: %d, %s", res, compressed_fpga_stream->msg);
|
||||||
|
}
|
||||||
|
if (res < 0) {
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
uncompressed_bytes_cnt++;
|
||||||
|
|
||||||
|
return *fpga_image_ptr++;
|
||||||
|
}
|
||||||
|
|
||||||
|
//----------------------------------------------------------------------------
|
||||||
|
// Undo the interleaving of several FPGA config files. FPGA config files
|
||||||
|
// are combined into one big file:
|
||||||
|
// 288 bytes from FPGA file 1, followed by 288 bytes from FGPA file 2, etc.
|
||||||
|
//----------------------------------------------------------------------------
|
||||||
|
static int get_from_fpga_stream(int bitstream_version, z_streamp compressed_fpga_stream, uint8_t *output_buffer)
|
||||||
|
{
|
||||||
|
while((uncompressed_bytes_cnt / FPGA_INTERLEAVE_SIZE) % FPGA_BITSTREAM_MAX != (bitstream_version - 1)) {
|
||||||
|
// skip undesired data belonging to other bitstream_versions
|
||||||
|
get_from_fpga_combined_stream(compressed_fpga_stream, output_buffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
return get_from_fpga_combined_stream(compressed_fpga_stream, output_buffer);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static voidpf fpga_inflate_malloc(voidpf opaque, uInt items, uInt size)
|
||||||
|
{
|
||||||
|
return BigBuf_malloc(items*size);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void fpga_inflate_free(voidpf opaque, voidpf address)
|
||||||
|
{
|
||||||
|
BigBuf_free();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//----------------------------------------------------------------------------
|
||||||
|
// Initialize decompression of the respective (HF or LF) FPGA stream
|
||||||
|
//----------------------------------------------------------------------------
|
||||||
|
static bool reset_fpga_stream(int bitstream_version, z_streamp compressed_fpga_stream, uint8_t *output_buffer)
|
||||||
|
{
|
||||||
|
uint8_t header[FPGA_BITSTREAM_FIXED_HEADER_SIZE];
|
||||||
|
|
||||||
|
uncompressed_bytes_cnt = 0;
|
||||||
|
|
||||||
|
// initialize z_stream structure for inflate:
|
||||||
|
compressed_fpga_stream->next_in = &_binary_obj_fpga_all_bit_z_start;
|
||||||
|
compressed_fpga_stream->avail_in = &_binary_obj_fpga_all_bit_z_start - &_binary_obj_fpga_all_bit_z_end;
|
||||||
|
compressed_fpga_stream->next_out = output_buffer;
|
||||||
|
compressed_fpga_stream->avail_out = OUTPUT_BUFFER_LEN;
|
||||||
|
compressed_fpga_stream->zalloc = &fpga_inflate_malloc;
|
||||||
|
compressed_fpga_stream->zfree = &fpga_inflate_free;
|
||||||
|
|
||||||
|
inflateInit2(compressed_fpga_stream, 0);
|
||||||
|
|
||||||
|
fpga_image_ptr = output_buffer;
|
||||||
|
|
||||||
|
for (uint16_t i = 0; i < FPGA_BITSTREAM_FIXED_HEADER_SIZE; i++) {
|
||||||
|
header[i] = get_from_fpga_stream(bitstream_version, compressed_fpga_stream, output_buffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check for a valid .bit file (starts with _bitparse_fixed_header)
|
||||||
|
if(memcmp(_bitparse_fixed_header, header, FPGA_BITSTREAM_FIXED_HEADER_SIZE) == 0) {
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
static void DownloadFPGA_byte(unsigned char w)
|
static void DownloadFPGA_byte(unsigned char w)
|
||||||
{
|
{
|
||||||
#define SEND_BIT(x) { if(w & (1<<x) ) HIGH(GPIO_FPGA_DIN); else LOW(GPIO_FPGA_DIN); HIGH(GPIO_FPGA_CCLK); LOW(GPIO_FPGA_CCLK); }
|
#define SEND_BIT(x) { if(w & (1<<x) ) HIGH(GPIO_FPGA_DIN); else LOW(GPIO_FPGA_DIN); HIGH(GPIO_FPGA_CCLK); LOW(GPIO_FPGA_CCLK); }
|
||||||
|
@ -163,10 +273,12 @@ static void DownloadFPGA_byte(unsigned char w)
|
||||||
SEND_BIT(0);
|
SEND_BIT(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Download the fpga image starting at FpgaImage and with length FpgaImageLen bytes
|
// Download the fpga image starting at current stream position with length FpgaImageLen bytes
|
||||||
// If bytereversal is set: reverse the byte order in each 4-byte word
|
static void DownloadFPGA(int bitstream_version, int FpgaImageLen, z_streamp compressed_fpga_stream, uint8_t *output_buffer)
|
||||||
static void DownloadFPGA(const char *FpgaImage, int FpgaImageLen, int bytereversal)
|
|
||||||
{
|
{
|
||||||
|
|
||||||
|
Dbprintf("DownloadFPGA(len: %d)", FpgaImageLen);
|
||||||
|
|
||||||
int i=0;
|
int i=0;
|
||||||
|
|
||||||
AT91C_BASE_PIOA->PIO_OER = GPIO_FPGA_ON;
|
AT91C_BASE_PIOA->PIO_OER = GPIO_FPGA_ON;
|
||||||
|
@ -218,23 +330,15 @@ static void DownloadFPGA(const char *FpgaImage, int FpgaImageLen, int byterevers
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(bytereversal) {
|
for(i = 0; i < FpgaImageLen; i++) {
|
||||||
/* This is only supported for uint32_t aligned images */
|
int b = get_from_fpga_stream(bitstream_version, compressed_fpga_stream, output_buffer);
|
||||||
if( ((int)FpgaImage % sizeof(uint32_t)) == 0 ) {
|
if (b < 0) {
|
||||||
i=0;
|
Dbprintf("Error %d during FpgaDownload", b);
|
||||||
while(FpgaImageLen-->0)
|
break;
|
||||||
DownloadFPGA_byte(FpgaImage[(i++)^0x3]);
|
|
||||||
/* Explanation of the magic in the above line:
|
|
||||||
* i^0x3 inverts the lower two bits of the integer i, counting backwards
|
|
||||||
* for each 4 byte increment. The generated sequence of (i++)^3 is
|
|
||||||
* 3 2 1 0 7 6 5 4 11 10 9 8 15 14 13 12 etc. pp.
|
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
} else {
|
DownloadFPGA_byte(b);
|
||||||
while(FpgaImageLen-->0)
|
|
||||||
DownloadFPGA_byte(*FpgaImage++);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// continue to clock FPGA until ready signal goes high
|
// continue to clock FPGA until ready signal goes high
|
||||||
i=100000;
|
i=100000;
|
||||||
while ( (i--) && ( !(AT91C_BASE_PIOA->PIO_PDSR & GPIO_FPGA_DONE ) ) ) {
|
while ( (i--) && ( !(AT91C_BASE_PIOA->PIO_PDSR & GPIO_FPGA_DONE ) ) ) {
|
||||||
|
@ -250,39 +354,21 @@ static void DownloadFPGA(const char *FpgaImage, int FpgaImageLen, int byterevers
|
||||||
LED_D_OFF();
|
LED_D_OFF();
|
||||||
}
|
}
|
||||||
|
|
||||||
static char *bitparse_headers_start;
|
|
||||||
static char *bitparse_bitstream_end;
|
|
||||||
static int bitparse_initialized = 0;
|
|
||||||
/* Simple Xilinx .bit parser. The file starts with the fixed opaque byte sequence
|
/* Simple Xilinx .bit parser. The file starts with the fixed opaque byte sequence
|
||||||
* 00 09 0f f0 0f f0 0f f0 0f f0 00 00 01
|
* 00 09 0f f0 0f f0 0f f0 0f f0 00 00 01
|
||||||
* After that the format is 1 byte section type (ASCII character), 2 byte length
|
* After that the format is 1 byte section type (ASCII character), 2 byte length
|
||||||
* (big endian), <length> bytes content. Except for section 'e' which has 4 bytes
|
* (big endian), <length> bytes content. Except for section 'e' which has 4 bytes
|
||||||
* length.
|
* length.
|
||||||
*/
|
*/
|
||||||
static const char _bitparse_fixed_header[] = {0x00, 0x09, 0x0f, 0xf0, 0x0f, 0xf0, 0x0f, 0xf0, 0x0f, 0xf0, 0x00, 0x00, 0x01};
|
static int bitparse_find_section(int bitstream_version, char section_name, unsigned int *section_length, z_streamp compressed_fpga_stream, uint8_t *output_buffer)
|
||||||
static int bitparse_init(void * start_address, void *end_address)
|
|
||||||
{
|
{
|
||||||
bitparse_initialized = 0;
|
|
||||||
|
|
||||||
if(memcmp(_bitparse_fixed_header, start_address, sizeof(_bitparse_fixed_header)) != 0) {
|
|
||||||
return 0; /* Not matched */
|
|
||||||
} else {
|
|
||||||
bitparse_headers_start= ((char*)start_address) + sizeof(_bitparse_fixed_header);
|
|
||||||
bitparse_bitstream_end= (char*)end_address;
|
|
||||||
bitparse_initialized = 1;
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int bitparse_find_section(char section_name, char **section_start, unsigned int *section_length)
|
|
||||||
{
|
|
||||||
char *pos = bitparse_headers_start;
|
|
||||||
int result = 0;
|
int result = 0;
|
||||||
|
#define MAX_FPGA_BIT_STREAM_HEADER_SEARCH 100 // maximum number of bytes to search for the requested section
|
||||||
if(!bitparse_initialized) return 0;
|
uint16_t numbytes = 0;
|
||||||
|
while(numbytes < MAX_FPGA_BIT_STREAM_HEADER_SEARCH) {
|
||||||
while(pos < bitparse_bitstream_end) {
|
char current_name = get_from_fpga_stream(bitstream_version, compressed_fpga_stream, output_buffer);
|
||||||
char current_name = *pos++;
|
numbytes++;
|
||||||
unsigned int current_length = 0;
|
unsigned int current_length = 0;
|
||||||
if(current_name < 'a' || current_name > 'e') {
|
if(current_name < 'a' || current_name > 'e') {
|
||||||
/* Strange section name, abort */
|
/* Strange section name, abort */
|
||||||
|
@ -292,11 +378,13 @@ int bitparse_find_section(char section_name, char **section_start, unsigned int
|
||||||
switch(current_name) {
|
switch(current_name) {
|
||||||
case 'e':
|
case 'e':
|
||||||
/* Four byte length field */
|
/* Four byte length field */
|
||||||
current_length += (*pos++) << 24;
|
current_length += get_from_fpga_stream(bitstream_version, compressed_fpga_stream, output_buffer) << 24;
|
||||||
current_length += (*pos++) << 16;
|
current_length += get_from_fpga_stream(bitstream_version, compressed_fpga_stream, output_buffer) << 16;
|
||||||
|
numbytes += 2;
|
||||||
default: /* Fall through, two byte length field */
|
default: /* Fall through, two byte length field */
|
||||||
current_length += (*pos++) << 8;
|
current_length += get_from_fpga_stream(bitstream_version, compressed_fpga_stream, output_buffer) << 8;
|
||||||
current_length += (*pos++) << 0;
|
current_length += get_from_fpga_stream(bitstream_version, compressed_fpga_stream, output_buffer) << 0;
|
||||||
|
numbytes += 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(current_name != 'e' && current_length > 255) {
|
if(current_name != 'e' && current_length > 255) {
|
||||||
|
@ -306,111 +394,123 @@ int bitparse_find_section(char section_name, char **section_start, unsigned int
|
||||||
|
|
||||||
if(current_name == section_name) {
|
if(current_name == section_name) {
|
||||||
/* Found it */
|
/* Found it */
|
||||||
*section_start = pos;
|
|
||||||
*section_length = current_length;
|
*section_length = current_length;
|
||||||
result = 1;
|
result = 1;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
pos += current_length; /* Skip section */
|
for (uint16_t i = 0; i < current_length && numbytes < MAX_FPGA_BIT_STREAM_HEADER_SEARCH; i++) {
|
||||||
|
get_from_fpga_stream(bitstream_version, compressed_fpga_stream, output_buffer);
|
||||||
|
numbytes++;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
// Find out which FPGA image format is stored in flash, then call DownloadFPGA
|
//----------------------------------------------------------------------------
|
||||||
// with the right parameters to download the image
|
// Check which FPGA image is currently loaded (if any). If necessary
|
||||||
//-----------------------------------------------------------------------------
|
// decompress and load the correct (HF or LF) image to the FPGA
|
||||||
extern char _binary_fpga_lf_bit_start, _binary_fpga_lf_bit_end;
|
//----------------------------------------------------------------------------
|
||||||
extern char _binary_fpga_hf_bit_start, _binary_fpga_hf_bit_end;
|
|
||||||
void FpgaDownloadAndGo(int bitstream_version)
|
void FpgaDownloadAndGo(int bitstream_version)
|
||||||
{
|
{
|
||||||
void *bit_start;
|
z_stream compressed_fpga_stream;
|
||||||
void *bit_end;
|
uint8_t output_buffer[OUTPUT_BUFFER_LEN];
|
||||||
|
|
||||||
// check whether or not the bitstream is already loaded
|
// check whether or not the bitstream is already loaded
|
||||||
if (FpgaGatherBitstreamVersion() == bitstream_version)
|
if (downloaded_bitstream == bitstream_version)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (bitstream_version == FPGA_BITSTREAM_LF) {
|
// make sure that we have enough memory to decompress
|
||||||
bit_start = &_binary_fpga_lf_bit_start;
|
BigBuf_free();
|
||||||
bit_end = &_binary_fpga_lf_bit_end;
|
|
||||||
} else if (bitstream_version == FPGA_BITSTREAM_HF) {
|
if (!reset_fpga_stream(bitstream_version, &compressed_fpga_stream, output_buffer)) {
|
||||||
bit_start = &_binary_fpga_hf_bit_start;
|
|
||||||
bit_end = &_binary_fpga_hf_bit_end;
|
|
||||||
} else
|
|
||||||
return;
|
return;
|
||||||
/* Check for the new flash image format: Should have the .bit file at &_binary_fpga_bit_start
|
|
||||||
*/
|
|
||||||
if(bitparse_init(bit_start, bit_end)) {
|
|
||||||
/* Successfully initialized the .bit parser. Find the 'e' section and
|
|
||||||
* send its contents to the FPGA.
|
|
||||||
*/
|
|
||||||
char *bitstream_start;
|
|
||||||
unsigned int bitstream_length;
|
|
||||||
if(bitparse_find_section('e', &bitstream_start, &bitstream_length)) {
|
|
||||||
DownloadFPGA(bitstream_start, bitstream_length, 0);
|
|
||||||
|
|
||||||
return; /* All done */
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Fallback for the old flash image format: Check for the magic marker 0xFFFFFFFF
|
unsigned int bitstream_length;
|
||||||
* 0xAA995566 at address 0x102000. This is raw bitstream with a size of 336,768 bits
|
if(bitparse_find_section(bitstream_version, 'e', &bitstream_length, &compressed_fpga_stream, output_buffer)) {
|
||||||
* = 10,524 uint32_t, stored as uint32_t e.g. little-endian in memory, but each DWORD
|
DownloadFPGA(bitstream_version, bitstream_length, &compressed_fpga_stream, output_buffer);
|
||||||
* is still to be transmitted in MSBit first order. Set the invert flag to indicate
|
downloaded_bitstream = bitstream_version;
|
||||||
* that the DownloadFPGA function should invert every 4 byte sequence when doing
|
}
|
||||||
* the bytewise download.
|
|
||||||
*/
|
|
||||||
if( *(uint32_t*)0x102000 == 0xFFFFFFFF && *(uint32_t*)0x102004 == 0xAA995566 )
|
|
||||||
DownloadFPGA((char*)0x102000, 10524*4, 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
int FpgaGatherBitstreamVersion()
|
inflateEnd(&compressed_fpga_stream);
|
||||||
{
|
}
|
||||||
char temp[256];
|
|
||||||
FpgaGatherVersion(temp, sizeof (temp));
|
|
||||||
if (!memcmp("LF", temp, 2))
|
|
||||||
return FPGA_BITSTREAM_LF;
|
|
||||||
else if (!memcmp("HF", temp, 2))
|
|
||||||
return FPGA_BITSTREAM_HF;
|
|
||||||
return FPGA_BITSTREAM_ERR;
|
|
||||||
}
|
|
||||||
|
|
||||||
void FpgaGatherVersion(char *dst, int len)
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
// Gather version information from FPGA image. Needs to decompress the begin
|
||||||
|
// of the respective (HF or LF) image.
|
||||||
|
// Note: decompression makes use of (i.e. overwrites) BigBuf[]. It is therefore
|
||||||
|
// advisable to call this only once and store the results for later use.
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
void FpgaGatherVersion(int bitstream_version, char *dst, int len)
|
||||||
{
|
{
|
||||||
char *fpga_info;
|
|
||||||
unsigned int fpga_info_len;
|
unsigned int fpga_info_len;
|
||||||
dst[0] = 0;
|
char tempstr[40];
|
||||||
if(!bitparse_find_section('e', &fpga_info, &fpga_info_len)) {
|
z_stream compressed_fpga_stream;
|
||||||
strncat(dst, "FPGA image: legacy image without version information", len-1);
|
uint8_t output_buffer[OUTPUT_BUFFER_LEN];
|
||||||
} else {
|
|
||||||
/* USB packets only have 48 bytes data payload, so be terse */
|
dst[0] = '\0';
|
||||||
if(bitparse_find_section('a', &fpga_info, &fpga_info_len) && fpga_info[fpga_info_len-1] == 0 ) {
|
|
||||||
if (!memcmp("fpga_lf", fpga_info, 7))
|
// ensure that we can allocate enough memory for decompression:
|
||||||
strncat(dst, "LF ", len-1);
|
BigBuf_free();
|
||||||
else if (!memcmp("fpga_hf", fpga_info, 7))
|
|
||||||
strncat(dst, "HF ", len-1);
|
if (!reset_fpga_stream(bitstream_version, &compressed_fpga_stream, output_buffer)) {
|
||||||
}
|
return;
|
||||||
strncat(dst, "FPGA image built", len-1);
|
|
||||||
#if 0
|
|
||||||
if(bitparse_find_section('b', &fpga_info, &fpga_info_len) && fpga_info[fpga_info_len-1] == 0 ) {
|
|
||||||
strncat(dst, " for ", len-1);
|
|
||||||
strncat(dst, fpga_info, len-1);
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
if(bitparse_find_section('c', &fpga_info, &fpga_info_len) && fpga_info[fpga_info_len-1] == 0 ) {
|
|
||||||
strncat(dst, " on ", len-1);
|
|
||||||
strncat(dst, fpga_info, len-1);
|
|
||||||
}
|
|
||||||
if(bitparse_find_section('d', &fpga_info, &fpga_info_len) && fpga_info[fpga_info_len-1] == 0 ) {
|
|
||||||
strncat(dst, " at ", len-1);
|
|
||||||
strncat(dst, fpga_info, len-1);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(bitparse_find_section(bitstream_version, 'a', &fpga_info_len, &compressed_fpga_stream, output_buffer)) {
|
||||||
|
for (uint16_t i = 0; i < fpga_info_len; i++) {
|
||||||
|
char c = (char)get_from_fpga_stream(bitstream_version, &compressed_fpga_stream, output_buffer);
|
||||||
|
if (i < sizeof(tempstr)) {
|
||||||
|
tempstr[i] = c;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!memcmp("fpga_lf", tempstr, 7))
|
||||||
|
strncat(dst, "LF ", len-1);
|
||||||
|
else if (!memcmp("fpga_hf", tempstr, 7))
|
||||||
|
strncat(dst, "HF ", len-1);
|
||||||
|
}
|
||||||
|
strncat(dst, "FPGA image built", len-1);
|
||||||
|
if(bitparse_find_section(bitstream_version, 'b', &fpga_info_len, &compressed_fpga_stream, output_buffer)) {
|
||||||
|
strncat(dst, " for ", len-1);
|
||||||
|
for (uint16_t i = 0; i < fpga_info_len; i++) {
|
||||||
|
char c = (char)get_from_fpga_stream(bitstream_version, &compressed_fpga_stream, output_buffer);
|
||||||
|
if (i < sizeof(tempstr)) {
|
||||||
|
tempstr[i] = c;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
strncat(dst, tempstr, len-1);
|
||||||
|
}
|
||||||
|
if(bitparse_find_section(bitstream_version, 'c', &fpga_info_len, &compressed_fpga_stream, output_buffer)) {
|
||||||
|
strncat(dst, " on ", len-1);
|
||||||
|
for (uint16_t i = 0; i < fpga_info_len; i++) {
|
||||||
|
char c = (char)get_from_fpga_stream(bitstream_version, &compressed_fpga_stream, output_buffer);
|
||||||
|
if (i < sizeof(tempstr)) {
|
||||||
|
tempstr[i] = c;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
strncat(dst, tempstr, len-1);
|
||||||
|
}
|
||||||
|
if(bitparse_find_section(bitstream_version, 'd', &fpga_info_len, &compressed_fpga_stream, output_buffer)) {
|
||||||
|
strncat(dst, " at ", len-1);
|
||||||
|
for (uint16_t i = 0; i < fpga_info_len; i++) {
|
||||||
|
char c = (char)get_from_fpga_stream(bitstream_version, &compressed_fpga_stream, output_buffer);
|
||||||
|
if (i < sizeof(tempstr)) {
|
||||||
|
tempstr[i] = c;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
strncat(dst, tempstr, len-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
strncat(dst, "\n", len-1);
|
||||||
|
|
||||||
|
inflateEnd(&compressed_fpga_stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
// Send a 16 bit command/data pair to the FPGA.
|
// Send a 16 bit command/data pair to the FPGA.
|
||||||
// The bit format is: C3 C2 C1 C0 D11 D10 D9 D8 D7 D6 D5 D4 D3 D2 D1 D0
|
// The bit format is: C3 C2 C1 C0 D11 D10 D9 D8 D7 D6 D5 D4 D3 D2 D1 D0
|
||||||
|
|
71
armsrc/fpgaloader.h
Normal file
71
armsrc/fpgaloader.h
Normal file
|
@ -0,0 +1,71 @@
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
// Jonathan Westhues, April 2006
|
||||||
|
// iZsh <izsh at fail0verflow.com>, 2014
|
||||||
|
//
|
||||||
|
// This code is licensed to you under the terms of the GNU GPL, version 2 or,
|
||||||
|
// at your option, any later version. See the LICENSE.txt file for the text of
|
||||||
|
// the license.
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
// Routines to load the FPGA image, and then to configure the FPGA's major
|
||||||
|
// mode once it is configured.
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
void FpgaSendCommand(uint16_t cmd, uint16_t v);
|
||||||
|
void FpgaWriteConfWord(uint8_t v);
|
||||||
|
void FpgaDownloadAndGo(int bitstream_version);
|
||||||
|
void FpgaGatherVersion(int bitstream_version, char *dst, int len);
|
||||||
|
void FpgaSetupSsc(void);
|
||||||
|
void SetupSpi(int mode);
|
||||||
|
bool FpgaSetupSscDma(uint8_t *buf, int len);
|
||||||
|
#define FpgaDisableSscDma(void) AT91C_BASE_PDC_SSC->PDC_PTCR = AT91C_PDC_RXTDIS;
|
||||||
|
#define FpgaEnableSscDma(void) AT91C_BASE_PDC_SSC->PDC_PTCR = AT91C_PDC_RXTEN;
|
||||||
|
void SetAdcMuxFor(uint32_t whichGpio);
|
||||||
|
|
||||||
|
// definitions for multiple FPGA config files support
|
||||||
|
#define FPGA_BITSTREAM_MAX 2 // the total number of FPGA bitstreams (configs)
|
||||||
|
#define FPGA_BITSTREAM_ERR 0
|
||||||
|
#define FPGA_BITSTREAM_LF 1
|
||||||
|
#define FPGA_BITSTREAM_HF 2
|
||||||
|
|
||||||
|
|
||||||
|
// Definitions for the FPGA commands.
|
||||||
|
#define FPGA_CMD_SET_CONFREG (1<<12)
|
||||||
|
#define FPGA_CMD_SET_DIVISOR (2<<12)
|
||||||
|
#define FPGA_CMD_SET_USER_BYTE1 (3<<12)
|
||||||
|
// Definitions for the FPGA configuration word.
|
||||||
|
// LF
|
||||||
|
#define FPGA_MAJOR_MODE_LF_ADC (0<<5)
|
||||||
|
#define FPGA_MAJOR_MODE_LF_EDGE_DETECT (1<<5)
|
||||||
|
#define FPGA_MAJOR_MODE_LF_PASSTHRU (2<<5)
|
||||||
|
// HF
|
||||||
|
#define FPGA_MAJOR_MODE_HF_READER_TX (0<<5)
|
||||||
|
#define FPGA_MAJOR_MODE_HF_READER_RX_XCORR (1<<5)
|
||||||
|
#define FPGA_MAJOR_MODE_HF_SIMULATOR (2<<5)
|
||||||
|
#define FPGA_MAJOR_MODE_HF_ISO14443A (3<<5)
|
||||||
|
// BOTH
|
||||||
|
#define FPGA_MAJOR_MODE_OFF (7<<5)
|
||||||
|
// Options for LF_ADC
|
||||||
|
#define FPGA_LF_ADC_READER_FIELD (1<<0)
|
||||||
|
// Options for LF_EDGE_DETECT
|
||||||
|
#define FPGA_CMD_SET_EDGE_DETECT_THRESHOLD FPGA_CMD_SET_USER_BYTE1
|
||||||
|
#define FPGA_LF_EDGE_DETECT_READER_FIELD (1<<0)
|
||||||
|
#define FPGA_LF_EDGE_DETECT_TOGGLE_MODE (1<<1)
|
||||||
|
// Options for the HF reader, tx to tag
|
||||||
|
#define FPGA_HF_READER_TX_SHALLOW_MOD (1<<0)
|
||||||
|
// Options for the HF reader, correlating against rx from tag
|
||||||
|
#define FPGA_HF_READER_RX_XCORR_848_KHZ (1<<0)
|
||||||
|
#define FPGA_HF_READER_RX_XCORR_SNOOP (1<<1)
|
||||||
|
#define FPGA_HF_READER_RX_XCORR_QUARTER_FREQ (1<<2)
|
||||||
|
// Options for the HF simulated tag, how to modulate
|
||||||
|
#define FPGA_HF_SIMULATOR_NO_MODULATION (0<<0)
|
||||||
|
#define FPGA_HF_SIMULATOR_MODULATE_BPSK (1<<0)
|
||||||
|
#define FPGA_HF_SIMULATOR_MODULATE_212K (2<<0)
|
||||||
|
#define FPGA_HF_SIMULATOR_MODULATE_424K (4<<0)
|
||||||
|
#define FPGA_HF_SIMULATOR_MODULATE_424K_8BIT 0x5//101
|
||||||
|
|
||||||
|
// Options for ISO14443A
|
||||||
|
#define FPGA_HF_ISO14443A_SNIFFER (0<<0)
|
||||||
|
#define FPGA_HF_ISO14443A_TAGSIM_LISTEN (1<<0)
|
||||||
|
#define FPGA_HF_ISO14443A_TAGSIM_MOD (2<<0)
|
||||||
|
#define FPGA_HF_ISO14443A_READER_LISTEN (3<<0)
|
||||||
|
#define FPGA_HF_ISO14443A_READER_MOD (4<<0)
|
|
@ -24,6 +24,7 @@ SECTIONS
|
||||||
} >osimage :text
|
} >osimage :text
|
||||||
|
|
||||||
.text : {
|
.text : {
|
||||||
|
KEEP(*(stage1_image))
|
||||||
*(.text)
|
*(.text)
|
||||||
*(.text.*)
|
*(.text.*)
|
||||||
*(.eh_frame)
|
*(.eh_frame)
|
||||||
|
@ -34,14 +35,13 @@ SECTIONS
|
||||||
.rodata : {
|
.rodata : {
|
||||||
*(.rodata)
|
*(.rodata)
|
||||||
*(.rodata.*)
|
*(.rodata.*)
|
||||||
*(fpga_lf_bit.data)
|
*(fpga_all_bit.data)
|
||||||
*(fpga_hf_bit.data)
|
|
||||||
KEEP(*(.version_information))
|
KEEP(*(.version_information))
|
||||||
|
. = ALIGN(8);
|
||||||
} >osimage :text
|
} >osimage :text
|
||||||
|
|
||||||
. = ALIGN(4);
|
|
||||||
|
|
||||||
.data : {
|
.data : {
|
||||||
|
KEEP(*(compressed_data))
|
||||||
*(.data)
|
*(.data)
|
||||||
*(.data.*)
|
*(.data.*)
|
||||||
*(.ramfunc)
|
*(.ramfunc)
|
||||||
|
@ -51,6 +51,7 @@ SECTIONS
|
||||||
__data_src_start__ = LOADADDR(.data);
|
__data_src_start__ = LOADADDR(.data);
|
||||||
__data_start__ = ADDR(.data);
|
__data_start__ = ADDR(.data);
|
||||||
__data_end__ = __data_start__ + SIZEOF(.data);
|
__data_end__ = __data_start__ + SIZEOF(.data);
|
||||||
|
__os_size__ = SIZEOF(.text) + SIZEOF(.data) + SIZEOF(.rodata);
|
||||||
|
|
||||||
.bss : {
|
.bss : {
|
||||||
__bss_start__ = .;
|
__bss_start__ = .;
|
||||||
|
|
|
@ -11,23 +11,75 @@
|
||||||
|
|
||||||
#include "proxmark3.h"
|
#include "proxmark3.h"
|
||||||
#include "apps.h"
|
#include "apps.h"
|
||||||
|
#include "zlib.h"
|
||||||
|
#include "BigBuf.h"
|
||||||
|
|
||||||
|
static uint8_t *next_free_memory;
|
||||||
|
extern struct common_area common_area;
|
||||||
|
extern char __data_src_start__, __data_start__, __data_end__, __bss_start__, __bss_end__;
|
||||||
|
|
||||||
|
|
||||||
|
static voidpf inflate_malloc(voidpf opaque, uInt items, uInt size)
|
||||||
|
{
|
||||||
|
uint8_t *allocated_memory;
|
||||||
|
|
||||||
|
allocated_memory = next_free_memory;
|
||||||
|
next_free_memory += items*size;
|
||||||
|
return allocated_memory;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void inflate_free(voidpf opaque, voidpf address)
|
||||||
|
{
|
||||||
|
// nothing to do
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
static void uncompress_data_section(void)
|
||||||
|
{
|
||||||
|
z_stream data_section;
|
||||||
|
|
||||||
|
next_free_memory = BigBuf_get_addr();
|
||||||
|
|
||||||
|
// initialize zstream structure
|
||||||
|
data_section.next_in = (uint8_t *) &__data_src_start__;
|
||||||
|
data_section.avail_in = &__data_end__ - &__data_start__; // uncompressed size. Wrong but doesn't matter.
|
||||||
|
data_section.next_out = (uint8_t *) &__data_start__;
|
||||||
|
data_section.avail_out = &__data_end__ - &__data_start__; // uncompressed size. Correct.
|
||||||
|
data_section.zalloc = &inflate_malloc;
|
||||||
|
data_section.zfree = &inflate_free;
|
||||||
|
data_section.opaque = NULL;
|
||||||
|
|
||||||
|
// initialize zlib for inflate
|
||||||
|
inflateInit2(&data_section, 15);
|
||||||
|
|
||||||
|
// uncompress data segment to RAM
|
||||||
|
inflate(&data_section, Z_FINISH);
|
||||||
|
|
||||||
|
// save the size of the compressed data section
|
||||||
|
common_area.arg1 = data_section.total_in;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
extern char __data_start__, __data_src_start__, __data_end__, __bss_start__, __bss_end__;
|
|
||||||
void __attribute__((section(".startos"))) Vector(void)
|
void __attribute__((section(".startos"))) Vector(void)
|
||||||
{
|
{
|
||||||
/* Stack should have been set up by the bootloader */
|
/* Stack should have been set up by the bootloader */
|
||||||
char *src, *dst, *end;
|
// char *src;
|
||||||
|
char *dst, *end;
|
||||||
|
|
||||||
|
uncompress_data_section();
|
||||||
|
|
||||||
/* Set up (that is: clear) BSS. */
|
/* Set up (that is: clear) BSS. */
|
||||||
dst = &__bss_start__;
|
dst = &__bss_start__;
|
||||||
end = &__bss_end__;
|
end = &__bss_end__;
|
||||||
while(dst < end) *dst++ = 0;
|
while(dst < end) *dst++ = 0;
|
||||||
|
|
||||||
/* Set up data segment: Copy from flash to ram */
|
// Set up data segment: Copy from flash to ram
|
||||||
src = &__data_src_start__;
|
// src = &__data_src_start__;
|
||||||
dst = &__data_start__;
|
// dst = &__data_start__;
|
||||||
end = &__data_end__;
|
// end = &__data_end__;
|
||||||
while(dst < end) *dst++ = *src++;
|
// while(dst < end) *dst++ = *src++;
|
||||||
|
|
||||||
|
|
||||||
AppMain();
|
AppMain();
|
||||||
}
|
}
|
||||||
|
|
|
@ -268,15 +268,15 @@ void FormatVersionInformation(char *dst, int len, const char *prefix, void *vers
|
||||||
dst[0] = 0;
|
dst[0] = 0;
|
||||||
strncat(dst, prefix, len-1);
|
strncat(dst, prefix, len-1);
|
||||||
if(v->magic != VERSION_INFORMATION_MAGIC) {
|
if(v->magic != VERSION_INFORMATION_MAGIC) {
|
||||||
strncat(dst, "Missing/Invalid version information", len - strlen(dst) - 1);
|
strncat(dst, "Missing/Invalid version information\n", len - strlen(dst) - 1);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if(v->versionversion != 1) {
|
if(v->versionversion != 1) {
|
||||||
strncat(dst, "Version information not understood", len - strlen(dst) - 1);
|
strncat(dst, "Version information not understood\n", len - strlen(dst) - 1);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if(!v->present) {
|
if(!v->present) {
|
||||||
strncat(dst, "Version information not available", len - strlen(dst) - 1);
|
strncat(dst, "Version information not available\n", len - strlen(dst) - 1);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -289,6 +289,7 @@ void FormatVersionInformation(char *dst, int len, const char *prefix, void *vers
|
||||||
|
|
||||||
strncat(dst, " ", len - strlen(dst) - 1);
|
strncat(dst, " ", len - strlen(dst) - 1);
|
||||||
strncat(dst, v->buildtime, len - strlen(dst) - 1);
|
strncat(dst, v->buildtime, len - strlen(dst) - 1);
|
||||||
|
strncat(dst, "\n", len - strlen(dst) - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
// -------------------------------------------------------------------------
|
// -------------------------------------------------------------------------
|
||||||
|
|
|
@ -9,12 +9,13 @@ include ../common/Makefile.common
|
||||||
CC=gcc
|
CC=gcc
|
||||||
CXX=g++
|
CXX=g++
|
||||||
#COMMON_FLAGS = -m32
|
#COMMON_FLAGS = -m32
|
||||||
VPATH = ../common
|
VPATH = ../common ../zlib
|
||||||
OBJDIR = obj
|
OBJDIR = obj
|
||||||
|
|
||||||
LDLIBS = -L/opt/local/lib -L/usr/local/lib ../liblua/liblua.a -lreadline -lpthread -lm
|
LDLIBS = -L/opt/local/lib -L/usr/local/lib -lreadline -lpthread -lm
|
||||||
|
LUALIB = ../liblua/liblua.a
|
||||||
LDFLAGS = $(COMMON_FLAGS)
|
LDFLAGS = $(COMMON_FLAGS)
|
||||||
CFLAGS = -std=c99 -I. -I../include -I../common -I/opt/local/include -I../liblua -Wall $(COMMON_FLAGS) -g -O4
|
CFLAGS = -std=c99 -I. -I../include -I../common -I../zlib -I/opt/local/include -I../liblua -Wall $(COMMON_FLAGS) -g -O4
|
||||||
LUAPLATFORM = generic
|
LUAPLATFORM = generic
|
||||||
|
|
||||||
ifneq (,$(findstring MINGW,$(platform)))
|
ifneq (,$(findstring MINGW,$(platform)))
|
||||||
|
@ -35,14 +36,13 @@ else ifeq ($(platform),Darwin)
|
||||||
else
|
else
|
||||||
CXXFLAGS = $(shell pkg-config --cflags QtCore QtGui 2>/dev/null) -Wall -O4
|
CXXFLAGS = $(shell pkg-config --cflags QtCore QtGui 2>/dev/null) -Wall -O4
|
||||||
QTLDLIBS = $(shell pkg-config --libs QtCore QtGui 2>/dev/null)
|
QTLDLIBS = $(shell pkg-config --libs QtCore QtGui 2>/dev/null)
|
||||||
|
LUALIB += -ldl
|
||||||
MOC = $(shell pkg-config --variable=moc_location QtCore)
|
MOC = $(shell pkg-config --variable=moc_location QtCore)
|
||||||
LDLIBS += -ldl
|
|
||||||
# Below is a variant you can use if you have problems compiling with QT5 on ubuntu. see http://www.proxmark.org/forum/viewtopic.php?id=1661 for more info.
|
# Below is a variant you can use if you have problems compiling with QT5 on ubuntu. see http://www.proxmark.org/forum/viewtopic.php?id=1661 for more info.
|
||||||
#MOC = /usr/lib/x86_64-linux-gnu/qt4/bin/moc
|
#MOC = /usr/lib/x86_64-linux-gnu/qt4/bin/moc
|
||||||
LUAPLATFORM = linux
|
LUAPLATFORM = linux
|
||||||
endif
|
endif
|
||||||
|
|
||||||
|
|
||||||
ifneq ($(QTLDLIBS),)
|
ifneq ($(QTLDLIBS),)
|
||||||
QTGUI = $(OBJDIR)/proxgui.o $(OBJDIR)/proxguiqt.o $(OBJDIR)/proxguiqt.moc.o
|
QTGUI = $(OBJDIR)/proxgui.o $(OBJDIR)/proxguiqt.o $(OBJDIR)/proxguiqt.moc.o
|
||||||
CFLAGS += -DHAVE_GUI
|
CFLAGS += -DHAVE_GUI
|
||||||
|
@ -104,20 +104,25 @@ CMDSRCS = nonce2key/crapto1.c\
|
||||||
protocols.c\
|
protocols.c\
|
||||||
sha1.c\
|
sha1.c\
|
||||||
|
|
||||||
|
ZLIBSRCS = deflate.c adler32.c trees.c zutil.c inflate.c inffast.c inftrees.c
|
||||||
|
ZLIB_FLAGS = -DZ_SOLO -DZ_PREFIX -DNO_GZIP -DZLIB_PM3_TUNED
|
||||||
|
#-DDEBUG -Dverbose=1
|
||||||
|
|
||||||
|
|
||||||
COREOBJS = $(CORESRCS:%.c=$(OBJDIR)/%.o)
|
COREOBJS = $(CORESRCS:%.c=$(OBJDIR)/%.o)
|
||||||
CMDOBJS = $(CMDSRCS:%.c=$(OBJDIR)/%.o)
|
CMDOBJS = $(CMDSRCS:%.c=$(OBJDIR)/%.o)
|
||||||
|
ZLIBOBJS = $(ZLIBSRCS:%.c=$(OBJDIR)/%.o)
|
||||||
|
|
||||||
RM = rm -f
|
RM = rm -f
|
||||||
BINS = proxmark3 flasher #snooper cli
|
BINS = proxmark3 flasher fpga_compress #snooper cli
|
||||||
CLEAN = cli cli.exe flasher flasher.exe proxmark3 proxmark3.exe snooper snooper.exe $(CMDOBJS) $(OBJDIR)/*.o *.o *.moc.cpp
|
CLEAN = cli cli.exe flasher flasher.exe proxmark3 proxmark3.exe fpga_compress fpga_compress.exe snooper snooper.exe $(CMDOBJS) $(OBJDIR)/*.o *.o *.moc.cpp
|
||||||
|
|
||||||
all: lua_build $(BINS)
|
all: lua_build $(BINS)
|
||||||
|
|
||||||
all-static: LDLIBS:=-static $(LDLIBS)
|
all-static: LDLIBS:=-static $(LDLIBS)
|
||||||
all-static: snooper cli flasher
|
all-static: snooper cli flasher fpga_compress
|
||||||
|
|
||||||
proxmark3: LDLIBS+=$(QTLDLIBS)
|
proxmark3: LDLIBS+=$(LUALIB) $(QTLDLIBS)
|
||||||
proxmark3: $(OBJDIR)/proxmark3.o $(COREOBJS) $(CMDOBJS) $(QTGUI)
|
proxmark3: $(OBJDIR)/proxmark3.o $(COREOBJS) $(CMDOBJS) $(QTGUI)
|
||||||
$(CXX) $(CXXFLAGS) $^ $(LDLIBS) -o $@
|
$(CXX) $(CXXFLAGS) $^ $(LDLIBS) -o $@
|
||||||
|
|
||||||
|
@ -130,8 +135,11 @@ cli: $(OBJDIR)/cli.o $(COREOBJS) $(CMDOBJS) $(OBJDIR)/guidummy.o
|
||||||
flasher: $(OBJDIR)/flash.o $(OBJDIR)/flasher.o $(COREOBJS)
|
flasher: $(OBJDIR)/flash.o $(OBJDIR)/flasher.o $(COREOBJS)
|
||||||
$(CXX) $(CXXFLAGS) $^ $(LDLIBS) -o $@
|
$(CXX) $(CXXFLAGS) $^ $(LDLIBS) -o $@
|
||||||
|
|
||||||
|
fpga_compress: $(OBJDIR)/fpga_compress.o $(ZLIBOBJS)
|
||||||
|
$(CXX) $(CXXFLAGS) $(ZLIB_FLAGS) $^ $(LDLIBS) -o $@
|
||||||
|
|
||||||
$(OBJDIR)/%.o: %.c
|
$(OBJDIR)/%.o: %.c
|
||||||
$(CC) $(CFLAGS) -c -o $@ $<
|
$(CC) $(CFLAGS) $(ZLIB_FLAGS) -c -o $@ $<
|
||||||
|
|
||||||
$(OBJDIR)/%.o: %.cpp
|
$(OBJDIR)/%.o: %.cpp
|
||||||
$(CXX) $(CXXFLAGS) -c -o $@ $<
|
$(CXX) $(CXXFLAGS) -c -o $@ $<
|
||||||
|
|
|
@ -23,9 +23,11 @@
|
||||||
|
|
||||||
static int CmdHelp(const char *Cmd);
|
static int CmdHelp(const char *Cmd);
|
||||||
|
|
||||||
static void lookupChipID(uint32_t iChipID)
|
static void lookupChipID(uint32_t iChipID, uint32_t mem_used)
|
||||||
{
|
{
|
||||||
char asBuff[100];
|
char asBuff[100];
|
||||||
|
uint32_t mem_avail = 0;
|
||||||
|
|
||||||
switch(iChipID)
|
switch(iChipID)
|
||||||
{
|
{
|
||||||
case 0x270B0A40:
|
case 0x270B0A40:
|
||||||
|
@ -103,37 +105,43 @@ static void lookupChipID(uint32_t iChipID)
|
||||||
switch((iChipID&0xF00)>>8)
|
switch((iChipID&0xF00)>>8)
|
||||||
{
|
{
|
||||||
case 0:
|
case 0:
|
||||||
sprintf(asBuff,"None");
|
mem_avail = 0;
|
||||||
break;
|
break;
|
||||||
case 1:
|
case 1:
|
||||||
sprintf(asBuff,"8K bytes");
|
mem_avail = 8;
|
||||||
break;
|
break;
|
||||||
case 2:
|
case 2:
|
||||||
sprintf(asBuff,"16K bytes");
|
mem_avail = 16;
|
||||||
break;
|
break;
|
||||||
case 3:
|
case 3:
|
||||||
sprintf(asBuff,"32K bytes");
|
mem_avail = 32;
|
||||||
break;
|
break;
|
||||||
case 5:
|
case 5:
|
||||||
sprintf(asBuff,"64K bytes");
|
mem_avail = 64;
|
||||||
break;
|
break;
|
||||||
case 7:
|
case 7:
|
||||||
sprintf(asBuff,"128K bytes");
|
mem_avail = 128;
|
||||||
break;
|
break;
|
||||||
case 9:
|
case 9:
|
||||||
sprintf(asBuff,"256K bytes");
|
mem_avail = 256;
|
||||||
break;
|
break;
|
||||||
case 10:
|
case 10:
|
||||||
sprintf(asBuff,"512K bytes");
|
mem_avail = 512;
|
||||||
break;
|
break;
|
||||||
case 12:
|
case 12:
|
||||||
sprintf(asBuff,"1024K bytes");
|
mem_avail = 1024;
|
||||||
break;
|
break;
|
||||||
case 14:
|
case 14:
|
||||||
sprintf(asBuff,"2048K bytes");
|
mem_avail = 2048;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
PrintAndLog("Nonvolatile Program Memory Size: %s",asBuff);
|
PrintAndLog("Nonvolatile Program Memory Size: %dK bytes. Used: %d bytes (%2.0f\%). Free: %d bytes (%2.0f\%).",
|
||||||
|
mem_avail,
|
||||||
|
mem_used,
|
||||||
|
mem_avail == 0 ? 0 : (float)mem_used/(mem_avail*1024)*100,
|
||||||
|
mem_avail*1024 - mem_used,
|
||||||
|
mem_avail == 0 ? 0 : (float)(mem_avail*1024-mem_used)/(mem_avail*1024)*100
|
||||||
|
);
|
||||||
switch((iChipID&0xF000)>>12)
|
switch((iChipID&0xF000)>>12)
|
||||||
{
|
{
|
||||||
case 0:
|
case 0:
|
||||||
|
@ -396,13 +404,24 @@ int CmdTune(const char *Cmd)
|
||||||
|
|
||||||
int CmdVersion(const char *Cmd)
|
int CmdVersion(const char *Cmd)
|
||||||
{
|
{
|
||||||
UsbCommand c = {CMD_VERSION};
|
|
||||||
UsbCommand resp;
|
UsbCommand c = {CMD_VERSION};
|
||||||
SendCommand(&c);
|
static UsbCommand resp = {0, {0, 0, 0}};
|
||||||
if (WaitForResponseTimeout(CMD_ACK,&resp,1000)) {
|
|
||||||
lookupChipID(resp.arg[0]);
|
if (resp.arg[0] == 0 && resp.arg[1] == 0) { // no cached information available
|
||||||
}
|
SendCommand(&c);
|
||||||
return 0;
|
if (WaitForResponseTimeout(CMD_ACK,&resp,1000) && Cmd != NULL) {
|
||||||
|
PrintAndLog("Prox/RFID mark3 RFID instrument");
|
||||||
|
PrintAndLog((char*)resp.d.asBytes);
|
||||||
|
lookupChipID(resp.arg[0], resp.arg[1]);
|
||||||
|
}
|
||||||
|
} else if (Cmd != NULL) {
|
||||||
|
PrintAndLog("Prox/RFID mark3 RFID instrument");
|
||||||
|
PrintAndLog((char*)resp.d.asBytes);
|
||||||
|
lookupChipID(resp.arg[0], resp.arg[1]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static command_t CommandTable[] =
|
static command_t CommandTable[] =
|
||||||
|
|
285
client/fpga_compress.c
Normal file
285
client/fpga_compress.c
Normal file
|
@ -0,0 +1,285 @@
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
// This code is licensed to you under the terms of the GNU GPL, version 2 or,
|
||||||
|
// at your option, any later version. See the LICENSE.txt file for the text of
|
||||||
|
// the license.
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
// Compression tool for FPGA config files. Compress several *.bit files at
|
||||||
|
// compile time. Decompression is done at run time (see fpgaloader.c).
|
||||||
|
// This uses the zlib library tuned to this specific case. The small file sizes
|
||||||
|
// allow to use "insane" parameters for optimum compression ratio.
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include "zlib.h"
|
||||||
|
|
||||||
|
#define MAX(a,b) ((a)>(b)?(a):(b))
|
||||||
|
|
||||||
|
// zlib configuration
|
||||||
|
#define COMPRESS_LEVEL 9 // use best possible compression
|
||||||
|
#define COMPRESS_WINDOW_BITS 15 // default = max = 15 for a window of 2^15 = 32KBytes
|
||||||
|
#define COMPRESS_MEM_LEVEL 9 // determines the amount of memory allocated during compression. Default = 8.
|
||||||
|
/* COMPRESS_STRATEGY can be
|
||||||
|
Z_DEFAULT_STRATEGY (the default),
|
||||||
|
Z_FILTERED (more huffmann, less string matching),
|
||||||
|
Z_HUFFMAN_ONLY (huffman only, no string matching)
|
||||||
|
Z_RLE (distances limited to one)
|
||||||
|
Z_FIXED (prevents the use of dynamic Huffman codes)
|
||||||
|
*/
|
||||||
|
#define COMPRESS_STRATEGY Z_DEFAULT_STRATEGY
|
||||||
|
// zlib tuning parameters:
|
||||||
|
#define COMPRESS_GOOD_LENGTH 258
|
||||||
|
#define COMPRESS_MAX_LAZY 258
|
||||||
|
#define COMPRESS_MAX_NICE_LENGTH 258
|
||||||
|
#define COMPRESS_MAX_CHAIN 8192
|
||||||
|
|
||||||
|
#define FPGA_INTERLEAVE_SIZE 288 // (the FPGA's internal config frame size is 288 bits. Interleaving with 288 bytes should give best compression)
|
||||||
|
#define FPGA_CONFIG_SIZE 42336 // our current fpga_[lh]f.bit files are 42175 bytes. Rounded up to next multiple of FPGA_INTERLEAVE_SIZE
|
||||||
|
|
||||||
|
static void usage(void)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Usage: fpga_compress <infile1> <infile2> ... <infile_n> <outfile>\n");
|
||||||
|
fprintf(stderr, " Combine n FPGA bitstream files and compress them into one.\n\n");
|
||||||
|
fprintf(stderr, " fpga_compress -d <infile> <outfile>");
|
||||||
|
fprintf(stderr, " Decompress <infile>. Write result to <outfile>");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static voidpf fpga_deflate_malloc(voidpf opaque, uInt items, uInt size)
|
||||||
|
{
|
||||||
|
return malloc(items*size);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void fpga_deflate_free(voidpf opaque, voidpf address)
|
||||||
|
{
|
||||||
|
return free(address);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static bool all_feof(FILE *infile[], uint8_t num_infiles)
|
||||||
|
{
|
||||||
|
for (uint16_t i = 0; i < num_infiles; i++) {
|
||||||
|
if (!feof(infile[i])) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int zlib_compress(FILE *infile[], uint8_t num_infiles, FILE *outfile)
|
||||||
|
{
|
||||||
|
uint8_t *fpga_config;
|
||||||
|
uint32_t i;
|
||||||
|
int ret;
|
||||||
|
uint8_t c;
|
||||||
|
z_stream compressed_fpga_stream;
|
||||||
|
|
||||||
|
fpga_config = malloc(num_infiles * FPGA_CONFIG_SIZE);
|
||||||
|
|
||||||
|
// read the input files. Interleave them into fpga_config[]
|
||||||
|
i = 0;
|
||||||
|
do {
|
||||||
|
for(uint16_t j = 0; j < num_infiles; j++) {
|
||||||
|
for(uint16_t k = 0; k < FPGA_INTERLEAVE_SIZE; k++) {
|
||||||
|
c = fgetc(infile[j]);
|
||||||
|
if (!feof(infile[j])) {
|
||||||
|
fpga_config[i++] = c;
|
||||||
|
} else if (num_infiles > 1) {
|
||||||
|
fpga_config[i++] = '\0';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (i >= num_infiles * FPGA_CONFIG_SIZE) {
|
||||||
|
fprintf(stderr, "Input files too big (total > %lu bytes). These are probably not PM3 FPGA config files.", num_infiles*FPGA_CONFIG_SIZE);
|
||||||
|
for(uint16_t j = 0; j < num_infiles; j++) {
|
||||||
|
fclose(infile[j]);
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
} while (!all_feof(infile, num_infiles));
|
||||||
|
|
||||||
|
// initialize zlib structures
|
||||||
|
compressed_fpga_stream.next_in = fpga_config;
|
||||||
|
compressed_fpga_stream.avail_in = i;
|
||||||
|
compressed_fpga_stream.zalloc = fpga_deflate_malloc;
|
||||||
|
compressed_fpga_stream.zfree = fpga_deflate_free;
|
||||||
|
|
||||||
|
ret = deflateInit2(&compressed_fpga_stream,
|
||||||
|
COMPRESS_LEVEL,
|
||||||
|
Z_DEFLATED,
|
||||||
|
COMPRESS_WINDOW_BITS,
|
||||||
|
COMPRESS_MEM_LEVEL,
|
||||||
|
COMPRESS_STRATEGY);
|
||||||
|
|
||||||
|
// estimate the size of the compressed output
|
||||||
|
unsigned int outsize_max = deflateBound(&compressed_fpga_stream, compressed_fpga_stream.avail_in);
|
||||||
|
uint8_t *outbuf = malloc(outsize_max);
|
||||||
|
compressed_fpga_stream.next_out = outbuf;
|
||||||
|
compressed_fpga_stream.avail_out = outsize_max;
|
||||||
|
|
||||||
|
if (ret == Z_OK) {
|
||||||
|
ret = deflateTune(&compressed_fpga_stream,
|
||||||
|
COMPRESS_GOOD_LENGTH,
|
||||||
|
COMPRESS_MAX_LAZY,
|
||||||
|
COMPRESS_MAX_NICE_LENGTH,
|
||||||
|
COMPRESS_MAX_CHAIN);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ret == Z_OK) {
|
||||||
|
ret = deflate(&compressed_fpga_stream, Z_FINISH);
|
||||||
|
}
|
||||||
|
|
||||||
|
fprintf(stderr, "compressed %lu input bytes to %lu output bytes\n", i, compressed_fpga_stream.total_out);
|
||||||
|
|
||||||
|
if (ret != Z_STREAM_END) {
|
||||||
|
fprintf(stderr, "Error in deflate(): %d %s\n", ret, compressed_fpga_stream.msg);
|
||||||
|
free(outbuf);
|
||||||
|
deflateEnd(&compressed_fpga_stream);
|
||||||
|
for(uint16_t j = 0; j < num_infiles; j++) {
|
||||||
|
fclose(infile[j]);
|
||||||
|
}
|
||||||
|
fclose(outfile);
|
||||||
|
free(infile);
|
||||||
|
free(fpga_config);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (i = 0; i < compressed_fpga_stream.total_out; i++) {
|
||||||
|
fputc(outbuf[i], outfile);
|
||||||
|
}
|
||||||
|
|
||||||
|
free(outbuf);
|
||||||
|
deflateEnd(&compressed_fpga_stream);
|
||||||
|
for(uint16_t j = 0; j < num_infiles; j++) {
|
||||||
|
fclose(infile[j]);
|
||||||
|
}
|
||||||
|
fclose(outfile);
|
||||||
|
free(infile);
|
||||||
|
free(fpga_config);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int zlib_decompress(FILE *infile, FILE *outfile)
|
||||||
|
{
|
||||||
|
#define DECOMPRESS_BUF_SIZE 1024
|
||||||
|
uint8_t outbuf[DECOMPRESS_BUF_SIZE];
|
||||||
|
uint8_t inbuf[DECOMPRESS_BUF_SIZE];
|
||||||
|
int ret;
|
||||||
|
|
||||||
|
z_stream compressed_fpga_stream;
|
||||||
|
|
||||||
|
// initialize zlib structures
|
||||||
|
compressed_fpga_stream.next_in = inbuf;
|
||||||
|
compressed_fpga_stream.avail_in = 0;
|
||||||
|
compressed_fpga_stream.next_out = outbuf;
|
||||||
|
compressed_fpga_stream.avail_out = DECOMPRESS_BUF_SIZE;
|
||||||
|
compressed_fpga_stream.zalloc = fpga_deflate_malloc;
|
||||||
|
compressed_fpga_stream.zfree = fpga_deflate_free;
|
||||||
|
|
||||||
|
ret = inflateInit2(&compressed_fpga_stream, 0);
|
||||||
|
|
||||||
|
do {
|
||||||
|
if (compressed_fpga_stream.avail_in == 0) {
|
||||||
|
compressed_fpga_stream.next_in = inbuf;
|
||||||
|
uint16_t i = 0;
|
||||||
|
do {
|
||||||
|
uint8_t c = fgetc(infile);
|
||||||
|
if (!feof(infile)) {
|
||||||
|
inbuf[i++] = c;
|
||||||
|
compressed_fpga_stream.avail_in++;
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} while (i < DECOMPRESS_BUF_SIZE);
|
||||||
|
}
|
||||||
|
|
||||||
|
ret = inflate(&compressed_fpga_stream, Z_SYNC_FLUSH);
|
||||||
|
|
||||||
|
if (ret != Z_OK && ret != Z_STREAM_END) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (compressed_fpga_stream.avail_out == 0) {
|
||||||
|
for (uint16_t i = 0; i < DECOMPRESS_BUF_SIZE; i++) {
|
||||||
|
fputc(outbuf[i], outfile);
|
||||||
|
}
|
||||||
|
compressed_fpga_stream.avail_out = DECOMPRESS_BUF_SIZE;
|
||||||
|
compressed_fpga_stream.next_out = outbuf;
|
||||||
|
}
|
||||||
|
} while (ret == Z_OK);
|
||||||
|
|
||||||
|
if (ret == Z_STREAM_END) { // reached end of input
|
||||||
|
uint16_t i = 0;
|
||||||
|
while (compressed_fpga_stream.avail_out < DECOMPRESS_BUF_SIZE) {
|
||||||
|
fputc(outbuf[i++], outfile);
|
||||||
|
compressed_fpga_stream.avail_out++;
|
||||||
|
}
|
||||||
|
fclose(outfile);
|
||||||
|
fclose(infile);
|
||||||
|
return 0;
|
||||||
|
} else {
|
||||||
|
fprintf(stderr, "Error. Inflate() returned error %d, %s", ret, compressed_fpga_stream.msg);
|
||||||
|
fclose(outfile);
|
||||||
|
fclose(infile);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int main(int argc, char **argv)
|
||||||
|
{
|
||||||
|
FILE **infiles;
|
||||||
|
FILE *outfile;
|
||||||
|
|
||||||
|
if (argc == 1 || argc == 2) {
|
||||||
|
usage();
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!strcmp(argv[1], "-d")) { // Decompress
|
||||||
|
infiles = calloc(1, sizeof(FILE*));
|
||||||
|
if (argc != 4) {
|
||||||
|
usage();
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
infiles[0] = fopen(argv[2], "rb");
|
||||||
|
if (infiles[0] == NULL) {
|
||||||
|
fprintf(stderr, "Error. Cannot open input file %s", argv[2]);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
outfile = fopen(argv[3], "wb");
|
||||||
|
if (outfile == NULL) {
|
||||||
|
fprintf(stderr, "Error. Cannot open output file %s", argv[3]);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
return zlib_decompress(infiles[0], outfile);
|
||||||
|
|
||||||
|
} else { // Compress
|
||||||
|
|
||||||
|
infiles = calloc(argc-2, sizeof(FILE*));
|
||||||
|
for (uint16_t i = 0; i < argc-2; i++) {
|
||||||
|
infiles[i] = fopen(argv[i+1], "rb");
|
||||||
|
if (infiles[i] == NULL) {
|
||||||
|
fprintf(stderr, "Error. Cannot open input file %s", argv[i+1]);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
outfile = fopen(argv[argc-1], "wb");
|
||||||
|
if (outfile == NULL) {
|
||||||
|
fprintf(stderr, "Error. Cannot open output file %s", argv[argc-1]);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
return zlib_compress(infiles, argc-2, outfile);
|
||||||
|
}
|
||||||
|
}
|
|
@ -24,7 +24,7 @@
|
||||||
#include "ui.h"
|
#include "ui.h"
|
||||||
#include "sleep.h"
|
#include "sleep.h"
|
||||||
#include "cmdparser.h"
|
#include "cmdparser.h"
|
||||||
#include "cmdmain.h"
|
#include "cmdhw.h"
|
||||||
|
|
||||||
// a global mutex to prevent interlaced printing from different threads
|
// a global mutex to prevent interlaced printing from different threads
|
||||||
pthread_mutex_t print_lock;
|
pthread_mutex_t print_lock;
|
||||||
|
@ -105,6 +105,8 @@ static void *main_loop(void *targ) {
|
||||||
if (arg->usb_present == 1) {
|
if (arg->usb_present == 1) {
|
||||||
rarg.run = 1;
|
rarg.run = 1;
|
||||||
pthread_create(&reader_thread, NULL, &uart_receiver, &rarg);
|
pthread_create(&reader_thread, NULL, &uart_receiver, &rarg);
|
||||||
|
// cache Version information now:
|
||||||
|
CmdVersion(NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
FILE *script_file = NULL;
|
FILE *script_file = NULL;
|
||||||
|
|
|
@ -25,6 +25,7 @@ CC = $(CROSS)gcc
|
||||||
AS = $(CROSS)as
|
AS = $(CROSS)as
|
||||||
LD = $(CROSS)ld
|
LD = $(CROSS)ld
|
||||||
OBJCOPY = $(CROSS)objcopy
|
OBJCOPY = $(CROSS)objcopy
|
||||||
|
GZIP=gzip
|
||||||
|
|
||||||
OBJDIR = obj
|
OBJDIR = obj
|
||||||
|
|
||||||
|
@ -61,8 +62,8 @@ DETECTED_OS=Windows
|
||||||
endif
|
endif
|
||||||
|
|
||||||
|
|
||||||
# Also search prerequisites in the common directory (for usb.c), and the fpga directory (for fpga.bit)
|
# Also search prerequisites in the common directory (for usb.c), the fpga directory (for fpga.bit), and the zlib directory
|
||||||
VPATH = . ../common/ ../fpga/
|
VPATH = . ../common ../fpga ../zlib
|
||||||
|
|
||||||
INCLUDES = ../include/proxmark3.h ../include/at91sam7s512.h ../include/config_gpio.h ../include/usb_cmd.h $(APP_INCLUDES)
|
INCLUDES = ../include/proxmark3.h ../include/at91sam7s512.h ../include/config_gpio.h ../include/usb_cmd.h $(APP_INCLUDES)
|
||||||
|
|
||||||
|
@ -71,9 +72,9 @@ LDFLAGS = -nostartfiles -nodefaultlibs -Wl,-gc-sections -n
|
||||||
|
|
||||||
LIBS = -lgcc
|
LIBS = -lgcc
|
||||||
|
|
||||||
THUMBOBJ = $(patsubst %.c,$(OBJDIR)/%.o,$(THUMBSRC))
|
THUMBOBJ = $(patsubst %.c,$(OBJDIR)/%.o,$(notdir $(THUMBSRC)))
|
||||||
ARMOBJ = $(ARMSRC:%.c=$(OBJDIR)/%.o)
|
ARMOBJ = $(patsubst %.c,$(OBJDIR)/%.o,$(notdir $(ARMSRC)))
|
||||||
ASMOBJ = $(patsubst %.s,$(OBJDIR)/%.o,$(ASMSRC))
|
ASMOBJ = $(patsubst %.s,$(OBJDIR)/%.o,$(notdir $(ASMSRC)))
|
||||||
VERSIONOBJ = $(OBJDIR)/version.o
|
VERSIONOBJ = $(OBJDIR)/version.o
|
||||||
|
|
||||||
$(THUMBOBJ): $(OBJDIR)/%.o: %.c $(INCLUDES)
|
$(THUMBOBJ): $(OBJDIR)/%.o: %.c $(INCLUDES)
|
||||||
|
@ -109,6 +110,7 @@ DEPENDENCY_FILES = $(patsubst %.c,$(OBJDIR)/%.d,$(notdir $(THUMBSRC))) \
|
||||||
$(patsubst %.s,$(OBJDIR)/%.d,$(notdir $(ASMSRC)))
|
$(patsubst %.s,$(OBJDIR)/%.d,$(notdir $(ASMSRC)))
|
||||||
|
|
||||||
$(DEPENDENCY_FILES): Makefile ../common/Makefile.common
|
$(DEPENDENCY_FILES): Makefile ../common/Makefile.common
|
||||||
|
|
||||||
$(patsubst %.o,%.d,$(THUMBOBJ) $(ARMOBJ)): $(OBJDIR)/%.d: %.c
|
$(patsubst %.o,%.d,$(THUMBOBJ) $(ARMOBJ)): $(OBJDIR)/%.d: %.c
|
||||||
@$(CC) -MM -MT "$(@) $(@:.d=.o)" $(CFLAGS) $< > $@
|
@$(CC) -MM -MT "$(@) $(@:.d=.o)" $(CFLAGS) $< > $@
|
||||||
$(patsubst %.o,%.d,$(ASMOBJ)):$(OBJDIR)/%.d: %.s
|
$(patsubst %.o,%.d,$(ASMOBJ)):$(OBJDIR)/%.d: %.s
|
||||||
|
|
|
@ -61,10 +61,6 @@
|
||||||
#define SPI_FPGA_MODE 0
|
#define SPI_FPGA_MODE 0
|
||||||
#define SPI_LCD_MODE 1
|
#define SPI_LCD_MODE 1
|
||||||
|
|
||||||
#define FPGA_BITSTREAM_ERR 0
|
|
||||||
#define FPGA_BITSTREAM_LF 1
|
|
||||||
#define FPGA_BITSTREAM_HF 2
|
|
||||||
|
|
||||||
#define TRUE 1
|
#define TRUE 1
|
||||||
#define FALSE 0
|
#define FALSE 0
|
||||||
|
|
||||||
|
|
1481
zlib/ChangeLog
Normal file
1481
zlib/ChangeLog
Normal file
File diff suppressed because it is too large
Load diff
368
zlib/FAQ
Normal file
368
zlib/FAQ
Normal file
|
@ -0,0 +1,368 @@
|
||||||
|
|
||||||
|
Frequently Asked Questions about zlib
|
||||||
|
|
||||||
|
|
||||||
|
If your question is not there, please check the zlib home page
|
||||||
|
http://zlib.net/ which may have more recent information.
|
||||||
|
The lastest zlib FAQ is at http://zlib.net/zlib_faq.html
|
||||||
|
|
||||||
|
|
||||||
|
1. Is zlib Y2K-compliant?
|
||||||
|
|
||||||
|
Yes. zlib doesn't handle dates.
|
||||||
|
|
||||||
|
2. Where can I get a Windows DLL version?
|
||||||
|
|
||||||
|
The zlib sources can be compiled without change to produce a DLL. See the
|
||||||
|
file win32/DLL_FAQ.txt in the zlib distribution. Pointers to the
|
||||||
|
precompiled DLL are found in the zlib web site at http://zlib.net/ .
|
||||||
|
|
||||||
|
3. Where can I get a Visual Basic interface to zlib?
|
||||||
|
|
||||||
|
See
|
||||||
|
* http://marknelson.us/1997/01/01/zlib-engine/
|
||||||
|
* win32/DLL_FAQ.txt in the zlib distribution
|
||||||
|
|
||||||
|
4. compress() returns Z_BUF_ERROR.
|
||||||
|
|
||||||
|
Make sure that before the call of compress(), the length of the compressed
|
||||||
|
buffer is equal to the available size of the compressed buffer and not
|
||||||
|
zero. For Visual Basic, check that this parameter is passed by reference
|
||||||
|
("as any"), not by value ("as long").
|
||||||
|
|
||||||
|
5. deflate() or inflate() returns Z_BUF_ERROR.
|
||||||
|
|
||||||
|
Before making the call, make sure that avail_in and avail_out are not zero.
|
||||||
|
When setting the parameter flush equal to Z_FINISH, also make sure that
|
||||||
|
avail_out is big enough to allow processing all pending input. Note that a
|
||||||
|
Z_BUF_ERROR is not fatal--another call to deflate() or inflate() can be
|
||||||
|
made with more input or output space. A Z_BUF_ERROR may in fact be
|
||||||
|
unavoidable depending on how the functions are used, since it is not
|
||||||
|
possible to tell whether or not there is more output pending when
|
||||||
|
strm.avail_out returns with zero. See http://zlib.net/zlib_how.html for a
|
||||||
|
heavily annotated example.
|
||||||
|
|
||||||
|
6. Where's the zlib documentation (man pages, etc.)?
|
||||||
|
|
||||||
|
It's in zlib.h . Examples of zlib usage are in the files test/example.c
|
||||||
|
and test/minigzip.c, with more in examples/ .
|
||||||
|
|
||||||
|
7. Why don't you use GNU autoconf or libtool or ...?
|
||||||
|
|
||||||
|
Because we would like to keep zlib as a very small and simple package.
|
||||||
|
zlib is rather portable and doesn't need much configuration.
|
||||||
|
|
||||||
|
8. I found a bug in zlib.
|
||||||
|
|
||||||
|
Most of the time, such problems are due to an incorrect usage of zlib.
|
||||||
|
Please try to reproduce the problem with a small program and send the
|
||||||
|
corresponding source to us at zlib@gzip.org . Do not send multi-megabyte
|
||||||
|
data files without prior agreement.
|
||||||
|
|
||||||
|
9. Why do I get "undefined reference to gzputc"?
|
||||||
|
|
||||||
|
If "make test" produces something like
|
||||||
|
|
||||||
|
example.o(.text+0x154): undefined reference to `gzputc'
|
||||||
|
|
||||||
|
check that you don't have old files libz.* in /usr/lib, /usr/local/lib or
|
||||||
|
/usr/X11R6/lib. Remove any old versions, then do "make install".
|
||||||
|
|
||||||
|
10. I need a Delphi interface to zlib.
|
||||||
|
|
||||||
|
See the contrib/delphi directory in the zlib distribution.
|
||||||
|
|
||||||
|
11. Can zlib handle .zip archives?
|
||||||
|
|
||||||
|
Not by itself, no. See the directory contrib/minizip in the zlib
|
||||||
|
distribution.
|
||||||
|
|
||||||
|
12. Can zlib handle .Z files?
|
||||||
|
|
||||||
|
No, sorry. You have to spawn an uncompress or gunzip subprocess, or adapt
|
||||||
|
the code of uncompress on your own.
|
||||||
|
|
||||||
|
13. How can I make a Unix shared library?
|
||||||
|
|
||||||
|
By default a shared (and a static) library is built for Unix. So:
|
||||||
|
|
||||||
|
make distclean
|
||||||
|
./configure
|
||||||
|
make
|
||||||
|
|
||||||
|
14. How do I install a shared zlib library on Unix?
|
||||||
|
|
||||||
|
After the above, then:
|
||||||
|
|
||||||
|
make install
|
||||||
|
|
||||||
|
However, many flavors of Unix come with a shared zlib already installed.
|
||||||
|
Before going to the trouble of compiling a shared version of zlib and
|
||||||
|
trying to install it, you may want to check if it's already there! If you
|
||||||
|
can #include <zlib.h>, it's there. The -lz option will probably link to
|
||||||
|
it. You can check the version at the top of zlib.h or with the
|
||||||
|
ZLIB_VERSION symbol defined in zlib.h .
|
||||||
|
|
||||||
|
15. I have a question about OttoPDF.
|
||||||
|
|
||||||
|
We are not the authors of OttoPDF. The real author is on the OttoPDF web
|
||||||
|
site: Joel Hainley, jhainley@myndkryme.com.
|
||||||
|
|
||||||
|
16. Can zlib decode Flate data in an Adobe PDF file?
|
||||||
|
|
||||||
|
Yes. See http://www.pdflib.com/ . To modify PDF forms, see
|
||||||
|
http://sourceforge.net/projects/acroformtool/ .
|
||||||
|
|
||||||
|
17. Why am I getting this "register_frame_info not found" error on Solaris?
|
||||||
|
|
||||||
|
After installing zlib 1.1.4 on Solaris 2.6, running applications using zlib
|
||||||
|
generates an error such as:
|
||||||
|
|
||||||
|
ld.so.1: rpm: fatal: relocation error: file /usr/local/lib/libz.so:
|
||||||
|
symbol __register_frame_info: referenced symbol not found
|
||||||
|
|
||||||
|
The symbol __register_frame_info is not part of zlib, it is generated by
|
||||||
|
the C compiler (cc or gcc). You must recompile applications using zlib
|
||||||
|
which have this problem. This problem is specific to Solaris. See
|
||||||
|
http://www.sunfreeware.com for Solaris versions of zlib and applications
|
||||||
|
using zlib.
|
||||||
|
|
||||||
|
18. Why does gzip give an error on a file I make with compress/deflate?
|
||||||
|
|
||||||
|
The compress and deflate functions produce data in the zlib format, which
|
||||||
|
is different and incompatible with the gzip format. The gz* functions in
|
||||||
|
zlib on the other hand use the gzip format. Both the zlib and gzip formats
|
||||||
|
use the same compressed data format internally, but have different headers
|
||||||
|
and trailers around the compressed data.
|
||||||
|
|
||||||
|
19. Ok, so why are there two different formats?
|
||||||
|
|
||||||
|
The gzip format was designed to retain the directory information about a
|
||||||
|
single file, such as the name and last modification date. The zlib format
|
||||||
|
on the other hand was designed for in-memory and communication channel
|
||||||
|
applications, and has a much more compact header and trailer and uses a
|
||||||
|
faster integrity check than gzip.
|
||||||
|
|
||||||
|
20. Well that's nice, but how do I make a gzip file in memory?
|
||||||
|
|
||||||
|
You can request that deflate write the gzip format instead of the zlib
|
||||||
|
format using deflateInit2(). You can also request that inflate decode the
|
||||||
|
gzip format using inflateInit2(). Read zlib.h for more details.
|
||||||
|
|
||||||
|
21. Is zlib thread-safe?
|
||||||
|
|
||||||
|
Yes. However any library routines that zlib uses and any application-
|
||||||
|
provided memory allocation routines must also be thread-safe. zlib's gz*
|
||||||
|
functions use stdio library routines, and most of zlib's functions use the
|
||||||
|
library memory allocation routines by default. zlib's *Init* functions
|
||||||
|
allow for the application to provide custom memory allocation routines.
|
||||||
|
|
||||||
|
Of course, you should only operate on any given zlib or gzip stream from a
|
||||||
|
single thread at a time.
|
||||||
|
|
||||||
|
22. Can I use zlib in my commercial application?
|
||||||
|
|
||||||
|
Yes. Please read the license in zlib.h.
|
||||||
|
|
||||||
|
23. Is zlib under the GNU license?
|
||||||
|
|
||||||
|
No. Please read the license in zlib.h.
|
||||||
|
|
||||||
|
24. The license says that altered source versions must be "plainly marked". So
|
||||||
|
what exactly do I need to do to meet that requirement?
|
||||||
|
|
||||||
|
You need to change the ZLIB_VERSION and ZLIB_VERNUM #defines in zlib.h. In
|
||||||
|
particular, the final version number needs to be changed to "f", and an
|
||||||
|
identification string should be appended to ZLIB_VERSION. Version numbers
|
||||||
|
x.x.x.f are reserved for modifications to zlib by others than the zlib
|
||||||
|
maintainers. For example, if the version of the base zlib you are altering
|
||||||
|
is "1.2.3.4", then in zlib.h you should change ZLIB_VERNUM to 0x123f, and
|
||||||
|
ZLIB_VERSION to something like "1.2.3.f-zachary-mods-v3". You can also
|
||||||
|
update the version strings in deflate.c and inftrees.c.
|
||||||
|
|
||||||
|
For altered source distributions, you should also note the origin and
|
||||||
|
nature of the changes in zlib.h, as well as in ChangeLog and README, along
|
||||||
|
with the dates of the alterations. The origin should include at least your
|
||||||
|
name (or your company's name), and an email address to contact for help or
|
||||||
|
issues with the library.
|
||||||
|
|
||||||
|
Note that distributing a compiled zlib library along with zlib.h and
|
||||||
|
zconf.h is also a source distribution, and so you should change
|
||||||
|
ZLIB_VERSION and ZLIB_VERNUM and note the origin and nature of the changes
|
||||||
|
in zlib.h as you would for a full source distribution.
|
||||||
|
|
||||||
|
25. Will zlib work on a big-endian or little-endian architecture, and can I
|
||||||
|
exchange compressed data between them?
|
||||||
|
|
||||||
|
Yes and yes.
|
||||||
|
|
||||||
|
26. Will zlib work on a 64-bit machine?
|
||||||
|
|
||||||
|
Yes. It has been tested on 64-bit machines, and has no dependence on any
|
||||||
|
data types being limited to 32-bits in length. If you have any
|
||||||
|
difficulties, please provide a complete problem report to zlib@gzip.org
|
||||||
|
|
||||||
|
27. Will zlib decompress data from the PKWare Data Compression Library?
|
||||||
|
|
||||||
|
No. The PKWare DCL uses a completely different compressed data format than
|
||||||
|
does PKZIP and zlib. However, you can look in zlib's contrib/blast
|
||||||
|
directory for a possible solution to your problem.
|
||||||
|
|
||||||
|
28. Can I access data randomly in a compressed stream?
|
||||||
|
|
||||||
|
No, not without some preparation. If when compressing you periodically use
|
||||||
|
Z_FULL_FLUSH, carefully write all the pending data at those points, and
|
||||||
|
keep an index of those locations, then you can start decompression at those
|
||||||
|
points. You have to be careful to not use Z_FULL_FLUSH too often, since it
|
||||||
|
can significantly degrade compression. Alternatively, you can scan a
|
||||||
|
deflate stream once to generate an index, and then use that index for
|
||||||
|
random access. See examples/zran.c .
|
||||||
|
|
||||||
|
29. Does zlib work on MVS, OS/390, CICS, etc.?
|
||||||
|
|
||||||
|
It has in the past, but we have not heard of any recent evidence. There
|
||||||
|
were working ports of zlib 1.1.4 to MVS, but those links no longer work.
|
||||||
|
If you know of recent, successful applications of zlib on these operating
|
||||||
|
systems, please let us know. Thanks.
|
||||||
|
|
||||||
|
30. Is there some simpler, easier to read version of inflate I can look at to
|
||||||
|
understand the deflate format?
|
||||||
|
|
||||||
|
First off, you should read RFC 1951. Second, yes. Look in zlib's
|
||||||
|
contrib/puff directory.
|
||||||
|
|
||||||
|
31. Does zlib infringe on any patents?
|
||||||
|
|
||||||
|
As far as we know, no. In fact, that was originally the whole point behind
|
||||||
|
zlib. Look here for some more information:
|
||||||
|
|
||||||
|
http://www.gzip.org/#faq11
|
||||||
|
|
||||||
|
32. Can zlib work with greater than 4 GB of data?
|
||||||
|
|
||||||
|
Yes. inflate() and deflate() will process any amount of data correctly.
|
||||||
|
Each call of inflate() or deflate() is limited to input and output chunks
|
||||||
|
of the maximum value that can be stored in the compiler's "unsigned int"
|
||||||
|
type, but there is no limit to the number of chunks. Note however that the
|
||||||
|
strm.total_in and strm_total_out counters may be limited to 4 GB. These
|
||||||
|
counters are provided as a convenience and are not used internally by
|
||||||
|
inflate() or deflate(). The application can easily set up its own counters
|
||||||
|
updated after each call of inflate() or deflate() to count beyond 4 GB.
|
||||||
|
compress() and uncompress() may be limited to 4 GB, since they operate in a
|
||||||
|
single call. gzseek() and gztell() may be limited to 4 GB depending on how
|
||||||
|
zlib is compiled. See the zlibCompileFlags() function in zlib.h.
|
||||||
|
|
||||||
|
The word "may" appears several times above since there is a 4 GB limit only
|
||||||
|
if the compiler's "long" type is 32 bits. If the compiler's "long" type is
|
||||||
|
64 bits, then the limit is 16 exabytes.
|
||||||
|
|
||||||
|
33. Does zlib have any security vulnerabilities?
|
||||||
|
|
||||||
|
The only one that we are aware of is potentially in gzprintf(). If zlib is
|
||||||
|
compiled to use sprintf() or vsprintf(), then there is no protection
|
||||||
|
against a buffer overflow of an 8K string space (or other value as set by
|
||||||
|
gzbuffer()), other than the caller of gzprintf() assuring that the output
|
||||||
|
will not exceed 8K. On the other hand, if zlib is compiled to use
|
||||||
|
snprintf() or vsnprintf(), which should normally be the case, then there is
|
||||||
|
no vulnerability. The ./configure script will display warnings if an
|
||||||
|
insecure variation of sprintf() will be used by gzprintf(). Also the
|
||||||
|
zlibCompileFlags() function will return information on what variant of
|
||||||
|
sprintf() is used by gzprintf().
|
||||||
|
|
||||||
|
If you don't have snprintf() or vsnprintf() and would like one, you can
|
||||||
|
find a portable implementation here:
|
||||||
|
|
||||||
|
http://www.ijs.si/software/snprintf/
|
||||||
|
|
||||||
|
Note that you should be using the most recent version of zlib. Versions
|
||||||
|
1.1.3 and before were subject to a double-free vulnerability, and versions
|
||||||
|
1.2.1 and 1.2.2 were subject to an access exception when decompressing
|
||||||
|
invalid compressed data.
|
||||||
|
|
||||||
|
34. Is there a Java version of zlib?
|
||||||
|
|
||||||
|
Probably what you want is to use zlib in Java. zlib is already included
|
||||||
|
as part of the Java SDK in the java.util.zip package. If you really want
|
||||||
|
a version of zlib written in the Java language, look on the zlib home
|
||||||
|
page for links: http://zlib.net/ .
|
||||||
|
|
||||||
|
35. I get this or that compiler or source-code scanner warning when I crank it
|
||||||
|
up to maximally-pedantic. Can't you guys write proper code?
|
||||||
|
|
||||||
|
Many years ago, we gave up attempting to avoid warnings on every compiler
|
||||||
|
in the universe. It just got to be a waste of time, and some compilers
|
||||||
|
were downright silly as well as contradicted each other. So now, we simply
|
||||||
|
make sure that the code always works.
|
||||||
|
|
||||||
|
36. Valgrind (or some similar memory access checker) says that deflate is
|
||||||
|
performing a conditional jump that depends on an uninitialized value.
|
||||||
|
Isn't that a bug?
|
||||||
|
|
||||||
|
No. That is intentional for performance reasons, and the output of deflate
|
||||||
|
is not affected. This only started showing up recently since zlib 1.2.x
|
||||||
|
uses malloc() by default for allocations, whereas earlier versions used
|
||||||
|
calloc(), which zeros out the allocated memory. Even though the code was
|
||||||
|
correct, versions 1.2.4 and later was changed to not stimulate these
|
||||||
|
checkers.
|
||||||
|
|
||||||
|
37. Will zlib read the (insert any ancient or arcane format here) compressed
|
||||||
|
data format?
|
||||||
|
|
||||||
|
Probably not. Look in the comp.compression FAQ for pointers to various
|
||||||
|
formats and associated software.
|
||||||
|
|
||||||
|
38. How can I encrypt/decrypt zip files with zlib?
|
||||||
|
|
||||||
|
zlib doesn't support encryption. The original PKZIP encryption is very
|
||||||
|
weak and can be broken with freely available programs. To get strong
|
||||||
|
encryption, use GnuPG, http://www.gnupg.org/ , which already includes zlib
|
||||||
|
compression. For PKZIP compatible "encryption", look at
|
||||||
|
http://www.info-zip.org/
|
||||||
|
|
||||||
|
39. What's the difference between the "gzip" and "deflate" HTTP 1.1 encodings?
|
||||||
|
|
||||||
|
"gzip" is the gzip format, and "deflate" is the zlib format. They should
|
||||||
|
probably have called the second one "zlib" instead to avoid confusion with
|
||||||
|
the raw deflate compressed data format. While the HTTP 1.1 RFC 2616
|
||||||
|
correctly points to the zlib specification in RFC 1950 for the "deflate"
|
||||||
|
transfer encoding, there have been reports of servers and browsers that
|
||||||
|
incorrectly produce or expect raw deflate data per the deflate
|
||||||
|
specification in RFC 1951, most notably Microsoft. So even though the
|
||||||
|
"deflate" transfer encoding using the zlib format would be the more
|
||||||
|
efficient approach (and in fact exactly what the zlib format was designed
|
||||||
|
for), using the "gzip" transfer encoding is probably more reliable due to
|
||||||
|
an unfortunate choice of name on the part of the HTTP 1.1 authors.
|
||||||
|
|
||||||
|
Bottom line: use the gzip format for HTTP 1.1 encoding.
|
||||||
|
|
||||||
|
40. Does zlib support the new "Deflate64" format introduced by PKWare?
|
||||||
|
|
||||||
|
No. PKWare has apparently decided to keep that format proprietary, since
|
||||||
|
they have not documented it as they have previous compression formats. In
|
||||||
|
any case, the compression improvements are so modest compared to other more
|
||||||
|
modern approaches, that it's not worth the effort to implement.
|
||||||
|
|
||||||
|
41. I'm having a problem with the zip functions in zlib, can you help?
|
||||||
|
|
||||||
|
There are no zip functions in zlib. You are probably using minizip by
|
||||||
|
Giles Vollant, which is found in the contrib directory of zlib. It is not
|
||||||
|
part of zlib. In fact none of the stuff in contrib is part of zlib. The
|
||||||
|
files in there are not supported by the zlib authors. You need to contact
|
||||||
|
the authors of the respective contribution for help.
|
||||||
|
|
||||||
|
42. The match.asm code in contrib is under the GNU General Public License.
|
||||||
|
Since it's part of zlib, doesn't that mean that all of zlib falls under the
|
||||||
|
GNU GPL?
|
||||||
|
|
||||||
|
No. The files in contrib are not part of zlib. They were contributed by
|
||||||
|
other authors and are provided as a convenience to the user within the zlib
|
||||||
|
distribution. Each item in contrib has its own license.
|
||||||
|
|
||||||
|
43. Is zlib subject to export controls? What is its ECCN?
|
||||||
|
|
||||||
|
zlib is not subject to export controls, and so is classified as EAR99.
|
||||||
|
|
||||||
|
44. Can you please sign these lengthy legal documents and fax them back to us
|
||||||
|
so that we can use your software in our product?
|
||||||
|
|
||||||
|
No. Go away. Shoo.
|
124
zlib/README
Normal file
124
zlib/README
Normal file
|
@ -0,0 +1,124 @@
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
// This version of zlib is modified for use within the Proxmark3 project.
|
||||||
|
// Files from the original distribution which are not required for this
|
||||||
|
// purpose are not included. All modifications can easily be found
|
||||||
|
// by searching for #ifdef ZLIB_PM3_TUNED and #ifndef ZLIB_PM3_TUNED.
|
||||||
|
//
|
||||||
|
// The rest of this file consists of the original README content
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
ZLIB DATA COMPRESSION LIBRARY
|
||||||
|
|
||||||
|
zlib 1.2.8 is a general purpose data compression library. All the code is
|
||||||
|
thread safe. The data format used by the zlib library is described by RFCs
|
||||||
|
(Request for Comments) 1950 to 1952 in the files
|
||||||
|
http://tools.ietf.org/html/rfc1950 (zlib format), rfc1951 (deflate format) and
|
||||||
|
rfc1952 (gzip format).
|
||||||
|
|
||||||
|
All functions of the compression library are documented in the file zlib.h
|
||||||
|
(volunteer to write man pages welcome, contact zlib@gzip.org). A usage example
|
||||||
|
of the library is given in the file test/example.c which also tests that
|
||||||
|
the library is working correctly. Another example is given in the file
|
||||||
|
test/minigzip.c. The compression library itself is composed of all source
|
||||||
|
files in the root directory.
|
||||||
|
|
||||||
|
To compile all files and run the test program, follow the instructions given at
|
||||||
|
the top of Makefile.in. In short "./configure; make test", and if that goes
|
||||||
|
well, "make install" should work for most flavors of Unix. For Windows, use
|
||||||
|
one of the special makefiles in win32/ or contrib/vstudio/ . For VMS, use
|
||||||
|
make_vms.com.
|
||||||
|
|
||||||
|
Questions about zlib should be sent to <zlib@gzip.org>, or to Gilles Vollant
|
||||||
|
<info@winimage.com> for the Windows DLL version. The zlib home page is
|
||||||
|
http://zlib.net/ . Before reporting a problem, please check this site to
|
||||||
|
verify that you have the latest version of zlib; otherwise get the latest
|
||||||
|
version and check whether the problem still exists or not.
|
||||||
|
|
||||||
|
PLEASE read the zlib FAQ http://zlib.net/zlib_faq.html before asking for help.
|
||||||
|
|
||||||
|
Mark Nelson <markn@ieee.org> wrote an article about zlib for the Jan. 1997
|
||||||
|
issue of Dr. Dobb's Journal; a copy of the article is available at
|
||||||
|
http://marknelson.us/1997/01/01/zlib-engine/ .
|
||||||
|
|
||||||
|
The changes made in version 1.2.8 are documented in the file ChangeLog.
|
||||||
|
|
||||||
|
Unsupported third party contributions are provided in directory contrib/ .
|
||||||
|
|
||||||
|
zlib is available in Java using the java.util.zip package, documented at
|
||||||
|
http://java.sun.com/developer/technicalArticles/Programming/compression/ .
|
||||||
|
|
||||||
|
A Perl interface to zlib written by Paul Marquess <pmqs@cpan.org> is available
|
||||||
|
at CPAN (Comprehensive Perl Archive Network) sites, including
|
||||||
|
http://search.cpan.org/~pmqs/IO-Compress-Zlib/ .
|
||||||
|
|
||||||
|
A Python interface to zlib written by A.M. Kuchling <amk@amk.ca> is
|
||||||
|
available in Python 1.5 and later versions, see
|
||||||
|
http://docs.python.org/library/zlib.html .
|
||||||
|
|
||||||
|
zlib is built into tcl: http://wiki.tcl.tk/4610 .
|
||||||
|
|
||||||
|
An experimental package to read and write files in .zip format, written on top
|
||||||
|
of zlib by Gilles Vollant <info@winimage.com>, is available in the
|
||||||
|
contrib/minizip directory of zlib.
|
||||||
|
|
||||||
|
|
||||||
|
Notes for some targets:
|
||||||
|
|
||||||
|
- For Windows DLL versions, please see win32/DLL_FAQ.txt
|
||||||
|
|
||||||
|
- For 64-bit Irix, deflate.c must be compiled without any optimization. With
|
||||||
|
-O, one libpng test fails. The test works in 32 bit mode (with the -n32
|
||||||
|
compiler flag). The compiler bug has been reported to SGI.
|
||||||
|
|
||||||
|
- zlib doesn't work with gcc 2.6.3 on a DEC 3000/300LX under OSF/1 2.1 it works
|
||||||
|
when compiled with cc.
|
||||||
|
|
||||||
|
- On Digital Unix 4.0D (formely OSF/1) on AlphaServer, the cc option -std1 is
|
||||||
|
necessary to get gzprintf working correctly. This is done by configure.
|
||||||
|
|
||||||
|
- zlib doesn't work on HP-UX 9.05 with some versions of /bin/cc. It works with
|
||||||
|
other compilers. Use "make test" to check your compiler.
|
||||||
|
|
||||||
|
- gzdopen is not supported on RISCOS or BEOS.
|
||||||
|
|
||||||
|
- For PalmOs, see http://palmzlib.sourceforge.net/
|
||||||
|
|
||||||
|
|
||||||
|
Acknowledgments:
|
||||||
|
|
||||||
|
The deflate format used by zlib was defined by Phil Katz. The deflate and
|
||||||
|
zlib specifications were written by L. Peter Deutsch. Thanks to all the
|
||||||
|
people who reported problems and suggested various improvements in zlib; they
|
||||||
|
are too numerous to cite here.
|
||||||
|
|
||||||
|
Copyright notice:
|
||||||
|
|
||||||
|
(C) 1995-2013 Jean-loup Gailly and Mark Adler
|
||||||
|
|
||||||
|
This software is provided 'as-is', without any express or implied
|
||||||
|
warranty. In no event will the authors be held liable for any damages
|
||||||
|
arising from the use of this software.
|
||||||
|
|
||||||
|
Permission is granted to anyone to use this software for any purpose,
|
||||||
|
including commercial applications, and to alter it and redistribute it
|
||||||
|
freely, subject to the following restrictions:
|
||||||
|
|
||||||
|
1. The origin of this software must not be misrepresented; you must not
|
||||||
|
claim that you wrote the original software. If you use this software
|
||||||
|
in a product, an acknowledgment in the product documentation would be
|
||||||
|
appreciated but is not required.
|
||||||
|
2. Altered source versions must be plainly marked as such, and must not be
|
||||||
|
misrepresented as being the original software.
|
||||||
|
3. This notice may not be removed or altered from any source distribution.
|
||||||
|
|
||||||
|
Jean-loup Gailly Mark Adler
|
||||||
|
jloup@gzip.org madler@alumni.caltech.edu
|
||||||
|
|
||||||
|
If you use the zlib library in a product, we would appreciate *not* receiving
|
||||||
|
lengthy legal documents to sign. The sources are provided for free but without
|
||||||
|
warranty of any kind. The library has been entirely written by Jean-loup
|
||||||
|
Gailly and Mark Adler; it does not include third-party code.
|
||||||
|
|
||||||
|
If you redistribute modified sources, we would appreciate that you include in
|
||||||
|
the file ChangeLog history information documenting your changes. Please read
|
||||||
|
the FAQ for more information on the distribution of modified source versions.
|
179
zlib/adler32.c
Normal file
179
zlib/adler32.c
Normal file
|
@ -0,0 +1,179 @@
|
||||||
|
/* adler32.c -- compute the Adler-32 checksum of a data stream
|
||||||
|
* Copyright (C) 1995-2011 Mark Adler
|
||||||
|
* For conditions of distribution and use, see copyright notice in zlib.h
|
||||||
|
*/
|
||||||
|
|
||||||
|
/* @(#) $Id$ */
|
||||||
|
|
||||||
|
#include "zutil.h"
|
||||||
|
|
||||||
|
#define local static
|
||||||
|
|
||||||
|
local uLong adler32_combine_ OF((uLong adler1, uLong adler2, z_off64_t len2));
|
||||||
|
|
||||||
|
#define BASE 65521 /* largest prime smaller than 65536 */
|
||||||
|
#define NMAX 5552
|
||||||
|
/* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */
|
||||||
|
|
||||||
|
#define DO1(buf,i) {adler += (buf)[i]; sum2 += adler;}
|
||||||
|
#define DO2(buf,i) DO1(buf,i); DO1(buf,i+1);
|
||||||
|
#define DO4(buf,i) DO2(buf,i); DO2(buf,i+2);
|
||||||
|
#define DO8(buf,i) DO4(buf,i); DO4(buf,i+4);
|
||||||
|
#define DO16(buf) DO8(buf,0); DO8(buf,8);
|
||||||
|
|
||||||
|
/* use NO_DIVIDE if your processor does not do division in hardware --
|
||||||
|
try it both ways to see which is faster */
|
||||||
|
#ifdef NO_DIVIDE
|
||||||
|
/* note that this assumes BASE is 65521, where 65536 % 65521 == 15
|
||||||
|
(thank you to John Reiser for pointing this out) */
|
||||||
|
# define CHOP(a) \
|
||||||
|
do { \
|
||||||
|
unsigned long tmp = a >> 16; \
|
||||||
|
a &= 0xffffUL; \
|
||||||
|
a += (tmp << 4) - tmp; \
|
||||||
|
} while (0)
|
||||||
|
# define MOD28(a) \
|
||||||
|
do { \
|
||||||
|
CHOP(a); \
|
||||||
|
if (a >= BASE) a -= BASE; \
|
||||||
|
} while (0)
|
||||||
|
# define MOD(a) \
|
||||||
|
do { \
|
||||||
|
CHOP(a); \
|
||||||
|
MOD28(a); \
|
||||||
|
} while (0)
|
||||||
|
# define MOD63(a) \
|
||||||
|
do { /* this assumes a is not negative */ \
|
||||||
|
z_off64_t tmp = a >> 32; \
|
||||||
|
a &= 0xffffffffL; \
|
||||||
|
a += (tmp << 8) - (tmp << 5) + tmp; \
|
||||||
|
tmp = a >> 16; \
|
||||||
|
a &= 0xffffL; \
|
||||||
|
a += (tmp << 4) - tmp; \
|
||||||
|
tmp = a >> 16; \
|
||||||
|
a &= 0xffffL; \
|
||||||
|
a += (tmp << 4) - tmp; \
|
||||||
|
if (a >= BASE) a -= BASE; \
|
||||||
|
} while (0)
|
||||||
|
#else
|
||||||
|
# define MOD(a) a %= BASE
|
||||||
|
# define MOD28(a) a %= BASE
|
||||||
|
# define MOD63(a) a %= BASE
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* ========================================================================= */
|
||||||
|
uLong ZEXPORT adler32(adler, buf, len)
|
||||||
|
uLong adler;
|
||||||
|
const Bytef *buf;
|
||||||
|
uInt len;
|
||||||
|
{
|
||||||
|
unsigned long sum2;
|
||||||
|
unsigned n;
|
||||||
|
|
||||||
|
/* split Adler-32 into component sums */
|
||||||
|
sum2 = (adler >> 16) & 0xffff;
|
||||||
|
adler &= 0xffff;
|
||||||
|
|
||||||
|
/* in case user likes doing a byte at a time, keep it fast */
|
||||||
|
if (len == 1) {
|
||||||
|
adler += buf[0];
|
||||||
|
if (adler >= BASE)
|
||||||
|
adler -= BASE;
|
||||||
|
sum2 += adler;
|
||||||
|
if (sum2 >= BASE)
|
||||||
|
sum2 -= BASE;
|
||||||
|
return adler | (sum2 << 16);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* initial Adler-32 value (deferred check for len == 1 speed) */
|
||||||
|
if (buf == Z_NULL)
|
||||||
|
return 1L;
|
||||||
|
|
||||||
|
/* in case short lengths are provided, keep it somewhat fast */
|
||||||
|
if (len < 16) {
|
||||||
|
while (len--) {
|
||||||
|
adler += *buf++;
|
||||||
|
sum2 += adler;
|
||||||
|
}
|
||||||
|
if (adler >= BASE)
|
||||||
|
adler -= BASE;
|
||||||
|
MOD28(sum2); /* only added so many BASE's */
|
||||||
|
return adler | (sum2 << 16);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* do length NMAX blocks -- requires just one modulo operation */
|
||||||
|
while (len >= NMAX) {
|
||||||
|
len -= NMAX;
|
||||||
|
n = NMAX / 16; /* NMAX is divisible by 16 */
|
||||||
|
do {
|
||||||
|
DO16(buf); /* 16 sums unrolled */
|
||||||
|
buf += 16;
|
||||||
|
} while (--n);
|
||||||
|
MOD(adler);
|
||||||
|
MOD(sum2);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* do remaining bytes (less than NMAX, still just one modulo) */
|
||||||
|
if (len) { /* avoid modulos if none remaining */
|
||||||
|
while (len >= 16) {
|
||||||
|
len -= 16;
|
||||||
|
DO16(buf);
|
||||||
|
buf += 16;
|
||||||
|
}
|
||||||
|
while (len--) {
|
||||||
|
adler += *buf++;
|
||||||
|
sum2 += adler;
|
||||||
|
}
|
||||||
|
MOD(adler);
|
||||||
|
MOD(sum2);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* return recombined sums */
|
||||||
|
return adler | (sum2 << 16);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ========================================================================= */
|
||||||
|
local uLong adler32_combine_(adler1, adler2, len2)
|
||||||
|
uLong adler1;
|
||||||
|
uLong adler2;
|
||||||
|
z_off64_t len2;
|
||||||
|
{
|
||||||
|
unsigned long sum1;
|
||||||
|
unsigned long sum2;
|
||||||
|
unsigned rem;
|
||||||
|
|
||||||
|
/* for negative len, return invalid adler32 as a clue for debugging */
|
||||||
|
if (len2 < 0)
|
||||||
|
return 0xffffffffUL;
|
||||||
|
|
||||||
|
/* the derivation of this formula is left as an exercise for the reader */
|
||||||
|
MOD63(len2); /* assumes len2 >= 0 */
|
||||||
|
rem = (unsigned)len2;
|
||||||
|
sum1 = adler1 & 0xffff;
|
||||||
|
sum2 = rem * sum1;
|
||||||
|
MOD(sum2);
|
||||||
|
sum1 += (adler2 & 0xffff) + BASE - 1;
|
||||||
|
sum2 += ((adler1 >> 16) & 0xffff) + ((adler2 >> 16) & 0xffff) + BASE - rem;
|
||||||
|
if (sum1 >= BASE) sum1 -= BASE;
|
||||||
|
if (sum1 >= BASE) sum1 -= BASE;
|
||||||
|
if (sum2 >= (BASE << 1)) sum2 -= (BASE << 1);
|
||||||
|
if (sum2 >= BASE) sum2 -= BASE;
|
||||||
|
return sum1 | (sum2 << 16);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ========================================================================= */
|
||||||
|
uLong ZEXPORT adler32_combine(adler1, adler2, len2)
|
||||||
|
uLong adler1;
|
||||||
|
uLong adler2;
|
||||||
|
z_off_t len2;
|
||||||
|
{
|
||||||
|
return adler32_combine_(adler1, adler2, len2);
|
||||||
|
}
|
||||||
|
|
||||||
|
uLong ZEXPORT adler32_combine64(adler1, adler2, len2)
|
||||||
|
uLong adler1;
|
||||||
|
uLong adler2;
|
||||||
|
z_off64_t len2;
|
||||||
|
{
|
||||||
|
return adler32_combine_(adler1, adler2, len2);
|
||||||
|
}
|
2061
zlib/deflate.c
Normal file
2061
zlib/deflate.c
Normal file
File diff suppressed because it is too large
Load diff
346
zlib/deflate.h
Normal file
346
zlib/deflate.h
Normal file
|
@ -0,0 +1,346 @@
|
||||||
|
/* deflate.h -- internal compression state
|
||||||
|
* Copyright (C) 1995-2012 Jean-loup Gailly
|
||||||
|
* For conditions of distribution and use, see copyright notice in zlib.h
|
||||||
|
*/
|
||||||
|
|
||||||
|
/* WARNING: this file should *not* be used by applications. It is
|
||||||
|
part of the implementation of the compression library and is
|
||||||
|
subject to change. Applications should only use zlib.h.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/* @(#) $Id$ */
|
||||||
|
|
||||||
|
#ifndef DEFLATE_H
|
||||||
|
#define DEFLATE_H
|
||||||
|
|
||||||
|
#include "zutil.h"
|
||||||
|
|
||||||
|
/* define NO_GZIP when compiling if you want to disable gzip header and
|
||||||
|
trailer creation by deflate(). NO_GZIP would be used to avoid linking in
|
||||||
|
the crc code when it is not needed. For shared libraries, gzip encoding
|
||||||
|
should be left enabled. */
|
||||||
|
#ifndef NO_GZIP
|
||||||
|
# define GZIP
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* ===========================================================================
|
||||||
|
* Internal compression state.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#define LENGTH_CODES 29
|
||||||
|
/* number of length codes, not counting the special END_BLOCK code */
|
||||||
|
|
||||||
|
#define LITERALS 256
|
||||||
|
/* number of literal bytes 0..255 */
|
||||||
|
|
||||||
|
#define L_CODES (LITERALS+1+LENGTH_CODES)
|
||||||
|
/* number of Literal or Length codes, including the END_BLOCK code */
|
||||||
|
|
||||||
|
#define D_CODES 30
|
||||||
|
/* number of distance codes */
|
||||||
|
|
||||||
|
#define BL_CODES 19
|
||||||
|
/* number of codes used to transfer the bit lengths */
|
||||||
|
|
||||||
|
#define HEAP_SIZE (2*L_CODES+1)
|
||||||
|
/* maximum heap size */
|
||||||
|
|
||||||
|
#define MAX_BITS 15
|
||||||
|
/* All codes must not exceed MAX_BITS bits */
|
||||||
|
|
||||||
|
#define Buf_size 16
|
||||||
|
/* size of bit buffer in bi_buf */
|
||||||
|
|
||||||
|
#define INIT_STATE 42
|
||||||
|
#define EXTRA_STATE 69
|
||||||
|
#define NAME_STATE 73
|
||||||
|
#define COMMENT_STATE 91
|
||||||
|
#define HCRC_STATE 103
|
||||||
|
#define BUSY_STATE 113
|
||||||
|
#define FINISH_STATE 666
|
||||||
|
/* Stream status */
|
||||||
|
|
||||||
|
|
||||||
|
/* Data structure describing a single value and its code string. */
|
||||||
|
typedef struct ct_data_s {
|
||||||
|
union {
|
||||||
|
ush freq; /* frequency count */
|
||||||
|
ush code; /* bit string */
|
||||||
|
} fc;
|
||||||
|
union {
|
||||||
|
ush dad; /* father node in Huffman tree */
|
||||||
|
ush len; /* length of bit string */
|
||||||
|
} dl;
|
||||||
|
} FAR ct_data;
|
||||||
|
|
||||||
|
#define Freq fc.freq
|
||||||
|
#define Code fc.code
|
||||||
|
#define Dad dl.dad
|
||||||
|
#define Len dl.len
|
||||||
|
|
||||||
|
typedef struct static_tree_desc_s static_tree_desc;
|
||||||
|
|
||||||
|
typedef struct tree_desc_s {
|
||||||
|
ct_data *dyn_tree; /* the dynamic tree */
|
||||||
|
int max_code; /* largest code with non zero frequency */
|
||||||
|
static_tree_desc *stat_desc; /* the corresponding static tree */
|
||||||
|
} FAR tree_desc;
|
||||||
|
|
||||||
|
typedef ush Pos;
|
||||||
|
typedef Pos FAR Posf;
|
||||||
|
typedef unsigned IPos;
|
||||||
|
|
||||||
|
/* A Pos is an index in the character window. We use short instead of int to
|
||||||
|
* save space in the various tables. IPos is used only for parameter passing.
|
||||||
|
*/
|
||||||
|
|
||||||
|
typedef struct internal_state {
|
||||||
|
z_streamp strm; /* pointer back to this zlib stream */
|
||||||
|
int status; /* as the name implies */
|
||||||
|
Bytef *pending_buf; /* output still pending */
|
||||||
|
ulg pending_buf_size; /* size of pending_buf */
|
||||||
|
Bytef *pending_out; /* next pending byte to output to the stream */
|
||||||
|
uInt pending; /* nb of bytes in the pending buffer */
|
||||||
|
int wrap; /* bit 0 true for zlib, bit 1 true for gzip */
|
||||||
|
gz_headerp gzhead; /* gzip header information to write */
|
||||||
|
uInt gzindex; /* where in extra, name, or comment */
|
||||||
|
Byte method; /* can only be DEFLATED */
|
||||||
|
int last_flush; /* value of flush param for previous deflate call */
|
||||||
|
|
||||||
|
/* used by deflate.c: */
|
||||||
|
|
||||||
|
uInt w_size; /* LZ77 window size (32K by default) */
|
||||||
|
uInt w_bits; /* log2(w_size) (8..16) */
|
||||||
|
uInt w_mask; /* w_size - 1 */
|
||||||
|
|
||||||
|
Bytef *window;
|
||||||
|
/* Sliding window. Input bytes are read into the second half of the window,
|
||||||
|
* and move to the first half later to keep a dictionary of at least wSize
|
||||||
|
* bytes. With this organization, matches are limited to a distance of
|
||||||
|
* wSize-MAX_MATCH bytes, but this ensures that IO is always
|
||||||
|
* performed with a length multiple of the block size. Also, it limits
|
||||||
|
* the window size to 64K, which is quite useful on MSDOS.
|
||||||
|
* To do: use the user input buffer as sliding window.
|
||||||
|
*/
|
||||||
|
|
||||||
|
ulg window_size;
|
||||||
|
/* Actual size of window: 2*wSize, except when the user input buffer
|
||||||
|
* is directly used as sliding window.
|
||||||
|
*/
|
||||||
|
|
||||||
|
Posf *prev;
|
||||||
|
/* Link to older string with same hash index. To limit the size of this
|
||||||
|
* array to 64K, this link is maintained only for the last 32K strings.
|
||||||
|
* An index in this array is thus a window index modulo 32K.
|
||||||
|
*/
|
||||||
|
|
||||||
|
Posf *head; /* Heads of the hash chains or NIL. */
|
||||||
|
|
||||||
|
uInt ins_h; /* hash index of string to be inserted */
|
||||||
|
uInt hash_size; /* number of elements in hash table */
|
||||||
|
uInt hash_bits; /* log2(hash_size) */
|
||||||
|
uInt hash_mask; /* hash_size-1 */
|
||||||
|
|
||||||
|
uInt hash_shift;
|
||||||
|
/* Number of bits by which ins_h must be shifted at each input
|
||||||
|
* step. It must be such that after MIN_MATCH steps, the oldest
|
||||||
|
* byte no longer takes part in the hash key, that is:
|
||||||
|
* hash_shift * MIN_MATCH >= hash_bits
|
||||||
|
*/
|
||||||
|
|
||||||
|
long block_start;
|
||||||
|
/* Window position at the beginning of the current output block. Gets
|
||||||
|
* negative when the window is moved backwards.
|
||||||
|
*/
|
||||||
|
|
||||||
|
uInt match_length; /* length of best match */
|
||||||
|
IPos prev_match; /* previous match */
|
||||||
|
int match_available; /* set if previous match exists */
|
||||||
|
uInt strstart; /* start of string to insert */
|
||||||
|
uInt match_start; /* start of matching string */
|
||||||
|
uInt lookahead; /* number of valid bytes ahead in window */
|
||||||
|
|
||||||
|
uInt prev_length;
|
||||||
|
/* Length of the best match at previous step. Matches not greater than this
|
||||||
|
* are discarded. This is used in the lazy match evaluation.
|
||||||
|
*/
|
||||||
|
|
||||||
|
uInt max_chain_length;
|
||||||
|
/* To speed up deflation, hash chains are never searched beyond this
|
||||||
|
* length. A higher limit improves compression ratio but degrades the
|
||||||
|
* speed.
|
||||||
|
*/
|
||||||
|
|
||||||
|
uInt max_lazy_match;
|
||||||
|
/* Attempt to find a better match only when the current match is strictly
|
||||||
|
* smaller than this value. This mechanism is used only for compression
|
||||||
|
* levels >= 4.
|
||||||
|
*/
|
||||||
|
# define max_insert_length max_lazy_match
|
||||||
|
/* Insert new strings in the hash table only if the match length is not
|
||||||
|
* greater than this length. This saves time but degrades compression.
|
||||||
|
* max_insert_length is used only for compression levels <= 3.
|
||||||
|
*/
|
||||||
|
|
||||||
|
int level; /* compression level (1..9) */
|
||||||
|
int strategy; /* favor or force Huffman coding*/
|
||||||
|
|
||||||
|
uInt good_match;
|
||||||
|
/* Use a faster search when the previous match is longer than this */
|
||||||
|
|
||||||
|
int nice_match; /* Stop searching when current match exceeds this */
|
||||||
|
|
||||||
|
/* used by trees.c: */
|
||||||
|
/* Didn't use ct_data typedef below to suppress compiler warning */
|
||||||
|
struct ct_data_s dyn_ltree[HEAP_SIZE]; /* literal and length tree */
|
||||||
|
struct ct_data_s dyn_dtree[2*D_CODES+1]; /* distance tree */
|
||||||
|
struct ct_data_s bl_tree[2*BL_CODES+1]; /* Huffman tree for bit lengths */
|
||||||
|
|
||||||
|
struct tree_desc_s l_desc; /* desc. for literal tree */
|
||||||
|
struct tree_desc_s d_desc; /* desc. for distance tree */
|
||||||
|
struct tree_desc_s bl_desc; /* desc. for bit length tree */
|
||||||
|
|
||||||
|
ush bl_count[MAX_BITS+1];
|
||||||
|
/* number of codes at each bit length for an optimal tree */
|
||||||
|
|
||||||
|
int heap[2*L_CODES+1]; /* heap used to build the Huffman trees */
|
||||||
|
int heap_len; /* number of elements in the heap */
|
||||||
|
int heap_max; /* element of largest frequency */
|
||||||
|
/* The sons of heap[n] are heap[2*n] and heap[2*n+1]. heap[0] is not used.
|
||||||
|
* The same heap array is used to build all trees.
|
||||||
|
*/
|
||||||
|
|
||||||
|
uch depth[2*L_CODES+1];
|
||||||
|
/* Depth of each subtree used as tie breaker for trees of equal frequency
|
||||||
|
*/
|
||||||
|
|
||||||
|
uchf *l_buf; /* buffer for literals or lengths */
|
||||||
|
|
||||||
|
uInt lit_bufsize;
|
||||||
|
/* Size of match buffer for literals/lengths. There are 4 reasons for
|
||||||
|
* limiting lit_bufsize to 64K:
|
||||||
|
* - frequencies can be kept in 16 bit counters
|
||||||
|
* - if compression is not successful for the first block, all input
|
||||||
|
* data is still in the window so we can still emit a stored block even
|
||||||
|
* when input comes from standard input. (This can also be done for
|
||||||
|
* all blocks if lit_bufsize is not greater than 32K.)
|
||||||
|
* - if compression is not successful for a file smaller than 64K, we can
|
||||||
|
* even emit a stored file instead of a stored block (saving 5 bytes).
|
||||||
|
* This is applicable only for zip (not gzip or zlib).
|
||||||
|
* - creating new Huffman trees less frequently may not provide fast
|
||||||
|
* adaptation to changes in the input data statistics. (Take for
|
||||||
|
* example a binary file with poorly compressible code followed by
|
||||||
|
* a highly compressible string table.) Smaller buffer sizes give
|
||||||
|
* fast adaptation but have of course the overhead of transmitting
|
||||||
|
* trees more frequently.
|
||||||
|
* - I can't count above 4
|
||||||
|
*/
|
||||||
|
|
||||||
|
uInt last_lit; /* running index in l_buf */
|
||||||
|
|
||||||
|
ushf *d_buf;
|
||||||
|
/* Buffer for distances. To simplify the code, d_buf and l_buf have
|
||||||
|
* the same number of elements. To use different lengths, an extra flag
|
||||||
|
* array would be necessary.
|
||||||
|
*/
|
||||||
|
|
||||||
|
ulg opt_len; /* bit length of current block with optimal trees */
|
||||||
|
ulg static_len; /* bit length of current block with static trees */
|
||||||
|
uInt matches; /* number of string matches in current block */
|
||||||
|
uInt insert; /* bytes at end of window left to insert */
|
||||||
|
|
||||||
|
#ifdef DEBUG
|
||||||
|
ulg compressed_len; /* total bit length of compressed file mod 2^32 */
|
||||||
|
ulg bits_sent; /* bit length of compressed data sent mod 2^32 */
|
||||||
|
#endif
|
||||||
|
|
||||||
|
ush bi_buf;
|
||||||
|
/* Output buffer. bits are inserted starting at the bottom (least
|
||||||
|
* significant bits).
|
||||||
|
*/
|
||||||
|
int bi_valid;
|
||||||
|
/* Number of valid bits in bi_buf. All bits above the last valid bit
|
||||||
|
* are always zero.
|
||||||
|
*/
|
||||||
|
|
||||||
|
ulg high_water;
|
||||||
|
/* High water mark offset in window for initialized bytes -- bytes above
|
||||||
|
* this are set to zero in order to avoid memory check warnings when
|
||||||
|
* longest match routines access bytes past the input. This is then
|
||||||
|
* updated to the new high water mark.
|
||||||
|
*/
|
||||||
|
|
||||||
|
} FAR deflate_state;
|
||||||
|
|
||||||
|
/* Output a byte on the stream.
|
||||||
|
* IN assertion: there is enough room in pending_buf.
|
||||||
|
*/
|
||||||
|
#define put_byte(s, c) {s->pending_buf[s->pending++] = (c);}
|
||||||
|
|
||||||
|
|
||||||
|
#define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1)
|
||||||
|
/* Minimum amount of lookahead, except at the end of the input file.
|
||||||
|
* See deflate.c for comments about the MIN_MATCH+1.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#define MAX_DIST(s) ((s)->w_size-MIN_LOOKAHEAD)
|
||||||
|
/* In order to simplify the code, particularly on 16 bit machines, match
|
||||||
|
* distances are limited to MAX_DIST instead of WSIZE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#define WIN_INIT MAX_MATCH
|
||||||
|
/* Number of bytes after end of data in window to initialize in order to avoid
|
||||||
|
memory checker errors from longest match routines */
|
||||||
|
|
||||||
|
/* in trees.c */
|
||||||
|
void ZLIB_INTERNAL _tr_init OF((deflate_state *s));
|
||||||
|
int ZLIB_INTERNAL _tr_tally OF((deflate_state *s, unsigned dist, unsigned lc));
|
||||||
|
void ZLIB_INTERNAL _tr_flush_block OF((deflate_state *s, charf *buf,
|
||||||
|
ulg stored_len, int last));
|
||||||
|
void ZLIB_INTERNAL _tr_flush_bits OF((deflate_state *s));
|
||||||
|
void ZLIB_INTERNAL _tr_align OF((deflate_state *s));
|
||||||
|
void ZLIB_INTERNAL _tr_stored_block OF((deflate_state *s, charf *buf,
|
||||||
|
ulg stored_len, int last));
|
||||||
|
|
||||||
|
#define d_code(dist) \
|
||||||
|
((dist) < 256 ? _dist_code[dist] : _dist_code[256+((dist)>>7)])
|
||||||
|
/* Mapping from a distance to a distance code. dist is the distance - 1 and
|
||||||
|
* must not have side effects. _dist_code[256] and _dist_code[257] are never
|
||||||
|
* used.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef DEBUG
|
||||||
|
/* Inline versions of _tr_tally for speed: */
|
||||||
|
|
||||||
|
#if defined(GEN_TREES_H) || !defined(STDC)
|
||||||
|
extern uch ZLIB_INTERNAL _length_code[];
|
||||||
|
extern uch ZLIB_INTERNAL _dist_code[];
|
||||||
|
#else
|
||||||
|
extern const uch ZLIB_INTERNAL _length_code[];
|
||||||
|
extern const uch ZLIB_INTERNAL _dist_code[];
|
||||||
|
#endif
|
||||||
|
|
||||||
|
# define _tr_tally_lit(s, c, flush) \
|
||||||
|
{ uch cc = (c); \
|
||||||
|
s->d_buf[s->last_lit] = 0; \
|
||||||
|
s->l_buf[s->last_lit++] = cc; \
|
||||||
|
s->dyn_ltree[cc].Freq++; \
|
||||||
|
flush = (s->last_lit == s->lit_bufsize-1); \
|
||||||
|
}
|
||||||
|
# define _tr_tally_dist(s, distance, length, flush) \
|
||||||
|
{ uch len = (length); \
|
||||||
|
ush dist = (distance); \
|
||||||
|
s->d_buf[s->last_lit] = dist; \
|
||||||
|
s->l_buf[s->last_lit++] = len; \
|
||||||
|
dist--; \
|
||||||
|
s->dyn_ltree[_length_code[len]+LITERALS+1].Freq++; \
|
||||||
|
s->dyn_dtree[d_code(dist)].Freq++; \
|
||||||
|
flush = (s->last_lit == s->lit_bufsize-1); \
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
# define _tr_tally_lit(s, c, flush) flush = _tr_tally(s, 0, c)
|
||||||
|
# define _tr_tally_dist(s, distance, length, flush) \
|
||||||
|
flush = _tr_tally(s, distance, length)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif /* DEFLATE_H */
|
340
zlib/inffast.c
Normal file
340
zlib/inffast.c
Normal file
|
@ -0,0 +1,340 @@
|
||||||
|
/* inffast.c -- fast decoding
|
||||||
|
* Copyright (C) 1995-2008, 2010, 2013 Mark Adler
|
||||||
|
* For conditions of distribution and use, see copyright notice in zlib.h
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "zutil.h"
|
||||||
|
#include "inftrees.h"
|
||||||
|
#include "inflate.h"
|
||||||
|
#include "inffast.h"
|
||||||
|
|
||||||
|
#ifndef ASMINF
|
||||||
|
|
||||||
|
/* Allow machine dependent optimization for post-increment or pre-increment.
|
||||||
|
Based on testing to date,
|
||||||
|
Pre-increment preferred for:
|
||||||
|
- PowerPC G3 (Adler)
|
||||||
|
- MIPS R5000 (Randers-Pehrson)
|
||||||
|
Post-increment preferred for:
|
||||||
|
- none
|
||||||
|
No measurable difference:
|
||||||
|
- Pentium III (Anderson)
|
||||||
|
- M68060 (Nikl)
|
||||||
|
*/
|
||||||
|
#ifdef POSTINC
|
||||||
|
# define OFF 0
|
||||||
|
# define PUP(a) *(a)++
|
||||||
|
#else
|
||||||
|
# define OFF 1
|
||||||
|
# define PUP(a) *++(a)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/*
|
||||||
|
Decode literal, length, and distance codes and write out the resulting
|
||||||
|
literal and match bytes until either not enough input or output is
|
||||||
|
available, an end-of-block is encountered, or a data error is encountered.
|
||||||
|
When large enough input and output buffers are supplied to inflate(), for
|
||||||
|
example, a 16K input buffer and a 64K output buffer, more than 95% of the
|
||||||
|
inflate execution time is spent in this routine.
|
||||||
|
|
||||||
|
Entry assumptions:
|
||||||
|
|
||||||
|
state->mode == LEN
|
||||||
|
strm->avail_in >= 6
|
||||||
|
strm->avail_out >= 258
|
||||||
|
start >= strm->avail_out
|
||||||
|
state->bits < 8
|
||||||
|
|
||||||
|
On return, state->mode is one of:
|
||||||
|
|
||||||
|
LEN -- ran out of enough output space or enough available input
|
||||||
|
TYPE -- reached end of block code, inflate() to interpret next block
|
||||||
|
BAD -- error in block data
|
||||||
|
|
||||||
|
Notes:
|
||||||
|
|
||||||
|
- The maximum input bits used by a length/distance pair is 15 bits for the
|
||||||
|
length code, 5 bits for the length extra, 15 bits for the distance code,
|
||||||
|
and 13 bits for the distance extra. This totals 48 bits, or six bytes.
|
||||||
|
Therefore if strm->avail_in >= 6, then there is enough input to avoid
|
||||||
|
checking for available input while decoding.
|
||||||
|
|
||||||
|
- The maximum bytes that a single length/distance pair can output is 258
|
||||||
|
bytes, which is the maximum length that can be coded. inflate_fast()
|
||||||
|
requires strm->avail_out >= 258 for each loop to avoid checking for
|
||||||
|
output space.
|
||||||
|
*/
|
||||||
|
void ZLIB_INTERNAL inflate_fast(strm, start)
|
||||||
|
z_streamp strm;
|
||||||
|
unsigned start; /* inflate()'s starting value for strm->avail_out */
|
||||||
|
{
|
||||||
|
struct inflate_state FAR *state;
|
||||||
|
z_const unsigned char FAR *in; /* local strm->next_in */
|
||||||
|
z_const unsigned char FAR *last; /* have enough input while in < last */
|
||||||
|
unsigned char FAR *out; /* local strm->next_out */
|
||||||
|
unsigned char FAR *beg; /* inflate()'s initial strm->next_out */
|
||||||
|
unsigned char FAR *end; /* while out < end, enough space available */
|
||||||
|
#ifdef INFLATE_STRICT
|
||||||
|
unsigned dmax; /* maximum distance from zlib header */
|
||||||
|
#endif
|
||||||
|
unsigned wsize; /* window size or zero if not using window */
|
||||||
|
unsigned whave; /* valid bytes in the window */
|
||||||
|
unsigned wnext; /* window write index */
|
||||||
|
unsigned char FAR *window; /* allocated sliding window, if wsize != 0 */
|
||||||
|
unsigned long hold; /* local strm->hold */
|
||||||
|
unsigned bits; /* local strm->bits */
|
||||||
|
code const FAR *lcode; /* local strm->lencode */
|
||||||
|
code const FAR *dcode; /* local strm->distcode */
|
||||||
|
unsigned lmask; /* mask for first level of length codes */
|
||||||
|
unsigned dmask; /* mask for first level of distance codes */
|
||||||
|
code here; /* retrieved table entry */
|
||||||
|
unsigned op; /* code bits, operation, extra bits, or */
|
||||||
|
/* window position, window bytes to copy */
|
||||||
|
unsigned len; /* match length, unused bytes */
|
||||||
|
unsigned dist; /* match distance */
|
||||||
|
unsigned char FAR *from; /* where to copy match from */
|
||||||
|
|
||||||
|
/* copy state to local variables */
|
||||||
|
state = (struct inflate_state FAR *)strm->state;
|
||||||
|
in = strm->next_in - OFF;
|
||||||
|
last = in + (strm->avail_in - 5);
|
||||||
|
out = strm->next_out - OFF;
|
||||||
|
beg = out - (start - strm->avail_out);
|
||||||
|
end = out + (strm->avail_out - 257);
|
||||||
|
#ifdef INFLATE_STRICT
|
||||||
|
dmax = state->dmax;
|
||||||
|
#endif
|
||||||
|
wsize = state->wsize;
|
||||||
|
whave = state->whave;
|
||||||
|
wnext = state->wnext;
|
||||||
|
window = state->window;
|
||||||
|
hold = state->hold;
|
||||||
|
bits = state->bits;
|
||||||
|
lcode = state->lencode;
|
||||||
|
dcode = state->distcode;
|
||||||
|
lmask = (1U << state->lenbits) - 1;
|
||||||
|
dmask = (1U << state->distbits) - 1;
|
||||||
|
|
||||||
|
/* decode literals and length/distances until end-of-block or not enough
|
||||||
|
input data or output space */
|
||||||
|
do {
|
||||||
|
if (bits < 15) {
|
||||||
|
hold += (unsigned long)(PUP(in)) << bits;
|
||||||
|
bits += 8;
|
||||||
|
hold += (unsigned long)(PUP(in)) << bits;
|
||||||
|
bits += 8;
|
||||||
|
}
|
||||||
|
here = lcode[hold & lmask];
|
||||||
|
dolen:
|
||||||
|
op = (unsigned)(here.bits);
|
||||||
|
hold >>= op;
|
||||||
|
bits -= op;
|
||||||
|
op = (unsigned)(here.op);
|
||||||
|
if (op == 0) { /* literal */
|
||||||
|
Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
|
||||||
|
"inflate: literal '%c'\n" :
|
||||||
|
"inflate: literal 0x%02x\n", here.val));
|
||||||
|
PUP(out) = (unsigned char)(here.val);
|
||||||
|
}
|
||||||
|
else if (op & 16) { /* length base */
|
||||||
|
len = (unsigned)(here.val);
|
||||||
|
op &= 15; /* number of extra bits */
|
||||||
|
if (op) {
|
||||||
|
if (bits < op) {
|
||||||
|
hold += (unsigned long)(PUP(in)) << bits;
|
||||||
|
bits += 8;
|
||||||
|
}
|
||||||
|
len += (unsigned)hold & ((1U << op) - 1);
|
||||||
|
hold >>= op;
|
||||||
|
bits -= op;
|
||||||
|
}
|
||||||
|
Tracevv((stderr, "inflate: length %u\n", len));
|
||||||
|
if (bits < 15) {
|
||||||
|
hold += (unsigned long)(PUP(in)) << bits;
|
||||||
|
bits += 8;
|
||||||
|
hold += (unsigned long)(PUP(in)) << bits;
|
||||||
|
bits += 8;
|
||||||
|
}
|
||||||
|
here = dcode[hold & dmask];
|
||||||
|
dodist:
|
||||||
|
op = (unsigned)(here.bits);
|
||||||
|
hold >>= op;
|
||||||
|
bits -= op;
|
||||||
|
op = (unsigned)(here.op);
|
||||||
|
if (op & 16) { /* distance base */
|
||||||
|
dist = (unsigned)(here.val);
|
||||||
|
op &= 15; /* number of extra bits */
|
||||||
|
if (bits < op) {
|
||||||
|
hold += (unsigned long)(PUP(in)) << bits;
|
||||||
|
bits += 8;
|
||||||
|
if (bits < op) {
|
||||||
|
hold += (unsigned long)(PUP(in)) << bits;
|
||||||
|
bits += 8;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
dist += (unsigned)hold & ((1U << op) - 1);
|
||||||
|
#ifdef INFLATE_STRICT
|
||||||
|
if (dist > dmax) {
|
||||||
|
strm->msg = (char *)"invalid distance too far back";
|
||||||
|
state->mode = BAD;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
hold >>= op;
|
||||||
|
bits -= op;
|
||||||
|
Tracevv((stderr, "inflate: distance %u\n", dist));
|
||||||
|
op = (unsigned)(out - beg); /* max distance in output */
|
||||||
|
if (dist > op) { /* see if copy from window */
|
||||||
|
op = dist - op; /* distance back in window */
|
||||||
|
if (op > whave) {
|
||||||
|
if (state->sane) {
|
||||||
|
strm->msg =
|
||||||
|
(char *)"invalid distance too far back";
|
||||||
|
state->mode = BAD;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
|
||||||
|
if (len <= op - whave) {
|
||||||
|
do {
|
||||||
|
PUP(out) = 0;
|
||||||
|
} while (--len);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
len -= op - whave;
|
||||||
|
do {
|
||||||
|
PUP(out) = 0;
|
||||||
|
} while (--op > whave);
|
||||||
|
if (op == 0) {
|
||||||
|
from = out - dist;
|
||||||
|
do {
|
||||||
|
PUP(out) = PUP(from);
|
||||||
|
} while (--len);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
from = window - OFF;
|
||||||
|
if (wnext == 0) { /* very common case */
|
||||||
|
from += wsize - op;
|
||||||
|
if (op < len) { /* some from window */
|
||||||
|
len -= op;
|
||||||
|
do {
|
||||||
|
PUP(out) = PUP(from);
|
||||||
|
} while (--op);
|
||||||
|
from = out - dist; /* rest from output */
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (wnext < op) { /* wrap around window */
|
||||||
|
from += wsize + wnext - op;
|
||||||
|
op -= wnext;
|
||||||
|
if (op < len) { /* some from end of window */
|
||||||
|
len -= op;
|
||||||
|
do {
|
||||||
|
PUP(out) = PUP(from);
|
||||||
|
} while (--op);
|
||||||
|
from = window - OFF;
|
||||||
|
if (wnext < len) { /* some from start of window */
|
||||||
|
op = wnext;
|
||||||
|
len -= op;
|
||||||
|
do {
|
||||||
|
PUP(out) = PUP(from);
|
||||||
|
} while (--op);
|
||||||
|
from = out - dist; /* rest from output */
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else { /* contiguous in window */
|
||||||
|
from += wnext - op;
|
||||||
|
if (op < len) { /* some from window */
|
||||||
|
len -= op;
|
||||||
|
do {
|
||||||
|
PUP(out) = PUP(from);
|
||||||
|
} while (--op);
|
||||||
|
from = out - dist; /* rest from output */
|
||||||
|
}
|
||||||
|
}
|
||||||
|
while (len > 2) {
|
||||||
|
PUP(out) = PUP(from);
|
||||||
|
PUP(out) = PUP(from);
|
||||||
|
PUP(out) = PUP(from);
|
||||||
|
len -= 3;
|
||||||
|
}
|
||||||
|
if (len) {
|
||||||
|
PUP(out) = PUP(from);
|
||||||
|
if (len > 1)
|
||||||
|
PUP(out) = PUP(from);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
from = out - dist; /* copy direct from output */
|
||||||
|
do { /* minimum length is three */
|
||||||
|
PUP(out) = PUP(from);
|
||||||
|
PUP(out) = PUP(from);
|
||||||
|
PUP(out) = PUP(from);
|
||||||
|
len -= 3;
|
||||||
|
} while (len > 2);
|
||||||
|
if (len) {
|
||||||
|
PUP(out) = PUP(from);
|
||||||
|
if (len > 1)
|
||||||
|
PUP(out) = PUP(from);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if ((op & 64) == 0) { /* 2nd level distance code */
|
||||||
|
here = dcode[here.val + (hold & ((1U << op) - 1))];
|
||||||
|
goto dodist;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
strm->msg = (char *)"invalid distance code";
|
||||||
|
state->mode = BAD;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if ((op & 64) == 0) { /* 2nd level length code */
|
||||||
|
here = lcode[here.val + (hold & ((1U << op) - 1))];
|
||||||
|
goto dolen;
|
||||||
|
}
|
||||||
|
else if (op & 32) { /* end-of-block */
|
||||||
|
Tracevv((stderr, "inflate: end of block\n"));
|
||||||
|
state->mode = TYPE;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
strm->msg = (char *)"invalid literal/length code";
|
||||||
|
state->mode = BAD;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} while (in < last && out < end);
|
||||||
|
|
||||||
|
/* return unused bytes (on entry, bits < 8, so in won't go too far back) */
|
||||||
|
len = bits >> 3;
|
||||||
|
in -= len;
|
||||||
|
bits -= len << 3;
|
||||||
|
hold &= (1U << bits) - 1;
|
||||||
|
|
||||||
|
/* update state and return */
|
||||||
|
strm->next_in = in + OFF;
|
||||||
|
strm->next_out = out + OFF;
|
||||||
|
strm->avail_in = (unsigned)(in < last ? 5 + (last - in) : 5 - (in - last));
|
||||||
|
strm->avail_out = (unsigned)(out < end ?
|
||||||
|
257 + (end - out) : 257 - (out - end));
|
||||||
|
state->hold = hold;
|
||||||
|
state->bits = bits;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
inflate_fast() speedups that turned out slower (on a PowerPC G3 750CXe):
|
||||||
|
- Using bit fields for code structure
|
||||||
|
- Different op definition to avoid & for extra bits (do & for table bits)
|
||||||
|
- Three separate decoding do-loops for direct, window, and wnext == 0
|
||||||
|
- Special case for distance > 1 copies to do overlapped load and store copy
|
||||||
|
- Explicit branch predictions (based on measured branch probabilities)
|
||||||
|
- Deferring match copy and interspersed it with decoding subsequent codes
|
||||||
|
- Swapping literal/length else
|
||||||
|
- Swapping window/direct else
|
||||||
|
- Larger unrolled copy loops (three is about right)
|
||||||
|
- Moving len -= 3 statement into middle of loop
|
||||||
|
*/
|
||||||
|
|
||||||
|
#endif /* !ASMINF */
|
11
zlib/inffast.h
Normal file
11
zlib/inffast.h
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
/* inffast.h -- header to use inffast.c
|
||||||
|
* Copyright (C) 1995-2003, 2010 Mark Adler
|
||||||
|
* For conditions of distribution and use, see copyright notice in zlib.h
|
||||||
|
*/
|
||||||
|
|
||||||
|
/* WARNING: this file should *not* be used by applications. It is
|
||||||
|
part of the implementation of the compression library and is
|
||||||
|
subject to change. Applications should only use zlib.h.
|
||||||
|
*/
|
||||||
|
|
||||||
|
void ZLIB_INTERNAL inflate_fast OF((z_streamp strm, unsigned start));
|
94
zlib/inffixed.h
Normal file
94
zlib/inffixed.h
Normal file
|
@ -0,0 +1,94 @@
|
||||||
|
/* inffixed.h -- table for decoding fixed codes
|
||||||
|
* Generated automatically by makefixed().
|
||||||
|
*/
|
||||||
|
|
||||||
|
/* WARNING: this file should *not* be used by applications.
|
||||||
|
It is part of the implementation of this library and is
|
||||||
|
subject to change. Applications should only use zlib.h.
|
||||||
|
*/
|
||||||
|
|
||||||
|
static const code lenfix[512] = {
|
||||||
|
{96,7,0},{0,8,80},{0,8,16},{20,8,115},{18,7,31},{0,8,112},{0,8,48},
|
||||||
|
{0,9,192},{16,7,10},{0,8,96},{0,8,32},{0,9,160},{0,8,0},{0,8,128},
|
||||||
|
{0,8,64},{0,9,224},{16,7,6},{0,8,88},{0,8,24},{0,9,144},{19,7,59},
|
||||||
|
{0,8,120},{0,8,56},{0,9,208},{17,7,17},{0,8,104},{0,8,40},{0,9,176},
|
||||||
|
{0,8,8},{0,8,136},{0,8,72},{0,9,240},{16,7,4},{0,8,84},{0,8,20},
|
||||||
|
{21,8,227},{19,7,43},{0,8,116},{0,8,52},{0,9,200},{17,7,13},{0,8,100},
|
||||||
|
{0,8,36},{0,9,168},{0,8,4},{0,8,132},{0,8,68},{0,9,232},{16,7,8},
|
||||||
|
{0,8,92},{0,8,28},{0,9,152},{20,7,83},{0,8,124},{0,8,60},{0,9,216},
|
||||||
|
{18,7,23},{0,8,108},{0,8,44},{0,9,184},{0,8,12},{0,8,140},{0,8,76},
|
||||||
|
{0,9,248},{16,7,3},{0,8,82},{0,8,18},{21,8,163},{19,7,35},{0,8,114},
|
||||||
|
{0,8,50},{0,9,196},{17,7,11},{0,8,98},{0,8,34},{0,9,164},{0,8,2},
|
||||||
|
{0,8,130},{0,8,66},{0,9,228},{16,7,7},{0,8,90},{0,8,26},{0,9,148},
|
||||||
|
{20,7,67},{0,8,122},{0,8,58},{0,9,212},{18,7,19},{0,8,106},{0,8,42},
|
||||||
|
{0,9,180},{0,8,10},{0,8,138},{0,8,74},{0,9,244},{16,7,5},{0,8,86},
|
||||||
|
{0,8,22},{64,8,0},{19,7,51},{0,8,118},{0,8,54},{0,9,204},{17,7,15},
|
||||||
|
{0,8,102},{0,8,38},{0,9,172},{0,8,6},{0,8,134},{0,8,70},{0,9,236},
|
||||||
|
{16,7,9},{0,8,94},{0,8,30},{0,9,156},{20,7,99},{0,8,126},{0,8,62},
|
||||||
|
{0,9,220},{18,7,27},{0,8,110},{0,8,46},{0,9,188},{0,8,14},{0,8,142},
|
||||||
|
{0,8,78},{0,9,252},{96,7,0},{0,8,81},{0,8,17},{21,8,131},{18,7,31},
|
||||||
|
{0,8,113},{0,8,49},{0,9,194},{16,7,10},{0,8,97},{0,8,33},{0,9,162},
|
||||||
|
{0,8,1},{0,8,129},{0,8,65},{0,9,226},{16,7,6},{0,8,89},{0,8,25},
|
||||||
|
{0,9,146},{19,7,59},{0,8,121},{0,8,57},{0,9,210},{17,7,17},{0,8,105},
|
||||||
|
{0,8,41},{0,9,178},{0,8,9},{0,8,137},{0,8,73},{0,9,242},{16,7,4},
|
||||||
|
{0,8,85},{0,8,21},{16,8,258},{19,7,43},{0,8,117},{0,8,53},{0,9,202},
|
||||||
|
{17,7,13},{0,8,101},{0,8,37},{0,9,170},{0,8,5},{0,8,133},{0,8,69},
|
||||||
|
{0,9,234},{16,7,8},{0,8,93},{0,8,29},{0,9,154},{20,7,83},{0,8,125},
|
||||||
|
{0,8,61},{0,9,218},{18,7,23},{0,8,109},{0,8,45},{0,9,186},{0,8,13},
|
||||||
|
{0,8,141},{0,8,77},{0,9,250},{16,7,3},{0,8,83},{0,8,19},{21,8,195},
|
||||||
|
{19,7,35},{0,8,115},{0,8,51},{0,9,198},{17,7,11},{0,8,99},{0,8,35},
|
||||||
|
{0,9,166},{0,8,3},{0,8,131},{0,8,67},{0,9,230},{16,7,7},{0,8,91},
|
||||||
|
{0,8,27},{0,9,150},{20,7,67},{0,8,123},{0,8,59},{0,9,214},{18,7,19},
|
||||||
|
{0,8,107},{0,8,43},{0,9,182},{0,8,11},{0,8,139},{0,8,75},{0,9,246},
|
||||||
|
{16,7,5},{0,8,87},{0,8,23},{64,8,0},{19,7,51},{0,8,119},{0,8,55},
|
||||||
|
{0,9,206},{17,7,15},{0,8,103},{0,8,39},{0,9,174},{0,8,7},{0,8,135},
|
||||||
|
{0,8,71},{0,9,238},{16,7,9},{0,8,95},{0,8,31},{0,9,158},{20,7,99},
|
||||||
|
{0,8,127},{0,8,63},{0,9,222},{18,7,27},{0,8,111},{0,8,47},{0,9,190},
|
||||||
|
{0,8,15},{0,8,143},{0,8,79},{0,9,254},{96,7,0},{0,8,80},{0,8,16},
|
||||||
|
{20,8,115},{18,7,31},{0,8,112},{0,8,48},{0,9,193},{16,7,10},{0,8,96},
|
||||||
|
{0,8,32},{0,9,161},{0,8,0},{0,8,128},{0,8,64},{0,9,225},{16,7,6},
|
||||||
|
{0,8,88},{0,8,24},{0,9,145},{19,7,59},{0,8,120},{0,8,56},{0,9,209},
|
||||||
|
{17,7,17},{0,8,104},{0,8,40},{0,9,177},{0,8,8},{0,8,136},{0,8,72},
|
||||||
|
{0,9,241},{16,7,4},{0,8,84},{0,8,20},{21,8,227},{19,7,43},{0,8,116},
|
||||||
|
{0,8,52},{0,9,201},{17,7,13},{0,8,100},{0,8,36},{0,9,169},{0,8,4},
|
||||||
|
{0,8,132},{0,8,68},{0,9,233},{16,7,8},{0,8,92},{0,8,28},{0,9,153},
|
||||||
|
{20,7,83},{0,8,124},{0,8,60},{0,9,217},{18,7,23},{0,8,108},{0,8,44},
|
||||||
|
{0,9,185},{0,8,12},{0,8,140},{0,8,76},{0,9,249},{16,7,3},{0,8,82},
|
||||||
|
{0,8,18},{21,8,163},{19,7,35},{0,8,114},{0,8,50},{0,9,197},{17,7,11},
|
||||||
|
{0,8,98},{0,8,34},{0,9,165},{0,8,2},{0,8,130},{0,8,66},{0,9,229},
|
||||||
|
{16,7,7},{0,8,90},{0,8,26},{0,9,149},{20,7,67},{0,8,122},{0,8,58},
|
||||||
|
{0,9,213},{18,7,19},{0,8,106},{0,8,42},{0,9,181},{0,8,10},{0,8,138},
|
||||||
|
{0,8,74},{0,9,245},{16,7,5},{0,8,86},{0,8,22},{64,8,0},{19,7,51},
|
||||||
|
{0,8,118},{0,8,54},{0,9,205},{17,7,15},{0,8,102},{0,8,38},{0,9,173},
|
||||||
|
{0,8,6},{0,8,134},{0,8,70},{0,9,237},{16,7,9},{0,8,94},{0,8,30},
|
||||||
|
{0,9,157},{20,7,99},{0,8,126},{0,8,62},{0,9,221},{18,7,27},{0,8,110},
|
||||||
|
{0,8,46},{0,9,189},{0,8,14},{0,8,142},{0,8,78},{0,9,253},{96,7,0},
|
||||||
|
{0,8,81},{0,8,17},{21,8,131},{18,7,31},{0,8,113},{0,8,49},{0,9,195},
|
||||||
|
{16,7,10},{0,8,97},{0,8,33},{0,9,163},{0,8,1},{0,8,129},{0,8,65},
|
||||||
|
{0,9,227},{16,7,6},{0,8,89},{0,8,25},{0,9,147},{19,7,59},{0,8,121},
|
||||||
|
{0,8,57},{0,9,211},{17,7,17},{0,8,105},{0,8,41},{0,9,179},{0,8,9},
|
||||||
|
{0,8,137},{0,8,73},{0,9,243},{16,7,4},{0,8,85},{0,8,21},{16,8,258},
|
||||||
|
{19,7,43},{0,8,117},{0,8,53},{0,9,203},{17,7,13},{0,8,101},{0,8,37},
|
||||||
|
{0,9,171},{0,8,5},{0,8,133},{0,8,69},{0,9,235},{16,7,8},{0,8,93},
|
||||||
|
{0,8,29},{0,9,155},{20,7,83},{0,8,125},{0,8,61},{0,9,219},{18,7,23},
|
||||||
|
{0,8,109},{0,8,45},{0,9,187},{0,8,13},{0,8,141},{0,8,77},{0,9,251},
|
||||||
|
{16,7,3},{0,8,83},{0,8,19},{21,8,195},{19,7,35},{0,8,115},{0,8,51},
|
||||||
|
{0,9,199},{17,7,11},{0,8,99},{0,8,35},{0,9,167},{0,8,3},{0,8,131},
|
||||||
|
{0,8,67},{0,9,231},{16,7,7},{0,8,91},{0,8,27},{0,9,151},{20,7,67},
|
||||||
|
{0,8,123},{0,8,59},{0,9,215},{18,7,19},{0,8,107},{0,8,43},{0,9,183},
|
||||||
|
{0,8,11},{0,8,139},{0,8,75},{0,9,247},{16,7,5},{0,8,87},{0,8,23},
|
||||||
|
{64,8,0},{19,7,51},{0,8,119},{0,8,55},{0,9,207},{17,7,15},{0,8,103},
|
||||||
|
{0,8,39},{0,9,175},{0,8,7},{0,8,135},{0,8,71},{0,9,239},{16,7,9},
|
||||||
|
{0,8,95},{0,8,31},{0,9,159},{20,7,99},{0,8,127},{0,8,63},{0,9,223},
|
||||||
|
{18,7,27},{0,8,111},{0,8,47},{0,9,191},{0,8,15},{0,8,143},{0,8,79},
|
||||||
|
{0,9,255}
|
||||||
|
};
|
||||||
|
|
||||||
|
static const code distfix[32] = {
|
||||||
|
{16,5,1},{23,5,257},{19,5,17},{27,5,4097},{17,5,5},{25,5,1025},
|
||||||
|
{21,5,65},{29,5,16385},{16,5,3},{24,5,513},{20,5,33},{28,5,8193},
|
||||||
|
{18,5,9},{26,5,2049},{22,5,129},{64,5,0},{16,5,2},{23,5,385},
|
||||||
|
{19,5,25},{27,5,6145},{17,5,7},{25,5,1537},{21,5,97},{29,5,24577},
|
||||||
|
{16,5,4},{24,5,769},{20,5,49},{28,5,12289},{18,5,13},{26,5,3073},
|
||||||
|
{22,5,193},{64,5,0}
|
||||||
|
};
|
1531
zlib/inflate.c
Normal file
1531
zlib/inflate.c
Normal file
File diff suppressed because it is too large
Load diff
122
zlib/inflate.h
Normal file
122
zlib/inflate.h
Normal file
|
@ -0,0 +1,122 @@
|
||||||
|
/* inflate.h -- internal inflate state definition
|
||||||
|
* Copyright (C) 1995-2009 Mark Adler
|
||||||
|
* For conditions of distribution and use, see copyright notice in zlib.h
|
||||||
|
*/
|
||||||
|
|
||||||
|
/* WARNING: this file should *not* be used by applications. It is
|
||||||
|
part of the implementation of the compression library and is
|
||||||
|
subject to change. Applications should only use zlib.h.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/* define NO_GZIP when compiling if you want to disable gzip header and
|
||||||
|
trailer decoding by inflate(). NO_GZIP would be used to avoid linking in
|
||||||
|
the crc code when it is not needed. For shared libraries, gzip decoding
|
||||||
|
should be left enabled. */
|
||||||
|
#ifndef NO_GZIP
|
||||||
|
# define GUNZIP
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* Possible inflate modes between inflate() calls */
|
||||||
|
typedef enum {
|
||||||
|
HEAD, /* i: waiting for magic header */
|
||||||
|
FLAGS, /* i: waiting for method and flags (gzip) */
|
||||||
|
TIME, /* i: waiting for modification time (gzip) */
|
||||||
|
OS, /* i: waiting for extra flags and operating system (gzip) */
|
||||||
|
EXLEN, /* i: waiting for extra length (gzip) */
|
||||||
|
EXTRA, /* i: waiting for extra bytes (gzip) */
|
||||||
|
NAME, /* i: waiting for end of file name (gzip) */
|
||||||
|
COMMENT, /* i: waiting for end of comment (gzip) */
|
||||||
|
HCRC, /* i: waiting for header crc (gzip) */
|
||||||
|
DICTID, /* i: waiting for dictionary check value */
|
||||||
|
DICT, /* waiting for inflateSetDictionary() call */
|
||||||
|
TYPE, /* i: waiting for type bits, including last-flag bit */
|
||||||
|
TYPEDO, /* i: same, but skip check to exit inflate on new block */
|
||||||
|
STORED, /* i: waiting for stored size (length and complement) */
|
||||||
|
COPY_, /* i/o: same as COPY below, but only first time in */
|
||||||
|
COPY, /* i/o: waiting for input or output to copy stored block */
|
||||||
|
TABLE, /* i: waiting for dynamic block table lengths */
|
||||||
|
LENLENS, /* i: waiting for code length code lengths */
|
||||||
|
CODELENS, /* i: waiting for length/lit and distance code lengths */
|
||||||
|
LEN_, /* i: same as LEN below, but only first time in */
|
||||||
|
LEN, /* i: waiting for length/lit/eob code */
|
||||||
|
LENEXT, /* i: waiting for length extra bits */
|
||||||
|
DIST, /* i: waiting for distance code */
|
||||||
|
DISTEXT, /* i: waiting for distance extra bits */
|
||||||
|
MATCH, /* o: waiting for output space to copy string */
|
||||||
|
LIT, /* o: waiting for output space to write literal */
|
||||||
|
CHECK, /* i: waiting for 32-bit check value */
|
||||||
|
LENGTH, /* i: waiting for 32-bit length (gzip) */
|
||||||
|
DONE, /* finished check, done -- remain here until reset */
|
||||||
|
BAD, /* got a data error -- remain here until reset */
|
||||||
|
MEM, /* got an inflate() memory error -- remain here until reset */
|
||||||
|
SYNC /* looking for synchronization bytes to restart inflate() */
|
||||||
|
} inflate_mode;
|
||||||
|
|
||||||
|
/*
|
||||||
|
State transitions between above modes -
|
||||||
|
|
||||||
|
(most modes can go to BAD or MEM on error -- not shown for clarity)
|
||||||
|
|
||||||
|
Process header:
|
||||||
|
HEAD -> (gzip) or (zlib) or (raw)
|
||||||
|
(gzip) -> FLAGS -> TIME -> OS -> EXLEN -> EXTRA -> NAME -> COMMENT ->
|
||||||
|
HCRC -> TYPE
|
||||||
|
(zlib) -> DICTID or TYPE
|
||||||
|
DICTID -> DICT -> TYPE
|
||||||
|
(raw) -> TYPEDO
|
||||||
|
Read deflate blocks:
|
||||||
|
TYPE -> TYPEDO -> STORED or TABLE or LEN_ or CHECK
|
||||||
|
STORED -> COPY_ -> COPY -> TYPE
|
||||||
|
TABLE -> LENLENS -> CODELENS -> LEN_
|
||||||
|
LEN_ -> LEN
|
||||||
|
Read deflate codes in fixed or dynamic block:
|
||||||
|
LEN -> LENEXT or LIT or TYPE
|
||||||
|
LENEXT -> DIST -> DISTEXT -> MATCH -> LEN
|
||||||
|
LIT -> LEN
|
||||||
|
Process trailer:
|
||||||
|
CHECK -> LENGTH -> DONE
|
||||||
|
*/
|
||||||
|
|
||||||
|
/* state maintained between inflate() calls. Approximately 10K bytes. */
|
||||||
|
struct inflate_state {
|
||||||
|
inflate_mode mode; /* current inflate mode */
|
||||||
|
int last; /* true if processing last block */
|
||||||
|
int wrap; /* bit 0 true for zlib, bit 1 true for gzip */
|
||||||
|
int havedict; /* true if dictionary provided */
|
||||||
|
int flags; /* gzip header method and flags (0 if zlib) */
|
||||||
|
unsigned dmax; /* zlib header max distance (INFLATE_STRICT) */
|
||||||
|
unsigned long check; /* protected copy of check value */
|
||||||
|
unsigned long total; /* protected copy of output count */
|
||||||
|
gz_headerp head; /* where to save gzip header information */
|
||||||
|
/* sliding window */
|
||||||
|
unsigned wbits; /* log base 2 of requested window size */
|
||||||
|
unsigned wsize; /* window size or zero if not using window */
|
||||||
|
unsigned whave; /* valid bytes in the window */
|
||||||
|
unsigned wnext; /* window write index */
|
||||||
|
unsigned char FAR *window; /* allocated sliding window, if needed */
|
||||||
|
/* bit accumulator */
|
||||||
|
unsigned long hold; /* input bit accumulator */
|
||||||
|
unsigned bits; /* number of bits in "in" */
|
||||||
|
/* for string and stored block copying */
|
||||||
|
unsigned length; /* literal or length of data to copy */
|
||||||
|
unsigned offset; /* distance back to copy string from */
|
||||||
|
/* for table and code decoding */
|
||||||
|
unsigned extra; /* extra bits needed */
|
||||||
|
/* fixed and dynamic code tables */
|
||||||
|
code const FAR *lencode; /* starting table for length/literal codes */
|
||||||
|
code const FAR *distcode; /* starting table for distance codes */
|
||||||
|
unsigned lenbits; /* index bits for lencode */
|
||||||
|
unsigned distbits; /* index bits for distcode */
|
||||||
|
/* dynamic table building */
|
||||||
|
unsigned ncode; /* number of code length code lengths */
|
||||||
|
unsigned nlen; /* number of length code lengths */
|
||||||
|
unsigned ndist; /* number of distance code lengths */
|
||||||
|
unsigned have; /* number of code lengths in lens[] */
|
||||||
|
code FAR *next; /* next available space in codes[] */
|
||||||
|
unsigned short lens[320]; /* temporary storage for code lengths */
|
||||||
|
unsigned short work[288]; /* work area for code table building */
|
||||||
|
code codes[ENOUGH]; /* space for code tables */
|
||||||
|
int sane; /* if false, allow invalid distance too far */
|
||||||
|
int back; /* bits back of last unprocessed length/lit */
|
||||||
|
unsigned was; /* initial length of match */
|
||||||
|
};
|
302
zlib/inftrees.c
Normal file
302
zlib/inftrees.c
Normal file
|
@ -0,0 +1,302 @@
|
||||||
|
/* inftrees.c -- generate Huffman trees for efficient decoding
|
||||||
|
* Copyright (C) 1995-2013 Mark Adler
|
||||||
|
* For conditions of distribution and use, see copyright notice in zlib.h
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "zutil.h"
|
||||||
|
#include "inftrees.h"
|
||||||
|
|
||||||
|
#define MAXBITS 15
|
||||||
|
|
||||||
|
const char inflate_copyright[] =
|
||||||
|
" inflate 1.2.8.f-Proxmark3 Copyright 1995-2013 Mark Adler ";
|
||||||
|
/*
|
||||||
|
If you use the zlib library in a product, an acknowledgment is welcome
|
||||||
|
in the documentation of your product. If for some reason you cannot
|
||||||
|
include such an acknowledgment, I would appreciate that you keep this
|
||||||
|
copyright string in the executable of your product.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
Build a set of tables to decode the provided canonical Huffman code.
|
||||||
|
The code lengths are lens[0..codes-1]. The result starts at *table,
|
||||||
|
whose indices are 0..2^bits-1. work is a writable array of at least
|
||||||
|
lens shorts, which is used as a work area. type is the type of code
|
||||||
|
to be generated, CODES, LENS, or DISTS. On return, zero is success,
|
||||||
|
-1 is an invalid code, and +1 means that ENOUGH isn't enough. table
|
||||||
|
on return points to the next available entry's address. bits is the
|
||||||
|
requested root table index bits, and on return it is the actual root
|
||||||
|
table index bits. It will differ if the request is greater than the
|
||||||
|
longest code or if it is less than the shortest code.
|
||||||
|
*/
|
||||||
|
int ZLIB_INTERNAL inflate_table(codetype type, unsigned short FAR *lens,
|
||||||
|
unsigned codes, code FAR * FAR *table,
|
||||||
|
unsigned FAR *bits, unsigned short FAR *work)
|
||||||
|
{
|
||||||
|
unsigned len; /* a code's length in bits */
|
||||||
|
unsigned sym; /* index of code symbols */
|
||||||
|
unsigned min, max; /* minimum and maximum code lengths */
|
||||||
|
unsigned root; /* number of index bits for root table */
|
||||||
|
unsigned curr; /* number of index bits for current table */
|
||||||
|
unsigned drop; /* code bits to drop for sub-table */
|
||||||
|
int left; /* number of prefix codes available */
|
||||||
|
unsigned used; /* code entries in table used */
|
||||||
|
unsigned huff; /* Huffman code */
|
||||||
|
unsigned incr; /* for incrementing code, index */
|
||||||
|
unsigned fill; /* index for replicating entries */
|
||||||
|
unsigned low; /* low bits for current root entry */
|
||||||
|
unsigned mask; /* mask for low root bits */
|
||||||
|
code here; /* table entry for duplication */
|
||||||
|
code FAR *next; /* next available space in table */
|
||||||
|
const unsigned short FAR *base; /* base value table to use */
|
||||||
|
const unsigned short FAR *extra; /* extra bits table to use */
|
||||||
|
int end; /* use base and extra for symbol > end */
|
||||||
|
unsigned short count[MAXBITS+1]; /* number of codes of each length */
|
||||||
|
unsigned short offs[MAXBITS+1]; /* offsets in table for each length */
|
||||||
|
static const unsigned short lbase[31] = { /* Length codes 257..285 base */
|
||||||
|
3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
|
||||||
|
35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
|
||||||
|
static const unsigned short lext[31] = { /* Length codes 257..285 extra */
|
||||||
|
16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18, 18,
|
||||||
|
19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 16, 72, 78};
|
||||||
|
static const unsigned short dbase[32] = { /* Distance codes 0..29 base */
|
||||||
|
1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
|
||||||
|
257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
|
||||||
|
8193, 12289, 16385, 24577, 0, 0};
|
||||||
|
static const unsigned short dext[32] = { /* Distance codes 0..29 extra */
|
||||||
|
16, 16, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22,
|
||||||
|
23, 23, 24, 24, 25, 25, 26, 26, 27, 27,
|
||||||
|
28, 28, 29, 29, 64, 64};
|
||||||
|
|
||||||
|
/*
|
||||||
|
Process a set of code lengths to create a canonical Huffman code. The
|
||||||
|
code lengths are lens[0..codes-1]. Each length corresponds to the
|
||||||
|
symbols 0..codes-1. The Huffman code is generated by first sorting the
|
||||||
|
symbols by length from short to long, and retaining the symbol order
|
||||||
|
for codes with equal lengths. Then the code starts with all zero bits
|
||||||
|
for the first code of the shortest length, and the codes are integer
|
||||||
|
increments for the same length, and zeros are appended as the length
|
||||||
|
increases. For the deflate format, these bits are stored backwards
|
||||||
|
from their more natural integer increment ordering, and so when the
|
||||||
|
decoding tables are built in the large loop below, the integer codes
|
||||||
|
are incremented backwards.
|
||||||
|
|
||||||
|
This routine assumes, but does not check, that all of the entries in
|
||||||
|
lens[] are in the range 0..MAXBITS. The caller must assure this.
|
||||||
|
1..MAXBITS is interpreted as that code length. zero means that that
|
||||||
|
symbol does not occur in this code.
|
||||||
|
|
||||||
|
The codes are sorted by computing a count of codes for each length,
|
||||||
|
creating from that a table of starting indices for each length in the
|
||||||
|
sorted table, and then entering the symbols in order in the sorted
|
||||||
|
table. The sorted table is work[], with that space being provided by
|
||||||
|
the caller.
|
||||||
|
|
||||||
|
The length counts are used for other purposes as well, i.e. finding
|
||||||
|
the minimum and maximum length codes, determining if there are any
|
||||||
|
codes at all, checking for a valid set of lengths, and looking ahead
|
||||||
|
at length counts to determine sub-table sizes when building the
|
||||||
|
decoding tables.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/* accumulate lengths for codes (assumes lens[] all in 0..MAXBITS) */
|
||||||
|
for (len = 0; len <= MAXBITS; len++)
|
||||||
|
count[len] = 0;
|
||||||
|
for (sym = 0; sym < codes; sym++)
|
||||||
|
count[lens[sym]]++;
|
||||||
|
|
||||||
|
/* bound code lengths, force root to be within code lengths */
|
||||||
|
root = *bits;
|
||||||
|
for (max = MAXBITS; max >= 1; max--)
|
||||||
|
if (count[max] != 0) break;
|
||||||
|
if (root > max) root = max;
|
||||||
|
if (max == 0) { /* no symbols to code at all */
|
||||||
|
here.op = (unsigned char)64; /* invalid code marker */
|
||||||
|
here.bits = (unsigned char)1;
|
||||||
|
here.val = (unsigned short)0;
|
||||||
|
*(*table)++ = here; /* make a table to force an error */
|
||||||
|
*(*table)++ = here;
|
||||||
|
*bits = 1;
|
||||||
|
return 0; /* no symbols, but wait for decoding to report error */
|
||||||
|
}
|
||||||
|
for (min = 1; min < max; min++)
|
||||||
|
if (count[min] != 0) break;
|
||||||
|
if (root < min) root = min;
|
||||||
|
|
||||||
|
/* check for an over-subscribed or incomplete set of lengths */
|
||||||
|
left = 1;
|
||||||
|
for (len = 1; len <= MAXBITS; len++) {
|
||||||
|
left <<= 1;
|
||||||
|
left -= count[len];
|
||||||
|
if (left < 0) return -1; /* over-subscribed */
|
||||||
|
}
|
||||||
|
if (left > 0 && (type == CODES || max != 1))
|
||||||
|
return -1; /* incomplete set */
|
||||||
|
|
||||||
|
/* generate offsets into symbol table for each length for sorting */
|
||||||
|
offs[1] = 0;
|
||||||
|
for (len = 1; len < MAXBITS; len++)
|
||||||
|
offs[len + 1] = offs[len] + count[len];
|
||||||
|
|
||||||
|
/* sort symbols by length, by symbol order within each length */
|
||||||
|
for (sym = 0; sym < codes; sym++)
|
||||||
|
if (lens[sym] != 0) work[offs[lens[sym]]++] = (unsigned short)sym;
|
||||||
|
|
||||||
|
/*
|
||||||
|
Create and fill in decoding tables. In this loop, the table being
|
||||||
|
filled is at next and has curr index bits. The code being used is huff
|
||||||
|
with length len. That code is converted to an index by dropping drop
|
||||||
|
bits off of the bottom. For codes where len is less than drop + curr,
|
||||||
|
those top drop + curr - len bits are incremented through all values to
|
||||||
|
fill the table with replicated entries.
|
||||||
|
|
||||||
|
root is the number of index bits for the root table. When len exceeds
|
||||||
|
root, sub-tables are created pointed to by the root entry with an index
|
||||||
|
of the low root bits of huff. This is saved in low to check for when a
|
||||||
|
new sub-table should be started. drop is zero when the root table is
|
||||||
|
being filled, and drop is root when sub-tables are being filled.
|
||||||
|
|
||||||
|
When a new sub-table is needed, it is necessary to look ahead in the
|
||||||
|
code lengths to determine what size sub-table is needed. The length
|
||||||
|
counts are used for this, and so count[] is decremented as codes are
|
||||||
|
entered in the tables.
|
||||||
|
|
||||||
|
used keeps track of how many table entries have been allocated from the
|
||||||
|
provided *table space. It is checked for LENS and DIST tables against
|
||||||
|
the constants ENOUGH_LENS and ENOUGH_DISTS to guard against changes in
|
||||||
|
the initial root table size constants. See the comments in inftrees.h
|
||||||
|
for more information.
|
||||||
|
|
||||||
|
sym increments through all symbols, and the loop terminates when
|
||||||
|
all codes of length max, i.e. all codes, have been processed. This
|
||||||
|
routine permits incomplete codes, so another loop after this one fills
|
||||||
|
in the rest of the decoding tables with invalid code markers.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/* set up for code type */
|
||||||
|
switch (type) {
|
||||||
|
case CODES:
|
||||||
|
base = extra = work; /* dummy value--not used */
|
||||||
|
end = 19;
|
||||||
|
break;
|
||||||
|
case LENS:
|
||||||
|
base = lbase;
|
||||||
|
base -= 257;
|
||||||
|
extra = lext;
|
||||||
|
extra -= 257;
|
||||||
|
end = 256;
|
||||||
|
break;
|
||||||
|
default: /* DISTS */
|
||||||
|
base = dbase;
|
||||||
|
extra = dext;
|
||||||
|
end = -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* initialize state for loop */
|
||||||
|
huff = 0; /* starting code */
|
||||||
|
sym = 0; /* starting code symbol */
|
||||||
|
len = min; /* starting code length */
|
||||||
|
next = *table; /* current table to fill in */
|
||||||
|
curr = root; /* current table index bits */
|
||||||
|
drop = 0; /* current bits to drop from code for index */
|
||||||
|
low = (unsigned)(-1); /* trigger new sub-table when len > root */
|
||||||
|
used = 1U << root; /* use root table entries */
|
||||||
|
mask = used - 1; /* mask for comparing low */
|
||||||
|
|
||||||
|
/* check available table space */
|
||||||
|
if ((type == LENS && used > ENOUGH_LENS) ||
|
||||||
|
(type == DISTS && used > ENOUGH_DISTS))
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
/* process all codes and make table entries */
|
||||||
|
for (;;) {
|
||||||
|
/* create table entry */
|
||||||
|
here.bits = (unsigned char)(len - drop);
|
||||||
|
if ((int)(work[sym]) < end) {
|
||||||
|
here.op = (unsigned char)0;
|
||||||
|
here.val = work[sym];
|
||||||
|
}
|
||||||
|
else if ((int)(work[sym]) > end) {
|
||||||
|
here.op = (unsigned char)(extra[work[sym]]);
|
||||||
|
here.val = base[work[sym]];
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
here.op = (unsigned char)(32 + 64); /* end of block */
|
||||||
|
here.val = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* replicate for those indices with low len bits equal to huff */
|
||||||
|
incr = 1U << (len - drop);
|
||||||
|
fill = 1U << curr;
|
||||||
|
min = fill; /* save offset to next table */
|
||||||
|
do {
|
||||||
|
fill -= incr;
|
||||||
|
next[(huff >> drop) + fill] = here;
|
||||||
|
} while (fill != 0);
|
||||||
|
|
||||||
|
/* backwards increment the len-bit code huff */
|
||||||
|
incr = 1U << (len - 1);
|
||||||
|
while (huff & incr)
|
||||||
|
incr >>= 1;
|
||||||
|
if (incr != 0) {
|
||||||
|
huff &= incr - 1;
|
||||||
|
huff += incr;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
huff = 0;
|
||||||
|
|
||||||
|
/* go to next symbol, update count, len */
|
||||||
|
sym++;
|
||||||
|
if (--(count[len]) == 0) {
|
||||||
|
if (len == max) break;
|
||||||
|
len = lens[work[sym]];
|
||||||
|
}
|
||||||
|
|
||||||
|
/* create new sub-table if needed */
|
||||||
|
if (len > root && (huff & mask) != low) {
|
||||||
|
/* if first time, transition to sub-tables */
|
||||||
|
if (drop == 0)
|
||||||
|
drop = root;
|
||||||
|
|
||||||
|
/* increment past last table */
|
||||||
|
next += min; /* here min is 1 << curr */
|
||||||
|
|
||||||
|
/* determine length of next table */
|
||||||
|
curr = len - drop;
|
||||||
|
left = (int)(1 << curr);
|
||||||
|
while (curr + drop < max) {
|
||||||
|
left -= count[curr + drop];
|
||||||
|
if (left <= 0) break;
|
||||||
|
curr++;
|
||||||
|
left <<= 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* check for enough space */
|
||||||
|
used += 1U << curr;
|
||||||
|
if ((type == LENS && used > ENOUGH_LENS) ||
|
||||||
|
(type == DISTS && used > ENOUGH_DISTS))
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
/* point entry in root table to sub-table */
|
||||||
|
low = huff & mask;
|
||||||
|
(*table)[low].op = (unsigned char)curr;
|
||||||
|
(*table)[low].bits = (unsigned char)root;
|
||||||
|
(*table)[low].val = (unsigned short)(next - *table);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* fill in remaining table entry if code is incomplete (guaranteed to have
|
||||||
|
at most one remaining entry, since if the code is incomplete, the
|
||||||
|
maximum code length that was allowed to get this far is one bit) */
|
||||||
|
if (huff != 0) {
|
||||||
|
here.op = (unsigned char)64; /* invalid code marker */
|
||||||
|
here.bits = (unsigned char)(len - drop);
|
||||||
|
here.val = (unsigned short)0;
|
||||||
|
next[huff] = here;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* set return parameters */
|
||||||
|
*table += used;
|
||||||
|
*bits = root;
|
||||||
|
return 0;
|
||||||
|
}
|
62
zlib/inftrees.h
Normal file
62
zlib/inftrees.h
Normal file
|
@ -0,0 +1,62 @@
|
||||||
|
/* inftrees.h -- header to use inftrees.c
|
||||||
|
* Copyright (C) 1995-2005, 2010 Mark Adler
|
||||||
|
* For conditions of distribution and use, see copyright notice in zlib.h
|
||||||
|
*/
|
||||||
|
|
||||||
|
/* WARNING: this file should *not* be used by applications. It is
|
||||||
|
part of the implementation of the compression library and is
|
||||||
|
subject to change. Applications should only use zlib.h.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/* Structure for decoding tables. Each entry provides either the
|
||||||
|
information needed to do the operation requested by the code that
|
||||||
|
indexed that table entry, or it provides a pointer to another
|
||||||
|
table that indexes more bits of the code. op indicates whether
|
||||||
|
the entry is a pointer to another table, a literal, a length or
|
||||||
|
distance, an end-of-block, or an invalid code. For a table
|
||||||
|
pointer, the low four bits of op is the number of index bits of
|
||||||
|
that table. For a length or distance, the low four bits of op
|
||||||
|
is the number of extra bits to get after the code. bits is
|
||||||
|
the number of bits in this code or part of the code to drop off
|
||||||
|
of the bit buffer. val is the actual byte to output in the case
|
||||||
|
of a literal, the base length or distance, or the offset from
|
||||||
|
the current table to the next table. Each entry is four bytes. */
|
||||||
|
typedef struct {
|
||||||
|
unsigned char op; /* operation, extra bits, table bits */
|
||||||
|
unsigned char bits; /* bits in this part of the code */
|
||||||
|
unsigned short val; /* offset in table or code value */
|
||||||
|
} code;
|
||||||
|
|
||||||
|
/* op values as set by inflate_table():
|
||||||
|
00000000 - literal
|
||||||
|
0000tttt - table link, tttt != 0 is the number of table index bits
|
||||||
|
0001eeee - length or distance, eeee is the number of extra bits
|
||||||
|
01100000 - end of block
|
||||||
|
01000000 - invalid code
|
||||||
|
*/
|
||||||
|
|
||||||
|
/* Maximum size of the dynamic table. The maximum number of code structures is
|
||||||
|
1444, which is the sum of 852 for literal/length codes and 592 for distance
|
||||||
|
codes. These values were found by exhaustive searches using the program
|
||||||
|
examples/enough.c found in the zlib distribtution. The arguments to that
|
||||||
|
program are the number of symbols, the initial root table size, and the
|
||||||
|
maximum bit length of a code. "enough 286 9 15" for literal/length codes
|
||||||
|
returns returns 852, and "enough 30 6 15" for distance codes returns 592.
|
||||||
|
The initial root table size (9 or 6) is found in the fifth argument of the
|
||||||
|
inflate_table() calls in inflate.c and infback.c. If the root table size is
|
||||||
|
changed, then these maximum sizes would be need to be recalculated and
|
||||||
|
updated. */
|
||||||
|
#define ENOUGH_LENS 852
|
||||||
|
#define ENOUGH_DISTS 592
|
||||||
|
#define ENOUGH (ENOUGH_LENS+ENOUGH_DISTS)
|
||||||
|
|
||||||
|
/* Type of code to build for inflate_table() */
|
||||||
|
typedef enum {
|
||||||
|
CODES,
|
||||||
|
LENS,
|
||||||
|
DISTS
|
||||||
|
} codetype;
|
||||||
|
|
||||||
|
int ZLIB_INTERNAL inflate_table OF((codetype type, unsigned short FAR *lens,
|
||||||
|
unsigned codes, code FAR * FAR *table,
|
||||||
|
unsigned FAR *bits, unsigned short FAR *work));
|
1239
zlib/trees.c
Normal file
1239
zlib/trees.c
Normal file
File diff suppressed because it is too large
Load diff
128
zlib/trees.h
Normal file
128
zlib/trees.h
Normal file
|
@ -0,0 +1,128 @@
|
||||||
|
/* header created automatically with -DGEN_TREES_H */
|
||||||
|
|
||||||
|
local const ct_data static_ltree[L_CODES+2] = {
|
||||||
|
{{ 12},{ 8}}, {{140},{ 8}}, {{ 76},{ 8}}, {{204},{ 8}}, {{ 44},{ 8}},
|
||||||
|
{{172},{ 8}}, {{108},{ 8}}, {{236},{ 8}}, {{ 28},{ 8}}, {{156},{ 8}},
|
||||||
|
{{ 92},{ 8}}, {{220},{ 8}}, {{ 60},{ 8}}, {{188},{ 8}}, {{124},{ 8}},
|
||||||
|
{{252},{ 8}}, {{ 2},{ 8}}, {{130},{ 8}}, {{ 66},{ 8}}, {{194},{ 8}},
|
||||||
|
{{ 34},{ 8}}, {{162},{ 8}}, {{ 98},{ 8}}, {{226},{ 8}}, {{ 18},{ 8}},
|
||||||
|
{{146},{ 8}}, {{ 82},{ 8}}, {{210},{ 8}}, {{ 50},{ 8}}, {{178},{ 8}},
|
||||||
|
{{114},{ 8}}, {{242},{ 8}}, {{ 10},{ 8}}, {{138},{ 8}}, {{ 74},{ 8}},
|
||||||
|
{{202},{ 8}}, {{ 42},{ 8}}, {{170},{ 8}}, {{106},{ 8}}, {{234},{ 8}},
|
||||||
|
{{ 26},{ 8}}, {{154},{ 8}}, {{ 90},{ 8}}, {{218},{ 8}}, {{ 58},{ 8}},
|
||||||
|
{{186},{ 8}}, {{122},{ 8}}, {{250},{ 8}}, {{ 6},{ 8}}, {{134},{ 8}},
|
||||||
|
{{ 70},{ 8}}, {{198},{ 8}}, {{ 38},{ 8}}, {{166},{ 8}}, {{102},{ 8}},
|
||||||
|
{{230},{ 8}}, {{ 22},{ 8}}, {{150},{ 8}}, {{ 86},{ 8}}, {{214},{ 8}},
|
||||||
|
{{ 54},{ 8}}, {{182},{ 8}}, {{118},{ 8}}, {{246},{ 8}}, {{ 14},{ 8}},
|
||||||
|
{{142},{ 8}}, {{ 78},{ 8}}, {{206},{ 8}}, {{ 46},{ 8}}, {{174},{ 8}},
|
||||||
|
{{110},{ 8}}, {{238},{ 8}}, {{ 30},{ 8}}, {{158},{ 8}}, {{ 94},{ 8}},
|
||||||
|
{{222},{ 8}}, {{ 62},{ 8}}, {{190},{ 8}}, {{126},{ 8}}, {{254},{ 8}},
|
||||||
|
{{ 1},{ 8}}, {{129},{ 8}}, {{ 65},{ 8}}, {{193},{ 8}}, {{ 33},{ 8}},
|
||||||
|
{{161},{ 8}}, {{ 97},{ 8}}, {{225},{ 8}}, {{ 17},{ 8}}, {{145},{ 8}},
|
||||||
|
{{ 81},{ 8}}, {{209},{ 8}}, {{ 49},{ 8}}, {{177},{ 8}}, {{113},{ 8}},
|
||||||
|
{{241},{ 8}}, {{ 9},{ 8}}, {{137},{ 8}}, {{ 73},{ 8}}, {{201},{ 8}},
|
||||||
|
{{ 41},{ 8}}, {{169},{ 8}}, {{105},{ 8}}, {{233},{ 8}}, {{ 25},{ 8}},
|
||||||
|
{{153},{ 8}}, {{ 89},{ 8}}, {{217},{ 8}}, {{ 57},{ 8}}, {{185},{ 8}},
|
||||||
|
{{121},{ 8}}, {{249},{ 8}}, {{ 5},{ 8}}, {{133},{ 8}}, {{ 69},{ 8}},
|
||||||
|
{{197},{ 8}}, {{ 37},{ 8}}, {{165},{ 8}}, {{101},{ 8}}, {{229},{ 8}},
|
||||||
|
{{ 21},{ 8}}, {{149},{ 8}}, {{ 85},{ 8}}, {{213},{ 8}}, {{ 53},{ 8}},
|
||||||
|
{{181},{ 8}}, {{117},{ 8}}, {{245},{ 8}}, {{ 13},{ 8}}, {{141},{ 8}},
|
||||||
|
{{ 77},{ 8}}, {{205},{ 8}}, {{ 45},{ 8}}, {{173},{ 8}}, {{109},{ 8}},
|
||||||
|
{{237},{ 8}}, {{ 29},{ 8}}, {{157},{ 8}}, {{ 93},{ 8}}, {{221},{ 8}},
|
||||||
|
{{ 61},{ 8}}, {{189},{ 8}}, {{125},{ 8}}, {{253},{ 8}}, {{ 19},{ 9}},
|
||||||
|
{{275},{ 9}}, {{147},{ 9}}, {{403},{ 9}}, {{ 83},{ 9}}, {{339},{ 9}},
|
||||||
|
{{211},{ 9}}, {{467},{ 9}}, {{ 51},{ 9}}, {{307},{ 9}}, {{179},{ 9}},
|
||||||
|
{{435},{ 9}}, {{115},{ 9}}, {{371},{ 9}}, {{243},{ 9}}, {{499},{ 9}},
|
||||||
|
{{ 11},{ 9}}, {{267},{ 9}}, {{139},{ 9}}, {{395},{ 9}}, {{ 75},{ 9}},
|
||||||
|
{{331},{ 9}}, {{203},{ 9}}, {{459},{ 9}}, {{ 43},{ 9}}, {{299},{ 9}},
|
||||||
|
{{171},{ 9}}, {{427},{ 9}}, {{107},{ 9}}, {{363},{ 9}}, {{235},{ 9}},
|
||||||
|
{{491},{ 9}}, {{ 27},{ 9}}, {{283},{ 9}}, {{155},{ 9}}, {{411},{ 9}},
|
||||||
|
{{ 91},{ 9}}, {{347},{ 9}}, {{219},{ 9}}, {{475},{ 9}}, {{ 59},{ 9}},
|
||||||
|
{{315},{ 9}}, {{187},{ 9}}, {{443},{ 9}}, {{123},{ 9}}, {{379},{ 9}},
|
||||||
|
{{251},{ 9}}, {{507},{ 9}}, {{ 7},{ 9}}, {{263},{ 9}}, {{135},{ 9}},
|
||||||
|
{{391},{ 9}}, {{ 71},{ 9}}, {{327},{ 9}}, {{199},{ 9}}, {{455},{ 9}},
|
||||||
|
{{ 39},{ 9}}, {{295},{ 9}}, {{167},{ 9}}, {{423},{ 9}}, {{103},{ 9}},
|
||||||
|
{{359},{ 9}}, {{231},{ 9}}, {{487},{ 9}}, {{ 23},{ 9}}, {{279},{ 9}},
|
||||||
|
{{151},{ 9}}, {{407},{ 9}}, {{ 87},{ 9}}, {{343},{ 9}}, {{215},{ 9}},
|
||||||
|
{{471},{ 9}}, {{ 55},{ 9}}, {{311},{ 9}}, {{183},{ 9}}, {{439},{ 9}},
|
||||||
|
{{119},{ 9}}, {{375},{ 9}}, {{247},{ 9}}, {{503},{ 9}}, {{ 15},{ 9}},
|
||||||
|
{{271},{ 9}}, {{143},{ 9}}, {{399},{ 9}}, {{ 79},{ 9}}, {{335},{ 9}},
|
||||||
|
{{207},{ 9}}, {{463},{ 9}}, {{ 47},{ 9}}, {{303},{ 9}}, {{175},{ 9}},
|
||||||
|
{{431},{ 9}}, {{111},{ 9}}, {{367},{ 9}}, {{239},{ 9}}, {{495},{ 9}},
|
||||||
|
{{ 31},{ 9}}, {{287},{ 9}}, {{159},{ 9}}, {{415},{ 9}}, {{ 95},{ 9}},
|
||||||
|
{{351},{ 9}}, {{223},{ 9}}, {{479},{ 9}}, {{ 63},{ 9}}, {{319},{ 9}},
|
||||||
|
{{191},{ 9}}, {{447},{ 9}}, {{127},{ 9}}, {{383},{ 9}}, {{255},{ 9}},
|
||||||
|
{{511},{ 9}}, {{ 0},{ 7}}, {{ 64},{ 7}}, {{ 32},{ 7}}, {{ 96},{ 7}},
|
||||||
|
{{ 16},{ 7}}, {{ 80},{ 7}}, {{ 48},{ 7}}, {{112},{ 7}}, {{ 8},{ 7}},
|
||||||
|
{{ 72},{ 7}}, {{ 40},{ 7}}, {{104},{ 7}}, {{ 24},{ 7}}, {{ 88},{ 7}},
|
||||||
|
{{ 56},{ 7}}, {{120},{ 7}}, {{ 4},{ 7}}, {{ 68},{ 7}}, {{ 36},{ 7}},
|
||||||
|
{{100},{ 7}}, {{ 20},{ 7}}, {{ 84},{ 7}}, {{ 52},{ 7}}, {{116},{ 7}},
|
||||||
|
{{ 3},{ 8}}, {{131},{ 8}}, {{ 67},{ 8}}, {{195},{ 8}}, {{ 35},{ 8}},
|
||||||
|
{{163},{ 8}}, {{ 99},{ 8}}, {{227},{ 8}}
|
||||||
|
};
|
||||||
|
|
||||||
|
local const ct_data static_dtree[D_CODES] = {
|
||||||
|
{{ 0},{ 5}}, {{16},{ 5}}, {{ 8},{ 5}}, {{24},{ 5}}, {{ 4},{ 5}},
|
||||||
|
{{20},{ 5}}, {{12},{ 5}}, {{28},{ 5}}, {{ 2},{ 5}}, {{18},{ 5}},
|
||||||
|
{{10},{ 5}}, {{26},{ 5}}, {{ 6},{ 5}}, {{22},{ 5}}, {{14},{ 5}},
|
||||||
|
{{30},{ 5}}, {{ 1},{ 5}}, {{17},{ 5}}, {{ 9},{ 5}}, {{25},{ 5}},
|
||||||
|
{{ 5},{ 5}}, {{21},{ 5}}, {{13},{ 5}}, {{29},{ 5}}, {{ 3},{ 5}},
|
||||||
|
{{19},{ 5}}, {{11},{ 5}}, {{27},{ 5}}, {{ 7},{ 5}}, {{23},{ 5}}
|
||||||
|
};
|
||||||
|
|
||||||
|
const uch ZLIB_INTERNAL _dist_code[DIST_CODE_LEN] = {
|
||||||
|
0, 1, 2, 3, 4, 4, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8,
|
||||||
|
8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10,
|
||||||
|
10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
|
||||||
|
11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
|
||||||
|
12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13,
|
||||||
|
13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13,
|
||||||
|
13, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
|
||||||
|
14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
|
||||||
|
14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
|
||||||
|
14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15, 15, 15,
|
||||||
|
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
|
||||||
|
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
|
||||||
|
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 0, 0, 16, 17,
|
||||||
|
18, 18, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 22, 22, 22, 22, 22, 22, 22, 22,
|
||||||
|
23, 23, 23, 23, 23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
|
||||||
|
24, 24, 24, 24, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25,
|
||||||
|
26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
|
||||||
|
26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 27, 27, 27, 27, 27, 27, 27, 27,
|
||||||
|
27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
|
||||||
|
27, 27, 27, 27, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
|
||||||
|
28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
|
||||||
|
28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
|
||||||
|
28, 28, 28, 28, 28, 28, 28, 28, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
|
||||||
|
29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
|
||||||
|
29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
|
||||||
|
29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29
|
||||||
|
};
|
||||||
|
|
||||||
|
const uch ZLIB_INTERNAL _length_code[MAX_MATCH-MIN_MATCH+1]= {
|
||||||
|
0, 1, 2, 3, 4, 5, 6, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 12, 12,
|
||||||
|
13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 15, 16, 16, 16, 16, 16, 16, 16, 16,
|
||||||
|
17, 17, 17, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18, 18, 18, 18, 19, 19, 19, 19,
|
||||||
|
19, 19, 19, 19, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
|
||||||
|
21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 22, 22, 22, 22,
|
||||||
|
22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 23, 23, 23, 23, 23, 23, 23, 23,
|
||||||
|
23, 23, 23, 23, 23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
|
||||||
|
24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
|
||||||
|
25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25,
|
||||||
|
25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 26, 26, 26, 26, 26, 26, 26, 26,
|
||||||
|
26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
|
||||||
|
26, 26, 26, 26, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
|
||||||
|
27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 28
|
||||||
|
};
|
||||||
|
|
||||||
|
local const int base_length[LENGTH_CODES] = {
|
||||||
|
0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16, 20, 24, 28, 32, 40, 48, 56,
|
||||||
|
64, 80, 96, 112, 128, 160, 192, 224, 0
|
||||||
|
};
|
||||||
|
|
||||||
|
local const int base_dist[D_CODES] = {
|
||||||
|
0, 1, 2, 3, 4, 6, 8, 12, 16, 24,
|
||||||
|
32, 48, 64, 96, 128, 192, 256, 384, 512, 768,
|
||||||
|
1024, 1536, 2048, 3072, 4096, 6144, 8192, 12288, 16384, 24576
|
||||||
|
};
|
||||||
|
|
511
zlib/zconf.h
Normal file
511
zlib/zconf.h
Normal file
|
@ -0,0 +1,511 @@
|
||||||
|
/* zconf.h -- configuration of the zlib compression library
|
||||||
|
* Copyright (C) 1995-2013 Jean-loup Gailly.
|
||||||
|
* For conditions of distribution and use, see copyright notice in zlib.h
|
||||||
|
*/
|
||||||
|
|
||||||
|
/* @(#) $Id$ */
|
||||||
|
|
||||||
|
#ifndef ZCONF_H
|
||||||
|
#define ZCONF_H
|
||||||
|
|
||||||
|
/*
|
||||||
|
* If you *really* need a unique prefix for all types and library functions,
|
||||||
|
* compile with -DZ_PREFIX. The "standard" zlib should be compiled without it.
|
||||||
|
* Even better than compiling with -DZ_PREFIX would be to use configure to set
|
||||||
|
* this permanently in zconf.h using "./configure --zprefix".
|
||||||
|
*/
|
||||||
|
#ifdef Z_PREFIX /* may be set to #if 1 by ./configure */
|
||||||
|
# define Z_PREFIX_SET
|
||||||
|
|
||||||
|
/* all linked symbols */
|
||||||
|
# define _dist_code z__dist_code
|
||||||
|
# define _length_code z__length_code
|
||||||
|
# define _tr_align z__tr_align
|
||||||
|
# define _tr_flush_bits z__tr_flush_bits
|
||||||
|
# define _tr_flush_block z__tr_flush_block
|
||||||
|
# define _tr_init z__tr_init
|
||||||
|
# define _tr_stored_block z__tr_stored_block
|
||||||
|
# define _tr_tally z__tr_tally
|
||||||
|
# define adler32 z_adler32
|
||||||
|
# define adler32_combine z_adler32_combine
|
||||||
|
# define adler32_combine64 z_adler32_combine64
|
||||||
|
# ifndef Z_SOLO
|
||||||
|
# define compress z_compress
|
||||||
|
# define compress2 z_compress2
|
||||||
|
# define compressBound z_compressBound
|
||||||
|
# endif
|
||||||
|
# define crc32 z_crc32
|
||||||
|
# define crc32_combine z_crc32_combine
|
||||||
|
# define crc32_combine64 z_crc32_combine64
|
||||||
|
# define deflate z_deflate
|
||||||
|
# define deflateBound z_deflateBound
|
||||||
|
# define deflateCopy z_deflateCopy
|
||||||
|
# define deflateEnd z_deflateEnd
|
||||||
|
# define deflateInit2_ z_deflateInit2_
|
||||||
|
# define deflateInit_ z_deflateInit_
|
||||||
|
# define deflateParams z_deflateParams
|
||||||
|
# define deflatePending z_deflatePending
|
||||||
|
# define deflatePrime z_deflatePrime
|
||||||
|
# define deflateReset z_deflateReset
|
||||||
|
# define deflateResetKeep z_deflateResetKeep
|
||||||
|
# define deflateSetDictionary z_deflateSetDictionary
|
||||||
|
# define deflateSetHeader z_deflateSetHeader
|
||||||
|
# define deflateTune z_deflateTune
|
||||||
|
# define deflate_copyright z_deflate_copyright
|
||||||
|
# define get_crc_table z_get_crc_table
|
||||||
|
# ifndef Z_SOLO
|
||||||
|
# define gz_error z_gz_error
|
||||||
|
# define gz_intmax z_gz_intmax
|
||||||
|
# define gz_strwinerror z_gz_strwinerror
|
||||||
|
# define gzbuffer z_gzbuffer
|
||||||
|
# define gzclearerr z_gzclearerr
|
||||||
|
# define gzclose z_gzclose
|
||||||
|
# define gzclose_r z_gzclose_r
|
||||||
|
# define gzclose_w z_gzclose_w
|
||||||
|
# define gzdirect z_gzdirect
|
||||||
|
# define gzdopen z_gzdopen
|
||||||
|
# define gzeof z_gzeof
|
||||||
|
# define gzerror z_gzerror
|
||||||
|
# define gzflush z_gzflush
|
||||||
|
# define gzgetc z_gzgetc
|
||||||
|
# define gzgetc_ z_gzgetc_
|
||||||
|
# define gzgets z_gzgets
|
||||||
|
# define gzoffset z_gzoffset
|
||||||
|
# define gzoffset64 z_gzoffset64
|
||||||
|
# define gzopen z_gzopen
|
||||||
|
# define gzopen64 z_gzopen64
|
||||||
|
# ifdef _WIN32
|
||||||
|
# define gzopen_w z_gzopen_w
|
||||||
|
# endif
|
||||||
|
# define gzprintf z_gzprintf
|
||||||
|
# define gzvprintf z_gzvprintf
|
||||||
|
# define gzputc z_gzputc
|
||||||
|
# define gzputs z_gzputs
|
||||||
|
# define gzread z_gzread
|
||||||
|
# define gzrewind z_gzrewind
|
||||||
|
# define gzseek z_gzseek
|
||||||
|
# define gzseek64 z_gzseek64
|
||||||
|
# define gzsetparams z_gzsetparams
|
||||||
|
# define gztell z_gztell
|
||||||
|
# define gztell64 z_gztell64
|
||||||
|
# define gzungetc z_gzungetc
|
||||||
|
# define gzwrite z_gzwrite
|
||||||
|
# endif
|
||||||
|
# define inflate z_inflate
|
||||||
|
# define inflateBack z_inflateBack
|
||||||
|
# define inflateBackEnd z_inflateBackEnd
|
||||||
|
# define inflateBackInit_ z_inflateBackInit_
|
||||||
|
# define inflateCopy z_inflateCopy
|
||||||
|
# define inflateEnd z_inflateEnd
|
||||||
|
# define inflateGetHeader z_inflateGetHeader
|
||||||
|
# define inflateInit2_ z_inflateInit2_
|
||||||
|
# define inflateInit_ z_inflateInit_
|
||||||
|
# define inflateMark z_inflateMark
|
||||||
|
# define inflatePrime z_inflatePrime
|
||||||
|
# define inflateReset z_inflateReset
|
||||||
|
# define inflateReset2 z_inflateReset2
|
||||||
|
# define inflateSetDictionary z_inflateSetDictionary
|
||||||
|
# define inflateGetDictionary z_inflateGetDictionary
|
||||||
|
# define inflateSync z_inflateSync
|
||||||
|
# define inflateSyncPoint z_inflateSyncPoint
|
||||||
|
# define inflateUndermine z_inflateUndermine
|
||||||
|
# define inflateResetKeep z_inflateResetKeep
|
||||||
|
# define inflate_copyright z_inflate_copyright
|
||||||
|
# define inflate_fast z_inflate_fast
|
||||||
|
# define inflate_table z_inflate_table
|
||||||
|
# ifndef Z_SOLO
|
||||||
|
# define uncompress z_uncompress
|
||||||
|
# endif
|
||||||
|
# define zError z_zError
|
||||||
|
# ifndef Z_SOLO
|
||||||
|
# define zcalloc z_zcalloc
|
||||||
|
# define zcfree z_zcfree
|
||||||
|
# endif
|
||||||
|
# define zlibCompileFlags z_zlibCompileFlags
|
||||||
|
# define zlibVersion z_zlibVersion
|
||||||
|
|
||||||
|
/* all zlib typedefs in zlib.h and zconf.h */
|
||||||
|
# define Byte z_Byte
|
||||||
|
# define Bytef z_Bytef
|
||||||
|
# define alloc_func z_alloc_func
|
||||||
|
# define charf z_charf
|
||||||
|
# define free_func z_free_func
|
||||||
|
# ifndef Z_SOLO
|
||||||
|
# define gzFile z_gzFile
|
||||||
|
# endif
|
||||||
|
# define gz_header z_gz_header
|
||||||
|
# define gz_headerp z_gz_headerp
|
||||||
|
# define in_func z_in_func
|
||||||
|
# define intf z_intf
|
||||||
|
# define out_func z_out_func
|
||||||
|
# define uInt z_uInt
|
||||||
|
# define uIntf z_uIntf
|
||||||
|
# define uLong z_uLong
|
||||||
|
# define uLongf z_uLongf
|
||||||
|
# define voidp z_voidp
|
||||||
|
# define voidpc z_voidpc
|
||||||
|
# define voidpf z_voidpf
|
||||||
|
|
||||||
|
/* all zlib structs in zlib.h and zconf.h */
|
||||||
|
# define gz_header_s z_gz_header_s
|
||||||
|
# define internal_state z_internal_state
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(__MSDOS__) && !defined(MSDOS)
|
||||||
|
# define MSDOS
|
||||||
|
#endif
|
||||||
|
#if (defined(OS_2) || defined(__OS2__)) && !defined(OS2)
|
||||||
|
# define OS2
|
||||||
|
#endif
|
||||||
|
#if defined(_WINDOWS) && !defined(WINDOWS)
|
||||||
|
# define WINDOWS
|
||||||
|
#endif
|
||||||
|
#if defined(_WIN32) || defined(_WIN32_WCE) || defined(__WIN32__)
|
||||||
|
# ifndef WIN32
|
||||||
|
# define WIN32
|
||||||
|
# endif
|
||||||
|
#endif
|
||||||
|
#if (defined(MSDOS) || defined(OS2) || defined(WINDOWS)) && !defined(WIN32)
|
||||||
|
# if !defined(__GNUC__) && !defined(__FLAT__) && !defined(__386__)
|
||||||
|
# ifndef SYS16BIT
|
||||||
|
# define SYS16BIT
|
||||||
|
# endif
|
||||||
|
# endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Compile with -DMAXSEG_64K if the alloc function cannot allocate more
|
||||||
|
* than 64k bytes at a time (needed on systems with 16-bit int).
|
||||||
|
*/
|
||||||
|
#ifdef SYS16BIT
|
||||||
|
# define MAXSEG_64K
|
||||||
|
#endif
|
||||||
|
#ifdef MSDOS
|
||||||
|
# define UNALIGNED_OK
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef __STDC_VERSION__
|
||||||
|
# ifndef STDC
|
||||||
|
# define STDC
|
||||||
|
# endif
|
||||||
|
# if __STDC_VERSION__ >= 199901L
|
||||||
|
# ifndef STDC99
|
||||||
|
# define STDC99
|
||||||
|
# endif
|
||||||
|
# endif
|
||||||
|
#endif
|
||||||
|
#if !defined(STDC) && (defined(__STDC__) || defined(__cplusplus))
|
||||||
|
# define STDC
|
||||||
|
#endif
|
||||||
|
#if !defined(STDC) && (defined(__GNUC__) || defined(__BORLANDC__))
|
||||||
|
# define STDC
|
||||||
|
#endif
|
||||||
|
#if !defined(STDC) && (defined(MSDOS) || defined(WINDOWS) || defined(WIN32))
|
||||||
|
# define STDC
|
||||||
|
#endif
|
||||||
|
#if !defined(STDC) && (defined(OS2) || defined(__HOS_AIX__))
|
||||||
|
# define STDC
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(__OS400__) && !defined(STDC) /* iSeries (formerly AS/400). */
|
||||||
|
# define STDC
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef STDC
|
||||||
|
# ifndef const /* cannot use !defined(STDC) && !defined(const) on Mac */
|
||||||
|
# define const /* note: need a more gentle solution here */
|
||||||
|
# endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(ZLIB_CONST) && !defined(z_const)
|
||||||
|
# define z_const const
|
||||||
|
#else
|
||||||
|
# define z_const
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* Some Mac compilers merge all .h files incorrectly: */
|
||||||
|
#if defined(__MWERKS__)||defined(applec)||defined(THINK_C)||defined(__SC__)
|
||||||
|
# define NO_DUMMY_DECL
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* Maximum value for memLevel in deflateInit2 */
|
||||||
|
#ifndef MAX_MEM_LEVEL
|
||||||
|
# ifdef MAXSEG_64K
|
||||||
|
# define MAX_MEM_LEVEL 8
|
||||||
|
# else
|
||||||
|
# define MAX_MEM_LEVEL 9
|
||||||
|
# endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* Maximum value for windowBits in deflateInit2 and inflateInit2.
|
||||||
|
* WARNING: reducing MAX_WBITS makes minigzip unable to extract .gz files
|
||||||
|
* created by gzip. (Files created by minigzip can still be extracted by
|
||||||
|
* gzip.)
|
||||||
|
*/
|
||||||
|
#ifndef MAX_WBITS
|
||||||
|
# define MAX_WBITS 15 /* 32K LZ77 window */
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* The memory requirements for deflate are (in bytes):
|
||||||
|
(1 << (windowBits+2)) + (1 << (memLevel+9))
|
||||||
|
that is: 128K for windowBits=15 + 128K for memLevel = 8 (default values)
|
||||||
|
plus a few kilobytes for small objects. For example, if you want to reduce
|
||||||
|
the default memory requirements from 256K to 128K, compile with
|
||||||
|
make CFLAGS="-O -DMAX_WBITS=14 -DMAX_MEM_LEVEL=7"
|
||||||
|
Of course this will generally degrade compression (there's no free lunch).
|
||||||
|
|
||||||
|
The memory requirements for inflate are (in bytes) 1 << windowBits
|
||||||
|
that is, 32K for windowBits=15 (default value) plus a few kilobytes
|
||||||
|
for small objects.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/* Type declarations */
|
||||||
|
|
||||||
|
#ifndef OF /* function prototypes */
|
||||||
|
# ifdef STDC
|
||||||
|
# define OF(args) args
|
||||||
|
# else
|
||||||
|
# define OF(args) ()
|
||||||
|
# endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef Z_ARG /* function prototypes for stdarg */
|
||||||
|
# if defined(STDC) || defined(Z_HAVE_STDARG_H)
|
||||||
|
# define Z_ARG(args) args
|
||||||
|
# else
|
||||||
|
# define Z_ARG(args) ()
|
||||||
|
# endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* The following definitions for FAR are needed only for MSDOS mixed
|
||||||
|
* model programming (small or medium model with some far allocations).
|
||||||
|
* This was tested only with MSC; for other MSDOS compilers you may have
|
||||||
|
* to define NO_MEMCPY in zutil.h. If you don't need the mixed model,
|
||||||
|
* just define FAR to be empty.
|
||||||
|
*/
|
||||||
|
#ifdef SYS16BIT
|
||||||
|
# if defined(M_I86SM) || defined(M_I86MM)
|
||||||
|
/* MSC small or medium model */
|
||||||
|
# define SMALL_MEDIUM
|
||||||
|
# ifdef _MSC_VER
|
||||||
|
# define FAR _far
|
||||||
|
# else
|
||||||
|
# define FAR far
|
||||||
|
# endif
|
||||||
|
# endif
|
||||||
|
# if (defined(__SMALL__) || defined(__MEDIUM__))
|
||||||
|
/* Turbo C small or medium model */
|
||||||
|
# define SMALL_MEDIUM
|
||||||
|
# ifdef __BORLANDC__
|
||||||
|
# define FAR _far
|
||||||
|
# else
|
||||||
|
# define FAR far
|
||||||
|
# endif
|
||||||
|
# endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(WINDOWS) || defined(WIN32)
|
||||||
|
/* If building or using zlib as a DLL, define ZLIB_DLL.
|
||||||
|
* This is not mandatory, but it offers a little performance increase.
|
||||||
|
*/
|
||||||
|
# ifdef ZLIB_DLL
|
||||||
|
# if defined(WIN32) && (!defined(__BORLANDC__) || (__BORLANDC__ >= 0x500))
|
||||||
|
# ifdef ZLIB_INTERNAL
|
||||||
|
# define ZEXTERN extern __declspec(dllexport)
|
||||||
|
# else
|
||||||
|
# define ZEXTERN extern __declspec(dllimport)
|
||||||
|
# endif
|
||||||
|
# endif
|
||||||
|
# endif /* ZLIB_DLL */
|
||||||
|
/* If building or using zlib with the WINAPI/WINAPIV calling convention,
|
||||||
|
* define ZLIB_WINAPI.
|
||||||
|
* Caution: the standard ZLIB1.DLL is NOT compiled using ZLIB_WINAPI.
|
||||||
|
*/
|
||||||
|
# ifdef ZLIB_WINAPI
|
||||||
|
# ifdef FAR
|
||||||
|
# undef FAR
|
||||||
|
# endif
|
||||||
|
# include <windows.h>
|
||||||
|
/* No need for _export, use ZLIB.DEF instead. */
|
||||||
|
/* For complete Windows compatibility, use WINAPI, not __stdcall. */
|
||||||
|
# define ZEXPORT WINAPI
|
||||||
|
# ifdef WIN32
|
||||||
|
# define ZEXPORTVA WINAPIV
|
||||||
|
# else
|
||||||
|
# define ZEXPORTVA FAR CDECL
|
||||||
|
# endif
|
||||||
|
# endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined (__BEOS__)
|
||||||
|
# ifdef ZLIB_DLL
|
||||||
|
# ifdef ZLIB_INTERNAL
|
||||||
|
# define ZEXPORT __declspec(dllexport)
|
||||||
|
# define ZEXPORTVA __declspec(dllexport)
|
||||||
|
# else
|
||||||
|
# define ZEXPORT __declspec(dllimport)
|
||||||
|
# define ZEXPORTVA __declspec(dllimport)
|
||||||
|
# endif
|
||||||
|
# endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef ZEXTERN
|
||||||
|
# define ZEXTERN extern
|
||||||
|
#endif
|
||||||
|
#ifndef ZEXPORT
|
||||||
|
# define ZEXPORT
|
||||||
|
#endif
|
||||||
|
#ifndef ZEXPORTVA
|
||||||
|
# define ZEXPORTVA
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef FAR
|
||||||
|
# define FAR
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if !defined(__MACTYPES__)
|
||||||
|
typedef unsigned char Byte; /* 8 bits */
|
||||||
|
#endif
|
||||||
|
typedef unsigned int uInt; /* 16 bits or more */
|
||||||
|
typedef unsigned long uLong; /* 32 bits or more */
|
||||||
|
|
||||||
|
#ifdef SMALL_MEDIUM
|
||||||
|
/* Borland C/C++ and some old MSC versions ignore FAR inside typedef */
|
||||||
|
# define Bytef Byte FAR
|
||||||
|
#else
|
||||||
|
typedef Byte FAR Bytef;
|
||||||
|
#endif
|
||||||
|
typedef char FAR charf;
|
||||||
|
typedef int FAR intf;
|
||||||
|
typedef uInt FAR uIntf;
|
||||||
|
typedef uLong FAR uLongf;
|
||||||
|
|
||||||
|
#ifdef STDC
|
||||||
|
typedef void const *voidpc;
|
||||||
|
typedef void FAR *voidpf;
|
||||||
|
typedef void *voidp;
|
||||||
|
#else
|
||||||
|
typedef Byte const *voidpc;
|
||||||
|
typedef Byte FAR *voidpf;
|
||||||
|
typedef Byte *voidp;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if !defined(Z_U4) && !defined(Z_SOLO) && defined(STDC)
|
||||||
|
# include <limits.h>
|
||||||
|
# if (UINT_MAX == 0xffffffffUL)
|
||||||
|
# define Z_U4 unsigned
|
||||||
|
# elif (ULONG_MAX == 0xffffffffUL)
|
||||||
|
# define Z_U4 unsigned long
|
||||||
|
# elif (USHRT_MAX == 0xffffffffUL)
|
||||||
|
# define Z_U4 unsigned short
|
||||||
|
# endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef Z_U4
|
||||||
|
typedef Z_U4 z_crc_t;
|
||||||
|
#else
|
||||||
|
typedef unsigned long z_crc_t;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef HAVE_UNISTD_H /* may be set to #if 1 by ./configure */
|
||||||
|
# define Z_HAVE_UNISTD_H
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef HAVE_STDARG_H /* may be set to #if 1 by ./configure */
|
||||||
|
# define Z_HAVE_STDARG_H
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef STDC
|
||||||
|
# ifndef Z_SOLO
|
||||||
|
# include <sys/types.h> /* for off_t */
|
||||||
|
# endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(STDC) || defined(Z_HAVE_STDARG_H)
|
||||||
|
# ifndef Z_SOLO
|
||||||
|
# include <stdarg.h> /* for va_list */
|
||||||
|
# endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
|
# ifndef Z_SOLO
|
||||||
|
# include <stddef.h> /* for wchar_t */
|
||||||
|
# endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* a little trick to accommodate both "#define _LARGEFILE64_SOURCE" and
|
||||||
|
* "#define _LARGEFILE64_SOURCE 1" as requesting 64-bit operations, (even
|
||||||
|
* though the former does not conform to the LFS document), but considering
|
||||||
|
* both "#undef _LARGEFILE64_SOURCE" and "#define _LARGEFILE64_SOURCE 0" as
|
||||||
|
* equivalently requesting no 64-bit operations
|
||||||
|
*/
|
||||||
|
#if defined(_LARGEFILE64_SOURCE) && -_LARGEFILE64_SOURCE - -1 == 1
|
||||||
|
# undef _LARGEFILE64_SOURCE
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(__WATCOMC__) && !defined(Z_HAVE_UNISTD_H)
|
||||||
|
# define Z_HAVE_UNISTD_H
|
||||||
|
#endif
|
||||||
|
#ifndef Z_SOLO
|
||||||
|
# if defined(Z_HAVE_UNISTD_H) || defined(_LARGEFILE64_SOURCE)
|
||||||
|
# include <unistd.h> /* for SEEK_*, off_t, and _LFS64_LARGEFILE */
|
||||||
|
# ifdef VMS
|
||||||
|
# include <unixio.h> /* for off_t */
|
||||||
|
# endif
|
||||||
|
# ifndef z_off_t
|
||||||
|
# define z_off_t off_t
|
||||||
|
# endif
|
||||||
|
# endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(_LFS64_LARGEFILE) && _LFS64_LARGEFILE-0
|
||||||
|
# define Z_LFS64
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(_LARGEFILE64_SOURCE) && defined(Z_LFS64)
|
||||||
|
# define Z_LARGE64
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS-0 == 64 && defined(Z_LFS64)
|
||||||
|
# define Z_WANT64
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if !defined(SEEK_SET) && !defined(Z_SOLO)
|
||||||
|
# define SEEK_SET 0 /* Seek from beginning of file. */
|
||||||
|
# define SEEK_CUR 1 /* Seek from current position. */
|
||||||
|
# define SEEK_END 2 /* Set file pointer to EOF plus "offset" */
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef z_off_t
|
||||||
|
# define z_off_t long
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if !defined(_WIN32) && defined(Z_LARGE64)
|
||||||
|
# define z_off64_t off64_t
|
||||||
|
#else
|
||||||
|
# if defined(_WIN32) && !defined(__GNUC__) && !defined(Z_SOLO)
|
||||||
|
# define z_off64_t __int64
|
||||||
|
# else
|
||||||
|
# define z_off64_t z_off_t
|
||||||
|
# endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* MVS linker does not support external names larger than 8 bytes */
|
||||||
|
#if defined(__MVS__)
|
||||||
|
#pragma map(deflateInit_,"DEIN")
|
||||||
|
#pragma map(deflateInit2_,"DEIN2")
|
||||||
|
#pragma map(deflateEnd,"DEEND")
|
||||||
|
#pragma map(deflateBound,"DEBND")
|
||||||
|
#pragma map(inflateInit_,"ININ")
|
||||||
|
#pragma map(inflateInit2_,"ININ2")
|
||||||
|
#pragma map(inflateEnd,"INEND")
|
||||||
|
#pragma map(inflateSync,"INSY")
|
||||||
|
#pragma map(inflateSetDictionary,"INSEDI")
|
||||||
|
#pragma map(compressBound,"CMBND")
|
||||||
|
#pragma map(inflate_table,"INTABL")
|
||||||
|
#pragma map(inflate_fast,"INFA")
|
||||||
|
#pragma map(inflate_copyright,"INCOPY")
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif /* ZCONF_H */
|
1786
zlib/zlib.h
Normal file
1786
zlib/zlib.h
Normal file
File diff suppressed because it is too large
Load diff
324
zlib/zutil.c
Normal file
324
zlib/zutil.c
Normal file
|
@ -0,0 +1,324 @@
|
||||||
|
/* zutil.c -- target dependent utility functions for the compression library
|
||||||
|
* Copyright (C) 1995-2005, 2010, 2011, 2012 Jean-loup Gailly.
|
||||||
|
* For conditions of distribution and use, see copyright notice in zlib.h
|
||||||
|
*/
|
||||||
|
|
||||||
|
/* @(#) $Id$ */
|
||||||
|
|
||||||
|
#include "zutil.h"
|
||||||
|
#ifndef Z_SOLO
|
||||||
|
# include "gzguts.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef NO_DUMMY_DECL
|
||||||
|
struct internal_state {int dummy;}; /* for buggy compilers */
|
||||||
|
#endif
|
||||||
|
|
||||||
|
z_const char * const z_errmsg[10] = {
|
||||||
|
"need dictionary", /* Z_NEED_DICT 2 */
|
||||||
|
"stream end", /* Z_STREAM_END 1 */
|
||||||
|
"", /* Z_OK 0 */
|
||||||
|
"file error", /* Z_ERRNO (-1) */
|
||||||
|
"stream error", /* Z_STREAM_ERROR (-2) */
|
||||||
|
"data error", /* Z_DATA_ERROR (-3) */
|
||||||
|
"insufficient memory", /* Z_MEM_ERROR (-4) */
|
||||||
|
"buffer error", /* Z_BUF_ERROR (-5) */
|
||||||
|
"incompatible version",/* Z_VERSION_ERROR (-6) */
|
||||||
|
""};
|
||||||
|
|
||||||
|
|
||||||
|
const char * ZEXPORT zlibVersion()
|
||||||
|
{
|
||||||
|
return ZLIB_VERSION;
|
||||||
|
}
|
||||||
|
|
||||||
|
uLong ZEXPORT zlibCompileFlags()
|
||||||
|
{
|
||||||
|
uLong flags;
|
||||||
|
|
||||||
|
flags = 0;
|
||||||
|
switch ((int)(sizeof(uInt))) {
|
||||||
|
case 2: break;
|
||||||
|
case 4: flags += 1; break;
|
||||||
|
case 8: flags += 2; break;
|
||||||
|
default: flags += 3;
|
||||||
|
}
|
||||||
|
switch ((int)(sizeof(uLong))) {
|
||||||
|
case 2: break;
|
||||||
|
case 4: flags += 1 << 2; break;
|
||||||
|
case 8: flags += 2 << 2; break;
|
||||||
|
default: flags += 3 << 2;
|
||||||
|
}
|
||||||
|
switch ((int)(sizeof(voidpf))) {
|
||||||
|
case 2: break;
|
||||||
|
case 4: flags += 1 << 4; break;
|
||||||
|
case 8: flags += 2 << 4; break;
|
||||||
|
default: flags += 3 << 4;
|
||||||
|
}
|
||||||
|
switch ((int)(sizeof(z_off_t))) {
|
||||||
|
case 2: break;
|
||||||
|
case 4: flags += 1 << 6; break;
|
||||||
|
case 8: flags += 2 << 6; break;
|
||||||
|
default: flags += 3 << 6;
|
||||||
|
}
|
||||||
|
#ifdef DEBUG
|
||||||
|
flags += 1 << 8;
|
||||||
|
#endif
|
||||||
|
#if defined(ASMV) || defined(ASMINF)
|
||||||
|
flags += 1 << 9;
|
||||||
|
#endif
|
||||||
|
#ifdef ZLIB_WINAPI
|
||||||
|
flags += 1 << 10;
|
||||||
|
#endif
|
||||||
|
#ifdef BUILDFIXED
|
||||||
|
flags += 1 << 12;
|
||||||
|
#endif
|
||||||
|
#ifdef DYNAMIC_CRC_TABLE
|
||||||
|
flags += 1 << 13;
|
||||||
|
#endif
|
||||||
|
#ifdef NO_GZCOMPRESS
|
||||||
|
flags += 1L << 16;
|
||||||
|
#endif
|
||||||
|
#ifdef NO_GZIP
|
||||||
|
flags += 1L << 17;
|
||||||
|
#endif
|
||||||
|
#ifdef PKZIP_BUG_WORKAROUND
|
||||||
|
flags += 1L << 20;
|
||||||
|
#endif
|
||||||
|
#ifdef FASTEST
|
||||||
|
flags += 1L << 21;
|
||||||
|
#endif
|
||||||
|
#if defined(STDC) || defined(Z_HAVE_STDARG_H)
|
||||||
|
# ifdef NO_vsnprintf
|
||||||
|
flags += 1L << 25;
|
||||||
|
# ifdef HAS_vsprintf_void
|
||||||
|
flags += 1L << 26;
|
||||||
|
# endif
|
||||||
|
# else
|
||||||
|
# ifdef HAS_vsnprintf_void
|
||||||
|
flags += 1L << 26;
|
||||||
|
# endif
|
||||||
|
# endif
|
||||||
|
#else
|
||||||
|
flags += 1L << 24;
|
||||||
|
# ifdef NO_snprintf
|
||||||
|
flags += 1L << 25;
|
||||||
|
# ifdef HAS_sprintf_void
|
||||||
|
flags += 1L << 26;
|
||||||
|
# endif
|
||||||
|
# else
|
||||||
|
# ifdef HAS_snprintf_void
|
||||||
|
flags += 1L << 26;
|
||||||
|
# endif
|
||||||
|
# endif
|
||||||
|
#endif
|
||||||
|
return flags;
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef DEBUG
|
||||||
|
|
||||||
|
# ifndef verbose
|
||||||
|
# define verbose 0
|
||||||
|
# endif
|
||||||
|
int ZLIB_INTERNAL z_verbose = verbose;
|
||||||
|
|
||||||
|
void ZLIB_INTERNAL z_error (m)
|
||||||
|
char *m;
|
||||||
|
{
|
||||||
|
fprintf(stderr, "%s\n", m);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* exported to allow conversion of error code to string for compress() and
|
||||||
|
* uncompress()
|
||||||
|
*/
|
||||||
|
const char * ZEXPORT zError(err)
|
||||||
|
int err;
|
||||||
|
{
|
||||||
|
return ERR_MSG(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
#if defined(_WIN32_WCE)
|
||||||
|
/* The Microsoft C Run-Time Library for Windows CE doesn't have
|
||||||
|
* errno. We define it as a global variable to simplify porting.
|
||||||
|
* Its value is always 0 and should not be used.
|
||||||
|
*/
|
||||||
|
int errno = 0;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef HAVE_MEMCPY
|
||||||
|
|
||||||
|
void ZLIB_INTERNAL zmemcpy(dest, source, len)
|
||||||
|
Bytef* dest;
|
||||||
|
const Bytef* source;
|
||||||
|
uInt len;
|
||||||
|
{
|
||||||
|
if (len == 0) return;
|
||||||
|
do {
|
||||||
|
*dest++ = *source++; /* ??? to be unrolled */
|
||||||
|
} while (--len != 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
int ZLIB_INTERNAL zmemcmp(s1, s2, len)
|
||||||
|
const Bytef* s1;
|
||||||
|
const Bytef* s2;
|
||||||
|
uInt len;
|
||||||
|
{
|
||||||
|
uInt j;
|
||||||
|
|
||||||
|
for (j = 0; j < len; j++) {
|
||||||
|
if (s1[j] != s2[j]) return 2*(s1[j] > s2[j])-1;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ZLIB_INTERNAL zmemzero(dest, len)
|
||||||
|
Bytef* dest;
|
||||||
|
uInt len;
|
||||||
|
{
|
||||||
|
if (len == 0) return;
|
||||||
|
do {
|
||||||
|
*dest++ = 0; /* ??? to be unrolled */
|
||||||
|
} while (--len != 0);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef Z_SOLO
|
||||||
|
|
||||||
|
#ifdef SYS16BIT
|
||||||
|
|
||||||
|
#ifdef __TURBOC__
|
||||||
|
/* Turbo C in 16-bit mode */
|
||||||
|
|
||||||
|
# define MY_ZCALLOC
|
||||||
|
|
||||||
|
/* Turbo C malloc() does not allow dynamic allocation of 64K bytes
|
||||||
|
* and farmalloc(64K) returns a pointer with an offset of 8, so we
|
||||||
|
* must fix the pointer. Warning: the pointer must be put back to its
|
||||||
|
* original form in order to free it, use zcfree().
|
||||||
|
*/
|
||||||
|
|
||||||
|
#define MAX_PTR 10
|
||||||
|
/* 10*64K = 640K */
|
||||||
|
|
||||||
|
local int next_ptr = 0;
|
||||||
|
|
||||||
|
typedef struct ptr_table_s {
|
||||||
|
voidpf org_ptr;
|
||||||
|
voidpf new_ptr;
|
||||||
|
} ptr_table;
|
||||||
|
|
||||||
|
local ptr_table table[MAX_PTR];
|
||||||
|
/* This table is used to remember the original form of pointers
|
||||||
|
* to large buffers (64K). Such pointers are normalized with a zero offset.
|
||||||
|
* Since MSDOS is not a preemptive multitasking OS, this table is not
|
||||||
|
* protected from concurrent access. This hack doesn't work anyway on
|
||||||
|
* a protected system like OS/2. Use Microsoft C instead.
|
||||||
|
*/
|
||||||
|
|
||||||
|
voidpf ZLIB_INTERNAL zcalloc (voidpf opaque, unsigned items, unsigned size)
|
||||||
|
{
|
||||||
|
voidpf buf = opaque; /* just to make some compilers happy */
|
||||||
|
ulg bsize = (ulg)items*size;
|
||||||
|
|
||||||
|
/* If we allocate less than 65520 bytes, we assume that farmalloc
|
||||||
|
* will return a usable pointer which doesn't have to be normalized.
|
||||||
|
*/
|
||||||
|
if (bsize < 65520L) {
|
||||||
|
buf = farmalloc(bsize);
|
||||||
|
if (*(ush*)&buf != 0) return buf;
|
||||||
|
} else {
|
||||||
|
buf = farmalloc(bsize + 16L);
|
||||||
|
}
|
||||||
|
if (buf == NULL || next_ptr >= MAX_PTR) return NULL;
|
||||||
|
table[next_ptr].org_ptr = buf;
|
||||||
|
|
||||||
|
/* Normalize the pointer to seg:0 */
|
||||||
|
*((ush*)&buf+1) += ((ush)((uch*)buf-0) + 15) >> 4;
|
||||||
|
*(ush*)&buf = 0;
|
||||||
|
table[next_ptr++].new_ptr = buf;
|
||||||
|
return buf;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ZLIB_INTERNAL zcfree (voidpf opaque, voidpf ptr)
|
||||||
|
{
|
||||||
|
int n;
|
||||||
|
if (*(ush*)&ptr != 0) { /* object < 64K */
|
||||||
|
farfree(ptr);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
/* Find the original pointer */
|
||||||
|
for (n = 0; n < next_ptr; n++) {
|
||||||
|
if (ptr != table[n].new_ptr) continue;
|
||||||
|
|
||||||
|
farfree(table[n].org_ptr);
|
||||||
|
while (++n < next_ptr) {
|
||||||
|
table[n-1] = table[n];
|
||||||
|
}
|
||||||
|
next_ptr--;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
ptr = opaque; /* just to make some compilers happy */
|
||||||
|
Assert(0, "zcfree: ptr not found");
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif /* __TURBOC__ */
|
||||||
|
|
||||||
|
|
||||||
|
#ifdef M_I86
|
||||||
|
/* Microsoft C in 16-bit mode */
|
||||||
|
|
||||||
|
# define MY_ZCALLOC
|
||||||
|
|
||||||
|
#if (!defined(_MSC_VER) || (_MSC_VER <= 600))
|
||||||
|
# define _halloc halloc
|
||||||
|
# define _hfree hfree
|
||||||
|
#endif
|
||||||
|
|
||||||
|
voidpf ZLIB_INTERNAL zcalloc (voidpf opaque, uInt items, uInt size)
|
||||||
|
{
|
||||||
|
if (opaque) opaque = 0; /* to make compiler happy */
|
||||||
|
return _halloc((long)items, size);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ZLIB_INTERNAL zcfree (voidpf opaque, voidpf ptr)
|
||||||
|
{
|
||||||
|
if (opaque) opaque = 0; /* to make compiler happy */
|
||||||
|
_hfree(ptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif /* M_I86 */
|
||||||
|
|
||||||
|
#endif /* SYS16BIT */
|
||||||
|
|
||||||
|
|
||||||
|
#ifndef MY_ZCALLOC /* Any system without a special alloc function */
|
||||||
|
|
||||||
|
#ifndef STDC
|
||||||
|
extern voidp malloc OF((uInt size));
|
||||||
|
extern voidp calloc OF((uInt items, uInt size));
|
||||||
|
extern void free OF((voidpf ptr));
|
||||||
|
#endif
|
||||||
|
|
||||||
|
voidpf ZLIB_INTERNAL zcalloc (opaque, items, size)
|
||||||
|
voidpf opaque;
|
||||||
|
unsigned items;
|
||||||
|
unsigned size;
|
||||||
|
{
|
||||||
|
if (opaque) items += size - size; /* make compiler happy */
|
||||||
|
return sizeof(uInt) > 2 ? (voidpf)malloc(items * size) :
|
||||||
|
(voidpf)calloc(items, size);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ZLIB_INTERNAL zcfree (opaque, ptr)
|
||||||
|
voidpf opaque;
|
||||||
|
voidpf ptr;
|
||||||
|
{
|
||||||
|
free(ptr);
|
||||||
|
if (opaque) return; /* make compiler happy */
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif /* MY_ZCALLOC */
|
||||||
|
|
||||||
|
#endif /* !Z_SOLO */
|
253
zlib/zutil.h
Normal file
253
zlib/zutil.h
Normal file
|
@ -0,0 +1,253 @@
|
||||||
|
/* zutil.h -- internal interface and configuration of the compression library
|
||||||
|
* Copyright (C) 1995-2013 Jean-loup Gailly.
|
||||||
|
* For conditions of distribution and use, see copyright notice in zlib.h
|
||||||
|
*/
|
||||||
|
|
||||||
|
/* WARNING: this file should *not* be used by applications. It is
|
||||||
|
part of the implementation of the compression library and is
|
||||||
|
subject to change. Applications should only use zlib.h.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/* @(#) $Id$ */
|
||||||
|
|
||||||
|
#ifndef ZUTIL_H
|
||||||
|
#define ZUTIL_H
|
||||||
|
|
||||||
|
#ifdef HAVE_HIDDEN
|
||||||
|
# define ZLIB_INTERNAL __attribute__((visibility ("hidden")))
|
||||||
|
#else
|
||||||
|
# define ZLIB_INTERNAL
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include "zlib.h"
|
||||||
|
|
||||||
|
#if defined(STDC) && !defined(Z_SOLO)
|
||||||
|
# if !(defined(_WIN32_WCE) && defined(_MSC_VER))
|
||||||
|
# include <stddef.h>
|
||||||
|
# endif
|
||||||
|
# include <string.h>
|
||||||
|
# include <stdlib.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef Z_SOLO
|
||||||
|
typedef long ptrdiff_t; /* guess -- will be caught if guess is wrong */
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef local
|
||||||
|
# define local static
|
||||||
|
#endif
|
||||||
|
/* compile with -Dlocal if your debugger can't find static symbols */
|
||||||
|
|
||||||
|
typedef unsigned char uch;
|
||||||
|
typedef uch FAR uchf;
|
||||||
|
typedef unsigned short ush;
|
||||||
|
typedef ush FAR ushf;
|
||||||
|
typedef unsigned long ulg;
|
||||||
|
|
||||||
|
extern z_const char * const z_errmsg[10]; /* indexed by 2-zlib_error */
|
||||||
|
/* (size given to avoid silly warnings with Visual C++) */
|
||||||
|
|
||||||
|
#define ERR_MSG(err) z_errmsg[Z_NEED_DICT-(err)]
|
||||||
|
|
||||||
|
#define ERR_RETURN(strm,err) \
|
||||||
|
return (strm->msg = ERR_MSG(err), (err))
|
||||||
|
/* To be used only when the state is known to be valid */
|
||||||
|
|
||||||
|
/* common constants */
|
||||||
|
|
||||||
|
#ifndef DEF_WBITS
|
||||||
|
# define DEF_WBITS MAX_WBITS
|
||||||
|
#endif
|
||||||
|
/* default windowBits for decompression. MAX_WBITS is for compression only */
|
||||||
|
|
||||||
|
#if MAX_MEM_LEVEL >= 8
|
||||||
|
# define DEF_MEM_LEVEL 8
|
||||||
|
#else
|
||||||
|
# define DEF_MEM_LEVEL MAX_MEM_LEVEL
|
||||||
|
#endif
|
||||||
|
/* default memLevel */
|
||||||
|
|
||||||
|
#define STORED_BLOCK 0
|
||||||
|
#define STATIC_TREES 1
|
||||||
|
#define DYN_TREES 2
|
||||||
|
/* The three kinds of block type */
|
||||||
|
|
||||||
|
#define MIN_MATCH 3
|
||||||
|
#define MAX_MATCH 258
|
||||||
|
/* The minimum and maximum match lengths */
|
||||||
|
|
||||||
|
#define PRESET_DICT 0x20 /* preset dictionary flag in zlib header */
|
||||||
|
|
||||||
|
/* target dependencies */
|
||||||
|
|
||||||
|
#if defined(MSDOS) || (defined(WINDOWS) && !defined(WIN32))
|
||||||
|
# define OS_CODE 0x00
|
||||||
|
# ifndef Z_SOLO
|
||||||
|
# if defined(__TURBOC__) || defined(__BORLANDC__)
|
||||||
|
# if (__STDC__ == 1) && (defined(__LARGE__) || defined(__COMPACT__))
|
||||||
|
/* Allow compilation with ANSI keywords only enabled */
|
||||||
|
void _Cdecl farfree( void *block );
|
||||||
|
void *_Cdecl farmalloc( unsigned long nbytes );
|
||||||
|
# else
|
||||||
|
# include <alloc.h>
|
||||||
|
# endif
|
||||||
|
# else /* MSC or DJGPP */
|
||||||
|
# include <malloc.h>
|
||||||
|
# endif
|
||||||
|
# endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef AMIGA
|
||||||
|
# define OS_CODE 0x01
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(VAXC) || defined(VMS)
|
||||||
|
# define OS_CODE 0x02
|
||||||
|
# define F_OPEN(name, mode) \
|
||||||
|
fopen((name), (mode), "mbc=60", "ctx=stm", "rfm=fix", "mrs=512")
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(ATARI) || defined(atarist)
|
||||||
|
# define OS_CODE 0x05
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef OS2
|
||||||
|
# define OS_CODE 0x06
|
||||||
|
# if defined(M_I86) && !defined(Z_SOLO)
|
||||||
|
# include <malloc.h>
|
||||||
|
# endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(MACOS) || defined(TARGET_OS_MAC)
|
||||||
|
# define OS_CODE 0x07
|
||||||
|
# ifndef Z_SOLO
|
||||||
|
# if defined(__MWERKS__) && __dest_os != __be_os && __dest_os != __win32_os
|
||||||
|
# include <unix.h> /* for fdopen */
|
||||||
|
# else
|
||||||
|
# ifndef fdopen
|
||||||
|
# define fdopen(fd,mode) NULL /* No fdopen() */
|
||||||
|
# endif
|
||||||
|
# endif
|
||||||
|
# endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef TOPS20
|
||||||
|
# define OS_CODE 0x0a
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef WIN32
|
||||||
|
# ifndef __CYGWIN__ /* Cygwin is Unix, not Win32 */
|
||||||
|
# define OS_CODE 0x0b
|
||||||
|
# endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef __50SERIES /* Prime/PRIMOS */
|
||||||
|
# define OS_CODE 0x0f
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(_BEOS_) || defined(RISCOS)
|
||||||
|
# define fdopen(fd,mode) NULL /* No fdopen() */
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if (defined(_MSC_VER) && (_MSC_VER > 600)) && !defined __INTERIX
|
||||||
|
# if defined(_WIN32_WCE)
|
||||||
|
# define fdopen(fd,mode) NULL /* No fdopen() */
|
||||||
|
# ifndef _PTRDIFF_T_DEFINED
|
||||||
|
typedef int ptrdiff_t;
|
||||||
|
# define _PTRDIFF_T_DEFINED
|
||||||
|
# endif
|
||||||
|
# else
|
||||||
|
# define fdopen(fd,type) _fdopen(fd,type)
|
||||||
|
# endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(__BORLANDC__) && !defined(MSDOS)
|
||||||
|
#pragma warn -8004
|
||||||
|
#pragma warn -8008
|
||||||
|
#pragma warn -8066
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* provide prototypes for these when building zlib without LFS */
|
||||||
|
#if !defined(_WIN32) && \
|
||||||
|
(!defined(_LARGEFILE64_SOURCE) || _LFS64_LARGEFILE-0 == 0)
|
||||||
|
ZEXTERN uLong ZEXPORT adler32_combine64 OF((uLong, uLong, z_off_t));
|
||||||
|
ZEXTERN uLong ZEXPORT crc32_combine64 OF((uLong, uLong, z_off_t));
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* common defaults */
|
||||||
|
|
||||||
|
#ifndef OS_CODE
|
||||||
|
# define OS_CODE 0x03 /* assume Unix */
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef F_OPEN
|
||||||
|
# define F_OPEN(name, mode) fopen((name), (mode))
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* functions */
|
||||||
|
|
||||||
|
#if defined(pyr) || defined(Z_SOLO)
|
||||||
|
# define NO_MEMCPY
|
||||||
|
#endif
|
||||||
|
#if defined(SMALL_MEDIUM) && !defined(_MSC_VER) && !defined(__SC__)
|
||||||
|
/* Use our own functions for small and medium model with MSC <= 5.0.
|
||||||
|
* You may have to use the same strategy for Borland C (untested).
|
||||||
|
* The __SC__ check is for Symantec.
|
||||||
|
*/
|
||||||
|
# define NO_MEMCPY
|
||||||
|
#endif
|
||||||
|
#if defined(STDC) && !defined(HAVE_MEMCPY) && !defined(NO_MEMCPY)
|
||||||
|
# define HAVE_MEMCPY
|
||||||
|
#endif
|
||||||
|
#ifdef HAVE_MEMCPY
|
||||||
|
# ifdef SMALL_MEDIUM /* MSDOS small or medium model */
|
||||||
|
# define zmemcpy _fmemcpy
|
||||||
|
# define zmemcmp _fmemcmp
|
||||||
|
# define zmemzero(dest, len) _fmemset(dest, 0, len)
|
||||||
|
# else
|
||||||
|
# define zmemcpy memcpy
|
||||||
|
# define zmemcmp memcmp
|
||||||
|
# define zmemzero(dest, len) memset(dest, 0, len)
|
||||||
|
# endif
|
||||||
|
#else
|
||||||
|
void ZLIB_INTERNAL zmemcpy OF((Bytef* dest, const Bytef* source, uInt len));
|
||||||
|
int ZLIB_INTERNAL zmemcmp OF((const Bytef* s1, const Bytef* s2, uInt len));
|
||||||
|
void ZLIB_INTERNAL zmemzero OF((Bytef* dest, uInt len));
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* Diagnostic functions */
|
||||||
|
#ifdef DEBUG
|
||||||
|
# include <stdio.h>
|
||||||
|
extern int ZLIB_INTERNAL z_verbose;
|
||||||
|
extern void ZLIB_INTERNAL z_error OF((char *m));
|
||||||
|
# define Assert(cond,msg) {if(!(cond)) z_error(msg);}
|
||||||
|
# define Trace(x) {if (z_verbose>=0) fprintf x ;}
|
||||||
|
# define Tracev(x) {if (z_verbose>0) fprintf x ;}
|
||||||
|
# define Tracevv(x) {if (z_verbose>1) fprintf x ;}
|
||||||
|
# define Tracec(c,x) {if (z_verbose>0 && (c)) fprintf x ;}
|
||||||
|
# define Tracecv(c,x) {if (z_verbose>1 && (c)) fprintf x ;}
|
||||||
|
#else
|
||||||
|
# define Assert(cond,msg)
|
||||||
|
# define Trace(x)
|
||||||
|
# define Tracev(x)
|
||||||
|
# define Tracevv(x)
|
||||||
|
# define Tracec(c,x)
|
||||||
|
# define Tracecv(c,x)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef Z_SOLO
|
||||||
|
voidpf ZLIB_INTERNAL zcalloc OF((voidpf opaque, unsigned items,
|
||||||
|
unsigned size));
|
||||||
|
void ZLIB_INTERNAL zcfree OF((voidpf opaque, voidpf ptr));
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define ZALLOC(strm, items, size) \
|
||||||
|
(*((strm)->zalloc))((strm)->opaque, (items), (size))
|
||||||
|
#define ZFREE(strm, addr) (*((strm)->zfree))((strm)->opaque, (voidpf)(addr))
|
||||||
|
#define TRY_FREE(s, p) {if (p) ZFREE(s, p);}
|
||||||
|
|
||||||
|
/* Reverse the bytes in a 32-bit value */
|
||||||
|
#define ZSWAP32(q) ((((q) >> 24) & 0xff) + (((q) >> 8) & 0xff00) + \
|
||||||
|
(((q) & 0xff00) << 8) + (((q) & 0xff) << 24))
|
||||||
|
|
||||||
|
#endif /* ZUTIL_H */
|
Loading…
Add table
Add a link
Reference in a new issue