Merge pull request #2593 from ludovic-henry/coop-fix-native-to-managed
[mono.git] / mcs / tools / pdb2mdb / PdbFileHeader.cs
1 //-----------------------------------------------------------------------------
2 //
3 // Copyright (C) Microsoft Corporation.  All Rights Reserved.
4 //
5 //-----------------------------------------------------------------------------
6 using System;
7 using System.IO;
8 using System.Text;
9
10 namespace Microsoft.Cci.Pdb {
11   internal class PdbFileHeader {
12     internal PdbFileHeader(int pageSize) {
13       this.magic = new byte[32] {
14                 0x4D, 0x69, 0x63, 0x72, 0x6F, 0x73, 0x6F, 0x66, // "Microsof"
15                 0x74, 0x20, 0x43, 0x2F, 0x43, 0x2B, 0x2B, 0x20, // "t C/C++ "
16                 0x4D, 0x53, 0x46, 0x20, 0x37, 0x2E, 0x30, 0x30, // "MSF 7.00"
17                 0x0D, 0x0A, 0x1A, 0x44, 0x53, 0x00, 0x00, 0x00  // "^^^DS^^^"
18             };
19       this.pageSize = pageSize;
20       this.zero = 0;
21     }
22
23     internal PdbFileHeader(Stream reader, BitAccess bits) {
24       bits.MinCapacity(56);
25       reader.Seek(0, SeekOrigin.Begin);
26       bits.FillBuffer(reader, 56);
27
28       this.magic = new byte[32];
29       bits.ReadBytes(this.magic);                 //   0..31
30       bits.ReadInt32(out this.pageSize);          //  32..35
31       bits.ReadInt32(out this.freePageMap);       //  36..39
32       bits.ReadInt32(out this.pagesUsed);         //  40..43
33       bits.ReadInt32(out this.directorySize);     //  44..47
34       bits.ReadInt32(out this.zero);              //  48..51
35       bits.ReadInt32(out this.directoryRoot);     //  52..55
36     }
37
38     internal string Magic {
39       get { return StringFromBytesUTF8(magic); }
40     }
41
42     internal void Write(Stream writer, BitAccess bits) {
43       bits.MinCapacity(56);
44       bits.WriteBytes(magic);                     //   0..31
45       bits.WriteInt32(pageSize);                  //  32..35
46       bits.WriteInt32(freePageMap);               //  36..39
47       bits.WriteInt32(pagesUsed);                 //  40..43
48       bits.WriteInt32(directorySize);             //  44..47
49       bits.WriteInt32(zero);                      //  48..51
50       bits.WriteInt32(directoryRoot);             //  52..55
51
52       writer.Seek(0, SeekOrigin.Begin);
53       bits.WriteBuffer(writer, 56);
54     }
55
56     //////////////////////////////////////////////////// Helper Functions.
57     //
58     internal string StringFromBytesUTF8(byte[] bytes) {
59       return StringFromBytesUTF8(bytes, 0, bytes.Length);
60     }
61
62     internal string StringFromBytesUTF8(byte[] bytes, int offset, int length) {
63       for (int i = 0; i < length; i++) {
64         if (bytes[offset + i] < ' ') {
65           length = i;
66         }
67       }
68       return Encoding.UTF8.GetString(bytes, offset, length);
69     }
70
71     ////////////////////////////////////////////////////////////// Fields.
72     //
73     internal readonly byte[] magic;
74     internal readonly int pageSize;
75     internal int freePageMap;
76     internal int pagesUsed;
77     internal int directorySize;
78     internal readonly int zero;
79     internal int directoryRoot;
80   }
81
82 }