Skip to content

Commit c0c7f90

Browse files
committed
Fix #890, #915
1 parent 6a9df2e commit c0c7f90

File tree

3 files changed

+32
-74
lines changed

3 files changed

+32
-74
lines changed

cmd/pdfcpu/process.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1664,6 +1664,7 @@ func processListBoxesCommand(conf *model.Configuration) {
16641664
ensurePDFExtension(inFile)
16651665
}
16661666
process(cli.ListBoxesCommand(inFile, selectedPages, nil, conf))
1667+
return
16671668
}
16681669

16691670
pb, err := api.PageBoundariesFromBoxList(flag.Arg(0))
@@ -1808,6 +1809,7 @@ func processListAnnotationsCommand(conf *model.Configuration) {
18081809

18091810
process(cli.ListAnnotationsCommand(inFile, selectedPages, conf))
18101811
}
1812+
18111813
func processRemoveAnnotationsCommand(conf *model.Configuration) {
18121814
if len(flag.Args()) < 1 {
18131815
fmt.Fprintf(os.Stderr, "usage: %s\n", usageAnnotsRemove)
@@ -2290,6 +2292,7 @@ func processNDownCommand(conf *model.Configuration) {
22902292
}
22912293

22922294
process(cli.NDownCommand(inFile, outDir, outFile, selectedPages, n, cut, conf))
2295+
return
22932296
}
22942297

22952298
// pdfcpu ndown description n inFile outDir outFile

pkg/pdfcpu/model/parse.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1105,13 +1105,20 @@ func detectMarker(line, marker string) int {
11051105
return i
11061106
}
11071107

1108+
if i+len(marker) >= len(line) {
1109+
return -1
1110+
}
1111+
11081112
// Skip until keyword is followed by eol.
11091113
for c := line[i+len(marker)]; c != 0x0A && c != 0x0D; {
11101114
line = line[i+len(marker):]
11111115
i = strings.Index(line, marker)
11121116
if i < 0 {
11131117
return i
11141118
}
1119+
if i+len(marker) >= len(line) {
1120+
return -1
1121+
}
11151122
}
11161123

11171124
return i

pkg/pdfcpu/read.go

Lines changed: 22 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1019,81 +1019,31 @@ func scanTrailerDictStart(s *bufio.Scanner, line *string) error {
10191019
}
10201020

10211021
func scanTrailerDictRemainder(s *bufio.Scanner, line string, buf bytes.Buffer) (string, error) {
1022-
var err error
1023-
var i, j, k int
1024-
1025-
buf.WriteString(line)
1026-
buf.WriteString("\x0a")
1027-
// log.Read.Printf("scanTrailer dictBuf after start tag: <%s>\n", line)
1028-
1029-
line = line[2:]
1030-
1031-
for {
1022+
var (
1023+
i int
1024+
err error
1025+
)
10321026

1033-
if len(line) == 0 {
1034-
if line, err = scanLine(s); err != nil {
1035-
return "", err
1036-
}
1037-
buf.WriteString(line)
1038-
buf.WriteString("\x0a")
1039-
// log.Read.Printf("scanTrailer dictBuf next line: <%s>\n", line)
1040-
}
1041-
1042-
i = strings.Index(line, "<<")
1043-
if i < 0 {
1044-
// No <<
1045-
j = strings.Index(line, ">>")
1046-
if j >= 0 {
1047-
// Yes >>
1048-
if k == 0 {
1049-
// Check for dict
1050-
ok, err := isDict(buf.String())
1051-
if err == nil && ok {
1052-
return buf.String(), nil
1053-
}
1054-
} else {
1055-
k--
1056-
}
1057-
line = line[j+2:]
1058-
continue
1059-
}
1060-
// No >>
1061-
line, err = scanLine(s)
1062-
if err != nil {
1063-
return "", err
1064-
}
1065-
buf.WriteString(line)
1066-
buf.WriteString("\x0a")
1067-
// log.Read.Printf("scanTrailer dictBuf next line: <%s>\n", line)
1068-
} else {
1069-
// Yes <<
1070-
j = strings.Index(line, ">>")
1071-
if j < 0 {
1072-
// No >>
1073-
k++
1074-
line = line[i+2:]
1075-
} else {
1076-
// Yes >>
1077-
if i < j {
1078-
// handle <<
1079-
k++
1080-
line = line[i+2:]
1081-
} else {
1082-
// handle >>
1083-
if k == 0 {
1084-
// Check for dict
1085-
ok, err := isDict(buf.String())
1086-
if err == nil && ok {
1087-
return buf.String(), nil
1088-
}
1089-
} else {
1090-
k--
1091-
}
1092-
line = line[j+2:]
1093-
}
1094-
}
1027+
for i = strings.Index(line, "startxref"); i < 0; {
1028+
if log.ReadEnabled() {
1029+
log.Read.Printf("line: <%s>\n", line)
10951030
}
1031+
buf.WriteString(line)
1032+
buf.WriteString("\x0a")
1033+
if line, err = scanLine(s); err != nil {
1034+
return "", err
1035+
}
1036+
i = strings.Index(line, "startxref")
10961037
}
1038+
1039+
line = line[:i]
1040+
if log.ReadEnabled() {
1041+
log.Read.Printf("line: <%s>\n", line)
1042+
}
1043+
buf.WriteString(line[:i])
1044+
buf.WriteString("\x0a")
1045+
1046+
return buf.String(), nil
10971047
}
10981048

10991049
func scanTrailer(s *bufio.Scanner, line string) (string, error) {
@@ -1102,12 +1052,10 @@ func scanTrailer(s *bufio.Scanner, line string) (string, error) {
11021052
log.Read.Printf("line: <%s>\n", line)
11031053
}
11041054

1105-
// Scan for dict start tag "<<".
11061055
if err := scanTrailerDictStart(s, &line); err != nil {
11071056
return "", err
11081057
}
11091058

1110-
// Scan for dict end tag ">>" but account for inner dicts.
11111059
return scanTrailerDictRemainder(s, line, buf)
11121060
}
11131061

0 commit comments

Comments
 (0)