1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
|
// Archive/RarIn.cpp
#include "StdAfx.h"
#include "../../../../C/7zCrc.h"
#include "Common/StringConvert.h"
#include "Common/UTFConvert.h"
#include "../../Common/LimitedStreams.h"
#include "../../Common/StreamUtils.h"
#include "../Common/FindSignature.h"
#include "RarIn.h"
namespace NArchive {
namespace NRar {
void CInArchive::ThrowExceptionWithCode(
CInArchiveException::CCauseType cause)
{
throw CInArchiveException(cause);
}
HRESULT CInArchive::Open(IInStream *inStream, const UInt64 *searchHeaderSizeLimit)
{
try
{
Close();
HRESULT res = Open2(inStream, searchHeaderSizeLimit);
if (res == S_OK)
return res;
Close();
return res;
}
catch(...) { Close(); throw; }
}
void CInArchive::Close()
{
m_Stream.Release();
}
static inline bool TestMarkerCandidate(const void *aTestBytes)
{
for (UInt32 i = 0; i < NHeader::kMarkerSize; i++)
if (((const Byte *)aTestBytes)[i] != NHeader::kMarker[i])
return false;
return true;
}
HRESULT CInArchive::FindAndReadMarker(IInStream *stream, const UInt64 *searchHeaderSizeLimit)
{
RINOK(FindSignatureInStream(stream,
NHeader::kMarker, NHeader::kMarkerSize,
searchHeaderSizeLimit, m_ArchiveStartPosition));
m_Stream = stream;
m_Position = m_ArchiveStartPosition + NHeader::kMarkerSize;
return m_Stream->Seek(m_Position, STREAM_SEEK_SET, NULL);
}
void CInArchive::ThrowUnexpectedEndOfArchiveException()
{
ThrowExceptionWithCode(CInArchiveException::kUnexpectedEndOfArchive);
}
bool CInArchive::ReadBytesAndTestSize(void *data, UInt32 size)
{
if (m_CryptoMode)
{
const Byte *bufData = (const Byte *)m_DecryptedData;
UInt32 bufSize = m_DecryptedDataSize;
UInt32 i;
for (i = 0; i < size && m_CryptoPos < bufSize; i++)
((Byte *)data)[i] = bufData[m_CryptoPos++];
return (i == size);
}
return (ReadStream_FALSE(m_Stream, data, size) == S_OK);
}
void CInArchive::ReadBytesAndTestResult(void *data, UInt32 size)
{
if(!ReadBytesAndTestSize(data,size))
ThrowUnexpectedEndOfArchiveException();
}
HRESULT CInArchive::ReadBytes(void *data, UInt32 size, UInt32 *processedSize)
{
size_t realProcessedSize = size;
HRESULT result = ReadStream(m_Stream, data, &realProcessedSize);
if (processedSize != NULL)
*processedSize = (UInt32)realProcessedSize;
AddToSeekValue(realProcessedSize);
return result;
}
static UInt32 CrcUpdateUInt16(UInt32 crc, UInt16 v)
{
crc = CRC_UPDATE_BYTE(crc, (Byte)(v & 0xFF));
crc = CRC_UPDATE_BYTE(crc, (Byte)((v >> 8) & 0xFF));
return crc;
}
static UInt32 CrcUpdateUInt32(UInt32 crc, UInt32 v)
{
crc = CRC_UPDATE_BYTE(crc, (Byte)(v & 0xFF));
crc = CRC_UPDATE_BYTE(crc, (Byte)((v >> 8) & 0xFF));
crc = CRC_UPDATE_BYTE(crc, (Byte)((v >> 16) & 0xFF));
crc = CRC_UPDATE_BYTE(crc, (Byte)((v >> 24) & 0xFF));
return crc;
}
HRESULT CInArchive::Open2(IInStream *stream, const UInt64 *searchHeaderSizeLimit)
{
m_CryptoMode = false;
RINOK(stream->Seek(0, STREAM_SEEK_SET, &m_StreamStartPosition));
m_Position = m_StreamStartPosition;
RINOK(FindAndReadMarker(stream, searchHeaderSizeLimit));
Byte buf[NHeader::NArchive::kArchiveHeaderSize];
UInt32 processedSize;
ReadBytes(buf, sizeof(buf), &processedSize);
if (processedSize != sizeof(buf))
return S_FALSE;
m_CurData = buf;
m_CurPos = 0;
m_PosLimit = sizeof(buf);
m_ArchiveHeader.CRC = ReadUInt16();
m_ArchiveHeader.Type = ReadByte();
m_ArchiveHeader.Flags = ReadUInt16();
m_ArchiveHeader.Size = ReadUInt16();
m_ArchiveHeader.Reserved1 = ReadUInt16();
m_ArchiveHeader.Reserved2 = ReadUInt32();
m_ArchiveHeader.EncryptVersion = 0;
UInt32 crc = CRC_INIT_VAL;
crc = CRC_UPDATE_BYTE(crc, m_ArchiveHeader.Type);
crc = CrcUpdateUInt16(crc, m_ArchiveHeader.Flags);
crc = CrcUpdateUInt16(crc, m_ArchiveHeader.Size);
crc = CrcUpdateUInt16(crc, m_ArchiveHeader.Reserved1);
crc = CrcUpdateUInt32(crc, m_ArchiveHeader.Reserved2);
if (m_ArchiveHeader.IsThereEncryptVer() && m_ArchiveHeader.Size > NHeader::NArchive::kArchiveHeaderSize)
{
ReadBytes(&m_ArchiveHeader.EncryptVersion, 1, &processedSize);
if (processedSize != 1)
return S_FALSE;
crc = CRC_UPDATE_BYTE(crc, m_ArchiveHeader.EncryptVersion);
}
if(m_ArchiveHeader.CRC != (CRC_GET_DIGEST(crc) & 0xFFFF))
ThrowExceptionWithCode(CInArchiveException::kArchiveHeaderCRCError);
if (m_ArchiveHeader.Type != NHeader::NBlockType::kArchiveHeader)
return S_FALSE;
m_ArchiveCommentPosition = m_Position;
m_SeekOnArchiveComment = true;
return S_OK;
}
void CInArchive::SkipArchiveComment()
{
if (!m_SeekOnArchiveComment)
return;
AddToSeekValue(m_ArchiveHeader.Size - m_ArchiveHeader.GetBaseSize());
m_SeekOnArchiveComment = false;
}
void CInArchive::GetArchiveInfo(CInArchiveInfo &archiveInfo) const
{
archiveInfo.StartPosition = m_ArchiveStartPosition;
archiveInfo.Flags = m_ArchiveHeader.Flags;
archiveInfo.CommentPosition = m_ArchiveCommentPosition;
archiveInfo.CommentSize = (UInt16)(m_ArchiveHeader.Size - NHeader::NArchive::kArchiveHeaderSize);
}
static void DecodeUnicodeFileName(const char *name, const Byte *encName,
int encSize, wchar_t *unicodeName, int maxDecSize)
{
int encPos = 0;
int decPos = 0;
int flagBits = 0;
Byte flags = 0;
Byte highByte = encName[encPos++];
while (encPos < encSize && decPos < maxDecSize)
{
if (flagBits == 0)
{
flags = encName[encPos++];
flagBits = 8;
}
switch(flags >> 6)
{
case 0:
unicodeName[decPos++] = encName[encPos++];
break;
case 1:
unicodeName[decPos++] = (wchar_t)(encName[encPos++] + (highByte << 8));
break;
case 2:
unicodeName[decPos++] = (wchar_t)(encName[encPos] + (encName[encPos + 1] << 8));
encPos += 2;
break;
case 3:
{
int length = encName[encPos++];
if (length & 0x80)
{
Byte correction = encName[encPos++];
for (length = (length & 0x7f) + 2;
length > 0 && decPos < maxDecSize; length--, decPos++)
unicodeName[decPos] = (wchar_t)(((name[decPos] + correction) & 0xff) + (highByte << 8));
}
else
for (length += 2; length > 0 && decPos < maxDecSize; length--, decPos++)
unicodeName[decPos] = name[decPos];
}
break;
}
flags <<= 2;
flagBits -= 2;
}
unicodeName[decPos < maxDecSize ? decPos : maxDecSize - 1] = 0;
}
void CInArchive::ReadName(CItemEx &item, int nameSize)
{
item.UnicodeName.Empty();
if (nameSize > 0)
{
m_NameBuffer.EnsureCapacity(nameSize + 1);
char *buffer = (char *)m_NameBuffer;
for (int i = 0; i < nameSize; i++)
buffer[i] = ReadByte();
int mainLen;
for (mainLen = 0; mainLen < nameSize; mainLen++)
if (buffer[mainLen] == '\0')
break;
buffer[mainLen] = '\0';
item.Name = buffer;
if(item.HasUnicodeName())
{
if(mainLen < nameSize)
{
int unicodeNameSizeMax = MyMin(nameSize, (0x400));
_unicodeNameBuffer.EnsureCapacity(unicodeNameSizeMax + 1);
DecodeUnicodeFileName(buffer, (const Byte *)buffer + mainLen + 1,
nameSize - (mainLen + 1), _unicodeNameBuffer, unicodeNameSizeMax);
item.UnicodeName = _unicodeNameBuffer;
}
else if (!ConvertUTF8ToUnicode(item.Name, item.UnicodeName))
item.UnicodeName.Empty();
}
}
else
item.Name.Empty();
}
Byte CInArchive::ReadByte()
{
if (m_CurPos >= m_PosLimit)
throw CInArchiveException(CInArchiveException::kIncorrectArchive);
return m_CurData[m_CurPos++];
}
UInt16 CInArchive::ReadUInt16()
{
UInt16 value = 0;
for (int i = 0; i < 2; i++)
{
Byte b = ReadByte();
value |= (UInt16(b) << (8 * i));
}
return value;
}
UInt32 CInArchive::ReadUInt32()
{
UInt32 value = 0;
for (int i = 0; i < 4; i++)
{
Byte b = ReadByte();
value |= (UInt32(b) << (8 * i));
}
return value;
}
void CInArchive::ReadTime(Byte mask, CRarTime &rarTime)
{
rarTime.LowSecond = (Byte)(((mask & 4) != 0) ? 1 : 0);
int numDigits = (mask & 3);
rarTime.SubTime[0] = rarTime.SubTime[1] = rarTime.SubTime[2] = 0;
for (int i = 0; i < numDigits; i++)
rarTime.SubTime[3 - numDigits + i] = ReadByte();
}
void CInArchive::ReadHeaderReal(CItemEx &item)
{
item.Flags = m_BlockHeader.Flags;
item.PackSize = ReadUInt32();
item.Size = ReadUInt32();
item.HostOS = ReadByte();
item.FileCRC = ReadUInt32();
item.MTime.DosTime = ReadUInt32();
item.UnPackVersion = ReadByte();
item.Method = ReadByte();
int nameSize = ReadUInt16();
item.Attrib = ReadUInt32();
item.MTime.LowSecond = 0;
item.MTime.SubTime[0] =
item.MTime.SubTime[1] =
item.MTime.SubTime[2] = 0;
if((item.Flags & NHeader::NFile::kSize64Bits) != 0)
{
item.PackSize |= ((UInt64)ReadUInt32() << 32);
item.Size |= ((UInt64)ReadUInt32() << 32);
}
ReadName(item, nameSize);
if (item.HasSalt())
for (int i = 0; i < sizeof(item.Salt); i++)
item.Salt[i] = ReadByte();
// some rar archives have HasExtTime flag without field.
if (m_CurPos < m_PosLimit && item.HasExtTime())
{
Byte accessMask = (Byte)(ReadByte() >> 4);
Byte b = ReadByte();
Byte modifMask = (Byte)(b >> 4);
Byte createMask = (Byte)(b & 0xF);
if ((modifMask & 8) != 0)
ReadTime(modifMask, item.MTime);
item.CTimeDefined = ((createMask & 8) != 0);
if (item.CTimeDefined)
{
item.CTime.DosTime = ReadUInt32();
ReadTime(createMask, item.CTime);
}
item.ATimeDefined = ((accessMask & 8) != 0);
if (item.ATimeDefined)
{
item.ATime.DosTime = ReadUInt32();
ReadTime(accessMask, item.ATime);
}
}
UInt16 fileHeaderWithNameSize = (UInt16)m_CurPos;
item.Position = m_Position;
item.MainPartSize = fileHeaderWithNameSize;
item.CommentSize = (UInt16)(m_BlockHeader.HeadSize - fileHeaderWithNameSize);
if (m_CryptoMode)
item.AlignSize = (UInt16)((16 - ((m_BlockHeader.HeadSize) & 0xF)) & 0xF);
else
item.AlignSize = 0;
AddToSeekValue(m_BlockHeader.HeadSize);
}
void CInArchive::AddToSeekValue(UInt64 addValue)
{
m_Position += addValue;
}
HRESULT CInArchive::GetNextItem(CItemEx &item, ICryptoGetTextPassword *getTextPassword, bool &decryptionError)
{
decryptionError = false;
if (m_SeekOnArchiveComment)
SkipArchiveComment();
for (;;)
{
if(!SeekInArchive(m_Position))
return S_FALSE;
if (!m_CryptoMode && (m_ArchiveHeader.Flags &
NHeader::NArchive::kBlockHeadersAreEncrypted) != 0)
{
m_CryptoMode = false;
if (getTextPassword == 0)
return S_FALSE;
if(!SeekInArchive(m_Position))
return S_FALSE;
if (!m_RarAES)
{
m_RarAESSpec = new NCrypto::NRar29::CDecoder;
m_RarAES = m_RarAESSpec;
}
m_RarAESSpec->SetRar350Mode(m_ArchiveHeader.IsEncryptOld());
// Salt
const UInt32 kSaltSize = 8;
Byte salt[kSaltSize];
if(!ReadBytesAndTestSize(salt, kSaltSize))
return S_FALSE;
m_Position += kSaltSize;
RINOK(m_RarAESSpec->SetDecoderProperties2(salt, kSaltSize))
// Password
CMyComBSTR password;
RINOK(getTextPassword->CryptoGetTextPassword(&password))
UString unicodePassword(password);
CByteBuffer buffer;
const UInt32 sizeInBytes = unicodePassword.Length() * 2;
buffer.SetCapacity(sizeInBytes);
for (int i = 0; i < unicodePassword.Length(); i++)
{
wchar_t c = unicodePassword[i];
((Byte *)buffer)[i * 2] = (Byte)c;
((Byte *)buffer)[i * 2 + 1] = (Byte)(c >> 8);
}
RINOK(m_RarAESSpec->CryptoSetPassword((const Byte *)buffer, sizeInBytes));
const UInt32 kDecryptedBufferSize = (1 << 12);
if (m_DecryptedData.GetCapacity() == 0)
{
m_DecryptedData.SetCapacity(kDecryptedBufferSize);
}
RINOK(m_RarAES->Init());
size_t decryptedDataSizeT = kDecryptedBufferSize;
RINOK(ReadStream(m_Stream, (Byte *)m_DecryptedData, &decryptedDataSizeT));
m_DecryptedDataSize = (UInt32)decryptedDataSizeT;
m_DecryptedDataSize = m_RarAES->Filter((Byte *)m_DecryptedData, m_DecryptedDataSize);
m_CryptoMode = true;
m_CryptoPos = 0;
}
m_FileHeaderData.EnsureCapacity(7);
if(!ReadBytesAndTestSize((Byte *)m_FileHeaderData, 7))
return S_FALSE;
m_CurData = (Byte *)m_FileHeaderData;
m_CurPos = 0;
m_PosLimit = 7;
m_BlockHeader.CRC = ReadUInt16();
m_BlockHeader.Type = ReadByte();
m_BlockHeader.Flags = ReadUInt16();
m_BlockHeader.HeadSize = ReadUInt16();
if (m_BlockHeader.HeadSize < 7)
ThrowExceptionWithCode(CInArchiveException::kIncorrectArchive);
if (m_BlockHeader.Type == NHeader::NBlockType::kEndOfArchive)
return S_FALSE;
if (m_BlockHeader.Type == NHeader::NBlockType::kFileHeader)
{
m_FileHeaderData.EnsureCapacity(m_BlockHeader.HeadSize);
m_CurData = (Byte *)m_FileHeaderData;
m_PosLimit = m_BlockHeader.HeadSize;
ReadBytesAndTestResult(m_CurData + m_CurPos, m_BlockHeader.HeadSize - 7);
ReadHeaderReal(item);
if ((CrcCalc(m_CurData + 2,
m_BlockHeader.HeadSize - item.CommentSize - 2) & 0xFFFF) != m_BlockHeader.CRC)
ThrowExceptionWithCode(CInArchiveException::kFileHeaderCRCError);
FinishCryptoBlock();
m_CryptoMode = false;
SeekInArchive(m_Position); // Move Position to compressed Data;
AddToSeekValue(item.PackSize); // m_Position points to next header;
return S_OK;
}
if (m_CryptoMode && m_BlockHeader.HeadSize > (1 << 10))
{
decryptionError = true;
return S_FALSE;
}
if ((m_BlockHeader.Flags & NHeader::NBlock::kLongBlock) != 0)
{
m_FileHeaderData.EnsureCapacity(7 + 4);
m_CurData = (Byte *)m_FileHeaderData;
ReadBytesAndTestResult(m_CurData + m_CurPos, 4); // test it
m_PosLimit = 7 + 4;
UInt32 dataSize = ReadUInt32();
AddToSeekValue(dataSize);
if (m_CryptoMode && dataSize > (1 << 27))
{
decryptionError = true;
return S_FALSE;
}
m_CryptoPos = m_BlockHeader.HeadSize;
}
else
m_CryptoPos = 0;
AddToSeekValue(m_BlockHeader.HeadSize);
FinishCryptoBlock();
m_CryptoMode = false;
}
}
bool CInArchive::SeekInArchive(UInt64 position)
{
UInt64 newPosition;
m_Stream->Seek(position, STREAM_SEEK_SET, &newPosition);
return newPosition == position;
}
ISequentialInStream* CInArchive::CreateLimitedStream(UInt64 position, UInt64 size)
{
CLimitedSequentialInStream *streamSpec = new CLimitedSequentialInStream;
CMyComPtr<ISequentialInStream> inStream(streamSpec);
SeekInArchive(position);
streamSpec->SetStream(m_Stream);
streamSpec->Init(size);
return inStream.Detach();
}
}}
|