Browse Source

CUtlBuffer, Color: misalign fixes

pull/26/head
nillerusr 3 years ago
parent
commit
99e02e25c9
  1. 20
      public/Color.h
  2. 20
      public/tier1/utlbuffer.h
  3. 9
      vpklib/packedstore.cpp

20
public/Color.h

@ -12,6 +12,8 @@ @@ -12,6 +12,8 @@
#pragma once
#endif
#include "tier1/strtools.h"
//-----------------------------------------------------------------------------
// Purpose: Basic handler for an rgb set of colors
// This class is fully inline
@ -22,7 +24,7 @@ public: @@ -22,7 +24,7 @@ public:
// constructors
Color()
{
*((int *)this) = 0;
Q_memset( _color, 0, sizeof _color );
}
Color(int _r,int _g,int _b)
{
@ -32,7 +34,7 @@ public: @@ -32,7 +34,7 @@ public:
{
SetColor(_r, _g, _b, _a);
}
// set the color
// r - red component (0-255)
// g - green component (0-255)
@ -56,19 +58,21 @@ public: @@ -56,19 +58,21 @@ public:
void SetRawColor( int color32 )
{
*((int *)this) = color32;
Q_memcpy( _color, &color32, sizeof _color );
}
int GetRawColor() const
{
return *((int *)this);
int color;
Q_memcpy( &color, _color, sizeof _color );
return color;
}
inline int r() const { return _color[0]; }
inline int g() const { return _color[1]; }
inline int b() const { return _color[2]; }
inline int a() const { return _color[3]; }
unsigned char &operator[](int index)
{
return _color[index];
@ -79,12 +83,12 @@ public: @@ -79,12 +83,12 @@ public:
return _color[index];
}
bool operator == (const Color &rhs) const
bool operator == (const Color &rhs)
{
return ( *((int *)this) == *((int *)&rhs) );
return Q_memcmp( this, &rhs, sizeof(Color) ) == 0;
}
bool operator != (const Color &rhs) const
bool operator != (const Color &rhs)
{
return !(operator==(rhs));
}

20
public/tier1/utlbuffer.h

@ -630,18 +630,18 @@ inline void CUtlBuffer::GetTypeBin( T &dest ) @@ -630,18 +630,18 @@ inline void CUtlBuffer::GetTypeBin( T &dest )
{
if ( !m_Byteswap.IsSwappingBytes() || ( sizeof( T ) == 1 ) )
{
dest = *(T *)PeekGet();
Q_memcpy(&dest, PeekGet(), sizeof(T) );
}
else
{
m_Byteswap.SwapBufferToTargetEndian<T>( &dest, (T*)PeekGet() );
}
m_Get += sizeof(T);
}
m_Get += sizeof(T);
}
else
{
dest = 0;
}
}
}
template <>
@ -661,18 +661,18 @@ inline void CUtlBuffer::GetTypeBin< float >( float &dest ) @@ -661,18 +661,18 @@ inline void CUtlBuffer::GetTypeBin< float >( float &dest )
else
{
// aligned read
dest = *(float *)pData;
Q_memcmp( &dest, pData, sizeof(float) );
}
if ( m_Byteswap.IsSwappingBytes() )
{
m_Byteswap.SwapBufferToTargetEndian< float >( &dest, &dest );
}
m_Get += sizeof( float );
}
m_Get += sizeof( float );
}
else
{
dest = 0;
}
}
}
template <typename T>
@ -816,7 +816,7 @@ inline void CUtlBuffer::PutObject( T *src ) @@ -816,7 +816,7 @@ inline void CUtlBuffer::PutObject( T *src )
{
if ( !m_Byteswap.IsSwappingBytes() || ( sizeof( T ) == 1 ) )
{
*(T *)PeekPut() = *src;
Q_memcpy( PeekPut(), src, sizeof( T ) );
}
else
{
@ -845,7 +845,7 @@ inline void CUtlBuffer::PutTypeBin( T src ) @@ -845,7 +845,7 @@ inline void CUtlBuffer::PutTypeBin( T src )
{
if ( !m_Byteswap.IsSwappingBytes() || ( sizeof( T ) == 1 ) )
{
*(T *)PeekPut() = src;
Q_memcpy( PeekPut(), &src, sizeof( T ) );
}
else
{

9
vpklib/packedstore.cpp

@ -29,7 +29,7 @@ @@ -29,7 +29,7 @@
typedef uint16 PackFileIndex_t;
#define PACKFILEINDEX_END 0xffff
const uint16 packedfileindex_end = 0xffff;
#pragma pack(1)
struct CFilePartDescr
@ -120,9 +120,12 @@ static int SkipFile( char const * &pData ) // returns highest file index @@ -120,9 +120,12 @@ static int SkipFile( char const * &pData ) // returns highest file index
int nHighestChunkIndex = -1;
pData += 1 + V_strlen( pData );
pData += sizeof( uint32 );
int nMetaDataSize = *(reinterpret_cast<uint16 const *>( pData ) );
uint16 nMetaDataSize;
Q_memcpy( &nMetaDataSize, pData, sizeof( uint16 ) );
pData += sizeof( uint16 );
while ( *( ( PackFileIndex_t const *) pData ) != PACKFILEINDEX_END )
while ( Q_memcmp( pData, &packedfileindex_end, sizeof( packedfileindex_end ) ) != 0 )
{
int nIdx = reinterpret_cast<CFilePartDescr const *>(pData)->m_nFileNumber;

Loading…
Cancel
Save