From 1f12c488c45b1cb95b861e1e7347ae3d7ae455bd Mon Sep 17 00:00:00 2001 From: ttyS3 Date: Sat, 23 Mar 2024 19:38:29 +0000 Subject: [PATCH] chore(unrar_sys): upgrade unrar source code version to 7.0.7 --- unrar_sys/vendor/unrar/arccmt.cpp | 66 +- unrar_sys/vendor/unrar/archive.cpp | 33 +- unrar_sys/vendor/unrar/archive.hpp | 40 +- unrar_sys/vendor/unrar/arcread.cpp | 222 +++--- unrar_sys/vendor/unrar/blake2s.cpp | 32 +- unrar_sys/vendor/unrar/blake2s_sse.cpp | 30 +- unrar_sys/vendor/unrar/blake2sp.cpp | 4 +- unrar_sys/vendor/unrar/cmddata.cpp | 381 ++++++---- unrar_sys/vendor/unrar/cmddata.hpp | 38 +- unrar_sys/vendor/unrar/cmdfilter.cpp | 82 ++- unrar_sys/vendor/unrar/cmdmix.cpp | 2 +- unrar_sys/vendor/unrar/coder.cpp | 8 +- unrar_sys/vendor/unrar/coder.hpp | 2 +- unrar_sys/vendor/unrar/compress.hpp | 15 +- unrar_sys/vendor/unrar/consio.cpp | 162 ++--- unrar_sys/vendor/unrar/consio.hpp | 8 +- unrar_sys/vendor/unrar/crc.cpp | 187 ++--- unrar_sys/vendor/unrar/crc.hpp | 4 - unrar_sys/vendor/unrar/crypt.cpp | 2 +- unrar_sys/vendor/unrar/crypt.hpp | 1 - unrar_sys/vendor/unrar/crypt1.cpp | 17 +- unrar_sys/vendor/unrar/crypt5.cpp | 1 + unrar_sys/vendor/unrar/dll.cpp | 86 +-- unrar_sys/vendor/unrar/dll.hpp | 11 +- unrar_sys/vendor/unrar/dll.rc | 10 +- unrar_sys/vendor/unrar/encname.cpp | 22 +- unrar_sys/vendor/unrar/encname.hpp | 6 +- unrar_sys/vendor/unrar/errhnd.cpp | 109 +-- unrar_sys/vendor/unrar/errhnd.hpp | 38 +- unrar_sys/vendor/unrar/extinfo.cpp | 68 +- unrar_sys/vendor/unrar/extinfo.hpp | 16 +- unrar_sys/vendor/unrar/extract.cpp | 532 ++++++++------ unrar_sys/vendor/unrar/extract.hpp | 33 +- unrar_sys/vendor/unrar/filcreat.cpp | 83 +-- unrar_sys/vendor/unrar/filcreat.hpp | 6 +- unrar_sys/vendor/unrar/file.cpp | 87 ++- unrar_sys/vendor/unrar/file.hpp | 34 +- unrar_sys/vendor/unrar/filefn.cpp | 327 +++++---- unrar_sys/vendor/unrar/filefn.hpp | 41 +- unrar_sys/vendor/unrar/filestr.cpp | 57 +- unrar_sys/vendor/unrar/filestr.hpp | 2 +- unrar_sys/vendor/unrar/find.cpp | 70 +- unrar_sys/vendor/unrar/find.hpp | 10 +- unrar_sys/vendor/unrar/getbits.cpp | 14 +- unrar_sys/vendor/unrar/getbits.hpp | 23 +- unrar_sys/vendor/unrar/hardlinks.cpp | 12 +- unrar_sys/vendor/unrar/hash.cpp | 152 +++- unrar_sys/vendor/unrar/hash.hpp | 19 +- unrar_sys/vendor/unrar/headers.cpp | 4 +- unrar_sys/vendor/unrar/headers.hpp | 26 +- unrar_sys/vendor/unrar/headers5.hpp | 35 +- unrar_sys/vendor/unrar/isnt.cpp | 18 +- unrar_sys/vendor/unrar/list.cpp | 111 ++- unrar_sys/vendor/unrar/loclang.hpp | 39 +- unrar_sys/vendor/unrar/log.cpp | 25 +- unrar_sys/vendor/unrar/log.hpp | 3 +- unrar_sys/vendor/unrar/makefile | 103 +-- unrar_sys/vendor/unrar/match.cpp | 90 ++- unrar_sys/vendor/unrar/match.hpp | 7 +- unrar_sys/vendor/unrar/model.cpp | 8 +- unrar_sys/vendor/unrar/model.hpp | 2 +- unrar_sys/vendor/unrar/options.cpp | 1 + unrar_sys/vendor/unrar/options.hpp | 28 +- unrar_sys/vendor/unrar/os.hpp | 99 ++- unrar_sys/vendor/unrar/pathfn.cpp | 924 +++++++++++++----------- unrar_sys/vendor/unrar/pathfn.hpp | 108 ++- unrar_sys/vendor/unrar/qopen.cpp | 12 +- unrar_sys/vendor/unrar/qopen.hpp | 2 +- unrar_sys/vendor/unrar/rar.cpp | 15 +- unrar_sys/vendor/unrar/rar.hpp | 4 - unrar_sys/vendor/unrar/rardefs.hpp | 15 + unrar_sys/vendor/unrar/raros.hpp | 11 +- unrar_sys/vendor/unrar/rartypes.hpp | 2 +- unrar_sys/vendor/unrar/rarvm.cpp | 2 +- unrar_sys/vendor/unrar/rawint.hpp | 71 +- unrar_sys/vendor/unrar/rawread.cpp | 15 +- unrar_sys/vendor/unrar/rawread.hpp | 4 +- unrar_sys/vendor/unrar/rdwrfn.cpp | 19 +- unrar_sys/vendor/unrar/rdwrfn.hpp | 5 - unrar_sys/vendor/unrar/recvol.cpp | 44 +- unrar_sys/vendor/unrar/recvol.hpp | 20 +- unrar_sys/vendor/unrar/recvol3.cpp | 117 ++- unrar_sys/vendor/unrar/recvol5.cpp | 92 ++- unrar_sys/vendor/unrar/resource.cpp | 9 - unrar_sys/vendor/unrar/resource.hpp | 2 - unrar_sys/vendor/unrar/rijndael.cpp | 46 +- unrar_sys/vendor/unrar/rijndael.hpp | 17 +- unrar_sys/vendor/unrar/rs16.cpp | 16 +- unrar_sys/vendor/unrar/rs16.hpp | 3 + unrar_sys/vendor/unrar/scantree.cpp | 165 +++-- unrar_sys/vendor/unrar/scantree.hpp | 23 +- unrar_sys/vendor/unrar/secpassword.cpp | 14 +- unrar_sys/vendor/unrar/secpassword.hpp | 2 + unrar_sys/vendor/unrar/sha256.cpp | 14 +- unrar_sys/vendor/unrar/sha256.hpp | 1 + unrar_sys/vendor/unrar/strfn.cpp | 276 ++++--- unrar_sys/vendor/unrar/strfn.hpp | 29 +- unrar_sys/vendor/unrar/strlist.cpp | 62 +- unrar_sys/vendor/unrar/strlist.hpp | 13 +- unrar_sys/vendor/unrar/system.cpp | 25 +- unrar_sys/vendor/unrar/system.hpp | 3 +- unrar_sys/vendor/unrar/timefn.cpp | 22 +- unrar_sys/vendor/unrar/timefn.hpp | 4 +- unrar_sys/vendor/unrar/ui.hpp | 58 +- unrar_sys/vendor/unrar/uicommon.cpp | 58 +- unrar_sys/vendor/unrar/uiconsole.cpp | 64 +- unrar_sys/vendor/unrar/uisilent.cpp | 21 +- unrar_sys/vendor/unrar/ulinks.cpp | 64 +- unrar_sys/vendor/unrar/unicode.cpp | 272 ++++++- unrar_sys/vendor/unrar/unicode.hpp | 16 +- unrar_sys/vendor/unrar/unpack.cpp | 132 ++-- unrar_sys/vendor/unrar/unpack.hpp | 79 +- unrar_sys/vendor/unrar/unpack15.cpp | 106 +-- unrar_sys/vendor/unrar/unpack20.cpp | 8 +- unrar_sys/vendor/unrar/unpack30.cpp | 65 +- unrar_sys/vendor/unrar/unpack50.cpp | 119 +-- unrar_sys/vendor/unrar/unpack50frag.cpp | 30 +- unrar_sys/vendor/unrar/unpack50mt.cpp | 68 +- unrar_sys/vendor/unrar/unpackinline.cpp | 59 +- unrar_sys/vendor/unrar/uowners.cpp | 39 +- unrar_sys/vendor/unrar/version.hpp | 10 +- unrar_sys/vendor/unrar/volume.cpp | 89 ++- unrar_sys/vendor/unrar/win32acl.cpp | 18 +- unrar_sys/vendor/unrar/win32lnk.cpp | 54 +- unrar_sys/vendor/unrar/win32stm.cpp | 92 ++- 125 files changed, 4388 insertions(+), 3165 deletions(-) diff --git a/unrar_sys/vendor/unrar/arccmt.cpp b/unrar_sys/vendor/unrar/arccmt.cpp index 8b7e498..fc9dccc 100644 --- a/unrar_sys/vendor/unrar/arccmt.cpp +++ b/unrar_sys/vendor/unrar/arccmt.cpp @@ -1,6 +1,6 @@ static bool IsAnsiEscComment(const wchar *Data,size_t Size); -bool Archive::GetComment(Array *CmtData) +bool Archive::GetComment(std::wstring &CmtData) { if (!MainComment) return false; @@ -11,7 +11,7 @@ bool Archive::GetComment(Array *CmtData) } -bool Archive::DoGetComment(Array *CmtData) +bool Archive::DoGetComment(std::wstring &CmtData) { #ifndef SFX_MODULE uint CmtLength; @@ -101,10 +101,9 @@ bool Archive::DoGetComment(Array *CmtData) // 4x memory for OEM to UTF-8 output here. OemToCharBuffA((char *)UnpData,(char *)UnpData,(DWORD)UnpDataSize); #endif - CmtData->Alloc(UnpDataSize+1); - memset(CmtData->Addr(0),0,CmtData->Size()*sizeof(wchar)); - CharToWide((char *)UnpData,CmtData->Addr(0),CmtData->Size()); - CmtData->Alloc(wcslen(CmtData->Addr(0))); +// CmtData.resize(UnpDataSize+1); + CharToWide((const char *)UnpData,CmtData); +// CmtData.resize(wcslen(CmtData->data())); } } } @@ -112,12 +111,12 @@ bool Archive::DoGetComment(Array *CmtData) { if (CmtLength==0) return false; - Array CmtRaw(CmtLength); - int ReadSize=Read(&CmtRaw[0],CmtLength); + std::vector CmtRaw(CmtLength); + int ReadSize=Read(CmtRaw.data(),CmtLength); if (ReadSize>=0 && (uint)ReadSize *CmtData) uiMsg(UIERROR_CMTBROKEN,FileName); return false; } - CmtData->Alloc(CmtLength+1); - CmtRaw.Push(0); +// CmtData.resize(CmtLength+1); + CmtRaw.push_back(0); #ifdef _WIN_ALL // If we ever decide to extend it to Android, we'll need to alloc // 4x memory for OEM to UTF-8 output here. - OemToCharA((char *)&CmtRaw[0],(char *)&CmtRaw[0]); + OemToCharA((char *)CmtRaw.data(),(char *)CmtRaw.data()); #endif - CharToWide((char *)&CmtRaw[0],CmtData->Addr(0),CmtData->Size()); - CmtData->Alloc(wcslen(CmtData->Addr(0))); + CharToWide((const char *)CmtRaw.data(),CmtData); +// CmtData->resize(wcslen(CmtData->data())); } #endif - return CmtData->Size() > 0; + return CmtData.size() > 0; } -bool Archive::ReadCommentData(Array *CmtData) +bool Archive::ReadCommentData(std::wstring &CmtData) { - Array CmtRaw; + std::vector CmtRaw; if (!ReadSubData(&CmtRaw,NULL,false)) return false; - size_t CmtSize=CmtRaw.Size(); - CmtRaw.Push(0); - CmtData->Alloc(CmtSize+1); + size_t CmtSize=CmtRaw.size(); + CmtRaw.push_back(0); +// CmtData->resize(CmtSize+1); if (Format==RARFMT50) - UtfToWide((char *)&CmtRaw[0],CmtData->Addr(0),CmtData->Size()); + UtfToWide((char *)CmtRaw.data(),CmtData); else if ((SubHead.SubFlags & SUBHEAD_FLAGS_CMT_UNICODE)!=0) { - RawToWide(&CmtRaw[0],CmtData->Addr(0),CmtSize/2); - (*CmtData)[CmtSize/2]=0; - + CmtData=RawToWide(CmtRaw); } else { - CharToWide((char *)&CmtRaw[0],CmtData->Addr(0),CmtData->Size()); + CharToWide((const char *)CmtRaw.data(),CmtData); } - CmtData->Alloc(wcslen(CmtData->Addr(0))); // Set buffer size to actual comment length. +// CmtData->resize(wcslen(CmtData->data())); // Set buffer size to actual comment length. return true; } @@ -170,15 +167,16 @@ void Archive::ViewComment() { if (Cmd->DisableComment) return; - Array CmtBuf; - if (GetComment(&CmtBuf)) // In GUI too, so "Test" command detects broken comments. + std::wstring CmtBuf; + if (GetComment(CmtBuf)) // In GUI too, so "Test" command detects broken comments. { - size_t CmtSize=CmtBuf.Size(); - wchar *ChPtr=wcschr(&CmtBuf[0],0x1A); - if (ChPtr!=NULL) - CmtSize=ChPtr-&CmtBuf[0]; - mprintf(L"\n"); - OutComment(&CmtBuf[0],CmtSize); + size_t CmtSize=CmtBuf.size(); + auto EndPos=CmtBuf.find(0x1A); + if (EndPos!=std::wstring::npos) + CmtSize=EndPos; + mprintf(St(MArcComment)); + mprintf(L":\n"); + OutComment(CmtBuf); } } diff --git a/unrar_sys/vendor/unrar/archive.cpp b/unrar_sys/vendor/unrar/archive.cpp index 25f0c3b..c2293de 100644 --- a/unrar_sys/vendor/unrar/archive.cpp +++ b/unrar_sys/vendor/unrar/archive.cpp @@ -39,7 +39,6 @@ Archive::Archive(CommandData *InitCmd) VolWrite=0; AddingFilesSize=0; AddingHeadersSize=0; - *FirstVolumeName=0; Splitting=false; NewArchive=false; @@ -74,7 +73,7 @@ void Archive::CheckArc(bool EnableBroken) #if !defined(SFX_MODULE) -void Archive::CheckOpen(const wchar *Name) +void Archive::CheckOpen(const std::wstring &Name) { TOpen(Name); CheckArc(false); @@ -82,7 +81,7 @@ void Archive::CheckOpen(const wchar *Name) #endif -bool Archive::WCheckOpen(const wchar *Name) +bool Archive::WCheckOpen(const std::wstring &Name) { if (!WOpen(Name)) return false; @@ -148,9 +147,9 @@ bool Archive::IsArchive(bool EnableBroken) } else { - Array Buffer(MAXSFXSIZE); + std::vector Buffer(MAXSFXSIZE); long CurPos=(long)Tell(); - int ReadSize=Read(&Buffer[0],Buffer.Size()-16); + int ReadSize=Read(Buffer.data(),Buffer.size()-16); for (int I=0;I0x10000000000ULL) + return 0; + uint64 Pow2=0x20000; // Power of 2 dictionary size. + for (;2*Pow2<=Size;Pow2*=2) + Flags+=FCI_DICT_BIT0; + if (Size==Pow2) + return Size; // If 'Size' is the power of 2, return it as is. + + // Get the number of Pow2/32 to add to Pow2 for nearest value not exceeding 'Size'. + uint64 Fraction=(Size-Pow2)/(Pow2/32); + Flags+=(uint)Fraction*FCI_DICT_FRACT0; + return Pow2+Fraction*(Pow2/32); +} diff --git a/unrar_sys/vendor/unrar/archive.hpp b/unrar_sys/vendor/unrar/archive.hpp index c0019ae..58bdafa 100644 --- a/unrar_sys/vendor/unrar/archive.hpp +++ b/unrar_sys/vendor/unrar/archive.hpp @@ -27,7 +27,7 @@ class Archive:public File { private: void UpdateLatestTime(FileHeader *CurBlock); - void ConvertNameCase(wchar *Name); + void ConvertNameCase(std::wstring &Name); void ConvertFileHeader(FileHeader *hd); size_t ReadHeader14(); size_t ReadHeader15(); @@ -36,9 +36,9 @@ class Archive:public File void RequestArcPassword(RarCheckPassword *SelPwd); void UnexpEndArcMsg(); void BrokenHeaderMsg(); - void UnkEncVerMsg(const wchar *Name,const wchar *Info); - bool DoGetComment(Array *CmtData); - bool ReadCommentData(Array *CmtData); + void UnkEncVerMsg(const std::wstring &Name,const std::wstring &Info); + bool DoGetComment(std::wstring &CmtData); + bool ReadCommentData(std::wstring &CmtData); #if !defined(RAR_NOCRYPT) CryptData HeadersCrypt; @@ -67,9 +67,9 @@ class Archive:public File size_t SearchRR(); size_t ReadHeader(); void CheckArc(bool EnableBroken); - void CheckOpen(const wchar *Name); - bool WCheckOpen(const wchar *Name); - bool GetComment(Array *CmtData); + void CheckOpen(const std::wstring &Name); + bool WCheckOpen(const std::wstring &Name); + bool GetComment(std::wstring &CmtData); void ViewComment(); void SetLatestTime(RarTime *NewTime); void SeekToNext(); @@ -79,23 +79,25 @@ class Archive:public File void VolSubtractHeaderSize(size_t SubSize); uint FullHeaderSize(size_t Size); int64 GetStartPos(); - void AddSubData(byte *SrcData,uint64 DataSize,File *SrcFile, + void AddSubData(const byte *SrcData,uint64 DataSize,File *SrcFile, const wchar *Name,uint Flags); - bool ReadSubData(Array *UnpData,File *DestFile,bool TestMode); + bool ReadSubData(std::vector *UnpData,File *DestFile,bool TestMode); HEADER_TYPE GetHeaderType() {return CurHeaderType;} CommandData* GetCommandData() {return Cmd;} void SetSilentOpen(bool Mode) {SilentOpen=Mode;} -#if 0 - void GetRecoveryInfo(bool Required,int64 *Size,int *Percent); -#endif #ifdef USE_QOPEN - bool Open(const wchar *Name,uint Mode=FMF_READ); - int Read(void *Data,size_t Size); - void Seek(int64 Offset,int Method); - int64 Tell(); + bool Open(const std::wstring &Name,uint Mode=FMF_READ) override; + int Read(void *Data,size_t Size) override; + void Seek(int64 Offset,int Method) override; + int64 Tell() override; void QOpenUnload() {QOpen.Unload();} void SetProhibitQOpen(bool Mode) {ProhibitQOpen=Mode;} #endif + static uint64 GetWinSize(uint64 Size,uint &Flags); + + // Needed to see wstring based Open from File. Otherwise compiler finds + // Open in Archive and doesn't check the base class overloads. + using File::Open; BaseBlock ShortBlock; MarkHeader MarkHead; @@ -135,12 +137,16 @@ class Archive:public File uint VolNumber; int64 VolWrite; + + // Total size of files adding to archive. Might also include the size of + // files repacked in solid archive. uint64 AddingFilesSize; + uint64 AddingHeadersSize; bool NewArchive; - wchar FirstVolumeName[NM]; + std::wstring FirstVolumeName; }; diff --git a/unrar_sys/vendor/unrar/arcread.cpp b/unrar_sys/vendor/unrar/arcread.cpp index 6b7dd98..1c59bc2 100644 --- a/unrar_sys/vendor/unrar/arcread.cpp +++ b/unrar_sys/vendor/unrar/arcread.cpp @@ -119,7 +119,7 @@ void Archive::BrokenHeaderMsg() } -void Archive::UnkEncVerMsg(const wchar *Name,const wchar *Info) +void Archive::UnkEncVerMsg(const std::wstring &Name,const std::wstring &Info) { uiMsg(UIERROR_UNKNOWNENCMETHOD,FileName,Name,Info); ErrHandler.SetErrorCode(RARX_WARNING); @@ -222,7 +222,7 @@ size_t Archive::ReadHeader15() { case HEAD_MAIN: MainHead.Reset(); - *(BaseBlock *)&MainHead=ShortBlock; + MainHead.SetBaseBlock(ShortBlock); MainHead.HighPosAV=Raw.Get2(); MainHead.PosAV=Raw.Get4(); @@ -248,7 +248,7 @@ size_t Archive::ReadHeader15() FileHeader *hd=FileBlock ? &FileHead:&SubHead; hd->Reset(); - *(BaseBlock *)hd=ShortBlock; + hd->SetBaseBlock(ShortBlock); hd->SplitBefore=(hd->Flags & LHD_SPLIT_BEFORE)!=0; hd->SplitAfter=(hd->Flags & LHD_SPLIT_AFTER)!=0; @@ -307,7 +307,7 @@ size_t Archive::ReadHeader15() if (hd->HostOS==HOST_UNIX && (hd->FileAttr & 0xF000)==0xA000) { hd->RedirType=FSREDIR_UNIXSYMLINK; - *hd->RedirName=0; + hd->RedirName.clear(); } hd->Inherited=!FileBlock && (hd->SubFlags & SUBHEAD_FLAGS_INHERITED)!=0; @@ -334,27 +334,26 @@ size_t Archive::ReadHeader15() if (hd->UnknownUnpSize) hd->UnpSize=INT64NDF; - char FileName[NM*4]; - size_t ReadNameSize=Min(NameSize,ASIZE(FileName)-1); - Raw.GetB((byte *)FileName,ReadNameSize); - FileName[ReadNameSize]=0; + size_t ReadNameSize=Min(NameSize,MAXPATHSIZE); + std::string FileName(ReadNameSize,0); + Raw.GetB((byte *)&FileName[0],ReadNameSize); if (FileBlock) { - *hd->FileName=0; + hd->FileName.clear(); if ((hd->Flags & LHD_UNICODE)!=0) { EncodeFileName NameCoder; - size_t Length=strlen(FileName); + size_t Length=strlen(FileName.data()); Length++; if (ReadNameSize>Length) - NameCoder.Decode(FileName,ReadNameSize,(byte *)FileName+Length, - ReadNameSize-Length,hd->FileName, - ASIZE(hd->FileName)); + NameCoder.Decode(FileName.data(),ReadNameSize, + (byte *)&FileName[Length], + ReadNameSize-Length,hd->FileName); } - if (*hd->FileName==0) - ArcCharToWide(FileName,hd->FileName,ASIZE(hd->FileName),ACTW_OEM); + if (hd->FileName.empty()) + ArcCharToWide(FileName.data(),hd->FileName,ACTW_OEM); #ifndef SFX_MODULE ConvertNameCase(hd->FileName); @@ -363,7 +362,7 @@ size_t Archive::ReadHeader15() } else { - CharToWide(FileName,hd->FileName,ASIZE(hd->FileName)); + CharToWide(FileName.data(),hd->FileName); // Calculate the size of optional data. int DataSize=int(hd->HeadSize-NameSize-SIZEOF_FILEHEAD3); @@ -374,14 +373,15 @@ size_t Archive::ReadHeader15() { // Here we read optional additional fields for subheaders. // They are stored after the file name and before salt. - hd->SubData.Alloc(DataSize); - Raw.GetB(&hd->SubData[0],DataSize); + hd->SubData.resize(DataSize); + Raw.GetB(hd->SubData.data(),DataSize); } if (hd->CmpName(SUBHEAD_TYPE_CMT)) MainComment=true; } + if ((hd->Flags & LHD_SALT)!=0) Raw.GetB(hd->Salt,SIZE_SALT30); hd->mtime.SetDos(FileTime); @@ -424,7 +424,7 @@ size_t Archive::ReadHeader15() NextBlockPos=SafeAdd(NextBlockPos,hd->PackSize,0); bool CRCProcessedOnly=hd->CommentInHeader; - ushort HeaderCRC=Raw.GetCRC15(CRCProcessedOnly); + uint HeaderCRC=Raw.GetCRC15(CRCProcessedOnly); if (hd->HeadCRC!=HeaderCRC) { BrokenHeader=true; @@ -441,7 +441,7 @@ size_t Archive::ReadHeader15() } break; case HEAD_ENDARC: - *(BaseBlock *)&EndArcHead=ShortBlock; + EndArcHead.SetBaseBlock(ShortBlock); EndArcHead.NextVolume=(EndArcHead.Flags & EARC_NEXT_VOLUME)!=0; EndArcHead.DataCRC=(EndArcHead.Flags & EARC_DATACRC)!=0; EndArcHead.RevSpace=(EndArcHead.Flags & EARC_REVSPACE)!=0; @@ -453,14 +453,14 @@ size_t Archive::ReadHeader15() break; #ifndef SFX_MODULE case HEAD3_CMT: - *(BaseBlock *)&CommHead=ShortBlock; + CommHead.SetBaseBlock(ShortBlock); CommHead.UnpSize=Raw.Get2(); CommHead.UnpVer=Raw.Get1(); CommHead.Method=Raw.Get1(); CommHead.CommCRC=Raw.Get2(); break; case HEAD3_PROTECT: - *(BaseBlock *)&ProtectHead=ShortBlock; + ProtectHead.SetBaseBlock(ShortBlock); ProtectHead.DataSize=Raw.Get4(); ProtectHead.Version=Raw.Get1(); ProtectHead.RecSectors=Raw.Get2(); @@ -469,7 +469,7 @@ size_t Archive::ReadHeader15() NextBlockPos+=ProtectHead.DataSize; break; case HEAD3_OLDSERVICE: // RAR 2.9 and earlier. - *(BaseBlock *)&SubBlockHead=ShortBlock; + SubBlockHead.SetBaseBlock(ShortBlock); SubBlockHead.DataSize=Raw.Get4(); NextBlockPos+=SubBlockHead.DataSize; SubBlockHead.SubType=Raw.Get2(); @@ -490,10 +490,13 @@ size_t Archive::ReadHeader15() StreamHead.Method=Raw.Get1(); StreamHead.StreamCRC=Raw.Get4(); StreamHead.StreamNameSize=Raw.Get2(); - if (StreamHead.StreamNameSize>=ASIZE(StreamHead.StreamName)) - StreamHead.StreamNameSize=ASIZE(StreamHead.StreamName)-1; - Raw.GetB(StreamHead.StreamName,StreamHead.StreamNameSize); - StreamHead.StreamName[StreamHead.StreamNameSize]=0; + + const size_t MaxStreamName20=260; // Maximum allowed stream name in RAR 2.x format. + if (StreamHead.StreamNameSize>MaxStreamName20) + StreamHead.StreamNameSize=MaxStreamName20; + + StreamHead.StreamName.resize(StreamHead.StreamNameSize); + Raw.GetB(&StreamHead.StreamName[0],StreamHead.StreamNameSize); break; } break; @@ -504,7 +507,7 @@ size_t Archive::ReadHeader15() break; } - ushort HeaderCRC=Raw.GetCRC15(false); + uint HeaderCRC=Raw.GetCRC15(false); // Old AV header does not have header CRC properly set. // Old Unix owners header didn't include string fields into header size, @@ -646,7 +649,7 @@ size_t Archive::ReadHeader50() } int SizeToRead=int(BlockSize); - SizeToRead-=FirstReadSize-SizeBytes-4; // Adjust overread size bytes if any. + SizeToRead-=int(FirstReadSize-SizeBytes-4); // Adjust overread size bytes if any. uint HeaderSize=4+SizeBytes+(uint)BlockSize; if (SizeToRead<0 || HeaderSizeCRYPT_VERSION) { - wchar Info[20]; - swprintf(Info,ASIZE(Info),L"h%u",CryptVersion); - UnkEncVerMsg(FileName,Info); + UnkEncVerMsg(FileName,L"h" + std::to_wstring(CryptVersion)); + FailedHeaderDecryption=true; return 0; } uint EncFlags=(uint)Raw.GetV(); @@ -725,9 +727,8 @@ size_t Archive::ReadHeader50() CryptHead.Lg2Count=Raw.Get1(); if (CryptHead.Lg2Count>CRYPT5_KDF_LG2_COUNT_MAX) { - wchar Info[20]; - swprintf(Info,ASIZE(Info),L"hc%u",CryptHead.Lg2Count); - UnkEncVerMsg(FileName,Info); + UnkEncVerMsg(FileName,L"hc" + std::to_wstring(CryptHead.Lg2Count)); + FailedHeaderDecryption=true; return 0; } @@ -739,12 +740,8 @@ size_t Archive::ReadHeader50() byte csum[SIZE_PSWCHECK_CSUM]; Raw.GetB(csum,SIZE_PSWCHECK_CSUM); - sha256_context ctx; - sha256_init(&ctx); - sha256_process(&ctx, CryptHead.PswCheck, SIZE_PSWCHECK); - byte Digest[SHA256_DIGEST_SIZE]; - sha256_done(&ctx, Digest); + sha256_get(CryptHead.PswCheck, SIZE_PSWCHECK, Digest); CryptHead.UsePswCheck=memcmp(csum,Digest,SIZE_PSWCHECK_CSUM)==0; } @@ -754,7 +751,7 @@ size_t Archive::ReadHeader50() case HEAD_MAIN: { MainHead.Reset(); - *(BaseBlock *)&MainHead=ShortBlock; + MainHead.SetBaseBlock(ShortBlock); uint ArcFlags=(uint)Raw.GetV(); Volume=(ArcFlags & MHFL_VOLUME)!=0; @@ -832,9 +829,14 @@ size_t Archive::ReadHeader50() // we may need to use the compression algorithm 15 in the future, // but it was already used in RAR 1.5 and Unpack needs to distinguish // them. - hd->UnpVer=(CompInfo & 0x3f) + 50; - if (hd->UnpVer!=50) // Only 5.0 compression is known now. - hd->UnpVer=VER_UNKNOWN; + uint UnpVer=(CompInfo & 0x3f); + if (UnpVer==0) + hd->UnpVer=VER_PACK5; + else + if (UnpVer==1) + hd->UnpVer=VER_PACK7; + else + hd->UnpVer=VER_UNKNOWN; hd->HostOS=(byte)Raw.GetV(); size_t NameSize=(size_t)Raw.GetV(); @@ -852,14 +854,29 @@ size_t Archive::ReadHeader50() hd->SubBlock=(hd->Flags & HFL_CHILD)!=0; hd->Solid=FileBlock && (CompInfo & FCI_SOLID)!=0; hd->Dir=(hd->FileFlags & FHFL_DIRECTORY)!=0; - hd->WinSize=hd->Dir ? 0:size_t(0x20000)<<((CompInfo>>10)&0xf); + if (hd->Dir || UnpVer>1) + hd->WinSize=0; + else + { + hd->WinSize=0x20000ULL<<((CompInfo>>10)&(UnpVer==0 ? 0x0f:0x1f)); + if (UnpVer==1) + { + hd->WinSize+=hd->WinSize/32*((CompInfo>>15)&0x1f); + + // RAR7 header with RAR5 compression. Needed to append RAR7 files + // to RAR5 solid stream if new dictionary is larger than existing. + if ((CompInfo & FCI_RAR5_COMPAT)!=0) + hd->UnpVer=VER_PACK5; + if (hd->WinSize>UNPACK_MAX_DICT) + hd->UnpVer=VER_UNKNOWN; + } + } - char FileName[NM*4]; - size_t ReadNameSize=Min(NameSize,ASIZE(FileName)-1); - Raw.GetB((byte *)FileName,ReadNameSize); - FileName[ReadNameSize]=0; + size_t ReadNameSize=Min(NameSize,MAXPATHSIZE); + std::string FileName(ReadNameSize,0); + Raw.GetB((byte *)&FileName[0],ReadNameSize); - UtfToWide(FileName,hd->FileName,ASIZE(hd->FileName)); + UtfToWide(FileName.data(),hd->FileName); // Should do it before converting names, because extra fields can // affect name processing, like in case of NTFS streams. @@ -884,7 +901,7 @@ size_t Archive::ReadHeader50() break; case HEAD_ENDARC: { - *(BaseBlock *)&EndArcHead=ShortBlock; + EndArcHead.SetBaseBlock(ShortBlock); uint ArcFlags=(uint)Raw.GetV(); EndArcHead.NextVolume=(ArcFlags & EHFL_NEXTVOLUME)!=0; EndArcHead.StoreVolNumber=false; @@ -916,7 +933,7 @@ void Archive::RequestArcPassword(RarCheckPassword *CheckPwd) *PasswordA=0; if (Cmd->Callback(UCM_NEEDPASSWORD,Cmd->UserData,(LPARAM)PasswordA,ASIZE(PasswordA))==-1) *PasswordA=0; - GetWideName(PasswordA,NULL,PasswordW,ASIZE(PasswordW)); + CharToWide(PasswordA,PasswordW,ASIZE(PasswordW)); cleandata(PasswordA,sizeof(PasswordA)); } Cmd->Password.Set(PasswordW); @@ -991,19 +1008,14 @@ void Archive::ProcessExtra50(RawRead *Raw,size_t ExtraSize,const BaseBlock *bb) if ((Flags & MHEXTRA_METADATA_NAME)!=0) { uint64 NameSize=Raw->GetV(); - if (NameSize>0 && NameSize<0x10000) // Prevent excessive allocation. + if (NameSize>0 && NameSize NameU((size_t)NameSize); // UTF-8 name. + std::string NameU((size_t)NameSize,0); // UTF-8 name. Raw->GetB(&NameU[0],(size_t)NameSize); // If starts from 0, the name was longer than reserved space // when saving this extra field. if (NameU[0]!=0) - { - NameU.push_back(0); - std::vector NameW(NameU.size()*4); - UtfToWide(&NameU[0],&NameW[0],NameW.size()); - hd->OrigName.assign(&NameW[0]); - } + UtfToWide(&NameU[0],hd->OrigName); } } if ((Flags & MHEXTRA_METADATA_CTIME)!=0) @@ -1029,11 +1041,7 @@ void Archive::ProcessExtra50(RawRead *Raw,size_t ExtraSize,const BaseBlock *bb) FileHeader *hd=(FileHeader *)bb; uint EncVersion=(uint)Raw->GetV(); if (EncVersion>CRYPT_VERSION) - { - wchar Info[20]; - swprintf(Info,ASIZE(Info),L"x%u",EncVersion); - UnkEncVerMsg(hd->FileName,Info); - } + UnkEncVerMsg(hd->FileName,L"x" + std::to_wstring(EncVersion)); else { uint Flags=(uint)Raw->GetV(); @@ -1041,11 +1049,7 @@ void Archive::ProcessExtra50(RawRead *Raw,size_t ExtraSize,const BaseBlock *bb) hd->UseHashKey=(Flags & FHEXTRA_CRYPT_HASHMAC)!=0; hd->Lg2Count=Raw->Get1(); if (hd->Lg2Count>CRYPT5_KDF_LG2_COUNT_MAX) - { - wchar Info[20]; - swprintf(Info,ASIZE(Info),L"xc%u",hd->Lg2Count); - UnkEncVerMsg(hd->FileName,Info); - } + UnkEncVerMsg(hd->FileName,L"xc" + std::to_wstring(hd->Lg2Count)); Raw->GetB(hd->Salt,SIZE_SALT50); Raw->GetB(hd->InitV,SIZE_INITV); if (hd->UsePswCheck) @@ -1062,12 +1066,8 @@ void Archive::ProcessExtra50(RawRead *Raw,size_t ExtraSize,const BaseBlock *bb) byte csum[SIZE_PSWCHECK_CSUM]; Raw->GetB(csum,SIZE_PSWCHECK_CSUM); - sha256_context ctx; - sha256_init(&ctx); - sha256_process(&ctx, hd->PswCheck, SIZE_PSWCHECK); - byte Digest[SHA256_DIGEST_SIZE]; - sha256_done(&ctx, Digest); + sha256_get(hd->PswCheck, SIZE_PSWCHECK, Digest); hd->UsePswCheck=memcmp(csum,Digest,SIZE_PSWCHECK_CSUM)==0; @@ -1133,31 +1133,27 @@ void Archive::ProcessExtra50(RawRead *Raw,size_t ExtraSize,const BaseBlock *bb) if (Version!=0) { hd->Version=true; - - wchar VerText[20]; - swprintf(VerText,ASIZE(VerText),L";%u",Version); - wcsncatz(hd->FileName,VerText,ASIZE(hd->FileName)); + hd->FileName += L';' + std::to_wstring(Version); } } break; case FHEXTRA_REDIR: { - hd->RedirType=(FILE_SYSTEM_REDIRECT)Raw->GetV(); + FILE_SYSTEM_REDIRECT RedirType=(FILE_SYSTEM_REDIRECT)Raw->GetV(); uint Flags=(uint)Raw->GetV(); - hd->DirTarget=(Flags & FHEXTRA_REDIR_DIR)!=0; size_t NameSize=(size_t)Raw->GetV(); - char UtfName[NM*4]; - *UtfName=0; - if (NameSize0 && NameSizeGetB(UtfName,NameSize); - UtfName[NameSize]=0; - } + std::string UtfName(NameSize,0); + hd->RedirType=RedirType; + hd->DirTarget=(Flags & FHEXTRA_REDIR_DIR)!=0; + Raw->GetB(&UtfName[0],NameSize); + UtfToWide(&UtfName[0],hd->RedirName); #ifdef _WIN_ALL - UnixSlashToDos(UtfName,UtfName,ASIZE(UtfName)); + UnixSlashToDos(hd->RedirName,hd->RedirName); #endif - UtfToWide(UtfName,hd->RedirName,ASIZE(hd->RedirName)); + } } break; case FHEXTRA_UOWNER: @@ -1214,8 +1210,8 @@ void Archive::ProcessExtra50(RawRead *Raw,size_t ExtraSize,const BaseBlock *bb) // We cannot allocate too much memory here, because above // we check FieldSize againt Raw size and we control that Raw size // is sensible when reading headers. - hd->SubData.Alloc((size_t)FieldSize); - Raw->GetB(hd->SubData.Addr(0),(size_t)FieldSize); + hd->SubData.resize((size_t)FieldSize); + Raw->GetB(hd->SubData.data(),(size_t)FieldSize); } break; } @@ -1238,7 +1234,7 @@ size_t Archive::ReadHeader14() Raw.GetB(Mark,4); uint HeadSize=Raw.Get2(); if (HeadSize<7) - return false; + return 0; byte Flags=Raw.Get1(); NextBlockPos=CurBlockPos+HeadSize; CurHeaderType=HEAD_MAIN; @@ -1261,7 +1257,7 @@ size_t Archive::ReadHeader14() FileHead.FileHash.CRC32=Raw.Get2(); FileHead.HeadSize=Raw.Get2(); if (FileHead.HeadSize<21) - return false; + return 0; uint FileTime=Raw.Get4(); FileHead.FileAttr=Raw.Get1(); FileHead.Flags=Raw.Get1()|LONG_BLOCK; @@ -1285,12 +1281,13 @@ size_t Archive::ReadHeader14() Raw.Read(NameSize); - char FileName[NM]; - size_t ReadNameSize=Min(NameSize,ASIZE(FileName)-1); - Raw.GetB((byte *)FileName,ReadNameSize); - FileName[ReadNameSize]=0; - IntToExt(FileName,FileName,ASIZE(FileName)); - CharToWide(FileName,FileHead.FileName,ASIZE(FileHead.FileName)); + // RAR 1.4 name size is stored in a single byte field and it can't + // exceed 255, so additional checks are not needed. + std::string FileName(NameSize,0); + Raw.GetB((byte *)&FileName[0],NameSize); + std::string NameA; + IntToExt(FileName,NameA); + CharToWide(NameA,FileHead.FileName); ConvertNameCase(FileHead.FileName); ConvertFileHeader(&FileHead); @@ -1304,7 +1301,7 @@ size_t Archive::ReadHeader14() #ifndef SFX_MODULE -void Archive::ConvertNameCase(wchar *Name) +void Archive::ConvertNameCase(std::wstring &Name) { if (Cmd->ConvertNames==NAMES_UPPERCASE) wcsupper(Name); @@ -1322,7 +1319,7 @@ bool Archive::IsArcDir() void Archive::ConvertAttributes() { -#if defined(_WIN_ALL) || defined(_EMX) +#ifdef _WIN_ALL if (FileHead.HSType!=HSYS_WINDOWS) FileHead.FileAttr=FileHead.Dir ? 0x10 : 0x20; #endif @@ -1387,19 +1384,23 @@ void Archive::ConvertAttributes() void Archive::ConvertFileHeader(FileHeader *hd) { +/* if (hd->HSType==HSYS_UNKNOWN) if (hd->Dir) hd->FileAttr=0x10; else hd->FileAttr=0x20; +*/ #ifdef _WIN_ALL if (hd->HSType==HSYS_UNIX) // Convert Unix, OS X and Android decomposed chracters to Windows precomposed. - ConvertToPrecomposed(hd->FileName,ASIZE(hd->FileName)); + ConvertToPrecomposed(hd->FileName); #endif - for (wchar *s=hd->FileName;*s!=0;s++) + for (uint I=0;IFileName.size();I++) { + wchar *s=&hd->FileName[I]; + #ifdef _UNIX // Backslash is the invalid character for Windows file headers, // but it can present in Unix file names extracted in Unix. @@ -1407,7 +1408,7 @@ void Archive::ConvertFileHeader(FileHeader *hd) *s='_'; #endif -#if defined(_WIN_ALL) || defined(_EMX) +#ifdef _WIN_ALL // RAR 5.0 archives do not use '\' as path separator, so if we see it, // it means that it is a part of Unix file name, which we cannot // extract in Windows. @@ -1432,6 +1433,9 @@ void Archive::ConvertFileHeader(FileHeader *hd) if (*s=='/' || *s=='\\' && Format!=RARFMT50) *s=CPATHDIVIDER; } + + // Zeroes inside might be possible in broken Unicode names decoded with EncodeFileName::Decode. + TruncateAtZero(hd->FileName); // Ensure there are no zeroes inside of string. } @@ -1446,7 +1450,7 @@ int64 Archive::GetStartPos() } -bool Archive::ReadSubData(Array *UnpData,File *DestFile,bool TestMode) +bool Archive::ReadSubData(std::vector *UnpData,File *DestFile,bool TestMode) { if (BrokenHeader) { @@ -1454,7 +1458,7 @@ bool Archive::ReadSubData(Array *UnpData,File *DestFile,bool TestMode) ErrHandler.SetErrorCode(RARX_CRC); return false; } - if (SubHead.Method>5 || SubHead.UnpVer>(Format==RARFMT50 ? VER_UNPACK5:VER_UNPACK)) + if (SubHead.Method>5 || SubHead.UnpVer>(Format==RARFMT50 ? VER_UNPACK7:VER_UNPACK)) { uiMsg(UIERROR_SUBHEADERUNKNOWN,FileName); return false; @@ -1481,7 +1485,7 @@ bool Archive::ReadSubData(Array *UnpData,File *DestFile,bool TestMode) SubDataIO.SetTestMode(true); else { - UnpData->Alloc((size_t)SubHead.UnpSize); + UnpData->resize((size_t)SubHead.UnpSize); SubDataIO.SetUnpackToMemory(&(*UnpData)[0],(uint)SubHead.UnpSize); } } @@ -1510,7 +1514,7 @@ bool Archive::ReadSubData(Array *UnpData,File *DestFile,bool TestMode) uiMsg(UIERROR_SUBHEADERDATABROKEN,FileName,SubHead.FileName); ErrHandler.SetErrorCode(RARX_CRC); if (UnpData!=NULL) - UnpData->Reset(); + UnpData->clear(); return false; } return true; diff --git a/unrar_sys/vendor/unrar/blake2s.cpp b/unrar_sys/vendor/unrar/blake2s.cpp index 317603d..ae9d4e0 100644 --- a/unrar_sys/vendor/unrar/blake2s.cpp +++ b/unrar_sys/vendor/unrar/blake2s.cpp @@ -2,6 +2,20 @@ #include "rar.hpp" +static const byte blake2s_sigma[10][16] = +{ + { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 } , + { 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 } , + { 11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4 } , + { 7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8 } , + { 9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13 } , + { 2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9 } , + { 12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11 } , + { 13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10 } , + { 6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5 } , + { 10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13 , 0 } , +}; + #ifdef USE_SSE #include "blake2s_sse.cpp" #endif @@ -18,20 +32,6 @@ static const uint32 blake2s_IV[8] = 0x510E527FUL, 0x9B05688CUL, 0x1F83D9ABUL, 0x5BE0CD19UL }; -static const byte blake2s_sigma[10][16] = -{ - { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 } , - { 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 } , - { 11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4 } , - { 7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8 } , - { 9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13 } , - { 2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9 } , - { 12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11 } , - { 13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10 } , - { 6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5 } , - { 10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13 , 0 } , -}; - static inline void blake2s_set_lastnode( blake2s_state *S ) { S->f[1] = ~0U; @@ -134,11 +134,7 @@ void blake2s_update( blake2s_state *S, const byte *in, size_t inlen ) blake2s_increment_counter( S, BLAKE2S_BLOCKBYTES ); #ifdef USE_SSE -#ifdef _WIN_32 // We use SSSE3 _mm_shuffle_epi8 only in x64 mode. - if (_SSE_Version>=SSE_SSE2) -#else if (_SSE_Version>=SSE_SSSE3) -#endif blake2s_compress_sse( S, S->buf ); else blake2s_compress( S, S->buf ); // Compress diff --git a/unrar_sys/vendor/unrar/blake2s_sse.cpp b/unrar_sys/vendor/unrar/blake2s_sse.cpp index 1a02f21..933fcad 100644 --- a/unrar_sys/vendor/unrar/blake2s_sse.cpp +++ b/unrar_sys/vendor/unrar/blake2s_sse.cpp @@ -1,15 +1,14 @@ // Based on public domain code written in 2012 by Samuel Neves -extern const byte blake2s_sigma[10][16]; - // Initialization vector. static __m128i blake2s_IV_0_3, blake2s_IV_4_7; -#ifdef _WIN_64 -// Constants for cyclic rotation. Used in 64-bit mode in mm_rotr_epi32 macro. +// Constants for cyclic rotation. static __m128i crotr8, crotr16; -#endif +#ifdef __GNUC__ +__attribute__((target("sse2"))) +#endif static void blake2s_init_sse() { // We cannot initialize these 128 bit variables in place when declaring @@ -24,28 +23,18 @@ static void blake2s_init_sse() blake2s_IV_0_3 = _mm_setr_epi32( 0x6A09E667, 0xBB67AE85, 0x3C6EF372, 0xA54FF53A ); blake2s_IV_4_7 = _mm_setr_epi32( 0x510E527F, 0x9B05688C, 0x1F83D9AB, 0x5BE0CD19 ); -#ifdef _WIN_64 crotr8 = _mm_set_epi8( 12, 15, 14, 13, 8, 11, 10, 9, 4, 7, 6, 5, 0, 3, 2, 1 ); crotr16 = _mm_set_epi8( 13, 12, 15, 14, 9, 8, 11, 10, 5, 4, 7, 6, 1, 0, 3, 2 ); -#endif } #define LOAD(p) _mm_load_si128( (__m128i *)(p) ) #define STORE(p,r) _mm_store_si128((__m128i *)(p), r) -#ifdef _WIN_32 -// 32-bit mode has less SSE2 registers and in MSVC2008 it is more efficient -// to not use _mm_shuffle_epi8 here. -#define mm_rotr_epi32(r, c) ( \ - _mm_xor_si128(_mm_srli_epi32( (r), c ),_mm_slli_epi32( (r), 32-c )) ) -#else #define mm_rotr_epi32(r, c) ( \ c==8 ? _mm_shuffle_epi8(r,crotr8) \ : c==16 ? _mm_shuffle_epi8(r,crotr16) \ : _mm_xor_si128(_mm_srli_epi32( (r), c ),_mm_slli_epi32( (r), 32-c )) ) -#endif - #define G1(row1,row2,row3,row4,buf) \ row1 = _mm_add_epi32( _mm_add_epi32( row1, buf), row2 ); \ @@ -73,14 +62,6 @@ static void blake2s_init_sse() row3 = _mm_shuffle_epi32( row3, _MM_SHUFFLE(1,0,3,2) ); \ row2 = _mm_shuffle_epi32( row2, _MM_SHUFFLE(2,1,0,3) ); -#ifdef _WIN_64 - // MSVC 2008 in x64 mode expands _mm_set_epi32 to store to stack and load - // from stack operations, which are slower than this code. - #define _mm_set_epi32(i3,i2,i1,i0) \ - _mm_unpacklo_epi32(_mm_unpacklo_epi32(_mm_cvtsi32_si128(i0),_mm_cvtsi32_si128(i2)), \ - _mm_unpacklo_epi32(_mm_cvtsi32_si128(i1),_mm_cvtsi32_si128(i3))) -#endif - // Original BLAKE2 SSE4.1 message loading code was a little slower in x86 mode // and about the same in x64 mode in our test. Perhaps depends on compiler. // We also tried _mm_i32gather_epi32 and _mm256_i32gather_epi32 AVX2 gather @@ -101,6 +82,9 @@ static void blake2s_init_sse() } +#ifdef __GNUC__ +__attribute__((target("ssse3"))) +#endif static int blake2s_compress_sse( blake2s_state *S, const byte block[BLAKE2S_BLOCKBYTES] ) { __m128i row[4]; diff --git a/unrar_sys/vendor/unrar/blake2sp.cpp b/unrar_sys/vendor/unrar/blake2sp.cpp index da64588..91dbd14 100644 --- a/unrar_sys/vendor/unrar/blake2sp.cpp +++ b/unrar_sys/vendor/unrar/blake2sp.cpp @@ -20,7 +20,7 @@ void blake2sp_init( blake2sp_state *S ) blake2s_init_param( &S->R, 0, 1 ); // Init root. - for( uint i = 0; i < PARALLELISM_DEGREE; ++i ) + for( uint32 i = 0; i < PARALLELISM_DEGREE; ++i ) blake2s_init_param( &S->S[i], i, 0 ); // Init leaf. S->R.last_node = 1; @@ -49,6 +49,8 @@ void Blake2ThreadData::Update() if (_SSE_Version>=SSE_SSE && inlen__ >= 2 * PARALLELISM_DEGREE * BLAKE2S_BLOCKBYTES) _mm_prefetch((char*)(in__ + PARALLELISM_DEGREE * BLAKE2S_BLOCKBYTES), _MM_HINT_T0); #endif + // We tried to _forceinline blake2s_update and blake2s_compress_sse, + // but it didn't improve performance. blake2s_update( S, in__, BLAKE2S_BLOCKBYTES ); in__ += PARALLELISM_DEGREE * BLAKE2S_BLOCKBYTES; inlen__ -= PARALLELISM_DEGREE * BLAKE2S_BLOCKBYTES; diff --git a/unrar_sys/vendor/unrar/cmddata.cpp b/unrar_sys/vendor/unrar/cmddata.cpp index ffc5ff9..b7834d5 100644 --- a/unrar_sys/vendor/unrar/cmddata.cpp +++ b/unrar_sys/vendor/unrar/cmddata.cpp @@ -13,8 +13,18 @@ void CommandData::Init() { RAROptions::Init(); - *Command=0; - *ArcName=0; + Command.clear(); + ArcName.clear(); + ExtrPath.clear(); + TempPath.clear(); + SFXModule.clear(); + CommentFile.clear(); + ArcPath.clear(); + ExclArcPath.clear(); + LogName.clear(); + EmailTo.clear(); + UseStdin.clear(); + FileLists=false; NoMoreSwitches=false; @@ -30,57 +40,45 @@ void CommandData::Init() StoreArgs.Reset(); Password.Clean(); NextVolSizes.clear(); -} - - -// Return the pointer to next position in the string and store dynamically -// allocated command line parameter in Par. -static const wchar *AllocCmdParam(const wchar *CmdLine,wchar **Par) -{ - const wchar *NextCmd=GetCmdParam(CmdLine,NULL,0); - if (NextCmd==NULL) - return NULL; - size_t ParSize=NextCmd-CmdLine+2; // Parameter size including the trailing zero. - *Par=(wchar *)malloc(ParSize*sizeof(wchar)); - if (*Par==NULL) - return NULL; - return GetCmdParam(CmdLine,*Par,ParSize); +#ifdef RARDLL + DllDestName.clear(); +#endif } #if !defined(SFX_MODULE) void CommandData::ParseCommandLine(bool Preprocess,int argc, char *argv[]) { - *Command=0; + Command.clear(); NoMoreSwitches=false; #ifdef CUSTOM_CMDLINE_PARSER // In Windows we may prefer to implement our own command line parser // to avoid replacing \" by " in standard parser. Such replacing corrupts // destination paths like "dest path\" in extraction commands. - const wchar *CmdLine=GetCommandLine(); + std::wstring CmdLine=GetCommandLine(); + + std::wstring Param; + std::wstring::size_type Pos=0; - wchar *Par; for (bool FirstParam=true;;FirstParam=false) { - if ((CmdLine=AllocCmdParam(CmdLine,&Par))==NULL) + if (!GetCmdParam(CmdLine,Pos,Param)) break; if (!FirstParam) // First parameter is the executable name. if (Preprocess) - PreprocessArg(Par); + PreprocessArg(Param.data()); else - ParseArg(Par); - free(Par); + ParseArg(Param.data()); } #else - Array Arg; for (int I=1;I0 && Arg[L-1]=='.' && (L==1 || L>=2 && (IsPathDiv(Arg[L-2]) || + Arg[L-2]=='.' && (L==2 || L>=3 && IsPathDiv(Arg[L-3]))))) + FolderArg=true; + + wchar CmdChar=toupperw(Command[0]); bool Add=wcschr(L"AFUM",CmdChar)!=NULL; bool Extract=CmdChar=='X' || CmdChar=='E'; bool Repair=CmdChar=='R' && Command[1]==0; - if (EndSeparator && !Add) - wcsncpyz(ExtrPath,Arg,ASIZE(ExtrPath)); + if (FolderArg && !Add) + ExtrPath=Arg; else if ((Add || CmdChar=='T') && (*Arg!='@' || ListMode==RCLM_REJECT_LISTS)) FileArgs.AddString(Arg); @@ -147,10 +158,10 @@ void CommandData::ParseArg(wchar *Arg) } else // We use 'destpath\' when extracting and reparing. - if (Found && FileData.IsDir && (Extract || Repair) && *ExtrPath==0) + if (Found && FileData.IsDir && (Extract || Repair) && ExtrPath.empty()) { - wcsncpyz(ExtrPath,Arg,ASIZE(ExtrPath)); - AddEndSlash(ExtrPath,ASIZE(ExtrPath)); + ExtrPath=Arg; + AddEndSlash(ExtrPath); } else FileArgs.AddString(Arg); @@ -178,12 +189,12 @@ void CommandData::ParseDone() #if !defined(SFX_MODULE) void CommandData::ParseEnvVar() { - char *EnvStr=getenv("RAR"); - if (EnvStr!=NULL) + char *EnvVar=getenv("RAR"); + if (EnvVar!=NULL) { - Array EnvStrW(strlen(EnvStr)+1); - CharToWide(EnvStr,&EnvStrW[0],EnvStrW.Size()); - ProcessSwitchesString(&EnvStrW[0]); + std::wstring EnvStr; + CharToWide(EnvVar,EnvStr); + ProcessSwitchesString(EnvStr); } } #endif @@ -192,7 +203,7 @@ void CommandData::ParseEnvVar() #if !defined(SFX_MODULE) // Preprocess those parameters, which must be processed before the rest of -// command line. Return 'false' to stop further processing. +// command line. void CommandData::PreprocessArg(const wchar *Arg) { if (IsSwitch(Arg[0]) && !NoMoreSwitches) @@ -201,7 +212,7 @@ void CommandData::PreprocessArg(const wchar *Arg) if (Arg[0]=='-' && Arg[1]==0) // Switch "--". NoMoreSwitches=true; if (wcsicomp(Arg,L"cfg-")==0) - ConfigDisabled=true; + ProcessSwitch(Arg); if (wcsnicomp(Arg,L"ilog",4)==0) { // Ensure that correct log file name is already set @@ -213,13 +224,13 @@ void CommandData::PreprocessArg(const wchar *Arg) { // Process -sc before reading any file lists. ProcessSwitch(Arg); - if (*LogName!=0) + if (!LogName.empty()) InitLogOptions(LogName,ErrlogCharset); } } else - if (*Command==0) - wcsncpy(Command,Arg,ASIZE(Command)); // Need for rar.ini. + if (Command.empty()) + Command=Arg; // Need for rar.ini. } #endif @@ -237,10 +248,10 @@ void CommandData::ReadConfig() Str++; if (wcsnicomp(Str,L"switches=",9)==0) ProcessSwitchesString(Str+9); - if (*Command!=0) + if (!Command.empty()) { wchar Cmd[16]; - wcsncpyz(Cmd,Command,ASIZE(Cmd)); + wcsncpyz(Cmd,Command.c_str(),ASIZE(Cmd)); wchar C0=toupperw(Cmd[0]); wchar C1=toupperw(Cmd[1]); if (C0=='I' || C0=='L' || C0=='M' || C0=='S' || C0=='V') @@ -260,14 +271,19 @@ void CommandData::ReadConfig() #if !defined(SFX_MODULE) -void CommandData::ProcessSwitchesString(const wchar *Str) +void CommandData::ProcessSwitchesString(const std::wstring &Str) { - wchar *Par; - while ((Str=AllocCmdParam(Str,&Par))!=NULL) + std::wstring Par; + std::wstring::size_type Pos=0; + while (GetCmdParam(Str,Pos,Par)) { - if (IsSwitch(*Par)) - ProcessSwitch(Par+1); - free(Par); + if (IsSwitch(Par[0])) + ProcessSwitch(&Par[1]); + else + { + mprintf(St(MSwSyntaxError),Par.c_str()); + ErrHandler.Exit(RARX_USERERROR); + } } } #endif @@ -330,13 +346,12 @@ void CommandData::ProcessSwitch(const wchar *Switch) break; } break; - case 'N': // Reserved for archive name. - break; case 'O': AddArcOnly=true; break; case 'P': - wcsncpyz(ArcPath,Switch+2,ASIZE(ArcPath)); + // Convert slashes here than before every comparison. + SlashToNative(Switch+2,ArcPath); break; case 'S': SyncFiles=true; @@ -347,7 +362,14 @@ void CommandData::ProcessSwitch(const wchar *Switch) } break; case 'C': - if (Switch[2]==0) + if (Switch[2]!=0) + { + if (wcsicomp(Switch+1,L"FG-")==0) + ConfigDisabled=true; + else + BadSwitch(Switch); + } + else switch(toupperw(Switch[1])) { case '-': @@ -359,10 +381,15 @@ void CommandData::ProcessSwitch(const wchar *Switch) case 'L': ConvertNames=NAMES_LOWERCASE; break; + default: + BadSwitch(Switch); + break; } break; case 'D': - if (Switch[2]==0) + if (Switch[2]!=0) + BadSwitch(Switch); + else switch(toupperw(Switch[1])) { case 'S': @@ -374,6 +401,9 @@ void CommandData::ProcessSwitch(const wchar *Switch) case 'F': DeleteFiles=true; break; + default: + BadSwitch(Switch); + break; } break; case 'E': @@ -395,7 +425,11 @@ void CommandData::ProcessSwitch(const wchar *Switch) ExclPath=EXCL_ABSPATH; break; case '4': - wcsncpyz(ExclArcPath,Switch+3,ASIZE(ExclArcPath)); + // Convert slashes here than before every comparison. + SlashToNative(Switch+3,ArcPath); + break; + default: + BadSwitch(Switch); break; } break; @@ -431,7 +465,7 @@ void CommandData::ProcessSwitch(const wchar *Switch) else if (!Password.IsSet()) { - uiGetPassword(UIPASSWORD_GLOBAL,NULL,&Password,NULL); + uiGetPassword(UIPASSWORD_GLOBAL,L"",&Password,NULL); eprintf(L"\n"); } break; @@ -443,7 +477,7 @@ void CommandData::ProcessSwitch(const wchar *Switch) case 'I': if (wcsnicomp(Switch+1,L"LOG",3)==0) { - wcsncpyz(LogName,Switch[4]!=0 ? Switch+4:DefLogName,ASIZE(LogName)); + LogName=Switch[4]!=0 ? Switch+4:DefLogName; break; } if (wcsnicomp(Switch+1,L"SND",3)==0) @@ -461,7 +495,7 @@ void CommandData::ProcessSwitch(const wchar *Switch) } if (wcsnicomp(Switch+1,L"EML",3)==0) { - wcsncpyz(EmailTo,Switch[4]!=0 ? Switch+4:L"@",ASIZE(EmailTo)); + EmailTo=Switch[4]!=0 ? Switch+4:L"@"; break; } if (wcsicomp(Switch+1,L"M")==0) // For compatibility with pre-WinRAR 6.0 -im syntax. Replaced with -idv. @@ -568,12 +602,14 @@ void CommandData::ProcessSwitch(const wchar *Switch) } switch(toupperw(*(Str++))) { - case 'T': Type=FILTER_PPM; break; +// case 'T': Type=FILTER_TEXT; break; case 'E': Type=FILTER_E8; break; case 'D': Type=FILTER_DELTA; break; - case 'A': Type=FILTER_AUDIO; break; - case 'C': Type=FILTER_RGB; break; - case 'R': Type=FILTER_ARM; break; +// case 'A': Type=FILTER_AUDIO; break; +// case 'C': Type=FILTER_RGB; break; +// case 'R': Type=FILTER_ARM; break; + case 'L': Type=FILTER_LONGRANGE; break; + case 'X': Type=FILTER_EXHAUSTIVE; break; } if (*Str=='+' || *Str=='-') State=*(Str++)=='+' ? FILTER_FORCE:FILTER_DISABLE; @@ -586,6 +622,44 @@ void CommandData::ProcessSwitch(const wchar *Switch) case 'M': break; case 'D': + { + bool SetDictLimit=toupperw(Switch[2])=='X'; + + uint64 Size=atoiw(Switch+(SetDictLimit ? 3 : 2)); + wchar LastChar=toupperw(Switch[wcslen(Switch)-1]); + if (IsDigit(LastChar)) + LastChar=SetDictLimit ? 'G':'M'; // Treat -md128 as -md128m and -mdx32 as -mdx32g. + switch(LastChar) + { + case 'K': + Size*=1024; + break; + case 'M': + Size*=1024*1024; + break; + case 'G': + Size*=1024*1024*1024; + break; + default: + BadSwitch(Switch); + } + + // 2023.07.22: For 4 GB and less we also check that it is power of 2, + // so archives are compatible with RAR 5.0+. + // We allow Size>PACK_MAX_DICT here, so we can use -md[x] to unpack + // archives created by future versions with higher PACK_MAX_DICTþ + uint Flags; + if ((Size=Archive::GetWinSize(Size,Flags))==0 || + Size<=0x100000000ULL && !IsPow2(Size)) + BadSwitch(Switch); + else + if (SetDictLimit) + WinSizeLimit=Size; + else + { + WinSize=Size; + } + } break; case 'E': if (toupperw(Switch[2])=='S' && Switch[3]==0) @@ -593,25 +667,20 @@ void CommandData::ProcessSwitch(const wchar *Switch) break; case 'S': { - wchar StoreNames[1024]; - wcsncpyz(StoreNames,(Switch[2]==0 ? DefaultStoreList:Switch+2),ASIZE(StoreNames)); - wchar *Names=StoreNames; - while (*Names!=0) + std::wstring StoreNames=(Switch[2]==0 ? DefaultStoreList:Switch+2); + size_t Pos=0; + while (Pos. + std::wstring ExclArcPath; // For -ep4 switch. + std::wstring LogName; + std::wstring EmailTo; + + // Read data from stdin and store in archive under a name specified here + // when archiving. Read an archive from stdin if any non-empty string + // is specified here when extracting. + std::wstring UseStdin; StringList FileArgs; StringList ExclArgs; @@ -69,6 +82,11 @@ class CommandData:public RAROptions SecPassword Password; std::vector NextVolSizes; + + +#ifdef RARDLL + std::wstring DllDestName; +#endif }; #endif diff --git a/unrar_sys/vendor/unrar/cmdfilter.cpp b/unrar_sys/vendor/unrar/cmdfilter.cpp index e0add14..c20d8d6 100644 --- a/unrar_sys/vendor/unrar/cmdfilter.cpp +++ b/unrar_sys/vendor/unrar/cmdfilter.cpp @@ -1,7 +1,7 @@ // Return 'true' if we need to exclude the file from processing as result // of -x switch. If CheckInclList is true, we also check the file against // the include list created with -n switch. -bool CommandData::ExclCheck(const wchar *CheckName,bool Dir,bool CheckFullPath,bool CheckInclList) +bool CommandData::ExclCheck(const std::wstring &CheckName,bool Dir,bool CheckFullPath,bool CheckInclList) { if (CheckArgs(&ExclArgs,Dir,CheckName,CheckFullPath,MATCH_WILDSUBPATH)) return true; @@ -13,17 +13,15 @@ bool CommandData::ExclCheck(const wchar *CheckName,bool Dir,bool CheckFullPath,b } -bool CommandData::CheckArgs(StringList *Args,bool Dir,const wchar *CheckName,bool CheckFullPath,int MatchMode) +bool CommandData::CheckArgs(StringList *Args,bool Dir,const std::wstring &CheckName,bool CheckFullPath,int MatchMode) { - wchar *Name=ConvertPath(CheckName,NULL,0); - wchar FullName[NM]; - wchar CurMask[NM]; - *FullName=0; + std::wstring Name,FullName,CurMask; + ConvertPath(&CheckName,&Name); Args->Rewind(); - while (Args->GetString(CurMask,ASIZE(CurMask))) + while (Args->GetString(CurMask)) { - wchar *LastMaskChar=PointToLastChar(CurMask); - bool DirMask=IsPathDiv(*LastMaskChar); // Mask for directories only. + wchar LastMaskChar=GetLastChar(CurMask); + bool DirMask=IsPathDiv(LastMaskChar); // Mask for directories only. if (Dir) { @@ -33,16 +31,33 @@ bool CommandData::CheckArgs(StringList *Args,bool Dir,const wchar *CheckName,boo // We process the directory and have the directory exclusion mask. // So let's convert "mask\" to "mask" and process it normally. - *LastMaskChar=0; + CurMask.pop_back(); } else { - // REMOVED, we want -npath\* to match empty folders too. - // If mask has wildcards in name part and does not have the trailing - // '\' character, we cannot use it for directories. - - // if (IsWildcard(PointToName(CurMask))) - // continue; + // This code doesn't allow to apply -n and -x wildcard masks without + // trailing slash to folders unless these masks are * and *.*. + // See the changes history below. + // 2023.03.26: Previously we removed this code completely to let + // 'rar a arc dir -ndir\path\*' include empty folders in 'path' too. + // But then we received an email from user not willing -x*.avi to + // exclude folders like dir.avi with non-avi files. Also rar.txt + // mentions that masks like *.avi exclude only files. Initially + // we wanted masks like -npath\* or -xpath\* to match the entire + // contents of path including empty folders and added the special + // check for "*" and "*.*". But this is not very straightforward, + // when *.* and *.avi are processed differently, especially taking + // into account that we can specify the exact folder name without + // wildcards to process it and masks like 'dir*\' can be used to + // exclude folders. So we decided to skip all usual wildcard masks + // for folders. + // 2023.11.22: We returned the special check for "*" and "*.*", + // because users expected 'rar a arc dir -xdir\*' to exclude + // everything including subfolders in 'dir'. For now we returned it + // both for -n and -x, but we can limit it to -x only if needed. + std::wstring Name=PointToName(CurMask); + if (IsWildcard(Name) && Name!=L"*" && Name!=L"*.*") + continue; } } else @@ -54,7 +69,7 @@ bool CommandData::CheckArgs(StringList *Args,bool Dir,const wchar *CheckName,boo // is excluded from further scanning. if (DirMask) - wcsncatz(CurMask,L"*",ASIZE(CurMask)); + CurMask+=L"*"; } #ifndef SFX_MODULE @@ -66,19 +81,20 @@ bool CommandData::CheckArgs(StringList *Args,bool Dir,const wchar *CheckName,boo // correctly. Moreover, removing "*\" from mask would break // the comparison, because now all names have the path. - if (*FullName==0) - ConvertNameToFull(CheckName,FullName,ASIZE(FullName)); + if (FullName.empty()) + ConvertNameToFull(CheckName,FullName); if (CmpName(CurMask,FullName,MatchMode)) return true; } else #endif { - wchar NewName[NM+2],*CurName=Name; + std::wstring CurName=Name; // Important to convert before "*\" check below, so masks like // d:*\something are processed properly. - wchar *CmpMask=ConvertPath(CurMask,NULL,0); + size_t MaskOffset=ConvertPath(&CurMask,nullptr); + std::wstring CmpMask=CurMask.substr(MaskOffset); if (CmpMask[0]=='*' && IsPathDiv(CmpMask[1])) { @@ -86,10 +102,9 @@ bool CommandData::CheckArgs(StringList *Args,bool Dir,const wchar *CheckName,boo // but also in the current directory. We convert the name // from 'name' to '.\name' to be matched by "*\" part even if it is // in current directory. - NewName[0]='.'; - NewName[1]=CPATHDIVIDER; - wcsncpyz(NewName+2,Name,ASIZE(NewName)-2); - CurName=NewName; + CurName=L'.'; + CurName+=CPATHDIVIDER; + CurName+=Name; } if (CmpName(CmpMask,CurName,MatchMode)) @@ -277,10 +292,10 @@ bool CommandData::SizeCheck(int64 Size) // Return 0 if file must not be processed or a number of matched parameter otherwise. int CommandData::IsProcessFile(FileHeader &FileHead,bool *ExactMatch,int MatchType, - bool Flags,wchar *MatchedArg,uint MatchedArgSize) + bool Flags,std::wstring *MatchedArg) { - if (MatchedArg!=NULL && MatchedArgSize>0) - *MatchedArg=0; + if (MatchedArg!=NULL) + MatchedArg->clear(); bool Dir=FileHead.Dir; if (ExclCheck(FileHead.FileName,Dir,false,true)) return 0; @@ -295,17 +310,22 @@ int CommandData::IsProcessFile(FileHeader &FileHead,bool *ExactMatch,int MatchTy if (!Dir && SizeCheck(FileHead.UnpSize)) return 0; #endif - wchar *ArgName; + std::wstring ArgName; FileArgs.Rewind(); - for (int StringCount=1;(ArgName=FileArgs.GetString())!=NULL;StringCount++) + for (int StringCount=1;FileArgs.GetString(ArgName);StringCount++) + { + // Ensure that both parameters of CmpName are either C++ strings or + // pointers, so we avoid time consuming string construction for one of + // parameters in this expensive loop. if (CmpName(ArgName,FileHead.FileName,MatchType)) { if (ExactMatch!=NULL) *ExactMatch=wcsicompc(ArgName,FileHead.FileName)==0; if (MatchedArg!=NULL) - wcsncpyz(MatchedArg,ArgName,MatchedArgSize); + *MatchedArg=ArgName; return StringCount; } + } return 0; } diff --git a/unrar_sys/vendor/unrar/cmdmix.cpp b/unrar_sys/vendor/unrar/cmdmix.cpp index 6bd1e1a..e70b7d0 100644 --- a/unrar_sys/vendor/unrar/cmdmix.cpp +++ b/unrar_sys/vendor/unrar/cmdmix.cpp @@ -103,7 +103,7 @@ void CommandData::OutHelp(RAR_EXIT ExitCode) if (CmpMSGID(Help[I],MCHelpSwOW)) continue; #endif -#if !defined(_WIN_ALL) && !defined(_EMX) +#ifndef _WIN_ALL if (CmpMSGID(Help[I],MCHelpSwAC)) continue; #endif diff --git a/unrar_sys/vendor/unrar/coder.cpp b/unrar_sys/vendor/unrar/coder.cpp index 9d971a8..6282371 100644 --- a/unrar_sys/vendor/unrar/coder.cpp +++ b/unrar_sys/vendor/unrar/coder.cpp @@ -1,8 +1,8 @@ -inline unsigned int RangeCoder::GetChar() +inline byte RangeCoder::GetChar() { - return(UnpackRead->GetChar()); + return UnpackRead->GetChar(); } @@ -11,8 +11,8 @@ void RangeCoder::InitDecoder(Unpack *UnpackRead) RangeCoder::UnpackRead=UnpackRead; low=code=0; - range=uint(-1); - for (int i=0;i < 4;i++) + range=0xffffffff; + for (uint i = 0; i < 4; i++) code=(code << 8) | GetChar(); } diff --git a/unrar_sys/vendor/unrar/coder.hpp b/unrar_sys/vendor/unrar/coder.hpp index 7b36ff2..42c3f33 100644 --- a/unrar_sys/vendor/unrar/coder.hpp +++ b/unrar_sys/vendor/unrar/coder.hpp @@ -11,7 +11,7 @@ class RangeCoder inline uint GetCurrentShiftCount(uint SHIFT); inline void Decode(); inline void PutChar(unsigned int c); - inline unsigned int GetChar(); + inline byte GetChar(); uint low, code, range; struct SUBRANGE diff --git a/unrar_sys/vendor/unrar/compress.hpp b/unrar_sys/vendor/unrar/compress.hpp index 4ef8570..018909a 100644 --- a/unrar_sys/vendor/unrar/compress.hpp +++ b/unrar_sys/vendor/unrar/compress.hpp @@ -21,10 +21,12 @@ class PackDef static const uint LOW_DIST_REP_COUNT = 16; static const uint NC = 306; /* alphabet = {0, 1, 2, ..., NC - 1} */ - static const uint DC = 64; + static const uint DCB = 64; // Base distance codes up to 4 GB. + static const uint DCX = 80; // Extended distance codes up to 1 TB. static const uint LDC = 16; static const uint RC = 44; - static const uint HUFF_TABLE_SIZE = NC + DC + RC + LDC; + static const uint HUFF_TABLE_SIZEB = NC + DCB + RC + LDC; + static const uint HUFF_TABLE_SIZEX = NC + DCX + RC + LDC; static const uint BC = 20; static const uint NC30 = 299; /* alphabet = {0, 1, 2, ..., NC - 1} */ @@ -43,10 +45,6 @@ class PackDef // Largest alphabet size among all values listed above. static const uint LARGEST_TABLE_SIZE = 306; - enum { - CODE_HUFFMAN, CODE_LZ, CODE_REPEATLZ, CODE_CACHELZ, CODE_STARTFILE, - CODE_ENDFILE, CODE_FILTER, CODE_FILTERDATA - }; }; @@ -54,7 +52,10 @@ enum FilterType { // These values must not be changed, because we use them directly // in RAR5 compression and decompression code. FILTER_DELTA=0, FILTER_E8, FILTER_E8E9, FILTER_ARM, - FILTER_AUDIO, FILTER_RGB, FILTER_ITANIUM, FILTER_PPM, FILTER_NONE + FILTER_AUDIO, FILTER_RGB, FILTER_ITANIUM, FILTER_TEXT, + + // These values can be changed. + FILTER_LONGRANGE,FILTER_EXHAUSTIVE,FILTER_NONE }; #endif diff --git a/unrar_sys/vendor/unrar/consio.cpp b/unrar_sys/vendor/unrar/consio.cpp index fa35d61..023c3f3 100644 --- a/unrar_sys/vendor/unrar/consio.cpp +++ b/unrar_sys/vendor/unrar/consio.cpp @@ -5,8 +5,6 @@ static MESSAGE_TYPE MsgStream=MSG_STDOUT; static RAR_CHARSET RedirectCharset=RCH_DEFAULT; static bool ProhibitInput=false; -const int MaxMsgSize=2*NM+2048; - static bool StdoutRedirected=false,StderrRedirected=false,StdinRedirected=false; #ifdef _WIN_ALL @@ -71,44 +69,42 @@ void ProhibitConsoleInput() #ifndef SILENT static void cvt_wprintf(FILE *dest,const wchar *fmt,va_list arglist) { - // This buffer is for format string only, not for entire output, - // so it can be short enough. - wchar fmtw[1024]; - PrintfPrepareFmt(fmt,fmtw,ASIZE(fmtw)); + // No need for PrintfPrepareFmt here, vwstrprintf calls it. + std::wstring s=vwstrprintf(fmt,arglist); + + ReplaceEsc(s); + #ifdef _WIN_ALL - safebuf wchar Msg[MaxMsgSize]; if (dest==stdout && StdoutRedirected || dest==stderr && StderrRedirected) { HANDLE hOut=GetStdHandle(dest==stdout ? STD_OUTPUT_HANDLE:STD_ERROR_HANDLE); - vswprintf(Msg,ASIZE(Msg),fmtw,arglist); DWORD Written; if (RedirectCharset==RCH_UNICODE) - WriteFile(hOut,Msg,(DWORD)wcslen(Msg)*sizeof(*Msg),&Written,NULL); + WriteFile(hOut,s.data(),(DWORD)s.size()*sizeof(s[0]),&Written,NULL); else { // Avoid Unicode for redirect in Windows, it does not work with pipes. - safebuf char MsgA[MaxMsgSize]; + std::string MsgA; if (RedirectCharset==RCH_UTF8) - WideToUtf(Msg,MsgA,ASIZE(MsgA)); + WideToUtf(s,MsgA); else - WideToChar(Msg,MsgA,ASIZE(MsgA)); + WideToChar(s,MsgA); if (RedirectCharset==RCH_DEFAULT || RedirectCharset==RCH_OEM) - CharToOemA(MsgA,MsgA); // Console tools like 'more' expect OEM encoding. + CharToOemA(&MsgA[0],&MsgA[0]); // Console tools like 'more' expect OEM encoding. // We already converted \n to \r\n above, so we use WriteFile instead // of C library to avoid unnecessary additional conversion. - WriteFile(hOut,MsgA,(DWORD)strlen(MsgA),&Written,NULL); + WriteFile(hOut,MsgA.data(),(DWORD)MsgA.size(),&Written,NULL); } return; } // MSVC2008 vfwprintf writes every character to console separately // and it is too slow. We use direct WriteConsole call instead. - vswprintf(Msg,ASIZE(Msg),fmtw,arglist); HANDLE hOut=GetStdHandle(dest==stderr ? STD_ERROR_HANDLE:STD_OUTPUT_HANDLE); DWORD Written; - WriteConsole(hOut,Msg,(DWORD)wcslen(Msg),&Written,NULL); + WriteConsole(hOut,s.data(),(DWORD)s.size(),&Written,NULL); #else - vfwprintf(dest,fmtw,arglist); + fputws(s.c_str(),dest); // We do not use setbuf(NULL) in Unix (see comments in InitConsole). fflush(dest); #endif @@ -160,13 +156,11 @@ static void QuitIfInputProhibited() } -static void GetPasswordText(wchar *Str,uint MaxLength) +static void GetPasswordText(std::wstring &Str) { - if (MaxLength==0) - return; QuitIfInputProhibited(); if (StdinRedirected) - getwstr(Str,MaxLength); // Read from pipe or redirected file. + getwstr(Str); // Read from pipe or redirected file. else { #ifdef _WIN_ALL @@ -175,50 +169,47 @@ static void GetPasswordText(wchar *Str,uint MaxLength) GetConsoleMode(hConIn,&ConInMode); SetConsoleMode(hConIn,ENABLE_LINE_INPUT); // Remove ENABLE_ECHO_INPUT. + std::vector Buf(MAXPASSWORD); + // We prefer ReadConsole to ReadFile, so we can read Unicode input. DWORD Read=0; - ReadConsole(hConIn,Str,MaxLength-1,&Read,NULL); - Str[Read]=0; + ReadConsole(hConIn,Buf.data(),(DWORD)Buf.size()-1,&Read,NULL); + Buf[Read]=0; + Str=Buf.data(); + cleandata(Buf.data(),Buf.size()*sizeof(Buf[0])); + SetConsoleMode(hConIn,ConInMode); - // If entered password is longer than MAXPASSWORD and truncated, - // read its unread part anyway, so it isn't read later as the second - // password for -p switch. Low level FlushConsoleInputBuffer doesn't help + // 2023.03.12: Previously we checked for presence of "\n" in entered + // passwords, supposing that truncated strings do not include it. + // We did it to read the rest of excessively long string, so it is not + // read later as the second password for -p switch. But this "\n" check + // doesn't seem to work in Windows 10 anymore and "\r" is present even + // in truncated strings. Also we increased MAXPASSWORD, so it is larger + // than MAXPASSWORD_RAR. Thus we removed this check as not working + // and not that necessary. Low level FlushConsoleInputBuffer doesn't help // for high level ReadConsole, which in line input mode seems to store // the rest of string in its own internal buffer. - if (wcschr(Str,'\r')==NULL) // If '\r' is missing, the password was truncated. - while (true) - { - wchar Trail[64]; - DWORD TrailRead=0; - // Use ASIZE(Trail)-1 to reserve the space for trailing 0. - ReadConsole(hConIn,Trail,ASIZE(Trail)-1,&TrailRead,NULL); - Trail[TrailRead]=0; - if (TrailRead==0 || wcschr(Trail,'\r')!=NULL) - break; - } - #else - char StrA[MAXPASSWORD*4]; // "*4" for multibyte UTF-8 characters. -#if defined(_EMX) || defined (__VMS) - fgets(StrA,ASIZE(StrA)-1,stdin); + std::vector StrA(MAXPASSWORD*4); // "*4" for multibyte UTF-8 characters. +#ifdef __VMS + fgets(StrA.data(),StrA.size()-1,stdin); #elif defined(__sun) - strncpyz(StrA,getpassphrase(""),ASIZE(StrA)); + strncpyz(StrA.data(),getpassphrase(""),StrA.size()); #else - strncpyz(StrA,getpass(""),ASIZE(StrA)); + strncpyz(StrA.data(),getpass(""),StrA.size()); #endif - CharToWide(StrA,Str,MaxLength); - cleandata(StrA,sizeof(StrA)); + CharToWide(StrA.data(),Str); + cleandata(StrA.data(),StrA.size()*sizeof(StrA[0])); #endif } - Str[MaxLength-1]=0; RemoveLF(Str); } #endif #ifndef SILENT -bool GetConsolePassword(UIPASSWORD_TYPE Type,const wchar *FileName,SecPassword *Password) +bool GetConsolePassword(UIPASSWORD_TYPE Type,const std::wstring &FileName,SecPassword *Password) { if (!StdinRedirected) uiAlarm(UIALARM_QUESTION); @@ -229,33 +220,33 @@ bool GetConsolePassword(UIPASSWORD_TYPE Type,const wchar *FileName,SecPassword * if (Type==UIPASSWORD_GLOBAL) eprintf(L"\n%s: ",St(MAskPsw)); else - eprintf(St(MAskPswFor),FileName); + eprintf(St(MAskPswFor),FileName.c_str()); - wchar PlainPsw[MAXPASSWORD+1]; - GetPasswordText(PlainPsw,ASIZE(PlainPsw)); - if (*PlainPsw==0 && Type==UIPASSWORD_GLOBAL) + std::wstring PlainPsw; + GetPasswordText(PlainPsw); + if (PlainPsw.empty() && Type==UIPASSWORD_GLOBAL) return false; - if (wcslen(PlainPsw)>=MAXPASSWORD) + if (PlainPsw.size()>=MAXPASSWORD) { - PlainPsw[MAXPASSWORD-1]=0; + PlainPsw.erase(MAXPASSWORD-1); uiMsg(UIERROR_TRUNCPSW,MAXPASSWORD-1); } if (!StdinRedirected && Type==UIPASSWORD_GLOBAL) { eprintf(St(MReAskPsw)); - wchar CmpStr[MAXPASSWORD]; - GetPasswordText(CmpStr,ASIZE(CmpStr)); - if (*CmpStr==0 || wcscmp(PlainPsw,CmpStr)!=0) + std::wstring CmpStr; + GetPasswordText(CmpStr); + if (CmpStr.empty() || PlainPsw!=CmpStr) { eprintf(St(MNotMatchPsw)); - cleandata(PlainPsw,sizeof(PlainPsw)); - cleandata(CmpStr,sizeof(CmpStr)); + cleandata(&PlainPsw[0],PlainPsw.size()*sizeof(PlainPsw[0])); + cleandata(&CmpStr[0],CmpStr.size()*sizeof(CmpStr[0])); continue; } - cleandata(CmpStr,sizeof(CmpStr)); + cleandata(&CmpStr[0],CmpStr.size()*sizeof(CmpStr[0])); } - Password->Set(PlainPsw); - cleandata(PlainPsw,sizeof(PlainPsw)); + Password->Set(PlainPsw.c_str()); + cleandata(&PlainPsw[0],PlainPsw.size()*sizeof(PlainPsw[0])); break; } return true; @@ -264,14 +255,17 @@ bool GetConsolePassword(UIPASSWORD_TYPE Type,const wchar *FileName,SecPassword * #ifndef SILENT -bool getwstr(wchar *str,size_t n) +bool getwstr(std::wstring &str) { // Print buffered prompt title function before waiting for input. fflush(stderr); QuitIfInputProhibited(); - *str=0; + str.clear(); + + const size_t MaxRead=MAXPATHSIZE; // Large enough to read a file name. + #if defined(_WIN_ALL) // fgetws does not work well with non-English text in Windows, // so we do not use it. @@ -279,11 +273,11 @@ bool getwstr(wchar *str,size_t n) { // fgets does not work well with pipes in Windows in our test. // Let's use files. - Array StrA(n*4); // Up to 4 UTF-8 characters per wchar_t. + std::vector StrA(MaxRead*4); // Up to 4 UTF-8 characters per wchar_t. File SrcFile; SrcFile.SetHandleType(FILE_HANDLESTD); SrcFile.SetLineInputMode(true); - int ReadSize=SrcFile.Read(&StrA[0],StrA.Size()-1); + int ReadSize=SrcFile.Read(&StrA[0],StrA.size()-1); if (ReadSize<=0) { // Looks like stdin is a null device. We can enter to infinite loop @@ -297,19 +291,23 @@ bool getwstr(wchar *str,size_t n) // use "chcp" in console. But we avoid OEM to ANSI conversion, // because we also want to handle ANSI files redirection correctly, // like "rar ... < ansifile.txt". - CharToWide(&StrA[0],str,n); - cleandata(&StrA[0],StrA.Size()); // We can use this function to enter passwords. + CharToWide(&StrA[0],str); + cleandata(&StrA[0],StrA.size()); // We can use this function to enter passwords. } else { + std::vector Buf(MaxRead); // Up to 4 UTF-8 characters per wchar_t. DWORD ReadSize=0; - if (ReadConsole(GetStdHandle(STD_INPUT_HANDLE),str,DWORD(n-1),&ReadSize,NULL)==0) + if (ReadConsole(GetStdHandle(STD_INPUT_HANDLE),&Buf[0],(DWORD)Buf.size()-1,&ReadSize,NULL)==0) return false; - str[ReadSize]=0; + Buf[ReadSize]=0; + str=Buf.data(); } #else - if (fgetws(str,n,stdin)==NULL) + std::vector Buf(MaxRead); // Up to 4 UTF-8 characters per wchar_t. + if (fgetws(&Buf[0],Buf.size(),stdin)==NULL) ErrHandler.Exit(RARX_USERBREAK); // Avoid infinite Ask() loop. + str=Buf.data(); #endif RemoveLF(str); return true; @@ -361,8 +359,8 @@ int Ask(const wchar *AskStr) eprintf(L"[%c]%ls",Item[I][KeyPos],&Item[I][KeyPos+1]); } eprintf(L" "); - wchar Str[50]; - getwstr(Str,ASIZE(Str)); + std::wstring Str; + getwstr(Str); wchar Ch=toupperw(Str[0]); for (int I=0;I[{key};"{string}"p used to redefine // a keyboard key on some terminals. @@ -389,18 +387,16 @@ static bool IsCommentUnsafe(const wchar *Data,size_t Size) } -void OutComment(const wchar *Comment,size_t Size) +void OutComment(const std::wstring &Comment) { - if (IsCommentUnsafe(Comment,Size)) + if (IsCommentUnsafe(Comment)) return; const size_t MaxOutSize=0x400; - for (size_t I=0;I>1)^0xEDB88320 : (C>>1); CRCTab[I]=C; } + +#ifdef USE_NEON_CRC32 + #ifdef _APPLE + // getauxval isn't available in OS X + uint Value=0; + size_t Size=sizeof(Value); + int RetCode=sysctlbyname("hw.optional.armv8_crc32",&Value,&Size,NULL,0); + CRC_Neon=RetCode==0 && Value!=0; + #else + CRC_Neon=(getauxval(AT_HWCAP) & HWCAP_CRC32)!=0; + #endif +#endif + } @@ -47,7 +64,7 @@ static void InitTables() for (uint I=0;I<256;I++) // Build additional lookup tables. { uint C=crc_tables[0][I]; - for (uint J=1;J<8;J++) + for (uint J=1;J<16;J++) { C=crc_tables[0][(byte)C]^(C>>8); crc_tables[J][I]=C; @@ -63,28 +80,66 @@ uint CRC32(uint StartCRC,const void *Addr,size_t Size) { byte *Data=(byte *)Addr; +#ifdef USE_NEON_CRC32 + if (CRC_Neon) + { + for (;Size>=8;Size-=8,Data+=8) +#ifdef __clang__ + StartCRC = __builtin_arm_crc32d(StartCRC, RawGet8(Data)); +#else + StartCRC = __builtin_aarch64_crc32x(StartCRC, RawGet8(Data)); +#endif + for (;Size>0;Size--,Data++) // Process left data. +#ifdef __clang__ + StartCRC = __builtin_arm_crc32b(StartCRC, *Data); +#else + StartCRC = __builtin_aarch64_crc32b(StartCRC, *Data); +#endif + return StartCRC; + } +#endif + #ifdef USE_SLICING - // Align Data to 8 for better performance. - for (;Size>0 && ((size_t)Data & 7);Size--,Data++) + // Align Data to 16 for better performance and to avoid ALLOW_MISALIGNED + // check below. + for (;Size>0 && ((size_t)Data & 15)!=0;Size--,Data++) StartCRC=crc_tables[0][(byte)(StartCRC^Data[0])]^(StartCRC>>8); - for (;Size>=8;Size-=8,Data+=8) + // 2023.12.06: We switched to slicing-by-16, which seems to be faster than + // slicing-by-8 on modern CPUs. Slicing-by-32 would require 32 KB for tables + // and could be limited by L1 cache size on some CPUs. + for (;Size>=16;Size-=16,Data+=16) { #ifdef BIG_ENDIAN - StartCRC ^= Data[0]|(Data[1] << 8)|(Data[2] << 16)|(Data[3] << 24); - uint NextData = Data[4]|(Data[5] << 8)|(Data[6] << 16)|(Data[7] << 24); + StartCRC ^= RawGet4(Data); + uint D1 = RawGet4(Data+4); + uint D2 = RawGet4(Data+8); + uint D3 = RawGet4(Data+12); #else + // We avoid RawGet4 here for performance reason, to access uint32 + // directly even if ALLOW_MISALIGNED isn't defined. We can do it, + // because we aligned 'Data' above. StartCRC ^= *(uint32 *) Data; - uint NextData = *(uint32 *) (Data+4); + uint D1 = *(uint32 *) (Data+4); + uint D2 = *(uint32 *) (Data+8); + uint D3 = *(uint32 *) (Data+12); #endif - StartCRC = crc_tables[7][(byte) StartCRC ] ^ - crc_tables[6][(byte)(StartCRC >> 8) ] ^ - crc_tables[5][(byte)(StartCRC >> 16)] ^ - crc_tables[4][(byte)(StartCRC >> 24)] ^ - crc_tables[3][(byte) NextData ] ^ - crc_tables[2][(byte)(NextData >> 8) ] ^ - crc_tables[1][(byte)(NextData >> 16)] ^ - crc_tables[0][(byte)(NextData >> 24)]; + StartCRC = crc_tables[15][(byte) StartCRC ] ^ + crc_tables[14][(byte)(StartCRC >> 8) ] ^ + crc_tables[13][(byte)(StartCRC >> 16)] ^ + crc_tables[12][(byte)(StartCRC >> 24)] ^ + crc_tables[11][(byte) D1 ] ^ + crc_tables[10][(byte)(D1 >> 8) ] ^ + crc_tables[ 9][(byte)(D1 >> 16)] ^ + crc_tables[ 8][(byte)(D1 >> 24)] ^ + crc_tables[ 7][(byte) D2 ] ^ + crc_tables[ 6][(byte)(D2 >> 8)] ^ + crc_tables[ 5][(byte)(D2 >> 16)] ^ + crc_tables[ 4][(byte)(D2 >> 24)] ^ + crc_tables[ 3][(byte) D3 ] ^ + crc_tables[ 2][(byte)(D3 >> 8)] ^ + crc_tables[ 1][(byte)(D3 >> 16)] ^ + crc_tables[ 0][(byte)(D3 >> 24)]; } #endif @@ -110,74 +165,6 @@ ushort Checksum14(ushort StartCRC,const void *Addr,size_t Size) #endif -#if 0 -static uint64 crc64_tables[8][256]; // Tables for Slicing-by-8 for CRC64. - -void InitCRC64(uint64 *CRCTab) -{ - const uint64 poly=INT32TO64(0xC96C5795, 0xD7870F42); // 0xC96C5795D7870F42; - for (uint I=0;I<256;I++) - { - uint64 C=I; - for (uint J=0;J<8;J++) - C=(C & 1) ? (C>>1)^poly: (C>>1); - CRCTab[I]=C; - } -} - - -static void InitTables64() -{ - InitCRC64(crc64_tables[0]); - - for (uint I=0;I<256;I++) // Build additional lookup tables. - { - uint64 C=crc64_tables[0][I]; - for (uint J=1;J<8;J++) - { - C=crc64_tables[0][(byte)C]^(C>>8); - crc64_tables[J][I]=C; - } - } -} - - -// We cannot place the intialization to CRC64(), because we use this function -// in multithreaded mode and it conflicts with multithreading. -struct CallInitCRC64 {CallInitCRC64() {InitTables64();}} static CallInit64; - -uint64 CRC64(uint64 StartCRC,const void *Addr,size_t Size) -{ - byte *Data=(byte *)Addr; - - // Align Data to 8 for better performance. - for (;Size>0 && ((size_t)Data & 7)!=0;Size--,Data++) - StartCRC=crc64_tables[0][(byte)(StartCRC^Data[0])]^(StartCRC>>8); - - for (byte *DataEnd=Data+Size/8*8; Data> 8 ) ] ^ - crc64_tables[ 5 ] [ ( byte ) (Index >> 16 ) ] ^ - crc64_tables[ 4 ] [ ( byte ) (Index >> 24 ) ] ^ - crc64_tables[ 3 ] [ ( byte ) (Index >> 32 ) ] ^ - crc64_tables[ 2 ] [ ( byte ) (Index >> 40 ) ] ^ - crc64_tables[ 1 ] [ ( byte ) (Index >> 48 ) ] ^ - crc64_tables[ 0 ] [ ( byte ) (Index >> 56 ) ] ; - } - - for (Size%=8;Size>0;Size--,Data++) // Process left data. - StartCRC=crc64_tables[0][(byte)(StartCRC^Data[0])]^(StartCRC>>8); - - return StartCRC; -} #if 0 @@ -186,6 +173,11 @@ struct TestCRCStruct {TestCRCStruct() {TestCRC();exit(0);}} GlobalTesCRC; void TestCRC() { + // This function is invoked from global object and _SSE_Version is global + // and can be initialized after this function. So we explicitly initialize + // it here to enable SSE support in Blake2sp. + _SSE_Version=GetSSEVersion(); + const uint FirstSize=300; byte b[FirstSize]; @@ -251,23 +243,38 @@ void TestCRC() const size_t BufSize=0x100000; byte *Buf=new byte[BufSize]; - memset(Buf,0,BufSize); + GetRnd(Buf,BufSize); clock_t StartTime=clock(); r32=0xffffffff; - const uint BufCount=5000; + const uint64 BufCount=5000; for (uint I=0;I>16); + Key15[3]+=ushort(P+(CRCTab[P]>>16)); } } -void CryptData::SetAV15Encryption() -{ - InitCRC32(CRCTab); - Method=CRYPT_RAR15; - Key15[0]=0x4765; - Key15[1]=0x9021; - Key15[2]=0x7382; - Key15[3]=0x5215; -} - - void CryptData::SetCmt13Encryption() { Method=CRYPT_RAR13; @@ -68,7 +55,7 @@ void CryptData::Crypt15(byte *Data,size_t Count) { Key15[0]+=0x1234; Key15[1]^=CRCTab[(Key15[0] & 0x1fe)>>1]; - Key15[2]-=CRCTab[(Key15[0] & 0x1fe)>>1]>>16; + Key15[2]-=ushort(CRCTab[(Key15[0] & 0x1fe)>>1]>>16); Key15[0]^=Key15[2]; Key15[3]=rotrs(Key15[3]&0xffff,1,16)^Key15[1]; Key15[3]=rotrs(Key15[3]&0xffff,1,16); diff --git a/unrar_sys/vendor/unrar/crypt5.cpp b/unrar_sys/vendor/unrar/crypt5.cpp index bb9b2ba..f0f5e72 100644 --- a/unrar_sys/vendor/unrar/crypt5.cpp +++ b/unrar_sys/vendor/unrar/crypt5.cpp @@ -208,6 +208,7 @@ void ConvertHashToMAC(HashValue *Value,byte *Key) Value->CRC32=0; for (uint I=0;ICRC32^=Digest[I] << ((I & 3) * 8); + Value->CRC32&=0xffffffff; // In case the variable size is larger than 32-bit. } if (Value->Type==HASH_BLAKE2) { diff --git a/unrar_sys/vendor/unrar/dll.cpp b/unrar_sys/vendor/unrar/dll.cpp index 77a58e0..d82bcd3 100644 --- a/unrar_sys/vendor/unrar/dll.cpp +++ b/unrar_sys/vendor/unrar/dll.cpp @@ -44,22 +44,21 @@ HANDLE PASCAL RAROpenArchiveEx(struct RAROpenArchiveDataEx *r) Data->Cmd.FileArgs.AddString(L"*"); Data->Cmd.KeepBroken=(r->OpFlags&ROADOF_KEEPBROKEN)!=0; - char AnsiArcName[NM]; - *AnsiArcName=0; - if (r->ArcName!=NULL) + std::string AnsiArcName; + if (r->ArcName!=nullptr) { - strncpyz(AnsiArcName,r->ArcName,ASIZE(AnsiArcName)); + AnsiArcName=r->ArcName; #ifdef _WIN_ALL if (!AreFileApisANSI()) - { - OemToCharBuffA(r->ArcName,AnsiArcName,ASIZE(AnsiArcName)); - AnsiArcName[ASIZE(AnsiArcName)-1]=0; - } + IntToExt(r->ArcName,AnsiArcName); #endif } - wchar ArcName[NM]; - GetWideName(AnsiArcName,r->ArcNameW,ArcName,ASIZE(ArcName)); + std::wstring ArcName; + if (r->ArcNameW!=nullptr && *r->ArcNameW!=0) + ArcName=r->ArcNameW; + else + CharToWide(AnsiArcName,ArcName); Data->Cmd.AddArcName(ArcName); Data->Cmd.Overwrite=OVERWRITE_ALL; @@ -112,35 +111,35 @@ HANDLE PASCAL RAROpenArchiveEx(struct RAROpenArchiveDataEx *r) if (Data->Arc.FirstVolume) r->Flags|=ROADF_FIRSTVOLUME; - Array CmtDataW; - if (r->CmtBufSize!=0 && Data->Arc.GetComment(&CmtDataW)) + std::wstring CmtDataW; + if (r->CmtBufSize!=0 && Data->Arc.GetComment(CmtDataW)) { if (r->CmtBufW!=NULL) { - CmtDataW.Push(0); - size_t Size=wcslen(&CmtDataW[0])+1; +// CmtDataW.push_back(0); + size_t Size=wcslen(CmtDataW.data())+1; r->CmtState=Size>r->CmtBufSize ? ERAR_SMALL_BUF:1; r->CmtSize=(uint)Min(Size,r->CmtBufSize); - memcpy(r->CmtBufW,&CmtDataW[0],(r->CmtSize-1)*sizeof(*r->CmtBufW)); + memcpy(r->CmtBufW,CmtDataW.data(),(r->CmtSize-1)*sizeof(*r->CmtBufW)); r->CmtBufW[r->CmtSize-1]=0; } else if (r->CmtBuf!=NULL) { - Array CmtData(CmtDataW.Size()*4+1); - memset(&CmtData[0],0,CmtData.Size()); - WideToChar(&CmtDataW[0],&CmtData[0],CmtData.Size()-1); - size_t Size=strlen(&CmtData[0])+1; + std::vector CmtData(CmtDataW.size()*4+1); + WideToChar(&CmtDataW[0],&CmtData[0],CmtData.size()-1); + size_t Size=strlen(CmtData.data())+1; r->CmtState=Size>r->CmtBufSize ? ERAR_SMALL_BUF:1; r->CmtSize=(uint)Min(Size,r->CmtBufSize); - memcpy(r->CmtBuf,&CmtData[0],r->CmtSize-1); + memcpy(r->CmtBuf,CmtData.data(),r->CmtSize-1); r->CmtBuf[r->CmtSize-1]=0; } } else r->CmtState=r->CmtSize=0; + Data->Extract.ExtractArchiveInit(Data->Arc); return (HANDLE)Data; } @@ -182,8 +181,7 @@ int PASCAL RARCloseArchive(HANDLE hArcData) int PASCAL RARReadHeader(HANDLE hArcData,struct RARHeaderData *D) { - struct RARHeaderDataEx X; - memset(&X,0,sizeof(X)); + struct RARHeaderDataEx X{}; int Code=RARReadHeaderEx(hArcData,&X); @@ -241,14 +239,18 @@ int PASCAL RARReadHeaderEx(HANDLE hArcData,struct RARHeaderDataEx *D) else return Code; } - wcsncpyz(D->ArcNameW,Data->Arc.FileName,ASIZE(D->ArcNameW)); + wcsncpyz(D->ArcNameW,Data->Arc.FileName.c_str(),ASIZE(D->ArcNameW)); WideToChar(D->ArcNameW,D->ArcName,ASIZE(D->ArcName)); + if (D->ArcNameEx!=nullptr) + wcsncpyz(D->ArcNameEx,Data->Arc.FileName.c_str(),D->ArcNameExSize); - wcsncpyz(D->FileNameW,hd->FileName,ASIZE(D->FileNameW)); + wcsncpyz(D->FileNameW,hd->FileName.c_str(),ASIZE(D->FileNameW)); WideToChar(D->FileNameW,D->FileName,ASIZE(D->FileName)); #ifdef _WIN_ALL CharToOemA(D->FileName,D->FileName); #endif + if (D->FileNameEx!=nullptr) + wcsncpyz(D->FileNameEx,hd->FileName.c_str(),D->FileNameExSize); D->Flags=0; if (hd->SplitBefore) @@ -310,7 +312,7 @@ int PASCAL RARReadHeaderEx(HANDLE hArcData,struct RARHeaderDataEx *D) // this RedirNameSize check sometimes later. if (hd->RedirType!=FSREDIR_NONE && D->RedirName!=NULL && D->RedirNameSize>0 && D->RedirNameSize<100000) - wcsncpyz(D->RedirName,hd->RedirName,D->RedirNameSize); + wcsncpyz(D->RedirName,hd->RedirName.c_str(),D->RedirNameSize); D->DirTarget=hd->DirTarget; } catch (RAR_EXIT ErrCode) @@ -345,50 +347,52 @@ int PASCAL ProcessFile(HANDLE hArcData,int Operation,char *DestPath,char *DestNa { Data->Cmd.DllOpMode=Operation; - *Data->Cmd.ExtrPath=0; - *Data->Cmd.DllDestName=0; + Data->Cmd.ExtrPath.clear(); + Data->Cmd.DllDestName.clear(); if (DestPath!=NULL) { - char ExtrPathA[NM]; - strncpyz(ExtrPathA,DestPath,ASIZE(ExtrPathA)-2); + std::string ExtrPathA=DestPath; #ifdef _WIN_ALL // We must not apply OemToCharBuffA directly to DestPath, // because we do not know DestPath length and OemToCharBuffA // does not stop at 0. - OemToCharA(ExtrPathA,ExtrPathA); + IntToExt(ExtrPathA,ExtrPathA); #endif - CharToWide(ExtrPathA,Data->Cmd.ExtrPath,ASIZE(Data->Cmd.ExtrPath)); - AddEndSlash(Data->Cmd.ExtrPath,ASIZE(Data->Cmd.ExtrPath)); + CharToWide(ExtrPathA,Data->Cmd.ExtrPath); + AddEndSlash(Data->Cmd.ExtrPath); } if (DestName!=NULL) { - char DestNameA[NM]; - strncpyz(DestNameA,DestName,ASIZE(DestNameA)-2); + std::string DestNameA=DestName; #ifdef _WIN_ALL // We must not apply OemToCharBuffA directly to DestName, // because we do not know DestName length and OemToCharBuffA // does not stop at 0. - OemToCharA(DestNameA,DestNameA); + IntToExt(DestNameA,DestNameA); #endif - CharToWide(DestNameA,Data->Cmd.DllDestName,ASIZE(Data->Cmd.DllDestName)); + CharToWide(DestNameA,Data->Cmd.DllDestName); } if (DestPathW!=NULL) { - wcsncpyz(Data->Cmd.ExtrPath,DestPathW,ASIZE(Data->Cmd.ExtrPath)); - AddEndSlash(Data->Cmd.ExtrPath,ASIZE(Data->Cmd.ExtrPath)); + Data->Cmd.ExtrPath=DestPathW; + AddEndSlash(Data->Cmd.ExtrPath); } if (DestNameW!=NULL) - wcsncpyz(Data->Cmd.DllDestName,DestNameW,ASIZE(Data->Cmd.DllDestName)); + Data->Cmd.DllDestName=DestNameW; - wcsncpyz(Data->Cmd.Command,Operation==RAR_EXTRACT ? L"X":L"T",ASIZE(Data->Cmd.Command)); + Data->Cmd.Command=Operation==RAR_EXTRACT ? L"X":L"T"; Data->Cmd.Test=Operation!=RAR_EXTRACT; bool Repeat=false; Data->Extract.ExtractCurrentFile(Data->Arc,Data->HeaderSize,Repeat); // Now we process extra file information if any. + // It is important to do it in the same ProcessFile(), because caller + // app can rely on this behavior, for example, to overwrite + // the extracted Mark of the Web with propagated from archive + // immediately after ProcessFile() call. // // Archive can be closed if we process volumes, next volume is missing // and current one is already removed or deleted. So we need to check @@ -455,7 +459,7 @@ void PASCAL RARSetPassword(HANDLE hArcData,char *Password) #ifndef RAR_NOCRYPT DataSet *Data=(DataSet *)hArcData; wchar PasswordW[MAXPASSWORD]; - GetWideName(Password,NULL,PasswordW,ASIZE(PasswordW)); + CharToWide(Password,PasswordW,ASIZE(PasswordW)); Data->Cmd.Password.Set(PasswordW); cleandata(PasswordW,sizeof(PasswordW)); #endif diff --git a/unrar_sys/vendor/unrar/dll.hpp b/unrar_sys/vendor/unrar/dll.hpp index c785ff1..ac8d53f 100644 --- a/unrar_sys/vendor/unrar/dll.hpp +++ b/unrar_sys/vendor/unrar/dll.hpp @@ -19,6 +19,7 @@ #define ERAR_MISSING_PASSWORD 22 #define ERAR_EREFERENCE 23 #define ERAR_BAD_PASSWORD 24 +#define ERAR_LARGE_DICT 25 #define RAR_OM_LIST 0 #define RAR_OM_EXTRACT 1 @@ -31,7 +32,7 @@ #define RAR_VOL_ASK 0 #define RAR_VOL_NOTIFY 1 -#define RAR_DLL_VERSION 8 +#define RAR_DLL_VERSION 9 #define RAR_HASH_NONE 0 #define RAR_HASH_CRC32 1 @@ -108,7 +109,11 @@ struct RARHeaderDataEx unsigned int CtimeHigh; unsigned int AtimeLow; unsigned int AtimeHigh; - unsigned int Reserved[988]; + wchar_t *ArcNameEx; + unsigned int ArcNameExSize; + wchar_t *FileNameEx; + unsigned int FileNameExSize; + unsigned int Reserved[982]; }; @@ -157,7 +162,7 @@ struct RAROpenArchiveDataEx enum UNRARCALLBACK_MESSAGES { UCM_CHANGEVOLUME,UCM_PROCESSDATA,UCM_NEEDPASSWORD,UCM_CHANGEVOLUMEW, - UCM_NEEDPASSWORDW + UCM_NEEDPASSWORDW,UCM_LARGEDICT }; typedef int (PASCAL *CHANGEVOLPROC)(char *ArcName,int Mode); diff --git a/unrar_sys/vendor/unrar/dll.rc b/unrar_sys/vendor/unrar/dll.rc index b7f0778..3cf8bbb 100644 --- a/unrar_sys/vendor/unrar/dll.rc +++ b/unrar_sys/vendor/unrar/dll.rc @@ -2,8 +2,8 @@ #include VS_VERSION_INFO VERSIONINFO -FILEVERSION 6, 24, 100, 1007 -PRODUCTVERSION 6, 24, 100, 1007 +FILEVERSION 7, 0, 100, 1154 +PRODUCTVERSION 7, 0, 100, 1154 FILEOS VOS__WINDOWS32 FILETYPE VFT_APP { @@ -14,9 +14,9 @@ FILETYPE VFT_APP VALUE "CompanyName", "Alexander Roshal\0" VALUE "ProductName", "RAR decompression library\0" VALUE "FileDescription", "RAR decompression library\0" - VALUE "FileVersion", "6.24.0\0" - VALUE "ProductVersion", "6.24.0\0" - VALUE "LegalCopyright", "Copyright © Alexander Roshal 1993-2023\0" + VALUE "FileVersion", "7.0.0\0" + VALUE "ProductVersion", "7.0.0\0" + VALUE "LegalCopyright", "Copyright © Alexander Roshal 1993-2024\0" VALUE "OriginalFilename", "Unrar.dll\0" } } diff --git a/unrar_sys/vendor/unrar/encname.cpp b/unrar_sys/vendor/unrar/encname.cpp index e1ba1ed..d602090 100644 --- a/unrar_sys/vendor/unrar/encname.cpp +++ b/unrar_sys/vendor/unrar/encname.cpp @@ -11,12 +11,13 @@ EncodeFileName::EncodeFileName() -void EncodeFileName::Decode(char *Name,size_t NameSize,byte *EncName,size_t EncSize, - wchar *NameW,size_t MaxDecSize) +void EncodeFileName::Decode(const char *Name,size_t NameSize, + const byte *EncName,size_t EncSize, + std::wstring &NameW) { size_t EncPos=0,DecPos=0; byte HighByte=EncPos=EncSize) break; + // We need DecPos also for ASCII "Name", so resize() instead of push_back(). + NameW.resize(DecPos+1); NameW[DecPos++]=EncName[EncPos++]; break; case 1: if (EncPos>=EncSize) break; + NameW.resize(DecPos+1); NameW[DecPos++]=EncName[EncPos++]+(HighByte<<8); break; case 2: if (EncPos+1>=EncSize) break; + NameW.resize(DecPos+1); NameW[DecPos++]=EncName[EncPos]+(EncName[EncPos+1]<<8); EncPos+=2; break; @@ -51,17 +56,22 @@ void EncodeFileName::Decode(char *Name,size_t NameSize,byte *EncName,size_t EncS if (EncPos>=EncSize) break; byte Correction=EncName[EncPos++]; - for (Length=(Length&0x7f)+2;Length>0 && DecPos0 && DecPos0 && DecPos0 && DecPos &EncName); byte Flags; uint FlagBits; @@ -12,8 +12,8 @@ class EncodeFileName size_t DestSize; public: EncodeFileName(); - size_t Encode(char *Name,wchar *NameW,byte *EncName); - void Decode(char *Name,size_t NameSize,byte *EncName,size_t EncSize,wchar *NameW,size_t MaxDecSize); + void Encode(const std::string &Name,const std::wstring &NameW,std::vector &EncName); + void Decode(const char *Name,size_t NameSize,const byte *EncName,size_t EncSize,std::wstring &NameW); }; #endif diff --git a/unrar_sys/vendor/unrar/errhnd.cpp b/unrar_sys/vendor/unrar/errhnd.cpp index 97193e5..b07e2f6 100644 --- a/unrar_sys/vendor/unrar/errhnd.cpp +++ b/unrar_sys/vendor/unrar/errhnd.cpp @@ -26,7 +26,7 @@ void ErrorHandler::MemoryError() } -void ErrorHandler::OpenError(const wchar *FileName) +void ErrorHandler::OpenError(const std::wstring &FileName) { #ifndef SILENT OpenErrorMsg(FileName); @@ -35,7 +35,7 @@ void ErrorHandler::OpenError(const wchar *FileName) } -void ErrorHandler::CloseError(const wchar *FileName) +void ErrorHandler::CloseError(const std::wstring &FileName) { if (!UserBreak) { @@ -51,7 +51,7 @@ void ErrorHandler::CloseError(const wchar *FileName) } -void ErrorHandler::ReadError(const wchar *FileName) +void ErrorHandler::ReadError(const std::wstring &FileName) { #ifndef SILENT ReadErrorMsg(FileName); @@ -62,13 +62,13 @@ void ErrorHandler::ReadError(const wchar *FileName) } -void ErrorHandler::AskRepeatRead(const wchar *FileName,bool &Ignore,bool &Retry,bool &Quit) +void ErrorHandler::AskRepeatRead(const std::wstring &FileName,bool &Ignore,bool &Retry,bool &Quit) { SetErrorCode(RARX_READ); #if !defined(SILENT) && !defined(SFX_MODULE) if (!Silent) { - uiMsg(UIERROR_FILEREAD,UINULL,FileName); + uiMsg(UIERROR_FILEREAD,L"",FileName); SysErrMsg(); if (ReadErrIgnoreAll) Ignore=true; @@ -88,7 +88,7 @@ void ErrorHandler::AskRepeatRead(const wchar *FileName,bool &Ignore,bool &Retry, } -void ErrorHandler::WriteError(const wchar *ArcName,const wchar *FileName) +void ErrorHandler::WriteError(const std::wstring &ArcName,const std::wstring &FileName) { #ifndef SILENT WriteErrorMsg(ArcName,FileName); @@ -100,7 +100,7 @@ void ErrorHandler::WriteError(const wchar *ArcName,const wchar *FileName) #ifdef _WIN_ALL -void ErrorHandler::WriteErrorFAT(const wchar *FileName) +void ErrorHandler::WriteErrorFAT(const std::wstring &FileName) { SysErrMsg(); uiMsg(UIERROR_NTFSREQUIRED,FileName); @@ -111,7 +111,7 @@ void ErrorHandler::WriteErrorFAT(const wchar *FileName) #endif -bool ErrorHandler::AskRepeatWrite(const wchar *FileName,bool DiskFull) +bool ErrorHandler::AskRepeatWrite(const std::wstring &FileName,bool DiskFull) { #ifndef SILENT if (!Silent) @@ -129,7 +129,7 @@ bool ErrorHandler::AskRepeatWrite(const wchar *FileName,bool DiskFull) } -void ErrorHandler::SeekError(const wchar *FileName) +void ErrorHandler::SeekError(const std::wstring &FileName) { if (!UserBreak) { @@ -144,13 +144,16 @@ void ErrorHandler::SeekError(const wchar *FileName) void ErrorHandler::GeneralErrMsg(const wchar *fmt,...) { +#ifndef RARDLL va_list arglist; va_start(arglist,fmt); - wchar Msg[1024]; - vswprintf(Msg,ASIZE(Msg),fmt,arglist); + + std::wstring Msg=vwstrprintf(fmt,arglist); uiMsg(UIERROR_GENERALERRMSG,Msg); SysErrMsg(); + va_end(arglist); +#endif } @@ -161,13 +164,13 @@ void ErrorHandler::MemoryErrorMsg() } -void ErrorHandler::OpenErrorMsg(const wchar *FileName) +void ErrorHandler::OpenErrorMsg(const std::wstring &FileName) { - OpenErrorMsg(NULL,FileName); + OpenErrorMsg(L"",FileName); } -void ErrorHandler::OpenErrorMsg(const wchar *ArcName,const wchar *FileName) +void ErrorHandler::OpenErrorMsg(const std::wstring &ArcName,const std::wstring &FileName) { uiMsg(UIERROR_FILEOPEN,ArcName,FileName); SysErrMsg(); @@ -179,13 +182,13 @@ void ErrorHandler::OpenErrorMsg(const wchar *ArcName,const wchar *FileName) } -void ErrorHandler::CreateErrorMsg(const wchar *FileName) +void ErrorHandler::CreateErrorMsg(const std::wstring &FileName) { - CreateErrorMsg(NULL,FileName); + CreateErrorMsg(L"",FileName); } -void ErrorHandler::CreateErrorMsg(const wchar *ArcName,const wchar *FileName) +void ErrorHandler::CreateErrorMsg(const std::wstring &ArcName,const std::wstring &FileName) { uiMsg(UIERROR_FILECREATE,ArcName,FileName); SysErrMsg(); @@ -193,13 +196,13 @@ void ErrorHandler::CreateErrorMsg(const wchar *ArcName,const wchar *FileName) } -void ErrorHandler::ReadErrorMsg(const wchar *FileName) +void ErrorHandler::ReadErrorMsg(const std::wstring &FileName) { - ReadErrorMsg(NULL,FileName); + ReadErrorMsg(L"",FileName); } -void ErrorHandler::ReadErrorMsg(const wchar *ArcName,const wchar *FileName) +void ErrorHandler::ReadErrorMsg(const std::wstring &ArcName,const std::wstring &FileName) { uiMsg(UIERROR_FILEREAD,ArcName,FileName); SysErrMsg(); @@ -207,7 +210,7 @@ void ErrorHandler::ReadErrorMsg(const wchar *ArcName,const wchar *FileName) } -void ErrorHandler::WriteErrorMsg(const wchar *ArcName,const wchar *FileName) +void ErrorHandler::WriteErrorMsg(const std::wstring &ArcName,const std::wstring &FileName) { uiMsg(UIERROR_FILEWRITE,ArcName,FileName); SysErrMsg(); @@ -215,21 +218,21 @@ void ErrorHandler::WriteErrorMsg(const wchar *ArcName,const wchar *FileName) } -void ErrorHandler::ArcBrokenMsg(const wchar *ArcName) +void ErrorHandler::ArcBrokenMsg(const std::wstring &ArcName) { uiMsg(UIERROR_ARCBROKEN,ArcName); SetErrorCode(RARX_CRC); } -void ErrorHandler::ChecksumFailedMsg(const wchar *ArcName,const wchar *FileName) +void ErrorHandler::ChecksumFailedMsg(const std::wstring &ArcName,const std::wstring &FileName) { uiMsg(UIERROR_CHECKSUM,ArcName,FileName); SetErrorCode(RARX_CRC); } -void ErrorHandler::UnknownMethodMsg(const wchar *ArcName,const wchar *FileName) +void ErrorHandler::UnknownMethodMsg(const std::wstring &ArcName,const std::wstring &FileName) { uiMsg(UIERROR_UNKNOWNMETHOD,ArcName,FileName); ErrHandler.SetErrorCode(RARX_FATAL); @@ -332,33 +335,44 @@ void ErrorHandler::Throw(RAR_EXIT Code) if (Code==RARX_USERBREAK && !EnableBreak) return; #if !defined(SILENT) - // Do not write "aborted" when just displaying online help. - if (Code!=RARX_SUCCESS && Code!=RARX_USERERROR) - mprintf(L"\n%s\n",St(MProgAborted)); + if (Code!=RARX_SUCCESS) + if (Code==RARX_USERERROR) // Do not write "aborted" when just displaying the online help. + mprintf(L"\n"); // For consistency with other errors, which print the final "\n". + else + mprintf(L"\n%s\n",St(MProgAborted)); #endif SetErrorCode(Code); throw Code; } -bool ErrorHandler::GetSysErrMsg(wchar *Msg,size_t Size) +bool ErrorHandler::GetSysErrMsg(std::wstring &Msg) { #ifndef SILENT #ifdef _WIN_ALL int ErrType=GetLastError(); if (ErrType!=0) - return FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS, - NULL,ErrType,MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT), - Msg,(DWORD)Size,NULL)!=0; + { + wchar *Buf=nullptr; + if (FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM| + FORMAT_MESSAGE_IGNORE_INSERTS|FORMAT_MESSAGE_ALLOCATE_BUFFER, + NULL,ErrType,MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT), + (LPTSTR)&Buf,0,NULL)!=0) + { + Msg=Buf; + LocalFree(Buf); + return true; + } + } #endif -#if defined(_UNIX) || defined(_EMX) +#ifdef _UNIX if (errno!=0) { char *err=strerror(errno); if (err!=NULL) { - CharToWide(err,Msg,Size); + CharToWide(err,Msg); return true; } } @@ -371,31 +385,26 @@ bool ErrorHandler::GetSysErrMsg(wchar *Msg,size_t Size) void ErrorHandler::SysErrMsg() { #ifndef SILENT - wchar Msg[1024]; - if (!GetSysErrMsg(Msg,ASIZE(Msg))) + std::wstring Msg; + if (!GetSysErrMsg(Msg)) return; #ifdef _WIN_ALL - wchar *CurMsg=Msg; - while (CurMsg!=NULL) // Print string with \r\n as several strings to multiple lines. + // Print string with \r\n as several strings to multiple lines. + size_t Pos=0; + while (Pos!=std::wstring::npos) { - while (*CurMsg=='\r' || *CurMsg=='\n') - CurMsg++; - if (*CurMsg==0) + while (Msg[Pos]=='\r' || Msg[Pos]=='\n') + Pos++; + if (Pos==Msg.size()) break; - wchar *EndMsg=wcschr(CurMsg,'\r'); - if (EndMsg==NULL) - EndMsg=wcschr(CurMsg,'\n'); - if (EndMsg!=NULL) - { - *EndMsg=0; - EndMsg++; - } + size_t EndPos=Msg.find_first_of(L"\r\n",Pos); + std::wstring CurMsg=Msg.substr(Pos,EndPos==std::wstring::npos ? EndPos:EndPos-Pos); uiMsg(UIERROR_SYSERRMSG,CurMsg); - CurMsg=EndMsg; + Pos=EndPos; } #endif -#if defined(_UNIX) || defined(_EMX) +#ifdef _UNIX uiMsg(UIERROR_SYSERRMSG,Msg); #endif diff --git a/unrar_sys/vendor/unrar/errhnd.hpp b/unrar_sys/vendor/unrar/errhnd.hpp index 06f4f61..3b558b1 100644 --- a/unrar_sys/vendor/unrar/errhnd.hpp +++ b/unrar_sys/vendor/unrar/errhnd.hpp @@ -33,26 +33,26 @@ class ErrorHandler ErrorHandler(); void Clean(); void MemoryError(); - void OpenError(const wchar *FileName); - void CloseError(const wchar *FileName); - void ReadError(const wchar *FileName); - void AskRepeatRead(const wchar *FileName,bool &Ignore,bool &Retry,bool &Quit); - void WriteError(const wchar *ArcName,const wchar *FileName); - void WriteErrorFAT(const wchar *FileName); - bool AskRepeatWrite(const wchar *FileName,bool DiskFull); - void SeekError(const wchar *FileName); + void OpenError(const std::wstring &FileName); + void CloseError(const std::wstring &FileName); + void ReadError(const std::wstring &FileName); + void AskRepeatRead(const std::wstring &FileName,bool &Ignore,bool &Retry,bool &Quit); + void WriteError(const std::wstring &ArcName,const std::wstring &FileName); + void WriteErrorFAT(const std::wstring &FileName); + bool AskRepeatWrite(const std::wstring &FileName,bool DiskFull); + void SeekError(const std::wstring &FileName); void GeneralErrMsg(const wchar *fmt,...); void MemoryErrorMsg(); - void OpenErrorMsg(const wchar *FileName); - void OpenErrorMsg(const wchar *ArcName,const wchar *FileName); - void CreateErrorMsg(const wchar *FileName); - void CreateErrorMsg(const wchar *ArcName,const wchar *FileName); - void ReadErrorMsg(const wchar *FileName); - void ReadErrorMsg(const wchar *ArcName,const wchar *FileName); - void WriteErrorMsg(const wchar *ArcName,const wchar *FileName); - void ArcBrokenMsg(const wchar *ArcName); - void ChecksumFailedMsg(const wchar *ArcName,const wchar *FileName); - void UnknownMethodMsg(const wchar *ArcName,const wchar *FileName); + void OpenErrorMsg(const std::wstring &FileName); + void OpenErrorMsg(const std::wstring &ArcName,const std::wstring &FileName); + void CreateErrorMsg(const std::wstring &FileName); + void CreateErrorMsg(const std::wstring &ArcName,const std::wstring &FileName); + void ReadErrorMsg(const std::wstring &FileName); + void ReadErrorMsg(const std::wstring &ArcName,const std::wstring &FileName); + void WriteErrorMsg(const std::wstring &ArcName,const std::wstring &FileName); + void ArcBrokenMsg(const std::wstring &ArcName); + void ChecksumFailedMsg(const std::wstring &ArcName,const std::wstring &FileName); + void UnknownMethodMsg(const std::wstring &ArcName,const std::wstring &FileName); void Exit(RAR_EXIT ExitCode); void SetErrorCode(RAR_EXIT Code); RAR_EXIT GetErrorCode() {return ExitCode;} @@ -60,7 +60,7 @@ class ErrorHandler void SetSignalHandlers(bool Enable); void Throw(RAR_EXIT Code); void SetSilent(bool Mode) {Silent=Mode;} - bool GetSysErrMsg(wchar *Msg,size_t Size); + bool GetSysErrMsg(std::wstring &Msg); void SysErrMsg(); int GetSystemErrorCode(); void SetSystemErrorCode(int Code); diff --git a/unrar_sys/vendor/unrar/extinfo.cpp b/unrar_sys/vendor/unrar/extinfo.cpp index d61747e..b3fe749 100644 --- a/unrar_sys/vendor/unrar/extinfo.cpp +++ b/unrar_sys/vendor/unrar/extinfo.cpp @@ -19,7 +19,7 @@ // RAR2 service header extra records. #ifndef SFX_MODULE -void SetExtraInfo20(CommandData *Cmd,Archive &Arc,wchar *Name) +void SetExtraInfo20(CommandData *Cmd,Archive &Arc,const std::wstring &Name) { #ifdef _WIN_ALL if (Cmd->Test) @@ -40,12 +40,12 @@ void SetExtraInfo20(CommandData *Cmd,Archive &Arc,wchar *Name) // RAR3 and RAR5 service header extra records. -void SetExtraInfo(CommandData *Cmd,Archive &Arc,wchar *Name) +void SetExtraInfo(CommandData *Cmd,Archive &Arc,const std::wstring &Name) { #ifdef _UNIX if (!Cmd->Test && Cmd->ProcessOwners && Arc.Format==RARFMT15 && Arc.SubHead.CmpName(SUBHEAD_TYPE_UOWNER)) - ExtractUnixOwner30(Arc,Name); + ExtractUnixOwner30(Arc,Name.c_str()); #endif #ifdef _WIN_ALL if (!Cmd->Test && Cmd->ProcessOwners && Arc.SubHead.CmpName(SUBHEAD_TYPE_ACL)) @@ -57,7 +57,7 @@ void SetExtraInfo(CommandData *Cmd,Archive &Arc,wchar *Name) // Extra data stored directly in file header. -void SetFileHeaderExtra(CommandData *Cmd,Archive &Arc,wchar *Name) +void SetFileHeaderExtra(CommandData *Cmd,Archive &Arc,const std::wstring &Name) { #ifdef _UNIX if (Cmd->ProcessOwners && Arc.Format==RARFMT50 && Arc.FileHead.UnixOwnerSet) @@ -68,39 +68,34 @@ void SetFileHeaderExtra(CommandData *Cmd,Archive &Arc,wchar *Name) -// Calculate a number of path components except \. and \.. -static int CalcAllowedDepth(const wchar *Name) +// Calculate the number of path components except \. and \.. +static int CalcAllowedDepth(const std::wstring &Name) { int AllowedDepth=0; - while (*Name!=0) - { - if (IsPathDiv(Name[0]) && Name[1]!=0 && !IsPathDiv(Name[1])) + for (size_t I=0;I=ASIZE(Path)) - return true; // It should not be that long, skip. - wcsncpyz(Path,Name,ASIZE(Path)); - for (wchar *s=Path+wcslen(Path)-1;s>Path;s--) - if (IsPathDiv(*s)) + if (Path.empty()) // So we can safely use Path.size()-1 below. + return false; + for (size_t I=Path.size()-1;I>0;I--) + if (IsPathDiv(Path[I])) { - *s=0; + Path.erase(I); FindData FD; if (FindFile::FastFind(Path,&FD,true) && (FD.IsLink || !FD.IsDir)) return true; @@ -109,7 +104,7 @@ static bool LinkInPath(const wchar *Name) } -bool IsRelativeSymlinkSafe(CommandData *Cmd,const wchar *SrcName,const wchar *PrepSrcName,const wchar *TargetName) +bool IsRelativeSymlinkSafe(CommandData *Cmd,const std::wstring &SrcName,std::wstring PrepSrcName,const std::wstring &TargetName) { // Catch root dir based /path/file paths also as stuff like \\?\. // Do not check PrepSrcName here, it can be root based if destination path @@ -119,14 +114,13 @@ bool IsRelativeSymlinkSafe(CommandData *Cmd,const wchar *SrcName,const wchar *Pr // Number of ".." in link target. int UpLevels=0; - for (int Pos=0;*TargetName!=0;Pos++) + for (uint Pos=0;PosExtrPath); - if (ExtrPathLength>0 && wcsncmp(PrepSrcName,Cmd->ExtrPath,ExtrPathLength)==0) + size_t ExtrPathLength=Cmd->ExtrPath.size(); + if (ExtrPathLength>0 && PrepSrcName.compare(0,ExtrPathLength,Cmd->ExtrPath)==0) { - PrepSrcName+=ExtrPathLength; - while (IsPathDiv(*PrepSrcName)) - PrepSrcName++; + while (IsPathDiv(PrepSrcName[ExtrPathLength])) + ExtrPathLength++; + PrepSrcName.erase(0,ExtrPathLength); } int PrepAllowedDepth=CalcAllowedDepth(PrepSrcName); @@ -161,7 +155,7 @@ bool IsRelativeSymlinkSafe(CommandData *Cmd,const wchar *SrcName,const wchar *Pr } -bool ExtractSymlink(CommandData *Cmd,ComprDataIO &DataIO,Archive &Arc,const wchar *LinkName,bool &UpLink) +bool ExtractSymlink(CommandData *Cmd,ComprDataIO &DataIO,Archive &Arc,const std::wstring &LinkName,bool &UpLink) { // Returning true in Uplink indicates that link target might include ".." // and enables additional checks. It is ok to falsely return true here, @@ -171,20 +165,20 @@ bool ExtractSymlink(CommandData *Cmd,ComprDataIO &DataIO,Archive &Arc,const wcha UpLink=true; // Assume the target might include potentially unsafe "..". #if defined(SAVE_LINKS) && defined(_UNIX) || defined(_WIN_ALL) if (Arc.Format==RARFMT50) // For RAR5 archives we can check RedirName for both Unix and Windows. - UpLink=wcsstr(Arc.FileHead.RedirName,L"..")!=NULL; + UpLink=Arc.FileHead.RedirName.find(L"..")!=std::wstring::npos; #endif #if defined(SAVE_LINKS) && defined(_UNIX) // For RAR 3.x archives we process links even in test mode to skip link data. if (Arc.Format==RARFMT15) - return ExtractUnixLink30(Cmd,DataIO,Arc,LinkName,UpLink); + return ExtractUnixLink30(Cmd,DataIO,Arc,LinkName.c_str(),UpLink); if (Arc.Format==RARFMT50) - return ExtractUnixLink50(Cmd,LinkName,&Arc.FileHead); + return ExtractUnixLink50(Cmd,LinkName.c_str(),&Arc.FileHead); #elif defined(_WIN_ALL) // RAR 5.0 archives store link information in file header, so there is // no need to additionally test it if we do not create a file. if (Arc.Format==RARFMT50) - return CreateReparsePoint(Cmd,LinkName,&Arc.FileHead); + return CreateReparsePoint(Cmd,LinkName.c_str(),&Arc.FileHead); #endif return false; } diff --git a/unrar_sys/vendor/unrar/extinfo.hpp b/unrar_sys/vendor/unrar/extinfo.hpp index da82ec3..11b89fe 100644 --- a/unrar_sys/vendor/unrar/extinfo.hpp +++ b/unrar_sys/vendor/unrar/extinfo.hpp @@ -1,23 +1,23 @@ #ifndef _RAR_EXTINFO_ #define _RAR_EXTINFO_ -bool IsRelativeSymlinkSafe(CommandData *Cmd,const wchar *SrcName,const wchar *PrepSrcName,const wchar *TargetName); -bool ExtractSymlink(CommandData *Cmd,ComprDataIO &DataIO,Archive &Arc,const wchar *LinkName,bool &UpLink); +bool IsRelativeSymlinkSafe(CommandData *Cmd,const std::wstring &SrcName,std::wstring PrepSrcName,const std::wstring &TargetName); +bool ExtractSymlink(CommandData *Cmd,ComprDataIO &DataIO,Archive &Arc,const std::wstring &LinkName,bool &UpLink); #ifdef _UNIX -void SetUnixOwner(Archive &Arc,const wchar *FileName); +void SetUnixOwner(Archive &Arc,const std::wstring &FileName); #endif -bool ExtractHardlink(CommandData *Cmd,wchar *NameNew,wchar *NameExisting,size_t NameExistingSize); +bool ExtractHardlink(CommandData *Cmd,const std::wstring &NameNew,const std::wstring &NameExisting); -void GetStreamNameNTFS(Archive &Arc,wchar *StreamName,size_t MaxSize); +std::wstring GetStreamNameNTFS(Archive &Arc); #ifdef _WIN_ALL bool SetPrivilege(LPCTSTR PrivName); #endif -void SetExtraInfo20(CommandData *Cmd,Archive &Arc,wchar *Name); -void SetExtraInfo(CommandData *Cmd,Archive &Arc,wchar *Name); -void SetFileHeaderExtra(CommandData *Cmd,Archive &Arc,wchar *Name); +void SetExtraInfo20(CommandData *Cmd,Archive &Arc,const std::wstring &Name); +void SetExtraInfo(CommandData *Cmd,Archive &Arc,const std::wstring &Name); +void SetFileHeaderExtra(CommandData *Cmd,Archive &Arc,const std::wstring &Name); #endif diff --git a/unrar_sys/vendor/unrar/extract.cpp b/unrar_sys/vendor/unrar/extract.cpp index f2eb166..f4ea12c 100644 --- a/unrar_sys/vendor/unrar/extract.cpp +++ b/unrar_sys/vendor/unrar/extract.cpp @@ -4,12 +4,8 @@ CmdExtract::CmdExtract(CommandData *Cmd) { CmdExtract::Cmd=Cmd; - *ArcName=0; - *DestFileName=0; - ArcAnalyzed=false; - Analyze=new AnalyzeData; - memset(Analyze,0,sizeof(*Analyze)); + Analyze={}; TotalFileCount=0; @@ -36,24 +32,21 @@ CmdExtract::~CmdExtract() { FreeAnalyzeData(); delete Unp; - delete Analyze; } void CmdExtract::FreeAnalyzeData() { - for (size_t I=0;ICommand[0]); - if (*Cmd->UseStdin==0) + if (Cmd->UseStdin.empty()) { FindData FD; - while (Cmd->GetArcName(ArcName,ASIZE(ArcName))) + while (Cmd->GetArcName(ArcName)) if (FindFile::FastFind(ArcName,&FD)) DataIO.TotalArcSize+=FD.Size; } Cmd->ArcNames.Rewind(); - while (Cmd->GetArcName(ArcName,ASIZE(ArcName))) + while (Cmd->GetArcName(ArcName)) { if (Cmd->ManualPassword) Cmd->Password.Clean(); // Clean user entered password before processing next archive. @@ -97,7 +90,7 @@ void CmdExtract::DoExtract() if (TotalFileCount==0 && Cmd->Command[0]!='I' && ErrHandler.GetErrorCode()!=RARX_BADPWD) // Not in case of wrong archive password. { - if (!PasswordCancelled) + if (!SuppressNoFilesMessage) uiMsg(UIERROR_NOFILESTOEXTRACT,ArcName); // Other error codes may explain a reason of "no files extracted" clearer, @@ -146,7 +139,7 @@ void CmdExtract::ExtractArchiveInit(Archive &Arc) EXTRACT_ARC_CODE CmdExtract::ExtractArchive() { Archive Arc(Cmd); - if (*Cmd->UseStdin!=0) + if (!Cmd->UseStdin.empty()) { Arc.SetHandleType(FILE_HANDLESTD); #ifdef USE_QOPEN @@ -155,8 +148,10 @@ EXTRACT_ARC_CODE CmdExtract::ExtractArchive() } else { -#if defined(_WIN_ALL) && !defined(SFX_MODULE) // WinRAR GUI code also resets the cache. - if (*Cmd->Command=='T' || Cmd->Test) + // We commented out "&& !defined(WINRAR)", because WinRAR GUI code resets + // the cache for usual test command, but not for test after archiving. +#if defined(_WIN_ALL) && !defined(SFX_MODULE) + if (Cmd->Command[0]=='T' || Cmd->Test) ResetFileCache(ArcName); // Reset the file cache when testing an archive. #endif if (!Arc.WOpen(ArcName)) @@ -168,8 +163,8 @@ EXTRACT_ARC_CODE CmdExtract::ExtractArchive() #if !defined(SFX_MODULE) && !defined(RARDLL) if (CmpExt(ArcName,L"rev")) { - wchar FirstVolName[NM]; - VolNameToFirstName(ArcName,FirstVolName,ASIZE(FirstVolName),true); + std::wstring FirstVolName; + VolNameToFirstName(ArcName,FirstVolName,true); // If several volume names from same volume set are specified // and current volume is not first in set and first volume is present @@ -183,7 +178,7 @@ EXTRACT_ARC_CODE CmdExtract::ExtractArchive() } #endif - mprintf(St(MNotRAR),ArcName); + mprintf(St(MNotRAR),ArcName.c_str()); #ifndef SFX_MODULE if (CmpExt(ArcName,L"rar")) @@ -198,8 +193,8 @@ EXTRACT_ARC_CODE CmdExtract::ExtractArchive() #ifndef SFX_MODULE if (Arc.Volume && !Arc.FirstVolume && !UseExactVolName) { - wchar FirstVolName[NM]; - VolNameToFirstName(ArcName,FirstVolName,ASIZE(FirstVolName),Arc.NewNumbering); + std::wstring FirstVolName; + VolNameToFirstName(ArcName,FirstVolName,Arc.NewNumbering); // If several volume names from same volume set are specified // and current volume is not first in set and first volume is present @@ -215,7 +210,7 @@ EXTRACT_ARC_CODE CmdExtract::ExtractArchive() int64 VolumeSetSize=0; // Total size of volumes after the current volume. #ifndef SFX_MODULE - if (!ArcAnalyzed && *Cmd->UseStdin==0) + if (!ArcAnalyzed && Cmd->UseStdin.empty()) { AnalyzeArchive(Arc.FileName,Arc.Volume,Arc.NewNumbering); ArcAnalyzed=true; // Avoid repeated analysis on EXTRACT_ARC_REPEAT. @@ -227,10 +222,10 @@ EXTRACT_ARC_CODE CmdExtract::ExtractArchive() #ifndef SFX_MODULE // Try to speed up extraction for independent solid volumes by starting // extraction from non-first volume if we can. - if (*Analyze->StartName!=0) + if (!Analyze.StartName.empty()) { - wcsncpyz(ArcName,Analyze->StartName,ASIZE(ArcName)); - *Analyze->StartName=0; + ArcName=Analyze.StartName; + Analyze.StartName.clear(); UseExactVolName=true; return EXTRACT_ARC_REPEAT; @@ -240,15 +235,14 @@ EXTRACT_ARC_CODE CmdExtract::ExtractArchive() // Calculate the total size of all accessible volumes. // This size is necessary to display the correct total progress indicator. - wchar NextName[NM]; - wcsncpyz(NextName,Arc.FileName,ASIZE(NextName)); + std::wstring NextName=Arc.FileName; while (true) { // First volume is already added to DataIO.TotalArcSize // in initial TotalArcSize calculation in DoExtract. // So we skip it and start from second volume. - NextVolumeName(NextName,ASIZE(NextName),!Arc.NewNumbering); + NextVolumeName(NextName,!Arc.NewNumbering); FindData FD; if (FindFile::FastFind(NextName,&FD)) VolumeSetSize+=FD.Size; @@ -260,11 +254,11 @@ EXTRACT_ARC_CODE CmdExtract::ExtractArchive() ExtractArchiveInit(Arc); - if (*Cmd->Command=='T' || *Cmd->Command=='I') + if (Cmd->Command[0]=='T' || Cmd->Command[0]=='I') Cmd->Test=true; - if (*Cmd->Command=='I') + if (Cmd->Command[0]=='I') { Cmd->DisablePercentage=true; } @@ -272,10 +266,10 @@ EXTRACT_ARC_CODE CmdExtract::ExtractArchive() uiStartArchiveExtract(!Cmd->Test,ArcName); #ifndef SFX_MODULE - if (Analyze->StartPos!=0) + if (Analyze.StartPos!=0) { - Arc.Seek(Analyze->StartPos,SEEK_SET); - Analyze->StartPos=0; + Arc.Seek(Analyze.StartPos,SEEK_SET); + Analyze.StartPos=0; } #endif @@ -336,8 +330,8 @@ bool CmdExtract::ExtractCurrentFile(Archive &Arc,size_t HeaderSize,bool &Repeat) if (HeaderType==HEAD_FILE) { // Unlike Arc.FileName, ArcName might store an old volume name here. - if (Analyze->EndPos!=0 && Analyze->EndPos==Arc.CurBlockPos && - (*Analyze->EndName==0 || wcscmp(Analyze->EndName,Arc.FileName)==0)) + if (Analyze.EndPos!=0 && Analyze.EndPos==Arc.CurBlockPos && + (Analyze.EndName.empty() || Analyze.EndName==Arc.FileName)) return false; } else @@ -376,32 +370,30 @@ bool CmdExtract::ExtractCurrentFile(Archive &Arc,size_t HeaderSize,bool &Repeat) // when reading an archive. But we prefer to do it here, because this // function is called directly in unrar.dll, so we fix bad parameters // passed to dll. Also we want to see real negative sizes in the listing - // of corrupt archive. To prevent uninitialized data access perform + // of corrupt archive. To prevent the uninitialized data access, perform // these checks after rejecting zero length and non-file headers above. if (Arc.FileHead.PackSize<0) Arc.FileHead.PackSize=0; if (Arc.FileHead.UnpSize<0) Arc.FileHead.UnpSize=0; - // 2022.03.20: We might remove this check in the future. - // It duplicates Analyze->EndPos and Analyze->EndName in all cases except - // volumes on removable media. + // This check duplicates Analyze.EndPos and Analyze.EndName + // in all cases except volumes on removable media. if (!Cmd->Recurse && MatchedArgs>=Cmd->FileArgs.ItemsCount() && AllMatchesExact) return false; int MatchType=MATCH_WILDSUBPATH; bool EqualNames=false; - wchar MatchedArg[NM]; - int MatchNumber=Cmd->IsProcessFile(Arc.FileHead,&EqualNames,MatchType,0,MatchedArg,ASIZE(MatchedArg)); - bool MatchFound=MatchNumber!=0; + std::wstring MatchedArg; + bool MatchFound=Cmd->IsProcessFile(Arc.FileHead,&EqualNames,MatchType,0,&MatchedArg)!=0; #ifndef SFX_MODULE if (Cmd->ExclPath==EXCL_BASEPATH) { - wcsncpyz(Cmd->ArcPath,MatchedArg,ASIZE(Cmd->ArcPath)); - *PointToName(Cmd->ArcPath)=0; + Cmd->ArcPath=MatchedArg; + GetPathWithSep(Cmd->ArcPath,Cmd->ArcPath); if (IsWildcard(Cmd->ArcPath)) // Cannot correctly process path*\* masks here. - *Cmd->ArcPath=0; + Cmd->ArcPath.clear(); } #endif if (MatchFound && !EqualNames) @@ -412,13 +404,13 @@ bool CmdExtract::ExtractCurrentFile(Archive &Arc,size_t HeaderSize,bool &Repeat) #if !defined(SFX_MODULE) && !defined(RARDLL) if (Arc.FileHead.SplitBefore && FirstFile && !UseExactVolName) { - wchar CurVolName[NM]; - wcsncpyz(CurVolName,ArcName,ASIZE(CurVolName)); - GetFirstVolIfFullSet(ArcName,Arc.NewNumbering,ArcName,ASIZE(ArcName)); + std::wstring StartVolName; + GetFirstVolIfFullSet(ArcName,Arc.NewNumbering,StartVolName); - if (wcsicomp(ArcName,CurVolName)!=0 && FileExist(ArcName)) + if (StartVolName!=ArcName && FileExist(StartVolName)) { - wcsncpyz(Cmd->ArcName,ArcName,ASIZE(ArcName)); // For GUI "Delete archive after extraction". + ArcName=StartVolName; + Cmd->ArcName=ArcName; // For GUI "Delete archive after extraction". // If first volume name does not match the current name and if such // volume name really exists, let's unpack from this first volume. Repeat=true; @@ -435,12 +427,11 @@ bool CmdExtract::ExtractCurrentFile(Archive &Arc,size_t HeaderSize,bool &Repeat) } } #endif - wcsncpyz(ArcName,CurVolName,ASIZE(ArcName)); } #endif - wchar ArcFileName[NM]; - ConvertPath(Arc.FileHead.FileName,ArcFileName,ASIZE(ArcFileName)); + std::wstring ArcFileName; + ConvertPath(&Arc.FileHead.FileName,&ArcFileName); if (Arc.FileHead.Version) { @@ -486,10 +477,10 @@ bool CmdExtract::ExtractCurrentFile(Archive &Arc,size_t HeaderSize,bool &Repeat) bool RefTarget=false; if (!MatchFound) - for (size_t I=0;ITest) // While harmless, it is useless for 't'. { @@ -500,11 +491,11 @@ bool CmdExtract::ExtractCurrentFile(Archive &Arc,size_t HeaderSize,bool &Repeat) // targets and it is possible that first target isn't unpacked // for some reason. Also targets might have associated service blocks // like ACLs. All this would complicate processing a lot. - wcsncpyz(DestFileName,*Cmd->TempPath!=0 ? Cmd->TempPath:Cmd->ExtrPath,ASIZE(DestFileName)); - AddEndSlash(DestFileName,ASIZE(DestFileName)); - wcsncatz(DestFileName,L"__tmp_reference_source_",ASIZE(DestFileName)); - MkTemp(DestFileName,ASIZE(DestFileName)); - MatchedRef->TmpName=wcsdup(DestFileName); + DestFileName=!Cmd->TempPath.empty() ? Cmd->TempPath:Cmd->ExtrPath; + AddEndSlash(DestFileName); + DestFileName+=L"__tmp_reference_source_"; + MkTemp(DestFileName); + MatchedRef.TmpName=DestFileName; } RefTarget=true; // Need it even for 't' to test the reference source. break; @@ -516,7 +507,7 @@ bool CmdExtract::ExtractCurrentFile(Archive &Arc,size_t HeaderSize,bool &Repeat) else MatchFound=false; // Skip only the current file for non-solid archive. - if (MatchFound || RefTarget || (SkipSolid=Arc.Solid)!=0) + if (MatchFound || RefTarget || (SkipSolid=Arc.Solid)!=false) { // First common call of uiStartFileExtract. It is done before overwrite // prompts, so if SkipSolid state is changed below, we'll need to make @@ -525,10 +516,10 @@ bool CmdExtract::ExtractCurrentFile(Archive &Arc,size_t HeaderSize,bool &Repeat) return false; if (!RefTarget) - ExtrPrepareName(Arc,ArcFileName,DestFileName,ASIZE(DestFileName)); + ExtrPrepareName(Arc,ArcFileName,DestFileName); // DestFileName can be set empty in case of excessive -ap switch. - ExtrFile=!SkipSolid && *DestFileName!=0 && !Arc.FileHead.SplitBefore; + ExtrFile=!SkipSolid && !DestFileName.empty() && !Arc.FileHead.SplitBefore; if ((Cmd->FreshFiles || Cmd->UpdateFiles) && (Command=='E' || Command=='X')) { @@ -580,7 +571,7 @@ bool CmdExtract::ExtractCurrentFile(Archive &Arc,size_t HeaderSize,bool &Repeat) #else if (!ExtrGetPassword(Arc,ArcFileName,CheckPwd.IsSet() ? &CheckPwd:NULL)) { - PasswordCancelled=true; + SuppressNoFilesMessage=true; return false; } #endif @@ -637,13 +628,25 @@ bool CmdExtract::ExtractCurrentFile(Archive &Arc,size_t HeaderSize,bool &Repeat) else DataIO.SetEncryption(false,CRYPT_NONE,NULL,NULL,NULL,0,NULL,NULL); + // Per file symlink conversion flag. Can be turned off in unrar.dll. + bool CurConvertSymlinkPaths=ConvertSymlinkPaths; + #ifdef RARDLL - if (*Cmd->DllDestName!=0) - wcsncpyz(DestFileName,Cmd->DllDestName,ASIZE(DestFileName)); + if (!Cmd->DllDestName.empty()) + { + DestFileName=Cmd->DllDestName; + + // If unrar.dll sets the entire destination pathname, there is no + // destination path and we can't convert symlinks, because we would + // risk converting important user or system symlinks in this case. + // If DllDestName is set, it turns off our path processing and app + // invoking the library cares about everything including safety. + CurConvertSymlinkPaths=false; + } #endif if (ExtrFile && Command!='P' && !Cmd->Test && !Cmd->AbsoluteLinks && - ConvertSymlinkPaths) + CurConvertSymlinkPaths) ExtrFile=LinksToDirs(DestFileName,Cmd->ExtrPath,LastCheckedSymlink); File CurFile; @@ -651,13 +654,17 @@ bool CmdExtract::ExtractCurrentFile(Archive &Arc,size_t HeaderSize,bool &Repeat) bool LinkEntry=Arc.FileHead.RedirType!=FSREDIR_NONE; if (LinkEntry && (Arc.FileHead.RedirType!=FSREDIR_FILECOPY)) { + if (Cmd->SkipSymLinks && (Arc.FileHead.RedirType==FSREDIR_UNIXSYMLINK || + Arc.FileHead.RedirType==FSREDIR_WINSYMLINK || Arc.FileHead.RedirType==FSREDIR_JUNCTION)) + ExtrFile=false; + if (ExtrFile && Command!='P' && !Cmd->Test) { // Overwrite prompt for symbolic and hard links and when we move // a temporary file to the file reference instead of copying it. bool UserReject=false; - if (FileExist(DestFileName) && !UserReject) - FileCreate(Cmd,NULL,DestFileName,ASIZE(DestFileName),&UserReject,Arc.FileHead.UnpSize,&Arc.FileHead.mtime); + if (FileExist(DestFileName)) + FileCreate(Cmd,NULL,DestFileName,&UserReject,Arc.FileHead.UnpSize,&Arc.FileHead.mtime); if (UserReject) ExtrFile=false; } @@ -676,7 +683,13 @@ bool CmdExtract::ExtractCurrentFile(Archive &Arc,size_t HeaderSize,bool &Repeat) } else if (ExtrFile) // Create files and file copies (FSREDIR_FILECOPY). + { + // Check the dictionary size before creating a file and issuing + // any overwrite prompts. + if (!CheckWinLimit(Arc,ArcFileName)) + return false; ExtrFile=ExtrCreateFile(Arc,CurFile); + } if (!ExtrFile && Arc.Solid) { @@ -690,6 +703,9 @@ bool CmdExtract::ExtractCurrentFile(Archive &Arc,size_t HeaderSize,bool &Repeat) // a solid archive. if (!uiStartFileExtract(ArcFileName,false,false,true)) return false; + // Check the dictionary size also for skipping files. + if (!CheckWinLimit(Arc,ArcFileName)) + return false; } if (ExtrFile) { @@ -712,21 +728,21 @@ bool CmdExtract::ExtractCurrentFile(Archive &Arc,size_t HeaderSize,bool &Repeat) FileCount++; if (Command!='I' && !Cmd->DisableNames) if (SkipSolid) - mprintf(St(MExtrSkipFile),ArcFileName); + mprintf(St(MExtrSkipFile),ArcFileName.c_str()); else switch(Cmd->Test ? 'T':Command) // "Test" can be also enabled by -t switch. { case 'T': - mprintf(St(MExtrTestFile),ArcFileName); + mprintf(St(MExtrTestFile),ArcFileName.c_str()); break; #ifndef SFX_MODULE case 'P': - mprintf(St(MExtrPrinting),ArcFileName); + mprintf(St(MExtrPrinting),ArcFileName.c_str()); break; #endif case 'X': case 'E': - mprintf(St(MExtrFile),DestFileName); + mprintf(St(MExtrFile),DestFileName.c_str()); break; } if (!Cmd->DisablePercentage && !Cmd->DisableNames) @@ -774,7 +790,7 @@ bool CmdExtract::ExtractCurrentFile(Archive &Arc,size_t HeaderSize,bool &Repeat) if (Type==FSREDIR_HARDLINK || Type==FSREDIR_FILECOPY) { - wchar RedirName[NM]; + std::wstring RedirName; // 2022.11.15: Might be needed when unpacking WinRAR 5.0 links with // Unix RAR. WinRAR 5.0 used \ path separators here, when beginning @@ -783,17 +799,17 @@ bool CmdExtract::ExtractCurrentFile(Archive &Arc,size_t HeaderSize,bool &Repeat) // We must perform this conversion before ConvertPath call, // so paths mixing different slashes like \dir1/dir2\file are // processed correctly. - SlashToNative(Arc.FileHead.RedirName,RedirName,ASIZE(RedirName)); + SlashToNative(Arc.FileHead.RedirName,RedirName); - ConvertPath(RedirName,RedirName,ASIZE(RedirName)); + ConvertPath(&RedirName,&RedirName); - wchar NameExisting[NM]; - ExtrPrepareName(Arc,RedirName,NameExisting,ASIZE(NameExisting)); - if (FileCreateMode && *NameExisting!=0) // *NameExisting can be 0 in case of excessive -ap switch. + std::wstring NameExisting; + ExtrPrepareName(Arc,RedirName,NameExisting); + if (FileCreateMode && !NameExisting.empty()) // *NameExisting can be empty in case of excessive -ap switch. if (Type==FSREDIR_HARDLINK) - LinkSuccess=ExtractHardlink(Cmd,DestFileName,NameExisting,ASIZE(NameExisting)); + LinkSuccess=ExtractHardlink(Cmd,DestFileName,NameExisting); else - LinkSuccess=ExtractFileCopy(CurFile,Arc.FileName,RedirName,DestFileName,NameExisting,ASIZE(NameExisting),Arc.FileHead.UnpSize); + LinkSuccess=ExtractFileCopy(CurFile,Arc.FileName,RedirName,DestFileName,NameExisting,Arc.FileHead.UnpSize); } else if (Type==FSREDIR_UNIXSYMLINK || Type==FSREDIR_WINSYMLINK || Type==FSREDIR_JUNCTION) @@ -803,10 +819,6 @@ bool CmdExtract::ExtractCurrentFile(Archive &Arc,size_t HeaderSize,bool &Repeat) bool UpLink; LinkSuccess=ExtractSymlink(Cmd,DataIO,Arc,DestFileName,UpLink); - // Unix symlink can have its own owner data. - if (LinkSuccess) - SetFileHeaderExtra(Cmd,Arc,DestFileName); - ConvertSymlinkPaths|=LinkSuccess && UpLink; // We do not actually need to reset the cache here if we cache @@ -844,7 +856,17 @@ bool CmdExtract::ExtractCurrentFile(Archive &Arc,size_t HeaderSize,bool &Repeat) UnstoreFile(DataIO,Arc.FileHead.UnpSize); else { - Unp->Init(Arc.FileHead.WinSize,Arc.FileHead.Solid); + try + { + Unp->Init(Arc.FileHead.WinSize,Arc.FileHead.Solid); + } + catch (std::bad_alloc) + { + if (Arc.FileHead.WinSize>=0x40000000) + uiMsg(UIERROR_EXTRDICTOUTMEM,Arc.FileName,uint(Arc.FileHead.WinSize/0x40000000+(Arc.FileHead.WinSize%0x40000000!=0 ? 1 : 0))); + throw; + } + Unp->SetDestSize(Arc.FileHead.UnpSize); #ifndef SFX_MODULE // RAR 1.3 - 1.5 archives do not set per file solid flag. @@ -912,24 +934,34 @@ bool CmdExtract::ExtractCurrentFile(Archive &Arc,size_t HeaderSize,bool &Repeat) if (SkipSolid) mprintf(L"\b\b\b\b\b "); } - - // If we successfully unpacked a hard link, we wish to set its file - // attributes. Hard link shares file metadata with link target, - // so we do not need to set link time or owner. But when we overwrite - // an existing link, we can call PrepareToDelete(), which affects - // link target attributes as well. So we set link attributes to restore - // both target and link attributes if PrepareToDelete() changed them. - bool SetAttrOnly=LinkEntry && Arc.FileHead.RedirType==FSREDIR_HARDLINK && LinkSuccess; - if (!TestMode && (Command=='X' || Command=='E') && - (!LinkEntry || SetAttrOnly || Arc.FileHead.RedirType==FSREDIR_FILECOPY && LinkSuccess) && - (!BrokenFile || Cmd->KeepBroken)) + (!LinkEntry || LinkSuccess) && (!BrokenFile || Cmd->KeepBroken)) { + // Set everything for usual files and file references. + bool SetAll=!LinkEntry || Arc.FileHead.RedirType==FSREDIR_FILECOPY; + + // Set time and adjust size for usual files and references. + // Symlink time requires the special treatment and it is set directly + // after creating a symlink. + bool SetTimeAndSize=SetAll; + + // Set file attributes for usual files, references and hard links. + // Hard link shares the file metadata with link target, so we do not + // need to set link time or owner. But when we overwrite an existing + // link, we can call PrepareToDelete(), which affects link target + // attributes too. So we set link attributes to restore both target + // and link attributes if PrepareToDelete() has changed them. + bool SetAttr=SetAll || Arc.FileHead.RedirType==FSREDIR_HARDLINK; + + // Call SetFileHeaderExtra to set Unix user and group for usual files, + // references and symlinks. Unix symlink can have its own owner data. + bool SetExtra=SetAll || Arc.FileHead.RedirType==FSREDIR_UNIXSYMLINK; + // Below we use DestFileName instead of CurFile.FileName, // so we can set file attributes also for hard links, which do not // have the open CurFile. These strings are the same for other items. - if (!SetAttrOnly) + if (SetTimeAndSize) { // We could preallocate more space that really written to broken file // or file with crafted header. @@ -942,28 +974,33 @@ bool CmdExtract::ExtractCurrentFile(Archive &Arc,size_t HeaderSize,bool &Repeat) Cmd->xctime==EXTTIME_NONE ? NULL:&Arc.FileHead.ctime, Cmd->xatime==EXTTIME_NONE ? NULL:&Arc.FileHead.atime); CurFile.Close(); + } + if (SetExtra) SetFileHeaderExtra(Cmd,Arc,DestFileName); + if (SetTimeAndSize) CurFile.SetCloseFileTime( Cmd->xmtime==EXTTIME_NONE ? NULL:&Arc.FileHead.mtime, Cmd->xatime==EXTTIME_NONE ? NULL:&Arc.FileHead.atime); - } + if (SetAttr) + { #if defined(_WIN_ALL) && !defined(SFX_MODULE) - if (Cmd->SetCompressedAttr && - (Arc.FileHead.FileAttr & FILE_ATTRIBUTE_COMPRESSED)!=0) - SetFileCompression(DestFileName,true); - if (Cmd->ClearArc) - Arc.FileHead.FileAttr&=~FILE_ATTRIBUTE_ARCHIVE; + if (Cmd->SetCompressedAttr && + (Arc.FileHead.FileAttr & FILE_ATTRIBUTE_COMPRESSED)!=0) + SetFileCompression(DestFileName,true); + if (Cmd->ClearArc) + Arc.FileHead.FileAttr&=~FILE_ATTRIBUTE_ARCHIVE; #endif - if (!Cmd->IgnoreGeneralAttr && !SetFileAttr(DestFileName,Arc.FileHead.FileAttr)) - { - uiMsg(UIERROR_FILEATTR,Arc.FileName,DestFileName); - // Android cannot set file attributes and while UIERROR_FILEATTR - // above is handled by Android RAR silently, this call would cause - // "Operation not permitted" message for every unpacked file. - ErrHandler.SysErrMsg(); + if (!Cmd->IgnoreGeneralAttr && !SetFileAttr(DestFileName,Arc.FileHead.FileAttr)) + { + uiMsg(UIERROR_FILEATTR,Arc.FileName,DestFileName); + // Android cannot set file attributes and while UIERROR_FILEATTR + // above is handled by Android RAR silently, this call would cause + // "Operation not permitted" message for every unpacked file. + ErrHandler.SysErrMsg(); + } } PrevProcessed=true; @@ -989,42 +1026,44 @@ bool CmdExtract::ExtractCurrentFile(Archive &Arc,size_t HeaderSize,bool &Repeat) void CmdExtract::UnstoreFile(ComprDataIO &DataIO,int64 DestUnpSize) { - Array Buffer(File::CopyBufferSize()); + std::vector Buffer(File::CopyBufferSize()); while (true) { - int ReadSize=DataIO.UnpRead(&Buffer[0],Buffer.Size()); + int ReadSize=DataIO.UnpRead(Buffer.data(),Buffer.size()); if (ReadSize<=0) break; int WriteSize=ReadSize0) { - DataIO.UnpWrite(&Buffer[0],WriteSize); + DataIO.UnpWrite(Buffer.data(),WriteSize); DestUnpSize-=WriteSize; } } } -bool CmdExtract::ExtractFileCopy(File &New,wchar *ArcName,const wchar *RedirName,wchar *NameNew,wchar *NameExisting,size_t NameExistingSize,int64 UnpSize) +bool CmdExtract::ExtractFileCopy(File &New,const std::wstring &ArcName,const std::wstring &RedirName,const std::wstring &NameNew,const std::wstring &NameExisting,int64 UnpSize) { File Existing; if (!Existing.Open(NameExisting)) { + std::wstring TmpExisting=NameExisting; // NameExisting is 'const', so copy it here. + bool OpenFailed=true; // If we couldn't find the existing file, check if match is present // in temporary reference sources list. - for (size_t I=0;IDllError=ERAR_EREFERENCE; @@ -1062,20 +1100,20 @@ bool CmdExtract::ExtractFileCopy(File &New,wchar *ArcName,const wchar *RedirName } } - Array Buffer(0x100000); + std::vector Buffer(0x100000); int64 CopySize=0; while (true) { Wait(); - int ReadSize=Existing.Read(&Buffer[0],Buffer.Size()); + int ReadSize=Existing.Read(Buffer.data(),Buffer.size()); if (ReadSize==0) break; // Update only the current file progress in WinRAR, set the total to 0 // to keep it as is. It looks better for WinRAR. uiExtractProgress(CopySize,UnpSize,0,0); - New.Write(&Buffer[0],ReadSize); + New.Write(Buffer.data(),ReadSize); CopySize+=ReadSize; } @@ -1083,7 +1121,7 @@ bool CmdExtract::ExtractFileCopy(File &New,wchar *ArcName,const wchar *RedirName } -void CmdExtract::ExtrPrepareName(Archive &Arc,const wchar *ArcFileName,wchar *DestName,size_t DestSize) +void CmdExtract::ExtrPrepareName(Archive &Arc,const std::wstring &ArcFileName,std::wstring &DestName) { if (Cmd->Test) { @@ -1091,15 +1129,15 @@ void CmdExtract::ExtrPrepareName(Archive &Arc,const wchar *ArcFileName,wchar *De // This check also allows to avoid issuing "Attempting to correct... // Renaming..." messages in MakeNameCompatible() below for problematic // names like aux.txt when testing an archive. - wcsncpyz(DestName,ArcFileName,DestSize); + DestName=ArcFileName; return; } - wcsncpyz(DestName,Cmd->ExtrPath,DestSize); + DestName=Cmd->ExtrPath; - if (*Cmd->ExtrPath!=0) + if (!Cmd->ExtrPath.empty()) { - wchar LastChar=*PointToLastChar(Cmd->ExtrPath); + wchar LastChar=GetLastChar(Cmd->ExtrPath); // We need IsPathDiv check here to correctly handle Unix forward slash // in the end of destination path in Windows: rar x arc dest/ // so we call IsPathDiv first instead of just calling AddEndSlash, @@ -1108,7 +1146,7 @@ void CmdExtract::ExtrPrepareName(Archive &Arc,const wchar *ArcFileName,wchar *De if (!IsPathDiv(LastChar) && !IsDriveDiv(LastChar)) { // Destination path can be without trailing slash if it come from GUI shell. - AddEndSlash(DestName,DestSize); + AddEndSlash(DestName); } } @@ -1118,38 +1156,40 @@ void CmdExtract::ExtrPrepareName(Archive &Arc,const wchar *ArcFileName,wchar *De switch(Cmd->AppendArcNameToPath) { case APPENDARCNAME_DESTPATH: // To subdir of destination path. - wcsncatz(DestName,PointToName(Arc.FirstVolumeName),DestSize); - SetExt(DestName,NULL,DestSize); + DestName+=PointToName(Arc.FirstVolumeName); + RemoveExt(DestName); break; case APPENDARCNAME_OWNSUBDIR: // To subdir of archive own dir. - wcsncpyz(DestName,Arc.FirstVolumeName,DestSize); - SetExt(DestName,NULL,DestSize); + DestName=Arc.FirstVolumeName; + RemoveExt(DestName); break; case APPENDARCNAME_OWNDIR: // To archive own dir. - wcsncpyz(DestName,Arc.FirstVolumeName,DestSize); + DestName=Arc.FirstVolumeName; RemoveNameFromPath(DestName); break; } - AddEndSlash(DestName,DestSize); + AddEndSlash(DestName); } #endif - + // We need to modify the name below and ArcFileName is const. + std::wstring CurName=ArcFileName; #ifndef SFX_MODULE - wchar *ArcPath=*Cmd->ExclArcPath!=0 ? Cmd->ExclArcPath:Cmd->ArcPath; - size_t ArcPathLength=wcslen(ArcPath); + std::wstring &ArcPath=!Cmd->ExclArcPath.empty() ? Cmd->ExclArcPath:Cmd->ArcPath; + size_t ArcPathLength=ArcPath.size(); if (ArcPathLength>0) { - size_t NameLength=wcslen(ArcFileName); - if (NameLength>=ArcPathLength && wcsnicompc(ArcPath,ArcFileName,ArcPathLength)==0 && + size_t NameLength=CurName.size(); + if (NameLength>=ArcPathLength && wcsnicompc(ArcPath,CurName,ArcPathLength)==0 && (IsPathDiv(ArcPath[ArcPathLength-1]) || - IsPathDiv(ArcFileName[ArcPathLength]) || ArcFileName[ArcPathLength]==0)) + IsPathDiv(CurName[ArcPathLength]) || CurName[ArcPathLength]==0)) { - ArcFileName+=Min(ArcPathLength,NameLength); - while (IsPathDiv(*ArcFileName)) - ArcFileName++; - if (*ArcFileName==0) // Excessive -ap switch. + size_t Pos=Min(ArcPathLength,NameLength); + while (PosExclPath==EXCL_ABSPATH && Command=='X' && IsDriveDiv(':'); - // We do not use any user specified destination paths when extracting - // absolute paths in -ep3 mode. if (AbsPaths) - *DestName=0; + { + // We do not use a user specified destination path when extracting + // absolute paths in -ep3 mode. + wchar DiskLetter=toupperw(CurName[0]); + if (CurName[1]=='_' && IsPathDiv(CurName[2]) && DiskLetter>='A' && DiskLetter<='Z') + DestName=CurName.substr(0,1) + L':' + CurName.substr(2); + else + if (CurName[0]=='_' && CurName[1]=='_') + DestName=std::wstring(2,CPATHDIVIDER) + CurName.substr(2); + else + AbsPaths=false; // Apply the destination path even with -ep3 for not absolute path. + } if (Command=='E' || Cmd->ExclPath==EXCL_SKIPWHOLEPATH) - wcsncatz(DestName,PointToName(ArcFileName),DestSize); - else - wcsncatz(DestName,ArcFileName,DestSize); + CurName=PointToName(CurName); + if (!AbsPaths) + DestName+=CurName; #ifdef _WIN_ALL // Must do after Cmd->ArcPath processing above, so file name and arc path // trailing spaces are in sync. if (!Cmd->AllowIncompatNames) - MakeNameCompatible(DestName,DestSize); + MakeNameCompatible(DestName); #endif - - wchar DiskLetter=toupperw(DestName[0]); - - if (AbsPaths) - { - if (DestName[1]=='_' && IsPathDiv(DestName[2]) && - DiskLetter>='A' && DiskLetter<='Z') - DestName[1]=':'; - else - if (DestName[0]=='_' && DestName[1]=='_') - { - // Convert __server\share to \\server\share. - DestName[0]=CPATHDIVIDER; - DestName[1]=CPATHDIVIDER; - } - } } @@ -1212,7 +1245,7 @@ bool CmdExtract::ExtrDllGetPassword() *PasswordA=0; if (Cmd->Callback(UCM_NEEDPASSWORD,Cmd->UserData,(LPARAM)PasswordA,ASIZE(PasswordA))==-1) *PasswordA=0; - GetWideName(PasswordA,NULL,PasswordW,ASIZE(PasswordW)); + CharToWide(PasswordA,PasswordW,ASIZE(PasswordW)); cleandata(PasswordA,sizeof(PasswordA)); } Cmd->Password.Set(PasswordW); @@ -1228,7 +1261,7 @@ bool CmdExtract::ExtrDllGetPassword() #ifndef RARDLL -bool CmdExtract::ExtrGetPassword(Archive &Arc,const wchar *ArcFileName,RarCheckPassword *CheckPwd) +bool CmdExtract::ExtrGetPassword(Archive &Arc,const std::wstring &ArcFileName,RarCheckPassword *CheckPwd) { if (!Cmd->Password.IsSet()) { @@ -1244,7 +1277,7 @@ bool CmdExtract::ExtrGetPassword(Archive &Arc,const wchar *ArcFileName,RarCheckP else if (!GlobalPassword && !Arc.FileHead.Solid) { - eprintf(St(MUseCurPsw),ArcFileName); + eprintf(St(MUseCurPsw),ArcFileName.c_str()); switch(Cmd->AllYes ? 1 : Ask(St(MYesNoAll))) { case -1: @@ -1285,13 +1318,13 @@ void CmdExtract::ConvertDosPassword(Archive &Arc,SecPassword &DestPwd) #endif -void CmdExtract::ExtrCreateDir(Archive &Arc,const wchar *ArcFileName) +void CmdExtract::ExtrCreateDir(Archive &Arc,const std::wstring &ArcFileName) { if (Cmd->Test) { if (!Cmd->DisableNames) { - mprintf(St(MExtrTestFile),ArcFileName); + mprintf(St(MExtrTestFile),ArcFileName.c_str()); mprintf(L" %s",St(MOk)); } return; @@ -1307,7 +1340,7 @@ void CmdExtract::ExtrCreateDir(Archive &Arc,const wchar *ArcFileName) // File with name same as this directory exists. Propose user // to overwrite it. bool UserReject; - FileCreate(Cmd,NULL,DestFileName,ASIZE(DestFileName),&UserReject,Arc.FileHead.UnpSize,&Arc.FileHead.mtime); + FileCreate(Cmd,NULL,DestFileName,&UserReject,Arc.FileHead.UnpSize,&Arc.FileHead.mtime); DirExist=false; } if (!DirExist) @@ -1317,17 +1350,15 @@ void CmdExtract::ExtrCreateDir(Archive &Arc,const wchar *ArcFileName) if (MDCode!=MKDIR_SUCCESS && !IsNameUsable(DestFileName)) { uiMsg(UIMSG_CORRECTINGNAME,Arc.FileName); - wchar OrigName[ASIZE(DestFileName)]; - wcsncpyz(OrigName,DestFileName,ASIZE(OrigName)); + std::wstring OrigName=DestFileName; MakeNameUsable(DestFileName,true); #ifndef SFX_MODULE uiMsg(UIERROR_RENAMING,Arc.FileName,OrigName,DestFileName); #endif DirExist=FileExist(DestFileName) && IsDir(GetFileAttr(DestFileName)); - if (!DirExist) + if (!DirExist && (Cmd->AbsoluteLinks || !ConvertSymlinkPaths || + LinksToDirs(DestFileName,Cmd->ExtrPath,LastCheckedSymlink))) { - if (!Cmd->AbsoluteLinks && ConvertSymlinkPaths) - LinksToDirs(DestFileName,Cmd->ExtrPath,LastCheckedSymlink); CreatePath(DestFileName,true,Cmd->DisableNames); MDCode=MakeDir(DestFileName,!Cmd->IgnoreGeneralAttr,Arc.FileHead.FileAttr); } @@ -1338,7 +1369,7 @@ void CmdExtract::ExtrCreateDir(Archive &Arc,const wchar *ArcFileName) { if (!Cmd->DisableNames) { - mprintf(St(MCreatDir),DestFileName); + mprintf(St(MCreatDir),DestFileName.c_str()); mprintf(L" %s",St(MOk)); } PrevProcessed=true; @@ -1388,7 +1419,7 @@ bool CmdExtract::ExtrCreateFile(Archive &Arc,File &CurFile) bool UserReject; // Specify "write only" mode to avoid OpenIndiana NAS problems // with SetFileTime and read+write files. - if (!FileCreate(Cmd,&CurFile,DestFileName,ASIZE(DestFileName),&UserReject,Arc.FileHead.UnpSize,&Arc.FileHead.mtime,true)) + if (!FileCreate(Cmd,&CurFile,DestFileName,&UserReject,Arc.FileHead.UnpSize,&Arc.FileHead.mtime,true)) { Success=false; if (!UserReject) @@ -1404,23 +1435,24 @@ bool CmdExtract::ExtrCreateFile(Archive &Arc,File &CurFile) { uiMsg(UIMSG_CORRECTINGNAME,Arc.FileName); - wchar OrigName[ASIZE(DestFileName)]; - wcsncpyz(OrigName,DestFileName,ASIZE(OrigName)); + std::wstring OrigName=DestFileName; MakeNameUsable(DestFileName,true); - if (!Cmd->AbsoluteLinks && ConvertSymlinkPaths) - LinksToDirs(DestFileName,Cmd->ExtrPath,LastCheckedSymlink); - CreatePath(DestFileName,true,Cmd->DisableNames); - if (FileCreate(Cmd,&CurFile,DestFileName,ASIZE(DestFileName),&UserReject,Arc.FileHead.UnpSize,&Arc.FileHead.mtime,true)) + if (Cmd->AbsoluteLinks || !ConvertSymlinkPaths || + LinksToDirs(DestFileName,Cmd->ExtrPath,LastCheckedSymlink)) { + CreatePath(DestFileName,true,Cmd->DisableNames); + if (FileCreate(Cmd,&CurFile,DestFileName,&UserReject,Arc.FileHead.UnpSize,&Arc.FileHead.mtime,true)) + { #ifndef SFX_MODULE - uiMsg(UIERROR_RENAMING,Arc.FileName,OrigName,DestFileName); + uiMsg(UIERROR_RENAMING,Arc.FileName,OrigName,DestFileName); #endif - Success=true; + Success=true; + } + else + ErrHandler.CreateErrorMsg(Arc.FileName,DestFileName); } - else - ErrHandler.CreateErrorMsg(Arc.FileName,DestFileName); } } } @@ -1429,11 +1461,11 @@ bool CmdExtract::ExtrCreateFile(Archive &Arc,File &CurFile) } -bool CmdExtract::CheckUnpVer(Archive &Arc,const wchar *ArcFileName) +bool CmdExtract::CheckUnpVer(Archive &Arc,const std::wstring &ArcFileName) { bool WrongVer; - if (Arc.Format==RARFMT50) // Both SFX and RAR can unpack RAR 5.0 archives. - WrongVer=Arc.FileHead.UnpVer>VER_UNPACK5; + if (Arc.Format==RARFMT50) // Both SFX and RAR can unpack RAR 5.0 and 7.0 archives. + WrongVer=Arc.FileHead.UnpVer>VER_UNPACK7; else { #ifdef SFX_MODULE // SFX can unpack only RAR 2.9 archives. @@ -1466,7 +1498,7 @@ bool CmdExtract::CheckUnpVer(Archive &Arc,const wchar *ArcFileName) // But it would be slower for solid archives than scaning headers // in first pass and extracting everything in second, as implemented now. // -void CmdExtract::AnalyzeArchive(const wchar *ArcName,bool Volume,bool NewNumbering) +void CmdExtract::AnalyzeArchive(const std::wstring &ArcName,bool Volume,bool NewNumbering) { FreeAnalyzeData(); // If processing non-first archive in multiple archives set. @@ -1476,11 +1508,11 @@ void CmdExtract::AnalyzeArchive(const wchar *ArcName,bool Volume,bool NewNumberi return; // No need to check further for * and *.* masks. // Start search from first volume if all volumes preceding current are available. - wchar NextName[NM]; + std::wstring NextName; if (Volume) - GetFirstVolIfFullSet(ArcName,NewNumbering,NextName,ASIZE(NextName)); + GetFirstVolIfFullSet(ArcName,NewNumbering,NextName); else - wcsncpyz(NextName,ArcName,ASIZE(NextName)); + NextName=ArcName; bool MatchFound=false; bool PrevMatched=false; @@ -1489,7 +1521,7 @@ void CmdExtract::AnalyzeArchive(const wchar *ArcName,bool Volume,bool NewNumberi bool FirstVolume=true; // We shall set FirstFile once for all volumes and not for each volume. - // So we do not reuse the outdated Analyze->StartPos from previous volume + // So we do not reuse the outdated Analyze.StartPos from previous volume // if extracted file resides completely in the beginning of current one. bool FirstFile=true; @@ -1503,8 +1535,8 @@ void CmdExtract::AnalyzeArchive(const wchar *ArcName,bool Volume,bool NewNumberi // If we couldn't open trailing volumes, we can't set early exit // parameters. It is possible that some volume are on removable media // and will be provided by user when extracting. - *Analyze->EndName=0; - Analyze->EndPos=0; + Analyze.EndName.clear(); + Analyze.EndPos=0; } break; } @@ -1539,24 +1571,24 @@ void CmdExtract::AnalyzeArchive(const wchar *ArcName,bool Volume,bool NewNumberi // if we set StartName for first volume or StartPos for first // archived file. if (!FirstVolume) - wcsncpyz(Analyze->StartName,NextName,ASIZE(Analyze->StartName)); + Analyze.StartName=NextName; // We shall set FirstFile once for all volumes for this code // to work properly. Alternatively we could append - // "|| Analyze->StartPos!=0" to the condition, so we do not reuse - // the outdated Analyze->StartPos value from previous volume. + // "|| Analyze.StartPos!=0" to the condition, so we do not reuse + // the outdated Analyze.StartPos value from previous volume. if (!FirstFile) - Analyze->StartPos=Arc.CurBlockPos; + Analyze.StartPos=Arc.CurBlockPos; } - if (Cmd->IsProcessFile(Arc.FileHead,NULL,MATCH_WILDSUBPATH,0,NULL,0)!=0) + if (Cmd->IsProcessFile(Arc.FileHead,NULL,MATCH_WILDSUBPATH,0,NULL)!=0) { MatchFound = true; PrevMatched = true; // Reset the previously set early exit position, if any, because // we found a new matched file. - Analyze->EndPos=0; + Analyze.EndPos=0; // Matched file reference pointing at maybe non-matched source file. // Even though we know RedirName, we can't check if source file @@ -1565,8 +1597,8 @@ void CmdExtract::AnalyzeArchive(const wchar *ArcName,bool Volume,bool NewNumberi if (Arc.FileHead.RedirType==FSREDIR_FILECOPY) { bool AlreadyAdded=false; - for (size_t I=0;IEndName,NextName,ASIZE(Analyze->EndName)); - Analyze->EndPos=Arc.CurBlockPos; + Analyze.EndName=NextName; + Analyze.EndPos=Arc.CurBlockPos; } PrevMatched=false; } @@ -1616,7 +1648,7 @@ void CmdExtract::AnalyzeArchive(const wchar *ArcName,bool Volume,bool NewNumberi if (Volume && OpenNext) { - NextVolumeName(NextName,ASIZE(NextName),!Arc.NewNumbering); + NextVolumeName(NextName,!Arc.NewNumbering); FirstVolume=false; // Needed for multivolume archives. Added in case some 'break' @@ -1631,8 +1663,8 @@ void CmdExtract::AnalyzeArchive(const wchar *ArcName,bool Volume,bool NewNumberi // If file references are present, we can't reliably skip in semi-solid // archives, because reference source can be present in skipped data. - if (RefList.Size()!=0) - memset(Analyze,0,sizeof(*Analyze)); + if (RefList.size()!=0) + Analyze={}; } #endif @@ -1640,26 +1672,46 @@ void CmdExtract::AnalyzeArchive(const wchar *ArcName,bool Volume,bool NewNumberi #ifndef SFX_MODULE // Return the first volume name if all volumes preceding the specified // are available. Otherwise return the specified volume name. -void CmdExtract::GetFirstVolIfFullSet(const wchar *SrcName,bool NewNumbering,wchar *DestName,size_t DestSize) +void CmdExtract::GetFirstVolIfFullSet(const std::wstring &SrcName,bool NewNumbering,std::wstring &DestName) { - wchar FirstVolName[NM]; - VolNameToFirstName(SrcName,FirstVolName,ASIZE(FirstVolName),NewNumbering); - wchar NextName[NM]; - wcsncpyz(NextName,FirstVolName,ASIZE(NextName)); - wchar ResultName[NM]; - wcsncpyz(ResultName,SrcName,ASIZE(ResultName)); + std::wstring FirstVolName; + VolNameToFirstName(SrcName,FirstVolName,NewNumbering); + std::wstring NextName=FirstVolName; + std::wstring ResultName=SrcName; while (true) { - if (wcscmp(SrcName,NextName)==0) + if (SrcName==NextName) { - wcsncpyz(ResultName,FirstVolName,DestSize); + ResultName=FirstVolName; // Reached the specified volume starting from the first. break; } if (!FileExist(NextName)) break; - NextVolumeName(NextName,ASIZE(NextName),!NewNumbering); + NextVolumeName(NextName,!NewNumbering); } - wcsncpyz(DestName,ResultName,DestSize); + DestName=ResultName; } +#endif -#endif \ No newline at end of file + +bool CmdExtract::CheckWinLimit(Archive &Arc,std::wstring &ArcFileName) +{ + if (Arc.FileHead.WinSize<=Cmd->WinSizeLimit || Arc.FileHead.WinSize<=Cmd->WinSize) + return true; + if (uiDictLimit(Cmd,ArcFileName,Arc.FileHead.WinSize,Max(Cmd->WinSizeLimit,Cmd->WinSize))) + { + // No more prompts when extracting other files. Important for GUI versions, + // where we might not have [Max]WinSize set permanently when extracting. + Cmd->WinSizeLimit=Arc.FileHead.WinSize; + } + else + { + ErrHandler.SetErrorCode(RARX_FATAL); +#ifdef RARDLL + Cmd->DllError=ERAR_LARGE_DICT; +#endif + Arc.SeekToNext(); + return false; + } + return true; +} diff --git a/unrar_sys/vendor/unrar/extract.hpp b/unrar_sys/vendor/unrar/extract.hpp index 18396c5..4400057 100644 --- a/unrar_sys/vendor/unrar/extract.hpp +++ b/unrar_sys/vendor/unrar/extract.hpp @@ -8,41 +8,42 @@ class CmdExtract private: struct ExtractRef { - wchar *RefName; - wchar *TmpName; + std::wstring RefName; + std::wstring TmpName; uint64 RefCount; }; - Array RefList; + std::vector RefList; struct AnalyzeData { - wchar StartName[NM]; + std::wstring StartName; uint64 StartPos; - wchar EndName[NM]; + std::wstring EndName; uint64 EndPos; - } *Analyze; + } Analyze; bool ArcAnalyzed; void FreeAnalyzeData(); EXTRACT_ARC_CODE ExtractArchive(); - bool ExtractFileCopy(File &New,wchar *ArcName,const wchar *RedirName,wchar *NameNew,wchar *NameExisting,size_t NameExistingSize,int64 UnpSize); - void ExtrPrepareName(Archive &Arc,const wchar *ArcFileName,wchar *DestName,size_t DestSize); + bool ExtractFileCopy(File &New,const std::wstring &ArcName,const std::wstring &RedirName,const std::wstring &NameNew,const std::wstring &NameExisting,int64 UnpSize); + void ExtrPrepareName(Archive &Arc,const std::wstring &ArcFileName,std::wstring &DestName); #ifdef RARDLL bool ExtrDllGetPassword(); #else - bool ExtrGetPassword(Archive &Arc,const wchar *ArcFileName,RarCheckPassword *CheckPwd); + bool ExtrGetPassword(Archive &Arc,const std::wstring &ArcFileName,RarCheckPassword *CheckPwd); #endif #if defined(_WIN_ALL) && !defined(SFX_MODULE) void ConvertDosPassword(Archive &Arc,SecPassword &DestPwd); #endif - void ExtrCreateDir(Archive &Arc,const wchar *ArcFileName); + void ExtrCreateDir(Archive &Arc,const std::wstring &ArcFileName); bool ExtrCreateFile(Archive &Arc,File &CurFile); - bool CheckUnpVer(Archive &Arc,const wchar *ArcFileName); + bool CheckUnpVer(Archive &Arc,const std::wstring &ArcFileName); #ifndef SFX_MODULE - void AnalyzeArchive(const wchar *ArcName,bool Volume,bool NewNumbering); - void GetFirstVolIfFullSet(const wchar *SrcName,bool NewNumbering,wchar *DestName,size_t DestSize); + void AnalyzeArchive(const std::wstring &ArcName,bool Volume,bool NewNumbering); + void GetFirstVolIfFullSet(const std::wstring &SrcName,bool NewNumbering,std::wstring &DestName); #endif + bool CheckWinLimit(Archive &Arc,std::wstring &ArcFileName); RarTime StartTime; // Time when extraction started. @@ -65,12 +66,12 @@ class CmdExtract // any wrong password hints. bool AnySolidDataUnpackedWell; - wchar ArcName[NM]; + std::wstring ArcName; bool GlobalPassword; bool PrevProcessed; // If previous file was successfully extracted or tested. - wchar DestFileName[NM]; - bool PasswordCancelled; + std::wstring DestFileName; + bool SuppressNoFilesMessage; // In Windows it is set to true if at least one symlink with ".." // in target was extracted. diff --git a/unrar_sys/vendor/unrar/filcreat.cpp b/unrar_sys/vendor/unrar/filcreat.cpp index d58e4f6..6b21aa2 100644 --- a/unrar_sys/vendor/unrar/filcreat.cpp +++ b/unrar_sys/vendor/unrar/filcreat.cpp @@ -3,7 +3,7 @@ // If NewFile==NULL, we delete created file after user confirmation. // It is useful if we need to overwrite an existing folder or file, // but need user confirmation for that. -bool FileCreate(CommandData *Cmd,File *NewFile,wchar *Name,size_t MaxNameSize, +bool FileCreate(CommandData *Cmd,File *NewFile,std::wstring &Name, bool *UserReject,int64 FileSize,RarTime *FileTime,bool WriteOnly) { if (UserReject!=NULL) @@ -29,7 +29,7 @@ bool FileCreate(CommandData *Cmd,File *NewFile,wchar *Name,size_t MaxNameSize, // autorename below can change the name, so we need to check it again. ShortNameChanged=false; #endif - UIASKREP_RESULT Choice=uiAskReplaceEx(Cmd,Name,MaxNameSize,FileSize,FileTime,(NewFile==NULL ? UIASKREP_F_NORENAME:0)); + UIASKREP_RESULT Choice=uiAskReplaceEx(Cmd,Name,FileSize,FileTime,(NewFile==NULL ? UIASKREP_F_NORENAME:0)); if (Choice==UIASKREP_R_REPLACE) break; @@ -56,85 +56,70 @@ bool FileCreate(CommandData *Cmd,File *NewFile,wchar *Name,size_t MaxNameSize, } -bool GetAutoRenamedName(wchar *Name,size_t MaxNameSize) -{ - wchar NewName[NM]; - size_t NameLength=wcslen(Name); - wchar *Ext=GetExt(Name); - if (Ext==NULL) - Ext=Name+NameLength; - for (uint FileVer=1;;FileVer++) - { - swprintf(NewName,ASIZE(NewName),L"%.*ls(%u)%ls",uint(Ext-Name),Name,FileVer,Ext); - if (!FileExist(NewName)) - { - wcsncpyz(Name,NewName,MaxNameSize); - break; - } - if (FileVer>=1000000) - return false; - } - return true; -} - - #if defined(_WIN_ALL) // If we find a file, which short name is equal to 'Name', we try to change // its short name, while preserving the long name. It helps when unpacking // an archived file, which long name is equal to short name of already // existing file. Otherwise we would overwrite the already existing file, // even though its long name does not match the name of unpacking file. -bool UpdateExistingShortName(const wchar *Name) +bool UpdateExistingShortName(const std::wstring &Name) { - wchar LongPathName[NM]; - DWORD Res=GetLongPathName(Name,LongPathName,ASIZE(LongPathName)); - if (Res==0 || Res>=ASIZE(LongPathName)) + DWORD Res=GetLongPathName(Name.c_str(),NULL,0); + if (Res==0) return false; - wchar ShortPathName[NM]; - Res=GetShortPathName(Name,ShortPathName,ASIZE(ShortPathName)); - if (Res==0 || Res>=ASIZE(ShortPathName)) + std::vector LongPathBuf(Res); + Res=GetLongPathName(Name.c_str(),LongPathBuf.data(),(DWORD)LongPathBuf.size()); + if (Res==0 || Res>=LongPathBuf.size()) return false; - wchar *LongName=PointToName(LongPathName); - wchar *ShortName=PointToName(ShortPathName); + Res=GetShortPathName(Name.c_str(),NULL,0); + if (Res==0) + return false; + std::vector ShortPathBuf(Res); + Res=GetShortPathName(Name.c_str(),ShortPathBuf.data(),(DWORD)ShortPathBuf.size()); + if (Res==0 || Res>=ShortPathBuf.size()) + return false; + std::wstring LongPathName=LongPathBuf.data(); + std::wstring ShortPathName=ShortPathBuf.data(); + + std::wstring LongName=PointToName(LongPathName); + std::wstring ShortName=PointToName(ShortPathName); // We continue only if file has a short name, which does not match its // long name, and this short name is equal to name of file which we need // to create. - if (*ShortName==0 || wcsicomp(LongName,ShortName)==0 || + if (ShortName.empty() || wcsicomp(LongName,ShortName)==0 || wcsicomp(PointToName(Name),ShortName)!=0) return false; // Generate the temporary new name for existing file. - wchar NewName[NM]; - *NewName=0; - for (int I=0;I<10000 && *NewName==0;I+=123) + std::wstring NewName; + for (uint I=0;I<10000 && NewName.empty();I+=123) { // Here we copy the path part of file to create. We'll make the temporary // file in the same folder. - wcsncpyz(NewName,Name,ASIZE(NewName)); + NewName=Name; // Here we set the random name part. - swprintf(PointToName(NewName),ASIZE(NewName),L"rtmp%d",I); + SetName(NewName,std::wstring(L"rtmp") + std::to_wstring(I)); // If such file is already exist, try next random name. if (FileExist(NewName)) - *NewName=0; + NewName.clear(); } // If we could not generate the name not used by any other file, we return. - if (*NewName==0) + if (NewName.empty()) return false; // FastFind returns the name without path, but we need the fully qualified // name for renaming, so we use the path from file to create and long name // from existing file. - wchar FullName[NM]; - wcsncpyz(FullName,Name,ASIZE(FullName)); - SetName(FullName,LongName,ASIZE(FullName)); + std::wstring FullName=Name; + SetName(FullName,LongName); // Rename the existing file to randomly generated name. Normally it changes // the short name too. - if (!MoveFile(FullName,NewName)) + if (!MoveFile(FullName.c_str(),NewName.c_str())) return false; // Now we need to create the temporary empty file with same name as @@ -149,7 +134,7 @@ bool UpdateExistingShortName(const wchar *Name) // Now we rename the existing file from temporary name to original long name. // Since its previous short name is occupied by another file, it should // get another short name. - MoveFile(NewName,FullName); + MoveFile(NewName.c_str(),FullName.c_str()); if (Created) { @@ -157,9 +142,9 @@ bool UpdateExistingShortName(const wchar *Name) KeepShortFile.Close(); KeepShortFile.Delete(); } - // We successfully changed the short name. Maybe sometimes we'll simplify - // this function by use of SetFileShortName Windows API call. - // But SetFileShortName is not available in older Windows. + // We successfully changed the short name. We do not use the simpler + // SetFileShortName Windows API call, because it requires SE_RESTORE_NAME + // privilege. return true; } #endif diff --git a/unrar_sys/vendor/unrar/filcreat.hpp b/unrar_sys/vendor/unrar/filcreat.hpp index 456a4a4..ad95fee 100644 --- a/unrar_sys/vendor/unrar/filcreat.hpp +++ b/unrar_sys/vendor/unrar/filcreat.hpp @@ -1,14 +1,12 @@ #ifndef _RAR_FILECREATE_ #define _RAR_FILECREATE_ -bool FileCreate(CommandData *Cmd,File *NewFile,wchar *Name,size_t MaxNameSize, +bool FileCreate(CommandData *Cmd,File *NewFile,std::wstring &Name, bool *UserReject,int64 FileSize=INT64NDF, RarTime *FileTime=NULL,bool WriteOnly=false); -bool GetAutoRenamedName(wchar *Name,size_t MaxNameSize); - #if defined(_WIN_ALL) -bool UpdateExistingShortName(const wchar *Name); +bool UpdateExistingShortName(const std::wstring &Name); #endif #endif diff --git a/unrar_sys/vendor/unrar/file.cpp b/unrar_sys/vendor/unrar/file.cpp index 7bf60fd..2d0d785 100644 --- a/unrar_sys/vendor/unrar/file.cpp +++ b/unrar_sys/vendor/unrar/file.cpp @@ -3,7 +3,6 @@ File::File() { hFile=FILE_BAD_HANDLE; - *FileName=0; NewFile=false; LastWrite=false; HandleType=FILE_HANDLENORMAL; @@ -40,12 +39,12 @@ void File::operator = (File &SrcFile) LastWrite=SrcFile.LastWrite; HandleType=SrcFile.HandleType; TruncatedAfterReadError=SrcFile.TruncatedAfterReadError; - wcsncpyz(FileName,SrcFile.FileName,ASIZE(FileName)); + FileName=SrcFile.FileName; SrcFile.SkipClose=true; } -bool File::Open(const wchar *Name,uint Mode) +bool File::Open(const std::wstring &Name,uint Mode) { ErrorType=FILE_SUCCESS; FileHandle hNewFile; @@ -63,17 +62,17 @@ bool File::Open(const wchar *Name,uint Mode) FindData FD; if (PreserveAtime) Access|=FILE_WRITE_ATTRIBUTES; // Needed to preserve atime. - hNewFile=CreateFile(Name,Access,ShareMode,NULL,OPEN_EXISTING,Flags,NULL); + hNewFile=CreateFile(Name.c_str(),Access,ShareMode,NULL,OPEN_EXISTING,Flags,NULL); DWORD LastError; if (hNewFile==FILE_BAD_HANDLE) { LastError=GetLastError(); - wchar LongName[NM]; - if (GetWinLongPath(Name,LongName,ASIZE(LongName))) + std::wstring LongName; + if (GetWinLongPath(Name,LongName)) { - hNewFile=CreateFile(LongName,Access,ShareMode,NULL,OPEN_EXISTING,Flags,NULL); + hNewFile=CreateFile(LongName.c_str(),Access,ShareMode,NULL,OPEN_EXISTING,Flags,NULL); // For archive names longer than 260 characters first CreateFile // (without \\?\) fails and sets LastError to 3 (access denied). @@ -112,10 +111,10 @@ bool File::Open(const wchar *Name,uint Mode) if (PreserveAtime) flags|=O_NOATIME; #endif - char NameA[NM]; - WideToChar(Name,NameA,ASIZE(NameA)); + std::string NameA; + WideToChar(Name,NameA); - int handle=open(NameA,flags); + int handle=open(NameA.c_str(),flags); #ifdef LOCK_EX #ifdef _OSF_SOURCE @@ -148,7 +147,7 @@ bool File::Open(const wchar *Name,uint Mode) if (Success) { hFile=hNewFile; - wcsncpyz(FileName,Name,ASIZE(FileName)); + FileName=Name; TruncatedAfterReadError=false; } return Success; @@ -156,7 +155,7 @@ bool File::Open(const wchar *Name,uint Mode) #if !defined(SFX_MODULE) -void File::TOpen(const wchar *Name) +void File::TOpen(const std::wstring &Name) { if (!WOpen(Name)) ErrHandler.Exit(RARX_OPEN); @@ -164,7 +163,7 @@ void File::TOpen(const wchar *Name) #endif -bool File::WOpen(const wchar *Name) +bool File::WOpen(const std::wstring &Name) { if (Open(Name)) return true; @@ -173,7 +172,7 @@ bool File::WOpen(const wchar *Name) } -bool File::Create(const wchar *Name,uint Mode) +bool File::Create(const std::wstring &Name,uint Mode) { // OpenIndiana based NAS and CIFS shares fail to set the file time if file // was created in read+write mode and some data was written and not flushed @@ -188,40 +187,40 @@ bool File::Create(const wchar *Name,uint Mode) // Windows automatically removes dots and spaces in the end of file name, // So we detect such names and process them with \\?\ prefix. - wchar *LastChar=PointToLastChar(Name); - bool Special=*LastChar=='.' || *LastChar==' '; + wchar LastChar=GetLastChar(Name); + bool Special=LastChar=='.' || LastChar==' '; if (Special && (Mode & FMF_STANDARDNAMES)==0) hFile=FILE_BAD_HANDLE; else - hFile=CreateFile(Name,Access,ShareMode,NULL,CREATE_ALWAYS,0,NULL); + hFile=CreateFile(Name.c_str(),Access,ShareMode,NULL,CREATE_ALWAYS,0,NULL); if (hFile==FILE_BAD_HANDLE) { - wchar LongName[NM]; - if (GetWinLongPath(Name,LongName,ASIZE(LongName))) - hFile=CreateFile(LongName,Access,ShareMode,NULL,CREATE_ALWAYS,0,NULL); + std::wstring LongName; + if (GetWinLongPath(Name,LongName)) + hFile=CreateFile(LongName.c_str(),Access,ShareMode,NULL,CREATE_ALWAYS,0,NULL); } #else - char NameA[NM]; - WideToChar(Name,NameA,ASIZE(NameA)); #ifdef FILE_USE_OPEN - hFile=open(NameA,(O_CREAT|O_TRUNC) | (WriteMode ? O_WRONLY : O_RDWR),0666); + std::string NameA; + WideToChar(Name,NameA); + hFile=open(NameA.c_str(),(O_CREAT|O_TRUNC) | (WriteMode ? O_WRONLY : O_RDWR),0666); #else - hFile=fopen(NameA,WriteMode ? WRITEBINARY:CREATEBINARY); + hFile=fopen(NameA.c_str(),WriteMode ? WRITEBINARY:CREATEBINARY); #endif #endif NewFile=true; HandleType=FILE_HANDLENORMAL; SkipClose=false; - wcsncpyz(FileName,Name,ASIZE(FileName)); + FileName=Name; return hFile!=FILE_BAD_HANDLE; } #if !defined(SFX_MODULE) -void File::TCreate(const wchar *Name,uint Mode) +void File::TCreate(const std::wstring &Name,uint Mode) { if (!WCreate(Name,Mode)) ErrHandler.Exit(RARX_FATAL); @@ -229,7 +228,7 @@ void File::TCreate(const wchar *Name,uint Mode) #endif -bool File::WCreate(const wchar *Name,uint Mode) +bool File::WCreate(const std::wstring &Name,uint Mode) { if (Create(Name,Mode)) return true; @@ -250,7 +249,7 @@ bool File::Close() // We use the standard system handle for stdout in Windows // and it must not be closed here. if (HandleType==FILE_HANDLENORMAL) - Success=CloseHandle(hFile)==TRUE; + Success=CloseHandle(hFile)!=FALSE; #else #ifdef FILE_USE_OPEN Success=close(hFile)!=-1; @@ -280,16 +279,16 @@ bool File::Delete() } -bool File::Rename(const wchar *NewName) +bool File::Rename(const std::wstring &NewName) { // No need to rename if names are already same. - bool Success=wcscmp(FileName,NewName)==0; + bool Success=(NewName==FileName); if (!Success) Success=RenameFile(FileName,NewName); if (Success) - wcsncpyz(FileName,NewName,ASIZE(FileName)); + FileName=NewName; return Success; } @@ -327,13 +326,13 @@ bool File::Write(const void *Data,size_t Size) const size_t MaxSize=0x4000; for (size_t I=0;IIsSet(); bool seta=fta!=NULL && fta->IsSet(); if (setm || seta) { - char NameA[NM]; - WideToChar(Name,NameA,ASIZE(NameA)); + std::string NameA; + WideToChar(Name,NameA); #ifdef UNIX_TIME_NS timespec times[2]; @@ -720,7 +719,7 @@ void File::SetCloseFileTimeByName(const wchar *Name,RarTime *ftm,RarTime *fta) times[0].tv_nsec=seta ? long(fta->GetUnixNS()%1000000000) : UTIME_NOW; times[1].tv_sec=setm ? ftm->GetUnix() : 0; times[1].tv_nsec=setm ? long(ftm->GetUnixNS()%1000000000) : UTIME_NOW; - utimensat(AT_FDCWD,NameA,times,0); + utimensat(AT_FDCWD,NameA.c_str(),times,0); #else utimbuf ut; if (setm) @@ -731,7 +730,7 @@ void File::SetCloseFileTimeByName(const wchar *Name,RarTime *ftm,RarTime *fta) ut.actime=fta->GetUnix(); else ut.actime=ut.modtime; // Need to set something, cannot left it 0. - utime(NameA,&ut); + utime(NameA.c_str(),&ut); #endif } #endif @@ -802,15 +801,15 @@ bool File::IsDevice() #ifndef SFX_MODULE int64 File::Copy(File &Dest,int64 Length) { - Array Buffer(File::CopyBufferSize()); + std::vector Buffer(File::CopyBufferSize()); int64 CopySize=0; bool CopyAll=(Length==INT64NDF); while (CopyAll || Length>0) { Wait(); - size_t SizeToRead=(!CopyAll && Length<(int64)Buffer.Size()) ? (size_t)Length:Buffer.Size(); - byte *Buf=&Buffer[0]; + size_t SizeToRead=(!CopyAll && Length<(int64)Buffer.size()) ? (size_t)Length:Buffer.size(); + byte *Buf=Buffer.data(); int ReadSize=Read(Buf,SizeToRead); if (ReadSize==0) break; diff --git a/unrar_sys/vendor/unrar/file.hpp b/unrar_sys/vendor/unrar/file.hpp index 5f55de9..a95cc39 100644 --- a/unrar_sys/vendor/unrar/file.hpp +++ b/unrar_sys/vendor/unrar/file.hpp @@ -83,12 +83,9 @@ class File protected: bool OpenShared; // Set by 'Archive' class. public: - wchar FileName[NM]; + std::wstring FileName; FILE_ERRORTYPE ErrorType; - - byte *SeekBuf; // To read instead of seek for stdin files. - static const size_t SeekBufSize=0x10000; public: File(); virtual ~File(); @@ -96,15 +93,15 @@ class File // Several functions below are 'virtual', because they are redefined // by Archive for QOpen and by MultiFile for split files in WinRAR. - virtual bool Open(const wchar *Name,uint Mode=FMF_READ); - void TOpen(const wchar *Name); - bool WOpen(const wchar *Name); - bool Create(const wchar *Name,uint Mode=FMF_UPDATE|FMF_SHAREREAD); - void TCreate(const wchar *Name,uint Mode=FMF_UPDATE|FMF_SHAREREAD); - bool WCreate(const wchar *Name,uint Mode=FMF_UPDATE|FMF_SHAREREAD); + virtual bool Open(const std::wstring &Name,uint Mode=FMF_READ); + void TOpen(const std::wstring &Name); + bool WOpen(const std::wstring &Name); + bool Create(const std::wstring &Name,uint Mode=FMF_UPDATE|FMF_SHAREREAD); + void TCreate(const std::wstring &Name,uint Mode=FMF_UPDATE|FMF_SHAREREAD); + bool WCreate(const std::wstring &Name,uint Mode=FMF_UPDATE|FMF_SHAREREAD); virtual bool Close(); // 'virtual' for MultiFile class. bool Delete(); - bool Rename(const wchar *NewName); + bool Rename(const std::wstring &NewName); bool Write(const void *Data,size_t Size); virtual int Read(void *Data,size_t Size); int DirectRead(void *Data,size_t Size); @@ -118,13 +115,13 @@ class File void Flush(); void SetOpenFileTime(RarTime *ftm,RarTime *ftc=NULL,RarTime *fta=NULL); void SetCloseFileTime(RarTime *ftm,RarTime *fta=NULL); - static void SetCloseFileTimeByName(const wchar *Name,RarTime *ftm,RarTime *fta); + static void SetCloseFileTimeByName(const std::wstring &Name,RarTime *ftm,RarTime *fta); #ifdef _UNIX static void StatToRarTime(struct stat &st,RarTime *ftm,RarTime *ftc,RarTime *fta); #endif void GetOpenFileTime(RarTime *ftm,RarTime *ftc=NULL,RarTime *fta=NULL); virtual bool IsOpened() {return hFile!=FILE_BAD_HANDLE;} // 'virtual' for MultiFile class. - int64 FileLength(); + virtual int64 FileLength(); // 'virtual' for MultiFile class. void SetHandleType(FILE_HANDLETYPE Type) {HandleType=Type;} void SetLineInputMode(bool Mode) {LineInput=Mode;} FILE_HANDLETYPE GetHandleType() {return HandleType;} @@ -151,14 +148,9 @@ class File #endif static size_t CopyBufferSize() { -#ifdef _WIN_ALL - // USB flash performance is poor with 64 KB buffer, 256+ KB resolved it. - // For copying from HDD to same HDD the best performance was with 256 KB - // buffer in XP and with 1 MB buffer in Win10. - return WinNT()==WNT_WXP ? 0x40000:0x100000; -#else - return 0x100000; -#endif + // Values in 0x100000 - 0x400000 range are ok, but multithreaded CRC32 + // seems to benefit from 0x400000, especially on ARM CPUs. + return 0x400000; } }; diff --git a/unrar_sys/vendor/unrar/filefn.cpp b/unrar_sys/vendor/unrar/filefn.cpp index 4bcc8bf..14316ae 100644 --- a/unrar_sys/vendor/unrar/filefn.cpp +++ b/unrar_sys/vendor/unrar/filefn.cpp @@ -1,18 +1,18 @@ #include "rar.hpp" -MKDIR_CODE MakeDir(const wchar *Name,bool SetAttr,uint Attr) +MKDIR_CODE MakeDir(const std::wstring &Name,bool SetAttr,uint Attr) { #ifdef _WIN_ALL // Windows automatically removes dots and spaces in the end of directory // name. So we detect such names and process them with \\?\ prefix. - wchar *LastChar=PointToLastChar(Name); - bool Special=*LastChar=='.' || *LastChar==' '; - BOOL RetCode=Special ? FALSE : CreateDirectory(Name,NULL); + wchar LastChar=GetLastChar(Name); + bool Special=LastChar=='.' || LastChar==' '; + BOOL RetCode=Special ? FALSE : CreateDirectory(Name.c_str(),NULL); if (RetCode==0 && !FileExist(Name)) { - wchar LongName[NM]; - if (GetWinLongPath(Name,LongName,ASIZE(LongName))) - RetCode=CreateDirectory(LongName,NULL); + std::wstring LongName; + if (GetWinLongPath(Name,LongName)) + RetCode=CreateDirectory(LongName.c_str(),NULL); } if (RetCode!=0) // Non-zero return code means success for CreateDirectory. { @@ -25,10 +25,10 @@ MKDIR_CODE MakeDir(const wchar *Name,bool SetAttr,uint Attr) return MKDIR_BADPATH; return MKDIR_ERROR; #elif defined(_UNIX) - char NameA[NM]; - WideToChar(Name,NameA,ASIZE(NameA)); + std::string NameA; + WideToChar(Name,NameA); mode_t uattr=SetAttr ? (mode_t)Attr:0777; - int ErrCode=mkdir(NameA,uattr); + int ErrCode=mkdir(NameA.c_str(),uattr); if (ErrCode==-1) return errno==ENOENT ? MKDIR_BADPATH:MKDIR_ERROR; return MKDIR_SUCCESS; @@ -38,12 +38,19 @@ MKDIR_CODE MakeDir(const wchar *Name,bool SetAttr,uint Attr) } -bool CreatePath(const wchar *Path,bool SkipLastName,bool Silent) +// Simplified version of MakeDir(). +bool CreateDir(const std::wstring &Name) { - if (Path==NULL || *Path==0) + return MakeDir(Name,false,0)==MKDIR_SUCCESS; +} + + +bool CreatePath(const std::wstring &Path,bool SkipLastName,bool Silent) +{ + if (Path.empty()) return false; -#if defined(_WIN_ALL) || defined(_EMX) +#ifdef _WIN_ALL uint DirAttr=0; #else uint DirAttr=0777; @@ -51,42 +58,36 @@ bool CreatePath(const wchar *Path,bool SkipLastName,bool Silent) bool Success=true; - for (const wchar *s=Path;*s!=0;s++) + for (size_t I=0;I=ASIZE(DirName)) - break; - // Process all kinds of path separators, so user can enter Unix style - // path in Windows or Windows in Unix. s>Path check avoids attempting + // path in Windows or Windows in Unix. I>0 check avoids attempting // creating an empty directory for paths starting from path separator. - if (IsPathDiv(*s) && s>Path) + if (IsPathDiv(Path[I]) && I>0) { #ifdef _WIN_ALL // We must not attempt to create "D:" directory, because first // CreateDirectory will fail, so we'll use \\?\D:, which forces Wine // to create "D:" directory. - if (s==Path+2 && Path[1]==':') + if (I==2 && Path[1]==':') continue; #endif - wcsncpy(DirName,Path,s-Path); - DirName[s-Path]=0; - + std::wstring DirName=Path.substr(0,I); Success=MakeDir(DirName,true,DirAttr)==MKDIR_SUCCESS; if (Success && !Silent) { - mprintf(St(MCreatDir),DirName); + mprintf(St(MCreatDir),DirName.c_str()); mprintf(L" %s",St(MOk)); } } } - if (!SkipLastName && !IsPathDiv(*PointToLastChar(Path))) + if (!SkipLastName && !IsPathDiv(GetLastChar(Path))) Success=MakeDir(Path,true,DirAttr)==MKDIR_SUCCESS; return Success; } -void SetDirTime(const wchar *Name,RarTime *ftm,RarTime *ftc,RarTime *fta) +void SetDirTime(const std::wstring &Name,RarTime *ftm,RarTime *ftc,RarTime *fta) { #if defined(_WIN_ALL) bool sm=ftm!=NULL && ftm->IsSet(); @@ -98,13 +99,13 @@ void SetDirTime(const wchar *Name,RarTime *ftm,RarTime *ftc,RarTime *fta) if (ResetAttr) SetFileAttr(Name,0); - HANDLE hFile=CreateFile(Name,GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE, + HANDLE hFile=CreateFile(Name.c_str(),GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,OPEN_EXISTING,FILE_FLAG_BACKUP_SEMANTICS,NULL); if (hFile==INVALID_HANDLE_VALUE) { - wchar LongName[NM]; - if (GetWinLongPath(Name,LongName,ASIZE(LongName))) - hFile=CreateFile(LongName,GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE, + std::wstring LongName; + if (GetWinLongPath(Name,LongName)) + hFile=CreateFile(LongName.c_str(),GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,OPEN_EXISTING,FILE_FLAG_BACKUP_SEMANTICS,NULL); } @@ -122,18 +123,18 @@ void SetDirTime(const wchar *Name,RarTime *ftm,RarTime *ftc,RarTime *fta) if (ResetAttr) SetFileAttr(Name,DirAttr); #endif -#if defined(_UNIX) || defined(_EMX) +#ifdef _UNIX File::SetCloseFileTimeByName(Name,ftm,fta); #endif } -bool IsRemovable(const wchar *Name) +bool IsRemovable(const std::wstring &Name) { #if defined(_WIN_ALL) - wchar Root[NM]; - GetPathRoot(Name,Root,ASIZE(Root)); - int Type=GetDriveType(*Root!=0 ? Root:NULL); + std::wstring Root; + GetPathRoot(Name,Root); + int Type=GetDriveType(Root.empty() ? nullptr : Root.c_str()); return Type==DRIVE_REMOVABLE || Type==DRIVE_CDROM; #else return false; @@ -142,25 +143,25 @@ bool IsRemovable(const wchar *Name) #ifndef SFX_MODULE -int64 GetFreeDisk(const wchar *Name) +int64 GetFreeDisk(const std::wstring &Name) { #ifdef _WIN_ALL - wchar Root[NM]; - GetFilePath(Name,Root,ASIZE(Root)); + std::wstring Root; + GetPathWithSep(Name,Root); ULARGE_INTEGER uiTotalSize,uiTotalFree,uiUserFree; uiUserFree.u.LowPart=uiUserFree.u.HighPart=0; - if (GetDiskFreeSpaceEx(*Root!=0 ? Root:NULL,&uiUserFree,&uiTotalSize,&uiTotalFree) && + if (GetDiskFreeSpaceEx(Root.empty() ? NULL:Root.c_str(),&uiUserFree,&uiTotalSize,&uiTotalFree) && uiUserFree.u.HighPart<=uiTotalFree.u.HighPart) return INT32TO64(uiUserFree.u.HighPart,uiUserFree.u.LowPart); return 0; #elif defined(_UNIX) - wchar Root[NM]; - GetFilePath(Name,Root,ASIZE(Root)); - char RootA[NM]; - WideToChar(Root,RootA,ASIZE(RootA)); + std::wstring Root; + GetPathWithSep(Name,Root); + std::string RootA; + WideToChar(Root,RootA); struct statvfs sfs; - if (statvfs(*RootA!=0 ? RootA:".",&sfs)!=0) + if (statvfs(RootA.empty() ? ".":RootA.c_str(),&sfs)!=0) return 0; int64 FreeSize=sfs.f_bsize; FreeSize=FreeSize*sfs.f_bavail; @@ -175,26 +176,27 @@ int64 GetFreeDisk(const wchar *Name) #if defined(_WIN_ALL) && !defined(SFX_MODULE) && !defined(SILENT) // Return 'true' for FAT and FAT32, so we can adjust the maximum supported // file size to 4 GB for these file systems. -bool IsFAT(const wchar *Name) +bool IsFAT(const std::wstring &Name) { - wchar Root[NM]; - GetPathRoot(Name,Root,ASIZE(Root)); + std::wstring Root; + GetPathRoot(Name,Root); wchar FileSystem[MAX_PATH+1]; - if (GetVolumeInformation(Root,NULL,0,NULL,NULL,NULL,FileSystem,ASIZE(FileSystem))) + // Root can be empty, when we create volumes with -v in the current folder. + if (GetVolumeInformation(Root.empty() ? NULL:Root.c_str(),NULL,0,NULL,NULL,NULL,FileSystem,ASIZE(FileSystem))) return wcscmp(FileSystem,L"FAT")==0 || wcscmp(FileSystem,L"FAT32")==0; return false; } #endif -bool FileExist(const wchar *Name) +bool FileExist(const std::wstring &Name) { #ifdef _WIN_ALL return GetFileAttr(Name)!=0xffffffff; #elif defined(ENABLE_ACCESS) - char NameA[NM]; - WideToChar(Name,NameA,ASIZE(NameA)); - return access(NameA,0)==0; + std::string NameA; + WideToChar(Name,NameA); + return access(NameA.c_str(),0)==0; #else FindData FD; return FindFile::FastFind(Name,&FD); @@ -202,7 +204,7 @@ bool FileExist(const wchar *Name) } -bool WildFileExist(const wchar *Name) +bool WildFileExist(const std::wstring &Name) { if (IsWildcard(Name)) { @@ -261,66 +263,63 @@ bool IsDeleteAllowed(uint FileAttr) } -void PrepareToDelete(const wchar *Name) +void PrepareToDelete(const std::wstring &Name) { -#if defined(_WIN_ALL) || defined(_EMX) +#ifdef _WIN_ALL SetFileAttr(Name,0); #endif #ifdef _UNIX - if (Name!=NULL) - { - char NameA[NM]; - WideToChar(Name,NameA,ASIZE(NameA)); - chmod(NameA,S_IRUSR|S_IWUSR|S_IXUSR); - } + std::string NameA; + WideToChar(Name,NameA); + chmod(NameA.c_str(),S_IRUSR|S_IWUSR|S_IXUSR); #endif } -uint GetFileAttr(const wchar *Name) +uint GetFileAttr(const std::wstring &Name) { #ifdef _WIN_ALL - DWORD Attr=GetFileAttributes(Name); + DWORD Attr=GetFileAttributes(Name.c_str()); if (Attr==0xffffffff) { - wchar LongName[NM]; - if (GetWinLongPath(Name,LongName,ASIZE(LongName))) - Attr=GetFileAttributes(LongName); + std::wstring LongName; + if (GetWinLongPath(Name,LongName)) + Attr=GetFileAttributes(LongName.c_str()); } return Attr; #else - char NameA[NM]; - WideToChar(Name,NameA,ASIZE(NameA)); + std::string NameA; + WideToChar(Name,NameA); struct stat st; - if (stat(NameA,&st)!=0) + if (stat(NameA.c_str(),&st)!=0) return 0; return st.st_mode; #endif } -bool SetFileAttr(const wchar *Name,uint Attr) +bool SetFileAttr(const std::wstring &Name,uint Attr) { #ifdef _WIN_ALL - bool Success=SetFileAttributes(Name,Attr)!=0; + bool Success=SetFileAttributes(Name.c_str(),Attr)!=0; if (!Success) { - wchar LongName[NM]; - if (GetWinLongPath(Name,LongName,ASIZE(LongName))) - Success=SetFileAttributes(LongName,Attr)!=0; + std::wstring LongName; + if (GetWinLongPath(Name,LongName)) + Success=SetFileAttributes(LongName.c_str(),Attr)!=0; } return Success; #elif defined(_UNIX) - char NameA[NM]; - WideToChar(Name,NameA,ASIZE(NameA)); - return chmod(NameA,(mode_t)Attr)==0; + std::string NameA; + WideToChar(Name,NameA); + return chmod(NameA.c_str(),(mode_t)Attr)==0; #else return false; #endif } -wchar *MkTemp(wchar *Name,size_t MaxSize) +wchar* MkTemp(wchar *Name,size_t MaxSize) { size_t Length=wcslen(Name); @@ -344,7 +343,11 @@ wchar *MkTemp(wchar *Name,size_t MaxSize) { uint Ext=Random%50000+Attempt; wchar RndText[50]; - swprintf(RndText,ASIZE(RndText),L"%u.%03u",PID,Ext); + // User asked to specify the single extension for all temporary files, + // so it can be added to server ransomware protection exceptions. + // He wrote, this protection blocks temporary files when adding + // a file to RAR archive with drag and drop. + swprintf(RndText,ASIZE(RndText),L"%u.%03u.rartemp",PID,Ext); if (Length+wcslen(RndText)>=MaxSize || Attempt==1000) return NULL; wcsncpyz(Name+Length,RndText,MaxSize-Length); @@ -355,6 +358,40 @@ wchar *MkTemp(wchar *Name,size_t MaxSize) } +bool MkTemp(std::wstring &Name) +{ + RarTime CurTime; + CurTime.SetCurrentTime(); + + // We cannot use CurTime.GetWin() as is, because its lowest bits can + // have low informational value, like being a zero or few fixed numbers. + uint Random=(uint)(CurTime.GetWin()/100000); + + // Using PID we guarantee that different RAR copies use different temp names + // even if started in exactly the same time. + uint PID=0; +#ifdef _WIN_ALL + PID=(uint)GetCurrentProcessId(); +#elif defined(_UNIX) + PID=(uint)getpid(); +#endif + + for (uint Attempt=0;;Attempt++) + { + uint Ext=Random%50000+Attempt; + if (Attempt==1000) + return false; + std::wstring NewName=Name + std::to_wstring(PID) + L"." + std::to_wstring(Ext) + L".rartemp"; + if (!FileExist(NewName)) + { + Name=NewName; + break; + } + } + return true; +} + + #if !defined(SFX_MODULE) void CalcFileSum(File *SrcFile,uint *CRC32,byte *Blake2,uint Threads,int64 Size,uint Flags) { @@ -370,8 +407,7 @@ void CalcFileSum(File *SrcFile,uint *CRC32,byte *Blake2,uint Threads,int64 Size, SrcFile->Seek(0,SEEK_SET); const size_t BufSize=0x100000; - Array Data(BufSize); - + std::vector Data(BufSize); DataHash HashCRC,HashBlake2; HashCRC.Init(HASH_CRC32,Threads); @@ -386,7 +422,7 @@ void CalcFileSum(File *SrcFile,uint *CRC32,byte *Blake2,uint Threads,int64 Size, SizeToRead=BufSize; // Then always attempt to read the entire buffer. else SizeToRead=(size_t)Min((int64)BufSize,Size); - int ReadSize=SrcFile->Read(&Data[0],SizeToRead); + int ReadSize=SrcFile->Read(Data.data(),SizeToRead); if (ReadSize==0) break; TotalRead+=ReadSize; @@ -410,9 +446,9 @@ void CalcFileSum(File *SrcFile,uint *CRC32,byte *Blake2,uint Threads,int64 Size, } if (CRC32!=NULL) - HashCRC.Update(&Data[0],ReadSize); + HashCRC.Update(Data.data(),ReadSize); if (Blake2!=NULL) - HashBlake2.Update(&Data[0],ReadSize); + HashBlake2.Update(Data.data(),ReadSize); if (Size!=INT64NDF) Size-=ReadSize; @@ -434,79 +470,78 @@ void CalcFileSum(File *SrcFile,uint *CRC32,byte *Blake2,uint Threads,int64 Size, #endif -bool RenameFile(const wchar *SrcName,const wchar *DestName) +bool RenameFile(const std::wstring &SrcName,const std::wstring &DestName) { #ifdef _WIN_ALL - bool Success=MoveFile(SrcName,DestName)!=0; + bool Success=MoveFile(SrcName.c_str(),DestName.c_str())!=0; if (!Success) { - wchar LongName1[NM],LongName2[NM]; - if (GetWinLongPath(SrcName,LongName1,ASIZE(LongName1)) && - GetWinLongPath(DestName,LongName2,ASIZE(LongName2))) - Success=MoveFile(LongName1,LongName2)!=0; + std::wstring LongName1,LongName2; + if (GetWinLongPath(SrcName,LongName1) && GetWinLongPath(DestName,LongName2)) + Success=MoveFile(LongName1.c_str(),LongName2.c_str())!=0; } return Success; #else - char SrcNameA[NM],DestNameA[NM]; - WideToChar(SrcName,SrcNameA,ASIZE(SrcNameA)); - WideToChar(DestName,DestNameA,ASIZE(DestNameA)); - bool Success=rename(SrcNameA,DestNameA)==0; + std::string SrcNameA,DestNameA; + WideToChar(SrcName,SrcNameA); + WideToChar(DestName,DestNameA); + bool Success=rename(SrcNameA.c_str(),DestNameA.c_str())==0; return Success; #endif } -bool DelFile(const wchar *Name) +bool DelFile(const std::wstring &Name) { #ifdef _WIN_ALL - bool Success=DeleteFile(Name)!=0; + bool Success=DeleteFile(Name.c_str())!=0; if (!Success) { - wchar LongName[NM]; - if (GetWinLongPath(Name,LongName,ASIZE(LongName))) - Success=DeleteFile(LongName)!=0; + std::wstring LongName; + if (GetWinLongPath(Name,LongName)) + Success=DeleteFile(LongName.c_str())!=0; } return Success; #else - char NameA[NM]; - WideToChar(Name,NameA,ASIZE(NameA)); - bool Success=remove(NameA)==0; + std::string NameA; + WideToChar(Name,NameA); + bool Success=remove(NameA.c_str())==0; return Success; #endif } -bool DelDir(const wchar *Name) +bool DelDir(const std::wstring &Name) { #ifdef _WIN_ALL - bool Success=RemoveDirectory(Name)!=0; + bool Success=RemoveDirectory(Name.c_str())!=0; if (!Success) { - wchar LongName[NM]; - if (GetWinLongPath(Name,LongName,ASIZE(LongName))) - Success=RemoveDirectory(LongName)!=0; + std::wstring LongName; + if (GetWinLongPath(Name,LongName)) + Success=RemoveDirectory(LongName.c_str())!=0; } return Success; #else - char NameA[NM]; - WideToChar(Name,NameA,ASIZE(NameA)); - bool Success=rmdir(NameA)==0; + std::string NameA; + WideToChar(Name,NameA); + bool Success=rmdir(NameA.c_str())==0; return Success; #endif } #if defined(_WIN_ALL) && !defined(SFX_MODULE) -bool SetFileCompression(const wchar *Name,bool State) +bool SetFileCompression(const std::wstring &Name,bool State) { - HANDLE hFile=CreateFile(Name,FILE_READ_DATA|FILE_WRITE_DATA, + HANDLE hFile=CreateFile(Name.c_str(),FILE_READ_DATA|FILE_WRITE_DATA, FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS|FILE_FLAG_SEQUENTIAL_SCAN,NULL); if (hFile==INVALID_HANDLE_VALUE) { - wchar LongName[NM]; - if (GetWinLongPath(Name,LongName,ASIZE(LongName))) - hFile=CreateFile(LongName,FILE_READ_DATA|FILE_WRITE_DATA, + std::wstring LongName; + if (GetWinLongPath(Name,LongName)) + hFile=CreateFile(LongName.c_str(),FILE_READ_DATA|FILE_WRITE_DATA, FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS|FILE_FLAG_SEQUENTIAL_SCAN,NULL); } @@ -521,11 +556,11 @@ bool SetFileCompression(const wchar *Name,bool State) } -void ResetFileCache(const wchar *Name) +void ResetFileCache(const std::wstring &Name) { // To reset file cache in Windows it is enough to open it with // FILE_FLAG_NO_BUFFERING and then close it. - HANDLE hSrc=CreateFile(Name,GENERIC_READ, + HANDLE hSrc=CreateFile(Name.c_str(),GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,OPEN_EXISTING,FILE_FLAG_NO_BUFFERING,NULL); if (hSrc!=INVALID_HANDLE_VALUE) @@ -546,7 +581,7 @@ void ResetFileCache(const wchar *Name) // Delete symbolic links in file path, if any, and replace them by directories. // Prevents extracting files outside of destination folder with symlink chains. -bool LinksToDirs(const wchar *SrcName,const wchar *SkipPart,std::wstring &LastChecked) +bool LinksToDirs(const std::wstring &SrcName,const std::wstring &SkipPart,std::wstring &LastChecked) { // Unlike Unix, Windows doesn't expand lnk1 in symlink targets like // "lnk1/../dir", but converts the path to "dir". In Unix we need to call @@ -563,43 +598,47 @@ bool LinksToDirs(const wchar *SrcName,const wchar *SkipPart,std::wstring &LastCh // extracting a symlink with ".." in target. So we enabled it for Windows // as well for extra safety. //#ifdef _UNIX - wchar Path[NM]; - if (wcslen(SrcName)>=ASIZE(Path)) - return false; // It should not be that long, skip. - wcsncpyz(Path,SrcName,ASIZE(Path)); + std::wstring Path=SrcName; - size_t SkipLength=wcslen(SkipPart); + size_t SkipLength=SkipPart.size(); - if (SkipLength>0 && wcsncmp(Path,SkipPart,SkipLength)!=0) + if (SkipLength>0 && Path.rfind(SkipPart,0)!=0) SkipLength=0; // Parameter validation, not really needed now. // Do not check parts already checked in previous path to improve performance. - for (uint I=0;Path[I]!=0 && ISkipLength) SkipLength=I; - wchar *Name=Path; - if (SkipLength>0) - { - // Avoid converting symlinks in destination path part specified by user. - Name+=SkipLength; - while (IsPathDiv(*Name)) - Name++; - } + // Avoid converting symlinks in destination path part specified by user. + while (SkipLengthName;s--) - if (IsPathDiv(*s)) - { - *s=0; - FindData FD; - if (FindFile::FastFind(Path,&FD,true) && FD.IsLink) + if (Path.size()>0) + for (size_t I=Path.size()-1;I>SkipLength;I--) + if (IsPathDiv(Path[I])) + { + Path.erase(I); + FindData FD; + if (FindFile::FastFind(Path,&FD,true) && FD.IsLink) + { #ifdef _WIN_ALL - if (!DelDir(Path)) + // Normally Windows symlinks to directory look like a directory + // and are deleted with DelDir(). It is possible to create + // a file-like symlink pointing at directory, which can be deleted + // only with && DelFile, but such symlink isn't really functional. + // Here we prefer to fail deleting such symlink and skip extracting + // a file. + if (!DelDir(Path)) #else - if (!DelFile(Path)) + if (!DelFile(Path)) #endif - return false; // Couldn't delete the symlink to replace it with directory. - } + { + ErrHandler.CreateErrorMsg(SrcName); // Extraction command will skip this file or directory. + return false; // Couldn't delete the symlink to replace it with directory. + } + } + } LastChecked=SrcName; //#endif return true; diff --git a/unrar_sys/vendor/unrar/filefn.hpp b/unrar_sys/vendor/unrar/filefn.hpp index d73d55e..4e06197 100644 --- a/unrar_sys/vendor/unrar/filefn.hpp +++ b/unrar_sys/vendor/unrar/filefn.hpp @@ -3,49 +3,52 @@ enum MKDIR_CODE {MKDIR_SUCCESS,MKDIR_ERROR,MKDIR_BADPATH}; -MKDIR_CODE MakeDir(const wchar *Name,bool SetAttr,uint Attr); -bool CreatePath(const wchar *Path,bool SkipLastName,bool Silent); -void SetDirTime(const wchar *Name,RarTime *ftm,RarTime *ftc,RarTime *fta); -bool IsRemovable(const wchar *Name); +MKDIR_CODE MakeDir(const std::wstring &Name,bool SetAttr,uint Attr); +bool CreateDir(const std::wstring &Name); +bool CreatePath(const std::wstring &Path,bool SkipLastName,bool Silent); +void SetDirTime(const std::wstring &Name,RarTime *ftm,RarTime *ftc,RarTime *fta); +bool IsRemovable(const std::wstring &Name); #ifndef SFX_MODULE -int64 GetFreeDisk(const wchar *Name); +int64 GetFreeDisk(const std::wstring &Name); #endif #if defined(_WIN_ALL) && !defined(SFX_MODULE) && !defined(SILENT) -bool IsFAT(const wchar *Root); +bool IsFAT(const std::wstring &Root); #endif -bool FileExist(const wchar *Name); -bool WildFileExist(const wchar *Name); +bool FileExist(const std::wstring &Name); +bool WildFileExist(const std::wstring &Name); bool IsDir(uint Attr); bool IsUnreadable(uint Attr); bool IsLink(uint Attr); -void SetSFXMode(const wchar *FileName); -void EraseDiskContents(const wchar *FileName); +void SetSFXMode(const std::wstring &FileName); +void EraseDiskContents(const std::wstring &FileName); bool IsDeleteAllowed(uint FileAttr); -void PrepareToDelete(const wchar *Name); -uint GetFileAttr(const wchar *Name); -bool SetFileAttr(const wchar *Name,uint Attr); +void PrepareToDelete(const std::wstring &Name); +uint GetFileAttr(const std::wstring &Name); +bool SetFileAttr(const std::wstring &Name,uint Attr); wchar* MkTemp(wchar *Name,size_t MaxSize); +bool MkTemp(std::wstring &Name); enum CALCFSUM_FLAGS {CALCFSUM_SHOWTEXT=1,CALCFSUM_SHOWPERCENT=2,CALCFSUM_SHOWPROGRESS=4,CALCFSUM_CURPOS=8}; void CalcFileSum(File *SrcFile,uint *CRC32,byte *Blake2,uint Threads,int64 Size=INT64NDF,uint Flags=0); -bool RenameFile(const wchar *SrcName,const wchar *DestName); -bool DelFile(const wchar *Name); -bool DelDir(const wchar *Name); +bool RenameFile(const std::wstring &SrcName,const std::wstring &DestName); +bool DelFile(const std::wstring &Name); +bool DelDir(const std::wstring &Name); #if defined(_WIN_ALL) && !defined(SFX_MODULE) -bool SetFileCompression(const wchar *Name,bool State); -void ResetFileCache(const wchar *Name); +bool SetFileCompression(const std::wstring &Name,bool State); +void ResetFileCache(const std::wstring &Name); #endif -bool LinksToDirs(const wchar *SrcName,const wchar *SkipPart,std::wstring &LastChecked); +// Keep it here and not in extinfo.cpp, because it is invoked from Zip.SFX too. +bool LinksToDirs(const std::wstring &SrcName,const std::wstring &SkipPart,std::wstring &LastChecked); #endif diff --git a/unrar_sys/vendor/unrar/filestr.cpp b/unrar_sys/vendor/unrar/filestr.cpp index a5d29d7..ec45654 100644 --- a/unrar_sys/vendor/unrar/filestr.cpp +++ b/unrar_sys/vendor/unrar/filestr.cpp @@ -1,7 +1,7 @@ #include "rar.hpp" bool ReadTextFile( - const wchar *Name, + const std::wstring &Name, StringList *List, bool Config, bool AbortOnError, @@ -10,17 +10,15 @@ bool ReadTextFile( bool SkipComments, bool ExpandEnvStr) { - wchar FileName[NM]; - *FileName=0; + std::wstring FileName; - if (Name!=NULL) - if (Config) - GetConfigName(Name,FileName,ASIZE(FileName),true,false); - else - wcsncpyz(FileName,Name,ASIZE(FileName)); + if (Config) + GetConfigName(Name,FileName,true,false); + else + FileName=Name; File SrcFile; - if (*FileName!=0) + if (!FileName.empty()) { bool OpenCode=AbortOnError ? SrcFile.WOpen(FileName):SrcFile.Open(FileName,0); @@ -34,36 +32,36 @@ bool ReadTextFile( else SrcFile.SetHandleType(FILE_HANDLESTD); - uint DataSize=0,ReadSize; + size_t DataSize=0,ReadSize; const int ReadBlock=4096; - Array Data(ReadBlock); + std::vector Data(ReadBlock); while ((ReadSize=SrcFile.Read(&Data[DataSize],ReadBlock))!=0) { DataSize+=ReadSize; - Data.Add(ReadSize); // Always have ReadBlock available for next data. + Data.resize(DataSize+ReadBlock); // Always have ReadBlock available for next data. } // Set to really read size, so we can zero terminate it correctly. - Data.Alloc(DataSize); + Data.resize(DataSize); int LittleEndian=DataSize>=2 && Data[0]==255 && Data[1]==254 ? 1:0; int BigEndian=DataSize>=2 && Data[0]==254 && Data[1]==255 ? 1:0; bool Utf8=DataSize>=3 && Data[0]==0xef && Data[1]==0xbb && Data[2]==0xbf; if (SrcCharset==RCH_DEFAULT) - SrcCharset=DetectTextEncoding(&Data[0],DataSize); + SrcCharset=DetectTextEncoding(Data.data(),DataSize); - Array DataW; + std::vector DataW(ReadBlock); if (SrcCharset==RCH_DEFAULT || SrcCharset==RCH_OEM || SrcCharset==RCH_ANSI) { - Data.Push(0); // Zero terminate. + Data.push_back(0); // Zero terminate. #if defined(_WIN_ALL) if (SrcCharset==RCH_OEM) - OemToCharA((char *)&Data[0],(char *)&Data[0]); + OemToCharA((char *)Data.data(),(char *)Data.data()); #endif - DataW.Alloc(Data.Size()); - CharToWide((char *)&Data[0],&DataW[0],DataW.Size()); + DataW.resize(Data.size()); + CharToWide((char *)Data.data(),DataW.data(),DataW.size()); } if (SrcCharset==RCH_UNICODE) @@ -75,8 +73,8 @@ bool ReadTextFile( LittleEndian=1; } - DataW.Alloc(Data.Size()/2+1); - size_t End=Data.Size() & ~1; // We need even bytes number for UTF-16. + DataW.resize(Data.size()/2+1); + size_t End=Data.size() & ~1; // We need even bytes number for UTF-16. for (size_t I=Start;IAddString(ExpName); + Expanded=true; } #endif if (!Expanded && *CurStr!=0) diff --git a/unrar_sys/vendor/unrar/filestr.hpp b/unrar_sys/vendor/unrar/filestr.hpp index febd0a2..3c28130 100644 --- a/unrar_sys/vendor/unrar/filestr.hpp +++ b/unrar_sys/vendor/unrar/filestr.hpp @@ -2,7 +2,7 @@ #define _RAR_FILESTR_ bool ReadTextFile( - const wchar *Name, + const std::wstring &Name, StringList *List, bool Config, bool AbortOnError=false, diff --git a/unrar_sys/vendor/unrar/find.cpp b/unrar_sys/vendor/unrar/find.cpp index c9f2c57..64d933e 100644 --- a/unrar_sys/vendor/unrar/find.cpp +++ b/unrar_sys/vendor/unrar/find.cpp @@ -2,7 +2,6 @@ FindFile::FindFile() { - *FindMask=0; FirstCall=true; #ifdef _WIN_ALL hFind=INVALID_HANDLE_VALUE; @@ -24,9 +23,9 @@ FindFile::~FindFile() } -void FindFile::SetMask(const wchar *Mask) +void FindFile::SetMask(const std::wstring &Mask) { - wcsncpyz(FindMask,Mask,ASIZE(FindMask)); + FindMask=Mask; FirstCall=true; } @@ -34,7 +33,7 @@ void FindFile::SetMask(const wchar *Mask) bool FindFile::Next(FindData *fd,bool GetSymLink) { fd->Error=false; - if (*FindMask==0) + if (FindMask.empty()) return false; #ifdef _WIN_ALL if (FirstCall) @@ -48,14 +47,14 @@ bool FindFile::Next(FindData *fd,bool GetSymLink) #else if (FirstCall) { - wchar DirName[NM]; - wcsncpyz(DirName,FindMask,ASIZE(DirName)); + std::wstring DirName; + DirName=FindMask; RemoveNameFromPath(DirName); - if (*DirName==0) - wcsncpyz(DirName,L".",ASIZE(DirName)); - char DirNameA[NM]; - WideToChar(DirName,DirNameA,ASIZE(DirNameA)); - if ((dirp=opendir(DirNameA))==NULL) + if (DirName.empty()) + DirName=L"."; + std::string DirNameA; + WideToChar(DirName,DirNameA); + if ((dirp=opendir(DirNameA.c_str()))==NULL) { fd->Error=(errno!=ENOENT); return false; @@ -63,32 +62,31 @@ bool FindFile::Next(FindData *fd,bool GetSymLink) } while (1) { - wchar Name[NM]; + std::wstring Name; struct dirent *ent=readdir(dirp); if (ent==NULL) return false; if (strcmp(ent->d_name,".")==0 || strcmp(ent->d_name,"..")==0) continue; - if (!CharToWide(ent->d_name,Name,ASIZE(Name))) - uiMsg(UIERROR_INVALIDNAME,UINULL,Name); + if (!CharToWide(std::string(ent->d_name),Name)) + uiMsg(UIERROR_INVALIDNAME,L"",Name); if (CmpName(FindMask,Name,MATCH_NAMES)) { - wchar FullName[NM]; - wcsncpyz(FullName,FindMask,ASIZE(FullName)); - *PointToName(FullName)=0; - if (wcslen(FullName)+wcslen(Name)>=ASIZE(FullName)-1) + std::wstring FullName=FindMask; + FullName.erase(GetNamePos(FullName)); + if (FullName.size()+Name.size()>=MAXPATHSIZE) { uiMsg(UIERROR_PATHTOOLONG,FullName,L"",Name); return false; } - wcsncatz(FullName,Name,ASIZE(FullName)); + FullName+=Name; if (!FastFind(FullName,fd,GetSymLink)) { ErrHandler.OpenErrorMsg(FullName); continue; } - wcsncpyz(fd->Name,FullName,ASIZE(fd->Name)); + fd->Name=FullName; break; } } @@ -98,14 +96,14 @@ bool FindFile::Next(FindData *fd,bool GetSymLink) fd->IsLink=IsLink(fd->FileAttr); FirstCall=false; - wchar *NameOnly=PointToName(fd->Name); - if (wcscmp(NameOnly,L".")==0 || wcscmp(NameOnly,L"..")==0) + std::wstring NameOnly=PointToName(fd->Name); + if (NameOnly==L"." || NameOnly==L"..") return Next(fd); return true; } -bool FindFile::FastFind(const wchar *FindMask,FindData *fd,bool GetSymLink) +bool FindFile::FastFind(const std::wstring &FindMask,FindData *fd,bool GetSymLink) { fd->Error=false; #ifndef _UNIX @@ -118,16 +116,16 @@ bool FindFile::FastFind(const wchar *FindMask,FindData *fd,bool GetSymLink) return false; FindClose(hFind); #elif defined(_UNIX) - char FindMaskA[NM]; - WideToChar(FindMask,FindMaskA,ASIZE(FindMaskA)); + std::string FindMaskA; + WideToChar(FindMask,FindMaskA); struct stat st; if (GetSymLink) { #ifdef SAVE_LINKS - if (lstat(FindMaskA,&st)!=0) + if (lstat(FindMaskA.c_str(),&st)!=0) #else - if (stat(FindMaskA,&st)!=0) + if (stat(FindMaskA.c_str(),&st)!=0) #endif { fd->Error=(errno!=ENOENT); @@ -135,7 +133,7 @@ bool FindFile::FastFind(const wchar *FindMask,FindData *fd,bool GetSymLink) } } else - if (stat(FindMaskA,&st)!=0) + if (stat(FindMaskA.c_str(),&st)!=0) { fd->Error=(errno!=ENOENT); return false; @@ -145,7 +143,7 @@ bool FindFile::FastFind(const wchar *FindMask,FindData *fd,bool GetSymLink) File::StatToRarTime(st,&fd->mtime,&fd->ctime,&fd->atime); - wcsncpyz(fd->Name,FindMask,ASIZE(fd->Name)); + fd->Name=FindMask; #endif fd->Flags=0; fd->IsDir=IsDir(fd->FileAttr); @@ -156,17 +154,17 @@ bool FindFile::FastFind(const wchar *FindMask,FindData *fd,bool GetSymLink) #ifdef _WIN_ALL -HANDLE FindFile::Win32Find(HANDLE hFind,const wchar *Mask,FindData *fd) +HANDLE FindFile::Win32Find(HANDLE hFind,const std::wstring &Mask,FindData *fd) { WIN32_FIND_DATA FindData; if (hFind==INVALID_HANDLE_VALUE) { - hFind=FindFirstFile(Mask,&FindData); + hFind=FindFirstFile(Mask.c_str(),&FindData); if (hFind==INVALID_HANDLE_VALUE) { - wchar LongMask[NM]; - if (GetWinLongPath(Mask,LongMask,ASIZE(LongMask))) - hFind=FindFirstFile(LongMask,&FindData); + std::wstring LongMask; + if (GetWinLongPath(Mask,LongMask)) + hFind=FindFirstFile(LongMask.c_str(),&FindData); } if (hFind==INVALID_HANDLE_VALUE) { @@ -190,8 +188,8 @@ HANDLE FindFile::Win32Find(HANDLE hFind,const wchar *Mask,FindData *fd) if (hFind!=INVALID_HANDLE_VALUE) { - wcsncpyz(fd->Name,Mask,ASIZE(fd->Name)); - SetName(fd->Name,FindData.cFileName,ASIZE(fd->Name)); + fd->Name=Mask; + SetName(fd->Name,FindData.cFileName); fd->Size=INT32TO64(FindData.nFileSizeHigh,FindData.nFileSizeLow); fd->FileAttr=FindData.dwFileAttributes; fd->ftCreationTime=FindData.ftCreationTime; diff --git a/unrar_sys/vendor/unrar/find.hpp b/unrar_sys/vendor/unrar/find.hpp index 250637f..6812def 100644 --- a/unrar_sys/vendor/unrar/find.hpp +++ b/unrar_sys/vendor/unrar/find.hpp @@ -7,7 +7,7 @@ enum FINDDATA_FLAGS { struct FindData { - wchar Name[NM]; + std::wstring Name; uint64 Size; uint FileAttr; bool IsDir; @@ -28,10 +28,10 @@ class FindFile { private: #ifdef _WIN_ALL - static HANDLE Win32Find(HANDLE hFind,const wchar *Mask,FindData *fd); + static HANDLE Win32Find(HANDLE hFind,const std::wstring &Mask,FindData *fd); #endif - wchar FindMask[NM]; + std::wstring FindMask; bool FirstCall; #ifdef _WIN_ALL HANDLE hFind; @@ -41,9 +41,9 @@ class FindFile public: FindFile(); ~FindFile(); - void SetMask(const wchar *Mask); + void SetMask(const std::wstring &Mask); bool Next(FindData *fd,bool GetSymLink=false); - static bool FastFind(const wchar *FindMask,FindData *fd,bool GetSymLink=false); + static bool FastFind(const std::wstring &FindMask,FindData *fd,bool GetSymLink=false); }; #endif diff --git a/unrar_sys/vendor/unrar/getbits.cpp b/unrar_sys/vendor/unrar/getbits.cpp index 5d5ad2b..a5c8c3a 100644 --- a/unrar_sys/vendor/unrar/getbits.cpp +++ b/unrar_sys/vendor/unrar/getbits.cpp @@ -5,11 +5,11 @@ BitInput::BitInput(bool AllocBuffer) ExternalBuffer=false; if (AllocBuffer) { - // getbits*() attempt to read data from InAddr, ... InAddr+4 positions. - // So let's allocate 4 additional bytes for situation, when we need to + // getbits*() attempt to read data from InAddr, ... InAddr+8 positions. + // So let's allocate 8 additional bytes for situation, when we need to // read only 1 byte from the last position of buffer and avoid a crash - // from access to next 4 bytes, which contents we do not need. - size_t BufSize=MAX_SIZE+4; + // from access to next 8 bytes, which contents we do not need. + size_t BufSize=MAX_SIZE+8; InBuf=new byte[BufSize]; // Ensure that we get predictable results when accessing bytes in area @@ -17,7 +17,7 @@ BitInput::BitInput(bool AllocBuffer) memset(InBuf,0,BufSize); } else - InBuf=NULL; + InBuf=nullptr; } @@ -30,14 +30,14 @@ BitInput::~BitInput() void BitInput::faddbits(uint Bits) { - // Function wrapped version of inline addbits to save code size. + // Function wrapped version of inline addbits to reduce the code size. addbits(Bits); } uint BitInput::fgetbits() { - // Function wrapped version of inline getbits to save code size. + // Function wrapped version of inline getbits to reduce the code size. return getbits(); } diff --git a/unrar_sys/vendor/unrar/getbits.hpp b/unrar_sys/vendor/unrar/getbits.hpp index 00acbea..65fb25a 100644 --- a/unrar_sys/vendor/unrar/getbits.hpp +++ b/unrar_sys/vendor/unrar/getbits.hpp @@ -34,8 +34,7 @@ class BitInput uint getbits() { #if defined(LITTLE_ENDIAN) && defined(ALLOW_MISALIGNED) - uint32 BitField=*(uint32*)(InBuf+InAddr); - BitField=ByteSwap32(BitField); + uint32 BitField=RawGetBE4(InBuf+InAddr); BitField >>= (16-InBit); #else uint BitField=(uint)InBuf[InAddr] << 16; @@ -51,19 +50,21 @@ class BitInput // Bit at (InAddr,InBit) has the highest position in returning data. uint getbits32() { -#if defined(LITTLE_ENDIAN) && defined(ALLOW_MISALIGNED) - uint32 BitField=*(uint32*)(InBuf+InAddr); - BitField=ByteSwap32(BitField); -#else - uint BitField=(uint)InBuf[InAddr] << 24; - BitField|=(uint)InBuf[InAddr+1] << 16; - BitField|=(uint)InBuf[InAddr+2] << 8; - BitField|=(uint)InBuf[InAddr+3]; -#endif + uint BitField=RawGetBE4(InBuf+InAddr); BitField <<= InBit; BitField|=(uint)InBuf[InAddr+4] >> (8-InBit); return BitField & 0xffffffff; } + + // Return 64 bits from current position in the buffer. + // Bit at (InAddr,InBit) has the highest position in returning data. + uint64 getbits64() + { + uint64 BitField=RawGetBE8(InBuf+InAddr); + BitField <<= InBit; + BitField|=(uint)InBuf[InAddr+8] >> (8-InBit); + return BitField; + } void faddbits(uint Bits); uint fgetbits(); diff --git a/unrar_sys/vendor/unrar/hardlinks.cpp b/unrar_sys/vendor/unrar/hardlinks.cpp index 171b5fa..5080da0 100644 --- a/unrar_sys/vendor/unrar/hardlinks.cpp +++ b/unrar_sys/vendor/unrar/hardlinks.cpp @@ -1,4 +1,4 @@ -bool ExtractHardlink(CommandData *Cmd,wchar *NameNew,wchar *NameExisting,size_t NameExistingSize) +bool ExtractHardlink(CommandData *Cmd,const std::wstring &NameNew,const std::wstring &NameExisting) { if (!FileExist(NameExisting)) { @@ -10,7 +10,7 @@ bool ExtractHardlink(CommandData *Cmd,wchar *NameNew,wchar *NameExisting,size_t CreatePath(NameNew,true,Cmd->DisableNames); #ifdef _WIN_ALL - bool Success=CreateHardLink(NameNew,NameExisting,NULL)!=0; + bool Success=CreateHardLink(NameNew.c_str(),NameExisting.c_str(),NULL)!=0; if (!Success) { uiMsg(UIERROR_HLINKCREATE,NameNew); @@ -19,10 +19,10 @@ bool ExtractHardlink(CommandData *Cmd,wchar *NameNew,wchar *NameExisting,size_t } return Success; #elif defined(_UNIX) - char NameExistingA[NM],NameNewA[NM]; - WideToChar(NameExisting,NameExistingA,ASIZE(NameExistingA)); - WideToChar(NameNew,NameNewA,ASIZE(NameNewA)); - bool Success=link(NameExistingA,NameNewA)==0; + std::string NameExistingA,NameNewA; + WideToChar(NameExisting,NameExistingA); + WideToChar(NameNew,NameNewA); + bool Success=link(NameExistingA.c_str(),NameNewA.c_str())==0; if (!Success) { uiMsg(UIERROR_HLINKCREATE,NameNew); diff --git a/unrar_sys/vendor/unrar/hash.cpp b/unrar_sys/vendor/unrar/hash.cpp index 106cc60..cde3db9 100644 --- a/unrar_sys/vendor/unrar/hash.cpp +++ b/unrar_sys/vendor/unrar/hash.cpp @@ -76,7 +76,7 @@ void DataHash::Init(HASH_TYPE Type,uint MaxThreads) if (Type==HASH_BLAKE2) blake2sp_init(blake2ctx); #ifdef RAR_SMP - DataHash::MaxThreads=Min(MaxThreads,MaxHashThreads); + DataHash::MaxThreads=Min(MaxThreads,HASH_POOL_THREADS); #endif } @@ -88,13 +88,19 @@ void DataHash::Update(const void *Data,size_t DataSize) CurCRC32=Checksum14((ushort)CurCRC32,Data,DataSize); #endif if (HashType==HASH_CRC32) + { +#ifdef RAR_SMP + UpdateCRC32MT(Data,DataSize); +#else CurCRC32=CRC32(CurCRC32,Data,DataSize); +#endif + } if (HashType==HASH_BLAKE2) { #ifdef RAR_SMP - if (MaxThreads>1 && ThPool==NULL) - ThPool=new ThreadPool(BLAKE2_THREADS_NUMBER); + if (MaxThreads>1 && ThPool==nullptr) + ThPool=new ThreadPool(HASH_POOL_THREADS); blake2ctx->ThPool=ThPool; blake2ctx->MaxThreads=MaxThreads; #endif @@ -103,6 +109,146 @@ void DataHash::Update(const void *Data,size_t DataSize) } +#ifdef RAR_SMP +THREAD_PROC(BuildCRC32Thread) +{ + DataHash::CRC32ThreadData *td=(DataHash::CRC32ThreadData *)Data; + + // Use 0 initial value to simplify combining the result with existing CRC32. + // It doesn't affect the first initial 0xffffffff in the data beginning. + // If we used 0xffffffff here, we would need to shift 0xffffffff left to + // block width and XOR it with block CRC32 to reset its initial value to 0. + td->DataCRC=CRC32(0,td->Data,td->DataSize); +} + + +// CRC is linear and distributive over addition, so CRC(a+b)=CRC(a)+CRC(b). +// Since addition in finite field is XOR, we have CRC(a^b)=CRC(a)^CRC(b). +// So CRC(aaabbb) = CRC(aaa000) ^ CRC(000bbb) = CRC(aaa000) ^ CRC(bbb), +// because CRC ignores leading zeroes. Thus to split CRC calculations +// to "aaa" and "bbb" blocks and then to threads we need to be able to +// find CRC(aaa000) knowing "aaa" quickly. We use Galois finite field to +// calculate the power of 2 to get "1000" and multiply it by "aaa". +void DataHash::UpdateCRC32MT(const void *Data,size_t DataSize) +{ + const size_t MinBlock=0x4000; + if (DataSize<2*MinBlock || MaxThreads<2) + { + CurCRC32=CRC32(CurCRC32,Data,DataSize); + return; + } + + if (ThPool==nullptr) + ThPool=new ThreadPool(HASH_POOL_THREADS); + + size_t Threads=MaxThreads; + size_t BlockSize=DataSize/Threads; + if (BlockSizeAddTask(BuildCRC32Thread,(void*)&td[I]); +#else + BuildCRC32Thread((void*)&td[I]); +#endif + } + +#ifdef USE_THREADS + ThPool->WaitDone(); +#endif // USE_THREADS + + uint StdShift=gfExpCRC(uint(8*td[0].DataSize)); + for (size_t I=0;I>=1) + Reversed|=(N & 1)<<(31-I); + return Reversed; +} + + +// Galois field multiplication modulo POLY. +uint DataHash::gfMulCRC(uint A, uint B) +{ + // For reversed 0xEDB88320 polynomial we bit reverse CRC32 before passing + // to this function, so we must use the normal polynomial here. + // We set the highest polynomial bit 33 for proper multiplication + // in case uint is larger than 32-bit. + const uint POLY=uint(0x104c11db7); + + uint R = 0 ; // Multiplication result. + while (A != 0 && B != 0) // If any of multipliers becomes 0, quit early. + { + // For non-zero lowest B bit, add A to result. + R ^= (B & 1)!=0 ? A : 0; + + // Make A twice larger before the next iteration. + // Subtract POLY to keep it modulo POLY if high bit is set. + A = (A << 1) ^ ((A & 0x80000000)!=0 ? POLY : 0); + + B >>= 1; // Move next B bit to lowest position. + } + return R; +} + + +// Calculate 2 power N with square-and-multiply algorithm. +uint DataHash::gfExpCRC(uint N) +{ + uint S = 2; // Starts from base value and contains the current square. + uint R = 1; // Exponentiation result. + while (N > 1) + { + if ((N & 1)!=0) // If N is odd. + R = gfMulCRC(R, S); + S = gfMulCRC(S, S); // Next square. + N >>= 1; + } + // We could change the loop condition to N > 0 and return R at expense + // of one additional gfMulCRC(S, S). + return gfMulCRC(R, S); +} + + void DataHash::Result(HashValue *Result) { Result->Type=HashType; diff --git a/unrar_sys/vendor/unrar/hash.hpp b/unrar_sys/vendor/unrar/hash.hpp index 6315680..0189113 100644 --- a/unrar_sys/vendor/unrar/hash.hpp +++ b/unrar_sys/vendor/unrar/hash.hpp @@ -32,7 +32,24 @@ class DataHash; class DataHash { + public: + struct CRC32ThreadData + { + void *Data; + size_t DataSize; + uint DataCRC; + }; private: + void UpdateCRC32MT(const void *Data,size_t DataSize); + uint BitReverse32(uint N); + uint gfMulCRC(uint A, uint B); + uint gfExpCRC(uint N); + + // Speed gain seems to vanish above 8 CRC32 threads. + static const uint CRC32_POOL_THREADS=8; + // Thread pool must allow at least BLAKE2_THREADS_NUMBER threads. + static const uint HASH_POOL_THREADS=Max(BLAKE2_THREADS_NUMBER,CRC32_POOL_THREADS); + HASH_TYPE HashType; uint CurCRC32; blake2sp_state *blake2ctx; @@ -41,8 +58,6 @@ class DataHash ThreadPool *ThPool; uint MaxThreads; - // Upper limit for maximum threads to prevent wasting threads in pool. - static const uint MaxHashThreads=8; #endif public: DataHash(); diff --git a/unrar_sys/vendor/unrar/headers.cpp b/unrar_sys/vendor/unrar/headers.cpp index b441376..b2d00d3 100644 --- a/unrar_sys/vendor/unrar/headers.cpp +++ b/unrar_sys/vendor/unrar/headers.cpp @@ -2,7 +2,7 @@ void FileHeader::Reset(size_t SubDataSize) { - SubData.Alloc(SubDataSize); + SubData.resize(SubDataSize); BaseBlock::Reset(); FileHash.Init(HASH_NONE); mtime.Reset(); @@ -37,6 +37,7 @@ void FileHeader::Reset(size_t SubDataSize) } +/* FileHeader& FileHeader::operator = (FileHeader &hd) { SubData.Reset(); @@ -45,6 +46,7 @@ FileHeader& FileHeader::operator = (FileHeader &hd) SubData=hd.SubData; return *this; } +*/ void MainHeader::Reset() diff --git a/unrar_sys/vendor/unrar/headers.hpp b/unrar_sys/vendor/unrar/headers.hpp index 5984f99..8697e38 100644 --- a/unrar_sys/vendor/unrar/headers.hpp +++ b/unrar_sys/vendor/unrar/headers.hpp @@ -15,8 +15,10 @@ #define VER_PACK 29U #define VER_PACK5 50U // It is stored as 0, but we subtract 50 when saving an archive. +#define VER_PACK7 70U // It is stored as 1, but we subtract 70 when saving an archive. #define VER_UNPACK 29U #define VER_UNPACK5 50U // It is stored as 0, but we add 50 when reading an archive. +#define VER_UNPACK7 70U // It is stored as 1, but we add 50 when reading an archive. #define VER_UNKNOWN 9999U // Just some large value. #define MHD_VOLUME 0x0001U @@ -82,7 +84,7 @@ enum HEADER_TYPE { }; -// RAR 2.9 and earlier. +// RAR 2.9 and earlier service haeders, mostly outdated and not supported. enum { EA_HEAD=0x100,UO_HEAD=0x101,MAC_HEAD=0x102,BEEA_HEAD=0x103, NTACL_HEAD=0x104,STREAM_HEAD=0x105 }; @@ -147,6 +149,14 @@ struct BaseBlock { SkipIfUnknown=false; } + + // We use it to assign this block data to inherited blocks. + // Such function seems to be cleaner than '(BaseBlock&)' cast or adding + // 'using BaseBlock::operator=;' to every inherited header. + void SetBaseBlock(BaseBlock &Src) + { + *this=Src; + } }; @@ -184,9 +194,9 @@ struct FileHeader:BlockHeader uint FileAttr; uint SubFlags; }; - wchar FileName[NM]; + std::wstring FileName; - Array SubData; + std::vector SubData; RarTime mtime; RarTime ctime; @@ -226,7 +236,7 @@ struct FileHeader:BlockHeader bool Dir; bool CommentInHeader; // RAR 2.0 file comment. bool Version; // name.ext;ver file name containing the version number. - size_t WinSize; + uint64 WinSize; bool Inherited; // New file inherits a subblock when updating a host file (for subblocks only). // 'true' if file sizes use 8 bytes instead of 4. Not used in RAR 5.0. @@ -239,7 +249,7 @@ struct FileHeader:BlockHeader HOST_SYSTEM_TYPE HSType; FILE_SYSTEM_REDIRECT RedirType; - wchar RedirName[NM]; + std::wstring RedirName; bool DirTarget; bool UnixOwnerSet,UnixOwnerNumeric,UnixGroupNumeric; @@ -256,10 +266,10 @@ struct FileHeader:BlockHeader bool CmpName(const wchar *Name) { - return(wcscmp(FileName,Name)==0); + return FileName==Name; } - FileHeader& operator = (FileHeader &hd); +// FileHeader& operator = (FileHeader &hd); }; @@ -340,7 +350,7 @@ struct StreamHeader:SubBlockHeader byte Method; uint StreamCRC; ushort StreamNameSize; - char StreamName[260]; + std::string StreamName; }; diff --git a/unrar_sys/vendor/unrar/headers5.hpp b/unrar_sys/vendor/unrar/headers5.hpp index 50f5955..361e554 100644 --- a/unrar_sys/vendor/unrar/headers5.hpp +++ b/unrar_sys/vendor/unrar/headers5.hpp @@ -42,20 +42,27 @@ // RAR 5.0 file compression flags. -#define FCI_ALGO_BIT0 0x0001 // Version of compression algorithm. -#define FCI_ALGO_BIT1 0x0002 // 0 .. 63. -#define FCI_ALGO_BIT2 0x0004 -#define FCI_ALGO_BIT3 0x0008 -#define FCI_ALGO_BIT4 0x0010 -#define FCI_ALGO_BIT5 0x0020 -#define FCI_SOLID 0x0040 // Solid flag. -#define FCI_METHOD_BIT0 0x0080 // Compression method. -#define FCI_METHOD_BIT1 0x0100 // 0 .. 5 (6 and 7 are not used). -#define FCI_METHOD_BIT2 0x0200 -#define FCI_DICT_BIT0 0x0400 // Dictionary size. -#define FCI_DICT_BIT1 0x0800 // 128 KB .. 4 GB. -#define FCI_DICT_BIT2 0x1000 -#define FCI_DICT_BIT3 0x2000 +#define FCI_ALGO_BIT0 0x00000001 // Version of compression algorithm. +#define FCI_ALGO_BIT1 0x00000002 // 0 .. 63. +#define FCI_ALGO_BIT2 0x00000004 +#define FCI_ALGO_BIT3 0x00000008 +#define FCI_ALGO_BIT4 0x00000010 +#define FCI_ALGO_BIT5 0x00000020 +#define FCI_SOLID 0x00000040 // Solid flag. +#define FCI_METHOD_BIT0 0x00000080 // Compression method. +#define FCI_METHOD_BIT1 0x00000100 // 0 .. 5 (6 and 7 are not used). +#define FCI_METHOD_BIT2 0x00000200 +#define FCI_DICT_BIT0 0x00000400 // Dictionary size. +#define FCI_DICT_BIT1 0x00000800 // 128 KB .. 1 TB. +#define FCI_DICT_BIT2 0x00001000 +#define FCI_DICT_BIT3 0x00002000 +#define FCI_DICT_BIT4 0x00004000 +#define FCI_DICT_FRACT0 0x00008000 // Dictionary fraction in 1/32 of size. +#define FCI_DICT_FRACT1 0x00010000 +#define FCI_DICT_FRACT2 0x00020000 +#define FCI_DICT_FRACT3 0x00040000 +#define FCI_DICT_FRACT4 0x00080000 +#define FCI_RAR5_COMPAT 0x00100000 // RAR7 compression flags and RAR5 compression algorithm. // Main header extra field values. #define MHEXTRA_LOCATOR 0x01 // Position of quick list and other blocks. diff --git a/unrar_sys/vendor/unrar/isnt.cpp b/unrar_sys/vendor/unrar/isnt.cpp index 59cf5f2..de5a6e6 100644 --- a/unrar_sys/vendor/unrar/isnt.cpp +++ b/unrar_sys/vendor/unrar/isnt.cpp @@ -63,30 +63,24 @@ static bool WMI_IsWindows10() hres = pSvc->ExecQuery(bstr_t("WQL"), bstr_t("SELECT * FROM Win32_OperatingSystem"), WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &pEnumerator); - if (FAILED(hres)) + if (FAILED(hres) || pEnumerator==NULL) { pSvc->Release(); pLoc->Release(); return false; } + bool Win10=false; + IWbemClassObject *pclsObj = NULL; ULONG uReturn = 0; - - bool Win10=false; - while (pEnumerator!=NULL) + pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn); + if (pclsObj!=NULL && uReturn>0) { - HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn); - - if (uReturn==0) - break; - VARIANT vtProp; - - hr = pclsObj->Get(L"Name", 0, &vtProp, 0, 0); + pclsObj->Get(L"Name", 0, &vtProp, 0, 0); Win10|=wcsstr(vtProp.bstrVal,L"Windows 10")!=NULL; VariantClear(&vtProp); - pclsObj->Release(); } diff --git a/unrar_sys/vendor/unrar/list.cpp b/unrar_sys/vendor/unrar/list.cpp index e4444e1..3eae4de 100644 --- a/unrar_sys/vendor/unrar/list.cpp +++ b/unrar_sys/vendor/unrar/list.cpp @@ -1,7 +1,6 @@ #include "rar.hpp" static void ListFileHeader(Archive &Arc,FileHeader &hd,bool &TitleShown,bool Verbose,bool Technical,bool Bare,bool DisableNames); -static void ListSymLink(Archive &Arc); static void ListFileAttr(uint A,HOST_SYSTEM_TYPE HostType,wchar *AttrStr,size_t AttrSize); static void ListOldSubHeader(Archive &Arc); static void ListNewSubHeader(CommandData *Cmd,Archive &Arc); @@ -15,8 +14,8 @@ void ListArchive(CommandData *Cmd) bool Bare=(Cmd->Command[1]=='B'); bool Verbose=(Cmd->Command[0]=='V'); - wchar ArcName[NM]; - while (Cmd->GetArcName(ArcName,ASIZE(ArcName))) + std::wstring ArcName; + while (Cmd->GetArcName(ArcName)) { if (Cmd->ManualPassword) Cmd->Password.Clean(); // Clean user entered password before processing next archive. @@ -35,33 +34,31 @@ void ListArchive(CommandData *Cmd) if (!Bare) { Arc.ViewComment(); - mprintf(L"\n%s: %s",St(MListArchive),Arc.FileName); + mprintf(L"\n%s: %s",St(MListArchive),Arc.FileName.c_str()); mprintf(L"\n%s: ",St(MListDetails)); - uint SetCount=0; const wchar *Fmt=Arc.Format==RARFMT14 ? L"RAR 1.4":(Arc.Format==RARFMT15 ? L"RAR 4":L"RAR 5"); - mprintf(L"%s%s", SetCount++ > 0 ? L", ":L"", Fmt); + mprintf(L"%s", Fmt); if (Arc.Solid) - mprintf(L"%s%s", SetCount++ > 0 ? L", ":L"", St(MListSolid)); + mprintf(L", %s", St(MListSolid)); if (Arc.SFXSize>0) - mprintf(L"%s%s", SetCount++ > 0 ? L", ":L"", St(MListSFX)); + mprintf(L", %s", St(MListSFX)); if (Arc.Volume) if (Arc.Format==RARFMT50) { // RAR 5.0 archives store the volume number in main header, // so it is already available now. - if (SetCount++ > 0) - mprintf(L", "); + mprintf(L", "); mprintf(St(MVolumeNumber),Arc.VolNumber+1); } else - mprintf(L"%s%s", SetCount++ > 0 ? L", ":L"", St(MListVolume)); + mprintf(L", %s", St(MListVolume)); if (Arc.Protected) - mprintf(L"%s%s", SetCount++ > 0 ? L", ":L"", St(MListRR)); + mprintf(L", %s", St(MListRR)); if (Arc.Locked) - mprintf(L"%s%s", SetCount++ > 0 ? L", ":L"", St(MListLock)); + mprintf(L", %s", St(MListLock)); if (Arc.Encrypted) - mprintf(L"%s%s", SetCount++ > 0 ? L", ":L"", St(MListEncHead)); + mprintf(L", %s", St(MListEncHead)); if (!Arc.MainHead.OrigName.empty()) mprintf(L"\n%s: %s",St(MOrigName),Arc.MainHead.OrigName.c_str()); @@ -100,7 +97,7 @@ void ListArchive(CommandData *Cmd) switch(HeaderType) { case HEAD_FILE: - FileMatched=Cmd->IsProcessFile(Arc.FileHead,NULL,MATCH_WILDSUBPATH,0,NULL,0)!=0; + FileMatched=Cmd->IsProcessFile(Arc.FileHead,NULL,MATCH_WILDSUBPATH,0,NULL)!=0; if (FileMatched) { ListFileHeader(Arc,Arc.FileHead,TitleShown,Verbose,Technical,Bare,Cmd->DisableNames); @@ -155,7 +152,7 @@ void ListArchive(CommandData *Cmd) ArcCount++; #ifndef NOVOLUME - if (Cmd->VolSize!=0 && (Arc.FileHead.SplitAfter || + if (Cmd->VolSize==VOLSIZE_AUTO && (Arc.FileHead.SplitAfter || Arc.GetHeaderType()==HEAD_ENDARC && Arc.EndArcHead.NextVolume) && MergeArchive(Arc,NULL,false,Cmd->Command[0])) Arc.Seek(0,SEEK_SET); @@ -166,7 +163,7 @@ void ListArchive(CommandData *Cmd) else { if (Cmd->ArcNames.ItemsCount()<2 && !Bare) - mprintf(St(MNotRAR),Arc.FileName); + mprintf(St(MNotRAR),Arc.FileName.c_str()); break; } } @@ -219,7 +216,7 @@ void ListFileHeader(Archive &Arc,FileHeader &hd,bool &TitleShown,bool Verbose,bo if (DisableNames) return; - wchar *Name=hd.FileName; + const wchar *Name=hd.FileName.c_str(); RARFORMAT Format=Arc.Format; if (Bare) @@ -266,9 +263,8 @@ void ListFileHeader(Archive &Arc,FileHeader &hd,bool &TitleShown,bool Verbose,bo if (!FileBlock && Arc.SubHead.CmpName(SUBHEAD_TYPE_STREAM)) { mprintf(L"\n%12ls: %ls",St(MListType),St(MListStream)); - wchar StreamName[NM]; - GetStreamNameNTFS(Arc,StreamName,ASIZE(StreamName)); - mprintf(L"\n%12ls: %ls",St(MListTarget),StreamName); + std::wstring StreamName=GetStreamNameNTFS(Arc); + mprintf(L"\n%12ls: %ls",St(MListTarget),StreamName.c_str()); } else { @@ -292,25 +288,27 @@ void ListFileHeader(Archive &Arc,FileHeader &hd,bool &TitleShown,bool Verbose,bo if (hd.RedirType!=FSREDIR_NONE) if (Format==RARFMT15) { - char LinkTargetA[NM]; + std::string LinkTargetA; if (Arc.FileHead.Encrypted) { // Link data are encrypted. We would need to ask for password // and initialize decryption routine to display the link target. - strncpyz(LinkTargetA,"*<-?->",ASIZE(LinkTargetA)); + LinkTargetA="*<-?->"; } else { - int DataSize=(int)Min(hd.PackSize,ASIZE(LinkTargetA)-1); - Arc.Read(LinkTargetA,DataSize); - LinkTargetA[DataSize > 0 ? DataSize : 0] = 0; + size_t DataSize=(size_t)Min(hd.PackSize,MAXPATHSIZE); + std::vector Buf(DataSize+1); + Arc.Read(Buf.data(),DataSize); + Buf[DataSize] = 0; + LinkTargetA=Buf.data(); } - wchar LinkTarget[NM]; - CharToWide(LinkTargetA,LinkTarget,ASIZE(LinkTarget)); - mprintf(L"\n%12ls: %ls",St(MListTarget),LinkTarget); + std::wstring LinkTarget; + CharToWide(LinkTargetA,LinkTarget); + mprintf(L"\n%12ls: %ls",St(MListTarget),LinkTarget.c_str()); } else - mprintf(L"\n%12ls: %ls",St(MListTarget),hd.RedirName); + mprintf(L"\n%12ls: %ls",St(MListTarget),hd.RedirName.c_str()); } if (!hd.Dir) { @@ -341,11 +339,11 @@ void ListFileHeader(Archive &Arc,FileHeader &hd,bool &TitleShown,bool Verbose,bo hd.FileHash.CRC32); if (hd.FileHash.Type==HASH_BLAKE2) { - wchar BlakeStr[BLAKE2_DIGEST_SIZE*2+1]; - BinToHex(hd.FileHash.Digest,BLAKE2_DIGEST_SIZE,NULL,BlakeStr,ASIZE(BlakeStr)); + std::wstring BlakeStr; + BinToHex(hd.FileHash.Digest,BLAKE2_DIGEST_SIZE,BlakeStr); mprintf(L"\n%12ls: %ls", hd.UseHashKey ? L"BLAKE2 MAC":hd.SplitAfter ? L"Pack-BLAKE2":L"BLAKE2", - BlakeStr); + BlakeStr.c_str()); } const wchar *HostOS=L""; @@ -362,11 +360,22 @@ void ListFileHeader(Archive &Arc,FileHeader &hd,bool &TitleShown,bool Verbose,bo if (*HostOS!=0) mprintf(L"\n%12ls: %ls",St(MListHostOS),HostOS); - mprintf(L"\n%12ls: RAR %ls(v%d) -m%d -md=%d%s",St(MListCompInfo), + std::wstring WinSize; + if (!hd.Dir) + if (hd.WinSize%1073741824==0) + WinSize=L" -md=" + std::to_wstring(hd.WinSize/1073741824) + L"g"; + else + if (hd.WinSize%1048576==0) + WinSize=L" -md=" + std::to_wstring(hd.WinSize/1048576) + L"m"; + else + if (hd.WinSize>=1024) + WinSize=L" -md=" + std::to_wstring(hd.WinSize/1024) + L"k"; + else + WinSize=L" -md=?"; + + mprintf(L"\n%12ls: RAR %ls(v%d) -m%d%s",St(MListCompInfo), Format==RARFMT15 ? L"1.5":L"5.0", - hd.UnpVer==VER_UNKNOWN ? 0 : hd.UnpVer,hd.Method, - hd.WinSize>=0x100000 ? hd.WinSize/0x100000:hd.WinSize/0x400, - hd.WinSize>=0x100000 ? L"M":L"K"); + hd.UnpVer==VER_UNKNOWN ? 0 : hd.UnpVer,hd.Method,WinSize.c_str()); if (hd.Solid || hd.Encrypted) { @@ -379,7 +388,7 @@ void ListFileHeader(Archive &Arc,FileHeader &hd,bool &TitleShown,bool Verbose,bo if (hd.Version) { - uint Version=ParseVersionFileName(Name,false); + uint Version=ParseVersionFileName(hd.FileName,false); if (Version!=0) mprintf(L"\n%12ls: %u",St(MListFileVer),Version); } @@ -388,13 +397,13 @@ void ListFileHeader(Archive &Arc,FileHeader &hd,bool &TitleShown,bool Verbose,bo { mprintf(L"\n%12ls: ",L"Unix owner"); if (*hd.UnixOwnerName!=0) - mprintf(L"%ls",GetWide(hd.UnixOwnerName)); + mprintf(L"%ls",GetWide(hd.UnixOwnerName).c_str()); else if (hd.UnixOwnerNumeric) mprintf(L"#%d",hd.UnixOwnerID); mprintf(L":"); if (*hd.UnixGroupName!=0) - mprintf(L"%ls",GetWide(hd.UnixGroupName)); + mprintf(L"%ls",GetWide(hd.UnixGroupName).c_str()); else if (hd.UnixGroupNumeric) mprintf(L"#%d",hd.UnixGroupID); @@ -422,31 +431,11 @@ void ListFileHeader(Archive &Arc,FileHeader &hd,bool &TitleShown,bool Verbose,bo mprintf(L"%02x%02x..%02x ",S[0],S[1],S[31]); } else - mprintf(L"???????? "); + mprintf(hd.Dir ? L" ":L"???????? "); // Missing checksum is ok for folder, not for file. } mprintf(L"%ls",Name); } -/* -void ListSymLink(Archive &Arc) -{ - if (Arc.FileHead.HSType==HSYS_UNIX && (Arc.FileHead.FileAttr & 0xF000)==0xA000) - if (Arc.FileHead.Encrypted) - { - // Link data are encrypted. We would need to ask for password - // and initialize decryption routine to display the link target. - mprintf(L"\n%22ls %ls",L"-->",L"*<-?->"); - } - else - { - char FileName[NM]; - uint DataSize=(uint)Min(Arc.FileHead.PackSize,sizeof(FileName)-1); - Arc.Read(FileName,DataSize); - FileName[DataSize]=0; - mprintf(L"\n%22ls %ls",L"-->",GetWide(FileName)); - } -} -*/ void ListFileAttr(uint A,HOST_SYSTEM_TYPE HostType,wchar *AttrStr,size_t AttrSize) { diff --git a/unrar_sys/vendor/unrar/loclang.hpp b/unrar_sys/vendor/unrar/loclang.hpp index 1e23ece..df94a8e 100644 --- a/unrar_sys/vendor/unrar/loclang.hpp +++ b/unrar_sys/vendor/unrar/loclang.hpp @@ -45,7 +45,7 @@ #define MCHelpCmdR L"\n r Repair archive" #define MCHelpCmdRC L"\n rc Reconstruct missing volumes" #define MCHelpCmdRN L"\n rn Rename archived files" -#define MCHelpCmdRR L"\n rr[N] Add the data recovery record" +#define MCHelpCmdRR L"\n rr[N] Add data recovery record" #define MCHelpCmdRV L"\n rv[N] Create recovery volumes" #define MCHelpCmdS L"\n s[name|-] Convert archive to or from SFX" #define MCHelpCmdT L"\n t Test archive files" @@ -94,9 +94,8 @@ #define MCHelpSwKB L"\n kb Keep broken extracted files" #define MCHelpSwLog L"\n log[f][=name] Write names to log file" #define MCHelpSwMn L"\n m<0..5> Set compression level (0-store...3-default...5-maximal)" -#define MCHelpSwMA L"\n ma[4|5] Specify a version of archiving format" #define MCHelpSwMC L"\n mc Set advanced compression parameters" -#define MCHelpSwMD L"\n md[k,m,g] Dictionary size in KB, MB or GB" +#define MCHelpSwMD L"\n md[x][kmg] Dictionary size in KB, MB or GB" #define MCHelpSwME L"\n me[par] Set encryption parameters" #define MCHelpSwMS L"\n ms[ext;ext] Specify file types to store" #define MCHelpSwMT L"\n mt Set the number of threads" @@ -107,7 +106,7 @@ #define MCHelpSwOC L"\n oc Set NTFS Compressed attribute" #define MCHelpSwOH L"\n oh Save hard links as the link instead of the file" #define MCHelpSwOI L"\n oi[0-4][:min] Save identical files as references" -#define MCHelpSwOL L"\n ol[a] Process symbolic links as the link [absolute paths]" +#define MCHelpSwOL L"\n ol[a,-] Process symbolic links as the link [absolute paths, skip]" #define MCHelpSwONI L"\n oni Allow potentially incompatible names" #define MCHelpSwOP L"\n op Set the output path for extracted files" #define MCHelpSwOR L"\n or Rename files automatically" @@ -126,8 +125,8 @@ #define MCHelpSwSC L"\n sc[obj] Specify the character set" #define MCHelpSwSFX L"\n sfx[name] Create SFX archive" #define MCHelpSwSI L"\n si[name] Read data from standard input (stdin)" -#define MCHelpSwSL L"\n sl Process files with size less than specified" -#define MCHelpSwSM L"\n sm Process files with size more than specified" +#define MCHelpSwSL L"\n sl[u] Process files with size less than specified" +#define MCHelpSwSM L"\n sm[u] Process files with size more than specified" #define MCHelpSwT L"\n t Test files after archiving" #define MCHelpSwTK L"\n tk Keep original archive time" #define MCHelpSwTL L"\n tl Set archive time to latest file" @@ -139,10 +138,9 @@ #define MCHelpSwU L"\n u Update files" #define MCHelpSwV L"\n v Create volumes with size autodetection or list all volumes" #define MCHelpSwVUnr L"\n v List all volumes" -#define MCHelpSwVn L"\n v[k,b] Create volumes with size=*1000 [*1024, *1]" +#define MCHelpSwVn L"\n v[u] Create volumes with size in [bBkKmMgGtT] units" #define MCHelpSwVD L"\n vd Erase disk contents before creating volume" #define MCHelpSwVER L"\n ver[n] File version control" -#define MCHelpSwVN L"\n vn Use the old style volume naming scheme" #define MCHelpSwVP L"\n vp Pause before each volume" #define MCHelpSwW L"\n w Assign work directory" #define MCHelpSwX L"\n x Exclude specified file" @@ -165,7 +163,7 @@ #define MErrRename L"\nCannot rename %s to %s" #define MAbsNextVol L"\nCannot find volume %s" #define MBreak L"\nUser break\n" -#define MAskCreatVol L"\nCreate next volume ?" +#define MAskCreatVol L"\nCreate next volume?" #define MAskNextDisk L"\nDisk full. Insert next" #define MCreatVol L"\n\nCreating %sarchive %s\n" #define MAskNextVol L"\nInsert disk with %s" @@ -212,7 +210,7 @@ #define MCRCFailed L"\n%-20s - checksum error" #define MExtrTest L"\n\nTesting archive %s\n" #define MExtracting L"\n\nExtracting from %s\n" -#define MUseCurPsw L"\n%s - use current password ?" +#define MUseCurPsw L"\n%s - use current password?" #define MCreatDir L"\nCreating %-56s" #define MExtrSkipFile L"\nSkipping %-56s" #define MExtrTestFile L"\nTesting %-56s" @@ -225,18 +223,17 @@ #define MExtrAllOk L"\nAll OK" #define MExtrTotalErr L"\nTotal errors: %ld" #define MAskReplace L"\n\nWould you like to replace the existing file %s\n%6s bytes, modified on %s\nwith a new one\n%6s bytes, modified on %s\n" -#define MAskOverwrite L"\nOverwrite %s ?" +#define MAskOverwrite L"\nOverwrite %s?" #define MAskNewName L"\nEnter new name: " #define MHeaderBroken L"\nCorrupt header is found" #define MMainHeaderBroken L"\nMain archive header is corrupt" #define MLogFileHead L"\n%s - the file header is corrupt" #define MLogProtectHead L"The data recovery header is corrupt" +#define MArcComment L"\nArchive comment" #define MReadStdinCmt L"\nReading comment from stdin\n" #define MReadCommFrom L"\nReading comment from %s" #define MDelComment L"\nDeleting comment from %s" #define MAddComment L"\nAdding comment to %s" -#define MFCommAdd L"\nAdding file comments" -#define MAskFComm L"\n\nReading comment for %s : %s from stdin\n" #define MLogCommBrk L"\nThe archive comment is corrupt" #define MCommAskCont L"\nPress 'Enter' to continue or 'Q' to quit:" #define MWriteCommTo L"\nWrite comment to %s" @@ -284,11 +281,9 @@ #define MListHostOS L"Host OS" #define MListFileVer L"File version" #define MListService L"Service" -#define MListUOHead L"\n Unix Owner/Group data: %-14s %-14s" #define MListNTACLHead L"\n NTFS security data" #define MListStrmHead L"\n NTFS stream: %s" #define MListUnkHead L"\n Unknown subheader type: 0x%04x" -#define MFileComment L"\nComment: " #define MYes L"Yes" #define MNo L"No" #define MListNoFiles L" 0 files\n" @@ -296,10 +291,10 @@ #define MRprBuild L"\nBuilding %s" #define MRprOldFormat L"\nCannot repair archive with old format" #define MRprFind L"\nFound %s" -#define MRprAskIsSol L"\nThe archive header is corrupt. Mark archive as solid ?" +#define MRprAskIsSol L"\nThe archive header is corrupt. Mark archive as solid?" #define MRprNoFiles L"\nNo files found" #define MLogUnexpEOF L"\nUnexpected end of archive" -#define MRepAskReconst L"\nReconstruct archive structure ?" +#define MRepAskReconst L"\nReconstruct archive structure?" #define MRRSearch L"\nSearching for recovery record" #define MAnalyzeFileData L"\nAnalyzing file data" #define MRecRNotFound L"\nData recovery record not found" @@ -316,7 +311,7 @@ #define MSetOwnersError L"\nWARNING: Cannot set %s owner and group\n" #define MErrLnkRead L"\nWARNING: Cannot read symbolic link %s" #define MSymLinkExists L"\nWARNING: Symbolic link %s already exists" -#define MAskRetryCreate L"\nCannot create %s. Retry ?" +#define MAskRetryCreate L"\nCannot create %s. Retry?" #define MDataBadCRC L"\n%-20s : packed data checksum error in volume %s" #define MFileRO L"\n%s is read-only" #define MACLGetError L"\nWARNING: Cannot get %s security data\n" @@ -330,6 +325,7 @@ #define MCorrectingName L"\nWARNING: Attempting to correct the invalid file or directory name" #define MUnpCannotMerge L"\nWARNING: You need to start extraction from a previous volume to unpack %s" #define MUnknownOption L"\nERROR: Unknown option: %s" +#define MSwSyntaxError L"\nERROR: '-' is expected in the beginning of: %s" #define MSubHeadCorrupt L"\nERROR: Corrupt data header found, ignored" #define MSubHeadUnknown L"\nWARNING: Unknown data header format, ignored" #define MSubHeadDataCRC L"\nERROR: Corrupt %s data block" @@ -386,6 +382,8 @@ #define MNeedAdmin L"\nYou may need to run RAR as administrator" #define MDictOutMem L"\nNot enough memory for %d MB compression dictionary, changed to %d MB." #define MUseSmalllerDict L"\nPlease use a smaller compression dictionary." +#define MExtrDictOutMem L"\nNot enough memory to unpack the archive with %u GB compression dictionary." +#define MSuggest64bit L"\n64-bit RAR version is necessary." #define MOpenErrAtime L"\nYou may need to remove -tsp switch to open this file." #define MErrReadInfo L"\nChoose 'Ignore' to continue with the already read file part only, 'Ignore all' to do it for all read errors, 'Retry' to repeat read and 'Quit' to abort." #define MErrReadTrunc L"\n%s is archived incompletely because of read error.\n" @@ -399,3 +397,8 @@ #define MOrigName L"Original name" #define MOriginalTime L"Original time" #define MFileRenamed L"\n%s is renamed to %s" +#define MDictNotAllowed L"\n%u GB dictionary exceeds %u GB limit and needs more than %u GB memory to unpack." +#define MDictExtrAnyway L"\nUse -md%ug or -mdx%ug switches to extract anyway." +#define MDictComprLimit L"\n%u GB dictionary exceeds %u GB limit and not allowed when compressing data." +#define MNeedSFX64 L"\n64-bit self-extracting module is necessary for %u GB compression dictionary." +#define MSkipUnsafeLink L"\nSkipping the potentially unsafe %s -> %s link. For archives from a trustworthy source use -ola to extract it anyway." diff --git a/unrar_sys/vendor/unrar/log.cpp b/unrar_sys/vendor/unrar/log.cpp index 8bbe8ee..57f7648 100644 --- a/unrar_sys/vendor/unrar/log.cpp +++ b/unrar_sys/vendor/unrar/log.cpp @@ -1,13 +1,14 @@ #include "rar.hpp" -static wchar LogName[NM]; -static RAR_CHARSET LogCharset=RCH_DEFAULT; -void InitLogOptions(const wchar *LogFileName,RAR_CHARSET CSet) +void InitLogOptions(const std::wstring &LogFileName,RAR_CHARSET CSet) +{ +} + + +void CloseLogOptions() { - wcsncpyz(LogName,LogFileName,ASIZE(LogName)); - LogCharset=CSet; } @@ -19,17 +20,15 @@ void Log(const wchar *ArcName,const wchar *fmt,...) uiAlarm(UIALARM_ERROR); - // This buffer is for format string only, not for entire output, - // so it can be short enough. - wchar fmtw[1024]; - PrintfPrepareFmt(fmt,fmtw,ASIZE(fmtw)); - - safebuf wchar Msg[2*NM+1024]; va_list arglist; va_start(arglist,fmt); - vswprintf(Msg,ASIZE(Msg),fmtw,arglist); + + std::wstring s=vwstrprintf(fmt,arglist); + + ReplaceEsc(s); + va_end(arglist); - eprintf(L"%ls",Msg); + eprintf(L"%ls",s.c_str()); ErrHandler.SetSystemErrorCode(Code); } #endif diff --git a/unrar_sys/vendor/unrar/log.hpp b/unrar_sys/vendor/unrar/log.hpp index 008ef11..22eaa8e 100644 --- a/unrar_sys/vendor/unrar/log.hpp +++ b/unrar_sys/vendor/unrar/log.hpp @@ -1,7 +1,8 @@ #ifndef _RAR_LOG_ #define _RAR_LOG_ -void InitLogOptions(const wchar *LogFileName,RAR_CHARSET CSet); +void InitLogOptions(const std::wstring &LogFileName,RAR_CHARSET CSet); +void CloseLogOptions(); #ifdef SILENT inline void Log(const wchar *ArcName,const wchar *fmt,...) {} diff --git a/unrar_sys/vendor/unrar/makefile b/unrar_sys/vendor/unrar/makefile index 55af49b..ce54a02 100644 --- a/unrar_sys/vendor/unrar/makefile +++ b/unrar_sys/vendor/unrar/makefile @@ -3,7 +3,7 @@ # Linux using GCC CXX=c++ -CXXFLAGS=-O2 -Wno-logical-op-parentheses -Wno-switch -Wno-dangling-else +CXXFLAGS=-march=native -O2 -std=c++11 -Wno-logical-op-parentheses -Wno-switch -Wno-dangling-else LIBFLAGS=-fPIC DEFINES=-D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -DRAR_SMP STRIP=strip @@ -11,107 +11,6 @@ AR=ar LDFLAGS=-pthread DESTDIR=/usr -# Linux using LCC -#CXX=lcc -#CXXFLAGS=-O2 -#DEFINES=-D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -#STRIP=strip -#AR=ar -#DESTDIR=/usr - -# CYGWIN using GCC -#CXX=c++ -#CXXFLAGS=-O2 -#LIBFLAGS= -#DEFINES=-D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -DRAR_SMP -#STRIP=strip -#AR=ar -#LDFLAGS=-pthread -#DESTDIR=/usr - -# HP UX using aCC -#CXX=aCC -#CXXFLAGS=-AA +O2 +Onolimit -#DEFINES=-D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -#STRIP=strip -#AR=ar -#DESTDIR=/usr - -# IRIX using GCC -#CXX=g++ -#CXXFLAGS=-O2 -#DEFINES=-DBIG_ENDIAN -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_BSD_COMPAT -D_XOPEN_SOURCE -D_XOPEN_SOURCE_EXTENDED=1 -#STRIP=strip -#AR=ar -#DESTDIR=/usr - -# IRIX using MIPSPro (experimental) -#CXX=CC -#CXXFLAGS=-O2 -mips3 -woff 1234,1156,3284 -LANG:std -#DEFINES=-DBIG_ENDIAN -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_BSD_COMPAT -Dint64=int64_t -#STRIP=strip -#AR=ar -#DESTDIR=/usr - -# AIX using xlC (IBM VisualAge C++ 5.0) -#CXX=xlC -#CXXFLAGS=-O -qinline -qro -qroconst -qmaxmem=16384 -qcpluscmt -#DEFINES=-D_LARGE_FILES -D_LARGE_FILE_API -#LIBS=-lbsd -#STRIP=strip -#AR=ar -#DESTDIR=/usr - -# Solaris using CC -#CXX=CC -#CXXFLAGS=-fast -erroff=wvarhidemem -#DEFINES=-D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -#STRIP=strip -#AR=ar -#DESTDIR=/usr - -# Solaris using GCC (optimized for UltraSPARC 1 CPU) -#CXX=g++ -#CXXFLAGS=-O3 -mcpu=v9 -mtune=ultrasparc -m32 -#DEFINES=-D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -#STRIP=/usr/ccs/bin/strip -#AR=/usr/ccs/bin/ar -#DESTDIR=/usr - -# Tru64 5.1B using GCC3 -#CXX=g++ -#CXXFLAGS=-O2 -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_XOPEN_SOURCE=500 -#STRIP=strip -#AR=ar -#LDFLAGS=-rpath /usr/local/gcc/lib -#DESTDIR=/usr - -# Tru64 5.1B using DEC C++ -#CXX=cxx -#CXXFLAGS=-O4 -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -Dint64=long -#STRIP=strip -#AR=ar -#LDFLAGS= -#DESTDIR=/usr - -# QNX 6.x using GCC -#CXX=g++ -#CXXFLAGS=-O2 -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -fexceptions -#STRIP=strip -#AR=ar -#LDFLAGS=-fexceptions -#DESTDIR=/usr - -# Cross-compile -# Linux using arm-linux-g++ -#CXX=arm-linux-g++ -#CXXFLAGS=-O2 -#DEFINES=-D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -#STRIP=arm-linux-strip -#AR=arm-linux-ar -#LDFLAGS=-static -#DESTDIR=/usr - ########################## COMPILE=$(CXX) $(CPPFLAGS) $(CXXFLAGS) $(DEFINES) diff --git a/unrar_sys/vendor/unrar/match.cpp b/unrar_sys/vendor/unrar/match.cpp index ec88fa6..0a78d8d 100644 --- a/unrar_sys/vendor/unrar/match.cpp +++ b/unrar_sys/vendor/unrar/match.cpp @@ -3,6 +3,7 @@ static bool match(const wchar *pattern,const wchar *string,bool ForceCase); static int mwcsicompc(const wchar *Str1,const wchar *Str2,bool ForceCase); static int mwcsnicompc(const wchar *Str1,const wchar *Str2,size_t N,bool ForceCase); +static bool IsWildcard(const wchar *Str,size_t CheckSize); inline uint touppercw(uint ch,bool ForceCase) { @@ -16,12 +17,15 @@ inline uint touppercw(uint ch,bool ForceCase) } -bool CmpName(const wchar *Wildcard,const wchar *Name,int CmpMode) +bool CmpName(const wchar *Wildcard,const wchar *Name,uint CmpMode) { bool ForceCase=(CmpMode&MATCH_FORCECASESENSITIVE)!=0; CmpMode&=MATCH_MODEMASK; + wchar *Name1=PointToName(Wildcard); + wchar *Name2=PointToName(Name); + if (CmpMode!=MATCH_NAMES) { size_t WildLength=wcslen(Wildcard); @@ -32,47 +36,48 @@ bool CmpName(const wchar *Wildcard,const wchar *Name,int CmpMode) // "path1" mask must match "path1\path2\filename.ext" and "path1" names. wchar NextCh=Name[WildLength]; if (NextCh==L'\\' || NextCh==L'/' || NextCh==0) - return(true); + return true; } // Nothing more to compare for MATCH_SUBPATHONLY. if (CmpMode==MATCH_SUBPATHONLY) - return(false); - - wchar Path1[NM],Path2[NM]; - GetFilePath(Wildcard,Path1,ASIZE(Path1)); - GetFilePath(Name,Path2,ASIZE(Path2)); - - if ((CmpMode==MATCH_EXACT || CmpMode==MATCH_EXACTPATH) && - mwcsicompc(Path1,Path2,ForceCase)!=0) - return(false); + return false; + + // 2023.08.29: We tried std::wstring Path1 and Path2 here, but performance + // impact for O(n^2) complexity loop in CmdExtract::AnalyzeArchive() + // was rather noticeable, 1.7s instead of 0.9s when extracting ~300 files + // with @listfile from archive with ~7000 files. + // This function can be invoked from other O(n^2) loops. So for now + // we prefer to avoid wstring and use pointers and path sizes here. + // Another option could be using std::wstring_view. + + size_t Path1Size=Name1-Wildcard; + size_t Path2Size=Name2-Name; + + if ((CmpMode==MATCH_EXACT || CmpMode==MATCH_EXACTPATH) && + (Path1Size!=Path2Size || + mwcsnicompc(Wildcard,Name,Path1Size,ForceCase)!=0)) + return false; if (CmpMode==MATCH_ALLWILD) return match(Wildcard,Name,ForceCase); if (CmpMode==MATCH_SUBPATH || CmpMode==MATCH_WILDSUBPATH) - if (IsWildcard(Path1)) - return(match(Wildcard,Name,ForceCase)); + if (IsWildcard(Wildcard,Path1Size)) + return match(Wildcard,Name,ForceCase); else if (CmpMode==MATCH_SUBPATH || IsWildcard(Wildcard)) { - if (*Path1 && mwcsnicompc(Path1,Path2,wcslen(Path1),ForceCase)!=0) - return(false); + if (Path1Size>0 && mwcsnicompc(Wildcard,Name,Path1Size,ForceCase)!=0) + return false; } else - if (mwcsicompc(Path1,Path2,ForceCase)!=0) - return(false); + if (Path1Size!=Path2Size || mwcsnicompc(Wildcard,Name,Path1Size,ForceCase)!=0) + return false; } - wchar *Name1=PointToName(Wildcard); - wchar *Name2=PointToName(Name); - - // Always return false for RAR temporary files to exclude them - // from archiving operations. -// if (mwcsnicompc(L"__rar_",Name2,6,false)==0) -// return(false); if (CmpMode==MATCH_EXACT) - return(mwcsicompc(Name1,Name2,ForceCase)==0); + return mwcsicompc(Name1,Name2,ForceCase)==0; - return(match(Name1,Name2,ForceCase)); + return match(Name1,Name2,ForceCase); } @@ -85,18 +90,18 @@ bool match(const wchar *pattern,const wchar *string,bool ForceCase) switch (patternc) { case 0: - return(stringc==0); + return stringc==0; case '?': if (stringc == 0) - return(false); + return false; break; case '*': if (*pattern==0) - return(true); + return true; if (*pattern=='.') { if (pattern[1]=='*' && pattern[2]==0) - return(true); + return true; const wchar *dot=wcschr(string,'.'); if (pattern[1]==0) return (dot==NULL || dot[1]==0); @@ -104,22 +109,22 @@ bool match(const wchar *pattern,const wchar *string,bool ForceCase) { string=dot; if (wcspbrk(pattern,L"*?")==NULL && wcschr(string+1,'.')==NULL) - return(mwcsicompc(pattern+1,string+1,ForceCase)==0); + return mwcsicompc(pattern+1,string+1,ForceCase)==0; } } while (*string) if (match(pattern,string++,ForceCase)) - return(true); - return(false); + return true; + return false; default: if (patternc != stringc) { // Allow "name." mask match "name" and "name.\" match "name\". if (patternc=='.' && (stringc==0 || stringc=='\\' || stringc=='.')) - return(match(pattern,string,ForceCase)); + return match(pattern,string,ForceCase); else - return(false); + return false; } break; } @@ -145,3 +150,18 @@ int mwcsnicompc(const wchar *Str1,const wchar *Str2,size_t N,bool ForceCase) return wcsnicomp(Str1,Str2,N); #endif } + + +bool IsWildcard(const wchar *Str,size_t CheckSize) +{ + size_t CheckPos=0; +#ifdef _WIN_ALL + // Not treat the special NTFS \\?\d: path prefix as a wildcard. + if (Str[0]=='\\' && Str[1]=='\\' && Str[2]=='?' && Str[3]=='\\') + CheckPos+=4; +#endif + for (size_t I=CheckPos;I= 9*sf)+(cf >= 12*sf)+(cf >= 15*sf); - pc->U.SummFreq += cf; + pc->U.SummFreq += (ushort)cf; } p=pc->U.Stats+ns1; p->Successor=Successor; p->Symbol = fs.Symbol; - p->Freq = cf; - pc->NumStats=++ns1; + p->Freq = (byte)cf; + pc->NumStats=(ushort)++ns1; } MaxContext=MinContext=fs.Successor; return; @@ -542,7 +542,7 @@ inline bool RARPPM_CONTEXT::decodeSymbol2(ModelPPM *Model) Model->CharMask[(*pps)->Symbol]=Model->EscCount; pps++; } while ( --i ); - psee2c->Summ += Model->Coder.SubRange.scale; + psee2c->Summ += (ushort)Model->Coder.SubRange.scale; Model->NumMasked = NumStats; } return true; diff --git a/unrar_sys/vendor/unrar/model.hpp b/unrar_sys/vendor/unrar/model.hpp index 52abc89..2c9fa40 100644 --- a/unrar_sys/vendor/unrar/model.hpp +++ b/unrar_sys/vendor/unrar/model.hpp @@ -25,7 +25,7 @@ struct RARPPM_SEE2_CONTEXT : RARPPM_DEF } uint getMean() { - uint RetVal=GET_SHORT16(Summ) >> Shift; + short RetVal=GET_SHORT16(Summ) >> Shift; Summ -= RetVal; return RetVal+(RetVal == 0); } diff --git a/unrar_sys/vendor/unrar/options.cpp b/unrar_sys/vendor/unrar/options.cpp index 22ae27c..ab8cff5 100644 --- a/unrar_sys/vendor/unrar/options.cpp +++ b/unrar_sys/vendor/unrar/options.cpp @@ -10,6 +10,7 @@ void RAROptions::Init() { memset(this,0,sizeof(RAROptions)); WinSize=0x2000000; + WinSizeLimit=0x100000000; Overwrite=OVERWRITE_DEFAULT; Method=3; MsgStream=MSG_STDOUT; diff --git a/unrar_sys/vendor/unrar/options.hpp b/unrar_sys/vendor/unrar/options.hpp index e249eb5..8f0f6ad 100644 --- a/unrar_sys/vendor/unrar/options.hpp +++ b/unrar_sys/vendor/unrar/options.hpp @@ -57,7 +57,13 @@ enum QOPEN_MODE { QOPEN_NONE, QOPEN_AUTO, QOPEN_ALWAYS }; enum RAR_CHARSET { RCH_DEFAULT=0,RCH_ANSI,RCH_OEM,RCH_UNICODE,RCH_UTF8 }; #define MAX_FILTER_TYPES 16 -enum FilterState {FILTER_DEFAULT=0,FILTER_AUTO,FILTER_FORCE,FILTER_DISABLE}; + +enum FilterState { + FILTER_DEFAULT=0, // No -mc switch. + FILTER_AUTO, // -mc switch is present. + FILTER_FORCE, // -mc+ switch is present. + FILTER_DISABLE // -mc- switch is present. +}; enum SAVECOPY_MODE { @@ -98,6 +104,7 @@ class RAROptions RAROptions(); void Init(); + uint ExclFileAttr; uint InclFileAttr; @@ -107,30 +114,24 @@ class RAROptions bool InclDir; bool InclAttrSet; - size_t WinSize; - wchar TempPath[NM]; - wchar SFXModule[NM]; + uint64 WinSize; + uint64 WinSizeLimit; // Switch -mdx. #ifdef USE_QOPEN QOPEN_MODE QOpenMode; #endif bool ConfigDisabled; // Switch -cfg-. - wchar ExtrPath[NM]; - wchar CommentFile[NM]; RAR_CHARSET CommentCharset; RAR_CHARSET FilelistCharset; RAR_CHARSET ErrlogCharset; RAR_CHARSET RedirectCharset; - wchar ArcPath[NM]; // For -ap. - wchar ExclArcPath[NM]; // For -ep4 switch. bool EncryptHeaders; bool SkipEncrypted; bool ManualPassword; // Password entered manually during operation, might need to clean for next archive. - wchar LogName[NM]; MESSAGE_TYPE MsgStream; SOUND_NOTIFY_MODE Sound; OVERWRITE_MODE Overwrite; @@ -164,6 +165,7 @@ class RAROptions bool SaveSymLinks; bool SaveHardLinks; bool AbsoluteLinks; + bool SkipSymLinks; int Priority; int SleepTime; bool KeepBroken; @@ -195,7 +197,6 @@ class RAROptions bool Test; bool VolumePause; FilterMode FilterModes[MAX_FILTER_TYPES]; - wchar EmailTo[NM]; uint VersionControl; APPENDARCNAME_MODE AppendArcNameToPath; POWER_MODE Shutdown; @@ -204,11 +205,6 @@ class RAROptions EXTTIME_MODE xatime; bool PreserveAtime; - // Read data from stdin and store in archive under a name specified here - // when archiving. Read an archive from stdin if any non-empty string - // is specified here when extracting. - wchar UseStdin[NM]; - uint Threads; // We use it to init hash even if RAR_SMP is not defined. @@ -216,7 +212,6 @@ class RAROptions #ifdef RARDLL - wchar DllDestName[NM]; int DllOpMode; int DllError; LPARAM UserData; @@ -224,5 +219,6 @@ class RAROptions CHANGEVOLPROC ChangeVolProc; PROCESSDATAPROC ProcessDataProc; #endif + }; #endif diff --git a/unrar_sys/vendor/unrar/os.hpp b/unrar_sys/vendor/unrar/os.hpp index 4b21e49..c399e08 100644 --- a/unrar_sys/vendor/unrar/os.hpp +++ b/unrar_sys/vendor/unrar/os.hpp @@ -4,10 +4,6 @@ #define FALSE 0 #define TRUE 1 -#ifdef __EMX__ - #define INCL_BASE -#endif - #if defined(RARDLL) && !defined(SILENT) #define SILENT #endif @@ -15,14 +11,12 @@ #include #include #include +#include // For automatic pointers. -#if defined(_WIN_ALL) || defined(_EMX) +#ifdef _WIN_ALL #define LITTLE_ENDIAN -#define NM 2048 - -#ifdef _WIN_ALL // We got a report that just "#define STRICT" is incompatible with @@ -41,15 +35,8 @@ #define _UNICODE // Set _T() macro to convert from narrow to wide strings. #endif -#if 0 -// 2021.09.05: Allow newer Vista+ APIs like IFileOpenDialog for WinRAR, -// but still keep SFX modules XP compatible. -#define WINVER _WIN32_WINNT_VISTA -#define _WIN32_WINNT _WIN32_WINNT_VISTA -#else #define WINVER _WIN32_WINNT_WINXP #define _WIN32_WINNT _WIN32_WINNT_WINXP -#endif #if !defined(ZIPSFX) #define RAR_SMP @@ -70,28 +57,23 @@ #include #include +// For WMI requests. +#include +#include +#pragma comment(lib, "wbemuuid.lib") -#endif // _WIN_ALL #include #include #include +#include +#include -#if !defined(_EMX) && !defined(_MSC_VER) - #include +// Use SSE only for x86/x64, not ARM Windows. +#if defined(_M_IX86) || defined(_M_X64) + #define USE_SSE + #define SSE_ALIGNMENT 16 #endif -#ifdef _MSC_VER - #include - #include - - // Use SSE only for x86/x64, not ARM Windows. - #if defined(_M_IX86) || defined(_M_X64) - #define USE_SSE - #define SSE_ALIGNMENT 16 - #endif -#else - #include -#endif // _MSC_VER #include #include @@ -113,7 +95,7 @@ #define SPATHDIVIDER L"\\" -#define CPATHDIVIDER '\\' +#define CPATHDIVIDER L'\\' #define MASKALL L"*" #define READBINARY "rb" @@ -123,25 +105,13 @@ #define WRITEBINARY "wb" #define APPENDTEXT "at" -#if defined(_WIN_ALL) - #ifdef _MSC_VER - #define _stdfunction __cdecl - #define _forceinline __forceinline - #else - #define _stdfunction _USERENTRY - #define _forceinline inline - #endif -#else - #define _stdfunction - #define _forceinline inline -#endif +#define _stdfunction __cdecl +#define _forceinline __forceinline -#endif // defined(_WIN_ALL) || defined(_EMX) +#endif // _WIN_ALL #ifdef _UNIX -#define NM 2048 - #include #include #include @@ -149,7 +119,7 @@ #if defined(__QNXNTO__) #include #endif -#if defined(RAR_SMP) && defined(__APPLE__) +#ifdef _APPLE #include #endif #ifndef SFX_MODULE @@ -172,6 +142,28 @@ #include #include +#ifdef __GNUC__ + #if defined(__i386__) || defined(__x86_64__) + #include + + #define USE_SSE + #define SSE_ALIGNMENT 16 + #endif +#endif + +#if defined(__aarch64__) && (defined(__ARM_FEATURE_CRYPTO) || defined(__ARM_FEATURE_CRC32)) +#include +#ifndef _APPLE +#include +#include +#endif +#ifdef __ARM_FEATURE_CRYPTO +#define USE_NEON_AES +#endif +#ifdef __ARM_FEATURE_CRC32 +#define USE_NEON_CRC32 +#endif +#endif #ifdef S_IFLNK #define SAVE_LINKS @@ -189,7 +181,7 @@ #define SPATHDIVIDER L"/" -#define CPATHDIVIDER '/' +#define CPATHDIVIDER L'/' #define MASKALL L"*" #define READBINARY "r" @@ -226,19 +218,12 @@ #endif // _UNIX -#if 0 - #define MSGID_INT - typedef int MSGID; -#else typedef const wchar* MSGID; -#endif #ifndef SSE_ALIGNMENT // No SSE use and no special data alignment is required. #define SSE_ALIGNMENT 1 #endif -#define safebuf static - // Solaris defines _LITTLE_ENDIAN or _BIG_ENDIAN. #if defined(_LITTLE_ENDIAN) && !defined(LITTLE_ENDIAN) #define LITTLE_ENDIAN @@ -269,8 +254,8 @@ #endif #endif -#if !defined(BIG_ENDIAN) && defined(_WIN_ALL) || defined(__i386__) || defined(__x86_64__) -// Allow not aligned integer access, increases speed in some operations. +#if !defined(BIG_ENDIAN) && defined(_WIN_ALL) || defined(__i386__) || defined(__x86_64__) || defined(__aarch64__) +// Allow unaligned integer access, increases speed in some operations. #define ALLOW_MISALIGNED #endif diff --git a/unrar_sys/vendor/unrar/pathfn.cpp b/unrar_sys/vendor/unrar/pathfn.cpp index e959e9d..7c0f919 100644 --- a/unrar_sys/vendor/unrar/pathfn.cpp +++ b/unrar_sys/vendor/unrar/pathfn.cpp @@ -9,6 +9,21 @@ wchar* PointToName(const wchar *Path) } +std::wstring PointToName(const std::wstring &Path) +{ + return std::wstring(Path.substr(GetNamePos(Path))); +} + + +size_t GetNamePos(const std::wstring &Path) +{ + for (int I=(int)Path.size()-1;I>=0;I--) + if (IsPathDiv(Path[I])) + return I+1; + return IsDriveLetter(Path) ? 2 : 0; +} + + wchar* PointToLastChar(const wchar *Path) { size_t Length=wcslen(Path); @@ -16,94 +31,95 @@ wchar* PointToLastChar(const wchar *Path) } -wchar* ConvertPath(const wchar *SrcPath,wchar *DestPath,size_t DestSize) +wchar GetLastChar(const std::wstring &Path) +{ + return Path.empty() ? 0:Path.back(); +} + + +size_t ConvertPath(const std::wstring *SrcPath,std::wstring *DestPath) { - const wchar *DestPtr=SrcPath; + const std::wstring &S=*SrcPath; // To avoid *SrcPath[] everywhere. + size_t DestPos=0; - // Prevent \..\ in any part of path string. - for (const wchar *s=DestPtr;*s!=0;s++) - if (IsPathDiv(s[0]) && s[1]=='.' && s[2]=='.' && IsPathDiv(s[3])) - DestPtr=s+4; + // Prevent \..\ in any part of path string and \.. at the end of string + for (size_t I=0;I:\ and any sequence of . and \ in the beginning of path string. - while (*DestPtr!=0) + while (DestPos='A' && Letter<='Z' && IsDriveDiv(Path[1]); } -int GetPathDisk(const wchar *Path) +int GetPathDisk(const std::wstring &Path) { if (IsDriveLetter(Path)) - return etoupperw(*Path)-'A'; + return etoupperw(Path[0])-'A'; else return -1; } -void AddEndSlash(wchar *Path,size_t MaxLength) +void AddEndSlash(std::wstring &Path) { - size_t Length=wcslen(Path); - if (Length>0 && Path[Length-1]!=CPATHDIVIDER && Length+12) + AddEndSlash(OutName); + OutName+=Name; + Pathname=OutName; } -// Returns file path including the trailing path separator symbol. -void GetFilePath(const wchar *FullName,wchar *Path,size_t MaxLength) +// Returns the file path including the trailing path separator symbol. +// It is allowed for both parameters to point to the same string. +void GetPathWithSep(const std::wstring &FullName,std::wstring &Path) { - if (MaxLength==0) - return; - size_t PathLength=Min(MaxLength-1,size_t(PointToName(FullName)-FullName)); - wcsncpy(Path,FullName,PathLength); - Path[PathLength]=0; + if (std::addressof(FullName)!=std::addressof(Path)) + Path=FullName; + Path.erase(GetNamePos(FullName)); } -// Removes name and returns file path without the trailing -// path separator symbol. -void RemoveNameFromPath(wchar *Path) +// Removes name and returns file path without the trailing path separator. +// But for names like d:\name return d:\ with trailing path separator. +void RemoveNameFromPath(std::wstring &Path) { - wchar *Name=PointToName(Path); - if (Name>=Path+2 && (!IsDriveDiv(Path[1]) || Name>=Path+4)) - Name--; - *Name=0; + auto NamePos=GetNamePos(Path); + if (NamePos>=2 && (!IsDriveDiv(Path[1]) || NamePos>=4)) + NamePos--; + Path.erase(NamePos); } #if defined(_WIN_ALL) && !defined(SFX_MODULE) -bool GetAppDataPath(wchar *Path,size_t MaxSize,bool Create) +bool GetAppDataPath(std::wstring &Path,bool Create) { LPMALLOC g_pMalloc; SHGetMalloc(&g_pMalloc); LPITEMIDLIST ppidl; - *Path=0; + Path.clear(); bool Success=false; if (SHGetSpecialFolderLocation(NULL,CSIDL_APPDATA,&ppidl)==NOERROR && - SHGetPathFromIDList(ppidl,Path) && *Path!=0) + SHGetPathStrFromIDList(ppidl,Path) && !Path.empty()) { - AddEndSlash(Path,MaxSize); - wcsncatz(Path,L"WinRAR",MaxSize); + AddEndSlash(Path); + Path+=L"WinRAR"; Success=FileExist(Path); if (!Success && Create) - Success=MakeDir(Path,false,0)==MKDIR_SUCCESS; + Success=CreateDir(Path); } g_pMalloc->Free(ppidl); return Success; @@ -243,24 +278,41 @@ bool GetAppDataPath(wchar *Path,size_t MaxSize,bool Create) #endif +#if defined(_WIN_ALL) +bool SHGetPathStrFromIDList(PCIDLIST_ABSOLUTE pidl,std::wstring &Path) +{ + std::vector Buf(MAX_PATH); + bool Success=SHGetPathFromIDList(pidl,Buf.data())!=FALSE; + Path=Buf.data(); + return Success; +} +#endif + + #if defined(_WIN_ALL) && !defined(SFX_MODULE) -void GetRarDataPath(wchar *Path,size_t MaxSize,bool Create) +void GetRarDataPath(std::wstring &Path,bool Create) { - *Path=0; + Path.clear(); HKEY hKey; if (RegOpenKeyEx(HKEY_CURRENT_USER,L"Software\\WinRAR\\Paths",0, KEY_QUERY_VALUE,&hKey)==ERROR_SUCCESS) { - DWORD DataSize=(DWORD)MaxSize,Type; - RegQueryValueEx(hKey,L"AppData",0,&Type,(BYTE *)Path,&DataSize); - RegCloseKey(hKey); + DWORD DataSize; + LSTATUS Code=RegQueryValueEx(hKey,L"AppData",NULL,NULL,NULL,&DataSize); + if (Code==ERROR_SUCCESS) + { + std::vector PathBuf(DataSize/sizeof(wchar)); + RegQueryValueEx(hKey,L"AppData",0,NULL,(BYTE *)PathBuf.data(),&DataSize); + Path=PathBuf.data(); + RegCloseKey(hKey); + } } - if (*Path==0 || !FileExist(Path)) - if (!GetAppDataPath(Path,MaxSize,Create)) + if (Path.empty() || !FileExist(Path)) + if (!GetAppDataPath(Path,Create)) { - GetModuleFileName(NULL,Path,(DWORD)MaxSize); + Path=GetModuleFileStr(); RemoveNameFromPath(Path); } } @@ -268,7 +320,7 @@ void GetRarDataPath(wchar *Path,size_t MaxSize,bool Create) #ifndef SFX_MODULE -bool EnumConfigPaths(uint Number,wchar *Path,size_t MaxSize,bool Create) +bool EnumConfigPaths(uint Number,std::wstring &Path,bool Create) { #ifdef _UNIX static const wchar *ConfPath[]={ @@ -278,24 +330,24 @@ bool EnumConfigPaths(uint Number,wchar *Path,size_t MaxSize,bool Create) { char *EnvStr=getenv("HOME"); if (EnvStr!=NULL) - CharToWide(EnvStr,Path,MaxSize); + CharToWide(EnvStr,Path); else - wcsncpyz(Path,ConfPath[0],MaxSize); + Path=ConfPath[0]; return true; } Number--; if (Number>=ASIZE(ConfPath)) return false; - wcsncpyz(Path,ConfPath[Number], MaxSize); + Path=ConfPath[Number]; return true; #elif defined(_WIN_ALL) if (Number>1) return false; if (Number==0) - GetRarDataPath(Path,MaxSize,Create); + GetRarDataPath(Path,Create); else { - GetModuleFileName(NULL,Path,(DWORD)MaxSize); + Path=GetModuleFileStr(); RemoveNameFromPath(Path); } return true; @@ -307,13 +359,15 @@ bool EnumConfigPaths(uint Number,wchar *Path,size_t MaxSize,bool Create) #ifndef SFX_MODULE -void GetConfigName(const wchar *Name,wchar *FullName,size_t MaxSize,bool CheckExist,bool Create) +void GetConfigName(const std::wstring &Name,std::wstring &FullName,bool CheckExist,bool Create) { - *FullName=0; - for (uint I=0;EnumConfigPaths(I,FullName,MaxSize,Create);I++) + FullName.clear(); + for (uint I=0;;I++) { - AddEndSlash(FullName,MaxSize); - wcsncatz(FullName,Name,MaxSize); + std::wstring ConfPath; + if (!EnumConfigPaths(I,ConfPath,Create)) + break; + MakeName(ConfPath,Name,FullName); if (!CheckExist || WildFileExist(FullName)) break; } @@ -321,112 +375,107 @@ void GetConfigName(const wchar *Name,wchar *FullName,size_t MaxSize,bool CheckEx #endif -// Returns a pointer to rightmost digit of volume number or to beginning +// Returns the position to rightmost digit of volume number or beginning // of file name if numeric part is missing. -wchar* GetVolNumPart(const wchar *ArcName) +size_t GetVolNumPos(const std::wstring &ArcName) { // We do not want to increment any characters in path component. - ArcName=PointToName(ArcName); + size_t NamePos=GetNamePos(ArcName); - if (*ArcName==0) - return (wchar *)ArcName; + if (NamePos==ArcName.size()) + return NamePos; // Pointing to last name character. - const wchar *ChPtr=ArcName+wcslen(ArcName)-1; + size_t Pos=ArcName.size()-1; // Skipping the archive extension. - while (!IsDigit(*ChPtr) && ChPtr>ArcName) - ChPtr--; + while (!IsDigit(ArcName[Pos]) && Pos>NamePos) + Pos--; // Skipping the numeric part of name. - const wchar *NumPtr=ChPtr; - while (IsDigit(*NumPtr) && NumPtr>ArcName) - NumPtr--; + size_t NumPos=Pos; + while (IsDigit(ArcName[NumPos]) && NumPos>NamePos) + NumPos--; // Searching for first numeric part in names like name.part##of##.rar. // Stop search on the first dot. - while (NumPtr>ArcName && *NumPtr!='.') + while (NumPos>NamePos && ArcName[NumPos]!='.') { - if (IsDigit(*NumPtr)) + if (IsDigit(ArcName[NumPos])) { // Validate the first numeric part only if it has a dot somewhere // before it. - const wchar *Dot=wcschr(ArcName,'.'); - if (Dot!=NULL && Dot|\"")==NULL; + return !Name.empty() && Name.find_first_of(L"?*<>|\"")==std::wstring::npos; } - - -void MakeNameUsable(wchar *Name,bool Extended) +void MakeNameUsable(std::wstring &Name,bool Extended) { - for (wchar *s=Name;*s!=0;s++) + for (size_t I=0;I|\"":L"?*",*s)!=NULL || Extended && (uint)*s<32) - *s='_'; + if (wcschr(Extended ? L"?*<>|\"":L"?*",Name[I])!=NULL || + Extended && (uint)Name[I]<32) + Name[I]='_'; #ifdef _UNIX // We were asked to apply Windows-like conversion in Linux in case // files are unpacked to Windows share. This code is invoked only @@ -478,26 +526,24 @@ void MakeNameUsable(wchar *Name,bool Extended) { // Windows shares in Unix do not allow the drive letter, // so unlike Windows version, we check all characters here. - if (*s==':') - *s='_'; + if (Name[I]==':') + Name[I]='_'; // No spaces or dots before the path separator are allowed on Windows // shares. But they are allowed and automatically removed at the end of - // file or folder name, so it is useless to replace them here. + // file or folder name, so we need to replace them only before + // the path separator, but not at the end of file name. // Since such files or folders are created successfully, a supposed - // conversion here would never be invoked. - if ((*s==' ' || *s=='.') && IsPathDiv(s[1])) - *s='_'; + // conversion at the end of file name would never be invoked here. + // While converting dots, we preserve "." and ".." path components, + // such as when specifying ".." in the destination path. + if (IsPathDiv(Name[I+1]) && (Name[I]==' ' || Name[I]=='.' && I>0 && + !IsPathDiv(Name[I-1]) && (Name[I-1]!='.' || I>1 && !IsPathDiv(Name[I-2])))) + Name[I]='_'; } #else - if (s-Name>1 && *s==':') - *s='_'; -#if 0 // We already can create such files. - // Remove ' ' and '.' before path separator, but allow .\ and ..\. - if (IsPathDiv(s[1]) && (*s==' ' || *s=='.' && s>Name && - !IsPathDiv(s[-1]) && (s[-1]!='.' || s>Name+1 && !IsPathDiv(s[-2])))) - *s='_'; -#endif + if (I>1 && Name[I]==':') + Name[I]='_'; #endif } } @@ -512,20 +558,38 @@ void UnixSlashToDos(const char *SrcName,char *DestName,size_t MaxLength) } -void DosSlashToUnix(const char *SrcName,char *DestName,size_t MaxLength) +void UnixSlashToDos(const wchar *SrcName,wchar *DestName,size_t MaxLength) { size_t Copied=0; for (;Copied0) - *Dest=0; + Dest.clear(); return; } #ifdef _WIN_ALL { - wchar FullName[NM],*NamePtr; - DWORD Code=GetFullPathName(Src,ASIZE(FullName),FullName,&NamePtr); - if (Code==0 || Code>ASIZE(FullName)) + DWORD Code=GetFullPathName(Src.c_str(),0,NULL,NULL); // Get the buffer size. + if (Code!=0) { - wchar LongName[NM]; - if (GetWinLongPath(Src,LongName,ASIZE(LongName))) - Code=GetFullPathName(LongName,ASIZE(FullName),FullName,&NamePtr); + std::vector FullName(Code); + Code=GetFullPathName(Src.c_str(),(DWORD)FullName.size(),FullName.data(),NULL); + + if (Code>0 && Code<=FullName.size()) + { + Dest=FullName.data(); + return; + } } - if (Code!=0 && Code FullName(Code); + Code=GetFullPathName(LongName.c_str(),(DWORD)FullName.size(),FullName.data(),NULL); + + if (Code>0 && Code<=FullName.size()) + { + Dest=FullName.data(); + return; + } + } + } + if (Src!=Dest) + Dest=Src; // Copy source to destination in case of failure. } #elif defined(_UNIX) if (IsFullPath(Src)) - *Dest=0; + Dest.clear(); else { - char CurDirA[NM]; - if (getcwd(CurDirA,ASIZE(CurDirA))==NULL) - *CurDirA=0; - CharToWide(CurDirA,Dest,MaxSize); - AddEndSlash(Dest,MaxSize); + std::vector CurDirA(MAXPATHSIZE); + if (getcwd(CurDirA.data(),CurDirA.size())==NULL) + CurDirA[0]=0; + CharToWide(CurDirA.data(),Dest); + AddEndSlash(Dest); } - wcsncatz(Dest,Src,MaxSize); + Dest+=Src; #else - wcsncpyz(Dest,Src,MaxSize); + Dest=Src; #endif } -bool IsFullPath(const wchar *Path) +bool IsFullPath(const std::wstring &Path) { -/* - wchar PathOnly[NM]; - GetFilePath(Path,PathOnly,ASIZE(PathOnly)); - if (IsWildcard(PathOnly)) - return true; -*/ -#if defined(_WIN_ALL) || defined(_EMX) - return Path[0]=='\\' && Path[1]=='\\' || IsDriveLetter(Path) && IsPathDiv(Path[2]); +#ifdef _WIN_ALL + return Path.size()>=2 && Path[0]=='\\' && Path[1]=='\\' || + Path.size()>=3 && IsDriveLetter(Path) && IsPathDiv(Path[2]); #else - return IsPathDiv(Path[0]); + return Path.size()>=1 && IsPathDiv(Path[0]); #endif } -bool IsFullRootPath(const wchar *Path) +bool IsFullRootPath(const std::wstring &Path) { return IsFullPath(Path) || IsPathDiv(Path[0]); } -void GetPathRoot(const wchar *Path,wchar *Root,size_t MaxSize) +// Both source and destination can point to the same string. +void GetPathRoot(const std::wstring &Path,std::wstring &Root) { - *Root=0; if (IsDriveLetter(Path)) - swprintf(Root,MaxSize,L"%c:\\",*Path); + Root=Path.substr(0,2) + L"\\"; else if (Path[0]=='\\' && Path[1]=='\\') { - const wchar *Slash=wcschr(Path+2,'\\'); - if (Slash!=NULL) + size_t Slash=Path.find('\\',2); + if (Slash!=std::wstring::npos) { size_t Length; - if ((Slash=wcschr(Slash+1,'\\'))!=NULL) - Length=Slash-Path+1; + if ((Slash=Path.find('\\',Slash+1))!=std::wstring::npos) + Length=Slash+1; else - Length=wcslen(Path); - if (Length>=MaxSize) - Length=0; - wcsncpy(Root,Path,Length); - Root[Length]=0; + Length=Path.size(); + Root=Path.substr(0,Length); } } + else + Root.clear(); } -int ParseVersionFileName(wchar *Name,bool Truncate) +int ParseVersionFileName(std::wstring &Name,bool Truncate) { int Version=0; - wchar *VerText=wcsrchr(Name,';'); - if (VerText!=NULL) + auto VerPos=Name.rfind(';'); + if (VerPos!=std::wstring::npos && VerPos+1FirstName;ChPtr--) - if (IsDigit(*ChPtr)) + for (size_t Pos=GetVolNumPos(Name);Pos>0;Pos--) + if (IsDigit(Name[Pos])) { - *ChPtr=N; // Set the rightmost digit to '1' and others to '0'. + Name[Pos]=N; // Set the rightmost digit to '1' and others to '0'. N='0'; } else - if (N=='0') + if (N=='0') // If we already set the rightmost '1' before. { - VolNumStart=ChPtr+1; // Store the position of leftmost digit in volume number. + VolNumStart=Pos+1; // Store the position of leftmost digit in volume number. break; } } else { // Old volume numbering scheme. Just set the extension to ".rar". - SetExt(FirstName,L"rar",MaxSize); - VolNumStart=GetExt(FirstName); + SetExt(Name,L"rar"); + VolNumStart=GetExtPos(Name); } - if (!FileExist(FirstName)) + if (!FileExist(Name)) { // If the first volume, which name we just generated, does not exist, // check if volume with same name and any other extension is available. // It can help in case of *.exe or *.sfx first volume. - wchar Mask[NM]; - wcsncpyz(Mask,FirstName,ASIZE(Mask)); - SetExt(Mask,L"*",ASIZE(Mask)); + std::wstring Mask=Name; + SetExt(Mask,L"*"); FindFile Find; Find.SetMask(Mask); FindData FD; @@ -689,32 +782,33 @@ wchar* VolNameToFirstName(const wchar *VolName,wchar *FirstName,size_t MaxSize,b Archive Arc; if (Arc.Open(FD.Name,0) && Arc.IsArchive(true) && Arc.FirstVolume) { - wcsncpyz(FirstName,FD.Name,MaxSize); + Name=FD.Name; break; } } } + FirstName=Name; return VolNumStart; } #endif #ifndef SFX_MODULE -static void GenArcName(wchar *ArcName,size_t MaxSize,const wchar *GenerateMask,uint ArcNumber,bool &ArcNumPresent) +static void GenArcName(std::wstring &ArcName,const std::wstring &GenerateMask,uint ArcNumber,bool &ArcNumPresent) { + size_t Pos=0; bool Prefix=false; - if (*GenerateMask=='+') + if (GenerateMask[0]=='+') { Prefix=true; // Add the time string before the archive name. - GenerateMask++; // Skip '+' in the beginning of time mask. + Pos++; // Skip '+' in the beginning of time mask. } - wchar Mask[MAX_GENERATE_MASK]; - wcsncpyz(Mask,*GenerateMask!=0 ? GenerateMask:L"yyyymmddhhmmss",ASIZE(Mask)); + std::wstring Mask=!GenerateMask.empty() ? GenerateMask.substr(Pos):L"yyyymmddhhmmss"; bool QuoteMode=false; uint MAsMinutes=0; // By default we treat 'M' as months. - for (uint I=0;Mask[I]!=0;I++) + for (uint I=0;I=4) CurWeek++; - char Field[10][11]; + const size_t FieldSize=11; + char Field[10][FieldSize]; - sprintf(Field[0],"%04u",rlt.Year); - sprintf(Field[1],"%02u",rlt.Month); - sprintf(Field[2],"%02u",rlt.Day); - sprintf(Field[3],"%02u",rlt.Hour); - sprintf(Field[4],"%02u",rlt.Minute); - sprintf(Field[5],"%02u",rlt.Second); - sprintf(Field[6],"%02u",(uint)CurWeek); - sprintf(Field[7],"%u",(uint)WeekDay+1); - sprintf(Field[8],"%03u",rlt.yDay+1); - sprintf(Field[9],"%05u",ArcNumber); + snprintf(Field[0],FieldSize,"%04u",rlt.Year); + snprintf(Field[1],FieldSize,"%02u",rlt.Month); + snprintf(Field[2],FieldSize,"%02u",rlt.Day); + snprintf(Field[3],FieldSize,"%02u",rlt.Hour); + snprintf(Field[4],FieldSize,"%02u",rlt.Minute); + snprintf(Field[5],FieldSize,"%02u",rlt.Second); + snprintf(Field[6],FieldSize,"%02u",(uint)CurWeek); + snprintf(Field[7],FieldSize,"%u",(uint)WeekDay+1); + snprintf(Field[8],FieldSize,"%03u",rlt.yDay+1); + snprintf(Field[9],FieldSize,"%05u",ArcNumber); const wchar *MaskChars=L"YMDHISWAEN"; // How many times every modifier character was encountered in the mask. - int CField[sizeof(Field)/sizeof(Field[0])]; + int CField[sizeof(Field)/sizeof(Field[0])]{}; - memset(CField,0,sizeof(CField)); QuoteMode=false; - for (uint I=0;Mask[I]!=0;I++) + for (uint I=0;I0) - DestW[DestSize-1]=0; - - return DestW; -} - - #ifdef _WIN_ALL // We should return 'true' even if resulting path is shorter than MAX_PATH, // because we can also use this function to open files with non-standard // characters, even if their path length is normal. -bool GetWinLongPath(const wchar *Src,wchar *Dest,size_t MaxSize) +bool GetWinLongPath(const std::wstring &Src,std::wstring &Dest) { - if (*Src==0) + if (Src.empty()) return false; - const wchar *Prefix=L"\\\\?\\"; - const size_t PrefixLength=4; - bool FullPath=IsDriveLetter(Src) && IsPathDiv(Src[2]); - size_t SrcLength=wcslen(Src); + const std::wstring Prefix=L"\\\\?\\"; + + bool FullPath=Src.size()>=3 && IsDriveLetter(Src) && IsPathDiv(Src[2]); if (IsFullPath(Src)) // Paths in d:\path\name format. { if (IsDriveLetter(Src)) { - if (MaxSize<=PrefixLength+SrcLength) - return false; - wcsncpyz(Dest,Prefix,MaxSize); - wcsncatz(Dest,Src,MaxSize); // "\\?\D:\very long path". + Dest=Prefix+Src; // "\\?\D:\very long path". return true; } else - if (Src[0]=='\\' && Src[1]=='\\') + if (Src.size()>2 && Src[0]=='\\' && Src[1]=='\\') { - if (MaxSize<=PrefixLength+SrcLength+2) - return false; - wcsncpyz(Dest,Prefix,MaxSize); - wcsncatz(Dest,L"UNC",MaxSize); - wcsncatz(Dest,Src+1,MaxSize); // "\\?\UNC\server\share". + Dest=Prefix+L"UNC"+Src.substr(1); // "\\?\UNC\server\share". return true; } - // We may be here only if we modify IsFullPath in the future. + // We can be here only if modify IsFullPath() in the future. return false; } else { - wchar CurDir[NM]; - DWORD DirCode=GetCurrentDirectory(ASIZE(CurDir)-1,CurDir); - if (DirCode==0 || DirCode>ASIZE(CurDir)-1) + std::wstring CurDir; + if (!GetCurDir(CurDir)) return false; if (IsPathDiv(Src[0])) // Paths in \path\name format. { - if (MaxSize<=PrefixLength+SrcLength+2) - return false; - wcsncpyz(Dest,Prefix,MaxSize); - CurDir[2]=0; - wcsncatz(Dest,CurDir,MaxSize); // Copy drive letter 'd:'. - wcsncatz(Dest,Src,MaxSize); + Dest=Prefix+CurDir[0]+L':'+Src; // Copy drive letter 'd:'. return true; } else // Paths in path\name format. { - AddEndSlash(CurDir,ASIZE(CurDir)); - if (MaxSize<=PrefixLength+wcslen(CurDir)+SrcLength) - return false; - wcsncpyz(Dest,Prefix,MaxSize); - wcsncatz(Dest,CurDir,MaxSize); + Dest=Prefix+CurDir; + AddEndSlash(Dest); + size_t Pos=0; if (Src[0]=='.' && IsPathDiv(Src[1])) // Remove leading .\ in pathname. - Src+=2; + Pos=2; - wcsncatz(Dest,Src,MaxSize); + Dest+=Src.substr(Pos); return true; } } @@ -1008,46 +1061,55 @@ bool GetWinLongPath(const wchar *Src,wchar *Dest,size_t MaxSize) // Convert Unix, OS X and Android decomposed chracters to Windows precomposed. -void ConvertToPrecomposed(wchar *Name,size_t NameSize) +void ConvertToPrecomposed(std::wstring &Name) { - wchar FileName[NM]; - if (WinNT()>=WNT_VISTA && // MAP_PRECOMPOSED is not supported in XP. - FoldString(MAP_PRECOMPOSED,Name,-1,FileName,ASIZE(FileName))!=0) - { - FileName[ASIZE(FileName)-1]=0; - wcsncpyz(Name,FileName,NameSize); - } + if (WinNT() FileName(Size); + if (FoldString(MAP_PRECOMPOSED,Name.c_str(),-1,FileName.data(),(int)FileName.size())!=0) + Name=FileName.data(); } -void MakeNameCompatible(wchar *Name,size_t MaxSize) +void MakeNameCompatible(std::wstring &Name) { // Remove trailing spaces and dots in file name and in dir names in path. - int Src=0,Dest=0; - while (true) - { - if (IsPathDiv(Name[Src]) || Name[Src]==0) - for (int I=Dest-1;I>0 && (Name[I]==' ' || Name[I]=='.');I--) + for (int I=0;I<(int)Name.size();I++) + if (I+1==Name.size() || IsPathDiv(Name[I+1])) + while (I>=0 && (Name[I]=='.' || Name[I]==' ')) { - // Permit path1/./path2 and ../path1 paths. - if (Name[I]=='.' && (IsPathDiv(Name[I-1]) || Name[I-1]=='.' && I==1)) + if (I==0 && Name[I]==' ') + { + // Windows 10 Explorer can't rename or delete " " files and folders. + Name[I]='_'; // Convert " /path" to "_/path". break; - Dest--; + } + if (Name[I]=='.') + { + // 2024.05.01: Permit ./path1, path1/./path2, ../path1, + // path1/../path2 and exotic Win32 d:.\path1, d:..\path1 paths + // requested by user. Leading dots are possible here if specified + // by user in the destination path. + if (I==0 || IsPathDiv(Name[I-1]) || I==2 && IsDriveLetter(Name)) + break; + if (I>=1 && Name[I-1]=='.' && (I==1 || IsPathDiv(Name[I-2]) || + I==3 && IsDriveLetter(Name))) + break; + } + Name.erase(I,1); + I--; } - Name[Dest]=Name[Src]; - if (Name[Src]==0) - break; - Src++; - Dest++; - } // Rename reserved device names, such as aux.txt to _aux.txt. // We check them in path components too, where they are also prohibited. - for (uint I=0;Name[I]!=0;I++) + for (size_t I=0;I0 && IsPathDiv(Name[I-1])) { static const wchar *Devices[]={L"CON",L"PRN",L"AUX",L"NUL",L"COM#",L"LPT#"}; - wchar *s=Name+I; + const wchar *s=&Name[I]; bool MatchFound=false; for (uint J=0;JI+1) // I+1, because we do not move the trailing 0. - memmove(s+1,s,(MaxSize-I-1)*sizeof(*s)); - *s='_'; + std::wstring OrigName=Name; + Name.insert(I,1,'_'); #ifndef SFX_MODULE uiMsg(UIMSG_CORRECTINGNAME,nullptr); uiMsg(UIERROR_RENAMING,nullptr,OrigName,Name); @@ -1084,3 +1143,54 @@ void MakeNameCompatible(wchar *Name,size_t MaxSize) #endif + + +#ifdef _WIN_ALL +std::wstring GetModuleFileStr() +{ + HMODULE hModule=nullptr; + + std::vector Path(256); + while (Path.size()<=MAXPATHSIZE) + { + if (GetModuleFileName(hModule,Path.data(),(DWORD)Path.size()) Buf(BufSize); + DWORD Code=GetCurrentDirectory((DWORD)Buf.size(),Buf.data()); + Dir=Buf.data(); + return Code!=0; +} +#endif + + diff --git a/unrar_sys/vendor/unrar/pathfn.hpp b/unrar_sys/vendor/unrar/pathfn.hpp index 62cae0a..57dab5a 100644 --- a/unrar_sys/vendor/unrar/pathfn.hpp +++ b/unrar_sys/vendor/unrar/pathfn.hpp @@ -2,39 +2,52 @@ #define _RAR_PATHFN_ wchar* PointToName(const wchar *Path); +std::wstring PointToName(const std::wstring &Path); +size_t GetNamePos(const std::wstring &Path); wchar* PointToLastChar(const wchar *Path); -wchar* ConvertPath(const wchar *SrcPath,wchar *DestPath,size_t DestSize); -void SetName(wchar *FullName,const wchar *Name,size_t MaxSize); -void SetExt(wchar *Name,const wchar *NewExt,size_t MaxSize); -void SetSFXExt(wchar *SFXName,size_t MaxSize); +wchar GetLastChar(const std::wstring &Path); +size_t ConvertPath(const std::wstring *SrcPath,std::wstring *DestPath); +void SetName(std::wstring &FullName,const std::wstring &Name); +void SetExt(std::wstring &Name,std::wstring NewExt); +void RemoveExt(std::wstring &Name); +void SetSFXExt(std::wstring &SFXName); wchar *GetExt(const wchar *Name); -bool CmpExt(const wchar *Name,const wchar *Ext); -bool IsWildcard(const wchar *Str); +std::wstring GetExt(const std::wstring &Name); +std::wstring::size_type GetExtPos(const std::wstring &Name); +bool CmpExt(const std::wstring &Name,const std::wstring &Ext); +bool IsWildcard(const std::wstring &Str); bool IsPathDiv(int Ch); bool IsDriveDiv(int Ch); -bool IsDriveLetter(const wchar *Path); -int GetPathDisk(const wchar *Path); -void AddEndSlash(wchar *Path,size_t MaxLength); -void MakeName(const wchar *Path,const wchar *Name,wchar *Pathname,size_t MaxSize); -void GetFilePath(const wchar *FullName,wchar *Path,size_t MaxLength); -void RemoveNameFromPath(wchar *Path); +bool IsDriveLetter(const std::wstring &Path); +int GetPathDisk(const std::wstring &Path); +void AddEndSlash(std::wstring &Path); +void MakeName(const std::wstring &Path,const std::wstring &Name,std::wstring &Pathname); +void GetPathWithSep(const std::wstring &FullName,std::wstring &Path); +void RemoveNameFromPath(std::wstring &Path); #if defined(_WIN_ALL) && !defined(SFX_MODULE) -bool GetAppDataPath(wchar *Path,size_t MaxSize,bool Create); -void GetRarDataPath(wchar *Path,size_t MaxSize,bool Create); +bool GetAppDataPath(std::wstring &Path,bool Create); +void GetRarDataPath(std::wstring &Path,bool Create); +#endif +#ifdef _WIN_ALL +bool SHGetPathStrFromIDList(PCIDLIST_ABSOLUTE pidl,std::wstring &Path); #endif #ifndef SFX_MODULE -bool EnumConfigPaths(uint Number,wchar *Path,size_t MaxSize,bool Create); -void GetConfigName(const wchar *Name,wchar *FullName,size_t MaxSize,bool CheckExist,bool Create); +bool EnumConfigPaths(uint Number,std::wstring &Path,bool Create); +void GetConfigName(const std::wstring &Name,std::wstring &FullName,bool CheckExist,bool Create); #endif -wchar* GetVolNumPart(const wchar *ArcName); -void NextVolumeName(wchar *ArcName,uint MaxLength,bool OldNumbering); -bool IsNameUsable(const wchar *Name); -void MakeNameUsable(wchar *Name,bool Extended); +size_t GetVolNumPos(const std::wstring &ArcName); +void NextVolumeName(std::wstring &ArcName,bool OldNumbering); +bool IsNameUsable(const std::wstring &Name); +void MakeNameUsable(std::wstring &Name,bool Extended); void UnixSlashToDos(const char *SrcName,char *DestName,size_t MaxLength); -void DosSlashToUnix(const char *SrcName,char *DestName,size_t MaxLength); void UnixSlashToDos(const wchar *SrcName,wchar *DestName,size_t MaxLength); +void UnixSlashToDos(const std::string &SrcName,std::string &DestName); +void UnixSlashToDos(const std::wstring &SrcName,std::wstring &DestName); +void DosSlashToUnix(const char *SrcName,char *DestName,size_t MaxLength); void DosSlashToUnix(const wchar *SrcName,wchar *DestName,size_t MaxLength); +void DosSlashToUnix(const std::string &SrcName,std::string &DestName); +void DosSlashToUnix(const std::wstring &SrcName,std::wstring &DestName); inline void SlashToNative(const char *SrcName,char *DestName,size_t MaxLength) { @@ -45,6 +58,15 @@ inline void SlashToNative(const char *SrcName,char *DestName,size_t MaxLength) #endif } +inline void SlashToNative(const std::string &SrcName,std::string &DestName) +{ +#ifdef _WIN_ALL + UnixSlashToDos(SrcName,DestName); +#else + DosSlashToUnix(SrcName,DestName); +#endif +} + inline void SlashToNative(const wchar *SrcName,wchar *DestName,size_t MaxLength) { #ifdef _WIN_ALL @@ -54,22 +76,44 @@ inline void SlashToNative(const wchar *SrcName,wchar *DestName,size_t MaxLength) #endif } -void ConvertNameToFull(const wchar *Src,wchar *Dest,size_t MaxSize); -bool IsFullPath(const wchar *Path); -bool IsFullRootPath(const wchar *Path); -void GetPathRoot(const wchar *Path,wchar *Root,size_t MaxSize); -int ParseVersionFileName(wchar *Name,bool Truncate); -wchar* VolNameToFirstName(const wchar *VolName,wchar *FirstName,size_t MaxSize,bool NewNumbering); -wchar* GetWideName(const char *Name,const wchar *NameW,wchar *DestW,size_t DestSize); +inline void SlashToNative(const std::wstring &SrcName,std::wstring &DestName) +{ +#ifdef _WIN_ALL + UnixSlashToDos(SrcName,DestName); +#else + DosSlashToUnix(SrcName,DestName); +#endif +} + +void ConvertNameToFull(const std::wstring &Src,std::wstring &Dest); +bool IsFullPath(const std::wstring &Path); +bool IsFullRootPath(const std::wstring &Path); +void GetPathRoot(const std::wstring &Path,std::wstring &Root); +int ParseVersionFileName(std::wstring &Name,bool Truncate); +size_t VolNameToFirstName(const std::wstring &VolName,std::wstring &FirstName,bool NewNumbering); #ifndef SFX_MODULE -void GenerateArchiveName(wchar *ArcName,size_t MaxSize,const wchar *GenerateMask,bool Archiving); +void GenerateArchiveName(std::wstring &ArcName,const std::wstring &GenerateMask,bool Archiving); +#endif + +#ifdef _WIN_ALL +bool GetWinLongPath(const std::wstring &Src,std::wstring &Dest); +void ConvertToPrecomposed(std::wstring &Name); +void MakeNameCompatible(std::wstring &Name); +#endif + + +#ifdef _WIN_ALL +std::wstring GetModuleFileStr(); +std::wstring GetProgramFile(const std::wstring &Name); +#endif + +#if defined(_WIN_ALL) +bool SetCurDir(const std::wstring &Dir); #endif #ifdef _WIN_ALL -bool GetWinLongPath(const wchar *Src,wchar *Dest,size_t MaxSize); -void ConvertToPrecomposed(wchar *Name,size_t NameSize); -void MakeNameCompatible(wchar *Name,size_t MaxSize); +bool GetCurDir(std::wstring &Dir); #endif diff --git a/unrar_sys/vendor/unrar/qopen.cpp b/unrar_sys/vendor/unrar/qopen.cpp index d906d06..e4ae8a9 100644 --- a/unrar_sys/vendor/unrar/qopen.cpp +++ b/unrar_sys/vendor/unrar/qopen.cpp @@ -114,7 +114,7 @@ void QuickOpen::Load(uint64 BlockPos) RawDataPos=0; ReadBufSize=0; ReadBufPos=0; - LastReadHeader.Reset(); + LastReadHeader.clear(); LastReadHeaderPos=0; ReadBuffer(); @@ -126,7 +126,7 @@ bool QuickOpen::Read(void *Data,size_t Size,size_t &Result) if (!Loaded) return false; // Find next suitable cached block. - while (LastReadHeaderPos+LastReadHeader.Size()<=SeekPos) + while (LastReadHeaderPos+LastReadHeader.size()<=SeekPos) if (!ReadNext()) break; if (!Loaded) @@ -138,9 +138,9 @@ bool QuickOpen::Read(void *Data,size_t Size,size_t &Result) return false; } - if (SeekPos>=LastReadHeaderPos && SeekPos+Size<=LastReadHeaderPos+LastReadHeader.Size()) + if (SeekPos>=LastReadHeaderPos && SeekPos+Size<=LastReadHeaderPos+LastReadHeader.size()) { - memcpy(Data,LastReadHeader+size_t(SeekPos-LastReadHeaderPos),Size); + memcpy(Data,&LastReadHeader[size_t(SeekPos-LastReadHeaderPos)],Size); Result=Size; SeekPos+=Size; UnsyncSeekPos=true; @@ -292,8 +292,8 @@ bool QuickOpen::ReadNext() size_t HeaderSize=(size_t)Raw.GetV(); if (HeaderSize>MAX_HEADER_SIZE_RAR5) return false; - LastReadHeader.Alloc(HeaderSize); - Raw.GetB(&LastReadHeader[0],HeaderSize); + LastReadHeader.resize(HeaderSize); + Raw.GetB(LastReadHeader.data(),HeaderSize); // Calculate the absolute position as offset from quick open service header. LastReadHeaderPos=QOHeaderPos-Offset; return true; diff --git a/unrar_sys/vendor/unrar/qopen.hpp b/unrar_sys/vendor/unrar/qopen.hpp index d745cea..7adca4b 100644 --- a/unrar_sys/vendor/unrar/qopen.hpp +++ b/unrar_sys/vendor/unrar/qopen.hpp @@ -43,7 +43,7 @@ class QuickOpen uint64 RawDataPos; // Current read position in QO data. size_t ReadBufSize; // Size of Buf data currently read from QO. size_t ReadBufPos; // Current read position in Buf data. - Array LastReadHeader; + std::vector LastReadHeader; uint64 LastReadHeaderPos; uint64 SeekPos; bool UnsyncSeekPos; // QOpen SeekPos does not match an actual file pointer. diff --git a/unrar_sys/vendor/unrar/rar.cpp b/unrar_sys/vendor/unrar/rar.cpp index 34b4b27..075e768 100644 --- a/unrar_sys/vendor/unrar/rar.cpp +++ b/unrar_sys/vendor/unrar/rar.cpp @@ -12,11 +12,11 @@ int main(int argc, char *argv[]) ErrHandler.SetSignalHandlers(true); #ifdef SFX_MODULE - wchar ModuleName[NM]; + std::wstring ModuleName; #ifdef _WIN_ALL - GetModuleFileName(NULL,ModuleName,ASIZE(ModuleName)); + ModuleName=GetModuleFileStr(); #else - CharToWide(argv[0],ModuleName,ASIZE(ModuleName)); + CharToWide(argv[0],ModuleName); #endif #endif @@ -35,9 +35,10 @@ int main(int argc, char *argv[]) try { - CommandData *Cmd=new CommandData; + // Use std::unique_ptr to free Cmd in case of exception. + std::unique_ptr Cmd(new CommandData); #ifdef SFX_MODULE - wcsncpyz(Cmd->Command,L"X",ASIZE(Cmd->Command)); + Cmd->Command=L"X"; char *Switch=argc>1 ? argv[1]:NULL; if (Switch!=NULL && Cmd->IsSwitch(Switch[0])) { @@ -68,7 +69,7 @@ int main(int argc, char *argv[]) #if defined(_WIN_ALL) && !defined(SFX_MODULE) ShutdownOnClose=Cmd->Shutdown; - if (ShutdownOnClose) + if (ShutdownOnClose!=POWERMODE_KEEP) ShutdownCheckAnother(true); #endif @@ -78,7 +79,6 @@ int main(int argc, char *argv[]) Cmd->OutTitle(); Cmd->ProcessCommand(); - delete Cmd; } catch (RAR_EXIT ErrCode) { @@ -100,6 +100,7 @@ int main(int argc, char *argv[]) Shutdown(ShutdownOnClose); #endif ErrHandler.MainExit=true; + CloseLogOptions(); return ErrHandler.GetErrorCode(); } #endif diff --git a/unrar_sys/vendor/unrar/rar.hpp b/unrar_sys/vendor/unrar/rar.hpp index 67edb67..38cd744 100644 --- a/unrar_sys/vendor/unrar/rar.hpp +++ b/unrar_sys/vendor/unrar/rar.hpp @@ -16,7 +16,6 @@ #include "unicode.hpp" #include "errhnd.hpp" #include "secpassword.hpp" -#include "array.hpp" #include "strlist.hpp" #include "timefn.hpp" #include "sha1.hpp" @@ -85,9 +84,6 @@ #include "global.hpp" -#if 0 -#include "benchmark.hpp" -#endif diff --git a/unrar_sys/vendor/unrar/rardefs.hpp b/unrar_sys/vendor/unrar/rardefs.hpp index 6858d39..433010f 100644 --- a/unrar_sys/vendor/unrar/rardefs.hpp +++ b/unrar_sys/vendor/unrar/rardefs.hpp @@ -17,13 +17,28 @@ #define MAXPASSWORD 512 #define MAXPASSWORD_RAR 128 +// Set some arbitrary sensible limit to maximum path length to prevent +// the excessive memory allocation for dynamically allocated strings. +#define MAXPATHSIZE 0x10000 + #define MAXSFXSIZE 0x200000 #define MAXCMTSIZE 0x40000 +#ifdef _WIN_32 +#define DefSFXName L"default32.sfx" +#else #define DefSFXName L"default.sfx" +#endif #define DefSortListName L"rarfiles.lst" +// Maximum dictionary allowed by compression. Can be less than +// maximum dictionary supported by decompression. +#define PACK_MAX_DICT 0x1000000000ULL // 64 GB. + +// Maximum dictionary allowed by decompression. +#define UNPACK_MAX_DICT 0x1000000000ULL // 64 GB. + #ifndef SFX_MODULE #define USE_QOPEN diff --git a/unrar_sys/vendor/unrar/raros.hpp b/unrar_sys/vendor/unrar/raros.hpp index 4f4f2ae..b701637 100644 --- a/unrar_sys/vendor/unrar/raros.hpp +++ b/unrar_sys/vendor/unrar/raros.hpp @@ -1,15 +1,6 @@ #ifndef _RAR_RAROS_ #define _RAR_RAROS_ -#ifdef __EMX__ - #define _EMX -#endif - -#ifdef __DJGPP__ - #define _DJGPP - #define _EMX -#endif - #if defined(__WIN32__) || defined(_WIN32) #define _WIN_ALL // Defined for all Windows platforms, 32 and 64 bit, mobile and desktop. #ifdef _M_X64 @@ -29,7 +20,7 @@ #define _APPLE #endif -#if !defined(_EMX) && !defined(_WIN_ALL) && !defined(_BEOS) && !defined(_APPLE) +#if !defined(_WIN_ALL) && !defined(_UNIX) #define _UNIX #endif diff --git a/unrar_sys/vendor/unrar/rartypes.hpp b/unrar_sys/vendor/unrar/rartypes.hpp index 3d3111b..a612c34 100644 --- a/unrar_sys/vendor/unrar/rartypes.hpp +++ b/unrar_sys/vendor/unrar/rartypes.hpp @@ -5,7 +5,7 @@ typedef uint8_t byte; // Unsigned 8 bits. typedef uint16_t ushort; // Preferably 16 bits, but can be more. -typedef unsigned int uint; // 32 bits or more. +typedef unsigned int uint; // Preferably 32 bits, likely can be more. typedef uint32_t uint32; // 32 bits exactly. typedef int32_t int32; // Signed 32 bits exactly. typedef uint64_t uint64; // 64 bits exactly. diff --git a/unrar_sys/vendor/unrar/rarvm.cpp b/unrar_sys/vendor/unrar/rarvm.cpp index 8d8675a..c44e021 100644 --- a/unrar_sys/vendor/unrar/rarvm.cpp +++ b/unrar_sys/vendor/unrar/rarvm.cpp @@ -246,7 +246,7 @@ bool RarVM::ExecuteStandardFilter(VM_StandardFilters FilterType) } else Predicted=PrevByte; - DestData[I]=PrevByte=(byte)(Predicted-*(SrcData++)); + PrevByte=DestData[I]=(byte)(Predicted-*(SrcData++)); } } for (uint I=PosR,Border=DataSize-2;I>16); D[3]=(byte)(Field>>24); #else - *(uint32 *)Data=Field; + *(uint32 *)Data=(uint32)Field; #endif } @@ -87,18 +87,32 @@ inline uint32 RawGetBE4(const byte *m) #elif defined(USE_MEM_BYTESWAP) && (defined(__clang__) || defined(__GNUC__)) return __builtin_bswap32(*(uint32 *)m); #else - return uint32(m[0]<<24) | uint32(m[1]<<16) | uint32(m[2]<<8) | m[3]; + return uint32(m[0])<<24 | uint32(m[1])<<16 | uint32(m[2])<<8 | m[3]; +#endif +} + + +// Load 8 big endian bytes from memory and return uint64. +inline uint64 RawGetBE8(const byte *m) +{ +#if defined(USE_MEM_BYTESWAP) && defined(_MSC_VER) + return _byteswap_uint64(*(uint64 *)m); +#elif defined(USE_MEM_BYTESWAP) && (defined(__clang__) || defined(__GNUC__)) + return __builtin_bswap64(*(uint64 *)m); +#else + return uint64(m[0])<<56 | uint64(m[1])<<48 | uint64(m[2])<<40 | uint64(m[3])<<32 | + uint64(m[4])<<24 | uint64(m[5])<<16 | uint64(m[6])<<8 | m[7]; #endif } // Save integer to memory as big endian. -inline void RawPutBE4(uint32 i,byte *mem) +inline void RawPutBE4(uint i,byte *mem) { #if defined(USE_MEM_BYTESWAP) && defined(_MSC_VER) - *(uint32*)mem = _byteswap_ulong(i); + *(uint32*)mem = _byteswap_ulong((uint32)i); #elif defined(USE_MEM_BYTESWAP) && (defined(__clang__) || defined(__GNUC__)) - *(uint32*)mem = __builtin_bswap32(i); + *(uint32*)mem = __builtin_bswap32((uint32)i); #else mem[0]=byte(i>>24); mem[1]=byte(i>>16); @@ -108,6 +122,26 @@ inline void RawPutBE4(uint32 i,byte *mem) } +// Save integer to memory as big endian. +inline void RawPutBE8(uint64 i,byte *mem) +{ +#if defined(USE_MEM_BYTESWAP) && defined(_MSC_VER) + *(uint64*)mem = _byteswap_uint64(i); +#elif defined(USE_MEM_BYTESWAP) && (defined(__clang__) || defined(__GNUC__)) + *(uint64*)mem = __builtin_bswap64(i); +#else + mem[0]=byte(i>>56); + mem[1]=byte(i>>48); + mem[2]=byte(i>>40); + mem[3]=byte(i>>32); + mem[4]=byte(i>>24); + mem[5]=byte(i>>16); + mem[6]=byte(i>>8); + mem[7]=byte(i); +#endif +} + + inline uint32 ByteSwap32(uint32 i) { #ifdef _MSC_VER @@ -119,4 +153,29 @@ inline uint32 ByteSwap32(uint32 i) #endif } + + + +inline bool IsPow2(uint64 n) // Check if 'n' is power of 2. +{ + return (n & (n-1))==0; +} + + +inline uint64 GetGreaterOrEqualPow2(uint64 n) +{ + uint64 p=1; + while (pRead(&Data[FullSize],AlignedReadSize); Crypt->DecryptBlock(&Data[FullSize],AlignedReadSize); DataSize+=ReadSize==0 ? 0:Size; @@ -55,7 +55,7 @@ size_t RawRead::Read(size_t Size) #endif if (Size!=0) { - Data.Add(Size); + Data.resize(Data.size()+Size); ReadSize=SrcFile->Read(&Data[DataSize],Size); DataSize+=ReadSize; } @@ -67,7 +67,7 @@ void RawRead::Read(byte *SrcData,size_t Size) { if (Size!=0) { - Data.Add(Size); + Data.resize(Data.size()+Size); memcpy(&Data[DataSize],SrcData,Size); DataSize+=Size; } @@ -96,8 +96,7 @@ uint RawRead::Get4() { if (ReadPos+3 Data; + std::vector Data; File *SrcFile; size_t DataSize; size_t ReadPos; @@ -27,7 +27,7 @@ class RawRead uint GetCRC50(); byte* GetDataPtr() {return &Data[0];} size_t Size() {return DataSize;} - size_t PaddedSize() {return Data.Size()-DataSize;} + size_t PaddedSize() {return Data.size()-DataSize;} size_t DataLeft() {return DataSize-ReadPos;} size_t GetPos() {return ReadPos;} void SetPos(size_t Pos) {ReadPos=Pos;} diff --git a/unrar_sys/vendor/unrar/rdwrfn.cpp b/unrar_sys/vendor/unrar/rdwrfn.cpp index fa71376..1f00688 100644 --- a/unrar_sys/vendor/unrar/rdwrfn.cpp +++ b/unrar_sys/vendor/unrar/rdwrfn.cpp @@ -259,15 +259,6 @@ void ComprDataIO::SetEncryption(bool Encrypt,CRYPT_METHOD Method, } -#if !defined(SFX_MODULE) && !defined(RAR_NOCRYPT) -void ComprDataIO::SetAV15Encryption() -{ - Decryption=true; - Decrypt->SetAV15Encryption(); -} -#endif - - #if !defined(SFX_MODULE) && !defined(RAR_NOCRYPT) void ComprDataIO::SetCmt13Encryption() { @@ -299,11 +290,15 @@ void ComprDataIO::AdjustTotalArcSize(Archive *Arc) // packed size to beginning of these blocks. Earlier we already calculated // the total size based on entire archive sizes. We also set LastArcSize // to start of first trailing block, to add it later to ProcessedArcSize. - int64 ArcLength=Arc->IsSeekable() ? Arc->FileLength() : 0; - if (Arc->MainHead.QOpenOffset!=0) // QO is always preceding RR record. + uint64 ArcLength=Arc->IsSeekable() ? Arc->FileLength() : 0; + // QO is always preceding RR record. + // Also we check QO and RR to be less than archive length to prevent + // negative "ArcLength-LastArcSize" and possible signed integer overflow + // when calculating TotalArcSize. + if (Arc->MainHead.QOpenOffset>0 && Arc->MainHead.QOpenOffsetMainHead.QOpenOffset; else - if (Arc->MainHead.RROffset!=0) + if (Arc->MainHead.RROffset>0 && Arc->MainHead.RROffsetMainHead.RROffset; else { diff --git a/unrar_sys/vendor/unrar/rdwrfn.hpp b/unrar_sys/vendor/unrar/rdwrfn.hpp index 3060a0f..7a13d07 100644 --- a/unrar_sys/vendor/unrar/rdwrfn.hpp +++ b/unrar_sys/vendor/unrar/rdwrfn.hpp @@ -6,10 +6,6 @@ class CmdAdd; class Unpack; class ArcFileSearch; -#if 0 -// We use external i/o calls for Benchmark command. -#define COMPRDATAIO_EXTIO -#endif class ComprDataIO { @@ -72,7 +68,6 @@ class ComprDataIO void SetSubHeader(FileHeader *hd,int64 *Pos) {SubHead=hd;SubHeadPos=Pos;} void SetEncryption(bool Encrypt,CRYPT_METHOD Method,SecPassword *Password, const byte *Salt,const byte *InitV,uint Lg2Cnt,byte *HashKey,byte *PswCheck); - void SetAV15Encryption(); void SetCmt13Encryption(); void SetUnpackToMemory(byte *Addr,uint Size); void SetCurrentCommand(wchar Cmd) {CurrentCommand=Cmd;} diff --git a/unrar_sys/vendor/unrar/recvol.cpp b/unrar_sys/vendor/unrar/recvol.cpp index b178207..426d9ce 100644 --- a/unrar_sys/vendor/unrar/recvol.cpp +++ b/unrar_sys/vendor/unrar/recvol.cpp @@ -1,11 +1,12 @@ #include "rar.hpp" + #include "recvol3.cpp" #include "recvol5.cpp" -bool RecVolumesRestore(CommandData *Cmd,const wchar *Name,bool Silent) +bool RecVolumesRestore(CommandData *Cmd,const std::wstring &Name,bool Silent) { Archive Arc(Cmd); if (!Arc.Open(Name)) @@ -42,24 +43,20 @@ bool RecVolumesRestore(CommandData *Cmd,const wchar *Name,bool Silent) } -void RecVolumesTest(CommandData *Cmd,Archive *Arc,const wchar *Name) +void RecVolumesTest(CommandData *Cmd,Archive *Arc,const std::wstring &Name) { - wchar RevName[NM]; - *RevName=0; - if (Arc!=NULL) + std::wstring RevName; + if (Arc==NULL) + RevName=Name; + else { // We received .rar or .exe volume as a parameter, trying to find // the matching .rev file number 1. bool NewNumbering=Arc->NewNumbering; - wchar ArcName[NM]; - wcsncpyz(ArcName,Name,ASIZE(ArcName)); - - wchar *VolNumStart=VolNameToFirstName(ArcName,ArcName,ASIZE(ArcName),NewNumbering); - wchar RecVolMask[NM]; - wcsncpyz(RecVolMask,ArcName,ASIZE(RecVolMask)); - size_t BaseNamePartLength=VolNumStart-ArcName; - wcsncpyz(RecVolMask+BaseNamePartLength,L"*.rev",ASIZE(RecVolMask)-BaseNamePartLength); + std::wstring RecVolMask; + size_t VolNumStart=VolNameToFirstName(Name,RecVolMask,NewNumbering); + RecVolMask.replace(VolNumStart, std::wstring::npos, L"*.rev"); FindFile Find; Find.SetMask(RecVolMask); @@ -67,31 +64,30 @@ void RecVolumesTest(CommandData *Cmd,Archive *Arc,const wchar *Name) while (Find.Next(&RecData)) { - wchar *Num=GetVolNumPart(RecData.Name); - if (*Num!='1') // Name must have "0...01" numeric part. + size_t NumPos=GetVolNumPos(RecData.Name); + if (RecData.Name[NumPos]!='1') // Name must have "0...01" numeric part. continue; bool FirstVol=true; - while (--Num>=RecData.Name && IsDigit(*Num)) - if (*Num!='0') + while (NumPos>0 && IsDigit(RecData.Name[--NumPos])) + if (RecData.Name[NumPos]!='0') { FirstVol=false; break; } if (FirstVol) { - wcsncpyz(RevName,RecData.Name,ASIZE(RevName)); - Name=RevName; + RevName=RecData.Name; break; } } - if (*RevName==0) // First .rev file not found. + if (RevName.empty()) // First .rev file not found. return; } File RevFile; - if (!RevFile.Open(Name)) + if (!RevFile.Open(RevName)) { - ErrHandler.OpenErrorMsg(Name); // It also sets RARX_OPEN. + ErrHandler.OpenErrorMsg(RevName); // It also sets RARX_OPEN. return; } mprintf(L"\n"); @@ -101,11 +97,11 @@ void RecVolumesTest(CommandData *Cmd,Archive *Arc,const wchar *Name) if (Rev5) { RecVolumes5 RecVol(Cmd,true); - RecVol.Test(Cmd,Name); + RecVol.Test(Cmd,RevName); } else { RecVolumes3 RecVol(Cmd,true); - RecVol.Test(Cmd,Name); + RecVol.Test(Cmd,RevName); } } diff --git a/unrar_sys/vendor/unrar/recvol.hpp b/unrar_sys/vendor/unrar/recvol.hpp index 4a6d663..159bd1a 100644 --- a/unrar_sys/vendor/unrar/recvol.hpp +++ b/unrar_sys/vendor/unrar/recvol.hpp @@ -8,7 +8,7 @@ class RecVolumes3 { private: File *SrcFile[256]; - Array Buf; + std::vector Buf; #ifdef RAR_SMP ThreadPool *RSThreadPool; @@ -16,16 +16,16 @@ class RecVolumes3 public: RecVolumes3(CommandData *Cmd,bool TestOnly); ~RecVolumes3(); - void Make(CommandData *Cmd,wchar *ArcName); - bool Restore(CommandData *Cmd,const wchar *Name,bool Silent); - void Test(CommandData *Cmd,const wchar *Name); + void Make(CommandData *Cmd,std::wstring ArcName); + bool Restore(CommandData *Cmd,const std::wstring &Name,bool Silent); + void Test(CommandData *Cmd,const std::wstring &Name); }; struct RecVolItem { File *f; - wchar Name[NM]; + std::wstring Name; uint CRC; uint64 FileSize; bool New; // Newly created RAR volume. @@ -52,7 +52,7 @@ class RecVolumes5 void ProcessRS(CommandData *Cmd,uint MaxRead,bool Encode); uint ReadHeader(File *RecFile,bool FirstRev); - Array RecItems; + std::vector RecItems; byte *RealReadBuffer; // Real pointer returned by 'new'. byte *ReadBuffer; // Pointer aligned for SSE instructions. @@ -78,11 +78,11 @@ class RecVolumes5 public: RecVolumes5(CommandData *Cmd,bool TestOnly); ~RecVolumes5(); - bool Restore(CommandData *Cmd,const wchar *Name,bool Silent); - void Test(CommandData *Cmd,const wchar *Name); + bool Restore(CommandData *Cmd,const std::wstring &Name,bool Silent); + void Test(CommandData *Cmd,const std::wstring &Name); }; -bool RecVolumesRestore(CommandData *Cmd,const wchar *Name,bool Silent); -void RecVolumesTest(CommandData *Cmd,Archive *Arc,const wchar *Name); +bool RecVolumesRestore(CommandData *Cmd,const std::wstring &Name,bool Silent); +void RecVolumesTest(CommandData *Cmd,Archive *Arc,const std::wstring &Name); #endif diff --git a/unrar_sys/vendor/unrar/recvol3.cpp b/unrar_sys/vendor/unrar/recvol3.cpp index 0138d0f..4199dee 100644 --- a/unrar_sys/vendor/unrar/recvol3.cpp +++ b/unrar_sys/vendor/unrar/recvol3.cpp @@ -23,12 +23,6 @@ class RSEncode // Encode or decode data area, one object per one thread. #ifdef RAR_SMP -THREAD_PROC(RSEncodeThread) -{ - RSEncode *rs=(RSEncode *)Data; - rs->EncodeBuf(); -} - THREAD_PROC(RSDecodeThread) { RSEncode *rs=(RSEncode *)Data; @@ -47,8 +41,7 @@ RecVolumes3::RecVolumes3(CommandData *Cmd,bool TestOnly) } else { - Buf.Alloc(TotalBufferSize); - memset(SrcFile,0,sizeof(SrcFile)); + Buf.resize(TotalBufferSize); #ifdef RAR_SMP RSThreadPool=new ThreadPool(Cmd->Threads); #endif @@ -68,30 +61,16 @@ RecVolumes3::~RecVolumes3() -void RSEncode::EncodeBuf() -{ - for (int BufPos=BufStart;BufPosName;Ext--) - if (!IsDigit(*Ext)) - if (*Ext=='_' && IsDigit(*(Ext-1))) + for (ExtPos--;ExtPos>0;ExtPos--) + if (!IsDigit(Name[ExtPos])) + if (Name[ExtPos]=='_' && IsDigit(Name[ExtPos-1])) DigitGroup++; else break; @@ -99,19 +78,19 @@ static bool IsNewStyleRev(const wchar *Name) } -bool RecVolumes3::Restore(CommandData *Cmd,const wchar *Name,bool Silent) +bool RecVolumes3::Restore(CommandData *Cmd,const std::wstring &Name,bool Silent) { - wchar ArcName[NM]; - wcsncpyz(ArcName,Name,ASIZE(ArcName)); - wchar *Ext=GetExt(ArcName); + std::wstring ArcName=Name; bool NewStyle=false; // New style .rev volumes are supported since RAR 3.10. - bool RevName=Ext!=NULL && wcsicomp(Ext,L".rev")==0; + bool RevName=CmpExt(ArcName,L"rev"); if (RevName) { NewStyle=IsNewStyleRev(ArcName); - while (Ext>ArcName+1 && (IsDigit(*(Ext-1)) || *(Ext-1)=='_')) - Ext--; - wcsncpyz(Ext,L"*.*",ASIZE(ArcName)-(Ext-ArcName)); + + size_t ExtPos=GetExtPos(ArcName); + while (ExtPos>1 && (IsDigit(ArcName[ExtPos-1]) || ArcName[ExtPos-1]=='_')) + ExtPos--; + ArcName.replace(ExtPos,std::wstring::npos,L"*.*"); FindFile Find; Find.SetMask(ArcName); @@ -121,7 +100,7 @@ bool RecVolumes3::Restore(CommandData *Cmd,const wchar *Name,bool Silent) Archive Arc(Cmd); if (Arc.WOpen(fd.Name) && Arc.IsArchive(true)) { - wcsncpyz(ArcName,fd.Name,ASIZE(ArcName)); + ArcName=fd.Name; break; } } @@ -138,11 +117,10 @@ bool RecVolumes3::Restore(CommandData *Cmd,const wchar *Name,bool Silent) bool NewNumbering=Arc.NewNumbering; Arc.Close(); - wchar *VolNumStart=VolNameToFirstName(ArcName,ArcName,ASIZE(ArcName),NewNumbering); - wchar RecVolMask[NM]; - wcsncpyz(RecVolMask,ArcName,ASIZE(RecVolMask)); - size_t BaseNamePartLength=VolNumStart-ArcName; - wcsncpyz(RecVolMask+BaseNamePartLength,L"*.rev",ASIZE(RecVolMask)-BaseNamePartLength); + size_t VolNumStart=VolNameToFirstName(ArcName,ArcName,NewNumbering); + std::wstring RecVolMask=ArcName; + RecVolMask.replace(VolNumStart,std::wstring::npos,L"*.rev"); + size_t BaseNamePartLength=VolNumStart; int64 RecFileSize=0; @@ -155,25 +133,25 @@ bool RecVolumes3::Restore(CommandData *Cmd,const wchar *Name,bool Silent) Find.SetMask(RecVolMask); FindData RecData; int FileNumber=0,RecVolNumber=0,FoundRecVolumes=0,MissingVolumes=0; - wchar PrevName[NM]; + std::wstring PrevName; while (Find.Next(&RecData)) { - wchar *CurName=RecData.Name; + std::wstring CurName=RecData.Name; int P[3]; if (!RevName && !NewStyle) { NewStyle=true; - wchar *Dot=GetExt(CurName); - if (Dot!=NULL) + size_t DotPos=GetExtPos(CurName); + if (DotPos!=std::wstring::npos) { - int LineCount=0; - Dot--; - while (Dot>CurName && *Dot!='.') + uint LineCount=0; + DotPos--; + while (DotPos>0 && CurName[DotPos]!='.') { - if (*Dot=='_') + if (CurName[DotPos]=='_') LineCount++; - Dot--; + DotPos--; } if (LineCount==2) NewStyle=false; @@ -209,17 +187,17 @@ bool RecVolumes3::Restore(CommandData *Cmd,const wchar *Name,bool Silent) } else { - wchar *Dot=GetExt(CurName); - if (Dot==NULL) + size_t DotPos=GetExtPos(CurName); + if (DotPos==std::wstring::npos) continue; bool WrongParam=false; for (size_t I=0;I=CurName+BaseNamePartLength); - P[I]=atoiw(Dot+1); + DotPos--; + } while (IsDigit(CurName[DotPos]) && DotPos>=BaseNamePartLength); + P[I]=atoiw(&CurName[DotPos+1]); if (P[I]==0 || P[I]>255) WrongParam=true; } @@ -235,7 +213,7 @@ bool RecVolumes3::Restore(CommandData *Cmd,const wchar *Name,bool Silent) } RecVolNumber=P[1]; FileNumber=P[2]; - wcsncpyz(PrevName,CurName,ASIZE(PrevName)); + PrevName=CurName; File *NewFile=new File; NewFile->TOpen(CurName); @@ -256,11 +234,9 @@ bool RecVolumes3::Restore(CommandData *Cmd,const wchar *Name,bool Silent) if (FoundRecVolumes==0) return false; - bool WriteFlags[256]; - memset(WriteFlags,0,sizeof(WriteFlags)); + bool WriteFlags[256]{}; - wchar LastVolName[NM]; - *LastVolName=0; + std::wstring LastVolName; for (int CurArcNum=0;CurArcNumClose(); - wchar NewName[NM]; - wcsncpyz(NewName,ArcName,ASIZE(NewName)); - wcsncatz(NewName,L".bad",ASIZE(NewName)); + std::wstring NewName=ArcName+L".bad"; uiMsg(UIMSG_BADARCHIVE,ArcName); uiMsg(UIMSG_RENAMING,ArcName,NewName); @@ -329,13 +303,13 @@ bool RecVolumes3::Restore(CommandData *Cmd,const wchar *Name,bool Silent) MissingVolumes++; if (CurArcNum==FileNumber-1) - wcsncpyz(LastVolName,ArcName,ASIZE(LastVolName)); + LastVolName=ArcName; uiMsg(UIMSG_MISSINGVOL,ArcName); uiMsg(UIEVENT_NEWARCHIVE,ArcName); } SrcFile[CurArcNum]=(File*)NewFile; - NextVolumeName(ArcName,ASIZE(ArcName),!NewNumbering); + NextVolumeName(ArcName,!NewNumbering); } uiMsg(UIMSG_RECVOLMISSING,MissingVolumes); @@ -460,7 +434,7 @@ bool RecVolumes3::Restore(CommandData *Cmd,const wchar *Name,bool Silent) CurFile->Close(); SrcFile[I]=NULL; } - if (*LastVolName!=0) + if (!LastVolName.empty()) { // Truncate the last volume to its real size. Archive Arc(Cmd); @@ -504,7 +478,7 @@ void RSEncode::DecodeBuf() } -void RecVolumes3::Test(CommandData *Cmd,const wchar *Name) +void RecVolumes3::Test(CommandData *Cmd,const std::wstring &Name) { if (!IsNewStyleRev(Name)) // RAR 3.0 name#_#_#.rev do not include CRC32. { @@ -512,8 +486,7 @@ void RecVolumes3::Test(CommandData *Cmd,const wchar *Name) return; } - wchar VolName[NM]; - wcsncpyz(VolName,Name,ASIZE(VolName)); + std::wstring VolName=Name; while (FileExist(VolName)) { @@ -525,7 +498,7 @@ void RecVolumes3::Test(CommandData *Cmd,const wchar *Name) } if (!uiStartFileExtract(VolName,false,true,false)) return; - mprintf(St(MExtrTestFile),VolName); + mprintf(St(MExtrTestFile),VolName.c_str()); mprintf(L" "); CurFile.Seek(0,SEEK_END); int64 Length=CurFile.Tell(); @@ -546,6 +519,6 @@ void RecVolumes3::Test(CommandData *Cmd,const wchar *Name) ErrHandler.SetErrorCode(RARX_CRC); } - NextVolumeName(VolName,ASIZE(VolName),false); + NextVolumeName(VolName,false); } } diff --git a/unrar_sys/vendor/unrar/recvol5.cpp b/unrar_sys/vendor/unrar/recvol5.cpp index 2d9c947..e094b17 100644 --- a/unrar_sys/vendor/unrar/recvol5.cpp +++ b/unrar_sys/vendor/unrar/recvol5.cpp @@ -48,8 +48,8 @@ RecVolumes5::~RecVolumes5() { delete[] RealBuf; delete[] RealReadBuffer; - for (uint I=0;IArcName && IsDigit(*(Num-1))) - Num--; - if (Num<=PointToName(ArcName)) + size_t NumPos=GetVolNumPos(ArcName); + while (NumPos>0 && IsDigit(ArcName[NumPos-1])) + NumPos--; + if (NumPos<=GetNamePos(ArcName)) return false; // Numeric part is missing or entire volume name is numeric, not possible for RAR or REV volume. - wcsncpyz(Num,L"*.*",ASIZE(ArcName)-(Num-ArcName)); + ArcName.replace(NumPos,std::wstring::npos,L"*.*"); - wchar FirstVolName[NM]; - *FirstVolName=0; - - wchar LongestRevName[NM]; - *LongestRevName=0; + std::wstring FirstVolName; + std::wstring LongestRevName; int64 RecFileSize=0; @@ -184,8 +180,8 @@ bool RecVolumes5::Restore(CommandData *Cmd,const wchar *Name,bool Silent) ItemPos=RecNum; FoundRecVolumes++; - if (wcslen(fd.Name)>wcslen(LongestRevName)) - wcsncpyz(LongestRevName,fd.Name,ASIZE(LongestRevName)); + if (fd.Name.size()>LongestRevName.size()) + LongestRevName=fd.Name; } } else @@ -204,35 +200,35 @@ bool RecVolumes5::Restore(CommandData *Cmd,const wchar *Name,bool Silent) // RAR volume found. Get its number, store the handle in appropriate // array slot, clean slots in between if we had to grow the array. - wchar *Num=GetVolNumPart(fd.Name); + size_t NumPos=GetVolNumPos(fd.Name); uint VolNum=0; - for (uint K=1;Num>=fd.Name && IsDigit(*Num);K*=10,Num--) - VolNum+=(*Num-'0')*K; + for (uint K=1;(int)NumPos>=0 && IsDigit(fd.Name[NumPos]);K*=10,NumPos--) + VolNum+=(fd.Name[NumPos]-'0')*K; if (VolNum==0 || VolNum>MaxVolumes) continue; - size_t CurSize=RecItems.Size(); + size_t CurSize=RecItems.size(); if (VolNum>CurSize) { - RecItems.Alloc(VolNum); - for (size_t I=CurSize;If=Vol; Item->New=false; - wcsncpyz(Item->Name,fd.Name,ASIZE(Item->Name)); + Item->Name=fd.Name; } } @@ -244,10 +240,10 @@ bool RecVolumes5::Restore(CommandData *Cmd,const wchar *Name,bool Silent) // If we did not find even a single .rar volume, create .rar volume name // based on the longest .rev file name. Use longest .rev, so we have // enough space for volume number. - if (*FirstVolName==0) + if (FirstVolName.empty()) { - SetExt(LongestRevName,L"rar",ASIZE(LongestRevName)); - VolNameToFirstName(LongestRevName,FirstVolName,ASIZE(FirstVolName),true); + SetExt(LongestRevName,L"rar"); + VolNameToFirstName(LongestRevName,FirstVolName,true); } uiMsg(UIMSG_RECVOLCALCCHECKSUM); @@ -309,9 +305,8 @@ bool RecVolumes5::Restore(CommandData *Cmd,const wchar *Name,bool Silent) { Item->f->Close(); - wchar NewName[NM]; - wcsncpyz(NewName,Item->Name,ASIZE(NewName)); - wcsncatz(NewName,L".bad",ASIZE(NewName)); + std::wstring NewName; + NewName=Item->Name+L".bad"; uiMsg(UIMSG_BADARCHIVE,Item->Name); uiMsg(UIMSG_RENAMING,Item->Name,NewName); @@ -322,12 +317,12 @@ bool RecVolumes5::Restore(CommandData *Cmd,const wchar *Name,bool Silent) if ((Item->New=(Item->f==NULL))==true) { - wcsncpyz(Item->Name,FirstVolName,ASIZE(Item->Name)); + Item->Name=FirstVolName; uiMsg(UIMSG_CREATING,Item->Name); uiMsg(UIEVENT_NEWARCHIVE,Item->Name); File *NewVol=new File; bool UserReject; - if (!FileCreate(Cmd,NewVol,Item->Name,ASIZE(Item->Name),&UserReject)) + if (!FileCreate(Cmd,NewVol,Item->Name,&UserReject)) { if (!UserReject) ErrHandler.CreateErrorMsg(Item->Name); @@ -336,7 +331,7 @@ bool RecVolumes5::Restore(CommandData *Cmd,const wchar *Name,bool Silent) NewVol->Prealloc(Item->FileSize); Item->f=NewVol; } - NextVolumeName(FirstVolName,ASIZE(FirstVolName),false); + NextVolumeName(FirstVolName,false); } @@ -389,7 +384,7 @@ bool RecVolumes5::Restore(CommandData *Cmd,const wchar *Name,bool Silent) J++; VolNum=J++; // Use next valid REV volume data instead of RAR. } - RecVolItem *Item=RecItems+VolNum; + RecVolItem *Item=&RecItems[VolNum]; byte *B=&ReadBuf[0]; int ReadSize=0; @@ -411,7 +406,7 @@ bool RecVolumes5::Restore(CommandData *Cmd,const wchar *Name,bool Silent) for (uint I=0,J=0;IFileSize); Item->f->Write(Buf+(J++)*RecBufferSize,WriteSize); Item->FileSize-=WriteSize; @@ -477,10 +472,10 @@ uint RecVolumes5::ReadHeader(File *RecFile,bool FirstRev) { // If we have read the first valid REV file, init data structures // using information from REV header. - size_t CurSize=RecItems.Size(); - RecItems.Alloc(TotalCount); - for (size_t I=CurSize;I=1) // Check the maximum supported cpuid function. @@ -102,8 +110,24 @@ void Rijndael::Init(bool Encrypt,const byte *key,uint keyLen,const byte * initVe } else AES_NI=false; -#elif defined(USE_NEON) - AES_Neon=(getauxval(AT_HWCAP) & HWCAP_AES)!=0; +#elif defined(__GNUC__) + AES_NI=__builtin_cpu_supports("aes"); +#endif + +#elif defined(USE_NEON_AES) + #ifdef _APPLE + // getauxval isn't available in OS X + uint Value=0; + size_t Size=sizeof(Value); + int RetCode=sysctlbyname("hw.optional.arm.FEAT_AES",&Value,&Size,NULL,0); + + // We treat sysctlbyname failure with -1 return code as AES presence, + // because "hw.optional.arm.FEAT_AES" was missing in OS X 11, but AES + // still was supported by Neon. + AES_Neon=RetCode!=0 || Value!=0; + #else + AES_Neon=(getauxval(AT_HWCAP) & HWCAP_AES)!=0; + #endif #endif // Other developers asked us to initialize it to suppress "may be used @@ -156,7 +180,7 @@ void Rijndael::blockEncrypt(const byte *input,size_t inputLen,byte *outBuffer) blockEncryptSSE(input,numBlocks,outBuffer); return; } -#elif defined(USE_NEON) +#elif defined(USE_NEON_AES) if (AES_Neon) { blockEncryptNeon(input,numBlocks,outBuffer); @@ -249,7 +273,7 @@ void Rijndael::blockEncryptSSE(const byte *input,size_t numBlocks,byte *outBuffe #endif -#ifdef USE_NEON +#ifdef USE_NEON_AES void Rijndael::blockEncryptNeon(const byte *input,size_t numBlocks,byte *outBuffer) { byte *prevBlock = m_initVector; @@ -295,7 +319,7 @@ void Rijndael::blockDecrypt(const byte *input, size_t inputLen, byte *outBuffer) blockDecryptSSE(input,numBlocks,outBuffer); return; } -#elif defined(USE_NEON) +#elif defined(USE_NEON_AES) if (AES_Neon) { blockDecryptNeon(input,numBlocks,outBuffer); @@ -392,7 +416,7 @@ void Rijndael::blockDecryptSSE(const byte *input, size_t numBlocks, byte *outBuf #endif -#ifdef USE_NEON +#ifdef USE_NEON_AES void Rijndael::blockDecryptNeon(const byte *input, size_t numBlocks, byte *outBuffer) { byte iv[16]; @@ -585,16 +609,16 @@ void TestRijndael() for (uint L=0;L<3;L++) { byte Out[16]; - wchar Str[sizeof(Out)*2+1]; + std::wstring Str; uint KeyLength=128+L*64; rij.Init(true,Key[L],KeyLength,IV); for (uint I=0;I>8; - ((byte *)&T1L)[I]=gfMul(I<<4,M); - ((byte *)&T1H)[I]=gfMul(I<<4,M)>>8; - ((byte *)&T2L)[I]=gfMul(I<<8,M); - ((byte *)&T2H)[I]=gfMul(I<<8,M)>>8; - ((byte *)&T3L)[I]=gfMul(I<<12,M); - ((byte *)&T3H)[I]=gfMul(I<<12,M)>>8; + ((byte *)&T0L)[I]=byte(gfMul(I,M)); + ((byte *)&T0H)[I]=byte(gfMul(I,M)>>8); + ((byte *)&T1L)[I]=byte(gfMul(I<<4,M)); + ((byte *)&T1H)[I]=byte(gfMul(I<<4,M)>>8); + ((byte *)&T2L)[I]=byte(gfMul(I<<8,M)); + ((byte *)&T2H)[I]=byte(gfMul(I<<8,M)>>8); + ((byte *)&T3L)[I]=byte(gfMul(I<<12,M)); + ((byte *)&T3H)[I]=byte(gfMul(I<<12,M)>>8); } size_t Pos=0; diff --git a/unrar_sys/vendor/unrar/rs16.hpp b/unrar_sys/vendor/unrar/rs16.hpp index b67a7ca..3833313 100644 --- a/unrar_sys/vendor/unrar/rs16.hpp +++ b/unrar_sys/vendor/unrar/rs16.hpp @@ -17,6 +17,9 @@ class RSCoder16 void InvertDecoderMatrix(); #ifdef USE_SSE +#if defined(USE_SSE) && defined(__GNUC__) + __attribute__((target("ssse3"))) +#endif bool SSE_UpdateECC(uint DataNum, uint ECCNum, const byte *Data, byte *ECC, size_t BlockSize); #endif diff --git a/unrar_sys/vendor/unrar/scantree.cpp b/unrar_sys/vendor/unrar/scantree.cpp index dbaf1e4..4313177 100644 --- a/unrar_sys/vendor/unrar/scantree.cpp +++ b/unrar_sys/vendor/unrar/scantree.cpp @@ -10,12 +10,11 @@ ScanTree::ScanTree(StringList *FileMasks,RECURSE_MODE Recurse,bool GetLinks,SCAN ScanEntireDisk=false; FolderWildcards=false; + FindStack.push_back(NULL); // We need a single NULL pointer for initial Depth==0. + SetAllMaskDepth=0; - *CurMask=0; - memset(FindStack,0,sizeof(FindStack)); Depth=0; Errors=0; - *ErrArcName=0; Cmd=NULL; ErrDirList=NULL; ErrDirSpecPathLength=NULL; @@ -42,7 +41,7 @@ SCAN_CODE ScanTree::GetNext(FindData *FD) SCAN_CODE FindCode; while (1) { - if (*CurMask==0 && !GetNextMask()) + if (CurMask.empty() && !GetNextMask()) return SCAN_DONE; #ifndef SILENT @@ -79,7 +78,7 @@ bool ScanTree::ExpandFolderMask() { bool WildcardFound=false; uint SlashPos=0; - for (int I=0;CurMask[I]!=0;I++) + for (uint I=0;I0 && ExpandedFolderList.GetString(CurMask,ASIZE(CurMask))) + if (ExpandedFolderList.ItemsCount()>0 && ExpandedFolderList.GetString(CurMask)) return true; FolderWildcards=false; FilterList.Reset(); - if (!FileMasks->GetString(CurMask,ASIZE(CurMask))) + if (!FileMasks->GetString(CurMask)) return false; // Check if folder wildcards present. @@ -144,10 +141,10 @@ bool ScanTree::GetFilteredMask() uint SlashPos=0; uint StartPos=0; #ifdef _WIN_ALL // Not treat the special NTFS \\?\d: path prefix as a wildcard. - if (CurMask[0]=='\\' && CurMask[1]=='\\' && CurMask[2]=='?' && CurMask[3]=='\\') + if (CurMask.rfind(L"\\\\?\\",0)==0) StartPos=4; #endif - for (uint I=StartPos;CurMask[I]!=0;I++) + for (uint I=StartPos;I2 && CurMask[0]=='\\' && CurMask[1]=='\\') { - const wchar *Slash=wcschr(CurMask+2,'\\'); - if (Slash!=NULL) + auto Slash=CurMask.find('\\',2); + if (Slash!=std::wstring::npos) { - Slash=wcschr(Slash+1,'\\'); - ScanEntireDisk=Slash!=NULL && *(Slash+1)==0; + Slash=CurMask.find('\\',Slash+1); + // If backslash is found and it is the last string character. + ScanEntireDisk=Slash!=std::wstring::npos && Slash+1==CurMask.size(); } } else ScanEntireDisk=IsDriveLetter(CurMask) && IsPathDiv(CurMask[2]) && CurMask[3]==0; - wchar *Name=PointToName(CurMask); - if (*Name==0) - wcsncatz(CurMask,MASKALL,ASIZE(CurMask)); - if (Name[0]=='.' && (Name[1]==0 || Name[1]=='.' && Name[2]==0)) + auto NamePos=GetNamePos(CurMask); + std::wstring Name=CurMask.substr(NamePos); + if (Name.empty()) + CurMask+=MASKALL; + if (Name==L"." || Name==L"..") { - AddEndSlash(CurMask,ASIZE(CurMask)); - wcsncatz(CurMask,MASKALL,ASIZE(CurMask)); + AddEndSlash(CurMask); + CurMask+=MASKALL; } - SpecPathLength=Name-CurMask; + SpecPathLength=NamePos; Depth=0; - wcsncpyz(OrigCurMask,CurMask,ASIZE(OrigCurMask)); + OrigCurMask=CurMask; return true; } @@ -250,7 +249,7 @@ bool ScanTree::GetNextMask() SCAN_CODE ScanTree::FindProc(FindData *FD) { - if (*CurMask==0) + if (CurMask.empty()) return SCAN_NEXT; bool FastFindFile=false; @@ -283,10 +282,9 @@ SCAN_CODE ScanTree::FindProc(FindData *FD) // Create the new FindFile object for wildcard based search. FindStack[Depth]=new FindFile; - wchar SearchMask[NM]; - wcsncpyz(SearchMask,CurMask,ASIZE(SearchMask)); + std::wstring SearchMask=CurMask; if (SearchAll) - SetName(SearchMask,MASKALL,ASIZE(SearchMask)); + SetName(SearchMask,MASKALL); FindStack[Depth]->SetMask(SearchMask); } else @@ -325,7 +323,7 @@ SCAN_CODE ScanTree::FindProc(FindData *FD) // It is not necessary for directories, because even in "fast find" // mode, directory recursing will quit by (Depth < 0) condition, // which returns SCAN_DONE to calling function. - *CurMask=0; + CurMask.clear(); return RetCode; } @@ -344,9 +342,6 @@ SCAN_CODE ScanTree::FindProc(FindData *FD) if (Error) ScanError(Error); - wchar DirName[NM]; - *DirName=0; - // Going to at least one directory level higher. delete FindStack[Depth]; FindStack[Depth--]=NULL; @@ -362,29 +357,32 @@ SCAN_CODE ScanTree::FindProc(FindData *FD) return SCAN_DONE; } - wchar *Slash=wcsrchr(CurMask,CPATHDIVIDER); - if (Slash!=NULL) + auto Slash=CurMask.rfind(CPATHDIVIDER); + if (Slash!=std::wstring::npos) { - wchar Mask[NM]; - wcsncpyz(Mask,Slash,ASIZE(Mask)); + std::wstring Mask; + Mask=CurMask.substr(Slash); // Name mask with leading slash like \*.* if (DepthIsDir) + { + FD->Flags|=FDDF_SECONDDIR; + return Error ? SCAN_ERROR:SCAN_SUCCESS; } - } - if (GetDirs==SCAN_GETDIRSTWICE && - FindFile::FastFind(DirName,FD,GetLinks) && FD->IsDir) - { - FD->Flags|=FDDF_SECONDDIR; - return Error ? SCAN_ERROR:SCAN_SUCCESS; } return Error ? SCAN_ERROR:SCAN_NEXT; } @@ -414,22 +412,22 @@ SCAN_CODE ScanTree::FindProc(FindData *FD) return FastFindFile ? SCAN_DONE:SCAN_NEXT; } - wchar Mask[NM]; - - wcsncpyz(Mask,FastFindFile ? MASKALL:PointToName(CurMask),ASIZE(Mask)); - wcsncpyz(CurMask,FD->Name,ASIZE(CurMask)); + std::wstring Mask=FastFindFile ? MASKALL:PointToName(CurMask); + CurMask=FD->Name; - if (wcslen(CurMask)+wcslen(Mask)+1>=NM || Depth>=MAXSCANDEPTH-1) + if (CurMask.size()+Mask.size()+1>=MAXPATHSIZE || Depth>=MAXSCANDEPTH-1) { uiMsg(UIERROR_PATHTOOLONG,CurMask,SPATHDIVIDER,Mask); return SCAN_ERROR; } - AddEndSlash(CurMask,ASIZE(CurMask)); - wcsncatz(CurMask,Mask,ASIZE(CurMask)); + AddEndSlash(CurMask); + CurMask+=Mask; Depth++; + FindStack.resize(Depth+1); + // We need to use OrigCurMask for depths less than SetAllMaskDepth // and "*" for depths equal or larger than SetAllMaskDepth. // It is important when "fast finding" directories at Depth > 0. @@ -470,19 +468,18 @@ void ScanTree::ScanError(bool &Error) // We cannot just check FD->FileAttr here, it can be undefined // if we process "folder\*" mask or if we process "folder" mask, // but "folder" is inaccessible. - wchar *Slash=PointToName(CurMask); - if (Slash>CurMask) + auto Slash=GetNamePos(CurMask); + if (Slash>1) { - *(Slash-1)=0; - DWORD Attr=GetFileAttributes(CurMask); - *(Slash-1)=CPATHDIVIDER; + std::wstring Parent=CurMask.substr(0,Slash-1); + DWORD Attr=GetFileAttr(Parent); if (Attr!=0xffffffff && (Attr & FILE_ATTRIBUTE_REPARSE_POINT)!=0) Error=false; } // Do not display an error if we cannot scan contents of // "System Volume Information" folder. Normally it is not accessible. - if (wcsstr(CurMask,L"System Volume Information\\")!=NULL) + if (CurMask.find(L"System Volume Information\\")!=std::wstring::npos) Error=false; } #endif @@ -495,10 +492,10 @@ void ScanTree::ScanError(bool &Error) if (ErrDirList!=NULL) ErrDirList->AddString(CurMask); if (ErrDirSpecPathLength!=NULL) - ErrDirSpecPathLength->Push((uint)SpecPathLength); - wchar FullName[NM]; + ErrDirSpecPathLength->push_back((uint)SpecPathLength); + std::wstring FullName; // This conversion works for wildcard masks too. - ConvertNameToFull(CurMask,FullName,ASIZE(FullName)); + ConvertNameToFull(CurMask,FullName); uiMsg(UIERROR_DIRSCAN,FullName); ErrHandler.SysErrMsg(); } diff --git a/unrar_sys/vendor/unrar/scantree.hpp b/unrar_sys/vendor/unrar/scantree.hpp index 7ebe69a..06bf545 100644 --- a/unrar_sys/vendor/unrar/scantree.hpp +++ b/unrar_sys/vendor/unrar/scantree.hpp @@ -11,20 +11,21 @@ enum SCAN_DIRS enum SCAN_CODE { SCAN_SUCCESS,SCAN_DONE,SCAN_ERROR,SCAN_NEXT }; -#define MAXSCANDEPTH (NM/2) - class CommandData; class ScanTree { private: + static constexpr size_t MAXSCANDEPTH = MAXPATHSIZE/2; + bool ExpandFolderMask(); bool GetFilteredMask(); bool GetNextMask(); SCAN_CODE FindProc(FindData *FD); void ScanError(bool &Error); - FindFile *FindStack[MAXSCANDEPTH]; +// FindFile *FindStack[MAXSCANDEPTH]; + std::vector FindStack; int Depth; int SetAllMaskDepth; @@ -33,13 +34,13 @@ class ScanTree RECURSE_MODE Recurse; bool GetLinks; SCAN_DIRS GetDirs; - int Errors; + uint Errors; // Set when processing paths like c:\ (root directory without wildcards). bool ScanEntireDisk; - wchar CurMask[NM]; - wchar OrigCurMask[NM]; + std::wstring CurMask; + std::wstring OrigCurMask; // Store all folder masks generated from folder wildcard mask in non-recursive mode. StringList ExpandedFolderList; @@ -49,7 +50,7 @@ class ScanTree // Save the list of unreadable dirs here. StringList *ErrDirList; - Array *ErrDirSpecPathLength; + std::vector *ErrDirSpecPathLength; // Set if processing a folder wildcard mask. bool FolderWildcards; @@ -57,7 +58,7 @@ class ScanTree bool SearchAllInRoot; size_t SpecPathLength; - wchar ErrArcName[NM]; + std::wstring ErrArcName; CommandData *Cmd; public: @@ -65,10 +66,10 @@ class ScanTree ~ScanTree(); SCAN_CODE GetNext(FindData *FindData); size_t GetSpecPathLength() {return SpecPathLength;} - int GetErrors() {return Errors;}; - void SetErrArcName(const wchar *Name) {wcsncpyz(ErrArcName,Name,ASIZE(ErrArcName));} + uint GetErrors() {return Errors;}; + void SetErrArcName(const std::wstring &Name) {ErrArcName=Name;} void SetCommandData(CommandData *Cmd) {ScanTree::Cmd=Cmd;} - void SetErrDirList(StringList *List,Array *Lengths) + void SetErrDirList(StringList *List,std::vector *Lengths) { ErrDirList=List; ErrDirSpecPathLength=Lengths; diff --git a/unrar_sys/vendor/unrar/secpassword.cpp b/unrar_sys/vendor/unrar/secpassword.cpp index 08da549..8d8f298 100644 --- a/unrar_sys/vendor/unrar/secpassword.cpp +++ b/unrar_sys/vendor/unrar/secpassword.cpp @@ -69,8 +69,8 @@ SecPassword::~SecPassword() void SecPassword::Clean() { PasswordSet=false; - if (Password.size()>0) - cleandata(&Password[0],Password.size()*sizeof(Password[0])); + if (!Password.empty()) + cleandata(Password.data(),Password.size()*sizeof(Password[0])); } @@ -79,7 +79,7 @@ void SecPassword::Clean() // So we use our own function for this purpose. void cleandata(void *data,size_t size) { - if (data==NULL || size==0) + if (data==nullptr || size==0) return; #if defined(_WIN_ALL) && defined(_MSC_VER) SecureZeroMemory(data,size); @@ -120,6 +120,14 @@ void SecPassword::Get(wchar *Psw,size_t MaxSize) } +void SecPassword::Get(std::wstring &Psw) +{ + wchar PswBuf[MAXPASSWORD]; + Get(PswBuf,ASIZE(PswBuf)); + Psw=PswBuf; +} + + void SecPassword::Set(const wchar *Psw) diff --git a/unrar_sys/vendor/unrar/secpassword.hpp b/unrar_sys/vendor/unrar/secpassword.hpp index 5284bce..44e073c 100644 --- a/unrar_sys/vendor/unrar/secpassword.hpp +++ b/unrar_sys/vendor/unrar/secpassword.hpp @@ -15,6 +15,7 @@ class SecPassword ~SecPassword(); void Clean(); void Get(wchar *Psw,size_t MaxSize); + void Get(std::wstring &Psw); void Set(const wchar *Psw); bool IsSet() {return PasswordSet;} size_t Length(); @@ -23,6 +24,7 @@ class SecPassword void cleandata(void *data,size_t size); +inline void cleandata(std::wstring &s) {cleandata(&s[0],s.size()*sizeof(s[0]));} void SecHideData(void *Data,size_t DataSize,bool Encode,bool CrossProcess); #endif diff --git a/unrar_sys/vendor/unrar/sha256.cpp b/unrar_sys/vendor/unrar/sha256.cpp index f90d2c0..4c93aba 100644 --- a/unrar_sys/vendor/unrar/sha256.cpp +++ b/unrar_sys/vendor/unrar/sha256.cpp @@ -63,7 +63,7 @@ static void sha256_transform(sha256_context *ctx) for (uint I = 0; I < 64; I++) { - uint T1 = v[7] + Sg1(v[4]) + Ch(v[4], v[5], v[6]) + K[I] + W[I]; + uint32 T1 = v[7] + Sg1(v[4]) + Ch(v[4], v[5], v[6]) + K[I] + W[I]; // It is possible to eliminate variable copying if we unroll loop // and rename variables every time. But my test did not show any speed @@ -74,7 +74,7 @@ static void sha256_transform(sha256_context *ctx) v[4] = v[3] + T1; // It works a little faster when moved here from beginning of loop. - uint T2 = Sg0(v[0]) + Maj(v[0], v[1], v[2]); + uint32 T2 = Sg0(v[0]) + Maj(v[0], v[1], v[2]); v[3] = v[2]; v[2] = v[1]; @@ -146,3 +146,13 @@ void sha256_done(sha256_context *ctx, byte *Digest) sha256_init(ctx); } + + +void sha256_get(const void *Data, size_t Size, byte *Digest) +{ + sha256_context ctx; + sha256_init(&ctx); + sha256_process(&ctx, Data, Size); + sha256_done(&ctx, Digest); +} + diff --git a/unrar_sys/vendor/unrar/sha256.hpp b/unrar_sys/vendor/unrar/sha256.hpp index b6837e7..186297c 100644 --- a/unrar_sys/vendor/unrar/sha256.hpp +++ b/unrar_sys/vendor/unrar/sha256.hpp @@ -13,5 +13,6 @@ typedef struct void sha256_init(sha256_context *ctx); void sha256_process(sha256_context *ctx, const void *Data, size_t Size); void sha256_done(sha256_context *ctx, byte *Digest); +void sha256_get(const void *Data, size_t Size, byte *Digest); #endif diff --git a/unrar_sys/vendor/unrar/strfn.cpp b/unrar_sys/vendor/unrar/strfn.cpp index 7617f7a..3fdaca5 100644 --- a/unrar_sys/vendor/unrar/strfn.cpp +++ b/unrar_sys/vendor/unrar/strfn.cpp @@ -2,66 +2,67 @@ const char *NullToEmpty(const char *Str) { - return Str==NULL ? "":Str; + return Str==nullptr ? "":Str; } const wchar *NullToEmpty(const wchar *Str) { - return Str==NULL ? L"":Str; + return Str==nullptr ? L"":Str; } -void IntToExt(const char *Src,char *Dest,size_t DestSize) +void IntToExt(const std::string &Src,std::string &Dest) { #ifdef _WIN_ALL - // OemToCharBuff does not stop at 0, so let's check source length. - size_t SrcLength=strlen(Src)+1; - if (DestSize>SrcLength) - DestSize=SrcLength; - OemToCharBuffA(Src,Dest,(DWORD)DestSize); - Dest[DestSize-1]=0; + if (std::addressof(Src)!=std::addressof(Dest)) + Dest=Src; + // OemToCharA use seems to be discouraged. So we use OemToCharBuffA, + // which doesn't stop at 0 and converts the entire passed length. + OemToCharBuffA(&Dest[0],&Dest[0],(DWORD)Dest.size()); + + std::string::size_type Pos=Dest.find('\0'); // Avoid zeroes inside of Dest. + if (Pos!=std::string::npos) + Dest.erase(Pos); + #else - if (Dest!=Src) - strncpyz(Dest,Src,DestSize); + if (std::addressof(Src)!=std::addressof(Dest)) + Dest=Src; #endif } // Convert archived names and comments to Unicode. // Allows user to select a code page in GUI. -void ArcCharToWide(const char *Src,wchar *Dest,size_t DestSize,ACTW_ENCODING Encoding) +void ArcCharToWide(const char *Src,std::wstring &Dest,ACTW_ENCODING Encoding) { #if defined(_WIN_ALL) // Console Windows RAR. if (Encoding==ACTW_UTF8) - UtfToWide(Src,Dest,DestSize); + UtfToWide(Src,Dest); else { - Array NameA; + std::string NameA; if (Encoding==ACTW_OEM) { - NameA.Alloc(DestSize+1); - IntToExt(Src,&NameA[0],NameA.Size()); - Src=&NameA[0]; + IntToExt(Src,NameA); + Src=NameA.data(); } - CharToWide(Src,Dest,DestSize); + CharToWide(Src,Dest); } #else // RAR for Unix. if (Encoding==ACTW_UTF8) - UtfToWide(Src,Dest,DestSize); + UtfToWide(Src,Dest); else - CharToWide(Src,Dest,DestSize); + CharToWide(Src,Dest); #endif - // Ensure that we return a zero terminate string for security reason. - // While [Jni]CharToWide might already do it, be protected in case of future - // changes in these functions. - if (DestSize>0) - Dest[DestSize-1]=0; + TruncateAtZero(Dest); // Ensure there are no zeroes inside of string. } + + int stricomp(const char *s1,const char *s2) { #ifdef _WIN_ALL @@ -113,6 +114,19 @@ wchar* RemoveEOL(wchar *Str) } +void RemoveEOL(std::wstring &Str) +{ + while (!Str.empty()) + { + wchar c=Str.back(); + if (c=='\r' || c=='\n' || c==' ' || c=='\t') + Str.pop_back(); + else + break; + } +} + + wchar* RemoveLF(wchar *Str) { for (int I=(int)wcslen(Str)-1;I>=0 && (Str[I]=='\r' || Str[I]=='\n');I--) @@ -121,6 +135,13 @@ wchar* RemoveLF(wchar *Str) } +void RemoveLF(std::wstring &Str) +{ + for (int I=(int)Str.size()-1;I>=0 && (Str[I]=='\r' || Str[I]=='\n');I--) + Str.erase(I); +} + + #if defined(SFX_MODULE) // char version of etoupperw. Used in console SFX module only. // Fast toupper for English only input and output. Additionally to speed, @@ -176,30 +197,18 @@ bool IsAlpha(int ch) -void BinToHex(const byte *Bin,size_t BinSize,char *HexA,wchar *HexW,size_t HexSize) +void BinToHex(const byte *Bin,size_t BinSize,std::wstring &Hex) { - uint A=0,W=0; // ASCII and Unicode hex output positions. + Hex.clear(); for (uint I=0;I> 4; uint Low=Bin[I] & 0xf; - uint HighHex=High>9 ? 'a'+High-10:'0'+High; - uint LowHex=Low>9 ? 'a'+Low-10:'0'+Low; - if (HexA!=NULL && A9 ? 'a'+High-10 : '0'+High; + uint LowHex=Low>9 ? 'a'+Low-10 : '0'+Low; + Hex+=HighHex; + Hex+=LowHex; } - if (HexA!=NULL && HexSize>0) - HexA[A]=0; - if (HexW!=NULL && HexSize>0) - HexW[W]=0; } @@ -217,22 +226,25 @@ uint GetDigits(uint Number) #endif -bool LowAscii(const char *Str) +bool LowAscii(const std::string &Str) { - for (size_t I=0;Str[I]!=0;I++) - if (/*(byte)Str[I]<32 || */(byte)Str[I]>127) + for (char Ch : Str) + { + // We convert char to byte in case char is signed. + if (/*(uint)Ch<32 || */(byte)Ch>127) return false; + } return true; } -bool LowAscii(const wchar *Str) +bool LowAscii(const std::wstring &Str) { - for (size_t I=0;Str[I]!=0;I++) + for (wchar Ch : Str) { // We convert wchar_t to uint just in case if some compiler // uses signed wchar_t. - if (/*(uint)Str[I]<32 || */(uint)Str[I]>127) + if (/*(uint)Ch<32 || */(uint)Ch>127) return false; } return true; @@ -249,6 +261,12 @@ int wcsicompc(const wchar *s1,const wchar *s2) // For path comparison. } +int wcsicompc(const std::wstring &s1,const std::wstring &s2) +{ + return wcsicompc(s1.c_str(),s2.c_str()); +} + + int wcsnicompc(const wchar *s1,const wchar *s2,size_t n) { #if defined(_UNIX) @@ -259,6 +277,12 @@ int wcsnicompc(const wchar *s1,const wchar *s2,size_t n) } +int wcsnicompc(const std::wstring &s1,const std::wstring &s2,size_t n) +{ + return wcsnicompc(s1.c_str(),s2.c_str(),n); +} + + // Safe copy: copies maxlen-1 max and for maxlen>0 returns zero terminated dest. void strncpyz(char *dest, const char *src, size_t maxlen) { @@ -383,88 +407,164 @@ void fmtitoa(int64 n,wchar *Str,size_t MaxSize) } -const wchar* GetWide(const char *Src) +std::wstring GetWide(const char *Src) { - const size_t MaxLength=NM; - static wchar StrTable[4][MaxLength]; - static uint StrNum=0; - if (++StrNum >= ASIZE(StrTable)) - StrNum=0; - wchar *Str=StrTable[StrNum]; - CharToWide(Src,Str,MaxLength); - Str[MaxLength-1]=0; + std::wstring Str; + CharToWide(Src,Str); return Str; } // Parse string containing parameters separated with spaces. -// Support quote marks. Param can be NULL to return the pointer to next -// parameter, which can be used to estimate the buffer size for Param. -const wchar* GetCmdParam(const wchar *CmdLine,wchar *Param,size_t MaxSize) +// Support quote marks. Accepts and updates the current position in the string. +// Returns false if there is nothing to parse. +bool GetCmdParam(const std::wstring &CmdLine,std::wstring::size_type &Pos,std::wstring &Param) { - while (IsSpace(*CmdLine)) - CmdLine++; - if (*CmdLine==0) - return NULL; + Param.clear(); + + while (IsSpace(CmdLine[Pos])) + Pos++; + if (Pos==CmdLine.size()) + return false; - size_t ParamSize=0; bool Quote=false; - while (*CmdLine!=0 && (Quote || !IsSpace(*CmdLine))) + while (Pos=0 || Msg.size()>MaxAllocSize) + break; + Msg.resize(Msg.size()*4); + } + std::wstring::size_type ZeroPos=Msg.find(L'\0'); + if (ZeroPos!=std::wstring::npos) + Msg.resize(ZeroPos); // Remove excessive zeroes at the end. + + return Msg; +} +#endif + + +#ifdef _WIN_ALL +bool ExpandEnvironmentStr(std::wstring &Str) +{ + DWORD ExpCode=ExpandEnvironmentStrings(Str.c_str(),nullptr,0); + if (ExpCode==0) + return false; + std::vector Buf(ExpCode); + ExpCode=ExpandEnvironmentStrings(Str.c_str(),Buf.data(),(DWORD)Buf.size()); + if (ExpCode==0 || ExpCode>Buf.size()) + return false; + Str=Buf.data(); + return true; } #endif + + +void TruncateAtZero(std::wstring &Str) +{ + std::wstring::size_type Pos=Str.find(L'\0'); + if (Pos!=std::wstring::npos) + Str.erase(Pos); +} + + +void ReplaceEsc(std::wstring &Str) +{ + std::wstring::size_type Pos=0; + while (true) + { + Pos=Str.find(L'\033',Pos); + if (Pos==std::wstring::npos) + break; + Str[Pos]=L'\''; + Str.insert(Pos+1,L"\\033'"); + Pos+=6; + } +} diff --git a/unrar_sys/vendor/unrar/strfn.hpp b/unrar_sys/vendor/unrar/strfn.hpp index 2a21fea..fd4793c 100644 --- a/unrar_sys/vendor/unrar/strfn.hpp +++ b/unrar_sys/vendor/unrar/strfn.hpp @@ -3,16 +3,18 @@ const char* NullToEmpty(const char *Str); const wchar* NullToEmpty(const wchar *Str); -void IntToExt(const char *Src,char *Dest,size_t DestSize); +void IntToExt(const std::string &Src,std::string &Dest); enum ACTW_ENCODING { ACTW_DEFAULT, ACTW_OEM, ACTW_UTF8}; -void ArcCharToWide(const char *Src,wchar *Dest,size_t DestSize,ACTW_ENCODING Encoding); +void ArcCharToWide(const char *Src,std::wstring &Dest,ACTW_ENCODING Encoding); int stricomp(const char *s1,const char *s2); int strnicomp(const char *s1,const char *s2,size_t n); wchar* RemoveEOL(wchar *Str); +void RemoveEOL(std::wstring &Str); wchar* RemoveLF(wchar *Str); +void RemoveLF(std::wstring &Str); void strncpyz(char *dest, const char *src, size_t maxlen); void wcsncpyz(wchar *dest, const wchar *src, size_t maxlen); @@ -28,25 +30,36 @@ bool IsDigit(int ch); bool IsSpace(int ch); bool IsAlpha(int ch); -void BinToHex(const byte *Bin,size_t BinSize,char *Hex,wchar *HexW,size_t HexSize); +void BinToHex(const byte *Bin,size_t BinSize,std::wstring &Hex); #ifndef SFX_MODULE uint GetDigits(uint Number); #endif -bool LowAscii(const char *Str); -bool LowAscii(const wchar *Str); +bool LowAscii(const std::string &Str); +bool LowAscii(const std::wstring &Str); int wcsicompc(const wchar *s1,const wchar *s2); +int wcsicompc(const std::wstring &s1,const std::wstring &s2); int wcsnicompc(const wchar *s1,const wchar *s2,size_t n); +int wcsnicompc(const std::wstring &s1,const std::wstring &s2,size_t n); void itoa(int64 n,char *Str,size_t MaxSize); void itoa(int64 n,wchar *Str,size_t MaxSize); void fmtitoa(int64 n,wchar *Str,size_t MaxSize); -const wchar* GetWide(const char *Src); -const wchar* GetCmdParam(const wchar *CmdLine,wchar *Param,size_t MaxSize); +std::wstring GetWide(const char *Src); +bool GetCmdParam(const std::wstring &CmdLine,std::wstring::size_type &Pos,std::wstring &Param); #ifndef RARDLL -void PrintfPrepareFmt(const wchar *Org,wchar *Cvt,size_t MaxSize); +void PrintfPrepareFmt(const wchar *Org,std::wstring &Cvt); +std::wstring wstrprintf(const wchar *fmt,...); +std::wstring vwstrprintf(const wchar *fmt,va_list arglist); #endif +#ifdef _WIN_ALL +bool ExpandEnvironmentStr(std::wstring &Str); +#endif + +void TruncateAtZero(std::wstring &Str); +void ReplaceEsc(std::wstring &Str); + #endif diff --git a/unrar_sys/vendor/unrar/strlist.cpp b/unrar_sys/vendor/unrar/strlist.cpp index 50d69c7..e76654f 100644 --- a/unrar_sys/vendor/unrar/strlist.cpp +++ b/unrar_sys/vendor/unrar/strlist.cpp @@ -9,18 +9,20 @@ StringList::StringList() void StringList::Reset() { Rewind(); - StringData.Reset(); + StringData.clear(); StringsCount=0; SavePosNumber=0; } +/* void StringList::AddStringA(const char *Str) { - Array StrW(strlen(Str)); - CharToWide(Str,&StrW[0],StrW.Size()); - AddString(&StrW[0]); + std::wstring StrW; + CharToWide(Str,StrW); + AddString(StrW); } +*/ void StringList::AddString(const wchar *Str) @@ -28,22 +30,30 @@ void StringList::AddString(const wchar *Str) if (Str==NULL) Str=L""; - size_t PrevSize=StringData.Size(); - StringData.Add(wcslen(Str)+1); + size_t PrevSize=StringData.size(); + StringData.resize(PrevSize+wcslen(Str)+1); wcscpy(&StringData[PrevSize],Str); StringsCount++; } +void StringList::AddString(const std::wstring &Str) +{ + AddString(Str.c_str()); +} + + +/* bool StringList::GetStringA(char *Str,size_t MaxLength) { - Array StrW(MaxLength); - if (!GetString(&StrW[0],StrW.Size())) + std::wstring StrW; + if (!GetString(StrW)) return false; - WideToChar(&StrW[0],Str,MaxLength); + WideToChar(StrW.c_str(),Str,MaxLength); return true; } +*/ bool StringList::GetString(wchar *Str,size_t MaxLength) @@ -56,6 +66,16 @@ bool StringList::GetString(wchar *Str,size_t MaxLength) } +bool StringList::GetString(std::wstring &Str) +{ + wchar *StrPtr; + if (!GetString(&StrPtr)) + return false; + Str=StrPtr; + return true; +} + + #ifndef SFX_MODULE bool StringList::GetString(wchar *Str,size_t MaxLength,int StringNum) { @@ -71,6 +91,22 @@ bool StringList::GetString(wchar *Str,size_t MaxLength,int StringNum) RestorePosition(); return RetCode; } + + +bool StringList::GetString(std::wstring &Str,int StringNum) +{ + SavePosition(); + Rewind(); + bool RetCode=true; + while (StringNum-- >=0) + if (!GetString(Str)) + { + RetCode=false; + break; + } + RestorePosition(); + return RetCode; +} #endif @@ -84,7 +120,7 @@ wchar* StringList::GetString() bool StringList::GetString(wchar **Str) { - if (CurPos>=StringData.Size()) // No more strings left unprocessed. + if (CurPos>=StringData.size()) // No more strings left unprocessed. { if (Str!=NULL) *Str=NULL; @@ -107,7 +143,7 @@ void StringList::Rewind() #ifndef SFX_MODULE -bool StringList::Search(const wchar *Str,bool CaseSensitive) +bool StringList::Search(const std::wstring &Str,bool CaseSensitive) { SavePosition(); Rewind(); @@ -115,8 +151,8 @@ bool StringList::Search(const wchar *Str,bool CaseSensitive) wchar *CurStr; while (GetString(&CurStr)) { - if (Str!=NULL && CurStr!=NULL) - if ((CaseSensitive ? wcscmp(Str,CurStr):wcsicomp(Str,CurStr))!=0) + if (CurStr!=NULL) + if (CaseSensitive && Str!=CurStr || !CaseSensitive && wcsicomp(Str,CurStr)!=0) continue; Found=true; break; diff --git a/unrar_sys/vendor/unrar/strlist.hpp b/unrar_sys/vendor/unrar/strlist.hpp index 16a2cbb..d89ba5c 100644 --- a/unrar_sys/vendor/unrar/strlist.hpp +++ b/unrar_sys/vendor/unrar/strlist.hpp @@ -4,7 +4,7 @@ class StringList { private: - Array StringData; + std::vector StringData; size_t CurPos; size_t StringsCount; @@ -13,17 +13,20 @@ class StringList public: StringList(); void Reset(); - void AddStringA(const char *Str); +// void AddStringA(const char *Str); void AddString(const wchar *Str); - bool GetStringA(char *Str,size_t MaxLength); + void AddString(const std::wstring &Str); +// bool GetStringA(char *Str,size_t MaxLength); bool GetString(wchar *Str,size_t MaxLength); + bool GetString(std::wstring &Str); bool GetString(wchar *Str,size_t MaxLength,int StringNum); + bool GetString(std::wstring &Str,int StringNum); wchar* GetString(); bool GetString(wchar **Str); void Rewind(); size_t ItemsCount() {return StringsCount;}; - size_t GetCharCount() {return StringData.Size();} - bool Search(const wchar *Str,bool CaseSensitive); + size_t GetCharCount() {return StringData.size();} + bool Search(const std::wstring &Str,bool CaseSensitive); void SavePosition(); void RestorePosition(); }; diff --git a/unrar_sys/vendor/unrar/system.cpp b/unrar_sys/vendor/unrar/system.cpp index 9e53622..cf9d183 100644 --- a/unrar_sys/vendor/unrar/system.cpp +++ b/unrar_sys/vendor/unrar/system.cpp @@ -150,16 +150,18 @@ bool ShutdownCheckAnother(bool Open) + #if defined(_WIN_ALL) // Load library from Windows System32 folder. Use this function to prevent // loading a malicious code from current folder or same folder as exe. HMODULE WINAPI LoadSysLibrary(const wchar *Name) { - wchar SysDir[NM]; - if (GetSystemDirectory(SysDir,ASIZE(SysDir))==0) - return NULL; - MakeName(SysDir,Name,SysDir,ASIZE(SysDir)); - return LoadLibrary(SysDir); + std::vector SysDir(MAX_PATH); + if (GetSystemDirectory(SysDir.data(),(UINT)SysDir.size())==0) + return nullptr; + std::wstring FullName; + MakeName(SysDir.data(),Name,FullName); + return LoadLibrary(FullName.c_str()); } @@ -186,6 +188,7 @@ SSE_VERSION _SSE_Version=GetSSEVersion(); SSE_VERSION GetSSEVersion() { +#ifdef _MSC_VER int CPUInfo[4]; __cpuid(CPUInfo, 0); @@ -210,6 +213,18 @@ SSE_VERSION GetSSEVersion() if ((CPUInfo[3] & 0x2000000)!=0) return SSE_SSE; } +#elif defined(__GNUC__) + if (__builtin_cpu_supports("avx2")) + return SSE_AVX2; + if (__builtin_cpu_supports("sse4.1")) + return SSE_SSE41; + if (__builtin_cpu_supports("ssse3")) + return SSE_SSSE3; + if (__builtin_cpu_supports("sse2")) + return SSE_SSE2; + if (__builtin_cpu_supports("sse")) + return SSE_SSE; +#endif return SSE_NONE; } #endif diff --git a/unrar_sys/vendor/unrar/system.hpp b/unrar_sys/vendor/unrar/system.hpp index a56d6b7..de4c14a 100644 --- a/unrar_sys/vendor/unrar/system.hpp +++ b/unrar_sys/vendor/unrar/system.hpp @@ -21,7 +21,7 @@ void InitSystemOptions(int SleepTime); void SetPriority(int Priority); clock_t MonoClock(); void Wait(); -bool EmailFile(const wchar *FileName,const wchar *MailToW); +bool EmailFile(const std::wstring &FileName,std::wstring MailToW); void Shutdown(POWER_MODE Mode); bool ShutdownCheckAnother(bool Open); @@ -30,7 +30,6 @@ HMODULE WINAPI LoadSysLibrary(const wchar *Name); bool IsUserAdmin(); #endif - #ifdef USE_SSE enum SSE_VERSION {SSE_NONE,SSE_SSE,SSE_SSE2,SSE_SSSE3,SSE_SSE41,SSE_AVX2}; SSE_VERSION GetSSEVersion(); diff --git a/unrar_sys/vendor/unrar/timefn.cpp b/unrar_sys/vendor/unrar/timefn.cpp index 0abf49d..aa90715 100644 --- a/unrar_sys/vendor/unrar/timefn.cpp +++ b/unrar_sys/vendor/unrar/timefn.cpp @@ -71,12 +71,12 @@ void RarTime::SetLocal(RarLocalTime *lt) { #ifdef _WIN_ALL SYSTEMTIME st; - st.wYear=lt->Year; - st.wMonth=lt->Month; - st.wDay=lt->Day; - st.wHour=lt->Hour; - st.wMinute=lt->Minute; - st.wSecond=lt->Second; + st.wYear=(WORD)lt->Year; + st.wMonth=(WORD)lt->Month; + st.wDay=(WORD)lt->Day; + st.wHour=(WORD)lt->Hour; + st.wMinute=(WORD)lt->Minute; + st.wSecond=(WORD)lt->Second; st.wMilliseconds=0; st.wDayOfWeek=0; FILETIME lft; @@ -183,8 +183,7 @@ void RarTime::SetUnix(time_t ut) // Get the high precision Unix time in nanoseconds since 01-01-1970. uint64 RarTime::GetUnixNS() { - // 11644473600000000000 - number of ns between 01-01-1601 and 01-01-1970. - uint64 ushift=INT32TO64(0xA1997B0B,0x4C6A0000); + const uint64 ushift=11644473600000000000ULL; // ns between 01-01-1601 and 01-01-1970. return itime*(1000000000/TICKS_PER_SECOND)-ushift; } @@ -192,8 +191,7 @@ uint64 RarTime::GetUnixNS() // Set the high precision Unix time in nanoseconds since 01-01-1970. void RarTime::SetUnixNS(uint64 ns) { - // 11644473600000000000 - number of ns between 01-01-1601 and 01-01-1970. - uint64 ushift=INT32TO64(0xA1997B0B,0x4C6A0000); + const uint64 ushift=11644473600000000000ULL; // ns between 01-01-1601 and 01-01-1970. itime=(ns+ushift)/(1000000000/TICKS_PER_SECOND); } @@ -327,14 +325,14 @@ void RarTime::Adjust(int64 ns) #ifndef SFX_MODULE -const wchar *GetMonthName(int Month) +const wchar *GetMonthName(uint Month) { return uiGetMonthName(Month); } #endif -bool IsLeapYear(int Year) +bool IsLeapYear(uint Year) { return (Year&3)==0 && (Year%100!=0 || Year%400==0); } diff --git a/unrar_sys/vendor/unrar/timefn.hpp b/unrar_sys/vendor/unrar/timefn.hpp index 49b61e8..6c265ec 100644 --- a/unrar_sys/vendor/unrar/timefn.hpp +++ b/unrar_sys/vendor/unrar/timefn.hpp @@ -70,7 +70,7 @@ class RarTime void Adjust(int64 ns); }; -const wchar *GetMonthName(int Month); -bool IsLeapYear(int Year); +const wchar *GetMonthName(uint Month); +bool IsLeapYear(uint Year); #endif diff --git a/unrar_sys/vendor/unrar/ui.hpp b/unrar_sys/vendor/unrar/ui.hpp index 5def26d..f37001b 100644 --- a/unrar_sys/vendor/unrar/ui.hpp +++ b/unrar_sys/vendor/unrar/ui.hpp @@ -19,8 +19,8 @@ enum UIMESSAGE_CODE { UIERROR_SUBHEADERBROKEN, UIERROR_SUBHEADERUNKNOWN, UIERROR_SUBHEADERDATABROKEN, UIERROR_RRDAMAGED, UIERROR_UNKNOWNMETHOD, UIERROR_UNKNOWNENCMETHOD, UIERROR_RENAMING, UIERROR_NEWERRAR, - UIERROR_NOTSFX, UIERROR_OLDTOSFX, - UIERROR_WRONGSFXVER, UIERROR_HEADENCMISMATCH, UIERROR_DICTOUTMEM, + UIERROR_NOTSFX, UIERROR_OLDTOSFX,UIERROR_WRONGSFXVER, + UIERROR_HEADENCMISMATCH, UIERROR_DICTOUTMEM,UIERROR_EXTRDICTOUTMEM, UIERROR_USESMALLERDICT, UIERROR_MODIFYUNKNOWN, UIERROR_MODIFYOLD, UIERROR_MODIFYLOCKED, UIERROR_MODIFYVOLUME, UIERROR_NOTVOLUME, UIERROR_NOTFIRSTVOLUME, UIERROR_RECVOLLIMIT, UIERROR_RECVOLDIFFSETS, @@ -32,14 +32,15 @@ enum UIMESSAGE_CODE { UIERROR_NOFILESTOADD, UIERROR_NOFILESTODELETE, UIERROR_NOFILESTOEXTRACT, UIERROR_MISSINGVOL, UIERROR_NEEDPREVVOL, UIERROR_UNKNOWNEXTRA, UIERROR_CORRUPTEXTRA, UIERROR_NTFSREQUIRED, UIERROR_ZIPVOLSFX, - UIERROR_FILERO, UIERROR_TOOLARGESFX, UIERROR_NOZIPSFX, UIERROR_EMAIL, - UIERROR_ACLGET, UIERROR_ACLBROKEN, UIERROR_ACLUNKNOWN, UIERROR_ACLSET, - UIERROR_STREAMBROKEN, UIERROR_STREAMUNKNOWN, UIERROR_INCOMPATSWITCH, - UIERROR_PATHTOOLONG, UIERROR_DIRSCAN, UIERROR_UOWNERGET, - UIERROR_UOWNERBROKEN, UIERROR_UOWNERGETOWNERID, UIERROR_UOWNERGETGROUPID, - UIERROR_UOWNERSET, UIERROR_ULINKREAD, UIERROR_ULINKEXIST, - UIERROR_OPENPRESERVEATIME, UIERROR_READERRTRUNCATED, UIERROR_READERRCOUNT, - UIERROR_DIRNAMEEXISTS,UIERROR_TRUNCPSW,UIERROR_ADJUSTVALUE, + UIERROR_FILERO, UIERROR_TOOLARGESFX, UIERROR_NOZIPSFX, UIERROR_NEEEDSFX64, + UIERROR_EMAIL, UIERROR_ACLGET, UIERROR_ACLBROKEN, UIERROR_ACLUNKNOWN, + UIERROR_ACLSET, UIERROR_STREAMBROKEN, UIERROR_STREAMUNKNOWN, + UIERROR_INCOMPATSWITCH, UIERROR_PATHTOOLONG, UIERROR_DIRSCAN, + UIERROR_UOWNERGET, UIERROR_UOWNERBROKEN, UIERROR_UOWNERGETOWNERID, + UIERROR_UOWNERGETGROUPID, UIERROR_UOWNERSET, UIERROR_ULINKREAD, + UIERROR_ULINKEXIST, UIERROR_OPENPRESERVEATIME, UIERROR_READERRTRUNCATED, + UIERROR_READERRCOUNT, UIERROR_DIRNAMEEXISTS,UIERROR_TRUNCPSW, + UIERROR_ADJUSTVALUE, UIERROR_SKIPUNSAFELINK, UIMSG_FIRST, UIMSG_STRING, UIMSG_BUILD, UIMSG_RRSEARCH, UIMSG_ANALYZEFILEDATA, @@ -76,19 +77,25 @@ enum UIASKREP_RESULT { UIASKREP_R_RENAME,UIASKREP_R_RENAMEAUTO,UIASKREP_R_CANCEL,UIASKREP_R_UNUSED }; -UIASKREP_RESULT uiAskReplace(wchar *Name,size_t MaxNameSize,int64 FileSize,RarTime *FileTime,uint Flags); -UIASKREP_RESULT uiAskReplaceEx(CommandData *Cmd,wchar *Name,size_t MaxNameSize,int64 FileSize,RarTime *FileTime,uint Flags); +UIASKREP_RESULT uiAskReplace(std::wstring &Name,int64 FileSize,RarTime *FileTime,uint Flags); +UIASKREP_RESULT uiAskReplaceEx(CommandData *Cmd,std::wstring &Name,int64 FileSize,RarTime *FileTime,uint Flags); void uiInit(SOUND_NOTIFY_MODE Sound); -void uiStartArchiveExtract(bool Extract,const wchar *ArcName); -bool uiStartFileExtract(const wchar *FileName,bool Extract,bool Test,bool Skip); +void uiStartArchiveExtract(bool Extract,const std::wstring &ArcName); +bool uiStartFileExtract(const std::wstring &FileName,bool Extract,bool Test,bool Skip); void uiExtractProgress(int64 CurFileSize,int64 TotalFileSize,int64 CurSize,int64 TotalSize); void uiProcessProgress(const char *Command,int64 CurSize,int64 TotalSize); -enum UIPASSWORD_TYPE {UIPASSWORD_GLOBAL,UIPASSWORD_FILE,UIPASSWORD_ARCHIVE}; -bool uiGetPassword(UIPASSWORD_TYPE Type,const wchar *FileName,SecPassword *Password,CheckPassword *CheckPwd); +enum UIPASSWORD_TYPE +{ + UIPASSWORD_GLOBAL, // For -p, -hp without parameter or Ctrl+P in WinRAR. + UIPASSWORD_FILE, // Extracting an encrypted file. + UIPASSWORD_ARCHIVE, // Extracting or opening an encrypted header archive. +}; + +bool uiGetPassword(UIPASSWORD_TYPE Type,const std::wstring &FileName,SecPassword *Password,CheckPassword *CheckPwd); bool uiIsGlobalPasswordSet(); enum UIALARM_TYPE {UIALARM_ERROR, UIALARM_INFO, UIALARM_QUESTION}; @@ -96,14 +103,16 @@ void uiAlarm(UIALARM_TYPE Type); void uiEolAfterMsg(); -bool uiAskNextVolume(wchar *VolName,size_t MaxSize); +bool uiAskNextVolume(std::wstring &VolName); #if !defined(SILENT) && !defined(SFX_MODULE) -void uiAskRepeatRead(const wchar *FileName,bool &Ignore,bool &All,bool &Retry,bool &Quit); +void uiAskRepeatRead(const std::wstring &FileName,bool &Ignore,bool &All,bool &Retry,bool &Quit); #endif -bool uiAskRepeatWrite(const wchar *FileName,bool DiskFull); +bool uiAskRepeatWrite(const std::wstring &FileName,bool DiskFull); + +bool uiDictLimit(CommandData *Cmd,const std::wstring &FileName,uint64 DictSize,uint64 MaxDictSize); #ifndef SFX_MODULE -const wchar *uiGetMonthName(int Month); +const wchar *uiGetMonthName(uint Month); #endif class uiMsgStore @@ -131,6 +140,12 @@ class uiMsgStore Str[StrSize++]=s; return *this; } + uiMsgStore& operator << (const std::wstring &s) + { + if (StrSizeOverwrite==OVERWRITE_NONE) return UIASKREP_R_SKIP; #if !defined(SFX_MODULE) && !defined(SILENT) // Must be before Cmd->AllYes check or -y switch would override -or. - if (Cmd->Overwrite==OVERWRITE_AUTORENAME && GetAutoRenamedName(Name,MaxNameSize)) + if (Cmd->Overwrite==OVERWRITE_AUTORENAME && GetAutoRenamedName(Name)) return UIASKREP_R_REPLACE; #endif - // This check must be after OVERWRITE_AUTORENAME processing or -y switch - // would override -or. - if (Cmd->AllYes || Cmd->Overwrite==OVERWRITE_ALL) - { - PrepareToDelete(Name); - return UIASKREP_R_REPLACE; - } - - wchar NewName[NM]; - wcsncpyz(NewName,Name,ASIZE(NewName)); - UIASKREP_RESULT Choice=uiAskReplace(NewName,ASIZE(NewName),FileSize,FileTime,Flags); + std::wstring NewName=Name; + UIASKREP_RESULT Choice=Cmd->AllYes || Cmd->Overwrite==OVERWRITE_ALL ? + UIASKREP_R_REPLACE : uiAskReplace(NewName,FileSize,FileTime,Flags); if (Choice==UIASKREP_R_REPLACE || Choice==UIASKREP_R_REPLACEALL) + { PrepareToDelete(Name); + // Overwrite the link itself instead of its target. + // For normal files we prefer to inherit file attributes, permissions + // and hard links. + FindData FD; + if (FindFile::FastFind(Name,&FD,true) && FD.IsLink) + DelFile(Name); + } + if (Choice==UIASKREP_R_REPLACEALL) { Cmd->Overwrite=OVERWRITE_ALL; @@ -46,16 +48,16 @@ UIASKREP_RESULT uiAskReplaceEx(CommandData *Cmd,wchar *Name,size_t MaxNameSize,i } if (Choice==UIASKREP_R_RENAME) { - if (PointToName(NewName)==NewName) - SetName(Name,NewName,MaxNameSize); + if (GetNamePos(NewName)==0) + SetName(Name,NewName); else - wcsncpyz(Name,NewName,MaxNameSize); + Name=NewName; if (FileExist(Name)) - return uiAskReplaceEx(Cmd,Name,MaxNameSize,FileSize,FileTime,Flags); + return uiAskReplaceEx(Cmd,Name,FileSize,FileTime,Flags); return UIASKREP_R_REPLACE; } #if !defined(SFX_MODULE) && !defined(SILENT) - if (Choice==UIASKREP_R_RENAMEAUTO && GetAutoRenamedName(Name,MaxNameSize)) + if (Choice==UIASKREP_R_RENAMEAUTO && GetAutoRenamedName(Name)) { Cmd->Overwrite=OVERWRITE_AUTORENAME; return UIASKREP_R_REPLACE; @@ -63,3 +65,23 @@ UIASKREP_RESULT uiAskReplaceEx(CommandData *Cmd,wchar *Name,size_t MaxNameSize,i #endif return Choice; } + + +bool GetAutoRenamedName(std::wstring &Name) +{ + std::wstring Ext=GetExt(Name); + for (uint FileVer=1;FileVer<1000000;FileVer++) + { + std::wstring NewName=Name; + RemoveExt(NewName); + wchar Ver[10]; + itoa(FileVer,Ver,ASIZE(Ver)); + NewName = NewName + L"(" + Ver + L")" + Ext; + if (!FileExist(NewName)) + { + Name=NewName; + return true; + } + } + return false; +} diff --git a/unrar_sys/vendor/unrar/uiconsole.cpp b/unrar_sys/vendor/unrar/uiconsole.cpp index b524c25..cc1c72d 100644 --- a/unrar_sys/vendor/unrar/uiconsole.cpp +++ b/unrar_sys/vendor/unrar/uiconsole.cpp @@ -1,12 +1,11 @@ static bool AnyMessageDisplayed=false; // For console -idn switch. // Purely user interface function. Gets and returns user input. -UIASKREP_RESULT uiAskReplace(wchar *Name,size_t MaxNameSize,int64 FileSize,RarTime *FileTime,uint Flags) +UIASKREP_RESULT uiAskReplace(std::wstring &Name,int64 FileSize,RarTime *FileTime,uint Flags) { wchar SizeText1[20],DateStr1[50],SizeText2[20],DateStr2[50]; - FindData ExistingFD; - memset(&ExistingFD,0,sizeof(ExistingFD)); // In case find fails. + FindData ExistingFD={}; // Init in case find fails. FindFile::FastFind(Name,&ExistingFD); itoa(ExistingFD.Size,SizeText1,ASIZE(SizeText1)); ExistingFD.mtime.GetText(DateStr1,ASIZE(DateStr1),false); @@ -14,16 +13,16 @@ UIASKREP_RESULT uiAskReplace(wchar *Name,size_t MaxNameSize,int64 FileSize,RarTi if (FileSize==INT64NDF || FileTime==NULL) { eprintf(L"\n"); - eprintf(St(MAskOverwrite),Name); + eprintf(St(MAskOverwrite),Name.c_str()); } else { itoa(FileSize,SizeText2,ASIZE(SizeText2)); FileTime->GetText(DateStr2,ASIZE(DateStr2),false); if ((Flags & UIASKREP_F_EXCHSRCDEST)==0) - eprintf(St(MAskReplace),Name,SizeText1,DateStr1,SizeText2,DateStr2); + eprintf(St(MAskReplace),Name.c_str(),SizeText1,DateStr1,SizeText2,DateStr2); else - eprintf(St(MAskReplace),Name,SizeText2,DateStr2,SizeText1,DateStr1); + eprintf(St(MAskReplace),Name.c_str(),SizeText2,DateStr2,SizeText1,DateStr1); } bool AllowRename=(Flags & UIASKREP_F_NORENAME)==0; @@ -46,7 +45,7 @@ UIASKREP_RESULT uiAskReplace(wchar *Name,size_t MaxNameSize,int64 FileSize,RarTi if (AllowRename && Choice==5) { mprintf(St(MAskNewName)); - if (getwstr(Name,MaxNameSize)) + if (getwstr(Name)) return UIASKREP_R_RENAME; else return UIASKREP_R_SKIP; // Process fwgets failure as if user answered 'No'. @@ -57,13 +56,13 @@ UIASKREP_RESULT uiAskReplace(wchar *Name,size_t MaxNameSize,int64 FileSize,RarTi -void uiStartArchiveExtract(bool Extract,const wchar *ArcName) +void uiStartArchiveExtract(bool Extract,const std::wstring &ArcName) { - mprintf(St(Extract ? MExtracting : MExtrTest), ArcName); + mprintf(St(Extract ? MExtracting : MExtrTest), ArcName.c_str()); } -bool uiStartFileExtract(const wchar *FileName,bool Extract,bool Test,bool Skip) +bool uiStartFileExtract(const std::wstring &FileName,bool Extract,bool Test,bool Skip) { return true; } @@ -237,6 +236,12 @@ void uiMsgStore::Msg() case UIERROR_RECVOLCANNOTFIX: mprintf(St(MRecVolCannotFix)); break; + case UIERROR_EXTRDICTOUTMEM: + Log(Str[0],St(MExtrDictOutMem),Num[0]); +#ifdef _WIN_32 + Log(Str[0],St(MSuggest64bit)); +#endif + break; case UIERROR_UNEXPEOF: Log(Str[0],St(MLogUnexpEOF)); break; @@ -344,6 +349,9 @@ void uiMsgStore::Msg() case UIERROR_ADJUSTVALUE: Log(NULL,St(MAdjustValue),Str[0],Str[1]); break; + case UIERROR_SKIPUNSAFELINK: + Log(NULL,St(MSkipUnsafeLink),Str[0],Str[1]); + break; #ifndef SFX_MODULE case UIMSG_STRING: @@ -389,7 +397,6 @@ void uiMsgStore::Msg() break; - case UIEVENT_RRTESTINGSTART: mprintf(L"%s ",St(MTestingRR)); break; @@ -397,7 +404,7 @@ void uiMsgStore::Msg() } -bool uiGetPassword(UIPASSWORD_TYPE Type,const wchar *FileName, +bool uiGetPassword(UIPASSWORD_TYPE Type,const std::wstring &FileName, SecPassword *Password,CheckPassword *CheckPwd) { // Unlike GUI we cannot provide Cancel button here, so we use the empty @@ -433,14 +440,14 @@ void uiAlarm(UIALARM_TYPE Type) -bool uiAskNextVolume(wchar *VolName,size_t MaxSize) +bool uiAskNextVolume(std::wstring &VolName) { - eprintf(St(MAskNextVol),VolName); + eprintf(St(MAskNextVol),VolName.c_str()); return Ask(St(MContinueQuit))!=2; } -void uiAskRepeatRead(const wchar *FileName,bool &Ignore,bool &All,bool &Retry,bool &Quit) +void uiAskRepeatRead(const std::wstring &FileName,bool &Ignore,bool &All,bool &Retry,bool &Quit) { eprintf(St(MErrReadInfo)); int Code=Ask(St(MIgnoreAllRetryQuit)); @@ -452,16 +459,37 @@ void uiAskRepeatRead(const wchar *FileName,bool &Ignore,bool &All,bool &Retry,bo } -bool uiAskRepeatWrite(const wchar *FileName,bool DiskFull) +bool uiAskRepeatWrite(const std::wstring &FileName,bool DiskFull) { mprintf(L"\n"); - Log(NULL,St(DiskFull ? MNotEnoughDisk:MErrWrite),FileName); + Log(NULL,St(DiskFull ? MNotEnoughDisk:MErrWrite),FileName.c_str()); return Ask(St(MRetryAbort))==1; } +bool uiDictLimit(CommandData *Cmd,const std::wstring &FileName,uint64 DictSize,uint64 MaxDictSize) +{ + mprintf(L"\n%s",FileName.c_str()); + const uint64 GB=1024*1024*1024; // We display sizes in GB here. + + // Include the reminder for switches like -md6400m. + DictSize=DictSize/GB+(DictSize%GB!=0 ? 1:0); + + // Exclude reminder for in case it is less than archive dictionary size, + // but still more than nearest GB value. Otherwise we could have message + // like "7 GB dictionary exceeds 7 GB limit", where first 7 might be actually + // 6272 MB and second is 6400 MB. + MaxDictSize/=GB; + + mprintf(St(MDictNotAllowed),(uint)DictSize,(uint)MaxDictSize,(uint)DictSize); + mprintf(St(MDictExtrAnyway),(uint)DictSize,(uint)DictSize); + mprintf(L"\n"); + return false; // Stop extracting. +} + + #ifndef SFX_MODULE -const wchar *uiGetMonthName(int Month) +const wchar *uiGetMonthName(uint Month) { static MSGID MonthID[12]={ MMonthJan,MMonthFeb,MMonthMar,MMonthApr,MMonthMay,MMonthJun, diff --git a/unrar_sys/vendor/unrar/uisilent.cpp b/unrar_sys/vendor/unrar/uisilent.cpp index 8155885..806e468 100644 --- a/unrar_sys/vendor/unrar/uisilent.cpp +++ b/unrar_sys/vendor/unrar/uisilent.cpp @@ -1,5 +1,5 @@ // Purely user interface function. Gets and returns user input. -UIASKREP_RESULT uiAskReplace(wchar *Name,size_t MaxNameSize,int64 FileSize,RarTime *FileTime,uint Flags) +UIASKREP_RESULT uiAskReplace(std::wstring &Name,int64 FileSize,RarTime *FileTime,uint Flags) { return UIASKREP_R_REPLACE; } @@ -7,12 +7,12 @@ UIASKREP_RESULT uiAskReplace(wchar *Name,size_t MaxNameSize,int64 FileSize,RarTi -void uiStartArchiveExtract(bool Extract,const wchar *ArcName) +void uiStartArchiveExtract(bool Extract,const std::wstring &ArcName) { } -bool uiStartFileExtract(const wchar *FileName,bool Extract,bool Test,bool Skip) +bool uiStartFileExtract(const std::wstring &FileName,bool Extract,bool Test,bool Skip) { return true; } @@ -33,7 +33,7 @@ void uiMsgStore::Msg() } -bool uiGetPassword(UIPASSWORD_TYPE Type,const wchar *FileName, +bool uiGetPassword(UIPASSWORD_TYPE Type,const std::wstring &FileName, SecPassword *Password,CheckPassword *CheckPwd) { return false; @@ -62,8 +62,19 @@ void uiGiveTick() } +bool uiDictLimit(CommandData *Cmd,const std::wstring &FileName,uint64 DictSize,uint64 MaxDictSize) +{ +#ifdef RARDLL + if (Cmd->Callback!=nullptr && + Cmd->Callback(UCM_LARGEDICT,Cmd->UserData,(LPARAM)(DictSize/1024),(LPARAM)(MaxDictSize/1024))==1) + return true; // Continue extracting if unrar.dll callback permits it. +#endif + return false; // Stop extracting. +} + + #ifndef SFX_MODULE -const wchar *uiGetMonthName(int Month) +const wchar *uiGetMonthName(uint Month) { return L""; } diff --git a/unrar_sys/vendor/unrar/ulinks.cpp b/unrar_sys/vendor/unrar/ulinks.cpp index 141a97f..ea84155 100644 --- a/unrar_sys/vendor/unrar/ulinks.cpp +++ b/unrar_sys/vendor/unrar/ulinks.cpp @@ -1,6 +1,6 @@ -static bool UnixSymlink(CommandData *Cmd,const char *Target,const wchar *LinkName,RarTime *ftm,RarTime *fta) +static bool UnixSymlink(CommandData *Cmd,const std::string &Target,const wchar *LinkName,RarTime *ftm,RarTime *fta) { CreatePath(LinkName,true,Cmd->DisableNames); @@ -9,15 +9,15 @@ static bool UnixSymlink(CommandData *Cmd,const char *Target,const wchar *LinkNam // called earlier inside of uiAskReplaceEx. DelFile(LinkName); - char LinkNameA[NM]; - WideToChar(LinkName,LinkNameA,ASIZE(LinkNameA)); - if (symlink(Target,LinkNameA)==-1) // Error. + std::string LinkNameA; + WideToChar(LinkName,LinkNameA); + if (symlink(Target.c_str(),LinkNameA.c_str())==-1) // Error. { if (errno==EEXIST) uiMsg(UIERROR_ULINKEXIST,LinkName); else { - uiMsg(UIERROR_SLINKCREATE,UINULL,LinkName); + uiMsg(UIERROR_SLINKCREATE,L"",LinkName); ErrHandler.SetErrorCode(RARX_WARNING); } return false; @@ -29,14 +29,14 @@ static bool UnixSymlink(CommandData *Cmd,const char *Target,const wchar *LinkNam times[0].tv_nsec=fta->IsSet() ? long(fta->GetUnixNS()%1000000000) : UTIME_NOW; times[1].tv_sec=ftm->GetUnix(); times[1].tv_nsec=ftm->IsSet() ? long(ftm->GetUnixNS()%1000000000) : UTIME_NOW; - utimensat(AT_FDCWD,LinkNameA,times,AT_SYMLINK_NOFOLLOW); + utimensat(AT_FDCWD,LinkNameA.c_str(),times,AT_SYMLINK_NOFOLLOW); #else struct timeval tv[2]; tv[0].tv_sec=fta->GetUnix(); tv[0].tv_usec=long(fta->GetUnixNS()%1000000000/1000); tv[1].tv_sec=ftm->GetUnix(); tv[1].tv_usec=long(ftm->GetUnixNS()%1000000000/1000); - lutimes(LinkNameA,tv); + lutimes(LinkNameA.c_str(),tv); #endif #endif @@ -55,9 +55,9 @@ static bool IsFullPath(const char *PathA) // Unix ASCII version. // it didn't affect the number of path related characters we analyze // in IsRelativeSymlinkSafe later. // This check is likely to be excessive, but let's keep it anyway. -static bool SafeCharToWide(const char *Src,wchar *Dest,size_t DestSize) +static bool SafeCharToWide(const std::string &Src,std::wstring &Dest) { - if (!CharToWide(Src,Dest,DestSize) || *Dest==0) + if (!CharToWide(Src,Dest) || Dest.empty()) return false; uint SrcChars=0,DestChars=0; for (uint I=0;Src[I]!=0;I++) @@ -73,18 +73,18 @@ static bool SafeCharToWide(const char *Src,wchar *Dest,size_t DestSize) static bool ExtractUnixLink30(CommandData *Cmd,ComprDataIO &DataIO,Archive &Arc, const wchar *LinkName,bool &UpLink) { - char Target[NM]; if (IsLink(Arc.FileHead.FileAttr)) { size_t DataSize=(size_t)Arc.FileHead.PackSize; - if (DataSize>ASIZE(Target)-1) + if (DataSize>MAXPATHSIZE) return false; - if ((size_t)DataIO.UnpRead((byte *)Target,DataSize)!=DataSize) + std::vector TargetBuf(DataSize+1); + if ((size_t)DataIO.UnpRead((byte*)TargetBuf.data(),DataSize)!=DataSize) return false; - Target[DataSize]=0; + std::string Target(TargetBuf.data(),TargetBuf.size()); DataIO.UnpHash.Init(Arc.FileHead.FileHash.Type,1); - DataIO.UnpHash.Update(Target,strlen(Target)); + DataIO.UnpHash.Update(Target.data(),strlen(Target.data())); DataIO.UnpHash.Result(&Arc.FileHead.FileHash); // Return true in case of bad checksum, so link will be processed further @@ -92,16 +92,21 @@ static bool ExtractUnixLink30(CommandData *Cmd,ComprDataIO &DataIO,Archive &Arc, if (!DataIO.UnpHash.Cmp(&Arc.FileHead.FileHash,Arc.FileHead.UseHashKey ? Arc.FileHead.HashKey:NULL)) return true; - wchar TargetW[NM]; - if (!SafeCharToWide(Target,TargetW,ASIZE(TargetW))) + std::wstring TargetW; + if (!SafeCharToWide(Target.data(),TargetW)) return false; + TruncateAtZero(TargetW); // Use Arc.FileHead.FileName instead of LinkName, since LinkName // can include the destination path as a prefix, which can // confuse IsRelativeSymlinkSafe algorithm. if (!Cmd->AbsoluteLinks && (IsFullPath(TargetW) || - !IsRelativeSymlinkSafe(Cmd,Arc.FileHead.FileName,LinkName,TargetW))) + !IsRelativeSymlinkSafe(Cmd,Arc.FileHead.FileName.c_str(),LinkName,TargetW.c_str()))) + { + uiMsg(UIERROR_SKIPUNSAFELINK,Arc.FileHead.FileName,TargetW); + ErrHandler.SetErrorCode(RARX_WARNING); return false; - UpLink=strstr(Target,"..")!=NULL; + } + UpLink=Target.find("..")!=std::string::npos; return UnixSymlink(Cmd,Target,LinkName,&Arc.FileHead.mtime,&Arc.FileHead.atime); } return false; @@ -110,27 +115,36 @@ static bool ExtractUnixLink30(CommandData *Cmd,ComprDataIO &DataIO,Archive &Arc, static bool ExtractUnixLink50(CommandData *Cmd,const wchar *Name,FileHeader *hd) { - char Target[NM]; - WideToChar(hd->RedirName,Target,ASIZE(Target)); + std::string Target; + WideToChar(hd->RedirName,Target); if (hd->RedirType==FSREDIR_WINSYMLINK || hd->RedirType==FSREDIR_JUNCTION) { // Cannot create Windows absolute path symlinks in Unix. Only relative path // Windows symlinks can be created here. RAR 5.0 used \??\ prefix // for Windows absolute symlinks, since RAR 5.1 /??/ is used. // We escape ? as \? to avoid "trigraph" warning - if (strncmp(Target,"\\??\\",4)==0 || strncmp(Target,"/\?\?/",4)==0) + if (Target.rfind("\\??\\",0)!=std::string::npos || + Target.rfind("/\?\?/",0)!=std::string::npos) + { + uiMsg(UIERROR_SLINKCREATE,nullptr,L"\"" + hd->FileName + L"\" -> \"" + hd->RedirName + L"\""); + ErrHandler.SetErrorCode(RARX_WARNING); return false; - DosSlashToUnix(Target,Target,ASIZE(Target)); + } + DosSlashToUnix(Target,Target); } - wchar TargetW[NM]; - if (!SafeCharToWide(Target,TargetW,ASIZE(TargetW))) + std::wstring TargetW; + if (!SafeCharToWide(Target,TargetW)) return false; // Use hd->FileName instead of LinkName, since LinkName can include // the destination path as a prefix, which can confuse // IsRelativeSymlinkSafe algorithm. if (!Cmd->AbsoluteLinks && (IsFullPath(TargetW) || - !IsRelativeSymlinkSafe(Cmd,hd->FileName,Name,TargetW))) + !IsRelativeSymlinkSafe(Cmd,hd->FileName.c_str(),Name,TargetW.c_str()))) + { + uiMsg(UIERROR_SKIPUNSAFELINK,hd->FileName,TargetW); + ErrHandler.SetErrorCode(RARX_WARNING); return false; + } return UnixSymlink(Cmd,Target,Name,&hd->mtime,&hd->atime); } diff --git a/unrar_sys/vendor/unrar/unicode.cpp b/unrar_sys/vendor/unrar/unicode.cpp index 73b09bb..812e35b 100644 --- a/unrar_sys/vendor/unrar/unicode.cpp +++ b/unrar_sys/vendor/unrar/unicode.cpp @@ -128,6 +128,26 @@ bool CharToWide(const char *Src,wchar *Dest,size_t DestSize) } +bool WideToChar(const std::wstring &Src,std::string &Dest) +{ + // We need more than 1 char per wchar_t for DBCS and up to 4 for UTF-8. + std::vector DestA(4*Src.size()+1); // "+1" for terminating zero. + bool Result=WideToChar(Src.c_str(),DestA.data(),DestA.size()); + Dest=DestA.data(); + return Result; +} + + +bool CharToWide(const std::string &Src,std::wstring &Dest) +{ + // 2 wchar_t per char in case char is converted to UTF-16 surrogate pair. + std::vector DestW(2*Src.size()+1); // "+1" for terminating zero. + bool Result=CharToWide(Src.c_str(),DestW.data(),DestW.size()); + Dest=DestW.data(); + return Result; +} + + #if defined(_UNIX) && defined(MBFUNCTIONS) // Convert and restore mapped inconvertible Unicode characters. // We use it for extended ASCII names in Unix. @@ -244,6 +264,19 @@ byte* WideToRaw(const wchar *Src,size_t SrcSize,byte *Dest,size_t DestSize) } +// Store UTF-16 raw byte stream. +void WideToRaw(const std::wstring &Src,std::vector &Dest) +{ + for (wchar C : Src) + { + Dest.push_back((byte)C); + Dest.push_back((byte)(C>>8)); + } + Dest.push_back(0); // 2 bytes of trailing UTF-16 zero. + Dest.push_back(0); +} + + wchar* RawToWide(const byte *Src,wchar *Dest,size_t DestSize) { for (size_t I=0;I &Src) +{ + std::wstring Dest; + for (size_t I=0;I+1>6)); + Dest.push_back(0x80|(c&0x3f)); + } + else + { + if (c>=0xd800 && c<=0xdbff && I=0xdc00 && Src[I]<=0xdfff) // Surrogate pair. + { + c=((c-0xd800)<<10)+(Src[I]-0xdc00)+0x10000; + I++; + } + if (c<0x10000) + { + Dest.push_back(0xe0|(c>>12)); + Dest.push_back(0x80|((c>>6)&0x3f)); + Dest.push_back(0x80|(c&0x3f)); + } + else + if (c < 0x200000) + { + Dest.push_back(0xf0|(c>>18)); + Dest.push_back(0x80|((c>>12)&0x3f)); + Dest.push_back(0x80|((c>>6)&0x3f)); + Dest.push_back(0x80|(c&0x3f)); + } + } + } +} + + + size_t WideToUtfSize(const wchar *Src) { size_t Size=0; @@ -397,6 +484,146 @@ bool UtfToWide(const char *Src,wchar *Dest,size_t DestSize) } +bool UtfToWide(const char *Src,std::wstring &Dest) +{ + bool Success=true; + Dest.clear(); + while (*Src!=0) + { + uint c=byte(*(Src++)),d; + if (c<0x80) + d=c; + else + if ((c>>5)==6) + { + if ((*Src&0xc0)!=0x80) + { + Success=false; + break; + } + d=((c&0x1f)<<6)|(*Src&0x3f); + Src++; + } + else + if ((c>>4)==14) + { + if ((Src[0]&0xc0)!=0x80 || (Src[1]&0xc0)!=0x80) + { + Success=false; + break; + } + d=((c&0xf)<<12)|((Src[0]&0x3f)<<6)|(Src[1]&0x3f); + Src+=2; + } + else + if ((c>>3)==30) + { + if ((Src[0]&0xc0)!=0x80 || (Src[1]&0xc0)!=0x80 || (Src[2]&0xc0)!=0x80) + { + Success=false; + break; + } + d=((c&7)<<18)|((Src[0]&0x3f)<<12)|((Src[1]&0x3f)<<6)|(Src[2]&0x3f); + Src+=3; + } + else + { + Success=false; + break; + } + if (d>0xffff) + { + if (d>0x10ffff) // UTF-8 must end at 0x10ffff according to RFC 3629. + { + Success=false; + continue; + } + if (sizeof(wchar_t)==2) // Use the surrogate pair. + { + Dest.push_back( ((d-0x10000)>>10)+0xd800 ); + Dest.push_back( (d&0x3ff)+0xdc00 ); + } + else + Dest.push_back( d ); + } + else + Dest.push_back( d ); + } + return Success; +} + + +/* +bool UtfToWide(const std::vector &Src,std::wstring &Dest) +{ + bool Success=true; + Dest.clear(); + for (size_t I=0;I>5)==6) + { + if (Src.size()-I<1 || (Src[I]&0xc0)!=0x80) + { + Success=false; + break; + } + d=((c&0x1f)<<6)|(Src[I]&0x3f); + I++; + } + else + if ((c>>4)==14) + { + if (Src.size()-I<2 || (Src[I]&0xc0)!=0x80 || (Src[I+1]&0xc0)!=0x80) + { + Success=false; + break; + } + d=((c&0xf)<<12)|((Src[I]&0x3f)<<6)|(Src[I+1]&0x3f); + I+=2; + } + else + if ((c>>3)==30) + { + if (Src.size()-I<3 || (Src[I]&0xc0)!=0x80 || (Src[I+1]&0xc0)!=0x80 || (Src[I+2]&0xc0)!=0x80) + { + Success=false; + break; + } + d=((c&7)<<18)|((Src[I]&0x3f)<<12)|((Src[I+1]&0x3f)<<6)|(Src[I+2]&0x3f); + I+=3; + } + else + { + Success=false; + break; + } + if (d>0xffff) + { + if (d>0x10ffff) // UTF-8 must end at 0x10ffff according to RFC 3629. + { + Success=false; + continue; + } + if (sizeof(Dest[0])==2) // Use the surrogate pair. + { + Dest.push_back( ((d-0x10000)>>10)+0xd800 ); + Dest.push_back( (d&0x3ff)+0xdc00 ); + } + else + Dest.push_back( d ); + } + else + Dest.push_back( d ); + } + return Success; +} +*/ + + // For zero terminated strings. bool IsTextUtf8(const byte *Src) { @@ -450,8 +677,10 @@ int wcsnicomp(const wchar *s1,const wchar *s2,size_t n) // If we specify 'n' exceeding the actual string length, CompareString goes // beyond the trailing zero and compares garbage. So we need to limit 'n' // to real string length. - size_t l1=Min(wcslen(s1)+1,n); - size_t l2=Min(wcslen(s2)+1,n); + size_t sl1=wcslen(s1); // Pre-compute to not call wcslen() in Min() twice. + size_t l1=Min(sl1+1,n); + size_t sl2=wcslen(s2); // Pre-compute to not call wcslen() in Min() twice. + size_t l2=Min(sl2+1,n); return CompareStringW(LOCALE_USER_DEFAULT,NORM_IGNORECASE|SORT_STRINGSORT,s1,(int)l1,s2,(int)l2)-2; #else if (n==0) @@ -483,7 +712,15 @@ const wchar_t* wcscasestr(const wchar_t *str, const wchar_t *search) if (tolowerw(str[i+j])!=tolowerw(search[j])) break; } - return NULL; + return nullptr; +} + + +// Case insensitive std::wstring substring search. +std::wstring::size_type wcscasestr(const std::wstring &str, const std::wstring &search) +{ + const wchar *Found=wcscasestr(str.c_str(),search.c_str()); + return Found==nullptr ? std::wstring::npos : Found-str.c_str(); } @@ -500,10 +737,14 @@ wchar* wcslower(wchar *s) #endif return s; } -#endif -#ifndef SFX_MODULE +void wcslower(std::wstring &s) +{ + wcslower(&s[0]); +} + + wchar* wcsupper(wchar *s) { #ifdef _WIN_ALL @@ -516,6 +757,12 @@ wchar* wcsupper(wchar *s) #endif return s; } + + +void wcsupper(std::wstring &s) +{ + wcsupper(&s[0]); +} #endif @@ -548,27 +795,28 @@ int tolowerw(int ch) } -int atoiw(const wchar *s) +int atoiw(const std::wstring &s) { return (int)atoilw(s); } -int64 atoilw(const wchar *s) +int64 atoilw(const std::wstring &s) { bool sign=false; - if (*s=='-') // We do use signed integers here, for example, in GUI SFX. + size_t Pos=0; + if (s[Pos]=='-') // We do use signed integers here, for example, in GUI SFX. { - s++; + Pos++; sign=true; } // Use unsigned type here, since long string can overflow the variable // and signed integer overflow is undefined behavior in C++. uint64 n=0; - while (*s>='0' && *s<='9') + while (s[Pos]>='0' && s[Pos]<='9') { - n=n*10+(*s-'0'); - s++; + n=n*10+(s[Pos]-'0'); + Pos++; } // Check int64(n)>=0 to avoid the signed overflow with undefined behavior // when negating 0x8000000000000000. diff --git a/unrar_sys/vendor/unrar/unicode.hpp b/unrar_sys/vendor/unrar/unicode.hpp index 9bfd9c5..2d867b3 100644 --- a/unrar_sys/vendor/unrar/unicode.hpp +++ b/unrar_sys/vendor/unrar/unicode.hpp @@ -7,25 +7,37 @@ bool WideToChar(const wchar *Src,char *Dest,size_t DestSize); bool CharToWide(const char *Src,wchar *Dest,size_t DestSize); +bool WideToChar(const std::wstring &Src,std::string &Dest); +bool CharToWide(const std::string &Src,std::wstring &Dest); byte* WideToRaw(const wchar *Src,size_t SrcSize,byte *Dest,size_t DestSize); +void WideToRaw(const std::wstring &Src,std::vector &Dest); wchar* RawToWide(const byte *Src,wchar *Dest,size_t DestSize); +std::wstring RawToWide(const std::vector &Src); void WideToUtf(const wchar *Src,char *Dest,size_t DestSize); +void WideToUtf(const std::wstring &Src,std::string &Dest); size_t WideToUtfSize(const wchar *Src); bool UtfToWide(const char *Src,wchar *Dest,size_t DestSize); +bool UtfToWide(const char *Src,std::wstring &Dest); +//bool UtfToWide(const std::vector &Src,std::wstring &Dest); bool IsTextUtf8(const byte *Src); bool IsTextUtf8(const byte *Src,size_t SrcSize); int wcsicomp(const wchar *s1,const wchar *s2); +inline int wcsicomp(const std::wstring &s1,const std::wstring &s2) {return wcsicomp(s1.c_str(),s2.c_str());} int wcsnicomp(const wchar *s1,const wchar *s2,size_t n); +inline int wcsnicomp(const std::wstring &s1,const std::wstring &s2,size_t n) {return wcsnicomp(s1.c_str(),s2.c_str(),n);} const wchar_t* wcscasestr(const wchar_t *str, const wchar_t *search); +std::wstring::size_type wcscasestr(const std::wstring &str, const std::wstring &search); #ifndef SFX_MODULE wchar* wcslower(wchar *s); +void wcslower(std::wstring &s); wchar* wcsupper(wchar *s); +void wcsupper(std::wstring &s); #endif int toupperw(int ch); int tolowerw(int ch); -int atoiw(const wchar *s); -int64 atoilw(const wchar *s); +int atoiw(const std::wstring &s); +int64 atoilw(const std::wstring &s); #ifdef DBCS_SUPPORTED class SupportDBCS diff --git a/unrar_sys/vendor/unrar/unpack.cpp b/unrar_sys/vendor/unrar/unpack.cpp index 9236e74..99c6f15 100644 --- a/unrar_sys/vendor/unrar/unpack.cpp +++ b/unrar_sys/vendor/unrar/unpack.cpp @@ -10,8 +10,8 @@ #ifndef SFX_MODULE #include "unpack15.cpp" #include "unpack20.cpp" -#endif #include "unpack30.cpp" +#endif #include "unpack50.cpp" #include "unpack50frag.cpp" @@ -24,18 +24,20 @@ Unpack::Unpack(ComprDataIO *DataIO) Suspended=false; UnpAllBuf=false; UnpSomeRead=false; + ExtraDist=false; #ifdef RAR_SMP MaxUserThreads=1; UnpThreadPool=NULL; ReadBufMT=NULL; UnpThreadData=NULL; #endif + AllocWinSize=0; MaxWinSize=0; MaxWinMask=0; // Perform initialization, which should be done only once for all files. - // It prevents crash if first DoUnpack call is later made with wrong - // (true) 'Solid' value. + // It prevents crash if first unpacked file has the wrong "true" Solid flag, + // so first DoUnpack call is made with the wrong "true" Solid value later. UnpInitData(false); #ifndef SFX_MODULE // RAR 1.5 decompression initialization @@ -47,10 +49,11 @@ Unpack::Unpack(ComprDataIO *DataIO) Unpack::~Unpack() { +#ifndef SFX_MODULE InitFilters30(false); +#endif - if (Window!=NULL) - free(Window); + free(Window); #ifdef RAR_SMP delete UnpThreadPool; delete[] ReadBufMT; @@ -70,13 +73,11 @@ void Unpack::SetThreads(uint Threads) #endif -void Unpack::Init(size_t WinSize,bool Solid) +// We get 64-bit WinSize, so we still can check and quit for dictionaries +// exceeding a threshold in 32-bit builds. Then we convert WinSize to size_t +// MaxWinSize. +void Unpack::Init(uint64 WinSize,bool Solid) { - // If 32-bit RAR unpacks an archive with 4 GB dictionary, the window size - // will be 0 because of size_t overflow. Let's issue the memory error. - if (WinSize==0) - ErrHandler.MemoryError(); - // Minimum window size must be at least twice more than maximum possible // size of filter block, which is 0x10000 in RAR now. If window size is // smaller, we can have a block with never cleared flt->NextWindow flag @@ -86,39 +87,53 @@ void Unpack::Init(size_t WinSize,bool Solid) if (WinSize>16)>0x10000) // Window size must not exceed 4 GB. + if (WinSize>Min(0x10000000000ULL,UNPACK_MAX_DICT)) // Window size must not exceed 1 TB. + throw std::bad_alloc(); + + // 32-bit build can't unpack dictionaries exceeding 32-bit even in theory. + // Also we've not verified if WrapUp and WrapDown work properly in 32-bit + // version and >2GB dictionary and if 32-bit version can handle >2GB + // distances. Since such version is unlikely to allocate >2GB anyway, + // we prohibit >2GB dictionaries for 32-bit build here. + if (WinSize>0x80000000 && sizeof(size_t)<=4) + throw std::bad_alloc(); + + // Solid block shall use the same window size for all files. + // But if Window isn't initialized when Solid is set, it means that + // first file in solid block doesn't have the solid flag. We initialize + // the window anyway for such malformed archive. + // Non-solid files shall use their specific window sizes, + // so current window size and unpack routine behavior doesn't depend on + // previously unpacked files and their extraction order. + if (!Solid || Window==nullptr) + { + MaxWinSize=(size_t)WinSize; + MaxWinMask=MaxWinSize-1; + } + + // Use the already allocated window when processing non-solid files + // with reducing dictionary sizes. + if (WinSize<=AllocWinSize) return; // Archiving code guarantees that window size does not grow in the same // solid stream. So if we are here, we are either creating a new window // or increasing the size of non-solid window. So we could safely reject - // current window data without copying them to a new window, though being - // extra cautious, we still handle the solid window grow case below. - bool Grow=Solid && (Window!=NULL || Fragmented); - - // We do not handle growth for existing fragmented window. - if (Grow && Fragmented) + // current window data without copying them to a new window. + if (Solid && (Window!=NULL || Fragmented && WinSize>FragWindow.GetWinSize())) throw std::bad_alloc(); - byte *NewWindow=Fragmented ? NULL : (byte *)malloc(WinSize); + free(Window); + + Window=Fragmented ? NULL : (byte *)malloc((size_t)WinSize); - if (NewWindow==NULL) - if (Grow || WinSize<0x1000000) - { - // We do not support growth for new fragmented window. - // Also exclude RAR4 and small dictionaries. - throw std::bad_alloc(); - } + if (Window==NULL) + if (WinSize<0x1000000 || sizeof(size_t)>4) + throw std::bad_alloc(); // Exclude RAR4, small dictionaries and 64-bit. else { - if (Window!=NULL) // If allocated by preceding files. - { - free(Window); - Window=NULL; - } - FragWindow.Init(WinSize); + if (WinSize>FragWindow.GetWinSize()) + FragWindow.Init((size_t)WinSize); Fragmented=true; } @@ -126,23 +141,12 @@ void Unpack::Init(size_t WinSize,bool Solid) { // Clean the window to generate the same output when unpacking corrupt // RAR files, which may access unused areas of sliding dictionary. - memset(NewWindow,0,WinSize); - - // If Window is not NULL, it means that window size has grown. - // In solid streams we need to copy data to a new window in such case. - // RAR archiving code does not allow it in solid streams now, - // but let's implement it anyway just in case we'll change it sometimes. - if (Grow) - for (size_t I=1;I<=MaxWinSize;I++) - NewWindow[(UnpPtr-I)&(WinSize-1)]=Window[(UnpPtr-I)&(MaxWinSize-1)]; - - if (Window!=NULL) - free(Window); - Window=NewWindow; - } + // 2023.10.31: We've added FirstWinDone based unused area access check + // in Unpack::CopyString(), so this memset might be unnecessary now. +// memset(Window,0,(size_t)WinSize); - MaxWinSize=WinSize; - MaxWinMask=MaxWinSize-1; + AllocWinSize=WinSize; + } } @@ -154,21 +158,23 @@ void Unpack::DoUnpack(uint Method,bool Solid) switch(Method) { #ifndef SFX_MODULE - case 15: // rar 1.5 compression + case 15: // RAR 1.5 compression. if (!Fragmented) Unpack15(Solid); break; - case 20: // rar 2.x compression - case 26: // files larger than 2GB + case 20: // RAR 2.x compression. + case 26: // Files larger than 2GB. if (!Fragmented) Unpack20(Solid); break; -#endif - case 29: // rar 3.x compression + case 29: // RAR 3.x compression. if (!Fragmented) Unpack29(Solid); break; - case 50: // RAR 5.0 compression algorithm. +#endif + case VER_PACK5: // 50. RAR 5.0 and 7.0 compression algorithms. + case VER_PACK7: // 70. + ExtraDist=(Method==VER_PACK7); #ifdef RAR_SMP if (MaxUserThreads>1) { @@ -194,13 +200,19 @@ void Unpack::UnpInitData(bool Solid) { if (!Solid) { - memset(OldDist,0,sizeof(OldDist)); + OldDist[0]=OldDist[1]=OldDist[2]=OldDist[3]=(size_t)-1; + OldDistPtr=0; - LastDist=LastLength=0; + + LastDist=(uint)-1; // Initialize it to -1 like LastDist. + LastLength=0; + // memset(Window,0,MaxWinSize); memset(&BlockTables,0,sizeof(BlockTables)); UnpPtr=WrPtr=0; - WriteBorder=Min(MaxWinSize,UNPACK_MAX_WRITE)&MaxWinMask; + PrevPtr=0; + FirstWinDone=false; + WriteBorder=Min(MaxWinSize,UNPACK_MAX_WRITE); } // Filters never share several solid files, so we can safely reset them // even in solid archive. @@ -215,8 +227,8 @@ void Unpack::UnpInitData(bool Solid) BlockHeader.BlockSize=-1; // '-1' means not defined yet. #ifndef SFX_MODULE UnpInitData20(Solid); -#endif UnpInitData30(Solid); +#endif UnpInitData50(Solid); } diff --git a/unrar_sys/vendor/unrar/unpack.hpp b/unrar_sys/vendor/unrar/unpack.hpp index 737b31c..5ed904a 100644 --- a/unrar_sys/vendor/unrar/unpack.hpp +++ b/unrar_sys/vendor/unrar/unpack.hpp @@ -2,7 +2,7 @@ #define _RAR_UNPACK_ // Maximum allowed number of compressed bits processed in quick mode. -#define MAX_QUICK_DECODE_BITS 10 +#define MAX_QUICK_DECODE_BITS 9 // Maximum number of filters per entire data block. Must be at least // twice more than MAX_PACK_FILTERS to store filters from two data blocks. @@ -102,7 +102,7 @@ struct UnpackDecodedItem ushort Length; union { - uint Distance; + size_t Distance; byte Literal[8]; // Store up to 8 chars here to speed up extraction. }; }; @@ -143,13 +143,13 @@ struct UnpackThreadData struct UnpackFilter { + // Groop 'byte' and 'bool' together to reduce the actual struct size. byte Type; - uint BlockStart; - uint BlockLength; byte Channels; -// uint Width; -// byte PosR; bool NextWindow; + + size_t BlockStart; + uint BlockLength; }; @@ -188,14 +188,16 @@ class FragmentedWindow void Reset(); byte *Mem[MAX_MEM_BLOCKS]; size_t MemSize[MAX_MEM_BLOCKS]; + size_t LastAllocated; public: FragmentedWindow(); ~FragmentedWindow(); void Init(size_t WinSize); byte& operator [](size_t Item); - void CopyString(uint Length,uint Distance,size_t &UnpPtr,size_t MaxWinMask); + void CopyString(uint Length,size_t Distance,size_t &UnpPtr,bool FirstWinDone,size_t MaxWinSize); void CopyData(byte *Dest,size_t WinPos,size_t Size); size_t GetBlockSize(size_t StartPos,size_t RequiredSize); + size_t GetWinSize() {return LastAllocated;} }; @@ -216,10 +218,9 @@ class Unpack:PackDef bool ReadTables(BitInput &Inp,UnpackBlockHeader &Header,UnpackBlockTables &Tables); void MakeDecodeTables(byte *LengthTable,DecodeTable *Dec,uint Size); _forceinline uint DecodeNumber(BitInput &Inp,DecodeTable *Dec); - void CopyString(); - inline void InsertOldDist(unsigned int Distance); + inline void InsertOldDist(size_t Distance); void UnpInitData(bool Solid); - _forceinline void CopyString(uint Length,uint Distance); + _forceinline void CopyString(uint Length,size_t Distance); uint ReadFilterData(BitInput &Inp); bool ReadFilter(BitInput &Inp,UnpackFilter &Filter); bool AddFilter(UnpackFilter &Filter); @@ -240,20 +241,25 @@ class Unpack:PackDef byte *ReadBufMT; #endif - Array FilterSrcMemory; - Array FilterDstMemory; + std::vector FilterSrcMemory; + std::vector FilterDstMemory; // Filters code, one entry per filter. - Array Filters; + std::vector Filters; - uint OldDist[4],OldDistPtr; + size_t OldDist[4],OldDistPtr; uint LastLength; // LastDist is necessary only for RAR2 and older with circular OldDist // array. In RAR3 last distance is always stored in OldDist[0]. uint LastDist; - size_t UnpPtr,WrPtr; + size_t UnpPtr; // Current position in window. + + size_t PrevPtr; // UnpPtr value for previous loop iteration. + bool FirstWinDone; // At least one dictionary was processed. + + size_t WrPtr; // Last written unpacked data position. // Top border of read packed data. int ReadTop; @@ -266,7 +272,7 @@ class Unpack:PackDef UnpackBlockHeader BlockHeader; UnpackBlockTables BlockTables; - size_t WriteBorder; + size_t WriteBorder; // Perform write when reaching this border. byte *Window; @@ -289,7 +295,7 @@ class Unpack:PackDef void LongLZ(); void HuffDecode(); void GetFlagsBuf(); - void UnpInitData15(int Solid); + void UnpInitData15(bool Solid); void InitHuff(); void CorrHuff(ushort *CharSet,byte *NumToPlace); void CopyString15(uint Distance,uint Length); @@ -359,15 +365,15 @@ class Unpack:PackDef BitInput VMCodeInp; // Filters code, one entry per filter. - Array Filters30; + std::vector Filters30; // Filters stack, several entrances of same filter are possible. - Array PrgStack; + std::vector PrgStack; // Lengths of preceding data blocks, one length of one last block // for every filter. Used to reduce the size required to write // the data block length if lengths are repeating. - Array OldFilterLengths; + std::vector OldFilterLengths; int LastFilter; /***************************** Unpack v 3.0 *********************************/ @@ -375,9 +381,9 @@ class Unpack:PackDef public: Unpack(ComprDataIO *DataIO); ~Unpack(); - void Init(size_t WinSize,bool Solid); + void Init(uint64 WinSize,bool Solid); void DoUnpack(uint Method,bool Solid); - bool IsFileExtracted() {return(FileExtracted);} + bool IsFileExtracted() {return FileExtracted;} void SetDestSize(int64 DestSize) {DestUnpSize=DestSize;FileExtracted=false;} void SetSuspended(bool Suspended) {Unpack::Suspended=Suspended;} @@ -386,10 +392,13 @@ class Unpack:PackDef void UnpackDecode(UnpackThreadData &D); #endif + uint64 AllocWinSize; size_t MaxWinSize; size_t MaxWinMask; - uint GetChar() + bool ExtraDist; // Allow distances up to 1 TB. + + byte GetChar() { if (Inp.InAddr>BitInput::MAX_SIZE-30) { @@ -399,6 +408,30 @@ class Unpack:PackDef } return Inp.InBuf[Inp.InAddr++]; } + + + // If window position crosses the window beginning, wrap it to window end. + // Replaces &MaxWinMask for non-power 2 window sizes. + // We can't use %WinSize everywhere not only for performance reason, + // but also because C++ % is reminder instead of modulo. + // We need additional checks in the code if WinPos distance from 0 + // can exceed MaxWinSize. Alternatively we could add such check here. + inline size_t WrapDown(size_t WinPos) + { + return WinPos >= MaxWinSize ? WinPos + MaxWinSize : WinPos; + } + + // If window position crosses the window end, wrap it to window beginning. + // Replaces &MaxWinMask for non-power 2 window sizes. + // Unlike WrapDown, we can use %WinSize here if there was no size_t + // overflow when calculating WinPos. + // We need additional checks in the code if WinPos distance from MaxWinSize + // can be MaxWinSize or more. Alternatively we could add such check here + // or use %WinSize. + inline size_t WrapUp(size_t WinPos) + { + return WinPos >= MaxWinSize ? WinPos - MaxWinSize : WinPos; + } }; #endif diff --git a/unrar_sys/vendor/unrar/unpack15.cpp b/unrar_sys/vendor/unrar/unpack15.cpp index 1e7cf76..683d945 100644 --- a/unrar_sys/vendor/unrar/unpack15.cpp +++ b/unrar_sys/vendor/unrar/unpack15.cpp @@ -1,40 +1,40 @@ #define STARTL1 2 -static unsigned int DecL1[]={0x8000,0xa000,0xc000,0xd000,0xe000,0xea00, +static uint DecL1[]={0x8000,0xa000,0xc000,0xd000,0xe000,0xea00, 0xee00,0xf000,0xf200,0xf200,0xffff}; -static unsigned int PosL1[]={0,0,0,2,3,5,7,11,16,20,24,32,32}; +static uint PosL1[]={0,0,0,2,3,5,7,11,16,20,24,32,32}; #define STARTL2 3 -static unsigned int DecL2[]={0xa000,0xc000,0xd000,0xe000,0xea00,0xee00, +static uint DecL2[]={0xa000,0xc000,0xd000,0xe000,0xea00,0xee00, 0xf000,0xf200,0xf240,0xffff}; -static unsigned int PosL2[]={0,0,0,0,5,7,9,13,18,22,26,34,36}; +static uint PosL2[]={0,0,0,0,5,7,9,13,18,22,26,34,36}; #define STARTHF0 4 -static unsigned int DecHf0[]={0x8000,0xc000,0xe000,0xf200,0xf200,0xf200, +static uint DecHf0[]={0x8000,0xc000,0xe000,0xf200,0xf200,0xf200, 0xf200,0xf200,0xffff}; -static unsigned int PosHf0[]={0,0,0,0,0,8,16,24,33,33,33,33,33}; +static uint PosHf0[]={0,0,0,0,0,8,16,24,33,33,33,33,33}; #define STARTHF1 5 -static unsigned int DecHf1[]={0x2000,0xc000,0xe000,0xf000,0xf200,0xf200, +static uint DecHf1[]={0x2000,0xc000,0xe000,0xf000,0xf200,0xf200, 0xf7e0,0xffff}; -static unsigned int PosHf1[]={0,0,0,0,0,0,4,44,60,76,80,80,127}; +static uint PosHf1[]={0,0,0,0,0,0,4,44,60,76,80,80,127}; #define STARTHF2 5 -static unsigned int DecHf2[]={0x1000,0x2400,0x8000,0xc000,0xfa00,0xffff, +static uint DecHf2[]={0x1000,0x2400,0x8000,0xc000,0xfa00,0xffff, 0xffff,0xffff}; -static unsigned int PosHf2[]={0,0,0,0,0,0,2,7,53,117,233,0,0}; +static uint PosHf2[]={0,0,0,0,0,0,2,7,53,117,233,0,0}; #define STARTHF3 6 -static unsigned int DecHf3[]={0x800,0x2400,0xee00,0xfe80,0xffff,0xffff, +static uint DecHf3[]={0x800,0x2400,0xee00,0xfe80,0xffff,0xffff, 0xffff}; -static unsigned int PosHf3[]={0,0,0,0,0,0,0,2,16,218,251,0,0}; +static uint PosHf3[]={0,0,0,0,0,0,0,2,16,218,251,0,0}; #define STARTHF4 8 -static unsigned int DecHf4[]={0xff00,0xffff,0xffff,0xffff,0xffff,0xffff}; -static unsigned int PosHf4[]={0,0,0,0,0,0,0,0,0,255,0,0,0}; +static uint DecHf4[]={0xff00,0xffff,0xffff,0xffff,0xffff,0xffff}; +static uint PosHf4[]={0,0,0,0,0,0,0,0,0,255,0,0,0}; void Unpack::Unpack15(bool Solid) @@ -60,6 +60,9 @@ void Unpack::Unpack15(bool Solid) { UnpPtr&=MaxWinMask; + FirstWinDone|=(PrevPtr>UnpPtr); + PrevPtr=UnpPtr; + if (Inp.InAddr>ReadTop-30 && !UnpReadBuf()) break; if (((WrPtr-UnpPtr) & MaxWinMask)<270 && WrPtr!=UnpPtr) @@ -116,27 +119,27 @@ void Unpack::Unpack15(bool Solid) void Unpack::ShortLZ() { - static unsigned int ShortLen1[]={1,3,4,4,5,6,7,8,8,4,4,5,6,6,4,0}; - static unsigned int ShortXor1[]={0,0xa0,0xd0,0xe0,0xf0,0xf8,0xfc,0xfe, + static uint ShortLen1[]={1,3,4,4,5,6,7,8,8,4,4,5,6,6,4,0}; + static uint ShortXor1[]={0,0xa0,0xd0,0xe0,0xf0,0xf8,0xfc,0xfe, 0xff,0xc0,0x80,0x90,0x98,0x9c,0xb0}; - static unsigned int ShortLen2[]={2,3,3,3,4,4,5,6,6,4,4,5,6,6,4,0}; - static unsigned int ShortXor2[]={0,0x40,0x60,0xa0,0xd0,0xe0,0xf0,0xf8, + static uint ShortLen2[]={2,3,3,3,4,4,5,6,6,4,4,5,6,6,4,0}; + static uint ShortXor2[]={0,0x40,0x60,0xa0,0xd0,0xe0,0xf0,0xf8, 0xfc,0xc0,0x80,0x90,0x98,0x9c,0xb0}; - unsigned int Length,SaveLength; - unsigned int LastDistance; - unsigned int Distance; + uint Length,SaveLength; + uint LastDistance; + uint Distance; int DistancePlace; NumHuf=0; - unsigned int BitField=Inp.fgetbits(); + uint BitField=Inp.fgetbits(); if (LCount==2) { Inp.faddbits(1); if (BitField >= 0x8000) { - CopyString15((unsigned int)LastDist,LastLength); + CopyString15(LastDist,LastLength); return; } BitField <<= 1; @@ -168,7 +171,7 @@ void Unpack::ShortLZ() if (Length == 9) { LCount++; - CopyString15((unsigned int)LastDist,LastLength); + CopyString15(LastDist,LastLength); return; } if (Length == 14) @@ -185,7 +188,7 @@ void Unpack::ShortLZ() LCount=0; SaveLength=Length; - Distance=OldDist[(OldDistPtr-(Length-9)) & 3]; + Distance=(uint)OldDist[(OldDistPtr-(Length-9)) & 3]; Length=DecodeNum(Inp.fgetbits(),STARTL1,DecL1,PosL1)+2; if (Length==0x101 && SaveLength==10) { @@ -214,8 +217,8 @@ void Unpack::ShortLZ() if (--DistancePlace != -1) { LastDistance=ChSetA[DistancePlace]; - ChSetA[DistancePlace+1]=LastDistance; - ChSetA[DistancePlace]=Distance; + ChSetA[DistancePlace+1]=(ushort)LastDistance; + ChSetA[DistancePlace]=(ushort)Distance; } Length+=2; OldDist[OldDistPtr++] = ++Distance; @@ -228,10 +231,10 @@ void Unpack::ShortLZ() void Unpack::LongLZ() { - unsigned int Length; - unsigned int Distance; - unsigned int DistancePlace,NewDistancePlace; - unsigned int OldAvr2,OldAvr3; + uint Length; + uint Distance; + uint DistancePlace,NewDistancePlace; + uint OldAvr2,OldAvr3; NumHuf=0; Nlzb+=16; @@ -242,7 +245,7 @@ void Unpack::LongLZ() } OldAvr2=AvrLn2; - unsigned int BitField=Inp.fgetbits(); + uint BitField=Inp.fgetbits(); if (AvrLn2 >= 122) Length=DecodeNum(BitField,STARTL2,DecL2,PosL2); else @@ -286,7 +289,7 @@ void Unpack::LongLZ() } ChSetB[DistancePlace & 0xff]=ChSetB[NewDistancePlace]; - ChSetB[NewDistancePlace]=Distance; + ChSetB[NewDistancePlace]=(ushort)Distance; Distance=((Distance & 0xff00) | (Inp.fgetbits() >> 8)) >> 1; Inp.faddbits(7); @@ -320,12 +323,12 @@ void Unpack::LongLZ() void Unpack::HuffDecode() { - unsigned int CurByte,NewBytePlace; - unsigned int Length; - unsigned int Distance; + uint CurByte,NewBytePlace; + uint Length; + uint Distance; int BytePlace; - unsigned int BitField=Inp.fgetbits(); + uint BitField=Inp.fgetbits(); if (AvrPlc > 0x75ff) BytePlace=DecodeNum(BitField,STARTHF4,DecHf4,PosHf4); @@ -392,14 +395,14 @@ void Unpack::HuffDecode() } ChSet[BytePlace]=ChSet[NewBytePlace]; - ChSet[NewBytePlace]=CurByte; + ChSet[NewBytePlace]=(ushort)CurByte; } void Unpack::GetFlagsBuf() { - unsigned int Flags,NewFlagsPlace; - unsigned int FlagsPlace=DecodeNum(Inp.fgetbits(),STARTHF2,DecHf2,PosHf2); + uint Flags,NewFlagsPlace; + uint FlagsPlace=DecodeNum(Inp.fgetbits(),STARTHF2,DecHf2,PosHf2); // Our Huffman table stores 257 items and needs all them in other parts // of code such as when StMode is on, so the first item is control item. @@ -420,11 +423,11 @@ void Unpack::GetFlagsBuf() } ChSetC[FlagsPlace]=ChSetC[NewFlagsPlace]; - ChSetC[NewFlagsPlace]=Flags; + ChSetC[NewFlagsPlace]=(ushort)Flags; } -void Unpack::UnpInitData15(int Solid) +void Unpack::UnpInitData15(bool Solid) { if (!Solid) { @@ -443,7 +446,7 @@ void Unpack::UnpInitData15(int Solid) void Unpack::InitHuff() { - for (unsigned int I=0;I<256;I++) + for (ushort I=0;I<256;I++) { ChSet[I]=ChSetB[I]=I<<8; ChSetA[I]=I; @@ -471,11 +474,18 @@ void Unpack::CorrHuff(ushort *CharSet,byte *NumToPlace) void Unpack::CopyString15(uint Distance,uint Length) { DestUnpSize-=Length; - while (Length--) - { - Window[UnpPtr]=Window[(UnpPtr-Distance) & MaxWinMask]; - UnpPtr=(UnpPtr+1) & MaxWinMask; - } + if (!FirstWinDone && Distance>UnpPtr || Distance>MaxWinSize) + while (Length-- > 0) + { + Window[UnpPtr]=0; + UnpPtr=(UnpPtr+1) & MaxWinMask; + } + else + while (Length-- > 0) + { + Window[UnpPtr]=Window[(UnpPtr-Distance) & MaxWinMask]; + UnpPtr=(UnpPtr+1) & MaxWinMask; + } } diff --git a/unrar_sys/vendor/unrar/unpack20.cpp b/unrar_sys/vendor/unrar/unpack20.cpp index 93c8ba0..a0e179d 100644 --- a/unrar_sys/vendor/unrar/unpack20.cpp +++ b/unrar_sys/vendor/unrar/unpack20.cpp @@ -2,7 +2,8 @@ void Unpack::CopyString20(uint Length,uint Distance) { - LastDist=OldDist[OldDistPtr++]=Distance; + LastDist=Distance; + OldDist[OldDistPtr++]=Distance; OldDistPtr = OldDistPtr & 3; // Needed if RAR 1.5 file is called after RAR 2.0. LastLength=Length; DestUnpSize-=Length; @@ -36,6 +37,9 @@ void Unpack::Unpack20(bool Solid) { UnpPtr&=MaxWinMask; + FirstWinDone|=(PrevPtr>UnpPtr); + PrevPtr=UnpPtr; + if (Inp.InAddr>ReadTop-30) if (!UnpReadBuf()) break; @@ -109,7 +113,7 @@ void Unpack::Unpack20(bool Solid) } if (Number<261) { - uint Distance=OldDist[(OldDistPtr-(Number-256)) & 3]; + uint Distance=(uint)OldDist[(OldDistPtr-(Number-256)) & 3]; uint LengthNumber=DecodeNumber(Inp,&BlockTables.RD); uint Length=LDecode[LengthNumber]+2; if ((Bits=LBits[LengthNumber])>0) diff --git a/unrar_sys/vendor/unrar/unpack30.cpp b/unrar_sys/vendor/unrar/unpack30.cpp index 7c2adfa..1fb0615 100644 --- a/unrar_sys/vendor/unrar/unpack30.cpp +++ b/unrar_sys/vendor/unrar/unpack30.cpp @@ -17,8 +17,8 @@ void Unpack::Unpack29(bool Solid) { static unsigned char LDecode[]={0,1,2,3,4,5,6,7,8,10,12,14,16,20,24,28,32,40,48,56,64,80,96,112,128,160,192,224}; static unsigned char LBits[]= {0,0,0,0,0,0,0,0,1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5}; - static int DDecode[DC]; - static byte DBits[DC]; + static int DDecode[DC30]; + static byte DBits[DC30]; static int DBitLengthCounts[]= {4,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,14,0,12}; static unsigned char SDDecode[]={0,4,8,16,32,64,128,192}; static unsigned char SDBits[]= {2,2,3, 4, 5, 6, 6, 6}; @@ -50,6 +50,9 @@ void Unpack::Unpack29(bool Solid) { UnpPtr&=MaxWinMask; + FirstWinDone|=(PrevPtr>UnpPtr); + PrevPtr=UnpPtr; + if (Inp.InAddr>ReadBorder) { if (!UnpReadBuf30()) @@ -219,7 +222,7 @@ void Unpack::Unpack29(bool Solid) if (Number<263) { uint DistNum=Number-259; - uint Distance=OldDist[DistNum]; + uint Distance=(uint)OldDist[DistNum]; for (uint I=DistNum;I>0;I--) OldDist[I]=OldDist[I-1]; OldDist[0]=Distance; @@ -306,7 +309,7 @@ bool Unpack::ReadVMCode() } if (Length==0) return false; - Array VMCode(Length); + std::vector VMCode(Length); for (uint I=0;I>8; Inp.addbits(8); } - return AddVMCode(FirstByte,&VMCode[0],Length); + return AddVMCode(FirstByte,VMCode.data(),Length); } @@ -346,7 +349,7 @@ bool Unpack::ReadVMCodePPM() } if (Length==0) return false; - Array VMCode(Length); + std::vector VMCode(Length); for (uint I=0;IFilters30.Size() || FiltPos>OldFilterLengths.Size()) + if (FiltPos>Filters30.size() || FiltPos>OldFilterLengths.size()) return false; LastFilter=FiltPos; - bool NewFilter=(FiltPos==Filters30.Size()); + bool NewFilter=(FiltPos==Filters30.size()); UnpackFilter30 *StackFilter=new UnpackFilter30; // New filter for PrgStack. @@ -393,15 +396,15 @@ bool Unpack::AddVMCode(uint FirstByte,byte *Code,uint CodeSize) return false; } - Filters30.Add(1); - Filters30[Filters30.Size()-1]=Filter=new UnpackFilter30; - StackFilter->ParentFilter=(uint)(Filters30.Size()-1); + StackFilter->ParentFilter=(uint)Filters30.size(); + Filter=new UnpackFilter30; + Filters30.push_back(Filter); // Reserve one item to store the data block length of our new filter // entry. We'll set it to real block length below, after reading it. // But we need to initialize it now, because when processing corrupt // data, we can access this item even before we set it to real value. - OldFilterLengths.Push(0); + OldFilterLengths.push_back(0); } else // Filter was used in the past. { @@ -410,7 +413,7 @@ bool Unpack::AddVMCode(uint FirstByte,byte *Code,uint CodeSize) } uint EmptyCount=0; - for (uint I=0;IMAX3_UNPACK_FILTERS) + if (PrgStack.size()>MAX3_UNPACK_FILTERS) { delete StackFilter; return false; } - PrgStack.Add(1); + PrgStack.resize(PrgStack.size()+1); EmptyCount=1; } - size_t StackPos=PrgStack.Size()-EmptyCount; + size_t StackPos=PrgStack.size()-EmptyCount; PrgStack[StackPos]=StackFilter; uint BlockStart=RarVM::ReadData(VMCodeInp); @@ -448,7 +451,7 @@ bool Unpack::AddVMCode(uint FirstByte,byte *Code,uint CodeSize) // for same filter. It is possible for corrupt data to access a new // and not filled yet item of OldFilterLengths array here. This is why // we set new OldFilterLengths items to zero above. - StackFilter->BlockLength=FiltPosBlockLength=FiltPosNextWindow=WrPtr!=UnpPtr && ((WrPtr-UnpPtr)&MaxWinMask)<=BlockStart; @@ -472,7 +475,7 @@ bool Unpack::AddVMCode(uint FirstByte,byte *Code,uint CodeSize) uint VMCodeSize=RarVM::ReadData(VMCodeInp); if (VMCodeSize>=0x10000 || VMCodeSize==0 || VMCodeInp.InAddr+VMCodeSize>CodeSize) return false; - Array VMCode(VMCodeSize); + std::vector VMCode(VMCodeSize); for (uint I=0;I>8; VMCodeInp.faddbits(8); } - VM.Prepare(&VMCode[0],VMCodeSize,&Filter->Prg); + VM.Prepare(VMCode.data(),VMCodeSize,&Filter->Prg); } StackFilter->Prg.Type=Filter->Prg.Type; @@ -520,7 +523,7 @@ void Unpack::UnpWriteBuf30() { uint WrittenBorder=(uint)WrPtr; uint WriteSize=(uint)((UnpPtr-WrittenBorder)&MaxWinMask); - for (size_t I=0;IFilteredDataSize; delete PrgStack[I]; - PrgStack[I]=NULL; - while (I+1FilteredDataSize; I++; delete PrgStack[I]; - PrgStack[I]=NULL; + PrgStack[I]=nullptr; } UnpIO->UnpWrite(FilteredData,FilteredDataSize); UnpSomeRead=true; @@ -601,10 +604,10 @@ void Unpack::UnpWriteBuf30() { // Current filter intersects the window write border, so we adjust // the window border to process this filter next time, not now. - for (size_t J=I;JNextWindow) + if (flt!=nullptr && flt->NextWindow) flt->NextWindow=false; } WrPtr=WrittenBorder; @@ -752,14 +755,14 @@ void Unpack::InitFilters30(bool Solid) { if (!Solid) { - OldFilterLengths.SoftReset(); + OldFilterLengths.clear(); LastFilter=0; - for (size_t I=0;IUnpPtr); + PrevPtr=UnpPtr; if (Inp.InAddr>=ReadBorder) { @@ -42,7 +47,8 @@ void Unpack::Unpack5(bool Solid) break; } - if (((WriteBorder-UnpPtr) & MaxWinMask)<=MAX_INC_LZ_MATCH && WriteBorder!=UnpPtr) + // WriteBorder==UnpPtr means that we have MaxWinSize data ahead. + if (WrapDown(WriteBorder-UnpPtr)<=MAX_INC_LZ_MATCH && WriteBorder!=UnpPtr) { UnpWriteBuf(); if (WrittenFileSize>DestUnpSize) @@ -67,7 +73,8 @@ void Unpack::Unpack5(bool Solid) { uint Length=SlotToLength(Inp,MainSlot-262); - uint DBits,Distance=1,DistSlot=DecodeNumber(Inp,&BlockTables.DD); + size_t Distance=1; + uint DBits,DistSlot=DecodeNumber(Inp,&BlockTables.DD); if (DistSlot<4) { DBits=0; @@ -76,7 +83,7 @@ void Unpack::Unpack5(bool Solid) else { DBits=DistSlot/2 - 1; - Distance+=(2 | (DistSlot & 1)) << DBits; + Distance+=size_t(2 | (DistSlot & 1)) << DBits; } if (DBits>0) @@ -85,11 +92,23 @@ void Unpack::Unpack5(bool Solid) { if (DBits>4) { - Distance+=((Inp.getbits32()>>(36-DBits))<<4); + // It is also possible to always use getbits64() here. + if (DBits>36) + Distance+=( ( size_t(Inp.getbits64() ) >> (68-DBits) ) << 4 ); + else + Distance+=( ( size_t(Inp.getbits32() ) >> (36-DBits) ) << 4 ); Inp.addbits(DBits-4); } uint LowDist=DecodeNumber(Inp,&BlockTables.LDD); Distance+=LowDist; + + // Distance can be 0 for multiples of 4 GB as result of size_t + // overflow in 32-bit build. Its lower 32-bit can also erroneously + // fit into dictionary after truncating upper 32-bits. Replace such + // invalid distances with -1, so CopyString sets 0 data for them. + // DBits>=30 also as DistSlot>=62 indicate distances >=0x80000001. + if (sizeof(Distance)==4 && DBits>=30) + Distance=(size_t)-1; } else { @@ -112,7 +131,7 @@ void Unpack::Unpack5(bool Solid) InsertOldDist(Distance); LastLength=Length; if (Fragmented) - FragWindow.CopyString(Length,Distance,UnpPtr,MaxWinMask); + FragWindow.CopyString(Length,Distance,UnpPtr,FirstWinDone,MaxWinSize); else CopyString(Length,Distance); continue; @@ -128,7 +147,7 @@ void Unpack::Unpack5(bool Solid) { if (LastLength!=0) if (Fragmented) - FragWindow.CopyString(LastLength,OldDist[0],UnpPtr,MaxWinMask); + FragWindow.CopyString(LastLength,OldDist[0],UnpPtr,FirstWinDone,MaxWinSize); else CopyString(LastLength,OldDist[0]); continue; @@ -136,7 +155,7 @@ void Unpack::Unpack5(bool Solid) if (MainSlot<262) { uint DistNum=MainSlot-258; - uint Distance=OldDist[DistNum]; + size_t Distance=OldDist[DistNum]; for (uint I=DistNum;I>0;I--) OldDist[I]=OldDist[I-1]; OldDist[0]=Distance; @@ -145,7 +164,7 @@ void Unpack::Unpack5(bool Solid) uint Length=SlotToLength(Inp,LengthSlot); LastLength=Length; if (Fragmented) - FragWindow.CopyString(Length,Distance,UnpPtr,MaxWinMask); + FragWindow.CopyString(Length,Distance,UnpPtr,FirstWinDone,MaxWinSize); else CopyString(Length,Distance); continue; @@ -196,20 +215,25 @@ bool Unpack::ReadFilter(BitInput &Inp,UnpackFilter &Filter) bool Unpack::AddFilter(UnpackFilter &Filter) { - if (Filters.Size()>=MAX_UNPACK_FILTERS) + if (Filters.size()>=MAX_UNPACK_FILTERS) { UnpWriteBuf(); // Write data, apply and flush filters. - if (Filters.Size()>=MAX_UNPACK_FILTERS) + if (Filters.size()>=MAX_UNPACK_FILTERS) InitFilters(); // Still too many filters, prevent excessive memory use. } // If distance to filter start is that large that due to circular dictionary // mode now it points to old not written yet data, then we set 'NextWindow' // flag and process this filter only after processing that older data. - Filter.NextWindow=WrPtr!=UnpPtr && ((WrPtr-UnpPtr)&MaxWinMask)<=Filter.BlockStart; - - Filter.BlockStart=uint((Filter.BlockStart+UnpPtr)&MaxWinMask); - Filters.Push(Filter); + Filter.NextWindow=WrPtr!=UnpPtr && WrapDown(WrPtr-UnpPtr)<=Filter.BlockStart; + + // In malformed archive Filter.BlockStart can be many times larger + // than window size, so here we must use the reminder instead of + // subtracting the single window size as WrapUp can do. So the result + // is always within the window. Since we add and not subtract here, + // reminder always provides the valid result in valid archives. + Filter.BlockStart=(Filter.BlockStart+UnpPtr)%MaxWinSize; + Filters.push_back(Filter); return true; } @@ -255,10 +279,10 @@ bool Unpack::UnpReadBuf() void Unpack::UnpWriteBuf() { size_t WrittenBorder=WrPtr; - size_t FullWriteSize=(UnpPtr-WrittenBorder)&MaxWinMask; + size_t FullWriteSize=WrapDown(UnpPtr-WrittenBorder); size_t WriteSizeLeft=FullWriteSize; bool NotAllFiltersProcessed=false; - for (size_t I=0;IBlockStart-WrPtr)&MaxWinMask)<=FullWriteSize) + // to next block and no further wrap arounds is possible. + if (WrapDown(flt->BlockStart-WrPtr)<=FullWriteSize) flt->NextWindow=false; continue; } - uint BlockStart=flt->BlockStart; + size_t BlockStart=flt->BlockStart; uint BlockLength=flt->BlockLength; - if (((BlockStart-WrittenBorder)&MaxWinMask)0) // We set it to 0 also for invalid filters. { - uint BlockEnd=(BlockStart+BlockLength)&MaxWinMask; + size_t BlockEnd=WrapUp(BlockStart+BlockLength); - FilterSrcMemory.Alloc(BlockLength); - byte *Mem=&FilterSrcMemory[0]; + FilterSrcMemory.resize(BlockLength); + byte *Mem=FilterSrcMemory.data(); if (BlockStartType!=FILTER_NONE) @@ -365,7 +389,7 @@ void Unpack::UnpWriteBuf() // Remove processed filters from queue. size_t EmptyCount=0; - for (size_t I=0;I0) Filters[I-EmptyCount]=Filters[I]; @@ -373,7 +397,7 @@ void Unpack::UnpWriteBuf() EmptyCount++; } if (EmptyCount>0) - Filters.Alloc(Filters.Size()-EmptyCount); + Filters.resize(Filters.size()-EmptyCount); if (!NotAllFiltersProcessed) // Only if all filters are processed. { @@ -385,12 +409,12 @@ void Unpack::UnpWriteBuf() // We prefer to write data in blocks not exceeding UNPACK_MAX_WRITE // instead of potentially huge MaxWinSize blocks. It also allows us // to keep the size of Filters array reasonable. - WriteBorder=(UnpPtr+Min(MaxWinSize,UNPACK_MAX_WRITE))&MaxWinMask; + WriteBorder=WrapUp(UnpPtr+Min(MaxWinSize,UNPACK_MAX_WRITE)); // Choose the nearest among WriteBorder and WrPtr actual written border. // If border is equal to UnpPtr, it means that we have MaxWinSize data ahead. if (WriteBorder==UnpPtr || - WrPtr!=UnpPtr && ((WrPtr-UnpPtr)&MaxWinMask)<((WriteBorder-UnpPtr)&MaxWinMask)) + WrPtr!=UnpPtr && WrapDown(WrPtr-UnpPtr)Channels,SrcPos=0; - FilterDstMemory.Alloc(DataSize); - byte *DstData=&FilterDstMemory[0]; + FilterDstMemory.resize(DataSize); + byte *DstData=FilterDstMemory.data(); // Bytes from same channels are grouped to continual data blocks, // so we need to place them back to their interleaving positions. @@ -492,13 +516,13 @@ void Unpack::UnpWriteArea(size_t StartPtr,size_t EndPtr) if (Fragmented) { - size_t SizeToWrite=(EndPtr-StartPtr) & MaxWinMask; + size_t SizeToWrite=WrapDown(EndPtr-StartPtr); while (SizeToWrite>0) { size_t BlockSize=FragWindow.GetBlockSize(StartPtr,SizeToWrite); UnpWriteData(&FragWindow[StartPtr],BlockSize); SizeToWrite-=BlockSize; - StartPtr=(StartPtr+BlockSize) & MaxWinMask; + StartPtr=WrapUp(StartPtr+BlockSize); } } else @@ -541,7 +565,7 @@ bool Unpack::ReadBlockHeader(BitInput &Inp,UnpackBlockHeader &Header) return false; Inp.faddbits((8-Inp.InBit)&7); - byte BlockFlags=Inp.fgetbits()>>8; + byte BlockFlags=byte(Inp.fgetbits()>>8); Inp.faddbits(8); uint ByteCount=((BlockFlags>>3)&3)+1; // Block size byte count. @@ -564,14 +588,24 @@ bool Unpack::ReadBlockHeader(BitInput &Inp,UnpackBlockHeader &Header) Header.BlockSize=BlockSize; byte CheckSum=byte(0x5a^BlockFlags^BlockSize^(BlockSize>>8)^(BlockSize>>16)); + + // 2024.01.04: In theory the valid block can have Header.BlockSize == 0 + // and Header.TablePresent == false in case the only block purpose is to + // store Header.LastBlockInFile flag if it didn't fit into previous block. + // So we do not reject Header.BlockSize == 0. Though currently RAR doesn't + // seem to issue such zero length blocks. if (CheckSum!=SavedCheckSum) return false; Header.BlockStart=Inp.InAddr; + + // We called Inp.faddbits(8) above, thus Header.BlockStart can't be 0 here. + // So there is no overflow even if Header.BlockSize is 0. ReadBorder=Min(ReadBorder,Header.BlockStart+Header.BlockSize-1); Header.LastBlockInFile=(BlockFlags & 0x40)!=0; Header.TablePresent=(BlockFlags & 0x80)!=0; + return true; } @@ -610,8 +644,8 @@ bool Unpack::ReadTables(BitInput &Inp,UnpackBlockHeader &Header,UnpackBlockTable MakeDecodeTables(BitLength,&Tables.BD,BC); - byte Table[HUFF_TABLE_SIZE]; - const uint TableSize=HUFF_TABLE_SIZE; + byte Table[HUFF_TABLE_SIZEX]; + const uint TableSize=ExtraDist ? HUFF_TABLE_SIZEX:HUFF_TABLE_SIZEB; for (uint I=0;IReadTop-5) @@ -674,14 +708,15 @@ bool Unpack::ReadTables(BitInput &Inp,UnpackBlockHeader &Header,UnpackBlockTable if (!Inp.ExternalBuffer && Inp.InAddr>ReadTop) return false; MakeDecodeTables(&Table[0],&Tables.LD,NC); - MakeDecodeTables(&Table[NC],&Tables.DD,DC); - MakeDecodeTables(&Table[NC+DC],&Tables.LDD,LDC); - MakeDecodeTables(&Table[NC+DC+LDC],&Tables.RD,RC); + uint DCodes=ExtraDist ? DCX : DCB; + MakeDecodeTables(&Table[NC],&Tables.DD,DCodes); + MakeDecodeTables(&Table[NC+DCodes],&Tables.LDD,LDC); + MakeDecodeTables(&Table[NC+DCodes+LDC],&Tables.RD,RC); return true; } void Unpack::InitFilters() { - Filters.SoftReset(); + Filters.clear(); } diff --git a/unrar_sys/vendor/unrar/unpack50frag.cpp b/unrar_sys/vendor/unrar/unpack50frag.cpp index 3c008ff..9208405 100644 --- a/unrar_sys/vendor/unrar/unpack50frag.cpp +++ b/unrar_sys/vendor/unrar/unpack50frag.cpp @@ -2,6 +2,7 @@ FragmentedWindow::FragmentedWindow() { memset(Mem,0,sizeof(Mem)); memset(MemSize,0,sizeof(MemSize)); + LastAllocated=0; } @@ -13,6 +14,7 @@ FragmentedWindow::~FragmentedWindow() void FragmentedWindow::Reset() { + LastAllocated=0; for (uint I=0;IUnpPtr) + { + SrcPtr+=MaxWinSize; + + if (Distance>MaxWinSize || !FirstWinDone) + { + while (Length-- > 0) + { + (*this)[UnpPtr]=0; + if (++UnpPtr>=MaxWinSize) + UnpPtr-=MaxWinSize; + } + return; + } + } + while (Length-- > 0) { - (*this)[UnpPtr]=(*this)[SrcPtr++ & MaxWinMask]; - // We need to have masked UnpPtr after quit from loop, so it must not - // be replaced with '(*this)[UnpPtr++ & MaxWinMask]' - UnpPtr=(UnpPtr+1) & MaxWinMask; + (*this)[UnpPtr]=(*this)[SrcPtr]; + if (++SrcPtr>=MaxWinSize) + SrcPtr-=MaxWinSize; + if (++UnpPtr>=MaxWinSize) + UnpPtr-=MaxWinSize; } } diff --git a/unrar_sys/vendor/unrar/unpack50mt.cpp b/unrar_sys/vendor/unrar/unpack50mt.cpp index 82c9c4a..3d3fde5 100644 --- a/unrar_sys/vendor/unrar/unpack50mt.cpp +++ b/unrar_sys/vendor/unrar/unpack50mt.cpp @@ -1,3 +1,5 @@ +// 2023.09.09: 0x400000 and 2 are optimal for i9-12900K. +// Further increasing the buffer size reduced the extraction speed. #define UNP_READ_SIZE_MT 0x400000 #define UNP_BLOCKS_PER_THREAD 2 @@ -277,7 +279,7 @@ void Unpack::Unpack5MT(bool Solid) } } } - UnpPtr&=MaxWinMask; // ProcessDecoded and maybe others can leave UnpPtr > MaxWinMask here. + UnpPtr=WrapUp(UnpPtr); // ProcessDecoded and maybe others can leave UnpPtr >= MaxWinSize here. UnpWriteBuf(); BlockHeader=UnpThreadData[LastBlockNum].BlockHeader; @@ -362,7 +364,8 @@ void Unpack::UnpackDecode(UnpackThreadData &D) { uint Length=SlotToLength(D.Inp,MainSlot-262); - uint DBits,Distance=1,DistSlot=DecodeNumber(D.Inp,&D.BlockTables.DD); + size_t Distance=1; + uint DBits,DistSlot=DecodeNumber(D.Inp,&D.BlockTables.DD); if (DistSlot<4) { DBits=0; @@ -371,7 +374,7 @@ void Unpack::UnpackDecode(UnpackThreadData &D) else { DBits=DistSlot/2 - 1; - Distance+=(2 | (DistSlot & 1)) << DBits; + Distance+=size_t(2 | (DistSlot & 1)) << DBits; } if (DBits>0) @@ -380,11 +383,23 @@ void Unpack::UnpackDecode(UnpackThreadData &D) { if (DBits>4) { - Distance+=((D.Inp.getbits32()>>(36-DBits))<<4); + // It is also possible to always use getbits64() here. + if (DBits>36) + Distance+=( ( size_t(D.Inp.getbits64() ) >> (68-DBits) ) << 4 ); + else + Distance+=( ( size_t(D.Inp.getbits32() ) >> (36-DBits) ) << 4 ); D.Inp.addbits(DBits-4); } uint LowDist=DecodeNumber(D.Inp,&D.BlockTables.LDD); Distance+=LowDist; + + // Distance can be 0 for multiples of 4 GB as result of size_t + // overflow in 32-bit build. Its lower 32-bit can also erroneously + // fit into dictionary after truncating upper 32-bits. Replace such + // invalid distances with -1, so CopyString sets 0 data for them. + // DBits>=30 also as DistSlot>=62 indicate distances >=0x80000001. + if (sizeof(Distance)==4 && DBits>=30) + Distance=(size_t)-1; } else { @@ -450,8 +465,12 @@ bool Unpack::ProcessDecoded(UnpackThreadData &D) UnpackDecodedItem *Item=D.Decoded,*Border=D.Decoded+D.DecodedSize; while (ItemUnpPtr); + PrevPtr=UnpPtr; + + if (WrapDown(WriteBorder-UnpPtr)<=MAX_INC_LZ_MATCH && WriteBorder!=UnpPtr) { UnpWriteBuf(); if (WrittenFileSize>DestUnpSize) @@ -469,7 +488,7 @@ bool Unpack::ProcessDecoded(UnpackThreadData &D) else #endif for (uint I=0;I<=Item->Length;I++) - Window[UnpPtr++ & MaxWinMask]=Item->Literal[I]; + Window[WrapUp(UnpPtr++)]=Item->Literal[I]; } else if (Item->Type==UNPDT_MATCH) @@ -481,8 +500,8 @@ bool Unpack::ProcessDecoded(UnpackThreadData &D) else if (Item->Type==UNPDT_REP) { - uint Distance=OldDist[Item->Distance]; - for (uint I=Item->Distance;I>0;I--) + size_t Distance=OldDist[Item->Distance]; + for (size_t I=Item->Distance;I>0;I--) OldDist[I]=OldDist[I-1]; OldDist[0]=Distance; LastLength=Item->Length; @@ -505,7 +524,7 @@ bool Unpack::ProcessDecoded(UnpackThreadData &D) Item++; Filter.Channels=(byte)Item->Length; - Filter.BlockLength=Item->Distance; + Filter.BlockLength=(uint)Item->Distance; AddFilter(Filter); } @@ -543,7 +562,11 @@ bool Unpack::UnpackLargeBlock(UnpackThreadData &D) while (true) { - UnpPtr&=MaxWinMask; + UnpPtr=WrapUp(UnpPtr); + + FirstWinDone|=(PrevPtr>UnpPtr); + PrevPtr=UnpPtr; + if (D.Inp.InAddr>=ReadBorder) { if (D.Inp.InAddr>BlockBorder || D.Inp.InAddr==BlockBorder && @@ -559,7 +582,7 @@ bool Unpack::UnpackLargeBlock(UnpackThreadData &D) break; } } - if (((WriteBorder-UnpPtr) & MaxWinMask)<=MAX_INC_LZ_MATCH && WriteBorder!=UnpPtr) + if (WrapDown(WriteBorder-UnpPtr)<=MAX_INC_LZ_MATCH && WriteBorder!=UnpPtr) { UnpWriteBuf(); if (WrittenFileSize>DestUnpSize) @@ -576,7 +599,8 @@ bool Unpack::UnpackLargeBlock(UnpackThreadData &D) { uint Length=SlotToLength(D.Inp,MainSlot-262); - uint DBits,Distance=1,DistSlot=DecodeNumber(D.Inp,&D.BlockTables.DD); + size_t Distance=1; + uint DBits,DistSlot=DecodeNumber(D.Inp,&D.BlockTables.DD); if (DistSlot<4) { DBits=0; @@ -585,7 +609,7 @@ bool Unpack::UnpackLargeBlock(UnpackThreadData &D) else { DBits=DistSlot/2 - 1; - Distance+=(2 | (DistSlot & 1)) << DBits; + Distance+=size_t(2 | (DistSlot & 1)) << DBits; } if (DBits>0) @@ -594,11 +618,23 @@ bool Unpack::UnpackLargeBlock(UnpackThreadData &D) { if (DBits>4) { - Distance+=((D.Inp.getbits32()>>(36-DBits))<<4); + // It is also possible to always use getbits64() here. + if (DBits>36) + Distance+=( ( size_t(D.Inp.getbits64() ) >> (68-DBits) ) << 4 ); + else + Distance+=( ( size_t(D.Inp.getbits32() ) >> (36-DBits) ) << 4 ); D.Inp.addbits(DBits-4); } uint LowDist=DecodeNumber(D.Inp,&D.BlockTables.LDD); Distance+=LowDist; + + // Distance can be 0 for multiples of 4 GB as result of size_t + // overflow in 32-bit build. Its lower 32-bit can also erroneously + // fit into dictionary after truncating upper 32-bits. Replace such + // invalid distances with -1, so CopyString sets 0 data for them. + // DBits>=30 also as DistSlot>=62 indicate distances >=0x80000001. + if (sizeof(Distance)==4 && DBits>=30) + Distance=(size_t)-1; } else { @@ -639,7 +675,7 @@ bool Unpack::UnpackLargeBlock(UnpackThreadData &D) if (MainSlot<262) { uint DistNum=MainSlot-258; - uint Distance=OldDist[DistNum]; + size_t Distance=OldDist[DistNum]; for (uint I=DistNum;I>0;I--) OldDist[I]=OldDist[I-1]; OldDist[0]=Distance; diff --git a/unrar_sys/vendor/unrar/unpackinline.cpp b/unrar_sys/vendor/unrar/unpackinline.cpp index 04c3d1f..68aec91 100644 --- a/unrar_sys/vendor/unrar/unpackinline.cpp +++ b/unrar_sys/vendor/unrar/unpackinline.cpp @@ -1,4 +1,4 @@ -_forceinline void Unpack::InsertOldDist(uint Distance) +_forceinline void Unpack::InsertOldDist(size_t Distance) { OldDist[3]=OldDist[2]; OldDist[2]=OldDist[1]; @@ -6,23 +6,58 @@ _forceinline void Unpack::InsertOldDist(uint Distance) OldDist[0]=Distance; } -#ifdef _MSC_VER -#define FAST_MEMCPY +#if defined(LITTLE_ENDIAN) && defined(ALLOW_MISALIGNED) +#define UNPACK_COPY8 // We can copy 8 bytes at any position as uint64. #endif -_forceinline void Unpack::CopyString(uint Length,uint Distance) +_forceinline void Unpack::CopyString(uint Length,size_t Distance) { size_t SrcPtr=UnpPtr-Distance; + + // Perform the correction here instead of "else", so matches crossing + // the window beginning can also be processed by first "if" part. + if (Distance>UnpPtr) // Unlike SrcPtr>=MaxWinSize, it catches invalid distances like 0xfffffff0 in 32-bit build. + { + // Same as WrapDown(SrcPtr), needed because of UnpPtr-Distance above. + // We need the same condition below, so we expanded WrapDown() here. + SrcPtr+=MaxWinSize; + + // About Distance>MaxWinSize check. + // SrcPtr can be >=MaxWinSize if distance exceeds MaxWinSize + // in a malformed archive. Our WrapDown replacement above might not + // correct it, so to prevent out of bound Window read we check it here. + // Unlike SrcPtr>=MaxWinSize check, it allows MaxWinSize>0x80000000 + // in 32-bit build, which could cause overflow in SrcPtr. + // About !FirstWinDone check. + // If first window hasn't filled yet and it points outside of window, + // set data to 0 instead of copying preceding file data, so result doesn't + // depend on previously extracted files in non-solid archive. + if (Distance>MaxWinSize || !FirstWinDone) + { + // Fill area of specified length with 0 instead of returning. + // So if only the distance is broken and rest of packed data is valid, + // it preserves offsets and allows to continue extraction. + // If we set SrcPtr to random offset instead, let's say, 0, + // we still will be copying preceding file data if UnpPtr is also 0. + while (Length-- > 0) + { + Window[UnpPtr]=0; + UnpPtr=WrapUp(UnpPtr+1); + } + return; + } + } + if (SrcPtr=8) @@ -40,7 +75,7 @@ _forceinline void Unpack::CopyString(uint Length,uint Distance) Dest+=8; Length-=8; } -#ifdef FAST_MEMCPY +#ifdef UNPACK_COPY8 else while (Length>=8) { @@ -49,9 +84,7 @@ _forceinline void Unpack::CopyString(uint Length,uint Distance) // But for real RAR archives Distance <= MaxWinSize - MAX_INC_LZ_MATCH // always, so overlap here is impossible. - // This memcpy expanded inline by MSVC. We could also use uint64 - // assignment, which seems to provide about the same speed. - memcpy(Dest,Src,8); + RawPut8(RawGet8(Src),Dest); Src+=8; Dest+=8; @@ -71,10 +104,10 @@ _forceinline void Unpack::CopyString(uint Length,uint Distance) else while (Length-- > 0) // Slow copying with all possible precautions. { - Window[UnpPtr]=Window[SrcPtr++ & MaxWinMask]; + Window[UnpPtr]=Window[WrapUp(SrcPtr++)]; // We need to have masked UnpPtr after quit from loop, so it must not - // be replaced with 'Window[UnpPtr++ & MaxWinMask]' - UnpPtr=(UnpPtr+1) & MaxWinMask; + // be replaced with 'Window[WrapUp(UnpPtr++)]' + UnpPtr=WrapUp(UnpPtr+1); } } diff --git a/unrar_sys/vendor/unrar/uowners.cpp b/unrar_sys/vendor/unrar/uowners.cpp index 5eb1279..cbe2f40 100644 --- a/unrar_sys/vendor/unrar/uowners.cpp +++ b/unrar_sys/vendor/unrar/uowners.cpp @@ -2,18 +2,16 @@ void ExtractUnixOwner30(Archive &Arc,const wchar *FileName) { - char NameA[NM]; - WideToChar(FileName,NameA,ASIZE(NameA)); - - if (memchr(&Arc.SubHead.SubData[0],0,Arc.SubHead.SubData.Size())==NULL) + // There must be 0 byte between owner and group strings. + // Otherwise strlen call below wouldn't be safe. + if (memchr(Arc.SubHead.SubData.data(),0,Arc.SubHead.SubData.size())==NULL) return; - char *OwnerName=(char *)&Arc.SubHead.SubData[0]; + char *OwnerName=(char *)Arc.SubHead.SubData.data(); int OwnerSize=strlen(OwnerName)+1; - int GroupSize=Arc.SubHead.SubData.Size()-OwnerSize; - char GroupName[NM]; - strncpy(GroupName,(char *)&Arc.SubHead.SubData[OwnerSize],GroupSize); - GroupName[GroupSize]=0; + int GroupSize=Arc.SubHead.SubData.size()-OwnerSize; + char *GroupName=(char *)&Arc.SubHead.SubData[OwnerSize]; + std::string GroupStr(GroupName,GroupName+GroupSize); struct passwd *pw; if ((pw=getpwnam(OwnerName))==NULL) @@ -25,7 +23,7 @@ void ExtractUnixOwner30(Archive &Arc,const wchar *FileName) uid_t OwnerID=pw->pw_uid; struct group *gr; - if ((gr=getgrnam(GroupName))==NULL) + if ((gr=getgrnam(GroupStr.c_str()))==NULL) { uiMsg(UIERROR_UOWNERGETGROUPID,Arc.FileName,GetWide(GroupName)); ErrHandler.SetErrorCode(RARX_WARNING); @@ -33,10 +31,14 @@ void ExtractUnixOwner30(Archive &Arc,const wchar *FileName) } uint Attr=GetFileAttr(FileName); gid_t GroupID=gr->gr_gid; + + std::string NameA; + WideToChar(FileName,NameA); + #if defined(SAVE_LINKS) && !defined(_APPLE) - if (lchown(NameA,OwnerID,GroupID)!=0) + if (lchown(NameA.c_str(),OwnerID,GroupID)!=0) #else - if (chown(NameA,OwnerID,GroupID)!=0) + if (chown(NameA.c_str(),OwnerID,GroupID)!=0) #endif { uiMsg(UIERROR_UOWNERSET,Arc.FileName,FileName); @@ -46,11 +48,8 @@ void ExtractUnixOwner30(Archive &Arc,const wchar *FileName) } -void SetUnixOwner(Archive &Arc,const wchar *FileName) +void SetUnixOwner(Archive &Arc,const std::wstring &FileName) { - char NameA[NM]; - WideToChar(FileName,NameA,ASIZE(NameA)); - // First, we try to resolve symbolic names. If they are missing or cannot // be resolved, we try to use numeric values if any. If numeric values // are missing too, function fails. @@ -85,10 +84,14 @@ void SetUnixOwner(Archive &Arc,const wchar *FileName) else hd.UnixGroupID=gr->gr_gid; } + + std::string NameA; + WideToChar(FileName,NameA); + #if defined(SAVE_LINKS) && !defined(_APPLE) - if (lchown(NameA,hd.UnixOwnerID,hd.UnixGroupID)!=0) + if (lchown(NameA.c_str(),hd.UnixOwnerID,hd.UnixGroupID)!=0) #else - if (chown(NameA,hd.UnixOwnerID,hd.UnixGroupID)!=0) + if (chown(NameA.c_str(),hd.UnixOwnerID,hd.UnixGroupID)!=0) #endif { uiMsg(UIERROR_UOWNERSET,Arc.FileName,FileName); diff --git a/unrar_sys/vendor/unrar/version.hpp b/unrar_sys/vendor/unrar/version.hpp index c9fffca..aca1bcc 100644 --- a/unrar_sys/vendor/unrar/version.hpp +++ b/unrar_sys/vendor/unrar/version.hpp @@ -1,6 +1,6 @@ -#define RARVER_MAJOR 6 -#define RARVER_MINOR 24 +#define RARVER_MAJOR 7 +#define RARVER_MINOR 0 #define RARVER_BETA 0 -#define RARVER_DAY 3 -#define RARVER_MONTH 10 -#define RARVER_YEAR 2023 +#define RARVER_DAY 26 +#define RARVER_MONTH 2 +#define RARVER_YEAR 2024 diff --git a/unrar_sys/vendor/unrar/volume.cpp b/unrar_sys/vendor/unrar/volume.cpp index 4924d8d..e171515 100644 --- a/unrar_sys/vendor/unrar/volume.cpp +++ b/unrar_sys/vendor/unrar/volume.cpp @@ -1,8 +1,8 @@ #include "rar.hpp" #ifdef RARDLL -static bool DllVolChange(CommandData *Cmd,wchar *NextName,size_t NameSize); -static bool DllVolNotify(CommandData *Cmd,wchar *NextName); +bool DllVolChange(CommandData *Cmd,std::wstring &NextName); +static bool DllVolNotify(CommandData *Cmd,const std::wstring &NextName); #endif @@ -35,9 +35,8 @@ bool MergeArchive(Archive &Arc,ComprDataIO *DataIO,bool ShowFileName,wchar Comma Arc.Close(); - wchar NextName[NM]; - wcsncpyz(NextName,Arc.FileName,ASIZE(NextName)); - NextVolumeName(NextName,ASIZE(NextName),!Arc.NewNumbering); + std::wstring NextName=Arc.FileName; + NextVolumeName(NextName,!Arc.NewNumbering); #if !defined(SFX_MODULE) && !defined(RARDLL) bool RecoveryDone=false; @@ -56,7 +55,7 @@ bool MergeArchive(Archive &Arc,ComprDataIO *DataIO,bool ShowFileName,wchar Comma // the next file might not be fully decoded yet. They write chunks of data // and then close the file again until the next chunk comes in. - if (Cmd->VolumePause && !uiAskNextVolume(NextName,ASIZE(NextName))) + if (Cmd->VolumePause && !uiAskNextVolume(NextName)) FailedOpen=true; #endif @@ -76,18 +75,17 @@ bool MergeArchive(Archive &Arc,ComprDataIO *DataIO,bool ShowFileName,wchar Comma { // Checking for new style volumes renamed by user to old style // name format. Some users did it for unknown reason. - wchar AltNextName[NM]; - wcsncpyz(AltNextName,Arc.FileName,ASIZE(AltNextName)); - NextVolumeName(AltNextName,ASIZE(AltNextName),true); + std::wstring AltNextName=Arc.FileName; + NextVolumeName(AltNextName,true); OldSchemeTested=true; if (Arc.Open(AltNextName,OpenMode)) { - wcsncpyz(NextName,AltNextName,ASIZE(NextName)); + NextName=AltNextName; break; } } #ifdef RARDLL - if (!DllVolChange(Cmd,NextName,ASIZE(NextName))) + if (!DllVolChange(Cmd,NextName)) { FailedOpen=true; break; @@ -109,7 +107,7 @@ bool MergeArchive(Archive &Arc,ComprDataIO *DataIO,bool ShowFileName,wchar Comma break; } #ifndef SILENT - if (Cmd->AllYes || !uiAskNextVolume(NextName,ASIZE(NextName))) + if (Cmd->AllYes || !uiAskNextVolume(NextName)) #endif { FailedOpen=true; @@ -128,7 +126,7 @@ bool MergeArchive(Archive &Arc,ComprDataIO *DataIO,bool ShowFileName,wchar Comma } if (Command=='T' || Command=='X' || Command=='E') - mprintf(St(Command=='T' ? MTestVol:MExtrVol),Arc.FileName); + mprintf(St(Command=='T' ? MTestVol:MExtrVol),Arc.FileName.c_str()); Arc.CheckArc(true); @@ -158,7 +156,7 @@ bool MergeArchive(Archive &Arc,ComprDataIO *DataIO,bool ShowFileName,wchar Comma } if (ShowFileName && !Cmd->DisableNames) { - mprintf(St(MExtrPoints),Arc.FileHead.FileName); + mprintf(St(MExtrPoints),Arc.FileHead.FileName.c_str()); if (!Cmd->DisablePercentage) mprintf(L" "); } @@ -190,45 +188,65 @@ bool MergeArchive(Archive &Arc,ComprDataIO *DataIO,bool ShowFileName,wchar Comma #ifdef RARDLL -bool DllVolChange(CommandData *Cmd,wchar *NextName,size_t NameSize) +bool DllVolChange(CommandData *Cmd,std::wstring &NextName) { bool DllVolChanged=false,DllVolAborted=false; if (Cmd->Callback!=NULL) { - wchar OrgNextName[NM]; - wcsncpyz(OrgNextName,NextName,ASIZE(OrgNextName)); - if (Cmd->Callback(UCM_CHANGEVOLUMEW,Cmd->UserData,(LPARAM)NextName,RAR_VOL_ASK)==-1) + std::wstring OrgNextName=NextName; + + std::vector NameBuf(MAXPATHSIZE); + std::copy(NextName.data(), NextName.data() + NextName.size() + 1, NameBuf.begin()); + + if (Cmd->Callback(UCM_CHANGEVOLUMEW,Cmd->UserData,(LPARAM)NameBuf.data(),RAR_VOL_ASK)==-1) DllVolAborted=true; else - if (wcscmp(OrgNextName,NextName)!=0) + { + NextName=NameBuf.data(); + if (OrgNextName!=NextName) DllVolChanged=true; else { - char NextNameA[NM],OrgNextNameA[NM]; - WideToChar(NextName,NextNameA,ASIZE(NextNameA)); - strncpyz(OrgNextNameA,NextNameA,ASIZE(OrgNextNameA)); - if (Cmd->Callback(UCM_CHANGEVOLUME,Cmd->UserData,(LPARAM)NextNameA,RAR_VOL_ASK)==-1) + std::string NextNameA; + WideToChar(NextName,NextNameA); + std::string OrgNextNameA=NextNameA; + + std::vector NameBufA(MAXPATHSIZE); + std::copy(NextNameA.data(), NextNameA.data() + NextNameA.size() + 1, NameBufA.begin()); + + if (Cmd->Callback(UCM_CHANGEVOLUME,Cmd->UserData,(LPARAM)NameBufA.data(),RAR_VOL_ASK)==-1) DllVolAborted=true; else - if (strcmp(OrgNextNameA,NextNameA)!=0) + { + NextNameA=NameBufA.data(); + if (OrgNextNameA!=NextNameA) { // We can damage some Unicode characters by U->A->U conversion, // so set Unicode name only if we see that ANSI name is changed. - CharToWide(NextNameA,NextName,NameSize); + CharToWide(NextNameA,NextName); DllVolChanged=true; } + } } + } } if (!DllVolChanged && Cmd->ChangeVolProc!=NULL) { - char NextNameA[NM]; - WideToChar(NextName,NextNameA,ASIZE(NextNameA)); - int RetCode=Cmd->ChangeVolProc(NextNameA,RAR_VOL_ASK); + std::string NextNameA; + WideToChar(NextName,NextNameA); + + std::vector NameBufA(MAXPATHSIZE); + std::copy(NextNameA.data(), NextNameA.data() + NextNameA.size() + 1, NameBufA.begin()); + + int RetCode=Cmd->ChangeVolProc(NameBufA.data(),RAR_VOL_ASK); if (RetCode==0) DllVolAborted=true; else - CharToWide(NextNameA,NextName,NameSize); + { + NextNameA=NameBufA.data(); + CharToWide(NextNameA,NextName); + } } // We quit only on 'abort' condition, but not on 'name not changed'. @@ -246,20 +264,21 @@ bool DllVolChange(CommandData *Cmd,wchar *NextName,size_t NameSize) #ifdef RARDLL -bool DllVolNotify(CommandData *Cmd,wchar *NextName) +static bool DllVolNotify(CommandData *Cmd,const std::wstring &NextName) { - char NextNameA[NM]; - WideToChar(NextName,NextNameA,ASIZE(NextNameA)); + std::string NextNameA; + WideToChar(NextName,NextNameA); + if (Cmd->Callback!=NULL) { - if (Cmd->Callback(UCM_CHANGEVOLUMEW,Cmd->UserData,(LPARAM)NextName,RAR_VOL_NOTIFY)==-1) + if (Cmd->Callback(UCM_CHANGEVOLUMEW,Cmd->UserData,(LPARAM)NextName.data(),RAR_VOL_NOTIFY)==-1) return false; - if (Cmd->Callback(UCM_CHANGEVOLUME,Cmd->UserData,(LPARAM)NextNameA,RAR_VOL_NOTIFY)==-1) + if (Cmd->Callback(UCM_CHANGEVOLUME,Cmd->UserData,(LPARAM)NextNameA.data(),RAR_VOL_NOTIFY)==-1) return false; } if (Cmd->ChangeVolProc!=NULL) { - int RetCode=Cmd->ChangeVolProc(NextNameA,RAR_VOL_NOTIFY); + int RetCode=Cmd->ChangeVolProc((char *)NextNameA.data(),RAR_VOL_NOTIFY); if (RetCode==0) return false; } diff --git a/unrar_sys/vendor/unrar/win32acl.cpp b/unrar_sys/vendor/unrar/win32acl.cpp index d4797bd..36cb852 100644 --- a/unrar_sys/vendor/unrar/win32acl.cpp +++ b/unrar_sys/vendor/unrar/win32acl.cpp @@ -5,7 +5,7 @@ static bool ReadSacl=false; #ifndef SFX_MODULE -void ExtractACL20(Archive &Arc,const wchar *FileName) +void ExtractACL20(Archive &Arc,const std::wstring &FileName) { SetACLPrivileges(); @@ -27,7 +27,7 @@ void ExtractACL20(Archive &Arc,const wchar *FileName) Unpack Unpack(&DataIO); Unpack.Init(0x10000,false); - Array UnpData(Arc.EAHead.UnpSize); + std::vector UnpData(Arc.EAHead.UnpSize); DataIO.SetUnpackToMemory(&UnpData[0],Arc.EAHead.UnpSize); DataIO.SetPackedSizeToRead(Arc.EAHead.DataSize); DataIO.EnableShowProgress(false); @@ -49,7 +49,7 @@ void ExtractACL20(Archive &Arc,const wchar *FileName) si|=SACL_SECURITY_INFORMATION; SECURITY_DESCRIPTOR *sd=(SECURITY_DESCRIPTOR *)&UnpData[0]; - int SetCode=SetFileSecurity(FileName,si,sd); + int SetCode=SetFileSecurity(FileName.c_str(),si,sd); if (!SetCode) { @@ -64,9 +64,9 @@ void ExtractACL20(Archive &Arc,const wchar *FileName) #endif -void ExtractACL(Archive &Arc,const wchar *FileName) +void ExtractACL(Archive &Arc,const std::wstring &FileName) { - Array SubData; + std::vector SubData; if (!Arc.ReadSubData(&SubData,NULL,false)) return; @@ -78,12 +78,12 @@ void ExtractACL(Archive &Arc,const wchar *FileName) si|=SACL_SECURITY_INFORMATION; SECURITY_DESCRIPTOR *sd=(SECURITY_DESCRIPTOR *)&SubData[0]; - int SetCode=SetFileSecurity(FileName,si,sd); + int SetCode=SetFileSecurity(FileName.c_str(),si,sd); if (!SetCode) { - wchar LongName[NM]; - if (GetWinLongPath(FileName,LongName,ASIZE(LongName))) - SetCode=SetFileSecurity(LongName,si,sd); + std::wstring LongName; + if (GetWinLongPath(FileName,LongName)) + SetCode=SetFileSecurity(LongName.c_str(),si,sd); } if (!SetCode) diff --git a/unrar_sys/vendor/unrar/win32lnk.cpp b/unrar_sys/vendor/unrar/win32lnk.cpp index 759c490..677c273 100644 --- a/unrar_sys/vendor/unrar/win32lnk.cpp +++ b/unrar_sys/vendor/unrar/win32lnk.cpp @@ -40,26 +40,24 @@ bool CreateReparsePoint(CommandData *Cmd,const wchar *Name,FileHeader *hd) PrivSet=true; } - const DWORD BufSize=sizeof(REPARSE_DATA_BUFFER)+2*NM*sizeof(wchar)+1024; - Array Buf(BufSize); - REPARSE_DATA_BUFFER *rdb=(REPARSE_DATA_BUFFER *)&Buf[0]; - - wchar SubstName[NM]; - wcsncpyz(SubstName,hd->RedirName,ASIZE(SubstName)); - size_t SubstLength=wcslen(SubstName); - - wchar PrintName[NM],*PrintNameSrc=SubstName,*PrintNameDst=PrintName; - bool WinPrefix=wcsncmp(PrintNameSrc,L"\\??\\",4)==0; - if (WinPrefix) - PrintNameSrc+=4; - if (WinPrefix && wcsncmp(PrintNameSrc,L"UNC\\",4)==0) - { - *(PrintNameDst++)='\\'; // Insert second \ in beginning of share name. - PrintNameSrc+=3; - } - wcscpy(PrintNameDst,PrintNameSrc); + const std::wstring &SubstName=hd->RedirName; + size_t SubstLength=SubstName.size(); + + // REPARSE_DATA_BUFFER receives both SubstName and PrintName strings, + // thus "*2" below. PrintName is either shorter or same length as SubstName. + const DWORD BufSize=sizeof(REPARSE_DATA_BUFFER)+((DWORD)SubstLength+1)*2*sizeof(wchar); + + std::vector Buf(BufSize); + REPARSE_DATA_BUFFER *rdb=(REPARSE_DATA_BUFFER *)Buf.data(); - size_t PrintLength=wcslen(PrintName); + // Remove \??\ NTFS junction prefix of present. + bool WinPrefix=SubstName.rfind(L"\\??\\",0)!=std::wstring::npos; + std::wstring PrintName=WinPrefix ? SubstName.substr(4):SubstName; + + if (WinPrefix && PrintName.rfind(L"UNC\\",0)!=std::wstring::npos) + PrintName=L"\\"+PrintName.substr(3); // Convert UNC\server\share to \\server\share. + + size_t PrintLength=PrintName.size(); bool AbsPath=WinPrefix; // IsFullPath is not really needed here, AbsPath check is enough. @@ -69,7 +67,11 @@ bool CreateReparsePoint(CommandData *Cmd,const wchar *Name,FileHeader *hd) // path as a prefix, which can confuse IsRelativeSymlinkSafe algorithm. if (!Cmd->AbsoluteLinks && (AbsPath || IsFullPath(hd->RedirName) || !IsRelativeSymlinkSafe(Cmd,hd->FileName,Name,hd->RedirName))) + { + uiMsg(UIERROR_SKIPUNSAFELINK,hd->FileName,hd->RedirName); + ErrHandler.SetErrorCode(RARX_WARNING); return false; + } CreatePath(Name,true,Cmd->DisableNames); @@ -86,9 +88,9 @@ bool CreateReparsePoint(CommandData *Cmd,const wchar *Name,FileHeader *hd) // Unix symlinks do not have their own 'directory' attribute. if (hd->Dir || hd->DirTarget) { - if (!CreateDirectory(Name,NULL)) + if (!CreateDir(Name)) { - uiMsg(UIERROR_DIRCREATE,UINULL,Name); + uiMsg(UIERROR_DIRCREATE,L"",Name); ErrHandler.SetErrorCode(RARX_CREATE); return false; } @@ -118,11 +120,11 @@ bool CreateReparsePoint(CommandData *Cmd,const wchar *Name,FileHeader *hd) rdb->MountPointReparseBuffer.SubstituteNameOffset=0; rdb->MountPointReparseBuffer.SubstituteNameLength=USHORT(SubstLength*sizeof(WCHAR)); - wcscpy(rdb->MountPointReparseBuffer.PathBuffer,SubstName); + wcscpy(rdb->MountPointReparseBuffer.PathBuffer,SubstName.data()); rdb->MountPointReparseBuffer.PrintNameOffset=USHORT((SubstLength+1)*sizeof(WCHAR)); rdb->MountPointReparseBuffer.PrintNameLength=USHORT(PrintLength*sizeof(WCHAR)); - wcscpy(rdb->MountPointReparseBuffer.PathBuffer+SubstLength+1,PrintName); + wcscpy(rdb->MountPointReparseBuffer.PathBuffer+SubstLength+1,PrintName.data()); } else if (hd->RedirType==FSREDIR_WINSYMLINK || hd->RedirType==FSREDIR_UNIXSYMLINK) @@ -139,11 +141,11 @@ bool CreateReparsePoint(CommandData *Cmd,const wchar *Name,FileHeader *hd) rdb->SymbolicLinkReparseBuffer.SubstituteNameOffset=0; rdb->SymbolicLinkReparseBuffer.SubstituteNameLength=USHORT(SubstLength*sizeof(WCHAR)); - wcscpy(rdb->SymbolicLinkReparseBuffer.PathBuffer,SubstName); + wcscpy(rdb->SymbolicLinkReparseBuffer.PathBuffer,SubstName.data()); rdb->SymbolicLinkReparseBuffer.PrintNameOffset=USHORT((SubstLength+1)*sizeof(WCHAR)); rdb->SymbolicLinkReparseBuffer.PrintNameLength=USHORT(PrintLength*sizeof(WCHAR)); - wcscpy(rdb->SymbolicLinkReparseBuffer.PathBuffer+SubstLength+1,PrintName); + wcscpy(rdb->SymbolicLinkReparseBuffer.PathBuffer+SubstLength+1,PrintName.data()); rdb->SymbolicLinkReparseBuffer.Flags=AbsPath ? 0:SYMLINK_FLAG_RELATIVE; } @@ -166,7 +168,7 @@ bool CreateReparsePoint(CommandData *Cmd,const wchar *Name,FileHeader *hd) rdb->ReparseDataLength,NULL,0,&Returned,NULL)) { CloseHandle(hFile); - uiMsg(UIERROR_SLINKCREATE,UINULL,Name); + uiMsg(UIERROR_SLINKCREATE,L"",Name); DWORD LastError=GetLastError(); if ((LastError==ERROR_ACCESS_DENIED || LastError==ERROR_PRIVILEGE_NOT_HELD) && diff --git a/unrar_sys/vendor/unrar/win32stm.cpp b/unrar_sys/vendor/unrar/win32stm.cpp index 3b77d2a..0c9c72a 100644 --- a/unrar_sys/vendor/unrar/win32stm.cpp +++ b/unrar_sys/vendor/unrar/win32stm.cpp @@ -2,7 +2,7 @@ #ifdef _WIN_ALL // StreamName must include the leading ':'. -static bool IsNtfsReservedStream(const wchar *StreamName) +static bool IsNtfsReservedStream(const std::wstring &StreamName) { const wchar *Reserved[]{ L"::$ATTRIBUTE_LIST",L"::$BITMAP",L"::$DATA",L"::$EA",L"::$EA_INFORMATION", @@ -19,7 +19,7 @@ static bool IsNtfsReservedStream(const wchar *StreamName) #if !defined(SFX_MODULE) && defined(_WIN_ALL) -void ExtractStreams20(Archive &Arc,const wchar *FileName) +void ExtractStreams20(Archive &Arc,const std::wstring &FileName) { if (Arc.BrokenHeader) { @@ -35,39 +35,39 @@ void ExtractStreams20(Archive &Arc,const wchar *FileName) return; } - wchar StreamName[NM+2]; - if (FileName[0]!=0 && FileName[1]==0) + std::wstring StreamName; + if (FileName.size()==1) { // Convert single character names like f:stream to .\f:stream to // resolve the ambiguity with drive letters. - wcsncpyz(StreamName,L".\\",ASIZE(StreamName)); - wcsncatz(StreamName,FileName,ASIZE(StreamName)); + StreamName=L".\\"+FileName; } else - wcsncpyz(StreamName,FileName,ASIZE(StreamName)); - if (wcslen(StreamName)+strlen(Arc.StreamHead.StreamName)>=ASIZE(StreamName) || - Arc.StreamHead.StreamName[0]!=':') + StreamName=FileName; + if (Arc.StreamHead.StreamName[0]!=':') { uiMsg(UIERROR_STREAMBROKEN,Arc.FileName,FileName); ErrHandler.SetErrorCode(RARX_CRC); return; } - wchar StoredName[NM]; - CharToWide(Arc.StreamHead.StreamName,StoredName,ASIZE(StoredName)); - ConvertPath(StoredName+1,StoredName+1,ASIZE(StoredName)-1); + std::wstring StoredName; + // "substr(1)" to exclude ':', so we can use ConvertPath() below. + CharToWide(Arc.StreamHead.StreamName.substr(1),StoredName); + ConvertPath(&StoredName,&StoredName); + StoredName=L":"+StoredName; if (IsNtfsReservedStream(StoredName)) return; - wcsncatz(StreamName,StoredName,ASIZE(StreamName)); + StreamName+=StoredName; - FindData fd; - bool Found=FindFile::FastFind(FileName,&fd); + FindData FD; + bool Found=FindFile::FastFind(FileName,&FD); - if ((fd.FileAttr & FILE_ATTRIBUTE_READONLY)!=0) - SetFileAttr(FileName,fd.FileAttr & ~FILE_ATTRIBUTE_READONLY); + if ((FD.FileAttr & FILE_ATTRIBUTE_READONLY)!=0) + SetFileAttr(FileName,FD.FileAttr & ~FILE_ATTRIBUTE_READONLY); File CurFile; if (CurFile.WCreate(StreamName)) @@ -93,31 +93,29 @@ void ExtractStreams20(Archive &Arc,const wchar *FileName) } File HostFile; if (Found && HostFile.Open(FileName,FMF_OPENSHARED|FMF_UPDATE)) - SetFileTime(HostFile.GetHandle(),&fd.ftCreationTime,&fd.ftLastAccessTime, - &fd.ftLastWriteTime); - if ((fd.FileAttr & FILE_ATTRIBUTE_READONLY)!=0) - SetFileAttr(FileName,fd.FileAttr); + SetFileTime(HostFile.GetHandle(),&FD.ftCreationTime,&FD.ftLastAccessTime, + &FD.ftLastWriteTime); + if ((FD.FileAttr & FILE_ATTRIBUTE_READONLY)!=0) + SetFileAttr(FileName,FD.FileAttr); } #endif #ifdef _WIN_ALL -void ExtractStreams(Archive &Arc,const wchar *FileName,bool TestMode) +void ExtractStreams(Archive &Arc,const std::wstring &FileName,bool TestMode) { - wchar FullName[NM+2]; + std::wstring FullName; if (FileName[0]!=0 && FileName[1]==0) { // Convert single character names like f:stream to .\f:stream to // resolve the ambiguity with drive letters. - wcsncpyz(FullName,L".\\",ASIZE(FullName)); - wcsncatz(FullName,FileName,ASIZE(FullName)); + FullName=L".\\"+FileName; } else - wcsncpyz(FullName,FileName,ASIZE(FullName)); + FullName=FileName; - wchar StreamName[NM]; - GetStreamNameNTFS(Arc,StreamName,ASIZE(StreamName)); - if (*StreamName!=':') + std::wstring StreamName=GetStreamNameNTFS(Arc); + if (StreamName[0]!=':') { uiMsg(UIERROR_STREAMBROKEN,Arc.FileName,FileName); ErrHandler.SetErrorCode(RARX_CRC); @@ -131,17 +129,17 @@ void ExtractStreams(Archive &Arc,const wchar *FileName,bool TestMode) return; } - wcsncatz(FullName,StreamName,ASIZE(FullName)); + FullName+=StreamName; if (IsNtfsReservedStream(StreamName)) return; - FindData fd; - bool HostFound=FindFile::FastFind(FileName,&fd); + FindData FD; + bool HostFound=FindFile::FastFind(FileName,&FD); - if ((fd.FileAttr & FILE_ATTRIBUTE_READONLY)!=0) - SetFileAttr(FileName,fd.FileAttr & ~FILE_ATTRIBUTE_READONLY); + if ((FD.FileAttr & FILE_ATTRIBUTE_READONLY)!=0) + SetFileAttr(FileName,FD.FileAttr & ~FILE_ATTRIBUTE_READONLY); File CurFile; if (CurFile.WCreate(FullName)) @@ -153,32 +151,26 @@ void ExtractStreams(Archive &Arc,const wchar *FileName,bool TestMode) // Restoring original file timestamps. File HostFile; if (HostFound && HostFile.Open(FileName,FMF_OPENSHARED|FMF_UPDATE)) - SetFileTime(HostFile.GetHandle(),&fd.ftCreationTime,&fd.ftLastAccessTime, - &fd.ftLastWriteTime); + SetFileTime(HostFile.GetHandle(),&FD.ftCreationTime,&FD.ftLastAccessTime, + &FD.ftLastWriteTime); // Restoring original file attributes. Important if file was read only // or did not have "Archive" attribute - SetFileAttr(FileName,fd.FileAttr); + SetFileAttr(FileName,FD.FileAttr); } #endif -void GetStreamNameNTFS(Archive &Arc,wchar *StreamName,size_t MaxSize) +std::wstring GetStreamNameNTFS(Archive &Arc) { - byte *Data=&Arc.SubHead.SubData[0]; - size_t DataSize=Arc.SubHead.SubData.Size(); + std::wstring Dest; if (Arc.Format==RARFMT15) - { - size_t DestSize=Min(DataSize/2,MaxSize-1); - RawToWide(Data,StreamName,DestSize); - StreamName[DestSize]=0; - } + Dest=RawToWide(Arc.SubHead.SubData); else { - char UtfString[NM*4]; - size_t DestSize=Min(DataSize,ASIZE(UtfString)-1); - memcpy(UtfString,Data,DestSize); - UtfString[DestSize]=0; - UtfToWide(UtfString,StreamName,MaxSize); + std::vector Src=Arc.SubHead.SubData; + Src.push_back(0); // Needed for our UtfToWide. + UtfToWide((char *)Src.data(),Dest); } + return Dest; }