Skip to content

Commit

Permalink
Fix exponent validation
Browse files Browse the repository at this point in the history
References #25
  • Loading branch information
andreashuber-lawo committed May 23, 2016
1 parent 47c89d1 commit 1d3310f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
26 changes: 13 additions & 13 deletions Lawo.EmberPlusSharp/Ember/EmberReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ private static double ReadReal(ReadBuffer readBuffer, int length)

var firstContentsOctet = readBuffer[readBuffer.Index++];
--length;
long sign;
long signBits;
int exponentLength;

// 8.5.3 - 8.5.7, encoding must be base 2, so the bits 6 to 3 must be 0. Moreover, bits 8 to 7 must not
Expand All @@ -484,42 +484,42 @@ private static double ReadReal(ReadBuffer readBuffer, int length)

// 8.5.7.4 a)
case 0x80:
sign = 0;
signBits = 0;
exponentLength = 1;
break;
case 0xC0:
sign = long.MinValue;
signBits = long.MinValue;
exponentLength = 1;
break;

// 8.5.7.4 b)
case 0x81:
sign = 0;
signBits = 0;
exponentLength = 2;
break;
case 0xC1:
sign = long.MinValue;
signBits = long.MinValue;
exponentLength = 2;
break;

// 8.5.7.4 c)
case 0x82:
sign = 0;
signBits = 0;
exponentLength = 3;
break;
case 0xC2:
sign = long.MinValue;
signBits = long.MinValue;
exponentLength = 3;
break;

// 8.5.7.4 d)
case 0x83:
sign = 0;
signBits = 0;
exponentLength = readBuffer[readBuffer.Index++];
--length;
break;
case 0xC3:
sign = long.MinValue;
signBits = long.MinValue;
exponentLength = readBuffer[readBuffer.Index++];
--length;
break;
Expand All @@ -538,14 +538,13 @@ private static double ReadReal(ReadBuffer readBuffer, int length)

var exponent = Read8Bit(readBuffer, exponentLength, true);

if ((exponent < -Constants.DoubleExponentBias) || (exponent > Constants.DoubleExponentBias))
// https://en.wikipedia.org/wiki/Double-precision_floating-point_format
if ((exponent <= -Constants.DoubleExponentBias) || (exponent > Constants.DoubleExponentBias))
{
throw CreateEmberException(
"The exponent of the Real at position {0} exceeds the expected range.", position);
}

exponent = (exponent + Constants.DoubleExponentBias) << Constants.DoubleMantissaBits;

var mantissa = Read8Bit(readBuffer, mantissaLength, false);

if (mantissa == 0)
Expand All @@ -567,7 +566,8 @@ private static double ReadReal(ReadBuffer readBuffer, int length)
}

mantissa &= Constants.DoubleMantissaMask;
return BitConverter.Int64BitsToDouble(sign | exponent | mantissa);
var exponentBits = (exponent + Constants.DoubleExponentBias) << Constants.DoubleMantissaBits;
return BitConverter.Int64BitsToDouble(signBits | exponentBits | mantissa);
}

/// <summary>See <i>"X.690"</i><cite>X.690</cite>, chapter 8.1.3.</summary>
Expand Down
2 changes: 1 addition & 1 deletion Lawo.EmberPlusSharpTest/Ember/EmberReaderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ public void ExceptionTest()

AssertEmberException(
"The exponent of the Real at position 4 exceeds the expected range.",
0x60, 0x06, 0x09, 0x04, 0x81, 0xFC, 0x00, 0x01);
0x60, 0x06, 0x09, 0x04, 0x81, 0xFC, 0x01, 0x01);

AssertEmberException(
"The mantissa of the Real at position 4 is zero.", 0x60, 0x05, 0x09, 0x03, 0x80, 0x00, 0x00);
Expand Down

0 comments on commit 1d3310f

Please sign in to comment.