From 0b2cc066ddee23b0a7bbd27205e7d81ca16640f5 Mon Sep 17 00:00:00 2001 From: Alibek Omarov Date: Fri, 11 Oct 2024 20:35:17 +0300 Subject: [PATCH] engine: fix signed integers encoding with alternate format for GoldSrc protocol --- engine/common/net_buffer.c | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/engine/common/net_buffer.c b/engine/common/net_buffer.c index 8c7798a9..5db1031e 100644 --- a/engine/common/net_buffer.c +++ b/engine/common/net_buffer.c @@ -213,25 +213,23 @@ void MSG_WriteSBitLong( sizebuf_t *sb, int data, int numbits ) // do we have a valid # of bits to encode with? Assert( numbits >= 1 && numbits <= 32 ); - if( data < 0 ) + if( sb->iAlternateSign ) { - if( sb->iAlternateSign ) - MSG_WriteOneBit( sb, 1 ); - - MSG_WriteUBitLong( sb, (uint)( 0x80000000 + data ), numbits - 1 ); - - if( !sb->iAlternateSign ) - MSG_WriteOneBit( sb, 1 ); + MSG_WriteOneBit( sb, data < 0 ? 1 : 0 ); + MSG_WriteUBitLong( sb, (uint)abs( data ), numbits - 1 ); } else { - if( sb->iAlternateSign ) - MSG_WriteOneBit( sb, 0 ); - - MSG_WriteUBitLong( sb, (uint)data, numbits - 1 ); - - if( !sb->iAlternateSign ) + if( data < 0 ) + { + MSG_WriteUBitLong( sb, (uint)( 0x80000000 + data ), numbits - 1 ); + MSG_WriteOneBit( sb, 1 ); + } + else + { + MSG_WriteUBitLong( sb, (uint)data, numbits - 1 ); MSG_WriteOneBit( sb, 0 ); + } } }