From e7f0ce27641d2b1fd220a8c2fbee3df683f45e6f Mon Sep 17 00:00:00 2001 From: Anders Jenbo Date: Sun, 27 Jun 2021 16:30:43 +0200 Subject: [PATCH] Apply cleanups to codec, control, and some of cursor --- Source/codec.cpp | 38 +++++++++++++++++--------------------- Source/control.cpp | 8 ++++---- Source/cursor.cpp | 44 ++++++++++++++++++++------------------------ Source/cursor.h | 6 +++--- 4 files changed, 44 insertions(+), 52 deletions(-) diff --git a/Source/codec.cpp b/Source/codec.cpp index 08a868c72..d828c8d97 100644 --- a/Source/codec.cpp +++ b/Source/codec.cpp @@ -25,25 +25,24 @@ struct CodecSignature { static void CodecInitKey(const char *pszPassword) { char key[136]; // last 64 bytes are the SHA1 - uint32_t rand_state = 0x7058; + uint32_t randState = 0x7058; for (char ¬ch : key) { - rand_state = rand_state * 214013 + 2531011; - notch = rand_state >> 16; // Downcasting to char keeps the 2 least-significant bytes + randState = randState * 214013 + 2531011; + notch = randState >> 16; // Downcasting to char keeps the 2 least-significant bytes } char pw[64]; - std::size_t password_i = 0; - for (std::size_t i = 0; i < sizeof(pw); ++i, ++password_i) { - if (pszPassword[password_i] == '\0') - password_i = 0; - pw[i] = pszPassword[password_i]; + for (std::size_t i = 0; i < sizeof(pw); i++) { + if (pszPassword[i] == '\0') + i = 0; + pw[i] = pszPassword[i]; } char digest[SHA1HashSize]; SHA1Reset(0); SHA1Calculate(0, pw, digest); SHA1Clear(); - for (std::size_t i = 0; i < sizeof(key); ++i) + for (std::size_t i = 0; i < sizeof(key); i++) key[i] ^= digest[i % SHA1HashSize]; memset(pw, 0, sizeof(pw)); memset(digest, 0, sizeof(digest)); @@ -85,7 +84,7 @@ std::size_t codec_decode(byte *pbSrcDst, std::size_t size, const char *pszPasswo } SHA1Result(0, dst); - if (sig->checksum != *(DWORD *)dst) { + if (sig->checksum != *(uint32_t *)dst) { memset(dst, 0, sizeof(dst)); goto error; } @@ -105,22 +104,19 @@ std::size_t codec_get_encoded_len(std::size_t dwSrcBytes) return dwSrcBytes + sizeof(CodecSignature); } -void codec_encode(byte *pbSrcDst, std::size_t size, std::size_t size_64, const char *pszPassword) +void codec_encode(byte *pbSrcDst, std::size_t size, std::size_t size64, const char *pszPassword) { char buf[128]; char tmp[SHA1HashSize]; char dst[SHA1HashSize]; - DWORD chunk; - uint16_t last_chunk; - CodecSignature *sig; - if (size_64 != codec_get_encoded_len(size)) + if (size64 != codec_get_encoded_len(size)) app_fatal("Invalid encode parameters"); CodecInitKey(pszPassword); - last_chunk = 0; + uint16_t lastChunk = 0; while (size != 0) { - chunk = size < BLOCKSIZE ? size : BLOCKSIZE; + uint16_t chunk = size < BLOCKSIZE ? size : BLOCKSIZE; memcpy(buf, pbSrcDst, chunk); if (chunk < BLOCKSIZE) memset(buf + chunk, 0, BLOCKSIZE - chunk); @@ -131,17 +127,17 @@ void codec_encode(byte *pbSrcDst, std::size_t size, std::size_t size_64, const c } memset(dst, 0, sizeof(dst)); memcpy(pbSrcDst, buf, BLOCKSIZE); - last_chunk = chunk; + lastChunk = chunk; pbSrcDst += BLOCKSIZE; size -= chunk; } memset(buf, 0, sizeof(buf)); SHA1Result(0, tmp); - sig = (CodecSignature *)pbSrcDst; + auto *sig = (CodecSignature *)pbSrcDst; sig->error = 0; sig->unused = 0; - sig->checksum = *(DWORD *)&tmp[0]; - sig->last_chunk_size = last_chunk; + sig->checksum = *(uint32_t *)&tmp[0]; + sig->last_chunk_size = lastChunk; SHA1Clear(); } diff --git a/Source/control.cpp b/Source/control.cpp index 00205298f..f36c0f4f9 100644 --- a/Source/control.cpp +++ b/Source/control.cpp @@ -206,10 +206,10 @@ const char *const PanBtnStr[8] = { }; /** Maps from attribute_id to the rectangle on screen used for attribute increment buttons. */ Rectangle ChrBtnsRect[4] = { - { 137, 138, 41, 22 }, - { 137, 166, 41, 22 }, - { 137, 195, 41, 22 }, - { 137, 223, 41, 22 } + { {137, 138}, {41, 22} }, + { {137, 166}, {41, 22} }, + { {137, 195}, {41, 22} }, + { {137, 223}, {41, 22} } }; /** Maps from spellbook page number and position to spell_id. */ diff --git a/Source/cursor.cpp b/Source/cursor.cpp index 67497f18b..b34ec88fb 100644 --- a/Source/cursor.cpp +++ b/Source/cursor.cpp @@ -159,39 +159,39 @@ int GetInvItemFrame(int i) return i < InvItems1Size ? i : i - (InvItems1Size - 1); } -Size GetInvItemSize(int i) +Size GetInvItemSize(int cursId) { - if (i >= InvItems1Size) - return { InvItemWidth2[i - (InvItems1Size - 1)], InvItemHeight2[i - (InvItems1Size - 1)] }; - return { InvItemWidth1[i], InvItemHeight1[i] }; + if (cursId >= InvItems1Size) + return { InvItemWidth2[cursId - (InvItems1Size - 1)], InvItemHeight2[cursId - (InvItems1Size - 1)] }; + return { InvItemWidth1[cursId], InvItemHeight1[cursId] }; } -void SetICursor(int i) +void SetICursor(int cursId) { - auto size = GetInvItemSize(i); + auto size = GetInvItemSize(cursId); icursW = size.width; icursH = size.height; icursW28 = icursW / 28; icursH28 = icursH / 28; } -void NewCursor(int i) +void NewCursor(int cursId) { - pcurs = i; - auto size = GetInvItemSize(i); + pcurs = cursId; + auto size = GetInvItemSize(cursId); cursW = size.width; cursH = size.height; - SetICursor(i); - if (IsHardwareCursorEnabled() && GetCurrentCursorInfo() != CursorInfo::GameCursor(pcurs) && pcurs != CURSOR_NONE) { - SetHardwareCursor(CursorInfo::GameCursor(pcurs)); + SetICursor(cursId); + if (IsHardwareCursorEnabled() && GetCurrentCursorInfo() != CursorInfo::GameCursor(cursId) && cursId != CURSOR_NONE) { + SetHardwareCursor(CursorInfo::GameCursor(cursId)); } } -void CelDrawCursor(const CelOutputBuffer &out, Point position, int pcurs) +void CelDrawCursor(const CelOutputBuffer &out, Point position, int cursId) { - const auto &sprite = GetInvItemSprite(pcurs); - const int frame = GetInvItemFrame(pcurs); - if (IsItemSprite(pcurs)) { + const auto &sprite = GetInvItemSprite(cursId); + const int frame = GetInvItemFrame(cursId); + if (IsItemSprite(cursId)) { const auto &heldItem = plr[myplr].HoldItem; CelBlitOutlineTo(out, GetOutlineColor(heldItem, true), position, sprite, frame, false); CelDrawItem(heldItem._iStatFlag, out, position, sprite, frame); @@ -215,10 +215,8 @@ void InitLevelCursor() void CheckTown() { - int i, mx; - - for (i = 0; i < nummissiles; i++) { - mx = missileactive[i]; + for (int i = 0; i < nummissiles; i++) { + int mx = missileactive[i]; if (missile[mx]._mitype == MIS_TOWN) { if ((cursmx == missile[mx].position.tile.x - 1 && cursmy == missile[mx].position.tile.y) || (cursmx == missile[mx].position.tile.x && cursmy == missile[mx].position.tile.y - 1) @@ -241,10 +239,8 @@ void CheckTown() void CheckRportal() { - int i, mx; - - for (i = 0; i < nummissiles; i++) { - mx = missileactive[i]; + for (int i = 0; i < nummissiles; i++) { + int mx = missileactive[i]; if (missile[mx]._mitype == MIS_RPORTAL) { if ((cursmx == missile[mx].position.tile.x - 1 && cursmy == missile[mx].position.tile.y) || (cursmx == missile[mx].position.tile.x && cursmy == missile[mx].position.tile.y - 1) diff --git a/Source/cursor.h b/Source/cursor.h index ae1d0a5a5..dc209b631 100644 --- a/Source/cursor.h +++ b/Source/cursor.h @@ -55,12 +55,12 @@ void CheckRportal(); void CheckTown(); void CheckCursMove(); -inline bool IsItemSprite(int pcurs) +inline bool IsItemSprite(int cursId) { - return pcurs >= CURSOR_FIRSTITEM; + return cursId >= CURSOR_FIRSTITEM; } -void CelDrawCursor(const CelOutputBuffer &out, Point position, int pcurs); +void CelDrawCursor(const CelOutputBuffer &out, Point position, int cursId); /** Returns the sprite for the given inventory index. */ const CelSprite &GetInvItemSprite(int i);