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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
|
// Copyright © Microsoft Corporation.
// This source file is subject to the Microsoft Permissive License.
// See http://www.microsoft.com/resources/sharedsource/licensingbasics/sharedsourcelicenses.mspx.
// All other rights reserved.
using System;
using System.Diagnostics;
#if CCINamespace
using Microsoft.Cci.Metadata;
#else
using System.Compiler.Metadata;
#endif
using System.Globalization;
using System.Text;
#if CCINamespace
namespace Microsoft.Cci{
#else
namespace System.Compiler
{
#endif
#if !(FxCop || NoWriter)
/// <summary>
/// High performance replacement for System.IO.BinaryWriter.
/// </summary>
public sealed class BinaryWriter
{
public MemoryStream/*!*/ BaseStream;
private bool UTF8 = true;
public BinaryWriter(MemoryStream/*!*/ output)
{
this.BaseStream = output;
//^ base();
}
public BinaryWriter(MemoryStream/*!*/ output, Encoding/*!*/ encoding)
{
Debug.Assert(encoding == Encoding.Unicode);
this.BaseStream = output;
this.UTF8 = false;
//^ base();
}
public void Write(bool value)
{
MemoryStream m = this.BaseStream;
int i = m.Position;
m.Position = i + 1;
m.Buffer[i] = (byte)(value ? 1 : 0);
}
public void Write(byte value)
{
MemoryStream m = this.BaseStream;
int i = m.Position;
m.Position = i + 1;
m.Buffer[i] = value;
}
public void Write(sbyte value)
{
MemoryStream m = this.BaseStream;
int i = m.Position;
m.Position = i + 1;
m.Buffer[i] = (byte)value;
}
public void Write(byte[] buffer)
{
if (buffer == null) return;
this.BaseStream.Write(buffer, 0, buffer.Length);
}
public void Write(char ch)
{
MemoryStream m = this.BaseStream;
int i = m.Position;
if (this.UTF8)
{
if (ch < 0x80)
{
m.Position = i + 1;
m.Buffer[i] = (byte)ch;
}
else
this.Write(new char[] { ch });
}
else
{
m.Position = i + 2;
byte[] buffer = m.Buffer;
buffer[i++] = (byte)ch;
buffer[i] = (byte)(ch >> 8);
}
}
public void Write(char[] chars)
{
if (chars == null) return;
MemoryStream m = this.BaseStream;
int n = chars.Length;
int i = m.Position;
if (this.UTF8)
{
m.Position = i + n;
byte[] buffer = m.Buffer;
for (int j = 0; j < n; j++)
{
char ch = chars[j];
if ((ch & 0x80) != 0) goto writeUTF8;
buffer[i++] = (byte)ch;
}
return;
writeUTF8:
int ch32 = 0;
for (int j = n - (m.Position - i); j < n; j++)
{
char ch = chars[j];
if (ch < 0x80)
{
m.Position = i + 1;
buffer = m.Buffer;
buffer[i++] = (byte)ch;
}
else if (ch < 0x800)
{
m.Position = i + 2;
buffer = m.Buffer;
buffer[i++] = (byte)(((ch >> 6) & 0x1F) | 0xC0);
buffer[i] = (byte)((ch & 0x3F) | 0x80);
}
else if (0xD800 <= ch && ch <= 0xDBFF)
{
ch32 = (ch & 0x3FF) << 10;
}
else if (0xDC00 <= ch && ch <= 0xDFFF)
{
ch32 |= ch & 0x3FF;
m.Position = i + 4;
buffer = m.Buffer;
buffer[i++] = (byte)(((ch32 >> 18) & 0x7) | 0xF0);
buffer[i++] = (byte)(((ch32 >> 12) & 0x3F) | 0x80);
buffer[i++] = (byte)(((ch32 >> 6) & 0x3F) | 0x80);
buffer[i] = (byte)((ch32 & 0x3F) | 0x80);
}
else
{
m.Position = i + 3;
buffer = m.Buffer;
buffer[i++] = (byte)(((ch >> 12) & 0xF) | 0xE0);
buffer[i++] = (byte)(((ch >> 6) & 0x3F) | 0x80);
buffer[i] = (byte)((ch & 0x3F) | 0x80);
}
}
}
else
{
m.Position = i + n * 2;
byte[] buffer = m.Buffer;
for (int j = 0; j < n; j++)
{
char ch = chars[j];
buffer[i++] = (byte)ch;
buffer[i++] = (byte)(ch >> 8);
}
}
}
public unsafe void Write(double value)
{
MemoryStream m = this.BaseStream;
int i = m.Position;
m.Position = i + 8;
fixed (byte* b = m.Buffer)
*((double*)(b + i)) = value;
}
public void Write(short value)
{
MemoryStream m = this.BaseStream;
int i = m.Position;
m.Position = i + 2;
byte[] buffer = m.Buffer;
buffer[i++] = (byte)value;
buffer[i] = (byte)(value >> 8);
}
public unsafe void Write(ushort value)
{
MemoryStream m = this.BaseStream;
int i = m.Position;
m.Position = i + 2;
byte[] buffer = m.Buffer;
buffer[i++] = (byte)value;
buffer[i] = (byte)(value >> 8);
}
public void Write(int value)
{
MemoryStream m = this.BaseStream;
int i = m.Position;
m.Position = i + 4;
byte[] buffer = m.Buffer;
buffer[i++] = (byte)value;
buffer[i++] = (byte)(value >> 8);
buffer[i++] = (byte)(value >> 16);
buffer[i] = (byte)(value >> 24);
}
public void Write(uint value)
{
MemoryStream m = this.BaseStream;
int i = m.Position;
m.Position = i + 4;
byte[] buffer = m.Buffer;
buffer[i++] = (byte)value;
buffer[i++] = (byte)(value >> 8);
buffer[i++] = (byte)(value >> 16);
buffer[i] = (byte)(value >> 24);
}
public void Write(long value)
{
MemoryStream m = this.BaseStream;
int i = m.Position;
m.Position = i + 8;
byte[] buffer = m.Buffer;
uint lo = (uint)value;
uint hi = (uint)(value >> 32);
buffer[i++] = (byte)lo;
buffer[i++] = (byte)(lo >> 8);
buffer[i++] = (byte)(lo >> 16);
buffer[i++] = (byte)(lo >> 24);
buffer[i++] = (byte)hi;
buffer[i++] = (byte)(hi >> 8);
buffer[i++] = (byte)(hi >> 16);
buffer[i] = (byte)(hi >> 24);
}
public unsafe void Write(ulong value)
{
MemoryStream m = this.BaseStream;
int i = m.Position;
m.Position = i + 8;
byte[] buffer = m.Buffer;
uint lo = (uint)value;
uint hi = (uint)(value >> 32);
buffer[i++] = (byte)lo;
buffer[i++] = (byte)(lo >> 8);
buffer[i++] = (byte)(lo >> 16);
buffer[i++] = (byte)(lo >> 24);
buffer[i++] = (byte)hi;
buffer[i++] = (byte)(hi >> 8);
buffer[i++] = (byte)(hi >> 16);
buffer[i] = (byte)(hi >> 24);
}
public unsafe void Write(float value)
{
MemoryStream m = this.BaseStream;
int i = m.Position;
m.Position = i + 4;
fixed (byte* b = m.Buffer)
*((float*)(b + i)) = value;
}
public void Write(string str)
{
this.Write(str, false);
}
public void Write(string str, bool emitNullTerminator)
{
if (str == null)
{
Debug.Assert(!emitNullTerminator);
this.Write((byte)0xff);
return;
}
int n = str.Length;
if (!emitNullTerminator)
{
if (this.UTF8)
Ir2md.WriteCompressedInt(this, this.GetUTF8ByteCount(str));
else
Ir2md.WriteCompressedInt(this, n * 2);
}
MemoryStream m = this.BaseStream;
int i = m.Position;
if (this.UTF8)
{
m.Position = i + n;
byte[] buffer = m.Buffer;
for (int j = 0; j < n; j++)
{
char ch = str[j];
if (ch >= 0x80) goto writeUTF8;
buffer[i++] = (byte)ch;
}
if (emitNullTerminator)
{
m.Position = i + 1;
buffer = m.Buffer;
buffer[i] = 0;
}
return;
writeUTF8:
int ch32 = 0;
for (int j = n - (m.Position - i); j < n; j++)
{
char ch = str[j];
if (ch < 0x80)
{
m.Position = i + 1;
buffer = m.Buffer;
buffer[i++] = (byte)ch;
}
else if (ch < 0x800)
{
m.Position = i + 2;
buffer = m.Buffer;
buffer[i++] = (byte)(((ch >> 6) & 0x1F) | 0xC0);
buffer[i++] = (byte)((ch & 0x3F) | 0x80);
}
else if (0xD800 <= ch && ch <= 0xDBFF)
{
ch32 = (ch & 0x3FF) << 10;
}
else if (0xDC00 <= ch && ch <= 0xDFFF)
{
ch32 |= ch & 0x3FF;
m.Position = i + 4;
buffer = m.Buffer;
buffer[i++] = (byte)(((ch32 >> 18) & 0x7) | 0xF0);
buffer[i++] = (byte)(((ch32 >> 12) & 0x3F) | 0x80);
buffer[i++] = (byte)(((ch32 >> 6) & 0x3F) | 0x80);
buffer[i++] = (byte)((ch32 & 0x3F) | 0x80);
}
else
{
m.Position = i + 3;
buffer = m.Buffer;
buffer[i++] = (byte)(((ch >> 12) & 0xF) | 0xE0);
buffer[i++] = (byte)(((ch >> 6) & 0x3F) | 0x80);
buffer[i++] = (byte)((ch & 0x3F) | 0x80);
}
}
if (emitNullTerminator)
{
m.Position = i + 1;
buffer = m.Buffer;
buffer[i] = 0;
}
}
else
{
m.Position = i + n * 2;
byte[] buffer = m.Buffer;
for (int j = 0; j < n; j++)
{
char ch = str[j];
buffer[i++] = (byte)ch;
buffer[i++] = (byte)(ch >> 8);
}
if (emitNullTerminator)
{
m.Position = i + 2;
buffer = m.Buffer;
buffer[i++] = 0;
buffer[i] = 0;
}
}
}
public int GetUTF8ByteCount(string str)
{
int count = 0;
for (int i = 0, n = str == null ? 0 : str.Length; i < n; i++)
{
//^ assume str != null;
char ch = str[i];
if (ch < 0x80)
{
count += 1;
}
else if (ch < 0x800)
{
count += 2;
}
else if (0xD800 <= ch && ch <= 0xDBFF)
{
count += 2;
}
else if (0xDC00 <= ch && ch <= 0xDFFF)
{
count += 2;
}
else
{
count += 3;
}
}
return count;
}
}
public sealed class MemoryStream
{
public byte[]/* ! */ Buffer;
public int Length;
public int position;
public int Position
{
get { return this.position; }
set
{
byte[] myBuffer = this.Buffer;
int n = myBuffer.Length;
if (value >= n) this.Grow(myBuffer, n, value);
if (value > this.Length) this.Length = value;
this.position = value;
}
}
public MemoryStream()
{
this.Buffer = new byte[64];
this.Length = 0;
this.position = 0;
//^ base();
}
public MemoryStream(byte[]/*!*/ bytes)
{
if (bytes == null) { Debug.Fail(""); }
this.Buffer = bytes;
this.Length = bytes.Length;
this.position = 0;
//^ base();
}
private void Grow(byte[]/*!*/ myBuffer, int n, int m)
{
if (myBuffer == null) { Debug.Fail(""); return; }
int n2 = n * 2;
while (m >= n2) n2 = n2 * 2;
byte[] newBuffer = this.Buffer = new byte[n2];
for (int i = 0; i < n; i++)
newBuffer[i] = myBuffer[i]; //TODO: optimize this
}
public void Seek(long offset, SeekOrigin loc)
{
Debug.Assert(loc == SeekOrigin.Begin);
Debug.Assert(offset <= int.MaxValue);
this.Position = (int)offset;
}
public byte[]/*!*/ ToArray()
{
int n = this.Length;
byte[] source = this.Buffer;
if (source.Length == n) return this.Buffer; //unlikely, but the check is cheap
byte[] result = new byte[n];
for (int i = 0; i < n; i++)
result[i] = source[i]; //TODO: optimize this
return result;
}
public void Write(byte[]/*!*/ buffer, int index, int count)
{
int p = this.position;
this.Position = p + count;
byte[] myBuffer = this.Buffer;
for (int i = 0, j = p, k = index; i < count; i++)
myBuffer[j++] = buffer[k++]; //TODO: optimize this
}
public void WriteTo(MemoryStream/*!*/ stream)
{
stream.Write(this.Buffer, 0, this.Length);
}
public void WriteTo(System.IO.Stream/*!*/ stream)
{
stream.Write(this.Buffer, 0, this.Length);
}
}
public enum SeekOrigin { Begin, Current, End }
#endif
/// <summary>
/// A version of System.IO.Path that does not throw exceptions.
/// </summary>
#if FxCop || NoWriter
internal sealed class BetterPath {
#else
public sealed class BetterPath
{
#endif
public static readonly char AltDirectorySeparatorChar = System.IO.Path.AltDirectorySeparatorChar;
public static readonly char DirectorySeparatorChar = System.IO.Path.DirectorySeparatorChar;
public static readonly char VolumeSeparatorChar = System.IO.Path.VolumeSeparatorChar;
public static string ChangeExtension(string path, string extension)
{
if (path == null) return null;
string text1 = path;
int num1 = path.Length;
while (--num1 >= 0)
{
char ch1 = path[num1];
if (ch1 == '.')
{
text1 = path.Substring(0, num1);
break;
}
if (((ch1 == BetterPath.DirectorySeparatorChar) || (ch1 == BetterPath.AltDirectorySeparatorChar)) || (ch1 == BetterPath.VolumeSeparatorChar))
break;
}
if (extension == null || path.Length == 0) return text1;
if (extension.Length == 0 || extension[0] != '.')
text1 = text1 + ".";
return text1 + extension;
}
public static string Combine(string path1, string path2)
{
if (path1 == null || path1.Length == 0) return path2;
if (path2 == null || path2.Length == 0) return path1;
char ch = path2[0];
if (ch == BetterPath.DirectorySeparatorChar || ch == BetterPath.AltDirectorySeparatorChar || (path2.Length >= 2 && path2[1] == BetterPath.VolumeSeparatorChar))
return path2;
ch = path1[path1.Length - 1];
if (ch != BetterPath.DirectorySeparatorChar && ch != BetterPath.AltDirectorySeparatorChar && ch != BetterPath.VolumeSeparatorChar)
return (path1 + BetterPath.DirectorySeparatorChar + path2);
return path1 + path2;
}
public static string GetExtension(string path)
{
if (path == null) return null;
int length = path.Length;
for (int i = length; --i >= 0; )
{
char ch = path[i];
if (ch == '.')
{
if (i != length - 1)
return path.Substring(i, length - i);
else
return String.Empty;
}
if (ch == BetterPath.DirectorySeparatorChar || ch == BetterPath.AltDirectorySeparatorChar || ch == BetterPath.VolumeSeparatorChar)
break;
}
return string.Empty;
}
public static String GetFileName(string path)
{
if (path == null) return null;
int length = path.Length;
for (int i = length; --i >= 0; )
{
char ch = path[i];
if (ch == BetterPath.DirectorySeparatorChar || ch == BetterPath.AltDirectorySeparatorChar || ch == BetterPath.VolumeSeparatorChar)
return path.Substring(i + 1);
}
return path;
}
public static string GetFileNameWithoutExtension(string path)
{
int num1;
path = BetterPath.GetFileName(path);
if (path == null) return null;
if ((num1 = path.LastIndexOf('.')) == -1) return path;
return path.Substring(0, num1);
}
public static String GetDirectoryName(string path)
{
if (path == null) return null;
int length = path.Length;
for (int i = length; --i >= 0; )
{
char ch = path[i];
if (ch == BetterPath.DirectorySeparatorChar || ch == BetterPath.AltDirectorySeparatorChar || ch == BetterPath.VolumeSeparatorChar)
return path.Substring(0, i);
}
return path;
}
public static char[] GetInvalidFileNameChars()
{
#if WHIDBEY
return System.IO.Path.GetInvalidFileNameChars();
#else
return System.IO.Path.InvalidPathChars;
#endif
}
public static char[] GetInvalidPathChars()
{
#if WHIDBEY
return System.IO.Path.GetInvalidPathChars();
#else
return System.IO.Path.InvalidPathChars;
#endif
}
public static string GetTempFileName()
{
return System.IO.Path.GetTempFileName();
}
public static bool HasExtension(string path)
{
if (path != null)
{
int num1 = path.Length;
while (--num1 >= 0)
{
char ch1 = path[num1];
if (ch1 == '.')
{
if (num1 != (path.Length - 1))
{
return true;
}
return false;
}
if (((ch1 == BetterPath.DirectorySeparatorChar) || (ch1 == BetterPath.AltDirectorySeparatorChar)) || (ch1 == BetterPath.VolumeSeparatorChar))
{
break;
}
}
}
return false;
}
}
}
|