本文整理汇总了C++中deflate函数的典型用法代码示例。如果您正苦于以下问题:C++ deflate函数的具体用法?C++ deflate怎么用?C++ deflate使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了deflate函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: destroy
//---------------------------------------------------------------------
void DeflateStream::compressFinal()
{
// Close temp stream
mTmpWriteStream->close();
// Copy & compress
// We do this rather than compress directly because some code seeks
// around while writing (e.g. to update size blocks) which is not
// possible when compressing on the fly
int ret, flush;
char in[OGRE_DEFLATE_TMP_SIZE];
char out[OGRE_DEFLATE_TMP_SIZE];
if (deflateInit(mZStream, Z_DEFAULT_COMPRESSION) != Z_OK)
{
destroy();
OGRE_EXCEPT(Exception::ERR_INVALID_STATE,
"Error initialising deflate compressed stream!",
"DeflateStream::init");
}
std::ifstream inFile;
inFile.open(mTempFileName.c_str(), std::ios::in | std::ios::binary);
do
{
inFile.read(in, OGRE_DEFLATE_TMP_SIZE);
mZStream->avail_in = (uInt)inFile.gcount();
if (inFile.bad())
{
deflateEnd(mZStream);
OGRE_EXCEPT(Exception::ERR_INVALID_STATE,
"Error reading temp uncompressed stream!",
"DeflateStream::init");
}
flush = inFile.eof() ? Z_FINISH : Z_NO_FLUSH;
mZStream->next_in = (Bytef*)in;
/* run deflate() on input until output buffer not full, finish
compression if all of source has been read in */
do
{
mZStream->avail_out = OGRE_DEFLATE_TMP_SIZE;
mZStream->next_out = (Bytef*)out;
ret = deflate(mZStream, flush); /* no bad return value */
assert(ret != Z_STREAM_ERROR); /* state not clobbered */
size_t compressed = OGRE_DEFLATE_TMP_SIZE - mZStream->avail_out;
mCompressedStream->write(out, compressed);
} while (mZStream->avail_out == 0);
assert(mZStream->avail_in == 0); /* all input will be used */
/* done when last data in file processed */
} while (flush != Z_FINISH);
assert(ret == Z_STREAM_END); /* stream will be complete */
(void)ret;
deflateEnd(mZStream);
inFile.close();
remove(mTempFileName.c_str());
}
示例2: lws_extension_callback_deflate_stream
int lws_extension_callback_deflate_stream(
struct libwebsocket_context *context,
struct libwebsocket_extension *ext,
struct libwebsocket *wsi,
enum libwebsocket_extension_callback_reasons reason,
void *user, void *in, size_t len)
{
struct lws_ext_deflate_stream_conn *conn =
(struct lws_ext_deflate_stream_conn *)user;
int n;
struct lws_tokens *eff_buf = (struct lws_tokens *)in;
switch (reason) {
/*
* for deflate-stream, both client and server sides act the same
*/
case LWS_EXT_CALLBACK_CLIENT_CONSTRUCT:
case LWS_EXT_CALLBACK_CONSTRUCT:
conn->zs_in.zalloc = conn->zs_out.zalloc = Z_NULL;
conn->zs_in.zfree = conn->zs_out.zfree = Z_NULL;
conn->zs_in.opaque = conn->zs_out.opaque = Z_NULL;
n = inflateInit2(&conn->zs_in, -LWS_ZLIB_WINDOW_BITS);
if (n != Z_OK) {
lws_log(LWS_LOG_WARNING, "deflateInit returned %d", n);
return 1;
}
n = deflateInit2(&conn->zs_out,
DEFLATE_STREAM_COMPRESSION_LEVEL, Z_DEFLATED,
-LWS_ZLIB_WINDOW_BITS, LWS_ZLIB_MEMLEVEL,
Z_DEFAULT_STRATEGY);
if (n != Z_OK) {
lws_log(LWS_LOG_WARNING, "deflateInit returned %d", n);
return 1;
}
conn->remaining_in = 0;
lws_log(LWS_LOG_DEBUG, "zlibs constructed");
break;
case LWS_EXT_CALLBACK_DESTROY:
(void)inflateEnd(&conn->zs_in);
(void)deflateEnd(&conn->zs_out);
lws_log(LWS_LOG_DEBUG, "zlibs destructed");
break;
case LWS_EXT_CALLBACK_PACKET_RX_PREPARSE:
/*
* inflate the incoming compressed data
* Notice, length may be 0 and pointer NULL
* in the case we are flushing with nothing new coming in
*/
if (conn->remaining_in)
{
conn->zs_in.next_in = conn->buf_in;
conn->zs_in.avail_in = conn->remaining_in;
conn->remaining_in = 0;
}
else
{
conn->zs_in.next_in = (unsigned char *)eff_buf->token;
conn->zs_in.avail_in = eff_buf->token_len;
}
conn->zs_in.next_out = conn->buf_out;
conn->zs_in.avail_out = sizeof(conn->buf_out);
n = inflate(&conn->zs_in, Z_SYNC_FLUSH);
switch (n) {
case Z_NEED_DICT:
case Z_DATA_ERROR:
case Z_MEM_ERROR:
/*
* screwed.. close the connection... we will get a
* destroy callback to take care of closing nicely
*/
lws_log(LWS_LOG_ERROR, "zlib error inflate %d", n);
return -1;
}
/* rewrite the buffer pointers and length */
eff_buf->token = (char *)conn->buf_out;
eff_buf->token_len = sizeof(conn->buf_out) - conn->zs_in.avail_out;
/* copy avail data if not consumed */
if (conn->zs_in.avail_in > 0)
{
conn->remaining_in = conn->zs_in.avail_in;
memcpy(conn->buf_in, conn->zs_in.next_in, conn->zs_in.avail_in);
return 1;
}
/*
* if we filled the output buffer, signal that we likely have
* more and need to be called again
*/
if (eff_buf->token_len == sizeof(conn->buf_out))
//.........这里部分代码省略.........
示例3: pptr
/*!
*/
bool
gzfilterstreambuf::writeData( int flush_type )
{
// size of data to write
int size = ( pptr() - pbase() ) * sizeof( char_type );
if ( size == 0 )
{
return true;
}
if ( M_output_stream == NULL )
{
//std::cerr << "create stream" << std::endl;
M_output_stream = new std::ostream( &M_strmbuf );
}
#ifdef HAVE_LIBZ
if ( M_level <= NO_COMPRESSION
|| BEST_COMPRESSION < M_level )
{
M_output_stream->write( M_output_buf, size );
}
else
{
if ( M_impl->comp_stream_ == NULL )
{
M_impl->comp_stream_ = new z_stream;
M_impl->comp_stream_->zalloc = Z_NULL;
M_impl->comp_stream_->zfree = Z_NULL;
M_impl->comp_stream_->opaque = NULL;
M_impl->comp_stream_->avail_in = 0;
M_impl->comp_stream_->next_in = 0;
M_impl->comp_stream_->next_out = 0;
M_impl->comp_stream_->avail_out = 0;
if ( deflateInit( M_impl->comp_stream_, M_level ) != Z_OK )
{
return false;
}
if ( M_write_buf == NULL )
{
//std::cerr << "create write buf" << std::endl;
M_write_buf = new char[ M_buf_size ];
}
M_impl->comp_stream_->next_out = (Bytef*)M_write_buf;
M_impl->comp_stream_->avail_out = M_buf_size;
}
M_impl->comp_stream_->next_in = (Bytef*)M_output_buf;
M_impl->comp_stream_->avail_in = size;
do
{
int bytes_out = - M_impl->comp_stream_->total_out;
int err = deflate( M_impl->comp_stream_, flush_type );
if ( err != Z_OK && err != Z_STREAM_END )
{
//std::cerr << "error deflating ["
// << M_impl->comp_stream_->msg << "]"
// << std::endl;
return false;
}
bytes_out += M_impl->comp_stream_->total_out;
//std::cerr << "compress loop size = " << bytes_out << std::endl;
M_output_stream->write( M_write_buf, bytes_out );
M_impl->comp_stream_->next_out = (Bytef*)M_write_buf;
M_impl->comp_stream_->avail_out = M_buf_size;
}
while ( M_impl->comp_stream_->avail_in != 0 );
// we want to keep writing until all the data has been
// consumed by the compression stream
deflateReset( M_impl->comp_stream_ );
}
#else
M_output_stream->write( M_output_buf, size );
#endif
if ( flush_type != NO_FLUSH ) // == Z_NO_FLUSH
{
// flush the underlying stream if a flush has been used
M_output_stream->flush();
}
return true;
}
示例4: zipfile_tool
//.........这里部分代码省略.........
if ((file_loc < 0) || (file_loc > INT_MAX)) {
// This is not a limitation of miniz or tinfl, but this example.
printf("File is too large to be processed by this example.\n");
fclose(pInfile);
return EXIT_FAILURE;
}
infile_size = (uint)file_loc;
// Open output file.
pOutfile = fopen(pDst_filename, "wb");
if (!pOutfile) {
printf("Failed opening output file!\n");
fclose(pInfile);
return EXIT_FAILURE;
}
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("Input file size: %u\n", infile_size);
// Init the z_stream
memset(&stream, 0, sizeof(stream));
stream.next_in = s_inbuf;
stream.avail_in = 0;
stream.next_out = s_outbuf;
stream.avail_out = BUF_SIZE;
if ((pMode[0] == 'c') || (pMode[0] == 'C')) {
// Compression.
uint infile_remaining = infile_size;
if (deflateInit(&stream, level) != Z_OK) {
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("deflateInit() failed!\n");
if(pInfile) fclose(pInfile);
if(pOutfile) fclose(pOutfile);
return EXIT_FAILURE;
}
for ( ; ; ) {
int status;
if (!stream.avail_in) {
// Input buffer is empty, so read more bytes from input file.
uint n = my_min(BUF_SIZE, infile_remaining);
if (fread(s_inbuf, 1, n, pInfile) != n) {
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("Failed reading from input file!\n");
if(pInfile) fclose(pInfile);
if(pOutfile) fclose(pOutfile);
return EXIT_FAILURE;
}
stream.next_in = s_inbuf;
stream.avail_in = n;
infile_remaining -= n;
//printf("Input bytes remaining: %u\n", infile_remaining);
}
status = deflate(&stream, infile_remaining ? Z_NO_FLUSH : Z_FINISH);
if ((status == Z_STREAM_END) || (!stream.avail_out)) {
// Output buffer is full, or compression is done, so write buffer to output file.
示例5: while
ATCResult ATCLocker_impl::writeFileData(ostream *dst, istream *src, size_t length)
{
int rest_length = length;
while (1)
{
if (z_.avail_in == 0)
{
size_t read_length = (rest_length < ATC_BUF_SIZE) ? rest_length : ATC_BUF_SIZE;
z_.next_in = reinterpret_cast<Bytef*>(input_buffer_);
z_.avail_in = static_cast<uInt>(src->read(input_buffer_, read_length).gcount());
rest_length -= z_.avail_in;
total_write_length_ += z_.avail_in;
if (total_write_length_ >= total_length_)
{
z_flush_ = Z_FINISH;
}
}
// TODO: コメントアウトするとなぜか正常に出力しない
// if(z_.avail_in == 0) break;
z_status_ = deflate(&z_, z_flush_);
if (z_status_ == Z_STREAM_END)
{
break;
}
if (z_status_ != Z_OK)
{
if (z_status_ == Z_BUF_ERROR)
{
return ATC_OK;
} else {
return ATC_ERR_ZLIB_ERROR;
}
}
if (z_.avail_out == 0)
{
encryptBuffer(output_buffer_, chain_buffer_);
dst->write(output_buffer_, ATC_BUF_SIZE);
z_.next_out = reinterpret_cast<Bytef*>(output_buffer_);
z_.avail_out = ATC_BUF_SIZE;
}
}
if (z_status_ == Z_STREAM_END)
{
int32_t count;
if ((count = ATC_BUF_SIZE - z_.avail_out) != 0)
{
char padding_num = (char)z_.avail_out;
for(int i = count; i < ATC_BUF_SIZE; i++)
{
output_buffer_[i] = padding_num;
}
encryptBuffer(output_buffer_, chain_buffer_);
dst->write(output_buffer_, ATC_BUF_SIZE);
}
return finish();
}
return ATC_OK;
}
示例6: zipWriteInFileInZip
extern int ZEXPORT zipWriteInFileInZip (
zipFile file,
const void* buf,
unsigned len)
{
zip_internal* zi;
int err=ZIP_OK;
if (file == NULL)
return ZIP_PARAMERROR;
zi = (zip_internal*)file;
if (zi->in_opened_file_inzip == 0)
return ZIP_PARAMERROR;
zi->ci.stream.next_in = (Bytef *)buf;
zi->ci.stream.avail_in = len;
zi->ci.crc32 = crc32(zi->ci.crc32,(const Bytef *)buf,len);
while ((err==ZIP_OK) && (zi->ci.stream.avail_in>0))
{
if (zi->ci.stream.avail_out == 0)
{
if (zipFlushWriteBuffer(zi) == ZIP_ERRNO)
err = ZIP_ERRNO;
zi->ci.stream.avail_out = (uInt)Z_BUFSIZE;
zi->ci.stream.next_out = zi->ci.buffered_data;
}
if(err != ZIP_OK)
break;
if ((zi->ci.method == Z_DEFLATED) && (!zi->ci.raw))
{
uLong uTotalOutBefore = zi->ci.stream.total_out;
err=deflate(&zi->ci.stream, Z_NO_FLUSH);
zi->ci.pos_in_buffered_data += (uInt)(zi->ci.stream.total_out - uTotalOutBefore) ;
}
else
{
uInt copy_this,i;
if (zi->ci.stream.avail_in < zi->ci.stream.avail_out)
copy_this = zi->ci.stream.avail_in;
else
copy_this = zi->ci.stream.avail_out;
for (i=0;i<copy_this;i++)
*(((char*)zi->ci.stream.next_out)+i) =
*(((const char*)zi->ci.stream.next_in)+i);
{
zi->ci.stream.avail_in -= copy_this;
zi->ci.stream.avail_out-= copy_this;
zi->ci.stream.next_in+= copy_this;
zi->ci.stream.next_out+= copy_this;
zi->ci.stream.total_in+= copy_this;
zi->ci.stream.total_out+= copy_this;
zi->ci.pos_in_buffered_data += copy_this;
}
}
}
return err;
}
示例7: writer
//.........这里部分代码省略.........
}
Data<<Stats[Pos]->StatsToXML(filters);
}
}
// Footer
Data<<" </frames>";
QString streamsAndFormats;
QXmlStreamWriter writer(&streamsAndFormats);
writer.setAutoFormatting(true);
writer.setAutoFormattingIndent(4);
if(streamsStats)
streamsStats->writeToXML(&writer);
if(formatStats)
formatStats->writeToXML(&writer);
// add indentation
QStringList splitted = streamsAndFormats.split("\n");
for(size_t i = 0; i < splitted.length(); ++i)
splitted[i] = QString(qAbs(writer.autoFormattingIndent()), writer.autoFormattingIndent() > 0 ? ' ' : '\t') + splitted[i];
streamsAndFormats = splitted.join("\n");
Data<<streamsAndFormats.toStdString() << "\n\n";
Data<<"</ffprobe:ffprobe>";
SharedFile file;
QString name;
if(ExportFileName.isEmpty())
{
file = SharedFile(new QTemporaryFile());
QFileInfo info(fileName() + ".qctools.xml.gz");
name = info.fileName();
} else {
file = SharedFile(new QFile(ExportFileName));
QFileInfo info(ExportFileName);
name = info.fileName();
}
string DataS=Data.str();
uLongf Buffer_Size=65536;
if(file->open(QIODevice::ReadWrite))
{
if(name.endsWith(".qctools.xml"))
{
auto bytesLeft = Data.str().size();
auto writePtr = DataS.c_str();
auto totalBytesWritten = 0;
while(bytesLeft) {
auto bytesToWrite = std::min(size_t(Buffer_Size), bytesLeft);
auto bytesWritten = file->write(writePtr, bytesToWrite);
totalBytesWritten += bytesWritten;
Q_EMIT statsFileGenerationProgress(totalBytesWritten, DataS.size());
writePtr += bytesToWrite;
bytesLeft -= bytesWritten;
if(bytesWritten != bytesToWrite)
break;
}
} else {
char* Buffer=new char[Buffer_Size];
z_stream strm;
strm.next_in = (Bytef *) DataS.c_str();
strm.avail_in = DataS.size() ;
strm.total_out = 0;
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
if (deflateInit2(&strm, Z_DEFAULT_COMPRESSION, Z_DEFLATED, 15 + 16, 8, Z_DEFAULT_STRATEGY)>=0) // 15 + 16 are magic values for gzip
{
do
{
strm.next_out = (unsigned char*) Buffer;
strm.avail_out = Buffer_Size;
if (deflate(&strm, Z_FINISH)<0)
break;
file->write(Buffer, Buffer_Size-strm.avail_out);
Q_EMIT statsFileGenerationProgress((char*) strm.next_in - DataS.c_str(), DataS.size());
}
while (strm.avail_out == 0);
deflateEnd (&strm);
}
delete[] Buffer;
}
file->flush();
file->seek(0);
}
Q_EMIT statsFileGenerated(file, name);
m_commentsUpdated = false;
}
示例8: main
//.........这里部分代码省略.........
if (!pInfile)
{
printf("Failed opening input file!\n");
return EXIT_FAILURE;
}
// Determine input file's size.
fseek(pInfile, 0, SEEK_END);
infile_size = ftell(pInfile);
fseek(pInfile, 0, SEEK_SET);
// Open output file.
pOutfile = fopen(pDst_filename, "wb");
if (!pOutfile)
{
printf("Failed opening output file!\n");
return EXIT_FAILURE;
}
printf("Input file size: %u\n", infile_size);
// Init the z_stream
memset(&stream, 0, sizeof(stream));
stream.next_in = s_inbuf;
stream.avail_in = 0;
stream.next_out = s_outbuf;
stream.avail_out = BUF_SIZE;
if ((pMode[0] == 'c') || (pMode[0] == 'C'))
{
// Compression.
uint infile_remaining = infile_size;
if (deflateInit2(&stream, level, LZHAM_Z_LZHAM, WINDOW_BITS, 9, LZHAM_Z_DEFAULT_STRATEGY) != Z_OK)
{
printf("deflateInit() failed!\n");
return EXIT_FAILURE;
}
for ( ; ; )
{
int status;
if (!stream.avail_in)
{
// Input buffer is empty, so read more bytes from input file.
uint n = my_min(BUF_SIZE, infile_remaining);
if (fread(s_inbuf, 1, n, pInfile) != n)
{
printf("Failed reading from input file!\n");
return EXIT_FAILURE;
}
stream.next_in = s_inbuf;
stream.avail_in = n;
infile_remaining -= n;
//printf("Input bytes remaining: %u\n", infile_remaining);
}
status = deflate(&stream, infile_remaining ? Z_NO_FLUSH : Z_FINISH);
if ((status == Z_STREAM_END) || (!stream.avail_out))
{
// Output buffer is full, or compression is done, so write buffer to output file.
uint n = BUF_SIZE - stream.avail_out;
示例9: HW_CaptureThread
static void* HW_CaptureThread(void* arg)
{
HWCapture* capture = (HWCapture*)arg;
z_stream stream;
unsigned char* zbuffer = 0;
int result;
unsigned int nodecount = 0;
unsigned int have = 0;
int savecapture = 1;
capture->done = 0;
if (capture->outputFile == 0)
{
savecapture = 0;
}
if (savecapture)
{
zbuffer = malloc(ZCHUNK);
if (zbuffer == 0)
{
fprintf(stderr, "Error allocating zbuffer\n");
exit(1);
}
stream.zalloc = Z_NULL;
stream.zfree = Z_NULL;
stream.opaque = Z_NULL;
result = deflateInit(&stream, ZCOMPLEVEL);
if (result != Z_OK)
{
fprintf(stderr, "Error initializing ZLIB\n");
exit(1);
}
}
while(capture->running)
{
HWBuffer* node = 0;
unsigned int available;
unsigned int capacity;
unsigned char* buffer;
unsigned int buffersize;
unsigned int bufferpos;
pthread_mutex_lock(&capture->mutex);
node = HW_BufferChainGetFirst(&capture->chain);
if (node)
{
available = node->capacity - node->size;
buffersize = node->size;
bufferpos = node->pos;
buffer = node->buffer;
capacity = node->capacity;
node->pos = node->size;
}
pthread_mutex_unlock(&capture->mutex);
if (node && (buffersize - bufferpos > 0))
HW_Process(&capture->process, buffer + bufferpos, buffersize - bufferpos);
if (node == 0 || available != 0)
{
mssleep(1);
continue;
}
if (savecapture)
{
stream.avail_in = capacity;
stream.next_in = buffer;
do
{
stream.avail_out = ZCHUNK;
stream.next_out = zbuffer;
result = deflate(&stream, Z_NO_FLUSH);
if (result == Z_STREAM_ERROR)
{
fprintf(stderr, "Error compressing stream\n");
exit(1);
}
have = ZCHUNK - stream.avail_out;
capture->compressedsize += have;
if (capture->outputFile)
{
if (fwrite(zbuffer, have, 1, capture->outputFile) != 1)
{
//.........这里部分代码省略.........
示例10: write_bytes_to_file
static void
write_bytes_to_file(FILE * file, char *addr, long bytes, int compression)
{
if (compression == COMPRESSION_LEVEL_NONE) {
while (bytes > 0) {
sword_t count = fwrite(addr, 1, bytes, file);
if (count > 0) {
bytes -= count;
addr += count;
}
else {
perror("error writing to core file");
lose("core file is incomplete or corrupt\n");
}
}
#ifdef LISP_FEATURE_SB_CORE_COMPRESSION
} else if ((compression >= -1) && (compression <= 9)) {
# define ZLIB_BUFFER_SIZE (1u<<16)
z_stream stream;
unsigned char buf[ZLIB_BUFFER_SIZE];
unsigned char * written, * end;
long total_written = 0;
int ret;
stream.zalloc = NULL;
stream.zfree = NULL;
stream.opaque = NULL;
stream.avail_in = bytes;
stream.next_in = (void*)addr;
ret = deflateInit(&stream, compression);
if (ret != Z_OK)
lose("deflateInit: %i\n", ret);
do {
stream.avail_out = sizeof(buf);
stream.next_out = buf;
ret = deflate(&stream, Z_FINISH);
if (ret < 0) lose("zlib deflate error: %i... exiting\n", ret);
written = buf;
end = buf+sizeof(buf)-stream.avail_out;
total_written += end - written;
while (written < end) {
long count = fwrite(written, 1, end-written, file);
if (count > 0) {
written += count;
} else {
perror("error writing to core file");
lose("core file is incomplete or corrupt\n");
}
}
} while (stream.avail_out == 0);
deflateEnd(&stream);
printf("compressed %lu bytes into %lu at level %i\n",
bytes, total_written, compression);
# undef ZLIB_BUFFER_SIZE
#endif
} else {
#ifdef LISP_FEATURE_SB_CORE_COMPRESSION
lose("Unknown core compression level %i, exiting\n", compression);
#else
lose("zlib-compressed core support not built in this runtime\n");
#endif
}
if (fflush(file) != 0) {
perror("error writing to core file");
lose("core file is incomplete or corrupt\n");
}
};
示例11: QC_encode
//we are allowed to trash our input here.
int QC_encode(progfuncs_t *progfuncs, int len, int method, char *in, int handle)
{
int i;
if (method == 0) //copy
{
SafeWrite(handle, in, len);
return len;
}
else if (method == 1) //xor encryption
{
for (i = 0; i < len; i++)
in[i] = in[i] ^ 0xA5;
SafeWrite(handle, in, len);
return len;
}
else if (method == 2) //compression (ZLIB)
{
#ifdef AVAIL_ZLIB
char out[8192];
z_stream strm = {
in,
len,
0,
out,
sizeof(out),
0,
NULL,
NULL,
NULL,
NULL,
NULL,
Z_BINARY,
0,
0
};
i=0;
deflateInit(&strm, Z_BEST_COMPRESSION);
while(deflate(&strm, Z_FINISH) == Z_OK)
{
SafeWrite(handle, out, sizeof(out) - strm.avail_out); //compress in chunks of 8192. Saves having to allocate a huge-mega-big buffer
i+=sizeof(out) - strm.avail_out;
strm.next_out = out;
strm.avail_out = sizeof(out);
}
SafeWrite(handle, out, sizeof(out) - strm.avail_out);
i+=sizeof(out) - strm.avail_out;
deflateEnd(&strm);
return i;
#endif
Sys_Error("ZLIB compression not supported in this build");
return 0;
}
//add your compression/decryption routine here.
else
{
Sys_Error("Wierd method");
return 0;
}
}
示例12: memcpy
int
vncEncodeTight::CompressData(BYTE *dest, int streamId, int dataLen,
int zlibLevel, int zlibStrategy)
{
if (dataLen < TIGHT_MIN_TO_COMPRESS) {
memcpy(dest, m_buffer, dataLen);
return dataLen;
}
z_streamp pz = &m_zsStruct[streamId];
// Initialize compression stream if needed.
if (!m_zsActive[streamId]) {
pz->zalloc = Z_NULL;
pz->zfree = Z_NULL;
pz->opaque = Z_NULL;
vnclog.Print(LL_INTINFO,
VNCLOG("calling deflateInit2 with zlib level:%d\n"),
zlibLevel);
int err = deflateInit2 (pz, zlibLevel, Z_DEFLATED, MAX_WBITS,
MAX_MEM_LEVEL, zlibStrategy);
if (err != Z_OK) {
vnclog.Print(LL_INTINFO,
VNCLOG("deflateInit2 returned error:%d:%s\n"),
err, pz->msg);
return -1;
}
m_zsActive[streamId] = true;
m_zsLevel[streamId] = zlibLevel;
}
int outBufferSize = dataLen + dataLen / 100 + 16;
// Prepare buffer pointers.
pz->next_in = (Bytef *)m_buffer;
pz->avail_in = dataLen;
pz->next_out = (Bytef *)dest;
pz->avail_out = outBufferSize;
// Change compression parameters if needed.
if (zlibLevel != m_zsLevel[streamId]) {
vnclog.Print(LL_INTINFO,
VNCLOG("calling deflateParams with zlib level:%d\n"),
zlibLevel);
int err = deflateParams (pz, zlibLevel, zlibStrategy);
if (err != Z_OK) {
vnclog.Print(LL_INTINFO,
VNCLOG("deflateParams returned error:%d:%s\n"),
err, pz->msg);
return -1;
}
m_zsLevel[streamId] = zlibLevel;
}
// Actual compression.
if ( deflate (pz, Z_SYNC_FLUSH) != Z_OK ||
pz->avail_in != 0 || pz->avail_out == 0 ) {
vnclog.Print(LL_INTINFO, VNCLOG("deflate() call failed.\n"));
return -1;
}
return SendCompressedData(outBufferSize - pz->avail_out);
}
示例13: zlibCompress
/*!
* zlibCompress()
*
* Input: datain (byte buffer with input data)
* nin (number of bytes of input data)
* &nout (<return> number of bytes of output data)
* Return: dataout (compressed data), or null on error
*
* Notes:
* (1) We repeatedly read in and fill up an input buffer,
* compress the data, and read it back out. zlib
* uses two byte buffers internally in the z_stream
* data structure. We use the bbuffers to feed data
* into the fixed bufferin, and feed it out of bufferout,
* in the same way that a pair of streams would normally
* be used if the data were being read from one file
* and written to another. This is done iteratively,
* compressing L_BUF_SIZE bytes of input data at a time.
*/
l_uint8 *
zlibCompress(l_uint8 *datain,
size_t nin,
size_t *pnout)
{
l_uint8 *dataout;
l_int32 status;
size_t nbytes;
l_uint8 *bufferin, *bufferout;
BBUFFER *bbin, *bbout;
z_stream z;
PROCNAME("zlibCompress");
if (!datain)
return (l_uint8 *)ERROR_PTR("datain not defined", procName, NULL);
/* Set up fixed size buffers used in z_stream */
if ((bufferin = (l_uint8 *)CALLOC(L_BUF_SIZE, sizeof(l_uint8))) == NULL)
return (l_uint8 *)ERROR_PTR("bufferin not made", procName, NULL);
if ((bufferout = (l_uint8 *)CALLOC(L_BUF_SIZE, sizeof(l_uint8))) == NULL)
return (l_uint8 *)ERROR_PTR("bufferout not made", procName, NULL);
/* Set up bbuffers and load bbin with the data */
if ((bbin = bbufferCreate(datain, nin)) == NULL)
return (l_uint8 *)ERROR_PTR("bbin not made", procName, NULL);
if ((bbout = bbufferCreate(NULL, 0)) == NULL)
return (l_uint8 *)ERROR_PTR("bbout not made", procName, NULL);
z.zalloc = (alloc_func)0;
z.zfree = (free_func)0;
z.opaque = (voidpf)0;
z.next_in = bufferin;
z.avail_in = 0;
z.next_out = bufferout;
z.avail_out = L_BUF_SIZE;
deflateInit(&z, ZLIB_COMPRESSION_LEVEL);
for ( ; ; ) {
if (z.avail_in == 0) {
z.next_in = bufferin;
bbufferWrite(bbin, bufferin, L_BUF_SIZE, &nbytes);
/* fprintf(stderr, " wrote %d bytes to bufferin\n", nbytes); */
z.avail_in = nbytes;
}
if (z.avail_in == 0)
break;
status = deflate(&z, Z_SYNC_FLUSH);
/* fprintf(stderr, " status is %d, bytesleft = %d, totalout = %d\n",
status, z.avail_out, z.total_out); */
nbytes = L_BUF_SIZE - z.avail_out;
if (nbytes) {
bbufferRead(bbout, bufferout, nbytes);
/* fprintf(stderr, " read %d bytes from bufferout\n", nbytes); */
}
z.next_out = bufferout;
z.avail_out = L_BUF_SIZE;
}
deflateEnd(&z);
bbufferDestroy(&bbin);
dataout = bbufferDestroyAndSaveData(&bbout, pnout);
FREE(bufferin);
FREE(bufferout);
return dataout;
}
示例14: sftp_compress_write_data
int sftp_compress_write_data(struct ssh2_packet *pkt) {
struct sftp_compress *comp;
z_stream *stream;
comp = &(write_compresses[write_comp_idx]);
stream = &(write_streams[write_comp_idx]);
if (comp->use_zlib &&
comp->stream_ready) {
unsigned char buf[16384], *input;
char *payload;
uint32_t input_len, payload_len = 0, payload_sz;
pool *sub_pool;
int zres;
if (pkt->payload_len == 0) {
return 0;
}
sub_pool = make_sub_pool(pkt->pool);
/* Use a copy of the payload, rather than the actual payload itself,
* as zlib may alter the payload contents and then encounter an error.
*/
input_len = pkt->payload_len;
input = palloc(sub_pool, input_len);
memcpy(input, pkt->payload, input_len);
/* Try to guess at how small the compressed data will be. Optimistic
* estimate, for now, will be a factor of 2, with a minimum of 1K.
*/
payload_sz = 1024;
if ((input_len * 2) > payload_sz) {
payload_sz = input_len * 2;
}
payload = palloc(sub_pool, payload_sz);
stream->next_in = input;
stream->avail_in = input_len;
stream->avail_out = 0;
while (stream->avail_out == 0) {
size_t copy_len = 0;
pr_signals_handle();
stream->next_out = buf;
stream->avail_out = sizeof(buf);
zres = deflate(stream, Z_SYNC_FLUSH);
switch (zres) {
case Z_OK:
copy_len = sizeof(buf) - stream->avail_out;
/* Allocate more space for the data if necessary. */
if ((payload_len + copy_len) > payload_sz) {
uint32_t new_sz;
char *tmp;
new_sz = payload_sz;
while ((payload_len + copy_len) > new_sz) {
pr_signals_handle();
/* Keep doubling the size until it is large enough. */
new_sz *= 2;
}
pr_trace_msg(trace_channel, 20,
"allocating larger payload size (%lu bytes) for "
"deflated data (%lu bytes) plus existing payload %lu bytes",
(unsigned long) new_sz, (unsigned long) copy_len,
(unsigned long) payload_len);
tmp = palloc(sub_pool, new_sz);
memcpy(tmp, payload, payload_len);
payload = tmp;
payload_sz = new_sz;
}
memcpy(payload + payload_len, buf, copy_len);
payload_len += copy_len;
pr_trace_msg(trace_channel, 20,
"deflated %lu bytes to %lu bytes",
(unsigned long) input_len, (unsigned long) copy_len);
break;
default:
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
"unhandled zlib error (%d) while compressing", zres);
destroy_pool(sub_pool);
errno = EIO;
return -1;
}
}
if (payload_len > 0) {
if (pkt->payload_len < payload_len) {
//.........这里部分代码省略.........
示例15: deflate_common
static int
deflate_common(struct mbuf *m, struct mbuf *md, size_t *lenp,
int mode) /* 0: compress 1: decompress */
{
struct mbuf *mprev;
struct mbuf *p;
struct mbuf *n = NULL, *n0 = NULL, **np;
z_stream zs;
int error = 0;
int zerror;
size_t offset;
#define MOREBLOCK() \
do { \
/* keep the reply buffer into our chain */ \
if (n) { \
n->m_len = zs.total_out - offset; \
offset = zs.total_out; \
*np = n; \
np = &n->m_next; \
n = NULL; \
} \
\
/* get a fresh reply buffer */ \
n = m_getcl(M_NOWAIT, MT_DATA, 0); \
if (!n) { \
error = ENOBUFS; \
goto fail; \
} \
n->m_len = 0; \
n->m_len = M_TRAILINGSPACE(n); \
n->m_next = NULL; \
/* \
* if this is the first reply buffer, reserve \
* region for ipcomp header. \
*/ \
if (*np == NULL) { \
n->m_len -= sizeof(struct ipcomp); \
n->m_data += sizeof(struct ipcomp); \
} \
\
zs.next_out = mtod(n, u_int8_t *); \
zs.avail_out = n->m_len; \
} while (0)
for (mprev = m; mprev && mprev->m_next != md; mprev = mprev->m_next)
;
if (!mprev)
panic("md is not in m in deflate_common");
bzero(&zs, sizeof(zs));
zs.zalloc = deflate_alloc;
zs.zfree = deflate_free;
zerror = mode ? inflateInit2(&zs, deflate_window_in)
: deflateInit2(&zs, deflate_policy, Z_DEFLATED,
deflate_window_out, deflate_memlevel,
Z_DEFAULT_STRATEGY);
if (zerror != Z_OK) {
error = ENOBUFS;
goto fail;
}
n0 = n = NULL;
np = &n0;
offset = 0;
zerror = 0;
p = md;
while (p && p->m_len == 0) {
p = p->m_next;
}
/* input stream and output stream are available */
while (p && zs.avail_in == 0) {
/* get input buffer */
if (p && zs.avail_in == 0) {
zs.next_in = mtod(p, u_int8_t *);
zs.avail_in = p->m_len;
p = p->m_next;
while (p && p->m_len == 0) {
p = p->m_next;
}
}
/* get output buffer */
if (zs.next_out == NULL || zs.avail_out == 0) {
MOREBLOCK();
}
zerror = mode ? inflate(&zs, Z_NO_FLUSH)
: deflate(&zs, Z_NO_FLUSH);
if (zerror == Z_STREAM_END)
; /* once more. */
else if (zerror == Z_OK) {
/* inflate: Z_OK can indicate the end of decode */
if (mode && !p && zs.avail_out != 0)
goto terminate;
else
; /* once more. */
//.........这里部分代码省略.........