zhao
2021-07-09 0821715ebc11d3934d0594a1cc2c39686d808906
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/* ====================================================================
   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
{
    using System;
    using System.Text;
    using HH.WMS.Utils.NPOI.Util;
 
    /**
     * Title:        FILESHARING
     * Description:  stores the encrypted Readonly for a workbook (Write protect) 
     * This functionality Is accessed from the options dialog box available when performing 'Save As'.<p/>
     * REFERENCE:  PG 314 Microsoft Excel 97 Developer's Kit (ISBN: 1-57231-498-2)<p/>
     * @author Andrew C. Oliver (acoliver at apache dot org)
     */
    public class FileSharingRecord : StandardRecord
    {
 
        public const short sid = 0x5b;
        private short field_1_Readonly;
        private short field_2_password;
        private byte field_3_username_unicode_options;
        private String field_3_username_value;
 
        public FileSharingRecord() { }
 
 
        /**
         * Constructs a FileSharing record and Sets its fields appropriately.
         * @param in the RecordInputstream to Read the record from
         */
 
        public FileSharingRecord(RecordInputStream in1)
        {
            field_1_Readonly = in1.ReadShort();
            field_2_password = in1.ReadShort();
 
            int nameLen = in1.ReadShort();
 
            if (nameLen > 0)
            {
                // TODO - Current examples(3) from junits only have zero Length username. 
                field_3_username_unicode_options = (byte)in1.ReadByte();
                field_3_username_value = in1.ReadCompressedUnicode(nameLen);
            }
            else
            {
                field_3_username_value = "";
            }
        }
 
 
        //this Is the world's lamest "security".  thanks to Wouter van Vugt for making me
        //not have to try real hard.  -ACO
        public static short HashPassword(String password)
        {
            byte[] passwordChars = Encoding.UTF8.GetBytes(password);
            int hash = 0;
            if (passwordChars.Length > 0)
            {
                int charIndex = passwordChars.Length;
                while (charIndex-- > 0)
                {
                    hash = ((hash >> 14) & 0x01) | ((hash << 1) & 0x7fff);
                    hash ^= passwordChars[charIndex];
                }
                // also hash with charcount
                hash = ((hash >> 14) & 0x01) | ((hash << 1) & 0x7fff);
                hash ^= passwordChars.Length;
                hash ^= (0x8000 | ('N' << 8) | 'K');
            }
            return (short)hash;
        }
 
 
        /**
         * Get the Readonly
         *
         * @return short  representing if this Is Read only (1 = true)
         */
        public short ReadOnly
        {
            get
            {
                return field_1_Readonly;
            }
            set { field_1_Readonly = value; }
        }
 
        /**
         * @returns password hashed with hashPassword() (very lame)
         */
        public short Password
        {
            get
            {
                return field_2_password;
            }
            set { field_2_password = value; }
        }
 
 
        /**
         * @returns username of the user that Created the file
         */
        public String Username
        {
            get { return field_3_username_value; }
            set { field_3_username_value = value; }
        }
 
        public override String ToString()
        {
            StringBuilder buffer = new StringBuilder();
 
            buffer.Append("[FILESHARING]\n");
            buffer.Append("    .Readonly       = ")
                .Append(ReadOnly == 1 ? "true" : "false").Append("\n");
            buffer.Append("    .password       = ")
                .Append(StringUtil.ToHexString(Password)).Append("\n");
            buffer.Append("    .username       = ")
                .Append(Username).Append("\n");
            buffer.Append("[/FILESHARING]\n");
            return buffer.ToString();
        }
 
        public override void Serialize(ILittleEndianOutput out1)
        {
            // TODO - junit
            out1.WriteShort(ReadOnly);
            out1.WriteShort(Password);
            out1.WriteShort(field_3_username_value.Length);
            if (field_3_username_value.Length > 0)
            {
                out1.WriteByte(field_3_username_unicode_options);
                StringUtil.PutCompressedUnicode(Username, out1);
            }
        }
 
        protected override int DataSize
        {
            get
            {
                int nameLen = field_3_username_value.Length;
                if (nameLen < 1)
                {
                    return 6;
                }
                return 7 + nameLen;
            }
        }
 
        public override short Sid
        {
            get
            {
                return sid;
            }
        }
 
        /**
         * Clone this record.
         */
        public override Object Clone()
        {
            FileSharingRecord Clone = new FileSharingRecord();
            Clone.ReadOnly = field_1_Readonly;
            Clone.Password = field_2_password;
            Clone.Username = field_3_username_value;
            return Clone;
        }
    }
}