Upload 2.0.2
[physicsfs] / lzma / CPP / 7zip / Archive / 7z / 7zOut.h
1 // 7z/Out.h
2
3 #ifndef __7Z_OUT_H
4 #define __7Z_OUT_H
5
6 #include "7zHeader.h"
7 #include "7zItem.h"
8 #include "7zCompressionMode.h"
9 #include "7zEncode.h"
10
11 #include "../../Common/OutBuffer.h"
12 #include "../../../Common/DynamicBuffer.h"
13
14 namespace NArchive {
15 namespace N7z {
16
17 class CWriteBufferLoc
18 {
19   Byte *_data;
20   size_t _size;
21   size_t _pos;
22 public:
23   CWriteBufferLoc(): _size(0), _pos(0) {}
24   void Init(Byte *data, size_t size)  
25   { 
26     _pos = 0;
27     _data = data;
28     _size = size; 
29   }
30   HRESULT Write(const void *data, size_t size)
31   {
32     if (_pos + size > _size)
33       return E_FAIL;
34     memmove(_data + _pos, data, size);
35     _pos += size;
36     return S_OK; 
37   }
38 };
39
40 class CWriteDynamicBuffer
41 {
42   CByteDynamicBuffer _buffer;
43   size_t _pos;
44 public:
45   CWriteDynamicBuffer(): _pos(0) {}
46   void Init()  
47   { 
48     _pos = 0;
49   }
50   void Write(const void *data, size_t size)
51   {
52     if (_pos + size > _buffer.GetCapacity())
53       _buffer.EnsureCapacity(_pos + size);
54     memmove(((Byte *)_buffer) +_pos, data, size);
55     _pos += size;
56   }
57   operator Byte *() { return (Byte *)_buffer; };
58   operator const Byte *() const { return (const Byte *)_buffer; };
59   size_t GetSize() const { return _pos; }
60 };
61
62 struct CHeaderOptions
63 {
64   // bool UseAdditionalHeaderStreams;
65   bool CompressMainHeader;
66   bool WriteModified;
67   bool WriteCreated;
68   bool WriteAccessed;
69
70   CHeaderOptions(): 
71       // UseAdditionalHeaderStreams(false), 
72       CompressMainHeader(true),
73       WriteModified(true),
74       WriteCreated(false),
75       WriteAccessed(false) {} 
76 };
77
78 class COutArchive
79 {
80   UInt64 _prefixHeaderPos;
81
82   HRESULT WriteDirect(const void *data, UInt32 size);
83   HRESULT WriteDirectByte(Byte b) { return WriteDirect(&b, 1); }
84   HRESULT WriteDirectUInt32(UInt32 value);
85   HRESULT WriteDirectUInt64(UInt64 value);
86   
87   HRESULT WriteBytes(const void *data, size_t size);
88   HRESULT WriteBytes(const CByteBuffer &data);
89   HRESULT WriteByte(Byte b);
90   HRESULT WriteUInt32(UInt32 value);
91   HRESULT WriteNumber(UInt64 value);
92   HRESULT WriteID(UInt64 value) { return WriteNumber(value); }
93
94   HRESULT WriteFolder(const CFolder &folder);
95   HRESULT WriteFileHeader(const CFileItem &itemInfo);
96   HRESULT WriteBoolVector(const CBoolVector &boolVector);
97   HRESULT WriteHashDigests(
98       const CRecordVector<bool> &digestsDefined,
99       const CRecordVector<UInt32> &hashDigests);
100
101   HRESULT WritePackInfo(
102       UInt64 dataOffset,
103       const CRecordVector<UInt64> &packSizes,
104       const CRecordVector<bool> &packCRCsDefined,
105       const CRecordVector<UInt32> &packCRCs);
106
107   HRESULT WriteUnPackInfo(const CObjectVector<CFolder> &folders);
108
109   HRESULT WriteSubStreamsInfo(
110       const CObjectVector<CFolder> &folders,
111       const CRecordVector<CNum> &numUnPackStreamsInFolders,
112       const CRecordVector<UInt64> &unPackSizes,
113       const CRecordVector<bool> &digestsDefined,
114       const CRecordVector<UInt32> &hashDigests);
115
116   /*
117   HRESULT WriteStreamsInfo(
118       UInt64 dataOffset,
119       const CRecordVector<UInt64> &packSizes,
120       const CRecordVector<bool> &packCRCsDefined,
121       const CRecordVector<UInt32> &packCRCs,
122       bool externalFolders,
123       UInt64 externalFoldersStreamIndex,
124       const CObjectVector<CFolder> &folders,
125       const CRecordVector<CNum> &numUnPackStreamsInFolders,
126       const CRecordVector<UInt64> &unPackSizes,
127       const CRecordVector<bool> &digestsDefined,
128       const CRecordVector<UInt32> &hashDigests);
129   */
130
131
132   HRESULT WriteTime(const CObjectVector<CFileItem> &files, Byte type);
133
134   HRESULT EncodeStream(
135       DECL_EXTERNAL_CODECS_LOC_VARS
136       CEncoder &encoder, const Byte *data, size_t dataSize,
137       CRecordVector<UInt64> &packSizes, CObjectVector<CFolder> &folders);
138   HRESULT EncodeStream(
139       DECL_EXTERNAL_CODECS_LOC_VARS
140       CEncoder &encoder, const CByteBuffer &data, 
141       CRecordVector<UInt64> &packSizes, CObjectVector<CFolder> &folders);
142   HRESULT WriteHeader(
143       const CArchiveDatabase &database,
144       const CHeaderOptions &headerOptions,
145       UInt64 &headerOffset);
146   
147   bool _mainMode;
148
149   bool _dynamicMode;
150
151   bool _countMode;
152   size_t _countSize;
153   COutBuffer _outByte;
154   CWriteBufferLoc _outByte2;
155   CWriteDynamicBuffer _dynamicBuffer;
156   UInt32 _crc;
157
158   #ifdef _7Z_VOL
159   bool _endMarker;
160   #endif
161
162   HRESULT WriteSignature();
163   #ifdef _7Z_VOL
164   HRESULT WriteFinishSignature();
165   #endif
166   HRESULT WriteStartHeader(const CStartHeader &h);
167   #ifdef _7Z_VOL
168   HRESULT WriteFinishHeader(const CFinishHeader &h);
169   #endif
170   CMyComPtr<IOutStream> Stream;
171 public:
172
173   COutArchive() { _outByte.Create(1 << 16); }
174   CMyComPtr<ISequentialOutStream> SeqStream;
175   HRESULT Create(ISequentialOutStream *stream, bool endMarker);
176   void Close();
177   HRESULT SkeepPrefixArchiveHeader();
178   HRESULT WriteDatabase(
179       DECL_EXTERNAL_CODECS_LOC_VARS
180       const CArchiveDatabase &database,
181       const CCompressionMethodMode *options, 
182       const CHeaderOptions &headerOptions);
183
184   #ifdef _7Z_VOL
185   static UInt32 GetVolHeadersSize(UInt64 dataSize, int nameLength = 0, bool props = false);
186   static UInt64 GetVolPureSize(UInt64 volSize, int nameLength = 0, bool props = false);
187   #endif
188
189 };
190
191 }}
192
193 #endif