Coverage Report

Created: 2026-03-31 07:04

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/xpdf-4.06/xpdf/OptionalContent.cc
Line
Count
Source
1
//========================================================================
2
//
3
// OptionalContent.cc
4
//
5
// Copyright 2008-2013 Glyph & Cog, LLC
6
//
7
//========================================================================
8
9
#include <aconf.h>
10
11
#include "gmempp.h"
12
#include "GString.h"
13
#include "GList.h"
14
#include "Error.h"
15
#include "Object.h"
16
#include "PDFDoc.h"
17
#include "TextString.h"
18
#include "OptionalContent.h"
19
20
//------------------------------------------------------------------------
21
22
10.0k
#define ocPolicyAllOn  1
23
7.64k
#define ocPolicyAnyOn  2
24
0
#define ocPolicyAnyOff 3
25
10.0k
#define ocPolicyAllOff 4
26
27
//------------------------------------------------------------------------
28
29
// Max depth of nested visibility expressions.  This is used to catch
30
// infinite loops in the visibility expression object structure.
31
639
#define visibilityExprRecursionLimit 50
32
33
// Max depth of nested display nodes.  This is used to catch infinite
34
// loops in the "Order" object structure.
35
1.46M
#define displayNodeRecursionLimit 50
36
37
//------------------------------------------------------------------------
38
39
15.3k
OptionalContent::OptionalContent(PDFDoc *doc) {
40
15.3k
  Object *ocProps;
41
15.3k
  Object ocgList, defView, uad, obj1, obj2, obj3, obj4;
42
15.3k
  Ref ref1;
43
15.3k
  OptionalContentGroup *ocg;
44
15.3k
  int i, j;
45
46
15.3k
  xref = doc->getXRef();
47
15.3k
  ocgs = new GList();
48
15.3k
  display = NULL;
49
50
15.3k
  if ((ocProps = doc->getCatalog()->getOCProperties())->isDict()) {
51
849
    if (ocProps->dictLookup("OCGs", &ocgList)->isArray()) {
52
53
      //----- read the OCG list
54
43.9k
      for (i = 0; i < ocgList.arrayGetLength(); ++i) {
55
43.1k
  if (ocgList.arrayGetNF(i, &obj1)->isRef()) {
56
11.2k
    ref1 = obj1.getRef();
57
11.2k
    obj1.fetch(xref, &obj2);
58
11.2k
    if ((ocg = OptionalContentGroup::parse(&ref1, &obj2))) {
59
5.99k
      ocgs->append(ocg);
60
5.99k
    }
61
11.2k
    obj2.free();
62
11.2k
  }
63
43.1k
  obj1.free();
64
43.1k
      }
65
66
      //----- read the default viewing OCCD
67
792
      if (ocProps->dictLookup("D", &defView)->isDict()) {
68
69
  //----- read the usage app dicts
70
625
  if (defView.dictLookup("AS", &obj1)->isArray()) {
71
3.07k
    for (i = 0; i < obj1.arrayGetLength(); ++i) {
72
3.01k
      if (obj1.arrayGet(i, &uad)->isDict()) {
73
239
        if (uad.dictLookup("Event", &obj2)->isName("View")) {
74
37
    if (uad.dictLookup("OCGs", &obj3)->isArray()) {
75
287
      for (j = 0; j < obj3.arrayGetLength(); ++j) {
76
253
        if (obj3.arrayGetNF(j, &obj4)->isRef()) {
77
75
          ref1 = obj4.getRef();
78
75
          if ((ocg = findOCG(&ref1))) {
79
30
      ocg->setInViewUsageAppDict();
80
30
          }
81
75
        }
82
253
        obj4.free();
83
253
      }
84
34
    }
85
37
    obj3.free();
86
37
        }
87
239
        obj2.free();
88
239
      }
89
3.01k
      uad.free();
90
3.01k
    }
91
61
  }
92
625
  obj1.free();
93
94
  //----- initial state from OCCD
95
625
  if (defView.dictLookup("OFF", &obj1)->isArray()) {
96
27.6k
    for (i = 0; i < obj1.arrayGetLength(); ++i) {
97
27.4k
      if (obj1.arrayGetNF(i, &obj2)->isRef()) {
98
21.6k
        ref1 = obj2.getRef();
99
21.6k
        if ((ocg = findOCG(&ref1))) {
100
8.36k
    ocg->setState(gFalse);
101
13.2k
        } else {
102
13.2k
    error(errSyntaxError, -1,
103
13.2k
          "Invalid OCG reference in OFF array in default viewing OCCD");
104
13.2k
        }
105
21.6k
      }
106
27.4k
      obj2.free();
107
27.4k
    }
108
156
  }
109
625
  obj1.free();
110
111
  //----- initial state from OCG usage dict
112
4.82k
  for (i = 0; i < ocgs->getLength(); ++i) {
113
4.20k
    ocg = (OptionalContentGroup *)ocgs->get(i);
114
4.20k
    if (ocg->getInViewUsageAppDict() &&
115
29
        ocg->getViewState() != ocUsageUnset) {
116
1
      ocg->setState(ocg->getViewState() == ocUsageOn);
117
1
    }
118
4.20k
  }
119
120
  //----- display order
121
625
  if (defView.dictLookup("Order", &obj1)->isArray()) {
122
496
    display = OCDisplayNode::parse(&obj1, this, xref);
123
496
  }
124
625
  obj1.free();
125
126
625
      } else {
127
167
  error(errSyntaxError, -1, "Missing or invalid default viewing OCCD");
128
167
      }
129
792
      defView.free();
130
131
792
    }
132
849
    ocgList.free();
133
849
  }
134
135
15.3k
  if (!display) {
136
14.8k
    display = new OCDisplayNode();
137
14.8k
  }
138
15.3k
}
139
140
15.3k
OptionalContent::~OptionalContent() {
141
15.3k
  deleteGList(ocgs, OptionalContentGroup);
142
15.3k
  delete display;
143
15.3k
}
144
145
0
int OptionalContent::getNumOCGs() {
146
0
  return ocgs->getLength();
147
0
}
148
149
0
OptionalContentGroup *OptionalContent::getOCG(int idx) {
150
0
  return (OptionalContentGroup *)ocgs->get(idx);
151
0
}
152
153
119k
OptionalContentGroup *OptionalContent::findOCG(Ref *ref) {
154
119k
  OptionalContentGroup *ocg;
155
119k
  int i;
156
157
1.27M
  for (i = 0; i < ocgs->getLength(); ++i) {
158
1.17M
    ocg = (OptionalContentGroup *)ocgs->get(i);
159
1.17M
    if (ocg->matches(ref)) {
160
19.0k
      return ocg;
161
19.0k
    }
162
1.17M
  }
163
100k
  return NULL;
164
119k
}
165
166
87.4k
GBool OptionalContent::evalOCObject(Object *obj, GBool *visible) {
167
87.4k
  OptionalContentGroup *ocg;
168
87.4k
  int policy;
169
87.4k
  Ref ref;
170
87.4k
  Object obj2, obj3, obj4, obj5;
171
87.4k
  GBool gotOCG;
172
87.4k
  int i;
173
174
87.4k
  if (obj->isNull()) {
175
78.4k
    return gFalse;
176
78.4k
  }
177
8.95k
  if (obj->isRef()) {
178
8.71k
    ref = obj->getRef();
179
8.71k
    if ((ocg = findOCG(&ref))) {
180
63
      *visible = ocg->getState();
181
63
      return gTrue;
182
63
    }
183
8.71k
  }
184
8.89k
  obj->fetch(xref, &obj2);
185
8.89k
  if (!obj2.isDict("OCMD")) {
186
3.80k
    obj2.free();
187
3.80k
    return gFalse;
188
3.80k
  }
189
5.08k
  if (obj2.dictLookup("VE", &obj3)->isArray()) {
190
73
    *visible = evalOCVisibilityExpr(&obj3, 0);
191
73
    obj3.free();
192
5.01k
  } else {
193
5.01k
    obj3.free();
194
5.01k
    policy = ocPolicyAnyOn;
195
5.01k
    if (obj2.dictLookup("P", &obj3)->isName()) {
196
0
      if (obj3.isName("AllOn")) {
197
0
  policy = ocPolicyAllOn;
198
0
      } else if (obj3.isName("AnyOn")) {
199
0
  policy = ocPolicyAnyOn;
200
0
      } else if (obj3.isName("AnyOff")) {
201
0
  policy = ocPolicyAnyOff;
202
0
      } else if (obj3.isName("AllOff")) {
203
0
  policy = ocPolicyAllOff;
204
0
      }
205
0
    }
206
5.01k
    obj3.free();
207
5.01k
    obj2.dictLookupNF("OCGs", &obj3);
208
5.01k
    ocg = NULL;
209
5.01k
    if (obj3.isRef()) {
210
36
      ref = obj3.getRef();
211
36
      ocg = findOCG(&ref);
212
36
    }
213
5.01k
    if (ocg) {
214
10
      *visible = (policy == ocPolicyAllOn || policy == ocPolicyAnyOn) ?
215
10
             ocg->getState() : !ocg->getState();
216
5.00k
    } else {
217
5.00k
      *visible = policy == ocPolicyAllOn || policy == ocPolicyAllOff;
218
5.00k
      if (!obj3.fetch(xref, &obj4)->isArray()) {
219
1.03k
  obj4.free();
220
1.03k
  obj3.free();
221
1.03k
  obj2.free();
222
1.03k
  return gFalse;
223
1.03k
      }
224
3.96k
      gotOCG = gFalse;
225
21.1k
      for (i = 0; i < obj4.arrayGetLength(); ++i) {
226
17.2k
  obj4.arrayGetNF(i, &obj5);
227
17.2k
  if (obj5.isRef()) {
228
4.39k
    ref = obj5.getRef();
229
4.39k
    if ((ocg = findOCG(&ref))) {
230
2.62k
      gotOCG = gTrue;
231
2.62k
      switch (policy) {
232
0
      case ocPolicyAllOn:
233
0
        *visible = *visible && ocg->getState();
234
0
        break;
235
2.62k
      case ocPolicyAnyOn:
236
2.62k
        *visible = *visible || ocg->getState();
237
2.62k
        break;
238
0
      case ocPolicyAnyOff:
239
0
        *visible = *visible || !ocg->getState();
240
0
        break;
241
0
      case ocPolicyAllOff:
242
0
        *visible = *visible && !ocg->getState();
243
0
        break;
244
2.62k
      }
245
2.62k
    }
246
4.39k
  }
247
17.2k
  obj5.free();
248
17.2k
      }
249
3.96k
      if (!gotOCG) {
250
  // if the "OCGs" array is "empty or contains references only
251
  // to null or deleted objects", this OCMD doesn't have any
252
  // effect
253
1.54k
  obj4.free();
254
1.54k
  obj3.free();
255
1.54k
  obj2.free();
256
1.54k
  return gFalse;
257
1.54k
      }
258
2.42k
      obj4.free();
259
2.42k
    }
260
2.43k
    obj3.free();
261
2.43k
  }
262
2.50k
  obj2.free();
263
2.50k
  return gTrue;
264
5.08k
}
265
266
639
GBool OptionalContent::evalOCVisibilityExpr(Object *expr, int recursion) {
267
639
  OptionalContentGroup *ocg;
268
639
  Object expr2, op, obj;
269
639
  Ref ref;
270
639
  GBool ret;
271
639
  int i;
272
273
639
  if (recursion > visibilityExprRecursionLimit) {
274
0
    error(errSyntaxError, -1,
275
0
    "Loop detected in optional content visibility expression");
276
0
    return gTrue;
277
0
  }
278
639
  if (expr->isRef()) {
279
92
    ref = expr->getRef();
280
92
    if ((ocg = findOCG(&ref))) {
281
42
      return ocg->getState();
282
42
    }
283
92
  }
284
597
  expr->fetch(xref, &expr2);
285
597
  if (!expr2.isArray() || expr2.arrayGetLength() < 1) {
286
498
    error(errSyntaxError, -1,
287
498
    "Invalid optional content visibility expression");
288
498
    expr2.free();
289
498
    return gTrue;
290
498
  }
291
99
  expr2.arrayGet(0, &op);
292
99
  if (op.isName("Not")) {
293
19
    if (expr2.arrayGetLength() == 2) {
294
13
      expr2.arrayGetNF(1, &obj);
295
13
      ret = !evalOCVisibilityExpr(&obj, recursion + 1);
296
13
      obj.free();
297
13
    } else {
298
6
      error(errSyntaxError, -1,
299
6
      "Invalid optional content visibility expression");
300
6
      ret = gTrue;
301
6
    }
302
80
  } else if (op.isName("And")) {
303
44
    ret = gTrue;
304
588
    for (i = 1; i < expr2.arrayGetLength() && ret; ++i) {
305
544
      expr2.arrayGetNF(i, &obj);
306
544
      ret = evalOCVisibilityExpr(&obj, recursion + 1);
307
544
      obj.free();
308
544
    }
309
44
  } else if (op.isName("Or")) {
310
9
    ret = gFalse;
311
18
    for (i = 1; i < expr2.arrayGetLength() && !ret; ++i) {
312
9
      expr2.arrayGetNF(i, &obj);
313
9
      ret = evalOCVisibilityExpr(&obj, recursion + 1);
314
9
      obj.free();
315
9
    }
316
27
  } else {
317
27
    error(errSyntaxError, -1,
318
27
    "Invalid optional content visibility expression");
319
27
    ret = gTrue;
320
27
  }
321
99
  op.free();
322
99
  expr2.free();
323
99
  return ret;
324
597
}
325
326
//------------------------------------------------------------------------
327
328
11.2k
OptionalContentGroup *OptionalContentGroup::parse(Ref *refA, Object *obj) {
329
11.2k
  TextString *nameA;
330
11.2k
  Object obj1, obj2, obj3;
331
11.2k
  OCUsageState viewStateA, printStateA;
332
333
11.2k
  if (!obj->isDict()) {
334
3.84k
    return NULL;
335
3.84k
  }
336
7.40k
  if (!obj->dictLookup("Name", &obj1)->isString()) {
337
1.41k
    error(errSyntaxError, -1, "Missing or invalid Name in OCG");
338
1.41k
    obj1.free();
339
1.41k
    return NULL;
340
1.41k
  }
341
5.99k
  nameA = new TextString(obj1.getString());
342
5.99k
  obj1.free();
343
344
5.99k
  viewStateA = printStateA = ocUsageUnset;
345
5.99k
  if (obj->dictLookup("Usage", &obj1)->isDict()) {
346
114
    if (obj1.dictLookup("View", &obj2)->isDict()) {
347
42
      if (obj2.dictLookup("ViewState", &obj3)->isName()) {
348
16
  if (obj3.isName("ON")) {
349
12
    viewStateA = ocUsageOn;
350
12
  } else {
351
4
    viewStateA = ocUsageOff;
352
4
  }
353
16
      }
354
42
      obj3.free();
355
42
    }
356
114
    obj2.free();
357
114
    if (obj1.dictLookup("Print", &obj2)->isDict()) {
358
46
      if (obj2.dictLookup("PrintState", &obj3)->isName()) {
359
17
  if (obj3.isName("ON")) {
360
13
    printStateA = ocUsageOn;
361
13
  } else {
362
4
    printStateA = ocUsageOff;
363
4
  }
364
17
      }
365
46
      obj3.free();
366
46
    }
367
114
    obj2.free();
368
114
  }
369
5.99k
  obj1.free();
370
371
5.99k
  return new OptionalContentGroup(refA, nameA, viewStateA, printStateA);
372
7.40k
}
373
374
OptionalContentGroup::OptionalContentGroup(Ref *refA, TextString *nameA,
375
             OCUsageState viewStateA,
376
5.99k
             OCUsageState printStateA) {
377
5.99k
  ref = *refA;
378
5.99k
  name = nameA;
379
5.99k
  viewState = viewStateA;
380
5.99k
  printState = printStateA;
381
5.99k
  state = gTrue;
382
5.99k
  inViewUsageAppDict = gFalse;
383
5.99k
}
384
385
5.99k
OptionalContentGroup::~OptionalContentGroup() {
386
5.99k
  delete name;
387
5.99k
}
388
389
1.17M
GBool OptionalContentGroup::matches(Ref *refA) {
390
1.17M
  return refA->num == ref.num && refA->gen == ref.gen;
391
1.17M
}
392
393
0
Unicode *OptionalContentGroup::getName() {
394
0
  return name->getUnicode();
395
0
}
396
397
0
int OptionalContentGroup::getNameLength() {
398
0
  return name->getLength();
399
0
}
400
401
//------------------------------------------------------------------------
402
403
OCDisplayNode *OCDisplayNode::parse(Object *obj, OptionalContent *oc,
404
1.46M
            XRef *xref, int recursion) {
405
1.46M
  Object obj2, obj3;
406
1.46M
  Ref ref;
407
1.46M
  OptionalContentGroup *ocgA;
408
1.46M
  OCDisplayNode *node, *child;
409
1.46M
  int i;
410
411
1.46M
  if (recursion > displayNodeRecursionLimit) {
412
191k
    error(errSyntaxError, -1, "Loop detected in optional content order");
413
191k
    return NULL;
414
191k
  }
415
1.27M
  if (obj->isRef()) {
416
84.7k
    ref = obj->getRef();
417
84.7k
    if ((ocgA = oc->findOCG(&ref))) {
418
7.95k
      return new OCDisplayNode(ocgA);
419
7.95k
    }
420
84.7k
  }
421
1.26M
  obj->fetch(xref, &obj2);
422
1.26M
  if (!obj2.isArray()) {
423
1.04M
    obj2.free();
424
1.04M
    return NULL;
425
1.04M
  }
426
219k
  i = 0;
427
219k
  if (obj2.arrayGetLength() >= 1) {
428
219k
    if (obj2.arrayGet(0, &obj3)->isString()) {
429
9.93k
      node = new OCDisplayNode(obj3.getString());
430
9.93k
      i = 1;
431
209k
    } else {
432
209k
      node = new OCDisplayNode();
433
209k
    }
434
219k
    obj3.free();
435
219k
  } else {
436
687
    node = new OCDisplayNode();
437
687
  }
438
1.68M
  for (; i < obj2.arrayGetLength(); ++i) {
439
1.46M
    obj2.arrayGetNF(i, &obj3);
440
1.46M
    if ((child = OCDisplayNode::parse(&obj3, oc, xref, recursion + 1))) {
441
227k
      if (!child->ocg && !child->name && node->getNumChildren() > 0) {
442
0
  if (child->getNumChildren() > 0) {
443
0
    node->getChild(node->getNumChildren() - 1)->
444
0
              addChildren(child->takeChildren());
445
0
  }
446
0
  delete child;
447
227k
      } else {
448
227k
  node->addChild(child);
449
227k
      }
450
227k
    }
451
1.46M
    obj3.free();
452
1.46M
  }
453
219k
  obj2.free();
454
219k
  return node;
455
1.26M
}
456
457
224k
OCDisplayNode::OCDisplayNode() {
458
224k
  name = new TextString();
459
224k
  ocg = NULL;
460
224k
  parent = NULL;
461
224k
  children = NULL;
462
224k
}
463
464
9.93k
OCDisplayNode::OCDisplayNode(GString *nameA) {
465
9.93k
  name = new TextString(nameA);
466
9.93k
  ocg = NULL;
467
9.93k
  children = NULL;
468
9.93k
}
469
470
7.95k
OCDisplayNode::OCDisplayNode(OptionalContentGroup *ocgA) {
471
7.95k
  name = new TextString(ocgA->name);
472
7.95k
  ocg = ocgA;
473
7.95k
  children = NULL;
474
7.95k
}
475
476
227k
void OCDisplayNode::addChild(OCDisplayNode *child) {
477
227k
  if (!children) {
478
154k
    children = new GList();
479
154k
  }
480
227k
  children->append(child);
481
227k
  child->parent = this;
482
227k
}
483
484
0
void OCDisplayNode::addChildren(GList *childrenA) {
485
0
  int i;
486
487
0
  if (!children) {
488
0
    children = new GList();
489
0
  }
490
0
  children->append(childrenA);
491
0
  for (i = 0; i < childrenA->getLength(); ++i) {
492
0
    ((OCDisplayNode *)childrenA->get(i))->parent = this;
493
0
  }
494
0
  delete childrenA;
495
0
}
496
497
0
GList *OCDisplayNode::takeChildren() {
498
0
  GList *childrenA;
499
0
  int i;
500
501
0
  childrenA = children;
502
0
  children = NULL;
503
0
  for (i = 0; i < childrenA->getLength(); ++i) {
504
0
    ((OCDisplayNode *)childrenA->get(i))->parent = NULL;
505
0
  }
506
0
  return childrenA;
507
0
}
508
509
242k
OCDisplayNode::~OCDisplayNode() {
510
242k
  delete name;
511
242k
  if (children) {
512
154k
    deleteGList(children, OCDisplayNode);
513
154k
  }
514
242k
}
515
516
0
Unicode *OCDisplayNode::getName() {
517
0
  return name->getUnicode();
518
0
}
519
520
0
int OCDisplayNode::getNameLength() {
521
0
  return name->getLength();
522
0
}
523
524
0
int OCDisplayNode::getNumChildren() {
525
0
  if (!children) {
526
0
    return 0;
527
0
  }
528
0
  return children->getLength();
529
0
}
530
531
0
OCDisplayNode *OCDisplayNode::getChild(int idx) {
532
0
  return (OCDisplayNode *)children->get(idx);
533
0
}