From c16bd3d58396036f078282ad0b7032562c0c0533 Mon Sep 17 00:00:00 2001 From: George Hazan Date: Tue, 1 Sep 2015 15:18:56 +0000 Subject: - adaptation for the kernel strdel(); - buffer overrun removed (rare crashes); - old useless code removed; - removal of a blank line after comments was a bad idea git-svn-id: http://svn.miranda-ng.org/main/trunk@15136 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- plugins/TabSRMM/src/utils.cpp | 205 +++++++++++++++++++----------------------- 1 file changed, 90 insertions(+), 115 deletions(-) (limited to 'plugins/TabSRMM/src/utils.cpp') diff --git a/plugins/TabSRMM/src/utils.cpp b/plugins/TabSRMM/src/utils.cpp index bad2772b2c..08d19e775f 100644 --- a/plugins/TabSRMM/src/utils.cpp +++ b/plugins/TabSRMM/src/utils.cpp @@ -1,4 +1,4 @@ -///////////////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////////////// // Miranda NG: the free IM client for Microsoft* Windows* // // Copyright (с) 2012-15 Miranda NG project, @@ -62,43 +62,11 @@ static TCHAR *formatting_strings_end[] = { _T("b0 "), _T("i0 "), _T("u0 "), _ #define NR_CODES 5 -TCHAR* Utils::FilterEventMarkers(TCHAR *wszText) -{ - tstring text(wszText); - size_t beginmark = 0, endmark = 0; - - while (true) { - if ((beginmark = text.find(_T("~-+"))) != text.npos) { - endmark = text.find(_T("+-~"), beginmark); - if (endmark != text.npos && (endmark - beginmark) > 5) { - text.erase(beginmark, (endmark - beginmark) + 3); - continue; - } - break; - } - break; - } - - while (true) { - if ((beginmark = text.find(_T("\xAA"))) != text.npos) { - endmark = beginmark + 2; - if (endmark != text.npos && (endmark - beginmark) > 1) { - text.erase(beginmark, endmark - beginmark); - continue; - } - break; - } - break; - } - - mir_tstrcpy(wszText, text.c_str()); - return wszText; -} - ///////////////////////////////////////////////////////////////////////////////////////// // this translates formatting tags into rtf sequences... // flags: loword = words only for simple * /_ formatting // hiword = bbcode support (strip bbcodes if 0) + const TCHAR* Utils::FormatRaw(TWindowData *dat, const TCHAR *msg, int flags, BOOL isSent) { bool clr_was_added = false, was_added; @@ -354,62 +322,50 @@ bool Utils::FormatTitleBar(const TWindowData *dat, const TCHAR *szFormat, CMStri dest.Append(tszGroup); } break; + + case 0: // wrongly formed format string + return true; } } return true; } +///////////////////////////////////////////////////////////////////////////////////////// + char* Utils::FilterEventMarkers(char *szText) { - std::string text(szText); - size_t beginmark = 0, endmark = 0; - - while (true) { - if ((beginmark = text.find("~-+")) != text.npos) { - endmark = text.find("+-~", beginmark); - if (endmark != text.npos && (endmark - beginmark) > 5) { - text.erase(beginmark, (endmark - beginmark) + 3); - continue; - } + for (char *p = strstr(szText, "~-+"); p != NULL; p = strstr(p, "~-+")) { + char *pEnd = strstr(p + 3, "+-~"); + if (pEnd == NULL) break; - } - break; - } - while (true) { - if ((beginmark = text.find("\xAA")) != text.npos) { - endmark = beginmark + 2; - if (endmark != text.npos && (endmark - beginmark) > 1) { - text.erase(beginmark, endmark - beginmark); - continue; - } - break; - } - break; + strdel(p, (pEnd - p) + 3); } - // - mir_strcpy(szText, text.c_str()); + return szText; } -const TCHAR* Utils::DoubleAmpersands(TCHAR *pszText) +WCHAR* Utils::FilterEventMarkers(WCHAR *wszText) { - tstring text(pszText); - - size_t textPos = 0; + for (WCHAR *p = wcsstr(wszText, L"~-+"); p != NULL; p = wcsstr(p, L"~-+")) { + WCHAR *pEnd = wcsstr(p + 3, L"+-~"); + if (pEnd == NULL) + break; - while (true) { - if ((textPos = text.find(_T("&"), textPos)) != text.npos) { - text.insert(textPos, _T("%")); - text.replace(textPos, 2, _T("&&")); - textPos += 2; - continue; - } - break; + strdelw(p, (pEnd - p) + 3); } - mir_tstrcpy(pszText, text.c_str()); - return pszText; + + return wszText; +} + +///////////////////////////////////////////////////////////////////////////////////////// + +void Utils::DoubleAmpersands(TCHAR *pszText, size_t len) +{ + CMString text(pszText); + text.Replace(_T("&"), _T("&&")); + mir_tstrncpy(pszText, text.c_str(), len); } ///////////////////////////////////////////////////////////////////////////////////////// @@ -418,6 +374,7 @@ const TCHAR* Utils::DoubleAmpersands(TCHAR *pszText) // @param szText source text // @param iMaxLen max length of the preview // @return TCHAR* result (caller must mir_free() it) + TCHAR* Utils::GetPreviewWithEllipsis(TCHAR *szText, size_t iMaxLen) { size_t uRequired; @@ -453,6 +410,7 @@ TCHAR* Utils::GetPreviewWithEllipsis(TCHAR *szText, size_t iMaxLen) ///////////////////////////////////////////////////////////////////////////////////////// // returns != 0 when one of the installed keyboard layouts belongs to an rtl language // used to find out whether we need to configure the message input box for bidirectional mode + int Utils::FindRTLLocale(TWindowData *dat) { HKL layouts[20]; @@ -478,6 +436,7 @@ int Utils::FindRTLLocale(TWindowData *dat) ///////////////////////////////////////////////////////////////////////////////////////// // init default color table. the table may grow when using custom colors via bbcodes + void Utils::RTF_CTableInit() { int iSize = sizeof(TRTFColorTable) * RTF_CTABLE_DEFSIZE; @@ -490,6 +449,7 @@ void Utils::RTF_CTableInit() ///////////////////////////////////////////////////////////////////////////////////////// // add a color to the global rtf color table + void Utils::RTF_ColorAdd(const TCHAR *tszColname, size_t length) { TCHAR *stopped; @@ -506,6 +466,7 @@ void Utils::RTF_ColorAdd(const TCHAR *tszColname, size_t length) ///////////////////////////////////////////////////////////////////////////////////////// // generic error popup dialog procedure + LRESULT CALLBACK Utils::PopupDlgProcError(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { MCONTACT hContact = PUGetContact(hWnd); @@ -532,6 +493,7 @@ LRESULT CALLBACK Utils::PopupDlgProcError(HWND hWnd, UINT message, WPARAM wParam // @param hContact: contact handle (0 = read global) // @param cs TContainerSettings* target structure // @return 0 on success, 1 failure (blob does not exist OR is not a valid private setting structure + int Utils::ReadContainerSettingsFromDB(const MCONTACT hContact, TContainerSettings *cs, const char *szKey) { memcpy(cs, &PluginConfig.globalContainerSettings, sizeof(TContainerSettings)); @@ -581,6 +543,7 @@ void Utils::ContainerToSettings(TContainerData *pContainer) // // @param pContainer container window info struct // @param fForce true -> force them private, even if they were not marked as private in the db + void Utils::ReadPrivateContainerSettings(TContainerData *pContainer, bool fForce) { char szCname[50]; @@ -625,6 +588,7 @@ void Utils::SaveContainerSettings(TContainerData *pContainer, const char *szSett // // @param: maxHeight - determines maximum height for the picture, width will // be scaled accordingly. + void Utils::scaleAvatarHeightLimited(const HBITMAP hBm, double& dNewWidth, double& dNewHeight, LONG maxHeight) { BITMAP bm; @@ -656,6 +620,7 @@ void Utils::scaleAvatarHeightLimited(const HBITMAP hBm, double& dNewWidth, doubl // // @param dat: _MessageWindowData* pointer to the window data // @return HICON: the icon handle + HICON Utils::iconFromAvatar(const TWindowData *dat) { if (!ServiceExists(MS_AV_GETAVATARBITMAP) || dat == NULL) @@ -744,6 +709,7 @@ void Utils::getIconSize(HICON hIcon, int& sizeX, int& sizeY) // @param szText menu item text (must NOT be 0) // @param uID the item command id // @param pos zero-based position index + void Utils::addMenuItem(const HMENU& m, MENUITEMINFO &mii, HICON hIcon, const TCHAR *szText, UINT uID, UINT pos) { mii.wID = uID; @@ -757,6 +723,7 @@ void Utils::addMenuItem(const HMENU& m, MENUITEMINFO &mii, HICON hIcon, const TC ///////////////////////////////////////////////////////////////////////////////////////// // return != 0 when the sound effect must be played for the given // session. Uses container sound settings + int Utils::mustPlaySound(const TWindowData *dat) { if (!dat) @@ -787,6 +754,7 @@ int Utils::mustPlaySound(const TWindowData *dat) ///////////////////////////////////////////////////////////////////////////////////////// // enable or disable a dialog control + void Utils::enableDlgControl(const HWND hwnd, UINT id, bool fEnable) { ::EnableWindow(::GetDlgItem(hwnd, id), fEnable); @@ -794,6 +762,7 @@ void Utils::enableDlgControl(const HWND hwnd, UINT id, bool fEnable) ///////////////////////////////////////////////////////////////////////////////////////// // show or hide a dialog control + void Utils::showDlgControl(const HWND hwnd, UINT id, int showCmd) { ::ShowWindow(::GetDlgItem(hwnd, id), showCmd); @@ -807,7 +776,7 @@ DWORD CALLBACK Utils::StreamOut(DWORD_PTR dwCookie, LPBYTE pbBuff, LONG cb, LONG HANDLE hFile = CreateFile(szFilename, GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); if (hFile != INVALID_HANDLE_VALUE) { SetFilePointer(hFile, 0, NULL, FILE_END); - FilterEventMarkers(reinterpret_cast(pbBuff)); + FilterEventMarkers((char*)pbBuff); WriteFile(hFile, pbBuff, cb, (DWORD *)pcb, NULL); *pcb = cb; CloseHandle(hFile); @@ -819,6 +788,7 @@ DWORD CALLBACK Utils::StreamOut(DWORD_PTR dwCookie, LPBYTE pbBuff, LONG cb, LONG ///////////////////////////////////////////////////////////////////////////////////////// // extract a resource from the given module // tszPath must end with \ + bool Utils::extractResource(const HMODULE h, const UINT uID, const TCHAR *tszName, const TCHAR *tszPath, const TCHAR *tszFilename, bool fForceOverwrite) { @@ -851,6 +821,7 @@ bool Utils::extractResource(const HMODULE h, const UINT uID, const TCHAR *tszNam // caller MUST mir_free() the returned string // @param hwndRich - rich edit window handle // @return wchar_t* extracted URL + TCHAR* Utils::extractURLFromRichEdit(const ENLINK* _e, const HWND hwndRich) { CHARRANGE sel = { 0 }; @@ -872,6 +843,7 @@ TCHAR* Utils::extractURLFromRichEdit(const ENLINK* _e, const HWND hwndRich) ///////////////////////////////////////////////////////////////////////////////////////// // generic command dispatcher // used in various places (context menus, info panel menus etc.) + LRESULT Utils::CmdDispatcher(UINT uType, HWND hwndDlg, UINT cmd, WPARAM wParam, LPARAM lParam, TWindowData *dat, TContainerData *pContainer) { switch (uType) { @@ -898,6 +870,7 @@ LRESULT Utils::CmdDispatcher(UINT uType, HWND hwndDlg, UINT cmd, WPARAM wParam, // or folder name. All invalid characters will be replaced by spaces. // // @param tszFilename - string to filter. + void Utils::sanitizeFilename(wchar_t* tszFilename) { static wchar_t *forbiddenCharacters = L"%/\\':|\"<>?"; @@ -914,6 +887,7 @@ void Utils::sanitizeFilename(wchar_t* tszFilename) ///////////////////////////////////////////////////////////////////////////////////////// // ensure that a path name ends on a trailing backslash // @param szPathname - pathname to check + void Utils::ensureTralingBackslash(wchar_t *szPathname) { if (szPathname[mir_wstrlen(szPathname) - 1] != '\\') @@ -925,6 +899,7 @@ void Utils::ensureTralingBackslash(wchar_t *szPathname) // handle. // // return 0 and throw an exception if something goes wrong. + HMODULE Utils::loadSystemLibrary(const wchar_t* szFilename) { wchar_t sysPathName[MAX_PATH + 2]; @@ -945,6 +920,7 @@ HMODULE Utils::loadSystemLibrary(const wchar_t* szFilename) ///////////////////////////////////////////////////////////////////////////////////////// // setting avatar's contact + void Utils::setAvatarContact(HWND hWnd, MCONTACT hContact) { MCONTACT hSub = db_mc_getSrmmSub(hContact); @@ -953,6 +929,7 @@ void Utils::setAvatarContact(HWND hWnd, MCONTACT hContact) ///////////////////////////////////////////////////////////////////////////////////////// // stub for copying data to clipboard + size_t Utils::CopyToClipBoard(const wchar_t *str, const HWND hwndOwner) { if (!OpenClipboard(hwndOwner) || str == 0) @@ -972,6 +949,7 @@ size_t Utils::CopyToClipBoard(const wchar_t *str, const HWND hwndOwner) ///////////////////////////////////////////////////////////////////////////////////////// // file list handler + void Utils::AddToFileList(TCHAR ***pppFiles, int *totalCount, LPCTSTR szFilename) { *pppFiles = (TCHAR**)mir_realloc(*pppFiles, (++*totalCount + 1) * sizeof(TCHAR*)); @@ -1000,23 +978,23 @@ void Utils::AddToFileList(TCHAR ***pppFiles, int *totalCount, LPCTSTR szFilename ///////////////////////////////////////////////////////////////////////////////////////// // implementation of the CWarning class +// +// IMPORTANT note to translators for translation of the warning dialogs: +// +// Make sure to NOT remove the pipe character ( | ) from the strings. This separates the +// warning title from the actual warning text. +// +// Also, do NOT insert multiple | characters in the translated string. Not well-formatted +// warnings cannot be translated and the plugin will show the untranslated versions. +// +// strings marked with a NOT TRANSLATABLE comment cannot be translated at all. This +// will be used for important and critical error messages only. +// +// some strings are empty, this is intentional and used for error messages that share +// the message with other possible error notifications (popups, tool tips etc.) +// +// Entries that do not use the LPGENT() macro are NOT TRANSLATABLE, so don't bother translating them. -/** IMPORTANT note to translators for translation of the warning dialogs: - * - * Make sure to NOT remove the pipe character ( | ) from the strings. This separates the - * warning title from the actual warning text. - * - * Also, do NOT insert multiple | characters in the translated string. Not well-formatted - * warnings cannot be translated and the plugin will show the untranslated versions. - * - * strings marked with a NOT TRANSLATABLE comment cannot be translated at all. This - * will be used for important and critical error messages only. - * - * some strings are empty, this is intentional and used for error messages that share - * the message with other possible error notifications (popups, tool tips etc.) - * - * Entries that do not use the LPGENT() macro are NOT TRANSLATABLE, so don't bother translating them. - */ static wchar_t* warnings[] = { LPGENT("Important release notes|A test warning message"), /* WARN_TEST */ /* reserved for important notes after upgrade - NOT translatable */ LPGENT("Icon pack version check|The installed icon pack is outdated and might be incompatible with TabSRMM version 3.\n\n\\b1Missing or misplaced icons are possible issues with the currently installed icon pack.\\b0"), /* WARN_ICONPACKVERSION */ @@ -1069,22 +1047,23 @@ __int64 CWarning::getMask() return(mask); } -/** - * send cancel message to all open warning dialogs so they are destroyed - * before TabSRMM is unloaded. - * - * called by the OkToExit handler in globals.cpp - */ +///////////////////////////////////////////////////////////////////////////////////////// +// send cancel message to all open warning dialogs so they are destroyed +// before TabSRMM is unloaded. +// +// called by the OkToExit handler in globals.cpp + void CWarning::destroyAll() { if (hWindowList) WindowList_Broadcast(hWindowList, WM_COMMAND, MAKEWPARAM(IDCANCEL, 0), 0); } -/** - * show a CWarning dialog using the id value. Check whether the user has chosen to - * not show this message again. This has room for 64 different warning dialogs, which - * should be enough in the first place. Extending it should not be too hard though. - */ + +///////////////////////////////////////////////////////////////////////////////////////// +// show a CWarning dialog using the id value. Check whether the user has chosen to +// not show this message again. This has room for 64 different warning dialogs, which +// should be enough in the first place. Extending it should not be too hard though. + LRESULT CWarning::show(const int uId, DWORD dwFlags, const wchar_t* tszTxt) { wchar_t* separator_pos = 0; @@ -1095,10 +1074,8 @@ LRESULT CWarning::show(const int uId, DWORD dwFlags, const wchar_t* tszTxt) if (0 == hWindowList) hWindowList = WindowList_Create(); - /* - * don't open new warnings when shutdown was initiated (modal ones will otherwise - * block the shutdown) - */ + // don't open new warnings when shutdown was initiated (modal ones will otherwise + // block the shutdown) if (CMimAPI::m_shutDown) return -1; @@ -1109,10 +1086,8 @@ LRESULT CWarning::show(const int uId, DWORD dwFlags, const wchar_t* tszTxt) if (dwFlags & CWF_UNTRANSLATED) _s = TranslateTS(warnings[uId]); else { - /* - * revert to untranslated warning when the translated message - * is not well-formatted. - */ + // revert to untranslated warning when the translated message + // is not well-formatted. _s = TranslateTS(warnings[uId]); if (mir_wstrlen(_s) < 3 || 0 == wcschr(_s, '|')) @@ -1156,9 +1131,9 @@ LRESULT CWarning::show(const int uId, DWORD dwFlags, const wchar_t* tszTxt) return -1; } -/** - * stub dlg procedure. Just register the object pointer in WM_INITDIALOG - */ +///////////////////////////////////////////////////////////////////////////////////////// +// stub dlg procedure.Just register the object pointer in WM_INITDIALOG + INT_PTR CALLBACK CWarning::stubDlgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { CWarning *w = reinterpret_cast(::GetWindowLongPtr(hwnd, GWLP_USERDATA)); @@ -1183,9 +1158,9 @@ INT_PTR CALLBACK CWarning::stubDlgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARA return FALSE; } -/** - * dialog procedure for the warning dialog box - */ +///////////////////////////////////////////////////////////////////////////////////////// +// dialog procedure for the warning dialog box + INT_PTR CALLBACK CWarning::dlgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { switch (msg) { -- cgit v1.2.3