diff --git a/mfbt/Char16.h b/mfbt/Char16.h index 380303a925f0..3d129332b43a 100644 --- a/mfbt/Char16.h +++ b/mfbt/Char16.h @@ -56,12 +56,6 @@ public: { return reinterpret_cast(mPtr); } - - operator wchar_t*() - { - return const_cast(reinterpret_cast(mPtr)); - } - operator const void*() const { return mPtr; diff --git a/widget/windows/IMMHandler.cpp b/widget/windows/IMMHandler.cpp index 159bee2799fb..39e34bb0fdcd 100644 --- a/widget/windows/IMMHandler.cpp +++ b/widget/windows/IMMHandler.cpp @@ -344,7 +344,7 @@ IMMHandler::InitKeyboardLayout(nsWindow* aWindow, // Add room for the terminating null character sIMEName.SetLength(++IMENameLength); IMENameLength = - ::ImmGetDescriptionW(aKeyboardLayout, sIMEName.get(), + ::ImmGetDescriptionW(aKeyboardLayout, wwc(sIMEName.BeginWriting()), IMENameLength); // Adjust the length to ignore the terminating null character sIMEName.SetLength(IMENameLength); diff --git a/xpcom/base/nsSystemInfo.cpp b/xpcom/base/nsSystemInfo.cpp index c49e1fc73cbb..29d6cd8a0470 100644 --- a/xpcom/base/nsSystemInfo.cpp +++ b/xpcom/base/nsSystemInfo.cpp @@ -229,7 +229,7 @@ nsresult GetCountryCode(nsAString& aCountryCode) } // Now get the string for real aCountryCode.SetLength(numChars); - numChars = GetGeoInfoW(geoid, GEO_ISO2, char16ptr_t(aCountryCode.BeginWriting()), + numChars = GetGeoInfoW(geoid, GEO_ISO2, wwc(aCountryCode.BeginWriting()), aCountryCode.Length(), 0); if (!numChars) { return NS_ERROR_FAILURE; diff --git a/xpcom/ds/nsWindowsRegKey.cpp b/xpcom/ds/nsWindowsRegKey.cpp index 7021148ebfb6..55571d8f94ba 100644 --- a/xpcom/ds/nsWindowsRegKey.cpp +++ b/xpcom/ds/nsWindowsRegKey.cpp @@ -354,8 +354,11 @@ nsWindowsRegKey::ReadStringValue(const nsAString& aName, nsAString& aResult) return NS_ERROR_OUT_OF_MEMORY; } + nsAString::iterator begin; + expandedResult.BeginWriting(begin); + resultLen = ExpandEnvironmentStringsW(flatSource.get(), - expandedResult.get(), + wwc(begin.get()), resultLen + 1); if (resultLen <= 0) { rv = ERROR_UNKNOWN_FEATURE; diff --git a/xpcom/io/FileUtilsWin.cpp b/xpcom/io/FileUtilsWin.cpp index 6ca0f41ae47a..732c074f7a54 100644 --- a/xpcom/io/FileUtilsWin.cpp +++ b/xpcom/io/FileUtilsWin.cpp @@ -61,7 +61,7 @@ HandleToFilename(HANDLE aHandle, const LARGE_INTEGER& aOffset, do { mappedFilename.SetLength(mappedFilename.Length() + MAX_PATH); len = GetMappedFileNameW(GetCurrentProcess(), view, - mappedFilename.get(), + wwc(mappedFilename.BeginWriting()), mappedFilename.Length()); } while (!len && GetLastError() == ERROR_INSUFFICIENT_BUFFER); if (!len) { diff --git a/xpcom/io/nsLocalFileWin.cpp b/xpcom/io/nsLocalFileWin.cpp index 16fd575cb067..e0f26adf44fd 100644 --- a/xpcom/io/nsLocalFileWin.cpp +++ b/xpcom/io/nsLocalFileWin.cpp @@ -995,7 +995,7 @@ nsLocalFile::ResolveShortcut() return NS_ERROR_OUT_OF_MEMORY; } - wchar_t* resolvedPath = mResolvedPath.get(); + wchar_t* resolvedPath = wwc(mResolvedPath.BeginWriting()); // resolve this shortcut nsresult rv = gResolver->Resolve(mWorkingPath.get(), resolvedPath); @@ -1365,7 +1365,7 @@ nsLocalFile::Create(uint32_t aType, uint32_t aAttributes) // Skip the first 'X:\' for the first form, and skip the first full // '\\machine\volume\' segment for the second form. - wchar_t* path = char16ptr_t(mResolvedPath.BeginWriting()); + wchar_t* path = wwc(mResolvedPath.BeginWriting()); if (path[0] == L'\\' && path[1] == L'\\') { // dealing with a UNC path here; skip past '\\machine\' @@ -3743,7 +3743,7 @@ nsDriveEnumerator::Init() if (!mDrives.SetLength(length + 1, fallible)) { return NS_ERROR_OUT_OF_MEMORY; } - if (!GetLogicalDriveStringsW(length, mDrives.get())) { + if (!GetLogicalDriveStringsW(length, wwc(mDrives.BeginWriting()))) { return NS_ERROR_FAILURE; } mDrives.BeginReading(mStartOfCurrentDrive); diff --git a/xpcom/io/nsNativeCharsetUtils.cpp b/xpcom/io/nsNativeCharsetUtils.cpp index bbabc0611d1a..e53307af5628 100644 --- a/xpcom/io/nsNativeCharsetUtils.cpp +++ b/xpcom/io/nsNativeCharsetUtils.cpp @@ -929,8 +929,12 @@ NS_CopyNativeToUnicode(const nsACString& aInput, nsAString& aOutput) return NS_ERROR_OUT_OF_MEMORY; } if (resultLen > 0) { - char16ptr_t result = aOutput.BeginWriting(); - ::MultiByteToWideChar(CP_ACP, 0, buf, inputLen, result, resultLen); + nsAString::iterator out_iter; + aOutput.BeginWriting(out_iter); + + char16_t* result = out_iter.get(); + + ::MultiByteToWideChar(CP_ACP, 0, buf, inputLen, wwc(result), resultLen); } return NS_OK; } @@ -974,6 +978,41 @@ NS_CopyUnicodeToNative(const nsAString& aInput, nsACString& aOutput) return NS_OK; } +// moved from widget/windows/nsToolkit.cpp +int32_t +NS_ConvertAtoW(const char* aStrInA, int aBufferSize, char16_t* aStrOutW) +{ + return MultiByteToWideChar(CP_ACP, 0, aStrInA, -1, wwc(aStrOutW), aBufferSize); +} + +int32_t +NS_ConvertWtoA(const char16_t* aStrInW, int aBufferSizeOut, + char* aStrOutA, const char* aDefault) +{ + if ((!aStrInW) || (!aStrOutA) || (aBufferSizeOut <= 0)) { + return 0; + } + + int numCharsConverted = WideCharToMultiByte(CP_ACP, 0, char16ptr_t(aStrInW), -1, + aStrOutA, aBufferSizeOut, + aDefault, nullptr); + + if (!numCharsConverted) { + if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) { + // Overflow, add missing null termination but return 0 + aStrOutA[aBufferSizeOut - 1] = '\0'; + } else { + // Other error, clear string and return 0 + aStrOutA[0] = '\0'; + } + } else if (numCharsConverted < aBufferSizeOut) { + // Add 2nd null (really necessary?) + aStrOutA[numCharsConverted] = '\0'; + } + + return numCharsConverted; +} + #else #include "nsReadableUtils.h" diff --git a/xpcom/string/nsString.h b/xpcom/string/nsString.h index 5bd0bb2d7e56..cfec6b9f13f7 100644 --- a/xpcom/string/nsString.h +++ b/xpcom/string/nsString.h @@ -148,6 +148,49 @@ private: NS_ConvertUTF8toUTF16(char16_t) = delete; }; + +#ifdef MOZ_USE_CHAR16_WRAPPER + +inline char16_t* +wwc(wchar_t* aStr) +{ + return reinterpret_cast(aStr); +} + +inline wchar_t* +wwc(char16_t* aStr) +{ + return reinterpret_cast(aStr); +} + +inline const char16_t* +wwc(const wchar_t* aStr) +{ + return reinterpret_cast(aStr); +} + +inline const wchar_t* +wwc(const char16_t* aStr) +{ + return reinterpret_cast(aStr); +} + +#else + +inline char16_t* +wwc(char16_t* aStr) +{ + return aStr; +} + +inline const char16_t* +wwc(const char16_t* aStr) +{ + return aStr; +} + +#endif + // the following are included/declared for backwards compatibility typedef nsAutoString nsVoidableString;