Coverage Report

Created: 2025-06-24 07:01

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