7zOut.h 5.0 KB

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