ZipHeaderFactory.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. using System;
  2. using System.IO;
  3. #if !NO_CRYPTO
  4. using System.Linq;
  5. #endif
  6. using SharpCompress.Common.Zip.Headers;
  7. using SharpCompress.IO;
  8. using System.Text;
  9. namespace SharpCompress.Common.Zip
  10. {
  11. internal class ZipHeaderFactory
  12. {
  13. internal const uint ENTRY_HEADER_BYTES = 0x04034b50;
  14. internal const uint POST_DATA_DESCRIPTOR = 0x08074b50;
  15. internal const uint DIRECTORY_START_HEADER_BYTES = 0x02014b50;
  16. internal const uint DIRECTORY_END_HEADER_BYTES = 0x06054b50;
  17. internal const uint DIGITAL_SIGNATURE = 0x05054b50;
  18. internal const uint SPLIT_ARCHIVE_HEADER_BYTES = 0x30304b50;
  19. internal const uint ZIP64_END_OF_CENTRAL_DIRECTORY = 0x06064b50;
  20. internal const uint ZIP64_END_OF_CENTRAL_DIRECTORY_LOCATOR = 0x07064b50;
  21. protected LocalEntryHeader _lastEntryHeader;
  22. private readonly string _password;
  23. private readonly StreamingMode _mode;
  24. private readonly ArchiveEncoding _archiveEncoding;
  25. protected ZipHeaderFactory(StreamingMode mode, string password, ArchiveEncoding archiveEncoding)
  26. {
  27. this._mode = mode;
  28. this._password = password;
  29. this._archiveEncoding = archiveEncoding;
  30. }
  31. protected ZipHeader ReadHeader(uint headerBytes, BinaryReader reader, bool zip64 = false)
  32. {
  33. switch (headerBytes)
  34. {
  35. case ENTRY_HEADER_BYTES:
  36. {
  37. var entryHeader = new LocalEntryHeader(_archiveEncoding);
  38. entryHeader.Read(reader);
  39. LoadHeader(entryHeader, reader.BaseStream);
  40. _lastEntryHeader = entryHeader;
  41. return entryHeader;
  42. }
  43. case DIRECTORY_START_HEADER_BYTES:
  44. {
  45. var entry = new DirectoryEntryHeader(_archiveEncoding);
  46. entry.Read(reader);
  47. return entry;
  48. }
  49. case POST_DATA_DESCRIPTOR:
  50. {
  51. if (FlagUtility.HasFlag(_lastEntryHeader.Flags, HeaderFlags.UsePostDataDescriptor))
  52. {
  53. _lastEntryHeader.Crc = reader.ReadUInt32();
  54. _lastEntryHeader.CompressedSize = zip64 ? (long)reader.ReadUInt64() : reader.ReadUInt32();
  55. _lastEntryHeader.UncompressedSize = zip64 ? (long)reader.ReadUInt64() : reader.ReadUInt32();
  56. }
  57. else
  58. {
  59. reader.ReadBytes(zip64 ? 20 : 12);
  60. }
  61. return null;
  62. }
  63. case DIGITAL_SIGNATURE:
  64. return null;
  65. case DIRECTORY_END_HEADER_BYTES:
  66. {
  67. var entry = new DirectoryEndHeader();
  68. entry.Read(reader);
  69. return entry;
  70. }
  71. case SPLIT_ARCHIVE_HEADER_BYTES:
  72. {
  73. return new SplitHeader();
  74. }
  75. case ZIP64_END_OF_CENTRAL_DIRECTORY:
  76. {
  77. var entry = new Zip64DirectoryEndHeader();
  78. entry.Read(reader);
  79. return entry;
  80. }
  81. case ZIP64_END_OF_CENTRAL_DIRECTORY_LOCATOR:
  82. {
  83. var entry = new Zip64DirectoryEndLocatorHeader();
  84. entry.Read(reader);
  85. return entry;
  86. }
  87. default:
  88. throw new NotSupportedException("Unknown header: " + headerBytes);
  89. }
  90. }
  91. internal static bool IsHeader(uint headerBytes)
  92. {
  93. switch (headerBytes)
  94. {
  95. case ENTRY_HEADER_BYTES:
  96. case DIRECTORY_START_HEADER_BYTES:
  97. case POST_DATA_DESCRIPTOR:
  98. case DIGITAL_SIGNATURE:
  99. case DIRECTORY_END_HEADER_BYTES:
  100. case SPLIT_ARCHIVE_HEADER_BYTES:
  101. case ZIP64_END_OF_CENTRAL_DIRECTORY:
  102. case ZIP64_END_OF_CENTRAL_DIRECTORY_LOCATOR:
  103. return true;
  104. default:
  105. return false;
  106. }
  107. }
  108. private void LoadHeader(ZipFileEntry entryHeader, Stream stream)
  109. {
  110. if (FlagUtility.HasFlag(entryHeader.Flags, HeaderFlags.Encrypted))
  111. {
  112. if (!entryHeader.IsDirectory && entryHeader.CompressedSize == 0 &&
  113. FlagUtility.HasFlag(entryHeader.Flags, HeaderFlags.UsePostDataDescriptor))
  114. {
  115. throw new NotSupportedException("SharpCompress cannot currently read non-seekable Zip Streams with encrypted data that has been written in a non-seekable manner.");
  116. }
  117. if (_password == null)
  118. {
  119. throw new CryptographicException("No password supplied for encrypted zip.");
  120. }
  121. entryHeader.Password = _password;
  122. if (entryHeader.CompressionMethod == ZipCompressionMethod.WinzipAes)
  123. {
  124. #if NO_CRYPTO
  125. throw new NotSupportedException("Cannot decrypt Winzip AES with Silverlight or WP7.");
  126. #else
  127. ExtraData data = entryHeader.Extra.SingleOrDefault(x => x.Type == ExtraDataType.WinZipAes);
  128. if (data != null)
  129. {
  130. var keySize = (WinzipAesKeySize)data.DataBytes[4];
  131. var salt = new byte[WinzipAesEncryptionData.KeyLengthInBytes(keySize) / 2];
  132. var passwordVerifyValue = new byte[2];
  133. stream.Read(salt, 0, salt.Length);
  134. stream.Read(passwordVerifyValue, 0, 2);
  135. entryHeader.WinzipAesEncryptionData =
  136. new WinzipAesEncryptionData(keySize, salt, passwordVerifyValue, _password);
  137. entryHeader.CompressedSize -= (uint)(salt.Length + 2);
  138. }
  139. #endif
  140. }
  141. }
  142. if (entryHeader.IsDirectory)
  143. {
  144. return;
  145. }
  146. //if (FlagUtility.HasFlag(entryHeader.Flags, HeaderFlags.UsePostDataDescriptor))
  147. //{
  148. // entryHeader.PackedStream = new ReadOnlySubStream(stream);
  149. //}
  150. //else
  151. //{
  152. switch (_mode)
  153. {
  154. case StreamingMode.Seekable:
  155. {
  156. entryHeader.DataStartPosition = stream.Position;
  157. stream.Position += entryHeader.CompressedSize;
  158. break;
  159. }
  160. case StreamingMode.Streaming:
  161. {
  162. entryHeader.PackedStream = stream;
  163. break;
  164. }
  165. default:
  166. {
  167. throw new InvalidFormatException("Invalid StreamingMode");
  168. }
  169. }
  170. //}
  171. }
  172. }
  173. }