Index: trunk/BNC/RTCM/RTCM2.cpp
===================================================================
--- trunk/BNC/RTCM/RTCM2.cpp	(revision 710)
+++ trunk/BNC/RTCM/RTCM2.cpp	(revision 711)
@@ -45,6 +45,4 @@
 //   2008/03/04  AHA  Fixed problems with PRN 32
 //   2008/03/05  AHA  Implemeted fix for Trimble 4000SSI receivers
-//   2008/03/07  AHA  Major revision of input buffer handling 
-//   2008/03/07  AHA  Removed unnecessary failure flag
 //
 // (c) DLR/GSOC
@@ -65,5 +63,5 @@
 // undersized packets in get(Unsigned)Bits
 
-#define DEBUG 0
+#define DEBUG 0    
 
 // Activate (1) or deactivate (0) rounding of measurement epochs to 100ms
@@ -138,4 +136,10 @@
 void ThirtyBitWord::clear() {
   W = 0;
+};
+
+// Failure indicator for input operations
+
+bool ThirtyBitWord::fail() const {
+  return failure; 
 };
 
@@ -255,4 +259,5 @@
 
 
+
 // Append a byte with six data bits
 
@@ -270,5 +275,5 @@
   // Bits 7 and 6 (of 0..7) must be "01" for valid data bytes
   if ( (b & 0x40) != 0x40 ) {
-    // We simply skip the invalid input byte and leave the word unchanged
+    failure = true;
     return;
   };
@@ -285,10 +290,10 @@
 // Get next 30bit word from string
 
-void ThirtyBitWord::get(const string& buf) {
+void ThirtyBitWord::get(string& buf) {
 
   // Check if string is long enough
    
   if (buf.size()<5) {
-    // Ignore; users should avoid this case prior to calling get()
+    failure = true;
     return;
   };
@@ -297,11 +302,13 @@
   
   for (int i=0; i<5; i++) append(buf[i]);
+  buf.erase(0,5);
 
 #if (DEBUG>0) 
   if (!validParity()) {
-    cerr << "Parity error in get()" 
+    cerr << "Parity error " 
          << bitset<32>(all()) << endl;
   };
 #endif
+  failure = false;
 
 };
@@ -321,8 +328,9 @@
 #if (DEBUG>0) 
   if (!validParity()) {
-    cerr << "Parity error in get()" 
+    cerr << "Parity error " 
          << bitset<32>(all()) << endl;
   };
 #endif
+  failure = false;
 
 };
@@ -332,28 +340,31 @@
 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()) 
+  unsigned int W_old = W;
   unsigned int i;
   
   i=0;
-  while (!isHeader() && i<buf.size() ) {
+  while (!isHeader() || i<5 ) {
+    // Check if string is long enough; if not restore old word and exit
+    if (buf.size()<i+1) {
+      W = W_old;
+      failure = true;
+      return;
+    };
     // Process byte
-    append(buf[i]);
-    // Increment count
-    i++;
-  };
-
-  // Remove processed bytes from buffer. Retain also the previous word to
-  // allow a resync if getHeader() is called repeatedly on the same buffer.
-  if (i>=(1+spare)*wordLen) buf.erase(0,i-(1+spare)*wordLen);
+    append(buf[i]); i++;
+  };
+
+  // Remove processed bytes from buffer
+  
+  buf.erase(0,i);
 
 #if (DEBUG>0) 
   if (!validParity()) {
-    cerr << "Parity error in getHeader()" 
+    cerr << "Parity error " 
          << bitset<32>(all()) << endl;
   };
 #endif
-  
+  failure = false;
+
 };
 
@@ -374,8 +385,9 @@
 #if (DEBUG>0) 
   if (!validParity()) {
-    cerr << "Parity error in getHeader()" 
+    cerr << "Parity error " 
          << bitset<32>(all()) << endl;
   };
 #endif
+  failure = false;
 
 };
@@ -431,69 +443,33 @@
 void RTCM2packet::getPacket(std::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 used in ThirtyBitWord::getHeader)
-  unsigned int n;
-  
-  // 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().
-  
-  // Locate and read the first header word
-  W.getHeader(buf);
-  if (!W.isHeader()) { 
-    // No header found; try again next time. buf retains only the spare
-    // words. The packet contents is cleared to indicate an unsuccessful
-    // termination of getPacket().
-    clear();
-    return; 
-  };
-  H1 = W.value();
-  
-  // Do we have enough bytes to read the next word? If not, the packet 
-  // contents is cleared to indicate an unsuccessful termination. The
-  // previously read spare and header bytes are retained in the buffer
-  // for use in the next call of getPacket().
-  if (buf.size()<(spare+2)*wordLen) { clear(); return; };
-  
-  // Read the second header word
-  W.get(buf.substr((spare+1)*wordLen,buf.size()-1-(spare+1)*wordLen));  
-  H2 = W.value();
-  if (!W.validParity()) { 
-    // Invalid H2 word; delete first buffer byte and try to resynch next time.
-    // The packet contents is cleared to indicate an unsuccessful termination.
-    clear(); 
-    buf.erase(0,1); 
-    return; 
-  };
+  int           n;
+  ThirtyBitWord W_old = W;
+  string        buf_old = buf;
+  
+  // Try to read a full packet. If the input buffer is too short
+  // clear all data and restore the latest 30-bit word prior to 
+  // the getPacket call. The empty header word will indicate
+  // an invalid message, which signals an unsuccessful getPacket()
+  // call.
+   
+  W.getHeader(buf); 
+  H1 = W.value(); 
+  if (W.fail()) { clear(); W=W_old; buf=buf_old; return; };
+  if (!W.validParity()) { clear(); return; };
+  
+  W.get(buf);       
+  H2 = W.value(); 
+  if (W.fail()) { clear(); W=W_old; buf=buf_old; return; };
+  if (!W.validParity()) { clear(); 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
-  // previously read spare and header bytes are retained in the buffer
-  // for use in the next call of getPacket().
-  if (buf.size()<(spare+2+n)*wordLen) { clear(); return; };
-  
   DW.resize(n);
-  for (unsigned int i=0; i<n; i++) {
-    W.get(buf.substr((spare+2+i)*wordLen,buf.size()-1-(spare+2+i)*wordLen)); 
-    DW[i] = W.value();
-    if (!W.validParity()) { 
-      // Invalid data word; delete first byte and try to resynch next time.
-      // The packet contents is cleared to indicate an unsuccessful termination.
-      clear(); 
-      buf.erase(0,1); 
-      return; 
-    };
-  };
-
-  // Successful packet extraction; delete total number of message bytes 
-  // from buffer. 
-  // Note: a total of "spare" words remain in the buffer to enable a
-  // parity resynchronization when searching the next header.
-  
-  buf.erase(0,(n+2)*wordLen);
-  
+  for (int i=0; i<n; i++) {
+    W.get(buf); 
+    DW[i] = W.value(); 
+    if (W.fail()) { clear(); W=W_old; buf=buf_old; return; };
+    if (!W.validParity()) { clear(); return; };
+  };
+
   return;
   
@@ -511,9 +487,9 @@
   W.getHeader(inp); 
   H1 = W.value(); 
-  if (inp.fail() || !W.isHeader()) { clear(); return; }
+  if (W.fail() || !W.validParity()) { clear(); return; }
   
   W.get(inp);       
   H2 = W.value(); 
-  if (inp.fail() || !W.validParity()) { clear(); return; }
+  if (W.fail() || !W.validParity()) { clear(); return; }
 
   n = nDataWords();
@@ -522,5 +498,5 @@
     W.get(inp); 
     DW[i] = W.value(); 
-    if (inp.fail() || !W.validParity()) { clear(); return; }
+    if (W.fail() || !W.validParity()) { clear(); return; }
   };
 
Index: trunk/BNC/RTCM/RTCM2.h
===================================================================
--- trunk/BNC/RTCM/RTCM2.h	(revision 710)
+++ trunk/BNC/RTCM/RTCM2.h	(revision 711)
@@ -28,5 +28,4 @@
 //   2006/10/17  OMO  Removed obsolete check of multiple message indicator
 //   2006/11/25  OMO  Revised check for presence of GLONASS data
-//   2008/03/07  AHA  Removed unnecessary failure flag
 //
 // (c) DLR/GSOC
@@ -82,5 +81,5 @@
     // Input
     
-    void         get(const std::string& buf);
+    void         get(std::string& buf);
     void         get(std::istream& inp);
     void         getHeader(std::string& buf);
@@ -95,5 +94,5 @@
   private:
 
-//    bool         failure;
+    bool         failure;
 
     //
