PageRenderTime 13ms CodeModel.GetById 1ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/symbols/pdb/Microsoft.Cci.Pdb/BitAccess.cs

http://github.com/jbevain/cecil
C# | 253 lines | 194 code | 37 blank | 22 comment | 16 complexity | d2e7e71e82b0a0f6026b6db546a5eacf MD5 | raw file
  1// Copyright (c) Microsoft. All rights reserved.
  2// Licensed under the MIT license. See LICENSE file in the project root for full license information.
  3
  4using System;
  5using System.IO;
  6using System.Text;
  7
  8namespace Microsoft.Cci.Pdb {
  9  internal class BitAccess {
 10
 11    internal BitAccess(int capacity) {
 12      this.buffer = new byte[capacity];
 13    }
 14
 15    internal BitAccess(byte[] buffer) {
 16      this.buffer = buffer;
 17      offset = 0;
 18    }
 19
 20    internal byte[] Buffer {
 21      get { return buffer; }
 22    }
 23    private byte[] buffer;
 24
 25    internal void FillBuffer(Stream stream, int capacity) {
 26      MinCapacity(capacity);
 27      stream.Read(buffer, 0, capacity);
 28      offset = 0;
 29    }
 30
 31    internal void Append(Stream stream, int count) {
 32      int newCapacity = offset + count;
 33      if (buffer.Length < newCapacity) {
 34        byte[] newBuffer = new byte[newCapacity];
 35        Array.Copy(buffer, newBuffer, buffer.Length);
 36        buffer = newBuffer;
 37      }
 38      stream.Read(buffer, offset, count);
 39      offset += count;
 40    }
 41
 42    internal int Position {
 43      get { return offset; }
 44      set { offset = value; }
 45    }
 46    private int offset;
 47
 48    //internal void WriteBuffer(Stream stream, int count) {
 49    //  stream.Write(buffer, 0, count);
 50    //}
 51
 52    internal void MinCapacity(int capacity) {
 53      if (buffer.Length < capacity) {
 54        buffer = new byte[capacity];
 55      }
 56      offset = 0;
 57    }
 58
 59    internal void Align(int alignment) {
 60      while ((offset % alignment) != 0) {
 61        offset++;
 62      }
 63    }
 64
 65    //internal void WriteInt32(int value) {
 66    //  buffer[offset + 0] = (byte)value;
 67    //  buffer[offset + 1] = (byte)(value >> 8);
 68    //  buffer[offset + 2] = (byte)(value >> 16);
 69    //  buffer[offset + 3] = (byte)(value >> 24);
 70    //  offset += 4;
 71    //}
 72
 73    //internal void WriteInt32(int[] values) {
 74    //  for (int i = 0; i < values.Length; i++) {
 75    //    WriteInt32(values[i]);
 76    //  }
 77    //}
 78
 79    //internal void WriteBytes(byte[] bytes) {
 80    //  for (int i = 0; i < bytes.Length; i++) {
 81    //    buffer[offset++] = bytes[i];
 82    //  }
 83    //}
 84
 85    internal void ReadInt16(out short value) {
 86      value = (short)((buffer[offset + 0] & 0xFF) |
 87                            (buffer[offset + 1] << 8));
 88      offset += 2;
 89    }
 90
 91    internal void ReadInt8(out sbyte value) {
 92      value = (sbyte)buffer[offset];
 93      offset += 1;
 94    }
 95
 96    internal void ReadInt32(out int value) {
 97      value = (int)((buffer[offset + 0] & 0xFF) |
 98                          (buffer[offset + 1] << 8) |
 99                          (buffer[offset + 2] << 16) |
100                          (buffer[offset + 3] << 24));
101      offset += 4;
102    }
103
104    internal void ReadInt64(out long value) {
105      value = (long)(((ulong)buffer[offset + 0] & 0xFF) |
106                           ((ulong)buffer[offset + 1] << 8) |
107                           ((ulong)buffer[offset + 2] << 16) |
108                           ((ulong)buffer[offset + 3] << 24) |
109                           ((ulong)buffer[offset + 4] << 32) |
110                           ((ulong)buffer[offset + 5] << 40) |
111                           ((ulong)buffer[offset + 6] << 48) |
112                           ((ulong)buffer[offset + 7] << 56));
113      offset += 8;
114    }
115
116    internal void ReadUInt16(out ushort value) {
117      value = (ushort)((buffer[offset + 0] & 0xFF) |
118                             (buffer[offset + 1] << 8));
119      offset += 2;
120    }
121
122    internal void ReadUInt8(out byte value) {
123      value = (byte)((buffer[offset + 0] & 0xFF));
124      offset += 1;
125    }
126
127    internal void ReadUInt32(out uint value) {
128      value = (uint)((buffer[offset + 0] & 0xFF) |
129                           (buffer[offset + 1] << 8) |
130                           (buffer[offset + 2] << 16) |
131                           (buffer[offset + 3] << 24));
132      offset += 4;
133    }
134
135    internal void ReadUInt64(out ulong value) {
136      value = (ulong)(((ulong)buffer[offset + 0] & 0xFF) |
137                           ((ulong)buffer[offset + 1] << 8) |
138                           ((ulong)buffer[offset + 2] << 16) |
139                           ((ulong)buffer[offset + 3] << 24) |
140                           ((ulong)buffer[offset + 4] << 32) |
141                           ((ulong)buffer[offset + 5] << 40) |
142                           ((ulong)buffer[offset + 6] << 48) |
143                           ((ulong)buffer[offset + 7] << 56));
144      offset += 8;
145    }
146
147    internal void ReadInt32(int[] values) {
148      for (int i = 0; i < values.Length; i++) {
149        ReadInt32(out values[i]);
150      }
151    }
152
153    internal void ReadUInt32(uint[] values) {
154      for (int i = 0; i < values.Length; i++) {
155        ReadUInt32(out values[i]);
156      }
157    }
158
159    internal void ReadBytes(byte[] bytes) {
160      for (int i = 0; i < bytes.Length; i++) {
161        bytes[i] = buffer[offset++];
162      }
163    }
164
165    internal float ReadFloat() {
166      float result = BitConverter.ToSingle(buffer, offset);
167      offset += 4;
168      return result;
169    }
170
171    internal double ReadDouble() {
172      double result = BitConverter.ToDouble(buffer, offset);
173      offset += 8;
174      return result;
175    }
176
177    internal decimal ReadDecimal() {
178      int[] bits = new int[4];
179      this.ReadInt32(bits);
180      return new decimal(bits[2], bits[3], bits[1], bits[0] < 0, (byte)((bits[0] & 0x00FF0000) >> 16));
181    }
182
183    internal void ReadBString(out string value) {
184      ushort len;
185      this.ReadUInt16(out len);
186      value = Encoding.UTF8.GetString(buffer, offset, len);
187      offset += len;
188    }
189
190    internal string ReadBString(int len) {
191      var result = Encoding.UTF8.GetString(buffer, offset, len);
192      offset += len;
193      return result;
194    }
195
196    internal void ReadCString(out string value) {
197      int len = 0;
198      while (offset + len < buffer.Length && buffer[offset + len] != 0) {
199        len++;
200      }
201      value = Encoding.UTF8.GetString(buffer, offset, len);
202      offset += len + 1;
203    }
204
205    internal void SkipCString(out string value) {
206      int len = 0;
207      while (offset + len < buffer.Length && buffer[offset + len] != 0) {
208        len++;
209      }
210      offset += len + 1;
211      value= null;
212    }
213
214    internal void ReadGuid(out Guid guid) {
215      uint a;
216      ushort b;
217      ushort c;
218      byte d;
219      byte e;
220      byte f;
221      byte g;
222      byte h;
223      byte i;
224      byte j;
225      byte k;
226
227      ReadUInt32(out a);
228      ReadUInt16(out b);
229      ReadUInt16(out c);
230      ReadUInt8(out d);
231      ReadUInt8(out e);
232      ReadUInt8(out f);
233      ReadUInt8(out g);
234      ReadUInt8(out h);
235      ReadUInt8(out i);
236      ReadUInt8(out j);
237      ReadUInt8(out k);
238
239      guid = new Guid(a, b, c, d, e, f, g, h, i, j, k);
240    }
241
242    internal string ReadString() {
243      int len = 0;
244      while (offset + len < buffer.Length && buffer[offset + len] != 0) {
245        len+=2;
246      }
247      string result = Encoding.Unicode.GetString(buffer, offset, len);
248      offset += len + 2;
249      return result;
250    }
251
252  }
253}