summaryrefslogtreecommitdiffstats
path: root/src/libs/7zip/win/CPP/7zip/Archive/Rar/RarIn.h
blob: a6998db264ddb13838744ba0ffbde5b9baf37a9f (plain)
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
// RarIn.h

#ifndef __ARCHIVE_RAR_IN_H
#define __ARCHIVE_RAR_IN_H

#include "Common/DynamicBuffer.h"
#include "Common/MyCom.h"

#include "../../ICoder.h"
#include "../../IStream.h"

#include "../../Common/StreamObjects.h"

#include "../../Crypto/RarAes.h"

#include "RarHeader.h"
#include "RarItem.h"

namespace NArchive {
namespace NRar {

class CInArchiveException
{
public:
  enum CCauseType
  {
    kUnexpectedEndOfArchive = 0,
    kArchiveHeaderCRCError,
    kFileHeaderCRCError,
    kIncorrectArchive
  }
  Cause;
  CInArchiveException(CCauseType cause) :   Cause(cause) {}
};


struct CInArchiveInfo
{
  UInt32 Flags;
  Byte EncryptVersion;
  UInt64 StartPosition;
  
  bool IsSolid() const { return (Flags & NHeader::NArchive::kSolid) != 0; }
  bool IsCommented() const {  return (Flags & NHeader::NArchive::kComment) != 0; }
  bool IsVolume() const {  return (Flags & NHeader::NArchive::kVolume) != 0; }
  bool HaveNewVolumeName() const {  return (Flags & NHeader::NArchive::kNewVolName) != 0; }
  bool IsEncrypted() const { return (Flags & NHeader::NArchive::kBlockEncryption) != 0; }
  bool IsThereEncryptVer() const { return (Flags & NHeader::NArchive::kEncryptVer) != 0; }
  bool IsEncryptOld() const { return (!IsThereEncryptVer() || EncryptVersion < 36); }
};

class CInArchive
{
  CMyComPtr<IInStream> m_Stream;
  
  UInt64 m_StreamStartPosition;
  
  CInArchiveInfo _header;
  CDynamicBuffer<char> m_NameBuffer;
  CDynamicBuffer<wchar_t> _unicodeNameBuffer;

  CByteBuffer _comment;
  
  void ReadName(CItemEx &item, int nameSize);
  void ReadHeaderReal(CItemEx &item);
  
  HRESULT ReadBytesSpec(void *data, size_t *size);
  bool ReadBytesAndTestSize(void *data, UInt32 size);
  
  HRESULT Open2(IInStream *stream, const UInt64 *searchHeaderSizeLimit);

  void ThrowExceptionWithCode(CInArchiveException::CCauseType cause);
  void ThrowUnexpectedEndOfArchiveException();
  
  void AddToSeekValue(UInt64 addValue);
  
  CDynamicBuffer<Byte> m_FileHeaderData;
  
  NHeader::NBlock::CBlock m_BlockHeader;

  NCrypto::NRar29::CDecoder *m_RarAESSpec;
  CMyComPtr<ICompressFilter> m_RarAES;
  
  Byte *m_CurData; // it must point to start of Rar::Block
  UInt32 m_CurPos;
  UInt32 m_PosLimit;
  Byte ReadByte();
  UInt16 ReadUInt16();
  UInt32 ReadUInt32();
  void ReadTime(Byte mask, CRarTime &rarTime);

  CBuffer<Byte> m_DecryptedData;
  Byte *m_DecryptedDataAligned;
  UInt32 m_DecryptedDataSize;

  bool m_CryptoMode;
  UInt32 m_CryptoPos;
  void FinishCryptoBlock()
  {
    if (m_CryptoMode)
      while ((m_CryptoPos & 0xF) != 0)
      {
        m_CryptoPos++;
        m_Position++;
      }
  }

public:
  UInt64 m_Position;

  HRESULT Open(IInStream *inStream, const UInt64 *searchHeaderSizeLimit);
  void Close();
  HRESULT GetNextItem(CItemEx &item, ICryptoGetTextPassword *getTextPassword, bool &decryptionError, AString &errorMessage);
  
  void GetArchiveInfo(CInArchiveInfo &archiveInfo) const;
  
  void SeekInArchive(UInt64 position);
  ISequentialInStream *CreateLimitedStream(UInt64 position, UInt64 size);
};
  
}}
  
#endif