Free cookie consent management tool by TermsFeed Policy Generator

source: tags/3.3.11/HeuristicLab.ExtLibs/HeuristicLab.Cecil/0.9.5/Mono.Cecil-0.9.5/Symbols/Mono.Cecil.Pdb/Microsoft.Cci.Pdb/BitAccess.cs @ 13398

Last change on this file since 13398 was 11700, checked in by jkarder, 10 years ago

#2077: created branch and added first version

File size: 6.0 KB
Line 
1//-----------------------------------------------------------------------------
2//
3// Copyright (C) Microsoft Corporation.  All Rights Reserved.
4//
5//-----------------------------------------------------------------------------
6using System;
7using System.IO;
8using System.Text;
9
10namespace Microsoft.Cci.Pdb {
11  internal class BitAccess {
12
13    internal BitAccess(int capacity) {
14      this.buffer = new byte[capacity];
15      this.offset = 0;
16    }
17
18    internal byte[] Buffer {
19      get { return buffer; }
20    }
21    private byte[] buffer;
22
23    internal void FillBuffer(Stream stream, int capacity) {
24      MinCapacity(capacity);
25      stream.Read(buffer, 0, capacity);
26      offset = 0;
27    }
28
29    internal int Position {
30      get { return offset; }
31      set { offset = value; }
32    }
33    private int offset;
34
35    internal void WriteBuffer(Stream stream, int count) {
36      stream.Write(buffer, 0, count);
37    }
38
39    internal void MinCapacity(int capacity) {
40      if (buffer.Length < capacity) {
41        buffer = new byte[capacity];
42      }
43      offset = 0;
44    }
45
46    internal void Align(int alignment) {
47      while ((offset % alignment) != 0) {
48        offset++;
49      }
50    }
51
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);
57      offset += 4;
58    }
59
60    internal void WriteInt32(int[] values) {
61      for (int i = 0; i < values.Length; i++) {
62        WriteInt32(values[i]);
63      }
64    }
65
66    internal void WriteBytes(byte[] bytes) {
67      for (int i = 0; i < bytes.Length; i++) {
68        buffer[offset++] = bytes[i];
69      }
70    }
71
72    internal void ReadInt16(out short value) {
73      value = (short)((buffer[offset + 0] & 0xFF) |
74                            (buffer[offset + 1] << 8));
75      offset += 2;
76    }
77
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));
83      offset += 4;
84    }
85
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));
95      offset += 8;
96    }
97
98    internal void ReadUInt16(out ushort value) {
99      value = (ushort)((buffer[offset + 0] & 0xFF) |
100                             (buffer[offset + 1] << 8));
101      offset += 2;
102    }
103
104    internal void ReadUInt8(out byte value) {
105      value = (byte)((buffer[offset + 0] & 0xFF));
106      offset += 1;
107    }
108
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));
114      offset += 4;
115    }
116
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));
126      offset += 8;
127    }
128
129    internal void ReadInt32(int[] values) {
130      for (int i = 0; i < values.Length; i++) {
131        ReadInt32(out values[i]);
132      }
133    }
134
135    internal void ReadUInt32(uint[] values) {
136      for (int i = 0; i < values.Length; i++) {
137        ReadUInt32(out values[i]);
138      }
139    }
140
141    internal void ReadBytes(byte[] bytes) {
142      for (int i = 0; i < bytes.Length; i++) {
143        bytes[i] = buffer[offset++];
144      }
145    }
146
147    internal float ReadFloat() {
148      float result = BitConverter.ToSingle(buffer, offset);
149      offset += 4;
150      return result;
151    }
152
153    internal double ReadDouble() {
154      double result = BitConverter.ToDouble(buffer, offset);
155      offset += 8;
156      return result;
157    }
158
159    internal decimal ReadDecimal() {
160      int[] bits = new int[4];
161      this.ReadInt32(bits);
162      return new decimal(bits);
163    }
164
165    internal void ReadBString(out string value) {
166      ushort len;
167      this.ReadUInt16(out len);
168      value = Encoding.UTF8.GetString(buffer, offset, len);
169      offset += len;
170    }
171
172    internal void ReadCString(out string value) {
173      int len = 0;
174      while (offset + len < buffer.Length && buffer[offset + len] != 0) {
175        len++;
176      }
177      value = Encoding.UTF8.GetString(buffer, offset, len);
178      offset += len + 1;
179    }
180
181    internal void SkipCString(out string value) {
182      int len = 0;
183      while (offset + len < buffer.Length && buffer[offset + len] != 0) {
184        len++;
185      }
186      offset += len + 1;
187      value= null;
188    }
189
190    internal void ReadGuid(out Guid guid) {
191      uint a;
192      ushort b;
193      ushort c;
194      byte d;
195      byte e;
196      byte f;
197      byte g;
198      byte h;
199      byte i;
200      byte j;
201      byte k;
202
203      ReadUInt32(out a);
204      ReadUInt16(out b);
205      ReadUInt16(out c);
206      ReadUInt8(out d);
207      ReadUInt8(out e);
208      ReadUInt8(out f);
209      ReadUInt8(out g);
210      ReadUInt8(out h);
211      ReadUInt8(out i);
212      ReadUInt8(out j);
213      ReadUInt8(out k);
214
215      guid = new Guid(a, b, c, d, e, f, g, h, i, j, k);
216    }
217
218    internal string ReadString() {
219      int len = 0;
220      while (offset + len < buffer.Length && buffer[offset + len] != 0) {
221        len+=2;
222      }
223      string result = Encoding.Unicode.GetString(buffer, offset, len);
224      offset += len + 2;
225      return result;
226    }
227
228  }
229}
Note: See TracBrowser for help on using the repository browser.