zhao
2021-06-24 02ca96debc6056275d58e55d97f7885a195542d0
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
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
 
namespace HH.WMS.Utils.ExcelLibrary.BinaryFileFormat
{
    public class StringDecoder
    {
        Record record;
        BinaryReader reader;
        int ContinuedIndex = -1;
 
        public StringDecoder(Record record, BinaryReader reader)
        {
            this.record = record;
            this.reader = reader;
        }
 
        public string ReadString(int lengthbits)
        {
            if (reader.BaseStream.Position == reader.BaseStream.Length)
            {
                if (ContinuedIndex < record.ContinuedRecords.Count - 1)
                {
                    SwitchToContinuedRecord();
                }
                else
                {
                    return null;
                }
            }
 
            int stringlength = lengthbits == 8 ? reader.ReadByte() : reader.ReadUInt16();
            byte option = reader.ReadByte();
            bool compressed = (option & 0x01) == 0;
            bool phonetic = (option & 0x04) == 0x04;
            bool richtext = (option & 0x08) == 0x08;
            int runs = 0; //Number of Rich-Text formatting runs
            int size = 0; //Size of Asian phonetic settings block in bytes
            if (richtext)
            {
                runs = reader.ReadUInt16();
            }
            if (phonetic)
            {
                size = reader.ReadInt32();
            }
 
            string firstpart = ReadString(stringlength, compressed);
            StringBuilder text = new StringBuilder();
            text.Append(firstpart);
            if (firstpart.Length < stringlength)
            {
                SwitchToContinuedRecord();
                text.Append(ReadContinuedString(stringlength - firstpart.Length));
            }
            ReadBytes(4 * runs + size);
            return text.ToString();
        }
 
        private string ReadString(int stringlength, bool compressed)
        {
            byte[] textData;
            if (compressed)
            {
                byte[] bytes = reader.ReadBytes(stringlength);
                textData = new byte[bytes.Length * 2];
                for (int i = 0; i < bytes.Length; i++)
                {
                    textData[i * 2] = bytes[i];
                    textData[i * 2 + 1] = 0;
                }
            }
            else
            {
                textData = reader.ReadBytes(stringlength * 2);
            }
            return Encoding.Unicode.GetString(textData);
        }
 
        private string ReadContinuedString(int stringlength)
        {
            if (reader.BaseStream.Position == reader.BaseStream.Length) return null;
            byte option = reader.ReadByte();
            bool compressed = (option & 0x01) == 0;
            string firstpart = ReadString(stringlength, compressed);
            if (firstpart.Length < stringlength)
            {
                SwitchToContinuedRecord();
                StringBuilder text = new StringBuilder();
                text.Append(firstpart);
                text.Append(ReadContinuedString(stringlength - firstpart.Length));
                return text.ToString();
            }
            else
            {
                return firstpart;
            }
        }
 
        private byte[] ReadBytes(int count)
        {
            byte[] bytes = reader.ReadBytes(count);
            int bytesRead = bytes.Length;
            if (bytesRead < count)
            {
                SwitchToContinuedRecord();
                byte[] allbytes = new byte[count];
                byte[] remainedbytes = ReadBytes(count - bytesRead);
                bytes.CopyTo(allbytes, 0);
                remainedbytes.CopyTo(allbytes, bytesRead);
                return allbytes;
            }
            return bytes;
        }
 
        private void SwitchToContinuedRecord()
        {
            ContinuedIndex++;
            MemoryStream stream = new MemoryStream(record.ContinuedRecords[ContinuedIndex].Data);
            reader = new BinaryReader(stream);
        }
 
        public string ReadString(int lengthbits, out RichTextFormat rtf)
        {
            /* BEGIN - SAME AS ReadString() */
            if (reader.BaseStream.Position == reader.BaseStream.Length)
            {
                if (ContinuedIndex < record.ContinuedRecords.Count - 1)
                {
                    SwitchToContinuedRecord();
                }
                else
                {
                    rtf = null;
                    return null;
                }
            }
 
            int stringlength = lengthbits == 8 ? reader.ReadByte() : reader.ReadUInt16();
            byte option = reader.ReadByte();
            bool compressed = (option & 0x01) == 0;
            bool phonetic = (option & 0x04) == 0x04;
            bool richtext = (option & 0x08) == 0x08;
            int runs = 0; //Number of Rich-Text formatting runs
            int size = 0; //Size of Asian phonetic settings block in bytes
            if (richtext)
            {
                runs = reader.ReadUInt16();
            }
            if (phonetic)
            {
                size = reader.ReadInt32();
            }
 
            string firstpart = ReadString(stringlength, compressed);
            StringBuilder text = new StringBuilder();
            text.Append(firstpart);
            if (firstpart.Length < stringlength)
            {
                SwitchToContinuedRecord();
                text.Append(ReadContinuedString(stringlength - firstpart.Length));
            }
            /* END - SAME AS ReadString() */
 
            /* 
             * Added, 2-13-2009
             * Sunil Shenoi
             * 
             * Process the rich text formatting information as in Section 2.5.3.
             */
            byte[] richTextBytes = ReadBytes(4 * runs + size);
            rtf = DecodeRichTextFormatting(richTextBytes, runs);
 
            return text.ToString();
        }
 
        /*
         * Added, 2-13-2009
         * Sunil Shenoi
         * 
         * Decode the rich text formatting information associated with a given string.
         */
        private RichTextFormat DecodeRichTextFormatting(byte[] richTextBytes, int runs)
        {
            RichTextFormat rtf = new RichTextFormat(runs);
 
            // process the byte array into pairs of UInt16's
            for (int i = 0; i < runs; i++)
            {
                rtf.CharIndexes.Add(BitConverter.ToUInt16(richTextBytes, (i * 4)));
                rtf.FontIndexes.Add(BitConverter.ToUInt16(richTextBytes, (i * 4) + 2));
            }
 
            return rtf;
        }
    }
}