From d85347f1f2028afb685142a776f2af4473de8273 Mon Sep 17 00:00:00 2001 From: George Hazan Date: Thu, 15 Jun 2023 13:08:02 +0300 Subject: memory buffer unzip function moved to core --- include/m_utils.h | 9 ++++++++ libs/win32/mir_app.lib | Bin 261724 -> 262034 bytes libs/win64/mir_app.lib | Bin 260716 -> 261036 bytes protocols/WhatsApp/src/message.cpp | 2 +- protocols/WhatsApp/src/proto.h | 3 --- protocols/WhatsApp/src/server.cpp | 2 +- protocols/WhatsApp/src/utils.cpp | 38 --------------------------------- src/mir_app/src/mir_app.def | 1 + src/mir_app/src/mir_app64.def | 1 + src/mir_app/src/utils.cpp | 42 +++++++++++++++++++++++++++++++++++++ 10 files changed, 55 insertions(+), 43 deletions(-) diff --git a/include/m_utils.h b/include/m_utils.h index 0d5b599ac6..2ea38f07b4 100644 --- a/include/m_utils.h +++ b/include/m_utils.h @@ -390,6 +390,15 @@ EXTERN_C MIR_CORE_DLL(void) Utils_GetRandom(void *pszDest, size_t cbLen); EXTERN_C MIR_CORE_DLL(bool) Utils_IsRtl(const wchar_t *pszwText); +///////////////////////////////////////////////////////////////////////////////////////// +// Unzip memory buffer + +MIR_APP_DLL(MBinBuffer) Utils_Unzip(const void *pData, size_t cbLen); + +__forceinline MBinBuffer Utils_Unzip(const MBinBuffer &buf) { + return Utils_Unzip(buf.data(), buf.length()); +} + ///////////////////////////////////////////////////////////////////////////////////////// // Replace variables in text // wParam = (char*/wchar_t*/wchar_t*)string (depends on RVF_UNICODE/RVF_TCHAR flag) diff --git a/libs/win32/mir_app.lib b/libs/win32/mir_app.lib index 774406a3c7..ab50fd28ce 100644 Binary files a/libs/win32/mir_app.lib and b/libs/win32/mir_app.lib differ diff --git a/libs/win64/mir_app.lib b/libs/win64/mir_app.lib index 8a827d81f7..f275ed81a5 100644 Binary files a/libs/win64/mir_app.lib and b/libs/win64/mir_app.lib differ diff --git a/protocols/WhatsApp/src/message.cpp b/protocols/WhatsApp/src/message.cpp index cc6e9d8ff9..985728208d 100644 --- a/protocols/WhatsApp/src/message.cpp +++ b/protocols/WhatsApp/src/message.cpp @@ -254,7 +254,7 @@ void WhatsAppProto::ProcessMessage(WAMSG type, const Wa__WebMessageInfo &msg) if (auto *pHist = protoMsg->historysyncnotification) { MBinBuffer buf(DownloadEncryptedFile(directPath2url(pHist->directpath), pHist->mediakey, "History")); if (!buf.isEmpty()) { - MBinBuffer inflate(unzip(unpadBuffer16(buf))); + MBinBuffer inflate(Utils_Unzip(unpadBuffer16(buf))); proto::HistorySync sync(inflate); if (sync) diff --git a/protocols/WhatsApp/src/proto.h b/protocols/WhatsApp/src/proto.h index 0ec03e5313..ef3bb4f408 100644 --- a/protocols/WhatsApp/src/proto.h +++ b/protocols/WhatsApp/src/proto.h @@ -435,9 +435,6 @@ class WhatsAppProto : public PROTO // Binary packets void ProcessBinaryPacket(const uint8_t *pData, size_t cbLen); - // unzip operations - MBinBuffer unzip(const MBinBuffer &src); - /// Avatars //////////////////////////////////////////////////////////////////////////// CMStringW GetAvatarFileName(MCONTACT hContact); void ServerFetchAvatar(const char *jid); diff --git a/protocols/WhatsApp/src/server.cpp b/protocols/WhatsApp/src/server.cpp index 24d64e5aa7..857666b2e4 100644 --- a/protocols/WhatsApp/src/server.cpp +++ b/protocols/WhatsApp/src/server.cpp @@ -191,7 +191,7 @@ void WhatsAppProto::ProcessBinaryPacket(const uint8_t *pData, size_t cbDataLen) auto b = rdr.readInt8(); if (b & 2) { buf.remove(1); - buf = unzip(buf); + buf = Utils_Unzip(buf); rdr = WAReader(buf.data(), buf.length()); } diff --git a/protocols/WhatsApp/src/utils.cpp b/protocols/WhatsApp/src/utils.cpp index a1d234e405..c49821e21e 100644 --- a/protocols/WhatsApp/src/utils.cpp +++ b/protocols/WhatsApp/src/utils.cpp @@ -362,44 +362,6 @@ void WhatsAppProto::Popup(MCONTACT hContact, const wchar_t *szMsg, const wchar_t ///////////////////////////////////////////////////////////////////////////////////////// -MBinBuffer WhatsAppProto::unzip(const MBinBuffer &src) -{ - z_stream strm = {}; - inflateInit(&strm); - - strm.avail_in = (uInt)src.length(); - strm.next_in = (Bytef *)src.data(); - - MBinBuffer res; - Bytef buf[2048]; - - while (strm.avail_in > 0) { - strm.avail_out = sizeof(buf); - strm.next_out = buf; - - int ret = inflate(&strm, Z_NO_FLUSH); - switch (ret) { - case Z_NEED_DICT: - ret = Z_DATA_ERROR; - __fallthrough; - - case Z_DATA_ERROR: - case Z_MEM_ERROR: - inflateEnd(&strm); - return res; - } - - res.append(buf, sizeof(buf) - strm.avail_out); - if (ret == Z_STREAM_END) - break; - } - - inflateEnd(&strm); - return res; -} - -///////////////////////////////////////////////////////////////////////////////////////// - void bin2file(const MBinBuffer &buf, const wchar_t *pwszFileName) { int fileId = _wopen(pwszFileName, _O_WRONLY | _O_TRUNC | _O_BINARY | _O_CREAT, _S_IREAD | _S_IWRITE); diff --git a/src/mir_app/src/mir_app.def b/src/mir_app/src/mir_app.def index d0abb0ecb0..0f02928081 100644 --- a/src/mir_app/src/mir_app.def +++ b/src/mir_app/src/mir_app.def @@ -875,3 +875,4 @@ Clist_RemoveEvent @989 ?Clist_GetEventByMenu@@YGPAUCListEvent@@H@Z @990 NONAME ?Clist_GetEventCount@@YGHXZ @991 NONAME ?Chat_EmptyHistory@@YGXPAUSESSION_INFO@@@Z @992 NONAME +?Utils_Unzip@@YG?AVMBinBuffer@@PBXI@Z @993 NONAME diff --git a/src/mir_app/src/mir_app64.def b/src/mir_app/src/mir_app64.def index ce3a55a6fd..9fe2e92c6a 100644 --- a/src/mir_app/src/mir_app64.def +++ b/src/mir_app/src/mir_app64.def @@ -875,3 +875,4 @@ Clist_RemoveEvent @989 ?Clist_GetEventByMenu@@YAPEAUCListEvent@@H@Z @990 NONAME ?Clist_GetEventCount@@YAHXZ @991 NONAME ?Chat_EmptyHistory@@YAXPEAUSESSION_INFO@@@Z @992 NONAME +?Utils_Unzip@@YA?AVMBinBuffer@@PEBX_K@Z @993 NONAME diff --git a/src/mir_app/src/utils.cpp b/src/mir_app/src/utils.cpp index 42e916d7d7..1c23ded07a 100644 --- a/src/mir_app/src/utils.cpp +++ b/src/mir_app/src/utils.cpp @@ -23,6 +23,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #include "stdafx.h" +#include "..\..\libs\zlib\src\zlib.h" #define MS_SYSTEM_GET_MD5I "Miranda/System/GetMD5I" @@ -372,6 +373,47 @@ bool ProcessFileDrop(HDROP hDrop, MCONTACT hContact) ///////////////////////////////////////////////////////////////////////////////////////// +MIR_APP_DLL(MBinBuffer) Utils_Unzip(const void *pData, size_t cbLen) +{ + MBinBuffer res; + if (pData == nullptr || cbLen == 0) + return res; + + z_stream strm = {}; + inflateInit(&strm); + + strm.avail_in = (uInt)cbLen; + strm.next_in = (Bytef *)pData; + + Bytef buf[2048]; + + while (strm.avail_in > 0) { + strm.avail_out = sizeof(buf); + strm.next_out = buf; + + int ret = inflate(&strm, Z_NO_FLUSH); + switch (ret) { + case Z_NEED_DICT: + ret = Z_DATA_ERROR; + __fallthrough; + + case Z_DATA_ERROR: + case Z_MEM_ERROR: + inflateEnd(&strm); + return res; + } + + res.append(buf, sizeof(buf) - strm.avail_out); + if (ret == Z_STREAM_END) + break; + } + + inflateEnd(&strm); + return res; +} + +///////////////////////////////////////////////////////////////////////////////////////// + int LoadUtilsModule(void) { CreateServiceFunction(MS_UTILS_GETCOUNTRYBYNUMBER, GetCountryByNumber); -- cgit v1.2.3