Skip to content

Commit

Permalink
Revert pr#96099 (#97264)
Browse files Browse the repository at this point in the history
* Revert "Forward minipal_get_length_utf16_to_utf8 and minipal_convert_utf16_to_utf8 from DAC (#97055)"

This reverts commit 1263107.

* Revert "Cleanup some string operating functions (#96099)"

This reverts commit bc81c55.
  • Loading branch information
tommcdon committed Jan 21, 2024
1 parent 6fafbad commit b8114f9
Show file tree
Hide file tree
Showing 19 changed files with 757 additions and 44 deletions.
4 changes: 2 additions & 2 deletions src/coreclr/debug/daccess/inspect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1049,8 +1049,8 @@ ClrDataValue::GetString(
{
*strLen = static_cast<ULONG32>(u16_strlen(msgStr) + 1);
}

status = u16_strcpy_s(str, bufLen, msgStr) != NULL ? S_OK : S_FALSE;
status = StringCchCopy(str, bufLen, msgStr) == S_OK ?
S_OK : S_FALSE;
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/debug/daccess/stack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -830,7 +830,7 @@ ClrDataFrame::GetArgumentByIndex(
*nameLen = 5;
}

u16_strcpy_s(name, bufLen, W("this"));
StringCchCopy(name, bufLen, W("this"));
}
else
{
Expand Down
5 changes: 5 additions & 0 deletions src/coreclr/debug/daccess/stdafx.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@
#include "dacimpl.h"


#define STRSAFE_NO_DEPRECATE
#include <strsafe.h>
#undef _ftcscat
#undef _ftcscpy

// from ntstatus.h
#define STATUS_STOWED_EXCEPTION ((NTSTATUS)0xC000027BL)

Expand Down
5 changes: 3 additions & 2 deletions src/coreclr/debug/daccess/task.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -732,7 +732,8 @@ ClrDataAppDomain::GetName(
}
else
{
status = u16_strcpy_s(name, bufLen, (PCWSTR)rawName) != NULL ? S_OK : S_FALSE;
status = StringCchCopy(name, bufLen, (PCWSTR)rawName) == S_OK ?
S_OK : S_FALSE;
if (nameLen)
{
size_t cchName = u16_strlen((PCWSTR)rawName) + 1;
Expand Down Expand Up @@ -4767,7 +4768,7 @@ ClrDataExceptionState::GetString(
message->GetStringLength(),
true);

status = u16_strcpy_s(str, bufLen, msgStr) != NULL ? S_OK : S_FALSE;
status = StringCchCopy(str, bufLen, msgStr) == S_OK ? S_OK : S_FALSE;
if (strLen != NULL)
{
size_t cchName = u16_strlen(msgStr) + 1;
Expand Down
3 changes: 0 additions & 3 deletions src/coreclr/dlls/mscordac/mscordac_unixexports.src
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,6 @@ nativeStringResourceTable_mscorrc
#PAL__pread
#PAL__close

#minipal_get_length_utf16_to_utf8
#minipal_convert_utf16_to_utf8

#_wcsicmp
#_stricmp
#sprintf_s
Expand Down
1 change: 1 addition & 0 deletions src/coreclr/ilasm/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "clrversion.h"
#include "shimload.h"

#include "strsafe.h"
#define ASSERTE_ALL_BUILDS(expr) _ASSERTE_ALL_BUILDS((expr))

WCHAR* EqualOrColon(_In_ __nullterminated WCHAR* szArg)
Expand Down
2 changes: 2 additions & 0 deletions src/coreclr/inc/clr/fs/path.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

#include "clrtypes.h"

#include "strsafe.h"

#include "clr/str.h"

namespace clr
Expand Down
64 changes: 51 additions & 13 deletions src/coreclr/inc/corhlprpriv.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#define __CORHLPRPRIV_H__

#include "corhlpr.h"
#include <minipal/utf8.h>
#include "fstring.h"

#if defined(_MSC_VER) && defined(HOST_X86)
#pragma optimize("y", on) // If routines don't get inlined, don't pay the EBP frame penalty
Expand Down Expand Up @@ -225,33 +225,71 @@ class CQuickMemoryBase
iSize = cbTotal;
}


// Convert UTF8 string to UNICODE string, optimized for speed
HRESULT ConvertUtf8_UnicodeNoThrow(const char * utf8str)
{
bool allAscii;
DWORD length;

HRESULT hr = FString::Utf8_Unicode_Length(utf8str, & allAscii, & length);

if (SUCCEEDED(hr))
{
LPWSTR buffer = (LPWSTR) AllocNoThrow((length + 1) * sizeof(WCHAR));

if (buffer == NULL)
{
hr = E_OUTOFMEMORY;
}
else
{
hr = FString::Utf8_Unicode(utf8str, allAscii, buffer, length);
}
}

return hr;
}

// Convert UTF8 string to UNICODE string, optimized for speed
void ConvertUtf8_Unicode(const char * utf8str)
{
size_t sourceLen = strlen(utf8str);
size_t destLen = minipal_get_length_utf8_to_utf16(utf8str, sourceLen, 0);
bool allAscii;
DWORD length;

CHAR16_T* buffer = (CHAR16_T*) AllocThrows((destLen + 1) * sizeof(CHAR16_T));
buffer[destLen] = W('\0');
HRESULT hr = FString::Utf8_Unicode_Length(utf8str, & allAscii, & length);

if (!minipal_convert_utf8_to_utf16(utf8str, sourceLen, buffer, destLen, 0))
if (SUCCEEDED(hr))
{
ThrowHR(EMAKEHR(errno));
LPWSTR buffer = (LPWSTR) AllocThrows((length + 1) * sizeof(WCHAR));

hr = FString::Utf8_Unicode(utf8str, allAscii, buffer, length);
}

if (FAILED(hr))
{
ThrowHR(hr);
}
}

// Convert UNICODE string to UTF8 string, optimized for speed
void ConvertUnicode_Utf8(const WCHAR * pString)
{
size_t sourceLen = u16_strlen(pString);
size_t destLen = minipal_get_length_utf16_to_utf8((const CHAR16_T*)pString, sourceLen, 0);
bool allAscii;
DWORD length;

HRESULT hr = FString::Unicode_Utf8_Length(pString, & allAscii, & length);

LPSTR buffer = (LPSTR) AllocThrows((destLen + 1) * sizeof(char));
buffer[destLen] = '\0';
if (SUCCEEDED(hr))
{
LPSTR buffer = (LPSTR) AllocThrows((length + 1) * sizeof(char));

hr = FString::Unicode_Utf8(pString, allAscii, buffer, length);
}

if (!minipal_convert_utf16_to_utf8((const CHAR16_T*)pString, sourceLen, buffer, destLen, 0))
if (FAILED(hr))
{
ThrowHR(EMAKEHR(errno));
ThrowHR(hr);
}
}

Expand Down
44 changes: 44 additions & 0 deletions src/coreclr/inc/fstring.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// ---------------------------------------------------------------------------
// FString.h (Fast String)
//

// ---------------------------------------------------------------------------

// ------------------------------------------------------------------------------------------
// FString is fast string handling namespace


// 1) Simple
// 2) No C++ exception
// 3) Optimized for speed


#ifndef _FSTRING_H_
#define _FSTRING_H_

namespace FString
{
// Note: All "length" parameters do not count the space for the null terminator.
// Caller of Unicode_Utf8 and Utf8_Unicode must pass in a buffer of size at least length + 1.

// Scan for ASCII only string, calculate result UTF8 string length
HRESULT Unicode_Utf8_Length(_In_z_ LPCWSTR pString, _Out_ bool * pAllAscii, _Out_ DWORD * pLength);

// Convert UNICODE string to UTF8 string. Direct/fast conversion if ASCII
HRESULT Unicode_Utf8(_In_z_ LPCWSTR pString, bool allAscii, _Out_writes_bytes_(length) LPSTR pBuffer, DWORD length);

// Scan for ASCII string, calculate result UNICODE string length
HRESULT Utf8_Unicode_Length(_In_z_ LPCSTR pString, _Out_ bool * pAllAscii, _Out_ DWORD * pLength);

// Convert UTF8 string to UNICODE. Direct/fast conversion if ASCII
HRESULT Utf8_Unicode(_In_z_ LPCSTR pString, bool allAscii, _Out_writes_bytes_(length) LPWSTR pBuffer, DWORD length);

HRESULT ConvertUnicode_Utf8(_In_z_ LPCWSTR pString, _Outptr_result_z_ LPSTR * pBuffer);

HRESULT ConvertUtf8_Unicode(_In_z_ LPCSTR pString, _Outptr_result_z_ LPWSTR * pBuffer);

} // namespace FString

#endif // _FSTRING_H_
21 changes: 21 additions & 0 deletions src/coreclr/inc/utilcode.h
Original file line number Diff line number Diff line change
Expand Up @@ -2834,6 +2834,27 @@ template <class T> class CChainedHash
};


//*****************************************************************************
//
//********** String helper functions.
//
//*****************************************************************************

//*****************************************************************************
// Checks if string length exceeds the specified limit
//*****************************************************************************
inline BOOL IsStrLongerThan(_In_ _In_z_ char* pstr, unsigned N)
{
LIMITED_METHOD_CONTRACT;
unsigned i = 0;
if(pstr)
{
for(i=0; (i < N)&&(pstr[i]); i++);
}
return (i >= N);
}


//*****************************************************************************
// Class to parse a list of simple assembly names and then find a match
//*****************************************************************************
Expand Down
6 changes: 0 additions & 6 deletions src/coreclr/minipal/Unix/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,6 @@ if(NOT CLR_CROSS_COMPONENTS_BUILD)
list(APPEND SOURCES
${CLR_SRC_NATIVE_DIR}/minipal/cpufeatures.c
)

if(CLR_CMAKE_HOST_OSX)
list(APPEND SOURCES
${CLR_SRC_NATIVE_DIR}/minipal/utf8.c
)
endif()
endif()

add_library(coreclrminipal
Expand Down
Loading

0 comments on commit b8114f9

Please sign in to comment.