diff options
author | Pär Björklund <per.bjorklund@gmail.com> | 2017-02-22 20:27:39 +0100 |
---|---|---|
committer | Pär Björklund <per.bjorklund@gmail.com> | 2017-03-01 18:13:29 +0100 |
commit | 7070d1cb0dbc7d53079a8612cb4553a9ea2f4622 (patch) | |
tree | 898b3d6325a4f08a419d0d625379b0b6b92be8bd /lib | |
parent | 78d6cdd5ae3c1b3b0829fd86a65c1aa57f4dd60c (diff) |
Move to using unicode win32 api methods
This should have been done a long time ago but
with UWP no longer supporting the ascii versions it's
time to get it done.
The changes in UnrarXLib isn't really tested but it's
about to get dropped from our tree anyway.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/UnrarXLib/arccmt.cpp | 4 | ||||
-rw-r--r-- | lib/UnrarXLib/crypt.cpp | 4 | ||||
-rw-r--r-- | lib/UnrarXLib/filcreat.cpp | 2 | ||||
-rw-r--r-- | lib/UnrarXLib/filefn.cpp | 23 | ||||
-rw-r--r-- | lib/UnrarXLib/filestr.cpp | 4 | ||||
-rw-r--r-- | lib/UnrarXLib/find.cpp | 6 | ||||
-rw-r--r-- | lib/UnrarXLib/pathfn.cpp | 2 | ||||
-rw-r--r-- | lib/UnrarXLib/strfn.cpp | 24 | ||||
-rw-r--r-- | lib/UnrarXLib/suballoc.cpp | 3 | ||||
-rw-r--r-- | lib/UnrarXLib/unicode.cpp | 46 | ||||
-rw-r--r-- | lib/UnrarXLib/unicode.hpp | 51 | ||||
-rw-r--r-- | lib/UnrarXLib/win32acl.cpp | 61 |
12 files changed, 119 insertions, 111 deletions
diff --git a/lib/UnrarXLib/arccmt.cpp b/lib/UnrarXLib/arccmt.cpp index b716cac621..accfcd9016 100644 --- a/lib/UnrarXLib/arccmt.cpp +++ b/lib/UnrarXLib/arccmt.cpp @@ -88,8 +88,8 @@ bool Archive::GetComment(Array<byte> &CmtData) } #endif #if defined(_WIN_32) && !defined(_WIN_CE) && !defined(_XBOX) && !defined(_LINUX) - if (CmtData.Size()>0) - OemToCharBuff((char*)&CmtData[0],(char*)&CmtData[0],CmtData.Size()); + //if (CmtData.Size()>0) + // OemToCharBuff((char*)&CmtData[0],(char*)&CmtData[0],CmtData.Size()); #endif return(CmtData.Size()>0); } diff --git a/lib/UnrarXLib/crypt.cpp b/lib/UnrarXLib/crypt.cpp index 4980e9d98d..4d1a95e247 100644 --- a/lib/UnrarXLib/crypt.cpp +++ b/lib/UnrarXLib/crypt.cpp @@ -201,11 +201,7 @@ void CryptData::SetCryptKeys(char *Password,byte *Salt,bool Encrypt,bool OldOnly Key[2]=0x7515A235L; Key[3]=0xA4E7F123L; memset(Psw,0,sizeof(Psw)); -#if defined(_WIN_32) && !defined(GUI) && !defined(TARGET_POSIX) - CharToOemBuff(Password,(char*)Psw,strlen(Password)); -#else strncpy((char *)Psw,Password,MAXPASSWORD-1); -#endif int PswLength=strlen(Password); memcpy(SubstTable,InitSubstTable,sizeof(SubstTable)); for (int J=0;J<256;J++) diff --git a/lib/UnrarXLib/filcreat.cpp b/lib/UnrarXLib/filcreat.cpp index 35c9b1718d..3df48c039d 100644 --- a/lib/UnrarXLib/filcreat.cpp +++ b/lib/UnrarXLib/filcreat.cpp @@ -53,7 +53,7 @@ bool FileCreate(RAROptions *Cmd,File *NewFile,char *Name,wchar *NameW, SrcFile.SetHandleType(FILE_HANDLESTD); int Size=SrcFile.Read(NewName,NM); NewName[Size]=0; - OemToChar(NewName,NewName); + //OemToChar(NewName,NewName); #else if (!fgets(NewName,sizeof(NewName),stdin)) continue; diff --git a/lib/UnrarXLib/filefn.cpp b/lib/UnrarXLib/filefn.cpp index 42998035fd..591a29d3e1 100644 --- a/lib/UnrarXLib/filefn.cpp +++ b/lib/UnrarXLib/filefn.cpp @@ -7,12 +7,13 @@ void SetDirTime(const char *Name,RarTime *ftm,RarTime *ftc,RarTime *fta) { #ifdef _WIN_32 + bool sm=ftm!=NULL && ftm->IsSet(); bool sc=ftc!=NULL && ftc->IsSet(); bool sa=ftc!=NULL && fta->IsSet(); if (!WinNT()) return; - HANDLE hFile=CreateFile(Name,GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE, + HANDLE hFile=CreateFile(unrarxlib::ToW(Name).c_str(),GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,OPEN_EXISTING,FILE_FLAG_BACKUP_SEMANTICS,NULL); if (hFile==INVALID_HANDLE_VALUE) return; @@ -40,7 +41,7 @@ bool IsRemovable(const char *Name) #elif defined(_WIN_32) char Root[NM]; GetPathRoot(Name,Root); - int Type=GetDriveType(*Root ? Root:NULL); + int Type=GetDriveType(*Root ? unrarxlib::ToW(Root).c_str():NULL); return(Type==DRIVE_REMOVABLE || Type==DRIVE_CDROM); #elif defined(_EMX) char Drive=toupper(Name[0]); @@ -77,7 +78,7 @@ Int64 GetFreeDisk(const char *Name) if (pGetDiskFreeSpaceEx==NULL) { - HMODULE hKernel=GetModuleHandle("kernel32.dll"); + HMODULE hKernel=GetModuleHandle(L"kernel32.dll"); if (hKernel!=NULL) pGetDiskFreeSpaceEx=(GETDISKFREESPACEEX)GetProcAddress(hKernel,"GetDiskFreeSpaceExA"); } @@ -86,13 +87,13 @@ Int64 GetFreeDisk(const char *Name) GetFilePath(Name,Root); ULARGE_INTEGER uiTotalSize,uiTotalFree,uiUserFree; uiUserFree.u.LowPart=uiUserFree.u.HighPart=0; - if (pGetDiskFreeSpaceEx(*Root ? Root:NULL,&uiUserFree,&uiTotalSize,&uiTotalFree) && + if (pGetDiskFreeSpaceEx(*Root ? unrarxlib::ToW(Root).c_str():NULL,&uiUserFree,&uiTotalSize,&uiTotalFree) && uiUserFree.u.HighPart<=uiTotalFree.u.HighPart) return(int32to64(uiUserFree.u.HighPart,uiUserFree.u.LowPart)); } DWORD SectorsPerCluster,BytesPerSector,FreeClusters,TotalClusters; - if (!GetDiskFreeSpace(*Root ? Root:NULL,&SectorsPerCluster,&BytesPerSector,&FreeClusters,&TotalClusters)) + if (!GetDiskFreeSpace(*Root ? unrarxlib::ToW(Root).c_str():NULL,&SectorsPerCluster,&BytesPerSector,&FreeClusters,&TotalClusters)) return(1457664); Int64 FreeSize=SectorsPerCluster*BytesPerSector; FreeSize=FreeSize*FreeClusters; @@ -151,7 +152,7 @@ bool FileExist(const char *Name,const wchar *NameW) return(GetFileAttributesW(NameW)!=0xffffffff); else #endif - return(GetFileAttributes(Name)!=0xffffffff); + return(GetFileAttributes(unrarxlib::ToW(Name).c_str())!=0xffffffff); #elif defined(ENABLE_ACCESS) return(access(Name,0)==0); #else @@ -247,7 +248,7 @@ uint GetFileAttr(const char *Name,const wchar *NameW) return(GetFileAttributesW(NameW)); else #endif - return(GetFileAttributes(Name)); + return(GetFileAttributes(unrarxlib::ToW(Name).c_str())); #elif defined(_DJGPP) return(_chmod(Name,0)); #else @@ -272,7 +273,7 @@ bool SetFileAttr(const char *Name,const wchar *NameW,uint Attr) success=SetFileAttributesW(NameW,Attr)!=0; else #endif - success=SetFileAttributes(Name,Attr)!=0; + success=SetFileAttributes(unrarxlib::ToW(Name).c_str(),Attr)!=0; #elif defined(_DJGPP) success=_chmod(Name,1,Attr)!=-1; #elif defined(_EMX) @@ -291,9 +292,9 @@ void ConvertNameToFull(const char *Src,char *Dest) #ifdef _WIN_32 //#ifndef _WIN_CE #if !defined(_WIN_CE) && !defined(TARGET_POSIX) - char FullName[NM],*NamePtr; - if (GetFullPathName(Src,sizeof(FullName),FullName,&NamePtr)) - strcpy(Dest,FullName); + wchar_t FullName[NM],*NamePtr; + if (GetFullPathName(unrarxlib::ToW(Src).c_str(),sizeof(FullName),FullName,&NamePtr)) + strcpy(Dest,unrarxlib::FromW(FullName).c_str()); else #endif if (Src!=Dest) diff --git a/lib/UnrarXLib/filestr.cpp b/lib/UnrarXLib/filestr.cpp index e43cbfc20f..ae7eeb46e7 100644 --- a/lib/UnrarXLib/filestr.cpp +++ b/lib/UnrarXLib/filestr.cpp @@ -114,8 +114,8 @@ bool ReadTextFile(char *Name,StringList *List,bool Config,bool AbortOnError, } } #if defined(_WIN_32) && !defined(TARGET_POSIX) - if (ConvertToAnsi) - OemToChar(CurStr,CurStr); + //if (ConvertToAnsi) + // OemToChar(CurStr,CurStr); #endif List->AddString(CurStr); } diff --git a/lib/UnrarXLib/find.cpp b/lib/UnrarXLib/find.cpp index 063577ad75..724645cefd 100644 --- a/lib/UnrarXLib/find.cpp +++ b/lib/UnrarXLib/find.cpp @@ -248,7 +248,7 @@ HANDLE FindFile::Win32Find(HANDLE hFind,const char *Mask,const wchar *MaskW,stru WIN32_FIND_DATA FindData; if (hFind==INVALID_HANDLE_VALUE) { - hFind=FindFirstFile(CharMask,&FindData); + hFind=FindFirstFile(unrarxlib::ToW(CharMask).c_str(),&FindData); if (hFind==INVALID_HANDLE_VALUE) { int SysErr=GetLastError(); @@ -265,11 +265,11 @@ HANDLE FindFile::Win32Find(HANDLE hFind,const char *Mask,const wchar *MaskW,stru if (hFind!=INVALID_HANDLE_VALUE) { strcpy(fd->Name,CharMask); - strcpy(PointToName(fd->Name),FindData.cFileName); + strcpy(PointToName(fd->Name),unrarxlib::FromW(FindData.cFileName).c_str()); CharToWide(fd->Name,fd->NameW); fd->Size=int32to64(FindData.nFileSizeHigh,FindData.nFileSizeLow); fd->FileAttr=FindData.dwFileAttributes; - strcpy(fd->ShortName,FindData.cAlternateFileName); + strcpy(fd->ShortName,unrarxlib::FromW(FindData.cAlternateFileName).c_str()); fd->ftCreationTime=FindData.ftCreationTime; fd->ftLastAccessTime=FindData.ftLastAccessTime; fd->ftLastWriteTime=FindData.ftLastWriteTime; diff --git a/lib/UnrarXLib/pathfn.cpp b/lib/UnrarXLib/pathfn.cpp index 70dd9bb15b..11aad0426d 100644 --- a/lib/UnrarXLib/pathfn.cpp +++ b/lib/UnrarXLib/pathfn.cpp @@ -345,7 +345,7 @@ bool EnumConfigPaths(char *Path,int Number) if (Number!=0) return(false); #if !defined(TARGET_POSIX) - GetModuleFileName(NULL,Path,NM); + GetModuleFileName(NULL,(LPWSTR)unrarxlib::ToW(Path).c_str(),NM); RemoveNameFromPath(Path); #endif return(true); diff --git a/lib/UnrarXLib/strfn.cpp b/lib/UnrarXLib/strfn.cpp index 53749d44cf..db3b17a88b 100644 --- a/lib/UnrarXLib/strfn.cpp +++ b/lib/UnrarXLib/strfn.cpp @@ -22,46 +22,30 @@ char *IntNameToExt(const char *Name) void ExtToInt(const char *Src,char *Dest) { -#if defined(_WIN_32) - CharToOem(Src,Dest); -#else if (Dest!=Src) strcpy(Dest,Src); -#endif } void IntToExt(const char *Src,char *Dest) { -#if defined(_WIN_32) - OemToChar(Src,Dest); -#else if (Dest!=Src) strcpy(Dest,Src); -#endif } char* strlower(char *Str) { -#ifdef _WIN_32 - CharLower((LPTSTR)Str); -#else for (char *ChPtr=Str;*ChPtr;ChPtr++) *ChPtr=(char)loctolower(*ChPtr); -#endif return(Str); } char* strupper(char *Str) { -#ifdef _WIN_32 - CharUpper((LPTSTR)Str); -#else for (char *ChPtr=Str;*ChPtr;ChPtr++) *ChPtr=(char)loctoupper(*ChPtr); -#endif return(Str); } @@ -102,21 +86,13 @@ char* RemoveLF(char *Str) unsigned int loctolower(byte ch) { -#ifdef _WIN_32 - return((int)CharLower((LPTSTR)ch)); -#else return(tolower(ch)); -#endif } unsigned int loctoupper(byte ch) { -#ifdef _WIN_32 - return((int)CharUpper((LPTSTR)ch)); -#else return(toupper(ch)); -#endif } diff --git a/lib/UnrarXLib/suballoc.cpp b/lib/UnrarXLib/suballoc.cpp index a23bc98eae..45cebcc478 100644 --- a/lib/UnrarXLib/suballoc.cpp +++ b/lib/UnrarXLib/suballoc.cpp @@ -91,9 +91,6 @@ bool SubAllocator::StartSubAllocator(int SASize) return FALSE; } - if(AllocSize != AllocSize2) - OutputDebugString("ERROR - had to allocate smaller data than required, extract can very well fail"); - #endif HeapEnd=HeapStart+AllocSize-UNIT_SIZE; SubAllocatorSize=t; diff --git a/lib/UnrarXLib/unicode.cpp b/lib/UnrarXLib/unicode.cpp index 135eac4873..234a98ee62 100644 --- a/lib/UnrarXLib/unicode.cpp +++ b/lib/UnrarXLib/unicode.cpp @@ -439,3 +439,49 @@ char* SupportDBCS::strrchrd(const char *s, int c) return((char *)found); } #endif +#if defined(TARGET_WINDOWS) || defined(TARGET_WIN10) +#include <memory> + +namespace unrarxlib +{ +std::string FromW(const wchar_t* str, size_t length) +{ + int result = WideCharToMultiByte(CP_UTF8, MB_ERR_INVALID_CHARS, str, length, nullptr, 0, nullptr, nullptr); + if (result == 0) + return std::string(); + + auto newStr = std::make_unique<char[]>(result); + result = WideCharToMultiByte(CP_UTF8, MB_ERR_INVALID_CHARS, str, length, newStr.get(), result, nullptr, nullptr); + if (result == 0) + return std::string(); + + return std::string(newStr.get(), result); +} + +std::string FromW(const std::wstring& str) +{ + return FromW(str.c_str(), str.length()); +} + +std::wstring ToW(const char* str, size_t length) +{ + int result = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, str, length, nullptr, 0); + if (result == 0) + return std::wstring(); + + auto newStr = std::make_unique<wchar_t[]>(result); + result = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, str, length, newStr.get(), result); + + if (result == 0) + return std::wstring(); + + return std::wstring(newStr.get(), result); +} + +std::wstring ToW(const std::string& str) +{ + return ToW(str.c_str(), str.length()); +} + +} +#endif diff --git a/lib/UnrarXLib/unicode.hpp b/lib/UnrarXLib/unicode.hpp index 673bf4951e..5b67276824 100644 --- a/lib/UnrarXLib/unicode.hpp +++ b/lib/UnrarXLib/unicode.hpp @@ -13,6 +13,8 @@ #define DBCS_SUPPORTED #endif +#include <string> + bool WideToChar(const wchar *Src,char *Dest,int DestSize=0x10000000); bool CharToWide(const char *Src,wchar *Dest,int DestSize=0x10000000); byte* WideToRaw(const wchar *Src,byte *Dest,int DestSize=0x10000000); @@ -74,4 +76,53 @@ inline void InitDBCS() {gdbcs.Init();} inline void copychrd(char *dest,const char *src) {*dest=*src;} #endif + + +#if defined(TARGET_WINDOWS) || defined(TARGET_WIN10) +namespace unrarxlib +{ +/** +* Convert UTF-16 to UTF-8 strings +* Windows specific method to avoid initialization issues +* and locking issues that are unique to Windows as API calls +* expect UTF-16 strings +* \param str[in] string to be converted +* \param length[in] length in characters of the string +* \returns utf8 string, empty string on failure +*/ +std::string FromW(const wchar_t* str, size_t length); + +/** +* Convert UTF-16 to UTF-8 strings +* Windows specific method to avoid initialization issues +* and locking issues that are unique to Windows as API calls +* expect UTF-16 strings +* \param str[in] string to be converted +* \returns utf8 string, empty string on failure +*/ +std::string FromW(const std::wstring& str); + +/** +* Convert UTF-8 to UTF-16 strings +* Windows specific method to avoid initialization issues +* and locking issues that are unique to Windows as API calls +* expect UTF-16 strings +* \param str[in] string to be converted +* \param length[in] length in characters of the string +* \returns UTF-16 string, empty string on failure +*/ +std::wstring ToW(const char* str, size_t length); + +/** +* Convert UTF-8 to UTF-16 strings +* Windows specific method to avoid initialization issues +* and locking issues that are unique to Windows as API calls +* expect UTF-16 strings +* \param str[in] string to be converted +* \returns UTF-16 string, empty string on failure +*/ +std::wstring ToW(const std::string& str); +} +#endif + #endif diff --git a/lib/UnrarXLib/win32acl.cpp b/lib/UnrarXLib/win32acl.cpp index 5e228b89a7..fd645f4cf7 100644 --- a/lib/UnrarXLib/win32acl.cpp +++ b/lib/UnrarXLib/win32acl.cpp @@ -7,73 +7,14 @@ static bool ReadSacl=false; #ifndef SFX_MODULE void ExtractACL(Archive &Arc,char *FileName,wchar *FileNameW) { -#if defined(_XBOX) || defined (_LINUX) return; -#else - if (!WinNT()) - return; - - SetPrivileges(); - - if (Arc.HeaderCRC!=Arc.EAHead.HeadCRC) - { - Log(Arc.FileName,St(MACLBroken),FileName); - ErrHandler.SetErrorCode(CRC_ERROR); - return; - } - - if (Arc.EAHead.Method<0x31 || Arc.EAHead.Method>0x35 || Arc.EAHead.UnpVer>PACK_VER) - { - Log(Arc.FileName,St(MACLUnknown),FileName); - ErrHandler.SetErrorCode(WARNING); - return; - } - - ComprDataIO DataIO; - Unpack Unpack(&DataIO); - Unpack.Init(); - - Array<unsigned char> UnpData(Arc.EAHead.UnpSize); - DataIO.SetUnpackToMemory(&UnpData[0],Arc.EAHead.UnpSize); - DataIO.SetPackedSizeToRead(Arc.EAHead.DataSize); - DataIO.EnableShowProgress(false); - DataIO.SetFiles(&Arc,NULL); - Unpack.SetDestSize(Arc.EAHead.UnpSize); - Unpack.DoUnpack(Arc.EAHead.UnpVer,false); - - if (Arc.EAHead.EACRC!=~DataIO.UnpFileCRC) - { - Log(Arc.FileName,St(MACLBroken),FileName); - ErrHandler.SetErrorCode(CRC_ERROR); - return; - } - - SECURITY_INFORMATION si=OWNER_SECURITY_INFORMATION|GROUP_SECURITY_INFORMATION| - DACL_SECURITY_INFORMATION; - if (ReadSacl) - si|=SACL_SECURITY_INFORMATION; - SECURITY_DESCRIPTOR *sd=(SECURITY_DESCRIPTOR *)&UnpData[0]; - - int SetCode; - if (FileNameW!=NULL) - SetCode=SetFileSecurityW(FileNameW,si,sd); - else - SetCode=SetFileSecurity(FileName,si,sd); - - if (!SetCode) - { - Log(Arc.FileName,St(MACLSetError),FileName); - ErrHandler.SysErrMsg(); - ErrHandler.SetErrorCode(WARNING); - } -#endif } #endif void ExtractACLNew(Archive &Arc,char *FileName,wchar *FileNameW) { -#if defined(_XBOX) || defined(_LINUX) +#if defined(_XBOX) || defined(_LINUX) || defined(TARGET_WINDOWS) || defined(TARGET_WIN10) return; #else if (!WinNT()) |