From a7bcc81eb188faf83c4ca10c8a8de0a6b67c3595 Mon Sep 17 00:00:00 2001
From: Jonas Jenwald <jonas.jenwald@gmail.com>
Date: Fri, 16 Feb 2024 12:53:09 +0100
Subject: [PATCH] Add a dummy `beginMarkedContentProps` operator when optional
 content parsing fails (issue 17679)

---
 src/core/evaluator.js                  |  5 ++
 src/display/optional_content_config.js |  4 +-
 test/pdfs/.gitignore                   |  2 +
 test/pdfs/issue17679.pdf               | 73 ++++++++++++++++++++++++++
 test/pdfs/issue17679_2.pdf             | 73 ++++++++++++++++++++++++++
 test/test_manifest.json                | 54 +++++++++++++++++++
 6 files changed, 209 insertions(+), 2 deletions(-)
 create mode 100644 test/pdfs/issue17679.pdf
 create mode 100644 test/pdfs/issue17679_2.pdf

diff --git a/src/core/evaluator.js b/src/core/evaluator.js
index 5e62e444e..8117828a1 100644
--- a/src/core/evaluator.js
+++ b/src/core/evaluator.js
@@ -2184,6 +2184,7 @@ class PartialEvaluator {
           case OPS.beginMarkedContentProps:
             if (!(args[0] instanceof Name)) {
               warn(`Expected name for beginMarkedContentProps arg0=${args[0]}`);
+              operatorList.addOp(OPS.beginMarkedContentProps, ["OC", null]);
               continue;
             }
             if (args[0].name === "OC") {
@@ -2204,6 +2205,10 @@ class PartialEvaluator {
                       warn(
                         `getOperatorList - ignoring beginMarkedContentProps: "${reason}".`
                       );
+                      operatorList.addOp(OPS.beginMarkedContentProps, [
+                        "OC",
+                        null,
+                      ]);
                       return;
                     }
                     throw reason;
diff --git a/src/display/optional_content_config.js b/src/display/optional_content_config.js
index aca8c3605..12c9a7621 100644
--- a/src/display/optional_content_config.js
+++ b/src/display/optional_content_config.js
@@ -13,7 +13,7 @@
  * limitations under the License.
  */
 
-import { objectFromMap, unreachable, warn } from "../shared/util.js";
+import { info, objectFromMap, unreachable, warn } from "../shared/util.js";
 import { MurmurHash3_64 } from "../shared/murmurhash3.js";
 
 const INTERNAL = Symbol("INTERNAL");
@@ -130,7 +130,7 @@ class OptionalContentConfig {
       return true;
     }
     if (!group) {
-      warn("Optional content group not defined.");
+      info("Optional content group not defined.");
       return true;
     }
     if (group.type === "OCG") {
diff --git a/test/pdfs/.gitignore b/test/pdfs/.gitignore
index cd967df1d..5fd7f0c1d 100644
--- a/test/pdfs/.gitignore
+++ b/test/pdfs/.gitignore
@@ -52,6 +52,8 @@
 !issue7847_radial.pdf
 !issue8844.pdf
 !issue17056.pdf
+!issue17679.pdf
+!issue17679_2.pdf
 !issue14953.pdf
 !issue15367.pdf
 !issue15372.pdf
diff --git a/test/pdfs/issue17679.pdf b/test/pdfs/issue17679.pdf
new file mode 100644
index 000000000..664dac7aa
--- /dev/null
+++ b/test/pdfs/issue17679.pdf
@@ -0,0 +1,73 @@
+%PDF-1.4
+%����
+1 0 obj
+<< /OCProperties 3 0 R /Pages 4 0 R /Type /Catalog >>
+endobj
+2 0 obj
+<< /Author (Haik Lorenz) /CreationDate (D:20240131090542+01'00') /Creator (Notepad) /ModDate (D:20240131090542+01'00') /Producer (Notepad) >>
+endobj
+3 0 obj
+<< /D 5 0 R /OCGs [ 6 0 R 7 0 R ] >>
+endobj
+4 0 obj
+<< /Count 1 /Kids [ 8 0 R ] /Type /Pages >>
+endobj
+5 0 obj
+<< /Order [ 6 0 R 7 0 R ] >>
+endobj
+6 0 obj
+<< /Name (green) /Type /OCG >>
+endobj
+7 0 obj
+<< /Name (red) /Type /OCG >>
+endobj
+8 0 obj
+<< /Count 1 /Kids [ 9 0 R ] /Parent 4 0 R /Type /Pages >>
+endobj
+9 0 obj
+<< /Contents [ 10 0 R 11 0 R ] /MediaBox [ 0 0 100 100 ] /Parent 8 0 R /Resources << /ExtGState << /BBGS 12 0 R >> /ProcSet [ /PDF ] /XObject << /BBL 13 0 R /BBL1 14 0 R >> >> /Type /Page >>
+endobj
+10 0 obj
+<< /BBType /Melted /Length 46 >>
+stream
+q 1 0 0 1 0 0 cm q 1 0 0 1 0 0 cm /BBL Do Q Q endstream
+endobj
+11 0 obj
+<< /BBType /Melted /Length 56 >>
+stream
+q /BBGS gs 1 0 0 1 0 0 cm q 1 0 0 1 0 0 cm /BBL1 Do Q Q endstream
+endobj
+12 0 obj
+<< /BM /Darken /Type /ExtGState >>
+endobj
+13 0 obj
+<< /BBox [ 0 0 100 100 ] /FormType 1 /Matrix [ 1 0 0 1 0 0 ] /OC 6 0 R /Resources << /ProcSet [ /PDF /Text /ImageC ] >> /Subtype /Form /Type /XObject /Length 60 >>
+stream
+q
0 1 0 RG
0 1 0 rg
0 0 m
55 0 l
55 100 l
0 100 l
0 0 l
f
Q
endstream
+endobj
+14 0 obj
+<< /BBox [ 0 0 2979 1684 ] /FormType 1 /Group << /S /Transparency >> /Matrix [ 1 0 0 1 0 0 ] /OC 7 0 R /Resources << /ProcSet [ /PDF /Text /ImageC ] >> /Subtype /Form /Type /XObject /Length 231 >>
+stream
+q
1 0.6 0 RG
1 0.6 0 rg
45 0 m
100 0 l
100 10 l
45 10 l
45 0 l
f
/OC /oc1 BDC
1 0.3 0 RG
1 0.3 0 rg
45 10 m
100 10 l
100 50 l
45 50 l
45 10 l
f
EMC
/OC /oc7 BDC
1 0 0 RG
1 0 0 rg
45 50 m
100 50 l
100 100 l
45 100 l
45 50 l
f
EMC
Q
endstream
+endobj
+xref
+0 15
+0000000000 65535 f 
+0000000015 00000 n 
+0000000084 00000 n 
+0000000241 00000 n 
+0000000293 00000 n 
+0000000352 00000 n 
+0000000396 00000 n 
+0000000442 00000 n 
+0000000486 00000 n 
+0000000559 00000 n 
+0000000765 00000 n 
+0000000877 00000 n 
+0000000999 00000 n 
+0000001050 00000 n 
+0000001307 00000 n 
+trailer << /Info 2 0 R /Root 1 0 R /Size 15 /ID [<4a98efbce201625d6366fd6b63e1ee86><12e9607655dd86f67f55a66ae8b94a12>] >>
+startxref
+1768
+%%EOF
diff --git a/test/pdfs/issue17679_2.pdf b/test/pdfs/issue17679_2.pdf
new file mode 100644
index 000000000..9ec6d5a5f
--- /dev/null
+++ b/test/pdfs/issue17679_2.pdf
@@ -0,0 +1,73 @@
+%PDF-1.4
+%����
+1 0 obj
+<< /OCProperties 3 0 R /Pages 4 0 R /Type /Catalog >>
+endobj
+2 0 obj
+<< /Author (Haik Lorenz) /CreationDate (D:20240131090542+01'00') /Creator (Notepad) /ModDate (D:20240131090542+01'00') /Producer (Notepad) >>
+endobj
+3 0 obj
+<< /D 5 0 R /OCGs [ 6 0 R 7 0 R ] >>
+endobj
+4 0 obj
+<< /Count 1 /Kids [ 8 0 R ] /Type /Pages >>
+endobj
+5 0 obj
+<< /Order [ 6 0 R 7 0 R ] >>
+endobj
+6 0 obj
+<< /Name (green) /Type /OCG >>
+endobj
+7 0 obj
+<< /Name (red) /Type /OCG >>
+endobj
+8 0 obj
+<< /Count 1 /Kids [ 9 0 R ] /Parent 4 0 R /Type /Pages >>
+endobj
+9 0 obj
+<< /Contents [ 10 0 R 11 0 R ] /MediaBox [ 0 0 100 100 ] /Parent 8 0 R /Resources << /ExtGState << /BBGS 12 0 R >> /ProcSet [ /PDF ] /XObject << /BBL 13 0 R /BBL1 14 0 R >> >> /Type /Page >>
+endobj
+10 0 obj
+<< /BBType /Melted /Length 46 >>
+stream
+q 1 0 0 1 0 0 cm q 1 0 0 1 0 0 cm /BBL Do Q Q endstream
+endobj
+11 0 obj
+<< /BBType /Melted /Length 56 >>
+stream
+q /BBGS gs 1 0 0 1 0 0 cm q 1 0 0 1 0 0 cm /BBL1 Do Q Q endstream
+endobj
+12 0 obj
+<< /BM /Darken /Type /ExtGState >>
+endobj
+13 0 obj
+<< /BBox [ 0 0 100 100 ] /FormType 1 /Matrix [ 1 0 0 1 0 0 ] /OC 6 0 R /Resources << /ProcSet [ /PDF /Text /ImageC ] >> /Subtype /Form /Type /XObject /Length 60 >>
+stream
+q
0 1 0 RG
0 1 0 rg
0 0 m
55 0 l
55 100 l
0 100 l
0 0 l
f
Q
endstream
+endobj
+14 0 obj
+<< /BBox [ 0 0 2979 1684 ] /FormType 1 /Group << /S /Transparency >> /Matrix [ 1 0 0 1 0 0 ] /OC 7 0 R /Resources << /ProcSet [ /PDF /Text /ImageC ] >> /Subtype /Form /Type /XObject /Length 231 >>
+stream
+q
1 0.6 0 RG
1 0.6 0 rg
45 0 m
100 0 l
100 10 l
45 10 l
45 0 l
f
123 /oc1 BDC
1 0.3 0 RG
1 0.3 0 rg
45 10 m
100 10 l
100 50 l
45 50 l
45 10 l
f
EMC
456 /oc7 BDC
1 0 0 RG
1 0 0 rg
45 50 m
100 50 l
100 100 l
45 100 l
45 50 l
f
EMC
Q
endstream
+endobj
+xref
+0 15
+0000000000 65535 f 
+0000000015 00000 n 
+0000000084 00000 n 
+0000000241 00000 n 
+0000000293 00000 n 
+0000000352 00000 n 
+0000000396 00000 n 
+0000000442 00000 n 
+0000000486 00000 n 
+0000000559 00000 n 
+0000000765 00000 n 
+0000000877 00000 n 
+0000000999 00000 n 
+0000001050 00000 n 
+0000001307 00000 n 
+trailer << /Info 2 0 R /Root 1 0 R /Size 15 /ID [<4a98efbce201625d6366fd6b63e1ee86><12e9607655dd86f67f55a66ae8b94a12>] >>
+startxref
+1768
+%%EOF
diff --git a/test/test_manifest.json b/test/test_manifest.json
index 24f309967..d81f8d21a 100644
--- a/test/test_manifest.json
+++ b/test/test_manifest.json
@@ -3714,6 +3714,60 @@
     "lastPage": 1,
     "type": "eq"
   },
+  {
+    "id": "issue17679-initial-oc",
+    "file": "pdfs/issue17679.pdf",
+    "md5": "2a7ce8b31a751a0cb829863aa6cf553a",
+    "rounds": 1,
+    "type": "eq"
+  },
+  {
+    "id": "issue17679-no-green-oc",
+    "file": "pdfs/issue17679.pdf",
+    "md5": "2a7ce8b31a751a0cb829863aa6cf553a",
+    "rounds": 1,
+    "type": "eq",
+    "optionalContent": {
+      "6R": false
+    }
+  },
+  {
+    "id": "issue17679-no-red-oc",
+    "file": "pdfs/issue17679.pdf",
+    "md5": "2a7ce8b31a751a0cb829863aa6cf553a",
+    "rounds": 1,
+    "type": "eq",
+    "optionalContent": {
+      "7R": false
+    }
+  },
+  {
+    "id": "issue17679_2-initial-oc",
+    "file": "pdfs/issue17679_2.pdf",
+    "md5": "17f145098ea35d63f3fa7389716c1aca",
+    "rounds": 1,
+    "type": "eq"
+  },
+  {
+    "id": "issue17679_2-no-green-oc",
+    "file": "pdfs/issue17679_2.pdf",
+    "md5": "17f145098ea35d63f3fa7389716c1aca",
+    "rounds": 1,
+    "type": "eq",
+    "optionalContent": {
+      "6R": false
+    }
+  },
+  {
+    "id": "issue17679_2-no-red-oc",
+    "file": "pdfs/issue17679_2.pdf",
+    "md5": "17f145098ea35d63f3fa7389716c1aca",
+    "rounds": 1,
+    "type": "eq",
+    "optionalContent": {
+      "7R": false
+    }
+  },
   {
     "id": "issue1127-text",
     "file": "pdfs/issue1127.pdf",