RarHeaderFactory.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using SharpCompress.IO;
  5. using SharpCompress.Readers;
  6. namespace SharpCompress.Common.Rar.Headers
  7. {
  8. internal class RarHeaderFactory
  9. {
  10. private bool _isRar5;
  11. internal RarHeaderFactory(StreamingMode mode, ReaderOptions options)
  12. {
  13. StreamingMode = mode;
  14. Options = options;
  15. }
  16. private ReaderOptions Options { get; }
  17. internal StreamingMode StreamingMode { get; }
  18. internal bool IsEncrypted { get; private set; }
  19. internal IEnumerable<IRarHeader> ReadHeaders(Stream stream)
  20. {
  21. var markHeader = MarkHeader.Read(stream, Options.LeaveStreamOpen, Options.LookForHeader);
  22. _isRar5 = markHeader.IsRar5;
  23. yield return markHeader;
  24. RarHeader header;
  25. while ((header = TryReadNextHeader(stream)) != null)
  26. {
  27. yield return header;
  28. if (header.HeaderType == HeaderType.EndArchive)
  29. {
  30. // End of archive marker. RAR does not read anything after this header letting to use third
  31. // party tools to add extra information such as a digital signature to archive.
  32. yield break;
  33. }
  34. }
  35. }
  36. private RarHeader TryReadNextHeader(Stream stream)
  37. {
  38. RarCrcBinaryReader reader;
  39. if (!IsEncrypted)
  40. {
  41. reader = new RarCrcBinaryReader(stream);
  42. }
  43. else
  44. {
  45. #if !NO_CRYPTO
  46. if (Options.Password == null)
  47. {
  48. throw new CryptographicException("Encrypted Rar archive has no password specified.");
  49. }
  50. reader = new RarCryptoBinaryReader(stream, Options.Password);
  51. #else
  52. throw new CryptographicException("Rar encryption unsupported on this platform");
  53. #endif
  54. }
  55. var header = RarHeader.TryReadBase(reader, _isRar5, Options.ArchiveEncoding);
  56. if (header == null)
  57. {
  58. return null;
  59. }
  60. switch (header.HeaderCode)
  61. {
  62. case HeaderCodeV.RAR5_ARCHIVE_HEADER:
  63. case HeaderCodeV.RAR4_ARCHIVE_HEADER:
  64. {
  65. var ah = new ArchiveHeader(header, reader);
  66. if (ah.IsEncrypted == true)
  67. {
  68. //!!! rar5 we don't know yet
  69. IsEncrypted = true;
  70. }
  71. return ah;
  72. }
  73. case HeaderCodeV.RAR4_PROTECT_HEADER:
  74. {
  75. var ph = new ProtectHeader(header, reader);
  76. // skip the recovery record data, we do not use it.
  77. switch (StreamingMode)
  78. {
  79. case StreamingMode.Seekable:
  80. {
  81. reader.BaseStream.Position += ph.DataSize;
  82. }
  83. break;
  84. case StreamingMode.Streaming:
  85. {
  86. reader.BaseStream.Skip(ph.DataSize);
  87. }
  88. break;
  89. default:
  90. {
  91. throw new InvalidFormatException("Invalid StreamingMode");
  92. }
  93. }
  94. return ph;
  95. }
  96. case HeaderCodeV.RAR5_SERVICE_HEADER:
  97. {
  98. var fh = new FileHeader(header, reader, HeaderType.Service);
  99. SkipData(fh, reader);
  100. return fh;
  101. }
  102. case HeaderCodeV.RAR4_NEW_SUB_HEADER:
  103. {
  104. var fh = new FileHeader(header, reader, HeaderType.NewSub);
  105. SkipData(fh, reader);
  106. return fh;
  107. }
  108. case HeaderCodeV.RAR5_FILE_HEADER:
  109. case HeaderCodeV.RAR4_FILE_HEADER:
  110. {
  111. var fh = new FileHeader(header, reader, HeaderType.File);
  112. switch (StreamingMode)
  113. {
  114. case StreamingMode.Seekable:
  115. {
  116. fh.DataStartPosition = reader.BaseStream.Position;
  117. reader.BaseStream.Position += fh.CompressedSize;
  118. }
  119. break;
  120. case StreamingMode.Streaming:
  121. {
  122. var ms = new ReadOnlySubStream(reader.BaseStream, fh.CompressedSize);
  123. if (fh.R4Salt == null)
  124. {
  125. fh.PackedStream = ms;
  126. }
  127. else
  128. {
  129. #if !NO_CRYPTO
  130. fh.PackedStream = new RarCryptoWrapper(ms, Options.Password, fh.R4Salt);
  131. #else
  132. throw new NotSupportedException("RarCrypto not supported");
  133. #endif
  134. }
  135. }
  136. break;
  137. default:
  138. {
  139. throw new InvalidFormatException("Invalid StreamingMode");
  140. }
  141. }
  142. return fh;
  143. }
  144. case HeaderCodeV.RAR5_END_ARCHIVE_HEADER:
  145. case HeaderCodeV.RAR4_END_ARCHIVE_HEADER:
  146. {
  147. return new EndArchiveHeader(header, reader);
  148. }
  149. case HeaderCodeV.RAR5_ARCHIVE_ENCRYPTION_HEADER:
  150. {
  151. var ch = new ArchiveCryptHeader(header, reader);
  152. IsEncrypted = true;
  153. return ch;
  154. }
  155. default:
  156. {
  157. throw new InvalidFormatException("Unknown Rar Header: " + header.HeaderCode);
  158. }
  159. }
  160. }
  161. private void SkipData(FileHeader fh, RarCrcBinaryReader reader) {
  162. switch (StreamingMode) {
  163. case StreamingMode.Seekable: {
  164. fh.DataStartPosition = reader.BaseStream.Position;
  165. reader.BaseStream.Position += fh.CompressedSize;
  166. }
  167. break;
  168. case StreamingMode.Streaming: {
  169. //skip the data because it's useless?
  170. reader.BaseStream.Skip(fh.CompressedSize);
  171. }
  172. break;
  173. default: {
  174. throw new InvalidFormatException("Invalid StreamingMode");
  175. }
  176. }
  177. }
  178. }
  179. }