From 8fd056158849d08c949e85c04c20ae9e070bd170 Mon Sep 17 00:00:00 2001 From: squidcc Date: Sun, 7 Oct 2018 00:35:20 +1000 Subject: [PATCH] clean all tmsg functions (#357) * clean all tmsg functions * type fix-ups --- Source/tmsg.cpp | 90 +++++++++++++++++-------------------------------- Source/tmsg.h | 8 ++--- structs.h | 6 ++-- 3 files changed, 38 insertions(+), 66 deletions(-) diff --git a/Source/tmsg.cpp b/Source/tmsg.cpp index f15e0cbd7..f4bc857fa 100644 --- a/Source/tmsg.cpp +++ b/Source/tmsg.cpp @@ -2,76 +2,48 @@ #include "../types.h" -TMsg *sgpTimedMsgHead; +static TMsg *sgpTimedMsgHead; -int __fastcall tmsg_get(unsigned char *pbMsg, unsigned int dwMaxLen) +int __fastcall tmsg_get(BYTE *pbMsg, DWORD dwMaxLen) { - unsigned char *v2; // ebx - DWORD v3; // eax - TMsg *v4; // esi - size_t v6; // edi + int len; + TMsg *head; - v2 = pbMsg; if ( !sgpTimedMsgHead ) return 0; - v3 = GetTickCount(); - v4 = sgpTimedMsgHead; - if ( (signed int)(sgpTimedMsgHead->hdr.dwTime - v3) >= 0 ) + + if ( (int)(sgpTimedMsgHead->hdr.dwTime - GetTickCount()) >= 0 ) return 0; - sgpTimedMsgHead = sgpTimedMsgHead->hdr.pNext; - v6 = v4->hdr.bLen; - memcpy(v2, v4->body, v6); - mem_free_dbg(v4); - return v6; + head = sgpTimedMsgHead; + sgpTimedMsgHead = head->hdr.pNext; + len = head->hdr.bLen; + // BUGFIX: ignores dwMaxLen + memcpy(pbMsg, head->body, len); + mem_free_dbg(head); + return len; } -void __fastcall tmsg_add(unsigned char *pbMsg, unsigned char bLen) +void __fastcall tmsg_add(BYTE *pbMsg, BYTE bLen) { - unsigned char v2; // bl - unsigned char *v3; // ebp - size_t v4; // edi - TMsg *v5; // eax - TMsg *v6; // esi - DWORD v7; // eax - TMsg *v8; // ecx - TMsg **v9; // eax - - v2 = bLen; - v3 = pbMsg; - v4 = bLen; - v5 = (TMsg *)DiabloAllocPtr(bLen + 12); - v6 = v5; - v5->hdr.pNext = 0; - v7 = GetTickCount(); - v6->hdr.bLen = v2; - v6->hdr.dwTime = v7 + 500; - memcpy(v6->body, v3, v4); - v8 = sgpTimedMsgHead; - v9 = &sgpTimedMsgHead; - while ( v8 ) - { - v9 = &v8->hdr.pNext; - v8 = v8->hdr.pNext; - } - *v9 = v6; + TMsg **tail; + + TMsg *msg = (TMsg*)DiabloAllocPtr(bLen + sizeof(*msg)); + msg->hdr.pNext = NULL; + msg->hdr.dwTime = GetTickCount() + 500; + msg->hdr.bLen = bLen; + memcpy(msg->body, pbMsg, bLen); + for ( tail = &sgpTimedMsgHead; *tail; tail = &(*tail)->hdr.pNext ); + *tail = msg; } -void __cdecl tmsg_cleanup() +void* __cdecl tmsg_cleanup() { - TMsg *v0; // eax - TMsg *v1; // esi - - v0 = sgpTimedMsgHead; - if ( sgpTimedMsgHead ) - { - do - { - v1 = v0->hdr.pNext; - sgpTimedMsgHead = 0; - mem_free_dbg(v0); - v0 = v1; - sgpTimedMsgHead = v1; - } - while ( v1 ); + while ( sgpTimedMsgHead ) { + TMsg *next = sgpTimedMsgHead->hdr.pNext; + TMsg *head = sgpTimedMsgHead; + sgpTimedMsgHead = NULL; + mem_free_dbg(head); + sgpTimedMsgHead = next; } + return sgpTimedMsgHead; } diff --git a/Source/tmsg.h b/Source/tmsg.h index 0396702c2..dc8b1dec1 100644 --- a/Source/tmsg.h +++ b/Source/tmsg.h @@ -2,10 +2,8 @@ #ifndef __TMSG_H__ #define __TMSG_H__ -extern TMsg *sgpTimedMsgHead; - -int __fastcall tmsg_get(unsigned char *pbMsg, unsigned int dwMaxLen); -void __fastcall tmsg_add(unsigned char *pbMsg, unsigned char bLen); -void __cdecl tmsg_cleanup(); +int __fastcall tmsg_get(BYTE *pbMsg, DWORD dwMaxLen); +void __fastcall tmsg_add(BYTE *pbMsg, BYTE bLen); +void* __cdecl tmsg_cleanup(); #endif /* __TMSG_H__ */ diff --git a/structs.h b/structs.h index 52b403e27..70941f98f 100644 --- a/structs.h +++ b/structs.h @@ -1534,13 +1534,15 @@ struct TMsg; struct TMsgHdr { TMsg *pNext; - unsigned int dwTime; - unsigned char bLen; + DWORD dwTime; + BYTE bLen; }; struct TMsg { TMsgHdr hdr; + // this is actually alignment padding, but the message body is appended to the struct + // so it's convenient to use byte-alignment and name it "body" unsigned char body[3]; }; #pragma pack(pop)