Skip to content

Commit

Permalink
Fix reading and writing of signed compressed integers
Browse files Browse the repository at this point in the history
  • Loading branch information
jbevain committed Jun 17, 2011
1 parent 2e6f7c2 commit 481ca4f
Showing 1 changed file with 23 additions and 6 deletions.
29 changes: 23 additions & 6 deletions Mono.Cecil.PE/ByteBuffer.cs
Expand Up @@ -140,11 +140,16 @@ public uint ReadCompressedUInt32 ()


public int ReadCompressedInt32 () public int ReadCompressedInt32 ()
{ {
var value = (int) ReadCompressedUInt32 (); var value = (int) (ReadCompressedUInt32 () >> 1);

if ((value & 1) == 0)
return (value & 1) != 0 return value;
? -(value >> 1) if (value < 0x40)
: value >> 1; return value - 0x40;
if (value < 0x2000)
return value - 0x2000;
if (value < 0x10000000)
return value - 0x10000000;
return value - 0x20000000;
} }


public float ReadSingle () public float ReadSingle ()
Expand Down Expand Up @@ -267,7 +272,19 @@ public void WriteCompressedUInt32 (uint value)


public void WriteCompressedInt32 (int value) public void WriteCompressedInt32 (int value)
{ {
WriteCompressedUInt32 ((uint) ((value < 0) ? ((-value) << 1) | 1 : value << 1)); if (value >= 0) {
WriteCompressedUInt32 ((uint) (value << 1));
return;
}

if (value > -0x40)
value = 0x40 + value;
else if (value >= -0x2000)
value = 0x2000 + value;
else if (value >= -0x20000000)
value = 0x20000000 + value;

WriteCompressedUInt32 ((uint) ((value << 1) | 1));
} }


public void WriteBytes (byte [] bytes) public void WriteBytes (byte [] bytes)
Expand Down

0 comments on commit 481ca4f

Please sign in to comment.