Listing 1
struct Mgt { bitset<8> tableId; bitset<1> sectionSyntaxIndicator; bitset<1> privateIndicator; bitset<2> reserved2a; bitset<12> sectionLength; bitset<16> tableIdExtention; bitset<2> reserved2b; bitset<5> versionNumber; bitset<1> currentNextIndicator; bitset<8> sectionNumber; bitset<8> lastSectionNumber; bitset<8> protocolVersion; bitset<16> tablesDefined; list< MgtDefinedTable > tables; bitset<12> descriptorsLength; list< SIDescriptor > descriptors; bitset<4> reserved4; } mgt; ibstream& operator>>(ibstream& source, Mgt& mgt) { source >> mgt.tableId; source >> mgt.sectionSyntaxIndicator; source >> mgt.privateIndicator; source >> mgt.reserved2a; source >> mgt.sectionLength; source >> mgt.tableIdExtention; source >> mgt.reserved2b; source >> mgt.versionNumber; source >> mgt.currentNextIndicator; source >> mgt.sectionNumber; source >> mgt.lastSectionNumber; source >> mgt.protocolVersion; source >> mgt.tablesDefined; mgt.tables.clear(); for (size_t i = 0; i < mgt.tablesDefined.to_ulong(); ++i) { MgtDefinedTable table; mgt.tables.push_back(table); source >> mgt.tables.back(); } assert(mgt.tablesDefined.to_ulong() == mgt.tables.size()); source.SkipBits(4); source >> mgt.descriptorsLength; for (size_t bytesToGo=mgt.descriptorsLength.to_ulong(); bytesToGo > 2; ) { SIDescriptor d; mgt.descriptors.push_back(d); source >> mgt.descriptors.back(); bytesToGo -= min(bytesToGo, (1 + 1 + mgt.descriptors.back().descriptorLength.to_ulong())); } return source ; } istrstream istr(data, ios::binary); ibstream ibs(istr.rdbuf()); ibs >> mgt;