Coverage Report

Created: 2026-09-14 07:34

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ghostpdl/devices/vector/gdevpdfp.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
/* Get/put parameters for PDF-writing driver */
18
#include "memory_.h"
19
#include "gx.h"
20
#include "gserrors.h"
21
#include "gdevpdfx.h"
22
#include "gdevpdfo.h"
23
#include "gdevpdfg.h"
24
#include "gdevpsdf.h"
25
#include "gsparamx.h"
26
#include "gsicc_manage.h"
27
28
/*
29
 * The pdfwrite device supports the following "real" parameters:
30
 *      OutputFile <string>
31
 *      all the Distiller parameters (also see gdevpsdp.c)
32
 * Only some of the Distiller parameters actually have any effect.
33
 *
34
 * The device also supports the following write-only pseudo-parameters that
35
 * serve only to communicate other information from the PostScript file.
36
 * Their "value" is an array of strings, some of which may be the result
37
 * of converting arbitrary PostScript objects to string form.
38
 *      pdfmark - see gdevpdfm.c
39
 *  DSC - processed in this file
40
 */
41
static int pdf_dsc_process(gx_device_pdf * pdev,
42
                            const gs_param_string_array * pma);
43
44
static const int CoreDistVersion = 5000;  /* Distiller 5.0 */
45
static const gs_param_item_t pdf_param_items[] = {
46
#define pi(key, type, memb) { key, type, offset_of(gx_device_pdf, memb) }
47
48
        /* Acrobat Distiller 4 parameters */
49
50
    /*
51
     * EndPage and StartPage are renamed because EndPage collides with
52
     * a page device parameter.
53
     */
54
    pi("PDFEndPage", gs_param_type_int, EndPage),
55
    pi("PDFStartPage", gs_param_type_int, StartPage),
56
    pi("Optimize", gs_param_type_bool, Optimize),
57
    pi("ParseDSCCommentsForDocInfo", gs_param_type_bool,
58
       ParseDSCCommentsForDocInfo),
59
    pi("ParseDSCComments", gs_param_type_bool, ParseDSCComments),
60
    pi("EmitDSCWarnings", gs_param_type_bool, EmitDSCWarnings),
61
    pi("CreateJobTicket", gs_param_type_bool, CreateJobTicket),
62
    pi("PreserveEPSInfo", gs_param_type_bool, PreserveEPSInfo),
63
    pi("AutoPositionEPSFiles", gs_param_type_bool, AutoPositionEPSFiles),
64
    pi("PreserveCopyPage", gs_param_type_bool, PreserveCopyPage),
65
    pi("UsePrologue", gs_param_type_bool, UsePrologue),
66
67
        /* Acrobat Distiller 5 parameters */
68
69
    pi("OffOptimizations", gs_param_type_int, OffOptimizations),
70
71
        /* Ghostscript-specific parameters */
72
73
    pi("ReAssignCharacters", gs_param_type_bool, ReAssignCharacters),
74
    pi("ReEncodeCharacters", gs_param_type_bool, ReEncodeCharacters),
75
    pi("FirstObjectNumber", gs_param_type_long, FirstObjectNumber),
76
    pi("CompressFonts", gs_param_type_bool, CompressFonts),
77
    pi("CompressStreams", gs_param_type_bool, CompressStreams),
78
    pi("PrintStatistics", gs_param_type_bool, PrintStatistics),
79
    pi("MaxInlineImageSize", gs_param_type_long, MaxInlineImageSize),
80
81
        /* PDF Encryption */
82
    pi("OwnerPassword", gs_param_type_string, OwnerPassword),
83
    pi("UserPassword", gs_param_type_string, UserPassword),
84
    pi("KeyLength", gs_param_type_int, KeyLength),
85
    pi("Permissions", gs_param_type_int, Permissions),
86
    pi("EncryptionR", gs_param_type_int, EncryptionR),
87
    pi("NoEncrypt", gs_param_type_string, NoEncrypt),
88
89
        /* Target viewer capabilities (Ghostscript-specific)  */
90
 /* pi("ForOPDFRead", gs_param_type_bool, ForOPDFRead),         pdfwrite-only */
91
    pi("ProduceDSC", gs_param_type_bool, ProduceDSC),
92
    pi("PatternImagemask", gs_param_type_bool, PatternImagemask),
93
    pi("MaxClipPathSize", gs_param_type_int, MaxClipPathSize),
94
    pi("MaxShadingBitmapSize", gs_param_type_int, MaxShadingBitmapSize),
95
    pi("HaveTrueTypes", gs_param_type_bool, HaveTrueTypes),
96
    pi("HaveCIDSystem", gs_param_type_bool, HaveCIDSystem),
97
    pi("HaveTransparency", gs_param_type_bool, HaveTransparency),
98
    pi("CompressEntireFile", gs_param_type_bool, CompressEntireFile),
99
    pi("PDFX", gs_param_type_int, PDFX),
100
    pi("PDFA", gs_param_type_int, PDFA),
101
    pi("DocumentUUID", gs_param_type_string, DocumentUUID),
102
    pi("InstanceUUID", gs_param_type_string, InstanceUUID),
103
    pi("DocumentTimeSeq", gs_param_type_int, DocumentTimeSeq),
104
105
    /* PDF/X parameters */
106
    pi("PDFXTrimBoxToMediaBoxOffset", gs_param_type_float_array, PDFXTrimBoxToMediaBoxOffset),
107
    pi("PDFXSetBleedBoxToMediaBox", gs_param_type_bool, PDFXSetBleedBoxToMediaBox),
108
    pi("PDFXBleedBoxToTrimBoxOffset", gs_param_type_float_array, PDFXBleedBoxToTrimBoxOffset),
109
110
    /* media selection parameters */
111
    pi("SetPageSize", gs_param_type_bool, SetPageSize),
112
    pi("RotatePages", gs_param_type_bool, RotatePages),
113
    pi("FitPages", gs_param_type_bool, FitPages),
114
    pi("CenterPages", gs_param_type_bool, CenterPages),
115
    pi("DoNumCopies", gs_param_type_bool, DoNumCopies),
116
    pi("PreserveSeparation", gs_param_type_bool, PreserveSeparation),
117
    pi("PreserveDeviceN", gs_param_type_bool, PreserveDeviceN),
118
    pi("PDFACompatibilityPolicy", gs_param_type_int, PDFACompatibilityPolicy),
119
    pi("DetectDuplicateImages", gs_param_type_bool, DetectDuplicateImages),
120
    pi("AllowIncrementalCFF", gs_param_type_bool, AllowIncrementalCFF),
121
    pi("WantsToUnicode", gs_param_type_bool, WantsToUnicode),
122
    pi("WantsOptionalContent", gs_param_type_bool, WantsOptionalContent),
123
    pi("PdfmarkCapable", gs_param_type_bool, PdfmarkCapable),
124
    pi("AllowPSRepeatFunctions", gs_param_type_bool, AllowPSRepeatFunctions),
125
    pi("IsDistiller", gs_param_type_bool, IsDistiller),
126
    pi("PreserveSMask", gs_param_type_bool, PreserveSMask),
127
    pi("PreserveTrMode", gs_param_type_bool, PreserveTrMode),
128
    pi("NoT3CCITT", gs_param_type_bool, NoT3CCITT),
129
    pi("FastWebView", gs_param_type_bool, Linearise),
130
    pi("NoOutputFonts", gs_param_type_bool, FlattenFonts),
131
    pi("WantsPageLabels", gs_param_type_bool, WantsPageLabels),
132
    pi("UserUnit", gs_param_type_float, UserUnit),
133
    pi("OmitInfoDate", gs_param_type_bool, OmitInfoDate),
134
    pi("OmitID", gs_param_type_bool, OmitID),
135
    pi("OmitXMP", gs_param_type_bool, OmitXMP),
136
    pi("ModifiesPageSize", gs_param_type_bool, ModifiesPageSize),
137
    pi("ModifiesPageOrder", gs_param_type_bool, ModifiesPageOrder),
138
    pi("WriteObjStms", gs_param_type_bool, WriteObjStms),
139
    pi("WriteXRefStm", gs_param_type_bool, WriteXRefStm),
140
    pi("ToUnicodeForStdEnc", gs_param_type_bool, ToUnicodeForStdEnc),
141
    pi("EmbedSubstituteFonts", gs_param_type_bool, EmbedSubstituteFonts),
142
    pi("UseBrotli", gs_param_type_bool, UseBrotli),
143
    pi("StreamEffort", gs_param_type_int, StreamEffort),
144
#undef pi
145
    gs_param_item_end
146
};
147
148
/*
149
  Notes on implementing the remaining Distiller functionality
150
  ===========================================================
151
152
  Architectural issues
153
  --------------------
154
155
  Must optionally disable application of TR, BG, UCR similarly.  Affects:
156
    PreserveHalftoneInfo
157
    PreserveOverprintSettings
158
    TransferFunctionInfo
159
    UCRandBGInfo
160
161
  Current limitations
162
  -------------------
163
164
  Non-primary elements in HalftoneType 5 are not written correctly
165
166
  Acrobat Distiller 3
167
  -------------------
168
169
  ---- Image parameters ----
170
171
  AntiAlias{Color,Gray,Mono}Images
172
173
  ---- Other parameters ----
174
175
  CompressPages
176
    Compress things other than page contents
177
  * PreserveHalftoneInfo
178
  PreserveOPIComments
179
    ? see OPI spec?
180
  * PreserveOverprintSettings
181
  * TransferFunctionInfo
182
  * UCRandBGInfo
183
  ColorConversionStrategy
184
    Select color space for drawing commands
185
  ConvertImagesToIndexed
186
    Postprocess image data *after* downsampling (requires an extra pass)
187
188
  Acrobat Distiller 4
189
  -------------------
190
191
  ---- Other functionality ----
192
193
  Document structure pdfmarks
194
195
  ---- Parameters ----
196
197
  xxxDownsampleType = /Bicubic
198
    Add new filter (or use siscale?) & to setup (gdevpsdi.c)
199
  DetectBlends
200
    Idiom recognition?  PatternType 2 patterns / shfill?  (see AD4)
201
  DoThumbnails
202
    Also output to memory device -- resolution issue
203
204
  ---- Job-level control ----
205
206
  EmitDSCWarnings
207
    Require DSC parser / interceptor
208
  CreateJobTicket
209
    ?
210
  AutoPositionEPSFiles
211
    Require DSC parsing
212
  PreserveCopyPage
213
    Concatenate Contents streams
214
  UsePrologue
215
    Needs hack in top-level control?
216
217
*/
218
219
/* Transfer a collection of parameters. */
220
static const byte xfer_item_sizes[] = {
221
    GS_PARAM_TYPE_SIZES(0)
222
};
223
int
224
gdev_pdf_get_param(gx_device *dev, char *Param, void *list)
225
1.60M
{
226
1.60M
    gx_device_pdf *pdev = (gx_device_pdf *)dev;
227
1.60M
    const gs_param_item_t *pi;
228
1.60M
    gs_param_list * plist = (gs_param_list *)list;
229
1.60M
    int code = 0;
230
231
97.5M
    for (pi = pdf_param_items; pi->key != 0; ++pi) {
232
97.0M
        if (strcmp(pi->key, Param) == 0) {
233
1.12M
            const char *key = pi->key;
234
1.12M
            const void *pvalue = (const void *)((const char *)pdev + pi->offset);
235
1.12M
            int size = xfer_item_sizes[pi->type];
236
1.12M
            gs_param_typed_value typed;
237
238
1.12M
            memcpy(&typed.value, pvalue, size);
239
1.12M
            typed.type = pi->type;
240
1.12M
            code = (*plist->procs->xmit_typed) (plist, key, &typed);
241
1.12M
            return code;
242
1.12M
        }
243
97.0M
    }
244
475k
    if (strcmp(Param, "CoreDistVersion") == 0) {
245
0
        return(param_write_int(plist, "CoreDistVersion", &CoreDistVersion));
246
0
    }
247
475k
    if (strcmp(Param, "CompatibilityLevel") == 0) {
248
0
        float f = pdev->CompatibilityLevel;
249
0
        return(param_write_float(plist, "CompatibilityLevel", &f));
250
0
    }
251
475k
    if (strcmp(Param, "ForOPDFRead") == 0) {
252
138k
        return(param_write_bool(plist, "ForOPDFRead", &pdev->ForOPDFRead));
253
138k
    }
254
336k
    if (strcmp(Param, "PassUserUnit") == 0) {
255
44.0k
        bool dummy;
256
44.0k
        if (pdev->CompatibilityLevel > 1.5)
257
10.4k
            dummy = true;
258
33.6k
        else
259
33.6k
            dummy = false;
260
44.0k
        return(param_write_bool(plist, "PassUserUnit", &dummy));
261
44.0k
    }
262
292k
    if (!pdev->is_ps2write) {
263
83.9k
        if (strcmp(Param, "pdfmark")  == 0){
264
4.16k
            return(param_write_null(plist, "pdfmark"));
265
4.16k
        }
266
79.7k
        if (strcmp(Param, "DSC") == 0){
267
0
            return(param_write_null(plist, "DSC"));
268
0
        }
269
79.7k
    }
270
271
#if OCR_VERSION > 0
272
    if (strcmp(Param, "OCRLanguage") == 0) {
273
        gs_param_string langstr;
274
        if (pdev->ocr_language[0]) {
275
            langstr.data = (const byte *)pdev->ocr_language;
276
            langstr.size = strlen(pdev->ocr_language);
277
            langstr.persistent = false;
278
        } else {
279
            langstr.data = (const byte *)"eng";
280
            langstr.size = 3;
281
            langstr.persistent = false;
282
        }
283
        return param_write_string(plist, "OCRLanguage", &langstr);
284
    }
285
    if (strcmp(Param, "OCREngine") == 0)
286
        return param_write_int(plist, "OCREngine", &pdev->ocr_engine);
287
288
    if (strcmp(Param, "UseOCR") == 0) {
289
        gs_param_string ocrstr;
290
291
        switch(pdev->UseOCR) {
292
            case UseOCRNever:
293
                ocrstr.data = (const byte *)"Never";
294
                ocrstr.size = 5;
295
                ocrstr.persistent = false;
296
                break;
297
            case UseOCRAsNeeded:
298
                ocrstr.data = (const byte *)"AsNeeded";
299
                ocrstr.size = 8;
300
                ocrstr.persistent = false;
301
                break;
302
            case UseOCRAlways:
303
                ocrstr.data = (const byte *)"Always";
304
                ocrstr.size = 8;
305
                ocrstr.persistent = false;
306
                break;
307
        }
308
        return param_write_string(plist, "UseOCR", &ocrstr);
309
    }
310
#endif
311
312
288k
    if (strcmp(Param, "OmitInfoDate") == 0)
313
0
        return(param_write_bool(plist, "OmitInfoDate", &pdev->OmitInfoDate));
314
288k
    if (strcmp(Param, "OmitXMP") == 0)
315
0
        return(param_write_bool(plist, "OmitXMP", &pdev->OmitXMP));
316
288k
    if (strcmp(Param, "OmitID") == 0)
317
0
        return(param_write_bool(plist, "OmitID", &pdev->OmitID));
318
319
288k
    return gdev_psdf_get_param(dev, Param, list);
320
288k
}
321
322
/* ---------------- Get parameters ---------------- */
323
324
/* Get parameters. */
325
int
326
gdev_pdf_get_params(gx_device * dev, gs_param_list * plist)
327
905k
{
328
905k
    gx_device_pdf *pdev = (gx_device_pdf *) dev;
329
905k
    float cl = (float)pdev->CompatibilityLevel;
330
905k
    int code;
331
905k
    int cdv = CoreDistVersion;
332
333
#if OCR_VERSION > 0
334
    gs_param_string langstr;
335
336
    if (pdev->ocr_language[0]) {
337
        langstr.data = (const byte *)pdev->ocr_language;
338
        langstr.size = strlen(pdev->ocr_language);
339
        langstr.persistent = false;
340
    } else {
341
        langstr.data = (const byte *)"eng";
342
        langstr.size = 3;
343
        langstr.persistent = false;
344
    }
345
346
    {
347
        gs_param_string ocrstr;
348
349
        switch(pdev->UseOCR) {
350
            case UseOCRNever:
351
                ocrstr.data = (const byte *)"Never";
352
                ocrstr.size = 5;
353
                ocrstr.persistent = false;
354
                break;
355
            case UseOCRAsNeeded:
356
                ocrstr.data = (const byte *)"AsNeeded";
357
                ocrstr.size = 8;
358
                ocrstr.persistent = false;
359
                break;
360
            case UseOCRAlways:
361
                ocrstr.data = (const byte *)"Always";
362
                ocrstr.size = 6;
363
                ocrstr.persistent = false;
364
                break;
365
        }
366
        code = param_write_string(plist, "UseOCR", &ocrstr);
367
    }
368
    code = param_write_string(plist, "OCRLanguage", &langstr);
369
    if(code < 0)
370
        return code;
371
    code = param_write_int(plist, "OCREngine", &pdev->ocr_engine);
372
    if(code < 0)
373
        return code;
374
#endif
375
376
905k
    pdev->ParamCompatibilityLevel = cl;
377
905k
    code = gdev_psdf_get_params(dev, plist);
378
905k
    if (code < 0 ||
379
905k
        (code = param_write_int(plist, "CoreDistVersion", &cdv)) < 0 ||
380
905k
        (code = param_write_float(plist, "CompatibilityLevel", &cl)) < 0 ||
381
905k
        (!pdev->is_ps2write && (code = param_write_bool(plist, "ForOPDFRead", &pdev->ForOPDFRead)) < 0) ||
382
        /* Indicate that we can process pdfmark and DSC. */
383
905k
        (param_requested(plist, "pdfmark") > 0 &&
384
0
         (code = param_write_null(plist, "pdfmark")) < 0) ||
385
905k
        (param_requested(plist, "DSC") > 0 &&
386
0
         (code = param_write_null(plist, "DSC")) < 0) ||
387
905k
        (code = gs_param_write_items(plist, pdev, NULL, pdf_param_items)) < 0
388
905k
        )
389
0
    {}
390
905k
    return code;
391
905k
}
392
393
/* ---------------- Put parameters ---------------- */
394
395
/* Put parameters, implementation */
396
static int
397
gdev_pdf_put_params_impl(gx_device * dev, const gx_device_pdf * save_dev, gs_param_list * plist)
398
372k
{
399
372k
    int ecode, code;
400
372k
    gx_device_pdf *pdev = (gx_device_pdf *) dev;
401
372k
    float cl = (float)pdev->CompatibilityLevel;
402
372k
    bool locked = pdev->params.LockDistillerParams, ForOPDFRead;
403
372k
    bool XRefStm_set = false, ObjStms_set = false;
404
372k
    gs_param_name param_name;
405
406
372k
    pdev->pdf_memory = gs_memory_stable(pdev->memory);
407
    /*
408
     * If this is a pseudo-parameter (pdfmark or DSC),
409
     * don't bother checking for any real ones.
410
     */
411
412
372k
    {
413
372k
        gs_param_string_array ppa;
414
372k
        gs_param_string pps;
415
416
372k
        code = param_read_string_array(plist, (param_name = "pdfmark"), &ppa);
417
372k
        switch (code) {
418
36.8k
            case 0:
419
36.8k
                code = pdfwrite_pdf_open_document(pdev);
420
36.8k
                if (code < 0)
421
0
                    return code;
422
36.8k
                code = pdfmark_process(pdev, &ppa);
423
36.8k
                if (code >= 0)
424
36.8k
                    return code;
425
                /* falls through for errors */
426
12
            default:
427
12
                param_signal_error(plist, param_name, code);
428
12
                return code;
429
335k
            case 1:
430
335k
                break;
431
372k
        }
432
433
335k
        code = param_read_string_array(plist, (param_name = "DSC"), &ppa);
434
335k
        switch (code) {
435
19.6k
            case 0:
436
19.6k
                code = pdfwrite_pdf_open_document(pdev);
437
19.6k
                if (code < 0)
438
0
                    return code;
439
19.6k
                code = pdf_dsc_process(pdev, &ppa);
440
19.6k
                if (code >= 0)
441
19.6k
                    return code;
442
                /* falls through for errors */
443
0
            default:
444
0
                param_signal_error(plist, param_name, code);
445
0
                return code;
446
316k
            case 1:
447
316k
                break;
448
335k
        }
449
450
316k
        code = param_read_string(plist, (param_name = "pdfpagelabels"), &pps);
451
316k
        switch (code) {
452
235
            case 0:
453
235
                {
454
235
                    if (!pdev->ForOPDFRead) {
455
235
                        cos_dict_t *const pcd = pdev->Catalog;
456
235
                        code = pdfwrite_pdf_open_document(pdev);
457
235
                        if (code < 0)
458
0
                            return code;
459
235
                        code = cos_dict_put_string(pcd, (const byte *)"/PageLabels", 11,
460
235
                                   pps.data, pps.size);
461
235
                        if (code >= 0)
462
235
                            return code;
463
235
                    } else
464
0
                        return 0;
465
235
                 }
466
                /* falls through for errors */
467
0
            default:
468
0
                param_signal_error(plist, param_name, code);
469
0
                return code;
470
316k
            case 1:
471
316k
                break;
472
316k
        }
473
316k
    }
474
475
#if OCR_VERSION > 0
476
    {
477
        int len;
478
        gs_param_string langstr;
479
        switch (code = param_read_string(plist, (param_name = "OCRLanguage"), &langstr)) {
480
            case 0:
481
                if (gs_is_path_control_active(pdev->memory)
482
                && (strlen(pdev->ocr_language) != langstr.size || memcmp(pdev->ocr_language, langstr.data, langstr.size) != 0)) {
483
                    return_error(gs_error_invalidaccess);
484
                }
485
                else {
486
                    len = langstr.size;
487
                    if (len >= sizeof(pdev->ocr_language))
488
                        len = sizeof(pdev->ocr_language)-1;
489
                    memcpy(pdev->ocr_language, langstr.data, len);
490
                    pdev->ocr_language[len] = 0;
491
                }
492
                break;
493
            case 1:
494
                break;
495
            default:
496
                ecode = code;
497
                param_signal_error(plist, param_name, ecode);
498
        }
499
    }
500
501
    {
502
        int engine;
503
        switch (code = param_read_int(plist, (param_name = "OCREngine"), &engine)) {
504
            case 0:
505
                pdev->ocr_engine = engine;
506
                break;
507
            case 1:
508
                break;
509
            default:
510
                ecode = code;
511
                param_signal_error(plist, param_name, ecode);
512
        }
513
    }
514
515
    {
516
        gs_param_string ocrstr;
517
518
        code = param_read_string(plist, (param_name = "UseOCR"), &ocrstr);
519
        switch(code) {
520
            case 0:
521
                if (ocrstr.size == 5 && memcmp(ocrstr.data, "Never", 5) == 0)
522
                    pdev->UseOCR = UseOCRNever;
523
                if (ocrstr.size == 8 && memcmp(ocrstr.data, "AsNeeded", 8) == 0)
524
                    pdev->UseOCR = UseOCRAsNeeded;
525
                if (ocrstr.size == 6 && memcmp(ocrstr.data, "Always", 6) == 0)
526
                    pdev->UseOCR = UseOCRAlways;
527
                break;
528
            case 1:
529
                break;
530
            default:
531
                param_signal_error(plist, param_name, code);
532
                break;
533
        }
534
    }
535
536
#endif
537
538
    /*
539
     * Check for LockDistillerParams before doing anything else.
540
     * If LockDistillerParams is true and is not being set to false,
541
     * ignore all resettings of PDF-specific parameters.  Note that
542
     * LockDistillerParams is read again, and reset if necessary, in
543
     * psdf_put_params.
544
     */
545
316k
    ecode = param_read_bool(plist, (param_name = "LockDistillerParams"), &locked);
546
316k
    if (ecode < 0)
547
0
        param_signal_error(plist, param_name, ecode);
548
549
    /* General parameters. */
550
551
316k
    {
552
316k
        int efo = 1;
553
554
316k
        ecode = param_put_int(plist, (param_name = ".EmbedFontObjects"), &efo, ecode);
555
316k
        if (ecode < 0)
556
0
            param_signal_error(plist, param_name, ecode);
557
316k
        if (efo != 1)
558
0
            param_signal_error(plist, param_name, ecode = gs_error_rangecheck);
559
316k
    }
560
316k
    {
561
316k
        int cdv = CoreDistVersion;
562
563
316k
        ecode = param_put_int(plist, (param_name = "CoreDistVersion"), &cdv, ecode);
564
316k
        if (ecode < 0)
565
0
            return gs_note_error(ecode);
566
316k
        if (cdv != CoreDistVersion)
567
0
            param_signal_error(plist, param_name, ecode = gs_error_rangecheck);
568
316k
    }
569
570
0
    switch (code = param_read_float(plist, (param_name = "CompatibilityLevel"), &cl)) {
571
0
        default:
572
0
            ecode = code;
573
0
            param_signal_error(plist, param_name, ecode);
574
0
            break;
575
45.5k
        case 0:
576
45.5k
            if (!(locked && pdev->params.LockDistillerParams)) {
577
                /*
578
                 * Must be 1.2, 1.3, 1.4, or 1.5.  Per Adobe documentation, substitute
579
                 * the nearest achievable value.
580
                 */
581
45.5k
                if (cl < (float)1.15)
582
4
                    cl = (float)1.1;
583
45.5k
                else if (cl < (float)1.25)
584
39.0k
                    cl = (float)1.2;
585
6.44k
                else if (cl < (float)1.35)
586
0
                    cl = (float)1.3;
587
6.44k
                else if (cl < (float)1.45)
588
24
                    cl = (float)1.4;
589
6.42k
                else if (cl < (float)1.55)
590
0
                    cl = (float)1.5;
591
6.42k
                else if (cl < (float)1.65)
592
0
                    cl = (float)1.6;
593
6.42k
                else if (cl < (float)1.75)
594
6.40k
                    cl = (float)1.7;
595
12
                else {
596
12
                    cl = (float)2.0;
597
12
                    if (pdev->params.TransferFunctionInfo == tfi_Preserve)
598
5
                        pdev->params.TransferFunctionInfo = tfi_Apply;
599
12
                }
600
45.5k
            }
601
316k
        case 1:
602
316k
            break;
603
316k
    }
604
605
316k
    ecode = param_read_bool(plist, (param_name = "WriteXRefStm"), &pdev->WriteXRefStm);
606
316k
    if (ecode < 0)
607
0
        param_signal_error(plist, param_name, ecode);
608
316k
    if (ecode == 0)
609
20.8k
        XRefStm_set = true;
610
316k
    ecode = param_read_bool(plist, (param_name = "WriteObjStms"), &pdev->WriteObjStms);
611
316k
    if (ecode < 0)
612
0
        param_signal_error(plist, param_name, ecode);
613
316k
    if (ecode == 0)
614
20.8k
        ObjStms_set = true;
615
616
316k
    {
617
316k
        code = gs_param_read_items(plist, pdev, pdf_param_items, pdev->pdf_memory);
618
316k
        if (code < 0 || (code = param_read_bool(plist, "ForOPDFRead", &ForOPDFRead)) < 0)
619
0
        {
620
0
        }
621
316k
        if (code == 0 && !pdev->is_ps2write && !(locked && pdev->params.LockDistillerParams))
622
6.40k
            pdev->ForOPDFRead = ForOPDFRead;
623
316k
    }
624
316k
    if (code < 0)
625
0
        ecode = code;
626
316k
    {
627
        /*
628
         * Setting FirstObjectNumber is only legal if the file
629
         * has just been opened and nothing has been written,
630
         * or if we are setting it to the same value.
631
         */
632
316k
        int64_t fon = pdev->FirstObjectNumber;
633
634
316k
        if (fon != save_dev->FirstObjectNumber) {
635
0
            if (fon <= 0 || fon > 0x7fff0000 ||
636
0
                (pdev->next_id != 0 &&
637
0
                 pdev->next_id !=
638
0
                 save_dev->FirstObjectNumber + pdf_num_initial_ids)
639
0
                ) {
640
0
                ecode = gs_error_rangecheck;
641
0
                param_signal_error(plist, "FirstObjectNumber", ecode);
642
0
            }
643
0
        }
644
316k
    }
645
316k
    {
646
        /*
647
         * Set ProcessColorModel now, because gx_default_put_params checks
648
         * it.
649
         */
650
316k
        static const char *const pcm_names[] = {
651
316k
            "DeviceGray", "DeviceRGB", "DeviceCMYK", "DeviceN", 0
652
316k
        };
653
316k
        int pcm = -1;
654
655
316k
        ecode = param_put_enum(plist, "ProcessColorModel", &pcm,
656
316k
                               pcm_names, ecode);
657
316k
        if (ecode < 0)
658
0
            goto fail;
659
316k
        if (pcm >= 0) {
660
20.8k
            pdf_set_process_color_model(pdev, pcm);
661
20.8k
            rc_decrement(pdev->icc_struct, "gdev_pdf_put_params_impl, ProcessColorModel changed");
662
20.8k
            pdev->icc_struct = 0;
663
20.8k
        }
664
316k
    }
665
666
316k
    if (pdev->is_ps2write && (code = param_read_bool(plist, "ProduceDSC", &pdev->ProduceDSC)) < 0) {
667
0
        param_signal_error(plist, param_name, code);
668
0
    }
669
670
316k
    if (pdev->OmitInfoDate && pdev->PDFX != 0) {
671
0
        emprintf(pdev->memory, "\nIt is not possible to omit the CreationDate when creating PDF/X\nOmitInfoDate is being ignored.\n");
672
0
        pdev->OmitInfoDate = 0;
673
0
    }
674
316k
    if (pdev->OmitID && pdev->PDFX != 0) {
675
0
        emprintf(pdev->memory, "\nIt is not possible to omit the ID array when creating PDF/X\nOmitID is being ignored.\n");
676
0
        pdev->OmitID = 0;
677
0
    }
678
316k
    if (pdev->OmitID && pdev->CompatibilityLevel > 1.7) {
679
0
        emprintf(pdev->memory, "\nIt is not possible to omit the ID array when creating a version 2.0 or greater PDF\nOmitID is being ignored.\n");
680
0
        pdev->OmitID = 0;
681
0
    }
682
316k
    if (pdev->OmitID && pdev->OwnerPassword.size != 0) {
683
0
        emprintf(pdev->memory, "\nIt is not possible to omit the ID array when creating an encrypted PDF\nOmitID is being ignored.\n");
684
0
        pdev->OmitID = 0;
685
0
    }
686
687
316k
    if (pdev->OmitXMP && pdev->PDFA != 0) {
688
0
        emprintf(pdev->memory, "\nIt is not possible to omit the XMP metadta when creating a PDF/A\nOmitXMP is being ignored.\n");
689
0
        pdev->OmitXMP = 0;
690
0
    }
691
692
    /* PDFA and PDFX are stored in the page device dictionary and therefore
693
     * set on every setpagedevice. However, if we have encountered a file which
694
     * can't be made this way, and the PDFACompatibilityPolicy is 1, we want to
695
     * continue producing the file, but not as a PDF/A or PDF/X file. Its more
696
     * or less impossible to alter the setting in the (potentially saved) page
697
     * device dictionary, so we use this rather clunky method.
698
     */
699
316k
    if (pdev->PDFA < 0 || pdev->PDFA > 3){
700
0
        ecode = gs_note_error(gs_error_rangecheck);
701
0
        param_signal_error(plist, "PDFA", ecode);
702
0
        goto fail;
703
0
    }
704
316k
    if(pdev->PDFA != 0 && pdev->AbortPDFAX)
705
0
        pdev->PDFA = 0;
706
316k
    if(pdev->PDFX && pdev->AbortPDFAX)
707
0
        pdev->PDFX = 0;
708
316k
    if (pdev->PDFX && pdev->PDFA != 0) {
709
0
        ecode = gs_note_error(gs_error_rangecheck);
710
0
        param_signal_error(plist, "PDFA", ecode);
711
0
        goto fail;
712
0
    }
713
316k
    if (pdev->PDFX != 0 && pdev->ForOPDFRead) {
714
0
        ecode = gs_note_error(gs_error_rangecheck);
715
0
        param_signal_error(plist, "PDFX", ecode);
716
0
        goto fail;
717
0
    }
718
316k
    if (pdev->PDFA != 0 && pdev->ForOPDFRead) {
719
0
        ecode = gs_note_error(gs_error_rangecheck);
720
0
        param_signal_error(plist, "PDFA", ecode);
721
0
        goto fail;
722
0
    }
723
316k
    if (pdev->PDFA == 1 || (pdev->PDFX > 0 && pdev->PDFX < 4) || pdev->CompatibilityLevel < 1.4) {
724
194k
         pdev->HaveTransparency = false;
725
194k
         pdev->PreserveSMask = false;
726
194k
         pdev->WantsOptionalContent = false;
727
194k
    }
728
729
    /* We cannot guarantee that all page objects have a Group with a /CS (ColorSpace) entry
730
     * which is a requirement for PDF/A-2+ when objects are defined in Device Independent Colour.
731
     * Try and warn the user.
732
     */
733
316k
    if (pdev->PDFA > 1 && pdev->params.ColorConversionStrategy == ccs_UseDeviceIndependentColor) {
734
0
        switch (pdev->PDFACompatibilityPolicy) {
735
0
            case 0:
736
0
                emprintf(pdev->memory,
737
0
                 "\n\tpdfwrite cannot guarantee creating a conformant PDF/A-2 file with device-independent colour.\n\tWe recommend converting to a device colour space.\n\tReverting to normal output.\n");
738
0
                pdev->AbortPDFAX = true;
739
0
                pdev->PDFA = 0;
740
0
                break;
741
0
            case 1:
742
0
                emprintf(pdev->memory,
743
0
                 "\n\tpdfwrite cannot guarantee creating a conformant PDF/A-2 file with device-independent colour.\n\tWe recommend converting to a device colour space.\n\tWe cannot ignore this request, reverting to normal output.\n");
744
0
                break;
745
0
            case 2:
746
0
                emprintf(pdev->memory,
747
0
                 "\n\tpdfwrite cannot guarantee creating a conformant PDF/A-2 file with device-independent colour.\n\tWe recommend converting to a device colour space.\n\tAborting.\n");
748
0
                return_error(gs_error_unknownerror);
749
0
                break;
750
0
            default:
751
0
                emprintf(pdev->memory,
752
0
                 "\n\tpdfwrite cannot guarantee creating a conformant PDF/A-2 file with device-independent colour.\n\tWe recommend converting to a device colour space.\n\tReverting to normal output.\n");
753
0
                pdev->AbortPDFAX = true;
754
0
                pdev->PDFA = 0;
755
0
                break;
756
0
        }
757
0
    }
758
    /*
759
     * We have to set version to the new value, because the set of
760
     * legal parameter values for psdf_put_params varies according to
761
     * the version.
762
     */
763
316k
    if (pdev->PDFX != 0) {
764
0
        if (pdev->PDFX < 4) {
765
0
            cl = (float)1.3; /* Instead pdev->CompatibilityLevel = 1.2; - see below. */
766
0
            if (pdev->WriteObjStms && ObjStms_set)
767
0
                emprintf(pdev->memory, "Can't use ObjStm before PDF 1.5, PDF/X does not support PDF 1.5, ignoring WriteObjStms directive\n");
768
0
            if (pdev->WriteXRefStm && XRefStm_set)
769
0
                emprintf(pdev->memory, "Can't use an XRef stream before PDF 1.5, PDF/X does not support PDF 1.5, ignoring WriteXRefStm directive\n");
770
771
0
            pdev->WriteObjStms = false;
772
0
            pdev->WriteXRefStm = false;
773
0
        } else {
774
0
            cl = (float)1.6; /* Instead pdev->CompatibilityLevel = 1.2; - see below. */
775
0
        }
776
0
    }
777
316k
    if (pdev->PDFA == 1 && cl != 1.4) {
778
0
        cl = (float)1.4;
779
780
0
        if (pdev->WriteObjStms && ObjStms_set)
781
0
            emprintf(pdev->memory, "Can't use ObjStm before PDF 1.5, PDF/A-1 does not support PDF 1.5, ignoring WriteObjStms directive\n");
782
0
        if (pdev->WriteXRefStm && XRefStm_set)
783
0
            emprintf(pdev->memory, "Can't use an XRef stream before PDF 1.5, PDF/A-1 does not support PDF 1.5, ignoring WriteXRefStm directive\n");
784
0
        pdev->WriteObjStms = false;
785
0
        pdev->WriteXRefStm = false;
786
0
    }
787
316k
    if (pdev->PDFA == 2 && cl < 1.7)
788
0
        cl = (float)1.7;
789
316k
    pdev->version = (cl < 1.2 ? psdf_version_level2 : psdf_version_ll3);
790
316k
    if (pdev->ForOPDFRead) {
791
218k
        pdev->ResourcesBeforeUsage = true;
792
218k
        pdev->HaveCFF = false;
793
218k
        pdev->HavePDFWidths = false;
794
218k
        pdev->HaveStrokeColor = false;
795
218k
        cl = (float)1.2; /* Instead pdev->CompatibilityLevel = 1.2; - see below. */
796
218k
        pdev->MaxInlineImageSize = max_long; /* Save printer's RAM from saving temporary image data.
797
                                                Immediate images doen't need buffering. */
798
218k
        pdev->version = psdf_version_level2;
799
218k
    } else {
800
97.3k
        pdev->ResourcesBeforeUsage = false;
801
97.3k
        pdev->HaveCFF = true;
802
97.3k
        pdev->HavePDFWidths = true;
803
97.3k
        pdev->HaveStrokeColor = true;
804
97.3k
    }
805
316k
    pdev->ParamCompatibilityLevel = cl;
806
316k
    if (cl < 1.2) {
807
0
        pdev->HaveCFF = false;
808
0
    }
809
316k
    if (cl < 1.5)
810
218k
        pdev->WantsOptionalContent = false;
811
812
316k
    ecode = param_read_float(plist, "UserUnit", &pdev->UserUnit);
813
316k
    if (ecode < 0)
814
0
        goto fail;
815
316k
    if (pdev->UserUnit == 0 || (pdev->UserUnit != 1 && pdev->CompatibilityLevel < 1.6)) {
816
0
        ecode = gs_note_error(gs_error_rangecheck);
817
0
        param_signal_error(plist, "UserUnit", ecode);
818
0
        goto fail;
819
0
    }
820
821
316k
    ecode = gdev_psdf_put_params(dev, plist);
822
316k
    if (ecode < 0)
823
336
        goto fail;
824
825
315k
    if (pdev->CompatibilityLevel > 1.7 && pdev->params.TransferFunctionInfo == tfi_Preserve) {
826
0
        pdev->params.TransferFunctionInfo = tfi_Apply;
827
0
        emprintf(pdev->memory, "\nIt is not possible to preserve transfer functions in PDF 2.0\ntransfer functions will be applied instead\n");
828
0
    }
829
830
315k
    if (pdev->params.ConvertCMYKImagesToRGB) {
831
0
        if (pdev->params.ColorConversionStrategy == ccs_CMYK) {
832
0
            emprintf(pdev->memory, "ConvertCMYKImagesToRGB is not compatible with ColorConversionStrategy of CMYK\n");
833
0
        } else {
834
0
            if (pdev->params.ColorConversionStrategy == ccs_Gray) {
835
0
                emprintf(pdev->memory, "ConvertCMYKImagesToRGB is not compatible with ColorConversionStrategy of Gray\n");
836
0
            } else {
837
0
                pdf_set_process_color_model(pdev,1);
838
0
                ecode = gsicc_init_device_profile_struct((gx_device *)pdev, NULL, 0);
839
0
                if (ecode < 0)
840
0
                    goto fail;
841
0
            }
842
0
        }
843
0
    }
844
    /* Record if the user has specified a custom profile, so we don't replace it
845
       with a default when we change color space - see win_pr2_set_bpp()
846
       Here, we only want to know *if* we have a user specced profile, actually
847
       setting it will be handled by gdev_vector_put_params()
848
     */
849
315k
    if (!pdev->user_icc) {
850
282k
        gs_param_string icc_pro_dummy;
851
852
282k
        pdev->user_icc = param_read_string(plist, "OutputICCProfile", &icc_pro_dummy) == 0;
853
282k
    }
854
315k
    switch (pdev->params.ColorConversionStrategy) {
855
0
        case ccs_ByObjectType:
856
315k
        case ccs_LeaveColorUnchanged:
857
315k
            break;
858
0
        case ccs_UseDeviceIndependentColor:
859
0
        case ccs_UseDeviceIndependentColorForImages:
860
0
            pdev->params.TransferFunctionInfo = tfi_Apply;
861
0
            break;
862
0
        case ccs_CMYK:
863
0
            pdf_set_process_color_model(pdev, 2);
864
0
            if (!pdev->user_icc) {
865
0
                ecode = gsicc_init_device_profile_struct((gx_device *)pdev, NULL, 0);
866
0
                if (ecode < 0)
867
0
                    goto fail;
868
0
            }
869
0
            break;
870
0
        case ccs_Gray:
871
0
            pdf_set_process_color_model(pdev,0);
872
0
            if (!pdev->user_icc) {
873
0
                ecode = gsicc_init_device_profile_struct((gx_device *)pdev, NULL, 0);
874
0
                if (ecode < 0)
875
0
                    goto fail;
876
0
            }
877
0
            break;
878
0
        case ccs_sRGB:
879
0
        case ccs_RGB:
880
            /* Only bother to do this if we didn't handle it above */
881
0
            if (!pdev->params.ConvertCMYKImagesToRGB) {
882
0
                pdf_set_process_color_model(pdev,1);
883
0
                if (!pdev->user_icc) {
884
0
                    ecode = gsicc_init_device_profile_struct((gx_device *)pdev, NULL, 0);
885
0
                    if (ecode < 0)
886
0
                        goto fail;
887
0
                }
888
0
            }
889
0
            break;
890
0
        default:
891
0
            break;
892
315k
    }
893
315k
    if (cl < 1.5f && pdev->params.ColorImage.Filter != NULL &&
894
169k
            !strcmp(pdev->params.ColorImage.Filter, "JPXEncode")) {
895
0
        emprintf(pdev->memory,
896
0
                 "JPXEncode requires CompatibilityLevel >= 1.5 .\n");
897
0
        ecode = gs_note_error(gs_error_rangecheck);
898
0
    }
899
315k
    if (cl < 1.5f && pdev->params.GrayImage.Filter != NULL &&
900
169k
            !strcmp(pdev->params.GrayImage.Filter, "JPXEncode")) {
901
0
        emprintf(pdev->memory,
902
0
                 "JPXEncode requires CompatibilityLevel >= 1.5 .\n");
903
0
        ecode = gs_note_error(gs_error_rangecheck);
904
0
    }
905
315k
    if (cl < 1.4f && pdev->params.MonoImage.Filter != NULL &&
906
218k
            !strcmp(pdev->params.MonoImage.Filter, "JBIG2Encode")) {
907
0
        emprintf(pdev->memory,
908
0
                 "JBIG2Encode requires CompatibilityLevel >= 1.4 .\n");
909
0
        ecode = gs_note_error(gs_error_rangecheck);
910
0
    }
911
315k
    if (pdev->HaveTrueTypes && pdev->version == psdf_version_level2) {
912
218k
        pdev->version = psdf_version_level2_with_TT ;
913
218k
    }
914
315k
    if (ecode < 0)
915
0
        goto fail;
916
917
315k
    if (pdev->FirstObjectNumber != save_dev->FirstObjectNumber) {
918
0
        if (pdev->xref.file != 0) {
919
0
            if (gp_fseek(pdev->xref.file, 0L, SEEK_SET) != 0) {
920
0
                ecode = gs_error_ioerror;
921
0
                goto fail;
922
0
            }
923
0
            pdf_initialize_ids(pdev);
924
0
        }
925
0
    }
926
    /* Handle the float/double mismatch. */
927
315k
    pdev->CompatibilityLevel = (int)(cl * 10 + 0.5) / 10.0;
928
929
315k
    if (pdev->ForOPDFRead && pdev->OwnerPassword.size != 0) {
930
0
        emprintf(pdev->memory, "\n\tSetting OwnerPassword for PostScript output would result in an encrypted\n\tunusable PostScript file, ignoring.\n");
931
0
        pdev->OwnerPassword.size = 0;
932
0
    }
933
934
315k
    if(pdev->OwnerPassword.size != save_dev->OwnerPassword.size ||
935
315k
        (pdev->OwnerPassword.size != 0 &&
936
0
         memcmp(pdev->OwnerPassword.data, save_dev->OwnerPassword.data,
937
0
         pdev->OwnerPassword.size) != 0)) {
938
0
        if (pdev->is_open) {
939
0
            if (pdev->PageCount == 0) {
940
0
                gs_closedevice((gx_device *)pdev);
941
0
                return 0;
942
0
            }
943
0
            else
944
0
                emprintf(pdev->memory, "Owner Password changed mid-job, ignoring.\n");
945
0
        }
946
0
    }
947
948
315k
    if (pdev->Linearise && pdev->file != NULL && !gp_fseekable(pdev->file)) {
949
0
        emprintf(pdev->memory, "Can't linearise a non-seekable output file, ignoring\n");
950
0
        pdev->Linearise = false;
951
0
    }
952
953
315k
    if (pdev->Linearise && pdev->is_ps2write) {
954
0
        emprintf(pdev->memory, "Can't linearise PostScript output, ignoring\n");
955
0
        pdev->Linearise = false;
956
0
    }
957
958
315k
    if (pdev->Linearise && pdev->OwnerPassword.size != 0) {
959
0
        emprintf(pdev->memory, "Can't linearise encrypted PDF, ignoring\n");
960
0
        pdev->Linearise = false;
961
0
    }
962
963
    /* Object streams and XRef streams */
964
315k
    if (pdev->is_ps2write) {
965
218k
        pdev->WriteObjStms = false;
966
218k
        pdev->WriteXRefStm = false;
967
218k
    } else {
968
97.3k
        if (pdev->Linearise) {
969
0
            if (XRefStm_set)
970
0
                emprintf(pdev->memory, "We don't support XRefStm with FastWebView (Linearized PDF), ignoring WriteXRefStm directive\n");
971
0
            pdev->WriteXRefStm = false;
972
0
            if (ObjStms_set)
973
0
                emprintf(pdev->memory, "We don't support ObjStms with FastWebView (Linearized PDF), ignoring WriteObjStms directive\n");
974
0
            pdev->WriteObjStms = false;
975
0
        }
976
97.3k
    }
977
315k
    if (pdev->WriteObjStms && pdev->CompatibilityLevel < 1.5) {
978
0
        if (ObjStms_set)
979
0
            emprintf(pdev->memory, "Can't use Object streams before PDF 1.5, ignoring WriteObjStms directive\n");
980
0
        pdev->WriteObjStms = false;
981
0
    }
982
315k
    if (pdev->WriteXRefStm && pdev->CompatibilityLevel < 1.5) {
983
0
        if (XRefStm_set)
984
0
            emprintf(pdev->memory, "Can't use an XRef stream before PDF 1.5, ignoring WriteXRefStm directive\n");
985
0
        pdev->WriteXRefStm = false;
986
0
    }
987
315k
    if (pdev->WriteObjStms && !pdev->WriteXRefStm) {
988
0
        if (ObjStms_set)
989
0
            emprintf(pdev->memory, "Can't use Object streams without XRef stream, ignoring WriteObjStms directive\n");
990
0
        pdev->WriteObjStms = false;
991
0
    }
992
315k
    if (pdev->WriteObjStms) {
993
97.3k
        if (pdev->next_id == 1)
994
0
            pdev->doubleXref = true;
995
97.3k
        if (!pdev->doubleXref) {
996
0
            emprintf(pdev->memory, "Already started writing output, too late to select WriteObjStms, ignoring directive\n");
997
0
            pdev->WriteObjStms = false;
998
0
        }
999
97.3k
    }
1000
1001
315k
    if (pdev->FlattenFonts)
1002
0
        pdev->PreserveTrMode = false;
1003
1004
315k
    if (pdev->ForOPDFRead)
1005
218k
        pdev->params.UseBrotliCompression = pdev->UseBrotli = false;
1006
97.3k
    else
1007
97.3k
        pdev->params.UseBrotliCompression = pdev->UseBrotli;
1008
1009
315k
    return 0;
1010
336
 fail:
1011
    /* Restore all the parameters to their original state. */
1012
336
    pdev->version = save_dev->version;
1013
336
    pdf_set_process_color_model(pdev, save_dev->pcm_color_info_index);
1014
336
    pdev->saved_fill_color = save_dev->saved_fill_color;
1015
336
    pdev->saved_stroke_color = save_dev->saved_fill_color;
1016
336
    {
1017
336
        const gs_param_item_t *ppi = pdf_param_items;
1018
1019
25.2k
        for (; ppi->key; ++ppi)
1020
24.8k
            memcpy((char *)pdev + ppi->offset,
1021
24.8k
                   (char *)save_dev + ppi->offset,
1022
24.8k
                   gs_param_type_sizes[ppi->type]);
1023
336
        pdev->ForOPDFRead = save_dev->ForOPDFRead;
1024
336
    }
1025
336
    return ecode;
1026
315k
}
1027
1028
/* Put parameters */
1029
int
1030
gdev_pdf_put_params(gx_device * dev, gs_param_list * plist)
1031
372k
{
1032
372k
    int code;
1033
372k
    gx_device_pdf *pdev = (gx_device_pdf *) dev;
1034
372k
    gs_memory_t *mem = gs_memory_stable(pdev->memory);
1035
372k
    gx_device_pdf *save_dev = gs_malloc(mem, sizeof(gx_device_pdf), 1,
1036
372k
        "saved gx_device_pdf");
1037
1038
372k
    if (!save_dev)
1039
0
        return_error(gs_error_VMerror);
1040
372k
    memcpy(save_dev, pdev, sizeof(gx_device_pdf));
1041
372k
    code = gdev_pdf_put_params_impl(dev, save_dev, plist);
1042
372k
    gs_free(mem, save_dev, sizeof(gx_device_pdf), 1, "saved gx_device_pdf");
1043
372k
    return code;
1044
372k
}
1045
1046
/* ---------------- Process DSC comments ---------------- */
1047
1048
/* Bug #695850 DSC comments are not actually encoded, nor are they PostScript strings
1049
 * they are simply a sequence of bytes. SO it would seem we should just preserve that
1050
 * byte sequence. Bizarrely, Distiller treats the comment as 'almost' a PostScript
1051
 * string. In particular it converts octal codes into an equivalent binary byte. It
1052
 * also converts (eg) '\n' and '\r' into 'n' and 'r' and invalid octal escapes
1053
 * (eg \11) simply have the '\' turned into a '?'.
1054
 * We think this is nuts and have no intention of trying to mimic it precisely. This
1055
 * routine will find octal escapes and convert them into binary. The way this works is
1056
 * a little obscure. The DSC parser does convert the comment into a PostScript string
1057
 * and so has to escape any unusual characters. This means our octal escaped values in
1058
 * the original DSC comment have had the escape character ('\') escaped to become '\\'.
1059
 * All we need to do is remove the escape of the escape and we will end up with a
1060
 * properly escaped PostScript string.
1061
 */
1062
static int unescape_octals(gx_device_pdf * pdev, char *src, int size)
1063
10.6k
{
1064
10.6k
    char *start, *dest;
1065
1066
10.6k
    start = src;
1067
10.6k
    dest = src;
1068
1069
510k
    while(size) {
1070
499k
        if (size > 4 && src[0] == '\\' && src[1] == '\\' &&
1071
3.29k
                src[2] > 0x29 && src[2] < 0x35 &&
1072
62
                src[3] > 0x29 &&src[3] < 0x38 &&
1073
44
                src[4] > 0x29 && src[4] < 0x38) {
1074
44
            src++;
1075
44
            size--;
1076
499k
        } else {
1077
499k
            *dest++ = *src++;
1078
499k
            size--;
1079
499k
        }
1080
499k
    }
1081
10.6k
    return (dest - start);
1082
10.6k
}
1083
1084
static int
1085
pdf_dsc_process(gx_device_pdf * pdev, const gs_param_string_array * pma)
1086
19.6k
{
1087
    /*
1088
     * The Adobe "Distiller Parameters" documentation says that Distiller
1089
     * looks at DSC comments, but it doesn't say which ones.  We look at
1090
     * the ones that we see how to map directly to obvious PDF constructs.
1091
     */
1092
19.6k
    int code = 0;
1093
19.6k
    uint i;
1094
1095
    /*
1096
     * If ParseDSCComments is false, all DSC comments are ignored, even if
1097
     * ParseDSCComentsForDocInfo or PreserveEPSInfo is true.
1098
     */
1099
19.6k
    if (!pdev->ParseDSCComments)
1100
0
        return 0;
1101
1102
38.9k
    for (i = 0; i + 1 < pma->size && code >= 0; i += 2) {
1103
19.3k
        const gs_param_string *pkey = &pma->data[i];
1104
19.3k
        gs_param_string *pvalue = (gs_param_string *)&pma->data[i + 1];
1105
19.3k
        const char *key;
1106
19.3k
        int newsize;
1107
1108
        /*
1109
         * %%For, %%Creator, and %%Title are recognized only if either
1110
         * ParseDSCCommentsForDocInfo or PreserveEPSInfo is true.
1111
         * The other DSC comments are always recognized.
1112
         *
1113
         * Acrobat Distiller sets CreationDate and ModDate to the current
1114
         * time, not the value of %%CreationDate.  We think this is wrong,
1115
         * but we do the same -- we ignore %%CreationDate here.
1116
         */
1117
1118
19.3k
        if (pdf_key_eq(pkey, "Creator")) {
1119
3.64k
            key = "/Creator";
1120
3.64k
            newsize = unescape_octals(pdev, (char *)pvalue->data, pvalue->size);
1121
3.64k
            code = cos_dict_put_c_key_string(pdev->Info, key,
1122
3.64k
                                             pvalue->data, newsize);
1123
3.64k
            continue;
1124
15.6k
        } else if (pdf_key_eq(pkey, "Title")) {
1125
6.50k
            key = "/Title";
1126
6.50k
            newsize = unescape_octals(pdev, (char *)pvalue->data, pvalue->size);
1127
6.50k
            code = cos_dict_put_c_key_string(pdev->Info, key,
1128
6.50k
                                             pvalue->data, newsize);
1129
6.50k
            continue;
1130
9.17k
        } else if (pdf_key_eq(pkey, "For")) {
1131
543
            key = "/Author";
1132
543
            newsize = unescape_octals(pdev, (char *)pvalue->data, pvalue->size);
1133
543
            code = cos_dict_put_c_key_string(pdev->Info, key,
1134
543
                                             pvalue->data, newsize);
1135
543
            continue;
1136
8.62k
        } else {
1137
8.62k
            pdf_page_dsc_info_t *ppdi;
1138
8.62k
            char scan_buf[200]; /* arbitrary */
1139
1140
8.62k
            if ((ppdi = &pdev->doc_dsc_info,
1141
8.62k
                 pdf_key_eq(pkey, "Orientation")) ||
1142
8.62k
                (ppdi = &pdev->page_dsc_info,
1143
8.62k
                 pdf_key_eq(pkey, "PageOrientation"))
1144
8.62k
                ) {
1145
1
                if (pvalue->size == 1 && pvalue->data[0] >= '0' &&
1146
1
                    pvalue->data[0] <= '3'
1147
1
                    )
1148
1
                    ppdi->orientation = pvalue->data[0] - '0';
1149
0
                else
1150
0
                    ppdi->orientation = -1;
1151
8.62k
            } else if ((ppdi = &pdev->doc_dsc_info,
1152
8.62k
                        pdf_key_eq(pkey, "ViewingOrientation")) ||
1153
8.62k
                       (ppdi = &pdev->page_dsc_info,
1154
8.62k
                        pdf_key_eq(pkey, "PageViewingOrientation"))
1155
8.62k
                       ) {
1156
0
                gs_matrix mat;
1157
0
                int orient;
1158
1159
0
                if(pvalue->size >= sizeof(scan_buf) - 1)
1160
0
                    continue; /* error */
1161
0
                memcpy(scan_buf, pvalue->data, pvalue->size);
1162
0
                scan_buf[pvalue->size] = 0;
1163
0
                if (sscanf(scan_buf, "[%g %g %g %g]",
1164
0
                           &mat.xx, &mat.xy, &mat.yx, &mat.yy) != 4
1165
0
                    )
1166
0
                    continue; /* error */
1167
0
                for (orient = 0; orient < 4; ++orient) {
1168
0
                    if (mat.xx == 1 && mat.xy == 0 && mat.yx == 0 && mat.yy == 1)
1169
0
                        break;
1170
0
                    gs_matrix_rotate(&mat, -90.0, &mat);
1171
0
                }
1172
0
                if (orient == 4) /* error */
1173
0
                    orient = -1;
1174
0
                ppdi->viewing_orientation = orient;
1175
8.62k
            } else {
1176
8.62k
                gs_rect box;
1177
1178
8.62k
                if (pdf_key_eq(pkey, "EPSF")) {
1179
1.34k
                    pdev->is_EPS = (pvalue->size >= 1 && pvalue->data[0] != '0');
1180
1.34k
                    continue;
1181
1.34k
                }
1182
                /*
1183
                 * We only parse the BoundingBox for the sake of
1184
                 * AutoPositionEPSFiles.
1185
                 */
1186
7.28k
                if (pdf_key_eq(pkey, "BoundingBox"))
1187
3.13k
                    ppdi = &pdev->doc_dsc_info;
1188
4.14k
                else if (pdf_key_eq(pkey, "PageBoundingBox"))
1189
0
                    ppdi = &pdev->page_dsc_info;
1190
4.14k
                else
1191
4.14k
                    continue;
1192
3.13k
                if(pvalue->size >= sizeof(scan_buf) - 1)
1193
0
                    continue; /* error */
1194
3.13k
                memcpy(scan_buf, pvalue->data, pvalue->size);
1195
3.13k
                scan_buf[pvalue->size] = 0;
1196
3.13k
                if (sscanf(scan_buf, "[%lg %lg %lg %lg]",
1197
3.13k
                           &box.p.x, &box.p.y, &box.q.x, &box.q.y) != 4
1198
3.13k
                    )
1199
0
                    continue; /* error */
1200
3.13k
                ppdi->bounding_box = box;
1201
3.13k
            }
1202
3.14k
            continue;
1203
8.62k
        }
1204
19.3k
    }
1205
19.6k
    return code;
1206
19.6k
}