uint32 
TileCollectionNotice::load( SharedBuffer& buf )
{
   // Load the m_indexByLayerID map.
   uint32 size = buf.readNextBAShort();
   {for ( uint32 i = 0; i < size; ++i ) {
      // Layer ID.
      int layerID = buf.readNextBAShort();

      // Index.
      int index = buf.readNextBAShort();
     
      m_indexByLayerID[ layerID ] = index;
   }}
  
   // Size of m_tilesForAllDetails.
   size = buf.readNextBAShort();

   m_tilesForAllDetails.reserve( size );

   // Load all the TilesForAllDetailsNotices. 
   {for ( uint32 i = 0; i < size; ++i ) {
      TilesForAllDetailsNotice notice;
      notice.load( buf );
      m_tilesForAllDetails.push_back( notice );
   }}

   return buf.getCurrentOffset();
}
uint32 
TileCollectionNotice::save( SharedBuffer& buf ) const
{
   // Store the m_indexByLayerID map.
   buf.writeNextBAShort( m_indexByLayerID.size() );
   for ( map<int,int>::const_iterator it = m_indexByLayerID.begin();
         it != m_indexByLayerID.end(); ++it ) {

      // Layer ID.
      buf.writeNextBAShort( it->first );

      // Index.
      buf.writeNextBAShort( it->second );
   }
  
   // Size of m_tilesForAllDetails.
   buf.writeNextBAShort( m_tilesForAllDetails.size() );

   // Store all the TilesForAllDetailsNotice. 
   
   for ( uint32 i = 0; i < m_tilesForAllDetails.size(); ++i ) {
      m_tilesForAllDetails[ i ].save( buf );
   }

   return buf.getCurrentOffset();
}
void
SFDLoadableHeader::loadInitialHeader( SharedBuffer& buf )
{
   // Load initial header.
   const char* str = buf.readNextString();
   if ( strcmp( str, "storkafinger" ) != 0 ) {
      m_state = failed_to_load;
      m_nbrBytesToRead = 0;
      innerLoad();
      return;
   }

   // Version
   m_version = buf.readNextBAByte();
  
   // Encryption type.
   m_encryptionType = encryption_t( buf.readNextBAByte() );

   // Set the right xorbuffer depending on the encryption type.
   switch ( m_encryptionType ) {
      case ( no_encryption ) :
         m_xorBuffer = NULL;
         break;
      case ( uid_encryption ) :
         MC2_ASSERT( m_uidXorBuffer != NULL );
         m_xorBuffer = m_uidXorBuffer;
         break;
      case ( warez_encryption ) :
         MC2_ASSERT( m_warezXorBuffer != NULL );
         m_xorBuffer = m_warezXorBuffer;
         break;
   }

   // Header size.
   m_headerSize = buf.readNextBALong();

   // Rest of buffer is encrypted.
   if ( m_xorBuffer != NULL ) {
      m_fileHandler->setXorHelper( XorHelper( m_xorBuffer->getBufferAddress(),
                                             m_xorBuffer->getBufferSize(),
                                             buf.getCurrentOffset() ) );
   }

   m_nbrBytesToRead = m_headerSize;
   
   // Load the rest.
   m_state = loaded_initial_header;
   innerLoad();
}