forked from qpdf/qpdf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQPDFObjectHandle.cc
2806 lines (2571 loc) · 72.5 KB
/
QPDFObjectHandle.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <qpdf/QPDFObjectHandle.hh>
#include <qpdf/QPDF.hh>
#include <qpdf/QPDF_Bool.hh>
#include <qpdf/QPDF_Null.hh>
#include <qpdf/QPDF_Integer.hh>
#include <qpdf/QPDF_Real.hh>
#include <qpdf/QPDF_Name.hh>
#include <qpdf/QPDF_String.hh>
#include <qpdf/QPDF_Operator.hh>
#include <qpdf/QPDF_InlineImage.hh>
#include <qpdf/QPDF_Array.hh>
#include <qpdf/QPDF_Dictionary.hh>
#include <qpdf/QPDF_Stream.hh>
#include <qpdf/QPDF_Reserved.hh>
#include <qpdf/Pl_Buffer.hh>
#include <qpdf/Pl_Concatenate.hh>
#include <qpdf/Pl_QPDFTokenizer.hh>
#include <qpdf/BufferInputSource.hh>
#include <qpdf/QPDFExc.hh>
#include <qpdf/QPDFPageObjectHelper.hh>
#include <qpdf/SparseOHArray.hh>
#include <qpdf/QTC.hh>
#include <qpdf/QUtil.hh>
#include <qpdf/QIntC.hh>
#include <stdexcept>
#include <stdlib.h>
#include <ctype.h>
#include <limits.h>
#include <cstring>
#include <algorithm>
class TerminateParsing
{
};
class CoalesceProvider: public QPDFObjectHandle::StreamDataProvider
{
public:
CoalesceProvider(QPDFObjectHandle containing_page,
QPDFObjectHandle old_contents) :
containing_page(containing_page),
old_contents(old_contents)
{
}
virtual ~CoalesceProvider()
{
}
virtual void provideStreamData(int objid, int generation,
Pipeline* pipeline);
private:
QPDFObjectHandle containing_page;
QPDFObjectHandle old_contents;
};
void
CoalesceProvider::provideStreamData(int, int, Pipeline* p)
{
QTC::TC("qpdf", "QPDFObjectHandle coalesce provide stream data");
Pl_Concatenate concat("concatenate", p);
std::string description = "page object " +
QUtil::int_to_string(containing_page.getObjectID()) + " " +
QUtil::int_to_string(containing_page.getGeneration());
std::string all_description;
old_contents.pipeContentStreams(&concat, description, all_description);
concat.manualFinish();
}
void
QPDFObjectHandle::TokenFilter::handleEOF()
{
}
void
QPDFObjectHandle::TokenFilter::setPipeline(Pipeline* p)
{
this->pipeline = p;
}
void
QPDFObjectHandle::TokenFilter::write(char const* data, size_t len)
{
if (! this->pipeline)
{
return;
}
if (len)
{
this->pipeline->write(QUtil::unsigned_char_pointer(data), len);
}
}
void
QPDFObjectHandle::TokenFilter::write(std::string const& str)
{
write(str.c_str(), str.length());
}
void
QPDFObjectHandle::TokenFilter::writeToken(QPDFTokenizer::Token const& token)
{
std::string value = token.getRawValue();
write(value.c_str(), value.length());
}
void
QPDFObjectHandle::ParserCallbacks::handleObject(QPDFObjectHandle)
{
throw std::logic_error("You must override one of the"
" handleObject methods in ParserCallbacks");
}
void
QPDFObjectHandle::ParserCallbacks::handleObject(
QPDFObjectHandle oh, size_t, size_t)
{
// This version of handleObject was added in qpdf 9. If the
// developer did not override it, fall back to the older
// interface.
handleObject(oh);
}
void
QPDFObjectHandle::ParserCallbacks::contentSize(size_t)
{
// Ignore by default; overriding this is optional.
}
void
QPDFObjectHandle::ParserCallbacks::terminateParsing()
{
throw TerminateParsing();
}
QPDFObjectHandle::Members::~Members()
{
}
QPDFObjectHandle::Members::Members() :
initialized(false),
qpdf(0),
objid(0),
generation(0),
reserved(false)
{
}
QPDFObjectHandle::Members::Members(QPDF* qpdf, int objid, int generation) :
initialized(true),
qpdf(qpdf),
objid(objid),
generation(generation),
reserved(false)
{
}
QPDFObjectHandle::Members::Members(QPDFObject* data) :
initialized(true),
qpdf(0),
objid(0),
generation(0),
obj(data),
reserved(false)
{
}
QPDFObjectHandle::QPDFObjectHandle() :
m(new Members)
{
}
QPDFObjectHandle::QPDFObjectHandle(QPDFObjectHandle const& rhs) :
m(new Members)
{
*m = *rhs.m;
}
QPDFObjectHandle&
QPDFObjectHandle::operator=(QPDFObjectHandle const& rhs)
{
if (this != &rhs)
{
*m = *rhs.m;
}
return *this;
}
QPDFObjectHandle::QPDFObjectHandle(QPDF* qpdf, int objid, int generation) :
m(new Members(qpdf, objid, generation))
{
}
QPDFObjectHandle::QPDFObjectHandle(QPDFObject* data) :
m(new Members(data))
{
}
void
QPDFObjectHandle::releaseResolved()
{
// Recursively break any resolved references to indirect objects.
// Do not cross over indirect object boundaries to avoid an
// infinite loop. This method may only be called during final
// destruction. See comments in QPDF::~QPDF().
if (isIndirect())
{
if (this->m->obj.getPointer())
{
this->m->obj = 0;
}
}
else
{
QPDFObject::ObjAccessor::releaseResolved(this->m->obj.getPointer());
}
}
void
QPDFObjectHandle::setObjectDescriptionFromInput(
QPDFObjectHandle object, QPDF* context,
std::string const& description, PointerHolder<InputSource> input,
qpdf_offset_t offset)
{
object.setObjectDescription(
context,
input->getName() + ", " + description +
" at offset " + QUtil::int_to_string(offset));
}
bool
QPDFObjectHandle::isInitialized() const
{
return this->m->initialized;
}
QPDFObject::object_type_e
QPDFObjectHandle::getTypeCode()
{
if (this->m->initialized)
{
dereference();
return this->m->obj->getTypeCode();
}
else
{
return QPDFObject::ot_uninitialized;
}
}
char const*
QPDFObjectHandle::getTypeName()
{
if (this->m->initialized)
{
dereference();
return this->m->obj->getTypeName();
}
else
{
return "uninitialized";
}
}
template <class T>
class QPDFObjectTypeAccessor
{
public:
static bool check(QPDFObject* o)
{
return (o && dynamic_cast<T*>(o));
}
static bool check(QPDFObject const* o)
{
return (o && dynamic_cast<T const*>(o));
}
};
bool
QPDFObjectHandle::isBool()
{
dereference();
return QPDFObjectTypeAccessor<QPDF_Bool>::check(m->obj.getPointer());
}
bool
QPDFObjectHandle::isDirectNull() const
{
return (this->m->initialized && (this->m->objid == 0) &&
QPDFObjectTypeAccessor<QPDF_Null>::check(m->obj.getPointer()));
}
bool
QPDFObjectHandle::isNull()
{
dereference();
return QPDFObjectTypeAccessor<QPDF_Null>::check(m->obj.getPointer());
}
bool
QPDFObjectHandle::isInteger()
{
dereference();
return QPDFObjectTypeAccessor<QPDF_Integer>::check(m->obj.getPointer());
}
bool
QPDFObjectHandle::isReal()
{
dereference();
return QPDFObjectTypeAccessor<QPDF_Real>::check(m->obj.getPointer());
}
bool
QPDFObjectHandle::isNumber()
{
return (isInteger() || isReal());
}
double
QPDFObjectHandle::getNumericValue()
{
double result = 0.0;
if (isInteger())
{
result = static_cast<double>(getIntValue());
}
else if (isReal())
{
result = atof(getRealValue().c_str());
}
else
{
typeWarning("number", "returning 0");
QTC::TC("qpdf", "QPDFObjectHandle numeric non-numeric");
}
return result;
}
bool
QPDFObjectHandle::isName()
{
dereference();
return QPDFObjectTypeAccessor<QPDF_Name>::check(m->obj.getPointer());
}
bool
QPDFObjectHandle::isString()
{
dereference();
return QPDFObjectTypeAccessor<QPDF_String>::check(m->obj.getPointer());
}
bool
QPDFObjectHandle::isOperator()
{
dereference();
return QPDFObjectTypeAccessor<QPDF_Operator>::check(m->obj.getPointer());
}
bool
QPDFObjectHandle::isInlineImage()
{
dereference();
return QPDFObjectTypeAccessor<QPDF_InlineImage>::check(m->obj.getPointer());
}
bool
QPDFObjectHandle::isArray()
{
dereference();
return QPDFObjectTypeAccessor<QPDF_Array>::check(m->obj.getPointer());
}
bool
QPDFObjectHandle::isDictionary()
{
dereference();
return QPDFObjectTypeAccessor<QPDF_Dictionary>::check(m->obj.getPointer());
}
bool
QPDFObjectHandle::isStream()
{
dereference();
return QPDFObjectTypeAccessor<QPDF_Stream>::check(m->obj.getPointer());
}
bool
QPDFObjectHandle::isReserved()
{
// dereference will clear reserved if this has been replaced
dereference();
return this->m->reserved;
}
bool
QPDFObjectHandle::isIndirect()
{
assertInitialized();
return (this->m->objid != 0);
}
bool
QPDFObjectHandle::isScalar()
{
return (! (isArray() || isDictionary() || isStream() ||
isOperator() || isInlineImage()));
}
// Bool accessors
bool
QPDFObjectHandle::getBoolValue()
{
if (isBool())
{
return dynamic_cast<QPDF_Bool*>(m->obj.getPointer())->getVal();
}
else
{
typeWarning("boolean", "returning false");
QTC::TC("qpdf", "QPDFObjectHandle boolean returning false");
return false;
}
}
// Integer accessors
long long
QPDFObjectHandle::getIntValue()
{
if (isInteger())
{
return dynamic_cast<QPDF_Integer*>(m->obj.getPointer())->getVal();
}
else
{
typeWarning("integer", "returning 0");
QTC::TC("qpdf", "QPDFObjectHandle integer returning 0");
return 0;
}
}
int
QPDFObjectHandle::getIntValueAsInt()
{
int result = 0;
long long v = getIntValue();
if (v < INT_MIN)
{
QTC::TC("qpdf", "QPDFObjectHandle int returning INT_MIN");
warnIfPossible(
"requested value of integer is too small; returning INT_MIN",
false);
result = INT_MIN;
}
else if (v > INT_MAX)
{
QTC::TC("qpdf", "QPDFObjectHandle int returning INT_MAX");
warnIfPossible(
"requested value of integer is too big; returning INT_MAX",
false);
result = INT_MAX;
}
else
{
result = static_cast<int>(v);
}
return result;
}
unsigned long long
QPDFObjectHandle::getUIntValue()
{
unsigned long long result = 0;
long long v = getIntValue();
if (v < 0)
{
QTC::TC("qpdf", "QPDFObjectHandle uint returning 0");
warnIfPossible(
"unsigned value request for negative number; returning 0",
false);
}
else
{
result = static_cast<unsigned long long>(v);
}
return result;
}
unsigned int
QPDFObjectHandle::getUIntValueAsUInt()
{
unsigned int result = 0;
long long v = getIntValue();
if (v < 0)
{
QTC::TC("qpdf", "QPDFObjectHandle uint uint returning 0");
warnIfPossible(
"unsigned integer value request for negative number; returning 0",
false);
result = 0;
}
else if (v > UINT_MAX)
{
QTC::TC("qpdf", "QPDFObjectHandle uint returning UINT_MAX");
warnIfPossible(
"requested value of unsigned integer is too big;"
" returning UINT_MAX",
false);
result = UINT_MAX;
}
else
{
result = static_cast<unsigned int>(v);
}
return result;
}
// Real accessors
std::string
QPDFObjectHandle::getRealValue()
{
if (isReal())
{
return dynamic_cast<QPDF_Real*>(m->obj.getPointer())->getVal();
}
else
{
typeWarning("real", "returning 0.0");
QTC::TC("qpdf", "QPDFObjectHandle real returning 0.0");
return "0.0";
}
}
// Name accessors
std::string
QPDFObjectHandle::getName()
{
if (isName())
{
return dynamic_cast<QPDF_Name*>(m->obj.getPointer())->getName();
}
else
{
typeWarning("name", "returning dummy name");
QTC::TC("qpdf", "QPDFObjectHandle name returning dummy name");
return "/QPDFFakeName";
}
}
// String accessors
std::string
QPDFObjectHandle::getStringValue()
{
if (isString())
{
return dynamic_cast<QPDF_String*>(m->obj.getPointer())->getVal();
}
else
{
typeWarning("string", "returning empty string");
QTC::TC("qpdf", "QPDFObjectHandle string returning empty string");
return "";
}
}
std::string
QPDFObjectHandle::getUTF8Value()
{
if (isString())
{
return dynamic_cast<QPDF_String*>(m->obj.getPointer())->getUTF8Val();
}
else
{
typeWarning("string", "returning empty string");
QTC::TC("qpdf", "QPDFObjectHandle string returning empty utf8");
return "";
}
}
// Operator and Inline Image accessors
std::string
QPDFObjectHandle::getOperatorValue()
{
if (isOperator())
{
return dynamic_cast<QPDF_Operator*>(m->obj.getPointer())->getVal();
}
else
{
typeWarning("operator", "returning fake value");
QTC::TC("qpdf", "QPDFObjectHandle operator returning fake value");
return "QPDFFAKE";
}
}
std::string
QPDFObjectHandle::getInlineImageValue()
{
if (isInlineImage())
{
return dynamic_cast<QPDF_InlineImage*>(m->obj.getPointer())->getVal();
}
else
{
typeWarning("inlineimage", "returning empty data");
QTC::TC("qpdf", "QPDFObjectHandle inlineimage returning empty data");
return "";
}
}
// Array accessors
int
QPDFObjectHandle::getArrayNItems()
{
if (isArray())
{
return dynamic_cast<QPDF_Array*>(m->obj.getPointer())->getNItems();
}
else
{
typeWarning("array", "treating as empty");
QTC::TC("qpdf", "QPDFObjectHandle array treating as empty");
return 0;
}
}
QPDFObjectHandle
QPDFObjectHandle::getArrayItem(int n)
{
QPDFObjectHandle result;
if (isArray() && (n < getArrayNItems()) && (n >= 0))
{
result = dynamic_cast<QPDF_Array*>(m->obj.getPointer())->getItem(n);
}
else
{
result = newNull();
if (isArray())
{
objectWarning("returning null for out of bounds array access");
QTC::TC("qpdf", "QPDFObjectHandle array bounds");
}
else
{
typeWarning("array", "returning null");
QTC::TC("qpdf", "QPDFObjectHandle array null for non-array");
}
QPDF* context = 0;
std::string description;
if (this->m->obj->getDescription(context, description))
{
result.setObjectDescription(
context,
description +
" -> null returned from invalid array access");
}
}
return result;
}
bool
QPDFObjectHandle::isRectangle()
{
if (! isArray())
{
return false;
}
if (getArrayNItems() != 4)
{
return false;
}
for (int i = 0; i < 4; ++i)
{
if (! getArrayItem(i).isNumber())
{
return false;
}
}
return true;
}
bool
QPDFObjectHandle::isMatrix()
{
if (! isArray())
{
return false;
}
if (getArrayNItems() != 6)
{
return false;
}
for (int i = 0; i < 6; ++i)
{
if (! getArrayItem(i).isNumber())
{
return false;
}
}
return true;
}
QPDFObjectHandle::Rectangle
QPDFObjectHandle::getArrayAsRectangle()
{
Rectangle result;
if (isRectangle())
{
// Rectangle coordinates are always supposed to be llx, lly,
// urx, ury, but files have been found in the wild where
// llx > urx or lly > ury.
double i0 = getArrayItem(0).getNumericValue();
double i1 = getArrayItem(1).getNumericValue();
double i2 = getArrayItem(2).getNumericValue();
double i3 = getArrayItem(3).getNumericValue();
result = Rectangle(std::min(i0, i2),
std::min(i1, i3),
std::max(i0, i2),
std::max(i1, i3));
}
return result;
}
QPDFObjectHandle::Matrix
QPDFObjectHandle::getArrayAsMatrix()
{
Matrix result;
if (isMatrix())
{
result = Matrix(getArrayItem(0).getNumericValue(),
getArrayItem(1).getNumericValue(),
getArrayItem(2).getNumericValue(),
getArrayItem(3).getNumericValue(),
getArrayItem(4).getNumericValue(),
getArrayItem(5).getNumericValue());
}
return result;
}
std::vector<QPDFObjectHandle>
QPDFObjectHandle::getArrayAsVector()
{
std::vector<QPDFObjectHandle> result;
if (isArray())
{
dynamic_cast<QPDF_Array*>(m->obj.getPointer())->getAsVector(result);
}
else
{
typeWarning("array", "treating as empty");
QTC::TC("qpdf", "QPDFObjectHandle array treating as empty vector");
}
return result;
}
// Array mutators
void
QPDFObjectHandle::setArrayItem(int n, QPDFObjectHandle const& item)
{
if (isArray())
{
dynamic_cast<QPDF_Array*>(m->obj.getPointer())->setItem(n, item);
}
else
{
typeWarning("array", "ignoring attempt to set item");
QTC::TC("qpdf", "QPDFObjectHandle array ignoring set item");
}
}
void
QPDFObjectHandle::setArrayFromVector(std::vector<QPDFObjectHandle> const& items)
{
if (isArray())
{
dynamic_cast<QPDF_Array*>(m->obj.getPointer())->setFromVector(items);
}
else
{
typeWarning("array", "ignoring attempt to replace items");
QTC::TC("qpdf", "QPDFObjectHandle array ignoring replace items");
}
}
void
QPDFObjectHandle::insertItem(int at, QPDFObjectHandle const& item)
{
if (isArray())
{
dynamic_cast<QPDF_Array*>(m->obj.getPointer())->insertItem(at, item);
}
else
{
typeWarning("array", "ignoring attempt to insert item");
QTC::TC("qpdf", "QPDFObjectHandle array ignoring insert item");
}
}
void
QPDFObjectHandle::appendItem(QPDFObjectHandle const& item)
{
if (isArray())
{
dynamic_cast<QPDF_Array*>(m->obj.getPointer())->appendItem(item);
}
else
{
typeWarning("array", "ignoring attempt to append item");
QTC::TC("qpdf", "QPDFObjectHandle array ignoring append item");
}
}
void
QPDFObjectHandle::eraseItem(int at)
{
if (isArray() && (at < getArrayNItems()) && (at >= 0))
{
dynamic_cast<QPDF_Array*>(m->obj.getPointer())->eraseItem(at);
}
else
{
if (isArray())
{
objectWarning("ignoring attempt to erase out of bounds array item");
QTC::TC("qpdf", "QPDFObjectHandle erase array bounds");
}
else
{
typeWarning("array", "ignoring attempt to erase item");
QTC::TC("qpdf", "QPDFObjectHandle array ignoring erase item");
}
}
}
// Dictionary accessors
bool
QPDFObjectHandle::hasKey(std::string const& key)
{
if (isDictionary())
{
return dynamic_cast<QPDF_Dictionary*>(m->obj.getPointer())->hasKey(key);
}
else
{
typeWarning("dictionary",
"returning false for a key containment request");
QTC::TC("qpdf", "QPDFObjectHandle dictionary false for hasKey");
return false;
}
}
QPDFObjectHandle
QPDFObjectHandle::getKey(std::string const& key)
{
QPDFObjectHandle result;
if (isDictionary())
{
result = dynamic_cast<QPDF_Dictionary*>(
m->obj.getPointer())->getKey(key);
}
else
{
typeWarning(
"dictionary", "returning null for attempted key retrieval");
QTC::TC("qpdf", "QPDFObjectHandle dictionary null for getKey");
result = newNull();
QPDF* qpdf = 0;
std::string description;
if (this->m->obj->getDescription(qpdf, description))
{
result.setObjectDescription(
qpdf,
description +
" -> null returned from getting key " +
key + " from non-Dictionary");
}
}
return result;
}
std::set<std::string>
QPDFObjectHandle::getKeys()
{
std::set<std::string> result;
if (isDictionary())
{
result = dynamic_cast<QPDF_Dictionary*>(m->obj.getPointer())->getKeys();
}
else
{
typeWarning("dictionary", "treating as empty");
QTC::TC("qpdf", "QPDFObjectHandle dictionary empty set for getKeys");
}
return result;
}
std::map<std::string, QPDFObjectHandle>
QPDFObjectHandle::getDictAsMap()
{
std::map<std::string, QPDFObjectHandle> result;
if (isDictionary())
{
result = dynamic_cast<QPDF_Dictionary*>(
m->obj.getPointer())->getAsMap();
}
else
{
typeWarning("dictionary", "treating as empty");
QTC::TC("qpdf", "QPDFObjectHandle dictionary empty map for asMap");
}
return result;
}
// Array and Name accessors
bool
QPDFObjectHandle::isOrHasName(std::string const& value)
{
if (isName() && (getName() == value))
{
return true;
}
else if (isArray())
{
int n = getArrayNItems();
for (int i = 0; i < n; ++i)
{
QPDFObjectHandle item = getArrayItem(0);
if (item.isName() && (item.getName() == value))
{
return true;
}
}
}
return false;
}
void
QPDFObjectHandle::mergeResources(QPDFObjectHandle other)
{
if (! (isDictionary() && other.isDictionary()))
{
QTC::TC("qpdf", "QPDFObjectHandle merge top type mismatch");
return;
}
std::set<std::string> other_keys = other.getKeys();
for (std::set<std::string>::iterator iter = other_keys.begin();
iter != other_keys.end(); ++iter)
{
std::string const& key = *iter;
QPDFObjectHandle other_val = other.getKey(key);
if (hasKey(key))
{
QPDFObjectHandle this_val = getKey(key);
if (this_val.isDictionary() && other_val.isDictionary())
{
if (this_val.isIndirect())
{
QTC::TC("qpdf", "QPDFObjectHandle replace with copy");
this_val = this_val.shallowCopy();
replaceKey(key, this_val);
}
std::set<std::string> other_val_keys = other_val.getKeys();
for (std::set<std::string>::iterator i2 =
other_val_keys.begin();
i2 != other_val_keys.end(); ++i2)
{
if (! this_val.hasKey(*i2))
{
QTC::TC("qpdf", "QPDFObjectHandle merge shallow copy");
this_val.replaceKey(
*i2, other_val.getKey(*i2).shallowCopy());
}
}
}
else if (this_val.isArray() && other_val.isArray())
{
std::set<std::string> scalars;
int n = this_val.getArrayNItems();
for (int i = 0; i < n; ++i)
{
QPDFObjectHandle this_item = this_val.getArrayItem(i);
if (this_item.isScalar())
{
scalars.insert(this_item.unparse());
}
}