Coverage Report

Created: 2026-11-12 07:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ghostpdl/devices/vector/gdevpdfm.c
Line
Count
Source
1
/* Copyright (C) 2001-2026 Artifex Software, Inc.
2
   All Rights Reserved.
3
4
   This software is provided AS-IS with no warranty, either express or
5
   implied.
6
7
   This software is distributed under license and may not be copied,
8
   modified or distributed except as expressly authorized under the terms
9
   of the license contained in the file LICENSE in this distribution.
10
11
   Refer to licensing information at http://www.artifex.com or contact
12
   Artifex Software, Inc.,  39 Mesa Street, Suite 108A, San Francisco,
13
   CA 94129, USA, for further information.
14
*/
15
16
17
/* pdfmark processing for PDF-writing driver */
18
#include "math_.h"
19
#include "memory_.h"
20
#include "gx.h"
21
#include "gserrors.h"
22
#include "gsutil.h"   /* for bytes_compare */
23
#include "gdevpdfx.h"
24
#include "gdevpdfo.h"
25
#include "szlibx.h"
26
#include "slzwx.h"
27
#include "sbrotlix.h"
28
29
/* GC descriptors */
30
private_st_pdf_article();
31
32
/*
33
 * The pdfmark pseudo-parameter indicates the occurrence of a pdfmark
34
 * operator in the input file.  Its "value" is the arguments of the operator,
35
 * passed through essentially unchanged:
36
 *      (key, value)*, CTM, type
37
 */
38
39
/*
40
 * Define an entry in a table of pdfmark-processing procedures.
41
 * (The actual table is at the end of this file, to avoid the need for
42
 * forward declarations for the procedures.)
43
 */
44
37.5k
#define PDFMARK_NAMEABLE 1  /* allows _objdef */
45
37.5k
#define PDFMARK_ODD_OK 2  /* OK if odd # of parameters */
46
29.1k
#define PDFMARK_KEEP_NAME 4  /* don't substitute reference for name */
47
                                /* in 1st argument */
48
37.5k
#define PDFMARK_NO_REFS 8  /* don't substitute references for names */
49
                                /* anywhere */
50
37.5k
#define PDFMARK_TRUECTM 16  /* pass the true CTM to the procedure, */
51
                                /* not the one transformed to reflect the default user space */
52
typedef struct pdfmark_name_s {
53
    const char *mname;
54
    pdfmark_proc((*proc));
55
    byte options;
56
} pdfmark_name;
57
58
/* ---------------- Public utilities ---------------- */
59
60
/* Compare a C string and a gs_param_string. */
61
bool
62
pdf_key_eq(const gs_param_string * pcs, const char *str)
63
1.56M
{
64
1.56M
    return (strlen(str) == pcs->size && pcs->data &&
65
343k
            !strncmp(str, (const char *)pcs->data, pcs->size));
66
1.56M
}
67
68
/* Scan an integer out of a parameter string. */
69
int
70
pdfmark_scan_int(const gs_param_string * pstr, int *pvalue)
71
1.20k
{
72
1.20k
#define MAX_INT_STR 20
73
1.20k
    uint size = pstr->size;
74
1.20k
    char str[MAX_INT_STR + 1];
75
76
1.20k
    if (size > MAX_INT_STR)
77
0
        return_error(gs_error_limitcheck);
78
1.20k
    memcpy(str, pstr->data, size);
79
1.20k
    str[size] = 0;
80
1.20k
    return (sscanf(str, "%d", pvalue) == 1 ? 0 :
81
1.20k
            gs_note_error(gs_error_rangecheck));
82
1.20k
#undef MAX_INT_STR
83
1.20k
}
84
85
/* ---------------- Private utilities ---------------- */
86
87
/* Find a key in a dictionary. */
88
static bool
89
pdfmark_find_key(const char *key, const gs_param_string * pairs, uint count,
90
                 gs_param_string * pstr)
91
3.98k
{
92
3.98k
    uint i;
93
94
19.3k
    for (i = 0; i < count; i += 2)
95
17.3k
        if (pdf_key_eq(&pairs[i], key)) {
96
1.94k
            *pstr = pairs[i + 1];
97
1.94k
            return true;
98
1.94k
        }
99
2.03k
    pstr->data = 0;
100
2.03k
    pstr->size = 0;
101
2.03k
    return false;
102
3.98k
}
103
104
/*
105
 * Get the page number for a page referenced by number or as /Next or /Prev.
106
 * The result may be 0 if the page number is 0 or invalid.
107
 */
108
static int
109
pdfmark_page_number(gx_device_pdf * pdev, const gs_param_string * pnstr)
110
957
{
111
957
    int page = pdev->next_page + 1;
112
113
957
    if (pnstr->data == 0);
114
957
    else if (pdf_key_eq(pnstr, "/Next"))
115
0
        ++page;
116
957
    else if (pdf_key_eq(pnstr, "/Prev"))
117
0
        --page;
118
957
    else if (pdfmark_scan_int(pnstr, &page) < 0)
119
0
        page = 0;
120
957
    return page;
121
957
}
122
123
/*
124
 * This routine checks the page number is inside the valid range FirstPage->LastPage
125
 * and if it is, and FirstPage is not 0, it updates the page number by rebasing it to
126
 * the new FirstPage. If all conditions are met we also update the 'max_referred_page'
127
 * which we check during closedown, and emit a warning if a reference should somehow
128
 * point outside the valid range of pages in the document. This can happen if we reference
129
 * a destination page that we haven't created yet, and when we get to the end of the
130
 * job we discover we never did create it.
131
 */
132
static int
133
update_max_page_reference(gx_device_pdf * pdev, int *page)
134
957
{
135
957
    if (*page < pdev->FirstPage || (pdev->LastPage != 0 && *page > pdev->LastPage)) {
136
0
        emprintf1(pdev->memory, "Destination page %d lies outside the valid page range.\n", *page);
137
0
        return -1;
138
0
    }
139
957
    else {
140
957
        if (pdev->FirstPage != 0)
141
0
            *page = (*page - pdev->FirstPage) + 1;
142
143
957
        if (pdev->max_referred_page < *page)
144
362
            pdev->max_referred_page = *page;
145
957
    }
146
957
    return 0;
147
957
}
148
149
/* Construct a destination string specified by /Page and/or /View. */
150
/* Return 0 if none (but still fill in a default), 1 or 2 if present */
151
/* (1 if only one of /Page or /View, 2 if both), <0 if error. */
152
static int
153
pdfmark_make_dest(char dstr[MAX_DEST_STRING], gx_device_pdf * pdev,
154
                  const char *Page_key, const char *View_key,
155
                  const gs_param_string * pairs, uint count, uint RequirePage)
156
1.49k
{
157
1.49k
    gs_param_string page_string, view_string;
158
1.49k
    int present =
159
1.49k
        pdfmark_find_key(Page_key, pairs, count, &page_string) +
160
1.49k
        pdfmark_find_key(View_key, pairs, count, &view_string);
161
1.49k
    int page=0;
162
1.49k
    gs_param_string action;
163
1.49k
    int len, code = 0;
164
165
1.49k
    if (present || RequirePage)
166
957
        page = pdfmark_page_number(pdev, &page_string);
167
168
1.49k
    if (view_string.size == 0)
169
541
        param_string_from_string(view_string, "[/XYZ null null null]");
170
1.49k
    if (page == 0)
171
541
        strcpy(dstr, "[null ");
172
957
    else if (pdfmark_find_key("/Action", pairs, count, &action) &&
173
0
             pdf_key_eq(&action, "/GoToR")
174
957
        )
175
0
        gs_snprintf(dstr, MAX_DEST_STRING, "[%d ", page - 1);
176
957
    else {
177
957
        code = update_max_page_reference(pdev, &page);
178
957
        if (code < 0)
179
0
            return code;
180
957
        gs_snprintf(dstr, MAX_DEST_STRING, "[%"PRId64" 0 R ", pdf_page_id(pdev, page));
181
957
    }
182
1.49k
    len = strlen(dstr);
183
1.49k
    if (len + view_string.size > MAX_DEST_STRING)
184
0
        return_error(gs_error_limitcheck);
185
1.49k
    if (view_string.data[0] != '[' ||
186
1.49k
        view_string.data[view_string.size - 1] != ']'
187
1.49k
        )
188
0
        return_error(gs_error_rangecheck);
189
1.49k
    memcpy(dstr + len, view_string.data + 1, view_string.size - 1);
190
1.49k
    dstr[len + view_string.size - 1] = 0;
191
1.49k
    return present;
192
1.49k
}
193
194
/*
195
 * If a named destination is specified by a string, convert it to a name,
196
 * update *dstr, and return 1; otherwise return 0.
197
 */
198
static int
199
pdfmark_coerce_dest(gs_param_string *dstr, char dest[MAX_DEST_STRING])
200
0
{
201
0
    const byte *data = dstr->data;
202
0
    uint size = dstr->size;
203
0
    if (size > MAX_DEST_STRING)
204
0
        return_error(gs_error_limitcheck);
205
0
    if (size == 0 || data[0] != '(')
206
0
        return 0;
207
    /****** HANDLE ESCAPES ******/
208
0
    memcpy(dest, data, size - 1);
209
0
    dest[0] = '/';
210
0
    dest[size - 1] = 0;
211
0
    dstr->data = (byte *)dest;
212
0
    dstr->size = size - 1;
213
0
    return 1;
214
0
}
215
216
/* Put pairs in a dictionary. */
217
static int
218
pdfmark_put_c_pair(cos_dict_t *pcd, const char *key,
219
                   const gs_param_string * pvalue)
220
6.39k
{
221
6.39k
    return cos_dict_put_c_key_string(pcd, key, pvalue->data, pvalue->size);
222
6.39k
}
223
static int
224
pdfmark_put_pair(cos_dict_t *pcd, const gs_param_string * pair)
225
37.6k
{
226
37.6k
    return cos_dict_put_string(pcd, pair->data, pair->size,
227
37.6k
                               pair[1].data, pair[1].size);
228
37.6k
}
229
230
/* Scan a Rect value. */
231
static int
232
pdfmark_scan_rect(gs_rect * prect, const gs_param_string * str,
233
                  const gs_matrix * pctm)
234
5.81k
{
235
5.81k
    uint size = str->size;
236
5.81k
    double v[4];
237
11.6k
#define MAX_RECT_STRING 100
238
5.81k
    char chars[MAX_RECT_STRING + 3];
239
5.81k
    int end_check;
240
241
5.81k
    if (str->size > MAX_RECT_STRING)
242
0
        return_error(gs_error_limitcheck);
243
5.81k
    memcpy(chars, str->data, size);
244
5.81k
    strcpy(chars + size, " 0");
245
5.81k
    if (sscanf(chars, "[%lg %lg %lg %lg]%d",
246
5.81k
               &v[0], &v[1], &v[2], &v[3], &end_check) != 5
247
5.81k
        )
248
2
        return_error(gs_error_rangecheck);
249
5.80k
    gs_point_transform(v[0], v[1], pctm, &prect->p);
250
5.80k
    gs_point_transform(v[2], v[3], pctm, &prect->q);
251
5.80k
    return 0;
252
5.81k
}
253
254
/* Make a Rect value. */
255
static void
256
pdfmark_make_rect(char str[MAX_RECT_STRING], const gs_rect * prect)
257
5.80k
{
258
    /*
259
     * We have to use a stream and pprintf, rather than sprintf,
260
     * because printf formats can't express the PDF restrictions on
261
     * the form of the output.
262
     */
263
5.80k
    stream s;
264
265
5.80k
    s_init(&s, NULL);
266
5.80k
    swrite_string(&s, (byte *)str, MAX_RECT_STRING - 1);
267
5.80k
    pprintg4(&s, "[%g %g %g %g]",
268
5.80k
             prect->p.x, prect->p.y, prect->q.x, prect->q.y);
269
5.80k
    str[stell(&s)] = 0;
270
5.80k
}
271
272
2.58k
#define MAX_BORDER_STRING 100
273
/* Write a transformed Border value on a stream. */
274
static int
275
pdfmark_write_border(stream *s, const gs_param_string *str,
276
                     const gs_matrix *pctm)
277
1.29k
{
278
1.29k
    int i;
279
280
11.0k
    for (i = 0; i < str->size; i++)
281
9.80k
        stream_putc(s, str->data[i]);
282
1.29k
    return 0;
283
1.29k
}
284
285
/* Put an element in a stream's dictionary. */
286
static int
287
cos_stream_put_c_strings(cos_stream_t *pcs, const char *key, const char *value)
288
0
{
289
0
    return cos_dict_put_c_strings(cos_stream_dict(pcs), key, value);
290
0
}
291
292
static int
293
setup_pdfmark_stream_no_compression(gx_device_psdf *pdev0,
294
                        cos_stream_t *pco)
295
0
{
296
    /* This function is for pdfwrite only. */
297
0
    gx_device_pdf *pdev = (gx_device_pdf *)pdev0;
298
0
    gs_memory_t *mem = pdev->pdf_memory;
299
300
0
    pco->input_strm = cos_write_stream_alloc(pco, pdev,
301
0
                                  "setup_pdfmark_stream_compression");
302
0
    if (pco->input_strm == 0)
303
0
        return_error(gs_error_VMerror);
304
0
    if (!pdev->binary_ok) {
305
0
        stream_state *ss = s_alloc_state(mem, s_A85E_template.stype,
306
0
                          "setup_pdfmark_stream_compression");
307
0
        if (ss == 0)
308
0
            return_error(gs_error_VMerror);
309
0
        if (s_add_filter(&pco->input_strm, &s_A85E_template, ss, mem) == 0) {
310
0
            gs_free_object(mem, ss, "setup_image_compression");
311
0
            return_error(gs_error_VMerror);
312
0
        }
313
0
    }
314
0
    return 0;
315
0
}
316
317
/* Setup pdfmak stream compression. */
318
static int
319
setup_pdfmark_stream_compression(gx_device_psdf *pdev0,
320
                        cos_stream_t *pco)
321
232
{
322
    /* This function is for pdfwrite only. */
323
232
    gx_device_pdf *pdev = (gx_device_pdf *)pdev0;
324
232
    gs_memory_t *mem = pdev->pdf_memory;
325
232
    static const pdf_filter_names_t fnames = {
326
232
        PDF_FILTER_NAMES
327
232
    };
328
232
    stream_state *st;
329
232
    const stream_template *templat;
330
331
232
    if (pdev->CompressStreams) {
332
232
        if(pdev->version >= psdf_version_ll3) {
333
224
            if (pdev->UseBrotli)
334
0
                templat = &s_brotliE_template;
335
224
            else
336
224
                templat = &s_zlibE_template;
337
224
        } else
338
8
            templat = &s_LZWE_template;
339
232
    } else
340
0
        return 0;
341
342
232
    pco->input_strm = cos_write_stream_alloc(pco, pdev,
343
232
                                  "setup_pdfmark_stream_compression");
344
232
    if (pco->input_strm == 0)
345
0
        return_error(gs_error_VMerror);
346
232
    if (!pdev->binary_ok) {
347
8
        stream_state *ss = s_alloc_state(mem, s_A85E_template.stype,
348
8
                          "setup_pdfmark_stream_compression");
349
8
        if (ss == 0)
350
0
            return_error(gs_error_VMerror);
351
8
        if (s_add_filter(&pco->input_strm, &s_A85E_template, ss, mem) == 0) {
352
0
            gs_free_object(mem, ss, "setup_image_compression");
353
0
            return_error(gs_error_VMerror);
354
0
        }
355
8
    }
356
232
    st = s_alloc_state(mem, templat->stype,
357
232
                            "setup_pdfmark_stream_compression");
358
232
    if (st == 0)
359
0
        return_error(gs_error_VMerror);
360
232
    if (templat->set_defaults)
361
232
        (*templat->set_defaults) (st);
362
232
    if (s_add_filter(&pco->input_strm, templat, st, mem) == 0) {
363
0
        gs_free_object(mem, st, "setup_image_compression");
364
0
        return_error(gs_error_VMerror);
365
0
    }
366
232
    return pdf_put_filters(cos_stream_dict(pco), pdev, pco->input_strm, &fnames);
367
232
}
368
369
static int
370
pdfmark_bind_named_object(gx_device_pdf *pdev, const gs_const_string *objname,
371
                          pdf_resource_t **pres)
372
0
{
373
0
    int code;
374
375
0
    if (objname != NULL && objname->size) {
376
0
        const cos_value_t *v = cos_dict_find(pdev->local_named_objects, objname->data, objname->size);
377
378
0
        if (v != NULL) {
379
0
            if (v->value_type == COS_VALUE_OBJECT) {
380
0
                if (cos_type(v->contents.object) == &cos_generic_procs) {
381
                    /* The object was referred but not defined.
382
                       Use the old object id.
383
                       The old object stub to be dropped. */
384
0
                    pdf_reserve_object_id(pdev, *pres, v->contents.object->id);
385
0
                } else if (!v->contents.object->written) {
386
                    /* We can't know whether the old object was referred or not.
387
                       Write it out for a consistent result in any case. */
388
0
                    code = cos_write_object(v->contents.object, pdev, resourceOther);
389
390
0
                    if (code < 0)
391
0
                        return code;
392
0
                    v->contents.object->written = true;
393
0
                }
394
0
            } else
395
0
                return_error(gs_error_rangecheck); /* Must not happen. */
396
0
        }
397
0
    }
398
0
    if ((*pres)->object->id == -1) {
399
0
        if(objname != NULL && objname->size)
400
0
            code = pdf_substitute_resource(pdev, pres, resourceXObject, NULL, false);
401
0
        else
402
0
            code = pdf_substitute_resource(pdev, pres, resourceXObject, NULL, true);
403
0
        (*pres)->where_used |= pdev->used_mask;
404
0
        if (code < 0)
405
0
            return code;
406
0
    } else {
407
        /* Unfortunately we can't apply pdf_substitute_resource,
408
           because the object may already be referred by its id.
409
           Redundant objects may happen in this case.
410
           For better results users should define objects before usage.
411
         */
412
0
    }
413
0
    if (objname != NULL && objname->size) {
414
0
        cos_value_t value;
415
416
0
        code = cos_dict_put(pdev->local_named_objects, objname->data,
417
0
                            objname->size, cos_object_value(&value, (cos_object_t *)(*pres)->object));
418
0
        if (code < 0)
419
0
            return code;
420
0
    }
421
0
    return 0;
422
0
}
423
424
/* ---------------- Miscellaneous pdfmarks ---------------- */
425
426
/*
427
 * Create the dictionary for an annotation or outline.  For some
428
 * unfathomable reason, PDF requires the following key substitutions
429
 * relative to pdfmarks:
430
 *   In annotation and link dictionaries:
431
 *     /Action => /A, /Color => /C, /Title => /T
432
 *   In outline directionaries:
433
 *     /Action => /A, but *not* /Color or /Title
434
 *   In Action subdictionaries:
435
 *     /Dest => /D, /File => /F, /Subtype => /S
436
 * and also the following substitutions:
437
 *     /Action /Launch /File xxx =>
438
 *       /A << /S /Launch /F xxx >>
439
 *     /Action /GoToR /File xxx /Dest yyy =>
440
 *       /A << /S /GoToR /F xxx /D yyy' >>
441
 *     /Action /Article /Dest yyy =>
442
 *       /A << /S /Thread /D yyy' >>
443
 *     /Action /GoTo => drop the Action key
444
 * Also, \n in Contents strings must be replaced with \r.
445
 * Also, an outline dictionary with no action, Dest, Page, or View has an
446
 * implied GoTo action with Dest = [{ThisPage} /XYZ null null null].
447
 * Note that for Thread actions, the Dest is not a real destination,
448
 * and must not be processed as one.
449
 *
450
 * We always treat /A and /F as equivalent to /Action and /File
451
 * respectively.  The pdfmark and PDF documentation is so confused on the
452
 * issue of when the long and short names should be used that we only give
453
 * this a 50-50 chance of being right.
454
 *
455
 * Note that we must transform Rect and Border coordinates.
456
 */
457
458
typedef struct named_obj_params {
459
    int object;
460
    cos_value_t *v;
461
}named_obj_params_t;
462
463
static int named_obj_proc(void *client_data, const byte *key_data, uint key_size, const cos_value_t *v)
464
0
{
465
0
    named_obj_params_t *data = (named_obj_params_t *)client_data;
466
467
0
    if (v->value_type == COS_VALUE_OBJECT && v->contents.object->id == data->object)
468
0
        data->v = (cos_value_t *)v;
469
0
    return 0;
470
0
}
471
472
static int find_named_object(cos_dict_t *pcd, int obj, cos_value_t **v)
473
0
{
474
0
    named_obj_params_t data;
475
0
    int code;
476
477
0
    data.object = obj;
478
0
    data.v = NULL;
479
0
    code = cos_dict_forall(pcd, &data, named_obj_proc);
480
0
    if (code < 0)
481
0
        return code;
482
483
0
    if (data.v != NULL) {
484
0
        *v = data.v;
485
0
        code = 1;
486
0
    }
487
0
    return code;
488
0
}
489
490
typedef struct ao_params_s {
491
    gx_device_pdf *pdev;  /* for pdfmark_make_dest */
492
    const char *subtype;  /* default Subtype in top-level dictionary */
493
    int src_pg;   /* set to SrcPg - 1 if any */
494
} ao_params_t;
495
static int
496
pdfmark_put_ao_pairs(gx_device_pdf * pdev, cos_dict_t *pcd,
497
                     const gs_param_string * pairs, uint count,
498
                     const gs_matrix * pctm, ao_params_t * params,
499
                     bool for_outline)
500
5.84k
{
501
5.84k
    const gs_param_string *Action = 0;
502
5.84k
    const gs_param_string *File = 0;
503
5.84k
    const gs_param_string *URI = 0;
504
5.84k
    gs_param_string Dest;
505
5.84k
    gs_param_string Subtype;
506
5.84k
    uint i;
507
5.84k
    int code;
508
5.84k
    char dest[MAX_DEST_STRING];
509
5.84k
    bool coerce_dest = false;
510
511
5.84k
    Subtype.data = 0;
512
5.84k
    Subtype.size = 0;
513
5.84k
    Dest.data = 0;
514
5.84k
    Dest.size = 0;
515
5.84k
    if (params->subtype)
516
5.60k
        param_string_from_string(Subtype, params->subtype);
517
245
    else
518
245
        Subtype.data = 0;
519
50.8k
    for (i = 0; i < count; i += 2) {
520
44.9k
        const gs_param_string *pair = &pairs[i];
521
44.9k
        unsigned int src_pg;
522
523
44.9k
        if (pdf_key_eq(pair, "/SrcPg")){
524
0
            unsigned char *buf0 = (unsigned char *)gs_alloc_bytes(pdev->memory, (size_t)(pair[1].size + 1) * sizeof(unsigned char),
525
0
                        "pdf_xmp_write_translated");
526
527
0
            if (buf0 == NULL)
528
0
                return_error(gs_error_VMerror);
529
530
0
            memcpy(buf0, pair[1].data, pair[1].size);
531
0
            buf0[pair[1].size] = 0x00;
532
0
            if (sscanf((char *)buf0, "%ud", &src_pg) == 1)
533
0
                params->src_pg = src_pg - 1;
534
0
            gs_free_object(pdev->memory, buf0, "pdf_xmp_write_translated");
535
0
        }
536
44.9k
        else if (!for_outline && pdf_key_eq(pair, "/Color"))
537
0
            pdfmark_put_c_pair(pcd, "/C", pair + 1);
538
44.9k
        else if (!for_outline && pdf_key_eq(pair, "/Title"))
539
0
            pdfmark_put_c_pair(pcd, "/T", pair + 1);
540
44.9k
        else if (pdf_key_eq(pair, "/Action") || pdf_key_eq(pair, "/A"))
541
541
            Action = pair;
542
        /* Previously also catered for '/F', but at the top level (outside an
543
         * Action dict which is handled below), a /F can only be the Flags for
544
         * the annotation, not a File or JavaScript action.
545
         */
546
44.4k
        else if (pdf_key_eq(pair, "/File") /* || pdf_key_eq(pair, "/F")*/)
547
0
            File = pair;
548
44.4k
        else if (pdf_key_eq(pair, "/Dest")) {
549
0
            Dest = pair[1];
550
0
            coerce_dest = true;
551
0
        }
552
44.4k
        else if (pdf_key_eq(pair, "/URI")) {
553
0
            URI = pair;   /* save it for placing into the Action dict */
554
0
        }
555
44.4k
        else if (pdf_key_eq(pair, "/Page") || pdf_key_eq(pair, "/View")) {
556
            /* Make a destination even if this is for an outline. */
557
1.58k
            if (Dest.data == 0) {
558
794
                code = pdfmark_make_dest(dest, params->pdev, "/Page", "/View",
559
794
                                         pairs, count, 0);
560
794
                if (code >= 0) {
561
794
                    param_string_from_string(Dest, dest);
562
794
                    if (for_outline)
563
219
                        coerce_dest = false;
564
794
                } else {
565
0
                    emprintf(pdev->memory, "   **** Warning: Outline has invalid link that was discarded.\n");
566
0
                    return gs_note_error(gs_error_rangecheck);
567
0
                }
568
794
            }
569
42.8k
        } else if (pdf_key_eq(pair, "/Subtype"))
570
5.60k
            Subtype = pair[1];
571
        /*
572
         * We also have to replace all occurrences of \n in Contents
573
         * strings with \r.  Unfortunately, they probably have already
574
         * been converted to \012....
575
         */
576
37.2k
        else if (pdf_key_eq(pair, "/Contents")) {
577
2.09k
            byte *cstr;
578
2.09k
            uint csize = pair[1].size;
579
2.09k
            cos_value_t *pcv;
580
2.09k
            uint i, j;
581
582
            /*
583
             * Copy the string into value storage, then update it in place.
584
             */
585
2.09k
            pdfmark_put_pair(pcd, pair);
586
            /* Break const so we can update the (copied) string. */
587
2.09k
            pcv = (cos_value_t *)cos_dict_find_c_key(pcd, "/Contents");
588
2.09k
            if (pcv == NULL)
589
0
                return_error(gs_error_ioerror); /* shouldn't be possible */
590
2.09k
            cstr = pcv->contents.chars.data;
591
            /* Loop invariant: j <= i < csize. */
592
107k
            for (i = j = 0; i < csize;)
593
105k
                if (csize - i >= 2 && !memcmp(cstr + i, "\\n", 2) &&
594
2
                    (i == 0 || cstr[i - 1] != '\\')
595
105k
                    ) {
596
2
                    cstr[j] = '\\', cstr[j + 1] = 'r';
597
2
                    i += 2, j += 2;
598
105k
                } else if (csize - i >= 4 && !memcmp(cstr + i, "\\012", 4) &&
599
0
                           (i == 0 || cstr[i - 1] != '\\')
600
105k
                    ) {
601
0
                    cstr[j] = '\\', cstr[j + 1] = 'r';
602
0
                    i += 4, j += 2;
603
0
                } else
604
105k
                    cstr[j++] = cstr[i++];
605
2.09k
            if (j != i) {
606
0
                pcv->contents.chars.data =
607
0
                gs_resize_string(pdev->pdf_memory, cstr, csize, j,
608
0
                                 "pdfmark_put_ao_pairs");
609
0
                pcv->contents.chars.size = j;
610
0
            }
611
35.1k
        } else if (pdf_key_eq(pair, "/L")) {
612
230
            gs_rect rect;
613
230
            char rstr[MAX_RECT_STRING];
614
230
            int code = pdfmark_scan_rect(&rect, pair + 1, pctm);
615
230
            if (code < 0)
616
0
                return code;
617
230
            pdfmark_make_rect(rstr, &rect);
618
230
            cos_dict_put_c_key_string(pcd, "/L", (byte *)rstr,
619
230
                                      strlen(rstr));
620
34.9k
        } else if (pdf_key_eq(pair, "/Rect")) {
621
5.58k
            gs_rect rect;
622
5.58k
            char rstr[MAX_RECT_STRING];
623
5.58k
            int code = pdfmark_scan_rect(&rect, pair + 1, pctm);
624
625
5.58k
            if (code < 0)
626
2
                return code;
627
5.57k
            pdfmark_make_rect(rstr, &rect);
628
5.57k
            cos_dict_put_c_key_string(pcd, "/Rect", (byte *)rstr,
629
5.57k
                                      strlen(rstr));
630
29.3k
        } else if (pdf_key_eq(pair, "/Border")) {
631
1.29k
            stream s;
632
1.29k
            char bstr[MAX_BORDER_STRING + 1];
633
1.29k
            int code;
634
635
1.29k
            s_init(&s, NULL);
636
1.29k
            swrite_string(&s, (byte *)bstr, MAX_BORDER_STRING + 1);
637
1.29k
            code = pdfmark_write_border(&s, pair + 1, pctm);
638
1.29k
            if (code < 0)
639
0
                return code;
640
1.29k
            if (stell(&s) > MAX_BORDER_STRING)
641
0
                return_error(gs_error_limitcheck);
642
1.29k
            bstr[stell(&s)] = 0;
643
1.29k
            cos_dict_put_c_key_string(pcd, "/Border", (byte *)bstr,
644
1.29k
                                      strlen(bstr));
645
28.0k
        } else if (for_outline && pdf_key_eq(pair, "/Count"))
646
28.0k
            DO_NOTHING;
647
27.8k
        else {
648
27.8k
            int i, j=0;
649
27.8k
            unsigned char *temp, *buf0 = (unsigned char *)gs_alloc_bytes(pdev->memory, (size_t)(pair[1].size) * 2 * sizeof(unsigned char),
650
27.8k
                        "pdf_xmp_write_translated");
651
27.8k
            if (buf0 == NULL)
652
0
                return_error(gs_error_VMerror);
653
703k
            for (i = 0; i < pair[1].size; i++) {
654
676k
                byte c = pair[1].data[i];
655
656
676k
                buf0[j++] = c;
657
                /* Acrobat doesn't like the 'short' escapes. and wants them as octal....
658
                 * I beliwvw this should be considered an Acrtobat bug, either escapes
659
                 * can be used, or not, we shouldn't have to force them to octal.
660
                 */
661
676k
                if (c == '\\') {
662
3.53k
                    switch(pair[1].data[i + 1]) {
663
0
                        case 'b':
664
0
                            buf0[j++] = '0';
665
0
                            buf0[j++] = '1';
666
0
                            buf0[j++] = '0';
667
0
                            i++;
668
0
                            break;
669
0
                        case 'f':
670
0
                            buf0[j++] = '0';
671
0
                            buf0[j++] = '1';
672
0
                            buf0[j++] = '4';
673
0
                            i++;
674
0
                            break;
675
110
                        case 'n':
676
110
                            buf0[j++] = '0';
677
110
                            buf0[j++] = '1';
678
110
                            buf0[j++] = '2';
679
110
                            i++;
680
110
                            break;
681
5
                        case 'r':
682
5
                            buf0[j++] = '0';
683
5
                            buf0[j++] = '1';
684
5
                            buf0[j++] = '5';
685
5
                            i++;
686
5
                            break;
687
0
                        case 't':
688
0
                            buf0[j++] = '0';
689
0
                            buf0[j++] = '1';
690
0
                            buf0[j++] = '1';
691
0
                            i++;
692
0
                            break;
693
2
                        case '\\':
694
2
                            buf0[j++] = '1';
695
2
                            buf0[j++] = '3';
696
2
                            buf0[j++] = '4';
697
2
                            i++;
698
2
                            break;
699
3.42k
                        default:
700
3.42k
                            break;
701
3.53k
                    }
702
3.53k
                }
703
676k
            }
704
27.8k
            i = pair[1].size;
705
27.8k
            temp = (unsigned char *)pair[1].data;
706
27.8k
            ((gs_param_string *)pair)[1].data = buf0;
707
27.8k
            ((gs_param_string *)pair)[1].size = j;
708
709
27.8k
            pdfmark_put_pair(pcd, pair);
710
711
27.8k
            ((gs_param_string *)pair)[1].data = temp;
712
27.8k
            ((gs_param_string *)pair)[1].size = i;
713
27.8k
            gs_free_object(pdev->memory, buf0, "pdf_xmp_write_translated");
714
27.8k
        }
715
44.9k
    }
716
5.84k
    if (!for_outline && pdf_key_eq(&Subtype, "/Link")) {
717
1.13k
        if (Action) {
718
            /* Don't delete the Dest for GoTo or file-GoToR. */
719
520
            if (pdf_key_eq(Action + 1, "/GoTo") ||
720
520
                (File && pdf_key_eq(Action + 1, "/GoToR"))
721
520
                )
722
520
                DO_NOTHING;
723
520
            else
724
520
                Dest.data = 0;
725
520
        }
726
1.13k
    }
727
728
    /* Now handle the deferred keys. */
729
5.84k
    if (Action) {
730
541
        const byte *astr = Action[1].data;
731
541
        const uint asize = Action[1].size;
732
733
541
        if ((File != 0 || Dest.data != 0 || URI != 0) &&
734
0
            (pdf_key_eq(Action + 1, "/Launch") ||
735
0
             (pdf_key_eq(Action + 1, "/GoToR") && File) ||
736
0
             pdf_key_eq(Action + 1, "/Article"))
737
541
            ) {
738
0
            cos_dict_t *adict;
739
0
            cos_value_t avalue;
740
741
0
            if (pdf_key_eq(Action + 1, "/Launch") && pdev->PDFA != 0) {
742
0
                switch (pdev->PDFACompatibilityPolicy) {
743
                    /* Default behaviour matches Adobe Acrobat, warn and continue,
744
                     * output file will not be PDF/A compliant
745
                     */
746
0
                    case 0:
747
0
                        emprintf(pdev->memory,
748
0
                                 "Launch annotations not permitted in PDF/A, reverting to normal PDF output\n");
749
0
                        pdev->AbortPDFAX = true;
750
0
                        pdev->PDFA = 0;
751
0
                        break;
752
                        /* Since the annotation would break PDF/A compatibility, do not
753
                         * include it, but warn the user that it has been dropped.
754
                         */
755
0
                    case 1:
756
0
                        emprintf(pdev->memory,
757
0
                                 "Launch annotations not permitted in PDF/A,  cannot drop annotation, aborting conversion\n");
758
0
                        return_error (gs_error_typecheck);
759
0
                        break;
760
0
                    case 2:
761
0
                        emprintf(pdev->memory,
762
0
                                 "Launch annotations not permitted in PDF/A,  aborting conversion\n");
763
0
                        return_error (gs_error_typecheck);
764
0
                        break;
765
0
                    default:
766
0
                        emprintf(pdev->memory,
767
0
                                 "Launch annotations not permitted in PDF/A, unrecognised PDFACompatibilityLevel,\nreverting to normal PDF output\n");
768
0
                        pdev->AbortPDFAX = true;
769
0
                        pdev->PDFA = 0;
770
0
                        break;
771
0
                }
772
0
            }
773
774
0
            adict = cos_dict_alloc(pdev, "action dict");
775
0
            if (adict == 0)
776
0
                return_error(gs_error_VMerror);
777
0
            if (!for_outline) {
778
                /* We aren't sure whether this is really needed.... */
779
0
                code = cos_dict_put_c_strings(adict, "/Type", "/Action");
780
0
                if (code < 0)
781
0
                    return code;
782
0
            }
783
0
            if (pdf_key_eq(Action + 1, "/Article")) {
784
0
                code = cos_dict_put_c_strings(adict, "/S", "/Thread");
785
0
                if (code < 0)
786
0
                    return code;
787
0
                coerce_dest = false; /* Dest is not a real destination */
788
0
            }
789
0
            else
790
0
                pdfmark_put_c_pair(adict, "/S", Action + 1);
791
0
            if (Dest.data) {
792
0
                if (coerce_dest)
793
0
                    pdfmark_coerce_dest(&Dest, dest);
794
0
                pdfmark_put_c_pair(adict, "/D", &Dest);
795
0
                Dest.data = 0;  /* so we don't write it again */
796
0
            }
797
0
            if (File) {
798
0
                pdfmark_put_c_pair(adict, "/F", File + 1);
799
0
                File = 0; /* so we don't write it again */
800
0
            }
801
0
            if (URI) {
802
                /* Adobe Distiller puts a /URI key from pdfmark into the */
803
                /* Action dict with /S /URI as Subtype */
804
0
                pdfmark_put_pair(adict, URI);
805
0
                code = cos_dict_put_c_strings(adict, "/S", "/URI");
806
0
                if (code < 0)
807
0
                    return code;
808
0
            }
809
0
            cos_dict_put(pcd, (const byte *)"/A", 2,
810
0
                         COS_OBJECT_VALUE(&avalue, adict));
811
541
        } else if (asize >= 4 && !memcmp(astr, "<<", 2)) {
812
            /* Replace occurrences of /Dest, /File, and /Subtype. */
813
407
            const byte *scan = astr + 2;
814
407
            const byte *end = astr + asize;
815
407
            gs_param_string key, value;
816
407
            cos_dict_t *adict = cos_dict_alloc(pdev, "action dict");
817
407
            cos_value_t avalue;
818
407
            int code = 0;
819
820
407
            if (adict == 0)
821
0
                return_error(gs_error_VMerror);
822
407
            if (URI) {
823
                /* Adobe Distiller puts a /URI key from pdfmark into the */
824
                /* Action dict with /S /URI as Subtype */
825
0
                pdfmark_put_pair(adict, URI);
826
0
                if(cos_dict_put_c_strings(adict, "/S", "/URI") < 0)
827
0
                    return code;
828
0
            }
829
1.59k
            while ((code = pdf_scan_token(&scan, end, &key.data)) > 0) {
830
1.59k
                key.size = scan - key.data;
831
1.59k
                if (key.data[0] != '/' ||
832
1.18k
                    (code = pdf_scan_token_composite(&scan, end, &value.data)) != 1)
833
407
                    break;
834
1.18k
                value.size = scan - value.data;
835
1.18k
                if (pdev->PDFA != 0 && (pdf_key_eq(&key, "/Subtype") || pdf_key_eq(&key, "/S"))) {
836
0
                    if (pdf_key_eq(&value, "/Launch")) {
837
0
                        switch (pdev->PDFACompatibilityPolicy) {
838
                            /* Default behaviour matches Adobe Acrobat, warn and continue,
839
                             * output file will not be PDF/A compliant
840
                             */
841
0
                            case 0:
842
0
                                emprintf(pdev->memory,
843
0
                                         "Launch annotations not permitted in PDF/A, reverting to normal PDF output\n");
844
0
                                pdev->AbortPDFAX = true;
845
0
                                pdev->PDFA = 0;
846
0
                                break;
847
                                /* Since the annotation would break PDF/A compatibility, do not
848
                                 * include it, but warn the user that it has been dropped.
849
                                 */
850
0
                            case 1:
851
0
                                emprintf(pdev->memory,
852
0
                                         "Launch annotations not permitted in PDF/A,  cannot drop annotation, aborting conversion\n");
853
0
                                gs_free_object(pdev->memory->stable_memory, adict,  "action dict");
854
0
                                return_error (gs_error_typecheck);
855
0
                                break;
856
0
                            case 2:
857
0
                                emprintf(pdev->memory,
858
0
                                         "Launch annotations not permitted in PDF/A,  aborting conversion\n");
859
0
                                gs_free_object(pdev->memory->stable_memory, adict,  "action dict");
860
0
                                return_error (gs_error_typecheck);
861
0
                                break;
862
0
                            default:
863
0
                                emprintf(pdev->memory,
864
0
                                         "Launch annotations not permitted in PDF/A, unrecognised PDFACompatibilityLevel,\nreverting to normal PDF output\n");
865
0
                                pdev->AbortPDFAX = true;
866
0
                                pdev->PDFA = 0;
867
0
                                break;
868
0
                        }
869
0
                    }
870
0
                }
871
1.18k
                if (pdf_key_eq(&key, "/Dest") || pdf_key_eq(&key, "/D")) {
872
19
                    param_string_from_string(key, "/D");
873
19
                    if (value.data[0] == '(' && pdev->CompatibilityLevel < 1.2) {
874
0
                        int i;
875
876
0
                        for (i = 0;i < value.size; i++) {
877
0
                            if (value.data[i] == '\\') {
878
0
                                emprintf(pdev->memory,
879
0
                                         "Link Destination contains characters which cannot be represented in a name.\nDestinations cannot be strings in versions prior to PDF 1.2. Annotation removed in the output.\n");
880
0
                                return gs_error_typecheck;
881
0
                            }
882
0
                        }
883
                        /****** FIXME: DETECT ESCAPES ******/
884
0
                        pdfmark_coerce_dest(&value, dest);
885
0
                    }
886
1.16k
                } else if (pdf_key_eq(&key, "/File"))
887
0
                    param_string_from_string(key, "/F");
888
1.16k
                else if (pdf_key_eq(&key, "/Subtype"))
889
0
                    param_string_from_string(key, "/S");
890
1.18k
                cos_dict_put_string(adict, key.data, key.size,
891
1.18k
                                    value.data, value.size);
892
1.18k
            }
893
407
            if (code <= 0 || !pdf_key_eq(&key, ">>")) {
894
0
                cos_free((cos_object_t *)adict, "action dict");
895
0
                return_error(gs_error_rangecheck);
896
0
            }
897
407
            cos_dict_put(pcd, (const byte *)"/A", 2,
898
407
                         COS_OBJECT_VALUE(&avalue, adict));
899
407
        } else if (pdf_key_eq(Action + 1, "/GoTo"))
900
0
            pdfmark_put_pair(pcd, Action);
901
134
        else {
902
            /* Hack: we could substitute names in pdfmark_process,
903
               now should recognize whether it was done.
904
               Not a perfect method though.
905
               Go with it for a while. */
906
134
            char buf[30];
907
134
            int d0, d1;
908
909
134
            if (Action[1].size > 29)
910
0
                return_error(gs_error_rangecheck);
911
134
            memcpy(buf, Action[1].data, Action[1].size);
912
134
            buf[Action[1].size] = 0;
913
134
            if (sscanf(buf, "%d %d R", &d0, &d1) == 2) {
914
134
                cos_value_t *v = NULL;
915
134
                cos_object_t *pco = NULL;
916
917
                /* This is all rather awkward. If we have an indirect reference to the Action
918
                 * dictionary, then we need to find the stored dictionary and check it to see if we have
919
                 * a Launch action, because that's barred in PDF/A.
920
                 * Unfortunately we have turned the unique label into an object number back in pdfmark_process
921
                 * so we have to look up the local (and potentially global) named resrouces to try and
922
                 * find the actual Action dictionary....
923
                 */
924
134
                if (pdev->PDFA != 0) {
925
0
                    if (find_named_object(pdev->local_named_objects, 5, &v) > 0) {
926
0
                        pco = (cos_object_t *)v->contents.object;
927
0
                    } else {
928
0
                        if (find_named_object(pdev->global_named_objects, 5, &v) > 0) {
929
0
                            pco = (cos_object_t *)v->contents.object;
930
0
                        }
931
0
                    }
932
0
                    if (pco && cos_type(pco) == cos_type_dict) {
933
0
                        const cos_value_t *v1 = NULL;
934
935
0
                        v1 = cos_dict_find_c_key((const cos_dict_t *)pco, "/S");
936
0
                        if (v1 != NULL) {
937
0
                            if (v1->value_type == COS_VALUE_SCALAR || v1->value_type == COS_VALUE_CONST) {
938
0
                                if (v1->contents.chars.size == 7 && strncmp((const char *)v1->contents.chars.data, "/Launch", 7) == 0) {
939
0
                                    switch (pdev->PDFACompatibilityPolicy) {
940
                                        /* Default behaviour matches Adobe Acrobat, warn and continue,
941
                                         * output file will not be PDF/A compliant
942
                                         */
943
0
                                        case 0:
944
0
                                            emprintf(pdev->memory,
945
0
                                                     "Launch annotations not permitted in PDF/A, reverting to normal PDF output\n");
946
0
                                            pdev->AbortPDFAX = true;
947
0
                                            pdev->PDFA = 0;
948
0
                                            break;
949
                                            /* Since the annotation would break PDF/A compatibility, do not
950
                                             * include it, but warn the user that it has been dropped.
951
                                             */
952
0
                                        case 1:
953
0
                                            emprintf(pdev->memory,
954
0
                                                     "Launch annotations not permitted in PDF/A,  cannot drop annotation, aborting conversion\n");
955
0
                                            return_error (gs_error_typecheck);
956
0
                                            break;
957
0
                                        case 2:
958
0
                                            emprintf(pdev->memory,
959
0
                                                     "Launch annotations not permitted in PDF/A,  aborting conversion\n");
960
0
                                            return_error (gs_error_typecheck);
961
0
                                            break;
962
0
                                        default:
963
0
                                            emprintf(pdev->memory,
964
0
                                                     "Launch annotations not permitted in PDF/A, unrecognised PDFACompatibilityLevel,\nreverting to normal PDF output\n");
965
0
                                            pdev->AbortPDFAX = true;
966
0
                                            pdev->PDFA = 0;
967
0
                                            break;
968
0
                                    }
969
0
                                }
970
0
                            }
971
0
                        }
972
0
                    }
973
0
                }
974
134
                pdfmark_put_pair(pcd, Action);
975
134
            }
976
134
        }
977
541
    }
978
    /*
979
     * If we have /Dest or /File without the right kind of action,
980
     * simply write it at the top level.  This doesn't seem right,
981
     * but I'm not sure what else to do.
982
     */
983
5.84k
    if (Dest.data) {
984
794
        if (coerce_dest)
985
0
            pdfmark_coerce_dest(&Dest, dest);
986
        /*
987
         * For PDF 1.2 or better  we write a Names tree, but in this case the names
988
         * are required to be (counter-intuitively) strings, NOT name objects....
989
         */
990
794
        if (pdev->CompatibilityLevel > 1.1) {
991
794
            gs_param_string DestString;
992
794
            int i = 0, j;
993
794
            char *D;
994
995
            /*
996
             * If the name has any 'unusual' characters, it is 'escaped' by starting with NULLs
997
             * I suspect we no longer need that, but here we remove the escaping NULLs
998
             */
999
794
            if (Dest.size > 3 && Dest.data[0] == 0x00 && Dest.data[1] == 0x00 && Dest.data[Dest.size - 1] == 0x00) {
1000
0
                i = 2;
1001
0
                if (Dest.size > 5 && Dest.data[2] == 0x00 && Dest.data[3] == 0x00)
1002
0
                    i += 2;
1003
                /* If it has preceeding NULLs, then it has a terminating NULL as well, get rid of that too */
1004
0
                Dest.size--;
1005
0
            }
1006
1007
794
            if (Dest.data[i] == '/') {
1008
0
                i++;
1009
1010
0
                DestString.data = gs_alloc_bytes(pdev->memory->stable_memory, ((size_t)Dest.size * 2) + 1, "DEST pdfmark temp string");
1011
0
                if (DestString.data == 0)
1012
0
                    return_error(gs_error_VMerror);
1013
0
                DestString.size = (Dest.size * 2) + 1;
1014
0
                DestString.persistent = 0;
1015
0
                D = (char *)DestString.data;
1016
0
                D[0] = '(';
1017
0
                for (j=1;i<Dest.size;i++, j++) {
1018
0
                    if (Dest.data[i] == '(' || Dest.data[i] == ')') {
1019
0
                        D[j++] = '\\';
1020
0
                    }
1021
0
                    D[j] = Dest.data[i];
1022
0
                }
1023
0
                D[j++] = ')';
1024
0
                DestString.size = j;
1025
0
                pdfmark_put_c_pair(pcd, "/Dest", &DestString);
1026
0
                gs_free_object(pdev->memory->stable_memory, D, "DEST pdfmark temp string");
1027
0
            } else
1028
794
                pdfmark_put_c_pair(pcd, "/Dest", &Dest);
1029
794
        } else
1030
0
            pdfmark_put_c_pair(pcd, "/Dest", &Dest);
1031
5.05k
    } else if (for_outline && !Action) {
1032
        /* Make an implicit destination. */
1033
5
        char dstr[1 + (sizeof(int64_t) * 8 / 3 + 1) + 25 + 1];
1034
5
        int64_t page_id = pdf_page_id(pdev, pdev->next_page + 1);
1035
1036
5
        gs_snprintf(dstr, MAX_DEST_STRING, "[%"PRId64" 0 R /XYZ null null null]", page_id);
1037
5
        cos_dict_put_c_key_string(pcd, "/Dest", (const unsigned char*) dstr,
1038
5
                                  strlen(dstr));
1039
5
    }
1040
5.84k
    if (File)
1041
0
        pdfmark_put_pair(pcd, File);
1042
5.84k
    if (Subtype.data)
1043
5.60k
        pdfmark_put_c_pair(pcd, "/Subtype", &Subtype);
1044
5.84k
    return 0;
1045
5.84k
}
1046
1047
/* Copy an annotation dictionary. */
1048
static int
1049
pdfmark_annot(gx_device_pdf * pdev, gs_param_string * pairs, uint count,
1050
              const gs_matrix * pctm, const gs_param_string *objname,
1051
              const char *subtype)
1052
5.60k
{
1053
5.60k
    ao_params_t params;
1054
5.60k
    cos_dict_t *pcd;
1055
5.60k
    int page_index = pdev->next_page;
1056
5.60k
    cos_array_t *annots;
1057
5.60k
    cos_value_t value;
1058
5.60k
    int code;
1059
1060
    /* Annotations are only permitted in PDF/A if they have the
1061
     * Print flag enabled, so we need to prescan for that here.
1062
     */
1063
5.60k
    if(pdev->PDFA != 0) {
1064
0
        int i;
1065
0
        unsigned int Flags = 0;
1066
        /* Check all the keys to see if we have a /F (Flags) key/value pair defined */
1067
0
        for (i = 0; i < count; i += 2) {
1068
0
            const gs_param_string *pair = &pairs[i];
1069
1070
0
            if (pdf_key_eq(pair, "/F")) {
1071
0
                char Buffer[32];
1072
1073
0
                pair = &pairs[i+1];
1074
0
                if (pair->size >= sizeof(Buffer))
1075
0
                    code = 0;
1076
0
                else {
1077
0
                    memcpy(Buffer, pair->data, pair->size);
1078
0
                    Buffer[pair->size] = 0x00;
1079
0
                    code = sscanf(Buffer, "%u", &Flags);
1080
0
                }
1081
0
                if (code != 1)
1082
0
                    emprintf(pdev->memory,
1083
0
                             "Annotation has an invalid /Flags attribute\n");
1084
0
                break;
1085
0
            }
1086
0
        }
1087
        /* Check the Print flag, PDF/A annotations *must* be set to print */
1088
0
        if ((Flags & 4) == 0){
1089
0
            switch (pdev->PDFACompatibilityPolicy) {
1090
                /* Default behaviour matches Adobe Acrobat, warn and continue,
1091
                 * output file will not be PDF/A compliant
1092
                 */
1093
0
                case 0:
1094
0
                    emprintf(pdev->memory,
1095
0
                             "Annotation set to non-printing,\n not permitted in PDF/A, reverting to normal PDF output\n");
1096
0
                    pdev->AbortPDFAX = true;
1097
0
                    pdev->PDFA = 0;
1098
0
                    break;
1099
                    /* Since the annotation would break PDF/A compatibility, do not
1100
                     * include it, but warn the user that it has been dropped.
1101
                     */
1102
0
                case 1:
1103
0
                    emprintf(pdev->memory,
1104
0
                             "Annotation set to non-printing,\n not permitted in PDF/A, annotation will not be present in output file\n");
1105
0
                    return 0;
1106
0
                    break;
1107
0
                case 2:
1108
0
                    emprintf(pdev->memory,
1109
0
                             "Annotation set to non-printing,\n not permitted in PDF/A, aborting conversion\n");
1110
0
                    return_error(gs_error_invalidfont);
1111
0
                    break;
1112
0
                default:
1113
0
                    emprintf(pdev->memory,
1114
0
                             "Annotation set to non-printing,\n not permitted in PDF/A, unrecognised PDFACompatibilityLevel,\nreverting to normal PDF output\n");
1115
0
                    pdev->AbortPDFAX = true;
1116
0
                    pdev->PDFA = 0;
1117
0
                    break;
1118
0
            }
1119
0
        }
1120
0
    }
1121
5.60k
    if (pdev->PDFX != 0) {
1122
0
        gs_param_string Subtype;
1123
0
        bool discard = true;
1124
0
        pdf_page_t *page = 0L;
1125
1126
0
        page = &pdev->pages[pdev->next_page];
1127
1128
0
        if (subtype) {
1129
0
            param_string_from_string(Subtype, subtype);
1130
0
            if (pdf_key_eq(&Subtype, "/TrapNet") ||
1131
0
                pdf_key_eq(&Subtype, "/PrinterMark"))
1132
0
                discard = false;
1133
0
        }
1134
0
        if (discard) {
1135
0
            int i;
1136
0
            for (i = 0; i < count; i += 2) {
1137
0
                const gs_param_string *pair = &pairs[i];
1138
0
                if (pdf_key_eq(pair, "/Rect")) {
1139
0
                    gs_rect rect;
1140
0
                    const cos_value_t *v_trimbox, *v_bleedbox, *v_artbox, *v_cropbox;
1141
0
                    const byte *p;
1142
0
                    char buf[100];
1143
0
                    int size, code;
1144
0
                    float temp[4]; /* the type is float for sscanf. */
1145
0
                    double pagebox[4] = {0, 0};
1146
1147
0
                    pagebox[2] = pdev->MediaSize[0];
1148
0
                    pagebox[3] = pdev->MediaSize[1];
1149
1150
0
                    v_cropbox = v_bleedbox = v_trimbox = v_artbox = 0x0L;
1151
1152
0
                    code = pdfmark_scan_rect(&rect, pair + 1, pctm);
1153
1154
0
                    if (code < 0)
1155
0
                        return code;
1156
1157
0
                    if (page && page->Page) {
1158
0
                        v_trimbox = cos_dict_find_c_key(page->Page, "/TrimBox");
1159
0
                        v_bleedbox = cos_dict_find_c_key(page->Page, "/BleedBox");
1160
0
                        v_artbox = cos_dict_find_c_key(page->Page, "/ArtBox");
1161
0
                        v_cropbox = cos_dict_find_c_key(page->Page, "/CropBox");
1162
0
                    }
1163
1164
0
                    if (v_cropbox != NULL && v_cropbox->value_type == COS_VALUE_SCALAR) {
1165
0
                        p = v_cropbox->contents.chars.data;
1166
0
                        size = min (v_cropbox->contents.chars.size, sizeof(buf) - 1);
1167
0
                        memcpy(buf, p, size);
1168
0
                        buf[size] = 0;
1169
0
                        if (sscanf(buf, "[ %g %g %g %g ]",
1170
0
                                &temp[0], &temp[1], &temp[2], &temp[3]) == 4) {
1171
0
                            if (temp[0] > pagebox[0]) pagebox[0] = temp[0];
1172
0
                            if (temp[1] > pagebox[1]) pagebox[1] = temp[1];
1173
0
                        }
1174
0
                    }
1175
0
                    if (v_bleedbox != NULL && v_bleedbox->value_type == COS_VALUE_SCALAR) {
1176
0
                        p = v_bleedbox->contents.chars.data;
1177
0
                        size = min (v_bleedbox->contents.chars.size, sizeof(buf) - 1);
1178
0
                        memcpy(buf, p, size);
1179
0
                        buf[size] = 0;
1180
0
                        if (sscanf(buf, "[ %g %g %g %g ]",
1181
0
                                &temp[0], &temp[1], &temp[2], &temp[3]) == 4) {
1182
0
                            if (temp[0] > pagebox[0]) pagebox[0] = temp[0];
1183
0
                            if (temp[1] > pagebox[1]) pagebox[1] = temp[1];
1184
0
                        }
1185
0
                    }
1186
0
                    if (v_trimbox != NULL && v_trimbox->value_type == COS_VALUE_SCALAR) {
1187
0
                        p = v_trimbox->contents.chars.data;
1188
0
                        size = min (v_trimbox->contents.chars.size, sizeof(buf) - 1);
1189
0
                        memcpy(buf, p, size);
1190
0
                        buf[size] = 0;
1191
0
                        if (sscanf(buf, "[ %g %g %g %g ]",
1192
0
                                &temp[0], &temp[1], &temp[2], &temp[3]) == 4) {
1193
0
                            if (temp[0] > pagebox[0]) pagebox[0] = temp[0];
1194
0
                            if (temp[1] > pagebox[1]) pagebox[1] = temp[1];
1195
0
                        }
1196
0
                    }
1197
0
                    if (v_artbox != NULL && v_artbox->value_type == COS_VALUE_SCALAR) {
1198
0
                        p = v_artbox->contents.chars.data;
1199
0
                        size = min (v_artbox->contents.chars.size, sizeof(buf) - 1);
1200
0
                        memcpy(buf, p, size);
1201
0
                        buf[size] = 0;
1202
0
                        if (sscanf(buf, "[ %g %g %g %g ]",
1203
0
                                &temp[0], &temp[1], &temp[2], &temp[3]) == 4) {
1204
0
                            if (temp[0] > pagebox[0]) pagebox[0] = temp[0];
1205
0
                            if (temp[1] > pagebox[1]) pagebox[1] = temp[1];
1206
0
                        }
1207
0
                    }
1208
0
                    if (v_cropbox == 0 && v_trimbox == 0 && v_artbox == 0 && v_bleedbox == 0) {
1209
0
                        if (pdev->PDFXTrimBoxToMediaBoxOffset.size >= 4 &&
1210
0
                                    pdev->PDFXTrimBoxToMediaBoxOffset.data[0] >= 0 &&
1211
0
                                    pdev->PDFXTrimBoxToMediaBoxOffset.data[1] >= 0 &&
1212
0
                                    pdev->PDFXTrimBoxToMediaBoxOffset.data[2] >= 0 &&
1213
0
                                    pdev->PDFXTrimBoxToMediaBoxOffset.data[3] >= 0) {
1214
0
                            pagebox[0] += pdev->PDFXTrimBoxToMediaBoxOffset.data[0];
1215
0
                            pagebox[1] += pdev->PDFXTrimBoxToMediaBoxOffset.data[3];
1216
0
                            pagebox[2] -= pdev->PDFXTrimBoxToMediaBoxOffset.data[1];
1217
0
                            pagebox[3] -= pdev->PDFXTrimBoxToMediaBoxOffset.data[2];
1218
0
                        } else if (pdev->PDFXBleedBoxToTrimBoxOffset.size >= 4 &&
1219
0
                                pdev->PDFXBleedBoxToTrimBoxOffset.data[0] >= 0 &&
1220
0
                                pdev->PDFXBleedBoxToTrimBoxOffset.data[1] >= 0 &&
1221
0
                                pdev->PDFXBleedBoxToTrimBoxOffset.data[2] >= 0 &&
1222
0
                                pdev->PDFXBleedBoxToTrimBoxOffset.data[3] >= 0) {
1223
0
                            pagebox[0] -= pdev->PDFXBleedBoxToTrimBoxOffset.data[0];
1224
0
                            pagebox[1] -= pdev->PDFXBleedBoxToTrimBoxOffset.data[3];
1225
0
                            pagebox[2] += pdev->PDFXBleedBoxToTrimBoxOffset.data[1];
1226
0
                            pagebox[3] += pdev->PDFXBleedBoxToTrimBoxOffset.data[2];
1227
0
                        }
1228
0
                    }
1229
1230
0
                    if (rect.p.x > pagebox[2] || rect.q.x < pagebox[0] ||
1231
0
                        rect.p.y > pagebox[3] || rect.q.y < pagebox[1])
1232
0
                        break;
1233
0
                    switch (pdev->PDFACompatibilityPolicy) {
1234
                        /* Default behaviour matches Adobe Acrobat, warn and continue,
1235
                         * output file will not be PDF/A compliant
1236
                         */
1237
0
                        case 0:
1238
0
                            emprintf(pdev->memory,
1239
0
                                     "Annotation (not TrapNet or PrinterMark) on page,\n not permitted in PDF/X, reverting to normal PDF output\n");
1240
0
                            pdev->AbortPDFAX = true;
1241
0
                            pdev->PDFX = 0;
1242
0
                            break;
1243
                            /* Since the annotation would break PDF/A compatibility, do not
1244
                             * include it, but warn the user that it has been dropped.
1245
                             */
1246
0
                        case 1:
1247
0
                            emprintf(pdev->memory,
1248
0
                                     "Annotation (not TrapNet or PrinterMark) on page,\n not permitted in PDF/X, annotation will not be present in output file\n");
1249
0
                            return 0;
1250
0
                            break;
1251
0
                        case 2:
1252
0
                            emprintf(pdev->memory,
1253
0
                                     "Annotation (not TrapNet or PrinterMark) on page,\n not permitted in PDF/X, aborting conversion\n");
1254
0
                            return_error(gs_error_invalidfont);
1255
0
                            break;
1256
0
                        default:
1257
0
                            emprintf(pdev->memory,
1258
0
                                     "Annotation s(not TrapNet or PrinterMark) on page,\n not permitted in PDF/A, unrecognised PDFACompatibilityLevel,\nreverting to normal PDF output\n");
1259
0
                            pdev->AbortPDFAX = true;
1260
0
                            pdev->PDFX = 0;
1261
0
                            break;
1262
0
                    }
1263
0
                    break;
1264
0
                }
1265
0
            }
1266
0
            if (i > count) {
1267
0
                switch (pdev->PDFACompatibilityPolicy) {
1268
                    /* Default behaviour matches Adobe Acrobat, warn and continue,
1269
                     * output file will not be PDF/X compliant
1270
                     */
1271
0
                    case 0:
1272
0
                        emprintf(pdev->memory,
1273
0
                                 "Annotation (not TrapNet or PrinterMark) potentially on page (no /Rect in dict),\n not permitted in PDF/X, reverting to normal PDF output\n");
1274
0
                        pdev->AbortPDFAX = true;
1275
0
                        pdev->PDFX = 0;
1276
0
                        break;
1277
                        /* Since the annotation would break PDF/X compatibility, do not
1278
                         * include it, but warn the user that it has been dropped.
1279
                         */
1280
0
                    case 1:
1281
0
                        emprintf(pdev->memory,
1282
0
                                 "Annotation (not TrapNet or PrinterMark) potentially on page (no /Rect in dict),\n not permitted in PDF/X, annotation will not be present in output file\n");
1283
0
                        return 0;
1284
0
                        break;
1285
0
                    case 2:
1286
0
                        emprintf(pdev->memory,
1287
0
                                 "Annotation (not TrapNet or PrinterMark) potentially on page (no /Rect in dict),\n not permitted in PDF/X, aborting conversion\n");
1288
0
                        return_error(gs_error_invalidfont);
1289
0
                        break;
1290
0
                    default:
1291
0
                        emprintf(pdev->memory,
1292
0
                                 "Annotation s(not TrapNet or PrinterMark) potentially on page (no /Rect in dict),\n not permitted in PDF/A, unrecognised PDFACompatibilityLevel,\nreverting to normal PDF output\n");
1293
0
                        pdev->AbortPDFAX = true;
1294
0
                        pdev->PDFX = 0;
1295
0
                        break;
1296
0
                }
1297
0
            }
1298
0
        }
1299
0
    }
1300
5.60k
    params.pdev = pdev;
1301
5.60k
    params.subtype = subtype;
1302
5.60k
    params.src_pg = -1;
1303
5.60k
    code = pdf_make_named_dict(pdev, objname, &pcd, true);
1304
5.60k
    if (code < 0)
1305
0
        return code;
1306
5.60k
    code = cos_dict_put_c_strings(pcd, "/Type", "/Annot");
1307
5.60k
    if (code < 0) {
1308
0
        (void)pdf_obj_mark_unused(pdev, pcd->id);
1309
0
        cos_free((cos_object_t *)pcd, "pdfmark_annot");
1310
0
        return code;
1311
0
    }
1312
5.60k
    code = pdfmark_put_ao_pairs(pdev, pcd, pairs, count, pctm, &params, false);
1313
5.60k
    if (code < 0) {
1314
2
        (void)pdf_obj_mark_unused(pdev, pcd->id);
1315
2
        cos_free((cos_object_t *)pcd, "pdfmark_annot");
1316
2
        return code;
1317
2
    }
1318
5.60k
    if (params.src_pg >= 0)
1319
0
        page_index = params.src_pg;
1320
5.60k
    if (pdf_page_id(pdev, page_index + 1) <= 0) {
1321
0
        (void)pdf_obj_mark_unused(pdev, pcd->id);
1322
0
        cos_free((cos_object_t *)pcd, "pdfmark_annot");
1323
0
        return_error(gs_error_rangecheck);
1324
0
    }
1325
5.60k
    annots = pdev->pages[page_index].Annots;
1326
5.60k
    if (annots == 0) {
1327
553
        annots = cos_array_alloc(pdev, "pdfmark_annot");
1328
553
        if (annots == 0) {
1329
0
            (void)pdf_obj_mark_unused(pdev, pcd->id);
1330
0
            cos_free((cos_object_t *)pcd, "pdfmark_annot");
1331
0
            return_error(gs_error_VMerror);
1332
0
        }
1333
553
        pdev->pages[page_index].Annots = annots;
1334
553
    }
1335
5.60k
    if (!objname) {
1336
        /* Write the annotation now. */
1337
5.60k
        COS_WRITE_OBJECT(pcd, pdev, resourceAnnotation);
1338
5.60k
        COS_RELEASE(pcd, "pdfmark_annot");
1339
5.60k
    }
1340
5.60k
    return cos_array_add(annots,
1341
5.60k
                         cos_object_value(&value, COS_OBJECT(pcd)));
1342
5.60k
}
1343
1344
/* ANN pdfmark */
1345
static int
1346
pdfmark_ANN(gx_device_pdf * pdev, gs_param_string * pairs, uint count,
1347
            const gs_matrix * pctm, const gs_param_string * objname)
1348
4.46k
{
1349
4.46k
    return pdfmark_annot(pdev, pairs, count, pctm, objname, "/Text");
1350
4.46k
}
1351
1352
/* LNK pdfmark (obsolescent) */
1353
static int
1354
pdfmark_LNK(gx_device_pdf * pdev, gs_param_string * pairs, uint count,
1355
            const gs_matrix * pctm, const gs_param_string * objname)
1356
1.13k
{
1357
1.13k
    return pdfmark_annot(pdev, pairs, count, pctm, objname, "/Link");
1358
1.13k
}
1359
1360
/* Write and release one node of the outline tree. */
1361
static int
1362
pdfmark_write_outline(gx_device_pdf * pdev, pdf_outline_node_t * pnode,
1363
                      int64_t next_id)
1364
245
{
1365
245
    stream *s;
1366
245
    int code = 0;
1367
1368
245
    pdf_open_separate(pdev, pnode->id, resourceOutline);
1369
245
    if (pnode->action != NULL)
1370
245
        pnode->action->id = pnode->id;
1371
0
    else {
1372
0
        emprintf1(pdev->memory,
1373
0
                  "pdfmark error: Outline node %ld has no action or destination.\n",
1374
0
                  (unsigned long)pnode->id);
1375
0
        code = gs_note_error(gs_error_undefined);
1376
0
    }
1377
245
    s = pdev->strm;
1378
245
    stream_puts(s, "<< ");
1379
245
    if (pnode->action != NULL)
1380
245
        cos_dict_elements_write(pnode->action, pdev);
1381
245
    if (pnode->count)
1382
33
        pprintd1(s, "/Count %d ", pnode->count);
1383
245
    pprinti64d1(s, "/Parent %"PRId64" 0 R\n", pnode->parent_id);
1384
245
    if (pnode->prev_id)
1385
44
        pprinti64d1(s, "/Prev %"PRId64" 0 R\n", pnode->prev_id);
1386
245
    if (next_id)
1387
44
        pprinti64d1(s, "/Next %"PRId64" 0 R\n", next_id);
1388
245
    if (pnode->first_id)
1389
31
        pprinti64d2(s, "/First %"PRId64" 0 R /Last %"PRId64" 0 R\n",
1390
31
                  pnode->first_id, pnode->last_id);
1391
245
    stream_puts(s, ">>\n");
1392
245
    pdf_end_separate(pdev, resourceOutline);
1393
245
    if (pnode->action != NULL)
1394
245
        COS_FREE(pnode->action, "pdfmark_write_outline");
1395
245
    pnode->action = 0;
1396
245
    return code;
1397
245
}
1398
1399
/* Adjust the parent's count when writing an outline node. */
1400
static void
1401
pdfmark_adjust_parent_count(pdf_outline_level_t * plevel)
1402
58
{
1403
58
    pdf_outline_level_t *parent = plevel - 1;
1404
58
    int count = plevel->last.count;
1405
1406
58
    if (count > 0) {
1407
0
        if (parent->last.count < 0)
1408
0
            parent->last.count -= count;
1409
0
        else
1410
0
            parent->last.count += count;
1411
0
    }
1412
58
}
1413
1414
/*
1415
 * Close the current level of the outline tree.  Note that if we are at
1416
 * the end of the document, some of the levels may be incomplete if the
1417
 * Count values were incorrect.
1418
 */
1419
int
1420
pdfmark_close_outline(gx_device_pdf * pdev)
1421
203
{
1422
203
    int depth = pdev->outline_depth;
1423
203
    pdf_outline_level_t *plevel = &pdev->outline_levels[depth];
1424
203
    int code = 0;
1425
1426
203
    if (plevel->last.id) { /* check for incomplete tree */
1427
201
        code = pdfmark_write_outline(pdev, &plevel->last, 0);
1428
201
    }
1429
203
    if (depth > 0) {
1430
33
        plevel[-1].last.last_id = plevel->last.id;
1431
33
        pdfmark_adjust_parent_count(plevel);
1432
33
        --plevel;
1433
33
        if (plevel->last.count < 0)
1434
0
            pdev->closed_outline_depth--;
1435
33
        pdev->outline_depth--;
1436
33
    }
1437
203
    return code;
1438
203
}
1439
1440
/* OUT pdfmark */
1441
static int
1442
pdfmark_OUT(gx_device_pdf * pdev, gs_param_string * pairs, uint count,
1443
            const gs_matrix * pctm, const gs_param_string * no_objname)
1444
245
{
1445
245
    int depth = pdev->outline_depth;
1446
245
    pdf_outline_level_t *plevel = &pdev->outline_levels[depth];
1447
245
    int sub_count = 0;
1448
245
    uint i;
1449
245
    pdf_outline_node_t node;
1450
245
    ao_params_t ao;
1451
245
    int code;
1452
1453
1.21k
    for (i = 0; i < count; i += 2) {
1454
973
        const gs_param_string *pair = &pairs[i];
1455
1456
973
        if (pdf_key_eq(pair, "/Count"))
1457
245
            pdfmark_scan_int(pair + 1, &sub_count);
1458
973
    }
1459
245
    if (sub_count != 0 && depth == pdev->max_outline_depth - 1) {
1460
0
        pdf_outline_level_t *new_ptr;
1461
1462
0
        new_ptr = (pdf_outline_level_t *)gs_alloc_bytes(pdev->pdf_memory, (pdev->max_outline_depth + INITIAL_MAX_OUTLINE_DEPTH) * sizeof(pdf_outline_level_t) * sizeof(pdf_outline_level_t), "outline_levels array");
1463
0
        if (!new_ptr)
1464
0
            return_error(gs_error_VMerror);
1465
0
        memcpy (new_ptr, pdev->outline_levels, pdev->max_outline_depth * sizeof(pdf_outline_level_t));
1466
0
        gs_free_object(pdev->pdf_memory, pdev->outline_levels, "outline_levels array");
1467
0
        pdev->outline_levels = new_ptr;
1468
0
        pdev->max_outline_depth += INITIAL_MAX_OUTLINE_DEPTH;
1469
0
        plevel = &pdev->outline_levels[depth];
1470
0
    }
1471
245
    node.action = cos_dict_alloc(pdev, "pdfmark_OUT");
1472
245
    if (node.action == 0)
1473
0
        return_error(gs_error_VMerror);
1474
245
    ao.pdev = pdev;
1475
245
    ao.subtype = 0;
1476
245
    ao.src_pg = -1;
1477
245
    code = pdfmark_put_ao_pairs(pdev, node.action, pairs, count, pctm, &ao,
1478
245
                                true);
1479
245
    if (code < 0) {
1480
0
        cos_free((cos_object_t *)node.action, "pdfmark_OUT");
1481
0
        return code;
1482
0
    }
1483
245
    if (pdev->outlines_id == 0)
1484
170
        pdev->outlines_id = pdf_obj_ref(pdev);
1485
245
    node.id = pdf_obj_ref(pdev);
1486
245
    node.parent_id =
1487
245
        (depth == 0 ? pdev->outlines_id : plevel[-1].last.id);
1488
245
    node.prev_id = plevel->last.id;
1489
245
    node.first_id = node.last_id = 0;
1490
245
    node.count = sub_count;
1491
    /* Add this node to the outline at the current level. */
1492
245
    if (plevel->first.id == 0) { /* First node at this level. */
1493
201
        if (depth > 0)
1494
31
            plevel[-1].last.first_id = node.id;
1495
201
        node.prev_id = 0;
1496
201
        plevel->first = node;
1497
201
        plevel->first.action = 0; /* never used */
1498
201
    } else {     /* Write the previous node. */
1499
44
        if (depth > 0)
1500
25
            pdfmark_adjust_parent_count(plevel);
1501
44
        pdfmark_write_outline(pdev, &plevel->last, node.id);
1502
44
    }
1503
245
    plevel->last = node;
1504
245
    plevel->left--;
1505
245
    if (!pdev->closed_outline_depth)
1506
245
        pdev->outlines_open++;
1507
    /* If this node has sub-nodes, descend one level. */
1508
245
    if (sub_count != 0) {
1509
33
        pdev->outline_depth++;
1510
33
        ++plevel;
1511
33
        plevel->left = (sub_count > 0 ? sub_count : -sub_count);
1512
33
        plevel->first.id = 0;
1513
33
        plevel->last.count = plevel->last.id = 0;
1514
33
        plevel->first.action = plevel->last.action = 0; /* for GC */
1515
33
        if (sub_count < 0)
1516
0
            pdev->closed_outline_depth++;
1517
212
    } else {
1518
240
        while ((depth = pdev->outline_depth) > 0 &&
1519
56
               pdev->outline_levels[depth].left == 0
1520
212
            )
1521
28
            pdfmark_close_outline(pdev);
1522
212
    }
1523
245
    return 0;
1524
245
}
1525
1526
/* Write an article bead. */
1527
static int
1528
pdfmark_write_bead(gx_device_pdf * pdev, const pdf_bead_t * pbead)
1529
0
{
1530
0
    stream *s;
1531
0
    char rstr[MAX_RECT_STRING];
1532
1533
0
    pdf_open_separate(pdev, pbead->id, resourceArticle);
1534
0
    s = pdev->strm;
1535
0
    pprinti64d3(s, "<</T %"PRId64" 0 R/V %"PRId64" 0 R/N %"PRId64" 0 R",
1536
0
              pbead->article_id, pbead->prev_id, pbead->next_id);
1537
0
    if (pbead->page_id != 0)
1538
0
        pprinti64d1(s, "/P %"PRId64" 0 R", pbead->page_id);
1539
0
    pdfmark_make_rect(rstr, &pbead->rect);
1540
0
    pprints1(s, "/R%s>>\n", rstr);
1541
0
    return pdf_end_separate(pdev, resourceArticle);
1542
0
}
1543
1544
/* Finish writing an article, and release its data. */
1545
int
1546
pdfmark_write_article(gx_device_pdf * pdev, const pdf_article_t * part)
1547
0
{
1548
0
    pdf_article_t art;
1549
0
    stream *s;
1550
1551
0
    art = *part;
1552
0
    if (art.last.id == 0) {
1553
        /* Only one bead in the article. */
1554
0
        art.first.prev_id = art.first.next_id = art.first.id;
1555
0
    } else {
1556
        /* More than one bead in the article. */
1557
0
        art.first.prev_id = art.last.id;
1558
0
        art.last.next_id = art.first.id;
1559
0
        pdfmark_write_bead(pdev, &art.last);
1560
0
    }
1561
0
    pdfmark_write_bead(pdev, &art.first);
1562
0
    pdf_open_separate(pdev, art.contents->id, resourceArticle);
1563
0
    s = pdev->strm;
1564
0
    pprinti64d1(s, "<</F %"PRId64" 0 R/I<<", art.first.id);
1565
0
    cos_dict_elements_write(art.contents, pdev);
1566
0
    stream_puts(s, ">> >>\n");
1567
0
    return pdf_end_separate(pdev, resourceArticle);
1568
0
}
1569
1570
/* ARTICLE pdfmark */
1571
static int
1572
pdfmark_ARTICLE(gx_device_pdf * pdev, gs_param_string * pairs, uint count,
1573
                const gs_matrix * pctm, const gs_param_string * no_objname)
1574
0
{
1575
0
    gs_memory_t *mem = pdev->pdf_memory;
1576
0
    gs_param_string title;
1577
0
    gs_param_string rectstr;
1578
0
    gs_rect rect;
1579
0
    int64_t bead_id;
1580
0
    pdf_article_t *part;
1581
0
    int code;
1582
1583
0
    if (!pdfmark_find_key("/Title", pairs, count, &title) ||
1584
0
        !pdfmark_find_key("/Rect", pairs, count, &rectstr)
1585
0
        )
1586
0
        return_error(gs_error_rangecheck);
1587
0
    if ((code = pdfmark_scan_rect(&rect, &rectstr, pctm)) < 0)
1588
0
        return code;
1589
0
    bead_id = pdf_obj_ref(pdev);
1590
1591
    /* Find the article with this title, or create one. */
1592
0
    for (part = pdev->articles; part != 0; part = part->next) {
1593
0
        const cos_value_t *a_title =
1594
0
            cos_dict_find_c_key(part->contents, "/Title");
1595
1596
0
        if (a_title != 0 && !COS_VALUE_IS_OBJECT(a_title) &&
1597
0
            !bytes_compare(a_title->contents.chars.data,
1598
0
                           a_title->contents.chars.size,
1599
0
                           title.data, title.size))
1600
0
            break;
1601
0
    }
1602
0
    if (part == 0) {   /* Create the article. */
1603
0
        cos_dict_t *contents =
1604
0
            cos_dict_alloc(pdev, "pdfmark_ARTICLE(contents)");
1605
1606
0
        if (contents == 0)
1607
0
            return_error(gs_error_VMerror);
1608
0
        part = gs_alloc_struct(mem, pdf_article_t, &st_pdf_article,
1609
0
                               "pdfmark_ARTICLE(article)");
1610
0
        if (part == 0 || contents == 0) {
1611
0
            gs_free_object(mem, part, "pdfmark_ARTICLE(article)");
1612
0
            if (contents)
1613
0
                COS_FREE(contents, "pdfmark_ARTICLE(contents)");
1614
0
            return_error(gs_error_VMerror);
1615
0
        }
1616
0
        contents->id = pdf_obj_ref(pdev);
1617
0
        part->next = pdev->articles;
1618
0
        pdev->articles = part;
1619
0
        cos_dict_put_string(contents, (const byte *)"/Title", 6,
1620
0
                            title.data, title.size);
1621
0
        part->first.id = part->last.id = 0;
1622
0
        part->contents = contents;
1623
0
    }
1624
    /*
1625
     * Add the bead to the article.  This is similar to what we do for
1626
     * outline nodes, except that articles have only a page number and
1627
     * not View/Dest.
1628
     */
1629
0
    if (part->last.id == 0) {
1630
0
        part->first.next_id = bead_id;
1631
0
        part->last.id = part->first.id;
1632
0
    } else {
1633
0
        part->last.next_id = bead_id;
1634
0
        pdfmark_write_bead(pdev, &part->last);
1635
0
    }
1636
0
    part->last.prev_id = part->last.id;
1637
0
    part->last.id = bead_id;
1638
0
    part->last.article_id = part->contents->id;
1639
0
    part->last.next_id = 0;
1640
0
    part->last.rect = rect;
1641
0
    {
1642
0
        gs_param_string page_string;
1643
0
        int page = 0;
1644
0
        uint i;
1645
1646
0
        pdfmark_find_key("/Page", pairs, count, &page_string);
1647
0
        page = pdfmark_page_number(pdev, &page_string);
1648
0
        code = update_max_page_reference(pdev, &page);
1649
0
        if (code < 0)
1650
0
            return code;
1651
0
        part->last.page_id = pdf_page_id(pdev, page);
1652
0
        for (i = 0; i < count; i += 2) {
1653
0
            if (pdf_key_eq(&pairs[i], "/Rect") || pdf_key_eq(&pairs[i], "/Page"))
1654
0
                continue;
1655
0
            pdfmark_put_pair(part->contents, &pairs[i]);
1656
0
        }
1657
0
    }
1658
0
    if (part->first.id == 0) { /* This is the first bead of the article. */
1659
0
        part->first = part->last;
1660
0
        part->last.id = 0;
1661
0
    }
1662
0
    return 0;
1663
0
}
1664
1665
/* EMBED pdfmark */
1666
static int
1667
pdfmark_EMBED(gx_device_pdf * pdev, gs_param_string * pairs, uint count,
1668
             const gs_matrix * pctm, const gs_param_string * objname)
1669
17
{
1670
17
    gs_param_string key;
1671
17
    int i;
1672
1673
17
    if (pdev->CompatibilityLevel < 1.4)
1674
0
        return_error(gs_error_undefined);
1675
17
    if (pdev->PDFA > 0 && pdev->PDFA < 2) {
1676
0
        switch(pdev->PDFACompatibilityPolicy) {
1677
0
            default:
1678
0
            case 0:
1679
0
                emprintf(pdev->memory,
1680
0
                "The PDF/A-1 specifcation prohibits the embedding of files, reverting to normal PDF output.\n");
1681
0
                pdev->AbortPDFAX = true;
1682
0
                pdev->PDFA = 0;
1683
0
                return 0;
1684
0
            case 1:
1685
0
                emprintf(pdev->memory,
1686
0
                "The PDF/A-1 specifcation prohibits the embedding of files, pdfamrk operatoin ignored.\n");
1687
0
                break;
1688
0
            case 2:
1689
0
                return_error(gs_error_undefined);
1690
0
        }
1691
0
    }
1692
17
    if (pdev->PDFA > 0 && pdev->PDFA < 3) {
1693
0
        emprintf(pdev->memory,
1694
0
        "The PDF/A-2 specifcation only permits the embedding of PDF/A-1 or PDF/A-2 files.\n");
1695
0
        emprintf(pdev->memory,
1696
0
        "The pdfwrite device has not validated this embedded file, output may not conform to PDF/A-2.\n");
1697
0
    }
1698
17
    if (!pdfmark_find_key("/FS", pairs, count, &key))
1699
0
        return_error(gs_error_rangecheck);
1700
17
    if (!pdfmark_find_key("/Name", pairs, count, &key))
1701
0
        return_error(gs_error_rangecheck);
1702
17
    if (!pdev->EmbeddedFiles) {
1703
17
        pdev->EmbeddedFiles = cos_dict_alloc(pdev, "pdfmark_EMBED(EmbeddedFiles)");
1704
17
        if (pdev->EmbeddedFiles == 0)
1705
0
            return_error(gs_error_VMerror);
1706
17
        pdev->EmbeddedFiles->id = pdf_obj_ref(pdev);
1707
17
    }
1708
17
    if (pdev->PDFA == 3 && !pdev->AF && !cos_dict_find_c_key(pdev->Catalog, "/AF")) {
1709
0
        pdev->AF = cos_array_alloc(pdev, "pdfmark_EMBED(EmbeddedFiles)");
1710
0
        if (pdev->AF == 0)
1711
0
            return_error(gs_error_VMerror);
1712
0
        pdev->AF->id = pdf_obj_ref(pdev);
1713
0
    }
1714
1715
34
    for (i = 0; i < count; i += 2) {
1716
34
        if (pdf_key_eq(&pairs[i], "/FS")) {
1717
17
            if (pdev->PDFA == 3 && !cos_dict_find_c_key(pdev->Catalog, "/AF")) {
1718
0
                uint written;
1719
0
                cos_value_t v;
1720
0
                cos_object_t *object;
1721
0
                int64_t id;
1722
0
                int code;
1723
0
                char *p = (char *)pairs[i+1].data;
1724
1725
                /* Skip past white space */
1726
0
                while (*p == ' ' || *p == '\t' || *p == '\r' || *p == '\n')
1727
0
                    p++;
1728
1729
                /* If we have a dictionary, then we assume this is a 'normal' /EMBED pdfmark */
1730
0
                if (*p == '<') {
1731
                    /* Write the dictionary to an object in the PDF file, and note the id we are
1732
                     * using. We need that below to write references to the object from the /EmbeddedFiles
1733
                     * entry in the Catalog dictionary and the /AF array, also in the Catalog dictionary.
1734
                     */
1735
0
                    id = pdf_obj_ref(pdev);
1736
1737
0
                    pdf_open_separate(pdev, id, resourceNone);
1738
0
                    sputs(pdev->strm, pairs[i+1].data, pairs[i+1].size, &written);
1739
0
                    if (pairs[i+1].data[pairs[i+1].size] != 0x0d && pdev->PDFA != 0)
1740
0
                        sputc(pdev->strm, 0x0d);
1741
0
                    pdf_end_separate(pdev, resourceNone);
1742
0
                } else {
1743
                    /* Otherwise, try to determine if we have an indirect reference to an existing FileSpec dictionary
1744
                     * If we get what looks like an indirect reference, try using the id for the /EmbeddedFiles
1745
                     * and /AF entries
1746
                     */
1747
0
                    if (sscanf(p, "%"PRId64" 0 R", &id) == 0)
1748
0
                        return_error(gs_error_syntaxerror);
1749
0
                }
1750
1751
0
                object = cos_reference_alloc(pdev, "embedded file");
1752
0
                if (object == NULL)
1753
0
                    return_error(gs_error_VMerror);
1754
0
                object->id = id;
1755
0
                COS_OBJECT_VALUE(&v, object);
1756
0
                code = cos_dict_put(pdev->EmbeddedFiles, key.data, key.size, &v);
1757
0
                if (code < 0)
1758
0
                    return code;
1759
1760
0
                object = cos_reference_alloc(pdev, "embedded file");
1761
0
                if (object == NULL)
1762
0
                    return_error(gs_error_VMerror);
1763
0
                object->id = id;
1764
0
                COS_OBJECT_VALUE(&v, object);
1765
0
                code = cos_array_add(pdev->AF, &v);
1766
0
                if (code < 0)
1767
0
                    return code;
1768
0
            }
1769
17
            else
1770
17
                return cos_dict_put_string(pdev->EmbeddedFiles, key.data, key.size, pairs[i+1].data, pairs[i+1].size);
1771
17
        }
1772
34
    }
1773
0
    return 0;
1774
17
}
1775
1776
/* DEST pdfmark */
1777
static int
1778
pdfmark_DEST(gx_device_pdf * pdev, gs_param_string * pairs, uint count,
1779
             const gs_matrix * pctm, const gs_param_string * objname)
1780
0
{
1781
0
    int present;
1782
0
    char dest[MAX_DEST_STRING];
1783
0
    gs_param_string key;
1784
0
    cos_value_t value;
1785
0
    cos_dict_t *ddict;
1786
0
    int i, code;
1787
1788
0
    if (!pdfmark_find_key("/Dest", pairs, count, &key) ||
1789
0
        (present =
1790
0
         pdfmark_make_dest(dest, pdev, "/Page", "/View", pairs, count, 1)) < 0
1791
0
        )
1792
0
        return_error(gs_error_rangecheck);
1793
0
    cos_string_value(&value, (byte *)dest, strlen(dest));
1794
0
    if (!pdev->Dests) {
1795
0
        pdev->Dests = cos_dict_alloc(pdev, "pdfmark_DEST(Dests)");
1796
0
        if (pdev->Dests == 0)
1797
0
            return_error(gs_error_VMerror);
1798
0
        pdev->Dests->id = pdf_obj_ref(pdev);
1799
0
    }
1800
1801
    /*
1802
     * Create the destination as a dictionary with a D key.
1803
     */
1804
0
    code = pdf_make_named_dict(pdev, objname, &ddict, false);
1805
0
    ddict->id = pdf_obj_ref(pdev);
1806
1807
0
    if (code < 0)
1808
0
        return code;
1809
0
    code = cos_dict_put_c_key_string(ddict, "/D", (byte *)dest,
1810
0
                                     strlen(dest));
1811
0
    for (i = 0; code >= 0 && i < count; i += 2)
1812
0
        if (!pdf_key_eq(&pairs[i], "/Dest") &&
1813
0
            !pdf_key_eq(&pairs[i], "/Page") &&
1814
0
            !pdf_key_eq(&pairs[i], "/View")
1815
0
            )
1816
0
            code = pdfmark_put_pair(ddict, &pairs[i]);
1817
0
    if (code < 0)
1818
0
        return code;
1819
0
    COS_WRITE_OBJECT(ddict, pdev, resourceOther);
1820
0
    COS_OBJECT_VALUE(&value, ddict);
1821
0
    COS_RELEASE(ddict, "pdfmark_DEST(Dests dict)");
1822
1823
0
    return cos_dict_put(pdev->Dests, key.data, key.size, &value);
1824
0
}
1825
1826
/* Check that pass-through PostScript code is a string. */
1827
static bool
1828
ps_source_ok(const gs_memory_t *mem, const gs_param_string * psource)
1829
0
{
1830
0
    if (psource->size >= 2 && psource->data[0] == '(' &&
1831
0
        psource->data[psource->size - 1] == ')'
1832
0
        )
1833
0
        return true;
1834
0
    else {
1835
0
        int i;
1836
0
        lprintf("bad PS passthrough: ");
1837
0
        for (i=0; i<psource->size; i++)
1838
0
            errprintf(mem, "%c", psource->data[i]);
1839
0
        errprintf(mem, "\n");
1840
0
        return false;
1841
0
    }
1842
0
}
1843
1844
/* Write the contents of pass-through PostScript code. */
1845
/* Return the size written on the file. */
1846
static uint
1847
pdfmark_write_ps(stream *s, const gs_param_string * psource)
1848
0
{
1849
    /****** REMOVE ESCAPES WITH PSSDecode, SEE gdevpdfr p. 2 ******/
1850
0
    uint size = psource->size - 2;
1851
1852
0
    stream_write(s, psource->data + 1, size);
1853
0
    stream_putc(s, '\n');
1854
0
    return size + 1;
1855
0
}
1856
1857
/* Start a XObject. */
1858
static int
1859
start_XObject(gx_device_pdf * pdev, bool compress, cos_stream_t **ppcs)
1860
0
{   pdf_resource_t *pres;
1861
0
    cos_stream_t *pcs;
1862
0
    int code;
1863
1864
0
    code = pdf_open_page(pdev, PDF_IN_STREAM);
1865
0
    if (code < 0)
1866
0
        return code;
1867
0
    code = pdf_enter_substream(pdev, resourceXObject, gs_no_id, &pres, false,
1868
0
                pdev->CompressStreams);
1869
0
    if (code < 0)
1870
0
        return code;
1871
0
    pdev->accumulating_a_global_object = true;
1872
0
    pcs = (cos_stream_t *)pres->object;
1873
0
    pdev->substream_Resources = cos_dict_alloc(pdev, "start_XObject");
1874
0
    if (!pdev->substream_Resources)
1875
0
        return_error(gs_error_VMerror);
1876
0
    if (pdev->ForOPDFRead) {
1877
0
        code = cos_dict_put_c_key_bool((cos_dict_t *)pres->object, "/.Global", true);
1878
0
        if (code < 0)
1879
0
            return code;
1880
0
    }
1881
0
    pres->named = true;
1882
0
    pres->where_used = 0; /* initially not used */
1883
0
    pcs->pres = pres;
1884
0
    *ppcs = pcs;
1885
0
    return 0;
1886
0
}
1887
1888
/* PS pdfmark */
1889
0
#define MAX_PS_INLINE 100
1890
static int
1891
pdfmark_PS(gx_device_pdf * pdev, gs_param_string * pairs, uint count,
1892
           const gs_matrix * pctm, const gs_param_string * objname)
1893
0
{
1894
0
    gs_param_string source;
1895
0
    gs_param_string level1;
1896
1897
0
    if (!pdfmark_find_key("/DataSource", pairs, count, &source) ||
1898
0
        !ps_source_ok(pdev->memory, &source) ||
1899
0
        (pdfmark_find_key("/Level1", pairs, count, &level1) &&
1900
0
         !ps_source_ok(pdev->memory, &level1))
1901
0
        )
1902
0
        return_error(gs_error_rangecheck);
1903
0
    if (level1.data == 0 && source.size <= MAX_PS_INLINE && objname == 0) {
1904
        /* Insert the PostScript code in-line */
1905
0
        int code = pdf_open_contents(pdev, PDF_IN_STREAM);
1906
0
        stream *s;
1907
1908
0
        if (code < 0)
1909
0
            return code;
1910
0
        s = pdev->strm;
1911
0
        stream_write(s, source.data, source.size);
1912
0
        stream_puts(s, " PS\n");
1913
0
    } else {
1914
        /* Put the PostScript code in a resource. */
1915
0
        cos_stream_t *pcs;
1916
0
        int code;
1917
0
        gs_id level1_id = gs_no_id;
1918
0
        pdf_resource_t *pres;
1919
1920
0
        if (level1.data != 0) {
1921
0
            pdf_resource_t *pres;
1922
1923
0
            code = pdf_enter_substream(pdev,
1924
0
                        resourceXObject,
1925
0
                        gs_no_id, &pres, true,
1926
0
                        pdev->CompressStreams);
1927
0
            if (code < 0)
1928
0
                return code;
1929
0
            pcs = (cos_stream_t *)pres->object;
1930
0
            if (pdev->ForOPDFRead && objname != 0) {
1931
0
                code = cos_dict_put_c_key_bool((cos_dict_t *)pres->object, "/.Global", true);
1932
0
                if (code < 0)
1933
0
                    return code;
1934
0
            }
1935
0
            pres->named = (objname != 0);
1936
0
            pres->where_used = 0;
1937
0
            pcs->pres = pres;
1938
0
            DISCARD(pdfmark_write_ps(pdev->strm, &level1));
1939
0
            code = pdf_exit_substream(pdev);
1940
0
            if (code < 0)
1941
0
                return code;
1942
0
            code = cos_write_object(pres->object, pdev, resourceOther);
1943
0
            if (code < 0)
1944
0
                return code;
1945
0
            level1_id = pres->object->id;
1946
0
        }
1947
0
        code = start_XObject(pdev, pdev->params.CompressPages, &pcs);
1948
0
        if (code < 0)
1949
0
            return code;
1950
0
        pres = pdev->accumulating_substream_resource;
1951
0
        if (pres == NULL)
1952
0
            return_error(gs_error_unregistered);
1953
0
        code = cos_stream_put_c_strings(pcs, "/Type", "/XObject");
1954
0
        if (code < 0)
1955
0
            return code;
1956
0
        code = cos_stream_put_c_strings(pcs, "/Subtype", "/PS");
1957
0
        if (code < 0)
1958
0
            return code;
1959
0
        if (level1_id != gs_no_id) {
1960
0
            char r[MAX_DEST_STRING];
1961
1962
0
            gs_snprintf(r, sizeof(r), "%ld 0 R", level1_id);
1963
0
            code = cos_dict_put_c_key_string(cos_stream_dict(pcs), "/Level1",
1964
0
                                             (byte *)r, strlen(r));
1965
0
            if (code < 0)
1966
0
                return code;
1967
0
        }
1968
0
        DISCARD(pdfmark_write_ps(pdev->strm, &source));
1969
0
        code = pdf_exit_substream(pdev);
1970
0
        if (code < 0)
1971
0
            return code;
1972
0
        {   gs_const_string objname1, *pon = NULL;
1973
1974
0
            if (objname != NULL) {
1975
0
                objname1.data = objname->data;
1976
0
                objname1.size = objname->size;
1977
0
                pon = &objname1;
1978
0
            }
1979
0
            code = pdfmark_bind_named_object(pdev, pon, &pres);
1980
0
            if (code < 0)
1981
0
                return code;
1982
0
        }
1983
0
        code = pdf_open_contents(pdev, PDF_IN_STREAM);
1984
0
        if (code < 0)
1985
0
            return code;
1986
0
        pcs->pres->where_used |= pdev->used_mask;
1987
0
        pprinti64d1(pdev->strm, "/R%"PRId64" Do\n", pcs->id);
1988
0
    }
1989
0
    return 0;
1990
0
}
1991
1992
/* Common code for pdfmarks that do PUT into a specific object. */
1993
static int
1994
pdfmark_put_pairs(cos_dict_t *pcd, gs_param_string * pairs, uint count)
1995
7.66k
{
1996
7.66k
    int code = 0, i;
1997
1998
7.66k
    if (count & 1)
1999
0
        return_error(gs_error_rangecheck);
2000
13.1k
    for (i = 0; code >= 0 && i < count; i += 2)
2001
5.46k
        code = pdfmark_put_pair(pcd, pairs + i);
2002
7.66k
    return code;
2003
7.66k
}
2004
2005
/* PAGES pdfmark */
2006
static int
2007
pdfmark_PAGES(gx_device_pdf * pdev, gs_param_string * pairs, uint count,
2008
              const gs_matrix * pctm, const gs_param_string * no_objname)
2009
0
{
2010
0
    return pdfmark_put_pairs(pdev->Pages, pairs, count);
2011
0
}
2012
2013
/* PAGE pdfmark */
2014
static int
2015
pdfmark_PAGE(gx_device_pdf * pdev, gs_param_string * pairs, uint count,
2016
             const gs_matrix * pctm, const gs_param_string * no_objname)
2017
6.14k
{
2018
6.14k
    return pdfmark_put_pairs(pdf_current_page_dict(pdev), pairs, count);
2019
6.14k
}
2020
2021
/* Add a page label for the current page. The last label on a page
2022
 * overrides all previous labels for this page. Unlabeled pages will get
2023
 * empty page labels. label == NULL flushes the last label */
2024
static int
2025
pdfmark_add_pagelabel(gx_device_pdf * pdev, const gs_param_string *label)
2026
9.15k
{
2027
9.15k
    cos_value_t value;
2028
9.15k
    cos_dict_t *dict = 0;
2029
9.15k
    int code = 0;
2030
2031
    /* create label dict (and page label array if not present yet) */
2032
9.15k
    if (label != 0) {
2033
0
        if (!pdev->PageLabels) {
2034
0
            pdev->PageLabels = cos_array_alloc(pdev,
2035
0
                    "pdfmark_add_pagelabel(PageLabels)");
2036
0
            if (pdev->PageLabels == 0)
2037
0
                return_error(gs_error_VMerror);
2038
0
            pdev->PageLabels->id = pdf_obj_ref(pdev);
2039
2040
            /* empty label for unlabled pages before first labled page */
2041
0
            pdev->PageLabels_current_page = 0;
2042
0
            pdev->PageLabels_current_label = cos_dict_alloc(pdev,
2043
0
                                           "pdfmark_add_pagelabel(first)");
2044
0
            if (pdev->PageLabels_current_label == 0)
2045
0
                return_error(gs_error_VMerror);
2046
0
        }
2047
2048
0
        dict = cos_dict_alloc(pdev, "pdfmark_add_pagelabel(dict)");
2049
0
        if (dict == 0)
2050
0
            return_error(gs_error_VMerror);
2051
2052
0
        code = cos_dict_put_c_key(dict, "/P", cos_string_value(&value,
2053
0
            label->data, label->size));
2054
0
        if (code < 0) {
2055
0
            COS_FREE(dict, "pdfmark_add_pagelabel(dict)");
2056
0
            return code;
2057
0
        }
2058
0
    }
2059
2060
    /* flush current label */
2061
9.15k
    if (label == 0 || pdev->next_page != pdev->PageLabels_current_page) {
2062
        /* handle current label */
2063
9.15k
        if (pdev->PageLabels_current_label) {
2064
0
            if (code >= 0) {
2065
0
                code = cos_array_add_int(pdev->PageLabels,
2066
0
                        pdev->PageLabels_current_page);
2067
0
                if (code >= 0)
2068
0
                    code = cos_array_add(pdev->PageLabels,
2069
0
                            COS_OBJECT_VALUE(&value,
2070
0
                                pdev->PageLabels_current_label));
2071
0
            }
2072
0
            pdev->PageLabels_current_label = 0;
2073
0
        }
2074
2075
        /* handle unlabled pages between current labeled page and
2076
         * next labeled page */
2077
9.15k
        if (pdev->PageLabels) {
2078
0
            if (pdev->next_page - pdev->PageLabels_current_page > 1) {
2079
0
                cos_dict_t *tmp = cos_dict_alloc(pdev,
2080
0
                        "pdfmark_add_pagelabel(tmp)");
2081
0
                if (tmp == 0)
2082
0
                    return_error(gs_error_VMerror);
2083
2084
0
                code = cos_array_add_int(pdev->PageLabels,
2085
0
                        pdev->PageLabels_current_page + 1);
2086
0
                if (code >= 0)
2087
0
                    code = cos_array_add(pdev->PageLabels,
2088
0
                            COS_OBJECT_VALUE(&value, tmp));
2089
0
            }
2090
0
        }
2091
9.15k
    }
2092
2093
    /* new current label */
2094
9.15k
    if (pdev->PageLabels_current_label)
2095
0
        COS_FREE(pdev->PageLabels_current_label,
2096
9.15k
                "pdfmark_add_pagelabel(current_label)");
2097
9.15k
    pdev->PageLabels_current_label = dict;
2098
9.15k
    pdev->PageLabels_current_page = pdev->next_page;
2099
2100
9.15k
    return code;
2101
9.15k
}
2102
2103
/* Close the pagelabel numtree.*/
2104
int
2105
pdfmark_end_pagelabels(gx_device_pdf * pdev)
2106
9.15k
{
2107
9.15k
    return pdfmark_add_pagelabel(pdev, 0);
2108
9.15k
}
2109
2110
/* [ /Label string /PlateColor string pdfmark */
2111
/* FIXME: /PlateColor is ignored */
2112
static int
2113
pdfmark_PAGELABEL(gx_device_pdf * pdev, gs_param_string * pairs, uint count,
2114
             const gs_matrix * pctm, const gs_param_string * no_objname)
2115
0
{
2116
0
    gs_param_string key;
2117
2118
0
    if (pdev->CompatibilityLevel >= 1.3) {
2119
0
        if (pdfmark_find_key("/Label", pairs, count, &key)) {
2120
0
            return pdfmark_add_pagelabel(pdev, &key);
2121
0
        }
2122
0
    }
2123
0
    return 0;
2124
0
}
2125
2126
static int is_XMP_Key(const gs_param_string *param)
2127
0
{
2128
0
    if (pdf_key_eq(param, "/Title"))
2129
0
        return 1;
2130
0
    if (pdf_key_eq(param, "/Author"))
2131
0
        return 1;
2132
0
    if (pdf_key_eq(param, "/Subject"))
2133
0
        return 1;
2134
0
    if (pdf_key_eq(param, "/Keywords"))
2135
0
        return 1;
2136
0
    if (pdf_key_eq(param, "/Creator"))
2137
0
        return 1;
2138
0
    if (pdf_key_eq(param, "/Producer"))
2139
0
        return 1;
2140
    /* These two aren't string data types and so won't affect anything
2141
     * in the DOCINFO pdfmark, which is the only client for this code currently
2142
     * but we may want to use this in future for other purposed.
2143
     */
2144
0
    if (pdf_key_eq(param, "/CreationDate"))
2145
0
        return 1;
2146
0
    if (pdf_key_eq(param, "/ModDate"))
2147
0
        return 1;
2148
0
    return 0;
2149
0
}
2150
2151
/* DOCINFO pdfmark */
2152
static int
2153
pdfmark_DOCINFO(gx_device_pdf * pdev, gs_param_string * pairs, uint count,
2154
                const gs_matrix * pctm, const gs_param_string * no_objname)
2155
1.18k
{
2156
    /*
2157
     * We could use pdfmark_put_pairs(pdev->Info, pairs, count), except
2158
     * that we want to replace "Distiller" with our own name as the
2159
     * Producer.
2160
     */
2161
1.18k
    cos_dict_t *const pcd = pdev->Info;
2162
1.18k
    int code = 0, i;
2163
2164
1.18k
    if (count & 1)
2165
0
        return_error(gs_error_rangecheck);
2166
3.32k
    for (i = 0; code >= 0 && i < count; i += 2) {
2167
2.13k
        const gs_param_string *pair = pairs + i;
2168
2169
2.13k
        if (pdev->CompatibilityLevel >= 2.0) {
2170
0
            if (!pdf_key_eq(pairs + i, "/ModDate") && !pdf_key_eq(pairs + i, "/CreationDate"))
2171
0
                continue;
2172
0
        }
2173
2174
2.13k
        if (pdev->PDFA !=0 && is_XMP_Key(pair)) {
2175
0
            const gs_param_string *p = pairs + i + 1;
2176
0
            bool abort = false;
2177
2178
            /* Ensure that we can write the XMP string. If not, handle this according to PDFACompatibilityPolicy */
2179
0
            code = pdf_xmp_write_translated(pdev, NULL, p->data + 1, p->size - 2, NULL);
2180
0
            abort = code < 0;
2181
2182
0
            if (abort == true)
2183
0
            {
2184
                /* Can't handle UTF16BE in PDF/A1, so abort this pair or abort PDF/A or just abort,
2185
                 * depending on PDFACompatibilityPolicy
2186
                 */
2187
0
                switch (pdev->PDFACompatibilityPolicy) {
2188
0
                    case 0:
2189
0
                        emprintf(pdev->memory,
2190
0
                         "Text string detected in DOCINFO cannot be represented in XMP for PDF/A1, reverting to normal PDF output\n");
2191
0
                        pdev->AbortPDFAX = true;
2192
0
                        pdev->PDFA = 0;
2193
0
                        break;
2194
0
                    case 1:
2195
0
                        emprintf(pdev->memory,
2196
0
                         "Text string detected in DOCINFO cannot be represented in XMP for PDF/A1, discarding DOCINFO\n");
2197
0
                        continue;
2198
0
                        break;
2199
0
                    case 2:
2200
0
                        emprintf(pdev->memory,
2201
0
                         "Text string detected in DOCINFO cannot be represented in XMP for PDF/A1, aborting conversion.\n");
2202
                        /* If we don't return a fatal error then zputdeviceparams simply ignores it (!) */
2203
0
                        return_error(gs_error_Fatal);
2204
0
                        break;
2205
0
                    default:
2206
0
                        break;
2207
0
                }
2208
0
            }
2209
0
        }
2210
2.13k
        if (pdf_key_eq(pairs + i, "/Producer")) {
2211
            /* Slightly screwy - separating the G, P and L characters this way to avoid
2212
             * accidental replacement by the release script.
2213
             */
2214
            /* This comment, the delimiting comments below, and the lines of
2215
             * source code between the delimiting comments may not be altered,
2216
             * replaced, changed, or otherwise modified for the purpose of
2217
             * misrepresenting the origin of works generated using this
2218
             * software.
2219
             *
2220
             * The supplemental term above has been added in accordance with
2221
             * Section 7(b) of the Affero General Public License version 3.
2222
             */
2223
2224
            /*  BEGIN -- CHANGES RESTRICTED UNDER 7(b) */
2225
0
            const byte gs_g_p_l_prod_fam[16] = {'G', 'P', 'L', ' ', 'G', 'h', 'o', 's', 't', 's', 'c', 'r', 'i', 'p', 't', '\0'};
2226
0
            string_match_params params;
2227
0
            params = string_match_params_default;
2228
0
            params.ignore_case = true;
2229
2230
0
            if (!string_match((const byte *)GS_PRODUCTFAMILY, strlen(GS_PRODUCTFAMILY), gs_g_p_l_prod_fam, 15, &params))
2231
0
                code = pdfmark_put_pair(pcd, pair);
2232
            /*  END -- CHANGES RESTRICTED UNDER 7(b) */
2233
0
        } else
2234
2.13k
            code = pdfmark_put_pair(pcd, pair);
2235
2.13k
        if (code < 0)
2236
0
            break;
2237
2.13k
    }
2238
1.18k
    return code;
2239
1.18k
}
2240
2241
/* DOCVIEW pdfmark */
2242
static int
2243
pdfmark_DOCVIEW(gx_device_pdf * pdev, gs_param_string * pairs, uint count,
2244
                const gs_matrix * pctm, const gs_param_string * no_objname)
2245
704
{
2246
704
    char dest[MAX_DEST_STRING];
2247
704
    int code = 0;
2248
2249
704
    if (count & 1)
2250
0
        return_error(gs_error_rangecheck);
2251
704
    code = pdfmark_make_dest(dest, pdev, "/Page", "/View", pairs, count, 0);
2252
704
    if (code < 0)
2253
0
        return gs_note_error(gs_error_rangecheck);
2254
2255
704
    if (code > 0) {
2256
163
        int i;
2257
2258
163
        code = cos_dict_put_c_key_string(pdev->Catalog, "/OpenAction",
2259
163
                                         (byte *)dest, strlen(dest));
2260
489
        for (i = 0; code >= 0 && i < count; i += 2)
2261
326
            if (!(pdf_key_eq(&pairs[i], "/Page") ||
2262
163
                  pdf_key_eq(&pairs[i], "/View"))
2263
326
                )
2264
0
                code = pdfmark_put_pair(pdev->Catalog, pairs + i);
2265
163
        return code;
2266
163
    } else
2267
541
        return pdfmark_put_pairs(pdev->Catalog, pairs, count);
2268
704
}
2269
2270
/* ---------------- Named object pdfmarks ---------------- */
2271
2272
/* [ /BBox [llx lly urx ury] /_objdef {obj} /BP pdfmark */
2273
static int
2274
pdfmark_BP(gx_device_pdf * pdev, gs_param_string * pairs, uint count,
2275
           const gs_matrix * pctm, const gs_param_string * objname)
2276
0
{
2277
0
    gs_rect bbox;
2278
0
    cos_stream_t *pcs;
2279
0
    int code;
2280
0
    gs_matrix ictm;
2281
0
    byte bbox_str[6 + 6 * 15], matrix_str[6 + 6 * 15];
2282
0
    char chars[MAX_RECT_STRING + 1];
2283
0
    int bbox_str_len, matrix_str_len;
2284
0
    stream s;
2285
2286
0
    if (objname == 0 || count != 2 || !pdf_key_eq(&pairs[0], "/BBox"))
2287
0
        return_error(gs_error_rangecheck);
2288
0
    code = gs_matrix_invert(pctm, &ictm);
2289
0
    if (code < 0)
2290
0
        return code;
2291
0
    if (pairs[1].size > MAX_RECT_STRING)
2292
0
        return_error(gs_error_limitcheck);
2293
0
    memcpy(chars, pairs[1].data, pairs[1].size);
2294
0
    chars[pairs[1].size] = 0;
2295
0
    if (sscanf(chars, "[%lg %lg %lg %lg]",
2296
0
               &bbox.p.x, &bbox.p.y, &bbox.q.x, &bbox.q.y) != 4)
2297
0
        return_error(gs_error_rangecheck);
2298
0
    if ((pdev->used_mask << 1) == 0)
2299
0
        return_error(gs_error_limitcheck);
2300
0
    code = start_XObject(pdev, pdev->params.CompressPages, &pcs);
2301
0
    if (code < 0)
2302
0
        return code;
2303
0
    { byte *s = gs_alloc_string(pdev->memory, objname->size, "pdfmark_PS");
2304
2305
0
        if (s == NULL)
2306
0
            return_error(gs_error_VMerror);
2307
0
        memcpy(s, objname->data, objname->size);
2308
0
        pdev->objname.data = s;
2309
0
        pdev->objname.size = objname->size;
2310
0
    }
2311
0
    pcs->is_graphics = true;
2312
0
    gs_bbox_transform(&bbox, pctm, &bbox);
2313
0
    s_init(&s, NULL);
2314
0
    swrite_string(&s, bbox_str, sizeof(bbox_str));
2315
0
    pprintg4(&s, "[%g %g %g %g]",
2316
0
            bbox.p.x, bbox.p.y, bbox.q.x, bbox.q.y);
2317
0
    bbox_str_len = stell(&s);
2318
0
    swrite_string(&s, matrix_str, sizeof(bbox_str));
2319
0
    pprintg6(&s, "[%g %g %g %g %g %g]",
2320
0
            ictm.xx, ictm.xy, ictm.yx, ictm.yy, ictm.tx, ictm.ty);
2321
0
    matrix_str_len = stell(&s);
2322
0
    if ((code = cos_stream_put_c_strings(pcs, "/Type", "/XObject")) < 0 ||
2323
0
        (code = cos_stream_put_c_strings(pcs, "/Subtype", "/Form")) < 0 ||
2324
0
        (code = cos_stream_put_c_strings(pcs, "/FormType", "1")) < 0 ||
2325
0
        (code = cos_dict_put_c_key_string(cos_stream_dict(pcs), "/BBox",
2326
0
                                          bbox_str, bbox_str_len)) < 0 ||
2327
0
        (code = cos_dict_put_c_key_string(cos_stream_dict(pcs), "/Matrix",
2328
0
                                      matrix_str, matrix_str_len)) < 0 ||
2329
0
        (code = cos_dict_put_c_key_object(cos_stream_dict(pcs), "/Resources",
2330
0
                                          COS_OBJECT(pdev->substream_Resources))) < 0
2331
0
        )
2332
0
        return code;
2333
    /* Don't add to local_named_objects untill the object is created
2334
       to prevent pending references, which may appear
2335
       if /PUT pdfmark executes before pdf_substitute_resource in pdfmark_EP
2336
       drops this object.
2337
    */
2338
0
    pdev->FormDepth++;
2339
0
    return 0;
2340
0
}
2341
2342
/* [ /EP pdfmark */
2343
static int
2344
pdfmark_EP(gx_device_pdf * pdev, gs_param_string * pairs, uint count,
2345
           const gs_matrix * pctm, const gs_param_string * no_objname)
2346
0
{
2347
0
    int code;
2348
0
    pdf_resource_t *pres = pdev->accumulating_substream_resource;
2349
0
    gs_const_string objname = pdev->objname;
2350
2351
    /* We are not currently accumulating a resource, this suggests an /EP
2352
     * without an opening /BP. This is (obviously) an error, Distiller throws
2353
     * an 'undefined' error, so we will too.
2354
     */
2355
0
    if (pres == NULL)
2356
0
        return_error(gs_error_undefined);
2357
2358
0
    if (pdev->CompatibilityLevel <= 1.7) {
2359
0
        code = pdf_add_procsets(pdev->substream_Resources, pdev->procsets);
2360
0
        if (code < 0)
2361
0
            return code;
2362
0
    }
2363
0
    code = pdf_exit_substream(pdev);
2364
0
    if (code < 0)
2365
0
        return code;
2366
0
    code = pdfmark_bind_named_object(pdev, &objname, &pres);
2367
0
    if (code < 0)
2368
0
        return 0;
2369
0
    gs_free_const_string(pdev->memory, objname.data, objname.size, "pdfmark_EP");
2370
0
    pdev->FormDepth--;
2371
0
    return 0;
2372
0
}
2373
2374
/* [ {obj} /SP pdfmark */
2375
static int
2376
pdfmark_SP(gx_device_pdf * pdev, gs_param_string * pairs, uint count,
2377
           const gs_matrix * pctm, const gs_param_string * no_objname)
2378
0
{
2379
0
    cos_object_t *pco;    /* stream */
2380
0
    int code;
2381
2382
0
    if (count != 1)
2383
0
        return_error(gs_error_rangecheck);
2384
0
    if ((code = pdf_get_named(pdev, &pairs[0], cos_type_stream, &pco)) < 0)
2385
0
        return code;
2386
0
    if (pco->is_open || !pco->is_graphics)
2387
0
        return_error(gs_error_rangecheck);
2388
0
    code = pdf_open_contents(pdev, PDF_IN_STREAM);
2389
0
    if (code < 0)
2390
0
        return code;
2391
0
    pdf_put_matrix(pdev, "q ", pctm, "cm");
2392
0
    pprinti64d1(pdev->strm, "/R%"PRId64" Do Q\n", pco->id);
2393
0
    pco->pres->where_used |= pdev->used_mask;
2394
2395
0
    code = pdf_add_resource(pdev, pdev->substream_Resources, "/XObject", pco->pres);
2396
0
    if (code < 0)
2397
0
        return code;
2398
2399
0
    return 0;
2400
0
}
2401
2402
/* [ /_objdef {array} /type /array /OBJ pdfmark */
2403
/* [ /_objdef {dict} /type /dict /OBJ pdfmark */
2404
/* [ /_objdef {stream} /type /stream /OBJ pdfmark */
2405
static int
2406
pdfmark_OBJ(gx_device_pdf * pdev, gs_param_string * pairs, uint count,
2407
            const gs_matrix * pctm, const gs_param_string * objname)
2408
1.04k
{
2409
1.04k
    cos_type_t cotype;
2410
1.04k
    cos_object_t *pco;
2411
1.04k
    bool stream = false;
2412
1.04k
    int code;
2413
2414
1.04k
    if (objname == 0 || count != 2 || !pdf_key_eq(&pairs[0], "/type"))
2415
0
        return_error(gs_error_rangecheck);
2416
1.04k
    if (pdf_key_eq(&pairs[1], "/array"))
2417
0
        cotype = cos_type_array;
2418
1.04k
    else if (pdf_key_eq(&pairs[1], "/dict"))
2419
814
        cotype = cos_type_dict;
2420
232
    else if ((stream = pdf_key_eq(&pairs[1], "/stream")))
2421
232
        cotype = cos_type_stream;
2422
0
    else
2423
0
        return_error(gs_error_rangecheck);
2424
1.04k
    if ((code = pdf_make_named(pdev, objname, cotype, &pco, true)) < 0) {
2425
        /*
2426
         * For Distiller compatibility, allows multiple /OBJ pdfmarks with
2427
         * the same name and type, even though the pdfmark specification
2428
         * doesn't say anything about this being legal.
2429
         */
2430
4
        if (code == gs_error_rangecheck &&
2431
4
            pdf_refer_named(pdev, objname, &pco) >= 0 &&
2432
4
            cos_type(pco) == cotype
2433
4
            )
2434
4
            return 0;    /* already exists, but OK */
2435
0
        return code;
2436
4
    }
2437
1.04k
    if (stream) {
2438
232
        if (pdev->CompressStreams)
2439
232
            return setup_pdfmark_stream_compression((gx_device_psdf *)pdev,
2440
232
                                                     (cos_stream_t *)pco);
2441
0
        else
2442
0
            return setup_pdfmark_stream_no_compression((gx_device_psdf *)pdev,
2443
0
                                                     (cos_stream_t *)pco);
2444
232
    }
2445
810
    return 0;
2446
1.04k
}
2447
2448
/* [ {array} index value /PUT pdfmark */
2449
/* Dictionaries are converted to .PUTDICT */
2450
/* Streams are converted to .PUTSTREAM */
2451
static int
2452
pdfmark_PUT(gx_device_pdf * pdev, gs_param_string * pairs, uint count,
2453
            const gs_matrix * pctm, const gs_param_string * no_objname)
2454
0
{
2455
0
    cos_object_t *pco;
2456
0
    cos_value_t value;
2457
0
    int code, index;
2458
2459
0
    if (count != 3)
2460
0
        return_error(gs_error_rangecheck);
2461
0
    if ((code = pdf_get_named(pdev, &pairs[0], cos_type_array, &pco)) < 0)
2462
0
        return code;
2463
0
    if ((code = pdfmark_scan_int(&pairs[1], &index)) < 0)
2464
0
        return code;
2465
0
    if (index < 0)
2466
0
        return_error(gs_error_rangecheck);
2467
0
    if (pco->written)
2468
0
        return_error(gs_error_rangecheck);
2469
0
    return cos_array_put((cos_array_t *)pco, index,
2470
0
                cos_string_value(&value, pairs[2].data, pairs[2].size));
2471
0
}
2472
2473
/* [ {dict} key value ... /.PUTDICT pdfmark */
2474
/* [ {stream} key value ... /.PUTDICT pdfmark */
2475
/*
2476
 * Adobe's pdfmark documentation doesn't allow PUTDICT with a stream,
2477
 * but it's reasonable and unambiguous, and Acrobat Distiller accepts it,
2478
 * so we do too.
2479
 */
2480
static int
2481
pdfmark_PUTDICT(gx_device_pdf * pdev, gs_param_string * pairs, uint count,
2482
                const gs_matrix * pctm, const gs_param_string * no_objname)
2483
981
{
2484
981
    cos_object_t *pco;
2485
981
    int code, i;
2486
2487
981
    if ((code = pdf_refer_named(pdev, &pairs[0], &pco)) < 0)
2488
0
        return code;
2489
981
    if (cos_type(pco) != cos_type_dict && cos_type(pco) != cos_type_stream)
2490
0
        return_error(gs_error_typecheck);
2491
981
    if (pco->written)
2492
0
        return_error(gs_error_rangecheck);
2493
2494
    /* If this is a stream, and we are doing PDF/A output, and the stream
2495
     * is a Metadata stream, then we must not write it compressed. Bizarrely PDF/A
2496
     * excludes this.
2497
     * Actually, we cna't write Metadata streams at all. Because we don't know how to extend
2498
     * the XMP Metadata to include them.
2499
     */
2500
981
    if (cos_type(pco) == cos_type_stream && pdev->PDFA) {
2501
0
        for (i=0;i<count;i++) {
2502
0
            if (pairs[i].size == 9 && strncmp((const char *)pairs[i].data, "/Metadata", 9) == 0) {
2503
0
                cos_dict_t *pcd = (cos_dict_t *)pco;
2504
2505
0
                if (pdev->PDFA) {
2506
0
                    switch (pdev->PDFACompatibilityPolicy) {
2507
0
                        case 0:
2508
0
                            emprintf(pdev->memory,
2509
0
                                     "Cannot preserve Marked Content in PDF/A, reverting to normal PDF output\n\n");
2510
0
                            pdev->AbortPDFAX = true;
2511
0
                            pdev->PDFA = 0;
2512
0
                            break;
2513
0
                        case 1:
2514
0
                            emprintf(pdev->memory,
2515
0
                                     "Cannot preserve Marked Content in PDF/A, dropping feature to preserve PDF/A compatibility\n");
2516
0
                            return 0;
2517
0
                            break;
2518
0
                        case 2:
2519
0
                            emprintf(pdev->memory,
2520
0
                                     "Cannot preserve Marked Content in PDF/A, aborting conversion\n");
2521
0
                            return_error (gs_error_typecheck);
2522
0
                            break;
2523
0
                        default:
2524
0
                            emprintf(pdev->memory,
2525
0
                                     "Cannot preserve Marked Content in PDF/A, unrecognised PDFACompatibilityLevel,\nreverting to normal PDF output\n");
2526
0
                            pdev->AbortPDFAX = true;
2527
0
                            pdev->PDFA = 0;
2528
0
                            break;
2529
0
                    }
2530
0
                }
2531
2532
                /* Close the compressed stream */
2533
0
                gs_free_object(pdev->pdf_memory, pco->input_strm, "free old stream, replacing with new stream");
2534
                /* And create a new uncompressed stream */
2535
0
                code = setup_pdfmark_stream_no_compression((gx_device_psdf *)pdev,
2536
0
                                                     (cos_stream_t *)pco);
2537
0
                if (code < 0)
2538
0
                    return code;
2539
2540
                /* We also need to remove any compression filters from the stream dictionary
2541
                 * The only possible error here is that the key isn't found, which is possible
2542
                 * and we don't care, so ignore the return code.
2543
                 */
2544
0
                cos_dict_delete_c_key(pcd, "/Filter");
2545
0
                cos_dict_delete_c_key(pcd, "/DecodeParams");
2546
2547
0
            }
2548
0
        }
2549
0
    }
2550
981
    return pdfmark_put_pairs((cos_dict_t *)pco, pairs + 1, count - 1);
2551
981
}
2552
2553
/* [ {stream} string ... /.PUTSTREAM pdfmark */
2554
static int
2555
pdfmark_PUTSTREAM(gx_device_pdf * pdev, gs_param_string * pairs, uint count,
2556
                  const gs_matrix * pctm, const gs_param_string * no_objname)
2557
229
{
2558
229
    cos_object_t *pco;
2559
229
    int code, i;
2560
229
    uint l;
2561
2562
229
    if (count < 2)
2563
0
        return_error(gs_error_rangecheck);
2564
229
    if ((code = pdf_get_named(pdev, &pairs[0], cos_type_stream, &pco)) < 0)
2565
0
        return code;
2566
229
    if (!pco->is_open) {
2567
0
        pdev->pdfmark_dup_stream = true;
2568
0
        return 0;
2569
0
    }
2570
458
    for (i = 1; i < count; ++i)
2571
229
        if (sputs(pco->input_strm, pairs[i].data, pairs[i].size, &l) != 0)
2572
0
            return_error(gs_error_ioerror);
2573
229
    if (pco->written)
2574
0
        return_error(gs_error_rangecheck);
2575
229
    pdev->pdfmark_dup_stream = false;
2576
229
    return code;
2577
229
}
2578
2579
/* [ {array} value /APPEND pdfmark */
2580
static int
2581
pdfmark_APPEND(gx_device_pdf * pdev, gs_param_string * pairs, uint count,
2582
               const gs_matrix * pctm, const gs_param_string * objname)
2583
0
{
2584
0
    cos_object_t *pco;
2585
0
    cos_value_t value;
2586
0
    int code;
2587
2588
0
    if (count != 2)
2589
0
        return_error(gs_error_rangecheck);
2590
0
    if ((code = pdf_get_named(pdev, &pairs[0], cos_type_array, &pco)) < 0)
2591
0
        return code;
2592
0
    return cos_array_add((cos_array_t *)pco,
2593
0
                cos_string_value(&value, pairs[1].data, pairs[1].size));
2594
0
}
2595
2596
/* [ {array} index value ... /.PUTINTERVAL pdfmark */
2597
static int
2598
pdfmark_PUTINTERVAL(gx_device_pdf * pdev, gs_param_string * pairs, uint count,
2599
                 const gs_matrix * pctm, const gs_param_string * no_objname)
2600
0
{
2601
0
    cos_object_t *pco;
2602
0
    cos_value_t value;
2603
0
    int code, index, i;
2604
2605
0
    if (count < 2)
2606
0
        return_error(gs_error_rangecheck);
2607
0
    if ((code = pdf_get_named(pdev, &pairs[0], cos_type_array, &pco)) < 0)
2608
0
        return code;
2609
0
    if ((code = pdfmark_scan_int(&pairs[1], &index)) < 0)
2610
0
        return code;
2611
0
    if (index < 0)
2612
0
        return_error(gs_error_rangecheck);
2613
0
    for (i = 2; code >= 0 && i < count; ++i)
2614
0
        code = cos_array_put((cos_array_t *)pco, index + i - 2,
2615
0
                cos_string_value(&value, pairs[i].data, pairs[i].size));
2616
0
    return code;
2617
0
}
2618
2619
/* [ {stream} /CLOSE pdfmark */
2620
static int
2621
pdfmark_CLOSE(gx_device_pdf * pdev, gs_param_string * pairs, uint count,
2622
              const gs_matrix * pctm, const gs_param_string * no_objname)
2623
223
{
2624
223
    cos_object_t *pco;
2625
223
    int code;
2626
2627
223
    if (count != 1)
2628
0
        return_error(gs_error_rangecheck);
2629
223
    if ((code = pdf_get_named(pdev, &pairs[0], cos_type_stream, &pco)) < 0)
2630
0
        return code;
2631
223
    if (!pco->is_open && !pdev->pdfmark_dup_stream)
2632
0
        return_error(gs_error_rangecheck);
2633
223
    pdev->pdfmark_dup_stream = false;
2634
    /* Currently we don't do anything special when closing a stream. */
2635
223
    pco->is_open = false;
2636
223
    return 0;
2637
223
}
2638
2639
/* [ /NamespacePush pdfmark */
2640
static int
2641
pdfmark_NamespacePush(gx_device_pdf *pdev, gs_param_string *pairs, uint count,
2642
                      const gs_matrix *pctm, const gs_param_string *objname)
2643
8
{
2644
8
    if (count != 0)
2645
0
        return_error(gs_error_rangecheck);
2646
8
    return pdf_push_namespace(pdev);
2647
8
}
2648
2649
/* [ /NamespacePop pdfmark */
2650
static int
2651
pdfmark_NamespacePop(gx_device_pdf *pdev, gs_param_string *pairs, uint count,
2652
                     const gs_matrix *pctm, const gs_param_string *objname)
2653
1
{
2654
1
    if (count != 0)
2655
0
        return_error(gs_error_rangecheck);
2656
1
    cos_dict_objects_write(pdev->local_named_objects, pdev);
2657
1
    return pdf_pop_namespace(pdev);
2658
1
}
2659
2660
/* [ /_objdef {image} /NI pdfmark */
2661
static int
2662
pdfmark_NI(gx_device_pdf *pdev, gs_param_string *pairs, uint count,
2663
           const gs_matrix *pctm, const gs_param_string *objname)
2664
6
{
2665
6
    cos_object_t *pco;
2666
6
    int code;
2667
2668
6
    if (objname == 0 || count != 0)
2669
0
        return_error(gs_error_rangecheck);
2670
6
    code = pdf_make_named(pdev, objname, cos_type_dict, &pco, true);
2671
6
    if (code < 0)
2672
0
        return code;
2673
6
    return cos_array_add_object(pdev->NI_stack, pco);
2674
6
}
2675
2676
/* ---------------- Named content pdfmarks ---------------- */
2677
2678
/* [ tag /MP pdfmark */
2679
static int
2680
pdfmark_MP(gx_device_pdf *pdev, gs_param_string *pairs, uint count,
2681
           const gs_matrix *pctm, const gs_param_string *objname)
2682
49
{
2683
49
    int code;
2684
49
    char *tag;
2685
2686
49
    if (count != 1) return_error(gs_error_rangecheck);
2687
2688
49
    tag = (char *)gs_alloc_bytes(pdev->memory, (size_t)(pairs[0].size + 1) * sizeof(unsigned char),
2689
49
                "pdfmark_MP");
2690
49
    if (tag == NULL)
2691
0
        return_error(gs_error_VMerror);
2692
2693
49
    memcpy(tag, pairs[0].data, pairs[0].size);
2694
49
    tag[pairs[0].size] = 0x00;
2695
2696
49
    code = pdf_open_contents(pdev, PDF_IN_STREAM);
2697
49
    if (code < 0) return code;
2698
2699
49
    pprints1(pdev->strm, "%s MP\n", tag);
2700
2701
49
    gs_free_object(pdev->memory, tag, "pdfmark_MP");
2702
49
    return 0;
2703
49
}
2704
2705
/* [ tag propdict /DP pdfmark */
2706
static int
2707
pdfmark_DP(gx_device_pdf *pdev, gs_param_string *pairs, uint count,
2708
           const gs_matrix *pctm, const gs_param_string *objname)
2709
21
{
2710
21
    int code;
2711
21
    cos_object_t *pco;
2712
21
    char *cstring;
2713
21
    pdf_resource_t *pres;
2714
2715
21
    if (count != 2) return_error(gs_error_rangecheck);
2716
2717
    /* check tag for /Name object syntax */
2718
21
    if ((pairs[0].data)[0] != '/') return_error(gs_error_rangecheck);
2719
2720
    /* check propdict for {object name} syntax */
2721
21
    if (pdf_objname_is_valid(pairs[1].data, pairs[1].size))
2722
0
    {
2723
0
        code = pdf_refer_named(pdev, &pairs[1], &pco);
2724
0
        if(code < 0) return code;
2725
0
    }
2726
21
    else /* << inline prop dict >> */
2727
21
    {
2728
        /* strip << and >> */
2729
21
        if ((pairs[1].data)[0]=='<'&&(pairs[1].data)[1]=='<')
2730
21
        {
2731
21
            int ix = 0;
2732
21
            byte *p = (byte *)pairs[1].data;
2733
2734
            /* Fortunately we don't use the 'size' when freeing the memory
2735
             * so we can quickly copy the string content up two places and reduce
2736
             * the size by 2 to remove the '<<'. This saves an alloc and free of the
2737
             * string data.
2738
             */
2739
599
            for (ix = 0; ix < pairs[1].size - 2;ix++)
2740
578
                p[ix] = pairs[1].data[ix + 2];
2741
21
            pairs[1].size-=2;
2742
21
        }
2743
0
        else
2744
0
            return_error(gs_error_rangecheck);
2745
2746
21
        if ((pairs[1].data)[pairs[1].size-1]=='>'&&(pairs[1].data)[pairs[1].size-2]=='>')
2747
21
            pairs[1].size-=2;
2748
2749
        /* convert inline propdict to C string with object names replaced by refs */
2750
21
        code = pdf_replace_names(pdev, &pairs[1], &pairs[1]);
2751
21
        if (code<0) return code;
2752
21
        cstring = (char *)gs_alloc_bytes(pdev->memory, (size_t)(pairs[1].size + 1) * sizeof(unsigned char),
2753
21
            "pdfmark_DP");
2754
21
        if (cstring == NULL)
2755
0
            return_error(gs_error_VMerror);
2756
2757
21
        memcpy(cstring, pairs[1].data, pairs[1].size);
2758
21
        cstring[pairs[1].size] = 0x00;
2759
2760
21
        code = pdf_make_named_dict(pdev, NULL, (cos_dict_t**) &pco, true);
2761
21
        if (code<0) return code;
2762
2763
        /* copy inline propdict to new object */
2764
21
        code = cos_dict_put_c_strings((cos_dict_t*) pco, cstring, "");
2765
21
        if(code < 0) return code;
2766
21
        COS_WRITE_OBJECT(pco, pdev, resourceProperties);
2767
21
        COS_RELEASE(pco, "pdfmark_DP");
2768
21
        gs_free_object(pdev->memory, cstring, "pdfmark_DP");
2769
21
    }
2770
2771
21
    pres = pdf_find_resource_by_resource_id(pdev, resourceProperties, pco->id);
2772
21
    if (pres==0){
2773
21
        if ((code = pdf_alloc_resource(pdev, resourceProperties, pco->id, &(pco->pres), pco->id))<0)
2774
0
            return code;
2775
21
    }
2776
2777
21
    cstring = (char *)gs_alloc_bytes(pdev->memory, (size_t)(pairs[0].size + 1) * sizeof(unsigned char),
2778
21
                "pdfmark_DP");
2779
21
    if (cstring == NULL)
2780
0
        return_error(gs_error_VMerror);
2781
21
    memcpy(cstring, pairs[0].data, pairs[0].size);
2782
21
    cstring[pairs[0].size] = 0x00;
2783
2784
    /* make sure we write to the correct stream */
2785
21
    code = pdf_open_contents(pdev, PDF_IN_STREAM);
2786
21
    if (code < 0) return code;
2787
2788
21
    pprints1(pdev->strm, "%s", cstring); /* write tag */
2789
21
    pprinti64d1(pdev->strm, "/R%"PRId64" DP\n", pco->id);
2790
21
    pco->pres->where_used |= pdev->used_mask;
2791
21
    if ((code = pdf_add_resource(pdev, pdev->substream_Resources, "/Properties", pco->pres))<0)
2792
0
        return code;
2793
2794
21
    gs_free_object(pdev->memory, cstring, "pdfmark_DP");
2795
21
    return 0;
2796
21
}
2797
2798
/* [ tag /BMC pdfmark */
2799
static int
2800
pdfmark_BMC(gx_device_pdf *pdev, gs_param_string *pairs, uint count,
2801
            const gs_matrix *pctm, const gs_param_string *objname)
2802
381
{
2803
381
    int code;
2804
381
    char *tag;
2805
2806
381
    if (count != 1) return_error(gs_error_rangecheck);
2807
2808
381
    tag = (char *)gs_alloc_bytes(pdev->memory, (size_t)(pairs[0].size + 1) * sizeof(unsigned char),
2809
381
                "pdfmark_BMC");
2810
381
    if (tag == NULL)
2811
0
        return_error(gs_error_VMerror);
2812
2813
381
    memcpy(tag, pairs[0].data, pairs[0].size);
2814
381
    tag[pairs[0].size] = 0x00;
2815
2816
381
    code = pdf_open_contents(pdev, PDF_IN_STREAM);
2817
381
    if (code < 0) return code;
2818
2819
381
    pprints1(pdev->strm, "%s BMC\n", tag);
2820
2821
381
    gs_free_object(pdev->memory, tag, "pdfmark_BMC");
2822
381
    return 0;
2823
381
}
2824
2825
/* [ tag propdict /BDC pdfmark */
2826
static int
2827
pdfmark_BDC(gx_device_pdf *pdev, gs_param_string *pairs, uint count,
2828
            const gs_matrix *pctm, const gs_param_string *objname)
2829
8.16k
{
2830
8.16k
    int code, id = 0, i, esc_size = 0;
2831
8.16k
    cos_object_t *pco = NULL;
2832
8.16k
    char *cstring;
2833
8.16k
    pdf_resource_t *pres;
2834
2835
8.16k
    if (count != 2) return_error(gs_error_rangecheck);
2836
2837
    /* check tag for /Name object syntax */
2838
8.16k
    if ((pairs[0].data)[0] != '/') return_error(gs_error_rangecheck);
2839
2840
    /* Check for /OC (Optional Content), if it is Optional Content, check the output PDF level is at least 1.5 */
2841
8.16k
    if (pairs[0].size == 3 && memcmp(pairs[0].data, "/OC", 3) == 0) {
2842
89
        if (pdev->CompatibilityLevel < 1.4999) {
2843
0
            if (pdev->PDFA) {
2844
0
                switch (pdev->PDFACompatibilityPolicy) {
2845
0
                    case 0:
2846
0
                        emprintf(pdev->memory,
2847
0
                                 "Optional (Marked) Content not valid in this version of PDF, reverting to normal PDF output\n\n");
2848
0
                        pdev->AbortPDFAX = true;
2849
0
                        pdev->PDFA = 0;
2850
0
                        break;
2851
0
                    case 1:
2852
0
                        emprintf(pdev->memory,
2853
0
                                 "Optional (Marked) Content not valid in this version of PDF. Dropping feature to preserve PDF/A compatibility\n");
2854
0
                        return 0;
2855
0
                        break;
2856
0
                    case 2:
2857
0
                        emprintf(pdev->memory,
2858
0
                                 "Optional (Marked) Content not valid in this version of PDF,  aborting conversion\n");
2859
0
                        return_error (gs_error_typecheck);
2860
0
                        break;
2861
0
                    default:
2862
0
                        emprintf(pdev->memory,
2863
0
                                 "Optional (Marked) Content not valid in this version of PDF, unrecognised PDFACompatibilityLevel,\nreverting to normal PDF output\n");
2864
0
                        pdev->AbortPDFAX = true;
2865
0
                        pdev->PDFA = 0;
2866
0
                        break;
2867
0
                }
2868
0
            } else {
2869
0
                emprintf(pdev->memory,
2870
0
                         "Optional (Marked) Content not valid in this version of PDF. Dropping feature to preserve compatibility\n");
2871
0
                return 0;
2872
0
            }
2873
0
        }
2874
89
    }
2875
2876
    /* check propdict for {object name} syntax */
2877
8.16k
    if (pdf_objname_is_valid(pairs[1].data, pairs[1].size))
2878
356
    {
2879
356
        code = pdf_refer_named(pdev, &pairs[1], &pco);
2880
356
        if(code < 0) return code;
2881
356
        id = pco->id;
2882
356
    }
2883
7.80k
    else /* << inline prop dict >> */
2884
7.80k
    {
2885
        /* strip << and >> */
2886
7.80k
        if ((pairs[1].data)[0]=='<'&&(pairs[1].data)[1]=='<')
2887
7.80k
        {
2888
7.80k
            int ix = 0;
2889
7.80k
            byte *p = (byte *)pairs[1].data;
2890
2891
            /* Fortunately we don't use the 'size' when freeing the memory
2892
             * so we can quickly copy the string content up two places and reduce
2893
             * the size by 2 to remove the '<<'. This saves an alloc and free of the
2894
             * string data.
2895
             */
2896
140k
            for (ix = 0; ix < pairs[1].size - 2;ix++)
2897
132k
                p[ix] = pairs[1].data[ix + 2];
2898
7.80k
            pairs[1].size-=2;
2899
2900
7.80k
            if ((pairs[1].data)[pairs[1].size-1]=='>'&&(pairs[1].data)[pairs[1].size-2]=='>')
2901
7.80k
            pairs[1].size-=2;
2902
2903
            /* convert inline propdict to C string with object names replaced by refs */
2904
7.80k
            code = pdf_replace_names(pdev, &pairs[1], &pairs[1]);
2905
7.80k
            if (code<0) return code;
2906
7.80k
            cstring = (char *)gs_alloc_bytes(pdev->memory, (size_t)(pairs[1].size + 1) * sizeof(unsigned char),
2907
7.80k
                "pdfmark_BDC");
2908
7.80k
            if (cstring == NULL)
2909
0
                return_error(gs_error_VMerror);
2910
2911
7.80k
            memcpy(cstring, pairs[1].data, pairs[1].size);
2912
7.80k
            cstring[pairs[1].size] = 0x00;
2913
2914
7.80k
            code = pdf_make_named_dict(pdev, NULL, (cos_dict_t**) &pco, true);
2915
7.80k
            if (code<0) return code;
2916
2917
            /* copy inline propdict to new object */
2918
7.80k
            code = cos_dict_put_c_strings((cos_dict_t*) pco, cstring, "");
2919
7.80k
            if(code < 0) return code;
2920
2921
7.80k
            COS_WRITE_OBJECT(pco, pdev, resourceProperties);
2922
2923
7.80k
            COS_RELEASE(pco, "pdfmark_BDC");
2924
7.80k
            gs_free_object(pdev->memory, cstring, "pdfmark_BDC");
2925
7.80k
            id = pco->id;
2926
7.80k
        }
2927
0
        else {
2928
0
            if ((pairs[1].data)[pairs[1].size-1]!='R') {
2929
0
                if ((pairs[1].data)[pairs[1].size-2]==' ' && (pairs[1].data)[pairs[1].size-1]!='R')
2930
0
                    return_error(gs_error_rangecheck);
2931
0
                if (sscanf((const char *)pairs[1].data, "%d 0 R", &id) != 1)
2932
0
                    return_error(gs_error_unknownerror);
2933
0
            }
2934
2935
0
        }
2936
7.80k
    }
2937
2938
8.16k
    pres = pdf_find_resource_by_resource_id(pdev, resourceProperties, id);
2939
8.16k
    if (pres==0){
2940
8.11k
        if (pco != NULL) {
2941
8.11k
            if ((code = pdf_alloc_resource(pdev, resourceProperties, pco->id, &(pco->pres), pco->id))<0) {
2942
0
                return code;
2943
0
            }
2944
8.11k
        }
2945
0
        else
2946
0
            if ((code = pdf_alloc_resource(pdev, resourceProperties, id, &pres, id))<0)
2947
0
                return code;
2948
8.11k
    }
2949
2950
    /* We need to make sure we escape any white space and delimeters in the tag */
2951
65.2k
    for (i = 0;i < pairs[0].size;i++) {
2952
57.1k
        switch(pairs[0].data[i]) {
2953
0
            case 0x00:
2954
0
            case 0x09:
2955
0
            case 0x0a:
2956
0
            case 0x0c:
2957
0
            case 0x0d:
2958
0
            case 0x20:
2959
0
            case 0x25:
2960
0
            case 0x28:
2961
0
            case 0x29:
2962
0
            case 0x3c:
2963
0
            case 0x3e:
2964
0
            case 0x5b:
2965
0
            case 0x5d:
2966
0
            case 0x7b:
2967
0
            case 0x7d:
2968
0
                esc_size += 3;
2969
0
                break;
2970
8.16k
            case 0x2f:
2971
8.16k
                esc_size++;
2972
                /* The buffer includes the initial "/" for names, so don't escape that */
2973
8.16k
                if (i > 0) {
2974
0
                    esc_size += 2;
2975
0
                }
2976
8.16k
                break;
2977
48.9k
            default:
2978
48.9k
                esc_size++;
2979
48.9k
                break;
2980
57.1k
        }
2981
57.1k
    }
2982
2983
8.16k
    cstring = (char *)gs_alloc_bytes(pdev->memory, (size_t)(esc_size + 1) * sizeof(unsigned char),
2984
8.16k
                "pdfmark_BDC");
2985
8.16k
    if (cstring == NULL)
2986
0
        return_error(gs_error_VMerror);
2987
2988
2989
8.16k
    esc_size = 0;
2990
65.2k
    for (i = 0;i < pairs[0].size;i++) {
2991
57.1k
        switch(pairs[0].data[i]) {
2992
0
            case 0x00:
2993
0
            case 0x09:
2994
0
            case 0x0a:
2995
0
            case 0x0c:
2996
0
            case 0x0d:
2997
0
            case 0x20:
2998
0
            case 0x25:
2999
0
            case 0x28:
3000
0
            case 0x29:
3001
0
            case 0x3c:
3002
0
            case 0x3e:
3003
0
            case 0x5b:
3004
0
            case 0x5d:
3005
0
            case 0x7b:
3006
0
            case 0x7d:
3007
0
                {
3008
0
                    char byte;
3009
3010
0
                    cstring[esc_size++] = '#';
3011
0
                    byte = pairs[0].data[i] >> 4;
3012
0
                    if (byte > 9)
3013
0
                        cstring[esc_size++] = byte + 0x37;
3014
0
                    else
3015
0
                        cstring[esc_size++] = byte + 0x30;
3016
0
                    byte = pairs[0].data[i] & 0x0f;
3017
0
                    if (byte > 9)
3018
0
                        cstring[esc_size++] = byte + 0x37;
3019
0
                    else
3020
0
                        cstring[esc_size++]= byte + 0x30;
3021
0
                }
3022
0
                break;
3023
8.16k
            case 0x2f:
3024
8.16k
                if (i > 0) {
3025
0
                    char byte;
3026
3027
0
                    cstring[esc_size++] = '#';
3028
0
                    byte = pairs[0].data[i] >> 4;
3029
0
                    if (byte > 9)
3030
0
                        cstring[esc_size++] = byte + 0x37;
3031
0
                    else
3032
0
                        cstring[esc_size++] = byte + 0x30;
3033
0
                    byte = pairs[0].data[i] & 0x0f;
3034
0
                    if (byte > 9)
3035
0
                        cstring[esc_size++] = byte + 0x37;
3036
0
                    else
3037
0
                        cstring[esc_size++]= byte + 0x30;
3038
0
                }
3039
8.16k
                else
3040
8.16k
                    cstring[esc_size++] = pairs[0].data[i];
3041
8.16k
                break;
3042
48.9k
            default:
3043
48.9k
                cstring[esc_size++] = pairs[0].data[i];
3044
48.9k
                break;
3045
57.1k
        }
3046
57.1k
    }
3047
8.16k
    cstring[esc_size] = 0x00;
3048
3049
    /* make sure we write to the correct stream */
3050
8.16k
    code = pdf_open_contents(pdev, PDF_IN_STREAM);
3051
8.16k
    if (code < 0) return code;
3052
3053
8.16k
    pprints1(pdev->strm, "%s", cstring); /* write tag */
3054
8.16k
    pprinti64d1(pdev->strm, "/R%"PRId64" BDC\n", id);
3055
8.16k
    if (pco != NULL) {
3056
8.16k
        pco->pres->where_used |= pdev->used_mask;
3057
8.16k
        if ((code = pdf_add_resource(pdev, pdev->substream_Resources, "/Properties", pco->pres))<0)
3058
0
            return code;
3059
8.16k
    }
3060
0
    else {
3061
0
        pres->where_used |= pdev->used_mask;
3062
0
        if ((code = pdf_add_resource(pdev, pdev->substream_Resources, "/Properties", pres))<0)
3063
0
            return code;
3064
0
    }
3065
3066
3067
8.16k
    gs_free_object(pdev->memory, cstring, "pdfmark_BDC");
3068
8.16k
    return 0;
3069
8.16k
}
3070
3071
/* [ /EMC pdfmark */
3072
static int
3073
pdfmark_EMC(gx_device_pdf *pdev, gs_param_string *pairs, uint count,
3074
            const gs_matrix *pctm, const gs_param_string *objname)
3075
12.4k
{
3076
12.4k
    int code;
3077
3078
12.4k
    code = pdf_open_contents(pdev, PDF_IN_STREAM);
3079
12.4k
    if (code < 0) return code;
3080
12.4k
    stream_puts(pdev->strm, "EMC\n");
3081
3082
12.4k
    return 0;
3083
12.4k
}
3084
3085
/* ---------------- Document structure pdfmarks ---------------- */
3086
3087
/* [ newsubtype1 stdsubtype1 ... /StRoleMap pdfmark */
3088
static int
3089
pdfmark_StRoleMap(gx_device_pdf *pdev, gs_param_string *pairs, uint count,
3090
                  const gs_matrix *pctm, const gs_param_string *objname)
3091
0
{
3092
0
    return 0;     /****** NOT IMPLEMENTED YET ******/
3093
0
}
3094
3095
/* [ class1 {attrobj1} ... /StClassMap pdfmark */
3096
static int
3097
pdfmark_StClassMap(gx_device_pdf *pdev, gs_param_string *pairs, uint count,
3098
                   const gs_matrix *pctm, const gs_param_string *objname)
3099
0
{
3100
0
    return 0;     /****** NOT IMPLEMENTED YET ******/
3101
0
}
3102
3103
/*
3104
 * [ [/_objdef {objname}] /Subtype name [/Title string] [/Alt string]
3105
 *   [/ID string] [/Class name] [/At index] [/Bookmark dict] [action_pairs...]
3106
 *   /StPNE pdfmark
3107
 */
3108
static int
3109
pdfmark_StPNE(gx_device_pdf *pdev, gs_param_string *pairs, uint count,
3110
              const gs_matrix *pctm, const gs_param_string *objname)
3111
0
{
3112
0
    return 0;     /****** NOT IMPLEMENTED YET ******/
3113
0
}
3114
3115
/* [ [/Title string] [/Open bool] [action_pairs...] /StBookmarkRoot pdfmark */
3116
static int
3117
pdfmark_StBookmarkRoot(gx_device_pdf *pdev, gs_param_string *pairs, uint count,
3118
                       const gs_matrix *pctm, const gs_param_string *objname)
3119
0
{
3120
0
    return 0;     /****** NOT IMPLEMENTED YET ******/
3121
0
}
3122
3123
/* [ [/E {elt}] /StPush pdfmark */
3124
static int
3125
pdfmark_StPush(gx_device_pdf *pdev, gs_param_string *pairs, uint count,
3126
               const gs_matrix *pctm, const gs_param_string *objname)
3127
0
{
3128
0
    return 0;     /****** NOT IMPLEMENTED YET ******/
3129
0
}
3130
3131
/* [ /StPop pdfmark */
3132
static int
3133
pdfmark_StPop(gx_device_pdf *pdev, gs_param_string *pairs, uint count,
3134
              const gs_matrix *pctm, const gs_param_string *objname)
3135
0
{
3136
0
    return 0;     /****** NOT IMPLEMENTED YET ******/
3137
0
}
3138
3139
/* [ /StPopAll pdfmark */
3140
static int
3141
pdfmark_StPopAll(gx_device_pdf *pdev, gs_param_string *pairs, uint count,
3142
                 const gs_matrix *pctm, const gs_param_string *objname)
3143
0
{
3144
0
    return 0;     /****** NOT IMPLEMENTED YET ******/
3145
0
}
3146
3147
/* [ [/T tagname] [/At index] /StBMC pdfmark */
3148
static int
3149
pdfmark_StBMC(gx_device_pdf *pdev, gs_param_string *pairs, uint count,
3150
              const gs_matrix *pctm, const gs_param_string *objname)
3151
0
{
3152
0
    return 0;     /****** NOT IMPLEMENTED YET ******/
3153
0
}
3154
3155
/* [ [/P propdict] [/T tagname] [/At index] /StBDC pdfmark */
3156
static int
3157
pdfmark_StBDC(gx_device_pdf *pdev, gs_param_string *pairs, uint count,
3158
              const gs_matrix *pctm, const gs_param_string *objname)
3159
0
{
3160
0
    return 0;     /****** NOT IMPLEMENTED YET ******/
3161
0
}
3162
3163
/* [ /Obj {obj} [/At index] /StOBJ pdfmark */
3164
static int
3165
pdfmark_StOBJ(gx_device_pdf *pdev, gs_param_string *pairs, uint count,
3166
              const gs_matrix *pctm, const gs_param_string *objname)
3167
0
{
3168
0
    return 0;     /****** NOT IMPLEMENTED YET ******/
3169
0
}
3170
3171
/* [ /Obj {obj} /StAttr pdfmark */
3172
static int
3173
pdfmark_StAttr(gx_device_pdf *pdev, gs_param_string *pairs, uint count,
3174
               const gs_matrix *pctm, const gs_param_string *objname)
3175
0
{
3176
0
    return 0;     /****** NOT IMPLEMENTED YET ******/
3177
0
}
3178
3179
/* [ /StoreName name /StStore pdfmark */
3180
static int
3181
pdfmark_StStore(gx_device_pdf *pdev, gs_param_string *pairs, uint count,
3182
                const gs_matrix *pctm, const gs_param_string *objname)
3183
0
{
3184
0
    return 0;     /****** NOT IMPLEMENTED YET ******/
3185
0
}
3186
3187
/* [ /StoreName name /StRetrieve pdfmark */
3188
static int
3189
pdfmark_StRetrieve(gx_device_pdf *pdev, gs_param_string *pairs, uint count,
3190
                   const gs_matrix *pctm, const gs_param_string *objname)
3191
0
{
3192
0
    return 0;     /****** NOT IMPLEMENTED YET ******/
3193
0
}
3194
3195
static int
3196
pdfmark_Metadata(gx_device_pdf * pdev, gs_param_string * pairs, uint count,
3197
             const gs_matrix * pctm, const gs_param_string * objname)
3198
0
{
3199
0
    int i;
3200
0
    gs_param_string key;
3201
0
    char data[10] = "/Metadata";
3202
3203
0
    if (pdev->CompatibilityLevel < 1.4) {
3204
0
        dmprintf(pdev->pdf_memory, "Cannot add Metadata to PDF files with version earlier than 1.4.\n");
3205
0
        return 0;
3206
0
    }
3207
0
    if (pdev->PDFA != 0)
3208
0
        dmprintf(pdev->pdf_memory, "Warning: PDF/A output requires specific metadata, this pdfmark has overridden that,\n         output conformance cannot be guaranteed\n");
3209
0
    if (pdev->PDFX != 0)
3210
0
        dmprintf(pdev->pdf_memory, "Warning: PDF/X output requires specific metadata, this pdfmark has overridden that,\n         output conformance cannot be guaranteed\n");
3211
3212
0
    if(pdev->ExtensionMetadata) {
3213
0
        dmprintf(pdev->pdf_memory, "Extension metadata exists when /Metadata pdfmark executed, discarding extension metadata.\n");
3214
0
        gs_free_object(pdev->pdf_memory->stable_memory, pdev->ExtensionMetadata, "Extension metadata discarded on /Metadata pdfmark");
3215
0
    }
3216
3217
0
    if (!pdev->Catalog) {
3218
0
        gs_param_string nstr;
3219
3220
0
        param_string_from_string(nstr, "{Catalog}");
3221
0
        pdf_create_named_dict(pdev, &nstr, &pdev->Catalog, 0L);
3222
0
    }
3223
3224
0
    key.data = (const byte *)&data;
3225
0
    key.size = 9;
3226
3227
0
    for (i = 0; i < count; i += 2) {
3228
0
        if (pdf_key_eq(&pairs[i], "{Catalog}")) {
3229
0
            return cos_dict_put_string(pdev->Catalog, key.data, key.size,
3230
0
                               pairs[i+1].data, pairs[i+1].size);
3231
0
        }
3232
0
    }
3233
0
    return 0;
3234
0
}
3235
3236
static int
3237
pdfmark_Ext_Metadata(gx_device_pdf * pdev, gs_param_string * pairs, uint count,
3238
             const gs_matrix * pctm, const gs_param_string * objname)
3239
0
{
3240
0
    int i, j=0;
3241
3242
0
    if (pdev->CompatibilityLevel < 1.4) {
3243
0
        dmprintf(pdev->pdf_memory, "Cannot add Metadata to PDF files with version earlier than 1.4.\n");
3244
0
        return 0;
3245
0
    }
3246
3247
0
    if (cos_dict_find_c_key(pdev->Catalog, "/Metadata")) {
3248
0
        dmprintf(pdev->pdf_memory, "Cannot add extension to Metadata specified with the /Metadata pdfmark\n");
3249
0
        return 0;
3250
0
    }
3251
3252
0
    if(pdev->ExtensionMetadata) {
3253
0
        dmprintf(pdev->pdf_memory, "Extension metadata already defined, discarding old data.\n");
3254
0
        gs_free_object(pdev->pdf_memory->stable_memory, pdev->ExtensionMetadata, "Extension metadata");
3255
0
    }
3256
0
    pdev->ExtensionMetadata = (char *)gs_alloc_bytes(pdev->pdf_memory->stable_memory, pairs[1].size - 1, "Extension metadata");
3257
0
    if (pdev->ExtensionMetadata == NULL)
3258
0
        return_error(gs_error_VMerror);
3259
3260
0
    memset(pdev->ExtensionMetadata, 0x00, pairs[1].size - 1);
3261
0
    for (i=1;i<pairs[1].size - 1;i++) {
3262
0
        if (pairs[1].data[i] == '\\') {
3263
0
            switch(pairs[1].data[i+1]) {
3264
0
                case '(':
3265
0
                case ')':
3266
0
                case '\\':
3267
0
                    pdev->ExtensionMetadata[j++] = pairs[1].data[i+1];
3268
0
                    i++;
3269
0
                    break;
3270
0
                case 'r':
3271
0
                    pdev->ExtensionMetadata[j++] = 0x0D;
3272
0
                    i++;
3273
0
                    break;
3274
0
                case 'n':
3275
0
                    pdev->ExtensionMetadata[j++] = 0x0A;
3276
0
                    i++;
3277
0
                    break;
3278
0
                case 't':
3279
0
                    pdev->ExtensionMetadata[j++] = 0x09;
3280
0
                    i++;
3281
0
                    break;
3282
0
                case 'b':
3283
0
                    pdev->ExtensionMetadata[j++] = 0x08;
3284
0
                    i++;
3285
0
                    break;
3286
0
                case 'f':
3287
0
                    pdev->ExtensionMetadata[j++] = 0x0C;
3288
0
                    i++;
3289
0
                    break;
3290
0
                default:
3291
0
                    if((pairs[1].data[i+1]) >= 0x30 && (pairs[1].data[i+1]) <= 0x39) {
3292
0
                        pdev->ExtensionMetadata[j++] = (pairs[1].data[i+1] - 0x30) * 64 + (pairs[1].data[i+2] - 0x30) * 8 + (pairs[1].data[i+3] - 0x30);
3293
0
                        i += 3;
3294
0
                    } else
3295
0
                        pdev->ExtensionMetadata[j++] = pairs[1].data[i];
3296
0
                    break;
3297
0
            }
3298
0
        } else
3299
0
            pdev->ExtensionMetadata[j++] = pairs[1].data[i];
3300
0
    }
3301
0
    return 0;
3302
0
}
3303
3304
static int
3305
pdfmark_OCProperties(gx_device_pdf * pdev, gs_param_string * pairs, uint count,
3306
             const gs_matrix * pctm, const gs_param_string * objname)
3307
76
{
3308
76
    char *str;
3309
3310
76
    if (pdev->CompatibilityLevel < 1.4999) {
3311
0
        if (pdev->PDFA) {
3312
0
            switch (pdev->PDFACompatibilityPolicy) {
3313
0
                case 0:
3314
0
                    emprintf(pdev->memory,
3315
0
                             "Optional Content Properties not valid in this version of PDF, reverting to normal PDF output\n\n");
3316
0
                    pdev->AbortPDFAX = true;
3317
0
                    pdev->PDFA = 0;
3318
0
                    break;
3319
0
                case 1:
3320
0
                    emprintf(pdev->memory,
3321
0
                             "Optional Content Properties not valid in this version of PDF. Dropping feature to preserve PDF/A compatibility\n");
3322
0
                    break;
3323
0
                case 2:
3324
0
                    emprintf(pdev->memory,
3325
0
                             "Optional Content Properties not valid in this version of PDF,  aborting conversion\n");
3326
0
                    return_error (gs_error_typecheck);
3327
0
                    break;
3328
0
                default:
3329
0
                    emprintf(pdev->memory,
3330
0
                             "Optional Content Properties not valid in this version of PDF, unrecognised PDFACompatibilityLevel,\nreverting to normal PDF output\n");
3331
0
                    pdev->AbortPDFAX = true;
3332
0
                    pdev->PDFA = 0;
3333
0
                    break;
3334
0
            }
3335
0
        } else {
3336
0
            emprintf(pdev->memory,
3337
0
                     "Optional Content Properties not valid in this version of PDF. Dropping feature to preserve compatibility\n");
3338
0
        }
3339
76
    } else {
3340
76
        str = (char *)gs_alloc_bytes(pdev->memory, pairs[0].size + 1, "pdfmark_OCProperties");
3341
76
        if (str == NULL)
3342
0
            return_error(gs_error_VMerror);
3343
3344
76
        memset(str, 0x00, pairs[0].size + 1);
3345
76
        memcpy(str, pairs[0].data, pairs[0].size);
3346
3347
76
        (void)cos_dict_put_c_key_string(pdev->Catalog, "/OCProperties",
3348
76
                                         (byte *)str, strlen(str));
3349
3350
76
        gs_free_object(pdev->memory, str, "pdfmark_OCProperties");
3351
76
    }
3352
76
    return 0;
3353
76
}
3354
3355
/* ---------------- Dispatch ---------------- */
3356
3357
/*
3358
 * Define the pdfmark types we know about.
3359
 */
3360
static const pdfmark_name mark_names[] =
3361
{
3362
        /* Miscellaneous. */
3363
    {"ANN",          pdfmark_ANN,         PDFMARK_NAMEABLE},
3364
    {"LNK",          pdfmark_LNK,         PDFMARK_NAMEABLE},
3365
    {"OUT",          pdfmark_OUT,         0},
3366
    {"ARTICLE",      pdfmark_ARTICLE,     0},
3367
    {"DEST",         pdfmark_DEST,        PDFMARK_NAMEABLE},
3368
    {"EMBED",        pdfmark_EMBED,       PDFMARK_NAMEABLE},
3369
    {"PS",           pdfmark_PS,          PDFMARK_NAMEABLE},
3370
    {"PAGES",        pdfmark_PAGES,       0},
3371
    {"PAGE",         pdfmark_PAGE,        0},
3372
    {"PAGELABEL",    pdfmark_PAGELABEL,   0},
3373
    {"DOCINFO",      pdfmark_DOCINFO,     0},
3374
    {"DOCVIEW",      pdfmark_DOCVIEW,     0},
3375
        /* Named objects. */
3376
    {"BP",           pdfmark_BP,          PDFMARK_NAMEABLE | PDFMARK_TRUECTM},
3377
    {"EP",           pdfmark_EP,          0},
3378
    {"SP",           pdfmark_SP,          PDFMARK_ODD_OK | PDFMARK_KEEP_NAME | PDFMARK_TRUECTM},
3379
    {"OBJ",          pdfmark_OBJ,         PDFMARK_NAMEABLE},
3380
    {"PUT",          pdfmark_PUT,         PDFMARK_ODD_OK | PDFMARK_KEEP_NAME},
3381
    {".PUTDICT",     pdfmark_PUTDICT,     PDFMARK_ODD_OK | PDFMARK_KEEP_NAME},
3382
    {".PUTINTERVAL", pdfmark_PUTINTERVAL, PDFMARK_ODD_OK | PDFMARK_KEEP_NAME},
3383
    {".PUTSTREAM",   pdfmark_PUTSTREAM,   PDFMARK_ODD_OK | PDFMARK_KEEP_NAME |
3384
                                          PDFMARK_NO_REFS},
3385
    {"APPEND",       pdfmark_APPEND,      PDFMARK_KEEP_NAME},
3386
    {"CLOSE",        pdfmark_CLOSE,       PDFMARK_ODD_OK | PDFMARK_KEEP_NAME},
3387
    {"NamespacePush", pdfmark_NamespacePush, 0},
3388
    {"NamespacePop", pdfmark_NamespacePop, 0},
3389
    {"NI",           pdfmark_NI,          PDFMARK_NAMEABLE},
3390
        /* Marked content. */
3391
    {"MP",           pdfmark_MP,          PDFMARK_ODD_OK},
3392
    {"DP",           pdfmark_DP,          0},
3393
    {"BMC",          pdfmark_BMC,         PDFMARK_ODD_OK},
3394
    {"BDC",          pdfmark_BDC,         PDFMARK_NO_REFS},
3395
    {"EMC",          pdfmark_EMC,         0},
3396
        /* Document structure. */
3397
    {"StRoleMap",    pdfmark_StRoleMap,   0},
3398
    {"StClassMap",   pdfmark_StClassMap,  0},
3399
    {"StPNE",        pdfmark_StPNE,       PDFMARK_NAMEABLE},
3400
    {"StBookmarkRoot", pdfmark_StBookmarkRoot, 0},
3401
    {"StPush",       pdfmark_StPush,       0},
3402
    {"StPop",        pdfmark_StPop,        0},
3403
    {"StPopAll",     pdfmark_StPopAll,     0},
3404
    {"StBMC",        pdfmark_StBMC,        0},
3405
    {"StBDC",        pdfmark_StBDC,        0},
3406
    /* EMC is listed under "Marked content" above. */
3407
    {"StOBJ",        pdfmark_StOBJ,        0},
3408
    {"StAttr",       pdfmark_StAttr,       0},
3409
    {"StStore",      pdfmark_StStore,      0},
3410
    {"StRetrieve",   pdfmark_StRetrieve,   0},
3411
    /* Metadata and extension */
3412
    {"Metadata",     pdfmark_Metadata,     0},
3413
    {"Ext_Metadata", pdfmark_Ext_Metadata, 0},
3414
    {"OCProperties", pdfmark_OCProperties, PDFMARK_ODD_OK},
3415
        /* End of list. */
3416
    {0, 0}
3417
};
3418
3419
/* Process a pdfmark. */
3420
int
3421
pdfmark_process(gx_device_pdf * pdev, const gs_param_string_array * pma)
3422
37.5k
{
3423
37.5k
    const gs_param_string *data = pma->data;
3424
37.5k
    uint size = pma->size;
3425
37.5k
    const gs_param_string *pts = &data[size - 1];
3426
37.5k
    const gs_param_string *objname = 0;
3427
37.5k
    gs_matrix ctm;
3428
37.5k
    const pdfmark_name *pmn;
3429
37.5k
    int code = 0;
3430
3431
37.5k
    if (size < 2)
3432
0
        return_error(gs_error_stackunderflow);
3433
3434
37.5k
    { int cnt, len = pts[-1].size;
3435
37.5k
        char buf[200]; /* 6 doubles should fit (%g == -0.14285714285714285e-101 = 25 chars) */
3436
3437
37.5k
        if (len > sizeof(buf) - 1)
3438
0
            return_error(gs_error_rangecheck);
3439
37.5k
        memcpy(buf, pts[-1].data, len);
3440
37.5k
        buf[len] = 0;
3441
37.5k
        cnt = sscanf(buf, "[%g %g %g %g %g %g]",
3442
37.5k
                   &ctm.xx, &ctm.xy, &ctm.yx, &ctm.yy, &ctm.tx, &ctm.ty);
3443
37.5k
        if (cnt != 6)
3444
5
            return_error(gs_error_rangecheck);
3445
37.5k
    }
3446
37.5k
    size -= 2;      /* remove CTM & pdfmark name */
3447
755k
    for (pmn = mark_names; pmn->mname != 0; ++pmn)
3448
755k
        if (pdf_key_eq(pts, pmn->mname)) {
3449
37.5k
            gs_memory_t *mem = pdev->pdf_memory;
3450
37.5k
            int odd_ok = (pmn->options & PDFMARK_ODD_OK) != 0;
3451
37.5k
            gs_param_string *pairs;
3452
37.5k
            int j, index;
3453
3454
            /*
3455
             * Our coordinate system is scaled so that user space is always
3456
             * default user space.  Adjust the CTM to match this, except if this
3457
             * particular pdfmark requires the "true" CTM.
3458
             */
3459
37.5k
            if (pmn->options & PDFMARK_TRUECTM)
3460
37.5k
                DO_NOTHING;
3461
37.5k
            else {
3462
37.5k
                double xscale = 72.0 / pdev->HWResolution[0],
3463
37.5k
                       yscale = 72.0 / pdev->HWResolution[1];
3464
37.5k
                ctm.xx *= xscale, ctm.xy *= yscale;
3465
37.5k
                ctm.yx *= xscale, ctm.yy *= yscale;
3466
37.5k
                ctm.tx *= xscale, ctm.ty *= yscale;
3467
37.5k
            }
3468
37.5k
            if (size & !odd_ok)
3469
0
                return_error(gs_error_rangecheck);
3470
37.5k
            if (pmn->options & PDFMARK_NAMEABLE) {
3471
                /* Look for an object name. */
3472
50.7k
                for (j = 0; j < size; j += 2) {
3473
45.1k
                    if (pdf_key_eq(&data[j], "/_objdef")) {
3474
1.05k
                        objname = &data[j + 1];
3475
1.05k
                        if (!pdf_objname_is_valid(objname->data,
3476
1.05k
                                                  objname->size)
3477
1.05k
                            )
3478
0
                            return_error(gs_error_rangecheck);
3479
                        /* Save the pairs without the name. */
3480
1.05k
                        size -= 2;
3481
1.05k
                        pairs = (gs_param_string *)
3482
1.05k
                            gs_alloc_byte_array(mem, size,
3483
1.05k
                                                sizeof(gs_param_string),
3484
1.05k
                                                "pdfmark_process(pairs)");
3485
1.05k
                        if (!pairs)
3486
0
                            return_error(gs_error_VMerror);
3487
3488
3.14k
                        for (index=0;index < size;index++)
3489
2.09k
                            pairs[index].data = NULL;
3490
1.05k
                        for (index=0;index < j;index++) {
3491
0
                            pairs[index].data = gs_alloc_bytes(mem, data[index].size, "pdfmark_process(pairs)");
3492
0
                            if (pairs[index].data == NULL)
3493
0
                                goto error;
3494
0
                            memcpy((byte *)pairs[index].data, data[index].data, data[index].size);
3495
0
                            pairs[index].size = data[index].size;
3496
0
                            pairs[index].persistent = 1;
3497
0
                        }
3498
3.14k
                        for (index=j+2;index < size + 2;index++) {
3499
2.09k
                            pairs[index - 2].data = gs_alloc_bytes(mem, data[index].size, "pdfmark_process(pairs)");
3500
2.09k
                            if (pairs[index - 2].data == NULL)
3501
0
                                goto error;
3502
2.09k
                            memcpy((byte *)pairs[index - 2].data, data[index].data, data[index].size);
3503
2.09k
                            pairs[index - 2].size = data[index].size;
3504
2.09k
                            pairs[index - 2].persistent = 1;
3505
2.09k
                        }
3506
1.05k
                        goto copied;
3507
1.05k
                    }
3508
45.1k
                }
3509
6.67k
            }
3510
            /* Save all the pairs. */
3511
36.4k
            pairs = (gs_param_string *)
3512
36.4k
                gs_alloc_byte_array(mem, size, sizeof(gs_param_string),
3513
36.4k
                                    "pdfmark_process(pairs)");
3514
36.4k
            if (!pairs)
3515
0
                return_error(gs_error_VMerror);
3516
160k
            for (j=0;j < size;j++)
3517
124k
                pairs[j].data = NULL;
3518
160k
            for (j=0;j < size;j++) {
3519
124k
                pairs[j].data = gs_alloc_bytes(mem, data[j].size, "pdfmark_process(pairs)");
3520
124k
                if (pairs[j].data == NULL)
3521
0
                    goto error;
3522
124k
                memcpy((byte *)pairs[j].data, data[j].data, data[j].size);
3523
124k
                pairs[j].size = data[j].size;
3524
124k
                pairs[j].persistent = 1;
3525
124k
            }
3526
37.5k
copied:   /* Substitute object references for names. */
3527
37.5k
            if (!(pmn->options & PDFMARK_NO_REFS)) {
3528
29.1k
                for (j = (pmn->options & PDFMARK_KEEP_NAME ? 1 : 1 - odd_ok);
3529
86.0k
                     j < size; j += 2 - odd_ok
3530
56.8k
                     ) {
3531
56.8k
                    code = pdf_replace_names(pdev, &pairs[j], &pairs[j]);
3532
56.8k
                    if (code < 0) {
3533
0
                        gs_free_object(mem, pairs, "pdfmark_process(pairs)");
3534
0
                        return code;
3535
0
                    }
3536
56.8k
                }
3537
29.1k
            }
3538
37.5k
            code = (*pmn->proc) (pdev, pairs, size, &ctm, objname);
3539
37.5k
error:
3540
164k
            for (j=0;j < size;j++)
3541
126k
                gs_free_object(mem, (byte *)pairs[j].data, "pdfmark_process(pairs)");
3542
37.5k
            gs_free_object(mem, pairs, "pdfmark_process(pairs)");
3543
37.5k
            break;
3544
37.5k
        }
3545
37.5k
    return code;
3546
37.5k
}