Skip to content

Commit 679194e

Browse files
authored
Merge pull request #39 from chlily1/main
v7.0.7: Extracted from https://www.rarlab.com/rar/unrarsrc-7.0.7.tar.gz
2 parents ea1ef69 + faa2c64 commit 679194e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

125 files changed

+4388
-3165
lines changed

arccmt.cpp

+32-34
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
static bool IsAnsiEscComment(const wchar *Data,size_t Size);
22

3-
bool Archive::GetComment(Array<wchar> *CmtData)
3+
bool Archive::GetComment(std::wstring &CmtData)
44
{
55
if (!MainComment)
66
return false;
@@ -11,7 +11,7 @@ bool Archive::GetComment(Array<wchar> *CmtData)
1111
}
1212

1313

14-
bool Archive::DoGetComment(Array<wchar> *CmtData)
14+
bool Archive::DoGetComment(std::wstring &CmtData)
1515
{
1616
#ifndef SFX_MODULE
1717
uint CmtLength;
@@ -101,67 +101,64 @@ bool Archive::DoGetComment(Array<wchar> *CmtData)
101101
// 4x memory for OEM to UTF-8 output here.
102102
OemToCharBuffA((char *)UnpData,(char *)UnpData,(DWORD)UnpDataSize);
103103
#endif
104-
CmtData->Alloc(UnpDataSize+1);
105-
memset(CmtData->Addr(0),0,CmtData->Size()*sizeof(wchar));
106-
CharToWide((char *)UnpData,CmtData->Addr(0),CmtData->Size());
107-
CmtData->Alloc(wcslen(CmtData->Addr(0)));
104+
// CmtData.resize(UnpDataSize+1);
105+
CharToWide((const char *)UnpData,CmtData);
106+
// CmtData.resize(wcslen(CmtData->data()));
108107
}
109108
}
110109
}
111110
else
112111
{
113112
if (CmtLength==0)
114113
return false;
115-
Array<byte> CmtRaw(CmtLength);
116-
int ReadSize=Read(&CmtRaw[0],CmtLength);
114+
std::vector<byte> CmtRaw(CmtLength);
115+
int ReadSize=Read(CmtRaw.data(),CmtLength);
117116
if (ReadSize>=0 && (uint)ReadSize<CmtLength) // Comment is shorter than declared.
118117
{
119118
CmtLength=ReadSize;
120-
CmtRaw.Alloc(CmtLength);
119+
CmtRaw.resize(CmtLength);
121120
}
122121

123122
if (Format!=RARFMT14 && CommHead.CommCRC!=(~CRC32(0xffffffff,&CmtRaw[0],CmtLength)&0xffff))
124123
{
125124
uiMsg(UIERROR_CMTBROKEN,FileName);
126125
return false;
127126
}
128-
CmtData->Alloc(CmtLength+1);
129-
CmtRaw.Push(0);
127+
// CmtData.resize(CmtLength+1);
128+
CmtRaw.push_back(0);
130129
#ifdef _WIN_ALL
131130
// If we ever decide to extend it to Android, we'll need to alloc
132131
// 4x memory for OEM to UTF-8 output here.
133-
OemToCharA((char *)&CmtRaw[0],(char *)&CmtRaw[0]);
132+
OemToCharA((char *)CmtRaw.data(),(char *)CmtRaw.data());
134133
#endif
135-
CharToWide((char *)&CmtRaw[0],CmtData->Addr(0),CmtData->Size());
136-
CmtData->Alloc(wcslen(CmtData->Addr(0)));
134+
CharToWide((const char *)CmtRaw.data(),CmtData);
135+
// CmtData->resize(wcslen(CmtData->data()));
137136
}
138137
#endif
139-
return CmtData->Size() > 0;
138+
return CmtData.size() > 0;
140139
}
141140

142141

143-
bool Archive::ReadCommentData(Array<wchar> *CmtData)
142+
bool Archive::ReadCommentData(std::wstring &CmtData)
144143
{
145-
Array<byte> CmtRaw;
144+
std::vector<byte> CmtRaw;
146145
if (!ReadSubData(&CmtRaw,NULL,false))
147146
return false;
148-
size_t CmtSize=CmtRaw.Size();
149-
CmtRaw.Push(0);
150-
CmtData->Alloc(CmtSize+1);
147+
size_t CmtSize=CmtRaw.size();
148+
CmtRaw.push_back(0);
149+
// CmtData->resize(CmtSize+1);
151150
if (Format==RARFMT50)
152-
UtfToWide((char *)&CmtRaw[0],CmtData->Addr(0),CmtData->Size());
151+
UtfToWide((char *)CmtRaw.data(),CmtData);
153152
else
154153
if ((SubHead.SubFlags & SUBHEAD_FLAGS_CMT_UNICODE)!=0)
155154
{
156-
RawToWide(&CmtRaw[0],CmtData->Addr(0),CmtSize/2);
157-
(*CmtData)[CmtSize/2]=0;
158-
155+
CmtData=RawToWide(CmtRaw);
159156
}
160157
else
161158
{
162-
CharToWide((char *)&CmtRaw[0],CmtData->Addr(0),CmtData->Size());
159+
CharToWide((const char *)CmtRaw.data(),CmtData);
163160
}
164-
CmtData->Alloc(wcslen(CmtData->Addr(0))); // Set buffer size to actual comment length.
161+
// CmtData->resize(wcslen(CmtData->data())); // Set buffer size to actual comment length.
165162
return true;
166163
}
167164

@@ -170,15 +167,16 @@ void Archive::ViewComment()
170167
{
171168
if (Cmd->DisableComment)
172169
return;
173-
Array<wchar> CmtBuf;
174-
if (GetComment(&CmtBuf)) // In GUI too, so "Test" command detects broken comments.
170+
std::wstring CmtBuf;
171+
if (GetComment(CmtBuf)) // In GUI too, so "Test" command detects broken comments.
175172
{
176-
size_t CmtSize=CmtBuf.Size();
177-
wchar *ChPtr=wcschr(&CmtBuf[0],0x1A);
178-
if (ChPtr!=NULL)
179-
CmtSize=ChPtr-&CmtBuf[0];
180-
mprintf(L"\n");
181-
OutComment(&CmtBuf[0],CmtSize);
173+
size_t CmtSize=CmtBuf.size();
174+
auto EndPos=CmtBuf.find(0x1A);
175+
if (EndPos!=std::wstring::npos)
176+
CmtSize=EndPos;
177+
mprintf(St(MArcComment));
178+
mprintf(L":\n");
179+
OutComment(CmtBuf);
182180
}
183181
}
184182

archive.cpp

+26-7
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ Archive::Archive(CommandData *InitCmd)
3939
VolWrite=0;
4040
AddingFilesSize=0;
4141
AddingHeadersSize=0;
42-
*FirstVolumeName=0;
4342

4443
Splitting=false;
4544
NewArchive=false;
@@ -74,15 +73,15 @@ void Archive::CheckArc(bool EnableBroken)
7473

7574

7675
#if !defined(SFX_MODULE)
77-
void Archive::CheckOpen(const wchar *Name)
76+
void Archive::CheckOpen(const std::wstring &Name)
7877
{
7978
TOpen(Name);
8079
CheckArc(false);
8180
}
8281
#endif
8382

8483

85-
bool Archive::WCheckOpen(const wchar *Name)
84+
bool Archive::WCheckOpen(const std::wstring &Name)
8685
{
8786
if (!WOpen(Name))
8887
return false;
@@ -148,9 +147,9 @@ bool Archive::IsArchive(bool EnableBroken)
148147
}
149148
else
150149
{
151-
Array<char> Buffer(MAXSFXSIZE);
150+
std::vector<char> Buffer(MAXSFXSIZE);
152151
long CurPos=(long)Tell();
153-
int ReadSize=Read(&Buffer[0],Buffer.Size()-16);
152+
int ReadSize=Read(Buffer.data(),Buffer.size()-16);
154153
for (int I=0;I<ReadSize;I++)
155154
if (Buffer[I]==0x52 && (Type=IsSignature((byte *)&Buffer[I],ReadSize-I))!=RARFMT_NONE)
156155
{
@@ -265,7 +264,7 @@ bool Archive::IsArchive(bool EnableBroken)
265264
Seek(SavePos,SEEK_SET);
266265
}
267266
if (!Volume || FirstVolume)
268-
wcsncpyz(FirstVolumeName,FileName,ASIZE(FirstVolumeName));
267+
FirstVolumeName=FileName;
269268

270269
return true;
271270
}
@@ -301,7 +300,7 @@ uint Archive::FullHeaderSize(size_t Size)
301300

302301

303302
#ifdef USE_QOPEN
304-
bool Archive::Open(const wchar *Name,uint Mode)
303+
bool Archive::Open(const std::wstring &Name,uint Mode)
305304
{
306305
// Important if we reuse Archive object and it has virtual QOpen
307306
// file position not matching real. For example, for 'l -v volname'.
@@ -336,3 +335,23 @@ int64 Archive::Tell()
336335
}
337336
#endif
338337

338+
339+
// Return 0 if dictionary size is invalid. If size is RAR7 only, return
340+
// the adjusted nearest bottom value. Return header flags in Flags.
341+
uint64 Archive::GetWinSize(uint64 Size,uint &Flags)
342+
{
343+
Flags=0;
344+
// Allow 128 KB - 1 TB range.
345+
if (Size<0x20000 || Size>0x10000000000ULL)
346+
return 0;
347+
uint64 Pow2=0x20000; // Power of 2 dictionary size.
348+
for (;2*Pow2<=Size;Pow2*=2)
349+
Flags+=FCI_DICT_BIT0;
350+
if (Size==Pow2)
351+
return Size; // If 'Size' is the power of 2, return it as is.
352+
353+
// Get the number of Pow2/32 to add to Pow2 for nearest value not exceeding 'Size'.
354+
uint64 Fraction=(Size-Pow2)/(Pow2/32);
355+
Flags+=(uint)Fraction*FCI_DICT_FRACT0;
356+
return Pow2+Fraction*(Pow2/32);
357+
}

archive.hpp

+23-17
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class Archive:public File
2727
{
2828
private:
2929
void UpdateLatestTime(FileHeader *CurBlock);
30-
void ConvertNameCase(wchar *Name);
30+
void ConvertNameCase(std::wstring &Name);
3131
void ConvertFileHeader(FileHeader *hd);
3232
size_t ReadHeader14();
3333
size_t ReadHeader15();
@@ -36,9 +36,9 @@ class Archive:public File
3636
void RequestArcPassword(RarCheckPassword *SelPwd);
3737
void UnexpEndArcMsg();
3838
void BrokenHeaderMsg();
39-
void UnkEncVerMsg(const wchar *Name,const wchar *Info);
40-
bool DoGetComment(Array<wchar> *CmtData);
41-
bool ReadCommentData(Array<wchar> *CmtData);
39+
void UnkEncVerMsg(const std::wstring &Name,const std::wstring &Info);
40+
bool DoGetComment(std::wstring &CmtData);
41+
bool ReadCommentData(std::wstring &CmtData);
4242

4343
#if !defined(RAR_NOCRYPT)
4444
CryptData HeadersCrypt;
@@ -67,9 +67,9 @@ class Archive:public File
6767
size_t SearchRR();
6868
size_t ReadHeader();
6969
void CheckArc(bool EnableBroken);
70-
void CheckOpen(const wchar *Name);
71-
bool WCheckOpen(const wchar *Name);
72-
bool GetComment(Array<wchar> *CmtData);
70+
void CheckOpen(const std::wstring &Name);
71+
bool WCheckOpen(const std::wstring &Name);
72+
bool GetComment(std::wstring &CmtData);
7373
void ViewComment();
7474
void SetLatestTime(RarTime *NewTime);
7575
void SeekToNext();
@@ -79,23 +79,25 @@ class Archive:public File
7979
void VolSubtractHeaderSize(size_t SubSize);
8080
uint FullHeaderSize(size_t Size);
8181
int64 GetStartPos();
82-
void AddSubData(byte *SrcData,uint64 DataSize,File *SrcFile,
82+
void AddSubData(const byte *SrcData,uint64 DataSize,File *SrcFile,
8383
const wchar *Name,uint Flags);
84-
bool ReadSubData(Array<byte> *UnpData,File *DestFile,bool TestMode);
84+
bool ReadSubData(std::vector<byte> *UnpData,File *DestFile,bool TestMode);
8585
HEADER_TYPE GetHeaderType() {return CurHeaderType;}
8686
CommandData* GetCommandData() {return Cmd;}
8787
void SetSilentOpen(bool Mode) {SilentOpen=Mode;}
88-
#if 0
89-
void GetRecoveryInfo(bool Required,int64 *Size,int *Percent);
90-
#endif
9188
#ifdef USE_QOPEN
92-
bool Open(const wchar *Name,uint Mode=FMF_READ);
93-
int Read(void *Data,size_t Size);
94-
void Seek(int64 Offset,int Method);
95-
int64 Tell();
89+
bool Open(const std::wstring &Name,uint Mode=FMF_READ) override;
90+
int Read(void *Data,size_t Size) override;
91+
void Seek(int64 Offset,int Method) override;
92+
int64 Tell() override;
9693
void QOpenUnload() {QOpen.Unload();}
9794
void SetProhibitQOpen(bool Mode) {ProhibitQOpen=Mode;}
9895
#endif
96+
static uint64 GetWinSize(uint64 Size,uint &Flags);
97+
98+
// Needed to see wstring based Open from File. Otherwise compiler finds
99+
// Open in Archive and doesn't check the base class overloads.
100+
using File::Open;
99101

100102
BaseBlock ShortBlock;
101103
MarkHeader MarkHead;
@@ -135,12 +137,16 @@ class Archive:public File
135137

136138
uint VolNumber;
137139
int64 VolWrite;
140+
141+
// Total size of files adding to archive. Might also include the size of
142+
// files repacked in solid archive.
138143
uint64 AddingFilesSize;
144+
139145
uint64 AddingHeadersSize;
140146

141147
bool NewArchive;
142148

143-
wchar FirstVolumeName[NM];
149+
std::wstring FirstVolumeName;
144150
};
145151

146152

0 commit comments

Comments
 (0)