+wxSocketBase& wxSocketBase::ReadMsg(char* buffer, size_t nbytes)
+{
+ unsigned long len, len2, sig;
+ struct {
+ char sig[4];
+ char len[4];
+ } msg;
+
+ // sig should be an explicit 32-bit unsigned integer; I've seen
+ // compilers in which size_t was actually a 16-bit unsigned integer
+
+ Read((char *)&msg, sizeof(msg));
+ if (m_lcount != sizeof(msg))
+ return *this;
+
+ sig = msg.sig[0] & 0xff;
+ sig |= (size_t)(msg.sig[1] & 0xff) << 8;
+ sig |= (size_t)(msg.sig[2] & 0xff) << 16;
+ sig |= (size_t)(msg.sig[3] & 0xff) << 24;
+
+ if (sig != 0xfeeddead)
+ return *this;
+ len = msg.len[0] & 0xff;
+ len |= (size_t)(msg.len[1] & 0xff) << 8;
+ len |= (size_t)(msg.len[2] & 0xff) << 16;
+ len |= (size_t)(msg.len[3] & 0xff) << 24;
+
+ // len2 is incorrectly computed in the original; this sequence is
+ // the fix
+ if (len > nbytes) {
+ len2 = len - nbytes;
+ len = nbytes;
+ }
+ else
+ len2 = 0;
+
+ // the "len &&" in the following statement is necessary so that
+ // we don't attempt to read (and possibly hang the system)
+ // if the message was zero bytes long
+ if (len && Read(buffer, len).LastCount() != len)
+ return *this;
+ if (len2 && (Read(NULL, len2).LastCount() != len2))
+ return *this;
+ if (Read((char *)&msg, sizeof(msg)).LastCount() != sizeof(msg))
+ return *this;
+
+ sig = msg.sig[0] & 0xff;
+ sig |= (size_t)(msg.sig[1] & 0xff) << 8;
+ sig |= (size_t)(msg.sig[2] & 0xff) << 16;
+ sig |= (size_t)(msg.sig[3] & 0xff) << 24;
+// ERROR
+// we return *this either way, so a smart optimizer will
+// optimize the following sequence out; I'm leaving it in anyway
+ if (sig != 0xdeadfeed)
+ return *this;
+
+ return *this;
+}
+