Coverage Report

Created: 2025-03-14 06:54

/src/qpdf/libqpdf/QPDFAcroFormDocumentHelper.cc
Line
Count
Source (jump to first uncovered line)
1
#include <qpdf/QPDFAcroFormDocumentHelper.hh>
2
3
#include <qpdf/Pl_Buffer.hh>
4
#include <qpdf/QPDFObjectHandle_private.hh>
5
#include <qpdf/QPDFPageDocumentHelper.hh>
6
#include <qpdf/QTC.hh>
7
#include <qpdf/QUtil.hh>
8
#include <qpdf/ResourceFinder.hh>
9
10
using namespace qpdf;
11
12
QPDFAcroFormDocumentHelper::Members::Members() :
13
30.0k
    cache_valid(false)
14
30.0k
{
15
30.0k
}
16
17
QPDFAcroFormDocumentHelper::QPDFAcroFormDocumentHelper(QPDF& qpdf) :
18
30.0k
    QPDFDocumentHelper(qpdf),
19
30.0k
    m(new Members())
20
30.0k
{
21
    // We have to analyze up front. Otherwise, when we are adding annotations and fields, we are in
22
    // a temporarily unstable configuration where some widget annotations are not reachable.
23
30.0k
    analyze();
24
30.0k
}
25
26
void
27
QPDFAcroFormDocumentHelper::invalidateCache()
28
0
{
29
0
    m->cache_valid = false;
30
0
    m->field_to_annotations.clear();
31
0
    m->annotation_to_field.clear();
32
0
}
33
34
bool
35
QPDFAcroFormDocumentHelper::hasAcroForm()
36
0
{
37
0
    return qpdf.getRoot().hasKey("/AcroForm");
38
0
}
39
40
QPDFObjectHandle
41
QPDFAcroFormDocumentHelper::getOrCreateAcroForm()
42
0
{
43
0
    auto acroform = qpdf.getRoot().getKey("/AcroForm");
44
0
    if (!acroform.isDictionary()) {
45
0
        acroform = qpdf.getRoot().replaceKeyAndGetNew(
46
0
            "/AcroForm", qpdf.makeIndirectObject(QPDFObjectHandle::newDictionary()));
47
0
    }
48
0
    return acroform;
49
0
}
50
51
void
52
QPDFAcroFormDocumentHelper::addFormField(QPDFFormFieldObjectHelper ff)
53
0
{
54
0
    auto acroform = getOrCreateAcroForm();
55
0
    auto fields = acroform.getKey("/Fields");
56
0
    if (!fields.isArray()) {
57
0
        fields = acroform.replaceKeyAndGetNew("/Fields", QPDFObjectHandle::newArray());
58
0
    }
59
0
    fields.appendItem(ff.getObjectHandle());
60
0
    QPDFObjGen::set visited;
61
0
    traverseField(ff.getObjectHandle(), QPDFObjectHandle::newNull(), 0, visited);
62
0
}
63
64
void
65
QPDFAcroFormDocumentHelper::addAndRenameFormFields(std::vector<QPDFObjectHandle> fields)
66
0
{
67
0
    analyze();
68
0
    std::map<std::string, std::string> renames;
69
0
    QPDFObjGen::set seen;
70
0
    for (std::list<QPDFObjectHandle> queue{fields.begin(), fields.end()}; !queue.empty();
71
0
         queue.pop_front()) {
72
0
        auto& obj = queue.front();
73
0
        if (seen.add(obj)) {
74
0
            auto kids = obj.getKey("/Kids");
75
0
            if (kids.isArray()) {
76
0
                for (auto const& kid: kids.aitems()) {
77
0
                    queue.push_back(kid);
78
0
                }
79
0
            }
80
81
0
            if (obj.hasKey("/T")) {
82
                // Find something we can append to the partial name that makes the fully qualified
83
                // name unique. When we find something, reuse the same suffix for all fields in this
84
                // group with the same name. We can only change the name of fields that have /T, and
85
                // this field's /T is always at the end of the fully qualified name, appending to /T
86
                // has the effect of appending the same thing to the fully qualified name.
87
0
                std::string old_name = QPDFFormFieldObjectHelper(obj).getFullyQualifiedName();
88
0
                if (renames.count(old_name) == 0) {
89
0
                    std::string new_name = old_name;
90
0
                    int suffix = 0;
91
0
                    std::string append;
92
0
                    while (!getFieldsWithQualifiedName(new_name).empty()) {
93
0
                        ++suffix;
94
0
                        append = "+" + std::to_string(suffix);
95
0
                        new_name = old_name + append;
96
0
                    }
97
0
                    renames[old_name] = append;
98
0
                }
99
0
                std::string append = renames[old_name];
100
0
                if (!append.empty()) {
101
0
                    obj.replaceKey(
102
0
                        "/T",
103
0
                        QPDFObjectHandle::newUnicodeString(
104
0
                            obj.getKey("/T").getUTF8Value() + append));
105
0
                }
106
0
            }
107
0
        }
108
0
    }
109
110
0
    for (auto const& i: fields) {
111
0
        addFormField(i);
112
0
    }
113
0
}
114
115
void
116
QPDFAcroFormDocumentHelper::removeFormFields(std::set<QPDFObjGen> const& to_remove)
117
0
{
118
0
    auto acroform = qpdf.getRoot().getKey("/AcroForm");
119
0
    if (!acroform.isDictionary()) {
120
0
        return;
121
0
    }
122
0
    auto fields = acroform.getKey("/Fields");
123
0
    if (!fields.isArray()) {
124
0
        return;
125
0
    }
126
127
0
    for (auto const& og: to_remove) {
128
0
        auto annotations = m->field_to_annotations.find(og);
129
0
        if (annotations != m->field_to_annotations.end()) {
130
0
            for (auto aoh: annotations->second) {
131
0
                m->annotation_to_field.erase(aoh.getObjectHandle().getObjGen());
132
0
            }
133
0
            m->field_to_annotations.erase(og);
134
0
        }
135
0
        auto name = m->field_to_name.find(og);
136
0
        if (name != m->field_to_name.end()) {
137
0
            m->name_to_fields[name->second].erase(og);
138
0
            if (m->name_to_fields[name->second].empty()) {
139
0
                m->name_to_fields.erase(name->second);
140
0
            }
141
0
            m->field_to_name.erase(og);
142
0
        }
143
0
    }
144
145
0
    int i = 0;
146
0
    while (i < fields.getArrayNItems()) {
147
0
        auto field = fields.getArrayItem(i);
148
0
        if (to_remove.count(field.getObjGen())) {
149
0
            fields.eraseItem(i);
150
0
        } else {
151
0
            ++i;
152
0
        }
153
0
    }
154
0
}
155
156
void
157
QPDFAcroFormDocumentHelper::setFormFieldName(QPDFFormFieldObjectHelper ff, std::string const& name)
158
0
{
159
0
    ff.setFieldAttribute("/T", name);
160
0
    QPDFObjGen::set visited;
161
0
    auto ff_oh = ff.getObjectHandle();
162
0
    traverseField(ff_oh, ff_oh.getKey("/Parent"), 0, visited);
163
0
}
164
165
std::vector<QPDFFormFieldObjectHelper>
166
QPDFAcroFormDocumentHelper::getFormFields()
167
0
{
168
0
    analyze();
169
0
    std::vector<QPDFFormFieldObjectHelper> result;
170
0
    for (auto const& iter: m->field_to_annotations) {
171
0
        result.emplace_back(qpdf.getObject(iter.first));
172
0
    }
173
0
    return result;
174
0
}
175
176
std::set<QPDFObjGen>
177
QPDFAcroFormDocumentHelper::getFieldsWithQualifiedName(std::string const& name)
178
0
{
179
0
    analyze();
180
    // Keep from creating an empty entry
181
0
    auto iter = m->name_to_fields.find(name);
182
0
    if (iter != m->name_to_fields.end()) {
183
0
        return iter->second;
184
0
    }
185
0
    return {};
186
0
}
187
188
std::vector<QPDFAnnotationObjectHelper>
189
QPDFAcroFormDocumentHelper::getAnnotationsForField(QPDFFormFieldObjectHelper h)
190
0
{
191
0
    analyze();
192
0
    std::vector<QPDFAnnotationObjectHelper> result;
193
0
    QPDFObjGen og(h.getObjectHandle().getObjGen());
194
0
    if (m->field_to_annotations.count(og)) {
195
0
        result = m->field_to_annotations[og];
196
0
    }
197
0
    return result;
198
0
}
199
200
std::vector<QPDFAnnotationObjectHelper>
201
QPDFAcroFormDocumentHelper::getWidgetAnnotationsForPage(QPDFPageObjectHelper h)
202
90.3k
{
203
90.3k
    return h.getAnnotations("/Widget");
204
90.3k
}
205
206
std::vector<QPDFFormFieldObjectHelper>
207
QPDFAcroFormDocumentHelper::getFormFieldsForPage(QPDFPageObjectHelper ph)
208
0
{
209
0
    analyze();
210
0
    QPDFObjGen::set todo;
211
0
    std::vector<QPDFFormFieldObjectHelper> result;
212
0
    for (auto& annot: getWidgetAnnotationsForPage(ph)) {
213
0
        auto field = getFieldForAnnotation(annot).getTopLevelField();
214
0
        if (todo.add(field) && field.getObjectHandle().isDictionary()) {
215
0
            result.push_back(field);
216
0
        }
217
0
    }
218
0
    return result;
219
0
}
220
221
QPDFFormFieldObjectHelper
222
QPDFAcroFormDocumentHelper::getFieldForAnnotation(QPDFAnnotationObjectHelper h)
223
98.4k
{
224
98.4k
    QPDFObjectHandle oh = h.getObjectHandle();
225
98.4k
    QPDFFormFieldObjectHelper result(QPDFObjectHandle::newNull());
226
98.4k
    if (!oh.isDictionaryOfType("", "/Widget")) {
227
0
        return result;
228
0
    }
229
98.4k
    analyze();
230
98.4k
    QPDFObjGen og(oh.getObjGen());
231
98.4k
    if (m->annotation_to_field.count(og)) {
232
93.7k
        result = m->annotation_to_field[og];
233
93.7k
    }
234
98.4k
    return result;
235
98.4k
}
236
237
void
238
QPDFAcroFormDocumentHelper::analyze()
239
128k
{
240
128k
    if (m->cache_valid) {
241
98.4k
        return;
242
98.4k
    }
243
30.0k
    m->cache_valid = true;
244
30.0k
    QPDFObjectHandle acroform = qpdf.getRoot().getKey("/AcroForm");
245
30.0k
    if (!(acroform.isDictionary() && acroform.hasKey("/Fields"))) {
246
21.0k
        return;
247
21.0k
    }
248
9.04k
    QPDFObjectHandle fields = acroform.getKey("/Fields");
249
9.04k
    if (auto fa = fields.as_array(strict)) {
250
        // Traverse /AcroForm to find annotations and map them bidirectionally to fields.
251
252
7.87k
        QPDFObjGen::set visited;
253
7.87k
        QPDFObjectHandle null(QPDFObjectHandle::newNull());
254
64.4k
        for (auto const& field: fa) {
255
64.4k
            traverseField(field, null, 0, visited);
256
64.4k
        }
257
7.87k
    } else {
258
1.16k
        QTC::TC("qpdf", "QPDFAcroFormDocumentHelper fields not array");
259
1.16k
        acroform.warnIfPossible("/Fields key of /AcroForm dictionary is not an array; ignoring");
260
1.16k
        fields = QPDFObjectHandle::newArray();
261
1.16k
    }
262
263
    // All Widget annotations should have been encountered by traversing /AcroForm, but in case any
264
    // weren't, find them by walking through pages, and treat any widget annotation that is not
265
    // associated with a field as its own field. This just ensures that requesting the field for any
266
    // annotation we find through a page's /Annots list will have some associated field. Note that
267
    // a file that contains this kind of error will probably not
268
    // actually work with most viewers.
269
270
24.1k
    for (auto const& ph: QPDFPageDocumentHelper(qpdf).getAllPages()) {
271
113k
        for (auto const& iter: getWidgetAnnotationsForPage(ph)) {
272
113k
            QPDFObjectHandle annot(iter.getObjectHandle());
273
113k
            QPDFObjGen og(annot.getObjGen());
274
113k
            if (m->annotation_to_field.count(og) == 0) {
275
8.06k
                QTC::TC("qpdf", "QPDFAcroFormDocumentHelper orphaned widget");
276
                // This is not supposed to happen, but it's easy enough for us to handle this case.
277
                // Treat the annotation as its own field. This could allow qpdf to sensibly handle a
278
                // case such as a PDF creator adding a self-contained annotation (merged with the
279
                // field dictionary) to the page's /Annots array and forgetting to also put it in
280
                // /AcroForm.
281
8.06k
                annot.warnIfPossible(
282
8.06k
                    "this widget annotation is not"
283
8.06k
                    " reachable from /AcroForm in the document catalog");
284
8.06k
                m->annotation_to_field[og] = QPDFFormFieldObjectHelper(annot);
285
8.06k
                m->field_to_annotations[og].emplace_back(annot);
286
8.06k
            }
287
113k
        }
288
24.1k
    }
289
9.04k
}
290
291
void
292
QPDFAcroFormDocumentHelper::traverseField(
293
    QPDFObjectHandle field, QPDFObjectHandle parent, int depth, QPDFObjGen::set& visited)
294
140k
{
295
140k
    if (depth > 100) {
296
        // Arbitrarily cut off recursion at a fixed depth to avoid specially crafted files that
297
        // could cause stack overflow.
298
0
        return;
299
0
    }
300
140k
    if (!field.isIndirect()) {
301
74.1k
        QTC::TC("qpdf", "QPDFAcroFormDocumentHelper direct field");
302
74.1k
        field.warnIfPossible(
303
74.1k
            "encountered a direct object as a field or annotation while "
304
74.1k
            "traversing /AcroForm; ignoring field or annotation");
305
74.1k
        return;
306
74.1k
    }
307
66.6k
    if (!field.isDictionary()) {
308
30.5k
        QTC::TC("qpdf", "QPDFAcroFormDocumentHelper non-dictionary field");
309
30.5k
        field.warnIfPossible(
310
30.5k
            "encountered a non-dictionary as a field or annotation while"
311
30.5k
            " traversing /AcroForm; ignoring field or annotation");
312
30.5k
        return;
313
30.5k
    }
314
36.1k
    QPDFObjGen og(field.getObjGen());
315
36.1k
    if (!visited.add(og)) {
316
2.71k
        QTC::TC("qpdf", "QPDFAcroFormDocumentHelper loop");
317
2.71k
        field.warnIfPossible("loop detected while traversing /AcroForm");
318
2.71k
        return;
319
2.71k
    }
320
321
    // A dictionary encountered while traversing the /AcroForm field may be a form field, an
322
    // annotation, or the merger of the two. A field that has no fields below it is a terminal. If a
323
    // terminal field looks like an annotation, it is an annotation because annotation dictionary
324
    // fields can be merged with terminal field dictionaries. Otherwise, the annotation fields might
325
    // be there to be inherited by annotations below it.
326
327
33.3k
    bool is_annotation = false;
328
33.3k
    bool is_field = (0 == depth);
329
33.3k
    if (auto a = field.getKey("/Kids").as_array(strict)) {
330
4.65k
        is_field = true;
331
76.3k
        for (auto const& item: a) {
332
76.3k
            traverseField(item, field, 1 + depth, visited);
333
76.3k
        }
334
28.7k
    } else {
335
28.7k
        if (field.hasKey("/Parent")) {
336
22.5k
            is_field = true;
337
22.5k
        }
338
28.7k
        if (field.hasKey("/Subtype") || field.hasKey("/Rect") || field.hasKey("/AP")) {
339
21.9k
            is_annotation = true;
340
21.9k
        }
341
28.7k
    }
342
343
33.3k
    QTC::TC("qpdf", "QPDFAcroFormDocumentHelper field found", (depth == 0) ? 0 : 1);
344
33.3k
    QTC::TC("qpdf", "QPDFAcroFormDocumentHelper annotation found", (is_field ? 0 : 1));
345
346
33.3k
    if (is_annotation) {
347
21.9k
        QPDFObjectHandle our_field = (is_field ? field : parent);
348
21.9k
        m->field_to_annotations[our_field.getObjGen()].emplace_back(field);
349
21.9k
        m->annotation_to_field[og] = QPDFFormFieldObjectHelper(our_field);
350
21.9k
    }
351
352
33.3k
    if (is_field && (field.hasKey("/T"))) {
353
23.1k
        QPDFFormFieldObjectHelper foh(field);
354
23.1k
        auto f_og = field.getObjGen();
355
23.1k
        std::string name = foh.getFullyQualifiedName();
356
23.1k
        auto old = m->field_to_name.find(f_og);
357
23.1k
        if (old != m->field_to_name.end()) {
358
            // We might be updating after a name change, so remove any old information
359
0
            std::string old_name = old->second;
360
0
            m->name_to_fields[old_name].erase(f_og);
361
0
        }
362
23.1k
        m->field_to_name[f_og] = name;
363
23.1k
        m->name_to_fields[name].insert(f_og);
364
23.1k
    }
365
33.3k
}
366
367
bool
368
QPDFAcroFormDocumentHelper::getNeedAppearances()
369
151k
{
370
151k
    bool result = false;
371
151k
    QPDFObjectHandle acroform = qpdf.getRoot().getKey("/AcroForm");
372
151k
    if (acroform.isDictionary() && acroform.getKey("/NeedAppearances").isBool()) {
373
4.07k
        result = acroform.getKey("/NeedAppearances").getBoolValue();
374
4.07k
    }
375
151k
    return result;
376
151k
}
377
378
void
379
QPDFAcroFormDocumentHelper::setNeedAppearances(bool val)
380
3.77k
{
381
3.77k
    QPDFObjectHandle acroform = qpdf.getRoot().getKey("/AcroForm");
382
3.77k
    if (!acroform.isDictionary()) {
383
0
        qpdf.getRoot().warnIfPossible(
384
0
            "ignoring call to QPDFAcroFormDocumentHelper::setNeedAppearances"
385
0
            " on a file that lacks an /AcroForm dictionary");
386
0
        return;
387
0
    }
388
3.77k
    if (val) {
389
0
        acroform.replaceKey("/NeedAppearances", QPDFObjectHandle::newBool(true));
390
3.77k
    } else {
391
3.77k
        acroform.removeKey("/NeedAppearances");
392
3.77k
    }
393
3.77k
}
394
395
void
396
QPDFAcroFormDocumentHelper::generateAppearancesIfNeeded()
397
15.1k
{
398
15.1k
    if (!getNeedAppearances()) {
399
11.1k
        return;
400
11.1k
    }
401
402
9.17k
    for (auto const& page: QPDFPageDocumentHelper(qpdf).getAllPages()) {
403
55.0k
        for (auto& aoh: getWidgetAnnotationsForPage(page)) {
404
55.0k
            QPDFFormFieldObjectHelper ffh = getFieldForAnnotation(aoh);
405
55.0k
            if (ffh.getFieldType() == "/Btn") {
406
                // Rather than generating appearances for button fields, rely on what's already
407
                // there. Just make sure /AS is consistent with /V, which we can do by resetting the
408
                // value of the field back to itself. This code is referenced in a comment in
409
                // QPDFFormFieldObjectHelper::generateAppearance.
410
13.5k
                if (ffh.isRadioButton() || ffh.isCheckbox()) {
411
13.2k
                    ffh.setV(ffh.getValue());
412
13.2k
                }
413
41.5k
            } else {
414
41.5k
                ffh.generateAppearance(aoh);
415
41.5k
            }
416
55.0k
        }
417
9.17k
    }
418
3.99k
    setNeedAppearances(false);
419
3.99k
}
420
421
void
422
QPDFAcroFormDocumentHelper::disableDigitalSignatures()
423
0
{
424
0
    qpdf.removeSecurityRestrictions();
425
0
    std::set<QPDFObjGen> to_remove;
426
0
    auto fields = getFormFields();
427
0
    for (auto& f: fields) {
428
0
        auto ft = f.getFieldType();
429
0
        if (ft == "/Sig") {
430
0
            auto oh = f.getObjectHandle();
431
0
            to_remove.insert(oh.getObjGen());
432
            // Make this no longer a form field. If it's also an annotation, the annotation will
433
            // survive. If it's only a field and is no longer referenced, it will disappear.
434
0
            oh.removeKey("/FT");
435
            // Remove fields that are specific to signature fields.
436
0
            oh.removeKey("/V");
437
0
            oh.removeKey("/SV");
438
0
            oh.removeKey("/Lock");
439
0
        }
440
0
    }
441
0
    removeFormFields(to_remove);
442
0
}
443
444
void
445
QPDFAcroFormDocumentHelper::adjustInheritedFields(
446
    QPDFObjectHandle obj,
447
    bool override_da,
448
    std::string const& from_default_da,
449
    bool override_q,
450
    int from_default_q)
451
0
{
452
    // Override /Q or /DA if needed. If this object has a field type, directly or inherited, it is a
453
    // field and not just an annotation. In that case, we need to override if we are getting a value
454
    // from the document that is different from the value we would have gotten from the old
455
    // document. We must take care not to override an explicit value. It's possible that /FT may be
456
    // inherited by lower fields that may explicitly set /DA or /Q or that this is a field whose
457
    // type does not require /DA or /Q and we may be put a value on the field that is unused. This
458
    // is harmless, so it's not worth trying to work around.
459
460
0
    auto has_explicit = [](QPDFFormFieldObjectHelper& field, std::string const& key) {
461
0
        if (field.getObjectHandle().hasKey(key)) {
462
0
            return true;
463
0
        }
464
0
        auto oh = field.getInheritableFieldValue(key);
465
0
        if (!oh.isNull()) {
466
0
            return true;
467
0
        }
468
0
        return false;
469
0
    };
470
471
0
    if (override_da || override_q) {
472
0
        QPDFFormFieldObjectHelper cur_field(obj);
473
0
        if (override_da && (!has_explicit(cur_field, "/DA"))) {
474
0
            std::string da = cur_field.getDefaultAppearance();
475
0
            if (da != from_default_da) {
476
0
                QTC::TC("qpdf", "QPDFAcroFormDocumentHelper override da");
477
0
                obj.replaceKey("/DA", QPDFObjectHandle::newUnicodeString(from_default_da));
478
0
            }
479
0
        }
480
0
        if (override_q && (!has_explicit(cur_field, "/Q"))) {
481
0
            int q = cur_field.getQuadding();
482
0
            if (q != from_default_q) {
483
0
                QTC::TC("qpdf", "QPDFAcroFormDocumentHelper override q");
484
0
                obj.replaceKey("/Q", QPDFObjectHandle::newInteger(from_default_q));
485
0
            }
486
0
        }
487
0
    }
488
0
}
489
490
namespace
491
{
492
    class ResourceReplacer: public QPDFObjectHandle::TokenFilter
493
    {
494
      public:
495
        ResourceReplacer(
496
            std::map<std::string, std::map<std::string, std::string>> const& dr_map,
497
            std::map<std::string, std::map<std::string, std::set<size_t>>> const& rnames);
498
0
        ~ResourceReplacer() override = default;
499
        void handleToken(QPDFTokenizer::Token const&) override;
500
501
      private:
502
        size_t offset{0};
503
        std::map<std::string, std::map<size_t, std::string>> to_replace;
504
    };
505
} // namespace
506
507
ResourceReplacer::ResourceReplacer(
508
    std::map<std::string, std::map<std::string, std::string>> const& dr_map,
509
    std::map<std::string, std::map<std::string, std::set<size_t>>> const& rnames)
510
0
{
511
    // We have:
512
    // * dr_map[resource_type][key] == new_key
513
    // * rnames[resource_type][key] == set of offsets
514
    //
515
    // We want:
516
    // * to_replace[key][offset] = new_key
517
518
0
    for (auto const& rn_iter: rnames) {
519
0
        std::string const& rtype = rn_iter.first;
520
0
        auto dr_map_rtype = dr_map.find(rtype);
521
0
        if (dr_map_rtype == dr_map.end()) {
522
0
            continue;
523
0
        }
524
0
        auto const& key_offsets = rn_iter.second;
525
0
        for (auto const& ko_iter: key_offsets) {
526
0
            std::string const& old_key = ko_iter.first;
527
0
            auto dr_map_rtype_old = dr_map_rtype->second.find(old_key);
528
0
            if (dr_map_rtype_old == dr_map_rtype->second.end()) {
529
0
                continue;
530
0
            }
531
0
            auto const& offsets = ko_iter.second;
532
0
            for (auto const& o_iter: offsets) {
533
0
                to_replace[old_key][o_iter] = dr_map_rtype_old->second;
534
0
            }
535
0
        }
536
0
    }
537
0
}
538
539
void
540
ResourceReplacer::handleToken(QPDFTokenizer::Token const& token)
541
0
{
542
0
    bool wrote = false;
543
0
    if (token.getType() == QPDFTokenizer::tt_name) {
544
0
        std::string name = QPDFObjectHandle::newName(token.getValue()).getName();
545
0
        if (to_replace.count(name) && to_replace[name].count(offset)) {
546
0
            QTC::TC("qpdf", "QPDFAcroFormDocumentHelper replaced DA token");
547
0
            write(to_replace[name][offset]);
548
0
            wrote = true;
549
0
        }
550
0
    }
551
0
    offset += token.getRawValue().length();
552
0
    if (!wrote) {
553
0
        writeToken(token);
554
0
    }
555
0
}
556
557
void
558
QPDFAcroFormDocumentHelper::adjustDefaultAppearances(
559
    QPDFObjectHandle obj, std::map<std::string, std::map<std::string, std::string>> const& dr_map)
560
0
{
561
    // This method is called on a field that has been copied from another file but whose /DA still
562
    // refers to resources in the original file's /DR.
563
564
    // When appearance streams are generated for variable text fields (see ISO 32000 PDF spec
565
    // section 12.7.3.3), the field's /DA is used to generate content of the appearance stream. /DA
566
    // contains references to resources that may be resolved in the document's /DR dictionary, which
567
    // appears in the document's /AcroForm dictionary. For fields that we copied from other
568
    // documents, we need to ensure that resources are mapped correctly in the case of conflicting
569
    // names. For example, if a.pdf's /DR has /F1 pointing to one font and b.pdf's /DR also has /F1
570
    // but it points elsewhere, we need to make sure appearance streams of fields copied from b.pdf
571
    // into a.pdf use whatever font /F1 meant in b.pdf, not whatever it means in a.pdf. This method
572
    // takes care of that. It is only called on fields copied from foreign files.
573
574
    // A few notes:
575
    //
576
    // * If the from document's /DR and the current document's /DR have conflicting keys, we have
577
    //   already resolved the conflicts before calling this method. The dr_map parameter contains
578
    //   the mapping from old keys to new keys.
579
    //
580
    // * /DA may be inherited from the document's /AcroForm dictionary. By the time this method has
581
    //   been called, we have already copied any document-level values into the fields to avoid
582
    //   having them inherit from the new document. This was done in adjustInheritedFields.
583
584
0
    auto DA = obj.getKey("/DA");
585
0
    if (!DA.isString()) {
586
0
        return;
587
0
    }
588
589
    // Find names in /DA. /DA is a string that contains content stream-like code, so we create a
590
    // stream out of the string and then filter it. We don't attach the stream to anything, so it
591
    // will get discarded.
592
0
    ResourceFinder rf;
593
0
    auto da_stream = QPDFObjectHandle::newStream(&qpdf, DA.getUTF8Value());
594
0
    try {
595
0
        auto nwarnings = qpdf.numWarnings();
596
0
        da_stream.parseAsContents(&rf);
597
0
        if (qpdf.numWarnings() > nwarnings) {
598
0
            QTC::TC("qpdf", "QPDFAcroFormDocumentHelper /DA parse error");
599
0
        }
600
0
    } catch (std::exception& e) {
601
        // No way to reproduce in test suite right now since error conditions are converted to
602
        // warnings.
603
0
        obj.warnIfPossible(
604
0
            std::string("Unable to parse /DA: ") + e.what() +
605
0
            "; this form field may not update properly");
606
0
        return;
607
0
    }
608
609
    // Regenerate /DA by filtering its tokens.
610
0
    ResourceReplacer rr(dr_map, rf.getNamesByResourceType());
611
0
    Pl_Buffer buf_pl("filtered DA");
612
0
    da_stream.filterAsContents(&rr, &buf_pl);
613
0
    std::string new_da = buf_pl.getString();
614
0
    obj.replaceKey("/DA", QPDFObjectHandle::newString(new_da));
615
0
}
616
617
void
618
QPDFAcroFormDocumentHelper::adjustAppearanceStream(
619
    QPDFObjectHandle stream, std::map<std::string, std::map<std::string, std::string>> dr_map)
620
0
{
621
    // We don't have to modify appearance streams or their resource dictionaries for them to display
622
    // properly, but we need to do so to make them save to regenerate. Suppose an appearance stream
623
    // as a font /F1 that is different from /F1 in /DR, and that when we copy the field, /F1 is
624
    // remapped to /F1_1. When the field is regenerated, /F1_1 won't appear in the stream's resource
625
    // dictionary, so the regenerated appearance stream will revert to the /F1_1 in /DR. If we
626
    // adjust existing appearance streams, we are protected from this problem.
627
628
0
    auto dict = stream.getDict();
629
0
    auto resources = dict.getKey("/Resources");
630
631
    // Make sure this stream has its own private resource dictionary.
632
0
    bool was_indirect = resources.isIndirect();
633
0
    resources = resources.shallowCopy();
634
0
    if (was_indirect) {
635
0
        resources = qpdf.makeIndirectObject(resources);
636
0
    }
637
0
    dict.replaceKey("/Resources", resources);
638
    // Create a dictionary with top-level keys so we can use mergeResources to force them to be
639
    // unshared. We will also use this to resolve conflicts that may already be in the resource
640
    // dictionary.
641
0
    auto merge_with = QPDFObjectHandle::newDictionary();
642
0
    for (auto const& top_key: dr_map) {
643
0
        merge_with.replaceKey(top_key.first, QPDFObjectHandle::newDictionary());
644
0
    }
645
0
    resources.mergeResources(merge_with);
646
    // Rename any keys in the resource dictionary that we remapped.
647
0
    for (auto const& i1: dr_map) {
648
0
        std::string const& top_key = i1.first;
649
0
        auto subdict = resources.getKey(top_key);
650
0
        if (!subdict.isDictionary()) {
651
0
            continue;
652
0
        }
653
0
        for (auto const& i2: i1.second) {
654
0
            std::string const& old_key = i2.first;
655
0
            std::string const& new_key = i2.second;
656
0
            auto existing_new = subdict.getKey(new_key);
657
0
            if (!existing_new.isNull()) {
658
                // The resource dictionary already has a key in it matching what we remapped an old
659
                // key to, so we'll have to move it out of the way. Stick it in merge_with, which we
660
                // will re-merge with the dictionary when we're done. We know merge_with already has
661
                // dictionaries for all the top keys.
662
0
                QTC::TC("qpdf", "QPDFAcroFormDocumentHelper ap conflict");
663
0
                merge_with.getKey(top_key).replaceKey(new_key, existing_new);
664
0
            }
665
0
            auto existing_old = subdict.getKey(old_key);
666
0
            if (!existing_old.isNull()) {
667
0
                QTC::TC("qpdf", "QPDFAcroFormDocumentHelper ap rename");
668
0
                subdict.replaceKey(new_key, existing_old);
669
0
                subdict.removeKey(old_key);
670
0
            }
671
0
        }
672
0
    }
673
    // Deal with any any conflicts by re-merging with merge_with and updating our local copy of
674
    // dr_map, which we will use to modify the stream contents.
675
0
    resources.mergeResources(merge_with, &dr_map);
676
    // Remove empty subdictionaries
677
0
    for (auto iter: resources.ditems()) {
678
0
        if (iter.second.isDictionary() && (iter.second.getKeys().size() == 0)) {
679
0
            resources.removeKey(iter.first);
680
0
        }
681
0
    }
682
683
    // Now attach a token filter to replace the actual resources.
684
0
    ResourceFinder rf;
685
0
    try {
686
0
        auto nwarnings = qpdf.numWarnings();
687
0
        stream.parseAsContents(&rf);
688
0
        if (qpdf.numWarnings() > nwarnings) {
689
0
            QTC::TC("qpdf", "QPDFAcroFormDocumentHelper AP parse error");
690
0
        }
691
0
        auto rr = new ResourceReplacer(dr_map, rf.getNamesByResourceType());
692
0
        auto tf = std::shared_ptr<QPDFObjectHandle::TokenFilter>(rr);
693
0
        stream.addTokenFilter(tf);
694
0
    } catch (std::exception& e) {
695
        // No way to reproduce in test suite right now since error conditions are converted to
696
        // warnings.
697
0
        stream.warnIfPossible(std::string("Unable to parse appearance stream: ") + e.what());
698
0
    }
699
0
}
700
701
void
702
QPDFAcroFormDocumentHelper::transformAnnotations(
703
    QPDFObjectHandle old_annots,
704
    std::vector<QPDFObjectHandle>& new_annots,
705
    std::vector<QPDFObjectHandle>& new_fields,
706
    std::set<QPDFObjGen>& old_fields,
707
    QPDFMatrix const& cm,
708
    QPDF* from_qpdf,
709
    QPDFAcroFormDocumentHelper* from_afdh)
710
0
{
711
0
    std::shared_ptr<QPDFAcroFormDocumentHelper> afdhph;
712
0
    if (!from_qpdf) {
713
        // Assume these are from the same QPDF.
714
0
        from_qpdf = &qpdf;
715
0
        from_afdh = this;
716
0
    } else if ((from_qpdf != &qpdf) && (!from_afdh)) {
717
0
        afdhph = std::make_shared<QPDFAcroFormDocumentHelper>(*from_qpdf);
718
0
        from_afdh = afdhph.get();
719
0
    }
720
0
    bool foreign = (from_qpdf != &qpdf);
721
722
    // It's possible that we will transform annotations that don't include any form fields. This
723
    // code takes care not to muck around with /AcroForm unless we have to.
724
725
0
    QPDFObjectHandle acroform = qpdf.getRoot().getKey("/AcroForm");
726
0
    QPDFObjectHandle from_acroform = from_qpdf->getRoot().getKey("/AcroForm");
727
728
    // /DA and /Q may be inherited from the document-level /AcroForm dictionary. If we are copying a
729
    // foreign stream and the stream is getting one of these values from its document's /AcroForm,
730
    // we will need to copy the value explicitly so that it doesn't start getting its default from
731
    // the destination document.
732
0
    bool override_da = false;
733
0
    bool override_q = false;
734
0
    std::string from_default_da;
735
0
    int from_default_q = 0;
736
    // If we copy any form fields, we will need to merge the source document's /DR into this
737
    // document's /DR.
738
0
    QPDFObjectHandle from_dr = QPDFObjectHandle::newNull();
739
0
    if (foreign) {
740
0
        std::string default_da;
741
0
        int default_q = 0;
742
0
        if (acroform.isDictionary()) {
743
0
            if (acroform.getKey("/DA").isString()) {
744
0
                default_da = acroform.getKey("/DA").getUTF8Value();
745
0
            }
746
0
            if (acroform.getKey("/Q").isInteger()) {
747
0
                default_q = acroform.getKey("/Q").getIntValueAsInt();
748
0
            }
749
0
        }
750
0
        if (from_acroform.isDictionary()) {
751
0
            if (from_acroform.getKey("/DR").isDictionary()) {
752
0
                from_dr = from_acroform.getKey("/DR");
753
0
                if (!from_dr.isIndirect()) {
754
0
                    from_dr = from_qpdf->makeIndirectObject(from_dr);
755
0
                }
756
0
                from_dr = qpdf.copyForeignObject(from_dr);
757
0
            }
758
0
            if (from_acroform.getKey("/DA").isString()) {
759
0
                from_default_da = from_acroform.getKey("/DA").getUTF8Value();
760
0
            }
761
0
            if (from_acroform.getKey("/Q").isInteger()) {
762
0
                from_default_q = from_acroform.getKey("/Q").getIntValueAsInt();
763
0
            }
764
0
        }
765
0
        if (from_default_da != default_da) {
766
0
            override_da = true;
767
0
        }
768
0
        if (from_default_q != default_q) {
769
0
            override_q = true;
770
0
        }
771
0
    }
772
773
    // If we have to merge /DR, we will need a mapping of conflicting keys for rewriting /DA. Set
774
    // this up for lazy initialization in case we encounter any form fields.
775
0
    std::map<std::string, std::map<std::string, std::string>> dr_map;
776
0
    bool initialized_dr_map = false;
777
0
    QPDFObjectHandle dr = QPDFObjectHandle::newNull();
778
0
    auto init_dr_map = [&]() {
779
0
        if (!initialized_dr_map) {
780
0
            initialized_dr_map = true;
781
            // Ensure that we have a /DR that is an indirect
782
            // dictionary object.
783
0
            if (!acroform.isDictionary()) {
784
0
                acroform = getOrCreateAcroForm();
785
0
            }
786
0
            dr = acroform.getKey("/DR");
787
0
            if (!dr.isDictionary()) {
788
0
                dr = QPDFObjectHandle::newDictionary();
789
0
            }
790
0
            dr.makeResourcesIndirect(qpdf);
791
0
            if (!dr.isIndirect()) {
792
0
                dr = acroform.replaceKeyAndGetNew("/DR", qpdf.makeIndirectObject(dr));
793
0
            }
794
            // Merge the other document's /DR, creating a conflict map. mergeResources checks to
795
            // make sure both objects are dictionaries. By this point, if this is foreign, from_dr
796
            // has been copied, so we use the target qpdf as the owning qpdf.
797
0
            from_dr.makeResourcesIndirect(qpdf);
798
0
            dr.mergeResources(from_dr, &dr_map);
799
800
0
            if (from_afdh->getNeedAppearances()) {
801
0
                setNeedAppearances(true);
802
0
            }
803
0
        }
804
0
    };
805
806
    // This helper prevents us from copying the same object multiple times.
807
0
    std::map<QPDFObjGen, QPDFObjectHandle> orig_to_copy;
808
0
    auto maybe_copy_object = [&](QPDFObjectHandle& to_copy) {
809
0
        auto og = to_copy.getObjGen();
810
0
        if (orig_to_copy.count(og)) {
811
0
            to_copy = orig_to_copy[og];
812
0
            return false;
813
0
        } else {
814
0
            to_copy = qpdf.makeIndirectObject(to_copy.shallowCopy());
815
0
            orig_to_copy[og] = to_copy;
816
0
            return true;
817
0
        }
818
0
    };
819
820
    // Now do the actual copies.
821
822
0
    QPDFObjGen::set added_new_fields;
823
0
    for (auto annot: old_annots.aitems()) {
824
0
        if (annot.isStream()) {
825
0
            annot.warnIfPossible("ignoring annotation that's a stream");
826
0
            continue;
827
0
        }
828
829
        // Make copies of annotations and fields down to the appearance streams, preserving all
830
        // internal referential integrity. When the incoming annotations are from a different file,
831
        // we first copy them locally. Then, whether local or foreign, we copy them again so that if
832
        // we bring the same annotation in multiple times (e.g. overlaying a foreign page onto
833
        // multiple local pages or a local page onto multiple other local pages), we don't create
834
        // annotations that are referenced in more than one place. If we did that, the effect of
835
        // applying transformations would be cumulative, which is definitely not what we want.
836
        // Besides, annotations and fields are not intended to be referenced in multiple places.
837
838
        // Determine if this annotation is attached to a form field. If so, the annotation may be
839
        // the same object as the form field, or the form field may have the annotation as a kid. In
840
        // either case, we have to walk up the field structure to find the top-level field. Within
841
        // one iteration through a set of annotations, we don't want to copy the same item more than
842
        // once. For example, suppose we have field A with kids B, C, and D, each of which has
843
        // annotations BA, CA, and DA. When we get to BA, we will find that BA is a kid of B which
844
        // is under A. When we do a copyForeignObject of A, it will also copy everything else
845
        // because of the indirect references. When we clone BA, we will want to clone A and then
846
        // update A's clone's kid to point B's clone and B's clone's parent to point to A's clone.
847
        // The same thing holds for annotations. Next, when we get to CA, we will again discover
848
        // that A is the top, but we don't want to re-copy A. We want CA's clone to be linked to the
849
        // same clone as BA's. Failure to do this will break up things like radio button groups,
850
        // which all have to kids of the same parent.
851
852
0
        auto ffield = from_afdh->getFieldForAnnotation(annot);
853
0
        auto ffield_oh = ffield.getObjectHandle();
854
0
        QPDFObjectHandle top_field;
855
0
        bool have_field = false;
856
0
        bool have_parent = false;
857
0
        if (ffield_oh.isStream()) {
858
0
            ffield_oh.warnIfPossible("ignoring form field that's a stream");
859
0
        } else if ((!ffield_oh.isNull()) && (!ffield_oh.isIndirect())) {
860
0
            ffield_oh.warnIfPossible("ignoring form field not indirect");
861
0
        } else if (!ffield_oh.isNull()) {
862
            // A field and its associated annotation can be the same object. This matters because we
863
            // don't want to clone the annotation and field separately in this case.
864
0
            have_field = true;
865
            // Find the top-level field. It may be the field itself.
866
0
            top_field = ffield.getTopLevelField(&have_parent).getObjectHandle();
867
0
            if (foreign) {
868
                // copyForeignObject returns the same value if called multiple times with the same
869
                // field. Create/retrieve the local copy of the original field. This pulls over
870
                // everything the field references including annotations and appearance streams, but
871
                // it's harmless to call copyForeignObject on them too. They will already be copied,
872
                // so we'll get the right object back.
873
874
                // top_field and ffield_oh are known to be indirect.
875
0
                top_field = qpdf.copyForeignObject(top_field);
876
0
                ffield_oh = qpdf.copyForeignObject(ffield_oh);
877
0
            } else {
878
                // We don't need to add top_field to old_fields if it's foreign because the new copy
879
                // of the foreign field won't be referenced anywhere. It's just the starting point
880
                // for us to make an additional local copy of.
881
0
                old_fields.insert(top_field.getObjGen());
882
0
            }
883
884
            // Traverse the field, copying kids, and preserving integrity.
885
0
            std::list<QPDFObjectHandle> queue;
886
0
            QPDFObjGen::set seen;
887
0
            if (maybe_copy_object(top_field)) {
888
0
                queue.push_back(top_field);
889
0
            }
890
0
            for (; !queue.empty(); queue.pop_front()) {
891
0
                auto& obj = queue.front();
892
0
                if (seen.add(obj)) {
893
0
                    auto parent = obj.getKey("/Parent");
894
0
                    if (parent.isIndirect()) {
895
0
                        auto parent_og = parent.getObjGen();
896
0
                        if (orig_to_copy.count(parent_og)) {
897
0
                            obj.replaceKey("/Parent", orig_to_copy[parent_og]);
898
0
                        } else {
899
0
                            parent.warnIfPossible(
900
0
                                "while traversing field " + obj.getObjGen().unparse(',') +
901
0
                                ", found parent (" + parent_og.unparse(',') +
902
0
                                ") that had not been seen, indicating likely invalid field "
903
0
                                "structure");
904
0
                        }
905
0
                    }
906
0
                    auto kids = obj.getKey("/Kids");
907
0
                    if (kids.isArray()) {
908
0
                        for (int i = 0; i < kids.getArrayNItems(); ++i) {
909
0
                            auto kid = kids.getArrayItem(i);
910
0
                            if (maybe_copy_object(kid)) {
911
0
                                kids.setArrayItem(i, kid);
912
0
                                queue.push_back(kid);
913
0
                            }
914
0
                        }
915
0
                    }
916
917
0
                    if (override_da || override_q) {
918
0
                        adjustInheritedFields(
919
0
                            obj, override_da, from_default_da, override_q, from_default_q);
920
0
                    }
921
0
                    if (foreign) {
922
                        // Lazily initialize our /DR and the conflict map.
923
0
                        init_dr_map();
924
                        // The spec doesn't say anything about /DR on the field, but lots of writers
925
                        // put one there, and it is frequently the same as the document-level /DR.
926
                        // To avoid having the field's /DR point to information that we are not
927
                        // maintaining, just reset it to that if it exists. Empirical evidence
928
                        // suggests that many readers, including Acrobat, Adobe Acrobat Reader,
929
                        // chrome, firefox, the mac Preview application, and several of the free
930
                        // readers on Linux all ignore /DR at the field level.
931
0
                        if (obj.hasKey("/DR")) {
932
0
                            obj.replaceKey("/DR", dr);
933
0
                        }
934
0
                    }
935
0
                    if (foreign && obj.getKey("/DA").isString() && (!dr_map.empty())) {
936
0
                        adjustDefaultAppearances(obj, dr_map);
937
0
                    }
938
0
                }
939
0
            }
940
941
            // Now switch to copies. We already switched for top_field
942
0
            maybe_copy_object(ffield_oh);
943
0
            ffield = QPDFFormFieldObjectHelper(ffield_oh);
944
0
        }
945
946
0
        QTC::TC(
947
0
            "qpdf",
948
0
            "QPDFAcroFormDocumentHelper copy annotation",
949
0
            (have_field ? 1 : 0) | (foreign ? 2 : 0));
950
0
        if (have_field) {
951
0
            QTC::TC(
952
0
                "qpdf",
953
0
                "QPDFAcroFormDocumentHelper field with parent",
954
0
                (have_parent ? 1 : 0) | (foreign ? 2 : 0));
955
0
        }
956
0
        if (foreign) {
957
0
            if (!annot.isIndirect()) {
958
0
                annot = from_qpdf->makeIndirectObject(annot);
959
0
            }
960
0
            annot = qpdf.copyForeignObject(annot);
961
0
        }
962
0
        maybe_copy_object(annot);
963
964
        // Now we have copies, so we can safely mutate.
965
0
        if (have_field && added_new_fields.add(top_field)) {
966
0
            new_fields.push_back(top_field);
967
0
        }
968
0
        new_annots.push_back(annot);
969
970
        // Identify and copy any appearance streams
971
972
0
        auto ah = QPDFAnnotationObjectHelper(annot);
973
0
        auto apdict = ah.getAppearanceDictionary();
974
0
        std::vector<QPDFObjectHandle> streams;
975
0
        auto replace_stream = [](auto& dict, auto& key, auto& old) {
976
0
            return dict.replaceKeyAndGetNew(key, old.copyStream());
977
0
        };
978
979
0
        for (auto& [key1, value1]: apdict.as_dictionary()) {
980
0
            if (value1.isStream()) {
981
0
                streams.emplace_back(replace_stream(apdict, key1, value1));
982
0
            } else {
983
0
                for (auto& [key2, value2]: value1.as_dictionary()) {
984
0
                    if (value2.isStream()) {
985
0
                        streams.emplace_back(replace_stream(value1, key2, value2));
986
0
                    }
987
0
                }
988
0
            }
989
0
        }
990
991
        // Now we can safely mutate the annotation and its appearance streams.
992
0
        for (auto& stream: streams) {
993
0
            auto dict = stream.getDict();
994
0
            auto omatrix = dict.getKey("/Matrix");
995
0
            QPDFMatrix apcm;
996
0
            if (omatrix.isArray()) {
997
0
                QTC::TC("qpdf", "QPDFAcroFormDocumentHelper modify ap matrix");
998
0
                auto m1 = omatrix.getArrayAsMatrix();
999
0
                apcm = QPDFMatrix(m1);
1000
0
            }
1001
0
            apcm.concat(cm);
1002
0
            auto new_matrix = QPDFObjectHandle::newFromMatrix(apcm);
1003
0
            if (omatrix.isArray() || (apcm != QPDFMatrix())) {
1004
0
                dict.replaceKey("/Matrix", new_matrix);
1005
0
            }
1006
0
            auto resources = dict.getKey("/Resources");
1007
0
            if ((!dr_map.empty()) && resources.isDictionary()) {
1008
0
                adjustAppearanceStream(stream, dr_map);
1009
0
            }
1010
0
        }
1011
0
        auto rect = cm.transformRectangle(annot.getKey("/Rect").getArrayAsRectangle());
1012
0
        annot.replaceKey("/Rect", QPDFObjectHandle::newFromRectangle(rect));
1013
0
    }
1014
0
}
1015
1016
void
1017
QPDFAcroFormDocumentHelper::fixCopiedAnnotations(
1018
    QPDFObjectHandle to_page,
1019
    QPDFObjectHandle from_page,
1020
    QPDFAcroFormDocumentHelper& from_afdh,
1021
    std::set<QPDFObjGen>* added_fields)
1022
0
{
1023
0
    auto old_annots = from_page.getKey("/Annots");
1024
0
    if ((!old_annots.isArray()) || (old_annots.getArrayNItems() == 0)) {
1025
0
        return;
1026
0
    }
1027
1028
0
    std::vector<QPDFObjectHandle> new_annots;
1029
0
    std::vector<QPDFObjectHandle> new_fields;
1030
0
    std::set<QPDFObjGen> old_fields;
1031
0
    transformAnnotations(
1032
0
        old_annots,
1033
0
        new_annots,
1034
0
        new_fields,
1035
0
        old_fields,
1036
0
        QPDFMatrix(),
1037
0
        &(from_afdh.getQPDF()),
1038
0
        &from_afdh);
1039
1040
0
    to_page.replaceKey("/Annots", QPDFObjectHandle::newArray(new_annots));
1041
0
    addAndRenameFormFields(new_fields);
1042
0
    if (added_fields) {
1043
0
        for (auto const& f: new_fields) {
1044
0
            added_fields->insert(f.getObjGen());
1045
0
        }
1046
0
    }
1047
0
}