Unpack.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #if !Rar2017_64bit
  2. using nint = System.Int32;
  3. using nuint = System.UInt32;
  4. using size_t = System.UInt32;
  5. #else
  6. using nint = System.Int64;
  7. using nuint = System.UInt64;
  8. using size_t = System.UInt64;
  9. #endif
  10. using int64 = System.Int64;
  11. using System;
  12. using System.IO;
  13. using SharpCompress.Common.Rar.Headers;
  14. using static SharpCompress.Compressors.Rar.UnpackV2017.PackDef;
  15. namespace SharpCompress.Compressors.Rar.UnpackV2017
  16. {
  17. internal partial class Unpack : IRarUnpack
  18. {
  19. private FileHeader fileHeader;
  20. private Stream readStream;
  21. private Stream writeStream;
  22. private void _UnpackCtor() {
  23. for (int i = 0; i < AudV.Length; i++) {
  24. AudV[i] = new AudioVariables();
  25. }
  26. }
  27. private int UnpIO_UnpRead(byte[] buf, int offset, int count) {
  28. // NOTE: caller has logic to check for -1 for error we throw instead.
  29. return readStream.Read(buf, offset, count);
  30. }
  31. private void UnpIO_UnpWrite(byte[] buf, size_t offset, uint count) {
  32. writeStream.Write(buf, checked((int)offset), checked((int)count));
  33. }
  34. public void DoUnpack(FileHeader fileHeader, Stream readStream, Stream writeStream)
  35. {
  36. // as of 12/2017 .NET limits array indexing to using a signed integer
  37. // MaxWinSize causes unpack to use a fragmented window when the file
  38. // window size exceeds MaxWinSize
  39. // uggh, that's not how this variable is used, it's the size of the currently allocated window buffer
  40. //x MaxWinSize = ((uint)int.MaxValue) + 1;
  41. // may be long.MaxValue which could indicate unknown size (not present in header)
  42. DestUnpSize = fileHeader.UncompressedSize;
  43. this.fileHeader = fileHeader;
  44. this.readStream = readStream;
  45. this.writeStream = writeStream;
  46. if (!fileHeader.IsStored) {
  47. Init(fileHeader.WindowSize, fileHeader.IsSolid);
  48. }
  49. Suspended = false;
  50. DoUnpack();
  51. }
  52. public void DoUnpack()
  53. {
  54. if (fileHeader.IsStored)
  55. {
  56. UnstoreFile();
  57. } else {
  58. DoUnpack(fileHeader.CompressionAlgorithm, fileHeader.IsSolid);
  59. }
  60. }
  61. private void UnstoreFile()
  62. {
  63. var b = new byte[0x10000];
  64. do {
  65. int n = readStream.Read(b, 0, (int)Math.Min(b.Length, DestUnpSize));
  66. if (n == 0)
  67. {
  68. break;
  69. }
  70. writeStream.Write(b, 0, n);
  71. DestUnpSize -= n;
  72. } while (!Suspended);
  73. }
  74. public bool Suspended { get; set; }
  75. public long DestSize { get => DestUnpSize; }
  76. public int Char
  77. {
  78. get
  79. {
  80. // TODO: coderb: not sure where the "MAXSIZE-30" comes from, ported from V1 code
  81. if (InAddr > MAX_SIZE - 30)
  82. {
  83. UnpReadBuf();
  84. }
  85. return InBuf[InAddr++];
  86. }
  87. }
  88. public int PpmEscChar { get => PPMEscChar; set => PPMEscChar = value; }
  89. public static byte[] EnsureCapacity(byte[] array, int length) {
  90. return array.Length < length ? new byte[length] : array;
  91. }
  92. }
  93. }