Index: code/branches/netp3/src/network/GamestateManager.cc
===================================================================
--- code/branches/netp3/src/network/GamestateManager.cc	(revision 3045)
+++ code/branches/netp3/src/network/GamestateManager.cc	(revision 3047)
@@ -132,6 +132,9 @@
     if(client){
 //       COUT(3) << "diffing" << std::endl;
+//       packet::Gamestate* gs1  = gs;
       gs = gs->diff(client);
+//       packet::Gamestate* gs2 = gs->undiff(client);
 //       gs = new packet::Gamestate(*gs);
+//       assert(*gs1==*gs2);
     }
     else{
@@ -161,4 +164,5 @@
       for(it = gamestateMap_[clientID].begin(); it!=gamestateMap_[clientID].end(); ){
         delete it->second;
+
         gamestateMap_[clientID].erase(it++);
       }
Index: code/branches/netp3/src/network/packet/Gamestate.cc
===================================================================
--- code/branches/netp3/src/network/packet/Gamestate.cc	(revision 3045)
+++ code/branches/netp3/src/network/packet/Gamestate.cc	(revision 3047)
@@ -226,14 +226,10 @@
   uint8_t *d1 = data_+GamestateHeader::getSize();
   uint8_t *d2 = gs.data_+GamestateHeader::getSize();
+  GamestateHeader* h1 = new GamestateHeader(data_);
+  GamestateHeader* h2 = new GamestateHeader(gs.data_);
+  assert(h1->getDataSize() == h2->getDataSize());
   assert(!isCompressed());
   assert(!gs.isCompressed());
-  while(d1<data_+header_->getDataSize())
-  {
-    if(*d1!=*d2)
-      return false;
-    d1++;
-    d2++;
-  }
-  return true;
+  return memcmp(d1, d2, h1->getDataSize())==0;
 }
 
@@ -339,46 +335,16 @@
   uint8_t *ndata = new uint8_t[dest_length*sizeof(uint8_t)+GamestateHeader::getSize()];
   uint8_t *dest = ndata + GamestateHeader::getSize();
-  
-  
-  // LOOP-UNROLLED DIFFING
-  uint32_t *dest32 = (uint32_t*)dest, *base32 = (uint32_t*)basep, *gs32 = (uint32_t*)gs;
-  // diff in 4-byte steps
-  while( of < (uint32_t)(header_->getDataSize())/4 ){
-    if( of < (uint32_t)(diffHeader.getDataSize())/4 )
-    {
-      *(dest32+of)=*(base32+of) ^ *(gs32+of); // do the xor
-      ++of;
-    } else
-    {
-      *(dest32+of)=*(gs32+of); // same as 0 ^ *(gs32+of)
-      ++of;
-    }
-  }
-  uint32_t base_rest = 0;
-  // fill the base_rest first with 0 and then with the remaining bytes in base32
-  switch( diffHeader.getDataSize()%sizeof(uint32_t) )
-  {
-    case 3:
-      *((uint8_t*)(&base_rest)+2) = *((uint8_t*)(base32+of-1)+2); // save the last byte to the buffer
-    case 2:
-      *((uint16_t*)(&base_rest)) = *((uint16_t*)(base32+of-1)); // xor 2 bytes at once (either 2nd and 3rd last or the 2 last bytes)
-      break;
-    case 1:
-      *((uint8_t*)(&base_rest)) = *((uint8_t*)(base32+of-1)); // xor the last byte
-    case 0:
-      break; // leave 0
-  }
-  // now diff the rest and save it to dest32 in 2- and 1-byte steps
-  switch( header_->getDataSize()%sizeof(uint32_t) )
-  {
-    case 3:
-      *((uint8_t*)(dest32+of)+2) = *((uint8_t*)&base_rest+2) ^ *((uint8_t*)(gs32+of-1)+2); // save the last byte to the buffer
-    case 2:
-      *((uint16_t*)(dest32+of)) = *((uint16_t*)&base_rest) ^ *((uint16_t*)(gs32+of-1)); // xor 2 bytes at once (either 2nd and 3rd last or the 2 last bytes)
-      break;
-    case 1:
-      *((uint8_t*)(dest32+of)) = *((uint8_t*)&base_rest) ^ *((uint8_t*)(gs32+of-1)); // xor the last byte
-    case 0:
-      break;
+  while(of < diffHeader.getDataSize() && of < header_->getDataSize()){
+    *(dest+of)=*(basep+of)^*(gs+of); // do the xor
+    ++of;
+  }
+  if(diffHeader.getDataSize()!=header_->getDataSize()){
+    uint8_t n=0;
+    if(diffHeader.getDataSize() < header_->getDataSize()){
+      while(of<dest_length){
+        *(dest+of)=n^*(gs+of);
+        of++;
+      }
+    }
   }
 
@@ -391,4 +357,56 @@
   return g;
 }
+
+// Gamestate *Gamestate::diff(Gamestate *base)
+// {
+//   assert(data_);
+//   assert(!header_->isCompressed());
+//   assert(!header_->isDiffed());
+//   GamestateHeader diffHeader(base->data_);
+//   uint8_t *basep = GAMESTATE_START(base->data_), *gs = GAMESTATE_START(this->data_);
+//   uint32_t of=0; // pointers offset
+//   uint32_t dest_length=0;
+//   dest_length=header_->getDataSize();
+//   if(dest_length==0)
+//     return NULL;
+//   uint8_t *ndata = new uint8_t[dest_length*sizeof(uint8_t)+GamestateHeader::getSize()];
+//   uint8_t *dest = ndata + GamestateHeader::getSize();
+//   
+//   
+//   // LOOP-UNROLLED DIFFING
+//   uint32_t *dest32 = (uint32_t*)dest, *base32 = (uint32_t*)basep, *gs32 = (uint32_t*)gs;
+//   // diff in 4-byte steps
+//   while( of < (uint32_t)(header_->getDataSize())/4 ){
+//     if( of < (uint32_t)(diffHeader.getDataSize())/4 )
+//     {
+//       *(dest32+of)=*(base32+of) ^ *(gs32+of); // do the xor
+//       ++of;
+//     }
+//     else
+//     {
+//       *(dest32+of)=*(gs32+of); // same as 0 ^ *(gs32+of)
+//       ++of;
+//     }
+//   }
+//   for( unsigned int of2 = 0; of2 < header_->getDataSize()%4; ++of2 )
+//   {
+//     if( of*4+of2 < diffHeader.getDataSize() )
+//     {
+//       *(dest+4*of+of2)=*(basep+4*of+of2) ^ *(gs+4*of+of2); // do the xor
+//     }
+//     else
+//     {
+//       *(dest+4*of+of2)=*(gs+4*of+of2); // same as 0 ^ *(gs32+of)
+//     }
+//   }
+// 
+//   Gamestate *g = new Gamestate(ndata, getClientID());
+//   *(g->header_) = *header_;
+//   g->header_->setDiffed( true );
+//   g->header_->setBaseID( base->getID() );
+//   g->flags_=flags_;
+//   g->packetDirection_ = packetDirection_;
+//   return g;
+// }
 
 Gamestate* Gamestate::doSelection(unsigned int clientID, unsigned int targetSize){
Index: code/branches/netp3/src/network/packet/Gamestate.h
===================================================================
--- code/branches/netp3/src/network/packet/Gamestate.h	(revision 3045)
+++ code/branches/netp3/src/network/packet/Gamestate.h	(revision 3047)
@@ -120,4 +120,5 @@
     bool compressData();
     bool decompressData();
+    bool operator ==(packet::Gamestate gs);
 
     // Packet functions
@@ -126,5 +127,4 @@
     virtual inline bool process();
 
-    bool operator ==(packet::Gamestate gs);
   private:
     uint32_t calcGamestateSize(int32_t id, uint8_t mode=0x0);
