1 //-----------------------------------------------------------------------------
3 // Copyright (C) Microsoft Corporation. All Rights Reserved.
5 //-----------------------------------------------------------------------------
10 namespace Microsoft
.Cci
.Pdb
{
11 internal class BitAccess
{
13 internal BitAccess(int capacity
) {
14 this.buffer
= new byte[capacity
];
18 internal byte[] Buffer
{
19 get { return buffer; }
21 private byte[] buffer
;
23 internal void FillBuffer(Stream stream
, int capacity
) {
24 MinCapacity(capacity
);
25 stream
.Read(buffer
, 0, capacity
);
29 internal int Position
{
30 get { return offset; }
31 set { offset = value; }
35 internal void WriteBuffer(Stream stream
, int count
) {
36 stream
.Write(buffer
, 0, count
);
39 internal void MinCapacity(int capacity
) {
40 if (buffer
.Length
< capacity
) {
41 buffer
= new byte[capacity
];
46 internal void Align(int alignment
) {
47 while ((offset
% alignment
) != 0) {
52 internal void WriteInt32(int value) {
53 buffer
[offset
+ 0] = (byte)value;
54 buffer
[offset
+ 1] = (byte)(value >> 8);
55 buffer
[offset
+ 2] = (byte)(value >> 16);
56 buffer
[offset
+ 3] = (byte)(value >> 24);
60 internal void WriteInt32(int[] values
) {
61 for (int i
= 0; i
< values
.Length
; i
++) {
62 WriteInt32(values
[i
]);
66 internal void WriteBytes(byte[] bytes
) {
67 for (int i
= 0; i
< bytes
.Length
; i
++) {
68 buffer
[offset
++] = bytes
[i
];
72 internal void ReadInt16(out short value) {
73 value = (short)((buffer
[offset
+ 0] & 0xFF) |
74 (buffer
[offset
+ 1] << 8));
78 internal void ReadInt32(out int value) {
79 value = (int)((buffer
[offset
+ 0] & 0xFF) |
80 (buffer
[offset
+ 1] << 8) |
81 (buffer
[offset
+ 2] << 16) |
82 (buffer
[offset
+ 3] << 24));
86 internal void ReadInt64(out long value) {
87 value = (long)((buffer
[offset
+ 0] & 0xFF) |
88 (buffer
[offset
+ 1] << 8) |
89 (buffer
[offset
+ 2] << 16) |
90 (buffer
[offset
+ 3] << 24) |
91 (buffer
[offset
+ 4] << 32) |
92 (buffer
[offset
+ 5] << 40) |
93 (buffer
[offset
+ 6] << 48) |
94 (buffer
[offset
+ 7] << 56));
98 internal void ReadUInt16(out ushort value) {
99 value = (ushort)((buffer
[offset
+ 0] & 0xFF) |
100 (buffer
[offset
+ 1] << 8));
104 internal void ReadUInt8(out byte value) {
105 value = (byte)((buffer
[offset
+ 0] & 0xFF));
109 internal void ReadUInt32(out uint value) {
110 value = (uint)((buffer
[offset
+ 0] & 0xFF) |
111 (buffer
[offset
+ 1] << 8) |
112 (buffer
[offset
+ 2] << 16) |
113 (buffer
[offset
+ 3] << 24));
117 internal void ReadUInt64(out ulong value) {
118 value = (ulong)((buffer
[offset
+ 0] & 0xFF) |
119 (buffer
[offset
+ 1] << 8) |
120 (buffer
[offset
+ 2] << 16) |
121 (buffer
[offset
+ 3] << 24) |
122 (buffer
[offset
+ 4] << 32) |
123 (buffer
[offset
+ 5] << 40) |
124 (buffer
[offset
+ 6] << 48) |
125 (buffer
[offset
+ 7] << 56));
129 internal void ReadInt32(int[] values
) {
130 for (int i
= 0; i
< values
.Length
; i
++) {
131 ReadInt32(out values
[i
]);
135 internal void ReadUInt32(uint[] values
) {
136 for (int i
= 0; i
< values
.Length
; i
++) {
137 ReadUInt32(out values
[i
]);
141 internal void ReadBytes(byte[] bytes
) {
142 for (int i
= 0; i
< bytes
.Length
; i
++) {
143 bytes
[i
] = buffer
[offset
++];
147 internal float ReadFloat() {
148 float result
= BitConverter
.ToSingle(buffer
, offset
);
153 internal double ReadDouble() {
154 double result
= BitConverter
.ToDouble(buffer
, offset
);
159 internal decimal ReadDecimal() {
160 int[] bits
= new int[4];
161 this.ReadInt32(bits
);
162 return new decimal(bits
);
165 internal void ReadBString(out string value) {
167 this.ReadUInt16(out len
);
168 value = Encoding
.UTF8
.GetString(buffer
, offset
, len
);
172 internal void ReadCString(out string value) {
174 while (offset
+ len
< buffer
.Length
&& buffer
[offset
+ len
] != 0) {
177 value = Encoding
.UTF8
.GetString(buffer
, offset
, len
);
181 internal void SkipCString(out string value) {
183 while (offset
+ len
< buffer
.Length
&& buffer
[offset
+ len
] != 0) {
190 internal void ReadGuid(out Guid guid
) {
215 guid
= new Guid(a
, b
, c
, d
, e
, f
, g
, h
, i
, j
, k
);
218 internal string ReadString() {
220 while (offset
+ len
< buffer
.Length
&& buffer
[offset
+ len
] != 0) {
223 string result
= Encoding
.Unicode
.GetString(buffer
, offset
, len
);