PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

Infinite Loop in ScanHexadecimalString + Solution
https://forum.pdfsharp.net/viewtopic.php?f=3&t=1818
Page 1 of 1

Author:  Karill [ Thu Oct 13, 2011 7:41 am ]
Post subject:  Infinite Loop in ScanHexadecimalString + Solution

Hey there,
i've already read 2 times about it in this forum, and someone even posted a solution too, but i wanted to show you my exactly problem and the solution to it.

First of all to say: PDF is created by ReportViewer.LocalReport.Render()-Method and saved as MemoryStream, so i cannot provide it here :)
(Plus i cannot easily save it WHEN the error occurs... when i am able to save it, the whole file already changed again and error "disappeared"... more or less, but read on to understand)

As the topic says, i got stuck in an infinite loop in ScanHexadecimalString-Method (Lexer.cs). My Problem there was a special character, that is number 174 (the "R" in a circle)
simple "skipping" the unreadable character, as the other solution (and the only real one) i found, fixed it, but might lead to "unexpected behavior" or whatever.
So i debugged a little bit and noticed, that the Lexer.ReadStream()-Method returns a wrong byte[]
Somehow i wondered, because it starts with CR and LF and the last 2 characters of the real stream (looked it up in Notepad++) were missing.
I found out that after the "stream" keyword (or whatever you call it) there is a single whitespace. Because of this, the CR and LF isn't skipped, how it was intended there (that is what the comment says)

Quick fix for me:
change
Code:
public byte[] ReadStream(int length)
{
   int pos = 0;
   // Skip new line behind «stream»
   if(this.currChar == Chars.CR)
   {
      if(this.nextChar == Chars.LF)
         pos = this.idxChar + 2;
      else
         pos = this.idxChar + 1;
   }
   else
      pos = this.idxChar + 1;

   this.pdf.Position = pos;
   byte[] bytes = new byte[length];
   int read = this.pdf.Read(bytes, 0, length);
   Debug.Assert(read == length);
   // synchronize idxChar etc.
   this.Position = pos + length;
   return bytes;
}
to
Code:
public byte[] ReadStream(int length)
{
   int pos = 0;
   // Skip new line behind «stream»
   MoveToNonWhiteSpace();
   if(this.currChar == Chars.CR)
   {
      if(this.nextChar == Chars.LF)
         pos = this.idxChar + 2;
      else
         pos = this.idxChar + 1;
   }
   else
      pos = this.idxChar + 1;

   this.pdf.Position = pos;
   byte[] bytes = new byte[length];
   int read = this.pdf.Read(bytes, 0, length);
   Debug.Assert(read == length);
   // synchronize idxChar etc.
   this.Position = pos + length;
   return bytes;
}
in Lexer.cs

still i don't understand why it only stucks this one time, because all streams are read wrong...
anyway, should be fixed :)

//EDIT:
short after posting, i even realised you could now remove the whole if-else part, because MoveToNonWhitespace() skips CR and LF too :D
Code:
// Skip new line behind «stream»
MoveToNonWhiteSpace();
byte[] bytes = new byte[length];
int read = this.pdf.Read(bytes, 0, length);
Debug.Assert(read == length);
// synchronize idxChar etc.
this.Position = pos + length;
return bytes;
(just an idea and not tested yet)

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/