| 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 |
|
| 4 |
using System; |
| 5 |
using System.IO; |
| 6 |
using System.Linq; |
| 7 |
using System.Text; |
| 8 |
|
| 9 |
namespace Microsoft.Cci.Pdb { |
| 10 |
internal class PdbFileHeader { |
| 11 |
private readonly byte[] windowsPdbMagic = new byte[32] { |
| 12 |
0x4D, 0x69, 0x63, 0x72, 0x6F, 0x73, 0x6F, 0x66, // "Microsof" |
| 13 |
0x74, 0x20, 0x43, 0x2F, 0x43, 0x2B, 0x2B, 0x20, // "t C/C++ " |
| 14 |
0x4D, 0x53, 0x46, 0x20, 0x37, 0x2E, 0x30, 0x30, // "MSF 7.00" |
| 15 |
0x0D, 0x0A, 0x1A, 0x44, 0x53, 0x00, 0x00, 0x00 // "^^^DS^^^" |
| 16 |
}; |
| 17 |
|
| 18 |
//internal PdbFileHeader(int pageSize) { |
| 19 |
// this.magic = new byte[32] { |
| 20 |
// 0x4D, 0x69, 0x63, 0x72, 0x6F, 0x73, 0x6F, 0x66, // "Microsof" |
| 21 |
// 0x74, 0x20, 0x43, 0x2F, 0x43, 0x2B, 0x2B, 0x20, // "t C/C++ " |
| 22 |
// 0x4D, 0x53, 0x46, 0x20, 0x37, 0x2E, 0x30, 0x30, // "MSF 7.00" |
| 23 |
// 0x0D, 0x0A, 0x1A, 0x44, 0x53, 0x00, 0x00, 0x00 // "^^^DS^^^" |
| 24 |
// }; |
| 25 |
// this.pageSize = pageSize; |
| 26 |
//} |
| 27 |
|
| 28 |
internal PdbFileHeader(Stream reader, BitAccess bits) { |
| 29 |
bits.MinCapacity(56); |
| 30 |
reader.Seek(0, SeekOrigin.Begin); |
| 31 |
bits.FillBuffer(reader, 52); |
| 32 |
|
| 33 |
this.magic = new byte[32]; |
| 34 |
bits.ReadBytes(this.magic); // 0..31 |
| 35 |
bits.ReadInt32(out this.pageSize); // 32..35 |
| 36 |
bits.ReadInt32(out this.freePageMap); // 36..39 |
| 37 |
bits.ReadInt32(out this.pagesUsed); // 40..43 |
| 38 |
bits.ReadInt32(out this.directorySize); // 44..47 |
| 39 |
bits.ReadInt32(out this.zero); // 48..51 |
| 40 |
|
| 41 |
if (!this.magic.SequenceEqual(windowsPdbMagic)) |
| 42 |
{ |
| 43 |
throw new PdbException("The PDB file is not recognized as a Windows PDB file"); |
| 44 |
} |
| 45 |
|
| 46 |
int directoryPages = ((((directorySize + pageSize - 1) / pageSize) * 4) + pageSize - 1) / pageSize; |
| 47 |
this.directoryRoot = new int[directoryPages]; |
| 48 |
bits.FillBuffer(reader, directoryPages * 4); |
| 49 |
bits.ReadInt32(this.directoryRoot); |
| 50 |
} |
| 51 |
|
| 52 |
//internal string Magic { |
| 53 |
// get { return StringFromBytesUTF8(magic); } |
| 54 |
//} |
| 55 |
|
| 56 |
//internal void Write(Stream writer, BitAccess bits) { |
| 57 |
// bits.MinCapacity(pageSize); |
| 58 |
// bits.WriteBytes(magic); // 0..31 |
| 59 |
// bits.WriteInt32(pageSize); // 32..35 |
| 60 |
// bits.WriteInt32(freePageMap); // 36..39 |
| 61 |
// bits.WriteInt32(pagesUsed); // 40..43 |
| 62 |
// bits.WriteInt32(directorySize); // 44..47 |
| 63 |
// bits.WriteInt32(zero); // 48..51 |
| 64 |
// bits.WriteInt32(directoryRoot); // 52..55 |
| 65 |
|
| 66 |
// writer.Seek(0, SeekOrigin.Begin); |
| 67 |
// bits.WriteBuffer(writer, pageSize); |
| 68 |
//} |
| 69 |
|
| 70 |
//////////////////////////////////////////////////// Helper Functions. |
| 71 |
// |
| 72 |
//internal static string StringFromBytesUTF8(byte[] bytes) { |
| 73 |
// return StringFromBytesUTF8(bytes, 0, bytes.Length); |
| 74 |
//} |
| 75 |
|
| 76 |
//internal static string StringFromBytesUTF8(byte[] bytes, int offset, int length) { |
| 77 |
// for (int i = 0; i < length; i++) { |
| 78 |
// if (bytes[offset + i] < ' ') { |
| 79 |
// length = i; |
| 80 |
// } |
| 81 |
// } |
| 82 |
// return Encoding.UTF8.GetString(bytes, offset, length); |
| 83 |
//} |
| 84 |
|
| 85 |
////////////////////////////////////////////////////////////// Fields. |
| 86 |
// |
| 87 |
internal readonly byte[] magic; |
| 88 |
internal readonly int pageSize; |
| 89 |
internal int freePageMap; |
| 90 |
internal int pagesUsed; |
| 91 |
internal int directorySize; |
| 92 |
internal readonly int zero; |
| 93 |
internal int[] directoryRoot; |
| 94 |
} |
| 95 |
|
| 96 |
} |