Esempio n. 1
0
void CDlg_Compress::OnBnClickedBtnCompress()
{
    // 因为压缩函数的输出缓冲必须比输入大0.1% + 12 然后一个DWORD用来保存压缩前的大小
    // int compress (Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen);

    CString strFileName;
    CString strCompress;
    GetDlgItem(IDC_COMPRESS_EDIT_FILENAME)->GetWindowText(strFileName);
    GetDlgItem(IDC_COMPRESS_EDIT_CONTENT)->GetWindowText(strCompress);

    int inLen = strCompress.GetLength();
    unsigned char *out = new unsigned char[inLen*2];
    unsigned long outLen = inLen*2;

    int ret = compress(out, &outLen, (LPBYTE)(LPCSTR)strCompress, inLen);
    if (0 == ret)
    {
        CFile File;
        if (File.Open(strFileName, CFile::modeCreate | CFile::modeReadWrite))
        {
            File.Write(out, outLen);
            File.Close();
        }
        MessageBox("Compress OK!");
    }
    else
    {
        strCompress.Format("Error:%s", zError(ret));
        MessageBox(strCompress);
    }

    delete[] out; 
}
Esempio n. 2
0
int DeflatingStreamBuf::readFromDevice(char* buffer, std::streamsize length)
{
	if (!_pIstr) return 0;
	if (_zstr.avail_in == 0 && !_eof)
	{
		int n = 0;
		if (_pIstr->good())
		{
			_pIstr->read(_buffer, DEFLATE_BUFFER_SIZE);
			n = static_cast<int>(_pIstr->gcount());
		}
		if (n > 0)
		{
			_zstr.next_in  = (unsigned char*) _buffer;
			_zstr.avail_in = n;
		}
		else
		{
			_zstr.next_in  = 0;
			_zstr.avail_in = 0;
			_eof = true;
		}
	}
	_zstr.next_out  = (unsigned char*) buffer;
	_zstr.avail_out = static_cast<unsigned>(length);
	for (;;)
	{
		int rc = deflate(&_zstr, _eof ? Z_FINISH : Z_NO_FLUSH);
		if (_eof && rc == Z_STREAM_END) 
		{
			_pIstr = 0;
			return static_cast<int>(length) - _zstr.avail_out;
		}
		if (rc != Z_OK) throw IOException(zError(rc)); 
		if (_zstr.avail_out == 0)
		{
			return static_cast<int>(length);
		}
		if (_zstr.avail_in == 0)
		{
			int n = 0;
			if (_pIstr->good())
			{
				_pIstr->read(_buffer, DEFLATE_BUFFER_SIZE);
				n = static_cast<int>(_pIstr->gcount());
			}
			if (n > 0)
			{
				_zstr.next_in  = (unsigned char*) _buffer;
				_zstr.avail_in = n;
			}
			else
			{
				_zstr.next_in  = 0;
				_zstr.avail_in = 0;
				_eof = true;
			}
		}
	}
}
Esempio n. 3
0
File: zlib.c Progetto: DQNEO/minigit
void DeflateInit(z_stream *stream) {
  // メモリの確保・解放は zlib に任せます.
  stream->zalloc = Z_NULL;
  stream->zfree = Z_NULL;
  stream->opaque = Z_NULL;

  // deflateInit() では zlib 形式になります.deflateInit2() の第 3 引数を
  // 24 以上 31 以下の値にすると gzip 形式になります.deflateInit2() で
  // zlib 形式の圧縮をする場合は,第 3 引数を 8 以上 15 以下にします.
  // deflateInit() の動作は 15 のときと同じです.
  int ret;
  switch (output_format) {
    case GZIP_FORMAT: {
      ret = deflateInit2(stream, compression_level,
          Z_DEFLATED, 31, 8, Z_DEFAULT_STRATEGY);
      break;
    }
    case ZLIB_FORMAT: {
      ret = deflateInit(stream, compression_level);
      break;
    }
    default: {
      ERROR("invalid format: %d", output_format);
    }
  }

  if (ret != Z_OK) {
    // deflateInit(), deflateInit2() はエラーが起きても .msg を更新しません.
    // エラーメッセージの取得には zError() を利用することになります.
    ERROR("%s", zError(ret));
  }
}
Esempio n. 4
0
static size_t
decompress_ctf(caddr_t cbuf, size_t cbufsz, caddr_t dbuf, size_t dbufsz)
{
	z_stream zstr;
	int rc;

	zstr.zalloc = (alloc_func)0;
	zstr.zfree = (free_func)0;
	zstr.opaque = (voidpf)0;

	zstr.next_in = (Bytef *)cbuf;
	zstr.avail_in = cbufsz;
	zstr.next_out = (Bytef *)dbuf;
	zstr.avail_out = dbufsz;

	if ((rc = inflateInit(&zstr)) != Z_OK ||
	    (rc = inflate(&zstr, Z_NO_FLUSH)) != Z_STREAM_END ||
	    (rc = inflateEnd(&zstr)) != Z_OK) {
		warning("CTF decompress zlib error %s\n", zError(rc));
		return (0);
	}

	debug(3, "reflated %lu bytes to %lu, pointer at %d\n",
	    zstr.total_in, zstr.total_out, (caddr_t)zstr.next_in - cbuf);

	return (zstr.total_out);
}
Esempio n. 5
0
InflatingStreamBuf::InflatingStreamBuf(std::istream& istr, StreamType type): 
	BufferedStreamBuf(STREAM_BUFFER_SIZE, std::ios::in),
	_pIstr(&istr),
	_pOstr(0),
	_eof(false),
	_check(type != STREAM_ZIP)
{
	_zstr.next_in   = 0;
	_zstr.avail_in  = 0;
	_zstr.total_in  = 0;
	_zstr.next_out  = 0;
	_zstr.avail_out = 0;
	_zstr.total_out = 0;
	_zstr.msg       = 0;
	_zstr.state     = 0;
	_zstr.zalloc    = Z_NULL;
	_zstr.zfree     = Z_NULL;
	_zstr.opaque    = Z_NULL;
	_zstr.data_type = 0;
	_zstr.adler     = 0;
	_zstr.reserved  = 0;

	_buffer = new char[INFLATE_BUFFER_SIZE];

	int rc = inflateInit2(&_zstr, 15 + (type == STREAM_GZIP ? 16 : 0));
	if (rc != Z_OK) 
	{
		delete [] _buffer;
		throw IOException(zError(rc)); 
	}
}
Esempio n. 6
0
void cmd_start_zip_out(struct ctrl_command *cmd)
{
#ifdef HAVE_LIBZ
  int ret;

  if (out_state.zip)
    send_error("can't start compression - already started!");

  out_state.zip_state.z_stream.total_in = 0;
  out_state.zip_state.z_stream.total_out = 0;
  out_state.zip_state.z_stream.zalloc = (alloc_func)0;
  out_state.zip_state.z_stream.zfree = (free_func)0;
  out_state.zip_state.z_stream.data_type = Z_ASCII;

  if (out_state.zip_state.level <= 0)
    out_state.zip_state.level = Z_DEFAULT_COMPRESSION;

  if ((ret = deflateInit(&out_state.zip_state.z_stream,
                         out_state.zip_state.level)) != Z_OK)
    send_error("deflateInit failed: %s", zError(ret));

  out_state.zip = 1;
#else
  send_error("can't start compression - no libz support!");
#endif
}
int
zip_error_to_str(char *buf, size_t len, int ze, int se)
{
    const char *zs, *ss;

    if (ze < 0 || ze >= _zip_nerr_str)
	return snprintf(buf, len, "Unknown error %d", ze);

    zs = _zip_err_str[ze];
	
    switch (_zip_err_type[ze]) {
    case ZIP_ET_SYS:
	ss = strerror(se);
	break;
	
    case ZIP_ET_ZLIB:
	ss = zError(se);
	break;
	
    default:
	ss = NULL;
    }

    return snprintf(buf, len, "%s%s%s",
		    zs, (ss ? ": " : ""), (ss ? ss : ""));
}
Esempio n. 8
0
	void DeflateStream::CompressBuffer() {
		SPADES_MARK_FUNCTION();
		SPAssert(mode == CompressModeCompress);
		char outputBuffer[chunkSize];
		
		if(!valid){
			SPRaise("State is invalid");
		}
		
		zstream.avail_in = (unsigned int) buffer.size();
		zstream.next_in = (Bytef*)buffer.data();
		
		do{
			zstream.avail_out = chunkSize;
			zstream.next_out = (Bytef*)outputBuffer;
			int ret = deflate(&zstream, Z_NO_FLUSH);
			if(ret == Z_STREAM_ERROR) {
				valid = false;
				deflateEnd(&zstream);
				SPRaise("Error while deflating: %s",
						zError(ret));
			}
			
			int got = chunkSize - zstream.avail_out;
			baseStream->Write(outputBuffer, got);
		}while(zstream.avail_out == 0);
		
		SPAssert(zstream.avail_in == 0);
		std::vector<char>().swap(buffer);
	}
Esempio n. 9
0
	DeflateStream::DeflateStream(IStream *stream,
								 CompressMode mode,
								 bool ac) {
		SPADES_MARK_FUNCTION();
		
		this->baseStream = stream;
		this->mode = mode;
		autoClose = ac;
		
		zstream.zalloc = Z_NULL;
		zstream.zfree = Z_NULL;
		zstream.opaque = Z_NULL;
		
		position = 0;
		
		int ret;
		if(mode == CompressModeCompress) {
			ret = deflateInit(&zstream, 5);
		}else if(mode == CompressModeDecompress){
			ret = inflateInit(&zstream);
		}else{
			SPInvalidEnum("mode", mode);
		}
		
		if(ret != Z_OK){
			SPRaise("Failed to initialize zlib deflator/inflator: %s",
					zError(ret));
		}
		
		valid = true;
		reachedEOF = false;
		bufferPos = 0;
		
	}
Esempio n. 10
0
/*
 * Uncompress the binary string 
 */
static char *
uncompress_string(const char *src, int size, int *uncompressed_len)
{
	Bytef * result;
	unsigned long resultlen;
	int status;
	*uncompressed_len = 0;
	
	if (src==NULL)
		return NULL;
		
	Assert(size >= sizeof(int));
		
	memcpy(uncompressed_len,src, sizeof(int));
	
	resultlen = *uncompressed_len;
	result = palloc(resultlen);
		
	status = gp_uncompress(result, &resultlen, (Bytef *)(src+sizeof(int)), size-sizeof(int));
	if (status != Z_OK)
		elog(ERROR,"Uncompress failed: %s (errno=%d compressed len %d, uncompressed %d)",
			 zError(status), status, size, *uncompressed_len);
		
	return (char *)result;
}
Esempio n. 11
0
static int gz_close (lua_State *L)
{
  gzFile *zf = tozfile(L);
  int result = gzclose(*zf);
  *zf = NULL;

  // need to emulate pushresult behavior, because gzerror
  // does not work anymore after closing stream *grrrr*

  if (result == Z_OK) {
    lua_pushboolean (L, 1);
    return 1;
  } else {
    const char* zmessage;
    if (result == Z_ERRNO) // error is a file io error
      zmessage = strerror(errno);
    else
      zmessage = zError(result);

    lua_pushnil(L);
    lua_pushfstring(L, zmessage);
    lua_pushinteger(L, result);
    lua_pushinteger(L, errno);
    return 4;
  }
}
Esempio n. 12
0
/*
 * Compress a (binary) string using zlib.
 * 
 * returns the compressed data and the size of the compressed data.
 */
static char *
compress_string(const char *src, int uncompressed_size, int *size)
{
	int level = 3;
	unsigned long compressed_size;
	int status;

	Bytef * result;

	Assert(size!=NULL);
	
	if (src == NULL)
	{
		*size = 0;
		return NULL;
	}
	
	compressed_size = gp_compressBound(uncompressed_size);  /* worst case */
	
	result = palloc(compressed_size + sizeof(int));
	memcpy(result, &uncompressed_size, sizeof(int)); 		/* save the original length */
	
	status = gp_compress2(result+sizeof(int), &compressed_size, (Bytef *)src, uncompressed_size, level);
	if (status != Z_OK)
		elog(ERROR,"Compression failed: %s (errno=%d) uncompressed len %d, compressed %d",
			 zError(status), status, uncompressed_size, (int)compressed_size);
		
	*size = compressed_size + sizeof(int);
	elog(DEBUG2,"Compressed from %d to %d ", uncompressed_size, *size);

	return (char *)result;
}
Esempio n. 13
0
File: c_zlib.c Progetto: 002301/node
static int bio_zlib_read(BIO *b, char *out, int outl)
	{
	BIO_ZLIB_CTX *ctx;
	int ret;
	z_stream *zin;
	if(!out || !outl) return 0;
	ctx = (BIO_ZLIB_CTX *)b->ptr;
	zin = &ctx->zin;
	BIO_clear_retry_flags(b);
	if(!ctx->ibuf)
		{
		ctx->ibuf = OPENSSL_malloc(ctx->ibufsize);
		if(!ctx->ibuf)
			{
			COMPerr(COMP_F_BIO_ZLIB_READ, ERR_R_MALLOC_FAILURE);
			return 0;
			}
		inflateInit(zin);
		zin->next_in = ctx->ibuf;
		zin->avail_in = 0;
		}

	/* Copy output data directly to supplied buffer */
	zin->next_out = (unsigned char *)out;
	zin->avail_out = (unsigned int)outl;
	for(;;)
		{
		/* Decompress while data available */
		while(zin->avail_in)
			{
			ret = inflate(zin, 0);
			if((ret != Z_OK) && (ret != Z_STREAM_END))
				{
				COMPerr(COMP_F_BIO_ZLIB_READ,
						COMP_R_ZLIB_INFLATE_ERROR);
				ERR_add_error_data(2, "zlib error:",
							zError(ret));
				return 0;
				}
			/* If EOF or we've read everything then return */
			if((ret == Z_STREAM_END) || !zin->avail_out)
				return outl - zin->avail_out;
			}

		/* No data in input buffer try to read some in,
		 * if an error then return the total data read.
		 */
		ret = BIO_read(b->next_bio, ctx->ibuf, ctx->ibufsize);
		if(ret <= 0)
			{
			/* Total data read */
			int tot = outl - zin->avail_out;
			BIO_copy_next_retry(b);
			if(ret < 0) return (tot > 0) ? tot : ret;
			return tot;
			}
		zin->avail_in = ret;
		zin->next_in = ctx->ibuf;
		}
	}
Esempio n. 14
0
void CDlg_Compress::OnBnClickedBtnUncompress()
{
	CString strCompress;
    CString strFileName;
    GetDlgItem(IDC_COMPRESS_EDIT_FILENAME)->GetWindowText(strFileName);

    CFile File;
    if (File.Open(strFileName, CFile::modeRead))
    {
        ULONG inLen = File.GetLength();
		unsigned char *in = new unsigned char[inLen];
        File.Read(in, inLen);
		File.Close();

        unsigned long outLen = 1000 * inLen;
        unsigned char *out = new BYTE[outLen];

        int ret = uncompress(out, &outLen, in, inLen);
        if (0 == ret)
        {
            out[outLen] = 0;
            GetDlgItem(IDC_COMPRESS_EDIT_CONTENT)->SetWindowText((LPCTSTR)out);
            MessageBox("Uncompress OK!");
        }
        else
        {
            strCompress.Format("Error:%s\n", zError(ret));
            TRACE(strCompress);
            MessageBox("Uncompress don't OK!");
        }

		delete[] in;
        delete[] out;
    }
}
Esempio n. 15
0
/**
Compresses a source buffer into a target buffer, using the ZLib library. 
On success, the target buffer contains a GZIP compatible layout.
Upon entry, target_size is the total size of the destination buffer, 
which must be at least 0.1% larger than source_size plus 24 bytes. 

@param target Destination buffer
@param target_size Size of the destination buffer, in bytes
@param source Source buffer
@param source_size Size of the source buffer, in bytes
@return Returns the actual size of the compressed buffer, returns 0 if an error occured
@see FreeImage_ZLibCompress
*/
DWORD DLL_CALLCONV 
FreeImage_ZLibGZip(BYTE *target, DWORD target_size, BYTE *source, DWORD source_size) {
	DWORD dest_len = target_size - 12;
    DWORD crc = crc32(0L, NULL, 0);

    // set up header (stolen from zlib/gzio.c)
    sprintf((char *)target, "%c%c%c%c%c%c%c%c", 0x1f, 0x8b,
         Z_DEFLATED, 0 /*flags*/, 0,0,0,0 /*time*/);
    int zerr = compress2(target + 8, &dest_len, source, source_size, Z_BEST_COMPRESSION);
	switch(zerr) {
		case Z_MEM_ERROR:	// not enough memory
		case Z_BUF_ERROR:	// not enough room in the output buffer
			FreeImage_OutputMessageProc(FIF_UNKNOWN, "Zlib error : %s", zError(zerr));
			return 0;
        case Z_OK: {
            // patch header, setup crc and length (stolen from mod_trace_output)
            BYTE *p = target + 8; *p++ = 2; *p = OS_CODE; // xflags, os_code
 	        crc = crc32(crc, source, source_size);
	        memcpy(target + 4 + dest_len, &crc, 4);
	        memcpy(target + 8 + dest_len, &source_size, 4);
            return dest_len + 12;
        }
	}
	return 0;
}
Esempio n. 16
0
/* {{{ php_zlib_encode() */
static zend_string *php_zlib_encode(const char *in_buf, size_t in_len, int encoding, int level)
{
	int status;
	z_stream Z;
	zend_string *out;

	memset(&Z, 0, sizeof(z_stream));
	Z.zalloc = php_zlib_alloc;
	Z.zfree = php_zlib_free;

	if (Z_OK == (status = deflateInit2(&Z, level, Z_DEFLATED, encoding, MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY))) {
		out = zend_string_alloc(PHP_ZLIB_BUFFER_SIZE_GUESS(in_len), 0);

		Z.next_in = (Bytef *) in_buf;
		Z.next_out = (Bytef *) out->val;
		Z.avail_in = in_len;
		Z.avail_out = out->len;

		status = deflate(&Z, Z_FINISH);
		deflateEnd(&Z);

		if (Z_STREAM_END == status) {
			/* size buffer down to actual length */
			out = zend_string_truncate(out, Z.total_out, 0);
			out->val[out->len] = '\0';
			return out;
		} else {
			zend_string_free(out);
		}
	}

	php_error_docref(NULL, E_WARNING, "%s", zError(status));
	return NULL;
}
Esempio n. 17
0
DWORD DLL_CALLCONV 
FreeImage_ZLibGUnzip(BYTE *target, DWORD target_size, BYTE *source, DWORD source_size) {
    DWORD src_len  = source_size;
    DWORD dest_len = target_size;
    int   zerr     = Z_DATA_ERROR;

    if (src_len > 0) {
        z_stream stream;
        memset(&stream, 0, sizeof (stream));
        if ((zerr = inflateInit2(&stream, -MAX_WBITS)) == Z_OK) {
            stream.next_in  = source;
            stream.avail_in = source_size;

            stream.next_out  = target;
            stream.avail_out = target_size;

            if ((zerr = checkheader(&stream)) == Z_OK) {
                zerr = inflate (&stream, Z_NO_FLUSH);
                dest_len = target_size - stream.avail_out;

                if (zerr == Z_OK || zerr == Z_STREAM_END)
                    inflateEnd(&stream);
            } 
        }
    }
    if (zerr != Z_OK && zerr != Z_STREAM_END) {
        FreeImage_OutputMessageProc(FIF_UNKNOWN, "Zlib error : %s", zError(zerr));
        return 0;
    }
    return dest_len;
}
Esempio n. 18
0
static ssize_t gzip_transform(codec_t *codec, char *dst, size_t *dcount, 
        const char *src, size_t src_sz)
{
    codec_gzip_t *iz;
    size_t consumed;
 
    dbg_return_if (codec == NULL, -1);
    dbg_return_if (src == NULL, -1);
    dbg_return_if (dst == NULL, -1); 
    dbg_return_if (dcount == NULL || *dcount == 0, -1);
    dbg_return_if (src_sz == 0, -1);

    iz = (codec_gzip_t*)codec;
    
    iz->zstr.next_out = dst;
    iz->zstr.avail_out = *dcount;

    iz->zstr.next_in = (char*)src;
    iz->zstr.avail_in = src_sz;

    iz->err = iz->op(&iz->zstr, Z_NO_FLUSH);
    dbg_err_if(iz->err != Z_OK && iz->err != Z_STREAM_END);

    consumed = src_sz - iz->zstr.avail_in;  /* consumed */
    *dcount = *dcount - iz->zstr.avail_out; /* written */

    return consumed; /* # of consumed input bytes */
err:
    u_dbg("%s", zError(iz->err));
    return -1;
}
Esempio n. 19
0
	void DeflateStream::DeflateEnd() {
		SPADES_MARK_FUNCTION();
		if(mode != CompressModeCompress){
			SPRaise("DeflareEnd called when decompressing");
		}
		if(!valid){
			SPRaise("State is invalid");
		}
		
		char outputBuffer[chunkSize];
		
		zstream.avail_in = 0;
		do{
			zstream.avail_out = chunkSize;
			zstream.next_out = (Bytef*)outputBuffer;
			int ret = deflate(&zstream, Z_FINISH);
			if(ret == Z_STREAM_ERROR) {
				valid = false;
				deflateEnd(&zstream);
				SPRaise("Error while deflating: %s",
						zError(ret));
			}
			
			int got = chunkSize - zstream.avail_out;
			baseStream->Write(outputBuffer, got);
		}while(zstream.avail_out == 0);
		
		deflateEnd(&zstream);
		valid = false;
	}
Esempio n. 20
0
void WebSocketPerMessageCompressionFilter::receiveFilter(WebSocketFrame& frame)
{
    // https://tools.ietf.org/html/rfc7692#section-7.2.2

    frame.writeBytes(DEFLATE_BYTE_BLOCK, DEFLATE_BYTE_BLOCK_SIZE);

    IO::ByteBuffer uncompressed;

    int rc = Z_OK;

    _inflateState.avail_in = frame.size();
    _inflateState.next_in = frame.getPtr();

    do
    {
        _inflateState.avail_out = BUFFER_SIZE;
        _inflateState.next_out = _buffer;

        rc = inflate(&_inflateState, Z_SYNC_FLUSH);

        if (rc != Z_OK)
        {
            throw Poco::IOException("Inflate: " + std::string(zError(rc)));
        }

        uncompressed.writeBytes(_buffer, BUFFER_SIZE - _inflateState.avail_out);

    }
    while (_inflateState.avail_out == 0);

    frame.clear();
    frame.writeBytes(uncompressed);
    frame.setRSV1(false);
}
Esempio n. 21
0
File: zlib.c Progetto: DQNEO/minigit
void InflateEnd(z_stream *stream) {
  int ret = inflateEnd(stream);
  if (ret != Z_OK) {
    // deflateEnd() はエラーが起きても .msg を更新しません.
    // エラーメッセージの取得には zError() を利用することになります.
    ERROR("%s", zError(ret));
  }
}
void InflatingStreamBuf::reset()
{
    int rc = inflateReset(&_zstr);
    if (rc == Z_OK)
        _eof = false;
    else
        throw IOException(zError(rc));
}
Esempio n. 23
0
/** @brief Inflates the data's tag
 *
 * buf must hold at least 8 bytes
 * @ingroup mat_internal
 * @param mat Pointer to the MAT file
 * @param matvar Pointer to the MAT variable
 * @param buf Pointer to store the data tag
 * @return Number of bytes read from the file
 */
size_t
InflateDataTag(mat_t *mat, matvar_t *matvar, void *buf)
{
    mat_uint8_t comp_buf[32];
    int    err;
    size_t bytesread = 0;

    if ( buf == NULL )
        return 0;

   if ( !matvar->internal->z->avail_in ) {
        matvar->internal->z->avail_in = 1;
        matvar->internal->z->next_in = comp_buf;
        bytesread += fread(comp_buf,1,1,(FILE*)mat->fp);
    }
    matvar->internal->z->avail_out = 8;
    matvar->internal->z->next_out = (Bytef*)buf;
    err = inflate(matvar->internal->z,Z_NO_FLUSH);
    if ( err == Z_STREAM_END ) {
        return bytesread;
    } else if ( err != Z_OK ) {
        Mat_Critical("InflateDataTag: %s - inflate returned %s",matvar->name,zError(err == Z_NEED_DICT ? Z_DATA_ERROR : err));
        return bytesread;
    }
    while ( matvar->internal->z->avail_out && !matvar->internal->z->avail_in ) {
        matvar->internal->z->avail_in = 1;
        matvar->internal->z->next_in = comp_buf;
        bytesread += fread(comp_buf,1,1,(FILE*)mat->fp);
        err = inflate(matvar->internal->z,Z_NO_FLUSH);
        if ( err == Z_STREAM_END ) {
            break;
        } else if ( err != Z_OK ) {
            Mat_Critical("InflateDataTag: %s - inflate returned %s",matvar->name,zError(err == Z_NEED_DICT ? Z_DATA_ERROR : err));
            return bytesread;
        }
    }

    if ( matvar->internal->z->avail_in ) {
        (void)fseek((FILE*)mat->fp,-(int)matvar->internal->z->avail_in,SEEK_CUR);
        bytesread -= matvar->internal->z->avail_in;
        matvar->internal->z->avail_in = 0;
    }

    return bytesread;
}
Esempio n. 24
0
/* push a bytes into the state tracker */
void telnet_recv(telnet_t *telnet, const char *buffer,
		size_t size) {
#if defined(HAVE_ZLIB)
	/* if we have an inflate (decompression) zlib stream, use it */
	if (telnet->z != 0 && !(telnet->flags & TELNET_PFLAG_DEFLATE)) {
		char inflate_buffer[1024];
		int rs;

		/* initialize zlib state */
		telnet->z->next_in = (unsigned char*)buffer;
		telnet->z->avail_in = (unsigned int)size;
		telnet->z->next_out = (unsigned char *)inflate_buffer;
		telnet->z->avail_out = sizeof(inflate_buffer);

		/* inflate until buffer exhausted and all output is produced */
		while (telnet->z->avail_in > 0 || telnet->z->avail_out == 0) {
			/* reset output buffer */

			/* decompress */
			rs = inflate(telnet->z, Z_SYNC_FLUSH);

			/* process the decompressed bytes on success */
			if (rs == Z_OK || rs == Z_STREAM_END)
				_process(telnet, inflate_buffer, sizeof(inflate_buffer) -
						telnet->z->avail_out);
			else
				_error(telnet, __LINE__, __func__, TELNET_ECOMPRESS, 1,
						"inflate() failed: %s", zError(rs));

			/* prepare output buffer for next run */
			telnet->z->next_out = (unsigned char *)inflate_buffer;
			telnet->z->avail_out = sizeof(inflate_buffer);

			/* on error (or on end of stream) disable further inflation */
			if (rs != Z_OK) {
				telnet_event_t ev;

				/* disable compression */
				inflateEnd(telnet->z);
				free(telnet->z);
				telnet->z = 0;

				/* send event */
				ev.type = TELNET_EV_COMPRESS;
				ev.compress.state = 0;
				telnet->eh(telnet, &ev, telnet->ud);

				break;
			}
		}

	/* COMPRESS2 is not negotiated, just process */
	} else
#endif /* defined(HAVE_ZLIB) */
		_process(telnet, buffer, size);
}
Esempio n. 25
0
double StraightRodPair::computeNextZ(double newDsLength, double newDsDistance, double lastDsDistance, double lastZ, BuildDir direction, int parity) {
  double d = smallDelta();
  double dz = zError();
  double ov = zOverlap();
  double maxr = maxBuildRadius();
  double minr = minBuildRadius();
 
  double newR = (parity > 0 ? maxr + d : minr - d) - newDsDistance/2;
  double lastR = (parity > 0 ? maxr - d : minr + d) + lastDsDistance/2;

  double newZ = lastZ;
  if (!beamSpotCover()) dz = 0;
  if (direction == BuildDir::RIGHT) {
    double originZ = parity > 0 ? dz : -dz;
    double newZorigin = (newZ - ov) * newR/lastR;
    double newZshifted = (newZ - originZ) * newR/lastR + originZ;
    if (beamSpotCover()) newZ = MIN(newZorigin, newZshifted);
    else newZ = newZorigin;
    if (forbiddenRange.state()) {
      double forbiddenRange_begin,forbiddenRange_end; 
      forbiddenRange_begin=(forbiddenRange[0]+forbiddenRange[1])/2;
      forbiddenRange_end=forbiddenRange[1];
      if (newZ-lastZ >= (forbiddenRange_begin - newDsLength) && newZ - lastZ <= (forbiddenRange_end - newDsLength)) {
        newZ = lastZ + forbiddenRange_begin - newDsLength;
      } else {
         forbiddenRange_begin=forbiddenRange[0];
         forbiddenRange_end=(forbiddenRange[0]+forbiddenRange[1])/2;
	 if (newZ-lastZ >= (forbiddenRange_begin - newDsLength) && newZ - lastZ <= (forbiddenRange_end - newDsLength)) {
            newZ = lastZ + forbiddenRange_begin - newDsLength;
	 }
      }
    }
  } 
  else {
    double originZ = parity > 0 ? -dz : dz;
    double newZorigin = (newZ + ov) * newR/lastR;
    double newZshifted = (newZ - originZ) * newR/lastR + originZ;
    if (beamSpotCover()) newZ = MAX(newZorigin, newZshifted);
    else newZ = newZorigin;
    if (forbiddenRange.state()) {
      double forbiddenRange_begin,forbiddenRange_end;              
      forbiddenRange_begin=(forbiddenRange[0]+forbiddenRange[1])/2;
      forbiddenRange_end=forbiddenRange[1];
      if (lastZ - newZ >= (forbiddenRange_begin - newDsLength) && lastZ - newZ <= (forbiddenRange_end - newDsLength)){
        newZ = lastZ - forbiddenRange_begin + newDsLength;
      } else {
        forbiddenRange_begin=forbiddenRange[0];
        forbiddenRange_end=(forbiddenRange[0]+forbiddenRange[1])/2;
        if (lastZ - newZ >= (forbiddenRange_begin - newDsLength) && lastZ - newZ <= (forbiddenRange_end - newDsLength)){
          newZ = lastZ - forbiddenRange_begin + newDsLength;
        }
      }
    }
  }
  return newZ;
}
Esempio n. 26
0
static void
compress_gzip(int fd_in, int fd_out, struct compress_params *params, const char *desc)
{
	char buffer[DPKG_BUFFER_SIZE];
	char combuf[6];
	int strategy;
	int z_errnum;
	gzFile gzfile;

	if (params->strategy == COMPRESSOR_STRATEGY_FILTERED)
		strategy = 'f';
	else if (params->strategy == COMPRESSOR_STRATEGY_HUFFMAN)
		strategy = 'h';
	else if (params->strategy == COMPRESSOR_STRATEGY_RLE)
		strategy = 'R';
	else if (params->strategy == COMPRESSOR_STRATEGY_FIXED)
		strategy = 'F';
	else
		strategy = ' ';

	snprintf(combuf, sizeof(combuf), "w%d%c", params->level, strategy);
	gzfile = gzdopen(fd_out, combuf);
	if (gzfile == NULL)
		ohshit(_("%s: error binding output to gzip stream"), desc);

	for (;;) {
		int actualread, actualwrite;

		actualread = fd_read(fd_in, buffer, sizeof(buffer));
		if (actualread < 0)
			ohshite(_("%s: internal gzip read error"), desc);
		if (actualread == 0) /* EOF. */
			break;

		actualwrite = gzwrite(gzfile, buffer, actualread);
		if (actualwrite != actualread) {
			const char *errmsg = gzerror(gzfile, &z_errnum);

			if (z_errnum == Z_ERRNO)
				errmsg = strerror(errno);
			ohshit(_("%s: internal gzip write error: '%s'"), desc,
			       errmsg);
		}
	}

	z_errnum = gzclose(gzfile);
	if (z_errnum) {
		const char *errmsg;

		if (z_errnum == Z_ERRNO)
			errmsg = strerror(errno);
		else
			errmsg = zError(z_errnum);
		ohshit(_("%s: internal gzip write error: %s"), desc, errmsg);
	}
}
Esempio n. 27
0
int err_gzclose(gzFile file)
{
	int ret = gzclose(file);
	if (Z_OK != ret)
	{
		_err_fatal_simple("gzclose", Z_ERRNO == ret ? strerror(errno) : zError(ret));
	}

	return ret;
}
Esempio n. 28
0
File: ctf.c Progetto: DataIX/src
static void
compress_end(resbuf_t *rb)
{
	int rc;

	compress_flush(rb, Z_FINISH);

	if ((rc = deflateEnd(&rb->rb_zstr)) != Z_OK)
		parseterminate("zlib end failed: %s", zError(rc));
}
Esempio n. 29
0
/*
 * Convert a zlib error code into a string error message.
 */
const char *
z_strerror(int err)
{
	int i = Z_NEED_DICT - err;

	if (i < 0 || i > Z_NEED_DICT - Z_VERSION_ERROR)
		return ("unknown error");

	return (zError(err));
}
Esempio n. 30
0
/*++

ZlipSetError

    Sets the interpreter's result to a human-readable error message.

Arguments:
    interp  - Interpreter to use for error reporting.

    message - Message describing the failed operation.

    status  - Zlib status code.

Return Value:
    None.

--*/
static void
ZlipSetError(
    Tcl_Interp *interp,
    const char *message,
    int status
)
{
    Tcl_ResetResult(interp);
    Tcl_AppendResult(interp, message, zError(status), NULL);
}