Пример #1
0
TEST tm_deflate_test()
{
	uint8_t out[1024*16] = { 0 };
	size_t out_total = 0;
	size_t out_len = sizeof(out), out_written = 0, in_read = 0;

	uint8_t in[1024*16] = { 0 };
	size_t in_total = 0;

	{
		tm_deflate_t deflator;
		tm_deflate_alloc(&deflator);

		ASSERT_EQ(tm_deflate_start(deflator, TM_GZIP, 1), 0);

		ASSERT_EQ(tm_deflate_write(deflator, helloworld, helloworld_len, &in_read, &out[out_total], out_len - out_total, &out_written), 0);
		out_total += out_written;
		ASSERT_EQ(out[0], 0x1F); // magic
		ASSERT_EQ(out[1], 0x8b); // magic
		ASSERT_EQ(in_read, helloworld_len);

		ASSERT_EQ(tm_deflate_end(deflator, &out[out_total], out_len - out_total, &out_written), 0);
		out_total += out_written;

		// Check compiled version.
		ASSERT_BUF_N_EQ(hello_gz, out, hello_gz_len);
	}

	{
		size_t in_len = sizeof(in), out_read = 0, in_written = 0;

		tm_inflate_t inflator;
		tm_inflate_alloc(&inflator);

		ASSERT_EQ(tm_inflate_start(inflator, TM_GZIP), 0);

		out_len = out_total;
		for (size_t offset = 0; offset < out_len; ) {
			size_t in_chunk_size = out_len - offset < 10 ? out_len - offset : 10;
			out_read = 0;
			int status = tm_inflate_write(inflator, &out[out_read + offset], in_chunk_size, &out_read, &in[in_total], in_len - in_total, &in_written);
			ASSERT_EQ(status, 0);
			ASSERT_FALSE(out_read == 0 && in_written == 0);

			offset += out_read;
			in_total += in_written;
		}

		ASSERT_EQ(tm_inflate_end(inflator, &in[in_total], in_len - in_total, &in_written), 0);
		in_total += in_written;

		// Compares result to "hello world"
		ASSERT_BUF_N_EQ(in, helloworld, in_total);
	}

	PASS();
}
Пример #2
0
static int l_tm_deflate_start (lua_State *L)
{
  uint8_t type = (uint8_t) lua_tonumber(L, 1);
  size_t level = (size_t) lua_tonumber(L, 2);

  tm_deflate_t deflate = (tm_deflate_t) lua_newuserdata(L, tm_deflate_alloc_size());

  // Need minimum 32kb dictionary size
  size_t out_len = 64*1024, out_total = 0;
  colony_createbuffer(L, out_len);

  int status = tm_deflate_start(deflate, type, level);
  lua_pushnumber(L, out_total);

  lua_pushnumber(L, status);
  return 4;
}