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 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
|
using System;
namespace Mono.Debugger
{
// <summary>
// This is a generic binary reader.
// </summary>
[Serializable]
public class TargetBinaryReader : TargetBinaryAccess
{
public TargetBinaryReader (byte[] contents, TargetMemoryInfo target_info)
: base (new TargetBlob (contents, target_info))
{ }
public TargetBinaryReader (TargetBlob blob)
: base (blob)
{
}
public byte PeekByte (long pos)
{
return blob.Contents[pos];
}
public byte PeekByte ()
{
return blob.Contents[pos];
}
public byte ReadByte ()
{
return blob.Contents[pos++];
}
public short PeekInt16 (long pos)
{
if (swap)
return ((short) (blob.Contents[pos+1] |
(blob.Contents[pos] << 8)));
else
return ((short) (blob.Contents[pos] |
(blob.Contents[pos+1] << 8)));
}
public short PeekInt16 ()
{
return PeekInt16 (pos);
}
public short ReadInt16 ()
{
short retval = PeekInt16 (pos);
pos += 2;
return retval;
}
public int PeekInt32 (long pos)
{
if (swap)
return (blob.Contents[pos+3] |
(blob.Contents[pos+2] << 8) |
(blob.Contents[pos+1] << 16) |
(blob.Contents[pos] << 24));
else
return (blob.Contents[pos] |
(blob.Contents[pos+1] << 8) |
(blob.Contents[pos+2] << 16) |
(blob.Contents[pos+3] << 24));
}
public int PeekInt32 ()
{
return PeekInt32 (pos);
}
public int ReadInt32 ()
{
int retval = PeekInt32 (pos);
pos += 4;
return retval;
}
public uint PeekUInt32 (long pos)
{
if (swap)
return ((uint) blob.Contents[pos+3] |
((uint) blob.Contents[pos+2] << 8) |
((uint) blob.Contents[pos+1] << 16) |
((uint) blob.Contents[pos] << 24));
else
return ((uint) blob.Contents[pos] |
((uint) blob.Contents[pos+1] << 8) |
((uint) blob.Contents[pos+2] << 16) |
((uint) blob.Contents[pos+3] << 24));
}
public uint PeekUInt32 ()
{
return PeekUInt32 (pos);
}
public uint ReadUInt32 ()
{
uint retval = PeekUInt32 (pos);
pos += 4;
return retval;
}
public long PeekInt64 (long pos)
{
uint ret_low, ret_high;
if (swap) {
ret_low = (uint) (blob.Contents[pos+7] |
(blob.Contents[pos+6] << 8) |
(blob.Contents[pos+5] << 16) |
(blob.Contents[pos+4] << 24));
ret_high = (uint) (blob.Contents[pos+3] |
(blob.Contents[pos+2] << 8) |
(blob.Contents[pos+1] << 16) |
(blob.Contents[pos] << 24));
} else {
ret_low = (uint) (blob.Contents[pos] |
(blob.Contents[pos+1] << 8) |
(blob.Contents[pos+2] << 16) |
(blob.Contents[pos+3] << 24));
ret_high = (uint) (blob.Contents[pos+4] |
(blob.Contents[pos+5] << 8) |
(blob.Contents[pos+6] << 16) |
(blob.Contents[pos+7] << 24));
}
return (long) ((((ulong) ret_high) << 32) | ret_low);
}
public long PeekInt64 ()
{
return PeekInt64 (pos);
}
public long ReadInt64 ()
{
long retval = PeekInt64 (pos);
pos += 8;
return retval;
}
public long PeekAddress (long pos)
{
if (AddressSize == 8)
return PeekInt64 (pos);
else
return PeekUInt32 (pos);
}
public long PeekAddress ()
{
return PeekAddress (pos);
}
public long ReadAddress ()
{
if (AddressSize == 8)
return ReadInt64 ();
else
return ReadUInt32 ();
}
public TargetAddress ReadTargetAddress ()
{
return new TargetAddress (TargetMemoryInfo.AddressDomain, ReadAddress ());
}
public string PeekString (long pos)
{
int length = 0;
while (blob.Contents[pos+length] != 0)
length++;
char[] retval = new char [length];
for (int i = 0; i < length; i++)
retval [i] = (char) blob.Contents[pos+i];
return new String (retval);
}
public string PeekString ()
{
return PeekString (pos);
}
public string ReadString ()
{
string retval = PeekString (pos);
pos += retval.Length + 1;
return retval;
}
public byte[] PeekBuffer (long offset, int size)
{
byte[] buffer = new byte [size];
Array.Copy (blob.Contents, (int) offset, buffer, 0, size);
return buffer;
}
public byte[] PeekBuffer (int size)
{
return PeekBuffer (pos, size);
}
public byte[] ReadBuffer (int size)
{
byte[] buffer = new byte [size];
Array.Copy (blob.Contents, pos, buffer, 0, size);
pos += size;
return buffer;
}
public int PeekLeb128 (long pos)
{
int size;
return PeekLeb128 (pos, out size);
}
public int PeekLeb128 (long pos, out int size)
{
int ret = 0;
int shift = 0;
byte b;
size = 0;
do {
b = PeekByte (pos + size);
size++;
ret = ret | ((b & 0x7f) << shift);
shift += 7;
} while ((b & 0x80) == 0x80);
return ret;
}
public int ReadLeb128 ()
{
int size;
int retval = PeekLeb128 (pos, out size);
pos += size;
return retval;
}
public int PeekSLeb128 (long pos)
{
int size;
return PeekSLeb128 (pos, out size);
}
public int PeekSLeb128 (long pos, out int size)
{
int ret = 0;
int shift = 0;
byte b;
size = 0;
do {
b = (byte) PeekByte (pos + size);
size++;
ret = ret | ((b & 0x7f) << shift);
shift += 7;
} while ((b & 0x80) == 0x80);
if ((shift < 31) && ((b & 0x40) == 0x40))
ret |= - (1 << shift);
return ret;
}
public int ReadSLeb128 ()
{
int size;
int retval = PeekSLeb128 (pos, out size);
pos += size;
return retval;
}
public long ReadInteger (int size)
{
switch (size) {
case 1:
return ReadByte ();
case 2:
return ReadInt16 ();
case 4:
return ReadInt32 ();
case 8:
return ReadInt64 ();
default:
throw new TargetMemoryException (
"Unknown integer size " + size);
}
}
}
}
|