]> git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - com_msg.c
console: improve default text legibility and background alpha behaviour
[xonotic/darkplaces.git] / com_msg.c
index f2110d17f05fefcafb6a9d5901c86d7e9d23fe3f..aaded00d3fc84d2699eeadb51d6d282e125509f4 100644 (file)
--- a/com_msg.c
+++ b/com_msg.c
@@ -30,6 +30,9 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 ============================================================================
 */
 
+/* Casting to unsigned when shifting by 24 bits here is necessary to prevent UB
+ * caused by shifting outside the range of int on platforms where int is 32 bits.
+ */
 
 float BuffBigFloat (const unsigned char *buffer)
 {
@@ -39,13 +42,13 @@ float BuffBigFloat (const unsigned char *buffer)
                unsigned int i;
        }
        u;
-       u.i = (buffer[0] << 24) | (buffer[1] << 16) | (buffer[2] << 8) | buffer[3];
+       u.i = ((unsigned)buffer[0] << 24) | (buffer[1] << 16) | (buffer[2] << 8) | buffer[3];
        return u.f;
 }
 
 int BuffBigLong (const unsigned char *buffer)
 {
-       return (buffer[0] << 24) | (buffer[1] << 16) | (buffer[2] << 8) | buffer[3];
+       return ((unsigned)buffer[0] << 24) | (buffer[1] << 16) | (buffer[2] << 8) | buffer[3];
 }
 
 short BuffBigShort (const unsigned char *buffer)
@@ -61,13 +64,13 @@ float BuffLittleFloat (const unsigned char *buffer)
                unsigned int i;
        }
        u;
-       u.i = (buffer[3] << 24) | (buffer[2] << 16) | (buffer[1] << 8) | buffer[0];
+       u.i = ((unsigned)buffer[3] << 24) | (buffer[2] << 16) | (buffer[1] << 8) | buffer[0];
        return u.f;
 }
 
 int BuffLittleLong (const unsigned char *buffer)
 {
-       return (buffer[3] << 24) | (buffer[2] << 16) | (buffer[1] << 8) | buffer[0];
+       return ((unsigned)buffer[3] << 24) | (buffer[2] << 16) | (buffer[1] << 8) | buffer[0];
 }
 
 short BuffLittleShort (const unsigned char *buffer)
@@ -287,7 +290,7 @@ int MSG_ReadLittleLong (sizebuf_t *sb)
                return -1;
        }
        sb->readcount += 4;
-       return sb->data[sb->readcount-4] | (sb->data[sb->readcount-3]<<8) | (sb->data[sb->readcount-2]<<16) | (sb->data[sb->readcount-1]<<24);
+       return sb->data[sb->readcount-4] | (sb->data[sb->readcount-3]<<8) | (sb->data[sb->readcount-2]<<16) | ((unsigned)sb->data[sb->readcount-1]<<24);
 }
 
 int MSG_ReadBigLong (sizebuf_t *sb)
@@ -298,7 +301,7 @@ int MSG_ReadBigLong (sizebuf_t *sb)
                return -1;
        }
        sb->readcount += 4;
-       return (sb->data[sb->readcount-4]<<24) + (sb->data[sb->readcount-3]<<16) + (sb->data[sb->readcount-2]<<8) + sb->data[sb->readcount-1];
+       return ((unsigned)sb->data[sb->readcount-4]<<24) + (sb->data[sb->readcount-3]<<16) + (sb->data[sb->readcount-2]<<8) + sb->data[sb->readcount-1];
 }
 
 float MSG_ReadLittleFloat (sizebuf_t *sb)
@@ -314,7 +317,7 @@ float MSG_ReadLittleFloat (sizebuf_t *sb)
                return -1;
        }
        sb->readcount += 4;
-       dat.l = sb->data[sb->readcount-4] | (sb->data[sb->readcount-3]<<8) | (sb->data[sb->readcount-2]<<16) | (sb->data[sb->readcount-1]<<24);
+       dat.l = sb->data[sb->readcount-4] | (sb->data[sb->readcount-3]<<8) | (sb->data[sb->readcount-2]<<16) | ((unsigned)sb->data[sb->readcount-1]<<24);
        return dat.f;
 }
 
@@ -331,27 +334,40 @@ float MSG_ReadBigFloat (sizebuf_t *sb)
                return -1;
        }
        sb->readcount += 4;
-       dat.l = (sb->data[sb->readcount-4]<<24) | (sb->data[sb->readcount-3]<<16) | (sb->data[sb->readcount-2]<<8) | sb->data[sb->readcount-1];
+       dat.l = ((unsigned)sb->data[sb->readcount-4]<<24) | (sb->data[sb->readcount-3]<<16) | (sb->data[sb->readcount-2]<<8) | sb->data[sb->readcount-1];
        return dat.f;
 }
 
 char *MSG_ReadString (sizebuf_t *sb, char *string, size_t maxstring)
 {
-       int c;
        size_t l = 0;
+
        // read string into sbfer, but only store as many characters as will fit
-       while ((c = MSG_ReadByte(sb)) > 0)
-               if (l < maxstring - 1)
-                       string[l++] = c;
-       string[l] = 0;
+       // if dest buffer is full sb->readcount will still be advanced to end of message string
+       while ((string[l] = MSG_ReadByte_opt(sb)) != '\0')
+               if (l < maxstring)
+                       ++l;
        return string;
 }
+size_t MSG_ReadString_len (sizebuf_t *sb, char *string, size_t maxstring)
+{
+       size_t l = 0;
 
-int MSG_ReadBytes (sizebuf_t *sb, int numbytes, unsigned char *out)
+       // read string into sbfer, but only store as many characters as will fit
+       // if dest buffer is full sb->readcount will still be advanced to end of message string
+       while ((string[l] = MSG_ReadByte_opt(sb)) != '\0')
+               if (l < maxstring)
+                       ++l;
+       return l;
+}
+
+size_t MSG_ReadBytes (sizebuf_t *sb, size_t numbytes, unsigned char *out)
 {
-       int l, c;
-       for (l = 0;l < numbytes && (c = MSG_ReadByte(sb)) != -1;l++)
-               out[l] = c;
+       size_t l = 0;
+
+       // when numbytes have been read sb->readcount won't be advanced any further
+       while (l < numbytes && !sb->badread)
+               out[l++] = MSG_ReadByte_opt(sb);
        return l;
 }