Skip to content

Commit b8114f9

Browse files
authored
Revert pr#96099 (#97264)
* 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.
1 parent 6fafbad commit b8114f9

File tree

19 files changed

+757
-44
lines changed

19 files changed

+757
-44
lines changed

src/coreclr/debug/daccess/inspect.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1049,8 +1049,8 @@ ClrDataValue::GetString(
10491049
{
10501050
*strLen = static_cast<ULONG32>(u16_strlen(msgStr) + 1);
10511051
}
1052-
1053-
status = u16_strcpy_s(str, bufLen, msgStr) != NULL ? S_OK : S_FALSE;
1052+
status = StringCchCopy(str, bufLen, msgStr) == S_OK ?
1053+
S_OK : S_FALSE;
10541054
}
10551055
else
10561056
{

src/coreclr/debug/daccess/stack.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -830,7 +830,7 @@ ClrDataFrame::GetArgumentByIndex(
830830
*nameLen = 5;
831831
}
832832

833-
u16_strcpy_s(name, bufLen, W("this"));
833+
StringCchCopy(name, bufLen, W("this"));
834834
}
835835
else
836836
{

src/coreclr/debug/daccess/stdafx.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@
5050
#include "dacimpl.h"
5151

5252

53+
#define STRSAFE_NO_DEPRECATE
54+
#include <strsafe.h>
55+
#undef _ftcscat
56+
#undef _ftcscpy
57+
5358
// from ntstatus.h
5459
#define STATUS_STOWED_EXCEPTION ((NTSTATUS)0xC000027BL)
5560

src/coreclr/debug/daccess/task.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -732,7 +732,8 @@ ClrDataAppDomain::GetName(
732732
}
733733
else
734734
{
735-
status = u16_strcpy_s(name, bufLen, (PCWSTR)rawName) != NULL ? S_OK : S_FALSE;
735+
status = StringCchCopy(name, bufLen, (PCWSTR)rawName) == S_OK ?
736+
S_OK : S_FALSE;
736737
if (nameLen)
737738
{
738739
size_t cchName = u16_strlen((PCWSTR)rawName) + 1;
@@ -4767,7 +4768,7 @@ ClrDataExceptionState::GetString(
47674768
message->GetStringLength(),
47684769
true);
47694770

4770-
status = u16_strcpy_s(str, bufLen, msgStr) != NULL ? S_OK : S_FALSE;
4771+
status = StringCchCopy(str, bufLen, msgStr) == S_OK ? S_OK : S_FALSE;
47714772
if (strLen != NULL)
47724773
{
47734774
size_t cchName = u16_strlen(msgStr) + 1;

src/coreclr/dlls/mscordac/mscordac_unixexports.src

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,6 @@ nativeStringResourceTable_mscorrc
6969
#PAL__pread
7070
#PAL__close
7171

72-
#minipal_get_length_utf16_to_utf8
73-
#minipal_convert_utf16_to_utf8
74-
7572
#_wcsicmp
7673
#_stricmp
7774
#sprintf_s

src/coreclr/ilasm/main.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "clrversion.h"
1212
#include "shimload.h"
1313

14+
#include "strsafe.h"
1415
#define ASSERTE_ALL_BUILDS(expr) _ASSERTE_ALL_BUILDS((expr))
1516

1617
WCHAR* EqualOrColon(_In_ __nullterminated WCHAR* szArg)

src/coreclr/inc/clr/fs/path.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
#include "clrtypes.h"
1212

13+
#include "strsafe.h"
14+
1315
#include "clr/str.h"
1416

1517
namespace clr

src/coreclr/inc/corhlprpriv.h

Lines changed: 51 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#define __CORHLPRPRIV_H__
1212

1313
#include "corhlpr.h"
14-
#include <minipal/utf8.h>
14+
#include "fstring.h"
1515

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

228+
229+
// Convert UTF8 string to UNICODE string, optimized for speed
230+
HRESULT ConvertUtf8_UnicodeNoThrow(const char * utf8str)
231+
{
232+
bool allAscii;
233+
DWORD length;
234+
235+
HRESULT hr = FString::Utf8_Unicode_Length(utf8str, & allAscii, & length);
236+
237+
if (SUCCEEDED(hr))
238+
{
239+
LPWSTR buffer = (LPWSTR) AllocNoThrow((length + 1) * sizeof(WCHAR));
240+
241+
if (buffer == NULL)
242+
{
243+
hr = E_OUTOFMEMORY;
244+
}
245+
else
246+
{
247+
hr = FString::Utf8_Unicode(utf8str, allAscii, buffer, length);
248+
}
249+
}
250+
251+
return hr;
252+
}
253+
228254
// Convert UTF8 string to UNICODE string, optimized for speed
229255
void ConvertUtf8_Unicode(const char * utf8str)
230256
{
231-
size_t sourceLen = strlen(utf8str);
232-
size_t destLen = minipal_get_length_utf8_to_utf16(utf8str, sourceLen, 0);
257+
bool allAscii;
258+
DWORD length;
233259

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

237-
if (!minipal_convert_utf8_to_utf16(utf8str, sourceLen, buffer, destLen, 0))
262+
if (SUCCEEDED(hr))
238263
{
239-
ThrowHR(EMAKEHR(errno));
264+
LPWSTR buffer = (LPWSTR) AllocThrows((length + 1) * sizeof(WCHAR));
265+
266+
hr = FString::Utf8_Unicode(utf8str, allAscii, buffer, length);
267+
}
268+
269+
if (FAILED(hr))
270+
{
271+
ThrowHR(hr);
240272
}
241273
}
242274

243275
// Convert UNICODE string to UTF8 string, optimized for speed
244276
void ConvertUnicode_Utf8(const WCHAR * pString)
245277
{
246-
size_t sourceLen = u16_strlen(pString);
247-
size_t destLen = minipal_get_length_utf16_to_utf8((const CHAR16_T*)pString, sourceLen, 0);
278+
bool allAscii;
279+
DWORD length;
280+
281+
HRESULT hr = FString::Unicode_Utf8_Length(pString, & allAscii, & length);
248282

249-
LPSTR buffer = (LPSTR) AllocThrows((destLen + 1) * sizeof(char));
250-
buffer[destLen] = '\0';
283+
if (SUCCEEDED(hr))
284+
{
285+
LPSTR buffer = (LPSTR) AllocThrows((length + 1) * sizeof(char));
286+
287+
hr = FString::Unicode_Utf8(pString, allAscii, buffer, length);
288+
}
251289

252-
if (!minipal_convert_utf16_to_utf8((const CHAR16_T*)pString, sourceLen, buffer, destLen, 0))
290+
if (FAILED(hr))
253291
{
254-
ThrowHR(EMAKEHR(errno));
292+
ThrowHR(hr);
255293
}
256294
}
257295

src/coreclr/inc/fstring.h

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// ---------------------------------------------------------------------------
4+
// FString.h (Fast String)
5+
//
6+
7+
// ---------------------------------------------------------------------------
8+
9+
// ------------------------------------------------------------------------------------------
10+
// FString is fast string handling namespace
11+
12+
13+
// 1) Simple
14+
// 2) No C++ exception
15+
// 3) Optimized for speed
16+
17+
18+
#ifndef _FSTRING_H_
19+
#define _FSTRING_H_
20+
21+
namespace FString
22+
{
23+
// Note: All "length" parameters do not count the space for the null terminator.
24+
// Caller of Unicode_Utf8 and Utf8_Unicode must pass in a buffer of size at least length + 1.
25+
26+
// Scan for ASCII only string, calculate result UTF8 string length
27+
HRESULT Unicode_Utf8_Length(_In_z_ LPCWSTR pString, _Out_ bool * pAllAscii, _Out_ DWORD * pLength);
28+
29+
// Convert UNICODE string to UTF8 string. Direct/fast conversion if ASCII
30+
HRESULT Unicode_Utf8(_In_z_ LPCWSTR pString, bool allAscii, _Out_writes_bytes_(length) LPSTR pBuffer, DWORD length);
31+
32+
// Scan for ASCII string, calculate result UNICODE string length
33+
HRESULT Utf8_Unicode_Length(_In_z_ LPCSTR pString, _Out_ bool * pAllAscii, _Out_ DWORD * pLength);
34+
35+
// Convert UTF8 string to UNICODE. Direct/fast conversion if ASCII
36+
HRESULT Utf8_Unicode(_In_z_ LPCSTR pString, bool allAscii, _Out_writes_bytes_(length) LPWSTR pBuffer, DWORD length);
37+
38+
HRESULT ConvertUnicode_Utf8(_In_z_ LPCWSTR pString, _Outptr_result_z_ LPSTR * pBuffer);
39+
40+
HRESULT ConvertUtf8_Unicode(_In_z_ LPCSTR pString, _Outptr_result_z_ LPWSTR * pBuffer);
41+
42+
} // namespace FString
43+
44+
#endif // _FSTRING_H_

src/coreclr/inc/utilcode.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2834,6 +2834,27 @@ template <class T> class CChainedHash
28342834
};
28352835

28362836

2837+
//*****************************************************************************
2838+
//
2839+
//********** String helper functions.
2840+
//
2841+
//*****************************************************************************
2842+
2843+
//*****************************************************************************
2844+
// Checks if string length exceeds the specified limit
2845+
//*****************************************************************************
2846+
inline BOOL IsStrLongerThan(_In_ _In_z_ char* pstr, unsigned N)
2847+
{
2848+
LIMITED_METHOD_CONTRACT;
2849+
unsigned i = 0;
2850+
if(pstr)
2851+
{
2852+
for(i=0; (i < N)&&(pstr[i]); i++);
2853+
}
2854+
return (i >= N);
2855+
}
2856+
2857+
28372858
//*****************************************************************************
28382859
// Class to parse a list of simple assembly names and then find a match
28392860
//*****************************************************************************

src/coreclr/minipal/Unix/CMakeLists.txt

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,6 @@ if(NOT CLR_CROSS_COMPONENTS_BUILD)
77
list(APPEND SOURCES
88
${CLR_SRC_NATIVE_DIR}/minipal/cpufeatures.c
99
)
10-
11-
if(CLR_CMAKE_HOST_OSX)
12-
list(APPEND SOURCES
13-
${CLR_SRC_NATIVE_DIR}/minipal/utf8.c
14-
)
15-
endif()
1610
endif()
1711

1812
add_library(coreclrminipal

0 commit comments

Comments
 (0)