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

📄 compapi.cpp

📁 我根据comp430s改写的DLL源码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
#include <windows.h>
#include <stdio.h>

#include "compress.h" /* contains the rest of the include file declarations */

//int size;
//static HASH hashsize;
//static int  bits;

/*
 * The following two parameter tables are the hash table sizes and
 * maximum code values for various code bit-lengths.  The requirements
 * are that Hashsize[n] must be a prime number and Maxcode[n] must be less
 * than Maxhash[n].  Table occupancy factor is (Maxcode - 256)/Maxhash.
 * Note:  I am using a lower Maxcode for 16-bit codes in order to
 * keep the hash table size less than 64k entries.
 */
CONST HASH hs[] = {
  0x13FF,       /* 12-bit codes, 75% occupancy */
  0x26C3,       /* 13-bit codes, 80% occupancy */
  0x4A1D,       /* 14-bit codes, 85% occupancy */
  0x8D0D,       /* 15-bit codes, 90% occupancy */
  0xFFD9        /* 16-bit codes, 94% occupancy, 6% of code values unused */
};
#define Hashsize(maxb) (hs[(maxb) -MINBITS])

CONST INTCODE mc[] = {
  0x0FFF,       /* 12-bit codes */
  0x1FFF,       /* 13-bit codes */
  0x3FFF,       /* 14-bit codes */
  0x7FFF,       /* 15-bit codes */
  0xEFFF        /* 16-bit codes, 6% of code values unused */
};
#define Maxcode(maxb) (mc[(maxb) -MINBITS])

#ifdef __STDC__
#ifdef DEBUG
#define allocx(type, ptr, size) \
    (((ptr) = (type FAR *) emalloc((unsigned int)(size),sizeof(type))) == NULLPTR(type) \
    ?   (/*fprintf(stderr,"%s: "#ptr" -- ", prog_name), */NOMEM) : OK \
    )
#else
#define allocx(type,ptr,size) \
    (((ptr) = (type FAR *) emalloc((unsigned int)(size),sizeof(type))) == NULLPTR(type) \
    ? NOMEM : OK \
    )
#endif
#else
#define allocx(type,ptr,size) \
    (((ptr) = (type FAR *) emalloc((unsigned int)(size),sizeof(type))) == NULLPTR(type) \
    ? NOMEM : OK \
    )
#endif

#define free_array(type,ptr,offset) \
    if (ptr != NULLPTR(type)) { \
        efree((ALLOCTYPE FAR *)((ptr) + (offset))); \
        (ptr) = NULLPTR(type); \
    }

  /*
   * Macro to allocate new memory to a pointer with an offset value.
   */
#define alloc_array(type, ptr, size, offset) \
    ( allocx(type, ptr, (size) - (offset)) != OK \
      ? NOMEM \
      : (((ptr) -= (offset)), OK) \
    )

static char FAR *sfx = NULLPTR(char) ;
#define suffix(code)     sfx[code]


#if (SPLIT_PFX)
  static CODE FAR *pfx[2] = {NULLPTR(CODE), NULLPTR(CODE)};
#else
  static CODE FAR *pfx = NULLPTR(CODE);
#endif


#if (SPLIT_HT)
  static CODE FAR *ht[2] = {NULLPTR(CODE),NULLPTR(CODE)};
#else
  static CODE FAR *ht = NULLPTR(CODE);
#endif


int alloc_tables(INTCODE maxcode, HASH hashsize, COMP_PARAMS *pparams)
{
    //static INTCODE oldmaxcode;
    //static HASH oldhashsize;

  if (hashsize > pparams->oldhashsize) {
#if (SPLIT_HT)
      free_array(CODE,ht[1], 0);
      free_array(CODE,ht[0], 0);
#else
      free_array(CODE,ht, 0);
#endif
    pparams->oldhashsize = 0;
  }

    if (maxcode > pparams->oldmaxcode) {
#if (SPLIT_PFX)
        free_array(CODE,pfx[1], 128);
        free_array(CODE,pfx[0], 128);
#else
        free_array(CODE,pfx, 256);
#endif
        free_array(char,sfx, 256);

        if (   alloc_array(char, sfx, maxcode + 1, 256)
#if (SPLIT_PFX)
            || alloc_array(CODE, pfx[0], (maxcode + 1) / 2, 128)
            || alloc_array(CODE, pfx[1], (maxcode + 1) / 2, 128)
#else
            || alloc_array(CODE, pfx, (maxcode + 1), 256)
#endif
        ) {
            pparams->oldmaxcode = 0;
            exit_stat = NOMEM;
            return(NOMEM);
        }
        pparams->oldmaxcode = maxcode;
    }
    if (hashsize > pparams->oldhashsize) {
        if (
#if (SPLIT_HT)
            alloc_array(CODE, ht[0], (hashsize / 2) + 1, 0)
            || alloc_array(CODE, ht[1], hashsize / 2, 0)
#else
            alloc_array(CODE, ht, hashsize, 0)
#endif
        ) {
            pparams->oldhashsize = 0;
            exit_stat = NOMEM;
            return(NOMEM);
        }
        pparams->oldhashsize = hashsize;
    }
    return (OK);
}

#if (SPLIT_PFX)
    /*
     * We have to split pfx[] table in half,
     * because it's potentially larger than 64k bytes.
     */
#define prefix(code)   (pfx[(code) & 1][(code) >> 1])
#else
    /*
     * Then pfx[] can't be larger than 64k bytes,
     * or we don't care if it is, so we don't split.
     */
#define prefix(code) (pfx[code])
#endif


/* The initializing of the tables can be done quicker with memset() */
/* but this way is portable through out the memory models.          */
/* If you use Microsoft halloc() to allocate the arrays, then       */
/* include the pragma #pragma function(memset)  and make sure that  */
/* the length of the memory block is not greater than 64K.          */
/* This also means that you MUST compile in a model that makes the  */
/* default pointers to be far pointers (compact or large models).   */
/* See the file COMPUSI.DOS to modify function emalloc().           */

#if (SPLIT_HT)
    /*
     * We have to split ht[] hash table in half,
     * because it's potentially larger than 64k bytes.
     */
#define probe(hash)    (ht[(hash) & 1][(hash) >> 1])
#define init_tables() \
    { \
      hash = pparams->hashsize >> 1; \
      ht[0][hash] = 0; \
      while (hash--) ht[0][hash] = ht[1][hash] = 0; \
      pparams->highcode = ~(~(INTCODE)0 << (pparams->bits = INITBITS)); \
      pparams->nextfree = (pparams->block_compress ? FIRSTFREE : 256); \
    }

#else

    /*
     * Then ht[] can't be larger than 64k bytes,
     * or we don't care if it is, so we don't split.
     */
#define probe(hash) (ht[hash])
#define init_tables() \
    { \
      hash = pparams->hashsize; \
      while (hash--) ht[hash] = 0; \
      pparams->highcode = ~(~(INTCODE)0 << (pparams->bits = INITBITS)); \
      pparams->nextfree = (pparams->block_compress ? FIRSTFREE : 256); \
    }

#endif

#ifdef COMP40
/* table clear for block compress */
/* this is for adaptive reset present in version 4.0 joe release */
/* DjG, sets it up and returns TRUE to compress and FALSE to not compress */
int cl_block (COMP_PARAMS *pparams)     
{
    register long int rat;

    checkpoint = pparams->in_count + CHECK_GAP;
#ifdef DEBUG
	if ( debug ) {
        //fprintf ( stderr, "count: %ld, ratio: ", pparams->in_count );
        //prratio ( stderr, pparams->in_count, pparams->bytes_out );
		//fprintf ( stderr, "\n");
	}
#endif

    if(pparams->in_count > 0x007fffff) {	/* shift will overflow */
        rat = pparams->bytes_out >> 8;
        if(rat == 0)       /* Don't divide by zero */
            rat = 0x7fffffff;
        else
            rat = pparams->in_count / rat;
    }
    else
        rat = (pparams->in_count << 8) / pparams->bytes_out;  /* 8 fractional bits */

    if ( rat > pparams->ratio ){
        pparams->ratio = rat;
        return FALSE;
    }
    else {
        pparams->ratio = 0;
#ifdef DEBUG
        //if(debug)
    	//	fprintf ( stderr, "clear\n" );
#endif
        return TRUE;    /* clear the table */
    }
    return FALSE; /* don't clear the table */
}
#endif

/*
 * compress  to 
 *
 */
int compress(COMP_PARAMS *pparams)
{
    int c,adjbits;
    register HASH hash;
    register INTCODE code;
    HASH hashf[256];

    pparams->maxcode = Maxcode(pparams->maxbits);
    pparams->hashsize = Hashsize(pparams->maxbits);

#ifdef COMP40
/* Only needed for adaptive reset */
    checkpoint = CHECK_GAP;
    pparams->ratio = 0;
#endif

    adjbits = pparams->maxbits -10;
    for (c = 256; --c >= 0; ){
        hashf[c] = ((( c &0x7) << 7) ^ c) << adjbits;
    }
    exit_stat = OK;
    if (alloc_tables(pparams->maxcode, pparams->hashsize, pparams))  /* exit_stat already set */
        return NOMEM;
    init_tables();
    /* if not zcat or filter */
    if(is_list && !zcat_flg) {  /* Open output file */
    //    if (freopen(ofname, WRITE_FILE_TYPE, fp_zip) == NULL) {
      //      exit_stat = NOTOPENED;
        //    return NOTOPENED;
        //}
        //if (!quiet)
          //  fprintf(stderr, "%s: ",ifname);
    }
    //setvbuf(stdout,zbuf,_IOFBF,ZBUFSIZE);
 /*
   * Check the input stream for previously seen strings.  We keep
   * adding characters to the previously seen prefix string until we
   * get a character which forms a new (unseen) string.  We then send
   * the code for the previously seen prefix string, and add the new
   * string to our tables.  The check for previous strings is done by
   * hashing.  If the code for the hash value is unused, then we have
   * a new string.  If the code is used, we check to see if the prefix
   * and suffix values match the current input; if so, we have found
   * a previously seen string.  Otherwise, we have a hash collision,
   * and we try secondary hash probes until we either find the current
   * string, or we find an unused entry (which indicates a new string).
   */
    if (!pparams->nomagic) {
        fputc(magic_header[0], pparams->fp_zip);
		fputc(magic_header[1], pparams->fp_zip);
        fputc((char)(pparams->maxbits | pparams->block_compress), pparams->fp_zip);
        if(ferror(pparams->fp_zip)){  /* check it on entry */
            exit_stat = WRITEERR;
            return WRITEERR;
        }
        pparams->bytes_out = 3L;     /* includes 3-byte header mojo */
    }
    else
        pparams->bytes_out = 0L;      /* no 3-byte header mojo */
    pparams->in_count = 1L;
    pparams->offset = 0;

    if ((c = fgetc(pparams->fp_src)) == EOF) {

⌨️ 快捷键说明

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