@@ -2444,7 +2444,6 @@ internal static byte[] ReadLogicalBytes(Stream stream, E57PhysicalOffset start,
24442444 return buffer ;
24452445 }
24462446 }
2447-
24482447 /// <summary>
24492448 /// Read given number of logical bytes (excluding 32-bit CRC at the end of each 1024 byte page) from stream,
24502449 /// starting at logical stream position 'start'.
@@ -2544,15 +2543,18 @@ private static byte[] GetImageBytes(XElement root, string elementName, Stream st
25442543 var element = GetElement ( root , elementName ) ;
25452544 if ( element == null ) return null ;
25462545 var blob = E57Blob . Parse ( GetElement ( root , elementName ) ) ;
2547- // Mysterious! Why is +16 required?
2548- var start = blob . FileOffset . Value + 16 ;
2549- // If start falls in checksum region move it forward the the next page
2550- if ( start % 1024 >= 1020 )
2551- {
2552- start += 1024 - ( start % 1024 ) ;
2553- }
2554- var offest = new E57PhysicalOffset ( start ) ;
2555- return ReadLogicalBytes ( stream , offest , ( int ) blob . Length ) ;
2546+
2547+ // The blob.FileOffset is the physical offset to the start of a Blob
2548+ // binary section and not directly to the binary blob data.
2549+ // And such a section has a header of exactly 16 bytes.
2550+ // Because the header could cross a CRC (in which case we would have
2551+ // to skip 20 bytes instead of 16), we immediately convert the physical offset
2552+ // to a logical offset, and skip the 16 bytes header in "logical address space",
2553+ // which will automatically handle all CRC-crossing corner cases for us.
2554+ // Finally, ReadLogicalBytes already has an overload for E57LogicalOffset,
2555+ // so we don't even have to convert back to a physical offset.
2556+ var start = ( E57LogicalOffset ) blob . FileOffset + 16 ;
2557+ return ReadLogicalBytes ( stream , start , ( int ) blob . Length ) ;
25562558 }
25572559 private static double ? GetFloatOrInteger ( XElement root , string elementName , bool required )
25582560 {
0 commit comments