Skip to content

Commit

Permalink
arch: revert ICU commit 1b671c9 to patch UWP Debug linker errors (uni…
Browse files Browse the repository at this point in the history
…code-org#14)

Reverts 1b671c9 changes to umapfile.cpp because it begins using CreateFileW and MapViewOfFile. 

This commit causes a link error with UWP Debug builds only,  due to duplicate symbols in SKIA, which also has an ICU build, and utilises these windows functions. Be sure to resolve this before moving forward.
  • Loading branch information
robert-craig-houston authored Oct 29, 2021
2 parents 753a012 + dff9851 commit 27e3804
Showing 1 changed file with 60 additions and 62 deletions.
122 changes: 60 additions & 62 deletions icu4c/source/common/umapfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,32 +37,12 @@
# define NOSERVICE
# define NOIME
# define NOMCX

# if U_PLATFORM_HAS_WINUWP_API == 1
// Some previous versions of the Windows 10 SDK don't expose various APIs for UWP applications
// to use, even though UWP apps are allowed to call and use them. Temporarily change the
// WINAPI family partition below to Desktop, so that function declarations are visible for UWP.
# include <winapifamily.h>
# if !(WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP | WINAPI_PARTITION_SYSTEM))
# pragma push_macro("WINAPI_PARTITION_DESKTOP")
# undef WINAPI_PARTITION_DESKTOP
# define WINAPI_PARTITION_DESKTOP 1
# define CHANGED_WINAPI_PARTITION_DESKTOP_VALUE
# endif
# endif

# include <windows.h>

# if U_PLATFORM_HAS_WINUWP_API == 1 && defined(CHANGED_WINAPI_PARTITION_DESKTOP_VALUE)
# pragma pop_macro("WINAPI_PARTITION_DESKTOP")
# endif

# include "cmemory.h"

typedef HANDLE MemoryMap;

# define IS_MAP(map) ((map)!=nullptr)
typedef HANDLE MemoryMap;

# define IS_MAP(map) ((map)!=NULL)
#elif MAP_IMPLEMENTATION==MAP_POSIX || MAP_IMPLEMENTATION==MAP_390DLL
typedef size_t MemoryMap;

Expand Down Expand Up @@ -94,7 +74,7 @@ typedef HANDLE MemoryMap;

typedef void *MemoryMap;

# define IS_MAP(map) ((map)!=nullptr)
# define IS_MAP(map) ((map)!=NULL)
#endif

/*----------------------------------------------------------------------------*
Expand Down Expand Up @@ -125,24 +105,20 @@ typedef HANDLE MemoryMap;
UErrorCode *status /* Error status, used to report out-of-memory errors. */
)
{
HANDLE map;
HANDLE file;

if (U_FAILURE(*status)) {
return FALSE;
}

HANDLE map = nullptr;
HANDLE file = INVALID_HANDLE_VALUE;

UDataMemory_init(pData); /* Clear the output struct. */

/* open the input file */
#if U_PLATFORM_HAS_WINUWP_API == 0
// Note: In the non-UWP code-path (ie: Win32), the value of the path variable might have come from
// the CRT 'getenv' function, and would be therefore be encoded in the default ANSI code page.
// This means that we can't call the *W version of API below, whereas in the UWP code-path
// there is no 'getenv' call, and thus the string will be only UTF-8/Invariant characters.
file=CreateFileA(path, GENERIC_READ, FILE_SHARE_READ, nullptr,
file=CreateFileA(path, GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL|FILE_FLAG_RANDOM_ACCESS, nullptr);
FILE_ATTRIBUTE_NORMAL|FILE_FLAG_RANDOM_ACCESS, NULL);
#else
// Convert from UTF-8 string to UTF-16 string.
wchar_t utf16Path[MAX_PATH];
Expand All @@ -158,9 +134,8 @@ typedef HANDLE MemoryMap;
return FALSE;
}

file = CreateFileW(utf16Path, GENERIC_READ, FILE_SHARE_READ, nullptr,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_RANDOM_ACCESS, nullptr);
// TODO: Is it worth setting extended parameters to specify random access?
file = CreateFile2(utf16Path, GENERIC_READ, FILE_SHARE_READ, OPEN_EXISTING, NULL);
#endif
if (file == INVALID_HANDLE_VALUE) {
// If we failed to open the file due to an out-of-memory error, then we want
Expand All @@ -171,13 +146,36 @@ typedef HANDLE MemoryMap;
return FALSE;
}

// Note: We use NULL/nullptr for lpAttributes parameter below.
// This means our handle cannot be inherited and we will get the default security descriptor.
/* create an unnamed Windows file-mapping object for the specified file */
map = CreateFileMappingW(file, nullptr, PAGE_READONLY, 0, 0, nullptr);
/* Declare and initialize a security descriptor.
This is required for multiuser systems on Windows 2000 SP4 and beyond */
// TODO: UWP does not have this function and I do not think it is required?
#if U_PLATFORM_HAS_WINUWP_API == 0

SECURITY_ATTRIBUTES mappingAttributes;
SECURITY_ATTRIBUTES *mappingAttributesPtr = NULL;
SECURITY_DESCRIPTOR securityDesc;

if (InitializeSecurityDescriptor(&securityDesc, SECURITY_DESCRIPTOR_REVISION)) {
/* give the security descriptor a Null Dacl done using the "TRUE, (PACL)NULL" here */
if (SetSecurityDescriptorDacl(&securityDesc, TRUE, (PACL)NULL, FALSE)) {
/* Make the security attributes point to the security descriptor */
uprv_memset(&mappingAttributes, 0, sizeof(mappingAttributes));
mappingAttributes.nLength = sizeof(mappingAttributes);
mappingAttributes.lpSecurityDescriptor = &securityDesc;
mappingAttributes.bInheritHandle = FALSE; /* object uninheritable */
mappingAttributesPtr = &mappingAttributes;
}
}
/* else creating security descriptors can fail when we are on Windows 98,
and mappingAttributesPtr == NULL for that case. */

/* create an unnamed Windows file-mapping object for the specified file */
map=CreateFileMapping(file, mappingAttributesPtr, PAGE_READONLY, 0, 0, NULL);
#else
map = CreateFileMappingFromApp(file, NULL, PAGE_READONLY, 0, NULL);
#endif
CloseHandle(file);
if (map == nullptr) {
if (map == NULL) {
// If we failed to create the mapping due to an out-of-memory error, then
// we want to report that error back to the caller.
if (HRESULT_FROM_WIN32(GetLastError()) == E_OUTOFMEMORY) {
Expand All @@ -187,22 +185,22 @@ typedef HANDLE MemoryMap;
}

/* map a view of the file into our address space */
pData->pHeader = reinterpret_cast<const DataHeader *>(MapViewOfFile(map, FILE_MAP_READ, 0, 0, 0));
if (pData->pHeader == nullptr) {
pData->pHeader=(const DataHeader *)MapViewOfFile(map, FILE_MAP_READ, 0, 0, 0);
if(pData->pHeader==NULL) {
CloseHandle(map);
return FALSE;
}
pData->map = map;
pData->map=map;
return TRUE;
}

U_CFUNC void
uprv_unmapFile(UDataMemory *pData) {
if (pData != nullptr && pData->map != nullptr) {
if(pData!=NULL && pData->map!=NULL) {
UnmapViewOfFile(pData->pHeader);
CloseHandle(pData->map);
pData->pHeader = nullptr;
pData->map = nullptr;
pData->pHeader=NULL;
pData->map=NULL;
}
}

Expand Down Expand Up @@ -257,13 +255,13 @@ typedef HANDLE MemoryMap;

U_CFUNC void
uprv_unmapFile(UDataMemory *pData) {
if(pData!=nullptr && pData->map!=nullptr) {
if(pData!=NULL && pData->map!=NULL) {
size_t dataLen = (char *)pData->map - (char *)pData->mapAddr;
if(munmap(pData->mapAddr, dataLen)==-1) {
}
pData->pHeader=nullptr;
pData->pHeader=NULL;
pData->map=0;
pData->mapAddr=nullptr;
pData->mapAddr=NULL;
}
}

Expand Down Expand Up @@ -297,7 +295,7 @@ typedef HANDLE MemoryMap;
UDataMemory_init(pData); /* Clear the output struct. */
/* open the input file */
file=fopen(path, "rb");
if(file==nullptr) {
if(file==NULL) {
return FALSE;
}

Expand All @@ -310,7 +308,7 @@ typedef HANDLE MemoryMap;

/* allocate the memory to hold the file data */
p=uprv_malloc(fileLength);
if(p==nullptr) {
if(p==NULL) {
fclose(file);
*status = U_MEMORY_ALLOCATION_ERROR;
return FALSE;
Expand All @@ -332,11 +330,11 @@ typedef HANDLE MemoryMap;

U_CFUNC void
uprv_unmapFile(UDataMemory *pData) {
if(pData!=nullptr && pData->map!=nullptr) {
if(pData!=NULL && pData->map!=NULL) {
uprv_free(pData->map);
pData->map = nullptr;
pData->mapAddr = nullptr;
pData->pHeader = nullptr;
pData->map = NULL;
pData->mapAddr = NULL;
pData->pHeader = NULL;
}
}

Expand Down Expand Up @@ -399,7 +397,7 @@ typedef HANDLE MemoryMap;
* Copy the ICU_DATA path to the path buffer and return that*/
const char *icuDataDir;
icuDataDir=u_getDataDirectory();
if(icuDataDir!=nullptr && *icuDataDir!=0) {
if(icuDataDir!=NULL && *icuDataDir!=0) {
return strcpy_returnEnd(pathBuffer, icuDataDir);
} else {
/* there is no icuDataDir either. Just return the empty pathBuffer. */
Expand Down Expand Up @@ -431,7 +429,7 @@ typedef HANDLE MemoryMap;
}

inBasename=uprv_strrchr(path, U_FILE_SEP_CHAR);
if(inBasename==nullptr) {
if(inBasename==NULL) {
inBasename = path;
} else {
inBasename++;
Expand Down Expand Up @@ -496,7 +494,7 @@ typedef HANDLE MemoryMap;
fprintf(stderr, " -> %08X\n", handle );
# endif

if(handle != nullptr) {
if(handle != NULL) {
/* we have a data DLL - what kind of lookup do we need here? */
/* try to find the Table of Contents */
UDataMemory_init(pData); /* Clear the output struct. */
Expand All @@ -517,11 +515,11 @@ typedef HANDLE MemoryMap;
}

U_CFUNC void uprv_unmapFile(UDataMemory *pData) {
if(pData!=nullptr && pData->map!=nullptr) {
if(pData!=NULL && pData->map!=NULL) {
uprv_free(pData->map);
pData->map = nullptr;
pData->mapAddr = nullptr;
pData->pHeader = nullptr;
pData->map = NULL;
pData->mapAddr = NULL;
pData->pHeader = NULL;
}
}

Expand Down

0 comments on commit 27e3804

Please sign in to comment.