zhao
2021-06-04 c7ec496f9e41c2227103b3ef776e4a3f91bce6b2
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
/* ====================================================================
   Licensed to the Apache Software Foundation (ASF) under one or more
   contributor license agreements.  See the NOTICE file distributed with
   this work for Additional information regarding copyright ownership.
   The ASF licenses this file to You under the Apache License, Version 2.0
   (the "License"); you may not use this file except in compliance with
   the License.  You may obtain a copy of the License at
 
       http://www.apache.org/licenses/LICENSE-2.0
 
   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
==================================================================== */
 
namespace HH.WMS.Utils.NPOI.HSSF.Record.Crypto
{
    using System;
    using System.IO;
    using HH.WMS.Utils.NPOI.Util;
 
    using HH.WMS.Utils.NPOI.HSSF.Record;
 
    /**
     *
     * @author Josh Micich
     */
    public class Biff8DecryptingStream : BiffHeaderInput, ILittleEndianInput
    {
 
        private ILittleEndianInput _le;
        private Biff8RC4 _rc4;
 
        public Biff8DecryptingStream(Stream in1, int InitialOffSet, Biff8EncryptionKey key)
        {
            _rc4 = new Biff8RC4(InitialOffSet, key);
 
            if (in1 is ILittleEndianInput)
            {
                // accessing directly is an optimisation
                _le = (ILittleEndianInput)in1;
            }
            else
            {
                // less optimal, but should work OK just the same. Often occurs in junit tests.
                _le = new LittleEndianInputStream(in1);
            }
        }
 
        public int Available()
        {
            return _le.Available();
        }
 
        /**
         * Reads an unsigned short value without decrypting
         */
        public int ReadRecordSID()
        {
            int sid = _le.ReadUShort();
            _rc4.SkipTwoBytes();
            _rc4.StartRecord(sid);
            return sid;
        }
 
        /**
         * Reads an unsigned short value without decrypting
         */
        public int ReadDataSize()
        {
            int dataSize = _le.ReadUShort();
            _rc4.SkipTwoBytes();
            return dataSize;
        }
 
        public double ReadDouble()
        {
            long valueLongBits = ReadLong();
            double result = BitConverter.Int64BitsToDouble(valueLongBits);
            if (Double.IsNaN(result))
            {
                throw new Exception("Did not expect to read NaN"); // (Because Excel typically doesn't write NaN
            }
            return result;
        }
 
        public void ReadFully(byte[] buf)
        {
            ReadFully(buf, 0, buf.Length);
        }
 
        public void ReadFully(byte[] buf, int off, int len)
        {
            _le.ReadFully(buf, off, len);
            _rc4.Xor(buf, off, len);
        }
 
 
        public int ReadUByte()
        {
            return _rc4.XorByte(_le.ReadUByte());
        }
        public int ReadByte()
        {
            return _rc4.XorByte(_le.ReadUByte());
        }
 
 
        public int ReadUShort()
        {
            return _rc4.Xorshort(_le.ReadUShort());
        }
        public short ReadShort()
        {
            return (short)_rc4.Xorshort(_le.ReadUShort());
        }
 
        public int ReadInt()
        {
            return _rc4.XorInt(_le.ReadInt());
        }
 
        public long ReadLong()
        {
            return _rc4.XorLong(_le.ReadLong());
        }
    }
}