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

Lzw decompression yields slightly incorrect data
https://forum.pdfsharp.net/viewtopic.php?f=3&t=3809
Page 1 of 1

Author:  MJLaukala [ Thu Jul 12, 2018 6:12 pm ]
Post subject:  Lzw decompression yields slightly incorrect data

I have 2 pdf documents, both generated via DOC1 Generate (versions 6.1.1373.0 and 6.6.6.60) that when opened with PdfSharp, return incorrect data from an lzw stream. As I have no issue with any other lzw streams from other pdf producers, this leads me to believe that the lzw compression they use may be bugged. However, Adobe Acrobat Reader DC opens the file with the correct data. As a result, my software needs to be able read it as well.

In a particular instance a "www." is outputing as "ww.".

I'm still working on getting permission to post the document but in the meantime, any information might be helpful for me to solve this on my own.

Author:  TH-Soft [ Thu Jul 12, 2018 8:01 pm ]
Post subject:  Re: Lzw decompression yields slightly incorrect data

PDFsharp uses the SharpZipLib for LZW compression/decompression.

Maybe try you stream with the latest version of SharpZipLib.

If the issue no longer exists with the latest SharpZipLib then PDFsharp needs an update.

If the issue also exists with the latest version, then maybe the SharpZipLib team can help to fix it.
It could be easier to get permission to share just a single stream instead of the complete PDF file.

Author:  MJLaukala [ Thu Jul 12, 2018 9:13 pm ]
Post subject:  Re: Lzw decompression yields slightly incorrect data

Are you sure about that?
My research lead me to LzwDecode.cs that is handling the lzw decoding.
Are there updates that are not yet reflected on GitHub?

I've attached a file containing the problem stream data.
Here is the code I used to decode the data.
Code:
byte[] data = File.ReadAllBytes(@"bytes.txt");
Console.WriteLine(string.Concat("Length: ", data.Length));

LzwDecode lzw = new LzwDecode();
string raw = lzw.DecodeToString(data);
File.WriteAllText(@"raw.txt", raw);


Line 133 in the generated raw.txt file, "(ww.ocfl.net/PayUtilities/) Tj", should be "(http://www.ocfl.net/PayUtilities/) Tj"
This line is the only data that I can specifically point out as being incorrect. But I do know that there is incorrect data elsewhere because when the page is edited and saved, adobe complains about an error and can't display the page correctly and Chrome displays it but with errors.

Attachments:
File comment: lzw stream
bytes.zip [3.82 KiB]
Downloaded 266 times

Author:  TH-Soft [ Fri Jul 13, 2018 5:11 pm ]
Post subject:  Re: Lzw decompression yields slightly incorrect data

MJLaukala wrote:
Are you sure about that?
No. Most likely I was on the wrong track, I had FlateDecode on my mind.
I think FlateDecoder also is LZW or maybe LZH or something like that.

So there are two options: Debugging the code or comparing the code with the specifications in the Adobe Reference (if there are any).
Or maybe find another implementation that is open source and compatible with an MIT license.
I can't say when I will have time to look after it. David is not available to look into it.

There is not much code, but still it can take some time to find out where it goes wrong.

Author:  MJLaukala [ Thu Aug 02, 2018 9:18 pm ]
Post subject:  Re: Lzw decompression yields slightly incorrect data

No worries. I'll look into it when I can. LZW seems pretty straight forward. There are specifications in the Adobe Reference and from what I could tell, your code follows that spec. I think the issue I am running into is another case of an out of spec document that Adobe PDF Reader reads just fine. Adobe PDF Reader seems to be very robust when it comes to reading out of spec and even very badly broken documents. I think Adobe PDF Reader's ability read these documents has made the waters of PDF spec very very muddy. If I come up with a suitable fix, I'll do a separate pull request on github than my standard "PDFSharp Fixes" pull request.

Author:  Gerben Vos [ Thu Aug 09, 2018 11:51 am ]
Post subject:  Re: Lzw decompression yields slightly incorrect data

It looks like the same issue as here: https://forum.pdfsharp.net/viewtopic.php?f=3&t=3410. I found it quite hard to debug, so I translated a LZW decoder from C instead, but I didn't post the patch for reasons. I'll see if I can still find it.

Author:  Gerben Vos [ Thu Aug 09, 2018 5:31 pm ]
Post subject:  Re: Lzw decompression yields slightly incorrect data

I posted my patched code to the thread mentioned in my previous post.

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