]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - common.c
Fixed 2 potential buffer overflows in SearchInfostring
[xonotic/darkplaces.git] / common.c
index 8987d7f3f4de81d6c39f13ec552def77cfadef17..4ff60bb9d4c9fd97bb5ca0acd82315c5f84efe1a 100644 (file)
--- a/common.c
+++ b/common.c
@@ -219,7 +219,7 @@ void MSG_WriteString (sizebuf_t *sb, const char *s)
                SZ_Write (sb, s, strlen(s)+1);
 }
 
-// used by server (always latest dpprotocol)
+// used by server (always latest PROTOCOL_DARKPLACES)
 void MSG_WriteDPCoord (sizebuf_t *sb, float f)
 {
        if (f >= 0)
@@ -339,7 +339,7 @@ char *MSG_ReadString (void)
 {
        static char string[2048];
        int l,c;
-       for (l = 0;l < sizeof(string) - 1 && (c = MSG_ReadChar()) != -1 && c != 0;l++)
+       for (l = 0;l < (int) sizeof(string) - 1 && (c = MSG_ReadChar()) != -1 && c != 0;l++)
                string[l] = c;
        string[l] = 0;
        return string;
@@ -353,7 +353,7 @@ int MSG_ReadBytes (int numbytes, unsigned char *out)
        return l;
 }
 
-// used by server (always latest dpprotocol)
+// used by server (always latest PROTOCOL_DARKPLACES)
 float MSG_ReadDPCoord (void)
 {
        return (signed short) MSG_ReadLittleShort();
@@ -362,9 +362,9 @@ float MSG_ReadDPCoord (void)
 // used by client
 float MSG_ReadCoord (void)
 {
-       if (dpprotocol == DPPROTOCOL_VERSION2 || dpprotocol == DPPROTOCOL_VERSION3)
+       if (cl.protocol == PROTOCOL_DARKPLACES2 || cl.protocol == PROTOCOL_DARKPLACES3 || cl.protocol == PROTOCOL_DARKPLACES4)
                return (signed short) MSG_ReadLittleShort();
-       else if (dpprotocol == DPPROTOCOL_VERSION1)
+       else if (cl.protocol == PROTOCOL_DARKPLACES1)
                return MSG_ReadLittleFloat();
        else
                return MSG_ReadLittleShort() * (1.0f/8.0f);
@@ -506,7 +506,7 @@ COM_ParseToken
 Parse a token out of a string
 ==============
 */
-int COM_ParseToken (const char **datapointer)
+int COM_ParseToken(const char **datapointer, int returnnewline)
 {
        int c;
        int len;
@@ -523,7 +523,7 @@ int COM_ParseToken (const char **datapointer)
 
 // skip whitespace
 skipwhite:
-       while ((c = *data) <= ' ')
+       while ((c = *data) <= ' ' && (c != '\n' || !returnnewline))
        {
                if (c == 0)
                {
@@ -534,15 +534,25 @@ skipwhite:
                data++;
        }
 
-// skip // comments
-       if (c=='/' && data[1] == '/')
+       // check if it's a comment
+       if (c == '/')
        {
-               while (*data && *data != '\n')
-                       data++;
-               goto skipwhite;
+               // skip // comments
+               if (data[1] == '/')
+               {
+                       while (*data && *data != '\n')
+                               data++;
+                       goto skipwhite;
+               }
+               // skip /* comments
+               if (data[1] == '*')
+               {
+                       while (*data && *data != '*' && data[1] != '/')
+                               data++;
+                       goto skipwhite;
+               }
        }
 
-
 // handle quoted strings specially
        if (c == '\"')
        {
@@ -550,7 +560,7 @@ skipwhite:
                while (1)
                {
                        c = *data++;
-                       if (c=='\"' || !c)
+                       if (c == '\"' || !c)
                        {
                                com_token[len] = 0;
                                *datapointer = data;
@@ -562,7 +572,7 @@ skipwhite:
        }
 
 // parse single characters
-       if (c=='{' || c=='}'|| c==')'|| c=='(' || c=='\'' || c==':')
+       if (c == '{' || c == '}' || c == ')' || c == '(' || c == ']' || c == '[' || c == '\'' || c == ':' || c == ',' || c == ';' || c == '\n')
        {
                com_token[len] = c;
                len++;
@@ -578,7 +588,7 @@ skipwhite:
                data++;
                len++;
                c = *data;
-               if (c=='{' || c=='}'|| c==')'|| c=='(' || c=='\'' || c==':')
+               if (c == '{' || c == '}' || c == ')' || c == '(' || c == ']' || c == '[' || c == '\'' || c == ':' || c == ',' || c == ';')
                        break;
        } while (c>32);
 
@@ -678,6 +688,14 @@ void COM_InitGameType (void)
                gamemode = GAME_HIPNOTIC;
        else if (strstr(name, "rogue"))
                gamemode = GAME_ROGUE;
+       else if (strstr(name, "gvb2"))
+               gamemode = GAME_GOODVSBAD2;
+       else if (strstr(name, "teu"))
+               gamemode = GAME_TEU;
+       else if (strstr(name, "battlemech"))
+               gamemode = GAME_BATTLEMECH;
+       else if (strstr(name, "zymotic"))
+               gamemode = GAME_ZYMOTIC;
        else
                gamemode = GAME_NORMAL;
 
@@ -693,6 +711,14 @@ void COM_InitGameType (void)
                gamemode = GAME_ROGUE;
        else if (COM_CheckParm ("-quake"))
                gamemode = GAME_NORMAL;
+       else if (COM_CheckParm ("-goodvsbad2"))
+               gamemode = GAME_GOODVSBAD2;
+       else if (COM_CheckParm ("-teu"))
+               gamemode = GAME_TEU;
+       else if (COM_CheckParm ("-battlemech"))
+               gamemode = GAME_BATTLEMECH;
+       else if (COM_CheckParm ("-zymotic"))
+               gamemode = GAME_ZYMOTIC;
 
        switch(gamemode)
        {
@@ -720,6 +746,22 @@ void COM_InitGameType (void)
                gamename = "Transfusion";
                gamedirname = "transfusion";
                break;
+       case GAME_GOODVSBAD2:
+               gamename = "GoodVs.Bad2";
+               gamedirname = "rts";
+               break;
+       case GAME_TEU:
+               gamename = "TheEvilUnleashed";
+               gamedirname = "baseteu";
+               break;
+       case GAME_BATTLEMECH:
+               gamename = "Battlemech";
+               gamedirname = "base";
+               break;
+       case GAME_ZYMOTIC:
+               gamename = "Zymotic";
+               gamedirname = "data";
+               break;
        default:
                Sys_Error("COM_InitGameType: unknown gamemode %i\n", gamemode);
                break;
@@ -832,3 +874,66 @@ int COM_StringBeginsWith(const char *s, const char *match)
                        return false;
        return true;
 }
+
+// written by Elric, thanks Elric!
+char *SearchInfostring(const char *infostring, const char *key)
+{
+       static char value [256];
+       char crt_key [256];
+       size_t value_ind, key_ind;
+       char c;
+
+       if (*infostring++ != '\\')
+               return NULL;
+
+       value_ind = 0;
+       for (;;)
+       {
+               key_ind = 0;
+
+               // Get the key name
+               for (;;)
+               {
+                       c = *infostring++;
+
+                       if (c == '\0')
+                               return NULL;
+                       if (c == '\\' || key_ind == sizeof (crt_key) - 1)
+                       {
+                               crt_key[key_ind] = '\0';
+                               break;
+                       }
+
+                       crt_key[key_ind++] = c;
+               }
+
+               // If it's the key we are looking for, save it in "value"
+               if (!strcmp(crt_key, key))
+               {
+                       for (;;)
+                       {
+                               c = *infostring++;
+
+                               if (c == '\0' || c == '\\' || value_ind == sizeof (value) - 1)
+                               {
+                                       value[value_ind] = '\0';
+                                       return value;
+                               }
+
+                               value[value_ind++] = c;
+                       }
+               }
+
+               // Else, skip the value
+               for (;;)
+               {
+                       c = *infostring++;
+
+                       if (c == '\0')
+                               return NULL;
+                       if (c == '\\')
+                               break;
+               }
+       }
+}
+