示例#1
0
    void writeCompressedPeaks(SpectrumPtr s, ostream& os)
    {
        // Build arrays to hold peaks prior to compression
        int numPeaks = (int) s->defaultArrayLength;
        double *pD = new double[numPeaks];
        float *pF = new float[numPeaks];

        const BinaryDataArray& mzArray = *s->getMZArray();
        const BinaryDataArray& intensityArray = *s->getIntensityArray();
        for(int j = 0; j < numPeaks; j++)
        {
            pD[j] = mzArray.data[j];
            pF[j] = (float) intensityArray.data[j];
        }

        // compress mz
        uLong sizeM = (uLong) (numPeaks * sizeDoubleMSn);
        uLong comprLenM = compressBound(sizeM);
        Byte *comprM = (Byte*)calloc((uInt)comprLenM, 1);
        int retM = compress(comprM, &comprLenM, (const Bytef*)pD, sizeM);
        
        // compress intensity
        uLong sizeI = (uLong) (numPeaks * sizeFloatMSn);
        uLong comprLenI = compressBound(sizeI);
        Byte *comprI = (Byte*)calloc((uInt)comprLenI, 1);
        int retI = compress(comprI, &comprLenI, (const Bytef*)pF, sizeI);

        // Write the compressed peaks if all is well
        if ((Z_OK == retM) && (Z_OK == retI))
        {
            // write length of compressed array of m/z
            os.write(reinterpret_cast<char *>(&comprLenM), sizeIntMSn);

            // write length of compressed array of intensities
            os.write(reinterpret_cast<char *>(&comprLenI), sizeIntMSn);

            // write compressed array of m/z
            os.write(reinterpret_cast<char *>(comprM), comprLenM);

            // write compressed array of intensities
            os.write(reinterpret_cast<char *>(comprI), comprLenI);
        }

        // Clean up memory
        free(comprM);
        free(comprI);
        delete [] pD;
        delete [] pF;
        
        // In case of error, throw exception AFTER cleaning up memory
        if (Z_OK != retM || Z_OK != retI)
        {
            throw runtime_error("[Serializer_MSn::writeCompressedPeaks] Error compressing peaks.");
        }
    }
示例#2
0
//Deryabin Andrew: vst chunks support
template <typename T> void
rdwr_writeRaw(T fd, std::vector<char> rawdata, const char *file, int line)
{
    unsigned long complen = compressBound(rawdata.size());
    char *compressed = new char [complen];
    if(!compressed)
    {
        fprintf(stderr, "Failed to allocate %lu bytes of memory at %s:%d\n", complen, file, line);
        throw RemotePluginClosedException();
    }

    std::vector<char>::pointer ptr = &rawdata [0];

    if(compress2((Bytef *)compressed, &complen, (Bytef *)ptr, rawdata.size(), 9) != Z_OK)
    {
        delete compressed;
        fprintf(stderr, "Failed to compress source buffer at %s:%d\n", file, line);
        throw RemotePluginClosedException();
    }

    fprintf(stderr, "compressed source buffer. size=%lu bytes\n", complen);

    int len = complen;
    rdwr_tryWrite(fd, &len, sizeof(int), file, line);
    len = rawdata.size();
    rdwr_tryWrite(fd, &len, sizeof(int), file, line);    
    rdwr_tryWrite(fd, compressed, complen, file, line);

    delete [] compressed;
}
示例#3
0
文件: Deflate.cpp 项目: unvBell/Bell
	//	圧縮
	std::vector<byte> compress(ArrayRef<byte> src, int level) {
		//	レベルを[1, 9]にクリップ
		level = clamp(level, 1, 9);

		//	データを格納するバッファの確保
		std::vector<byte> data;
		data.resize(compressBound(static_cast<uLong>(src.size())));

		//	初期化
		z_stream zs		= {};
		zs.next_in		= const_cast<byte*>(src.data());	//	データは書き換えられない
		zs.avail_in		= static_cast<uInt>(src.size());
		zs.next_out		= &data[0];
		zs.avail_out	= data.size();

		if(deflateInit2(&zs, level, Z_DEFLATED, -MAX_WBITS, 8, Z_DEFAULT_STRATEGY))
			BELL_THROW(DeflateError, "Failed to initialize compress stream.");

		//	圧縮
		int res = ::deflate(&zs, Z_FINISH);

		data.resize(zs.total_out);

		//	片付け
		deflateEnd(&zs);
		if(res != Z_STREAM_END)
			BELL_THROW(DeflateError, "Failed to compress.");

		return data;
	}
示例#4
0
struct lumberjack *lumberjack_new(const char *host, unsigned short port, size_t window_size) {
  struct lumberjack *lumberjack;
  lumberjack_init(); /* global one-time init */

  lumberjack = malloc(sizeof(struct lumberjack));
  lumberjack->host = host;
  lumberjack->port = port;
  lumberjack->fd = -1;
  lumberjack->sequence = 0; //rand();
  lumberjack->ssl = NULL;
  lumberjack->connected = 0;

  /* I tried with 128, 256, 512, 1024, 2048, and 16384,
   * in a local network, an window size of 1024 seemed to have the best
   * performance (equal to 2048 and 16384) for the least memory cost. */
  if (window_size < 1024) {
    flog(stdout, "Window size less than 1024 (%d) isn't shown to have " \
         "speed-performance benefits", window_size);
  }

  lumberjack->ring_size = window_size;
  lumberjack->ring = ring_new_size(lumberjack->ring_size);

  /* Create this once. */
  lumberjack->ssl_context = SSL_CTX_new(SSLv23_client_method());
  SSL_CTX_set_verify(lumberjack->ssl_context, SSL_VERIFY_PEER, NULL);

  lumberjack->io_buffer = str_new_size(16384); /* TODO(sissel): tunable */

  /* zlib provides compressBound() to give a 'worst case' compressed
   * payload size on a input payload of a given size. */
  lumberjack->compression_buffer = str_new_size(compressBound(16384));
  return lumberjack;
} /* lumberjack_new */
示例#5
0
static int LUA_C_zlib_compress(lua_State* ls)
  {
  size_t l = 0;
  auto s = luaL_checklstring(ls, 1, &l);

  if(l == 0)
    {
    lua_pushstring(ls, "");
    return 1;
    }
  string cp;
  cp.reserve(compressBound(l));
  while(true)
    {
    size_t size = cp.capacity();
    const intptr_t rets = compress(
      (Bytef*)cp.end()._Ptr,
      (uLongf*)&size,
      (const Bytef*)s,
      (uLongf)l);
    switch(rets)
      {
      case Z_OK:
        lua_pushlstring(ls, cp.end()._Ptr, size);
        return 1;
      case Z_BUF_ERROR:
        cp.reserve(cp.capacity() + 0x10);
        continue;
      default:
        break;
      }
    lua_pushstring(ls, (xmsg() << "zlibѹËõʧ°Ü : " << rets).c_str());
    return lua_error(ls);
    }
  }
示例#6
0
//! Compresses another packet and stores it in self (source left intact)
void WorldPacket::Compress(z_stream* compressionStream, WorldPacket const* source)
{
    ASSERT(source != this);

    Opcodes uncompressedOpcode = source->GetOpcode();
    if (uncompressedOpcode & COMPRESSED_OPCODE_MASK)
    {
        sLog->outError(LOG_FILTER_NETWORKIO, "Packet with opcode 0x%04X is already compressed!", uncompressedOpcode);
        return;
    }

    Opcodes opcode = Opcodes(uncompressedOpcode | COMPRESSED_OPCODE_MASK);
    uint32 size = source->size();
    uint32 destsize = compressBound(size);

    size_t sizePos = 0;
    resize(destsize + sizeof(uint32));

    _compressionStream = compressionStream;
    Compress(static_cast<void*>(&_storage[0] + sizeof(uint32)), &destsize, static_cast<const void*>(source->contents()), size);
    if (destsize == 0)
        return;

    put<uint32>(sizePos, size);
    resize(destsize + sizeof(uint32));

    SetOpcode(opcode);

	sLog->outInfo(LOG_FILTER_NETWORKIO, "%s (len %u) successfully compressed to %04X (len %u)", GetOpcodeNameForLogging(uncompressedOpcode, true).c_str(), size, opcode, destsize);
}
示例#7
0
//! Compresses packet in place
void WorldPacket::Compress(z_stream* compressionStream)
{
    Opcodes uncompressedOpcode = GetOpcode();
    if (uncompressedOpcode & COMPRESSED_OPCODE_MASK)
    {
        sLog->outError(LOG_FILTER_NETWORKIO, "Packet with opcode 0x%04X is already compressed!", uncompressedOpcode);
        return;
    }

    Opcodes opcode = Opcodes(uncompressedOpcode | COMPRESSED_OPCODE_MASK);
    uint32 size = wpos();
    uint32 destsize = compressBound(size);

    std::vector<uint8> storage(destsize);

    _compressionStream = compressionStream;
    Compress(static_cast<void*>(&storage[0]), &destsize, static_cast<const void*>(contents()), size);
    if (destsize == 0)
        return;

    clear();
    reserve(destsize + sizeof(uint32));
    *this << uint32(size);
    append(&storage[0], destsize);
    SetOpcode(opcode);

	sLog->outInfo(LOG_FILTER_NETWORKIO, "%s (len %u) successfully compressed to %04X (len %u)", GetOpcodeNameForLogging(uncompressedOpcode, true).c_str(), size, opcode, destsize);
}
示例#8
0
  void ZlibCompressor::Compress(std::string& compressed,
                                const void* uncompressed,
                                size_t uncompressedSize)
  {
    if (uncompressedSize == 0)
    {
      compressed.clear();
      return;
    }

    uLongf compressedSize = compressBound(uncompressedSize) + 1024 /* security margin */;
    if (compressedSize == 0)
    {
      compressedSize = 1;
    }

    uint8_t* target;
    if (HasPrefixWithUncompressedSize())
    {
      compressed.resize(compressedSize + sizeof(uint64_t));
      target = reinterpret_cast<uint8_t*>(&compressed[0]) + sizeof(uint64_t);
    }
    else
    {
      compressed.resize(compressedSize);
      target = reinterpret_cast<uint8_t*>(&compressed[0]);
    }

    int error = compress2(target,
                          &compressedSize,
                          const_cast<Bytef *>(static_cast<const Bytef *>(uncompressed)), 
                          uncompressedSize,
                          GetCompressionLevel());

    if (error != Z_OK)
    {
      compressed.clear();

      switch (error)
      {
      case Z_MEM_ERROR:
        throw OrthancException(ErrorCode_NotEnoughMemory);

      default:
        throw OrthancException(ErrorCode_InternalError);
      }  
    }

    // The compression was successful
    if (HasPrefixWithUncompressedSize())
    {
      uint64_t s = static_cast<uint64_t>(uncompressedSize);
      memcpy(&compressed[0], &s, sizeof(uint64_t));
      compressed.resize(compressedSize + sizeof(uint64_t));
    }
    else
    {
      compressed.resize(compressedSize);
    }
  }
示例#9
0
Variant f_nzcompress(CStrRef uncompressed) {
  nzlib_format_t* format = NULL;
  size_t len = 0;
  char *compressed = NULL;
  int rc;

  len = compressBound(uncompressed.size());
  format = (nzlib_format_t*)malloc(sizeof(*format) + len);
  if (format == NULL) {
    goto error;
  }

  format->magic = htonl(NZLIB_MAGIC);
  format->uncompressed_sz = htonl(uncompressed.size());

  rc = compress(format->buf, &len, (uint8_t*)uncompressed.data(),
                uncompressed.size());
  if (rc != Z_OK) {
    goto error;
  }

  compressed = (char*)realloc(format, len + sizeof(*format) + 1);
  if (compressed == NULL) {
    goto error;
  }
  compressed[len + sizeof(*format)] = '\0';
  return String(compressed, len + sizeof(*format), AttachString);

error:
  free(format);
  return false;
}
示例#10
0
char *catalog_query_compress_update(const char *text, unsigned long *data_length)
{
	unsigned long compress_data_length;
	/* Default to buffer error incase we don't compress. */
	int success = Z_BUF_ERROR;

	/* Estimates the bounds for the compressed data. */
	compress_data_length = compressBound(*data_length);
	char* compress_data= malloc(compress_data_length);

	success = compress((Bytef*)compress_data+1, &compress_data_length, (const Bytef*)text, *data_length);
	/* Prefix the data with 0x1A (Control-Z) to indicate a compressed packet. */
	compress_data[0] = 0x1A;

	/* Copy data over if not compressing or compression failed. */
	if(success!=Z_OK) {
		/* Compression failed, fall back to original uncompressed update. */
		debug(D_DEBUG,"warning: Unable to compress data for update.\n");
		free(compress_data);
		return NULL;
	} else {
		/* Add 1 to the compresed data length to account for the leading 0x1A. */
		*data_length = compress_data_length + 1;
		return compress_data;
	}
}
示例#11
0
void compressUtil(unsigned long originalDataLen) {
	//get compress buffer bound
	int rv;
	int compressBufBound = compressBound(originalDataLen);
	compressedBuf = (unsigned char*) malloc(sizeof(unsigned char)*compressBufBound);
	unsigned long compressedDataLen = compressBufBound;
	//compress
	rv = compress2(compressedBuf, &compressedDataLen, dataBuf, originalDataLen, 6);
	if (Z_OK != rv) {
		LOGE(1, "compression error");
		free(compressedBuf);
		return;
	}
	LOGI(1, "upper bound:%d; input: %d; compressed: %d",
			compressBufBound, originalDataLen, compressedDataLen);
	//decompress and verify result
	unsigned long decompressedDataLen = S_BUF_SIZE;
	rv = uncompress(decompressedBuf, &decompressedDataLen, compressedBuf, compressedDataLen);
	if (Z_OK != rv) {
		LOGE(1, "decompression error");
		free(compressedBuf);
		return;
	}
	LOGI(1, "decompressed: %d", decompressedDataLen);
	if (0 == memcmp(dataBuf, decompressedBuf, originalDataLen)) {
		LOGI(1, "decompressed data same as original data");
	} else {
		LOGI(1, "decompressed data different from original data");
	}
	//free resource
	free(compressedBuf);
}
示例#12
0
cv::Mat compressData2(const cv::Mat & data)
{
	cv::Mat bytes;
	if(!data.empty())
	{
		uLong sourceLen = uLong(data.total())*uLong(data.elemSize());
		uLong destLen = compressBound(sourceLen);
		bytes = cv::Mat(1, destLen+3*sizeof(int), CV_8UC1);
		int errCode = compress(
						(Bytef *)bytes.data,
						&destLen,
						(const Bytef *)data.data,
						sourceLen);
		bytes = cv::Mat(bytes, cv::Rect(0,0, destLen+3*sizeof(int), 1));
		*((int*)&bytes.data[destLen]) = data.rows;
		*((int*)&bytes.data[destLen+sizeof(int)]) = data.cols;
		*((int*)&bytes.data[destLen+2*sizeof(int)]) = data.type();

		if(errCode == Z_MEM_ERROR)
		{
			UERROR("Z_MEM_ERROR : Insufficient memory.");
		}
		else if(errCode == Z_BUF_ERROR)
		{
			UERROR("Z_BUF_ERROR : The buffer dest was not large enough to hold the uncompressed data.");
		}
	}
	return bytes;
}
size_t
mongoc_compressor_max_compressed_length (int32_t compressor_id, size_t len)
{
   TRACE ("Getting compression length for '%s' (%d)",
          mongoc_compressor_id_to_name (compressor_id),
          compressor_id);
   switch (compressor_id) {
#ifdef MONGOC_ENABLE_COMPRESSION_SNAPPY
   case MONGOC_COMPRESSOR_SNAPPY_ID:
      return snappy_max_compressed_length (len);
      break;
#endif

#ifdef MONGOC_ENABLE_COMPRESSION_ZLIB
   case MONGOC_COMPRESSOR_ZLIB_ID:
      return compressBound (len);
      break;
#endif

   case MONGOC_COMPRESSOR_NOOP_ID:
      return len;
      break;
   default:
      return 0;
   }
}
示例#14
0
  static
  JSBool
  ejszlib_compress (JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) 
  {
    EJS_CHECK_NUM_ARGS(cx,obj,1,argc);
    
    char* ctype;
    size_t len;
    STRING_TO_CHARVEC(argv[0],ctype,len);
    // todo: perhaps simply return an empty string
    if (!len) EJS_THROW_ERROR(cx,obj,"nothing to compress");
    
    uLong destLen=compressBound(len);
    
    Byte* dest=(Byte *)JS_malloc(cx, destLen);
    if (!dest) return JS_FALSE;

    if (compress(dest, &destLen, (Byte*)ctype, len)!=Z_OK) {
      JS_free(cx,dest);
      EJS_THROW_ERROR(cx,obj,"compression failed");
    }

    assert(destLen>0);
    dest=(Byte *)JS_realloc(cx,dest,destLen);

    RETSTR(dest,destLen,rval);
  }
示例#15
0
std::vector<unsigned char> compressData(const cv::Mat & data)
{
	std::vector<unsigned char> bytes;
	if(!data.empty())
	{
		uLong sourceLen = uLong(data.total())*uLong(data.elemSize());
		uLong destLen = compressBound(sourceLen);
		bytes.resize(destLen);
		int errCode = compress(
						(Bytef *)bytes.data(),
						&destLen,
						(const Bytef *)data.data,
						sourceLen);

		bytes.resize(destLen+3*sizeof(int));
		*((int*)&bytes[destLen]) = data.rows;
		*((int*)&bytes[destLen+sizeof(int)]) = data.cols;
		*((int*)&bytes[destLen+2*sizeof(int)]) = data.type();

		if(errCode == Z_MEM_ERROR)
		{
			UERROR("Z_MEM_ERROR : Insufficient memory.");
		}
		else if(errCode == Z_BUF_ERROR)
		{
			UERROR("Z_BUF_ERROR : The buffer dest was not large enough to hold the uncompressed data.");
		}
	}
	return bytes;
}
void idSaveGame::FinalizeCache( void ) {
	if( !isCompressed ) {
		return;
	}
	int offset = sizeof( int );
	//resize destination buffer
	CRawVector zipped;
	int zipsize = compressBound( cache.size() );
	zipped.resize( zipsize );
	//compress the cache
	int err = compress( ( Bytef * )&zipped[0], ( uLongf * )&zipsize,
						( const Bytef * )&cache[0], cache.size() );
	if( err != Z_OK ) {
		gameLocal.Error( "idSaveGame::FinalizeCache: compress failed with code %d", err );
	}
	zipped.resize( zipsize );
	//write compressed size and uncompressed size
	file->WriteInt( zipped.size() );
	offset += sizeof( int );
	file->WriteInt( cache.size() );
	offset += sizeof( int );
	//write compressed data
	file->Write( &zipped[0], zipped.size() );
	offset += zipped.size();
	//write offset from EOF to cache start
	file->WriteInt( -offset );
	cache.clear();
}
示例#17
0
/**
 * Compress two strings and return the length of the compressed data
 * @param x String x
 * @param y String y
 * @return length of the compressed data.
 */
static float compress_str2(hstring_t x, hstring_t y)
{
    unsigned long tmp, width;
    unsigned char *src, *dst;

    assert(x.type == y.type);

    width = x.type == TYPE_SYM ? sizeof(sym_t) : sizeof(char);
    tmp = compressBound((x.len + y.len) * width);

    dst = malloc(tmp);
    src = malloc(tmp);
    if (!src || !dst) {
        error("Failed to allocate memory for compression");
        return -1;
    }

    /* Concatenate sequences y and x */
    memcpy(src, y.str.s, y.len * width);
    memcpy(src + y.len * width, x.str.s, x.len * width);

    compress2(dst, &tmp, src, (x.len + y.len) * width, level);

    free(dst);
    free(src);
    return (float) tmp;
}
/**
 * Try to compress the given block of data.
 *
 * @param data block to compress; if compression
 *        resulted in a smaller block, the first
 *        bytes of data are updated to the compressed
 *        data
 * @param oldSize number of bytes in data
 * @param result set to the compressed data
 * @param newSize set to size of result
 * @return GNUNET_YES if compression reduce the size,
 *         GNUNET_NO if compression did not help
 */
static int
try_compression (const char *data, size_t oldSize, char **result,
                 size_t * newSize)
{
  char *tmp;
  uLongf dlen;

#ifdef compressBound
  dlen = compressBound (oldSize);
#else
  dlen = oldSize + (oldSize / 100) + 20;
  /* documentation says 100.1% oldSize + 12 bytes, but we
   * should be able to overshoot by more to be safe */
#endif
  tmp = GNUNET_malloc (dlen);
  if (Z_OK ==
      compress2 ((Bytef *) tmp, &dlen, (const Bytef *) data, oldSize, 9))
  {
    if (dlen < oldSize)
    {
      *result = tmp;
      *newSize = dlen;
      return GNUNET_YES;
    }
  }
  GNUNET_free (tmp);
  return GNUNET_NO;
}
示例#19
0
int CDataFileWriter::AddData(int Size, void *pData)
{
	if(!m_File) return 0;

	dbg_assert(m_NumDatas < 1024, "too much data");

	CDataInfo *pInfo = &m_aDatas[m_NumDatas];
	unsigned long s = compressBound(Size);
	void *pCompData = mem_alloc(s, 1); // temporary buffer that we use during compression

	int Result = compress((Bytef*)pCompData, &s, (Bytef*)pData, Size); // ignore_convention
	if(Result != Z_OK)
	{
		dbg_msg("datafile", "compression error %d", Result);
		dbg_assert(0, "zlib error");
	}

	pInfo->m_UncompressedSize = Size;
	pInfo->m_CompressedSize = (int)s;
	pInfo->m_pCompressedData = mem_alloc(pInfo->m_CompressedSize, 1);
	mem_copy(pInfo->m_pCompressedData, pCompData, pInfo->m_CompressedSize);
	mem_free(pCompData);

	m_NumDatas++;
	return m_NumDatas-1;
}
示例#20
0
uint32_t
zlib_buf_extra(uint64_t buflen)
{
	if (buflen > SINGLE_CALL_MAX)
		buflen = SINGLE_CALL_MAX;
	return (compressBound(buflen) - buflen);
}
示例#21
0
文件: UserReport.cpp 项目: 2asoft/0ad
	void ConstructRequestData(const CUserReport& report)
	{
		// Construct the POST request data in the application/x-www-form-urlencoded format

		std::string r;

		r += "user_id=";
		AppendEscaped(r, m_UserID);

		r += "&time=" + CStr::FromInt64(report.m_Time);

		r += "&type=";
		AppendEscaped(r, report.m_Type);

		r += "&version=" + CStr::FromInt(report.m_Version);

		r += "&data=";
		AppendEscaped(r, report.m_Data);

		// Compress the content with zlib to save bandwidth.
		// (Note that we send a request with unlabelled compressed data instead
		// of using Content-Encoding, because Content-Encoding is a mess and causes
		// problems with servers and breaks Content-Length and this is much easier.)
		std::string compressed;
		compressed.resize(compressBound(r.size()));
		uLongf destLen = compressed.size();
		int ok = compress((Bytef*)compressed.c_str(), &destLen, (const Bytef*)r.c_str(), r.size());
		ENSURE(ok == Z_OK);
		compressed.resize(destLen);

		m_RequestData.swap(compressed);
	}
示例#22
0
size_t
Zlib::Deflate(const char *in, size_t in_size, char **out, int l)
{
    z_stream l_stream;
    uLongf l_out_size = compressBound(in_size);

    *out = new char[l_out_size];

    l_stream.next_in  = reinterpret_cast<Bytef *>(const_cast<char *>(in));
    l_stream.avail_in = static_cast<uInt>(in_size);

    l_stream.next_out  = reinterpret_cast<Bytef *>(*out);
    l_stream.avail_out = static_cast<uInt>(l_out_size);

    l_stream.zalloc = static_cast<alloc_func>(0);
    l_stream.zfree  = static_cast<free_func>(0);
    l_stream.opaque = static_cast<voidpf>(0);

    if (deflateInit(&l_stream, l) != Z_OK) {
        MMWARNING("Failed to initialize deflate.");
        return(0);
    }

    if (deflate(&l_stream, Z_FINISH) != Z_STREAM_END) {
        deflateEnd(&l_stream);
        MMWARNING("Failed during deflation.");
        return(0);
    }

    l_out_size = l_stream.total_out;
    deflateEnd(&l_stream);

    return(l_out_size);
}
示例#23
0
static bool _savePNGState(struct GBA* gba, struct VFile* vf) {
	unsigned stride;
	const void* pixels = 0;
	gba->video.renderer->getPixels(gba->video.renderer, &stride, &pixels);
	if (!pixels) {
		return false;
	}

	struct GBASerializedState* state = GBAAllocateState();
	if (!state) {
		return false;
	}
	GBASerialize(gba, state);
	uLongf len = compressBound(sizeof(*state));
	void* buffer = malloc(len);
	if (!buffer) {
		GBADeallocateState(state);
		return false;
	}
	compress(buffer, &len, (const Bytef*) state, sizeof(*state));
	GBADeallocateState(state);

	png_structp png = PNGWriteOpen(vf);
	png_infop info = PNGWriteHeader(png, VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS);
	if (!png || !info) {
		PNGWriteClose(png, info);
		free(buffer);
		return false;
	}
	PNGWritePixels(png, VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS, stride, pixels);
	PNGWriteCustomChunk(png, "gbAs", len, buffer);
	PNGWriteClose(png, info);
	free(buffer);
	return true;
}
示例#24
0
int   XComDoc_Compress(_xcd_int8* dest_buf,_xcd_int8* src_buf, int src_len,int compressRate)
{
	if(compressRate == XCOMDOC_COMPRESS_ENCRYPT)
		return XComDoc_Encrypot(dest_buf,src_buf,src_len);

    if(compressRate <= 10)
    {
#ifndef _WIN32_WCE
        uLong dest_len =  compressBound(src_len);
#else
        uLong dest_len =  2 * src_len + 12;
#endif
        compress2((Byte *)dest_buf,&dest_len,(Byte *)src_buf,src_len,compressRate - 1);
        return dest_len;
    }    
    else//大于10。用LZMA压缩
    {
        size_t dest_len = 2 * src_len + 12;
#ifdef _USE_LZMA_        
        struct _outProp
        {
            char _v;
            int  _disSize;
        }outProps;
        CompressLzma(dest_buf , &dest_len , src_buf ,src_len , (unsigned char*)&outProps , compressRate - 6 );
#else
        XEVOL_LOG(eXL_ERROR_FALT , "lzma not compiled, uncompressed failed\n");
        assert(0);
#endif
        return (int)dest_len;
    }
	
}
	void ShaderManager::Save(Archive& archive)
	{
		UINT count = mData.size();
		archive.Write(&count);

		for (auto it : mData)
		{
			std::shared_ptr<Shader> shader = it.second;

			archive.Write(&it.first);

			char* source = (char*)shader->Source();
			ULONG  size = (ULONG)strlen(source);
			ULONG bound = compressBound(size);

			BYTE* comp_source = (BYTE*)malloc(bound);

			compress(comp_source, &bound, reinterpret_cast<const Bytef*>(source), size);

			archive.Write(&size);
			archive.Write(&bound);
			archive.Write(comp_source, bound);

			free(comp_source);
		}
	}
示例#26
0
const char *test_zlib_vmbuf() {
    ssize_t data_size;
    for (data_size = DATA_SIZE - 5; data_size < DATA_SIZE + 5; ++data_size) {
        int data[data_size];
        int i;
        for (i = 0; i < data_size; ++i) {
            data[i] = i;
        }
        size_t expected_size = compressBound(sizeof(data));
        uint8_t *expected = malloc(expected_size);
        compress(expected, &expected_size, (uint8_t *)data, sizeof(data));

        struct vmbuf compressed = VMBUF_INITIALIZER;
        vmbuf_init(&compressed, 128);
        if (0 > vmbuf_deflate_ptr(data, sizeof(data), &compressed))
            mu_fail("vmbuf_deflate_ptr() failed");
        // printf("%zu ==> %zu  (%zu)\n", sizeof(data), vmbuf_wlocpos(&compressed), expected_size);
        mu_assert_eqi(expected_size + 12 /* gzip header and trailer */, vmbuf_wlocpos(&compressed));
        struct vmbuf decompressed = VMBUF_INITIALIZER;
        vmbuf_init(&decompressed, 128);
        vmbuf_inflate2(&compressed, &decompressed);
        mu_assert_eqi(vmbuf_wlocpos(&decompressed), sizeof(data));
        typeof(data[0]) *decompressed_buf = vmbuf_mem(&decompressed);
        for (i = 0; i < data_size; ++i)
            mu_assert_eqi_idx(i, data[i], decompressed_buf[i]);
        vmbuf_free(&compressed);
        vmbuf_free(&decompressed);
        free(expected);
    }
    return NULL;
}
示例#27
0
      bool encode(tbnet::DataBuffer *output)
      {
         output->writeInt32(modified_time);
         output->writeInt32(type);
         output->writeString(group_name);
         if (size>0) {
            unsigned long source_len = size;
            unsigned long dest_len = compressBound(source_len);
            unsigned char *dest = (unsigned char*) malloc(dest_len);
            int ret = compress(dest, &dest_len, (unsigned char*)data, source_len);
            if (ret != Z_OK) {
               log_warn( "compress error, ret: %d", ret);
            }

            if (ret == Z_OK && dest_len>0) {
               output->writeInt32(dest_len);
               output->writeInt32(size);
               output->writeBytes(dest, dest_len);
            } else {
               output->writeInt32(size);
               output->writeInt32(0);
               output->writeBytes(data, size);
            }
            ::free(dest);
            dest = NULL;
         } else {
            output->writeInt32(size);
            output->writeInt32(0);
         }
         return true;
      }
/**
 * Compute a type map message for this peer.
 *
 * @return this peers current type map message.
 */
struct GNUNET_MessageHeader *
GSC_TYPEMAP_compute_type_map_message ()
{
  char *tmp;
  uLongf dlen;
  struct GNUNET_MessageHeader *hdr;

#ifdef compressBound
  dlen = compressBound (sizeof (my_type_map));
#else
  dlen = sizeof (my_type_map) + (sizeof (my_type_map) / 100) + 20;
  /* documentation says 100.1% oldSize + 12 bytes, but we
   * should be able to overshoot by more to be safe */
#endif
  hdr = GNUNET_malloc (dlen + sizeof (struct GNUNET_MessageHeader));
  tmp = (char *) &hdr[1];
  if ((Z_OK !=
       compress2 ((Bytef *) tmp, &dlen, (const Bytef *) &my_type_map,
                  sizeof (my_type_map), 9)) || (dlen >= sizeof (my_type_map)))
  {
    dlen = sizeof (my_type_map);
    memcpy (tmp, &my_type_map, sizeof (my_type_map));
    hdr->type = htons (GNUNET_MESSAGE_TYPE_CORE_BINARY_TYPE_MAP);
  }
  else
  {
    hdr->type = htons (GNUNET_MESSAGE_TYPE_CORE_COMPRESSED_TYPE_MAP);
  }
  hdr->size = htons ((uint16_t) dlen + sizeof (struct GNUNET_MessageHeader));
  return hdr;
}
示例#29
0
/*
src [in]
srcLen [in]
dest [out]
destLen [out]
compressLevel [in]

compressLevel Copy from zlib.h
#define Z_NO_COMPRESSION 0
#define Z_BEST_SPEED 1
#define Z_BEST_COMPRESSION 9
#define Z_DEFAULT_COMPRESSION (-1)
*/
int Compress(const char * src, unsigned long int srcLen, 
			std::string & dest, unsigned long int & destLen,
			int compressLevel =Z_DEFAULT_COMPRESSION)
{
	destLen =compressBound(srcLen);
	dest.resize(destLen);
	return compress2((Bytef*)(&dest[0]),(uLongf*)(&destLen),
					   (Bytef*)src,(uLong)srcLen, compressLevel);	
}
示例#30
0
u32 SimpleXP3::writeIndex(FILE* f, u64* size, bool compress)
{
    SIndexHeader indexHead;
    indexHead.compressed = 0;

    //calc size of uncompressed index
    indexHead.sizeUncompressed = 0;
    for(std::list<SIndexItem>::iterator it = m_items.begin(); it != m_items.end(); it++)
    {
        SIndexItem* pItem = &(*it);
        indexHead.sizeUncompressed += pItem->getSize();
    }

    //construct complete index in memory
    unique_buffer<u8> uncompressedIndex = unique_buffer<u8>(indexHead.sizeUncompressed);
    u8* ptr = uncompressedIndex.get();
    for(std::list<SIndexItem>::iterator it = m_items.begin(); it != m_items.end(); it++)
    {
        SIndexItem* pItem = &(*it);
        pItem->write(ptr);
        ptr += pItem->getSize();
    }

    if (compress)
    {
        u64 bufferSize = compressBound(indexHead.sizeUncompressed);
        unique_buffer<u8> compressedIndex = unique_buffer<u8>(bufferSize);
        s32 res = compress2(compressedIndex.get(), 
                &bufferSize, 
                uncompressedIndex.get(), 
                indexHead.sizeUncompressed, 
                ZLIB_LEVEL);
        if (res != Z_OK)
        {
            return XP3_COMPRESSERR;
        }
        else
        {
            indexHead.sizeCompressed = bufferSize;
            indexHead.compressed = 1;
            indexHead.write(f);
            WRITE_ARRAY_DYN(f,compressedIndex.get(),indexHead.sizeCompressed);
        }
    }
    else
    {
        indexHead.sizeCompressed = indexHead.sizeUncompressed;
        indexHead.compressed = 0;
        indexHead.write(f);
        WRITE_ARRAY_DYN(f,uncompressedIndex.get(),indexHead.sizeCompressed);
    }

    if (size)
        *size = indexHead.sizeCompressed;

    return XP3_OK;
}