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
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
/* ====================================================================
   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.
==================================================================== */
 
/* ================================================================
 * About NPOI
 * Author: Tony Qu 
 * Author's email: tonyqus (at) gmail.com 
 * Author's Blog: tonyqus.wordpress.com.cn (wp.tonyqus.cn)
 * HomePage: http://www.codeplex.com/npoi
 * Contributors:
 * 
 * ==============================================================*/
 
namespace HH.WMS.Utils.NPOI.HPSF
{
    using System;
    using System.Collections;
    using System.IO;
    using System.Text;
    using HH.WMS.Utils.NPOI.Util;
    using System.Globalization;
 
    /// <summary>
    /// Supports Reading and writing of variant data.
    /// <strong>FIXME (3):</strong>
    ///  Reading and writing should be made more
    /// uniform than it is now. The following items should be resolved:
    /// Reading requires a Length parameter that is 4 byte greater than the
    /// actual data, because the variant type field is included.
    /// Reading Reads from a byte array while writing Writes To an byte array
    /// output stream.
    /// @author Rainer Klute 
    /// <a href="mailto:klute@rainer-klute.de">&lt;klute@rainer-klute.de&gt;</a>
    /// @since 2003-08-08
    /// </summary>
    public class VariantSupport : Variant
    {
 
        private static bool logUnsupportedTypes = false;
 
        /// <summary>
        /// Checks whether logging of unsupported variant types warning is turned
        /// on or off.
        /// </summary>
        /// <value>
        ///     <c>true</c> if logging is turned on; otherwise, <c>false</c>.
        /// </value>
        public static bool IsLogUnsupportedTypes
        {
            get
            {
                return logUnsupportedTypes;
            }
            set 
            { 
                logUnsupportedTypes = value; 
            }
        }
 
 
 
        /**
         * Keeps a list of the variant types an "unsupported" message has alReady
         * been issued for.
         */
        protected static IList unsupportedMessage;
 
        /// <summary>
        /// Writes a warning To System.err that a variant type Is
        /// unsupported by HPSF. Such a warning is written only once for each variant
        /// type. Log messages can be turned on or off by
        /// </summary>
        /// <param name="ex">The exception To log</param>
        public static void WriteUnsupportedTypeMessage
            (UnsupportedVariantTypeException ex)
        {
            if (IsLogUnsupportedTypes)
            {
                if (unsupportedMessage == null)
                    unsupportedMessage = new ArrayList();
                long vt = ex.VariantType;
                if (!unsupportedMessage.Contains(vt))
                {
                    Console.Error.WriteLine(ex.Message);
                    unsupportedMessage.Add(vt);
                }
            }
        }
 
 
        /**
         * HPSF is able To Read these {@link Variant} types.
         */
        static public int[] SUPPORTED_TYPES = { Variant.VT_EMPTY,
            Variant.VT_I2, Variant.VT_I4, Variant.VT_I8, Variant.VT_R8,
            Variant.VT_FILETIME, Variant.VT_LPSTR, Variant.VT_LPWSTR,
            Variant.VT_CF, Variant.VT_BOOL };
 
 
 
        /// <summary>
        /// Checks whether HPSF supports the specified variant type. Unsupported
        /// types should be implemented included in the {@link #SUPPORTED_TYPES}
        /// array.
        /// </summary>
        /// <param name="variantType">the variant type To check</param>
        /// <returns>
        ///     <c>true</c> if HPFS supports this type,otherwise, <c>false</c>.
        /// </returns>
        public bool IsSupportedType(int variantType)
        {
            for (int i = 0; i < SUPPORTED_TYPES.Length; i++)
                if (variantType == SUPPORTED_TYPES[i])
                    return true;
            return false;
        }
 
 
 
        /// <summary>
        /// Reads a variant type from a byte array
        /// </summary>
        /// <param name="src">The byte array</param>
        /// <param name="offset">The offset in the byte array where the variant starts</param>
        /// <param name="Length">The Length of the variant including the variant type field</param>
        /// <param name="type">The variant type To Read</param>
        /// <param name="codepage">The codepage To use for non-wide strings</param>
        /// <returns>A Java object that corresponds best To the variant field. For
        /// example, a VT_I4 is returned as a {@link long}, a VT_LPSTR as a
        /// {@link String}.</returns>
        public static Object Read(byte[] src, int offset,
                                  int Length, long type,
                                  int codepage)
        {
            Object value;
            int o1 = offset;
            int l1 = Length - LittleEndianConsts.INT_SIZE;
            long lType = type;
 
            /* Instead of trying To Read 8-bit characters from a Unicode string,
             * Read 16-bit characters. */
            if (codepage == (int)Constants.CP_UNICODE && type == Variant.VT_LPSTR)
                lType = Variant.VT_LPWSTR;
 
            switch ((int)lType)
            {
                case Variant.VT_EMPTY:
                    {
                        value = null;
                        break;
                    }
                case Variant.VT_I2:
                    {
                        /*
                         * Read a short. In Java it is represented as an
                         * Integer object.
                         */
                        value = LittleEndian.GetShort(src, o1);
                        break;
                    }
                case Variant.VT_I4:
                    {
                        /*
                         * Read a word. In Java it is represented as an
                         * Integer object.
                         */
                        value = LittleEndian.GetInt(src, o1);
                        break;
                    }
                case Variant.VT_I8:
                    {
                        /*
                         * Read a double word. In Java it is represented as a
                         * long object.
                         */
                        value = LittleEndian.GetLong(src, o1);
                        break;
                    }
                case Variant.VT_R8:
                    {
                        /*
                         * Read an eight-byte double value. In Java it is represented as
                         * a Double object.
                         */
                        value = LittleEndian.GetDouble(src, o1);
                        break;
                    }
                case Variant.VT_FILETIME:
                    {
                        /*
                         * Read a FILETIME object. In Java it is represented
                         * as a Date object.
                         */
                        int low = LittleEndian.GetInt(src, o1);
                        o1 += LittleEndianConsts.INT_SIZE;
                        int high = LittleEndian.GetInt(src, o1);
                        if (low == 0 && high == 0)
                            value = null;
                        else
                            value = Util.FiletimeToDate(high, low);
                        break;
                    }
                case Variant.VT_LPSTR:
                    {
                        /*
                         * Read a byte string. In Java it is represented as a
                         * String object. The 0x00 bytes at the end must be
                         * stripped.
                         */
                        int first = o1 + LittleEndianConsts.INT_SIZE;
                        long last = first + LittleEndian.GetUInt(src, o1) - 1;
                        o1 += LittleEndianConsts.INT_SIZE;
                        while (src[(int)last] == 0 && first <= last)
                            last--;
                        int l = (int)(last - first + 1);
                        value = codepage != -1 ?
                            Encoding.GetEncoding(codepage).GetString(src, first, l) :
                            Encoding.UTF8.GetString(src, first, l);
                        break;
                    }
                case Variant.VT_LPWSTR:
                    {
                        /*
                         * Read a Unicode string. In Java it is represented as
                         * a String object. The 0x00 bytes at the end must be
                         * stripped.
                         */
                        int first = o1 + LittleEndianConsts.INT_SIZE;
                        long last = first + LittleEndian.GetUInt(src, o1) - 1;
                        long l = last - first;
                        o1 += LittleEndianConsts.INT_SIZE;
                        StringBuilder b = new StringBuilder((int)(last - first));
                        for (int i = 0; i <= l; i++)
                        {
                            int i1 = o1 + (i * 2);
                            int i2 = i1 + 1;
                            int high = src[i2] << 8;
                            int low = src[i1] & 0x00ff;
                            char c = (char)(high | low);
                            b.Append(c);
                        }
                        /* Strip 0x00 characters from the end of the string: */
                        while (b.Length > 0 && b[b.Length - 1] == 0x00)
                            b.Length = b.Length - 1;
                        value = b.ToString();
                        break;
                    }
                case Variant.VT_CF:
                    {
                        if (l1 < 0)
                        {
                            /**
                             *  YK: reading the ClipboardData packet (VT_CF) is not quite correct.
                             *  The size of the data is determined by the first four bytes of the packet
                             *  while the current implementation calculates it in the Section constructor.
                             *  Test files in Bugzilla 42726 and 45583 clearly show that this approach does not always work.
                             *  The workaround below attempts to gracefully handle such cases instead of throwing exceptions.
                             *
                             *  August 20, 2009
                             */
                            l1 = LittleEndian.GetInt(src, o1); 
                            o1 += LittleEndian.INT_SIZE;
                        }
                        byte[] v = new byte[l1];
                        for (int i = 0; i < l1; i++)
                            v[i] = src[(o1 + i)];
                        value = v;
                        break;
                    }
                case Variant.VT_BOOL:
                    {
                        /*
                         * The first four bytes in src, from src[offset] To
                         * src[offset + 3] contain the DWord for VT_BOOL, so
                         * skip it, we don't need it.
                         */
                        // int first = offset + LittleEndianConstants.INT_SIZE;
                        long boolean = LittleEndian.GetUInt(src, o1);
                        if (boolean != 0)
                            value = true;
                        else
                            value = false;
                        break;
                    }
                default:
                    {
                        byte[] v = new byte[l1];
                        for (int i = 0; i < l1; i++)
                            v[i] = src[(o1 + i)];
                        throw new ReadingNotSupportedException(type, v);
                    }
            }
            return value;
        }
 
 
        /// <summary>
        /// Writes a variant value To an output stream. This method ensures that
        /// always a multiple of 4 bytes is written.
        /// If the codepage is UTF-16, which is encouraged, strings
        /// <strong>must</strong> always be written as {@link Variant#VT_LPWSTR}
        /// strings, not as {@link Variant#VT_LPSTR} strings. This method ensure this
        /// by Converting strings appropriately, if needed.
        /// </summary>
        /// <param name="out1">The stream To Write the value To.</param>
        /// <param name="type">The variant's type.</param>
        /// <param name="value">The variant's value.</param>
        /// <param name="codepage">The codepage To use To Write non-wide strings</param>
        /// <returns>The number of entities that have been written. In many cases an
        /// "entity" is a byte but this is not always the case.</returns>
        public static int Write(Stream out1, long type,
                                Object value, int codepage)
        {
            int length = 0;
            switch ((int)type)
            {
                case Variant.VT_BOOL:
                    {
                        int trueOrFalse;
                        if ((bool)value)
                            trueOrFalse = 1;
                        else
                            trueOrFalse = 0;
                        length = TypeWriter.WriteUIntToStream(out1, (uint)trueOrFalse);
                        break;
                    }
                case Variant.VT_LPSTR:
                    {
                        if (codepage == 0)
                            throw new ArgumentOutOfRangeException("codepage");
                        byte[] bytes =
                            (codepage == -1 ?
                            Encoding.UTF8.GetBytes((string)value) :
                            Encoding.GetEncoding(codepage).GetBytes((string)value));
                            
                        length = TypeWriter.WriteUIntToStream(out1, (uint)bytes.Length + 1);
                        byte[] b = new byte[bytes.Length + 1];
                        Array.Copy(bytes, 0, b, 0, bytes.Length);
                        b[b.Length - 1] = 0x00;
                        out1.Write(b, 0, b.Length);
                        length += b.Length;
                        break;
                    }
                case Variant.VT_LPWSTR:
                    {
                        int nrOfChars = ((String)value).Length + 1;
                        length += TypeWriter.WriteUIntToStream(out1, (uint)nrOfChars);
                        char[] s = Util.Pad4((String)value);
                        for (int i = 0; i < s.Length; i++)
                        {
                            int high = ((s[i] & 0x0000ff00) >> 8);
                            int low = (s[i] & 0x000000ff);
                            byte highb = (byte)high;
                            byte lowb = (byte)low;
                            out1.WriteByte(lowb);
                            out1.WriteByte(highb);
                            length += 2;
                        }
                        out1.WriteByte(0x00);
                        out1.WriteByte(0x00);
                        length += 2;
                        break;
                    }
                case Variant.VT_CF:
                    {
                        byte[] b = (byte[])value;
                        out1.Write(b, 0, b.Length);
                        length = b.Length;
                        break;
                    }
                case Variant.VT_EMPTY:
                    {
                        TypeWriter.WriteUIntToStream(out1, Variant.VT_EMPTY);
                        length = LittleEndianConsts.INT_SIZE;
                        break;
                    }
                case Variant.VT_I2:
                    {
                        short x;
                        try
                        {
                            x = Convert.ToInt16(value, CultureInfo.InvariantCulture);
                        }catch(OverflowException)
                        {
                            x=(short)((int)value);
                        }
                        TypeWriter.WriteToStream(out1, x);
                        length = LittleEndianConsts.SHORT_SIZE;
                        break;
                    }
                case Variant.VT_I4:
                    {
                        if (!(value is int))
                        {
                            throw new Exception("Could not cast an object To "
                                    + "int" + ": "
                                    + value.GetType().Name + ", "
                                    + value.ToString());
                        }
                        length += TypeWriter.WriteToStream(out1, (int)value);
                        break;
                    }
                case Variant.VT_I8:
                    {
                        TypeWriter.WriteToStream(out1, Convert.ToInt64(value, CultureInfo.CurrentCulture));
                        length = LittleEndianConsts.LONG_SIZE;
                        break;
                    }
                case Variant.VT_R8:
                    {
                        length += TypeWriter.WriteToStream(out1,
                                  (Double)value);
                        break;
                    }
                case Variant.VT_FILETIME:
                    {
                        long filetime;
                        if (value != null)
                        {
                            filetime = Util.DateToFileTime((DateTime)value);
                        }
                        else
                        {
                            filetime = 0;
                        }
                        int high = (int)((filetime >> 32) & 0x00000000FFFFFFFFL);
                        int low = (int)(filetime & 0x00000000FFFFFFFFL);
                        length += TypeWriter.WriteUIntToStream
                            (out1, (uint)(0x0000000FFFFFFFFL & low));
                        length += TypeWriter.WriteUIntToStream
                            (out1, (uint)(0x0000000FFFFFFFFL & high));
                        
                        break;
                    }
                default:
                    {
                        /* The variant type is not supported yet. However, if the value
                         * is a byte array we can Write it nevertheless. */
                        if (value is byte[])
                        {
                            byte[] b = (byte[])value;
                            out1.Write(b, 0, b.Length);
                            length = b.Length;
                            WriteUnsupportedTypeMessage
                                (new WritingNotSupportedException(type, value));
                        }
                        else
                            throw new WritingNotSupportedException(type, value);
                        break;
                    }
            }
 
            return length;
        }
 
    }
}