Пример #1
0
void opentv_read_summaries (unsigned char *data, unsigned int length, bool huffman_debug)
{
	if (length < 20) return;
	
	unsigned short int channel_id	= (data[3] << 8) | data[4];
	unsigned short int mjd_time	    = (data[8] << 8) | data[9];
	
	if ((channel_id > 0) && (mjd_time > 0))
	{
		unsigned int offset = 10;
		
		while (offset + 4 < length)
		{
			unsigned short int	event_id;
			int					packet_length = ((data[offset + 2] & 0x0f) << 8) | data[offset + 3];
			int					packet_length2 = packet_length;
			char				buffer[MAX_SUMMARIE_SIZE];
			unsigned short int	buffer_size = 0;
			unsigned int offset2;

			if (packet_length == 0) break;

			event_id = (data[offset] << 8) | data[offset + 1];
			offset += 4;
			offset2 = offset;
			while (packet_length2 > 0)
			{
				unsigned char descriptor_tag = data[offset2];
				unsigned char descriptor_length = data[offset2 + 1];

				offset2 += 2;

				if (descriptor_tag == 0xb9 &&
					MAX_SUMMARIE_SIZE > buffer_size + descriptor_length &&
					offset2 + descriptor_length < length)
				{
					memcpy(&buffer[buffer_size], &data[offset2], descriptor_length);
					buffer_size += descriptor_length;
				}

				packet_length2 -= descriptor_length + 2;
				offset2 += descriptor_length;
			}

			offset += packet_length;

			if (buffer_size > 0 && channels[channel_id] != NULL)
			{
				epgdb_title_t *title = epgdb_titles_get_by_id_and_mjd (channels[channel_id], event_id, mjd_time);
				if (title != NULL)
				{
					char tmp[MAX_SUMMARIE_SIZE * 2];
					if (!huffman_decode (buffer, buffer_size, tmp, MAX_SUMMARIE_SIZE * 2, huffman_debug))
						tmp[0] = '\0';
					
					if (huffman_debug)
					{
						char mtime[20];
						struct tm *loctime = localtime ((time_t*)&title->start_time);
						printf ("Nid: %x Tsid: %x Sid: %x\n", channels[channel_id]->nid, channels[channel_id]->tsid, channels[channel_id]->sid);
						strftime (mtime, 20, "%d/%m/%Y %H:%M", loctime);
						printf ("Start time: %s\n", mtime);
					}
					
					epgdb_titles_set_long_description (title, tmp);
				}
			}
		}
	}
}
bool dbmerge_merge (FILE *fd_h, FILE *fd_d, void(*progress_callback)(int, int))
{
	char tmp[256];
	unsigned char revision;
	int channels_count, i, j, aliases_groups_count, indexes_count;
	time_t now = time (NULL);

	/* read headers */
	fread (tmp, strlen (MAGIC_HEADERS), 1, fd_h);
	if (memcmp (tmp, MAGIC_HEADERS, strlen (MAGIC_HEADERS)) != 0)
	{
		log_add ("Bad magic header");
		return false;
	}
	fread (&revision, sizeof (unsigned char), 1, fd_h);
	if (revision != DB_REVISION)
	{
		log_add ("Bad db revision");
		return false;
	}

	fseek (fd_h, 22, SEEK_SET);

	fread (&channels_count, sizeof (int), 1, fd_h);
	for (i=0; i<channels_count; i++)
	{
		int titles_count;
		epgdb_channel_t *tmp;
		epgdb_channel_t *channel, *channel_tmp;

		channel_tmp = _malloc (sizeof (epgdb_channel_t));
		fread (channel_tmp, sizeof (epgdb_channel_header_t), 1, fd_h);
		channel = epgdb_channels_add (channel_tmp->nid, channel_tmp->tsid, channel_tmp->sid);
		_free (channel_tmp);

		fread (&titles_count, sizeof (int), 1, fd_h);

		for (j=0; j<titles_count; j++)
		{
			char *desc, *ldesc;
			epgdb_title_t *title = _malloc (sizeof (epgdb_title_t));
			fread (title, sizeof (epgdb_title_header_t), 1, fd_h);

			desc = dbmerge_read_description (fd_d, title);
			ldesc = dbmerge_read_long_description (fd_d, title);

			title = epgdb_titles_add (channel, title);

			epgdb_titles_set_description(title, desc);
			epgdb_titles_set_long_description(title, ldesc);

			_free (desc);
			_free (ldesc);
		}

		if (progress_callback)
			progress_callback (i, channels_count);
	}


	return true;
}