]> git.xonotic.org Git - xonotic/darkplaces.git/commitdiff
MSG_ReadFloat now checks if the message has enough bytes for a float before trying...
authorhavoc <havoc@d7cf8633-e32d-0410-b094-e92efae38249>
Fri, 28 Mar 2003 10:29:42 +0000 (10:29 +0000)
committerhavoc <havoc@d7cf8633-e32d-0410-b094-e92efae38249>
Fri, 28 Mar 2003 10:29:42 +0000 (10:29 +0000)
added MSG_ReadBigShort, MSG_ReadBigLong, MSG_ReadBigFloat, MSG_ReadBytes
renamed MSG_ReadShort to MSG_ReadLittleShort, MSG_ReadLong to MSG_ReadLittleLong, and MSG_ReadFloat to MSG_ReadLittleFloat
added #defines for MSG_ReadShort, MSG_ReadLong, and MSG_ReadFloat to use their Little versions
optimized/cleaned up MSG_Read functions (some are now significantly shorter)

git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@2866 d7cf8633-e32d-0410-b094-e92efae38249

common.c
common.h

index cd6c392c2686733c192c889ecbbef1343f4fdbd3..6363ad1e2fa39998754c67ad5d00a557d7c4fcb9 100644 (file)
--- a/common.c
+++ b/common.c
@@ -163,7 +163,7 @@ Handles byte ordering and avoids alignment errors
 void MSG_WriteChar (sizebuf_t *sb, int c)
 {
        qbyte    *buf;
-       
+
        buf = SZ_GetSpace (sb, 1);
        buf[0] = c;
 }
@@ -171,7 +171,7 @@ void MSG_WriteChar (sizebuf_t *sb, int c)
 void MSG_WriteByte (sizebuf_t *sb, int c)
 {
        qbyte    *buf;
-       
+
        buf = SZ_GetSpace (sb, 1);
        buf[0] = c;
 }
@@ -257,61 +257,81 @@ void MSG_BeginReading (void)
        msg_badread = false;
 }
 
-int MSG_ReadShort (void)
+int MSG_ReadLittleShort (void)
 {
-       int c;
-
        if (msg_readcount+2 > net_message.cursize)
        {
                msg_badread = true;
                return -1;
        }
-
-       c = (short)(net_message.data[msg_readcount]
-       + (net_message.data[msg_readcount+1]<<8));
-
        msg_readcount += 2;
-
-       return c;
+       return (short)(net_message.data[msg_readcount-2] | (net_message.data[msg_readcount-1]<<8));
 }
 
-int MSG_ReadLong (void)
+int MSG_ReadBigShort (void)
 {
-       int c;
+       if (msg_readcount+2 > net_message.cursize)
+       {
+               msg_badread = true;
+               return -1;
+       }
+       msg_readcount += 2;
+       return (short)((net_message.data[msg_readcount-2]<<8) + net_message.data[msg_readcount-1]);
+}
 
+int MSG_ReadLittleLong (void)
+{
        if (msg_readcount+4 > net_message.cursize)
        {
                msg_badread = true;
                return -1;
        }
-
-       c = net_message.data[msg_readcount]
-       + (net_message.data[msg_readcount+1]<<8)
-       + (net_message.data[msg_readcount+2]<<16)
-       + (net_message.data[msg_readcount+3]<<24);
-
        msg_readcount += 4;
+       return net_message.data[msg_readcount-4] | (net_message.data[msg_readcount-3]<<8) | (net_message.data[msg_readcount-2]<<16) | (net_message.data[msg_readcount-1]<<24);
+}
 
-       return c;
+int MSG_ReadBigLong (void)
+{
+       if (msg_readcount+4 > net_message.cursize)
+       {
+               msg_badread = true;
+               return -1;
+       }
+       msg_readcount += 4;
+       return (net_message.data[msg_readcount-4]<<24) + (net_message.data[msg_readcount-3]<<16) + (net_message.data[msg_readcount-2]<<8) + net_message.data[msg_readcount-1];
 }
 
-float MSG_ReadFloat (void)
+float MSG_ReadLittleFloat (void)
 {
        union
        {
-               qbyte b[4];
                float f;
                int l;
        } dat;
-
-       dat.b[0] =      net_message.data[msg_readcount];
-       dat.b[1] =      net_message.data[msg_readcount+1];
-       dat.b[2] =      net_message.data[msg_readcount+2];
-       dat.b[3] =      net_message.data[msg_readcount+3];
+       if (msg_readcount+4 > net_message.cursize)
+       {
+               msg_badread = true;
+               return -1;
+       }
        msg_readcount += 4;
+       dat.l = net_message.data[msg_readcount-4] | (net_message.data[msg_readcount-3]<<8) | (net_message.data[msg_readcount-2]<<16) | (net_message.data[msg_readcount-1]<<24);
+       return dat.f;
+}
 
-       dat.l = LittleLong (dat.l);
-
+float MSG_ReadBigFloat (void)
+{
+       union
+       {
+               float f;
+               int l;
+       } dat;
+       if (msg_readcount+4 > net_message.cursize)
+       {
+               msg_badread = true;
+               return -1;
+       }
+       msg_readcount += 4;
+       dat.l = (net_message.data[msg_readcount-4]<<24) | (net_message.data[msg_readcount-3]<<16) | (net_message.data[msg_readcount-2]<<8) | net_message.data[msg_readcount-1];
        return dat.f;
 }
 
@@ -319,37 +339,35 @@ char *MSG_ReadString (void)
 {
        static char string[2048];
        int l,c;
-
-       l = 0;
-       do
-       {
-               c = MSG_ReadChar ();
-               if (c == -1 || c == 0)
-                       break;
+       for (l = 0;l < sizeof(string) - 1 && (c = MSG_ReadChar()) != -1 && c != 0;l++)
                string[l] = c;
-               l++;
-       } while (l < (int)sizeof(string)-1);
-
        string[l] = 0;
-
        return string;
 }
 
+int MSG_ReadBytes (int numbytes, unsigned char *out)
+{
+       int l, c;
+       for (l = 0;l < numbytes && (c = MSG_ReadChar()) != -1;l++)
+               out[l] = c;
+       return l;
+}
+
 // used by server (always latest dpprotocol)
 float MSG_ReadDPCoord (void)
 {
-       return (signed short) MSG_ReadShort();
+       return (signed short) MSG_ReadLittleShort();
 }
 
 // used by client
 float MSG_ReadCoord (void)
 {
        if (dpprotocol == DPPROTOCOL_VERSION2 || dpprotocol == DPPROTOCOL_VERSION3)
-               return (signed short) MSG_ReadShort();
+               return (signed short) MSG_ReadLittleShort();
        else if (dpprotocol == DPPROTOCOL_VERSION1)
-               return MSG_ReadFloat();
+               return MSG_ReadLittleFloat();
        else
-               return MSG_ReadShort() * (1.0f/8.0f);
+               return MSG_ReadLittleShort() * (1.0f/8.0f);
 }
 
 
index 743b499ae1026c405bd565e74a8482a111829819..7058d0b3be74a9296f96c0b5b607deddb1b4de0f 100644 (file)
--- a/common.h
+++ b/common.h
@@ -8,7 +8,7 @@ of the License, or (at your option) any later version.
 
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 
 See the GNU General Public License for more details.
 
@@ -114,13 +114,20 @@ extern    int                     msg_readcount;
 extern qboolean        msg_badread;            // set if a read goes beyond end of message
 
 void MSG_BeginReading (void);
-int MSG_ReadShort (void);
-int MSG_ReadLong (void);
-float MSG_ReadFloat (void);
+int MSG_ReadLittleShort (void);
+int MSG_ReadBigShort (void);
+int MSG_ReadLittleLong (void);
+int MSG_ReadBigLong (void);
+float MSG_ReadLittleFloat (void);
+float MSG_ReadBigFloat (void);
 char *MSG_ReadString (void);
+int MSG_ReadBytes (int numbytes, unsigned char *out);
 
 #define MSG_ReadChar() (msg_readcount >= net_message.cursize ? (msg_badread = true, -1) : (signed char)net_message.data[msg_readcount++])
 #define MSG_ReadByte() (msg_readcount >= net_message.cursize ? (msg_badread = true, -1) : (unsigned char)net_message.data[msg_readcount++])
+#define MSG_ReadShort MSG_ReadLittleShort
+#define MSG_ReadLong MSG_ReadLittleLong
+#define MSG_ReadFloat MSG_ReadLittleFloat
 
 float MSG_ReadCoord (void);