示例#1
0
文件: lbuffer.c 项目: nan1888/lask
/*
** c, w, i = reader:getlist("cwi")
*/
static int lbuffer_getlist(lua_State *L)
{
	Buffer *buf = buffer_lcheck(L, 1);
	const char *types = luaL_checkstring(L, 2);
	const char *p = types;
	bool empty = false;
	bool be = buf->be;
	
	while (*p != 0 && !empty) {
		char type = *p;
		switch (type) {
		case 'c': {
				if (buf->datasiz < 1)  {
					empty = true;
				} else {
					lua_pushinteger(L, (int)(uint32)buf->data[0]);
					buffer_shift(buf, 1);
				}
				break;
			}
		case 'w': {
				if (buf->datasiz < 2)  {
					empty = true;
				} else {
					const uint8 *p = buf->data;
					lua_pushinteger(L, be ? (int)(bytes_to_uint16_be(p)) : (int)(bytes_to_uint16_le(p)));
					buffer_shift(buf, 2);
				}
				break;
			}
		case 'i': {
				if (buf->datasiz < 4)  {
					empty = true;
				} else {
					const uint8 *p = buf->data;
					lua_pushinteger(L, be ? (int)(bytes_to_uint32_be(p)) : (int)(bytes_to_uint32_le(p)));
					buffer_shift(buf, 4);
				}
				break;
			}
		case 'u': {
				if (buf->datasiz < 4) {
					empty = true;
				} else {
					const uint8 *p = buf->data;
					lua_pushnumber(L, (lua_Number)(be ? bytes_to_uint32_be(p) : bytes_to_uint32_le(p)));
				}
				break;
			}
		default:
			break;
		}
		if (!empty)
			p++;
	}
	return (int)(p - types);
}
示例#2
0
文件: lbuffer.c 项目: nan1888/lask
/*
** u1, u2, .. uN = reader:getu(N = 1)
**
** read n unsigned 32-bits integers
*/
static int lbuffer_getu(lua_State *L)
{
	Buffer *buf = buffer_lcheck(L, 1);
	size_t num = (size_t)luaL_optint(L, 2, 1);
	bool be = buf->be;
	const uint8 *p = buf->data;
	
	if (num * 4 > buf->datasiz)
		num = buf->datasiz / 4;
	
	for (size_t i = 0; i < num; i++) {
		lua_pushnumber(L, be ? bytes_to_uint32_be(p) : bytes_to_uint32_le(p));
		p += 4;
	}
	buffer_shift(buf, 4 * num);
	return (int)num;
}
示例#3
0
bool sound_archive::load_from_th_file(const uint8_t* pData, size_t iDataLength)
{
    if(iDataLength < sizeof(uint32_t) + sizeof(sound_dat_file_header))
        return false;

    uint32_t iHeaderPosition = bytes_to_uint32_le(pData + iDataLength - sizeof(uint32_t));

    if(static_cast<size_t>(iHeaderPosition) >= iDataLength - sizeof(sound_dat_file_header))
        return false;

    header = *reinterpret_cast<const sound_dat_file_header*>(pData + iHeaderPosition);

    delete[] data;
    data = new (std::nothrow) uint8_t[iDataLength];
    if(data == nullptr)
        return false;
    std::memcpy(data, pData, iDataLength);

    sound_files = reinterpret_cast<sound_dat_sound_info*>(data + header.table_position);
    sound_file_count = header.table_length / sizeof(sound_dat_sound_info);
    return true;
}