Пример #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_end (lua_State *L)
{
  tm_deflate_t deflate = (tm_deflate_t) lua_touserdata(L, 1);
  size_t out_len = 0;
  uint8_t* out = colony_tobuffer(L, 2, &out_len);
  size_t out_total = (size_t) lua_tonumber(L, 3);

  // TODO check for < half of buffer available

  size_t out_written = 0;
  int status = tm_deflate_end(deflate, &out[out_total], out_len - out_total, &out_written);

  lua_pushvalue(L, 2);
  lua_pushnumber(L, out_total + out_written);

  lua_pushnumber(L, status);
  return 3;
}