File size: 4,682 Bytes
fab29d7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Copyright (c) Dr. Dirk Lellinger. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SlobViewer.Common;

namespace SlobViewer.Slob
{
  public class StoreItemBase
  {
    protected string[] _content;
    protected byte[] _compressedContent;
    protected byte[] _contentIds;

    protected void DecompressContent(Encoding encoding, string compressionMethod, byte[] buffer)
    {
      Func<Stream, Stream> createInflaterStream = null;
      switch (compressionMethod)
      {
        case "lzma":
        case "lzma2":
          {
            createInflaterStream = (inStream) =>
            {
              var outStream = new MemoryStream();

              int iDictionaryByte = 30;
              int dictionarySize = iDictionaryByte == 40 ? -1 : (2 + iDictionaryByte % 2) << (11 + iDictionaryByte / 2);


              inStream.Read(buffer, 0, 6); // Read 6 header bytes. currently, we do not know exactly what those header bytes mean. Seems that byte[5] is the encoding prop, and byte[3] and [4] the compressed length
              byte[] properties = new byte[5];
              properties[0] = buffer[5];
              Array.Copy(BitConverter.GetBytes(dictionarySize), 0, properties, 1, 4); // copy dictionary size into array

              var decoder = new SevenZip.Compression.LZMA.Decoder();
              decoder.SetDecoderProperties(properties);
              long outSize = long.MaxValue;
              long compressedSize = inStream.Length - inStream.Position;
              decoder.Code(inStream, outStream, compressedSize, outSize, null);
              outStream.Seek(0, SeekOrigin.Begin);
              return outStream;
            };

            DecompressWithInflaterStream(createInflaterStream, encoding, buffer);
          }
          break;
        case "zlib":
          createInflaterStream = (s) => new ICSharpCode.SharpZipLib.Zip.Compression.Streams.InflaterInputStream(s);
          DecompressWithInflaterStream(createInflaterStream, encoding, buffer);
          break;
        case "bz2":
        case "bzip2":
          createInflaterStream = (s) => new ICSharpCode.SharpZipLib.BZip2.BZip2InputStream(s);
          DecompressWithInflaterStream(createInflaterStream, encoding, buffer);
          break;
        case "gzip":
          createInflaterStream = (s) => new ICSharpCode.SharpZipLib.GZip.GZipInputStream(s);
          DecompressWithInflaterStream(createInflaterStream, encoding, buffer);
          break;
        case "lzw":
          createInflaterStream = (s) => new ICSharpCode.SharpZipLib.Lzw.LzwInputStream(s);
          DecompressWithInflaterStream(createInflaterStream, encoding, buffer);
          break;
        default:
          throw new NotImplementedException();
      }
    }

    protected void DecompressWithInflaterStream(Func<Stream, Stream> createDeflaterStream, Encoding encoding, byte[] buffer)
    {
      _content = _content ?? new string[_contentIds.Length];
      using (var inStream = new MemoryStream(_compressedContent))
      {

        using (var outStream = createDeflaterStream(inStream))
        {
          var offsets = new uint[_contentIds.Length];

          for (int i = 0; i < _contentIds.Length; ++i)
          {
            outStream.Read(buffer, 0, 4);
            offsets[i] = BigEndianBitConverter.ToUInt32(buffer, 0);
          }

          long position = 4 * _contentIds.Length;
          long positionAfterTable = position;

          for (int i = 0; i < _contentIds.Length; ++i)
          {
            var toSkip = position - (offsets[i] + positionAfterTable);
            if (0 != toSkip)
            {
              Skip(outStream, toSkip, buffer);
            }
            else if (toSkip < 0)
            {
              throw new InvalidOperationException("Can not go backwards");
            }
            try
            {
              _content[i] = BigEndianBitConverter.ReadBigText(outStream, encoding, buffer, ref position);
            }
            catch (Exception e)
            {
              System.Diagnostics.Debugger.Launch();

              _content[i] = "Content is not available because lzma2 decompression reached end before position were content was supposed to be";
            }
          }
        }
      }
    }

    void Skip(Stream stream, long count, byte[] buffer)
    {
      var l = buffer.Length;
      while (count > 0)
      {

        var rd = stream.Read(buffer, 0, (int)Math.Min(l, count));
        count -= rd;
      }
    }

  }
}