+descriptorMap eventData::descriptors;
+__u8 eventData::data[4108];
+extern const uint32_t crc32_table[256];
+
+eventData::eventData(const eit_event_struct* e, int size, int type)
+ :ByteSize(size&0xFF), type(type&0xFF)
+{
+ if (!e)
+ return;
+
+ __u32 descr[65];
+ __u32 *pdescr=descr;
+
+ __u8 *data = (__u8*)e;
+ int ptr=10;
+ int descriptors_length = (data[ptr++]&0x0F) << 8;
+ descriptors_length |= data[ptr++];
+ while ( descriptors_length > 0 )
+ {
+ __u8 *descr = data+ptr;
+ int descr_len = descr[1]+2;
+
+ __u32 crc = 0;
+ int cnt=0;
+ while(cnt++ < descr_len)
+ crc = (crc << 8) ^ crc32_table[((crc >> 24) ^ data[ptr++]) & 0xFF];
+
+ descriptorMap::iterator it =
+ descriptors.find(crc);
+ if ( it == descriptors.end() )
+ {
+ CacheSize+=descr_len;
+ __u8 *d = new __u8[descr_len];
+ memcpy(d, descr, descr_len);
+ descriptors[crc] = descriptorPair(1, d);
+ }
+ else
+ ++it->second.first;
+
+ *pdescr++=crc;
+ descriptors_length -= descr_len;
+ }
+ ByteSize = 12+((pdescr-descr)*4);
+ EITdata = new __u8[ByteSize];
+ CacheSize+=ByteSize;
+ memcpy(EITdata, (__u8*) e, 12);
+ memcpy(EITdata+12, descr, ByteSize-12);
+}
+
+const eit_event_struct* eventData::get() const
+{
+ int pos = 12;
+ int tmp = ByteSize-12;
+ memcpy(data, EITdata, 12);
+ __u32 *p = (__u32*)(EITdata+12);
+ while(tmp>0)
+ {
+ descriptorMap::iterator it =
+ descriptors.find(*p++);
+ if ( it != descriptors.end() )
+ {
+ int b = it->second.second[1]+2;
+ memcpy(data+pos, it->second.second, b );
+ pos += b;
+ }
+ tmp-=4;
+ }
+
+ return (const eit_event_struct*)data;
+}
+
+eventData::~eventData()
+{
+ if ( ByteSize )
+ {
+ CacheSize-=ByteSize;
+ ByteSize-=12;
+ __u32 *d = (__u32*)(EITdata+12);
+ while(ByteSize)
+ {
+ descriptorMap::iterator it =
+ descriptors.find(*d++);
+ if ( it != descriptors.end() )
+ {
+ descriptorPair &p = it->second;
+ if (!--p.first) // no more used descriptor
+ {
+ CacheSize -= it->second.second[1];
+ delete [] it->second.second; // free descriptor memory
+ descriptors.erase(it); // remove entry from descriptor map
+ }
+ }
+ ByteSize-=4;
+ }
+ delete [] EITdata;
+ }
+}
+
+void eventData::load(FILE *f)
+{
+ int size=0;
+ int id=0;
+ __u8 header[2];
+ descriptorPair p;
+ fread(&size, sizeof(int), 1, f);
+ while(size)
+ {
+ fread(&id, sizeof(__u32), 1, f);
+ fread(&p.first, sizeof(int), 1, f);
+ fread(header, 2, 1, f);
+ int bytes = header[1]+2;
+ p.second = new __u8[bytes];
+ p.second[0] = header[0];
+ p.second[1] = header[1];
+ fread(p.second+2, bytes-2, 1, f);
+ descriptors[id]=p;
+ --size;
+ CacheSize+=bytes;
+ }
+}
+
+void eventData::save(FILE *f)
+{
+ int size=descriptors.size();
+ descriptorMap::iterator it(descriptors.begin());
+ fwrite(&size, sizeof(int), 1, f);
+ while(size)
+ {
+ fwrite(&it->first, sizeof(__u32), 1, f);
+ fwrite(&it->second.first, sizeof(int), 1, f);
+ fwrite(it->second.second, it->second.second[1]+2, 1, f);
+ ++it;
+ --size;
+ }
+}