Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ByteReader.cs - github.com/mono/ikdasm.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7e3c6f5045d4970c7292a1381d17aea5edc2ac1c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
  Copyright (C) 2009 Jeroen Frijters

  This software is provided 'as-is', without any express or implied
  warranty.  In no event will the authors be held liable for any damages
  arising from the use of this software.

  Permission is granted to anyone to use this software for any purpose,
  including commercial applications, and to alter it and redistribute it
  freely, subject to the following restrictions:

  1. The origin of this software must not be misrepresented; you must not
     claim that you wrote the original software. If you use this software
     in a product, an acknowledgment in the product documentation would be
     appreciated but is not required.
  2. Altered source versions must be plainly marked as such, and must not be
     misrepresented as being the original software.
  3. This notice may not be removed or altered from any source distribution.

  Jeroen Frijters
  jeroen@frijters.net
  
*/
using System;
using System.Collections.Generic;
using System.Text;

namespace IKVM.Reflection.Reader
{
    sealed class ByteReader
    {
        private byte[] buffer;
        private int pos;
        private int end;

        internal ByteReader(byte[] buffer, int offset, int length)
        {
            this.buffer = buffer;
            this.pos = offset;
            this.end = pos + length;
        }

        internal static ByteReader FromBlob(byte[] blobHeap, int blob)
        {
            ByteReader br = new ByteReader(blobHeap, blob, 4);
            int length = br.ReadCompressedInt();
            br.end = br.pos + length;
            return br;
        }

        internal int Length
        {
            get { return end - pos; }
        }

        internal byte PeekByte()
        {
            if (pos == end)
                throw new BadImageFormatException();
            return buffer[pos];
        }

        internal byte ReadByte()
        {
            if (pos == end)
                throw new BadImageFormatException();
            return buffer[pos++];
        }

        internal byte[] ReadBytes(int count)
        {
            if (count < 0)
                throw new BadImageFormatException();
            if (end - pos < count)
                throw new BadImageFormatException();
            byte[] buf = new byte[count];
            Buffer.BlockCopy(buffer, pos, buf, 0, count);
            pos += count;
            return buf;
        }

        internal int ReadCompressedInt()
        {
            byte b1 = ReadByte();
            if (b1 <= 0x7F)
            {
                return b1;
            }
            else if ((b1 & 0xC0) == 0x80)
            {
                byte b2 = ReadByte();
                return ((b1 & 0x3F) << 8) | b2;
            }
            else
            {
                byte b2 = ReadByte();
                byte b3 = ReadByte();
                byte b4 = ReadByte();
                return ((b1 & 0x3F) << 24) + (b2 << 16) + (b3 << 8) + b4;
            }
        }

        internal string ReadString()
        {
            if (PeekByte() == 0xFF)
            {
                pos++;
                return null;
            }
            int length = ReadCompressedInt();
            string str = Encoding.UTF8.GetString(buffer, pos, length);
            pos += length;
            return str;
        }

        internal char ReadChar()
        {
            return (char)ReadInt16();
        }

        internal sbyte ReadSByte()
        {
            return (sbyte)ReadByte();
        }

        internal short ReadInt16()
        {
            if (end - pos < 2)
                throw new BadImageFormatException();
            byte b1 = buffer[pos++];
            byte b2 = buffer[pos++];
            return (short)(b1 | (b2 << 8));
        }

        internal ushort ReadUInt16()
        {
            return (ushort)ReadInt16();
        }

        internal int ReadInt32()
        {
            if (end - pos < 4)
                throw new BadImageFormatException();
            byte b1 = buffer[pos++];
            byte b2 = buffer[pos++];
            byte b3 = buffer[pos++];
            byte b4 = buffer[pos++];
            return (int)(b1 | (b2 << 8) | (b3 << 16) | (b4 << 24));
        }

        internal uint ReadUInt32()
        {
            return (uint)ReadInt32();
        }

        internal long ReadInt64()
        {
            ulong lo = ReadUInt32();
            ulong hi = ReadUInt32();
            return (long)(lo | (hi << 32));
        }

        internal ulong ReadUInt64()
        {
            return (ulong)ReadInt64();
        }

        internal float ReadSingle()
        {
            return SingleConverter.Int32BitsToSingle(ReadInt32());
        }

        internal double ReadDouble()
        {
            return BitConverter.Int64BitsToDouble(ReadInt64());
        }

        internal ByteReader Slice(int length)
        {
            if (end - pos < length)
                throw new BadImageFormatException();
            ByteReader br = new ByteReader(buffer, pos, length);
            pos += length;
            return br;
        }

        // NOTE this method only works if the original offset was aligned and for alignments that are a power of 2
        internal void Align(int alignment)
        {
            alignment--;
            pos = (pos + alignment) & ~alignment;
        }
    }
}