--- pgp/src/zdeflate.c 2018/04/24 16:37:53 1.1 +++ pgp/src/zdeflate.c 2018/04/24 16:45:45 1.1.1.7 @@ -68,7 +68,11 @@ * attributes. */ +#include "zunzip.h" #include "zip.h" +#ifdef MACTC5 +#include "Macutil3.h" +#endif /* =========================================================================== * Configuration parameters @@ -120,7 +124,7 @@ typedef unsigned IPos; */ #ifndef DYN_ALLOC - uch far window[2L*WSIZE]; + uch far slide[2L*WSIZE]; /* 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 @@ -138,9 +142,10 @@ typedef unsigned IPos; Pos far head[HASH_SIZE]; /* Heads of the hash chains or NIL */ #else - uch far * near window = NULL; + uch far * near slide = NULL; Pos far * near prev = NULL; - Pos far * near head; + static void far *__slide, *__prev; + static Pos far * near head; #endif long block_start; @@ -163,7 +168,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 +240,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. */ @@ -255,7 +261,7 @@ local void check_match OF((IPos start, * (except for the last MIN_MATCH-1 bytes of the input file). */ #define INSERT_STRING(s, match_head) \ - (UPDATE_HASH(ins_h, window[(s) + MIN_MATCH-1]), \ + (UPDATE_HASH(ins_h, slide[(s) + MIN_MATCH-1]), \ prev[(s) & WMASK] = match_head = head[ins_h], \ head[ins_h] = (s)) @@ -272,15 +278,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"); - } - } + __slide = slide = (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 (slide == NULL || prev == NULL || head == NULL) { + err(ZE_MEM, "window allocation"); + } #endif /* DYN_ALLOC */ #ifdef ASM match_init(); /* initialize the asm code */ @@ -300,11 +304,11 @@ void lm_init (pack_level, flags) strstart = 0; block_start = 0L; -#if defined(MSDOS) && !defined(__32BIT__) +#if defined(MSDOS) && !defined(__32BIT__) && !defined(__GO32__) /* Can't read a 64K block under MSDOS */ - lookahead = read_buf((char*)window, (unsigned)WSIZE); + lookahead = read_buf((char*)slide, (unsigned)WSIZE); #else - lookahead = read_buf((char*)window, 2*WSIZE); + lookahead = read_buf((char*)slide, 2*WSIZE); #endif if (lookahead == 0 || lookahead == (unsigned)EOF) { eofile = 1, lookahead = 0; @@ -317,12 +321,29 @@ void lm_init (pack_level, flags) while (lookahead < MIN_LOOKAHEAD && !eofile) fill_window(); ins_h = 0; - for (j=0; j (IPos)MAX_DIST ? strstart - (IPos)MAX_DIST : NIL; /* Stop when cur_match becomes <= limit. To simplify the code, - * we prevent matches with the string of window index 0. + * we prevent matches with the string of slide index 0. */ #ifdef UNALIGNED_OK register ush scan_start = *(ush*)scan; @@ -363,7 +384,7 @@ int longest_match(cur_match) do { Assert(cur_match < strstart, "no future"); - match = window + cur_match; + match = slide + cur_match; /* Skip to next match if the match length cannot increase * or if the match length is less than 2: @@ -439,8 +460,8 @@ local void check_match(start, match, len int length; { /* check that the match is indeed a match */ - if (memcmp((char*)window + match, - (char*)window + start, length) != EQUAL) { + if (memcmp((char*)slide + match, + (char*)slide + start, length) != EQUAL) { fprintf(stderr, " start %d, match %d, length %d\n", start, match, length); @@ -448,7 +469,8 @@ local void check_match(start, match, len } if (verbose > 1) { fprintf(stderr,"\\[%d,%d]", start-match, length); - do { putc(window[start++], stderr); } while (--length != 0); + /* putc a macro, not safe to modify args!! */ + do { putc(slide[start], stderr); start++; } while (--length!=0); } } #else @@ -481,7 +503,7 @@ local void fill_window() /* By the IN assertion, the window is not empty so we can't confuse * more == 0 with more == 64K on a 16 bit machine. */ - memcpy((char*)window, (char*)window+WSIZE, (unsigned)WSIZE); + memcpy((char*)slide, (char*)slide+WSIZE, (unsigned)WSIZE); match_start -= WSIZE; strstart -= WSIZE; /* strstart - WSIZE >= WSIZE - 1 - lookahead >= WSIZE - MIN_LOOKAHEAD @@ -507,7 +529,7 @@ local void fill_window() #endif } /* At this point, more >= 2 */ - n = read_buf((char*)window+strstart+lookahead, more); + n = read_buf((char*)slide+strstart+lookahead, more); if (n == 0 || n == (unsigned)EOF) { eofile = 1; } else { @@ -520,7 +542,7 @@ local void fill_window() * IN assertion: strstart is set to the end of the current match. */ #define FLUSH_BLOCK(eof) \ - flush_block(block_start >= 0L ? (char*)&window[block_start] : (char*)NULL,\ + flush_block(block_start >= 0L ? (char*)&slide[block_start] : (char*)NULL,\ (long)strstart - block_start, (eof)) /* =========================================================================== @@ -535,9 +557,12 @@ ulg deflate() prev_length = MIN_MATCH-1; while (lookahead != 0) { - /* Insert the string window[strstart .. strstart+2] in the + /* Insert the string slide[strstart .. strstart+2] in the * dictionary, and set hash_head to the head of the hash chain: */ +#ifdef MACTC5 + mac_poll_for_break(); +#endif INSERT_STRING(strstart, hash_head); /* Find the longest match, discarding those <= prev_length. @@ -545,7 +570,7 @@ ulg deflate() */ if (hash_head != NIL && strstart - hash_head <= MAX_DIST) { /* To simplify the code, we prevent matches with the string - * of window index 0 (in particular we have to avoid a match + * of slide index 0 (in particular we have to avoid a match * of the string with itself at the start of the input file). */ match_length = longest_match (hash_head); @@ -570,7 +595,7 @@ ulg deflate() } while (--match_length != 0); } else { /* No match, output a literal byte */ - flush = ct_tally (0, window[strstart]); + flush = ct_tally (0, slide[strstart]); lookahead--; } strstart++; @@ -605,9 +630,12 @@ ulg deflate() /* Process the input block. */ while (lookahead != 0) { - /* Insert the string window[strstart .. strstart+2] in the + /* Insert the string slide[strstart .. strstart+2] in the * dictionary, and set hash_head to the head of the hash chain: */ +#ifdef MACTC5 + mac_poll_for_break(); +#endif INSERT_STRING(strstart, hash_head); /* Find the longest match, discarding those <= prev_length. @@ -618,7 +646,7 @@ ulg deflate() if (hash_head != NIL && prev_length < max_lazy_match && strstart - hash_head <= MAX_DIST) { /* To simplify the code, we prevent matches with the string - * of window index 0 (in particular we have to avoid a match + * of slide index 0 (in particular we have to avoid a match * of the string with itself at the start of the input file). */ match_length = longest_match (hash_head); @@ -657,26 +685,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])); + Tracevv((stderr,"%c",slide[strstart-1])); + if (ct_tally (0, slide[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 @@ -685,7 +718,7 @@ ulg deflate() */ while (lookahead < MIN_LOOKAHEAD && !eofile) fill_window(); } - if (match_available) ct_tally (0, window[strstart-1]); + if (match_available) ct_tally (0, slide[strstart-1]); return FLUSH_BLOCK(1); /* eof */ }