Example #1
0
BUFFER* BUFFER::compressZlib()
{
	z_stream strm;
	int ret;
	memset(&strm, 0, sizeof(z_stream));
	char* dest = (char*)malloc(_size);
	if(deflateInit(&strm, Z_BEST_COMPRESSION) != Z_OK) { Com_Error(false, "Failed to compress zlib buffer!"); return NULL; }
	strm.next_out = (Bytef*)dest;
	strm.next_in = (Bytef*)_origin;
	strm.avail_out = _size;
	strm.avail_in = _size;
	
	ret = deflate(&strm, Z_FINISH);
	if(ret != Z_STREAM_END) { Com_Error(false, "Failed to compress zlib buffer!"); return NULL; }
	ret = deflateEnd(&strm);
	if(ret != Z_OK) { Com_Error(false, "Failed to compress zlib buffer!"); return NULL; }
	return new BUFFER(dest, strm.total_out);
}
Example #2
0
/*************************************************************************
 *
 * NAME  DeflateStream::DeflateStream()
 *
 * SYNOPSIS
 *   DeflateStream::DeflateStream (ofs)
 *   DeflateStream::DeflateStream (ofstream &)
 *
 * DESCRIPTION
 *   Constructor for DeflateStream, assign the ofstream ofs to be deflated
 *
 * INPUTS
 *   ofs                    - an ofstream
 *           
 * RESULT    
 *   none
 *           
 * KNOWN BUGS
 *   none
 *           
 ******/     
DeflateStream::DeflateStream(std::ofstream *ofs)
{
    int status;
    this->errornum=0;
    this->ofs=ofs;

    this->zstrm.next_out=this->output_buffer;
    this->zstrm.avail_out=BUFFER_SIZE;
    this->zstrm.opaque=Z_NULL;
    this->zstrm.zalloc=Z_NULL;
    this->zstrm.zfree=Z_NULL;
    this->zstrm.opaque=Z_NULL;

    status=deflateInit(&this->zstrm,Z_DEFAULT_COMPRESSION);
    if (status!=Z_OK)
        this->errornum=1;

}
Example #3
0
static int lz_deflate_new(lua_State *L) {
    int level = luaL_optint(L, 1, Z_DEFAULT_COMPRESSION);

    /*  Allocate the stream: */
    z_stream* stream = (z_stream*)lua_newuserdata(L, sizeof(z_stream));

    stream->zalloc = Z_NULL;
    stream->zfree  = Z_NULL;
    lz_assert(L, deflateInit(stream, level), stream, __FILE__, __LINE__);

    /*  Don't allow destructor to execute unless deflateInit was successful: */
    luaL_getmetatable(L, "lz.deflate.meta");
    lua_setmetatable(L, -2);

    lua_pushnil(L);
    lua_pushcclosure(L, lz_deflate, 2);
    return 1;
}
Example #4
0
int deflate_init(struct comp_ctx **comp_ctx, int level)
{
	z_stream *strm;

	if (init_comp_ctx(comp_ctx) < 0)
		return -1;

	strm = &(*comp_ctx)->strm;

	if (deflateInit(strm, level) != Z_OK) {
		deinit_comp_ctx(comp_ctx);
		return -1;
	}

	(*comp_ctx)->cur_lvl = level;

	return 0;
}
Example #5
0
static void zlib_deflate(ge_Buffer* b_out, ge_Buffer* b_in){
	int ret, flush;
	unsigned have;
	z_stream strm;
	unsigned char in[CHUNK];
	unsigned char out[CHUNK];

	/* allocate deflate state */
	strm.zalloc = Z_NULL;
	strm.zfree = Z_NULL;
	strm.opaque = Z_NULL;
	ret = deflateInit(&strm, Z_DEFAULT_COMPRESSION);
	if (ret != Z_OK){
		return;
	}

	/* compress until end of file */
	do {
		strm.avail_in = geBufferRead(b_in, in, CHUNK);
		if (strm.avail_in < 0){
			break;
		}
		flush = (strm.avail_in == 0) ? Z_FINISH : Z_NO_FLUSH;
		strm.next_in = in;

		/* run deflate() on input until output buffer not full, finish
		   compression if all of source has been read in */
		do {
			strm.avail_out = CHUNK;
			strm.next_out = out;
			ret = deflate(&strm, flush);	/* no bad return value */
			have = CHUNK - strm.avail_out;
			if(geBufferWrite(b_out, out, have) != have){
				(void)deflateEnd(&strm);
				return;
			}
		} while (strm.avail_out == 0);

		/* done when last data in file processed */
	} while (flush != Z_FINISH);

	/* clean up and return */
	(void)deflateEnd(&strm);
}
Example #6
0
void zfwrite(char* p_ptr, int p_size, FILE* p_fp, int p_sig)
{
	if (p_ptr && p_fp)
	{
		const int	bufsize = 1024 * 512; // 512kb
		char		buf[bufsize];

		z_stream z;
		
		z.zalloc = Z_NULL;
		z.zfree  = Z_NULL;
		z.opaque = Z_NULL;
		
		if (deflateInit(&z, Z_DEFAULT_COMPRESSION) != Z_OK && z.msg) OutputDebugString(z.msg);

		z.next_in	= (Bytef*)p_ptr;
		z.avail_in	= p_size;
		z.next_out	= (Bytef*)buf;
		z.avail_out = bufsize;
		
		/* 識別子 */
		int fsig = p_sig;
		fwrite(&fsig, 1, 4, p_fp);

		/* 元のサイズ */
		fwrite(&p_size, 1, 4, p_fp);
		
		int status = Z_OK;
		while (status != Z_STREAM_END)
		{
			status = deflate(&z, Z_FINISH);
			if (status < Z_OK && z.msg) OutputDebugString(z.msg);
			
			if (z.avail_out == 0 || status == Z_STREAM_END)
			{
				fwrite(buf, 1, bufsize - z.avail_out, p_fp);
				z.next_out = (Bytef*)buf;
				z.avail_out = bufsize;
			}
		}
		
		if (deflateEnd(&z) != Z_OK && z.msg) OutputDebugString(z.msg);
	}
}
Example #7
0
int Compression::compress(uint8_t *p_dst, const uint8_t *p_src, int p_src_size,Mode p_mode) {

	switch(p_mode) {
		case MODE_FASTLZ: {

			if (p_src_size<16) {
				uint8_t src[16];
				zeromem(&src[p_src_size],16-p_src_size);
				copymem(src,p_src,p_src_size);
				return fastlz_compress(src,16,p_dst);
			} else {
				return fastlz_compress(p_src,p_src_size,p_dst);
			}

		} break;
		case MODE_DEFLATE: {

			z_stream strm;
			strm.zalloc = zipio_alloc;
			strm.zfree = zipio_free;
			strm.opaque = Z_NULL;
			int err = deflateInit(&strm,Z_DEFAULT_COMPRESSION);
			if (err!=Z_OK)
			    return -1;

			strm.avail_in=p_src_size;
			int aout = deflateBound(&strm,p_src_size);;
			/*if (aout>p_src_size) {
				deflateEnd(&strm);
				return -1;
			}*/
			strm.avail_out=aout;
			strm.next_in=(Bytef*)p_src;
			strm.next_out=p_dst;
			deflate(&strm,Z_FINISH);
			aout = aout - strm.avail_out;
			deflateEnd(&strm);
			return aout;

		} break;
	}

	ERR_FAIL_V(-1);
}
Example #8
0
static SCM squeeze(SCM source, int method) {
	z_stream strm;
	DEFLATE_BLOB *blob;
	int ret;
	size_t inlen, outlen;
	char *inbuf, *outbuf;
	strm.zalloc = Z_NULL;
	strm.zfree = Z_NULL;
	strm.opaque = Z_NULL;
	if (method == SQUEEZE_GZIP) {
		ret = deflateInit2(&strm, Z_DEFAULT_COMPRESSION,
				Z_DEFLATED, 31, 8, Z_DEFAULT_STRATEGY);
		}
	else {
		ret = deflateInit(&strm, Z_DEFAULT_COMPRESSION);
		}
	if (ret != Z_OK) {
		log_msg("zlib: deflate init failed\n");
		return SCM_BOOL_F;
		}
	inbuf = scm_to_utf8_stringn(source, &inlen);
	strm.total_in = strm.avail_in = inlen;
	strm.next_in = (unsigned char*)inbuf;
	outlen = deflateBound(&strm, inlen);
	outbuf = (char *)malloc(outlen);
	strm.total_out = 0;
	strm.avail_out = outlen;
	strm.next_out = (unsigned char *)outbuf;
	ret = deflate(&strm, Z_FINISH);
	free(inbuf);
	blob = (DEFLATE_BLOB *)scm_gc_malloc(sizeof(DEFLATE_BLOB) +
						strm.total_out, "gzip-blob");
	blob->zip_len = strm.total_out;
	blob->orig_len = inlen;
	log_msg("compress %s %d -> %d\n",
		(method == SQUEEZE_GZIP ? "gzip" : "deflate"),
		blob->orig_len, blob->zip_len);
	blob->method = method;
	ret = deflateEnd(&strm);
	if (ret != Z_OK) printf("deflateEnd: %d\n", ret);
	memcpy(blob->payload, outbuf, blob->zip_len);
	free(outbuf);
	SCM_RETURN_NEWSMOB(deflate_tag, blob);
	}
Example #9
0
/* Set up new zlib compression streams, close the old ones. Only
 * called from gen_new_keys() */
static void gen_new_zstreams() {

	/* create new zstreams */
	if (ses.newkeys->recv_algo_comp == DROPBEAR_COMP_ZLIB) {
		ses.newkeys->recv_zstream = (z_streamp)m_malloc(sizeof(z_stream));
		ses.newkeys->recv_zstream->zalloc = Z_NULL;
		ses.newkeys->recv_zstream->zfree = Z_NULL;
		
		if (inflateInit(ses.newkeys->recv_zstream) != Z_OK) {
			dropbear_exit("zlib error");
		}
	} else {
		ses.newkeys->recv_zstream = NULL;
	}

	if (ses.newkeys->trans_algo_comp == DROPBEAR_COMP_ZLIB) {
		ses.newkeys->trans_zstream = (z_streamp)m_malloc(sizeof(z_stream));
		ses.newkeys->trans_zstream->zalloc = Z_NULL;
		ses.newkeys->trans_zstream->zfree = Z_NULL;
	
		if (deflateInit(ses.newkeys->trans_zstream, Z_DEFAULT_COMPRESSION) 
				!= Z_OK) {
			dropbear_exit("zlib error");
		}
	} else {
		ses.newkeys->trans_zstream = NULL;
	}
	
	/* clean up old keys */
	if (ses.keys->recv_zstream != NULL) {
		if (inflateEnd(ses.keys->recv_zstream) == Z_STREAM_ERROR) {
			/* Z_DATA_ERROR is ok, just means that stream isn't ended */
			dropbear_exit("crypto error");
		}
		m_free(ses.keys->recv_zstream);
	}
	if (ses.keys->trans_zstream != NULL) {
		if (deflateEnd(ses.keys->trans_zstream) == Z_STREAM_ERROR) {
			/* Z_DATA_ERROR is ok, just means that stream isn't ended */
			dropbear_exit("crypto error");
		}
		m_free(ses.keys->trans_zstream);
	}
}
Example #10
0
void compressZlib(SharedBuffer<u8> data, std::ostream &os)
{
	z_stream z;
	const s32 bufsize = 16384;
	char output_buffer[bufsize];
	int status = 0;
	int ret;

	z.zalloc = Z_NULL;
	z.zfree = Z_NULL;
	z.opaque = Z_NULL;

	ret = deflateInit(&z, -1);
	if(ret != Z_OK)
		throw SerializationError("compressZlib: deflateInit failed");
	
	// Point zlib to our input buffer
	z.next_in = (Bytef*)&data[0];
	z.avail_in = data.getSize();
	// And get all output
	for(;;)
	{
		z.next_out = (Bytef*)output_buffer;
		z.avail_out = bufsize;
		
		status = deflate(&z, Z_FINISH);
		if(status == Z_NEED_DICT || status == Z_DATA_ERROR
				|| status == Z_MEM_ERROR)
		{
			zerr(status);
			throw SerializationError("compressZlib: deflate failed");
		}
		int count = bufsize - z.avail_out;
		if(count)
			os.write(output_buffer, count);
		// This determines zlib has given all output
		if(status == Z_STREAM_END)
			break;
	}

	deflateEnd(&z);

}
Example #11
0
// Don't call this outside of the constructor.
void TZlibTransport::initZlib() {
  int rv;
  bool r_init = false;
  try {
    rstream_ = new z_stream;
    wstream_ = new z_stream;

    rstream_->zalloc = Z_NULL;
    wstream_->zalloc = Z_NULL;
    rstream_->zfree  = Z_NULL;
    wstream_->zfree  = Z_NULL;
    rstream_->opaque = Z_NULL;
    wstream_->opaque = Z_NULL;

    rstream_->next_in   = crbuf_;
    wstream_->next_in   = uwbuf_;
    rstream_->next_out  = urbuf_;
    wstream_->next_out  = cwbuf_;
    rstream_->avail_in  = 0;
    wstream_->avail_in  = 0;
    rstream_->avail_out = urbuf_size_;
    wstream_->avail_out = cwbuf_size_;

    rv = inflateInit(rstream_);
    checkZlibRv(rv, rstream_->msg);

    // Have to set this flag so we know whether to de-initialize.
    r_init = true;

    rv = deflateInit(wstream_, Z_DEFAULT_COMPRESSION);
    checkZlibRv(rv, wstream_->msg);
  }

  catch (...) {
    if (r_init) {
      rv = inflateEnd(rstream_);
      checkZlibRvNothrow(rv, rstream_->msg);
    }
    // There is no way we can get here if wstream_ was initialized.

    throw;
  }
}
Example #12
0
size_t
block_deflate(uint8_t *uncompr, uint8_t *compr, const size_t len)
{
	z_stream c_stream;
	int err;

	if (len == 0)
		return (0);

	c_stream.zalloc = myalloc;
	c_stream.zfree = myfree;
	c_stream.opaque = NULL;

	err = deflateInit(&c_stream, Z_DEFAULT_COMPRESSION);
	check_err(err, "deflateInit");

	c_stream.next_in  = uncompr;
	c_stream.next_out = compr;
	c_stream.avail_in = len;
	c_stream.avail_out = len*2u +512u;

	while (c_stream.total_in != len && c_stream.total_out < (len*2u + 512u)) {
		err = deflate(&c_stream, Z_NO_FLUSH);
#ifdef DEBUG
		printf("deflate progress: len = %zd  total_in = %lu  total_out = %lu\n", len, c_stream.total_in, c_stream.total_out);
#endif
		check_err(err, "deflate(..., Z_NO_FLUSH)");
	}

	for (;;) {
		err = deflate(&c_stream, Z_FINISH);
#ifdef DEBUG
		printf("deflate    final: len = %zd  total_in = %lu  total_out = %lu\n", len, c_stream.total_in, c_stream.total_out);
#endif
		if (err == Z_STREAM_END) break;
		check_err(err, "deflate(..., Z_STREAM_END)");
	}

	err = deflateEnd(&c_stream);
	check_err(err, "deflateEnd");

	return ((size_t)c_stream.total_out);
}
Example #13
0
IoObject *IoZlibEncoder_beginProcessing(IoZlibEncoder *self, IoObject *locals, IoMessage *m)
{
	/*doc ZlibEncoder beginProcessing
	Initializes the algorithm.
	*/
	
	z_stream *strm = DATA(self)->strm;
	int ret;

	strm->zalloc = Z_NULL;
	strm->zfree = Z_NULL;
	strm->opaque = Z_NULL;
	strm->avail_in = 0;
	strm->next_in = Z_NULL;
	ret = deflateInit(strm, DATA(self)->level);
	IOASSERT(ret == Z_OK, "unable to initialize zlib via inflateInit()");

	return self;
}
Example #14
0
/*
** zip_init
**      Initialize compression structures for a server.
**      If failed, zip_free() has to be called.
*/
int     zip_init(aClient *cptr, int compressionlevel)
{
  cptr->zip  = (aZdata *) MyMalloc(sizeof(aZdata));
  cptr->zip->incount = 0;
  cptr->zip->outcount = 0;

  cptr->zip->in  = (z_stream *) MyMalloc(sizeof(z_stream));
  bzero(cptr->zip->in, sizeof(z_stream)); /* Just to be sure -- Syzop */
  cptr->zip->in->total_in = 0;
  cptr->zip->in->total_out = 0;
  cptr->zip->in->zalloc = NULL;
  cptr->zip->in->zfree = NULL;
  cptr->zip->in->data_type = Z_ASCII;
  if (inflateInit(cptr->zip->in) != Z_OK)
    {
      cptr->zip->out = NULL;
      return -1;
    }

  cptr->zip->out = (z_stream *) MyMalloc(sizeof(z_stream));
  bzero(cptr->zip->out, sizeof(z_stream)); /* Just to be sure -- Syzop */
  cptr->zip->out->total_in = 0;
  cptr->zip->out->total_out = 0;
  cptr->zip->out->zalloc = NULL;
  cptr->zip->out->zfree = NULL;
  cptr->zip->out->data_type = Z_ASCII;
  if (deflateInit(cptr->zip->out, compressionlevel) != Z_OK)
    return -1;

  if (!unzipbuf)
  {
  	unzipbuf = MyMallocEx(UNZIP_BUFFER_SIZE); /* big chunk! */
  	if (!unzipbuf)
  	{
  		ircd_log(LOG_ERROR, "zip_init(): out of memory (trying to alloc %d bytes)!", UNZIP_BUFFER_SIZE);
  		sendto_realops("zip_init(): out of memory (trying to alloc %d bytes)!", UNZIP_BUFFER_SIZE);
  		return -1;
  	}
  }


  return 0;
}
Example #15
0
static int compress_cb(lua_State* L)
{
	size_t srcsize;
	const char* srcbuffer = luaL_checklstring(L, 1, &srcsize);

	int outputchunks = 0;
	uint8_t outputbuffer[64*1024];

	z_stream zs = {0};
	int i = deflateInit(&zs, 1);
	if (i != Z_OK)
		return 0;

	zs.avail_in = srcsize;
	zs.next_in = (uint8_t*) srcbuffer;

	luaL_checkstack(L, STACKSIZE, "out of memory");
	do
	{
		zs.avail_out = sizeof(outputbuffer);
		zs.next_out = outputbuffer;

		i = deflate(&zs, Z_FINISH);

		int have = sizeof(outputbuffer) - zs.avail_out;
		lua_pushlstring(L, (char*) outputbuffer, have);
		outputchunks++;

		if (outputchunks == (STACKSIZE-1))
		{
			/* Stack full! Concatenate what we've got, to empty the stack, and
			 * keep going. This will only happen on very large input files. */
			lua_concat(L, outputchunks);
			outputchunks = 1;
		}
	}
	while (i != Z_STREAM_END);

	(void)deflateEnd(&zs);

	lua_concat(L, outputchunks);
	return 1;
}
Example #16
0
BOOL ThreadData::zlibInit( void )
{
    proto->Log( "Zlib init..." );
    zStreamIn.zalloc = Z_NULL;
    zStreamIn.zfree = Z_NULL;
    zStreamIn.opaque = Z_NULL;
    zStreamIn.next_in = Z_NULL;
    zStreamIn.avail_in = 0;

    zStreamOut.zalloc = Z_NULL;
    zStreamOut.zfree = Z_NULL;
    zStreamOut.opaque = Z_NULL;

    if ( deflateInit( &zStreamOut, Z_BEST_COMPRESSION) != Z_OK ) return FALSE;
    if ( inflateInit( &zStreamIn ) != Z_OK ) return FALSE;

    zRecvReady = true;
    return TRUE;
}
Example #17
0
bool CCompressedConnection::SetupOutputStream()
{
	m_pZOutput = new CBuffer(8192);
	if(m_pZOutput == 0)
	{
		return false;
	}

	if(deflateInit(&m_sOutput, Z_DEFAULT_COMPRESSION) != Z_OK)
	{
		delete m_pZOutput;
		m_pZOutput = 0;
		return false;
	}
	m_nNextDeflateFlush = m_nTotalOutput + 4096;
	m_tDeflateFlush.start();

	return true;
}
Example #18
0
int CompressData( const BYTE* abSrc, int nLenSrc, BYTE* abDst, int nLenDst )
{
    z_stream zInfo ={0};
    zInfo.total_in=  zInfo.avail_in=  nLenSrc;
    zInfo.total_out= zInfo.avail_out= nLenDst;
    zInfo.next_in= (BYTE*)abSrc;
    zInfo.next_out= abDst;
    
    int nErr, nRet= -1;
    nErr= deflateInit( &zInfo, Z_DEFAULT_COMPRESSION ); // zlib function
    if ( nErr == Z_OK ) {
        nErr= deflate( &zInfo, Z_FINISH );              // zlib function
        if ( nErr == Z_STREAM_END ) {
            nRet= zInfo.total_out;
        }
    }
    deflateEnd( &zInfo );    // zlib function
    return( nRet );
}
Example #19
0
void zlib_compress_stream(std::istream& input_buffer, std::ostream& output_buffer) {
    int ret, flush;
    unsigned have;
    z_stream strm;
    unsigned char in[ZLIB_CHUNK_SIZE];
    unsigned char out[ZLIB_CHUNK_SIZE];

    strm.zalloc = Z_NULL;
    strm.zfree = Z_NULL;
    strm.opaque = Z_NULL;
    int level = Z_DEFAULT_COMPRESSION;
    ret = deflateInit(&strm, level);
    if (ret != Z_OK) {
        throw std::runtime_error("zlib_compress_stream: Failed initializing stream");
    }

    do {
        input_buffer.read((char*)in, ZLIB_CHUNK_SIZE);
        strm.avail_in = input_buffer.gcount();
        if (!input_buffer && !input_buffer.eof()) {
            (void)deflateEnd(&strm);
            throw std::runtime_error("zlib_compress_stream: Unexpected buffer underflow");
        }
        flush = input_buffer.eof() ? Z_FINISH : Z_NO_FLUSH;
        strm.next_in = in;

        do {
            strm.avail_out = ZLIB_CHUNK_SIZE;
            strm.next_out = out;
            ret = deflate(&strm, flush);
            assert(ret != Z_STREAM_ERROR);
            have = ZLIB_CHUNK_SIZE - strm.avail_out;
            output_buffer.write((const char*)out, have);
        } while (strm.avail_out == 0);
        assert(strm.avail_in == 0);

    } while (flush != Z_FINISH);
    assert(ret == Z_STREAM_END);

    output_buffer.flush();

    (void)deflateEnd(&strm);
}
Example #20
0
uint16_t readAndCompress(FILE * file, uint8_t ** dataBuf, uint16_t pos, uint16_t * uncompressedDataLen) {

   uint16_t have = 0;
   *uncompressedDataLen = fread((*dataBuf) + pos, 1, *uncompressedDataLen, file);
#ifdef USE_ZLIB
   // So now we have to check if zlib can compress this or not
   z_stream strm;
   uint8_t in[*uncompressedDataLen];
   uint8_t out[*uncompressedDataLen];
   
   strm.zalloc = Z_NULL;
   strm.zfree = Z_NULL;
   strm.opaque = Z_NULL;
   deflateInit(&strm, Z_DEFAULT_COMPRESSION);
   
   strm.avail_in = *uncompressedDataLen;
   memcpy(in, (*dataBuf) + pos, *uncompressedDataLen);
   strm.next_in = in;
   
   //do {
      strm.avail_out = *uncompressedDataLen;
      strm.next_out = out + have;
   
      deflate(&strm, Z_FINISH);
      have = *uncompressedDataLen - strm.avail_out;
      //printf("%s:%s[%d] have=%d strm.avail_out=%d\n", __FILE__, __func__, __LINE__, have, strm.avail_out);
      //getchar();
   //}while(strm.avail_out != 0);
   
   deflateEnd(&strm);
   
   //printf("%s:%s[%d] *uncompressedDataLen=%d have=%d strm.avail_out=%d\n", __FILE__, __func__, __LINE__, *uncompressedDataLen, have, strm.avail_out);
   //if(strm.avail_out == 0 && have > 0 && *uncompressedDataLen > have) {
   if(have < *uncompressedDataLen) {
      memcpy((*dataBuf) + pos, out, have);
   } else 
#endif
   {
      have = *uncompressedDataLen;
   }
   
   return have;
}
Example #21
0
static int index_fd(const char *path, int namelen, struct cache_entry *ce, int fd, struct stat *st)
{
	z_stream stream;
	int max_out_bytes = namelen + st->st_size + 200;
	void *out = malloc(max_out_bytes);
	void *metadata = malloc(namelen + 200);
	void *in = mmap(NULL, st->st_size, PROT_READ, MAP_PRIVATE, fd, 0);
	SHA_CTX c;

	close(fd);
	if (!out || (int)(long)in == -1)
		return -1;

	memset(&stream, 0, sizeof(stream));
	deflateInit(&stream, Z_BEST_COMPRESSION);

	/*
	 * ASCII size + nul byte
	 */	
	stream.next_in = metadata;
	stream.avail_in = 1+sprintf(metadata, "blob %lu", (unsigned long) st->st_size);
	stream.next_out = out;
	stream.avail_out = max_out_bytes;
	while (deflate(&stream, 0) == Z_OK)
		/* nothing */;

	/*
	 * File content
	 */
	stream.next_in = in;
	stream.avail_in = st->st_size;
	while (deflate(&stream, Z_FINISH) == Z_OK)
		/*nothing */;

	deflateEnd(&stream);
	
	SHA1_Init(&c);
	SHA1_Update(&c, out, stream.total_out);
	SHA1_Final(ce->sha1, &c);

	return write_sha1_buffer(ce->sha1, out, stream.total_out);
}
void Flate::Deflate(ISequentialStream* pOutput, ISequentialStream* pInput)
{
	z_stream zstm;
	memset(&zstm,0,sizeof(z_stream));

	BYTE zBufIn[UNCOMP_BUFSIZE];
	BYTE zBufOut[COMP_BUFSIZE];

	deflateInit(&zstm, Z_DEFAULT_COMPRESSION);

	int err = Z_OK;
	while ( TRUE )
	{
		ULONG cbRead = 0;
		HRESULT hResult = pInput->Read(zBufIn, sizeof(zBufIn), &cbRead);
		if ( hResult != S_OK || cbRead == 0 )
			break;

		zstm.next_in = (Bytef*)zBufIn;
		zstm.avail_in = (uInt)cbRead;

		while ( TRUE )
		{
			zstm.next_out = (Bytef*)zBufOut;
			zstm.avail_out = sizeof(zBufOut);

			err = deflate(&zstm, Z_SYNC_FLUSH);
			if (err != Z_OK)
				break;

			ULONG cbWrite = sizeof(zBufOut) - zstm.avail_out;
			hResult = pOutput->Write(zBufOut, cbWrite, &cbWrite );
			if ( hResult != S_OK || cbWrite == 0 )
				break;

			if ( zstm.avail_out != 0 )
				break;
		}
	}

	err = deflateEnd(&zstm);
}
Example #23
0
/*
    compressString(data: String): String
 */
static EjsString *zlib_compressString(Ejs *ejs, EjsObj *unused, int argc, EjsObj **argv)
{
    EjsString       *in;
    MprBuf          *out;
    z_stream        zs;
    uchar           outbuf[ZBUFSIZE];
    ssize           size, nbytes;
    int             level, flush;

    in = (EjsString*) argv[0];
    if ((size = in->length) == 0) {
        return ESV(empty);
    }
    if ((out = mprCreateBuf(in->length, 0)) == 0) {
        return 0;
    }
    zs.zalloc = Z_NULL;
    zs.zfree = Z_NULL;
    zs.opaque = Z_NULL;
    level = Z_DEFAULT_COMPRESSION;
    deflateInit(&zs, level);
    zs.next_in = (uchar*) in->value;
    zs.avail_in = (int) size;
    do {
        flush = (zs.avail_in == 0) ? Z_FINISH : Z_NO_FLUSH;
        do {
            zs.avail_out = ZBUFSIZE;
            zs.next_out = outbuf;
            deflate(&zs, flush);
            nbytes = ZBUFSIZE - zs.avail_out;
            if (mprPutBlockToBuf(out, (char*) outbuf, nbytes) != nbytes) {
                ejsThrowIOError(ejs, "Cannot copy to output buffer");
                deflateEnd(&zs);
                return 0;
            }
        } while (zs.avail_out == 0);
        assure(zs.avail_in == 0);
    } while (flush != Z_FINISH);

    deflateEnd(&zs);
    return ejsCreateStringFromBytes(ejs, mprGetBufStart(out), mprGetBufLength(out));
}
Example #24
0
/*************************************************************************
 *
 * NAME  packfile()
 *
 * SYNOPSIS
 *   ret = packfile (fin, len, fout)
 *   int packfile (FILE *, int, FILE *)
 *
 * DESCRIPTION
 *   Pack 'len' bytes from fin to fout.
 *
 * INPUTS
 *   fin                    - input file
 *   len                    - number of bytes to input (0 means until EOF)
 *   fout                   - output file
 *           
 * RESULT    
 *   ret                    - status (0=OK)
 *           
 * KNOWN BUGS
 *   len==0 does not produce the correct result
 *           
 ******/     
int packfile(FILE *fin, int len, FILE *fout)
{
    z_stream zstrm;
    char input_buffer[BUFFER_SIZE];
    char output_buffer[BUFFER_SIZE];
    int count,status;

    zstrm.zalloc=Z_NULL;
    zstrm.zfree=Z_NULL;
    zstrm.opaque=Z_NULL;

    status=deflateInit(&zstrm,Z_DEFAULT_COMPRESSION);
    if (status!=Z_OK)
	return status;

    zstrm.avail_in = 0;
    zstrm.next_out = output_buffer;
    zstrm.avail_out = BUFFER_SIZE;
    while (!(ferror(fin) || ferror(fout))) {
        if ( zstrm.avail_in == 0 ) {
            zstrm.next_in = input_buffer;
            zstrm.avail_in = fread( input_buffer, 1, BUFFER_SIZE, fin );
        }
        if ( zstrm.avail_in != 0 ) {
	    status = deflate( &zstrm, Z_NO_FLUSH );
	} else {
	    status = deflate( &zstrm, Z_FINISH );
	}
        count = BUFFER_SIZE - zstrm.avail_out;
        if ( count )
            fwrite( output_buffer, 1, count, fout );
        zstrm.next_out = output_buffer;
        zstrm.avail_out = BUFFER_SIZE;
	if (status!=Z_OK)
	    break;
    }    
    deflateEnd(&zstrm);

    if (status==Z_STREAM_END)
	return Z_OK;
    return status;
}
Example #25
0
Error compressString(const std::string& toCompress, std::vector<unsigned char>* compressedData)
{
   if (toCompress.empty())
   {
      *compressedData = std::vector<unsigned char>();
      return Success();
   }

   size_t dataLen = toCompress.size();
   ByteBuffer srcBuff(dataLen);
   ByteBuffer destBuff(dataLen);

   srcBuff.append(toCompress);

   z_stream zStream;
   size_t remainIn, remainOut;
   makeZStream(srcBuff, destBuff, &remainIn, &remainOut, &zStream);

   int res = deflateInit(&zStream, Z_BEST_COMPRESSION);
   if (res != Z_OK)
      return systemError(res, "ZLib initialization error", ERROR_LOCATION);

   while (res != Z_STREAM_END)
   {
      updateOut(&destBuff, &remainOut, &zStream);
      res = deflate(&zStream, Z_FINISH);
      destBuff.setDataSize(zStream.total_out);
      if (res == Z_STREAM_ERROR)
      {
         deflateEnd(&zStream);
         LOG_ERROR_MESSAGE("Could not compress string \"" +
                           toCompress + "\"");
         return systemError(res, "ZLib deflation error", ERROR_LOCATION);
      }
      updateIn(&srcBuff, &remainIn, &zStream);
   }

   deflateEnd(&zStream);

   compressedData->assign(destBuff.get(), destBuff.get() + destBuff.dataSize());
   return Success();
}
void Flate::Deflate(CFile* pOutput, CFile* pInput)
{
	z_stream zstm;
	memset(&zstm,0,sizeof(z_stream));

	BYTE zBufIn[UNCOMP_BUFSIZE];
	BYTE zBufOut[COMP_BUFSIZE];

	deflateInit(&zstm, Z_DEFAULT_COMPRESSION);

	int err = Z_OK;
	while ( TRUE )
	{
		UINT cbRead = 0;
		cbRead = pInput->Read(zBufIn, sizeof(zBufIn));
		if ( cbRead == 0 )
			break;

		zstm.next_in = (Bytef*)zBufIn;
		zstm.avail_in = (uInt)cbRead;

		while ( TRUE )
		{
			zstm.next_out = (Bytef*)zBufOut;
			zstm.avail_out = sizeof(zBufOut);

			err = deflate(&zstm, Z_FINISH);
			if (err != Z_OK)
				break;

			UINT cbWrite = sizeof(zBufOut) - zstm.avail_out;
			if ( cbWrite == 0 )
				break;
			pOutput->Write(zBufOut, cbWrite);

			if ( zstm.avail_out != 0 )
				break;
		}
	}

	err = deflateEnd(&zstm);
}
Example #27
0
static int
ZIPSetupEncode(TIFF* tif)
{
	ZIPState* sp = EncoderState(tif);
	static const char module[] = "ZIPSetupEncode";

	assert(sp != NULL);
	if (sp->state & ZSTATE_INIT_DECODE) {
            inflateEnd(&sp->stream);
            sp->state = 0;
        }

	if (deflateInit(&sp->stream, sp->zipquality) != Z_OK) {
		TIFFErrorExt(tif->tif_clientdata, module, "%s: %s", tif->tif_name, sp->stream.msg);
		return (0);
	} else {
		sp->state |= ZSTATE_INIT_ENCODE;
		return (1);
	}
}
Example #28
0
BOOL Socket::InitCompression()
{
    int ret = 0;

    zstream.zalloc = NULL;
    zstream.zfree = NULL;
    zstream.opaque = NULL;
    zstream.next_in = NULL;
    zstream.avail_in = 0;
    zstream.next_out = cbuff;
    zstream.avail_out = 4096;
    ret = deflateInit(&zstream, 9);
    if (ret != Z_OK)
        {
            return false;
        }
    cbuff = new unsigned char[MCCP_BUFFER_SIZE];
    _compressing = true;
    return true;
}
Example #29
0
int initialize_ozstream(struct ozstream *self, struct ostream *ostream,
			void *buf, unsigned long int len)
{
	static const struct ostream_ops ops = {
		.write = write_ozstream,
		.flush = flush_ozstream,
	};
	self->ostream = ostream;
	self->buf = buf;
	self->len = len;
	self->zstream.next_out = self->buf;
	self->zstream.avail_out = self->len;
	self->zstream.zalloc = Z_NULL;
	self->zstream.zfree = Z_NULL;
	self->zstream.opaque = Z_NULL;
	if (deflateInit(&self->zstream, Z_DEFAULT_COMPRESSION) != Z_OK)
		return -1;
	self->up.ops = &ops;
	return 0;
}
Example #30
0
static z_stream *initcompress(ssh_session session, int level) {
    z_stream *stream = NULL;
    int status;

    stream = malloc(sizeof(z_stream));
    if (stream == NULL) {
        return NULL;
    }
    memset(stream, 0, sizeof(z_stream));

    status = deflateInit(stream, level);
    if (status != Z_OK) {
        SAFE_FREE(stream);
        ssh_set_error(session, SSH_FATAL,
                      "status %d inititalising zlib deflate", status);
        return NULL;
    }

    return stream;
}