예제 #1
0
block buffer_manager::read(string table_name, FILE_TYPE t, int offset)
{
	for (int i = 0; i < BUFFER_SIZE; i++)
	{
		if (m_block_age[i] >= 0)
		{
			if (block_equal(i, table_name, t, offset) == true)
			{
				age_plus_1();
				m_block_age[i] = 0;
				return m_block[i];
			}
		}
	}
	block b;
	FILE* fp = NULL;
	if (t == FILE_TYPE::RECORD)
		fp = fopen((table_name + ".blo").c_str(), "rb");
	else if (t == FILE_TYPE::INDEX)
		fp = fopen((table_name + ".ind").c_str(), "rb");
	if (fp == NULL)
	{
		error e(1, "", "", "");
		throw e;
	}
	fseek(fp, offset * 4096, SEEK_SET);
	fread(b.m_data, sizeof(BYTE), 4096, fp);
	fclose(fp);
	b.m_offset = offset;
	b.m_table_name = table_name;
	b.m_file_type = t;

	refresh_block_to_buffer(b);
	return b;
}
예제 #2
0
파일: generic_gmac.c 프로젝트: corny/fastd
/** Verifies and decrypts a packet */
static bool method_decrypt(fastd_peer_t *peer, fastd_method_session_state_t *session, fastd_buffer_t *out, fastd_buffer_t in, bool *reordered) {
	if (in.len < COMMON_HEADBYTES+sizeof(fastd_block128_t))
		return false;

	if (!method_session_is_valid(session))
		return false;

	uint8_t in_nonce[COMMON_NONCEBYTES];
	uint8_t flags;
	int64_t age;
	if (!fastd_method_handle_common_header(&session->common, &in, in_nonce, &flags, &age))
		return false;

	if (flags)
		return false;

	uint8_t nonce[session->method->cipher_info->iv_length] __attribute__((aligned(8)));
	fastd_method_expand_nonce(nonce, in_nonce, sizeof(nonce));

	size_t tail_len = alignto(in.len, sizeof(fastd_block128_t))-in.len;
	*out = fastd_buffer_alloc(in.len, 0, tail_len);

	int n_blocks = block_count(in.len, sizeof(fastd_block128_t));

	fastd_block128_t *inblocks = in.data;
	fastd_block128_t *outblocks = out->data;
	fastd_block128_t tag;

	bool ok = session->cipher->crypt(session->cipher_state, outblocks, inblocks, n_blocks*sizeof(fastd_block128_t), nonce);

	if (ok) {
		if (tail_len)
			memset(in.data+in.len, 0, tail_len);

		put_size(&inblocks[n_blocks], in.len-sizeof(fastd_block128_t));

		ok = session->ghash->digest(session->ghash_state, &tag, inblocks+1, n_blocks*sizeof(fastd_block128_t));
	}

	if (!ok || !block_equal(&tag, &outblocks[0])) {
		fastd_buffer_free(*out);
		return false;
	}

	fastd_buffer_free(in);

	fastd_buffer_push_head(out, sizeof(fastd_block128_t));

	fastd_tristate_t reorder_check = fastd_method_reorder_check(peer, &session->common, in_nonce, age);
	if (reorder_check.set) {
		*reordered = reorder_check.state;
	}
	else {
		fastd_buffer_free(*out);
		*out = fastd_buffer_alloc(0, 0, 0);
	}

	return true;
}
예제 #3
0
void buffer_manager::refresh_block_to_buffer(block b)
{
	for (int i = 0; i < BUFFER_SIZE; i++)
	{
		if (m_block_age[i] >= 0)
		{
			if (block_equal(i, b.m_table_name, b.m_file_type, b.m_offset) == true)
			{
				age_plus_1();
				m_block_age[i] = 0;
				m_block[i] = b;
				return;
			}
		}
	}
	for (int i = 0; i < BUFFER_SIZE; i++)
	{
		if (m_block_age[i] < 0)
		{
			age_plus_1();
			m_block_age[i] = 0;
			m_block[i] = b;
			return;
		}
	}

	int max_age = 0;
	int max_index = 0;
	for (int i = 0; i < BUFFER_SIZE; i++)
	{
		if (m_block_age[i]>max_age)
		{
			max_age = m_block_age[i];
			max_index = i;
		}
	}

	FILE* fp = NULL;
	if (m_block[max_index].m_file_type == FILE_TYPE::RECORD)
		fp = fopen((m_block[max_index].m_table_name + ".blo").c_str(), "rb+");
	else if (m_block[max_index].m_file_type == FILE_TYPE::INDEX)
		fp = fopen((m_block[max_index].m_table_name + ".ind").c_str(), "rb+");
	if (fp == NULL)
	{
		error e;
		throw e;
	}
	fseek(fp, m_block[max_index].m_offset * 4096, SEEK_SET);
	fwrite(m_block[max_index].m_data, sizeof(BYTE), 4096, fp);
	fclose(fp);

	age_plus_1();
	m_block_age[max_index] = 0;
	m_block[max_index] = b;
	return;

}
예제 #4
0
void chunk_block_set(Chunk *chunk, Vector3i *pos, Block *block)
{
  int index = pos->x+(pos->y*16)+(pos->z*256);
  chunk->changed = !block_equal(chunk->blocks[index], block);
  chunk->blocks[index] = block;

  if(block == NULL) {
    chunk->empty = true;
    for(int i=0;i<4096;++i) {
      if(chunk->blocks[i] != NULL) {
	chunk->empty = false;
	break;
      }
    }
  } else {
    chunk->empty = false;
  }
}