zhao
2021-07-19 8347f2fbddbd25369359dcb2da1233ac48a19fdc
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
 
/* ====================================================================
   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.DDF
{
    using System;
    using System.IO;
    using System.Text;
    using System.Collections;
    using HH.WMS.Utils.NPOI.Util;
 
 
    /// <summary>
    /// The BSE record is related closely to the EscherBlipRecord and stores
    /// extra information about the blip.  A blip record is actually stored inside
    /// the BSE record even though the BSE record isn't actually a container record.
    /// @author Glen Stampoultzis
    /// @see EscherBlipRecord
    /// </summary>
    public class EscherBSERecord : EscherRecord
    {
        public const short RECORD_ID = unchecked((short)0xF007);
        public const String RECORD_DESCRIPTION = "MsofbtBSE";
 
        public const byte BT_ERROR = 0;
        public const byte BT_UNKNOWN = 1;
        public const byte BT_EMF = 2;
        public const byte BT_WMF = 3;
        public const byte BT_PICT = 4;
        public const byte BT_JPEG = 5;
        public const byte BT_PNG = 6;
        public const byte BT_DIB = 7;
 
        private byte field_1_blipTypeWin32;
        private byte field_2_blipTypeMacOS;
        private byte[] field_3_uid;  // 16 bytes
        private short field_4_tag;
        private int field_5_size;
        private int field_6_ref;
        private int field_7_offset;
        private byte field_8_usage;
        private byte field_9_name;
        private byte field_10_unused2;
        private byte field_11_unused3;
        private EscherBlipRecord field_12_blipRecord;
 
        private byte[] _remainingData;
 
        /// <summary>
        /// This method deSerializes the record from a byte array.
        /// </summary>
        /// <param name="data">The byte array containing the escher record information</param>
        /// <param name="offset">The starting offset into data</param>
        /// <param name="recordFactory">May be null since this is not a container record.</param>
        /// <returns>The number of bytes Read from the byte array.</returns>
        public override int FillFields(byte[] data, int offset,
                                  EscherRecordFactory recordFactory
                                  )
        {
            int bytesRemaining = ReadHeader(data, offset);
            int pos = offset + 8;
            field_1_blipTypeWin32 = data[pos];
            field_2_blipTypeMacOS = data[pos + 1];
            field_3_uid = new byte[16];
            Array.Copy(data, pos + 2, field_3_uid, 0, 16);
            field_4_tag = LittleEndian.GetShort(data, pos + 18);
            field_5_size = LittleEndian.GetInt(data, pos + 20);
            field_6_ref = LittleEndian.GetInt(data, pos + 24);
            field_7_offset = LittleEndian.GetInt(data, pos + 28);
            field_8_usage = data[pos + 32];
            field_9_name = data[pos + 33];
            field_10_unused2 = data[pos + 34];
            field_11_unused3 = data[pos + 35];
            bytesRemaining -= 36;
            int bytesRead = 0;
            if (bytesRemaining > 0)
            {
                field_12_blipRecord = (EscherBlipRecord)recordFactory.CreateRecord(data, pos + 36);
                bytesRead = field_12_blipRecord.FillFields(data, pos + 36, recordFactory);
            }
            pos += 36 + bytesRead;
            bytesRemaining -= bytesRead;
 
            _remainingData = new byte[bytesRemaining];
            Array.Copy(data, pos, _remainingData, 0, bytesRemaining);
            return bytesRemaining + 8 + 36 + (field_12_blipRecord == null ? 0 : field_12_blipRecord.RecordSize);
 
        }
 
        /// <summary>
        /// This method Serializes this escher record into a byte array.  
        /// </summary>
        /// <param name="offset">The offset into 
        /// data to start writing the record data to</param>
        /// <param name="data">The byte array to Serialize to.</param>
        /// <param name="listener">a listener for begin and end serialization events.</param>
        /// <returns>The number of bytes written.</returns>
        public override int Serialize(int offset, byte[] data, EscherSerializationListener listener)
        {
            listener.BeforeRecordSerialize(offset, RecordId, this);
 
            if (_remainingData == null)
                _remainingData = new byte[0];
 
            LittleEndian.PutShort(data, offset, Options);
            LittleEndian.PutShort(data, offset + 2, RecordId);
            if (_remainingData == null) _remainingData = new byte[0];
            int blipSize = field_12_blipRecord == null ? 0 : field_12_blipRecord.RecordSize;
            int remainingBytes = _remainingData.Length + 36 + blipSize;
            LittleEndian.PutInt(data, offset + 4, remainingBytes);
 
            data[offset + 8] = field_1_blipTypeWin32;
            data[offset + 9] = field_2_blipTypeMacOS;
            for (int i = 0; i < 16; i++)
                data[offset + 10 + i] = field_3_uid[i];
            LittleEndian.PutShort(data, offset + 26, field_4_tag);
            LittleEndian.PutInt(data, offset + 28, field_5_size);
            LittleEndian.PutInt(data, offset + 32, field_6_ref);
            LittleEndian.PutInt(data, offset + 36, field_7_offset);
            data[offset + 40] = field_8_usage;
            data[offset + 41] = field_9_name;
            data[offset + 42] = field_10_unused2;
            data[offset + 43] = field_11_unused3;
            int bytesWritten = 0;
            if (field_12_blipRecord != null)
            {
                bytesWritten = field_12_blipRecord.Serialize(offset + 44, data);
            }
            if (_remainingData == null)
                _remainingData = new byte[0];
            Array.Copy(_remainingData, 0, data, offset + 44 + bytesWritten, _remainingData.Length);
            int pos = offset + 8 + 36 + _remainingData.Length + bytesWritten;
 
            listener.AfterRecordSerialize(pos, RecordId, pos - offset, this);
            return pos - offset;
        }
 
        /// <summary>
        /// Returns the number of bytes that are required to Serialize this record.
        /// </summary>
        /// <value>Number of bytes</value>
        public override int RecordSize
        {
            get 
            {
                int field_12_size = 0;
                if (field_12_blipRecord != null)
                {
                    field_12_size = field_12_blipRecord.RecordSize;
                }
                int remaining_size = 0;
                if (_remainingData != null)
                {
                    remaining_size = _remainingData.Length;
                }
                return 8 + 1 + 1 + 16 + 2 + 4 + 4 + 4 + 1 + 1 +
                    1 + 1 + field_12_size + remaining_size;            
            }
        }
 
        /// <summary>
        /// The short name for this record
        /// </summary>
        /// <value></value>
        public override String RecordName
        {
            get { return "BSE"; }
        }
 
        /// <summary>
        /// Gets or sets the expected blip type under windows (failure to match this blip type will result in
        /// Excel converting to this format).
        /// </summary>
        /// <value>The blip type win32.</value>
        public byte BlipTypeWin32
        {
            get { return field_1_blipTypeWin32; }
            set { field_1_blipTypeWin32 = value; }
        }
 
        /// <summary>
        /// Gets or sets the expected blip type under MacOS (failure to match this blip type will result in
        /// Excel converting to this format).
        /// </summary>
        /// <value>The blip type mac OS.</value>
        public byte BlipTypeMacOS
        {
            get { return field_2_blipTypeMacOS; }
            set { field_2_blipTypeMacOS = value; }
        }
 
 
        /// <summary>
        /// Gets or sets 16 byte MD4 checksum.
        /// </summary>
        /// <value>The UID.</value>
        public byte[] UID
        {
            get { return field_3_uid; }
            set { field_3_uid = value; }
        }
 
        /// <summary>
        /// Gets or sets the tag. (Unused)
        /// </summary>
        /// <value>The tag.</value>
        public short Tag
        {
            get { return field_4_tag; }
            set { field_4_tag = value; }
        }
 
        /// <summary>
        /// Gets or sets Blip size in stream..
        /// </summary>
        /// <value>The size.</value>
        public int Size
        {
            get { return field_5_size; }
            set { field_5_size = value; }
        }
 
        /// <summary>
        /// Gets or sets the reference count of this blip.
        /// </summary>
        /// <value>The ref.</value>
        public int Ref
        {
            get { return field_6_ref; }
            set { field_6_ref = value; }
        }
        /// <summary>
        /// Gets or sets the offset in the delay stream..
        /// </summary>
        /// <value>The offset.</value>
        public int Offset
        {
            get { return field_7_offset; }
            set { field_7_offset = value; }
        }
 
        /// <summary>
        /// Defines the way this blip is used.
        /// </summary>
        /// <value>The usage.</value>
        public byte Usage
        {
            get { return field_8_usage; }
            set { field_8_usage = value; }
        }
 
 
        /// <summary>
        /// Gets or sets the blip name.
        /// </summary>
        /// <value>The name.</value>
        public byte Name
        {
            get { return field_9_name; }
            set { field_9_name = value; }
        }
 
 
        /// <summary>
        /// Gets or sets the unused2.
        /// </summary>
        /// <value>The unused2.</value>
        public byte Unused2
        {
            get { return field_10_unused2; }
            set { field_10_unused2 = value; }
        }
 
        /// <summary>
        /// Gets or sets the unused3.
        /// </summary>
        /// <value>The unused3.</value>
        public byte Unused3
        {
            get { return field_11_unused3; }
            set { field_11_unused3 = value; }
        }
 
        /// <summary>
        /// Gets or sets the blip record.
        /// </summary>
        /// <value>The blip record.</value>
        public EscherBlipRecord BlipRecord
        {
            get { return field_12_blipRecord; }
            set { field_12_blipRecord = value; }
        }
 
        /// <summary>
        /// Gets or sets any remaining data in this record.
        /// </summary>
        /// <value>The remaining data.</value>
        public byte[] RemainingData
        {
            get { return _remainingData; }
            set { _remainingData = value; }
        }
        /// <summary>
        /// Returns a <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
        /// </returns>
        public override String ToString()
        {
            String nl = Environment.NewLine;
 
            String extraData;
            using (MemoryStream b = new MemoryStream())
            {
                try
                {
                    HexDump.Dump(this._remainingData, 0, b, 0);
                    extraData = b.ToString();
                }
                catch (Exception e)
                {
                    extraData = e.ToString();
                }
                return GetType().Name + ":" + nl +
                        "  RecordId: 0x" + HexDump.ToHex(RECORD_ID) + nl +
                        "  Options: 0x" + HexDump.ToHex(Options) + nl +
                        "  BlipTypeWin32: " + field_1_blipTypeWin32 + nl +
                        "  BlipTypeMacOS: " + field_2_blipTypeMacOS + nl +
                        "  SUID: " + HexDump.ToHex(field_3_uid) + nl +
                        "  Tag: " + field_4_tag + nl +
                        "  Size: " + field_5_size + nl +
                        "  Ref: " + field_6_ref + nl +
                        "  Offset: " + field_7_offset + nl +
                        "  Usage: " + field_8_usage + nl +
                        "  Name: " + field_9_name + nl +
                        "  Unused2: " + field_10_unused2 + nl +
                        "  Unused3: " + field_11_unused3 + nl +
                        "  blipRecord: " + (field_12_blipRecord == null ? "null" : field_12_blipRecord.ToString()) + nl;
                //"  Extra Data:" + nl + extraData;
            }
        }
 
        /// <summary>
        /// Retrieve the string representation given a blip id.
        /// </summary>
        /// <param name="b">The b.</param>
        /// <returns></returns>
        public String GetBlipType(byte b)
        {
            switch (b)
            {
                case BT_ERROR:
                    return " ERROR";
                case BT_UNKNOWN:
                    return " UNKNOWN";
                case BT_EMF:
                    return " EMF";
                case BT_WMF:
                    return " WMF";
                case BT_PICT:
                    return " PICT";
                case BT_JPEG:
                    return " JPEG";
                case BT_PNG:
                    return " PNG";
                case BT_DIB:
                    return " DIB";
                default:
                    if (b < 32)
                        return " NotKnown";
                    else
                        return " Client";
            }
        }
 
 
    }
}