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
/* ====================================================================
   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;
 
    using System.IO;
    using System.Collections;
    using System.Collections.Generic;
 
 
    /**
     * The obj record is used to hold various graphic objects and controls.
     *
     * @author Glen Stampoultzis (glens at apache.org)
     */
    public class ObjRecord : Record
    {
        private static int NORMAL_PAD_ALIGNMENT = 2;
        private static int MAX_PAD_ALIGNMENT = 4;
 
        public const short sid = 0x5D;
        private List<SubRecord> subrecords;
            /** used when POI has no idea what is going on */
        private byte[] _uninterpretedData;
    /**
* Excel seems to tolerate padding to quad or double byte length
*/
        private bool _isPaddedToQuadByteMultiple;
 
        //00000000 15 00 12 00 01 00 01 00 11 60 00 00 00 00 00 0D .........`......
        //00000010 26 01 00 00 00 00 00 00 00 00                   &.........
 
 
        public ObjRecord()
        {
            subrecords = new List<SubRecord>(2);
            // TODO - ensure 2 sub-records (ftCmo 15h, and ftEnd 00h) are always created
            _uninterpretedData = null;
        }
 
        /**
         * Constructs a OBJ record and Sets its fields appropriately.
         *
         * @param in the RecordInputstream to Read the record from
         */
 
        public ObjRecord(RecordInputStream in1)
        {
            // TODO - problems with OBJ sub-records stream
            // MS spec says first sub-record is always CommonObjectDataSubRecord,
            // and last is
            // always EndSubRecord. OOO spec does not mention ObjRecord(0x005D).
            // Existing POI test data seems to violate that rule. Some test data
            // seems to contain
            // garbage, and a crash is only averted by stopping at what looks like
            // the 'EndSubRecord'
 
            //Check if this can be continued, if so then the
            //following wont work properly
            //int subSize = 0;
            byte[] subRecordData = in1.ReadRemainder();
 
            if (LittleEndian.GetUShort(subRecordData, 0) != CommonObjectDataSubRecord.sid)
            {
                // seems to occur in just one junit on "OddStyleRecord.xls" (file created by CrystalReports)
                // Excel tolerates the funny ObjRecord, and replaces it with a corrected version
                // The exact logic/reasoning is not yet understood
                _uninterpretedData = subRecordData;
                subrecords = null;
                return;
            }
            //if (subRecordData.Length % 2 != 0)
            //{
            //    String msg = "Unexpected length of subRecordData : " + HexDump.ToHex(subRecordData);
            //    throw new RecordFormatException(msg);
            //}
            subrecords = new List<SubRecord>();
            using (MemoryStream bais = new MemoryStream(subRecordData))
            {
                LittleEndianInputStream subRecStream = new LittleEndianInputStream(bais);
                CommonObjectDataSubRecord cmo = (CommonObjectDataSubRecord)SubRecord.CreateSubRecord(subRecStream, 0);
                subrecords.Add(cmo);
                while (true)
                {
                    SubRecord subRecord = SubRecord.CreateSubRecord(subRecStream, cmo.ObjectType);
                    subrecords.Add(subRecord);
                    if (subRecord.IsTerminating)
                    {
                        break;
                    }
                }
                int nRemainingBytes = subRecStream.Available();
                if (nRemainingBytes > 0)
                {
                    // At present (Oct-2008), most unit test samples have (subRecordData.length % 2 == 0)
                    _isPaddedToQuadByteMultiple = subRecordData.Length % MAX_PAD_ALIGNMENT == 0;
                    if (nRemainingBytes >= (_isPaddedToQuadByteMultiple ? MAX_PAD_ALIGNMENT : NORMAL_PAD_ALIGNMENT))
                    {
                        if (!CanPaddingBeDiscarded(subRecordData, nRemainingBytes))
                        {
                            String msg = "Leftover " + nRemainingBytes
                                + " bytes in subrecord data " + HexDump.ToHex(subRecordData);
                            throw new RecordFormatException(msg);
                        }
                        _isPaddedToQuadByteMultiple = false;
                    }
                }
                else
                {
                    _isPaddedToQuadByteMultiple = false;
                }
                _uninterpretedData = null;
            }
        }
        /**
     * Some XLS files have ObjRecords with nearly 8Kb of excessive padding. These were probably
     * written by a version of POI (around 3.1) which incorrectly interpreted the second short of
     * the ftLbs subrecord (0x1FEE) as a length, and read that many bytes as padding (other bugs
     * helped allow this to occur).
     * 
     * Excel reads files with this excessive padding OK, truncating the over-sized ObjRecord back
     * to the its proper size.  POI does the same.
     */
        private static bool CanPaddingBeDiscarded(byte[] data, int nRemainingBytes)
        {
            // make sure none of the padding looks important
            for (int i = data.Length - nRemainingBytes; i < data.Length; i++)
            {
                if (data[i] != 0x00)
                {
                    return false;
                }
            }
            return true;
        }
 
        public override String ToString()
        {
            StringBuilder sb = new StringBuilder();
 
            sb.Append("[OBJ]\n");
            for (int i = 0; i < subrecords.Count; i++)
            {
                SubRecord record = subrecords[i];
                sb.Append("SUBRECORD: ").Append(record.ToString());
            }
            sb.Append("[/OBJ]\n");
            return sb.ToString();
        }
 
        public override int Serialize(int offset, byte [] data)
        {
            int recSize = RecordSize;
            int dataSize = recSize - 4;
 
            LittleEndianByteArrayOutputStream out1 = new LittleEndianByteArrayOutputStream(data, offset, recSize);
 
            out1.WriteShort(sid);
            out1.WriteShort(dataSize);
 
            if (_uninterpretedData == null)
            {
                for (int i = 0; i < subrecords.Count; i++)
                {
                    SubRecord record = subrecords[i];
                    record.Serialize(out1);
                }
                int expectedEndIx = offset + dataSize;
                // padding
                while (out1.WriteIndex < expectedEndIx)
                {
                    out1.WriteByte(0);
                }
            }
            else
            {
                out1.Write(_uninterpretedData);
            }
            return recSize;
        }
 
        /**
         * Size of record (excluding 4 byte header)
         */
        public override int RecordSize
        {
            get
            {
                if (_uninterpretedData != null)
                {
                    return _uninterpretedData.Length + 4;
                }
                int size = 0;
                for (int i = subrecords.Count - 1; i >= 0; i--)
                {
                    SubRecord record = subrecords[i];
                    size += record.DataSize + 4;
                }
                if (_isPaddedToQuadByteMultiple)
                {
                    while (size % MAX_PAD_ALIGNMENT != 0)
                    {
                        size++;
                    }
                }
                else
                {
                    while (size % NORMAL_PAD_ALIGNMENT != 0)
                    {
                        size++;
                    }
                }
                return size + 4;
            }
        }
 
        public override short Sid
        {
            get { return sid; }
        }
 
        public List<SubRecord> SubRecords
        {
            get
            {
                return subrecords;
            }
        }
 
        public void ClearSubRecords()
        {
            subrecords.Clear();
        }
 
        public void AddSubRecord(int index, SubRecord element)
        {
            subrecords.Insert(index, element);
        }
 
        public void AddSubRecord(SubRecord o)
        {
            subrecords.Add(o);
        }
 
        public override Object Clone()
        {
            ObjRecord rec = new ObjRecord();
 
            for (int i = 0; i < subrecords.Count; i++)
            {
                SubRecord record = subrecords[i];
                rec.AddSubRecord((SubRecord)record.Clone());
            }
 
            return rec;
        }
    }
}