diff --git a/Contrib/Library/LibraryLocal/LibraryLocal.cpp b/Contrib/Library/LibraryLocal/LibraryLocal.cpp index 3ffff7c8..19e275c9 100644 --- a/Contrib/Library/LibraryLocal/LibraryLocal.cpp +++ b/Contrib/Library/LibraryLocal/LibraryLocal.cpp @@ -47,8 +47,9 @@ int GetTLBVersion(tstring& filepath, DWORD& high, DWORD & low) hr = LoadTypeLib(fullpath, &typeLib); #else // If built without UNICODE, we still need to convert this string to a Unicode string. - WCHAR *ole_filename = winchar_fromTchar(fullpath); + WCHAR *ole_filename = wcsdup_fromTchar(fullpath, CP_ACP); hr = LoadTypeLib(ole_filename, &typeLib); + free(ole_filename); #endif if (SUCCEEDED(hr)) { diff --git a/Source/DialogTemplate.cpp b/Source/DialogTemplate.cpp index 058affa7..7e053856 100644 --- a/Source/DialogTemplate.cpp +++ b/Source/DialogTemplate.cpp @@ -59,7 +59,7 @@ void ReadVarLenArr(LPBYTE &seeker, WCHAR* &readInto, unsigned int uCodePage) { break; default: { - readInto = winchar_strdup((WCHAR *) arr); + readInto = wcsdup((WCHAR *) arr); PWCHAR wseeker = PWCHAR(seeker); while (*wseeker++); seeker = LPBYTE(wseeker); @@ -78,14 +78,14 @@ void ReadVarLenArr(LPBYTE &seeker, WCHAR* &readInto, unsigned int uCodePage) { seeker += sizeof(WORD); \ } \ else { \ - winchar_strcpy((WCHAR *) seeker, x); \ - seeker += winchar_strlen((WCHAR *) seeker) * sizeof(WCHAR) + sizeof(WCHAR); \ + wcscpy((WCHAR *) seeker, x); \ + seeker += wcslen((WCHAR *) seeker) * sizeof(WCHAR) + sizeof(WCHAR); \ } \ else \ seeker += sizeof(WORD); // A macro that adds the size of x (which can be a string a number, or nothing) to dwSize -#define AddStringOrIdSize(x) dwSize += x ? (IS_INTRESOURCE(x) ? sizeof(DWORD) : (winchar_strlen(x) + 1) * sizeof(WCHAR)) : sizeof(WORD) +#define AddStringOrIdSize(x) dwSize += x ? (IS_INTRESOURCE(x) ? sizeof(DWORD) : (wcslen(x) + 1) * sizeof(WCHAR)) : sizeof(WORD) ////////////////////////////////////////////////////////////////////// // Construction/Destruction @@ -227,7 +227,7 @@ CDialogTemplate::~CDialogTemplate() { if (m_szTitle) delete [] m_szTitle; if (m_szFont) - delete [] m_szFont; + free(m_szFont); for (unsigned int i = 0; i < m_vItems.size(); i++) { if (m_vItems[i]->szClass && !IS_INTRESOURCE(m_vItems[i]->szClass)) @@ -291,8 +291,8 @@ void CDialogTemplate::SetFont(TCHAR* szFaceName, WORD wFontSize) { } m_bCharset = DEFAULT_CHARSET; m_dwStyle |= DS_SETFONT; - if (m_szFont) delete [] m_szFont; - m_szFont = winchar_fromTchar(szFaceName, m_uCodePage); + if (m_szFont) free(m_szFont); + m_szFont = wcsdup_fromTchar(szFaceName, m_uCodePage); m_sFontSize = wFontSize; } @@ -302,10 +302,10 @@ void CDialogTemplate::AddItem(DialogItemTemplate item) { CopyMemory(newItem, &item, sizeof(DialogItemTemplate)); if (item.szClass && !IS_INTRESOURCE(item.szClass)) { - newItem->szClass = winchar_strdup(item.szClass); + newItem->szClass = _wcsdup(item.szClass); } if (item.szTitle && !IS_INTRESOURCE(item.szTitle)) { - newItem->szTitle = winchar_strdup(item.szTitle); + newItem->szTitle = _wcsdup(item.szTitle); } if (item.wCreateDataSize) { newItem->szCreationData = new char[item.wCreateDataSize]; diff --git a/Source/ResourceEditor.cpp b/Source/ResourceEditor.cpp index c095ea56..033d5f33 100644 --- a/Source/ResourceEditor.cpp +++ b/Source/ResourceEditor.cpp @@ -222,13 +222,13 @@ static WCHAR* ResStringToUnicode(const char *szString) { if (IS_INTRESOURCE(szString)) return MAKEINTRESOURCEW((ULONG_PTR)szString); else - return winchar_fromTchar(szString); + return wcsdup_fromTchar(szString, CP_ACP); } #endif static void FreeUnicodeResString(WCHAR* szwString) { if (!IS_INTRESOURCE(szwString)) - delete [] szwString; + free(szwString); } @@ -580,7 +580,7 @@ CResourceDirectory* CResourceEditor::ScanDirectory(PRESOURCE_DIRECTORY rdRoot, P size_t nameSize = ConvertEndianness(rds->Length); szName = new WCHAR[nameSize+1]; - winchar_strncpy(szName, rds->NameString, nameSize); + wcsncpy(szName, rds->NameString, nameSize); szName[nameSize] = 0; } // Else, set the name to this entry's id @@ -709,7 +709,7 @@ void CResourceEditor::WriteRsrcSec(BYTE* pbRsrcSec) { PMY_IMAGE_RESOURCE_DIRECTORY_ENTRY(cRDirE->m_ulWrittenAt)->UName.NameString.NameOffset = ConvertEndianness((DWORD) (seeker - pbRsrcSec)); WCHAR* szName = cRDirE->GetName(); - WORD iLen = winchar_strlen(szName) + 1; + WORD iLen = wcslen(szName) + 1; *(WORD*)seeker = ConvertEndianness(iLen); CopyMemory(seeker + sizeof(WORD), szName, iLen*sizeof(WCHAR)); @@ -818,7 +818,7 @@ void CResourceDirectory::AddEntry(CResourceDirectoryEntry* entry) { WCHAR* szEntName = entry->GetName(); for (i = 0; i < m_rdDir.NumberOfNamedEntries; i++) { WCHAR* szName = m_vEntries[i]->GetName(); - int cmp = winchar_strcmp(szName, szEntName); + int cmp = wcscmp(szName, szEntName); delete [] szName; if (cmp == 0) { delete [] szEntName; @@ -870,7 +870,7 @@ int CResourceDirectory::Find(WCHAR* szName) { continue; WCHAR* szEntName = m_vEntries[i]->GetName(); - int cmp = winchar_strcmp(szName, szEntName); + int cmp = wcscmp(szName, szEntName); delete [] szEntName; if (!cmp) @@ -942,7 +942,7 @@ CResourceDirectoryEntry::CResourceDirectoryEntry(WCHAR* szName, CResourceDirecto } else { m_bHasName = true; - m_szName = winchar_strdup(szName); + m_szName = _wcsdup(szName); } m_bIsDataDirectory = true; m_rdSubDir = rdSubDir; @@ -956,7 +956,7 @@ CResourceDirectoryEntry::CResourceDirectoryEntry(WCHAR* szName, CResourceDataEnt } else { m_bHasName = true; - m_szName = winchar_strdup(szName); + m_szName = _wcsdup(szName); } m_bIsDataDirectory = false; m_rdeData = rdeData; @@ -979,11 +979,11 @@ bool CResourceDirectoryEntry::HasName() { WCHAR* CResourceDirectoryEntry::GetName() { if (!m_bHasName) return 0; - return winchar_strdup(m_szName); + return _wcsdup(m_szName); } int CResourceDirectoryEntry::GetNameLength() { - return winchar_strlen(m_szName); + return wcslen(m_szName); } WORD CResourceDirectoryEntry::GetId() { diff --git a/Source/ResourceVersionInfo.cpp b/Source/ResourceVersionInfo.cpp index 30c0a506..6199840b 100644 --- a/Source/ResourceVersionInfo.cpp +++ b/Source/ResourceVersionInfo.cpp @@ -140,7 +140,7 @@ int GetVersionHeader (LPSTR &p, WORD &wLength, WORD &wValueLength, WORD &wType) wType = *(WORD*)p; p += sizeof(WORD); szKey = (WCHAR*)p; - p += (winchar_strlen(szKey) + 1) * sizeof (WCHAR); + p += (wcslen(szKey) + 1) * sizeof (WCHAR); while ( ((ULONG_PTR)p % 4) != 0 ) p++; return p - baseP; @@ -171,7 +171,7 @@ void SaveVersionHeader (GrowBuf &strm, WORD wLength, WORD wValueLength, WORD wTy strm.add (&wValueLength, sizeof (wValueLength)); strm.add (&wType, sizeof (wType)); - keyLen = WORD((winchar_strlen(key) + 1) * sizeof (WCHAR)); + keyLen = WORD((wcslen(key) + 1) * sizeof (WCHAR)); strm.add ((void*)key, keyLen); PadStream(strm); @@ -210,11 +210,11 @@ void CResourceVersionInfo::ExportToStream(GrowBuf &strm, int Index) PadStream (stringInfoStream); p = stringInfoStream.getlen(); - KeyName = winchar_fromTchar(pChildStrings->getname(i), codepage); - KeyValue = winchar_fromTchar(pChildStrings->getvalue(i), codepage); - SaveVersionHeader (stringInfoStream, 0, WORD(winchar_strlen(KeyValue) + 1), 1, KeyName, (void*)KeyValue); - delete [] KeyName; - delete [] KeyValue; + KeyName = wcsdup_fromTchar(pChildStrings->getname(i), codepage); + KeyValue = wcsdup_fromTchar(pChildStrings->getvalue(i), codepage); + SaveVersionHeader (stringInfoStream, 0, WORD(wcslen(KeyValue) + 1), 1, KeyName, (void*)KeyValue); + free(KeyName); + free(KeyValue); wSize = WORD(stringInfoStream.getlen() - p); *(WORD*)((PBYTE)stringInfoStream.get()+p)=wSize; diff --git a/Source/Tests/winchar.cpp b/Source/Tests/winchar.cpp index 5da4665b..f2abaa72 100644 --- a/Source/Tests/winchar.cpp +++ b/Source/Tests/winchar.cpp @@ -22,18 +22,18 @@ class WinCharTest : public CppUnit::TestFixture { public: void testFromTchar() { WCHAR test[] = { _x('t'), _x('e'), _x('s'), _x('t'), 0 }; - WCHAR *dyn = winchar_fromTchar("test"); + WCHAR *dyn = wcsdup_fromansi("test"); CPPUNIT_ASSERT_EQUAL( 0, memcmp(test, dyn, 5) ); - delete [] dyn; + free(dyn); } void testStrCpy() { WCHAR a[] = { _x('t'), _x('e'), _x('s'), _x('t'), 0 }; WCHAR b[5]; - CPPUNIT_ASSERT_EQUAL( (WCHAR*) b, (WCHAR*) winchar_strcpy(b, a) ); + CPPUNIT_ASSERT_EQUAL( (WCHAR*) b, (WCHAR*) wcscpy(b, a) ); CPPUNIT_ASSERT_EQUAL( 0, memcmp(a, b, 5) ); } @@ -41,22 +41,22 @@ public: WCHAR a1[] = { _x('t'), _x('e'), _x('s'), _x('t'), 0 }; WCHAR b[5]; - CPPUNIT_ASSERT_EQUAL( (WCHAR*) b, (WCHAR*) winchar_strncpy(b, a1, 5) ); + CPPUNIT_ASSERT_EQUAL( (WCHAR*) b, (WCHAR*) wcsncpy(b, a1, 5) ); CPPUNIT_ASSERT_EQUAL( 0, memcmp(a1, b, 5 * sizeof(WCHAR)) ); WCHAR a2[] = { _x('t'), _x('e'), 0, 0, 0 }; - CPPUNIT_ASSERT_EQUAL( (WCHAR*) b, (WCHAR*) winchar_strncpy(b, a2, 5) ); + CPPUNIT_ASSERT_EQUAL( (WCHAR*) b, (WCHAR*) wcsncpy(b, a2, 5) ); CPPUNIT_ASSERT_EQUAL( 0, memcmp(a2, b, 5 * sizeof(WCHAR)) ); - CPPUNIT_ASSERT_EQUAL( (WCHAR*) b, (WCHAR*) winchar_strncpy(b, a1, 2) ); + CPPUNIT_ASSERT_EQUAL( (WCHAR*) b, (WCHAR*) wcsncpy(b, a1, 2) ); CPPUNIT_ASSERT_EQUAL( 0, memcmp(a2, b, 5 * sizeof(WCHAR)) ); } void testStrLen() { WCHAR test[] = { _x('t'), _x('e'), _x('s'), _x('t'), 0 }; - CPPUNIT_ASSERT_EQUAL( (size_t) 4, winchar_strlen(test) ); + CPPUNIT_ASSERT_EQUAL( (size_t) 4, wcslen(test) ); } static int simplifyNumber(int n) { @@ -78,7 +78,7 @@ public: #define TEST_STR_CMP(x, y) \ CPPUNIT_ASSERT_EQUAL(\ simplifyNumber(strcmp(x, y)), \ - simplifyNumber(winchar_strcmp(w##x, w##y)) \ + simplifyNumber(wcscmp(w##x, w##y)) \ ) TEST_STR_CMP(a, b); @@ -93,9 +93,9 @@ public: void testStrDup() { WCHAR a[] = { _x('a'), _x('b'), _x('c'), 0 }; - WCHAR *b = winchar_strdup(a); + WCHAR *b = _wcsdup(a); - CPPUNIT_ASSERT_EQUAL( 0, winchar_strcmp(a, b) ); + CPPUNIT_ASSERT_EQUAL( 0, wcscmp(a, b) ); delete [] b; } diff --git a/Source/build.cpp b/Source/build.cpp index 4d76f052..ce709aa7 100644 --- a/Source/build.cpp +++ b/Source/build.cpp @@ -477,7 +477,7 @@ int CEXEBuild::add_string(const TCHAR *string, int process/*=1*/, WORD codepage/ int CEXEBuild::add_intstring(const int i) // returns offset in stringblock { - TCHAR i_str[1024]; + TCHAR i_str[32]; wsprintf(i_str, _T("%d"), i); return add_string(i_str); } diff --git a/Source/script.cpp b/Source/script.cpp index e9645051..a32d37fc 100644 --- a/Source/script.cpp +++ b/Source/script.cpp @@ -1589,26 +1589,26 @@ int CEXEBuild::doCommand(int which_token, LineParser &line) return PS_ERROR; } rewind(fp); - TCHAR *data=(TCHAR*)malloc(datalen+2*sizeof(TCHAR)); + char *data=(char*)malloc(datalen+2); if (!data) { ERROR_MSG(_T("Internal compiler error #12345: LicenseData malloc(%d) failed.\n"), datalen+2); return PS_ERROR; } MANAGE_WITH(data, free); - TCHAR *ldata=data+1; + char *ldata=data+1; if (fread((void*)ldata,1,datalen,fp) != datalen) { ERROR_MSG(_T("LicenseLangString: can't read file.\n")); return PS_ERROR; } - ldata[datalen/sizeof(TCHAR)]=0; - if (!strncmp((char*)ldata,"{\\rtf",sizeof("{\\rtf")-1)) + ldata[datalen]='\0'; + if (!memcmp(ldata,"{\\rtf",6)) *data = SF_RTF; else *data = SF_TEXT; - int ret = SetLangString(name, lang, data); + int ret = SetLangString(name, lang, (TCHAR*) data); if (ret == PS_WARNING) warning_fl(_T("LicenseLangString \"%s\" set multiple times for %d, wasting space"), name, lang); else if (ret == PS_ERROR) @@ -4886,11 +4886,7 @@ int CEXEBuild::doCommand(int which_token, LineParser &line) case TOK_GETCURRENTADDR: ent.which=EW_ASSIGNVAR; ent.offsets[0]=GetUserVarIndex(line, 1); - { - TCHAR buf[32]; - wsprintf(buf,_T("%d"),1+(cur_header->blocks[NB_ENTRIES].num)); - ent.offsets[1]=add_string(buf); - } + ent.offsets[1]=add_intstring(1+(cur_header->blocks[NB_ENTRIES].num)); if (ent.offsets[0] < 0) PRINTHELP() ent.offsets[2]=0; ent.offsets[3]=0; @@ -5970,7 +5966,7 @@ int CEXEBuild::doCommand(int which_token, LineParser &line) // next, call it ent.which=EW_REGISTERDLL; - ent.offsets[0]=add_string(tempDLL);; + ent.offsets[0]=add_string(tempDLL); ent.offsets[1]=add_string(funcname.c_str()); ent.offsets[2]=0; ent.offsets[3]=nounload|build_plugin_unload; diff --git a/Source/util.cpp b/Source/util.cpp index e9d79a4e..08ab55f0 100644 --- a/Source/util.cpp +++ b/Source/util.cpp @@ -212,7 +212,7 @@ int WideCharToMultiByte(UINT CodePage, DWORD dwFlags, LPCWSTR lpWideCharStr, } if (cchWideChar < 0) { - cchWideChar = (int) winchar_strlen(lpWideCharStr) + 1; + cchWideChar = (int) _wcslen(lpWideCharStr) + 1; } if (cbMultiByte == 0) { @@ -546,7 +546,7 @@ static bool GetDLLVersionUsingRE(const tstring& filepath, DWORD& high, DWORD & l { // get VS_FIXEDFILEINFO from VS_VERSIONINFO WCHAR *szKey = (WCHAR *)(ver + sizeof(WORD) * 3); - int len = (winchar_strlen(szKey) + 1) * sizeof(WCHAR) + sizeof(WORD) * 3; + int len = (wcslen(szKey) + 1) * sizeof(WCHAR) + sizeof(WORD) * 3; len = (len + 3) & ~3; // align on DWORD boundry VS_FIXEDFILEINFO *verinfo = (VS_FIXEDFILEINFO *)(ver + len); if (versize > len && verinfo->dwSignature == VS_FFI_SIGNATURE) diff --git a/Source/winchar.cpp b/Source/winchar.cpp index 35c243cc..3f549a57 100644 --- a/Source/winchar.cpp +++ b/Source/winchar.cpp @@ -24,11 +24,8 @@ using std::runtime_error; -WCHAR *winchar_fromTchar(const TCHAR* s, unsigned int codepage/*=CP_ACP*/) +WCHAR *wcsdup_fromansi(const char* s, unsigned int codepage/* = CP_ACP*/) { -#ifdef _UNICODE - return _wcsdup(s); // codepage is not used in this mode -#else int l = MultiByteToWideChar(codepage, 0, s, -1, 0, 0); if (l == 0) throw runtime_error("Unicode conversion failed"); @@ -39,10 +36,10 @@ WCHAR *winchar_fromTchar(const TCHAR* s, unsigned int codepage/*=CP_ACP*/) throw runtime_error("Unicode conversion failed"); return ws; -#endif } -WCHAR *winchar_strcpy(WCHAR *ws1, const WCHAR *ws2) +#if 0 // Needed by some RTL missing wchar string functions ? +WCHAR *wcscpy(WCHAR *ws1, const WCHAR *ws2) { WCHAR *ret = ws1; @@ -56,7 +53,7 @@ WCHAR *winchar_strcpy(WCHAR *ws1, const WCHAR *ws2) return ret; } -WCHAR *winchar_strncpy(WCHAR *ws1, const WCHAR *ws2, size_t n) +WCHAR *wcsncpy(WCHAR *ws1, const WCHAR *ws2, size_t n) { WCHAR *ret = ws1; @@ -74,7 +71,7 @@ WCHAR *winchar_strncpy(WCHAR *ws1, const WCHAR *ws2, size_t n) return ret; } -size_t winchar_strlen(const WCHAR *ws) +size_t wcslen(const WCHAR *ws) { size_t len = 0; @@ -86,14 +83,14 @@ size_t winchar_strlen(const WCHAR *ws) return len; } -WCHAR *winchar_strdup(const WCHAR *ws) +WCHAR *_wcsdup(const WCHAR *ws) { - WCHAR *dup = new WCHAR[winchar_strlen(ws) + 1]; - winchar_strcpy(dup, ws); + WCHAR *dup = (WCHAR*) malloc(sizeof(WCHAR)*(wcslen(ws)+1)]; + wcscpy(dup, ws); return dup; } -int winchar_strcmp(const WCHAR *ws1, const WCHAR *ws2) +int wcscmp(const WCHAR *ws1, const WCHAR *ws2) { int diff = 0; @@ -105,3 +102,4 @@ int winchar_strcmp(const WCHAR *ws1, const WCHAR *ws2) return diff; } +#endif diff --git a/Source/winchar.h b/Source/winchar.h index ed3f1f24..e786c1ed 100644 --- a/Source/winchar.h +++ b/Source/winchar.h @@ -18,9 +18,17 @@ #include "Platform.h" -WCHAR *winchar_fromTchar(const TCHAR* s, unsigned int codepage = CP_ACP); -WCHAR *winchar_strcpy(WCHAR *ws1, const WCHAR *ws2); -WCHAR *winchar_strncpy(WCHAR *ws1, const WCHAR *ws2, size_t n); -size_t winchar_strlen(const WCHAR *ws); -WCHAR *winchar_strdup(const WCHAR *ws); -int winchar_strcmp(const WCHAR *ws1, const WCHAR *ws2); +WCHAR *wcsdup_fromansi(const char* s, unsigned int codepage = CP_ACP); +#ifdef _UNICODE +#define wcsdup_fromTchar(s, codepage) _wcsdup(s) // codepage is not used in this mode +#else +#define wcsdup_fromTchar(s, codepage) wcsdup_fromansi(s, codepage) +#endif + +#if 0 // Needed by some RTL missing wchar string functions ? +WCHAR *wcscpy(WCHAR *ws1, const WCHAR *ws2); +WCHAR *wcsncpy(WCHAR *ws1, const WCHAR *ws2, size_t n); +size_t wcslen(const WCHAR *ws); +WCHAR *_wcsdup(const WCHAR *ws); +int wcscmp(const WCHAR *ws1, const WCHAR *ws2); +#endif