Coverage Report

Created: 2024-09-08 06:22

/src/mupdf/source/pdf/pdf-form.c
Line
Count
Source (jump to first uncovered line)
1
// Copyright (C) 2004-2024 Artifex Software, Inc.
2
//
3
// This file is part of MuPDF.
4
//
5
// MuPDF is free software: you can redistribute it and/or modify it under the
6
// terms of the GNU Affero General Public License as published by the Free
7
// Software Foundation, either version 3 of the License, or (at your option)
8
// any later version.
9
//
10
// MuPDF is distributed in the hope that it will be useful, but WITHOUT ANY
11
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12
// FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
13
// details.
14
//
15
// You should have received a copy of the GNU Affero General Public License
16
// along with MuPDF. If not, see <https://www.gnu.org/licenses/agpl-3.0.en.html>
17
//
18
// Alternative licensing terms are available from the licensor.
19
// For commercial licensing, see <https://www.artifex.com/> or contact
20
// Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco,
21
// CA 94129, USA, for further information.
22
23
#include "mupdf/fitz.h"
24
#include "pdf-annot-imp.h"
25
26
#include <string.h>
27
28
/* Must be kept in sync with definitions in pdf_util.js */
29
enum
30
{
31
  Display_Visible,
32
  Display_Hidden,
33
  Display_NoPrint,
34
  Display_NoView
35
};
36
37
enum
38
{
39
  SigFlag_SignaturesExist = 1,
40
  SigFlag_AppendOnly = 2
41
};
42
43
const char *pdf_field_value(fz_context *ctx, pdf_obj *field)
44
2.12k
{
45
2.12k
  pdf_obj *v = pdf_dict_get_inheritable(ctx, field, PDF_NAME(V));
46
2.12k
  if (pdf_is_name(ctx, v))
47
0
    return pdf_to_name(ctx, v);
48
2.12k
  if (pdf_is_stream(ctx, v))
49
0
  {
50
    // FIXME: pdf_dict_put_inheritable...
51
0
    char *str = pdf_new_utf8_from_pdf_stream_obj(ctx, v);
52
0
    fz_try(ctx)
53
0
      pdf_dict_put_text_string(ctx, field, PDF_NAME(V), str);
54
0
    fz_always(ctx)
55
0
      fz_free(ctx, str);
56
0
    fz_catch(ctx)
57
0
      fz_rethrow(ctx);
58
0
    v = pdf_dict_get(ctx, field, PDF_NAME(V));
59
0
  }
60
2.12k
  return pdf_to_text_string(ctx, v);
61
2.12k
}
62
63
int pdf_field_flags(fz_context *ctx, pdf_obj *obj)
64
2.53k
{
65
2.53k
  return pdf_dict_get_inheritable_int(ctx, obj, PDF_NAME(Ff));
66
2.53k
}
67
68
int pdf_field_type(fz_context *ctx, pdf_obj *obj)
69
0
{
70
0
  pdf_obj *type = pdf_dict_get_inheritable(ctx, obj, PDF_NAME(FT));
71
0
  int flags = pdf_field_flags(ctx, obj);
72
0
  if (pdf_name_eq(ctx, type, PDF_NAME(Btn)))
73
0
  {
74
0
    if (flags & PDF_BTN_FIELD_IS_PUSHBUTTON)
75
0
      return PDF_WIDGET_TYPE_BUTTON;
76
0
    else if (flags & PDF_BTN_FIELD_IS_RADIO)
77
0
      return PDF_WIDGET_TYPE_RADIOBUTTON;
78
0
    else
79
0
      return PDF_WIDGET_TYPE_CHECKBOX;
80
0
  }
81
0
  else if (pdf_name_eq(ctx, type, PDF_NAME(Tx)))
82
0
    return PDF_WIDGET_TYPE_TEXT;
83
0
  else if (pdf_name_eq(ctx, type, PDF_NAME(Ch)))
84
0
  {
85
0
    if (flags & PDF_CH_FIELD_IS_COMBO)
86
0
      return PDF_WIDGET_TYPE_COMBOBOX;
87
0
    else
88
0
      return PDF_WIDGET_TYPE_LISTBOX;
89
0
  }
90
0
  else if (pdf_name_eq(ctx, type, PDF_NAME(Sig)))
91
0
    return PDF_WIDGET_TYPE_SIGNATURE;
92
0
  else
93
0
    return PDF_WIDGET_TYPE_BUTTON;
94
0
}
95
96
const char *pdf_field_type_string(fz_context *ctx, pdf_obj *obj)
97
0
{
98
0
  switch (pdf_field_type(ctx, obj))
99
0
  {
100
0
  default:
101
0
  case PDF_WIDGET_TYPE_BUTTON: return "button";
102
0
  case PDF_WIDGET_TYPE_CHECKBOX: return "checkbox";
103
0
  case PDF_WIDGET_TYPE_COMBOBOX: return "combobox";
104
0
  case PDF_WIDGET_TYPE_LISTBOX: return "listbox";
105
0
  case PDF_WIDGET_TYPE_RADIOBUTTON: return "radiobutton";
106
0
  case PDF_WIDGET_TYPE_SIGNATURE: return "signature";
107
0
  case PDF_WIDGET_TYPE_TEXT: return "text";
108
0
  }
109
0
}
110
111
/* Find the point in a field hierarchy where all descendants
112
 * share the same name */
113
static pdf_obj *find_head_of_field_group(fz_context *ctx, pdf_obj *obj)
114
0
{
115
0
  if (obj == NULL || pdf_dict_get(ctx, obj, PDF_NAME(T)))
116
0
    return obj;
117
0
  else
118
0
    return find_head_of_field_group(ctx, pdf_dict_get(ctx, obj, PDF_NAME(Parent)));
119
0
}
120
121
static void pdf_field_mark_dirty(fz_context *ctx, pdf_obj *field)
122
0
{
123
0
  pdf_document *doc = pdf_get_bound_document(ctx, field);
124
0
  pdf_obj *kids = pdf_dict_get(ctx, field, PDF_NAME(Kids));
125
0
  if (kids)
126
0
  {
127
0
    int i, n = pdf_array_len(ctx, kids);
128
0
    for (i = 0; i < n; i++)
129
0
      pdf_field_mark_dirty(ctx, pdf_array_get(ctx, kids, i));
130
0
  }
131
0
  pdf_dirty_obj(ctx, field);
132
0
  if (doc)
133
0
    doc->resynth_required = 1;
134
0
}
135
136
static void update_field_value(fz_context *ctx, pdf_document *doc, pdf_obj *obj, const char *text)
137
0
{
138
0
  const char *old_text;
139
0
  pdf_obj *grp;
140
141
0
  if (!text)
142
0
    text = "";
143
144
  /* All fields of the same name should be updated, so
145
   * set the value at the head of the group */
146
0
  grp = find_head_of_field_group(ctx, obj);
147
0
  if (grp)
148
0
    obj = grp;
149
150
  /* Only update if we change the actual value. */
151
0
  old_text = pdf_dict_get_text_string(ctx, obj, PDF_NAME(V));
152
0
  if (old_text && !strcmp(old_text, text))
153
0
    return;
154
155
0
  pdf_dict_put_text_string(ctx, obj, PDF_NAME(V), text);
156
157
0
  pdf_field_mark_dirty(ctx, obj);
158
0
}
159
160
static pdf_obj *
161
pdf_lookup_field_imp(fz_context *ctx, pdf_obj *arr, const char *str, pdf_cycle_list *cycle_up);
162
163
static pdf_obj *
164
lookup_field_sub(fz_context *ctx, pdf_obj *dict, const char *str, pdf_cycle_list *cycle_up)
165
0
{
166
0
  pdf_obj *kids;
167
0
  pdf_obj *name;
168
169
0
  name = pdf_dict_get(ctx, dict, PDF_NAME(T));
170
171
  /* If we have a name, check it matches. If it matches, consume that
172
   * portion of str. If not, exit. */
173
0
  if (name)
174
0
  {
175
0
    const char *match = pdf_to_text_string(ctx, name);
176
0
    const char *e = str;
177
0
    size_t len;
178
0
    while (*e && *e != '.')
179
0
      e++;
180
0
    len = e-str;
181
0
    if (strncmp(str, match, len) != 0 || (match[len] != 0 && match[len] != '.'))
182
      /* name doesn't match. */
183
0
      return NULL;
184
0
    str = e;
185
0
    if (*str == '.')
186
0
      str++;
187
0
  }
188
189
  /* If there is a kids array, but the search string is not empty, we have
190
  encountered an internal field which represents a set of terminal fields. */
191
192
  /* If there is a kids array and the search string is not empty,
193
  walk those looking for the appropriate one. */
194
0
  kids = pdf_dict_get(ctx, dict, PDF_NAME(Kids));
195
0
  if (kids && *str != 0)
196
0
    return pdf_lookup_field_imp(ctx, kids, str, cycle_up);
197
198
  /* The field may be a terminal or an internal field at this point.
199
  Accept it as the match if the match string is exhausted. */
200
0
  if (*str == 0)
201
0
    return dict;
202
203
0
  return NULL;
204
0
}
205
206
static pdf_obj *
207
pdf_lookup_field_imp(fz_context *ctx, pdf_obj *arr, const char *str, pdf_cycle_list *cycle_up)
208
0
{
209
0
  pdf_cycle_list cycle;
210
0
  int len = pdf_array_len(ctx, arr);
211
0
  int i;
212
213
0
  for (i = 0; i < len; i++)
214
0
  {
215
0
    pdf_obj *k = pdf_array_get(ctx, arr, i);
216
0
    pdf_obj *found;
217
0
    if (pdf_cycle(ctx, &cycle, cycle_up, k))
218
0
      fz_throw(ctx, FZ_ERROR_FORMAT, "cycle in fields");
219
0
    found = lookup_field_sub(ctx, k, str, &cycle);
220
0
    if (found)
221
0
      return found;
222
0
  }
223
224
0
  return NULL;
225
0
}
226
227
pdf_obj *
228
pdf_lookup_field(fz_context *ctx, pdf_obj *arr, const char *str)
229
0
{
230
0
  return pdf_lookup_field_imp(ctx, arr, str, NULL);
231
0
}
232
233
static void reset_form_field(fz_context *ctx, pdf_document *doc, pdf_obj *field)
234
0
{
235
  /* Set V to DV wherever DV is present, and delete V where DV is not.
236
   * FIXME: we assume for now that V has not been set unequal
237
   * to DV higher in the hierarchy than "field".
238
   *
239
   * At the bottom of the hierarchy we may find widget annotations
240
   * that aren't also fields, but DV and V will not be present in their
241
   * dictionaries, and attempts to remove V will be harmless. */
242
0
  pdf_obj *dv = pdf_dict_get(ctx, field, PDF_NAME(DV));
243
0
  pdf_obj *kids = pdf_dict_get(ctx, field, PDF_NAME(Kids));
244
245
0
  if (dv)
246
0
    pdf_dict_put(ctx, field, PDF_NAME(V), dv);
247
0
  else
248
0
    pdf_dict_del(ctx, field, PDF_NAME(V));
249
250
0
  if (kids == NULL)
251
0
  {
252
    /* The leaves of the tree are widget annotations
253
     * In some cases we need to update the appearance state;
254
     * in others we need to mark the field as dirty so that
255
     * the appearance stream will be regenerated. */
256
0
    switch (pdf_field_type(ctx, field))
257
0
    {
258
0
    case PDF_WIDGET_TYPE_CHECKBOX:
259
0
    case PDF_WIDGET_TYPE_RADIOBUTTON:
260
0
      {
261
0
        pdf_obj *leafv = pdf_dict_get_inheritable(ctx, field, PDF_NAME(V));
262
0
        pdf_obj *ap = pdf_dict_get(ctx, field, PDF_NAME(AP));
263
0
        pdf_obj *n = pdf_dict_get(ctx, ap, PDF_NAME(N));
264
265
        /* Value does not refer to any appearance state in the
266
        normal appearance stream dictionary, default to Off instead. */
267
0
        if (pdf_is_dict(ctx, n) && !pdf_dict_get(ctx, n, leafv))
268
0
          leafv = NULL;
269
270
0
        if (!pdf_is_name(ctx, leafv))
271
0
          leafv = PDF_NAME(Off);
272
0
        pdf_dict_put(ctx, field, PDF_NAME(AS), leafv);
273
0
      }
274
0
      pdf_field_mark_dirty(ctx, field);
275
0
      break;
276
277
0
    case PDF_WIDGET_TYPE_BUTTON:
278
0
    case PDF_WIDGET_TYPE_SIGNATURE:
279
      /* Pushbuttons and signatures have no value to reset. */
280
0
      break;
281
282
0
    default:
283
0
      pdf_field_mark_dirty(ctx, field);
284
0
      break;
285
0
    }
286
0
  }
287
0
}
288
289
void pdf_field_reset(fz_context *ctx, pdf_document *doc, pdf_obj *field)
290
0
{
291
0
  pdf_obj *kids = pdf_dict_get(ctx, field, PDF_NAME(Kids));
292
293
0
  reset_form_field(ctx, doc, field);
294
295
0
  if (kids)
296
0
  {
297
0
    int i, n = pdf_array_len(ctx, kids);
298
299
0
    for (i = 0; i < n; i++)
300
0
      pdf_field_reset(ctx, doc, pdf_array_get(ctx, kids, i));
301
0
  }
302
0
}
303
304
static void add_field_hierarchy_to_array(fz_context *ctx, pdf_obj *array, pdf_obj *field, pdf_obj *fields, int exclude)
305
0
{
306
0
  pdf_obj *kids = pdf_dict_get(ctx, field, PDF_NAME(Kids));
307
0
  char *needle = pdf_load_field_name(ctx, field);
308
0
  int i, n;
309
310
0
  fz_try(ctx)
311
0
  {
312
0
    n = pdf_array_len(ctx, fields);
313
0
    for (i = 0; i < n; i++)
314
0
    {
315
0
      char *name = pdf_load_field_name(ctx, pdf_array_get(ctx, fields, i));
316
0
      int found = !strcmp(needle, name);
317
0
      fz_free(ctx, name);
318
0
      if (found)
319
0
        break;
320
0
    }
321
0
  }
322
0
  fz_always(ctx)
323
0
    fz_free(ctx, needle);
324
0
  fz_catch(ctx)
325
0
    fz_rethrow(ctx);
326
327
0
  if ((exclude && i < n) || (!exclude && i == n))
328
0
    return;
329
330
0
  pdf_array_push(ctx, array, field);
331
332
0
  if (kids)
333
0
  {
334
0
    int i, n = pdf_array_len(ctx, kids);
335
336
0
    for (i = 0; i < n; i++)
337
0
      add_field_hierarchy_to_array(ctx, array, pdf_array_get(ctx, kids, i), fields, exclude);
338
0
  }
339
0
}
340
341
/*
342
  When resetting or submitting a form, the fields to act upon are defined
343
  by an array of either field references or field names, plus a flag determining
344
  whether to act upon the fields in the array, or all fields other than those in
345
  the array. specified_fields interprets this information and produces the array
346
  of fields to be acted upon.
347
*/
348
static pdf_obj *specified_fields(fz_context *ctx, pdf_document *doc, pdf_obj *fields, int exclude)
349
0
{
350
0
  pdf_obj *form = pdf_dict_getl(ctx, pdf_trailer(ctx, doc), PDF_NAME(Root), PDF_NAME(AcroForm), PDF_NAME(Fields), NULL);
351
0
  int i, n;
352
0
  pdf_obj *result = pdf_new_array(ctx, doc, 0);
353
354
0
  fz_try(ctx)
355
0
  {
356
0
    n = pdf_array_len(ctx, fields);
357
358
0
    for (i = 0; i < n; i++)
359
0
    {
360
0
      pdf_obj *field = pdf_array_get(ctx, fields, i);
361
362
0
      if (pdf_is_string(ctx, field))
363
0
        field = pdf_lookup_field(ctx, form, pdf_to_str_buf(ctx, field));
364
365
0
      if (field)
366
0
        add_field_hierarchy_to_array(ctx, result, field, fields, exclude);
367
0
    }
368
0
  }
369
0
  fz_catch(ctx)
370
0
  {
371
0
    pdf_drop_obj(ctx, result);
372
0
    fz_rethrow(ctx);
373
0
  }
374
375
0
  return result;
376
0
}
377
378
void pdf_reset_form(fz_context *ctx, pdf_document *doc, pdf_obj *fields, int exclude)
379
0
{
380
0
  pdf_obj *sfields = specified_fields(ctx, doc, fields, exclude);
381
0
  fz_try(ctx)
382
0
  {
383
0
    int i, n = pdf_array_len(ctx, sfields);
384
0
    for (i = 0; i < n; i++)
385
0
      reset_form_field(ctx, doc, pdf_array_get(ctx, sfields, i));
386
0
    doc->recalculate = 1;
387
0
  }
388
0
  fz_always(ctx)
389
0
    pdf_drop_obj(ctx, sfields);
390
0
  fz_catch(ctx)
391
0
    fz_rethrow(ctx);
392
0
}
393
394
typedef struct
395
{
396
  pdf_obj *pageobj;
397
  pdf_obj *chk;
398
} lookup_state;
399
400
static void *find_widget_on_page(fz_context *ctx, fz_page *page_, void *state_)
401
0
{
402
0
  lookup_state *state = (lookup_state *) state_;
403
0
  pdf_page *page = (pdf_page *) page_;
404
0
  pdf_annot *widget;
405
406
0
  if (state->pageobj && pdf_objcmp_resolve(ctx, state->pageobj, page->obj))
407
0
    return NULL;
408
409
0
  for (widget = pdf_first_widget(ctx, page); widget != NULL; widget = pdf_next_widget(ctx, widget))
410
0
  {
411
0
    if (!pdf_objcmp_resolve(ctx, state->chk, widget->obj))
412
0
      return widget;
413
0
  }
414
415
0
  return NULL;
416
0
}
417
418
static pdf_annot *find_widget(fz_context *ctx, pdf_document *doc, pdf_obj *chk)
419
0
{
420
0
  lookup_state state;
421
422
0
  state.pageobj = pdf_dict_get(ctx, chk, PDF_NAME(P));
423
0
  state.chk = chk;
424
425
0
  return fz_process_opened_pages(ctx, (fz_document *) doc, find_widget_on_page, &state);
426
0
}
427
428
static void set_check(fz_context *ctx, pdf_document *doc, pdf_obj *chk, pdf_obj *name)
429
0
{
430
0
  pdf_obj *n = pdf_dict_getp(ctx, chk, "AP/N");
431
0
  pdf_obj *val;
432
433
  /* If name is a possible value of this check
434
  * box then use it, otherwise use "Off" */
435
0
  if (pdf_dict_get(ctx, n, name))
436
0
    val = name;
437
0
  else
438
0
    val = PDF_NAME(Off);
439
440
0
  if (pdf_name_eq(ctx, pdf_dict_get(ctx, chk, PDF_NAME(AS)), val))
441
0
    return;
442
443
0
  pdf_dict_put(ctx, chk, PDF_NAME(AS), val);
444
0
  pdf_set_annot_has_changed(ctx, find_widget(ctx, doc, chk));
445
0
}
446
447
/* Set the values of all fields in a group defined by a node
448
 * in the hierarchy */
449
static void set_check_grp(fz_context *ctx, pdf_document *doc, pdf_obj *grp, pdf_obj *val)
450
0
{
451
0
  pdf_obj *kids = pdf_dict_get(ctx, grp, PDF_NAME(Kids));
452
453
0
  if (kids == NULL)
454
0
  {
455
0
    set_check(ctx, doc, grp, val);
456
0
  }
457
0
  else
458
0
  {
459
0
    int i, n = pdf_array_len(ctx, kids);
460
461
0
    for (i = 0; i < n; i++)
462
0
      set_check_grp(ctx, doc, pdf_array_get(ctx, kids, i), val);
463
0
  }
464
0
}
465
466
void pdf_calculate_form(fz_context *ctx, pdf_document *doc)
467
0
{
468
0
  if (doc->js)
469
0
  {
470
0
    fz_try(ctx)
471
0
    {
472
0
      pdf_obj *co = pdf_dict_getp(ctx, pdf_trailer(ctx, doc), "Root/AcroForm/CO");
473
0
      int i, n = pdf_array_len(ctx, co);
474
0
      for (i = 0; i < n; i++)
475
0
      {
476
0
        pdf_obj *field = pdf_array_get(ctx, co, i);
477
0
        pdf_field_event_calculate(ctx, doc, field);
478
0
      }
479
0
    }
480
0
    fz_always(ctx)
481
0
      doc->recalculate = 0;
482
0
    fz_catch(ctx)
483
0
      fz_rethrow(ctx);
484
0
  }
485
0
}
486
487
static pdf_obj *find_on_state(fz_context *ctx, pdf_obj *dict)
488
370
{
489
370
  int i, n = pdf_dict_len(ctx, dict);
490
370
  for (i = 0; i < n; ++i)
491
366
  {
492
366
    pdf_obj *key = pdf_dict_get_key(ctx, dict, i);
493
366
    if (key != PDF_NAME(Off))
494
366
      return key;
495
366
  }
496
4
  return NULL;
497
370
}
498
499
pdf_obj *pdf_button_field_on_state(fz_context *ctx, pdf_obj *field)
500
368
{
501
368
  pdf_obj *ap = pdf_dict_get(ctx, field, PDF_NAME(AP));
502
368
  pdf_obj *on = find_on_state(ctx, pdf_dict_get(ctx, ap, PDF_NAME(N)));
503
368
  if (!on) on = find_on_state(ctx, pdf_dict_get(ctx, ap, PDF_NAME(D)));
504
368
  if (!on) on = PDF_NAME(Yes);
505
368
  return on;
506
368
}
507
508
static void
509
begin_annot_op(fz_context *ctx, pdf_annot *annot, const char *op)
510
0
{
511
0
  if (!annot->page)
512
0
    fz_throw(ctx, FZ_ERROR_ARGUMENT, "annotation not bound to any page");
513
514
0
  pdf_begin_operation(ctx, annot->page->doc, op);
515
0
}
516
517
static void
518
end_annot_op(fz_context *ctx, pdf_annot *annot)
519
0
{
520
0
  pdf_end_operation(ctx, annot->page->doc);
521
0
}
522
523
static void
524
abandon_annot_op(fz_context *ctx, pdf_annot *annot)
525
0
{
526
0
  pdf_abandon_operation(ctx, annot->page->doc);
527
0
}
528
529
static void toggle_check_box(fz_context *ctx, pdf_annot *annot)
530
0
{
531
0
  pdf_document *doc = annot->page->doc;
532
533
0
  begin_annot_op(ctx, annot, "Toggle checkbox");
534
535
0
  fz_try(ctx)
536
0
  {
537
0
    pdf_obj *field = annot->obj;
538
0
    int ff = pdf_field_flags(ctx, field);
539
0
    int is_radio = (ff & PDF_BTN_FIELD_IS_RADIO);
540
0
    int is_no_toggle_to_off = (ff & PDF_BTN_FIELD_IS_NO_TOGGLE_TO_OFF);
541
0
    pdf_obj *grp, *as, *val;
542
543
0
    grp = find_head_of_field_group(ctx, field);
544
0
    if (!grp)
545
0
      grp = field;
546
547
    /* TODO: check V value as well as or instead of AS? */
548
0
    as = pdf_dict_get(ctx, field, PDF_NAME(AS));
549
0
    if (as && as != PDF_NAME(Off))
550
0
    {
551
0
      if (is_radio && is_no_toggle_to_off)
552
0
      {
553
0
        end_annot_op(ctx, annot);
554
0
        break;
555
0
      }
556
0
      val = PDF_NAME(Off);
557
0
    }
558
0
    else
559
0
    {
560
0
      val = pdf_button_field_on_state(ctx, field);
561
0
    }
562
563
0
    pdf_dict_put(ctx, grp, PDF_NAME(V), val);
564
0
    set_check_grp(ctx, doc, grp, val);
565
0
    doc->recalculate = 1;
566
0
    end_annot_op(ctx, annot);
567
0
  }
568
0
  fz_catch(ctx)
569
0
  {
570
0
    abandon_annot_op(ctx, annot);
571
0
    fz_rethrow(ctx);
572
0
  }
573
574
0
  pdf_set_annot_has_changed(ctx, annot);
575
0
}
576
577
int pdf_has_unsaved_changes(fz_context *ctx, pdf_document *doc)
578
0
{
579
0
  int i;
580
581
0
  if (doc->num_incremental_sections == 0)
582
0
    return 0;
583
584
0
  for (i = 0; i < doc->xref_sections->num_objects; i++)
585
0
    if (doc->xref_sections->subsec->table[i].type != 0)
586
0
      break;
587
0
  return i != doc->xref_sections->num_objects;
588
0
}
589
590
int pdf_was_repaired(fz_context *ctx, pdf_document *doc)
591
0
{
592
0
  return doc->repair_attempted;
593
0
}
594
595
int pdf_toggle_widget(fz_context *ctx, pdf_annot *widget)
596
0
{
597
0
  switch (pdf_widget_type(ctx, widget))
598
0
  {
599
0
  default:
600
0
    return 0;
601
0
  case PDF_WIDGET_TYPE_CHECKBOX:
602
0
  case PDF_WIDGET_TYPE_RADIOBUTTON:
603
0
    toggle_check_box(ctx, widget);
604
0
    return 1;
605
0
  }
606
0
  return 0;
607
0
}
608
609
int
610
pdf_update_page(fz_context *ctx, pdf_page *page)
611
1.68k
{
612
1.68k
  pdf_annot *annot;
613
1.68k
  pdf_annot *widget;
614
1.68k
  int changed = 0;
615
616
3.37k
  fz_try(ctx)
617
3.37k
  {
618
1.68k
    pdf_begin_implicit_operation(ctx, page->doc);
619
1.68k
    if (page->doc->recalculate)
620
0
      pdf_calculate_form(ctx, page->doc);
621
622
4.08k
    for (annot = page->annots; annot; annot = annot->next)
623
2.39k
      if (pdf_update_annot(ctx, annot))
624
1.20k
        changed = 1;
625
5.94k
    for (widget = page->widgets; widget; widget = widget->next)
626
4.25k
      if (pdf_update_annot(ctx, widget))
627
2.53k
        changed = 1;
628
1.68k
    pdf_end_operation(ctx, page->doc);
629
1.68k
  }
630
3.37k
  fz_catch(ctx)
631
0
  {
632
0
    pdf_abandon_operation(ctx, page->doc);
633
0
    fz_rethrow(ctx);
634
0
  }
635
636
1.68k
  return changed;
637
1.68k
}
638
639
pdf_annot *pdf_first_widget(fz_context *ctx, pdf_page *page)
640
1.72k
{
641
1.72k
  return page->widgets;
642
1.72k
}
643
644
pdf_annot *pdf_next_widget(fz_context *ctx, pdf_annot *widget)
645
8.51k
{
646
8.51k
  return widget->next;
647
8.51k
}
648
649
enum pdf_widget_type pdf_widget_type(fz_context *ctx, pdf_annot *widget)
650
0
{
651
0
  enum pdf_widget_type ret = PDF_WIDGET_TYPE_BUTTON;
652
653
0
  pdf_annot_push_local_xref(ctx, widget);
654
655
0
  fz_try(ctx)
656
0
  {
657
0
    pdf_obj *subtype = pdf_dict_get(ctx, widget->obj, PDF_NAME(Subtype));
658
0
    if (pdf_name_eq(ctx, subtype, PDF_NAME(Widget)))
659
0
      ret = pdf_field_type(ctx, widget->obj);
660
0
  }
661
0
  fz_always(ctx)
662
0
    pdf_annot_pop_local_xref(ctx, widget);
663
0
  fz_catch(ctx)
664
0
    fz_rethrow(ctx);
665
666
0
  return ret;
667
0
}
668
669
static int set_validated_field_value(fz_context *ctx, pdf_document *doc, pdf_obj *field, const char *text, int ignore_trigger_events)
670
0
{
671
0
  char *newtext = NULL;
672
673
0
  if (!ignore_trigger_events)
674
0
  {
675
0
    if (!pdf_field_event_validate(ctx, doc, field, text, &newtext))
676
0
      return 0;
677
0
  }
678
679
0
  update_field_value(ctx, doc, field, newtext ? newtext : text);
680
681
0
  fz_free(ctx, newtext);
682
683
0
  return 1;
684
0
}
685
686
static void update_checkbox_selector(fz_context *ctx, pdf_document *doc, pdf_obj *field, const char *val)
687
0
{
688
0
  pdf_obj *kids = pdf_dict_get(ctx, field, PDF_NAME(Kids));
689
690
0
  if (kids)
691
0
  {
692
0
    int i, n = pdf_array_len(ctx, kids);
693
694
0
    for (i = 0; i < n; i++)
695
0
      update_checkbox_selector(ctx, doc, pdf_array_get(ctx, kids, i), val);
696
0
  }
697
0
  else
698
0
  {
699
0
    pdf_obj *n = pdf_dict_getp(ctx, field, "AP/N");
700
0
    pdf_obj *oval;
701
702
0
    if (pdf_dict_gets(ctx, n, val))
703
0
      oval = pdf_new_name(ctx, val);
704
0
    else
705
0
      oval = PDF_NAME(Off);
706
0
    pdf_dict_put_drop(ctx, field, PDF_NAME(AS), oval);
707
0
  }
708
0
}
709
710
static int set_checkbox_value(fz_context *ctx, pdf_document *doc, pdf_obj *field, const char *val)
711
0
{
712
0
  update_checkbox_selector(ctx, doc, field, val);
713
0
  update_field_value(ctx, doc, field, val);
714
0
  return 1;
715
0
}
716
717
int pdf_set_field_value(fz_context *ctx, pdf_document *doc, pdf_obj *field, const char *text, int ignore_trigger_events)
718
0
{
719
0
  int accepted = 0;
720
721
0
  switch (pdf_field_type(ctx, field))
722
0
  {
723
0
  case PDF_WIDGET_TYPE_TEXT:
724
0
  case PDF_WIDGET_TYPE_COMBOBOX:
725
0
  case PDF_WIDGET_TYPE_LISTBOX:
726
0
    accepted = set_validated_field_value(ctx, doc, field, text, ignore_trigger_events);
727
0
    break;
728
729
0
  case PDF_WIDGET_TYPE_CHECKBOX:
730
0
  case PDF_WIDGET_TYPE_RADIOBUTTON:
731
0
    accepted = set_checkbox_value(ctx, doc, field, text);
732
0
    break;
733
734
0
  default:
735
0
    update_field_value(ctx, doc, field, text);
736
0
    accepted = 1;
737
0
    break;
738
0
  }
739
740
0
  if (!ignore_trigger_events)
741
0
    doc->recalculate = 1;
742
743
0
  return accepted;
744
0
}
745
746
char *pdf_field_border_style(fz_context *ctx, pdf_obj *field)
747
0
{
748
0
  const char *bs = pdf_to_name(ctx, pdf_dict_getl(ctx, field, PDF_NAME(BS), PDF_NAME(S), NULL));
749
0
  switch (*bs)
750
0
  {
751
0
  case 'S': return "Solid";
752
0
  case 'D': return "Dashed";
753
0
  case 'B': return "Beveled";
754
0
  case 'I': return "Inset";
755
0
  case 'U': return "Underline";
756
0
  }
757
0
  return "Solid";
758
0
}
759
760
void pdf_field_set_border_style(fz_context *ctx, pdf_obj *field, const char *text)
761
0
{
762
0
  pdf_obj *val;
763
764
0
  if (!strcmp(text, "Solid"))
765
0
    val = PDF_NAME(S);
766
0
  else if (!strcmp(text, "Dashed"))
767
0
    val = PDF_NAME(D);
768
0
  else if (!strcmp(text, "Beveled"))
769
0
    val = PDF_NAME(B);
770
0
  else if (!strcmp(text, "Inset"))
771
0
    val = PDF_NAME(I);
772
0
  else if (!strcmp(text, "Underline"))
773
0
    val = PDF_NAME(U);
774
0
  else
775
0
    return;
776
777
0
  pdf_dict_putl_drop(ctx, field, val, PDF_NAME(BS), PDF_NAME(S), NULL);
778
0
  pdf_field_mark_dirty(ctx, field);
779
0
}
780
781
void pdf_field_set_button_caption(fz_context *ctx, pdf_obj *field, const char *text)
782
0
{
783
0
  if (pdf_field_type(ctx, field) == PDF_WIDGET_TYPE_BUTTON)
784
0
  {
785
0
    pdf_obj *val = pdf_new_text_string(ctx, text);
786
0
    pdf_dict_putl_drop(ctx, field, val, PDF_NAME(MK), PDF_NAME(CA), NULL);
787
0
    pdf_field_mark_dirty(ctx, field);
788
0
  }
789
0
}
790
791
int pdf_field_display(fz_context *ctx, pdf_obj *field)
792
0
{
793
0
  pdf_obj *kids;
794
0
  int f, res = Display_Visible;
795
796
  /* Base response on first of children. Not ideal,
797
   * but not clear how to handle children with
798
   * differing values */
799
0
  while ((kids = pdf_dict_get(ctx, field, PDF_NAME(Kids))) != NULL)
800
0
    field = pdf_array_get(ctx, kids, 0);
801
802
0
  f = pdf_dict_get_int(ctx, field, PDF_NAME(F));
803
804
0
  if (f & PDF_ANNOT_IS_HIDDEN)
805
0
  {
806
0
    res = Display_Hidden;
807
0
  }
808
0
  else if (f & PDF_ANNOT_IS_PRINT)
809
0
  {
810
0
    if (f & PDF_ANNOT_IS_NO_VIEW)
811
0
      res = Display_NoView;
812
0
  }
813
0
  else
814
0
  {
815
0
    if (f & PDF_ANNOT_IS_NO_VIEW)
816
0
      res = Display_Hidden;
817
0
    else
818
0
      res = Display_NoPrint;
819
0
  }
820
821
0
  return res;
822
0
}
823
824
/*
825
 * get the field name in a char buffer that has spare room to
826
 * add more characters at the end.
827
 */
828
static char *load_field_name(fz_context *ctx, pdf_obj *field, int spare, pdf_cycle_list *cycle_up)
829
0
{
830
0
  pdf_cycle_list cycle;
831
0
  char *res = NULL;
832
0
  pdf_obj *parent;
833
0
  const char *lname;
834
0
  int llen;
835
836
0
  if (pdf_cycle(ctx, &cycle, cycle_up, field))
837
0
    fz_throw(ctx, FZ_ERROR_FORMAT, "Cycle in field parents");
838
839
0
  parent = pdf_dict_get(ctx, field, PDF_NAME(Parent));
840
0
  lname = pdf_dict_get_text_string(ctx, field, PDF_NAME(T));
841
0
  llen = (int)strlen(lname);
842
843
  // Limit fields to 16K
844
0
  if (llen > (16 << 10) || llen + spare > (16 << 10))
845
0
    fz_throw(ctx, FZ_ERROR_LIMIT, "Field name too long");
846
847
  /*
848
   * If we found a name at this point in the field hierarchy
849
   * then we'll need extra space for it and a dot
850
   */
851
0
  if (llen)
852
0
    spare += llen+1;
853
854
0
  if (parent)
855
0
  {
856
0
    res = load_field_name(ctx, parent, spare, &cycle);
857
0
  }
858
0
  else
859
0
  {
860
0
    res = Memento_label(fz_malloc(ctx, spare+1), "form_field_name");
861
0
    res[0] = 0;
862
0
  }
863
864
0
  if (llen)
865
0
  {
866
0
    if (res[0])
867
0
      strcat(res, ".");
868
869
0
    strcat(res, lname);
870
0
  }
871
872
0
  return res;
873
0
}
874
875
char *pdf_load_field_name(fz_context *ctx, pdf_obj *field)
876
0
{
877
0
  return load_field_name(ctx, field, 0, NULL);
878
0
}
879
880
void pdf_create_field_name(fz_context *ctx, pdf_document *doc, const char *prefix, char *buf, size_t len)
881
0
{
882
0
  pdf_obj *form = pdf_dict_getl(ctx, pdf_trailer(ctx, doc),
883
0
    PDF_NAME(Root), PDF_NAME(AcroForm), PDF_NAME(Fields), NULL);
884
0
  int i;
885
0
  for (i = 0; i < 65536; ++i) {
886
0
    fz_snprintf(buf, len, "%s%d", prefix, i);
887
0
    if (!pdf_lookup_field(ctx, form, buf))
888
0
      return;
889
0
  }
890
0
  fz_throw(ctx, FZ_ERROR_LIMIT, "Could not create unique field name.");
891
0
}
892
893
const char *pdf_field_label(fz_context *ctx, pdf_obj *field)
894
0
{
895
0
  pdf_obj *label = pdf_dict_get_inheritable(ctx, field, PDF_NAME(TU));
896
0
  if (!label)
897
0
    label = pdf_dict_get_inheritable(ctx, field, PDF_NAME(T));
898
0
  if (label)
899
0
    return pdf_to_text_string(ctx, label);
900
0
  return "Unnamed";
901
0
}
902
903
void pdf_field_set_display(fz_context *ctx, pdf_obj *field, int d)
904
0
{
905
0
  pdf_obj *kids = pdf_dict_get(ctx, field, PDF_NAME(Kids));
906
907
0
  if (!kids)
908
0
  {
909
0
    int mask = (PDF_ANNOT_IS_HIDDEN|PDF_ANNOT_IS_PRINT|PDF_ANNOT_IS_NO_VIEW);
910
0
    int f = pdf_dict_get_int(ctx, field, PDF_NAME(F)) & ~mask;
911
912
0
    switch (d)
913
0
    {
914
0
    case Display_Visible:
915
0
      f |= PDF_ANNOT_IS_PRINT;
916
0
      break;
917
0
    case Display_Hidden:
918
0
      f |= PDF_ANNOT_IS_HIDDEN;
919
0
      break;
920
0
    case Display_NoView:
921
0
      f |= (PDF_ANNOT_IS_PRINT|PDF_ANNOT_IS_NO_VIEW);
922
0
      break;
923
0
    case Display_NoPrint:
924
0
      break;
925
0
    }
926
927
0
    pdf_dict_put_int(ctx, field, PDF_NAME(F), f);
928
0
  }
929
0
  else
930
0
  {
931
0
    int i, n = pdf_array_len(ctx, kids);
932
933
0
    for (i = 0; i < n; i++)
934
0
      pdf_field_set_display(ctx, pdf_array_get(ctx, kids, i), d);
935
0
  }
936
0
}
937
938
void pdf_field_set_fill_color(fz_context *ctx, pdf_obj *field, pdf_obj *col)
939
0
{
940
  /* col == NULL mean transparent, but we can simply pass it on as with
941
   * non-NULL values because pdf_dict_putp interprets a NULL value as
942
   * delete */
943
0
  pdf_dict_putl(ctx, field, col, PDF_NAME(MK), PDF_NAME(BG), NULL);
944
0
  pdf_field_mark_dirty(ctx, field);
945
0
}
946
947
void pdf_field_set_text_color(fz_context *ctx, pdf_obj *field, pdf_obj *col)
948
0
{
949
0
  char buf[100];
950
0
  const char *font;
951
0
  float size, color[4];
952
  /* TODO? */
953
0
  const char *da = pdf_to_str_buf(ctx, pdf_dict_get_inheritable(ctx, field, PDF_NAME(DA)));
954
0
  int n;
955
956
0
  pdf_parse_default_appearance(ctx, da, &font, &size, &n, color);
957
958
0
  switch (pdf_array_len(ctx, col))
959
0
  {
960
0
  default:
961
0
    n = 0;
962
0
    color[0] = color[1] = color[2] = color[3] = 0;
963
0
    break;
964
0
  case 1:
965
0
    n = 1;
966
0
    color[0] = pdf_array_get_real(ctx, col, 0);
967
0
    break;
968
0
  case 3:
969
0
    n = 3;
970
0
    color[0] = pdf_array_get_real(ctx, col, 0);
971
0
    color[1] = pdf_array_get_real(ctx, col, 1);
972
0
    color[2] = pdf_array_get_real(ctx, col, 2);
973
0
    break;
974
0
  case 4:
975
0
    n = 4;
976
0
    color[0] = pdf_array_get_real(ctx, col, 0);
977
0
    color[1] = pdf_array_get_real(ctx, col, 1);
978
0
    color[2] = pdf_array_get_real(ctx, col, 2);
979
0
    color[3] = pdf_array_get_real(ctx, col, 3);
980
0
    break;
981
0
  }
982
983
0
  pdf_print_default_appearance(ctx, buf, sizeof buf, font, size, n, color);
984
0
  pdf_dict_put_string(ctx, field, PDF_NAME(DA), buf, strlen(buf));
985
0
  pdf_field_mark_dirty(ctx, field);
986
0
}
987
988
pdf_annot *
989
pdf_keep_widget(fz_context *ctx, pdf_annot *widget)
990
0
{
991
0
  return pdf_keep_annot(ctx, widget);
992
0
}
993
994
void
995
pdf_drop_widget(fz_context *ctx, pdf_annot *widget)
996
4.25k
{
997
4.25k
  pdf_drop_annot(ctx, widget);
998
4.25k
}
999
1000
void
1001
pdf_drop_widgets(fz_context *ctx, pdf_annot *widget)
1002
10.8k
{
1003
15.1k
  while (widget)
1004
4.25k
  {
1005
4.25k
    pdf_annot *next = widget->next;
1006
4.25k
    pdf_drop_widget(ctx, widget);
1007
4.25k
    widget = next;
1008
4.25k
  }
1009
10.8k
}
1010
1011
pdf_annot *
1012
pdf_create_signature_widget(fz_context *ctx, pdf_page *page, char *name)
1013
0
{
1014
0
  fz_rect rect = { 12, 12, 12+100, 12+50 };
1015
0
  pdf_annot *annot;
1016
1017
0
  pdf_begin_operation(ctx, page->doc, "Create signature");
1018
1019
0
  annot = pdf_create_annot_raw(ctx, page, PDF_ANNOT_WIDGET);
1020
1021
0
  fz_try(ctx)
1022
0
  {
1023
0
    pdf_obj *obj = annot->obj;
1024
0
    pdf_obj *root = pdf_dict_get(ctx, pdf_trailer(ctx, page->doc), PDF_NAME(Root));
1025
0
    pdf_obj *acroform = pdf_dict_get(ctx, root, PDF_NAME(AcroForm));
1026
0
    pdf_obj *fields, *lock;
1027
0
    if (!acroform)
1028
0
    {
1029
0
      acroform = pdf_new_dict(ctx, page->doc, 1);
1030
0
      pdf_dict_put_drop(ctx, root, PDF_NAME(AcroForm), acroform);
1031
0
    }
1032
0
    fields = pdf_dict_get(ctx, acroform, PDF_NAME(Fields));
1033
0
    if (!fields)
1034
0
    {
1035
0
      fields = pdf_new_array(ctx, page->doc, 1);
1036
0
      pdf_dict_put_drop(ctx, acroform, PDF_NAME(Fields), fields);
1037
0
    }
1038
0
    pdf_set_annot_rect(ctx, annot, rect);
1039
0
    pdf_dict_put(ctx, obj, PDF_NAME(FT), PDF_NAME(Sig));
1040
0
    pdf_dict_put_int(ctx, obj, PDF_NAME(F), PDF_ANNOT_IS_PRINT);
1041
0
    pdf_dict_put_text_string(ctx, obj, PDF_NAME(DA), "/Helv 0 Tf 0 g");
1042
0
    pdf_dict_put_text_string(ctx, obj, PDF_NAME(T), name);
1043
0
    pdf_array_push(ctx, fields, obj);
1044
0
    lock = pdf_dict_put_dict(ctx, obj, PDF_NAME(Lock), 1);
1045
0
    pdf_dict_put(ctx, lock, PDF_NAME(Action), PDF_NAME(All));
1046
0
    pdf_end_operation(ctx, page->doc);
1047
0
  }
1048
0
  fz_catch(ctx)
1049
0
  {
1050
0
    pdf_abandon_operation(ctx, page->doc);
1051
0
    pdf_delete_annot(ctx, page, annot);
1052
0
  }
1053
0
  return (pdf_annot *)annot;
1054
0
}
1055
1056
fz_rect
1057
pdf_bound_widget(fz_context *ctx, pdf_annot *widget)
1058
0
{
1059
0
  return pdf_bound_annot(ctx, widget);
1060
0
}
1061
1062
int
1063
pdf_update_widget(fz_context *ctx, pdf_annot *widget)
1064
0
{
1065
0
  return pdf_update_annot(ctx, widget);
1066
0
}
1067
1068
int pdf_text_widget_max_len(fz_context *ctx, pdf_annot *tw)
1069
0
{
1070
0
  pdf_annot *annot = (pdf_annot *)tw;
1071
0
  return pdf_dict_get_inheritable_int(ctx, annot->obj, PDF_NAME(MaxLen));
1072
0
}
1073
1074
int pdf_text_widget_format(fz_context *ctx, pdf_annot *tw)
1075
0
{
1076
0
  pdf_annot *annot = (pdf_annot *)tw;
1077
0
  int type = PDF_WIDGET_TX_FORMAT_NONE;
1078
0
  pdf_obj *js = pdf_dict_getl(ctx, annot->obj, PDF_NAME(AA), PDF_NAME(F), PDF_NAME(JS), NULL);
1079
0
  if (js)
1080
0
  {
1081
0
    char *code = pdf_load_stream_or_string_as_utf8(ctx, js);
1082
0
    if (strstr(code, "AFNumber_Format"))
1083
0
      type = PDF_WIDGET_TX_FORMAT_NUMBER;
1084
0
    else if (strstr(code, "AFSpecial_Format"))
1085
0
      type = PDF_WIDGET_TX_FORMAT_SPECIAL;
1086
0
    else if (strstr(code, "AFDate_FormatEx"))
1087
0
      type = PDF_WIDGET_TX_FORMAT_DATE;
1088
0
    else if (strstr(code, "AFTime_FormatEx"))
1089
0
      type = PDF_WIDGET_TX_FORMAT_TIME;
1090
0
    fz_free(ctx, code);
1091
0
  }
1092
1093
0
  return type;
1094
0
}
1095
1096
static char *
1097
merge_changes(fz_context *ctx, const char *value, int start, int end, const char *change)
1098
0
{
1099
0
  int changelen = change ? (int)strlen(change) : 0;
1100
0
  int valuelen = value ? (int)strlen(value) : 0;
1101
0
  int prelen = (start >= 0 ? (start < valuelen ? start : valuelen) : 0);
1102
0
  int postlen = (end >= 0 && end <= valuelen ? valuelen - end : 0);
1103
0
  int newlen =  prelen + changelen + postlen + 1;
1104
0
  char *merged = fz_malloc(ctx, newlen);
1105
0
  char *m = merged;
1106
1107
0
  if (prelen)
1108
0
  {
1109
0
    memcpy(m, value, prelen);
1110
0
    m += prelen;
1111
0
  }
1112
0
  if (changelen)
1113
0
  {
1114
0
    memcpy(m, change, changelen);
1115
0
    m += changelen;
1116
0
  }
1117
0
  if (postlen)
1118
0
  {
1119
0
    memcpy(m, &value[end], postlen);
1120
0
    m += postlen;
1121
0
  }
1122
0
  *m = 0;
1123
1124
0
  return merged;
1125
0
}
1126
1127
int pdf_set_text_field_value(fz_context *ctx, pdf_annot *widget, const char *update)
1128
0
{
1129
0
  pdf_document *doc;
1130
0
  pdf_keystroke_event evt = { 0 };
1131
0
  char *new_change = NULL;
1132
0
  char *new_value = NULL;
1133
0
  char *merged_value = NULL;
1134
0
  int rc = 1;
1135
1136
0
  begin_annot_op(ctx, widget, "Edit text field");
1137
0
  doc = widget->page->doc;
1138
1139
0
  fz_var(new_value);
1140
0
  fz_var(new_change);
1141
0
  fz_var(merged_value);
1142
0
  fz_try(ctx)
1143
0
  {
1144
0
    if (!widget->ignore_trigger_events)
1145
0
    {
1146
0
      evt.value = pdf_annot_field_value(ctx, widget);
1147
0
      evt.change = update;
1148
0
      evt.selStart = 0;
1149
0
      evt.selEnd = (int)strlen(evt.value);
1150
0
      evt.willCommit = 0;
1151
0
      rc = pdf_annot_field_event_keystroke(ctx, doc, widget, &evt);
1152
0
      new_change = evt.newChange;
1153
0
      new_value = evt.newValue;
1154
0
      evt.newValue = NULL;
1155
0
      evt.newChange = NULL;
1156
0
      if (rc)
1157
0
      {
1158
0
        merged_value = merge_changes(ctx, new_value, evt.selStart, evt.selEnd, new_change);
1159
0
        evt.value = merged_value;
1160
0
        evt.change = "";
1161
0
        evt.selStart = -1;
1162
0
        evt.selEnd = -1;
1163
0
        evt.willCommit = 1;
1164
0
        rc = pdf_annot_field_event_keystroke(ctx, doc, widget, &evt);
1165
0
        if (rc)
1166
0
          rc = pdf_set_annot_field_value(ctx, doc, widget, evt.newValue, 0);
1167
0
      }
1168
0
    }
1169
0
    else
1170
0
    {
1171
0
      rc = pdf_set_annot_field_value(ctx, doc, widget, update, 1);
1172
0
    }
1173
0
    end_annot_op(ctx, widget);
1174
0
  }
1175
0
  fz_always(ctx)
1176
0
  {
1177
0
    fz_free(ctx, new_value);
1178
0
    fz_free(ctx, evt.newValue);
1179
0
    fz_free(ctx, new_change);
1180
0
    fz_free(ctx, evt.newChange);
1181
0
    fz_free(ctx, merged_value);
1182
0
  }
1183
0
  fz_catch(ctx)
1184
0
  {
1185
0
    abandon_annot_op(ctx, widget);
1186
0
    fz_warn(ctx, "could not set widget text");
1187
0
    rc = 0;
1188
0
  }
1189
0
  return rc;
1190
0
}
1191
1192
int pdf_edit_text_field_value(fz_context *ctx, pdf_annot *widget, const char *value, const char *change, int *selStart, int *selEnd, char **result)
1193
0
{
1194
0
  pdf_document *doc = widget->page->doc;
1195
0
  pdf_keystroke_event evt = {0};
1196
0
  int rc = 1;
1197
1198
0
  pdf_begin_operation(ctx, doc, "Text field keystroke");
1199
1200
0
  fz_try(ctx)
1201
0
  {
1202
0
    if (!widget->ignore_trigger_events)
1203
0
    {
1204
0
      evt.value = value;
1205
0
      evt.change = change;
1206
0
      evt.selStart = *selStart;
1207
0
      evt.selEnd = *selEnd;
1208
0
      evt.willCommit = 0;
1209
0
      rc = pdf_annot_field_event_keystroke(ctx, doc, widget, &evt);
1210
0
      if (rc)
1211
0
      {
1212
0
        *result = merge_changes(ctx, evt.newValue, evt.selStart, evt.selEnd, evt.newChange);
1213
0
        *selStart = evt.selStart + (int)strlen(evt.newChange);
1214
0
        *selEnd = *selStart;
1215
0
      }
1216
0
    }
1217
0
    else
1218
0
    {
1219
0
      *result = merge_changes(ctx, value, *selStart, *selEnd, change);
1220
0
      *selStart = evt.selStart + (int)strlen(change);
1221
0
      *selEnd = *selStart;
1222
0
    }
1223
0
    pdf_end_operation(ctx, doc);
1224
0
  }
1225
0
  fz_always(ctx)
1226
0
  {
1227
0
    fz_free(ctx, evt.newValue);
1228
0
    fz_free(ctx, evt.newChange);
1229
0
  }
1230
0
  fz_catch(ctx)
1231
0
  {
1232
0
    pdf_abandon_operation(ctx, doc);
1233
0
    fz_warn(ctx, "could not process text widget keystroke");
1234
0
    rc = 0;
1235
0
  }
1236
0
  return rc;
1237
0
}
1238
1239
int pdf_set_choice_field_value(fz_context *ctx, pdf_annot *widget, const char *new_value)
1240
0
{
1241
  /* Choice widgets use almost the same keystroke processing as text fields. */
1242
0
  return pdf_set_text_field_value(ctx, widget, new_value);
1243
0
}
1244
1245
int pdf_choice_widget_options(fz_context *ctx, pdf_annot *tw, int exportval, const char *opts[])
1246
0
{
1247
0
  pdf_annot *annot = (pdf_annot *)tw;
1248
0
  pdf_obj *optarr;
1249
0
  int i, n, m;
1250
1251
0
  optarr = pdf_dict_get_inheritable(ctx, annot->obj, PDF_NAME(Opt));
1252
0
  n = pdf_array_len(ctx, optarr);
1253
1254
0
  if (opts)
1255
0
  {
1256
0
    for (i = 0; i < n; i++)
1257
0
    {
1258
0
      m = pdf_array_len(ctx, pdf_array_get(ctx, optarr, i));
1259
      /* If it is a two element array, the second item is the one that we want if we want the listing value. */
1260
0
      if (m == 2)
1261
0
        if (exportval)
1262
0
          opts[i] = pdf_array_get_text_string(ctx, pdf_array_get(ctx, optarr, i), 0);
1263
0
        else
1264
0
          opts[i] = pdf_array_get_text_string(ctx, pdf_array_get(ctx, optarr, i), 1);
1265
0
      else
1266
0
        opts[i] = pdf_array_get_text_string(ctx, optarr, i);
1267
0
    }
1268
0
  }
1269
0
  return n;
1270
0
}
1271
1272
int pdf_choice_field_option_count(fz_context *ctx, pdf_obj *field)
1273
0
{
1274
0
  pdf_obj *opt = pdf_dict_get_inheritable(ctx, field, PDF_NAME(Opt));
1275
0
  return pdf_array_len(ctx, opt);
1276
0
}
1277
1278
const char *pdf_choice_field_option(fz_context *ctx, pdf_obj *field, int export, int i)
1279
0
{
1280
0
  pdf_obj *opt = pdf_dict_get_inheritable(ctx, field, PDF_NAME(Opt));
1281
0
  pdf_obj *ent = pdf_array_get(ctx, opt, i);
1282
0
  if (pdf_array_len(ctx, ent) == 2)
1283
0
    return pdf_array_get_text_string(ctx, ent, export ? 0 : 1);
1284
0
  else
1285
0
    return pdf_to_text_string(ctx, ent);
1286
0
}
1287
1288
int pdf_choice_widget_is_multiselect(fz_context *ctx, pdf_annot *tw)
1289
0
{
1290
0
  pdf_annot *annot = (pdf_annot *)tw;
1291
1292
0
  if (!annot) return 0;
1293
1294
0
  switch (pdf_field_type(ctx, annot->obj))
1295
0
  {
1296
0
  case PDF_WIDGET_TYPE_LISTBOX:
1297
0
    return (pdf_field_flags(ctx, annot->obj) & PDF_CH_FIELD_IS_MULTI_SELECT) != 0;
1298
0
  default:
1299
0
    return 0;
1300
0
  }
1301
0
}
1302
1303
int pdf_choice_widget_value(fz_context *ctx, pdf_annot *tw, const char *opts[])
1304
0
{
1305
0
  pdf_annot *annot = (pdf_annot *)tw;
1306
0
  pdf_obj *optarr;
1307
0
  int i, n;
1308
1309
0
  if (!annot)
1310
0
    return 0;
1311
1312
0
  optarr = pdf_dict_get(ctx, annot->obj, PDF_NAME(V));
1313
1314
0
  if (pdf_is_string(ctx, optarr))
1315
0
  {
1316
0
    if (opts)
1317
0
      opts[0] = pdf_to_text_string(ctx, optarr);
1318
0
    return 1;
1319
0
  }
1320
0
  else
1321
0
  {
1322
0
    n = pdf_array_len(ctx, optarr);
1323
0
    if (opts)
1324
0
    {
1325
0
      for (i = 0; i < n; i++)
1326
0
      {
1327
0
        pdf_obj *elem = pdf_array_get(ctx, optarr, i);
1328
0
        if (pdf_is_array(ctx, elem))
1329
0
          elem = pdf_array_get(ctx, elem, 1);
1330
0
        opts[i] = pdf_to_text_string(ctx, elem);
1331
0
      }
1332
0
    }
1333
0
    return n;
1334
0
  }
1335
0
}
1336
1337
void pdf_choice_widget_set_value(fz_context *ctx, pdf_annot *tw, int n, const char *opts[])
1338
0
{
1339
0
  pdf_annot *annot = (pdf_annot *)tw;
1340
0
  pdf_obj *optarr = NULL;
1341
0
  int i;
1342
1343
0
  if (!annot)
1344
0
    return;
1345
1346
0
  begin_annot_op(ctx, annot, "Set choice");
1347
1348
0
  fz_var(optarr);
1349
0
  fz_try(ctx)
1350
0
  {
1351
0
    if (n != 1)
1352
0
    {
1353
0
      optarr = pdf_new_array(ctx, annot->page->doc, n);
1354
1355
0
      for (i = 0; i < n; i++)
1356
0
        pdf_array_push_text_string(ctx, optarr, opts[i]);
1357
1358
0
      pdf_dict_put_drop(ctx, annot->obj, PDF_NAME(V), optarr);
1359
0
    }
1360
0
    else
1361
0
      pdf_dict_put_text_string(ctx, annot->obj, PDF_NAME(V), opts[0]);
1362
1363
    /* FIXME: when n > 1, we should be regenerating the indexes */
1364
0
    pdf_dict_del(ctx, annot->obj, PDF_NAME(I));
1365
1366
0
    pdf_field_mark_dirty(ctx, annot->obj);
1367
0
    end_annot_op(ctx, annot);
1368
0
  }
1369
0
  fz_catch(ctx)
1370
0
  {
1371
0
    abandon_annot_op(ctx, annot);
1372
0
    pdf_drop_obj(ctx, optarr);
1373
0
    fz_rethrow(ctx);
1374
0
  }
1375
0
}
1376
1377
int pdf_signature_byte_range(fz_context *ctx, pdf_document *doc, pdf_obj *signature, fz_range *byte_range)
1378
0
{
1379
0
  pdf_obj *br = pdf_dict_getl(ctx, signature, PDF_NAME(V), PDF_NAME(ByteRange), NULL);
1380
0
  int i, n = pdf_array_len(ctx, br)/2;
1381
1382
0
  if (byte_range)
1383
0
  {
1384
0
    for (i = 0; i < n; i++)
1385
0
    {
1386
0
      int64_t offset = pdf_array_get_int(ctx, br, 2*i);
1387
0
      int length = pdf_array_get_int(ctx, br, 2*i+1);
1388
1389
0
      if (offset < 0 || offset > doc->file_size)
1390
0
        fz_throw(ctx, FZ_ERROR_FORMAT, "offset of signature byte range outside of file");
1391
0
      else if (length < 0)
1392
0
        fz_throw(ctx, FZ_ERROR_FORMAT, "length of signature byte range negative");
1393
0
      else if (offset + length > doc->file_size)
1394
0
        fz_throw(ctx, FZ_ERROR_FORMAT, "signature byte range extends past end of file");
1395
1396
0
      byte_range[i].offset = offset;
1397
0
      byte_range[i].length = length;
1398
0
    }
1399
0
  }
1400
1401
0
  return n;
1402
0
}
1403
1404
static int is_white(int c)
1405
0
{
1406
0
  return c == '\x00' || c == '\x09' || c == '\x0a' || c == '\x0c' || c == '\x0d' || c == '\x20';
1407
0
}
1408
1409
static int is_hex_or_white(int c)
1410
0
{
1411
0
  return (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f') || (c >= '0' && c <= '9') || is_white(c);
1412
0
}
1413
1414
static void validate_certificate_data(fz_context *ctx, pdf_document *doc, fz_range *hole)
1415
0
{
1416
0
  fz_stream *stm;
1417
0
  int c;
1418
1419
0
  stm = fz_open_range_filter(ctx, doc->file, hole, 1);
1420
0
  fz_try(ctx)
1421
0
  {
1422
0
    while (is_white((c = fz_read_byte(ctx, stm))))
1423
0
      ;
1424
1425
0
    if (c == '<')
1426
0
      c = fz_read_byte(ctx, stm);
1427
1428
0
    while (is_hex_or_white(c))
1429
0
      c = fz_read_byte(ctx, stm);
1430
1431
0
    if (c == '>')
1432
0
      c = fz_read_byte(ctx, stm);
1433
1434
0
    while (is_white(c))
1435
0
      c = fz_read_byte(ctx, stm);
1436
1437
0
    if (c != EOF)
1438
0
      fz_throw(ctx, FZ_ERROR_FORMAT, "signature certificate data contains invalid character");
1439
0
    if ((size_t)fz_tell(ctx, stm) != hole->length)
1440
0
      fz_throw(ctx, FZ_ERROR_FORMAT, "premature end of signature certificate data");
1441
0
  }
1442
0
  fz_always(ctx)
1443
0
    fz_drop_stream(ctx, stm);
1444
0
  fz_catch(ctx)
1445
0
    fz_rethrow(ctx);
1446
0
}
1447
1448
static int rangecmp(const void *a_, const void *b_)
1449
0
{
1450
0
  const fz_range *a = (const fz_range *) a_;
1451
0
  const fz_range *b = (const fz_range *) b_;
1452
0
  return (int) (a->offset - b->offset);
1453
0
}
1454
1455
static void validate_byte_ranges(fz_context *ctx, pdf_document *doc, fz_range *unsorted, int nranges)
1456
0
{
1457
0
  int64_t offset = 0;
1458
0
  fz_range *sorted;
1459
0
  int i;
1460
1461
0
  sorted = fz_calloc(ctx, nranges, sizeof(*sorted));
1462
0
  memcpy(sorted, unsorted, nranges * sizeof(*sorted));
1463
0
  qsort(sorted, nranges, sizeof(*sorted), rangecmp);
1464
1465
0
  fz_try(ctx)
1466
0
  {
1467
0
    offset = 0;
1468
1469
0
    for (i = 0; i < nranges; i++)
1470
0
    {
1471
0
      if (sorted[i].offset > offset)
1472
0
      {
1473
0
        fz_range hole;
1474
1475
0
        hole.offset = offset;
1476
0
        hole.length = sorted[i].offset - offset;
1477
1478
0
        validate_certificate_data(ctx, doc, &hole);
1479
0
      }
1480
1481
0
      offset = fz_maxi64(offset, sorted[i].offset + sorted[i].length);
1482
0
    }
1483
0
  }
1484
0
  fz_always(ctx)
1485
0
    fz_free(ctx, sorted);
1486
0
  fz_catch(ctx)
1487
0
    fz_rethrow(ctx);
1488
0
}
1489
1490
fz_stream *pdf_signature_hash_bytes(fz_context *ctx, pdf_document *doc, pdf_obj *signature)
1491
0
{
1492
0
  fz_range *byte_range = NULL;
1493
0
  int byte_range_len;
1494
0
  fz_stream *bytes = NULL;
1495
1496
0
  fz_var(byte_range);
1497
0
  fz_try(ctx)
1498
0
  {
1499
0
    byte_range_len = pdf_signature_byte_range(ctx, doc, signature, NULL);
1500
0
    if (byte_range_len)
1501
0
    {
1502
0
      byte_range = fz_calloc(ctx, byte_range_len, sizeof(*byte_range));
1503
0
      pdf_signature_byte_range(ctx, doc, signature, byte_range);
1504
0
    }
1505
1506
0
    validate_byte_ranges(ctx, doc, byte_range, byte_range_len);
1507
0
    bytes = fz_open_range_filter(ctx, doc->file, byte_range, byte_range_len);
1508
0
  }
1509
0
  fz_always(ctx)
1510
0
  {
1511
0
    fz_free(ctx, byte_range);
1512
0
  }
1513
0
  fz_catch(ctx)
1514
0
  {
1515
0
    fz_rethrow(ctx);
1516
0
  }
1517
1518
0
  return bytes;
1519
0
}
1520
1521
int pdf_incremental_change_since_signing_widget(fz_context *ctx, pdf_annot *widget)
1522
0
{
1523
0
  if (!widget->page)
1524
0
    fz_throw(ctx, FZ_ERROR_GENERIC, "annotation not bound to any page");
1525
0
  return pdf_signature_incremental_change_since_signing(ctx, widget->page->doc, widget->obj);
1526
0
}
1527
1528
int pdf_signature_incremental_change_since_signing(fz_context *ctx, pdf_document *doc, pdf_obj *signature)
1529
0
{
1530
0
  fz_range *byte_range = NULL;
1531
0
  int byte_range_len;
1532
0
  int changed = 0;
1533
1534
0
  if (pdf_dict_get_inheritable(ctx, signature, PDF_NAME(FT)) != PDF_NAME(Sig))
1535
0
    fz_throw(ctx, FZ_ERROR_GENERIC, "annotation is not a signature widget");
1536
0
  if (!pdf_signature_is_signed(ctx, doc, signature))
1537
0
    return 0;
1538
1539
0
  fz_var(byte_range);
1540
0
  fz_try(ctx)
1541
0
  {
1542
0
    byte_range_len = pdf_signature_byte_range(ctx, doc, signature, NULL);
1543
0
    if (byte_range_len)
1544
0
    {
1545
0
      fz_range *last_range;
1546
0
      int64_t end_of_range;
1547
1548
0
      byte_range = fz_calloc(ctx, byte_range_len, sizeof(*byte_range));
1549
0
      pdf_signature_byte_range(ctx, doc, signature, byte_range);
1550
1551
0
      last_range = &byte_range[byte_range_len -1];
1552
0
      end_of_range = last_range->offset + last_range->length;
1553
1554
      /* We can see how long the document was when signed by inspecting the byte
1555
       * ranges of the signature.  The document, when read in, may have already
1556
       * had changes tagged on to it, past its extent when signed, or we may have
1557
       * made changes since reading it, which will be held in a new incremental
1558
       * xref section. */
1559
0
      if (doc->file_size > end_of_range || doc->num_incremental_sections > 0)
1560
0
        changed = 1;
1561
0
    }
1562
0
  }
1563
0
  fz_always(ctx)
1564
0
  {
1565
0
    fz_free(ctx, byte_range);
1566
0
  }
1567
0
  fz_catch(ctx)
1568
0
  {
1569
0
    fz_rethrow(ctx);
1570
0
  }
1571
1572
0
  return changed;
1573
0
}
1574
1575
int pdf_signature_is_signed(fz_context *ctx, pdf_document *doc, pdf_obj *field)
1576
10
{
1577
10
  pdf_obj *v;
1578
10
  pdf_obj* vtype;
1579
1580
10
  if (pdf_dict_get_inheritable(ctx, field, PDF_NAME(FT)) != PDF_NAME(Sig))
1581
0
    return 0;
1582
  /* Signatures can only be signed if the value is a dictionary,
1583
   * and if the value has a Type, it should be Sig. */
1584
10
  v = pdf_dict_get_inheritable(ctx, field, PDF_NAME(V));
1585
10
  vtype = pdf_dict_get(ctx, v, PDF_NAME(Type));
1586
10
  return pdf_is_dict(ctx, v) && (vtype ? pdf_name_eq(ctx, vtype, PDF_NAME(Sig)) : 1);
1587
10
}
1588
1589
int pdf_widget_is_signed(fz_context *ctx, pdf_annot *widget)
1590
0
{
1591
0
  if (widget == NULL)
1592
0
    return 0;
1593
1594
0
  if (!widget->page)
1595
0
    fz_throw(ctx, FZ_ERROR_GENERIC, "annotation not bound to any page");
1596
1597
0
  return pdf_signature_is_signed(ctx, widget->page->doc, widget->obj);
1598
0
}
1599
1600
int pdf_widget_is_readonly(fz_context *ctx, pdf_annot *widget)
1601
0
{
1602
0
  int fflags;
1603
0
  if (widget == NULL)
1604
0
    return 0;
1605
0
  fflags = pdf_field_flags(ctx, ((pdf_annot *) widget)->obj);
1606
0
  return fflags & PDF_FIELD_IS_READ_ONLY;
1607
0
}
1608
1609
size_t pdf_signature_contents(fz_context *ctx, pdf_document *doc, pdf_obj *signature, char **contents)
1610
0
{
1611
0
  pdf_obj *v_ref = pdf_dict_get_inheritable(ctx, signature, PDF_NAME(V));
1612
0
  pdf_obj *v_obj = pdf_load_unencrypted_object(ctx, doc, pdf_to_num(ctx, v_ref));
1613
0
  char *copy = NULL;
1614
0
  size_t len;
1615
1616
0
  fz_var(copy);
1617
0
  fz_try(ctx)
1618
0
  {
1619
0
    pdf_obj *c = pdf_dict_get(ctx, v_obj, PDF_NAME(Contents));
1620
0
    char *s;
1621
1622
0
    s = pdf_to_str_buf(ctx, c);
1623
0
    len = pdf_to_str_len(ctx, c);
1624
1625
0
    if (contents)
1626
0
    {
1627
0
      copy = Memento_label(fz_malloc(ctx, len), "sig_contents");
1628
0
      memcpy(copy, s, len);
1629
0
    }
1630
0
  }
1631
0
  fz_always(ctx)
1632
0
    pdf_drop_obj(ctx, v_obj);
1633
0
  fz_catch(ctx)
1634
0
  {
1635
0
    fz_free(ctx, copy);
1636
0
    fz_rethrow(ctx);
1637
0
  }
1638
1639
0
  if (contents)
1640
0
    *contents = copy;
1641
0
  return len;
1642
0
}
1643
1644
static fz_xml_doc *load_xfa(fz_context *ctx, pdf_document *doc)
1645
0
{
1646
0
  pdf_obj *xfa;
1647
0
  fz_buffer *buf = NULL;
1648
0
  fz_buffer *packet = NULL;
1649
0
  int i;
1650
1651
0
  if (doc->xfa)
1652
0
    return doc->xfa; /* Already loaded, and present. */
1653
1654
0
  xfa = pdf_dict_getp(ctx, pdf_trailer(ctx, doc), "Root/AcroForm/XFA");
1655
0
  if (!pdf_is_array(ctx, xfa) && !pdf_is_stream(ctx, xfa))
1656
0
    return NULL; /* No XFA */
1657
1658
0
  fz_var(buf);
1659
0
  fz_var(packet);
1660
1661
0
  fz_try(ctx)
1662
0
  {
1663
0
    if (pdf_is_stream(ctx, xfa))
1664
0
    {
1665
      /* Load entire XFA resource */
1666
0
      buf = pdf_load_stream(ctx, xfa);
1667
0
    }
1668
0
    else
1669
0
    {
1670
      /* Concatenate packets to create entire XFA resource */
1671
0
      buf = fz_new_buffer(ctx, 1024);
1672
0
      for(i = 0; i < pdf_array_len(ctx, xfa); ++i)
1673
0
      {
1674
0
        pdf_obj *ref = pdf_array_get(ctx, xfa, i);
1675
0
        if (pdf_is_stream(ctx, ref))
1676
0
        {
1677
0
          packet = pdf_load_stream(ctx, ref);
1678
0
          fz_append_buffer(ctx, buf, packet);
1679
0
          fz_drop_buffer(ctx, packet);
1680
0
          packet = NULL;
1681
0
        }
1682
0
      }
1683
0
    }
1684
1685
    /* Parse and stow away XFA resource in document */
1686
0
    doc->xfa = fz_parse_xml(ctx, buf, 0);
1687
0
  }
1688
0
  fz_always(ctx)
1689
0
  {
1690
0
    fz_drop_buffer(ctx, packet);
1691
0
    fz_drop_buffer(ctx, buf);
1692
0
  }
1693
0
  fz_catch(ctx)
1694
0
  {
1695
0
    fz_rethrow(ctx);
1696
0
  }
1697
1698
0
  return doc->xfa;
1699
0
}
1700
1701
static fz_xml *
1702
get_xfa_resource(fz_context *ctx, pdf_document *doc, const char *str)
1703
0
{
1704
0
  fz_xml_doc *xfa;
1705
1706
0
  xfa = load_xfa(ctx, doc);
1707
0
  if (!xfa)
1708
0
    return NULL;
1709
1710
0
  return fz_xml_find_down(fz_xml_root(xfa), str);
1711
0
}
1712
1713
static int
1714
find_name_component(char **np, char **sp, char **ep)
1715
0
{
1716
0
  char *n = *np;
1717
0
  char *s, *e;
1718
0
  int idx = 0;
1719
1720
0
  if (*n == '.')
1721
0
    n++;
1722
1723
  /* Find the next name we are looking for. */
1724
0
  s = e = n;
1725
0
  while (*e && *e != '[' && *e != '.')
1726
0
    e++;
1727
1728
  /* So the next name is s..e */
1729
0
  n = e;
1730
0
  if (*n == '[')
1731
0
  {
1732
0
    n++;
1733
0
    while (*n >= '0' && *n <= '9')
1734
0
      idx = idx*10 + *n++ - '0';
1735
0
    while (*n && *n != ']')
1736
0
      n++;
1737
0
    if (*n == ']')
1738
0
      n++;
1739
0
  }
1740
0
  *np = n;
1741
0
  *sp = s;
1742
0
  *ep = e;
1743
1744
0
  return idx;
1745
0
}
1746
1747
static pdf_obj *
1748
annot_from_name(fz_context *ctx, pdf_document *doc, const char *str)
1749
0
{
1750
0
  pdf_obj *fields = pdf_dict_getp(ctx, pdf_trailer(ctx, doc), "Root/AcroForm/Fields");
1751
1752
0
  if (strncmp(str, "xfa[0].", 7) == 0)
1753
0
    str += 7;
1754
0
  if (strncmp(str, "template[0].", 12) == 0)
1755
0
    str += 12;
1756
1757
0
  return pdf_lookup_field(ctx, fields, str);
1758
0
}
1759
1760
static pdf_obj *
1761
get_locked_fields_from_xfa(fz_context *ctx, pdf_document *doc, pdf_obj *field)
1762
0
{
1763
0
  char *name = pdf_load_field_name(ctx, field);
1764
0
  char *n = name;
1765
0
  const char *use;
1766
0
  fz_xml *node;
1767
1768
0
  if (name == NULL)
1769
0
    return NULL;
1770
1771
0
  fz_try(ctx)
1772
0
  {
1773
0
    node = get_xfa_resource(ctx, doc, "template");
1774
1775
0
    do
1776
0
    {
1777
0
      char c, *s, *e;
1778
0
      int idx = 0;
1779
0
      char *key;
1780
1781
0
      idx = find_name_component(&n, &s, &e);
1782
      /* We want the idx'th occurrence of s..e */
1783
1784
      /* Hacky */
1785
0
      c = *e;
1786
0
      *e = 0;
1787
0
      key = *n ? "subform" : "field";
1788
0
      node = fz_xml_find_down_match(node, key, "name", s);
1789
0
      while (node && idx > 0)
1790
0
      {
1791
0
        node = fz_xml_find_next_match(node, key, "name", s);
1792
0
        idx--;
1793
0
      }
1794
0
      *e = c;
1795
0
    }
1796
0
    while (node && *n == '.');
1797
0
  }
1798
0
  fz_always(ctx)
1799
0
    fz_free(ctx, name);
1800
0
  fz_catch(ctx)
1801
0
    fz_rethrow(ctx);
1802
1803
0
  if (node == NULL)
1804
0
    return NULL;
1805
1806
0
  node = fz_xml_find_down(node, "ui");
1807
0
  node = fz_xml_find_down(node, "signature");
1808
0
  node = fz_xml_find_down(node, "manifest");
1809
1810
0
  use = fz_xml_att(node, "use");
1811
0
  if (use == NULL)
1812
0
    return NULL;
1813
0
  if (*use == '#')
1814
0
    use++;
1815
1816
  /* Now look for a variables entry in a subform that defines this. */
1817
0
  while (node)
1818
0
  {
1819
0
    fz_xml *variables, *manifest, *ref;
1820
0
    pdf_obj *arr;
1821
1822
    /* Find the enclosing subform */
1823
0
    do {
1824
0
      node = fz_xml_up(node);
1825
0
    } while (node && strcmp(fz_xml_tag(node), "subform"));
1826
1827
    /* Look for a variables within that. */
1828
0
    variables = fz_xml_find_down(node, "variables");
1829
0
    if (variables == NULL)
1830
0
      continue;
1831
1832
0
    manifest = fz_xml_find_down_match(variables, "manifest", "id", use);
1833
0
    if (manifest == NULL)
1834
0
      continue;
1835
1836
0
    arr = pdf_new_array(ctx, doc, 16);
1837
0
    fz_try(ctx)
1838
0
    {
1839
0
      ref = fz_xml_find_down(manifest, "ref");
1840
0
      while (ref)
1841
0
      {
1842
0
        const char *s = fz_xml_text(fz_xml_down(ref));
1843
0
        pdf_array_push(ctx, arr, annot_from_name(ctx, doc, s));
1844
0
        ref = fz_xml_find_next(ref, "ref");
1845
0
      }
1846
0
    }
1847
0
    fz_catch(ctx)
1848
0
    {
1849
0
      pdf_drop_obj(ctx, arr);
1850
0
      fz_rethrow(ctx);
1851
0
    }
1852
0
    return arr;
1853
0
  }
1854
1855
0
  return NULL;
1856
0
}
1857
1858
static void
1859
lock_field(fz_context *ctx, pdf_obj *f)
1860
0
{
1861
0
  int ff = pdf_dict_get_inheritable_int(ctx, f, PDF_NAME(Ff));
1862
1863
0
  if ((ff & PDF_FIELD_IS_READ_ONLY) ||
1864
0
    !pdf_name_eq(ctx, pdf_dict_get(ctx, f, PDF_NAME(Type)), PDF_NAME(Annot)) ||
1865
0
    !pdf_name_eq(ctx, pdf_dict_get(ctx, f, PDF_NAME(Subtype)), PDF_NAME(Widget)))
1866
0
    return;
1867
1868
0
  pdf_dict_put_int(ctx, f, PDF_NAME(Ff), ff | PDF_FIELD_IS_READ_ONLY);
1869
0
}
1870
1871
static void
1872
lock_xfa_locked_fields(fz_context *ctx, pdf_obj *a)
1873
0
{
1874
0
  int i;
1875
0
  int len = pdf_array_len(ctx, a);
1876
1877
0
  for (i = 0; i < len; i++)
1878
0
  {
1879
0
    lock_field(ctx, pdf_array_get(ctx, a, i));
1880
0
  }
1881
0
}
1882
1883
1884
void pdf_signature_set_value(fz_context *ctx, pdf_document *doc, pdf_obj *field, pdf_pkcs7_signer *signer, int64_t stime)
1885
0
{
1886
0
  pdf_obj *v = NULL;
1887
0
  pdf_obj *o = NULL;
1888
0
  pdf_obj *r = NULL;
1889
0
  pdf_obj *t = NULL;
1890
0
  pdf_obj *a = NULL;
1891
0
  pdf_obj *b = NULL;
1892
0
  pdf_obj *l = NULL;
1893
0
  pdf_obj *indv;
1894
0
  int vnum;
1895
0
  size_t max_digest_size;
1896
0
  char *buf = NULL;
1897
1898
0
  vnum = pdf_create_object(ctx, doc);
1899
0
  indv = pdf_new_indirect(ctx, doc, vnum, 0);
1900
0
  pdf_dict_put_drop(ctx, field, PDF_NAME(V), indv);
1901
1902
0
  max_digest_size = signer->max_digest_size(ctx, signer);
1903
1904
0
  fz_var(v);
1905
0
  fz_var(o);
1906
0
  fz_var(r);
1907
0
  fz_var(t);
1908
0
  fz_var(a);
1909
0
  fz_var(b);
1910
0
  fz_var(l);
1911
0
  fz_var(buf);
1912
0
  fz_try(ctx)
1913
0
  {
1914
0
    v = pdf_new_dict(ctx, doc, 4);
1915
0
    pdf_update_object(ctx, doc, vnum, v);
1916
1917
0
    buf = fz_calloc(ctx, max_digest_size, 1);
1918
1919
    /* Ensure that the /Filter entry is the first entry in the
1920
       dictionary after the digest contents since we look for
1921
       this tag when completing signatures in pdf-write.c in order
1922
       to generate the correct byte range. */
1923
0
    pdf_dict_put_array(ctx, v, PDF_NAME(ByteRange), 4);
1924
0
    pdf_dict_put_string(ctx, v, PDF_NAME(Contents), buf, max_digest_size);
1925
0
    pdf_dict_put(ctx, v, PDF_NAME(Filter), PDF_NAME(Adobe_PPKLite));
1926
0
    pdf_dict_put(ctx, v, PDF_NAME(SubFilter), PDF_NAME(adbe_pkcs7_detached));
1927
0
    pdf_dict_put(ctx, v, PDF_NAME(Type), PDF_NAME(Sig));
1928
0
    pdf_dict_put_date(ctx, v, PDF_NAME(M), stime);
1929
1930
0
    o = pdf_dict_put_array(ctx, v, PDF_NAME(Reference), 1);
1931
0
    r = pdf_array_put_dict(ctx, o, 0, 4);
1932
0
    pdf_dict_put(ctx, r, PDF_NAME(Data), pdf_dict_get(ctx, pdf_trailer(ctx, doc), PDF_NAME(Root)));
1933
0
    pdf_dict_put(ctx, r, PDF_NAME(TransformMethod), PDF_NAME(FieldMDP));
1934
0
    pdf_dict_put(ctx, r, PDF_NAME(Type), PDF_NAME(SigRef));
1935
0
    t = pdf_dict_put_dict(ctx, r, PDF_NAME(TransformParams), 5);
1936
1937
0
    l = pdf_dict_getp(ctx, field, "Lock/Action");
1938
0
    if (l)
1939
0
    {
1940
0
      a = pdf_dict_getp(ctx, field, "Lock/Fields");
1941
0
    }
1942
0
    else
1943
0
    {
1944
      /* Lock action wasn't specified so we need to encode an Include.
1945
       * Before we just use an empty array, check in the XFA for locking
1946
       * details. */
1947
0
      a = get_locked_fields_from_xfa(ctx, doc, field);
1948
0
      if (a)
1949
0
        lock_xfa_locked_fields(ctx, a);
1950
1951
      /* If we don't get a result from the XFA, just encode an empty array
1952
       * (leave a == NULL), even if Lock/Fields exists because we don't really
1953
       * know what to do with the information if the action isn't defined. */
1954
0
      l = PDF_NAME(Include);
1955
0
    }
1956
1957
0
    pdf_dict_put(ctx, t, PDF_NAME(Action), l);
1958
1959
0
    if (pdf_name_eq(ctx, l, PDF_NAME(Include)) || pdf_name_eq(ctx, l, PDF_NAME(Exclude)))
1960
0
    {
1961
      /* For action Include and Exclude, we need to encode a Fields array */
1962
0
      if (!a)
1963
0
      {
1964
        /* If one wasn't defined or we chose to ignore it because no action
1965
         * was defined then use an empty one. */
1966
0
        b = pdf_new_array(ctx, doc, 0);
1967
0
        a = b;
1968
0
      }
1969
1970
0
      pdf_dict_put_drop(ctx, t, PDF_NAME(Fields), pdf_copy_array(ctx, a));
1971
0
    }
1972
1973
0
    pdf_dict_put(ctx, t, PDF_NAME(Type), PDF_NAME(TransformParams));
1974
0
    pdf_dict_put(ctx, t, PDF_NAME(V), PDF_NAME(1_2));
1975
1976
    /* Record details within the document structure so that contents
1977
    * and byte_range can be updated with their correct values at
1978
    * saving time */
1979
0
    pdf_xref_store_unsaved_signature(ctx, doc, field, signer);
1980
0
  }
1981
0
  fz_always(ctx)
1982
0
  {
1983
0
    pdf_drop_obj(ctx, v);
1984
0
    pdf_drop_obj(ctx, b);
1985
0
    fz_free(ctx, buf);
1986
0
  }
1987
0
  fz_catch(ctx)
1988
0
  {
1989
0
    fz_rethrow(ctx);
1990
0
  }
1991
0
}
1992
1993
void pdf_set_widget_editing_state(fz_context *ctx, pdf_annot *widget, int editing)
1994
0
{
1995
0
  widget->ignore_trigger_events = editing;
1996
0
}
1997
1998
int pdf_get_widget_editing_state(fz_context *ctx, pdf_annot *widget)
1999
0
{
2000
0
  return widget->ignore_trigger_events;
2001
0
}
2002
2003
static void pdf_execute_js_action(fz_context *ctx, pdf_document *doc, pdf_obj *target, const char *path, pdf_obj *js)
2004
0
{
2005
0
  if (js)
2006
0
  {
2007
0
    char *code = pdf_load_stream_or_string_as_utf8(ctx, js);
2008
0
    int in_op = 0;
2009
2010
0
    fz_var(in_op);
2011
0
    fz_try(ctx)
2012
0
    {
2013
0
      char buf[100];
2014
0
      fz_snprintf(buf, sizeof buf, "%d/%s", pdf_to_num(ctx, target), path);
2015
0
      pdf_begin_operation(ctx, doc, "Javascript Event");
2016
0
      in_op = 1;
2017
0
      pdf_js_execute(doc->js, buf, code, NULL);
2018
0
      pdf_end_operation(ctx, doc);
2019
0
    }
2020
0
    fz_always(ctx)
2021
0
    {
2022
0
      fz_free(ctx, code);
2023
0
    }
2024
0
    fz_catch(ctx)
2025
0
    {
2026
0
      if (in_op)
2027
0
        pdf_abandon_operation(ctx, doc);
2028
0
      fz_rethrow(ctx);
2029
0
    }
2030
0
  }
2031
0
}
2032
2033
static void pdf_execute_action_imp(fz_context *ctx, pdf_document *doc, pdf_obj *target, const char *path, pdf_obj *action)
2034
0
{
2035
0
  pdf_obj *S = pdf_dict_get(ctx, action, PDF_NAME(S));
2036
0
  if (pdf_name_eq(ctx, S, PDF_NAME(JavaScript)))
2037
0
  {
2038
0
    if (doc->js)
2039
0
      pdf_execute_js_action(ctx, doc, target, path, pdf_dict_get(ctx, action, PDF_NAME(JS)));
2040
0
  }
2041
0
  if (pdf_name_eq(ctx, S, PDF_NAME(ResetForm)))
2042
0
  {
2043
0
    pdf_obj *fields = pdf_dict_get(ctx, action, PDF_NAME(Fields));
2044
0
    int flags = pdf_dict_get_int(ctx, action, PDF_NAME(Flags));
2045
0
    pdf_reset_form(ctx, doc, fields, flags & 1);
2046
0
  }
2047
0
}
2048
2049
static void pdf_execute_action_chain(fz_context *ctx, pdf_document *doc, pdf_obj *target, const char *path, pdf_obj *action, pdf_cycle_list *cycle_up)
2050
0
{
2051
0
  pdf_cycle_list cycle;
2052
0
  pdf_obj *next;
2053
2054
0
  if (pdf_cycle(ctx, &cycle, cycle_up, action))
2055
0
    fz_throw(ctx, FZ_ERROR_FORMAT, "cycle in action chain");
2056
2057
0
  if (pdf_is_array(ctx, action))
2058
0
  {
2059
0
    int i, n = pdf_array_len(ctx, action);
2060
0
    for (i = 0; i < n; ++i)
2061
0
      pdf_execute_action_chain(ctx, doc, target, path, pdf_array_get(ctx, action, i), &cycle);
2062
0
  }
2063
0
  else
2064
0
  {
2065
0
    pdf_execute_action_imp(ctx, doc, target, path, action);
2066
0
    next = pdf_dict_get(ctx, action, PDF_NAME(Next));
2067
0
    if (next)
2068
0
      pdf_execute_action_chain(ctx, doc, target, path, next, &cycle);
2069
0
  }
2070
0
}
2071
2072
static void pdf_execute_action(fz_context *ctx, pdf_document *doc, pdf_obj *target, const char *path)
2073
0
{
2074
0
  pdf_obj *action = pdf_dict_getp_inheritable(ctx, target, path);
2075
0
  if (action)
2076
0
    pdf_execute_action_chain(ctx, doc, target, path, action, NULL);
2077
0
}
2078
2079
void pdf_document_event_will_close(fz_context *ctx, pdf_document *doc)
2080
0
{
2081
0
  pdf_execute_action(ctx, doc, pdf_trailer(ctx, doc), "Root/AA/WC");
2082
0
}
2083
2084
void pdf_document_event_will_save(fz_context *ctx, pdf_document *doc)
2085
0
{
2086
0
  pdf_execute_action(ctx, doc, pdf_trailer(ctx, doc), "Root/AA/WS");
2087
0
}
2088
2089
void pdf_document_event_did_save(fz_context *ctx, pdf_document *doc)
2090
0
{
2091
0
  pdf_execute_action(ctx, doc, pdf_trailer(ctx, doc), "Root/AA/DS");
2092
0
}
2093
2094
void pdf_document_event_will_print(fz_context *ctx, pdf_document *doc)
2095
0
{
2096
0
  pdf_execute_action(ctx, doc, pdf_trailer(ctx, doc), "Root/AA/WP");
2097
0
}
2098
2099
void pdf_document_event_did_print(fz_context *ctx, pdf_document *doc)
2100
0
{
2101
0
  pdf_execute_action(ctx, doc, pdf_trailer(ctx, doc), "Root/AA/DP");
2102
0
}
2103
2104
void pdf_page_event_open(fz_context *ctx, pdf_page *page)
2105
0
{
2106
0
  pdf_execute_action(ctx, page->doc, page->obj, "AA/O");
2107
0
}
2108
2109
void pdf_page_event_close(fz_context *ctx, pdf_page *page)
2110
0
{
2111
0
  pdf_execute_action(ctx, page->doc, page->obj, "AA/C");
2112
0
}
2113
2114
static void
2115
annot_execute_action(fz_context *ctx, pdf_annot *annot, const char *act)
2116
0
{
2117
0
  begin_annot_op(ctx, annot, "JavaScript action");
2118
2119
0
  fz_try(ctx)
2120
0
  {
2121
0
    pdf_execute_action(ctx, annot->page->doc, annot->obj, act);
2122
0
    end_annot_op(ctx, annot);
2123
0
  }
2124
0
  fz_catch(ctx)
2125
0
  {
2126
0
    abandon_annot_op(ctx, annot);
2127
0
    fz_rethrow(ctx);
2128
0
  }
2129
0
}
2130
2131
void pdf_annot_event_enter(fz_context *ctx, pdf_annot *annot)
2132
0
{
2133
0
  annot_execute_action(ctx, annot, "AA/E");
2134
0
}
2135
2136
void pdf_annot_event_exit(fz_context *ctx, pdf_annot *annot)
2137
0
{
2138
0
  annot_execute_action(ctx, annot, "AA/X");
2139
0
}
2140
2141
void pdf_annot_event_down(fz_context *ctx, pdf_annot *annot)
2142
0
{
2143
0
  annot_execute_action(ctx, annot, "AA/D");
2144
0
}
2145
2146
void pdf_annot_event_up(fz_context *ctx, pdf_annot *annot)
2147
0
{
2148
0
  pdf_obj *action;
2149
2150
0
  begin_annot_op(ctx, annot, "JavaScript action");
2151
2152
0
  fz_try(ctx)
2153
0
  {
2154
0
    action = pdf_dict_get(ctx, annot->obj, PDF_NAME(A));
2155
0
    if (action)
2156
0
      pdf_execute_action_chain(ctx, annot->page->doc, annot->obj, "A", action, NULL);
2157
0
    else
2158
0
      pdf_execute_action(ctx, annot->page->doc, annot->obj, "AA/U");
2159
0
    end_annot_op(ctx, annot);
2160
0
  }
2161
0
  fz_catch(ctx)
2162
0
  {
2163
0
    abandon_annot_op(ctx, annot);
2164
0
    fz_rethrow(ctx);
2165
0
  }
2166
0
}
2167
2168
void pdf_annot_event_focus(fz_context *ctx, pdf_annot *annot)
2169
0
{
2170
0
  annot_execute_action(ctx, annot, "AA/Fo");
2171
0
}
2172
2173
void pdf_annot_event_blur(fz_context *ctx, pdf_annot *annot)
2174
0
{
2175
0
  annot_execute_action(ctx, annot, "AA/Bl");
2176
0
}
2177
2178
void pdf_annot_event_page_open(fz_context *ctx, pdf_annot *annot)
2179
0
{
2180
0
  annot_execute_action(ctx, annot, "AA/PO");
2181
0
}
2182
2183
void pdf_annot_event_page_close(fz_context *ctx, pdf_annot *annot)
2184
0
{
2185
0
  annot_execute_action(ctx, annot, "AA/PC");
2186
0
}
2187
2188
void pdf_annot_event_page_visible(fz_context *ctx, pdf_annot *annot)
2189
0
{
2190
0
  annot_execute_action(ctx, annot, "AA/PV");
2191
0
}
2192
2193
void pdf_annot_event_page_invisible(fz_context *ctx, pdf_annot *annot)
2194
0
{
2195
0
  annot_execute_action(ctx, annot, "AA/PI");
2196
0
}
2197
2198
int pdf_field_event_keystroke(fz_context *ctx, pdf_document *doc, pdf_obj *field, pdf_keystroke_event *evt)
2199
0
{
2200
0
  pdf_js *js = doc->js;
2201
0
  if (js)
2202
0
  {
2203
0
    pdf_obj *action = pdf_dict_getp_inheritable(ctx, field, "AA/K/JS");
2204
0
    if (action)
2205
0
    {
2206
0
      pdf_js_event_init_keystroke(js, field, evt);
2207
0
      pdf_execute_js_action(ctx, doc, field, "AA/K/JS", action);
2208
0
      return pdf_js_event_result_keystroke(js, evt);
2209
0
    }
2210
0
  }
2211
0
  evt->newChange = fz_strdup(ctx, evt->change);
2212
0
  evt->newValue = fz_strdup(ctx, evt->value);
2213
0
  return 1;
2214
0
}
2215
2216
int pdf_annot_field_event_keystroke(fz_context *ctx, pdf_document *doc, pdf_annot *annot, pdf_keystroke_event *evt)
2217
0
{
2218
0
  int ret;
2219
2220
0
  pdf_annot_push_local_xref(ctx, annot);
2221
2222
0
  fz_try(ctx)
2223
0
    ret = pdf_field_event_keystroke(ctx, doc, annot->obj, evt);
2224
0
  fz_always(ctx)
2225
0
    pdf_annot_pop_local_xref(ctx, annot);
2226
0
  fz_catch(ctx)
2227
0
    fz_rethrow(ctx);
2228
2229
0
  return ret;
2230
0
}
2231
2232
char *pdf_field_event_format(fz_context *ctx, pdf_document *doc, pdf_obj *field)
2233
2.11k
{
2234
2.11k
  pdf_js *js = doc->js;
2235
2.11k
  if (js)
2236
0
  {
2237
0
    pdf_obj *action = pdf_dict_getp_inheritable(ctx, field, "AA/F/JS");
2238
0
    if (action)
2239
0
    {
2240
0
      const char *value = pdf_field_value(ctx, field);
2241
0
      pdf_js_event_init(js, field, value, 1);
2242
0
      pdf_execute_js_action(ctx, doc, field, "AA/F/JS", action);
2243
0
      return pdf_js_event_value(js);
2244
0
    }
2245
0
  }
2246
2.11k
  return NULL;
2247
2.11k
}
2248
2249
int pdf_field_event_validate(fz_context *ctx, pdf_document *doc, pdf_obj *field, const char *value, char **newvalue)
2250
0
{
2251
0
  pdf_js *js = doc->js;
2252
2253
0
  *newvalue = NULL;
2254
0
  if (js)
2255
0
  {
2256
0
    pdf_obj *action = pdf_dict_getp_inheritable(ctx, field, "AA/V/JS");
2257
0
    if (action)
2258
0
    {
2259
0
      pdf_js_event_init(js, field, value, 1);
2260
0
      pdf_execute_js_action(ctx, doc, field, "AA/V/JS", action);
2261
0
      return pdf_js_event_result_validate(js, newvalue);
2262
0
    }
2263
0
  }
2264
0
  return 1;
2265
0
}
2266
2267
void pdf_field_event_calculate(fz_context *ctx, pdf_document *doc, pdf_obj *field)
2268
0
{
2269
0
  pdf_js *js = doc->js;
2270
0
  if (js)
2271
0
  {
2272
0
    pdf_obj *action = pdf_dict_getp_inheritable(ctx, field, "AA/C/JS");
2273
0
    if (action)
2274
0
    {
2275
0
      char *old_value = fz_strdup(ctx, pdf_field_value(ctx, field));
2276
0
      char *new_value = NULL;
2277
0
      fz_var(new_value);
2278
0
      fz_try(ctx)
2279
0
      {
2280
0
        pdf_js_event_init(js, field, old_value, 1);
2281
0
        pdf_execute_js_action(ctx, doc, field, "AA/C/JS", action);
2282
0
        if (pdf_js_event_result(js))
2283
0
        {
2284
0
          new_value = pdf_js_event_value(js);
2285
0
          if (strcmp(old_value, new_value))
2286
0
            pdf_set_field_value(ctx, doc, field, new_value, 0);
2287
0
        }
2288
0
      }
2289
0
      fz_always(ctx)
2290
0
      {
2291
0
        fz_free(ctx, old_value);
2292
0
        fz_free(ctx, new_value);
2293
0
      }
2294
0
      fz_catch(ctx)
2295
0
        fz_rethrow(ctx);
2296
0
    }
2297
0
  }
2298
0
}
2299
2300
static void
2301
count_sigs(fz_context *ctx, pdf_obj *field, void *arg, pdf_obj **ft)
2302
0
{
2303
0
  int *n = (int *)arg;
2304
2305
0
  if (!pdf_name_eq(ctx, pdf_dict_get(ctx, field, PDF_NAME(Type)), PDF_NAME(Annot)) ||
2306
0
    !pdf_name_eq(ctx, pdf_dict_get(ctx, field, PDF_NAME(Subtype)), PDF_NAME(Widget)) ||
2307
0
    !pdf_name_eq(ctx, *ft, PDF_NAME(Sig)))
2308
0
    return;
2309
2310
0
  (*n)++;
2311
0
}
2312
2313
static pdf_obj *ft_name[2] = { PDF_NAME(FT), NULL };
2314
2315
int pdf_count_signatures(fz_context *ctx, pdf_document *doc)
2316
0
{
2317
0
  int n = 0;
2318
0
  pdf_obj *ft = NULL;
2319
0
  pdf_obj *form_fields = pdf_dict_getp(ctx, pdf_trailer(ctx, doc), "Root/AcroForm/Fields");
2320
0
  pdf_walk_tree(ctx, form_fields, PDF_NAME(Kids), count_sigs, NULL, &n, ft_name, &ft);
2321
0
  return n;
2322
0
}
2323
2324
/*
2325
 * Bake interactive form fields into static content.
2326
 */
2327
2328
static pdf_obj *get_annot_ap(fz_context *ctx, pdf_obj *annot)
2329
0
{
2330
0
  pdf_obj *ap = pdf_dict_get(ctx, annot, PDF_NAME(AP));
2331
0
  pdf_obj *as = pdf_dict_get(ctx, annot, PDF_NAME(AS));
2332
0
  if (ap)
2333
0
  {
2334
0
    ap = pdf_dict_get(ctx, ap, PDF_NAME(N));
2335
0
    if (pdf_is_stream(ctx, ap))
2336
0
      return ap;
2337
0
    ap = pdf_dict_get(ctx, ap, as);
2338
0
    if (pdf_is_stream(ctx, ap))
2339
0
      return ap;
2340
0
  }
2341
0
  return NULL;
2342
0
}
2343
2344
static fz_matrix get_annot_transform(fz_context *ctx, pdf_obj *annot, pdf_obj *ap)
2345
0
{
2346
0
  float w, h, x, y;
2347
0
  fz_matrix transform;
2348
0
  fz_rect bbox;
2349
0
  fz_rect rect;
2350
2351
0
  rect = pdf_dict_get_rect(ctx, annot, PDF_NAME(Rect));
2352
0
  bbox = pdf_dict_get_rect(ctx, ap, PDF_NAME(BBox));
2353
0
  transform = pdf_dict_get_matrix(ctx, ap, PDF_NAME(Matrix));
2354
2355
0
  bbox = fz_transform_rect(bbox, transform);
2356
0
  w = (rect.x1 - rect.x0) / (bbox.x1 - bbox.x0);
2357
0
  h = (rect.y1 - rect.y0) / (bbox.y1 - bbox.y0);
2358
0
  x = rect.x0 - bbox.x0 * w;
2359
0
  y = rect.y0 - bbox.y0 * h;
2360
2361
0
  return fz_make_matrix(w, 0, 0, h, x, y);
2362
0
}
2363
2364
static void pdf_bake_annot(fz_context *ctx, fz_buffer *buf, pdf_document *doc, pdf_obj *page, pdf_obj *res_xobj, pdf_obj *annot)
2365
0
{
2366
0
  fz_matrix m;
2367
0
  pdf_obj *ap;
2368
0
  char name[20];
2369
2370
0
  ap = get_annot_ap(ctx, annot);
2371
0
  if (ap)
2372
0
  {
2373
0
    fz_snprintf(name, sizeof name, "Annot%d", pdf_to_num(ctx, annot));
2374
0
    pdf_dict_puts(ctx, res_xobj, name, ap);
2375
0
    pdf_dict_put(ctx, ap, PDF_NAME(Type), PDF_NAME(XObject));
2376
0
    pdf_dict_put(ctx, ap, PDF_NAME(Subtype), PDF_NAME(Form));
2377
0
    m = get_annot_transform(ctx, annot, ap);
2378
0
    fz_append_printf(ctx, buf,
2379
0
      "q\n%g %g %g %g %g %g cm\n/%s Do\nQ\n",
2380
0
      m.a, m.b, m.c, m.d, m.e, m.f,
2381
0
      name
2382
0
    );
2383
0
  }
2384
0
}
2385
2386
static void pdf_bake_page(fz_context *ctx, pdf_document *doc, pdf_obj *page, int bake_annots, int bake_widgets)
2387
0
{
2388
0
  pdf_obj *res;
2389
0
  pdf_obj *res_xobj;
2390
0
  pdf_obj *contents;
2391
0
  pdf_obj *new_contents = NULL;
2392
0
  pdf_obj *annots;
2393
0
  pdf_obj *annot;
2394
0
  pdf_obj *subtype;
2395
0
  pdf_obj *prologue = NULL;
2396
0
  fz_buffer *buf = NULL;
2397
0
  int prepend, append;
2398
0
  int i;
2399
2400
0
  fz_var(buf);
2401
0
  fz_var(prologue);
2402
0
  fz_var(new_contents);
2403
2404
0
  annots = pdf_dict_get(ctx, page, PDF_NAME(Annots));
2405
0
  if (pdf_array_len(ctx, annots) == 0)
2406
0
    return;
2407
2408
0
  res = pdf_dict_get(ctx, page, PDF_NAME(Resources));
2409
0
  if (!res)
2410
0
    res = pdf_dict_put_dict(ctx, page, PDF_NAME(Resources), 4);
2411
2412
0
  res_xobj = pdf_dict_get(ctx, res, PDF_NAME(XObject));
2413
0
  if (!res_xobj)
2414
0
    res_xobj = pdf_dict_put_dict(ctx, res, PDF_NAME(XObject), 8);
2415
2416
0
  fz_try(ctx)
2417
0
  {
2418
    // Ensure that the graphics state is balanced.
2419
0
    contents = pdf_dict_get(ctx, page, PDF_NAME(Contents));
2420
0
    pdf_count_q_balance(ctx, doc, res, contents, &prepend, &append);
2421
2422
0
    if (prepend)
2423
0
    {
2424
      // Prepend enough 'q' to ensure we can get back to initial state.
2425
0
      buf = fz_new_buffer(ctx, 1024);
2426
0
      while (prepend-- > 0)
2427
0
        fz_append_string(ctx, buf, "q\n");
2428
2429
0
      prologue = pdf_add_stream(ctx, doc, buf, NULL, 0);
2430
0
      fz_drop_buffer(ctx, buf);
2431
0
      buf = NULL;
2432
0
    }
2433
2434
    // Append enough 'Q' to get back to initial state.
2435
0
    buf = fz_new_buffer(ctx, 1024);
2436
0
    while (append-- > 0)
2437
0
      fz_append_string(ctx, buf, "Q\n");
2438
2439
0
    for (i = 0; i < pdf_array_len(ctx, annots); )
2440
0
    {
2441
0
      annot = pdf_array_get(ctx, annots, i);
2442
0
      subtype = pdf_dict_get(ctx, annot, PDF_NAME(Subtype));
2443
0
      if (subtype == PDF_NAME(Link))
2444
0
      {
2445
0
        ++i;
2446
0
      }
2447
0
      else if (subtype == PDF_NAME(Widget))
2448
0
      {
2449
0
        if (bake_widgets)
2450
0
        {
2451
0
          pdf_bake_annot(ctx, buf, doc, page, res_xobj, annot);
2452
0
          pdf_array_delete(ctx, annots, i);
2453
0
        }
2454
0
        else
2455
0
        {
2456
0
          ++i;
2457
0
        }
2458
0
      }
2459
0
      else
2460
0
      {
2461
0
        if (bake_annots)
2462
0
        {
2463
0
          pdf_bake_annot(ctx, buf, doc, page, res_xobj, annot);
2464
0
          pdf_array_delete(ctx, annots, i);
2465
0
        }
2466
0
        else
2467
0
        {
2468
0
          ++i;
2469
0
        }
2470
0
      }
2471
0
    }
2472
2473
0
    if (!pdf_is_array(ctx, contents))
2474
0
    {
2475
0
      new_contents = pdf_new_array(ctx, doc, 10);
2476
0
      if (prologue)
2477
0
        pdf_array_push(ctx, new_contents, prologue);
2478
0
      if (contents)
2479
0
        pdf_array_push(ctx, new_contents, contents);
2480
0
      pdf_dict_put(ctx, page, PDF_NAME(Contents), new_contents);
2481
0
      pdf_drop_obj(ctx, new_contents);
2482
0
      contents = new_contents;
2483
0
      new_contents = NULL;
2484
0
    }
2485
0
    else if (prologue)
2486
0
    {
2487
0
      pdf_array_insert(ctx, contents, prologue, 0);
2488
0
    }
2489
2490
0
    pdf_array_push_drop(ctx, contents, pdf_add_stream(ctx, doc, buf, NULL, 0));
2491
0
  }
2492
0
  fz_always(ctx)
2493
0
  {
2494
0
    fz_drop_buffer(ctx, buf);
2495
0
    pdf_drop_obj(ctx, prologue);
2496
0
    pdf_drop_obj(ctx, new_contents);
2497
0
  }
2498
0
  fz_catch(ctx)
2499
0
  {
2500
0
    fz_rethrow(ctx);
2501
0
  }
2502
0
}
2503
2504
void pdf_bake_document(fz_context *ctx, pdf_document *doc, int bake_annots, int bake_widgets)
2505
0
{
2506
0
  pdf_page *page = NULL;
2507
0
  pdf_annot *annot;
2508
0
  int i, n;
2509
2510
0
  fz_var(page);
2511
2512
0
  pdf_begin_operation(ctx, doc, "Bake interactive content");
2513
0
  fz_try(ctx)
2514
0
  {
2515
0
    n = pdf_count_pages(ctx, doc);
2516
0
    for (i = 0; i < n; ++i)
2517
0
    {
2518
0
      page = pdf_load_page(ctx, doc, i);
2519
2520
0
      if (bake_annots)
2521
0
        for (annot = pdf_first_annot(ctx, page); annot; annot = pdf_next_annot(ctx, annot))
2522
0
          pdf_annot_request_synthesis(ctx, annot);
2523
0
      if (bake_widgets)
2524
0
        for (annot = pdf_first_widget(ctx, page); annot; annot = pdf_next_widget(ctx, annot))
2525
0
          pdf_annot_request_synthesis(ctx, annot);
2526
0
      pdf_update_page(ctx, page);
2527
2528
0
      pdf_bake_page(ctx, doc, page->obj, bake_annots, bake_widgets);
2529
2530
0
      fz_drop_page(ctx, (fz_page*)page);
2531
0
      page = NULL;
2532
0
    }
2533
2534
0
    if (bake_widgets)
2535
0
    {
2536
0
      pdf_obj *trailer = pdf_trailer(ctx, doc);
2537
0
      pdf_obj *root = pdf_dict_get(ctx, trailer, PDF_NAME(Root));
2538
0
      pdf_dict_del(ctx, root, PDF_NAME(AcroForm));
2539
0
    }
2540
0
    pdf_end_operation(ctx, doc);
2541
0
  }
2542
0
  fz_always(ctx)
2543
0
  {
2544
0
    fz_drop_page(ctx, (fz_page*)page);
2545
0
  }
2546
0
  fz_catch(ctx)
2547
0
  {
2548
0
    pdf_abandon_operation(ctx, doc);
2549
0
  }
2550
0
}