Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More flexible logic for libtiff #25

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

lzambarda
Copy link

Hey there! This is probably too big as a single PR, but some changes can be cherry-picked for the benefit of the project!

a962e84 addresses issue #22 . The previous header check logic was too strict and also had a bug (7dfab7e)

Some other changes are also to address #20 which seems to be a limitation in the current implementation. I left a comment in the code but also adding here the original resource which made me appyl the change: https://stackoverflow.com/questions/11896858/does-the-eof-in-a-pdf-have-to-appear-within-the-last-1024-bytes-of-the-file

In e3c03b5 I fixed a bug not correctly surfacing panics as errors inside the library.

I have also added a go.mod file to please replace.
Hope this can help!

read.go Outdated
@@ -129,7 +129,7 @@ func NewReader(f io.ReaderAt, size int64) (*Reader, error) {
func NewReaderEncrypted(f io.ReaderAt, size int64, pw func() string) (*Reader, error) {
buf := make([]byte, 10)
f.ReadAt(buf, 0)
if !bytes.HasPrefix(buf, []byte("%PDF-1.")) || buf[7] < '0' || buf[7] > '7' || buf[8] != '\r' && buf[8] != '\n' {
if !bytes.HasPrefix(buf, []byte("%PDF-1.")) || buf[7] < '0' || buf[7] > '7' || buf[8] != '\r' || buf[8] != '\n' {

This comment was marked as outdated.

This comment was marked as outdated.

// NewReaderEncrypted opens a file for reading, using the data in f with the given total size.
// If the PDF is encrypted, NewReaderEncrypted calls pw repeatedly to obtain passwords
// to try. If pw returns the empty string, NewReaderEncrypted stops trying to decrypt
// the file and returns an error.
func NewReaderEncrypted(f io.ReaderAt, size int64, pw func() string) (*Reader, error) {
buf := make([]byte, 10)
const headerLen = 11
buf := make([]byte, 11)
Copy link

@romanpickl romanpickl Mar 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use headerLen otherwise delete const?

// headerRegexp is used to check the validity of the header line of a PDF.
// This should be able to support extra spaces between the version and the
// newline (as inserted by libtiff/tiff2pdf) as well as supporting CRLF and LF.
var headerRegexp = regexp.MustCompile(`^%PDF-1\.[0-7]\s*\r?\n`)
Copy link

@romanpickl romanpickl Mar 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think %PDF-1.7\r
is a valid case as well.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in my tests i changed this to
^%PDF-1\.[0-7][ ]*[\r\n]+
need to check the spec again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants