From 2b010b549e52a6666a1f6448e660030da032335b Mon Sep 17 00:00:00 2001 From: fo76utils <87907510+fo76utils@users.noreply.github.com> Date: Wed, 18 Sep 2024 16:08:51 +0200 Subject: [PATCH] NifOStream: fixed extra null character at the end of tShortString --- src/io/nifstream.cpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/io/nifstream.cpp b/src/io/nifstream.cpp index fa733516..8c80024a 100644 --- a/src/io/nifstream.cpp +++ b/src/io/nifstream.cpp @@ -363,7 +363,7 @@ bool NifIStream::read( NifValue & val ) //string.replace( "\r", "\\r" ); //string.replace( "\n", "\\n" ); - *static_cast(val.val.data) = QString::fromLocal8Bit( string ); + *static_cast(val.val.data) = QString::fromLatin1( string ); } return true; case NifValue::tText: @@ -814,14 +814,16 @@ bool NifOStream::write( const NifValue & val ) } case NifValue::tShortString: { - QByteArray string = static_cast(val.val.data)->toLocal8Bit(); - string.replace( "\\r", "\r" ); - string.replace( "\\n", "\n" ); + QByteArray string = static_cast(val.val.data)->toLatin1(); + //string.replace( "\\r", "\r" ); + //string.replace( "\\n", "\n" ); if ( string.size() > 254 ) string.resize( 254 ); + // make sure the string has exactly one terminating null character + string.resize( std::max< qsizetype >( string.indexOf( '\0' ), 0 ) + 1, '\0' ); - unsigned char len = string.size() + 1; + unsigned char len = (unsigned char) string.size(); if ( device->write( (char *)&len, 1 ) != 1 ) return false; @@ -1048,10 +1050,8 @@ int NifSStream::size( const NifValue & val ) //string.replace( "\\r", "\r" ); //string.replace( "\\n", "\n" ); - if ( string.size() > 254 ) - string.resize( 254 ); - return 1 + string.size() + 1; + return std::min< qsizetype >( std::max< qsizetype >( string.indexOf( '\0' ), 0 ) + 2, 256 ); } case NifValue::tText: {