From cf8fe1861f4814030c055109132a4c11f1b238e7 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Fri, 8 Nov 2019 18:45:31 -0500 Subject: [PATCH 1/5] IOStreams: Normalize cpp file inclusions --- Runtime/IOStreams.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Runtime/IOStreams.cpp b/Runtime/IOStreams.cpp index 03089beb4..598a4a887 100644 --- a/Runtime/IOStreams.cpp +++ b/Runtime/IOStreams.cpp @@ -1,5 +1,5 @@ -#include "IOStreams.hpp" -#include "hecl/hecl.hpp" +#include "Runtime/IOStreams.hpp" +#include namespace urde { From 3894c0539e818eadd41c42c42596121e47039671 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Fri, 8 Nov 2019 18:49:37 -0500 Subject: [PATCH 2/5] IOStreams: Use fmt where applicable Eliminates the use of printf and instead uses fmt so types can automatically be formatted without the use of casts. While we're at it, we can resolve a sign-conversion warning in PrintBinary(). --- Runtime/IOStreams.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Runtime/IOStreams.cpp b/Runtime/IOStreams.cpp index 598a4a887..780ff42ef 100644 --- a/Runtime/IOStreams.cpp +++ b/Runtime/IOStreams.cpp @@ -7,7 +7,7 @@ namespace urde { #if DUMP_BITS static void PrintBinary(u32 val, u32 count) { - for (int i = 0; i < count; ++i) { + for (u32 i = 0; i < count; ++i) { fmt::print(fmt("{}"), (val >> (count - i - 1)) & 0x1); } } @@ -21,8 +21,8 @@ static void PrintBinary(u32 val, u32 count) { */ s32 CBitStreamReader::ReadEncoded(u32 bitCount) { #if DUMP_BITS - auto pos = position(); - auto boff = x20_bitOffset; + const auto pos = position(); + const auto boff = x20_bitOffset; #endif u32 ret = 0; @@ -55,9 +55,9 @@ s32 CBitStreamReader::ReadEncoded(u32 bitCount) { } #if DUMP_BITS - printf("READ "); + std::fputs("READ ", stdout); PrintBinary(ret, bitCount); - printf(" %d %d\n", int(pos), int(boff)); + fmt::print(fmt(" {} {}\n"), pos, boff); #endif return ret; @@ -65,9 +65,9 @@ s32 CBitStreamReader::ReadEncoded(u32 bitCount) { void CBitStreamWriter::WriteEncoded(u32 val, u32 bitCount) { #if DUMP_BITS - printf("WRITE "); + std::fputs("WRITE ", stdout); PrintBinary(val, bitCount); - printf(" %d %d\n", int(position()), int(x18_bitOffset)); + fmt::print(fmt(" {} {}\n"), position(), x18_bitOffset); #endif s32 shiftAmt = x18_bitOffset - s32(bitCount); From 95a0b0e559979d9fe895c81503f040238033e354 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Fri, 8 Nov 2019 18:55:10 -0500 Subject: [PATCH 3/5] IOStreams: Remove reinterpret_casts where applicable We can simply make use of writeBytes() where applicable, which does it for us, which makes the code a slight bit less verbose. --- Runtime/IOStreams.cpp | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/Runtime/IOStreams.cpp b/Runtime/IOStreams.cpp index 780ff42ef..3ecd66ad7 100644 --- a/Runtime/IOStreams.cpp +++ b/Runtime/IOStreams.cpp @@ -78,7 +78,7 @@ void CBitStreamWriter::WriteEncoded(u32 val, u32 bitCount) { /* Write out 32-bits */ x14_val = hecl::SBig(x14_val); - writeUBytes(reinterpret_cast(&x14_val), 4); + writeBytes(&x14_val, sizeof(x14_val)); /* Cache remaining bits */ x18_bitOffset = 0x20 + shiftAmt; @@ -94,15 +94,19 @@ void CBitStreamWriter::WriteEncoded(u32 val, u32 bitCount) { } void CBitStreamWriter::Flush() { - if (x18_bitOffset < 0x20) { - auto pos = std::div(0x20 - x18_bitOffset, 8); - if (pos.rem) - ++pos.quot; - x14_val = hecl::SBig(x14_val); - writeUBytes(reinterpret_cast(&x14_val), pos.quot); - x18_bitOffset = 0x20; - x14_val = 0; + if (x18_bitOffset >= 0x20) { + return; } + + auto pos = std::div(0x20 - s32(x18_bitOffset), 8); + if (pos.rem != 0) { + ++pos.quot; + } + + x14_val = hecl::SBig(x14_val); + writeBytes(&x14_val, pos.quot); + x18_bitOffset = 0x20; + x14_val = 0; } class CZipSupport { @@ -123,7 +127,7 @@ CZipInputStream::CZipInputStream(std::unique_ptr&& strm) CZipInputStream::~CZipInputStream() { inflateEnd(&x30_zstrm); } atUint64 CZipInputStream::readUBytesToBuf(void* buf, atUint64 len) { - x30_zstrm.next_out = (Bytef*)buf; + x30_zstrm.next_out = static_cast(buf); x30_zstrm.avail_out = len; x30_zstrm.total_out = 0; while (x30_zstrm.avail_out != 0) { From 9a728a38d63c087d05a5ef63b10f1f857e8850bf Mon Sep 17 00:00:00 2001 From: Lioncash Date: Fri, 8 Nov 2019 19:05:42 -0500 Subject: [PATCH 4/5] IOStreams: Use 1U when shifting bitmasks Otherwise this is technically shifting a signed value, which can lead to warnings. While we're at it, we can use UINT32_MAX instead of 0xFFFFFFFF to mean the same thing. --- Runtime/IOStreams.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Runtime/IOStreams.cpp b/Runtime/IOStreams.cpp index 3ecd66ad7..4bf6a875f 100644 --- a/Runtime/IOStreams.cpp +++ b/Runtime/IOStreams.cpp @@ -26,10 +26,10 @@ s32 CBitStreamReader::ReadEncoded(u32 bitCount) { #endif u32 ret = 0; - s32 shiftAmt = x20_bitOffset - s32(bitCount); + const s32 shiftAmt = x20_bitOffset - s32(bitCount); if (shiftAmt < 0) { /* OR in remaining bits of cached value */ - u32 mask = bitCount == 32 ? 0xffffffff : ((1 << bitCount) - 1); + u32 mask = bitCount == 32 ? UINT32_MAX : ((1U << bitCount) - 1); ret |= (x1c_val << u32(-shiftAmt)) & mask; /* Load in exact number of bytes remaining */ @@ -43,11 +43,11 @@ s32 CBitStreamReader::ReadEncoded(u32 bitCount) { x20_bitOffset = loadDiv.quot * 8 + shiftAmt; /* OR in next bits */ - mask = (1 << u32(-shiftAmt)) - 1; + mask = (1U << u32(-shiftAmt)) - 1; ret |= (x1c_val >> x20_bitOffset) & mask; } else { /* OR in bits of cached value */ - u32 mask = bitCount == 32 ? 0xffffffff : ((1 << bitCount) - 1); + const u32 mask = bitCount == 32 ? UINT32_MAX : ((1U << bitCount) - 1); ret |= (x1c_val >> u32(shiftAmt)) & mask; /* New bit offset */ @@ -70,10 +70,10 @@ void CBitStreamWriter::WriteEncoded(u32 val, u32 bitCount) { fmt::print(fmt(" {} {}\n"), position(), x18_bitOffset); #endif - s32 shiftAmt = x18_bitOffset - s32(bitCount); + const s32 shiftAmt = x18_bitOffset - s32(bitCount); if (shiftAmt < 0) { /* OR remaining bits to cached value */ - u32 mask = (1 << x18_bitOffset) - 1; + const u32 mask = (1U << x18_bitOffset) - 1; x14_val |= (val >> u32(-shiftAmt)) & mask; /* Write out 32-bits */ @@ -85,7 +85,7 @@ void CBitStreamWriter::WriteEncoded(u32 val, u32 bitCount) { x14_val = val << x18_bitOffset; } else { /* OR bits to cached value */ - u32 mask = bitCount == 32 ? 0xffffffff : ((1 << bitCount) - 1); + const u32 mask = bitCount == 32 ? UINT32_MAX : ((1U << bitCount) - 1); x14_val |= (val & mask) << u32(shiftAmt); /* New bit offset */ From 0546553f2dd94125cd6ee450592bc396c43256ec Mon Sep 17 00:00:00 2001 From: Lioncash Date: Sat, 9 Nov 2019 02:57:36 -0500 Subject: [PATCH 5/5] IOStreams: Replace CZipSupport with lambda functions We can safely replace the class with direct lambda functions, given they don't need to capture any state. --- Runtime/IOStreams.cpp | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/Runtime/IOStreams.cpp b/Runtime/IOStreams.cpp index 4bf6a875f..39ba621a0 100644 --- a/Runtime/IOStreams.cpp +++ b/Runtime/IOStreams.cpp @@ -109,18 +109,12 @@ void CBitStreamWriter::Flush() { x14_val = 0; } -class CZipSupport { -public: - static void* Alloc(void*, u32 c, u32 n) { return new u8[c * n]; } - static void Free(void*, void* buf) { delete[] static_cast(buf); } -}; - CZipInputStream::CZipInputStream(std::unique_ptr&& strm) : x24_compBuf(new u8[4096]), x28_strm(std::move(strm)) { x30_zstrm.next_in = x24_compBuf.get(); x30_zstrm.avail_in = 0; - x30_zstrm.zalloc = CZipSupport::Alloc; - x30_zstrm.zfree = CZipSupport::Free; + x30_zstrm.zalloc = [](void*, u32 c, u32 n) -> void* { return new u8[size_t{c} * size_t{n}]; }; + x30_zstrm.zfree = [](void*, void* buf) { delete[] static_cast(buf); }; inflateInit(&x30_zstrm); }