From 0ddd983a97f602dfb96dd5e5e2ccf354cbfa0104 Mon Sep 17 00:00:00 2001 From: George Hazan Date: Tue, 4 Sep 2012 18:29:27 +0000 Subject: attempt to fix memory crash git-svn-id: http://svn.miranda-ng.org/main/trunk@1535 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- protocols/NewsAggregator/Src/Utils.cpp | 107 +++++++++++++++++++++++++++------ 1 file changed, 87 insertions(+), 20 deletions(-) (limited to 'protocols/NewsAggregator') diff --git a/protocols/NewsAggregator/Src/Utils.cpp b/protocols/NewsAggregator/Src/Utils.cpp index cad76293e0..92dd9f383a 100644 --- a/protocols/NewsAggregator/Src/Utils.cpp +++ b/protocols/NewsAggregator/Src/Utils.cpp @@ -420,36 +420,103 @@ time_t __stdcall DateToUnixTime(TCHAR* stamp, BOOL FeedType) return ( time_t ) 0; } -VOID StrReplace(TCHAR* Search, TCHAR* Replace, TCHAR*& Resource) +/******************************************************************************/ + +TCHAR * _tcsistr(const TCHAR * str, const TCHAR * substr) { - int i = 0; - int SearchLen = (int)_tcslen(Search); - TCHAR* Work = mir_tstrdup(Replace); - int ReplaceLen = (int)_tcslen(Work); + if (!str || !substr || (substr[0] == _T('\0'))) + return (TCHAR *) str; + + size_t nLen = _tcslen(substr); + while (*str) + { + if (_tcsnicmp(str, substr, nLen) == 0) + break; + str++; + } + + if (*str == _T('\0')) + str = NULL; + + return (TCHAR *) str; +} + +int StrReplace(TCHAR* lpszOld, TCHAR* lpszNew, TCHAR*& lpszStr) +{ + if (!lpszStr || !lpszOld || !lpszNew) + return 0; + + size_t nStrLen = _tcslen(lpszStr); + if (nStrLen == 0) + return 0; + + size_t nOldLen = _tcslen(lpszOld); + if (nOldLen == 0) + return 0; + + size_t nNewLen = _tcslen(lpszNew); + + // loop once to figure out the size of the result string + int nCount = 0; + TCHAR *pszStart = (TCHAR *) lpszStr; + TCHAR *pszEnd = (TCHAR *) lpszStr + nStrLen; + TCHAR *pszTarget = NULL; + TCHAR * pszResultStr = NULL; - TCHAR* Pointer = _tcsstr(Resource, Search); + while (pszStart < pszEnd) + { + while ((pszTarget = _tcsistr(pszStart, lpszOld)) != NULL) + { + nCount++; + pszStart = pszTarget + nOldLen; + } + pszStart += _tcslen(pszStart); + } - while (Pointer != NULL) + // if any changes, make them now + if (nCount > 0) { - int PointerLen = (int)_tcslen(Pointer); - int ResourceLen = (int)_tcslen(Resource); + // allocate buffer for result string + size_t nResultStrSize = nStrLen + (nNewLen - nOldLen) * nCount + 2; + pszResultStr = new TCHAR [nResultStrSize]; + ZeroMemory(pszResultStr, nResultStrSize*sizeof(TCHAR)); + + pszStart = (TCHAR *) lpszStr; + pszEnd = (TCHAR *) lpszStr + nStrLen; + TCHAR *cp = pszResultStr; + + // loop again to actually do the work + while (pszStart < pszEnd) + { + while ((pszTarget = _tcsistr(pszStart, lpszOld)) != NULL) + { + int nCopyLen = (int)(pszTarget - pszStart); + _tcsncpy(cp, &lpszStr[pszStart-lpszStr], nCopyLen); - TCHAR* NewText = (TCHAR*)mir_calloc((ResourceLen - SearchLen + ReplaceLen + 1)*sizeof(TCHAR)); + cp += nCopyLen; - _tcsncpy(NewText, Resource, ResourceLen - PointerLen); - _tcscat(NewText, Work); - _tcscat(NewText, Pointer + SearchLen); + pszStart = pszTarget + nOldLen; - Resource = (TCHAR*)mir_alloc((ResourceLen - SearchLen + ReplaceLen + 1)*sizeof(TCHAR)); + _tcscpy(cp, lpszNew); - for (i = 0; i < (ResourceLen - SearchLen + ReplaceLen); i++) - Resource[i] = NewText[i]; - Resource[i] = 0; - mir_free(NewText); + cp += nNewLen; + } + _tcscpy(cp, pszStart); + pszStart += _tcslen(pszStart); + } - Pointer = _tcsstr(Resource + (ResourceLen - PointerLen + ReplaceLen), Search); + if (pszResultStr) + lpszStr = mir_tstrdup(pszResultStr); } - mir_free(Work); + + int nSize = 0; + if (pszResultStr) + { + nSize = (int)_tcslen(pszResultStr); + delete [] pszResultStr; + } + + return nSize; } BOOL DownloadFile(LPCTSTR tszURL, LPCTSTR tszLocal) -- cgit v1.2.3