Less tedious support for strings

This commit is contained in:
HikikoMarmy
2025-02-20 02:40:30 +00:00
parent 4469603710
commit 893385346f
2 changed files with 49 additions and 14 deletions

View File

@@ -61,10 +61,27 @@ T ByteStream::read()
return value;
}
void ByteStream::write_utf8( const std::string &value )
void ByteStream::write_utf8( const std::string &value, const size_t length )
{
write_u32( value.size() );
write_bytes( std::vector< uint8_t >( value.begin(), value.end() ) );
if( length != -1 )
{
write_u32( length );
if (length > value.size())
{
write_bytes( std::vector< uint8_t >( value.begin(), value.end() ) );
write_bytes( std::vector< uint8_t >( length - value.size(), 0 ) );
}
else
{
write_bytes(std::vector< uint8_t >(value.begin(), value.begin() + length));
}
}
else
{
write_u32(value.size());
write_bytes(std::vector< uint8_t >(value.begin(), value.end()));
}
}
void ByteStream::write_utf16( const std::wstring &value )
@@ -82,10 +99,18 @@ void ByteStream::write_utf16( const std::wstring &value )
write_u16( 0 );
}
void ByteStream::write_sz_utf8( const std::string &value )
void ByteStream::write_sz_utf8( const std::string &value, const size_t length )
{
write_bytes( std::vector< uint8_t >( value.begin(), value.end() ) );
write< uint8_t >( 0 );
if( length != -1 )
{
write_bytes( std::vector< uint8_t >( value.begin(), value.end() ) );
write_bytes( std::vector< uint8_t >( length - value.size(), 0 ) );
}
else
{
write_bytes(std::vector< uint8_t >(value.begin(), value.end()));
write< uint8_t >(0);
}
}
void ByteStream::write_sz_utf16( const std::wstring &value )
@@ -163,9 +188,13 @@ float_t ByteStream::read_f32()
return read< float_t >();
}
std::string ByteStream::read_utf8()
std::string ByteStream::read_utf8( size_t length )
{
auto length = read_u32();
if( length == -1 )
{
length = read_u32();
}
std::string value;
for( size_t i = 0; i < length; i++ )
{
@@ -177,9 +206,15 @@ std::string ByteStream::read_utf8()
return value;
}
std::wstring ByteStream::read_utf16()
std::wstring ByteStream::read_utf16( size_t length )
{
auto length = read_u32() * 2;
if( length == -1 )
{
length = read_u32();
}
length *= 2;
std::wstring value;
for( size_t i = 0; i < length; i += 2 )

View File

@@ -35,9 +35,9 @@ public:
void write_i32( int32_t value );
void write_f32( float_t value );
void write_utf8( const std::string &value );
void write_utf8( const std::string &value, const size_t length = -1 );
void write_utf16( const std::wstring &value );
void write_sz_utf8( const std::string &value );
void write_sz_utf8( const std::string &value, const size_t length = -1);
void write_sz_utf16( const std::wstring &value );
void write_encrypted_utf8( const std::string &value );
void write_encrypted_utf16( const std::wstring &value );
@@ -50,8 +50,8 @@ public:
int32_t read_i32();
float_t read_f32();
std::string read_utf8();
std::wstring read_utf16();
std::string read_utf8( size_t length = -1 );
std::wstring read_utf16( size_t length = -1);
std::string read_sz_utf8();
std::wstring read_sz_utf16();
std::string read_encrypted_utf8( bool hasBlockLength = true );