Esempio n. 1
0
static void
output(int code)
{
    cur_accum &= masks[cur_bits];

    if (cur_bits > 0)
        cur_accum |= ((long)code << cur_bits);
    else
        cur_accum = code;

    cur_bits += n_bits;

    while( cur_bits >= 8 ) {
        char_out( (int)((unsigned int) cur_accum & 0xff) );
        cur_accum >>= 8;
        cur_bits -= 8;
    }

    /*
     * If the next entry is going to be too big for the code size, then
     * increase it, if possible.
     */
    if (free_ent > maxcode || clear_flg) {
        if (clear_flg) {
            maxcode = MAXCODE (n_bits = g_init_bits);
            clear_flg = 0;
        } else {
            n_bits++;

            if ( n_bits == maxbits )
                maxcode = maxmaxcode;
            else
                maxcode = MAXCODE(n_bits);
        }
    }

    if (code == EOFCode) {
        /* At EOF, write the rest of the buffer */
        while( cur_bits > 0 ) {
            char_out( (int)((unsigned int)cur_accum & 0xff) );
            cur_accum >>= 8;
            cur_bits -= 8;
        }

        flush_char();
        fflush( g_outfile );

#ifdef FOO
        if(ferror( g_outfile))
            FatalError("unable to write GIF file");
#endif
    }
}
Esempio n. 2
0
static void output(int code, struct aap *a)
{
  a->cur_accum &= masks[a->cur_bits];

  if (a->cur_bits > 0)
    a->cur_accum |= ((long)code << a->cur_bits);
  else
    a->cur_accum = code;
	
  a->cur_bits += a->n_bits;

  while( a->cur_bits >= 8 ) {
    char_out( (int) (a->cur_accum & 0xff), a );
    a->cur_accum >>= 8;
    a->cur_bits -= 8;
  }

  /*
   * If the next entry is going to be too big for the code size,
   * then increase it, if possible.
   */

  if (a->free_ent > a->maxcode || a->clear_flg) {

    if( a->clear_flg ) {
      a->maxcode = MAXCODE (a->n_bits = a->g_init_bits);
      a->clear_flg = 0;
    }
    else {
      a->n_bits++;
      if ( a->n_bits == XV_BITS )
	a->maxcode = (1<<XV_BITS);
      else
	a->maxcode = MAXCODE(a->n_bits);
    }
  }
	
  if( code == a->EOFCode ) {
    /* At EOF, write the rest of the buffer */
    while( a->cur_bits > 0 ) {
      char_out( (int)(a->cur_accum & 0xff), a );
      a->cur_accum >>= 8;
      a->cur_bits -= 8;
    }

    flush_char(a);
	
    fflush( a->g_outfile );

  }
}
Esempio n. 3
0
LOCAL void
output (gif_dest_ptr dinfo, code_int code)
/* Emit a code of n_bits bits */
/* Uses cur_accum and cur_bits to reblock into 8-bit bytes */
{
  dinfo->cur_accum |= ((INT32) code) << dinfo->cur_bits;
  dinfo->cur_bits += dinfo->n_bits;

  while (dinfo->cur_bits >= 8) {
    CHAR_OUT(dinfo, dinfo->cur_accum & 0xFF);
    dinfo->cur_accum >>= 8;
    dinfo->cur_bits -= 8;
  }

  /*
   * If the next entry is going to be too big for the code size,
   * then increase it, if possible.  We do this here to ensure
   * that it's done in sync with the decoder's codesize increases.
   */
  if (dinfo->free_code > dinfo->maxcode) {
    dinfo->n_bits++;
    if (dinfo->n_bits == MAX_LZW_BITS)
      dinfo->maxcode = LZW_TABLE_SIZE; /* free_code will never exceed this */
    else
      dinfo->maxcode = MAXCODE(dinfo->n_bits);
  }
}
Esempio n. 4
0
int CsObjectInt::InitComp (BYTE_TYP * outbuf,
                     SAP_INT    outlen,
                     SAP_INT    sumlen)
/*--------------------------------------------------------------------*/
/* Setup header info                                                  */
/* Clear hash table                                                   */
/* Initialize static variables for compression                        */
/*--------------------------------------------------------------------*/
{
  if (outlen < CS_HEAD_SIZE)       /* too small ......................*/
    return CS_E_OUT_BUFFER_LEN;

  if (sumlen <= 0L)
    return CS_E_INVALID_SUMLEN;

  csc.clear_flg      = 0;              /* init compression states ........*/
  csc.ratio          = 0;

  csc.block_compress = BLOCK_MASK;
  csc.maxbits        = CS_BITS;

  csc.checkpoint     = CHECK_GAP;
  csc.maxcode        = MAXCODE (csc.n_bits = INIT_CS_BITS);
  csc.maxmaxcode     = (CODE_INT)1 << CS_BITS;
  csc.free_ent       = ((csc.block_compress) ? FIRST : 256);
  csc.hsize          = HSIZE;
  CL_HASH (csc.hsize);         /* clear hash table .......................*/
                           /* fill in header informations ............*/
  CsSetHead (outbuf, sumlen,
             (BYTE_TYP) ((CS_VERSION << 4) | CS_ALGORITHM),
             (BYTE_TYP) (csc.maxbits | csc.block_compress));

  return 0;
}
Esempio n. 5
0
//-----------------------------------------------------------------
// write an LZW code 
void mgLZWEncode::writeCode(
  mgLZWCode nCode)
{
  // Uses nCurAccum and nCurBits to reblock into 8-bit bytes
  m_curAccum |= ((long) nCode) << m_curBits;
  m_curBits += m_bits;

  while (m_curBits >= 8) 
  {
    writeLZWByte(m_curAccum & 0xFF);
    m_curAccum >>= 8;
    m_curBits -= 8;
  }

  // If the next entry is going to be too big for the code size,
  // then increase it, if possible.  We do this here to ensure
  // that it's done in sync with the decoder's codesize increases.

  if (m_freeCode > m_maxCode) 
  {
    m_bits++;
    if (m_bits == MAX_LZW_BITS)
      m_maxCode = LZW_TABLE_SIZE; // free_code will never exceed this
    else m_maxCode = MAXCODE(m_bits);
  }
}
Esempio n. 6
0
/*
 * Reset encoding state at the start of a strip.
 */
static int
LZWPreEncode(TIFF* tif, uint16 s)
{
	LZWCodecState *sp = EncoderState(tif);

	(void) s;
	assert(sp != NULL);

	if( sp->enc_hashtab == NULL )
        {
            tif->tif_setupencode( tif );
        }

	sp->lzw_nbits = BITS_MIN;
	sp->lzw_maxcode = MAXCODE(BITS_MIN);
	sp->lzw_free_ent = CODE_FIRST;
	sp->lzw_nextbits = 0;
	sp->lzw_nextdata = 0;
	sp->enc_checkpoint = CHECK_GAP;
	sp->enc_ratio = 0;
	sp->enc_incount = 0;
	sp->enc_outcount = 0;
	/*
	 * The 4 here insures there is space for 2 max-sized
	 * codes in LZWEncode and LZWPostDecode.
	 */
	sp->enc_rawlimit = tif->tif_rawdata + tif->tif_rawdatasize-1 - 4;
	cl_hash(sp);		/* clear hash table */
	sp->enc_oldcode = (hcode_t) -1;	/* generates CODE_CLEAR in LZWEncode */
	return (1);
}
Esempio n. 7
0
//-----------------------------------------------------------------
// reset compressor and issue a clear code
void mgLZWEncode::clearBlock()
{
  clearHash();			// delete all the symbols 

  m_freeCode = m_clearCode + 2;
  writeCode(m_clearCode);	// inform decoder 
  m_bits = m_initBits;	// reset code size 
  m_maxCode = MAXCODE(m_bits);
}
Esempio n. 8
0
clear_block (gif_dest_ptr dinfo)
/* Reset compressor and issue a Clear code */
{
  clear_hash(dinfo);			/* delete all the symbols */
  dinfo->free_code = dinfo->ClearCode + 2;
  output(dinfo, dinfo->ClearCode);	/* inform decoder */
  dinfo->n_bits = dinfo->init_bits;	/* reset code size */
  dinfo->maxcode = MAXCODE(dinfo->n_bits);
}
Esempio n. 9
0
compress_init (gif_dest_ptr dinfo, int i_bits)
{
  
  dinfo->n_bits = i_bits;
  dinfo->maxcode = MAXCODE(dinfo->n_bits);
  dinfo->ClearCode = (1 << (i_bits - 1));
  dinfo->EOFCode = dinfo->ClearCode + 1;
  dinfo->code_counter = dinfo->ClearCode + 2;
  
  dinfo->bytesinpkt = 0;
  dinfo->cur_accum = 0;
  dinfo->cur_bits = 0;
  
  output(dinfo, dinfo->ClearCode);
}
Esempio n. 10
0
compress_init (gif_dest_ptr dinfo, int i_bits)
/* Initialize pseudo-compressor */
{
  /* init all the state variables */
  dinfo->n_bits = i_bits;
  dinfo->maxcode = MAXCODE(dinfo->n_bits);
  dinfo->ClearCode = (1 << (i_bits - 1));
  dinfo->EOFCode = dinfo->ClearCode + 1;
  dinfo->code_counter = dinfo->ClearCode + 2;
  /* init output buffering vars */
  dinfo->bytesinpkt = 0;
  dinfo->cur_accum = 0;
  dinfo->cur_bits = 0;
  /* GIF specifies an initial Clear code */
  output(dinfo, dinfo->ClearCode);
}
Esempio n. 11
0
//-----------------------------------------------------------------
// constructor
mgLZWEncode::mgLZWEncode()
{
  m_hashCode = new mgLZWCode[HASH_SIZE];
  m_hashValue = new mgLZWHash[HASH_SIZE];

  // init all the state variables
  m_bits = m_initBits = 9;  // data size + 1
  m_maxCode = MAXCODE(m_bits);
  m_clearCode = ((mgLZWCode) 1 << (m_initBits - 1));
  m_EOFCode = m_clearCode + 1;
  m_freeCode = m_clearCode + 2;
  m_firstByte = TRUE;	// no waiting symbol yet

  // init output buffering vars
  m_curAccum = 0;
  m_curBits = 0;

  clearHash();        // clear hash table
}
Esempio n. 12
0
compress_init (gif_dest_ptr dinfo, int i_bits)
/* Initialize LZW compressor */
{
  /* init all the state variables */
  dinfo->n_bits = dinfo->init_bits = i_bits;
  dinfo->maxcode = MAXCODE(dinfo->n_bits);
  dinfo->ClearCode = ((code_int) 1 << (i_bits - 1));
  dinfo->EOFCode = dinfo->ClearCode + 1;
  dinfo->free_code = dinfo->ClearCode + 2;
  dinfo->first_byte = TRUE;	/* no waiting symbol yet */
  /* init output buffering vars */
  dinfo->bytesinpkt = 0;
  dinfo->cur_accum = 0;
  dinfo->cur_bits = 0;
  /* clear hash table */
  clear_hash(dinfo);
  /* GIF specifies an initial Clear code */
  output(dinfo, dinfo->ClearCode);
}
Esempio n. 13
0
int lzwInit(lzw_streamp strm)
{
    struct lzw_internal_state *state;
    hcode_t code;

    state = cli_malloc(sizeof(struct lzw_internal_state));
    if (state == NULL) {
        strm->msg = "failed to allocate state";
        return LZW_MEM_ERROR;
    }

    /* general state setup */
    state->nbits = BITS_MIN;
    state->nextdata = 0;
    state->nextbits = 0;

    /* dictionary setup */
    state->dec_codetab = cli_calloc(CSIZE, sizeof(code_t));
    if (state->dec_codetab == NULL) {
        free(state);
        strm->msg = "failed to allocate code table";
        return LZW_MEM_ERROR;
    }

    for (code = 0; code < CODE_BASIC; code++) {
        state->dec_codetab[code].next = NULL;
        state->dec_codetab[code].length = 1;
        state->dec_codetab[code].value = code;
        state->dec_codetab[code].firstchar = code;
    }

    state->dec_restart = 0;
    state->dec_nbitsmask = MAXCODE(BITS_MIN);
    state->dec_free_entp = state->dec_codetab + CODE_FIRST;
    state->dec_oldcodep = &state->dec_codetab[CODE_CLEAR];
    state->dec_maxcodep = &state->dec_codetab[state->dec_nbitsmask-1];

    strm->state = state;
    return LZW_OK;
}
Esempio n. 14
0
static void compress(int init_bits, FILE *outfile, byte *data, int len)
{
    register long fcode;
    register int i = 0;
    register int c;
    register int ent;
    register int disp;
    register int hsize_reg;
    register int hshift;

    /*
     * Set up the globals:  g_init_bits - initial number of bits g_outfile -
     * pointer to output file
     */
    g_init_bits = init_bits;
    g_outfile   = outfile;

    /* initialize 'compress' globals */
    maxbits = XV_BITS;
    maxmaxcode = 1<<XV_BITS;
    memset(htab, 0, sizeof(htab));
    memset(codetab, 0, sizeof(codetab));
    hsize = HSIZE;
    free_ent = 0;
    clear_flg = 0;
    in_count = 1;
    out_count = 0;
    cur_accum = 0;
    cur_bits = 0;

    /* Set up the necessary values */
    out_count = 0;
    clear_flg = 0;
    in_count = 1;
    maxcode = MAXCODE(n_bits = g_init_bits);

    ClearCode = (1 << (init_bits - 1));
    EOFCode = ClearCode + 1;
    free_ent = ClearCode + 2;

    char_init();
    ent = pc2nc[*data++];
    len--;

    hshift = 0;
    for (fcode = (long)hsize; fcode < 65536L; fcode *= 2L )
        hshift++;

    hshift = 8 - hshift; /* set hash code range bound */

    hsize_reg = hsize;
    cl_hash( (count_int) hsize_reg); /* clear hash table */

    output(ClearCode);

    while (len) {
        c = pc2nc[*data++];
        len--;
        in_count++;

        fcode = (long)(((long) c << maxbits) + ent);
        i = (((int) c << hshift) ^ ent);    /* xor hashing */

        if ( HashTabOf (i) == fcode ) {
            ent = CodeTabOf (i);
            continue;
        } else if ( (long)HashTabOf (i) < 0) {
            /* empty slot */
            goto nomatch;
        }

        disp = hsize_reg - i;   /* secondary hash (after G. Knott) */

        if ( i == 0 )
            disp = 1;

probe:
        if ((i -= disp) < 0)
            i += hsize_reg;

        if (HashTabOf (i) == fcode) {
            ent = CodeTabOf (i);
            continue;
        }

        if ((long)HashTabOf (i) >= 0)
            goto probe;

nomatch:
        output(ent);
        out_count++;
        ent = c;

        if (free_ent < maxmaxcode) {
            CodeTabOf (i) = free_ent++; /* code -> hashtable */
            HashTabOf (i) = fcode;
        } else {
            cl_block();
        }
    }

    /* Put out the final code */
    output(ent);
    out_count++;
    output(EOFCode);
}
Esempio n. 15
0
static int
LZWDecodeCompat(TIFF* tif, tidata_t op0, tsize_t occ0, tsample_t s)
{
	LZWCodecState *sp = DecoderState(tif);
	char *op = (char*) op0;
	long occ = (long) occ0;
	char *tp;
	u_char *bp;
	int code, nbits;
	long nextbits, nextdata, nbitsmask;
	code_t *codep, *free_entp, *maxcodep, *oldcodep;

	(void) s;
	assert(sp != NULL);
	/*
	 * Restart interrupted output operation.
	 */
	if (sp->dec_restart) {
		long residue;

		codep = sp->dec_codep;
		residue = codep->length - sp->dec_restart;
		if (residue > occ) {
			/*
			 * Residue from previous decode is sufficient
			 * to satisfy decode request.  Skip to the
			 * start of the decoded string, place decoded
			 * values in the output buffer, and return.
			 */
			sp->dec_restart += occ;
			do {
				codep = codep->next;
			} while (--residue > occ);
			tp = op + occ;
			do {
				*--tp = codep->value;
				codep = codep->next;
			} while (--occ);
			return (1);
		}
		/*
		 * Residue satisfies only part of the decode request.
		 */
		op += residue, occ -= residue;
		tp = op;
		do {
			*--tp = codep->value;
			codep = codep->next;
		} while (--residue);
		sp->dec_restart = 0;
	}

	bp = (u_char *)tif->tif_rawcp;
	nbits = sp->lzw_nbits;
	nextdata = sp->lzw_nextdata;
	nextbits = sp->lzw_nextbits;
	nbitsmask = sp->dec_nbitsmask;
	oldcodep = sp->dec_oldcodep;
	free_entp = sp->dec_free_entp;
	maxcodep = sp->dec_maxcodep;

	while (occ > 0) {
		NextCode(tif, sp, bp, code, GetNextCodeCompat);
		if (code == CODE_EOI)
			break;
		if (code == CODE_CLEAR) {
			free_entp = sp->dec_codetab + CODE_FIRST;
			nbits = BITS_MIN;
			nbitsmask = MAXCODE(BITS_MIN);
			maxcodep = sp->dec_codetab + nbitsmask;
			NextCode(tif, sp, bp, code, GetNextCodeCompat);
			if (code == CODE_EOI)
				break;
			*op++ = (char) code, occ--;
			oldcodep = sp->dec_codetab + code;
			continue;
		}
		codep = sp->dec_codetab + code;

		/*
	 	 * Add the new entry to the code table.
	 	 */
		if (free_entp < &sp->dec_codetab[0] ||
			free_entp >= &sp->dec_codetab[CSIZE]) {
			TIFFError(tif->tif_name,
			"LZWDecodeCompat: Corrupted LZW table at scanline %d",
			tif->tif_row);
			return (0);
		}

		free_entp->next = oldcodep;
		if (free_entp->next < &sp->dec_codetab[0] ||
			free_entp->next >= &sp->dec_codetab[CSIZE]) {
			TIFFError(tif->tif_name,
			"LZWDecodeCompat: Corrupted LZW table at scanline %d",
			tif->tif_row);
			return (0);
		}
		free_entp->firstchar = free_entp->next->firstchar;
		free_entp->length = free_entp->next->length+1;
		free_entp->value = (codep < free_entp) ?
		    codep->firstchar : free_entp->firstchar;
		if (++free_entp > maxcodep) {
			if (++nbits > BITS_MAX)		/* should not happen */
				nbits = BITS_MAX;
			nbitsmask = MAXCODE(nbits);
			maxcodep = sp->dec_codetab + nbitsmask;
		}
		oldcodep = codep;
		if (code >= 256) {
			/*
		 	 * Code maps to a string, copy string
			 * value to output (written in reverse).
		 	 */
			if(codep->length == 0) {
			    TIFFError(tif->tif_name,
	    		    "LZWDecodeCompat: Wrong length of decoded "
			    "string: data probably corrupted at scanline %d",
			    tif->tif_row);	
			    return (0);
			}
			if (codep->length > occ) {
				/*
				 * String is too long for decode buffer,
				 * locate portion that will fit, copy to
				 * the decode buffer, and setup restart
				 * logic for the next decoding call.
				 */
				sp->dec_codep = codep;
				do {
					codep = codep->next;
				} while (codep->length > occ);
				sp->dec_restart = occ;
				tp = op + occ;
				do  {
					*--tp = codep->value;
					codep = codep->next;
				}  while (--occ);
				break;
			}
			op += codep->length, occ -= codep->length;
			tp = op;
			do {
				*--tp = codep->value;
			} while( (codep = codep->next) != NULL);
		} else
			*op++ = (char) code, occ--;
	}

	tif->tif_rawcp = (tidata_t) bp;
	sp->lzw_nbits = (u_short) nbits;
	sp->lzw_nextdata = nextdata;
	sp->lzw_nextbits = nextbits;
	sp->dec_nbitsmask = nbitsmask;
	sp->dec_oldcodep = oldcodep;
	sp->dec_free_entp = free_entp;
	sp->dec_maxcodep = maxcodep;

	if (occ > 0) {
		TIFFError(tif->tif_name,
	    "LZWDecodeCompat: Not enough data at scanline %d (short %d bytes)",
		    tif->tif_row, occ);
		return (0);
	}
	return (1);
}
Esempio n. 16
0
static void
compress(
	int init_bits,
	FILE *outfile,
	ifun_t* ReadValue
)
{
    register long fcode;
    register code_int i = 0;
    register int c;
    register code_int ent;
    register code_int disp;
    register code_int hsize_reg;
    register int hshift;

    /*
     * Set up the globals:  g_init_bits - initial number of bits
     *                      g_outfile   - pointer to output file
     */
    g_init_bits = init_bits;
    g_outfile = outfile;
    /*
     * Set up the necessary values
     */
    offset = 0;
    out_count = 0;
    clear_flg = 0;
    in_count = 1;
    maxcode = MAXCODE(n_bits = g_init_bits);
    ClearCode = (1 << (init_bits - 1));
    EOFCode = ClearCode + 1;
    free_ent = ClearCode + 2;
    char_init();
    ent = GIFNextPixel( ReadValue );
    hshift = 0;
    for ( fcode = (long) hsize;  fcode < 65536L; fcode *= 2L )
        hshift++;
    hshift = 8 - hshift;                /* set hash code range bound */
    hsize_reg = hsize;
    cl_hash( (count_int) hsize_reg);            /* clear hash table */
    output( (code_int)ClearCode );
    while ( (c = GIFNextPixel( ReadValue )) != EOF ) {
        in_count++;
        fcode = (long) (((long) c << maxbits) + ent);
        /* i = (((code_int)c << hshift) ~ ent); */   /* xor hashing */
        i = (((code_int)c << hshift) ^ ent);    /* xor hashing */
        if ( HashTabOf (i) == fcode ) {
            ent = CodeTabOf (i);
            continue;
        } else if ( (long)HashTabOf (i) < 0 )      /* empty slot */
            goto nomatch;
        disp = hsize_reg - i;           /* secondary hash (after G. Knott) */
        if ( i == 0 )
            disp = 1;
probe:
        if ( (i -= disp) < 0 )
            i += hsize_reg;
        if ( HashTabOf (i) == fcode ) {
            ent = CodeTabOf (i);
            continue;
        }
        if ( (long)HashTabOf (i) > 0 )
            goto probe;
nomatch:
        output ( (code_int) ent );
        out_count++;
        ent = c;
        if ( free_ent < maxmaxcode ) {
            CodeTabOf (i) = free_ent++; /* code -> hashtable */
            HashTabOf (i) = fcode;
        } else
                cl_block();
    }
    /*
     * Put out the final code.
     */
    output( (code_int)ent );
    out_count++;
    output( (code_int) EOFCode );
    return;
}
Esempio n. 17
0
int decrunch_compress(xmp_file in, xmp_file out)
{
	char_type *stackp;
	code_int code;
	int finchar;
	code_int oldcode;
	code_int incode;
	int inbits;
	int posbits;
	int outpos;
	int insize;
	int bitmask;
	code_int free_ent;
	code_int maxcode;
	code_int maxmaxcode;
	int n_bits;
	int rsize;
	int maxbits;
	int block_mode;
	int i;

	long bytes_in;			/* Total number of byte from input */
	long bytes_out;			/* Total number of byte to output */
	char_type inbuf[IBUFSIZ + 64];	/* Input buffer */
	char_type outbuf[OBUFSIZ + 2048];/* Output buffer */
	count_int htab[HSIZE];
	unsigned short codetab[HSIZE];

	bytes_in = 0;
	bytes_out = 0;
	insize = 0;

	rsize = xmp_fread(inbuf, 1, IBUFSIZ, in);
	insize += rsize;

	if (insize < 3 || inbuf[0] != MAGIC_1 || inbuf[1] != MAGIC_2) {
		return -1;
	}

	maxbits = inbuf[2] & BIT_MASK;
	block_mode = inbuf[2] & BLOCK_MODE;
	maxmaxcode = MAXCODE(maxbits);

	if (maxbits > BITS) {
		/*fprintf(stderr,
		   "%s: compressed with %d bits, can only handle %d bits\n",
		   (*ifname != '\0' ? ifname : "stdin"), maxbits, BITS);
		   exit_code = 4; */
		return -1;
	}

	bytes_in = insize;
	maxcode = MAXCODE(n_bits = INIT_BITS) - 1;
	bitmask = (1 << n_bits) - 1;
	oldcode = -1;
	finchar = 0;
	outpos = 0;
	posbits = 3 << 3;

	free_ent = ((block_mode) ? FIRST : 256);

	clear_tab_prefixof();	/* As above, initialize the first
				   256 entries in the table. */

	for (code = 255; code >= 0; --code)
		tab_suffixof(code) = (char_type) code;

	do {
	      resetbuf:;
		{
			int i;
			int e;
			int o;

			o = posbits >> 3;
			e = o <= insize ? insize - o : 0;

			for (i = 0; i < e; ++i)
				inbuf[i] = inbuf[i + o];

			insize = e;
			posbits = 0;
		}

		if (insize < sizeof(inbuf) - IBUFSIZ) {
			if ((rsize = xmp_fread(inbuf + insize, 1, IBUFSIZ, in)) < 0)
				return -1;

			insize += rsize;
		}

		inbits = ((rsize > 0) ? (insize - insize % n_bits) << 3 :
			  (insize << 3) - (n_bits - 1));

		while (inbits > posbits) {
			if (free_ent > maxcode) {
				posbits = ((posbits - 1) + ((n_bits << 3) -
							    (posbits - 1 +
							     (n_bits << 3)) %
							    (n_bits << 3)));

				++n_bits;
				if (n_bits == maxbits)
					maxcode = maxmaxcode;
				else
					maxcode = MAXCODE(n_bits) - 1;

				bitmask = (1 << n_bits) - 1;
				goto resetbuf;
			}

			input(inbuf, posbits, code, n_bits, bitmask);

			if (oldcode == -1) {
				if (code >= 256) {
					fprintf(stderr, "oldcode:-1 code:%i\n",
						(int)(code));
					fprintf(stderr, "uncompress: corrupt input\n");
					/* abort_compress(); */
					return -1;
				}
				outbuf[outpos++] = (char_type)(finchar = (int)(oldcode = code));
				continue;
			}

			if (code == CLEAR && block_mode) {
				clear_tab_prefixof();
				free_ent = FIRST - 1;
				posbits = ((posbits - 1) + ((n_bits << 3) -
					    (posbits - 1 + (n_bits << 3)) %
							   (n_bits << 3)));
				maxcode = MAXCODE(n_bits = INIT_BITS) - 1;
				bitmask = (1 << n_bits) - 1;
				goto resetbuf;
			}

			incode = code;
			stackp = de_stack;

			if (code >= free_ent) {	/* Special case for KwKwK string. */
				if (code > free_ent) {
					/*char_type *p;

					posbits -= n_bits;
					p = &inbuf[posbits >> 3];

					fprintf(stderr,
						"insize:%d posbits:%d inbuf:%02X %02X %02X %02X %02X (%d)\n",
						insize, posbits, p[-1], p[0],
						p[1], p[2], p[3],
						(posbits & 07));*/
					fprintf(stderr,
						"uncompress: corrupt input\n");
					/* abort_compress(); */
					return -1;
				}

				*--stackp = (char_type) finchar;
				code = oldcode;
			}

			while ((cmp_code_int) code >= (cmp_code_int) 256) {	/* Generate output characters in reverse order */
				*--stackp = tab_suffixof(code);
				code = tab_prefixof(code);
			}

			*--stackp = (char_type) (finchar = tab_suffixof(code));

			/* And put them out in forward order */

			if (outpos + (i = (de_stack - stackp)) >= OBUFSIZ) {
				do {
					if (i > OBUFSIZ - outpos)
						i = OBUFSIZ - outpos;

					if (i > 0) {
						memcpy(outbuf + outpos, stackp, i);
						outpos += i;
					}

					if (outpos >= OBUFSIZ) {
						if (xmp_fwrite(outbuf, 1, outpos, out) != outpos) {
							return -1;
							/*write_error(); */
						}

						outpos = 0;
					}
					stackp += i;
				}
				while ((i = (de_stack - stackp)) > 0);
			} else {
				memcpy(outbuf + outpos, stackp, i);
				outpos += i;
			}

			if ((code = free_ent) < maxmaxcode) {	/* Generate the new entry. */
				tab_prefixof(code) = (unsigned short)oldcode;
				tab_suffixof(code) = (char_type) finchar;
				free_ent = code + 1;
			}

			oldcode = incode;	/* Remember previous code.      */
		}

		bytes_in += rsize;
	}
	while (rsize > 0);

	if (outpos > 0 && xmp_fwrite(outbuf, 1, outpos, out) != outpos)
		return -1;

	return 0;
}
Esempio n. 18
0
void 
compress(void)
{
	register long   fcode;
	register code_int i = 0;
	register int    c;
	register code_int ent;
	register int    disp;
	register code_int hsize_reg;
	register int    hshift;


	offset = 0;
	bytes_out = 3;		/* includes 3-byte header mojo */
	out_count = 0;
	clear_flg = 0;
	ratio = 0;
	in_count = 1;
	checkpoint = CHECK_GAP;
	maxcode = MAXCODE(n_bits = INIT_BITS);
	free_ent = ((block_compress) ? (FIRST) : (256));

	ent = getbyte();

	hshift = 0;
	for (fcode = (long) hsize; fcode < 65536L; fcode *= 2L) {
		hshift++;
	}

	hshift = 8 - hshift;	/* set hash code range bound */

	hsize_reg = hsize;
	cl_hash((count_int) hsize_reg);	/* clear hash table */


	while (InCnt > 0) {	/* apsim_loop 11 0 */
		int             apsim_bound111 = 0;

		c = getbyte();	/* decrements InCnt */

		in_count++;
		fcode = (long) (((long) c << maxbits) + ent);
		i = ((c << hshift) ^ ent);	/* xor hashing */

		if (htabof(i) == fcode) {
			ent = codetabof(i);
			continue;
		} else if ((long) htabof(i) < 0) {	/* empty slot */
			goto nomatch;
		}
		disp = hsize_reg - i;	/* secondary hash (after G. Knott) */
		if (i == 0) {
			disp = 1;
		}
probe:

		if ((i -= disp) < 0) {	/* apsim_loop 111 11 */
			i += hsize_reg;
		}
		if (htabof(i) == fcode) {
			ent = codetabof(i);
			continue;
		}
		if ((long) htabof(i) > 0 && (++apsim_bound111 < in_count))
			goto probe;
nomatch:

		out_count++;
		ent = c;
		if (free_ent < maxmaxcode) {
			codetabof(i) = free_ent++;	/* apsim_unknown codetab */
			htabof(i) = fcode;	/* apsim_unknown htab */
		} else if (((count_int) in_count >= checkpoint) && (block_compress)) {
			cl_block();
		}
	}
	if (bytes_out > in_count) {	/* exit(2) if no savings */
		exit_stat = 2;
	}
	return;
}
Esempio n. 19
0
/*
 * Open LZW file
 */
hidden_in_another_lib
lzwFile *lzw_fdopen(int fd)
{
	lzwFile *ret;
	unsigned char buf[3];

	if (read(fd, buf, 3) != 3)
		goto err_out;

	if (buf[0] != LZW_MAGIC_1 || buf[1] != LZW_MAGIC_2 || buf[2] & 0x60)
		goto err_out;

	if ((ret = malloc(sizeof(*ret))) == NULL)
		goto err_out;

	memset(ret, 0x00, sizeof(*ret));
	ret->fd = fd;
	ret->eof = 0;
	ret->inbuf = malloc(sizeof(unsigned char) * IN_BUFSIZE);
	ret->outbuf = malloc(sizeof(unsigned char) * OUT_BUFSIZE);
	ret->stackp = NULL;
	ret->insize = 3; /* we read three bytes above */
	ret->outpos = 0;
	ret->rsize = 0;

	ret->flags = buf[2];
	ret->maxbits = ret->flags & 0x1f;    /* Mask for 'number of compresssion bits' */
	ret->block_mode = ret->flags & 0x80;

	ret->n_bits = INIT_BITS;
	ret->maxcode = MAXCODE(INIT_BITS) - 1;
	ret->bitmask = (1<<INIT_BITS)-1;
	ret->oldcode = -1;
	ret->finchar = 0;
	ret->posbits = 3<<3;
	ret->free_ent = ((ret->block_mode) ? FIRST : 256);

	/* initialize the first 256 entries in the table */
	memset(ret->codetab, 0x00, sizeof(ret->codetab));
	for (ret->code = 255; ret->code >= 0; --ret->code)
		ret->htab[ret->code] = ret->code;

	if (ret->inbuf == NULL || ret->outbuf == NULL) {
		errno = ENOMEM;
		goto err_out_free;
	}
	if (ret->maxbits > BITS) {
		errno = EINVAL;
		goto err_out_free;
	}

	return ret;

err_out:
	errno = EINVAL;
	return NULL;

err_out_free:
	if (ret->inbuf) free(ret->inbuf);
	if (ret->outbuf) free(ret->outbuf);
	free(ret);
	return NULL;
}
Esempio n. 20
0
static void
compress(int init_bits, gdIOCtxPtr outfile, gdImagePtr im, GifCtx *ctx)
{
    register long fcode;
    register code_int i /* = 0 */;
    register int c;
    register code_int ent;
    register code_int disp;
    register code_int hsize_reg;
    register int hshift;

    /*
     * Set up the globals:  g_init_bits - initial number of bits
     *                      g_outfile   - pointer to output file
     */
    ctx->g_init_bits = init_bits;
    ctx->g_outfile = outfile;

    /*
     * Set up the necessary values
     */
    ctx->offset = 0;
    ctx->out_count = 0;
    ctx->clear_flg = 0;
    ctx->in_count = 1;
    ctx->maxcode = MAXCODE(ctx->n_bits = ctx->g_init_bits);

    ctx->ClearCode = (1 << (init_bits - 1));
    ctx->EOFCode = ctx->ClearCode + 1;
    ctx->free_ent = ctx->ClearCode + 2;

    char_init(ctx);

    ent = GIFNextPixel( im, ctx );

    hshift = 0;
    for ( fcode = (long) hsize;  fcode < 65536L; fcode *= 2L )
        ++hshift;
    hshift = 8 - hshift;                /* set hash code range bound */

    hsize_reg = hsize;
    cl_hash( (count_int) hsize_reg, ctx );            /* clear hash table */

    output( (code_int)ctx->ClearCode, ctx );

#ifdef SIGNED_COMPARE_SLOW
    while ( (c = GIFNextPixel( im )) != (unsigned) EOF ) {
#else /*SIGNED_COMPARE_SLOW*/
    while ( (c = GIFNextPixel( im, ctx )) != EOF ) {  /* } */
#endif /*SIGNED_COMPARE_SLOW*/

        ++(ctx->in_count);

        fcode = (long) (((long) c << maxbits) + ent);
        i = (((code_int)c << hshift) ^ ent);    /* xor hashing */

        if ( HashTabOf (i) == fcode ) {
            ent = CodeTabOf (i);
            continue;
        } else if ( (long)HashTabOf (i) < 0 )      /* empty slot */
            goto nomatch;
        disp = hsize_reg - i;           /* secondary hash (after G. Knott) */
        if ( i == 0 )
            disp = 1;
probe:
        if ( (i -= disp) < 0 )
            i += hsize_reg;

        if ( HashTabOf (i) == fcode ) {
            ent = CodeTabOf (i);
            continue;
        }
        if ( (long)HashTabOf (i) > 0 )
            goto probe;
nomatch:
        output ( (code_int) ent, ctx );
        ++(ctx->out_count);
        ent = c;
#ifdef SIGNED_COMPARE_SLOW
        if ( (unsigned) ctx->free_ent < (unsigned) maxmaxcode) {
#else /*SIGNED_COMPARE_SLOW*/
        if ( ctx->free_ent < maxmaxcode ) {  /* } */
#endif /*SIGNED_COMPARE_SLOW*/
            CodeTabOf (i) = ctx->free_ent++; /* code -> hashtable */
            HashTabOf (i) = fcode;
        } else
                cl_block(ctx);
    }
    /*
     * Put out the final code.
     */
    output( (code_int)ent, ctx );
    ++(ctx->out_count);
    output( (code_int) ctx->EOFCode, ctx );
}

/*****************************************************************
 * TAG( output )
 *
 * Output the given code.
 * Inputs:
 *      code:   A n_bits-bit integer.  If == -1, then EOF.  This assumes
 *              that n_bits =< (long)wordsize - 1.
 * Outputs:
 *      Outputs code to the file.
 * Assumptions:
 *      Chars are 8 bits long.
 * Algorithm:
 *      Maintain a GIFBITS character long buffer (so that 8 codes will
 * fit in it exactly).  Use the VAX insv instruction to insert each
 * code in turn.  When the buffer fills up empty it and start over.
 */

static const unsigned long masks[] = { 0x0000, 0x0001, 0x0003, 0x0007, 0x000F,
                                  0x001F, 0x003F, 0x007F, 0x00FF,
                                  0x01FF, 0x03FF, 0x07FF, 0x0FFF,
                                  0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF };


/* Arbitrary value to mark output is done.  When we see EOFCode, then we don't
 * expect to see any more data.  If we do (e.g. corrupt image inputs), cur_bits
 * might be negative, so flag it to return early.
 */
#define CUR_BITS_FINISHED -1000


static void
output(code_int code, GifCtx *ctx)
{
	if (ctx->cur_bits == CUR_BITS_FINISHED) {
		return;
	}

    ctx->cur_accum &= masks[ ctx->cur_bits ];

    if( ctx->cur_bits > 0 )
        ctx->cur_accum |= ((long)code << ctx->cur_bits);
    else
        ctx->cur_accum = code;

    ctx->cur_bits += ctx->n_bits;

    while( ctx->cur_bits >= 8 ) {
        char_out( (unsigned int)(ctx->cur_accum & 0xff), ctx );
        ctx->cur_accum >>= 8;
        ctx->cur_bits -= 8;
    }

    /*
     * If the next entry is going to be too big for the code size,
     * then increase it, if possible.
     */
   if ( ctx->free_ent > ctx->maxcode || ctx->clear_flg ) {

            if( ctx->clear_flg ) {

                ctx->maxcode = MAXCODE (ctx->n_bits = ctx->g_init_bits);
                ctx->clear_flg = 0;

            } else {

                ++(ctx->n_bits);
                if ( ctx->n_bits == maxbits )
                    ctx->maxcode = maxmaxcode;
                else
                    ctx->maxcode = MAXCODE(ctx->n_bits);
            }
        }

    if( code == ctx->EOFCode ) {
        /*
         * At EOF, write the rest of the buffer.
         */
        while( ctx->cur_bits > 0 ) {
                char_out( (unsigned int)(ctx->cur_accum & 0xff), ctx);
                ctx->cur_accum >>= 8;
                ctx->cur_bits -= 8;
        }

		/* Flag that it's done to prevent re-entry. */
		ctx->cur_bits = CUR_BITS_FINISHED;

        flush_char(ctx);
    }
}

/*
 * Clear out the hash table
 */
static void
cl_block (GifCtx *ctx)             /* table clear for block compress */
{

        cl_hash ( (count_int) hsize, ctx );
        ctx->free_ent = ctx->ClearCode + 2;
        ctx->clear_flg = 1;

        output( (code_int)ctx->ClearCode, ctx);
}

static void
cl_hash(register count_int chsize, GifCtx *ctx)          /* reset code table */

{

        register count_int *htab_p = ctx->htab+chsize;

        register long i;
        register long m1 = -1;

        i = chsize - 16;
        do {                            /* might use Sys V memset(3) here */
                *(htab_p-16) = m1;
                *(htab_p-15) = m1;
                *(htab_p-14) = m1;
                *(htab_p-13) = m1;
                *(htab_p-12) = m1;
                *(htab_p-11) = m1;
                *(htab_p-10) = m1;
                *(htab_p-9) = m1;
                *(htab_p-8) = m1;
                *(htab_p-7) = m1;
                *(htab_p-6) = m1;
                *(htab_p-5) = m1;
                *(htab_p-4) = m1;
                *(htab_p-3) = m1;
                *(htab_p-2) = m1;
                *(htab_p-1) = m1;
                htab_p -= 16;
        } while ((i -= 16) >= 0);

        for ( i += 16; i > 0; --i )
                *--htab_p = m1;
}

/******************************************************************************
 *
 * GIF Specific routines
 *
 ******************************************************************************/

/*
 * Set up the 'byte output' routine
 */
static void
char_init(GifCtx *ctx)
{
        ctx->a_count = 0;
}

/*
 * Add a character to the end of the current packet, and if it is 254
 * characters, flush the packet to disk.
 */
static void
char_out(int c, GifCtx *ctx)
{
        ctx->accum[ ctx->a_count++ ] = c;
        if( ctx->a_count >= 254 )
                flush_char(ctx);
}
Esempio n. 21
0
/*
 * Setup state for decoding a strip.
 */
static int
LZWPreDecode(TIFF* tif, uint16 s)
{
	static const char module[] = "LZWPreDecode";
	LZWCodecState *sp = DecoderState(tif);

	(void) s;
	assert(sp != NULL);
	if( sp->dec_codetab == NULL )
        {
            tif->tif_setupdecode( tif );
	    if( sp->dec_codetab == NULL )
		return (0);
        }

	/*
	 * Check for old bit-reversed codes.
	 */
	if (tif->tif_rawcc >= 2 &&
	    tif->tif_rawdata[0] == 0 && (tif->tif_rawdata[1] & 0x1)) {
#ifdef LZW_COMPAT
		if (!sp->dec_decode) {
			TIFFWarningExt(tif->tif_clientdata, module,
			    "Old-style LZW codes, convert file");
			/*
			 * Override default decoding methods with
			 * ones that deal with the old coding.
			 * Otherwise the predictor versions set
			 * above will call the compatibility routines
			 * through the dec_decode method.
			 */
			tif->tif_decoderow = LZWDecodeCompat;
			tif->tif_decodestrip = LZWDecodeCompat;
			tif->tif_decodetile = LZWDecodeCompat;
			/*
			 * If doing horizontal differencing, must
			 * re-setup the predictor logic since we
			 * switched the basic decoder methods...
			 */
			(*tif->tif_setupdecode)(tif);
			sp->dec_decode = LZWDecodeCompat;
		}
		sp->lzw_maxcode = MAXCODE(BITS_MIN);
#else /* !LZW_COMPAT */
		if (!sp->dec_decode) {
			TIFFErrorExt(tif->tif_clientdata, module,
			    "Old-style LZW codes not supported");
			sp->dec_decode = LZWDecode;
		}
		return (0);
#endif/* !LZW_COMPAT */
	} else {
		sp->lzw_maxcode = MAXCODE(BITS_MIN)-1;
		sp->dec_decode = LZWDecode;
	}
	sp->lzw_nbits = BITS_MIN;
	sp->lzw_nextbits = 0;
	sp->lzw_nextdata = 0;

	sp->dec_restart = 0;
	sp->dec_nbitsmask = MAXCODE(BITS_MIN);
#ifdef LZW_CHECKEOS
	sp->dec_bitsleft = 0;
#endif
	sp->dec_free_entp = sp->dec_codetab + CODE_FIRST;
	/*
	 * Zero entries that are not yet filled in.  We do
	 * this to guard against bogus input data that causes
	 * us to index into undefined entries.  If you can
	 * come up with a way to safely bounds-check input codes
	 * while decoding then you can remove this operation.
	 */
	_TIFFmemset(sp->dec_free_entp, 0, (CSIZE-CODE_FIRST)*sizeof (code_t));
	sp->dec_oldcodep = &sp->dec_codetab[-1];
	sp->dec_maxcodep = &sp->dec_codetab[sp->dec_nbitsmask-1];
	return (1);
}
Esempio n. 22
0
int main() {
    register long fcode;
    register code_int i = 0;
    register int c;
    register code_int ent;
#ifdef XENIX_16
    register code_int disp;
#else	/* Normal machine */
    register int disp;
#endif
    register code_int hsize_reg;
    register int hshift;

#ifndef COMPATIBLE
    if (nomagic == 0) {
	/* putchar(magic_header[0]); putchar(magic_header[1]);
	putchar((char)(maxbits | block_compress)); */
    }
#endif /* COMPATIBLE */

    offset = 0;
    bytes_out = 3;		/* includes 3-byte header mojo */
    out_count = 0;
    clear_flg = 0;
    ratio = 0;
    in_count = 1;

    printf("main: bytes_out %d... hsize %d\n", (int)bytes_out, (int)hsize);

    checkpoint = CHECK_GAP;
    maxcode = MAXCODE(n_bits = INIT_BITS);
    free_ent = ((block_compress) ? FIRST : 256 );


    ent = '\0'; /* getchar (); */

    hshift = 0;
    for ( fcode = (long) hsize;  fcode < 65536L; fcode *= 2L )
    	hshift++;
    hshift = 8 - hshift;		/* set hash code range bound */
    printf("main: hshift %d...\n", hshift);

    hsize_reg = hsize;
    cl_hash( (count_int) hsize_reg);		/* clear hash table */

/*#ifdef SIGNED_COMPARE_SLOW
    while ( (c = getchar()) != (unsigned) EOF ) {
#else
    while ( (c = getchar()) != EOF ) {
#endif*/
    printf("main: bytes_out %d...\n", (int)bytes_out);
    printf("main: hsize_reg %d...\n", (int)hsize_reg);
    printf("main: before compress %d...\n", (int)in_count);
    while (in_count < BYTES_TO_COMPRESS) {
        c = in_count % 255;
        
        printf("main: compressing %d...\n", (int)in_count);
	in_count++;
	fcode = (long) (((long) c << maxbits) + ent);
 	i = (((long)c << hshift) ^ ent);	/* xor hashing */
	
	if ( htabof (i) == fcode ) {
	    ent = codetabof (i);
	    continue;
	} else if ( (long)htabof (i) < 0 ) 	/* empty slot */
	    goto nomatch;
	 
 	disp = hsize_reg - i;		/* secondary hash (after G. Knott) */
	if ( i == 0 )
	    disp = 1;
probe:
	if ( (i -= disp) < 0 )
	    i += hsize_reg;

	if ( htabof (i) == fcode ) {
	    ent = codetabof (i);
	    continue;
	}
	if ( (long)htabof (i) > 0 ) 
	    goto probe;
nomatch:
	output ( (code_int) ent );
	out_count++;
 	ent = c;
#ifdef SIGNED_COMPARE_SLOW
	if ( (unsigned) free_ent < (unsigned) maxmaxcode) {
#else
	if ( free_ent < maxmaxcode ) {
#endif
 	    codetabof (i) = free_ent++;	/* code -> hashtable */
	    htabof (i) = fcode;
	}
	else if ( (count_int)in_count >= checkpoint && block_compress )
	    cl_block ();
    }
    /*
     * Put out the final code.
     */
    printf("main: output...\n");
    output( (code_int)ent );
    out_count++;
    output( (code_int)-1 );

    if(bytes_out > in_count)	/* exit(2) if no savings */
	exit_stat = 2;
    printf("main: end...\n");
    report (0xdeaddead);
    return 0;
}

/*****************************************************************
 * TAG( output )
 *
 * Output the given code.
 * Inputs:
 * 	code:	A n_bits-bit integer.  If == -1, then EOF.  This assumes
 *		that n_bits =< (long)wordsize - 1.
 * Outputs:
 * 	Outputs code to the file.
 * Assumptions:
 *	Chars are 8 bits long.
 * Algorithm:
 * 	Maintain a BITS character long buffer (so that 8 codes will
 * fit in it exactly).  Use the VAX insv instruction to insert each
 * code in turn.  When the buffer fills up empty it and start over.
 */

static char buf[BITS];

#ifndef vax
char_type lmask[9] = {0xff, 0xfe, 0xfc, 0xf8, 0xf0, 0xe0, 0xc0, 0x80, 0x00};
char_type rmask[9] = {0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff};
#endif /* vax */

void output( code )
code_int  code;
{

    /*
     * On the VAX, it is important to have the register declarations
     * in exactly the order given, or the asm will break.
     */
    register int r_off = offset, bits= n_bits;
    register char * bp = buf;

    if ( code >= 0 ) {
#ifdef vax
	/* VAX DEPENDENT!! Implementation on other machines is below.
	 *
	 * Translation: Insert BITS bits from the argument starting at
	 * offset bits from the beginning of buf.
	 */
	0;	/* Work around for pcc -O bug with asm and if stmt */
	asm( "insv	4(ap),r11,r10,(r9)" );
#else /* not a vax */
/* 
 * byte/bit numbering on the VAX is simulated by the following code
 */
	/*
	 * Get to the first byte.
	 */
	bp += (r_off >> 3);
	r_off &= 7;
	/*
	 * Since code is always >= 8 bits, only need to mask the first
	 * hunk on the left.
	 */
	*bp = (*bp & rmask[r_off]) | ((code << r_off) & lmask[r_off]);
	bp++;
	bits -= (8 - r_off);
	code >>= 8 - r_off;
	/* Get any 8 bit parts in the middle (<=1 for up to 16 bits). */
	if ( bits >= 8 ) {
	    *bp++ = code;
	    code >>= 8;
	    bits -= 8;
	}
	/* Last bits. */
	if(bits)
	    *bp = code;
#endif /* vax */
	offset += n_bits;
	if ( offset == (n_bits << 3) ) {
	    bp = buf;
	    bits = n_bits;
	    bytes_out += bits;
	/*    do
		putchar(*bp++); */
	    while(--bits);
	    offset = 0;
	}

	/*
	 * If the next entry is going to be too big for the code size,
	 * then increase it, if possible.
	 */
	if ( free_ent > maxcode || (clear_flg > 0))
	{
	    /*
	     * Write the whole buffer, because the input side won't
	     * discover the size increase until after it has read it.
	     */
	    if ( offset > 0 ) {
		/* if( fwrite( buf, 1, n_bits, stdout ) != n_bits)
			writeerr(); */
		bytes_out += n_bits;
	    }
	    offset = 0;

	    if ( clear_flg ) {
    	        maxcode = MAXCODE (n_bits = INIT_BITS);
	        clear_flg = 0;
	    }
	    else {
	    	n_bits++;
	    	if ( n_bits == maxbits )
		    maxcode = maxmaxcode;
	    	else
		    maxcode = MAXCODE(n_bits);
	    }
	}
    } else {
Esempio n. 23
0
int lzwInflate(lzw_streamp strm)
{
    struct lzw_internal_state *state;
    uint8_t *from, *to;
    unsigned in, out;
    unsigned have, left;
    long nbits, nextbits, nextdata, nbitsmask;
    code_t *codep, *free_entp, *maxcodep, *oldcodep;

    uint8_t *wp;
    hcode_t code, free_code;
    int echg, ret = LZW_OK;
    uint32_t flags;

    if (strm == NULL || strm->state == NULL || strm->next_out == NULL ||
        (strm->next_in == NULL && strm->avail_in != 0))
        return LZW_STREAM_ERROR;

    /* load state */
    to = strm->next_out;
    out = left = strm->avail_out;

    from = strm->next_in;
    in = have = strm->avail_in;

    flags = strm->flags;
    state = strm->state;

    nbits = state->nbits;
    nextdata = state->nextdata;
    nextbits = state->nextbits;
    nbitsmask = state->dec_nbitsmask;
    oldcodep = state->dec_oldcodep;
    free_entp = state->dec_free_entp;
    maxcodep = state->dec_maxcodep;

    echg = flags & LZW_FLAG_EARLYCHG;
    free_code = free_entp - &state->dec_codetab[0];

    if (oldcodep == &state->dec_codetab[CODE_EOI])
        return LZW_STREAM_END;

    /*
     * Restart interrupted output operation.
     */
    if (state->dec_restart) {
        long residue;

        codep = state->dec_codep;
        residue = codep->length - state->dec_restart;
        if (residue > left) {
            /*
             * Residue from previous decode is sufficient
             * to satisfy decode request.  Skip to the
             * start of the decoded string, place decoded
             * values in the output buffer, and return.
             */
            state->dec_restart += left;
            do {
                codep = codep->next;
            } while (--residue > left);
            to = wp = to + left;
            do {
                *--wp = codep->value;
                codep = codep->next;
            } while (--left);
            goto inf_end;
        }
        /*
         * Residue satisfies only part of the decode request.
         */
        to += residue, left -= residue;
        wp = to;
        do {
            *--wp = codep->value;
            codep = codep->next;
        } while (--residue);
        state->dec_restart = 0;
    }

    /* guarentee valid initial state */
    if (left > 0 && (oldcodep == &state->dec_codetab[CODE_CLEAR])) {
        code = CODE_CLEAR;
        CodeClear(code);
        if (ret != LZW_OK)
            goto inf_end;
    }

    while (left > 0) {
        GetNextCode(code);
        if (code == CODE_EOI) {
            ret = LZW_STREAM_END;
            break;
        }
        if (code == CODE_CLEAR) {
            CodeClear(code);
            if (ret != LZW_OK)
                break;
            continue;
        }
        codep = state->dec_codetab + code;

        /* non-earlychange bit expansion */
        if (!echg && free_entp > maxcodep) {
            if (++nbits > BITS_VALID) {
                flags |= LZW_FLAG_BIGDICT;
                if (nbits > BITS_MAX)     /* should not happen */
                    nbits = BITS_MAX;
            }
            nbitsmask = MAXCODE(nbits);
            maxcodep = state->dec_codetab + nbitsmask-1;
        }
        /*
         * Add the new entry to the code table.
         */
        if (&state->dec_codetab[0] > free_entp || free_entp >= &state->dec_codetab[CSIZE]) {
            cli_dbgmsg("%p <= %p, %p < %p(%ld)\n", &state->dec_codetab[0], free_entp, free_entp, &state->dec_codetab[CSIZE], CSIZE);
            strm->msg = "full dictionary, cannot add new entry";
            ret = LZW_DICT_ERROR;
            break;
        }
        free_entp->next = oldcodep;
        free_entp->firstchar = free_entp->next->firstchar;
        free_entp->length = free_entp->next->length+1;
        free_entp->value = (codep < free_entp) ?
            codep->firstchar : free_entp->firstchar;
        free_entp++;
        /* earlychange bit expansion */
        if (echg && free_entp > maxcodep) {
            if (++nbits > BITS_VALID) {
                flags |= LZW_FLAG_BIGDICT;
                if (nbits > BITS_MAX)     /* should not happen */
                    nbits = BITS_MAX;
            }
            nbitsmask = MAXCODE(nbits);
            maxcodep = state->dec_codetab + nbitsmask-1;
        }
        free_code++;
        oldcodep = codep;
        if (code >= CODE_BASIC) {
            /* check if code is valid */
            if (code >= free_code) {
                strm->msg = "cannot reference unpopulated dictionary entries";
                ret = LZW_DATA_ERROR;
                break;
            }

            /*
             * Code maps to a string, copy string
             * value to output (written in reverse).
             */
            if (codep->length > left) {
                /*
                 * String is too long for decode buffer,
                 * locate portion that will fit, copy to
                 * the decode buffer, and setup restart
                 * logic for the next decoding call.
                 */
                state->dec_codep = codep;
                do {
                    codep = codep->next;
                } while (codep->length > left);
                state->dec_restart = left;
                to = wp = to + left;
                do  {
                    *--wp = codep->value;
                    codep = codep->next;
                }  while (--left);
                goto inf_end;
            }

            to += codep->length, left -= codep->length;
            wp = to;
            do {
                *--wp = codep->value;
                codep = codep->next;
            } while(codep != NULL);
        } else
            *to++ = code, left--;
    }

inf_end:
    /* restore state */
    strm->next_out = to;
    strm->avail_out = left;
    strm->next_in = from;
    strm->avail_in = have;
    strm->flags = flags;

    state->nbits = (uint16_t)nbits;
    state->nextdata = nextdata;
    state->nextbits = nextbits;
    state->dec_nbitsmask = nbitsmask;
    state->dec_oldcodep = oldcodep;
    state->dec_free_entp = free_entp;
    state->dec_maxcodep = maxcodep;

    /* update state */
    in -= strm->avail_in;
    out -= strm->avail_out;
    strm->total_in += in;
    strm->total_out += out;

    if ((in == 0 && out == 0) && ret == LZW_OK) {
        strm->msg = "no data was processed";
        ret = LZW_BUF_ERROR;
    }
    return ret;
}
Esempio n. 24
0
static void compress(int init_bits, FILE *outfile, byte *data, int   len, struct GifStore *aGifStore)
{ register long fcode;
  register int i = 0;
  register int c;
  register int ent;
  register int disp;
  register int hsize_reg;
  register int hshift;

  unsigned short codetab [HSIZE];
  count_int      htab [HSIZE];

  /*
   * Set up the globals:  g_init_bits - initial number of bits
   *                      g_outfile   - pointer to output file
   */
  aGifStore->g_init_bits = init_bits;
  aGifStore->g_outfile   = outfile;

  /* initialize 'compress' globals */
  memset((char *) htab,    0, sizeof(htab));
  memset((char *) codetab, 0, sizeof(codetab));
  aGifStore->free_ent = 0;
  aGifStore->clear_flg = 0;
  aGifStore->cur_accum = 0;
  aGifStore->cur_bits = 0;

  /*
   * Set up the necessary values
   */
  aGifStore->clear_flg = 0;
  aGifStore->maxcode = MAXCODE(aGifStore->n_bits = aGifStore->g_init_bits);

  aGifStore->ClearCode = 1 << (init_bits - 1);
  aGifStore->EOFCode = aGifStore->ClearCode + 1;
  aGifStore->free_ent = aGifStore->ClearCode + 2;

  aGifStore->a_count = 0;
  ent = aGifStore->pc2nc[*data++];  len--;

  hshift = 0;
  for ( fcode = (long)HSIZE;  fcode < 65536L; fcode *= 2L )
    hshift++;
  hshift = 8 - hshift;                /* set hash code range bound */

  hsize_reg = HSIZE;
  cl_hash2( (count_int) hsize_reg, htab);       /* clear hash table */

  output(aGifStore->ClearCode, aGifStore);

  while (len)
  { c = aGifStore->pc2nc[*data++];
    len--;

    fcode = (long) ( ( (long) c << XV_BITS) + ent);
    i = (((int) c << hshift) ^ ent);    /* xor hashing */

    if ( htab[i] == fcode )
    { ent = codetab[i];
      continue;
    }

    else if ( (long)htab[i] < 0 )      /* empty slot */
      goto nomatch;

    disp = hsize_reg - i;           /* secondary hash (after G. Knott) */
    if ( i == 0 )
      disp = 1;

probe:
    if ( (i -= disp) < 0 )
      i += hsize_reg;

    if ( htab[i] == fcode )
    { ent = codetab[i];
      continue;
    }

    if ( (long)htab[i] >= 0 )
      goto probe;

nomatch:
    output(ent, aGifStore);
    ent = c;

    if ( aGifStore->free_ent < (1<<XV_BITS) )
    { codetab[i] = (unsigned short)(aGifStore->free_ent++); /* code -> hashtable */
      htab[i]    = fcode;
    }
    else
      cl_block(htab, aGifStore);
  }

  /* Put out the final code */
  output(ent, aGifStore);
  output(aGifStore->EOFCode, aGifStore);
}
Esempio n. 25
0
/*-
 * compress write
 *
 * Algorithm:  use open addressing double hashing (no chaining) on the
 * prefix code / next character combination.  We do a variant of Knuth's
 * algorithm D (vol. 3, sec. 6.4) along with G. Knott's relatively-prime
 * secondary probe.  Here, the modular division first probe is gives way
 * to a faster exclusive-or manipulation.  Also do block compression with
 * an adaptive reset, whereby the code table is cleared when the compression
 * ratio decreases, but after the table fills.  The variable-length output
 * codes are re-sized at this point, and a special CLEAR code is generated
 * for the decompressor.  Late addition:  construct the table according to
 * file size for noticeable speed improvement on small files.  Please direct
 * questions about this implementation to ames!jaw.
 */
static int
zwrite(void *cookie, const char *wbp, int num)
{
	code_int i;
	int c, disp;
	struct s_zstate *zs;
	const u_char *bp;
	u_char tmp;
	int count;

	if (num == 0)
		return (0);

	zs = cookie;
	count = num;
	bp = wbp;
	if (state == S_MIDDLE)
		goto middle;
	state = S_MIDDLE;

	maxmaxcode = 1L << maxbits;
	if (fwrite(magic_header,
	    sizeof(char), sizeof(magic_header), fp) != sizeof(magic_header))
		return (-1);
	tmp = (u_char)((maxbits) | block_compress);
	if (fwrite(&tmp, sizeof(char), sizeof(tmp), fp) != sizeof(tmp))
		return (-1);

	offset = 0;
	bytes_out = 3;		/* Includes 3-byte header mojo. */
	out_count = 0;
	clear_flg = 0;
	ratio = 0;
	in_count = 1;
	checkpoint = CHECK_GAP;
	maxcode = MAXCODE(n_bits = INIT_BITS);
	free_ent = ((block_compress) ? FIRST : 256);

	ent = *bp++;
	--count;

	hshift = 0;
	for (fcode = (long)hsize; fcode < 65536L; fcode *= 2L)
		hshift++;
	hshift = 8 - hshift;	/* Set hash code range bound. */

	hsize_reg = hsize;
	cl_hash(zs, (count_int)hsize_reg);	/* Clear hash table. */

middle:	for (i = 0; count--;) {
		c = *bp++;
		in_count++;
		fcode = (long)(((long)c << maxbits) + ent);
		i = ((c << hshift) ^ ent);	/* Xor hashing. */

		if (htabof(i) == fcode) {
			ent = codetabof(i);
			continue;
		} else if ((long)htabof(i) < 0)	/* Empty slot. */
			goto nomatch;
		disp = hsize_reg - i;	/* Secondary hash (after G. Knott). */
		if (i == 0)
			disp = 1;
probe:		if ((i -= disp) < 0)
			i += hsize_reg;

		if (htabof(i) == fcode) {
			ent = codetabof(i);
			continue;
		}
		if ((long)htabof(i) >= 0)
			goto probe;
nomatch:	if (output(zs, (code_int) ent) == -1)
			return (-1);
		out_count++;
		ent = c;
		if (free_ent < maxmaxcode) {
			codetabof(i) = free_ent++;	/* code -> hashtable */
			htabof(i) = fcode;
		} else if ((count_int)in_count >=
		    checkpoint && block_compress) {
			if (cl_block(zs) == -1)
				return (-1);
		}
	}
	return (num);
}
Esempio n. 26
0
typedef int_64		count_int;
typedef	unsigned char	char_type;

/*
 * Private globals variables used by the compression and
 * decompression routines .
 */
static char_type magic_header[] = { "\037\235" };  /* 1F 9D */
static int_64 	 n_bits;			   /* Number of bits/code */
static int_64 	 maxbits;		   	   /* User settable max # bits/code */
static code_int  maxcode;			   /* Maximum code, given n_bits */
static code_int  maxmaxcode;	   		   /* Should NEVER generate this code */
static count_int htab [HSIZE];			   /* Hash table */
static code_int  hsize = HSIZE;			   /* Used for dynamic table sizing */

static int_64 	 nmask = MAXCODE(INIT_BITS);	   /* Used for decompression */
static int_64 	 obuf[2*IBUFL/4];		   /* Compressed word buffer */
static int_64 	 cbuf[2*IBUFL];			   /* Decompress: compressed buffer */
						   /* Compress: input buffer */
static int_64 	 coutbuf[2*IBUFL];		   /* Compress: temp file between 
						    * _lz_output() and _lz_pack_output() 
						    * Decompress: de_stack
						    */

static code_int  free_ent = 0;			   /* First unused entry */
static int_64 	 r_off_global;
static int_64 	 b_global;

/*
 * Block compression parameters -- after all codes are 
 * used up, and compression rate changes, start over.
Esempio n. 27
0
static int
LZWDecodeCompat(TIFF* tif, uint8* op0, tmsize_t occ0, uint16 s)
{
	static const char module[] = "LZWDecodeCompat";
	LZWCodecState *sp = DecoderState(tif);
	char *op = (char*) op0;
	long occ = (long) occ0;
	char *tp;
	unsigned char *bp;
	int code, nbits;
	long nextbits, nextdata, nbitsmask;
	code_t *codep, *free_entp, *maxcodep, *oldcodep;

	(void) s;
	assert(sp != NULL);

	/*
	  Fail if value does not fit in long.
	*/
	if ((tmsize_t) occ != occ0)
	        return (0);

	/*
	 * Restart interrupted output operation.
	 */
	if (sp->dec_restart) {
		long residue;

		codep = sp->dec_codep;
		residue = codep->length - sp->dec_restart;
		if (residue > occ) {
			/*
			 * Residue from previous decode is sufficient
			 * to satisfy decode request.  Skip to the
			 * start of the decoded string, place decoded
			 * values in the output buffer, and return.
			 */
			sp->dec_restart += occ;
			do {
				codep = codep->next;
			} while (--residue > occ);
			tp = op + occ;
			do {
				*--tp = codep->value;
				codep = codep->next;
			} while (--occ);
			return (1);
		}
		/*
		 * Residue satisfies only part of the decode request.
		 */
		op += residue;
		occ -= residue;
		tp = op;
		do {
			*--tp = codep->value;
			codep = codep->next;
		} while (--residue);
		sp->dec_restart = 0;
	}

	bp = (unsigned char *)tif->tif_rawcp;
#ifdef LZW_CHECKEOS
	sp->dec_bitsleft = (((uint64)tif->tif_rawcc) << 3);
#endif
	nbits = sp->lzw_nbits;
	nextdata = sp->lzw_nextdata;
	nextbits = sp->lzw_nextbits;
	nbitsmask = sp->dec_nbitsmask;
	oldcodep = sp->dec_oldcodep;
	free_entp = sp->dec_free_entp;
	maxcodep = sp->dec_maxcodep;

	while (occ > 0) {
		NextCode(tif, sp, bp, code, GetNextCodeCompat);
		if (code == CODE_EOI)
			break;
		if (code == CODE_CLEAR) {
			do {
				free_entp = sp->dec_codetab + CODE_FIRST;
				_TIFFmemset(free_entp, 0,
					    (CSIZE - CODE_FIRST) * sizeof (code_t));
				nbits = BITS_MIN;
				nbitsmask = MAXCODE(BITS_MIN);
				maxcodep = sp->dec_codetab + nbitsmask;
				NextCode(tif, sp, bp, code, GetNextCodeCompat);
			} while (code == CODE_CLEAR);	/* consecutive CODE_CLEAR codes */
			if (code == CODE_EOI)
				break;
			if (code > CODE_CLEAR) {
				TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
				"LZWDecode: Corrupted LZW table at scanline %d",
					     tif->tif_row);
				return (0);
			}
			*op++ = (char)code;
			occ--;
			oldcodep = sp->dec_codetab + code;
			continue;
		}
		codep = sp->dec_codetab + code;

		/*
		 * Add the new entry to the code table.
		 */
		if (free_entp < &sp->dec_codetab[0] ||
		    free_entp >= &sp->dec_codetab[CSIZE]) {
			TIFFErrorExt(tif->tif_clientdata, module,
			    "Corrupted LZW table at scanline %d", tif->tif_row);
			return (0);
		}

		free_entp->next = oldcodep;
		if (free_entp->next < &sp->dec_codetab[0] ||
		    free_entp->next >= &sp->dec_codetab[CSIZE]) {
			TIFFErrorExt(tif->tif_clientdata, module,
			    "Corrupted LZW table at scanline %d", tif->tif_row);
			return (0);
		}
		free_entp->firstchar = free_entp->next->firstchar;
		free_entp->length = free_entp->next->length+1;
		free_entp->value = (codep < free_entp) ?
		    codep->firstchar : free_entp->firstchar;
		if (++free_entp > maxcodep) {
			if (++nbits > BITS_MAX)		/* should not happen */
				nbits = BITS_MAX;
			nbitsmask = MAXCODE(nbits);
			maxcodep = sp->dec_codetab + nbitsmask;
		}
		oldcodep = codep;
		if (code >= 256) {
			/*
			 * Code maps to a string, copy string
			 * value to output (written in reverse).
			 */
			if(codep->length == 0) {
				TIFFErrorExt(tif->tif_clientdata, module,
				    "Wrong length of decoded "
				    "string: data probably corrupted at scanline %d",
				    tif->tif_row);
				return (0);
			}
			if (codep->length > occ) {
				/*
				 * String is too long for decode buffer,
				 * locate portion that will fit, copy to
				 * the decode buffer, and setup restart
				 * logic for the next decoding call.
				 */
				sp->dec_codep = codep;
				do {
					codep = codep->next;
				} while (codep->length > occ);
				sp->dec_restart = occ;
				tp = op + occ;
				do  {
					*--tp = codep->value;
					codep = codep->next;
				}  while (--occ);
				break;
			}
			assert(occ >= codep->length);
			op += codep->length;
			occ -= codep->length;
			tp = op;
			do {
				*--tp = codep->value;
			} while( (codep = codep->next) != NULL );
		} else {
			*op++ = (char)code;
			occ--;
		}
	}

	tif->tif_rawcc -= (tmsize_t)( (uint8*) bp - tif->tif_rawcp );
	tif->tif_rawcp = (uint8*) bp;
	sp->lzw_nbits = (unsigned short)nbits;
	sp->lzw_nextdata = nextdata;
	sp->lzw_nextbits = nextbits;
	sp->dec_nbitsmask = nbitsmask;
	sp->dec_oldcodep = oldcodep;
	sp->dec_free_entp = free_entp;
	sp->dec_maxcodep = maxcodep;

	if (occ > 0) {
#if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
		TIFFErrorExt(tif->tif_clientdata, module,
			"Not enough data at scanline %d (short %I64d bytes)",
			     tif->tif_row, (unsigned __int64) occ);
#else
		TIFFErrorExt(tif->tif_clientdata, module,
			"Not enough data at scanline %d (short %llu bytes)",
			     tif->tif_row, (unsigned long long) occ);
#endif
		return (0);
	}
	return (1);
}
Esempio n. 28
0
/*
 * Read the full COMPRESS-compressed data stream.
 */
Ics_Error IcsReadCompress (Ics_Header* IcsStruct, void* outbuf, size_t len)
{
   ICSINIT;
   Ics_BlockRead* br = (Ics_BlockRead*)IcsStruct->BlockRead;
   unsigned char *stackp;
   long int code;
   int finchar;
   long int oldcode;
   long int incode;
   int inbits;
   int posbits;
   size_t outpos = 0;
   size_t insize;
   int bitmask;
   long int free_ent;
   long int maxcode;
   long int maxmaxcode;
   int n_bits;
   size_t rsize;
   int block_mode;
   int maxbits;
   int ii;
   int offset;
   unsigned char *inbuf = NULL;
   unsigned char *htab = NULL;
   unsigned short *codetab = NULL;

   /* Dynamically allocate memory that's static in (N)compress. */
   inbuf = (unsigned char*)malloc (IBUFSIZ+IBUFXTRA);
   if (inbuf == NULL) {
      error = IcsErr_Alloc;
      goto error_exit;
   }
   htab = (unsigned char*)malloc (HSIZE*4); /* Not sure about the size of this thing, original code uses a long int array that's cast to char */
   if (htab == NULL) {
      error = IcsErr_Alloc;
      goto error_exit;
   }
   codetab = (unsigned short*)malloc (HSIZE*sizeof(unsigned short));
   if (codetab == NULL) {
      error = IcsErr_Alloc;
      goto error_exit;
   }

   if ((rsize = fread(inbuf, 1, IBUFSIZ, br->DataFilePtr)) <= 0) {
      error = IcsErr_FReadIds;
      goto error_exit;
   }
   insize = rsize;
   if (insize < 3 || inbuf[0] != MAGIC_1 || inbuf[1] != MAGIC_2) {
      printf("point 1!\n");
      error = IcsErr_CorruptedStream;
      goto error_exit;
   }

   maxbits = inbuf[2] & BIT_MASK;
   block_mode = inbuf[2] & BLOCK_MODE;
   maxmaxcode = MAXCODE(maxbits);
   if (maxbits > BITS) {
      error = IcsErr_DecompressionProblem;
      goto error_exit;
   }

   maxcode = MAXCODE(n_bits = INIT_BITS)-1;
   bitmask = (1<<n_bits)-1;
   oldcode = -1;
   finchar = 0;
   posbits = 3<<3;

   free_ent = ((block_mode) ? FIRST : 256);

   clear_tab_prefixof();   /* As above, initialize the first 256 entries in the table. */
   for (code = 255 ; code >= 0 ; --code) {
      tab_suffixof(code) = (unsigned char)code;
   }

   do {
resetbuf:

      offset = posbits >> 3;
      insize = offset <= insize ? insize - offset : 0;
      for (ii = 0 ; ii < insize ; ++ii) {
         inbuf[ii] = inbuf[ii+offset];
      }
      posbits = 0;

      if (insize < IBUFXTRA) {
         rsize = fread(inbuf+insize, 1, IBUFSIZ, br->DataFilePtr);
         if (rsize <= 0 && !feof(br->DataFilePtr)) {
            error = IcsErr_FReadIds;
            goto error_exit;
         }
         insize += rsize;
      }

      inbits = ((rsize > 0) ? (insize - insize%n_bits)<<3 : (insize<<3)-(n_bits-1));

      while (inbits > posbits) {
         if (free_ent > maxcode) {
            posbits = ((posbits-1) + ((n_bits<<3) - (posbits-1+(n_bits<<3))%(n_bits<<3)));
            ++n_bits;
            if (n_bits == maxbits) {
               maxcode = maxmaxcode;
            }
            else {
               maxcode = MAXCODE(n_bits)-1;
            }
            bitmask = (1<<n_bits)-1;
            goto resetbuf;
         }

         input(inbuf,posbits,code,n_bits,bitmask);

         if (oldcode == -1) {
            if (code >= 256) {
               printf("point 3!\n");
               error = IcsErr_CorruptedStream;
               goto error_exit;
            }
            ((unsigned char*)outbuf)[outpos++] = (unsigned char)(finchar = (int)(oldcode = code));
            continue;
         }

         if (code == CLEAR && block_mode) {
            clear_tab_prefixof();
            free_ent = FIRST - 1;
            posbits = ((posbits-1) + ((n_bits<<3) - (posbits-1+(n_bits<<3))%(n_bits<<3)));
            maxcode = MAXCODE(n_bits = INIT_BITS)-1;
            bitmask = (1<<n_bits)-1;
            goto resetbuf;
         }

         incode = code;
         stackp = de_stack;

         if (code >= free_ent) { /* Special case for KwKwK string.   */
            if (code > free_ent) {
               printf("point 4!\n");
               error = IcsErr_CorruptedStream;
               goto error_exit;
            }
            *--stackp = (unsigned char)finchar;
            code = oldcode;
         }

         /* Generate output characters in reverse order */
         while (code >= 256) {
            *--stackp = tab_suffixof(code);
            code = tab_prefixof(code);
         }
         *--stackp = (unsigned char)(finchar = tab_suffixof(code));

         /* And put them out in forward order */
         ii = de_stack-stackp;
         if (outpos+ii > len) {
            ii = len-outpos; /* do not write more in buffer than fits! */
         }
         memcpy(((unsigned char*)outbuf)+outpos, stackp, ii);
         outpos += ii;
         if (outpos == len) {
            goto error_exit;
         }

         if ((code = free_ent) < maxmaxcode) { /* Generate the new entry. */
            tab_prefixof(code) = (unsigned short)oldcode;
            tab_suffixof(code) = (unsigned char)finchar;
            free_ent = code+1;
         } 

         oldcode = incode; /* Remember previous code. */
      }

   } while (rsize > 0);
   
   if (outpos != len) {
      error = IcsErr_OutputNotFilled;
   }

error_exit:
   /* Deallocate stuff */
   if (inbuf) free(inbuf);
   if (htab) free(htab);
   if (codetab) free(codetab);
   return error;
}
Esempio n. 29
0
/*
 * Encode a chunk of pixels.
 *
 * Uses an open addressing double hashing (no chaining) on the 
 * prefix code/next character combination.  We do a variant of
 * Knuth's algorithm D (vol. 3, sec. 6.4) along with G. Knott's
 * relatively-prime secondary probe.  Here, the modular division
 * first probe is gives way to a faster exclusive-or manipulation. 
 * Also do block compression with an adaptive reset, whereby the
 * code table is cleared when the compression ratio decreases,
 * but after the table fills.  The variable-length output codes
 * are re-sized at this point, and a CODE_CLEAR is generated
 * for the decoder. 
 */
static int
LZWEncode(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s)
{
	register LZWCodecState *sp = EncoderState(tif);
	register long fcode;
	register hash_t *hp;
	register int h, c;
	hcode_t ent;
	long disp;
	long incount, outcount, checkpoint;
	unsigned long nextdata;
        long nextbits;
	int free_ent, maxcode, nbits;
	uint8* op;
	uint8* limit;

	(void) s;
	if (sp == NULL)
		return (0);

        assert(sp->enc_hashtab != NULL);

	/*
	 * Load local state.
	 */
	incount = sp->enc_incount;
	outcount = sp->enc_outcount;
	checkpoint = sp->enc_checkpoint;
	nextdata = sp->lzw_nextdata;
	nextbits = sp->lzw_nextbits;
	free_ent = sp->lzw_free_ent;
	maxcode = sp->lzw_maxcode;
	nbits = sp->lzw_nbits;
	op = tif->tif_rawcp;
	limit = sp->enc_rawlimit;
	ent = (hcode_t)sp->enc_oldcode;

	if (ent == (hcode_t) -1 && cc > 0) {
		/*
		 * NB: This is safe because it can only happen
		 *     at the start of a strip where we know there
		 *     is space in the data buffer.
		 */
		PutNextCode(op, CODE_CLEAR);
		ent = *bp++; cc--; incount++;
	}
	while (cc > 0) {
		c = *bp++; cc--; incount++;
		fcode = ((long)c << BITS_MAX) + ent;
		h = (c << HSHIFT) ^ ent;	/* xor hashing */
#ifdef _WINDOWS
		/*
		 * Check hash index for an overflow.
		 */
		if (h >= HSIZE)
			h -= HSIZE;
#endif
		hp = &sp->enc_hashtab[h];
		if (hp->hash == fcode) {
			ent = hp->code;
			continue;
		}
		if (hp->hash >= 0) {
			/*
			 * Primary hash failed, check secondary hash.
			 */
			disp = HSIZE - h;
			if (h == 0)
				disp = 1;
			do {
				/*
				 * Avoid pointer arithmetic because of
				 * wraparound problems with segments.
				 */
				if ((h -= disp) < 0)
					h += HSIZE;
				hp = &sp->enc_hashtab[h];
				if (hp->hash == fcode) {
					ent = hp->code;
					goto hit;
				}
			} while (hp->hash >= 0);
		}
		/*
		 * New entry, emit code and add to table.
		 */
		/*
		 * Verify there is space in the buffer for the code
		 * and any potential Clear code that might be emitted
		 * below.  The value of limit is setup so that there
		 * are at least 4 bytes free--room for 2 codes.
		 */
		if (op > limit) {
			tif->tif_rawcc = (tmsize_t)(op - tif->tif_rawdata);
			if( !TIFFFlushData1(tif) )
                            return 0;
			op = tif->tif_rawdata;
		}
		PutNextCode(op, ent);
		ent = (hcode_t)c;
		hp->code = (hcode_t)(free_ent++);
		hp->hash = fcode;
		if (free_ent == CODE_MAX-1) {
			/* table is full, emit clear code and reset */
			cl_hash(sp);
			sp->enc_ratio = 0;
			incount = 0;
			outcount = 0;
			free_ent = CODE_FIRST;
			PutNextCode(op, CODE_CLEAR);
			nbits = BITS_MIN;
			maxcode = MAXCODE(BITS_MIN);
		} else {
			/*
			 * If the next entry is going to be too big for
			 * the code size, then increase it, if possible.
			 */
			if (free_ent > maxcode) {
				nbits++;
				assert(nbits <= BITS_MAX);
				maxcode = (int) MAXCODE(nbits);
			} else if (incount >= checkpoint) {
				long rat;
				/*
				 * Check compression ratio and, if things seem
				 * to be slipping, clear the hash table and
				 * reset state.  The compression ratio is a
				 * 24+8-bit fractional number.
				 */
				checkpoint = incount+CHECK_GAP;
				CALCRATIO(sp, rat);
				if (rat <= sp->enc_ratio) {
					cl_hash(sp);
					sp->enc_ratio = 0;
					incount = 0;
					outcount = 0;
					free_ent = CODE_FIRST;
					PutNextCode(op, CODE_CLEAR);
					nbits = BITS_MIN;
					maxcode = MAXCODE(BITS_MIN);
				} else
					sp->enc_ratio = rat;
			}
		}
	hit:
		;
	}

	/*
	 * Restore global state.
	 */
	sp->enc_incount = incount;
	sp->enc_outcount = outcount;
	sp->enc_checkpoint = checkpoint;
	sp->enc_oldcode = ent;
	sp->lzw_nextdata = nextdata;
	sp->lzw_nextbits = nextbits;
	sp->lzw_free_ent = (unsigned short)free_ent;
	sp->lzw_maxcode = (unsigned short)maxcode;
	sp->lzw_nbits = (unsigned short)nbits;
	tif->tif_rawcp = op;
	return (1);
}
/*
 * Setup callback.
 */
static int
archive_compressor_compress_open(struct archive_write_filter *f)
{
	int ret;
	struct private_data *state;
	size_t bs = 65536, bpb;

	f->code = ARCHIVE_COMPRESSION_COMPRESS;
	f->name = "compress";

	ret = __archive_write_open_filter(f->next_filter);
	if (ret != ARCHIVE_OK)
		return (ret);

	state = (struct private_data *)calloc(1, sizeof(*state));
	if (state == NULL) {
		archive_set_error(f->archive, ENOMEM,
		    "Can't allocate data for compression");
		return (ARCHIVE_FATAL);
	}

	if (f->archive->magic == ARCHIVE_WRITE_MAGIC) {
		/* Buffer size should be a multiple number of the of bytes
		 * per block for performance. */
		bpb = archive_write_get_bytes_per_block(f->archive);
		if (bpb > bs)
			bs = bpb;
		else if (bpb != 0)
			bs -= bs % bpb;
	}
	state->compressed_buffer_size = bs;
	state->compressed = malloc(state->compressed_buffer_size);

	if (state->compressed == NULL) {
		archive_set_error(f->archive, ENOMEM,
		    "Can't allocate data for compression buffer");
		free(state);
		return (ARCHIVE_FATAL);
	}

	f->write = archive_compressor_compress_write;
	f->close = archive_compressor_compress_close;
	f->free = archive_compressor_compress_free;

	state->max_maxcode = 0x10000;	/* Should NEVER generate this code. */
	state->in_count = 0;		/* Length of input. */
	state->bit_buf = 0;
	state->bit_offset = 0;
	state->out_count = 3;		/* Includes 3-byte header mojo. */
	state->compress_ratio = 0;
	state->checkpoint = CHECK_GAP;
	state->code_len = 9;
	state->cur_maxcode = MAXCODE(state->code_len);
	state->first_free = FIRST;

	memset(state->hashtab, 0xff, sizeof(state->hashtab));

	/* Prime output buffer with a gzip header. */
	state->compressed[0] = 0x1f; /* Compress */
	state->compressed[1] = 0x9d;
	state->compressed[2] = 0x90; /* Block mode, 16bit max */
	state->compressed_offset = 3;

	f->data = state;
	return (0);
}