Index: trunk/BNC/RTCM/RTCM2.cpp
===================================================================
--- trunk/BNC/RTCM/RTCM2.cpp	(revision 725)
+++ trunk/BNC/RTCM/RTCM2.cpp	(revision 728)
@@ -49,6 +49,6 @@
 //   2008/03/10  AHA  Corrected extraction of antenna serial number
 //   2008/03/10  AHA  Corrected buffer length check in getPacket()
-//   2008/03/11  AHA  Added checks for data consistency in extraction routines
 //   2008/03/11  AHA  isGPS-flag in RTCM2_Obs is now set to false on clear()
+//   2008/03/13  AHA  Added checks for data consistency in extraction routines
 //
 // (c) DLR/GSOC
@@ -296,8 +296,10 @@
    
   if (buf.size()<5) {
-    // Ignore; users should avoid this case prior to calling get()    
+    // Ignore; users should avoid this case prior to calling get()
+    
 #if ( DEBUG > 0 )    
     cerr << "Error in get(): packet too short (" << buf.size() <<")" << endl;
 #endif
+        
     return;
   };
@@ -341,10 +343,19 @@
 void ThirtyBitWord::getHeader(string& buf) {
 
-  const int wordLen = 5; // Number of bytes representing a 30-bit word
-  const int spare   = 1; // Number of spare words for resync of parity
-                         // (same value as inRTCM2packet::getPacket()) 
+  const unsigned int wordLen = 5; // Number of bytes representing a 30-bit word
+  const unsigned int spare   = 1; // Number of spare words for resync of parity
+                                  // (same value as inRTCM2packet::getPacket()) 
   unsigned int i;
   
   i=0;
+  // append spare word (to get correct parity) and first consecutive word  
+  while (i<(spare+1)*wordLen) {
+    // Process byte
+    append(buf[i]);
+    // Increment count
+    i++;
+  };
+  
+  // start searching for preamble in first word after spare word
   while (!isHeader() && i<buf.size() ) {
     // Process byte
@@ -445,7 +456,13 @@
   unsigned int n;
   
+  // Does the package content at least spare bytes and first header byte?
+  if (buf.size()<(spare+1)*wordLen) { 
+      clear();       
+      return;
+  };
+    
   // Try to read a full packet. Processed bytes are removed from the input 
   // buffer except for the latest spare*wordLen bytes to restore the parity 
-  // bytes upon subseqeunt calls of getPAcket().
+  // bytes upon subseqeunt calls of getPacket().
   
   // Locate and read the first header word
@@ -456,7 +473,9 @@
     // termination of getPacket().
     clear();
+    
 #if ( DEBUG > 0 )
     cerr << "Error in getPacket(): W.isHeader() = false  for H1" << endl;
 #endif
+    
     return; 
   };
@@ -469,7 +488,9 @@
   if (buf.size()<(spare+2)*wordLen) { 
     clear(); 
+    
 #if ( DEBUG > 0 )    
     cerr << "Error in getPacket(): buffer too short for complete H2" << endl;
 #endif
+    
     return;
   };
@@ -483,12 +504,14 @@
     clear(); 
     buf.erase(0,1); 
+    
 #if ( DEBUG > 0 )    
     cerr << "Error in getPacket(): W.validParity() = false for H2" << endl;
 #endif
+        
     return; 
   };
 
   n = nDataWords();
-
+  
   // Do we have enough bytes to read the next word? If not, the packet 
   // contents is cleared to indicate an unsuccessful termination. The
@@ -496,9 +519,11 @@
   // for use in the next call of getPacket().
   if (buf.size()<(spare+2+n)*wordLen) { 
-    clear();     
+    clear(); 
+    
 #if ( DEBUG > 0 )    
     cerr << "Error in getPacket(): buffer too short for complete " << n
          << " DWs" << endl;
 #endif
+    
     return; 
   };
@@ -513,8 +538,10 @@
       clear(); 
       buf.erase(0,1); 
+      
 #if ( DEBUG > 0 )    
     cerr << "Error in getPacket(): W.validParity() = false for DW"
          << i << endl;
 #endif
+        
       return; 
     };
@@ -527,5 +554,5 @@
   
   buf.erase(0,(n+2)*wordLen);
-  
+    
   return;
   
@@ -762,7 +789,11 @@
 void RTCM2_23::extract(const RTCM2packet& P) {
 
-  int  nad, nas;
-  
-  // Check validity and packet type
+  unsigned int       nad, nas;
+  
+  const unsigned int nF1  = 8; // bits in first field (R,AF,SF,NAD)
+  const unsigned int nF2  =16; // bits in second field (SETUP ID,R,NAS)
+  const unsigned int nBits=24; // data bits in  30bit word
+  
+  // Check validity, packet type and number of data words
   
   validMsg = (P.valid()); 
@@ -771,17 +802,46 @@
   validMsg = (P.ID()==23);  
   if (!validMsg) return;
-  
+
+  // Check number of data words (can nad be read in?)
+  
+  validMsg = (P.nDataWords()>=1);  
+  if (!validMsg){
+    cerr << "RTCM2_23::extract: P.nDataWords()>=1" << endl;
+    return;
+  }
+
   // Antenna descriptor 
   antType = "";
   nad = P.getUnsignedBits(3,5);
-  for (int i=0;i<nad;i++) 
-    antType += (char)P.getUnsignedBits(8+i*8,8);
+  
+  // Check number of data words (can antenna description be read in?) 
+  validMsg = ( P.nDataWords() >= 
+               (unsigned int)ceil((nF1+nad*8)/(double)nBits) );
+
+  if (!validMsg) return;
+  
+  for (unsigned int i=0;i<nad;i++) 
+    antType += (char)P.getUnsignedBits(nF1+i*8,8);
 
   // Optional antenna serial numbers
   if (P.getUnsignedBits(2,1)==1) {
+
+    // Check number of data words (can nas be read in?)
+    
+    validMsg = ( P.nDataWords() >=
+                 (unsigned int)ceil((nF1+nad*8+nF2)/(double)nBits) );
+    if (!validMsg) return;
+    
     nas = P.getUnsignedBits(19+8*nad,5);
+
+    // Check number of data words (can antenna serial number be read in?)
+    
+    validMsg = ( P.nDataWords() >=
+                 (unsigned int)ceil((nF1+nad*8+nF2+nas*8)/(double)nBits) );
+    if (!validMsg) return;
+
     antSN = "";
-    for (int i=0;i<nas;i++) 
-      antSN += (char)P.getUnsignedBits(24+8*nad+i*8,8);
+    for (unsigned int i=0;i<nas;i++) 
+      antSN += (char)P.getUnsignedBits(nF1+8*nad+nF2+i*8,8);
   };
 
@@ -962,4 +1022,5 @@
          << P.nDataWords() << ") detected" << endl;
 #endif
+    
     return;
   };
@@ -972,4 +1033,5 @@
          << P.nDataWords() << ") detected" << endl;
 #endif
+    
     return;
   };
@@ -1033,4 +1095,10 @@
     if ( isL1 && !isGPS) availability.set(bit_L1cphGLO);
     if (!isL1 && !isGPS) availability.set(bit_L2cphGLO);
+    
+#if ( DEBUG > 0 )
+    cerr << "RTCM2_Obs::extract(): availability " 
+         << bitset<8>(availability) << endl;  
+#endif
+    
     
     // Process all satellites
@@ -1148,4 +1216,9 @@
     if ( isL1 && !isGPS) availability.set(bit_L1rngGLO);
     if (!isL1 && !isGPS) availability.set(bit_L2rngGLO);
+
+#if ( DEBUG > 0 )
+    cerr << "RTCM2_Obs::extract(): availability " 
+         << bitset<8>(availability) << endl;  
+#endif
 
     // Process all satellites
