From b14f9e8965f5d816018ef2ec57eaeec72bc9e239 Mon Sep 17 00:00:00 2001 From: Armin Novak Date: Wed, 19 Mar 2025 16:33:39 +0100 Subject: [PATCH] [winpr] refactor complex expressions * avoid unnecessary casts --- winpr/include/winpr/bitstream.h | 53 ++++++++++++++----------- winpr/include/winpr/interlocked.h | 1 - winpr/include/winpr/rpc.h | 1 - winpr/include/winpr/timezone.h | 7 ++-- winpr/include/winpr/winsock.h | 1 - winpr/libwinpr/sspi/NTLM/ntlm_message.c | 6 +-- winpr/libwinpr/sspi/NTLM/ntlm_message.h | 12 +++--- winpr/libwinpr/timezone/timezone.c | 2 +- 8 files changed, 43 insertions(+), 40 deletions(-) diff --git a/winpr/include/winpr/bitstream.h b/winpr/include/winpr/bitstream.h index 4a8327eee..b8e8d1fb9 100644 --- a/winpr/include/winpr/bitstream.h +++ b/winpr/include/winpr/bitstream.h @@ -53,42 +53,47 @@ extern "C" WINPR_ASSERT(_bs); (_bs->prefetch) = 0; - if (((UINT32)(_bs->pointer - _bs->buffer) + 4) < (_bs->capacity)) - (_bs->prefetch) |= ((UINT32) * (_bs->pointer + 4) << 24); - if (((UINT32)(_bs->pointer - _bs->buffer) + 5) < (_bs->capacity)) - (_bs->prefetch) |= ((UINT32) * (_bs->pointer + 5) << 16); - if (((UINT32)(_bs->pointer - _bs->buffer) + 6) < (_bs->capacity)) - (_bs->prefetch) |= ((UINT32) * (_bs->pointer + 6) << 8); - if (((UINT32)(_bs->pointer - _bs->buffer) + 7) < (_bs->capacity)) - (_bs->prefetch) |= ((UINT32) * (_bs->pointer + 7) << 0); + + const intptr_t diff = _bs->pointer - _bs->buffer; + if ((diff + 4) < _bs->capacity) + (_bs->prefetch) |= ((UINT32)_bs->pointer[4] << 24); + if ((diff + 5) < _bs->capacity) + (_bs->prefetch) |= ((UINT32)_bs->pointer[5] << 16); + if ((diff + 6) < _bs->capacity) + (_bs->prefetch) |= ((UINT32)_bs->pointer[6] << 8); + if ((diff + 7) < _bs->capacity) + (_bs->prefetch) |= ((UINT32)_bs->pointer[7] << 0); } static INLINE void BitStream_Fetch(wBitStream* _bs) { WINPR_ASSERT(_bs); (_bs->accumulator) = 0; - if (((UINT32)(_bs->pointer - _bs->buffer) + 0) < (_bs->capacity)) - (_bs->accumulator) |= ((UINT32) * (_bs->pointer + 0) << 24); - if (((UINT32)(_bs->pointer - _bs->buffer) + 1) < (_bs->capacity)) - (_bs->accumulator) |= ((UINT32) * (_bs->pointer + 1) << 16); - if (((UINT32)(_bs->pointer - _bs->buffer) + 2) < (_bs->capacity)) - (_bs->accumulator) |= ((UINT32) * (_bs->pointer + 2) << 8); - if (((UINT32)(_bs->pointer - _bs->buffer) + 3) < (_bs->capacity)) - (_bs->accumulator) |= ((UINT32) * (_bs->pointer + 3) << 0); + + const intptr_t diff = _bs->pointer - _bs->buffer; + if ((diff + 0) < _bs->capacity) + (_bs->accumulator) |= ((UINT32)_bs->pointer[0] << 24); + if ((diff + 1) < _bs->capacity) + (_bs->accumulator) |= ((UINT32)_bs->pointer[1] << 16); + if ((diff + 2) < _bs->capacity) + (_bs->accumulator) |= ((UINT32)_bs->pointer[2] << 8); + if ((diff + 3) < _bs->capacity) + (_bs->accumulator) |= ((UINT32)_bs->pointer[3] << 0); BitStream_Prefetch(_bs); } static INLINE void BitStream_Flush(wBitStream* _bs) { WINPR_ASSERT(_bs); - if (((UINT32)(_bs->pointer - _bs->buffer) + 0) < (_bs->capacity)) - *(_bs->pointer + 0) = (BYTE)((UINT32)_bs->accumulator >> 24); - if (((UINT32)(_bs->pointer - _bs->buffer) + 1) < (_bs->capacity)) - *(_bs->pointer + 1) = (BYTE)((UINT32)_bs->accumulator >> 16); - if (((UINT32)(_bs->pointer - _bs->buffer) + 2) < (_bs->capacity)) - *(_bs->pointer + 2) = (BYTE)((UINT32)_bs->accumulator >> 8); - if (((UINT32)(_bs->pointer - _bs->buffer) + 3) < (_bs->capacity)) - *(_bs->pointer + 3) = (BYTE)((UINT32)_bs->accumulator >> 0); + const intptr_t diff = _bs->pointer - _bs->buffer; + if ((diff + 0) < _bs->capacity) + _bs->pointer[0] = (BYTE)((UINT32)_bs->accumulator >> 24); + if ((diff + 1) < _bs->capacity) + _bs->pointer[1] = (BYTE)((UINT32)_bs->accumulator >> 16); + if ((diff + 2) < _bs->capacity) + _bs->pointer[2] = (BYTE)((UINT32)_bs->accumulator >> 8); + if ((diff + 3) < _bs->capacity) + _bs->pointer[3] = (BYTE)((UINT32)_bs->accumulator >> 0); } static INLINE void BitStream_Shift(wBitStream* _bs, UINT32 _nbits) diff --git a/winpr/include/winpr/interlocked.h b/winpr/include/winpr/interlocked.h index a0f952116..d38b1b571 100644 --- a/winpr/include/winpr/interlocked.h +++ b/winpr/include/winpr/interlocked.h @@ -24,7 +24,6 @@ #include #include #include -#include #ifdef __cplusplus extern "C" diff --git a/winpr/include/winpr/rpc.h b/winpr/include/winpr/rpc.h index 4bfb3af15..0610cff2c 100644 --- a/winpr/include/winpr/rpc.h +++ b/winpr/include/winpr/rpc.h @@ -41,7 +41,6 @@ typedef PCONTEXT_HANDLE PCHANNEL_CONTEXT_HANDLE_SERIALIZE; #else #include -#include #include #include #include diff --git a/winpr/include/winpr/timezone.h b/winpr/include/winpr/timezone.h index 4f9d41418..a22282da8 100644 --- a/winpr/include/winpr/timezone.h +++ b/winpr/include/winpr/timezone.h @@ -110,10 +110,11 @@ extern "C" #if !defined(_WIN32) || (defined(_WIN32) && (_WIN32_WINNT < 0x0602)) /* Windows 8 */ - WINPR_API DWORD EnumDynamicTimeZoneInformation( - const DWORD dwIndex, PDYNAMIC_TIME_ZONE_INFORMATION lpTimeZoneInformation); + WINPR_API + DWORD EnumDynamicTimeZoneInformation(DWORD dwIndex, + PDYNAMIC_TIME_ZONE_INFORMATION lpTimeZoneInformation); WINPR_API DWORD GetDynamicTimeZoneInformationEffectiveYears( - const PDYNAMIC_TIME_ZONE_INFORMATION lpTimeZoneInformation, LPDWORD FirstYear, + const DYNAMIC_TIME_ZONE_INFORMATION* lpTimeZoneInformation, LPDWORD FirstYear, LPDWORD LastYear); #else diff --git a/winpr/include/winpr/winsock.h b/winpr/include/winpr/winsock.h index 95bcce6c2..24db06fac 100644 --- a/winpr/include/winpr/winsock.h +++ b/winpr/include/winpr/winsock.h @@ -90,7 +90,6 @@ WINPR_API INT winpr_inet_pton(INT Family, PCSTR pszAddrString, PVOID pAddrBuf); #include #include -#include #define WSAEVENT HANDLE #define LPWSAEVENT LPHANDLE diff --git a/winpr/libwinpr/sspi/NTLM/ntlm_message.c b/winpr/libwinpr/sspi/NTLM/ntlm_message.c index de941346f..5c6f994d4 100644 --- a/winpr/libwinpr/sspi/NTLM/ntlm_message.c +++ b/winpr/libwinpr/sspi/NTLM/ntlm_message.c @@ -563,7 +563,7 @@ SECURITY_STATUS ntlm_read_NegotiateMessage(NTLM_CONTEXT* context, PSecBuffer buf return SEC_I_CONTINUE_NEEDED; } -SECURITY_STATUS ntlm_write_NegotiateMessage(NTLM_CONTEXT* context, const PSecBuffer buffer) +SECURITY_STATUS ntlm_write_NegotiateMessage(NTLM_CONTEXT* context, SecBuffer* buffer) { wStream sbuffer; wStream* s = NULL; @@ -813,7 +813,7 @@ fail: return status; } -SECURITY_STATUS ntlm_write_ChallengeMessage(NTLM_CONTEXT* context, const PSecBuffer buffer) +SECURITY_STATUS ntlm_write_ChallengeMessage(NTLM_CONTEXT* context, SecBuffer* buffer) { wStream sbuffer; wStream* s = NULL; @@ -1212,7 +1212,7 @@ fail: * @param buffer The buffer to write */ -SECURITY_STATUS ntlm_write_AuthenticateMessage(NTLM_CONTEXT* context, const PSecBuffer buffer) +SECURITY_STATUS ntlm_write_AuthenticateMessage(NTLM_CONTEXT* context, SecBuffer* buffer) { wStream sbuffer; wStream* s = NULL; diff --git a/winpr/libwinpr/sspi/NTLM/ntlm_message.h b/winpr/libwinpr/sspi/NTLM/ntlm_message.h index 58ff35dd5..c7cd072c1 100644 --- a/winpr/libwinpr/sspi/NTLM/ntlm_message.h +++ b/winpr/libwinpr/sspi/NTLM/ntlm_message.h @@ -22,12 +22,12 @@ #include "ntlm.h" -SECURITY_STATUS ntlm_read_NegotiateMessage(NTLM_CONTEXT* context, PSecBuffer buffer); -SECURITY_STATUS ntlm_write_NegotiateMessage(NTLM_CONTEXT* context, const PSecBuffer buffer); -SECURITY_STATUS ntlm_read_ChallengeMessage(NTLM_CONTEXT* context, PSecBuffer buffer); -SECURITY_STATUS ntlm_write_ChallengeMessage(NTLM_CONTEXT* context, const PSecBuffer buffer); -SECURITY_STATUS ntlm_read_AuthenticateMessage(NTLM_CONTEXT* context, PSecBuffer buffer); -SECURITY_STATUS ntlm_write_AuthenticateMessage(NTLM_CONTEXT* context, const PSecBuffer buffer); +SECURITY_STATUS ntlm_read_NegotiateMessage(NTLM_CONTEXT* context, SecBuffer* buffer); +SECURITY_STATUS ntlm_write_NegotiateMessage(NTLM_CONTEXT* context, SecBuffer* buffer); +SECURITY_STATUS ntlm_read_ChallengeMessage(NTLM_CONTEXT* context, SecBuffer* buffer); +SECURITY_STATUS ntlm_write_ChallengeMessage(NTLM_CONTEXT* context, SecBuffer* buffer); +SECURITY_STATUS ntlm_read_AuthenticateMessage(NTLM_CONTEXT* context, SecBuffer* buffer); +SECURITY_STATUS ntlm_write_AuthenticateMessage(NTLM_CONTEXT* context, SecBuffer* buffer); SECURITY_STATUS ntlm_server_AuthenticateComplete(NTLM_CONTEXT* context); diff --git a/winpr/libwinpr/timezone/timezone.c b/winpr/libwinpr/timezone/timezone.c index 133de5e60..0123a76be 100644 --- a/winpr/libwinpr/timezone/timezone.c +++ b/winpr/libwinpr/timezone/timezone.c @@ -882,7 +882,7 @@ DWORD EnumDynamicTimeZoneInformation(const DWORD dwIndex, // NOLINTBEGIN(readability-non-const-parameter) DWORD GetDynamicTimeZoneInformationEffectiveYears( - const PDYNAMIC_TIME_ZONE_INFORMATION lpTimeZoneInformation, LPDWORD FirstYear, LPDWORD LastYear) + const DYNAMIC_TIME_ZONE_INFORMATION* lpTimeZoneInformation, LPDWORD FirstYear, LPDWORD LastYear) // NOLINTEND(readability-non-const-parameter) { WINPR_UNUSED(lpTimeZoneInformation);