90 {
91 if (s.Length == 0)
92 {
93 return 0;
94 }
95
96 int bLen;
97 char c;
98
99 int colorIndex, sizeIndex;
100 byte* buffer = stackalloc byte[1024];
101 byte* first = dPtr;
102
103 byte[] utf8;
104 fixed (char* p = s)
105 {
106 utf8 = new byte[Encoding.UTF8.GetEncoder().GetByteCount(p, s.Length, true)];
107 bool completed;
108 fixed (byte* b = utf8)
109 {
110 Encoding.UTF8.GetEncoder().Convert(p, s.Length, b, utf8.Length, true,
111 out int charsUsed, out int bytesUsed, out completed);
112 }
113
114 if (!completed)
115 {
116 throw new Exception("Could not encode the MSBin file.");
117 }
118 }
119
120
121 char[] utf8_to_char_array = new char[utf8.Length];
122 for (int i = 0; i < utf8.Length; i++)
123 {
124 utf8_to_char_array[i] = (char) utf8[i];
125 }
126
127 fixed (char* p = utf8_to_char_array)
128 {
129 char* sPtr = p, ceil = sPtr + utf8_to_char_array.Length;
130
131 while (sPtr < ceil)
132 {
133 c = *sPtr++;
134 if (c == '<')
135 {
136 char* last = sPtr;
137 byte* tPtr = buffer;
138 while (last < ceil && (c = *last++) != '>')
139 {
140 if (c != ' ')
141 {
142 *tPtr++ = (byte) c;
143 }
144 }
145
146 if (c != '>')
147 {
148 *dPtr++ = (byte) '<';
149 }
150 else
151 {
152 sPtr = last;
153 bLen = (int) tPtr - (int) buffer;
154 ToLower(buffer, bLen);
155
156 byte* control = dPtr;
157 byte* flag = dPtr + 1;
158 byte* data = dPtr + 2;
159
160 if ((sizeIndex = IndexOf(buffer, bLen, "size=")) >= 0)
161 {
162 *control = 0x12;
163 *flag |= 1;
164 *data++ = 0x0D;
165
166 tPtr = buffer + sizeIndex + 5;
167 ReadHex(ref data, ref tPtr, 2);
168
169 dPtr = data;
170 }
171
172 if ((colorIndex = IndexOf(buffer, bLen, "color=")) >= 0)
173 {
174 *control = 0x12;
175 *flag |= 2;
176 *data++ = 0x0C;
177
178 tPtr = buffer + colorIndex + 6;
179 ReadHex(ref data, ref tPtr, 4);
180
181 dPtr = data;
182 }
183
184 if (StrContains(buffer, bLen, "end"))
185 {
186 *dPtr++ = 0x13;
187 }
188 }
189 }
190 else if (c == '[')
191 {
192 char* last = sPtr;
193 byte* tPtr = buffer;
194 while (last < ceil && (c = *last++) != ']')
195 {
196 if (c != ' ')
197 {
198 *tPtr++ = (byte) c;
199 }
200 }
201
202 if (c != ']')
203 {
204 *dPtr++ = (byte) ']';
205 }
206 else
207 {
208 sPtr = last;
209 bLen = (int) tPtr - (int) buffer;
210 ToLower(buffer, bLen);
211
212 if ((colorIndex = IndexOf(buffer, bLen, "border=")) >= 0)
213 {
214 tPtr = buffer + colorIndex + 7;
215
216 *dPtr++ = 0x11;
217 ReadHex(ref dPtr, ref tPtr, 4);
218 }
219 }
220 }
221 else if (c == '\\')
222 {
223 if (sPtr < ceil)
224 {
225 *dPtr++ = (byte) *sPtr++;
226 }
227 }
228 else if (c == '\r')
229 {
230 }
231 else
232 {
233 *dPtr++ = (byte) c;
234 }
235 }
236 }
237
238 return (int) dPtr - (int) first;
239 }