示例#1
0
static int lmz_compress(lua_State* L)
{
  int level, ret;
  size_t in_len, out_len;
  const unsigned char *inb;
  unsigned char *outb;
  in_len = 0;
  inb = (const unsigned char *)luaL_checklstring(L, 1, &in_len);
  level = luaL_optint(L, 1, MZ_DEFAULT_COMPRESSION);
  if (level < MZ_DEFAULT_COMPRESSION || level > MZ_BEST_COMPRESSION) {
    luaL_error(L, "Compression level must be between -1 and 9");
  }

  out_len = mz_compressBound(in_len);
  outb = malloc(out_len);
  ret = mz_compress2(outb, &out_len, inb, in_len, level);

  switch (ret) {
    case MZ_OK:
      lua_pushlstring(L, (const char*)outb, out_len);
      ret = 1;
      break;
    default:
      lua_pushnil(L);
      lua_pushstring(L, mz_error(ret));
      ret = 2;
      break;
  }
  free(outb);
  return ret;
}
示例#2
0
void Message::compress() {
	if (isCompressed())
		return;
#ifdef BUILD_WITH_COMPRESSION_MINIZ

	mz_ulong compressedSize = mz_compressBound(_size);
	int cmp_status;
	uint8_t *pCmp;

	pCmp = (mz_uint8 *)malloc((size_t)compressedSize);

	// last argument is speed size tradeoff: BEST_SPEED [0-9] BEST_COMPRESSION
	cmp_status = mz_compress2(pCmp, &compressedSize, (const unsigned char *)_data.get(), _size, 5);
	if (cmp_status != Z_OK) {
		// error
		free(pCmp);
	}

	_data = SharedPtr<char>((char*)pCmp);
	_meta["um.compressed"] = toStr(_size);
	_size = compressedSize;

#elif defined(BUILD_WITH_COMPRESSION_FASTLZ)

	// The minimum input buffer size is 16.
	if (_size < 16)
		return;

	// The output buffer must be at least 5% larger than the input buffer and can not be smaller than 66 bytes.
	int compressedSize = _size + (double)_size * 0.06;
	if (compressedSize < 66)
		compressedSize = 66;

	char* compressedData = (char*)malloc(compressedSize);
	compressedSize = fastlz_compress(_data.get(), _size, compressedData);

	// If the input is not compressible, the return value might be larger than length
	if (compressedSize > _size) {
		free(compressedData);
		return;
	}

//	std::cout << _size << " -> " << compressedSize << " = " << ((float)compressedSize / (float)_size) << std::endl;

	_data = SharedPtr<char>((char*)compressedData);
	_meta["um.compressed"] = toStr(_size);
	_size = compressedSize;

#endif

}
示例#3
0
文件: deflate.c 项目: bdobe/packages
static int l_compress(lua_State *L) {
	size_t in_length;
	const unsigned char *in = (unsigned char *)luaL_checklstring(L, 1, &in_length);

	mz_ulong out_length = mz_compressBound(in_length);
	unsigned char *out = malloc(out_length);

	int status = mz_compress(out, &out_length, in, in_length);
	if (status) {
		free(out);

		lua_pushnil(L);
		lua_pushstring(L, mz_error(status));
		return 2;
	}

	lua_pushlstring(L, (const char*)out, out_length);
	free(out);

	return 1;
}
示例#4
0
void Message::compress(Message::Compression type, int level) {
	if (isCompressed())
		return;

	switch (type) {
#ifdef BUILD_WITH_COMPRESSION_MINIZ
	case COMPRESS_MINIZ: {
		mz_ulong compressedSize = mz_compressBound(_size);
		int cmp_status;
		uint8_t *pCmp;

		pCmp = (mz_uint8 *)malloc((size_t)compressedSize);
		level = (level >= 0 ? level : BUILD_WITH_COMPRESSION_LEVEL_MINIZ);

		// last argument is speed size tradeoff: BEST_SPEED [0-9] BEST_COMPRESSION
		cmp_status = mz_compress2(pCmp, &compressedSize, (const unsigned char *)_data.get(), _size, level);
		if (cmp_status != Z_OK) {
			// error
			free(pCmp);
		}

		_data = SharedPtr<char>((char*)pCmp);
		_meta["um.compressed"] = toStr(_size) + ":miniz";
		_size = compressedSize;
	}
	return;
#endif
#ifdef BUILD_WITH_COMPRESSION_FASTLZ
	case COMPRESS_FASTLZ: {
		// The minimum input buffer size is 16.
		if (_size < 16)
			return;

		// The output buffer must be at least 5% larger than the input buffer and can not be smaller than 66 bytes.
		int compressedSize = _size + (double)_size * 0.06;
		if (compressedSize < 66)
			compressedSize = 66;

		char* compressedData = (char*)malloc(compressedSize);
		compressedSize = fastlz_compress(_data.get(), _size, compressedData);

		// If the input is not compressible, the return value might be larger than length
		if (compressedSize > _size) {
			free(compressedData);
			return;
		}

		//	std::cout << _size << " -> " << compressedSize << " = " << ((float)compressedSize / (float)_size) << std::endl;

		_data = SharedPtr<char>((char*)compressedData);
		_meta["um.compressed"] = toStr(_size) + ":fastlz";
		_size = compressedSize;
	}
	return;
#endif
#ifdef BUILD_WITH_COMPRESSION_LZ4
	case COMPRESS_LZ4: {
		level = (level >= 0 ? level : BUILD_WITH_COMPRESSION_LEVEL_LZ4);

#ifdef LZ4_FRAME
		LZ4F_preferences_t lz4_preferences = {
			{ LZ4F_max256KB, LZ4F_blockLinked, LZ4F_noContentChecksum, LZ4F_frame, 0, { 0, 0 } },
			level,   /* compression level */
			0,   /* autoflush */
			{ 0, 0, 0, 0 },  /* reserved, must be set to 0 */
		};

		LZ4F_errorCode_t r;
		LZ4F_compressionContext_t ctx;

		r = LZ4F_createCompressionContext(&ctx, LZ4F_VERSION);
		if (LZ4F_isError(r)) {
			//        printf("Failed to create context: error %zu", r);
			return;
		}

#define LZ4_HEADER_SIZE 19
#define LZ4_FOOTER_SIZE 4
		size_t n, offset = 0;


		size_t frameSize = LZ4F_compressBound(_size, &lz4_preferences);
		size_t compressedSize = frameSize + LZ4_HEADER_SIZE + LZ4_FOOTER_SIZE;
		char* compressedData = (char*)malloc(compressedSize);

		n = LZ4F_compressBegin(ctx, compressedData, compressedSize, &lz4_preferences);
		if (LZ4F_isError(n)) {
			//        printf("Failed to start compression: error %zu", n);
			LZ4F_freeCompressionContext(ctx);
			free(compressedData);
			return;
		}
		offset += n;

		n = LZ4F_compressUpdate(ctx, compressedData + offset, compressedSize - offset, _data.get(), _size, NULL);
		if (LZ4F_isError(n)) {
			//        printf("Compression failed: error %zu", n);
			LZ4F_freeCompressionContext(ctx);
			free(compressedData);
			return;
		}
		offset += n;

		n = LZ4F_compressEnd(ctx, compressedData + offset, compressedSize - offset, NULL);
		if (LZ4F_isError(n)) {
			//        printf("Failed to end compression: error %zu", n);
			LZ4F_freeCompressionContext(ctx);
			free(compressedData);
			return;
		}
		offset += n;

		_data = SharedPtr<char>((char*)compressedData);
		_meta["um.compressed"] = toStr(_size) + ":lz4";
		_size = offset;

		LZ4F_freeCompressionContext(ctx);
#else
		size_t compressedSize = LZ4_compressBound(_size);
		char* compressedData = (char*)malloc(compressedSize);
		int actualSize = 0;

		actualSize = LZ4_compress_fast(_data.get(), compressedData, _size, compressedSize, level);
		if (actualSize == 0) {
			free(compressedData);
			return;
		}

		_data = SharedPtr<char>((char*)compressedData);
		_meta["um.compressed"] = toStr(_size) + ":lz4";
		_size = actualSize;
#endif

	}
	return;
#endif
	default:
		break;
	}
}