⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pgpzdeflate.c

📁 vc环境下的pgp源码
💻 C
📖 第 1 页 / 共 2 页
字号:
lm_free(ZDeflateContext *ctx)
{
	PGPContextRef cdkContext;

	cdkContext = ctx->cdkContext;

#ifdef DYN_ALLOC
	/* Wipe and free buffers */
    if (ctx->o_window != NULL) {
		pgpClearMemory(ctx->o_window, (WSIZE+8)*2*sizeof(PGPByte));
		pgpContextMemFree (cdkContext, ctx->o_window);
    }
    if (ctx->o_prev != NULL) {
		pgpClearMemory(ctx->o_prev, (WSIZE+8) * sizeof(Pos));
		pgpClearMemory(ctx->o_head, (HASH_SIZE+8) * sizeof(Pos));
		pgpContextMemFree (cdkContext, ctx->o_prev);
		pgpContextMemFree (cdkContext, ctx->o_head);
    }
#endif /* DYN_ALLOC */

	/* Wipe everything else */
	pgpClearMemory (ctx, sizeof(*ctx));
	pgpContextMemFree (cdkContext, ctx);
}

/* ===========================================================================
 * 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,
 * in which case the result is equal to prev_length and match_start is
 * garbage.
 * IN assertions: cur_match is the head of the hash chain for the current
 *   string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1
 */
#ifndef ASMV
/* For MSDOS, OS/2 and 386 Unix, an optimized version is in match.asm or
 * match.s. The code is functionally equivalent, so you can use the C version
 * if desired.  A 68000 version is in amiga/match_68.a -- this could be used
 * with other 68000 based systems such as Macintosh with a little effort.
 */
int
longest_match(ZDeflateContext *ctx, IPos cur_match)
{
    unsigned chain_length = ctx->max_chain_length; /* max hash chain length */
    register PGPByte *scan = ctx->window + ctx->strstart; /* current string */
    register PGPByte *match;                    /* matched string */
    register int len;                           /* length of current match */
    int best_len = ctx->prev_length;            /* best match length so far */
    IPos limit = ctx->strstart > (IPos)MAX_DIST ?
						ctx->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.
     */

/* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.
 * It is easy to get rid of this optimization if necessary.
 */
#if HASH_BITS < 8 || MAX_MATCH != 258
# error Code too clever
#endif

#if UNALIGNED_OK
    /* Compare two bytes at a time. Note: this is not always beneficial.
     * Try with and without -DUNALIGNED_OK to check.
     */
    register PGPByte *strend = ctx->window + ctx->strstart + MAX_MATCH - 1;
    register PGPUInt16 scan_start = *(PGPUInt16 *)scan;
    register PGPUInt16 scan_end   = *(PGPUInt16 *)(scan+best_len-1);
#else
    register PGPByte *strend = ctx->window + ctx->strstart + MAX_MATCH;
    register PGPByte scan_end1  = scan[best_len-1];
    register PGPByte scan_end   = scan[best_len];
#endif

    /* Do not waste too much time if we already have a good match: */
    if (ctx->prev_length >= ctx->good_match)
        chain_length >>= 2;
    ZipAssert(ctx->strstart <= ctx->window_size-MIN_LOOKAHEAD,
			  	"insufficient lookahead");

    do {
		ZipAssert(cur_match < ctx->strstart, "no future");
		match = ctx->window + cur_match;

        /* Skip to next match if the match length cannot increase
         * or if the match length is less than 2:
		 */
#if UNALIGNED_OK && MAX_MATCH == 258
		/* This code assumes sizeof(unsigned short) == 2. Do not use
         * UNALIGNED_OK if your compiler uses a different size.
         */
		if (*(PGPUInt16 *)(match+best_len-1) != scan_end ||
            *(PGPUInt16 *)match != scan_start) continue;

        /* It is not necessary to compare scan[2] and match[2] since they are
		 * always equal when the other bytes match, given that the hash keys
         * are equal and that HASH_BITS >= 8. Compare 2 bytes at a time at
         * strstart+3, +5, ... up to strstart+257. We check for insufficient
		 * lookahead only every 4th comparison; the 128th check will be made
		 * at strstart+257. If MAX_MATCH-2 is not a multiple of 8, it is
         * necessary to put more guard bytes at the end of the window, or
         * to check more often for insufficient lookahead.
         */
        scan++, match++;
        do {
        } while (*(PGPUInt16 *)(scan+=2) == *(PGPUInt16 *)(match+=2) &&
				 *(PGPUInt16 *)(scan+=2) == *(PGPUInt16 *)(match+=2) &&
                 *(PGPUInt16 *)(scan+=2) == *(PGPUInt16 *)(match+=2) &&
                 *(PGPUInt16 *)(scan+=2) == *(PGPUInt16 *)(match+=2) &&
                 scan < strend);
        /* The funny "do {}" generates better code on most compilers */

        /* Here, scan <= window+strstart+257 */
        ZipAssert(scan <= ctx->window+(unsigned)(ctx->window_size-1),
				  "wild scan");
        if (*scan == *match)
			scan++;

		len = (MAX_MATCH - 1) - (int)(strend-scan);
		scan = strend - (MAX_MATCH-1);

#else /* UNALIGNED_OK */

        if (match[best_len]   != scan_end  ||
			match[best_len-1] != scan_end1 ||
            *match            != *scan     ||
            *++match          != scan[1])      continue;

        /* The check at best_len-1 can be removed because it will be made
         * again later. (This heuristic is not always a win.)
         * It is not necessary to compare scan[2] and match[2] since they
		 * are always equal when the other bytes match, given that
         * the hash keys are equal and that HASH_BITS >= 8.
         */
        scan += 2, match++;

        /* We check for insufficient lookahead only every 8th comparison;
         * the 256th check will be made at strstart+258.
         */
        do {
        } while (*++scan == *++match && *++scan == *++match &&
                 *++scan == *++match && *++scan == *++match &&
				 *++scan == *++match && *++scan == *++match &&
                 *++scan == *++match && *++scan == *++match &&
                 scan < strend);

        len = MAX_MATCH - (int)(strend - scan);
        scan = strend - MAX_MATCH;

#endif /* UNALIGNED_OK */

		if (len > best_len) {
			ctx->match_start = cur_match;
			best_len = len;
			if (len >= ctx->nice_match)
				break;
#if UNALIGNED_OK
			scan_end = *(PGPUInt16 *)(scan+best_len-1);
#else
			scan_end1  = scan[best_len-1];
			scan_end   = scan[best_len];
#endif
		}
    } while ((cur_match = ctx->prev[cur_match & WMASK]) > limit
             && --chain_length != 0);

    return best_len;
}
#endif /* ASMV */

#ifdef ZIPDEBUG
/* ===========================================================================
 * Check that the match at match_start is indeed a match.
 */
static void
check_match(ZDeflateContext *ctx, IPos start, IPos match, int length)
{
    /* check that the match is indeed a match */
    if (memcmp(ctx->window + match, ctx->window + start, length) != 0) {
        fprintf(stderr, " start %d, match %d, length %d\n",
                start, match, length);
        error("invalid match");
    }
    if (ctx->verbose > 1) {
		fprintf(stderr,"\\[%d,%d]", start-match, length);
		do { putc(ctx->window[start++], stderr); } while (--length != 0);
    }
}
#else
#  define check_match(ctx, start, match, length)
#endif

static void
slide_window(ZDeflateContext *ctx)
{
    register unsigned n, m;

    memcpy(ctx->window, ctx->window+WSIZE, (size_t)WSIZE);
    ctx->match_start -= WSIZE;
    ctx->strstart    -= WSIZE; /* we now have strstart >= MAX_DIST: */

    ctx->block_start -= (long) WSIZE;

    for (n = 0; n < HASH_SIZE; n++) {
		m = ctx->head[n];
		ctx->head[n] = (Pos)(m >= WSIZE ? m-WSIZE : NIL);
    }
    for (n = 0; n < WSIZE; n++) {
		m = ctx->prev[n];
		ctx->prev[n] = (Pos)(m >= WSIZE ? m-WSIZE : NIL);
		/* If n is not on any hash chain, prev[n] is garbage but
		 * its value will never be used.
		 */
    }
}

/* ===========================================================================
 * Flush the current block, with given end-of-file flag.
 * IN assertion: strstart is set to the end of the current match.
 */
#define FLUSH_BLOCK(ctx, ztctx, zbctx, eof) \
   flush_block((ztctx), (zbctx), (ctx)->block_start >= 0L ? \
		(char*)&(ctx)->window[(unsigned)(ctx)->block_start] : \
		(char*)NULL, (PGPUInt32)(ctx)->strstart - (ctx)->block_start, (eof), \
		(ctx)->window)

/* ===========================================================================
 * Same as above, but achieves better compression. We use a lazy
 * evaluation for matches: a match is finally adopted only if there is
 * no better match at the next window position.
 */
static int
deflate_sub(ZDeflateContext *ctx, struct ZTreesContext *ztcontext,
	struct ZBitsContext *zbcontext)
{
    IPos hash_head;          /* head of hash chain */
    IPos prev_match;         /* previous match */
    int flush;               /* set if current block must be flushed */
    int match_available = 0; /* set if previous match exists */
    register unsigned match_length = MIN_MATCH-1; /* length of best match */

#if 0
    if (ctx->level <= 3) return deflate_fast(ctx); /* optimized for speed */
#endif

    match_available = ctx->s_match_available;
    match_length = ctx->s_match_length;

    /* Process the input block. */
    while (ctx->lookahead != 0) {
        /* Insert the string window[strstart .. strstart+2] in the
         * dictionary, and set hash_head to the head of the hash chain:
         */
        INSERT_STRING(ctx, ctx->strstart, hash_head);

		/* Find the longest match, discarding those <= prev_length.
		 */
		ctx->prev_length = match_length, prev_match = ctx->match_start;
        match_length = MIN_MATCH-1;

        if (hash_head != NIL && ctx->prev_length < ctx->max_lazy_match &&
			ctx->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 the string with itself at the start of the input file).
             */
            match_length = longest_match (ctx, hash_head);
            /* longest_match() sets match_start */
            if (match_length > ctx->lookahead) match_length = ctx->lookahead;

            /* Ignore a length 3 match if it is too distant: */
            if (match_length == MIN_MATCH &&
				ctx->strstart-ctx->match_start > TOO_FAR){
                /* If prev_match is also MIN_MATCH, match_start is garbage
                 * but we will ignore the current match anyway.
				 */
				match_length--;
			}
        }
        /* If there was a match at the previous step and the current
         * match is not better, output the previous match:
         */
		if (ctx->prev_length >= MIN_MATCH &&
			match_length <= ctx->prev_length) {

            check_match(ctx, ctx->strstart-1, prev_match, ctx->prev_length);

            flush = ct_tally(ztcontext, ctx->strstart-1-prev_match,
							 ctx->prev_length - MIN_MATCH,
							 ctx->block_start, ctx->strstart);

            /* Insert in hash table all strings up to the end of the match.
             * strstart-1 and strstart are already inserted.
			 */
            ctx->lookahead -= ctx->prev_length-1;
			ctx->prev_length -= 2;
            do {
                ctx->strstart++;
                INSERT_STRING(ctx, ctx->strstart, hash_head);
				/* strstart never exceeds WSIZE-MAX_MATCH, so there are
                 * always MIN_MATCH bytes ahead. If lookahead < MIN_MATCH
                 * these bytes are garbage, but it does not matter since the
				 * next lookahead bytes will always be emitted as literals.
                 */
            } while (--ctx->prev_length != 0);
            match_available = 0;
            match_length = MIN_MATCH-1;
            ctx->strstart++;
            if (flush) {
				FLUSH_BLOCK(ctx, ztcontext, zbcontext, 0);
				ctx->block_start = ctx->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.
             */
            Tracevv((stderr,"%c",ctx->window[ctx->strstart-1]));
            if (ct_tally (ztcontext, 0, ctx->window[ctx->strstart-1],
						  ctx->block_start, ctx->strstart)) {
                FLUSH_BLOCK(ctx, ztcontext, zbcontext, 0);
				ctx->block_start = ctx->strstart;
            }
			ctx->strstart++;
			ctx->lookahead--;
        } else {
            /* There is no previous match to compare with, wait for
             * the next step to decide.
             */
			match_available = 1;
			ctx->strstart++;
			ctx->lookahead--;
		}

		/* Make sure that we always have enough lookahead, except
		 * at the end of the input file. We need MAX_MATCH bytes
		 * for the next match, plus MIN_MATCH bytes to insert the
		 * string following the next match.
		 */
		if (ctx->lookahead < MIN_LOOKAHEAD && !ctx->eofile) {
			ctx->s_match_available = match_available;
			ctx->s_match_length = match_length;
			return 0;
			/* fill_window();*/
		}
    }
    if (match_available) ct_tally (ztcontext, 0, ctx->window[ctx->strstart-1],
								   ctx->block_start, ctx->strstart);

    return FLUSH_BLOCK(ctx, ztcontext, zbcontext, 1); /* eof */
}


/* Zip a buffer of input */
void
zip_input(ZDeflateContext *ctx, struct ZTreesContext *ztcontext,
	struct ZBitsContext *zbcontext, char const *buf, unsigned len)
{
    unsigned more;

    while (len) {
		more = (unsigned)((PGPUInt32)2*WSIZE - ctx->lookahead - ctx->strstart);

		if (more <= 1) {
			slide_window(ctx);
			more += WSIZE;
		}
		if (more > len)
			more = len;
		memcpy(ctx->window+ctx->strstart+ctx->lookahead, buf, more);
		ctx->lookahead += more;

		if (ctx->lookahead >= MIN_LOOKAHEAD) {
			if (!ctx->strstart) {	/* First time */
				register int j;
				ctx->ins_h = 0;
				for (j=0; j<MIN_MATCH-1; j++)
					UPDATE_HASH(ctx->ins_h, ctx->window[j]);
			}
			deflate_sub(ctx, ztcontext, zbcontext);
        }
		buf += more;
		len -= more;
    }

    return;
}

/* Zip EOF */
void
zip_finish(ZDeflateContext *ctx, struct ZTreesContext *ztcontext,
	struct ZBitsContext *zbcontext)
{
    ctx->eofile = 1;

    if (!ctx->strstart) {	/* First time */
		register int j;
		ctx->ins_h = 0;
		for (j=0; j<MIN_MATCH-1; j++)
			UPDATE_HASH(ctx->ins_h, ctx->window[j]);
    }

    deflate_sub(ctx, ztcontext, zbcontext);

    return;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -