News for hackers. Stuff that matters.
Re:What is the smallest possible valid PDF? by simonw on Friday March 20, 2020 @11:47AM (#22638510)
There's actually a good practical use-case for this: you're building software that needs to detect PDF files (as a smaller detail of what it dies, not its primary purpose) and you want to include a tiny one in your unit tests.

I did that here with tiny images in JPEG, PNG and GIF https://github.com/simonw/datasette-render-images/blob/maste...

Re:What is the smallest possible valid PDF? by anonydsfsfs on Friday March 20, 2020 @12:19PM (#22638791)
Re:What is the smallest possible valid PDF? by hombre_fatal on Friday March 20, 2020 @04:19PM (#22641249)
Kinda silly including 0-byte files which is most of them. Now you're just building a list of things that take empty input.

They should move those to a simple list in the README and reserve the repo for non-empty files.

Re:What is the smallest possible valid PDF? by lioeters on Friday March 20, 2020 @05:15PM (#22641731)
That is indeed a nice one, "Smallest possible syntactically valid files of different types".

Following the GitHub link (datasette-render-images) in the comment you replied to, there's a code comment with a link to the same library (small).

Re:What is the smallest possible valid PDF? by eesmith on Friday March 20, 2020 @12:05PM (#22638686)
Doesn't a PDF detector only need to check if the first few bytes are '%PDF-1.' ?

That is, do you need a "valid PDF" detector or "more likely a PDF than anything else" detector?

Re:What is the smallest possible valid PDF? by jahewson on Friday March 20, 2020 @10:27PM (#22643734)
Realistically, yes, but strictly speaking PDFs start with a footer and most PDF readers will accept highly corrupt files.
Re:What is the smallest possible valid PDF? by rovr138 on Friday March 20, 2020 @12:08PM (#22638711)
Depends. If you need to parse it, that might still cause errors.
Re:What is the smallest possible valid PDF? by eesmith on Friday March 20, 2020 @12:19PM (#22638799)
Agreed.

Though when I think of "detector" I think more of https://en.wikipedia.org/wiki/File_(command) and not something which verifies the file is in the correct format.

Re:What is the smallest possible valid PDF? by BiteCode_dev on Friday March 20, 2020 @01:07PM (#22639235)
Well...

file 'setupTests.ts' setupTests.ts: Java source, ASCII text

I wouldn't put too much trust in file.

Re:What is the smallest possible valid PDF? by klodolph on Friday March 20, 2020 @01:16PM (#22639335)
I don’t know what you expected. File is just there to give a good guess at a file’s format. There are a ton of reasons why this problem is hard, and there are reasons to make “file” less accurate in order to make the implementation simpler and more secure.

But it will work fine for PDFs, often enough.

Re:What is the smallest possible valid PDF? by jtvjan on Friday March 20, 2020 @01:25PM (#22639426)
The heuristic used for Java source looks like this:

0 regex \^import.*;$ Java source

Re:What is the smallest possible valid PDF? by meshy on Friday March 20, 2020 @02:41PM (#22640288)
That use is indeed exactly what inspired the question!
Re:What is the smallest possible valid PDF? by pmiller2 on Friday March 20, 2020 @07:57PM (#22642979)
I'm curious why you needed the absolute smallest PDF file for testing? As an intellectual exercise, golfing the PDF format sounds like a bit of fun, but given that https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pd... is 13KB, I would think you could load that in a test suite on pretty much any reasonable, non-embedded platform. And, why would you want to load it on an embedded platform, anyway?
Re:What is the smallest possible valid PDF? by kadoban on Saturday March 21, 2020 @01:26AM (#22644711)
Testing near/at boundary conditions is generally good practice.

A minimal size file could easily catch some cases where your code assumes some sort of structure (some flag, header, metadata structure, etc.) exists when it's possible it actually doesn't.