--- pgp/src/zdeflate.c 2018/04/24 16:37:53 1.1 +++ pgp/src/zdeflate.c 2018/04/24 16:41:16 1.1.1.5 @@ -68,6 +68,7 @@ * attributes. */ +#include "zunzip.h" #include "zip.h" /* =========================================================================== @@ -140,7 +141,8 @@ typedef unsigned IPos; #else uch far * near window = NULL; Pos far * near prev = NULL; - Pos far * near head; + static void far *__window, *__prev; + static Pos far * near head; #endif long block_start; @@ -163,7 +165,7 @@ unsigned int near prev_length; */ unsigned near strstart; /* start of string to insert */ - unsigned near match_start; /* start of matching string */ +unsigned near match_start; /* start of matching string */ local int near eofile; /* flag set at end of input file */ local unsigned near lookahead; /* number of valid bytes ahead in window */ @@ -235,6 +237,7 @@ local void fill_window OF((void)); local void check_match OF((IPos start, IPos match, int length)); #endif +#undef MIN #define MIN(a,b) ((a) <= (b) ? (a) : (b)) /* The arguments must not have side effects. */ @@ -272,15 +275,13 @@ void lm_init (pack_level, flags) /* Use dynamic allocation if compiler does not like big static arrays: */ #ifdef DYN_ALLOC - if (window == NULL) { - window = (uch far*) fcalloc(WSIZE, 2*sizeof(uch)); - prev = (Pos far*) fcalloc(WSIZE, sizeof(Pos)); - head = (Pos far*) fcalloc(HASH_SIZE, sizeof(Pos)); - - if (window == NULL || prev == NULL || head == NULL) { - err(ZE_MEM, "window allocation"); - } - } + __window = window = (uch far*) fcalloc(WSIZE*2*sizeof(uch)+16, 1); + __prev = prev = (Pos far*) fcalloc(WSIZE*sizeof(Pos)+16, 1); + head = (Pos far*) fcalloc(HASH_SIZE, sizeof(Pos)); + + if (window == NULL || prev == NULL || head == NULL) { + err(ZE_MEM, "window allocation"); + } #endif /* DYN_ALLOC */ #ifdef ASM match_init(); /* initialize the asm code */ @@ -323,6 +324,17 @@ void lm_init (pack_level, flags) */ } +void lm_free() +{ +#ifdef DYN_ALLOC + fcfree(__window); + fcfree(__prev); + fcfree(head); + window = NULL; + prev = head = NULL; +#endif +} + /* =========================================================================== * Set match_start to the longest match starting at the given string and * return its length. Matches shorter or equal to prev_length are discarded, @@ -657,26 +669,31 @@ ulg deflate() } while (--prev_length != 0); match_available = 0; match_length = MIN_MATCH-1; + strstart++; + if (flush) FLUSH_BLOCK(0), block_start = strstart; } else if (match_available) { /* If there was no match at the previous position, output a * single literal. If there was a match but the current match * is longer, truncate the previous match to a single literal. */ - flush = ct_tally (0, window[strstart-1]); Tracevv((stderr,"%c",window[strstart-1])); + if (ct_tally (0, window[strstart-1])) { + FLUSH_BLOCK(0), block_start = strstart; + } + strstart++; lookahead--; } else { /* There is no previous match to compare with, wait for * the next step to decide. */ match_available = 1; - flush = 0; + strstart++; lookahead--; } - if (flush) FLUSH_BLOCK(0), block_start = strstart; - strstart++; +#if 0 /* for pgp: disabled to allow compiling with -DDEBUG */ Assert (strstart <= isize && lookahead <= isize, "a bit too far"); +#endif /* Make sure that we always have enough lookahead, except * at the end of the input file. We need MAX_MATCH bytes