Coverage Report

Created: 2025-08-28 06:46

/src/xpdf-4.05/xpdf/OptionalContent.cc
Line
Count
Source (jump to first uncovered line)
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
0
#define ocPolicyAllOn  1
23
0
#define ocPolicyAnyOn  2
24
0
#define ocPolicyAnyOff 3
25
0
#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
0
#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
2.29M
#define displayNodeRecursionLimit 50
36
37
//------------------------------------------------------------------------
38
39
1.43k
OptionalContent::OptionalContent(PDFDoc *doc) {
40
1.43k
  Object *ocProps;
41
1.43k
  Object ocgList, defView, uad, obj1, obj2, obj3, obj4;
42
1.43k
  Ref ref1;
43
1.43k
  OptionalContentGroup *ocg;
44
1.43k
  int i, j;
45
46
1.43k
  xref = doc->getXRef();
47
1.43k
  ocgs = new GList();
48
1.43k
  display = NULL;
49
50
1.43k
  if ((ocProps = doc->getCatalog()->getOCProperties())->isDict()) {
51
134
    if (ocProps->dictLookup("OCGs", &ocgList)->isArray()) {
52
53
      //----- read the OCG list
54
10.1k
      for (i = 0; i < ocgList.arrayGetLength(); ++i) {
55
9.99k
  if (ocgList.arrayGetNF(i, &obj1)->isRef()) {
56
2.51k
    ref1 = obj1.getRef();
57
2.51k
    obj1.fetch(xref, &obj2);
58
2.51k
    if ((ocg = OptionalContentGroup::parse(&ref1, &obj2))) {
59
828
      ocgs->append(ocg);
60
828
    }
61
2.51k
    obj2.free();
62
2.51k
  }
63
9.99k
  obj1.free();
64
9.99k
      }
65
66
      //----- read the default viewing OCCD
67
128
      if (ocProps->dictLookup("D", &defView)->isDict()) {
68
69
  //----- read the usage app dicts
70
102
  if (defView.dictLookup("AS", &obj1)->isArray()) {
71
238
    for (i = 0; i < obj1.arrayGetLength(); ++i) {
72
217
      if (obj1.arrayGet(i, &uad)->isDict()) {
73
53
        if (uad.dictLookup("Event", &obj2)->isName("View")) {
74
14
    if (uad.dictLookup("OCGs", &obj3)->isArray()) {
75
146
      for (j = 0; j < obj3.arrayGetLength(); ++j) {
76
133
        if (obj3.arrayGetNF(j, &obj4)->isRef()) {
77
32
          ref1 = obj4.getRef();
78
32
          if ((ocg = findOCG(&ref1))) {
79
3
      ocg->setInViewUsageAppDict();
80
3
          }
81
32
        }
82
133
        obj4.free();
83
133
      }
84
13
    }
85
14
    obj3.free();
86
14
        }
87
53
        obj2.free();
88
53
      }
89
217
      uad.free();
90
217
    }
91
21
  }
92
102
  obj1.free();
93
94
  //----- initial state from OCCD
95
102
  if (defView.dictLookup("OFF", &obj1)->isArray()) {
96
2.14k
    for (i = 0; i < obj1.arrayGetLength(); ++i) {
97
2.10k
      if (obj1.arrayGetNF(i, &obj2)->isRef()) {
98
1.24k
        ref1 = obj2.getRef();
99
1.24k
        if ((ocg = findOCG(&ref1))) {
100
277
    ocg->setState(gFalse);
101
967
        } else {
102
967
    error(errSyntaxError, -1,
103
967
          "Invalid OCG reference in OFF array in default viewing OCCD");
104
967
        }
105
1.24k
      }
106
2.10k
      obj2.free();
107
2.10k
    }
108
41
  }
109
102
  obj1.free();
110
111
  //----- initial state from OCG usage dict
112
410
  for (i = 0; i < ocgs->getLength(); ++i) {
113
308
    ocg = (OptionalContentGroup *)ocgs->get(i);
114
308
    if (ocg->getInViewUsageAppDict() &&
115
308
        ocg->getViewState() != ocUsageUnset) {
116
0
      ocg->setState(ocg->getViewState() == ocUsageOn);
117
0
    }
118
308
  }
119
120
  //----- display order
121
102
  if (defView.dictLookup("Order", &obj1)->isArray()) {
122
63
    display = OCDisplayNode::parse(&obj1, this, xref);
123
63
  }
124
102
  obj1.free();
125
126
102
      } else {
127
26
  error(errSyntaxError, -1, "Missing or invalid default viewing OCCD");
128
26
      }
129
128
      defView.free();
130
131
128
    }
132
134
    ocgList.free();
133
134
  }
134
135
1.43k
  if (!display) {
136
1.36k
    display = new OCDisplayNode();
137
1.36k
  }
138
1.43k
}
139
140
1.43k
OptionalContent::~OptionalContent() {
141
1.43k
  deleteGList(ocgs, OptionalContentGroup);
142
1.43k
  delete display;
143
1.43k
}
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
234k
OptionalContentGroup *OptionalContent::findOCG(Ref *ref) {
154
234k
  OptionalContentGroup *ocg;
155
234k
  int i;
156
157
654k
  for (i = 0; i < ocgs->getLength(); ++i) {
158
431k
    ocg = (OptionalContentGroup *)ocgs->get(i);
159
431k
    if (ocg->matches(ref)) {
160
11.6k
      return ocg;
161
11.6k
    }
162
431k
  }
163
223k
  return NULL;
164
234k
}
165
166
0
GBool OptionalContent::evalOCObject(Object *obj, GBool *visible) {
167
0
  OptionalContentGroup *ocg;
168
0
  int policy;
169
0
  Ref ref;
170
0
  Object obj2, obj3, obj4, obj5;
171
0
  GBool gotOCG;
172
0
  int i;
173
174
0
  if (obj->isNull()) {
175
0
    return gFalse;
176
0
  }
177
0
  if (obj->isRef()) {
178
0
    ref = obj->getRef();
179
0
    if ((ocg = findOCG(&ref))) {
180
0
      *visible = ocg->getState();
181
0
      return gTrue;
182
0
    }
183
0
  }
184
0
  obj->fetch(xref, &obj2);
185
0
  if (!obj2.isDict("OCMD")) {
186
0
    obj2.free();
187
0
    return gFalse;
188
0
  }
189
0
  if (obj2.dictLookup("VE", &obj3)->isArray()) {
190
0
    *visible = evalOCVisibilityExpr(&obj3, 0);
191
0
    obj3.free();
192
0
  } else {
193
0
    obj3.free();
194
0
    policy = ocPolicyAnyOn;
195
0
    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
0
    obj3.free();
207
0
    obj2.dictLookupNF("OCGs", &obj3);
208
0
    ocg = NULL;
209
0
    if (obj3.isRef()) {
210
0
      ref = obj3.getRef();
211
0
      ocg = findOCG(&ref);
212
0
    }
213
0
    if (ocg) {
214
0
      *visible = (policy == ocPolicyAllOn || policy == ocPolicyAnyOn) ?
215
0
             ocg->getState() : !ocg->getState();
216
0
    } else {
217
0
      *visible = policy == ocPolicyAllOn || policy == ocPolicyAllOff;
218
0
      if (!obj3.fetch(xref, &obj4)->isArray()) {
219
0
  obj4.free();
220
0
  obj3.free();
221
0
  obj2.free();
222
0
  return gFalse;
223
0
      }
224
0
      gotOCG = gFalse;
225
0
      for (i = 0; i < obj4.arrayGetLength(); ++i) {
226
0
  obj4.arrayGetNF(i, &obj5);
227
0
  if (obj5.isRef()) {
228
0
    ref = obj5.getRef();
229
0
    if ((ocg = findOCG(&ref))) {
230
0
      gotOCG = gTrue;
231
0
      switch (policy) {
232
0
      case ocPolicyAllOn:
233
0
        *visible = *visible && ocg->getState();
234
0
        break;
235
0
      case ocPolicyAnyOn:
236
0
        *visible = *visible || ocg->getState();
237
0
        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
0
      }
245
0
    }
246
0
  }
247
0
  obj5.free();
248
0
      }
249
0
      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
0
  obj4.free();
254
0
  obj3.free();
255
0
  obj2.free();
256
0
  return gFalse;
257
0
      }
258
0
      obj4.free();
259
0
    }
260
0
    obj3.free();
261
0
  }
262
0
  obj2.free();
263
0
  return gTrue;
264
0
}
265
266
0
GBool OptionalContent::evalOCVisibilityExpr(Object *expr, int recursion) {
267
0
  OptionalContentGroup *ocg;
268
0
  Object expr2, op, obj;
269
0
  Ref ref;
270
0
  GBool ret;
271
0
  int i;
272
273
0
  if (recursion > visibilityExprRecursionLimit) {
274
0
    error(errSyntaxError, -1,
275
0
    "Loop detected in optional content visibility expression");
276
0
    return gTrue;
277
0
  }
278
0
  if (expr->isRef()) {
279
0
    ref = expr->getRef();
280
0
    if ((ocg = findOCG(&ref))) {
281
0
      return ocg->getState();
282
0
    }
283
0
  }
284
0
  expr->fetch(xref, &expr2);
285
0
  if (!expr2.isArray() || expr2.arrayGetLength() < 1) {
286
0
    error(errSyntaxError, -1,
287
0
    "Invalid optional content visibility expression");
288
0
    expr2.free();
289
0
    return gTrue;
290
0
  }
291
0
  expr2.arrayGet(0, &op);
292
0
  if (op.isName("Not")) {
293
0
    if (expr2.arrayGetLength() == 2) {
294
0
      expr2.arrayGetNF(1, &obj);
295
0
      ret = !evalOCVisibilityExpr(&obj, recursion + 1);
296
0
      obj.free();
297
0
    } else {
298
0
      error(errSyntaxError, -1,
299
0
      "Invalid optional content visibility expression");
300
0
      ret = gTrue;
301
0
    }
302
0
  } else if (op.isName("And")) {
303
0
    ret = gTrue;
304
0
    for (i = 1; i < expr2.arrayGetLength() && ret; ++i) {
305
0
      expr2.arrayGetNF(i, &obj);
306
0
      ret = evalOCVisibilityExpr(&obj, recursion + 1);
307
0
      obj.free();
308
0
    }
309
0
  } else if (op.isName("Or")) {
310
0
    ret = gFalse;
311
0
    for (i = 1; i < expr2.arrayGetLength() && !ret; ++i) {
312
0
      expr2.arrayGetNF(i, &obj);
313
0
      ret = evalOCVisibilityExpr(&obj, recursion + 1);
314
0
      obj.free();
315
0
    }
316
0
  } else {
317
0
    error(errSyntaxError, -1,
318
0
    "Invalid optional content visibility expression");
319
0
    ret = gTrue;
320
0
  }
321
0
  op.free();
322
0
  expr2.free();
323
0
  return ret;
324
0
}
325
326
//------------------------------------------------------------------------
327
328
2.51k
OptionalContentGroup *OptionalContentGroup::parse(Ref *refA, Object *obj) {
329
2.51k
  TextString *nameA;
330
2.51k
  Object obj1, obj2, obj3;
331
2.51k
  OCUsageState viewStateA, printStateA;
332
333
2.51k
  if (!obj->isDict()) {
334
1.36k
    return NULL;
335
1.36k
  }
336
1.14k
  if (!obj->dictLookup("Name", &obj1)->isString()) {
337
317
    error(errSyntaxError, -1, "Missing or invalid Name in OCG");
338
317
    obj1.free();
339
317
    return NULL;
340
317
  }
341
828
  nameA = new TextString(obj1.getString());
342
828
  obj1.free();
343
344
828
  viewStateA = printStateA = ocUsageUnset;
345
828
  if (obj->dictLookup("Usage", &obj1)->isDict()) {
346
6
    if (obj1.dictLookup("View", &obj2)->isDict()) {
347
0
      if (obj2.dictLookup("ViewState", &obj3)->isName()) {
348
0
  if (obj3.isName("ON")) {
349
0
    viewStateA = ocUsageOn;
350
0
  } else {
351
0
    viewStateA = ocUsageOff;
352
0
  }
353
0
      }
354
0
      obj3.free();
355
0
    }
356
6
    obj2.free();
357
6
    if (obj1.dictLookup("Print", &obj2)->isDict()) {
358
0
      if (obj2.dictLookup("PrintState", &obj3)->isName()) {
359
0
  if (obj3.isName("ON")) {
360
0
    printStateA = ocUsageOn;
361
0
  } else {
362
0
    printStateA = ocUsageOff;
363
0
  }
364
0
      }
365
0
      obj3.free();
366
0
    }
367
6
    obj2.free();
368
6
  }
369
828
  obj1.free();
370
371
828
  return new OptionalContentGroup(refA, nameA, viewStateA, printStateA);
372
1.14k
}
373
374
OptionalContentGroup::OptionalContentGroup(Ref *refA, TextString *nameA,
375
             OCUsageState viewStateA,
376
828
             OCUsageState printStateA) {
377
828
  ref = *refA;
378
828
  name = nameA;
379
828
  viewState = viewStateA;
380
828
  printState = printStateA;
381
828
  state = gTrue;
382
828
  inViewUsageAppDict = gFalse;
383
828
}
384
385
828
OptionalContentGroup::~OptionalContentGroup() {
386
828
  delete name;
387
828
}
388
389
431k
GBool OptionalContentGroup::matches(Ref *refA) {
390
431k
  return refA->num == ref.num && refA->gen == ref.gen;
391
431k
}
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
2.29M
            XRef *xref, int recursion) {
405
2.29M
  Object obj2, obj3;
406
2.29M
  Ref ref;
407
2.29M
  OptionalContentGroup *ocgA;
408
2.29M
  OCDisplayNode *node, *child;
409
2.29M
  int i;
410
411
2.29M
  if (recursion > displayNodeRecursionLimit) {
412
251k
    error(errSyntaxError, -1, "Loop detected in optional content order");
413
251k
    return NULL;
414
251k
  }
415
2.04M
  if (obj->isRef()) {
416
233k
    ref = obj->getRef();
417
233k
    if ((ocgA = oc->findOCG(&ref))) {
418
11.4k
      return new OCDisplayNode(ocgA);
419
11.4k
    }
420
233k
  }
421
2.03M
  obj->fetch(xref, &obj2);
422
2.03M
  if (!obj2.isArray()) {
423
1.99M
    obj2.free();
424
1.99M
    return NULL;
425
1.99M
  }
426
41.0k
  i = 0;
427
41.0k
  if (obj2.arrayGetLength() >= 1) {
428
41.0k
    if (obj2.arrayGet(0, &obj3)->isString()) {
429
2.92k
      node = new OCDisplayNode(obj3.getString());
430
2.92k
      i = 1;
431
38.0k
    } else {
432
38.0k
      node = new OCDisplayNode();
433
38.0k
    }
434
41.0k
    obj3.free();
435
41.0k
  } else {
436
14
    node = new OCDisplayNode();
437
14
  }
438
2.33M
  for (; i < obj2.arrayGetLength(); ++i) {
439
2.29M
    obj2.arrayGetNF(i, &obj3);
440
2.29M
    if ((child = OCDisplayNode::parse(&obj3, oc, xref, recursion + 1))) {
441
52.3k
      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
52.3k
      } else {
448
52.3k
  node->addChild(child);
449
52.3k
      }
450
52.3k
    }
451
2.29M
    obj3.free();
452
2.29M
  }
453
41.0k
  obj2.free();
454
41.0k
  return node;
455
2.03M
}
456
457
39.4k
OCDisplayNode::OCDisplayNode() {
458
39.4k
  name = new TextString();
459
39.4k
  ocg = NULL;
460
39.4k
  parent = NULL;
461
39.4k
  children = NULL;
462
39.4k
}
463
464
2.92k
OCDisplayNode::OCDisplayNode(GString *nameA) {
465
2.92k
  name = new TextString(nameA);
466
2.92k
  ocg = NULL;
467
2.92k
  children = NULL;
468
2.92k
}
469
470
11.4k
OCDisplayNode::OCDisplayNode(OptionalContentGroup *ocgA) {
471
11.4k
  name = new TextString(ocgA->name);
472
11.4k
  ocg = ocgA;
473
11.4k
  children = NULL;
474
11.4k
}
475
476
52.3k
void OCDisplayNode::addChild(OCDisplayNode *child) {
477
52.3k
  if (!children) {
478
32.7k
    children = new GList();
479
32.7k
  }
480
52.3k
  children->append(child);
481
52.3k
  child->parent = this;
482
52.3k
}
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
53.8k
OCDisplayNode::~OCDisplayNode() {
510
53.8k
  delete name;
511
53.8k
  if (children) {
512
32.7k
    deleteGList(children, OCDisplayNode);
513
32.7k
  }
514
53.8k
}
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
}