void CL_ZipFileHeader::load(CL_IODevice &input)
{
	signature = input.read_int32();
	if (signature != 0x02014b50)
	{
		throw CL_Exception("Incorrect File Header signature");
	}
	version_made_by = input.read_int16();
	version_needed_to_extract = input.read_int16();
	general_purpose_bit_flag = input.read_int16();
	compression_method = input.read_int16();
	last_mod_file_time = input.read_int16();
	last_mod_file_date = input.read_int16();
	crc32 = input.read_uint32();
	compressed_size = input.read_int32();
	uncompressed_size = input.read_int32();
	file_name_length = input.read_int16();
	extra_field_length = input.read_int16();
	file_comment_length = input.read_int16();
	disk_number_start = input.read_int16();
	internal_file_attributes = input.read_int16();
	external_file_attributes = input.read_int32();
	relative_offset_of_local_header = input.read_int32();
	filename.resize(file_name_length);

	char *str1 = new char[file_name_length];
	char *str2 = new char[extra_field_length];
	char *str3 = new char[file_comment_length];
	try
	{
		input.read(str1, file_name_length);
		input.read(str2, extra_field_length);
		input.read(str3, file_comment_length);
		if (general_purpose_bit_flag & CL_ZIP_USE_UTF8)
		{
			filename = CL_StringHelp::utf8_to_text(CL_StringRef8(str1, file_name_length, false));
			file_comment = CL_StringHelp::utf8_to_text(CL_StringRef8(str3, file_comment_length, false));
		}
		else
		{
			filename = CL_StringHelp::cp437_to_text(CL_StringRef8(str1, file_name_length, false));
			file_comment = CL_StringHelp::cp437_to_text(CL_StringRef8(str3, file_comment_length, false));
		}

		extra_field = CL_DataBuffer(str2, extra_field_length);

		delete[] str1;
		delete[] str2;
		delete[] str3;
	}
	catch (...)
	{
		delete[] str1;
		delete[] str2;
		delete[] str3;
		throw;
	}
}
void CL_SoundProvider_Wave_Impl::load(CL_IODevice &source)
{
	source.set_little_endian_mode();

	char chunk_id[4];
	source.read(chunk_id, 4);
	if (memcmp(chunk_id, "RIFF", 4))
		throw CL_Exception("Expected RIFF header!");
	cl_ubyte32 chunk_size = source.read_uint32();

	char format_id[4];
	source.read(format_id, 4);
	if (memcmp(format_id, "WAVE", 4))
		throw CL_Exception("Expected WAVE header!");

	cl_ubyte32 subchunk_pos = source.get_position();
	cl_ubyte32 subchunk1_size = find_subchunk("fmt ", source, subchunk_pos, chunk_size);

	cl_ubyte16 audio_format = source.read_uint16();
	num_channels = source.read_uint16();
	frequency = source.read_uint32();
	cl_ubyte32 byte_rate = source.read_uint32();
	cl_ubyte16 block_align = source.read_uint16();
	cl_ubyte16 bits_per_sample = source.read_uint16();

	if (bits_per_sample == 16)
		format = sf_16bit_signed;
	else if (bits_per_sample == 8)
		format = sf_8bit_unsigned;
	else
		throw CL_Exception("Unsupported wave sample format");

	cl_ubyte32 subchunk2_size = find_subchunk("data", source, subchunk_pos, chunk_size);

	data = new char[subchunk2_size];
	source.read(data, subchunk2_size);

	num_samples = subchunk2_size / block_align;
}
unsigned int CL_SoundProvider_Wave_Impl::find_subchunk(const char *chunk, CL_IODevice &source, unsigned int file_offset, unsigned int max_offset )
{
	char subchunk1_id[4];

	max_offset -= 8;	// Each subchunk must contains at least name and size
	while(file_offset < max_offset)
	{
		source.seek(file_offset);
		source.read(subchunk1_id, 4);
		cl_ubyte32 subchunk1_size = source.read_uint32();
		if (!memcmp(subchunk1_id, chunk, 4))
		{
			// Found chunk
			return subchunk1_size;
		}
		file_offset += subchunk1_size + 8;
	}
	throw CL_Exception("Block not found!");

}