Imported Upstream version 9.20
[platform/upstream/7zip.git] / CPP / 7zip / Archive / 7z / 7zOut.h
1 // 7zOut.h\r
2 \r
3 #ifndef __7Z_OUT_H\r
4 #define __7Z_OUT_H\r
5 \r
6 #include "7zCompressionMode.h"\r
7 #include "7zEncode.h"\r
8 #include "7zHeader.h"\r
9 #include "7zItem.h"\r
10 \r
11 #include "../../Common/OutBuffer.h"\r
12 \r
13 namespace NArchive {\r
14 namespace N7z {\r
15 \r
16 class CWriteBufferLoc\r
17 {\r
18   Byte *_data;\r
19   size_t _size;\r
20   size_t _pos;\r
21 public:\r
22   CWriteBufferLoc(): _size(0), _pos(0) {}\r
23   void Init(Byte *data, size_t size)\r
24   {\r
25     _data = data;\r
26     _size = size;\r
27     _pos = 0;\r
28   }\r
29   void WriteBytes(const void *data, size_t size)\r
30   {\r
31     if (size > _size - _pos)\r
32       throw 1;\r
33     memcpy(_data + _pos, data, size);\r
34     _pos += size;\r
35   }\r
36   void WriteByte(Byte b)\r
37   {\r
38     if (_size == _pos)\r
39       throw 1;\r
40     _data[_pos++] = b;\r
41   }\r
42   size_t GetPos() const { return _pos; }\r
43 };\r
44 \r
45 struct CHeaderOptions\r
46 {\r
47   bool CompressMainHeader;\r
48   bool WriteCTime;\r
49   bool WriteATime;\r
50   bool WriteMTime;\r
51 \r
52   CHeaderOptions():\r
53       CompressMainHeader(true),\r
54       WriteCTime(false),\r
55       WriteATime(false),\r
56       WriteMTime(true)\r
57       {}\r
58 };\r
59 \r
60 class COutArchive\r
61 {\r
62   UInt64 _prefixHeaderPos;\r
63 \r
64   HRESULT WriteDirect(const void *data, UInt32 size);\r
65   \r
66   UInt64 GetPos() const;\r
67   void WriteBytes(const void *data, size_t size);\r
68   void WriteBytes(const CByteBuffer &data) { WriteBytes(data, data.GetCapacity()); }\r
69   void WriteByte(Byte b);\r
70   void WriteUInt32(UInt32 value);\r
71   void WriteUInt64(UInt64 value);\r
72   void WriteNumber(UInt64 value);\r
73   void WriteID(UInt64 value) { WriteNumber(value); }\r
74 \r
75   void WriteFolder(const CFolder &folder);\r
76   HRESULT WriteFileHeader(const CFileItem &itemInfo);\r
77   void WriteBoolVector(const CBoolVector &boolVector);\r
78   void WriteHashDigests(\r
79       const CRecordVector<bool> &digestsDefined,\r
80       const CRecordVector<UInt32> &hashDigests);\r
81 \r
82   void WritePackInfo(\r
83       UInt64 dataOffset,\r
84       const CRecordVector<UInt64> &packSizes,\r
85       const CRecordVector<bool> &packCRCsDefined,\r
86       const CRecordVector<UInt32> &packCRCs);\r
87 \r
88   void WriteUnpackInfo(const CObjectVector<CFolder> &folders);\r
89 \r
90   void WriteSubStreamsInfo(\r
91       const CObjectVector<CFolder> &folders,\r
92       const CRecordVector<CNum> &numUnpackStreamsInFolders,\r
93       const CRecordVector<UInt64> &unpackSizes,\r
94       const CRecordVector<bool> &digestsDefined,\r
95       const CRecordVector<UInt32> &hashDigests);\r
96 \r
97   void SkipAlign(unsigned pos, unsigned alignSize);\r
98   void WriteAlignedBoolHeader(const CBoolVector &v, int numDefined, Byte type, unsigned itemSize);\r
99   void WriteUInt64DefVector(const CUInt64DefVector &v, Byte type);\r
100 \r
101   HRESULT EncodeStream(\r
102       DECL_EXTERNAL_CODECS_LOC_VARS\r
103       CEncoder &encoder, const CByteBuffer &data,\r
104       CRecordVector<UInt64> &packSizes, CObjectVector<CFolder> &folders);\r
105   void WriteHeader(\r
106       const CArchiveDatabase &db,\r
107       const CHeaderOptions &headerOptions,\r
108       UInt64 &headerOffset);\r
109   \r
110   bool _countMode;\r
111   bool _writeToStream;\r
112   size_t _countSize;\r
113   UInt32 _crc;\r
114   COutBuffer _outByte;\r
115   CWriteBufferLoc _outByte2;\r
116 \r
117   #ifdef _7Z_VOL\r
118   bool _endMarker;\r
119   #endif\r
120 \r
121   HRESULT WriteSignature();\r
122   #ifdef _7Z_VOL\r
123   HRESULT WriteFinishSignature();\r
124   #endif\r
125   HRESULT WriteStartHeader(const CStartHeader &h);\r
126   #ifdef _7Z_VOL\r
127   HRESULT WriteFinishHeader(const CFinishHeader &h);\r
128   #endif\r
129   CMyComPtr<IOutStream> Stream;\r
130 public:\r
131 \r
132   COutArchive() { _outByte.Create(1 << 16); }\r
133   CMyComPtr<ISequentialOutStream> SeqStream;\r
134   HRESULT Create(ISequentialOutStream *stream, bool endMarker);\r
135   void Close();\r
136   HRESULT SkipPrefixArchiveHeader();\r
137   HRESULT WriteDatabase(\r
138       DECL_EXTERNAL_CODECS_LOC_VARS\r
139       const CArchiveDatabase &db,\r
140       const CCompressionMethodMode *options,\r
141       const CHeaderOptions &headerOptions);\r
142 \r
143   #ifdef _7Z_VOL\r
144   static UInt32 GetVolHeadersSize(UInt64 dataSize, int nameLength = 0, bool props = false);\r
145   static UInt64 GetVolPureSize(UInt64 volSize, int nameLength = 0, bool props = false);\r
146   #endif\r
147 \r
148 };\r
149 \r
150 }}\r
151 \r
152 #endif\r