@@ -19,6 +19,7 @@ package pdfcpu
1919import (
2020 "bufio"
2121 "bytes"
22+ "fmt"
2223 "io"
2324 "os"
2425 "sort"
@@ -780,6 +781,98 @@ func scanLine(s *bufio.Scanner) (string, error) {
780781 return s .Text (), nil
781782}
782783
784+ func scanTrailer (s * bufio.Scanner , line string ) (string , error ) {
785+
786+ var buf bytes.Buffer
787+ var err error
788+ var i , j , k int
789+
790+ log .Read .Printf ("line: <%s>\n " , line )
791+
792+ // Scan for dict start tag "<<".
793+ for {
794+ i = strings .Index (line , "<<" )
795+ if i >= 0 {
796+ break
797+ }
798+ line , err = scanLine (s )
799+ log .Read .Printf ("line: <%s>\n " , line )
800+ if err != nil {
801+ return "" , err
802+ }
803+ }
804+
805+ line = line [i :]
806+ buf .WriteString (line )
807+ buf .WriteString (" " )
808+ log .Read .Printf ("scanTrailer dictBuf after start tag: <%s>\n " , line )
809+
810+ // Scan for dict end tag ">>" but account for inner dicts.
811+ line = line [2 :]
812+
813+ for {
814+
815+ if len (line ) == 0 {
816+ line , err = scanLine (s )
817+ if err != nil {
818+ return "" , err
819+ }
820+ buf .WriteString (line )
821+ buf .WriteString (" " )
822+ log .Read .Printf ("scanTrailer dictBuf next line: <%s>\n " , line )
823+ }
824+
825+ i = strings .Index (line , "<<" )
826+ if i < 0 {
827+ // No <<
828+ j = strings .Index (line , ">>" )
829+ if j >= 0 {
830+ // Yes >>
831+ if k == 0 {
832+ return buf .String (), nil
833+ }
834+ k --
835+ println (k )
836+ line = line [j + 2 :]
837+ continue
838+ }
839+ // No >>
840+ line , err = scanLine (s )
841+ if err != nil {
842+ return "" , err
843+ }
844+ buf .WriteString (line )
845+ buf .WriteString (" " )
846+ log .Read .Printf ("scanTrailer dictBuf next line: <%s>\n " , line )
847+ } else {
848+ // Yes <<
849+ j = strings .Index (line , ">>" )
850+ if j < 0 {
851+ // No >>
852+ k ++
853+ println (k )
854+ line = line [i + 2 :]
855+ } else {
856+ // Yes >>
857+ if i < j {
858+ // handle <<
859+ k ++
860+ println (k )
861+ line = line [i + 2 :]
862+ } else {
863+ // handle >>
864+ if k == 0 {
865+ return buf .String (), nil
866+ }
867+ k --
868+ println (k )
869+ line = line [j + 2 :]
870+ }
871+ }
872+ }
873+ }
874+ }
875+
783876func scanTrailerDict (s * bufio.Scanner , startTag bool ) (string , error ) {
784877
785878 var buf bytes.Buffer
@@ -864,16 +957,9 @@ func parseXRefSection(s *bufio.Scanner, ctx *Context) (*int64, error) {
864957 log .Read .Printf ("line (len %d) <%s>\n " , len (line ), line )
865958 }
866959
867- // Unless trailerDict already scanned into trailerString
868- if strings .Index (trailerString , ">>" ) == - 1 {
869-
870- // scan lines until we have the complete trailer dict: << ... >>
871- trailerDictString , err := scanTrailerDict (s , strings .Index (trailerString , "<<" ) > 0 )
872- if err != nil {
873- return nil , err
874- }
875-
876- trailerString += trailerDictString
960+ trailerString , err = scanTrailer (s , trailerString )
961+ if err != nil {
962+ return nil , err
877963 }
878964
879965 log .Read .Printf ("parseXRefSection: trailerString: (len:%d) <%s>\n " , len (trailerString ), trailerString )
@@ -2179,6 +2265,7 @@ func setupEncryptionKey(ctx *Context, encryptDictObjNr int) error {
21792265 if err != nil {
21802266 return err
21812267 }
2268+ fmt .Printf ("read: id = %0X\n " , enc .ID )
21822269
21832270 var ok bool
21842271
@@ -2189,7 +2276,7 @@ func setupEncryptionKey(ctx *Context, encryptDictObjNr int) error {
21892276 }
21902277
21912278 // If the owner password does not match we generally move on if the user password is correct
2192- // unless we need to insist on a correct owner password.
2279+ // unless we need to insist on a correct owner password due to the specific command in progress .
21932280 if ! ok && needsOwnerAndUserPassword (ctx .Mode ) {
21942281 return errors .New ("owner password authentication error" )
21952282 }
0 commit comments