zhao
2021-07-02 23ee356c6f260ecc1a48bbb8bd60932b979e4698
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/* ====================================================================
   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.Aggregates
{
 
    using HH.WMS.Utils.NPOI.HSSF.Model;
    using HH.WMS.Utils.NPOI.HSSF.Record;
    using HH.WMS.Utils.NPOI.Util;
    using System;
 
    /**
     * Groups the sheet protection records for a worksheet.
     * <p/>
     *
     * See OOO excelfileformat.pdf sec 4.18.2 'Sheet Protection in a Workbook
     * (BIFF5-BIFF8)'
     *
     * @author Josh Micich
     */
    public class WorksheetProtectionBlock : RecordAggregate
    {
        // Every one of these component records is optional
        // (The whole WorksheetProtectionBlock may not be present)
        private ProtectRecord _protectRecord;
        private ObjectProtectRecord _objectProtectRecord;
        private ScenarioProtectRecord _scenarioProtectRecord;
        private PasswordRecord _passwordRecord;
 
        /**
         * Creates an empty WorksheetProtectionBlock
         */
        public WorksheetProtectionBlock()
        {
            // all fields emptyv
        }
 
        /**
         * @return <c>true</c> if the specified Record sid is one belonging to
         *         the 'Page Settings Block'.
         */
        public static bool IsComponentRecord(int sid)
        {
            switch (sid)
            {
                case ProtectRecord.sid:
                case ObjectProtectRecord.sid:
                case ScenarioProtectRecord.sid:
                case PasswordRecord.sid:
                    return true;
            }
            return false;
        }
 
        private bool ReadARecord(RecordStream rs)
        {
            
            switch (rs.PeekNextSid())
            {
                case ProtectRecord.sid:
                    CheckNotPresent(_protectRecord);
                    _protectRecord = rs.GetNext() as ProtectRecord;
                    break;
                case ObjectProtectRecord.sid:
                    CheckNotPresent(_objectProtectRecord);
                    _objectProtectRecord = rs.GetNext() as ObjectProtectRecord;
                    break;
                case ScenarioProtectRecord.sid:
                    CheckNotPresent(_scenarioProtectRecord);
                    _scenarioProtectRecord = rs.GetNext() as ScenarioProtectRecord;
                    break;
                case PasswordRecord.sid:
                    CheckNotPresent(_passwordRecord);
                    _passwordRecord = rs.GetNext() as PasswordRecord;
                    break;
                default:
                    // all other record types are not part of the PageSettingsBlock
                    return false;
            }
            return true;
        }
 
        private void CheckNotPresent(Record rec)
        {
            if (rec != null)
            {
                throw new RecordFormatException("Duplicate WorksheetProtectionBlock record (sid=0x"
                        + StringUtil.ToHexString(rec.Sid) + ")");
            }
        }
 
        public override void VisitContainedRecords(RecordVisitor rv)
        {
            // Replicates record order from Excel 2007, though this is not critical
 
            VisitIfPresent(_protectRecord, rv);
            VisitIfPresent(_objectProtectRecord, rv);
            VisitIfPresent(_scenarioProtectRecord, rv);
            VisitIfPresent(_passwordRecord, rv);
        }
 
        private static void VisitIfPresent(Record r, RecordVisitor rv)
        {
            if (r != null)
            {
                rv.VisitRecord(r);
            }
        }
 
        public PasswordRecord GetPasswordRecord()
        {
            return _passwordRecord;
        }
 
        public ScenarioProtectRecord GetHCenter()
        {
            return _scenarioProtectRecord;
        }
 
        /**
         * This method Reads {@link WorksheetProtectionBlock} records from the supplied RecordStream
         * until the first non-WorksheetProtectionBlock record is encountered. As each record is Read,
         * it is incorporated into this WorksheetProtectionBlock.
         * <p/>
         * As per the OOO documentation, the protection block records can be expected to be written
         * toGether (with no intervening records), but earlier versions of POI (prior to Jun 2009)
         * didn't do this.  Workbooks with sheet protection Created by those earlier POI versions
         * seemed to be valid (Excel opens them OK). So PO allows continues to support Reading of files
         * with non continuous worksheet protection blocks.
         *
         * <p/>
         * <b>Note</b> - when POI Writes out this WorksheetProtectionBlock, the records will always be
         * written in one consolidated block (in the standard ordering) regardless of how scattered the
         * records were when they were originally Read.
         */
        public void AddRecords(RecordStream rs)
        {
            while (true)
            {
                if (!ReadARecord(rs))
                {
                    break;
                }
            }
        }
 
        /// <summary>
        /// the ProtectRecord. If one is not contained in the sheet, then one is created.
        /// </summary>
        private ProtectRecord Protect
        {
            get
            {
                if (_protectRecord == null)
                {
                    _protectRecord = new ProtectRecord(false);
                }
                return _protectRecord;
            }
        }
        /// <summary>
        /// the PasswordRecord. If one is not Contained in the sheet, then one is Created.
        /// </summary>
        public PasswordRecord Password
        {
            get
            {
                if (_passwordRecord == null)
                {
                    _passwordRecord = CreatePassword();
                }
                return _passwordRecord;
            }
        }
 
        /// <summary>
        /// protect a spreadsheet with a password (not encrypted, just sets protect flags and the password.)
        /// </summary>
        /// <param name="password">password to set;Pass <code>null</code> to remove all protection</param>
        /// <param name="shouldProtectObjects">shouldProtectObjects are protected</param>
        /// <param name="shouldProtectScenarios">shouldProtectScenarios are protected</param>
        public void ProtectSheet(String password, bool shouldProtectObjects,
                bool shouldProtectScenarios)
        {
            if (password == null)
            {
                _passwordRecord = null;
                _protectRecord = null;
                _objectProtectRecord = null;
                _scenarioProtectRecord = null;
                return;
            }
 
            ProtectRecord prec = this.Protect;
            PasswordRecord pass = this.Password;
            prec.Protect = true;
            pass.Password = (PasswordRecord.HashPassword(password));
            if (_objectProtectRecord == null && shouldProtectObjects)
            {
                ObjectProtectRecord rec = CreateObjectProtect();
                rec.Protect = (true);
                _objectProtectRecord = rec;
            }
            if (_scenarioProtectRecord == null && shouldProtectScenarios)
            {
                ScenarioProtectRecord srec = CreateScenarioProtect();
                srec.Protect = (true);
                _scenarioProtectRecord = srec;
            }
        }
 
        public bool IsSheetProtected
        {
            get
            {
                return _protectRecord != null && _protectRecord.Protect;
            }
        }
 
        public bool IsObjectProtected
        {
            get{
                return _objectProtectRecord != null && _objectProtectRecord.Protect;
            }
        }
 
        public bool IsScenarioProtected
        {
            get
            {
                return _scenarioProtectRecord != null && _scenarioProtectRecord.Protect;
            }
        }
 
        /// <summary>
        /// Creates an ObjectProtect record with protect set to false.
        /// </summary>
        /// <returns></returns>
        private static ObjectProtectRecord CreateObjectProtect() {
        ObjectProtectRecord retval = new ObjectProtectRecord();
        retval.Protect = (false);
        return retval;
    }
        /// <summary>
        /// Creates a ScenarioProtect record with protect set to false.
        /// </summary>
        /// <returns></returns>
        private static ScenarioProtectRecord CreateScenarioProtect() {
        ScenarioProtectRecord retval = new ScenarioProtectRecord();
        retval.Protect = (false);
        return retval;
    }
 
        /// <summary>
        ///Creates a Password record with password set to 0x0000.
        /// </summary>
        /// <returns></returns>
        private static PasswordRecord CreatePassword()
        {
            return new PasswordRecord(0x0000);
        }
 
        public int PasswordHash
        {
            get
            {
                if (_passwordRecord == null)
                {
                    return 0;
                }
                return _passwordRecord.Password;
            }
        }
    }
}