Coverage Report

Created: 2026-11-12 07:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ghostpdl/devices/vector/gdevpdfo.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
/* Cos object support */
18
#include "memory_.h"
19
#include "gx.h"
20
#include "gserrors.h"
21
#include "gsparam.h"
22
#include "gsutil.h"   /* for bytes_compare */
23
#include "gdevpdfx.h"
24
#include "gdevpdfo.h"
25
#include "strimpl.h"
26
#include "sa85x.h"
27
#include "sarc4.h"
28
#include "sstring.h"
29
30
#define CHECK(expr)\
31
212k
  BEGIN if ((code = (expr)) < 0) return code; END
32
33
/* ---------------- Structure definitions ---------------- */
34
35
/*
36
 * Define the generic structure for elements of arrays and
37
 * dictionaries/streams.
38
 */
39
#define cos_element_common(etype)\
40
    etype *next
41
struct cos_element_s {
42
    cos_element_common(cos_element_t);
43
};
44
#define private_st_cos_element()  /* in gdevpdfo.c */\
45
  gs_private_st_ptrs1(st_cos_element, cos_element_t, "cos_element_t",\
46
    cos_element_enum_ptrs, cos_element_reloc_ptrs, next)
47
4.16M
#define cos_element_num_ptrs 1
48
49
/*
50
 * Define the structure for a piece of stream contents.
51
 */
52
struct cos_stream_piece_s {
53
    cos_element_common(cos_stream_piece_t);
54
    gs_offset_t position;   /* in streams file */
55
    uint size;
56
};
57
#define private_st_cos_stream_piece() /* in gdevpdfo.c */\
58
  gs_private_st_suffix_add0_local(st_cos_stream_piece, cos_stream_piece_t,\
59
    "cos_stream_piece_t", cos_element_enum_ptrs, cos_element_reloc_ptrs,\
60
    st_cos_element)
61
62
/*
63
 * Define Cos arrays, dictionaries, and streams.
64
 */
65
     /* array */
66
struct cos_array_element_s {
67
    cos_element_common(cos_array_element_t);
68
    long index;
69
    cos_value_t value;
70
};
71
#define private_st_cos_array_element()  /* in gdevpdfo.c */\
72
  gs_private_st_composite(st_cos_array_element, cos_array_element_t,\
73
    "cos_array_element_t", cos_array_element_enum_ptrs, cos_array_element_reloc_ptrs)
74
    /* dict */
75
struct cos_dict_element_s {
76
    cos_element_common(cos_dict_element_t);
77
    gs_string key;
78
    bool owns_key;  /* if false, key is shared, do not trace or free */
79
    cos_value_t value;
80
};
81
#define private_st_cos_dict_element() /* in gdevpdfo.c */\
82
  gs_private_st_composite(st_cos_dict_element, cos_dict_element_t,\
83
    "cos_dict_element_t", cos_dict_element_enum_ptrs, cos_dict_element_reloc_ptrs)
84
85
/* GC descriptors */
86
private_st_cos_element();
87
private_st_cos_stream_piece();
88
private_st_cos_object();
89
private_st_cos_value();
90
private_st_cos_array_element();
91
private_st_cos_dict_element();
92
93
/* GC procedures */
94
static
95
1.32M
ENUM_PTRS_WITH(cos_value_enum_ptrs, cos_value_t *pcv) return 0;
96
686k
 case 0:
97
686k
    switch (pcv->value_type) {
98
429k
    case COS_VALUE_SCALAR:
99
429k
        return ENUM_STRING(&pcv->contents.chars);
100
52.6k
    case COS_VALUE_CONST:
101
52.6k
        break;
102
204k
    case COS_VALUE_OBJECT:
103
204k
    case COS_VALUE_RESOURCE:
104
204k
        return ENUM_OBJ(pcv->contents.object);
105
686k
    }
106
52.6k
    return 0;
107
1.32M
ENUM_PTRS_END
108
static
109
686k
RELOC_PTRS_WITH(cos_value_reloc_ptrs, cos_value_t *pcv)
110
686k
{
111
686k
    switch (pcv->value_type) {
112
429k
    case COS_VALUE_SCALAR:
113
429k
        RELOC_STRING_VAR(pcv->contents.chars);
114
482k
    case COS_VALUE_CONST:
115
482k
        break;
116
204k
    case COS_VALUE_OBJECT:
117
204k
    case COS_VALUE_RESOURCE:
118
204k
        RELOC_VAR(pcv->contents.object);
119
204k
        break;
120
686k
    }
121
686k
}
122
686k
RELOC_PTRS_END
123
static
124
400k
ENUM_PTRS_WITH(cos_array_element_enum_ptrs, cos_array_element_t *pcae)
125
400k
{
126
400k
    return (index < cos_element_num_ptrs ?
127
400k
            ENUM_USING_PREFIX(st_cos_element, 0) :
128
400k
            ENUM_USING(st_cos_value, &pcae->value, sizeof(cos_value_t),
129
400k
                       index - cos_element_num_ptrs));
130
0
}
131
400k
ENUM_PTRS_END
132
static
133
139k
RELOC_PTRS_WITH(cos_array_element_reloc_ptrs, cos_array_element_t *pcae)
134
139k
{
135
139k
    RELOC_PREFIX(st_cos_element);
136
139k
    RELOC_USING(st_cos_value, &pcae->value, sizeof(cos_value_t));
137
139k
}
138
139k
RELOC_PTRS_END
139
static
140
2.15M
ENUM_PTRS_WITH(cos_dict_element_enum_ptrs, cos_dict_element_t *pcde)
141
2.15M
{
142
2.15M
    return (index < cos_element_num_ptrs ?
143
2.15M
            ENUM_USING_PREFIX(st_cos_element, 0) :
144
2.15M
            (index -= cos_element_num_ptrs) > 0 ?
145
1.06M
            ENUM_USING(st_cos_value, &pcde->value, sizeof(cos_value_t),
146
1.60M
                       index - 1) :
147
1.60M
            pcde->owns_key ? ENUM_STRING(&pcde->key) : ENUM_OBJ(NULL));
148
0
}
149
2.15M
ENUM_PTRS_END
150
static
151
547k
RELOC_PTRS_WITH(cos_dict_element_reloc_ptrs, cos_dict_element_t *pcde)
152
547k
{
153
547k
    RELOC_PREFIX(st_cos_element);
154
547k
    if (pcde->owns_key)
155
173k
        RELOC_STRING_VAR(pcde->key);
156
547k
    RELOC_USING(st_cos_value, &pcde->value, sizeof(cos_value_t));
157
547k
}
158
547k
RELOC_PTRS_END
159
160
static int unescape_name(char *data, uint *size);
161
162
/* ---------------- Generic support ---------------- */
163
164
/* Initialize a just-allocated cos object. */
165
static void
166
cos_object_init(cos_object_t *pco, gx_device_pdf *pdev,
167
                const cos_object_procs_t *procs)
168
1.30M
{
169
1.30M
    if (pco) {
170
1.30M
        pco->cos_procs = procs;
171
1.30M
        pco->id = 0;
172
1.30M
        pco->elements = 0;
173
1.30M
        pco->pieces = 0;
174
1.30M
        pco->mem = pdev->pdf_memory;
175
1.30M
        pco->pres = 0;
176
1.30M
        pco->is_open = true;
177
1.30M
        pco->is_graphics = false;
178
1.30M
        pco->written = false;
179
1.30M
        pco->length = 0;
180
1.30M
        pco->input_strm = 0;
181
1.30M
        pco->md5_valid = 0;
182
1.30M
        pco->stream_md5_valid = 0;
183
1.30M
        memset(&pco->hash, 0x00, 16);
184
1.30M
    }
185
1.30M
}
186
187
/* Get the allocator for a Cos object. */
188
gs_memory_t *
189
cos_object_memory(const cos_object_t *pco)
190
13.7M
{
191
13.7M
    return pco->mem;
192
13.7M
}
193
194
/* Change a generic cos object into one of a specific type. */
195
int
196
cos_become(cos_object_t *pco, cos_type_t cotype)
197
326k
{
198
326k
    if (cos_type(pco) != cos_type_generic)
199
0
        return_error(gs_error_typecheck);
200
326k
    cos_type(pco) = cotype;
201
326k
    return 0;
202
326k
}
203
204
/* Release a cos object. */
205
cos_proc_release(cos_release);  /* check prototype */
206
void
207
cos_release(cos_object_t *pco, client_name_t cname)
208
1.25M
{
209
1.25M
    pco->cos_procs->release(pco, cname);
210
1.25M
}
211
212
/* Free a cos object. */
213
void
214
cos_free(cos_object_t *pco, client_name_t cname)
215
971k
{
216
971k
    cos_release(pco, cname);
217
971k
    gs_free_object(cos_object_memory(pco), pco, cname);
218
971k
}
219
220
/* Write a cos object on the output. */
221
cos_proc_write(cos_write);  /* check prototype */
222
int
223
cos_write(const cos_object_t *pco, gx_device_pdf *pdev, gs_id object_id)
224
605k
{
225
605k
    return pco->cos_procs->write(pco, pdev, object_id);
226
605k
}
227
228
/* Write a cos object as a PDF object. */
229
int
230
cos_stream_write(const cos_object_t *pco, gx_device_pdf *pdev, gs_id object_id);
231
232
int
233
cos_write_object(cos_object_t *pco, gx_device_pdf *pdev, pdf_resource_type_t type)
234
129k
{
235
129k
    int code;
236
237
129k
    if (pco->id == 0 || pco->written)
238
0
        return_error(gs_error_Fatal);
239
129k
    if (pco->cos_procs->write == cos_stream_write)
240
61.2k
        pdf_open_separate_noObjStm(pdev, pco->id, type);
241
68.1k
    else
242
68.1k
        pdf_open_separate(pdev, pco->id, type);
243
129k
    code = cos_write(pco, pdev, pco->id);
244
129k
    if (pco->cos_procs->write == cos_stream_write)
245
61.2k
        pdf_end_separate_noObjStm(pdev, type);
246
68.1k
    else
247
68.1k
        pdf_end_separate(pdev, type);
248
129k
    pco->written = true;
249
129k
    return code;
250
129k
}
251
252
/* Make a value to store into a composite object. */
253
const cos_value_t *
254
cos_string_value(cos_value_t *pcv, const byte *data, uint size)
255
1.65M
{
256
    /*
257
     * It's OK to break const here, because the value will be copied
258
     * before being stored in the collection.
259
     */
260
1.65M
    pcv->contents.chars.data = (byte *)data;
261
1.65M
    pcv->contents.chars.size = size;
262
1.65M
    pcv->value_type = COS_VALUE_SCALAR;
263
1.65M
    return pcv;
264
1.65M
}
265
const cos_value_t *
266
cos_c_string_value(cos_value_t *pcv, const char *str)
267
690k
{
268
    /*
269
     * We shouldn't break const here, because the value will not be copied
270
     * or freed (or traced), but that would require a lot of bothersome
271
     * casting elsewhere.
272
     */
273
690k
    pcv->contents.chars.data = (byte *)str;
274
690k
    pcv->contents.chars.size = strlen(str);
275
690k
    pcv->value_type = COS_VALUE_CONST;
276
690k
    return pcv;
277
690k
}
278
const cos_value_t *
279
cos_object_value(cos_value_t *pcv, cos_object_t *pco)
280
643k
{
281
643k
    pcv->contents.object = pco;
282
643k
    pcv->value_type = COS_VALUE_OBJECT;
283
643k
    return pcv;
284
643k
}
285
const cos_value_t *
286
cos_resource_value(cos_value_t *pcv, cos_object_t *pco)
287
47.8k
{
288
47.8k
    pcv->contents.object = pco;
289
47.8k
    pcv->value_type = COS_VALUE_RESOURCE;
290
47.8k
    return pcv;
291
47.8k
}
292
293
/* Free a value. */
294
void
295
cos_value_free(const cos_value_t *pcv, gs_memory_t *mem,
296
               client_name_t cname)
297
2.95M
{
298
2.95M
    switch (pcv->value_type) {
299
1.62M
    case COS_VALUE_SCALAR:
300
1.62M
        gs_free_string(mem, pcv->contents.chars.data,
301
1.62M
                       pcv->contents.chars.size, cname);
302
2.31M
    case COS_VALUE_CONST:
303
2.31M
        break;
304
639k
    case COS_VALUE_OBJECT:
305
        /* Free the object if this is the only reference to it. */
306
639k
        if (pcv->contents.object != NULL) /* see cos_dict_objects_delete. */
307
639k
            if (!pcv->contents.object->id)
308
600k
                cos_free(pcv->contents.object, cname);
309
640k
    case COS_VALUE_RESOURCE:
310
640k
        break;
311
2.95M
    }
312
2.95M
}
313
314
/* Write a value on the output. */
315
static int
316
cos_value_write_spaced(const cos_value_t *pcv, gx_device_pdf *pdev,
317
                       bool do_space, gs_id object_id)
318
2.42M
{
319
2.42M
    stream *s = pdev->strm;
320
321
2.42M
    switch (pcv->value_type) {
322
1.36M
    case COS_VALUE_SCALAR:
323
1.90M
    case COS_VALUE_CONST:
324
1.90M
        if (do_space)
325
1.24M
            switch (pcv->contents.chars.data[0]) {
326
298k
            case '/': case '(': case '<': break;
327
944k
            default: stream_putc(s, ' ');
328
1.24M
            }
329
1.90M
        return pdf_write_value(pdev, pcv->contents.chars.data,
330
1.90M
                        pcv->contents.chars.size, object_id);
331
47.7k
    case COS_VALUE_RESOURCE:
332
47.7k
        pprinti64d1(s, "/R%"PRId64"", pcv->contents.object->id);
333
47.7k
        break;
334
473k
    case COS_VALUE_OBJECT: {
335
473k
        cos_object_t *pco = pcv->contents.object;
336
337
473k
        if (!pco->id) {
338
447k
            if (do_space &&
339
362k
                !(pco->cos_procs == cos_type_array ||
340
24.4k
                  pco->cos_procs == cos_type_dict)
341
447k
                ) {
342
                /* Arrays and dictionaries (only) are self-delimiting. */
343
0
                stream_putc(s, ' ');
344
0
            }
345
447k
            return cos_write(pco, pdev, object_id);
346
447k
        }
347
26.6k
        if (do_space)
348
17.3k
            stream_putc(s, ' ');
349
26.6k
        pprinti64d1(s, "%"PRId64" 0 R", pco->id);
350
26.6k
        if (pco->cos_procs == cos_type_reference)
351
0
            pco->id = 0;
352
26.6k
        break;
353
473k
    }
354
0
    default:      /* can't happen */
355
0
        return_error(gs_error_Fatal);
356
2.42M
    }
357
74.3k
    return 0;
358
2.42M
}
359
int
360
cos_value_write(const cos_value_t *pcv, gx_device_pdf *pdev)
361
50.7k
{
362
50.7k
    return cos_value_write_spaced(pcv, pdev, false, 0);
363
50.7k
}
364
365
/* Copy a value if necessary for putting into an array or dictionary. */
366
static int
367
cos_copy_element_value(cos_value_t *pcv, gs_memory_t *mem,
368
                       const cos_value_t *pvalue, bool copy)
369
2.95M
{
370
2.95M
    *pcv = *pvalue;
371
2.95M
    if (pvalue->value_type == COS_VALUE_SCALAR && copy) {
372
1.34M
        byte *value_data = gs_alloc_string(mem, pvalue->contents.chars.size,
373
1.34M
                                           "cos_copy_element_value");
374
375
1.34M
        if (value_data == 0)
376
0
            return_error(gs_error_VMerror);
377
1.34M
        memcpy(value_data, pvalue->contents.chars.data,
378
1.34M
               pvalue->contents.chars.size);
379
1.34M
        pcv->contents.chars.data = value_data;
380
1.34M
    }
381
2.95M
    return 0;
382
2.95M
}
383
384
/* Release a value copied for putting, if the operation fails. */
385
static void
386
cos_uncopy_element_value(cos_value_t *pcv, gs_memory_t *mem, bool copy)
387
0
{
388
0
    if (pcv->value_type == COS_VALUE_SCALAR && copy)
389
0
        gs_free_string(mem, pcv->contents.chars.data, pcv->contents.chars.size,
390
0
                       "cos_uncopy_element_value");
391
0
}
392
393
static int cos_value_hash(cos_value_t *pcv0, gs_md5_state_t *md5, gs_md5_byte_t *hash, gx_device_pdf *pdev)
394
872k
{
395
872k
    int code;
396
872k
    switch (pcv0->value_type) {
397
521k
        case COS_VALUE_SCALAR:
398
674k
        case COS_VALUE_CONST:
399
674k
            gs_md5_append(md5, pcv0->contents.chars.data, pcv0->contents.chars.size);
400
674k
            break;
401
402
198k
        case COS_VALUE_OBJECT:
403
198k
            code = pcv0->contents.object->cos_procs->hash(pcv0->contents.object, md5, hash, pdev);
404
198k
            if (code < 0)
405
1
                return code;
406
198k
            break;
407
198k
        case COS_VALUE_RESOURCE:
408
0
            break;
409
872k
    }
410
872k
    return 0;
411
872k
}
412
413
/* ---------------- Specific object types ---------------- */
414
415
/* ------ Generic objects ------ */
416
417
static cos_proc_release(cos_reference_release);
418
static cos_proc_release(cos_generic_release);
419
static cos_proc_write(cos_generic_write);
420
static cos_proc_equal(cos_generic_equal);
421
static cos_proc_hash(cos_generic_hash);
422
const cos_object_procs_t cos_generic_procs = {
423
    cos_generic_release, cos_generic_write, cos_generic_equal, cos_generic_hash
424
};
425
const cos_object_procs_t cos_reference_procs = {
426
    cos_reference_release, cos_generic_write, cos_generic_equal, cos_generic_hash
427
};
428
429
cos_object_t *
430
cos_object_alloc(gx_device_pdf *pdev, client_name_t cname)
431
463k
{
432
463k
    gs_memory_t *mem = pdev->pdf_memory;
433
463k
    cos_object_t *pco =
434
463k
        gs_alloc_struct(mem, cos_object_t, &st_cos_object, cname);
435
436
463k
    cos_object_init(pco, pdev, &cos_generic_procs);
437
463k
    return pco;
438
463k
}
439
440
cos_object_t *
441
cos_reference_alloc(gx_device_pdf *pdev, client_name_t cname)
442
0
{
443
0
    gs_memory_t *mem = pdev->pdf_memory;
444
0
    cos_object_t *pco =
445
0
        gs_alloc_struct(mem, cos_object_t, &st_cos_object, cname);
446
447
0
    cos_object_init(pco, pdev, &cos_reference_procs);
448
0
    return pco;
449
0
}
450
451
static void
452
cos_reference_release(cos_object_t *pco, client_name_t cname)
453
0
{
454
    /* Do nothing. */
455
0
}
456
457
static void
458
cos_generic_release(cos_object_t *pco, client_name_t cname)
459
91.4k
{
460
    /* Do nothing. */
461
91.4k
}
462
463
static int
464
cos_generic_write(const cos_object_t *pco, gx_device_pdf *pdev, gs_id object_id)
465
27
{
466
27
    return_error(gs_error_Fatal);
467
27
}
468
469
static int
470
cos_generic_equal(const cos_object_t *pco0, const cos_object_t *pco1, gx_device_pdf *pdev)
471
0
{
472
0
    return_error(gs_error_Fatal);
473
0
}
474
475
static int
476
cos_generic_hash(const cos_object_t *pco0, gs_md5_state_t *md5, gs_md5_byte_t *hash, gx_device_pdf *pdev)
477
0
{
478
0
    return_error(gs_error_Fatal);
479
0
}
480
481
/* ------ Arrays ------ */
482
483
static cos_proc_release(cos_array_release);
484
static cos_proc_write(cos_array_write);
485
static cos_proc_equal(cos_array_equal);
486
static cos_proc_hash(cos_array_hash);
487
const cos_object_procs_t cos_array_procs = {
488
    cos_array_release, cos_array_write, cos_array_equal, cos_array_hash
489
};
490
491
cos_array_t *
492
cos_array_alloc(gx_device_pdf *pdev, client_name_t cname)
493
480k
{
494
480k
    gs_memory_t *mem = pdev->pdf_memory;
495
480k
    cos_array_t *pca =
496
480k
        gs_alloc_struct(mem, cos_array_t, &st_cos_object, cname);
497
498
480k
    cos_object_init((cos_object_t *)pca, pdev, &cos_array_procs);
499
480k
    return pca;
500
480k
}
501
502
cos_array_t *
503
cos_array_from_floats(gx_device_pdf *pdev, const float *pf, uint size,
504
                      client_name_t cname)
505
23.9k
{
506
23.9k
    cos_array_t *pca = cos_array_alloc(pdev, cname);
507
23.9k
    uint i;
508
509
23.9k
    if (pca == 0)
510
0
        return 0;
511
142k
    for (i = 0; i < size; ++i) {
512
118k
        int code = cos_array_add_real(pca, pf[i]);
513
514
118k
        if (code < 0) {
515
0
            COS_FREE(pca, cname);
516
0
            return 0;
517
0
        }
518
118k
    }
519
23.9k
    return pca;
520
23.9k
}
521
522
static void
523
cos_array_release(cos_object_t *pco, client_name_t cname)
524
480k
{
525
480k
    cos_array_t *const pca = (cos_array_t *)pco;
526
480k
    cos_array_element_t *cur;
527
480k
    cos_array_element_t *next;
528
529
1.40M
    for (cur = pca->elements; cur; cur = next) {
530
920k
        next = cur->next;
531
920k
        cos_value_free(&cur->value, cos_object_memory(pco), cname);
532
920k
        gs_free_object(cos_object_memory(pco), cur, cname);
533
920k
    }
534
480k
    pca->elements = 0;
535
480k
}
536
537
static cos_array_element_t *cos_array_reorder(const cos_array_t *pca,
538
                                               cos_array_element_t *first);
539
static int
540
cos_array_write(const cos_object_t *pco, gx_device_pdf *pdev, gs_id object_id)
541
342k
{
542
342k
    stream *s = pdev->strm;
543
342k
    const cos_array_t *const pca = (const cos_array_t *)pco;
544
342k
    cos_array_element_t *first = cos_array_reorder(pca, NULL);
545
342k
    cos_array_element_t *pcae;
546
342k
    uint last_index = 0, Element_Count = 0;
547
548
342k
    stream_puts(s, "[");
549
1.09M
    for (pcae = first; pcae; ++last_index, pcae = pcae->next) {
550
747k
        Element_Count++;
551
552
747k
        if(pdev->PDFA != 0 && Element_Count > 8191) {
553
0
            switch (pdev->PDFACompatibilityPolicy) {
554
0
            case 0:
555
0
                emprintf(pdev->memory,
556
0
                    "Too many entries in array,\n max 8191 in PDF/A, reverting to normal PDF output\n");
557
0
                pdev->AbortPDFAX = true;
558
0
                pdev->PDFA = 0;
559
0
                break;
560
0
            case 1:
561
0
                emprintf(pdev->memory,
562
0
                     "Too many entries in array,\n max 8191 in PDF/A. Cannot simply elide dictionary, reverting to normal output\n");
563
0
                pdev->AbortPDFAX = true;
564
0
                pdev->PDFA = 0;
565
0
                break;
566
0
            case 2:
567
0
                emprintf(pdev->memory,
568
0
                     "Too many entries in array,\n max 8191 in PDF/A. aborting conversion\n");
569
                /* Careful here, only certain errors will bubble up
570
                 * through the text processing.
571
                 */
572
0
                return_error(gs_error_limitcheck);
573
0
                break;
574
0
            default:
575
0
                emprintf(pdev->memory,
576
0
                     "Too many entries in array,\n max 8191 in PDF/A. Unrecognised PDFACompatibilityLevel,\nreverting to normal PDF output\n");
577
0
                pdev->AbortPDFAX = true;
578
0
                pdev->PDFA = 0;
579
0
                break;
580
0
            }
581
0
        }
582
747k
        if (pcae != first)
583
405k
            stream_putc(s, '\n');
584
747k
        for (; pcae->index > last_index; ++last_index)
585
0
            stream_puts(s, "null\n");
586
747k
        cos_value_write_spaced(&pcae->value, pdev, false, object_id);
587
747k
    }
588
342k
    DISCARD(cos_array_reorder(pca, first));
589
342k
    stream_puts(s, "]");
590
342k
    if (pdev->PDFA != 0)
591
0
        stream_puts(s, "\n");
592
342k
    return 0;
593
342k
}
594
595
static int
596
cos_array_equal(const cos_object_t *pco0, const cos_object_t *pco1, gx_device_pdf *pdev)
597
0
{
598
0
    const cos_array_t *const pca0 = (const cos_array_t *)pco0;
599
0
    const cos_array_t *const pca1 = (const cos_array_t *)pco1;
600
0
    int code;
601
602
0
    if (!pca0->md5_valid) {
603
0
        gs_md5_init((gs_md5_state_t *)&pca0->md5);
604
0
        code = cos_array_hash(pco0, (gs_md5_state_t *)&pca0->md5, (gs_md5_byte_t *)pca0->hash, pdev);
605
0
        if (code < 0)
606
0
            return code;
607
0
        gs_md5_finish((gs_md5_state_t *)&pca0->md5, (gs_md5_byte_t *)pca0->hash);
608
0
        ((cos_object_t *)pca0)->md5_valid = true;
609
0
    }
610
0
    if (!pca1->md5_valid) {
611
0
        gs_md5_init((gs_md5_state_t *)&pca1->md5);
612
0
        code = cos_array_hash(pco1, (gs_md5_state_t *)&pca1->md5, (gs_md5_byte_t *)pca1->hash, pdev);
613
0
        if (code < 0)
614
0
            return code;
615
0
        gs_md5_finish((gs_md5_state_t *)&pca1->md5, (gs_md5_byte_t *)pca1->hash);
616
0
        ((cos_object_t *)pca1)->md5_valid = true;
617
0
    }
618
0
    if (memcmp(&pca0->hash, &pca1->hash, 16) != 0)
619
0
        return false;
620
621
0
    return true;
622
0
}
623
624
static int
625
cos_array_hash(const cos_object_t *pco0, gs_md5_state_t *md5, gs_md5_byte_t *hash, gx_device_pdf *pdev)
626
142k
{
627
142k
    const cos_array_t *const pca0 = (const cos_array_t *)pco0;
628
142k
    cos_array_element_t *first0 = pca0->elements;
629
142k
    cos_array_element_t *pcae0;
630
142k
    int code;
631
632
523k
    for (pcae0 = first0; pcae0;pcae0 = pcae0->next) {
633
380k
        code = cos_value_hash(&pcae0->value, md5, hash, pdev);
634
380k
        if (code < 0)
635
0
            return code;
636
380k
    }
637
142k
    return 0;
638
142k
}
639
640
/* Put/add an element in/to an array. */
641
int
642
cos_array_put(cos_array_t *pca, int64_t index, const cos_value_t *pvalue)
643
920k
{
644
920k
    gs_memory_t *mem = COS_OBJECT_MEMORY(pca);
645
920k
    cos_value_t value;
646
920k
    int code = cos_copy_element_value(&value, mem, pvalue, true);
647
648
920k
    if (code >= 0) {
649
920k
        code = cos_array_put_no_copy(pca, index, &value);
650
920k
        if (code < 0)
651
0
            cos_uncopy_element_value(&value, mem, true);
652
920k
    }
653
920k
    pca->md5_valid = false;
654
920k
    return code;
655
920k
}
656
int
657
cos_array_put_no_copy(cos_array_t *pca, int64_t index, const cos_value_t *pvalue)
658
921k
{
659
921k
    gs_memory_t *mem = COS_OBJECT_MEMORY(pca);
660
921k
    cos_array_element_t **ppcae = &pca->elements;
661
921k
    cos_array_element_t *pcae;
662
921k
    cos_array_element_t *next;
663
664
921k
    while ((next = *ppcae) != 0 && next->index > index)
665
0
        ppcae = &next->next;
666
921k
    if (next && next->index == index) {
667
        /* We're replacing an existing element. */
668
0
        cos_value_free(&next->value, mem,
669
0
                       "cos_array_put(old value)");
670
0
        pcae = next;
671
921k
    } else {
672
        /* Create a new element. */
673
921k
        pcae = gs_alloc_struct(mem, cos_array_element_t, &st_cos_array_element,
674
921k
                               "cos_array_put(element)");
675
921k
        if (pcae == 0)
676
0
            return_error(gs_error_VMerror);
677
921k
        pcae->index = index;
678
921k
        pcae->next = next;
679
921k
        *ppcae = pcae;
680
921k
    }
681
921k
    pcae->value = *pvalue;
682
921k
    pca->md5_valid = false;
683
921k
    return 0;
684
921k
}
685
static long
686
cos_array_next_index(const cos_array_t *pca)
687
921k
{
688
921k
    return (pca->elements ? pca->elements->index + 1 : 0L);
689
921k
}
690
int
691
cos_array_add(cos_array_t *pca, const cos_value_t *pvalue)
692
920k
{
693
920k
    pca->md5_valid = false;
694
920k
    return cos_array_put(pca, cos_array_next_index(pca), pvalue);
695
920k
}
696
int
697
cos_array_add_no_copy(cos_array_t *pca, const cos_value_t *pvalue)
698
790
{
699
790
    pca->md5_valid = false;
700
790
    return cos_array_put_no_copy(pca, cos_array_next_index(pca), pvalue);
701
790
}
702
int
703
cos_array_add_c_string(cos_array_t *pca, const char *str)
704
354k
{
705
354k
    cos_value_t value;
706
354k
    uint size = strlen(str) + 1;
707
708
354k
    if (size > 1 && str[0] == '/') {
709
270k
        unescape_name((char *)str, &size);
710
270k
    }
711
354k
    return cos_array_add(pca, cos_c_string_value(&value, str));
712
354k
}
713
int
714
cos_array_add_int(cos_array_t *pca, int i)
715
12.9k
{
716
12.9k
    char str[sizeof(int) * 8 / 3 + 3]; /* sign, rounding, 0 terminator */
717
12.9k
    cos_value_t v;
718
719
12.9k
    gs_snprintf(str, sizeof(str), "%d", i);
720
12.9k
    return cos_array_add(pca, cos_string_value(&v, (byte *)str, strlen(str)));
721
12.9k
}
722
int
723
cos_array_add_real(cos_array_t *pca, double r)
724
451k
{
725
451k
    byte str[50];   /****** ADHOC ******/
726
451k
    stream s;
727
451k
    cos_value_t v;
728
729
451k
    s_init(&s, NULL);
730
451k
    swrite_string(&s, str, sizeof(str));
731
451k
    pprintg1(&s, "%g", r);
732
451k
    return cos_array_add(pca, cos_string_value(&v, str, stell(&s)));
733
451k
}
734
int
735
cos_array_add_object(cos_array_t *pca, cos_object_t *pco)
736
85.9k
{
737
85.9k
    cos_value_t value;
738
739
85.9k
    value.contents.chars.size = 0; /* Quiet a warning appeared with MSVC6 inline optimization. */
740
85.9k
    return cos_array_add(pca, cos_object_value(&value, pco));
741
85.9k
}
742
743
/*
744
 * Remove and return the last element of an array.  Since this is intended
745
 * specifically for arrays used as stacks, it gives an error if there is a
746
 * gap in indices between the last element and the element before it.
747
 */
748
int
749
cos_array_unadd(cos_array_t *pca, cos_value_t *pvalue)
750
97.4k
{
751
97.4k
    cos_array_element_t *pcae = pca->elements;
752
753
97.4k
    if (pcae == 0 ||
754
835
        pcae->index != (pcae->next == 0 ? 0 : pcae->next->index + 1)
755
97.4k
        )
756
96.5k
        return_error(gs_error_rangecheck);
757
835
    *pvalue = pcae->value;
758
835
    pca->elements = pcae->next;
759
835
    gs_free_object(COS_OBJECT_MEMORY(pca), pcae, "cos_array_unadd");
760
835
    pca->md5_valid = false;
761
835
    return 0;
762
97.4k
}
763
764
/* Get the first / next element for enumerating an array. */
765
const cos_array_element_t *
766
cos_array_element_first(const cos_array_t *pca)
767
6.00k
{
768
6.00k
    return pca->elements;
769
6.00k
}
770
const cos_array_element_t *
771
cos_array_element_next(const cos_array_element_t *pca, int64_t *pindex,
772
                       const cos_value_t **ppvalue)
773
24.0k
{
774
24.0k
    *pindex = pca->index;
775
24.0k
    *ppvalue = &pca->value;
776
24.0k
    return pca->next;
777
24.0k
}
778
779
unsigned int cos_array_size(const cos_array_t *pca)
780
0
{
781
0
    cos_array_element_t *pcae = pca->elements;
782
0
    return(pcae->index + 1);
783
0
}
784
785
const cos_value_t *cos_array_get(const cos_array_t *pca, unsigned int index)
786
0
{
787
0
    cos_array_element_t **ppcae = (cos_array_element_t **)&pca->elements;
788
0
    cos_array_element_t *next;
789
790
0
    while ((next = *ppcae) != 0 && next->index > index)
791
0
        ppcae = &next->next;
792
0
    return(&next->value);
793
0
}
794
795
/*
796
 * Reorder the elements of an array for writing or after writing.  Usage:
797
 *  first_element = cos_array_reorder(pca, NULL);
798
 *  ...
799
 *  cos_array_reorder(pca, first_element);
800
 */
801
static cos_array_element_t *
802
cos_array_reorder(const cos_array_t *pca, cos_array_element_t *first)
803
684k
{
804
684k
    cos_array_element_t *last;
805
684k
    cos_array_element_t *next;
806
684k
    cos_array_element_t *pcae;
807
808
2.18M
    for (pcae = (first ? first : pca->elements), last = NULL; pcae;
809
1.49M
         pcae = next)
810
1.49M
        next = pcae->next, pcae->next = last, last = pcae;
811
684k
    return last;
812
684k
}
813
814
/* ------ Dictionaries ------ */
815
816
static cos_proc_release(cos_dict_release);
817
static cos_proc_write(cos_dict_write);
818
static cos_proc_equal(cos_dict_equal);
819
static cos_proc_hash(cos_dict_hash);
820
const cos_object_procs_t cos_dict_procs = {
821
    cos_dict_release, cos_dict_write, cos_dict_equal, cos_dict_hash
822
};
823
824
cos_dict_t *
825
cos_dict_alloc(gx_device_pdf *pdev, client_name_t cname)
826
221k
{
827
221k
    gs_memory_t *mem = pdev->pdf_memory;
828
221k
    cos_dict_t *pcd =
829
221k
        gs_alloc_struct(mem, cos_dict_t, &st_cos_object, cname);
830
831
221k
    cos_object_init((cos_object_t *)pcd, pdev, &cos_dict_procs);
832
221k
    return pcd;
833
221k
}
834
835
static void
836
cos_dict_element_free(cos_dict_t *pcd, cos_dict_element_t *pcde,
837
                      client_name_t cname)
838
2.02M
{
839
2.02M
    gs_memory_t *mem = COS_OBJECT_MEMORY(pcd);
840
841
2.02M
    cos_value_free(&pcde->value, mem, cname);
842
2.02M
    if (pcde->owns_key)
843
448k
        gs_free_string(mem, pcde->key.data, pcde->key.size, cname);
844
2.02M
    gs_free_object(mem, pcde, cname);
845
2.02M
}
846
847
static int
848
cos_dict_delete(cos_dict_t *pcd, const byte *key_data, uint key_size)
849
0
{
850
0
    cos_dict_element_t *pcde = pcd->elements, *prev = 0;
851
852
0
    for (; pcde; pcde = pcde->next) {
853
0
        if (!bytes_compare(key_data, key_size, pcde->key.data, pcde->key.size)) {
854
0
            if (prev != 0)
855
0
                prev->next = pcde->next;
856
0
            else
857
0
                pcd->elements = pcde->next;
858
0
            cos_dict_element_free(pcd, pcde, "cos_dict_delete");
859
0
            return 0;
860
0
        }
861
0
        prev = pcde;
862
0
    }
863
0
    return -1;
864
0
}
865
int
866
cos_dict_delete_c_key(cos_dict_t *pcd, const char *key)
867
0
{
868
0
    return cos_dict_delete(pcd, (const byte *)key, strlen(key));
869
0
}
870
871
static void
872
cos_dict_release(cos_object_t *pco, client_name_t cname)
873
695k
{
874
695k
    cos_dict_t *const pcd = (cos_dict_t *)pco;
875
695k
    cos_dict_element_t *cur;
876
695k
    cos_dict_element_t *next;
877
878
2.72M
    for (cur = pcd->elements; cur; cur = next) {
879
2.02M
        next = cur->next;
880
2.02M
        cos_dict_element_free(pcd, cur, cname);
881
2.02M
    }
882
695k
    pcd->elements = 0;
883
695k
}
884
885
/* Write the elements of a dictionary. */
886
static int
887
cos_elements_write(stream *s, const cos_dict_element_t *pcde,
888
                   gx_device_pdf *pdev, bool do_space, gs_id object_id)
889
486k
{
890
486k
    int Element_Count = 0;
891
892
486k
    if (pcde) {
893
        /* Temporarily replace the output stream in pdev. */
894
384k
        stream *save = pdev->strm;
895
896
384k
        pdev->strm = s;
897
1.62M
        for (;;) {
898
1.62M
            gs_id object_id1 = (pdev->NoEncrypt.size == 0 ||
899
0
                                bytes_compare(pdev->NoEncrypt.data, pdev->NoEncrypt.size,
900
0
                                    pcde->key.data, pcde->key.size)
901
1.62M
                                ? object_id : (gs_id)-1);
902
903
1.62M
            Element_Count++;
904
905
1.62M
            if(pdev->PDFA != 0 && Element_Count > 4095) {
906
0
                switch (pdev->PDFACompatibilityPolicy) {
907
0
                    case 0:
908
0
                        emprintf(pdev->memory,
909
0
                             "Too many entries in dictionary,\n max 4095 in PDF/A, reverting to normal PDF output\n");
910
0
                        pdev->AbortPDFAX = true;
911
0
                        pdev->PDFA = 0;
912
0
                        break;
913
0
                    case 1:
914
0
                        emprintf(pdev->memory,
915
0
                             "Too many entries in dictionary,\n max 4095 in PDF/A. Cannot simply elide dictionary, reverting to normal output\n");
916
0
                        pdev->AbortPDFAX = true;
917
0
                        pdev->PDFA = 0;
918
0
                        break;
919
0
                    case 2:
920
0
                        emprintf(pdev->memory,
921
0
                             "Too many entries in dictionary,\n max 4095 in PDF/A. aborting conversion\n");
922
                        /* Careful here, only certain errors will bubble up
923
                         * through the text processing.
924
                         */
925
0
                        return_error(gs_error_limitcheck);
926
0
                        break;
927
0
                    default:
928
0
                        emprintf(pdev->memory,
929
0
                             "Too many entries in dictionary,\n max 4095 in PDF/A. Unrecognised PDFACompatibilityLevel,\nreverting to normal PDF output\n");
930
0
                        pdev->AbortPDFAX = true;
931
0
                        pdev->PDFA = 0;
932
0
                        break;
933
0
                }
934
0
            }
935
936
1.62M
            pdf_write_value(pdev, pcde->key.data, pcde->key.size, object_id1);
937
1.62M
            cos_value_write_spaced(&pcde->value, pdev, true, object_id1);
938
1.62M
            pcde = pcde->next;
939
1.62M
            if (pcde || do_space)
940
1.41M
                stream_putc(s, '\n');
941
1.62M
            if (!pcde)
942
384k
                break;
943
1.62M
        }
944
384k
        pdev->strm = save;
945
384k
    }
946
486k
    return 0;
947
486k
}
948
int
949
cos_dict_elements_write(const cos_dict_t *pcd, gx_device_pdf *pdev)
950
92.2k
{
951
92.2k
    return cos_elements_write(pdev->strm, pcd->elements, pdev, true, pcd->id);
952
92.2k
}
953
954
static int
955
cos_dict_write(const cos_object_t *pco, gx_device_pdf *pdev, gs_id object_id)
956
173k
{
957
173k
    stream *s = pdev->strm;
958
959
173k
    stream_puts(s, "<<");
960
173k
    cos_elements_write(s, ((const cos_dict_t *)pco)->elements, pdev, false, object_id);
961
173k
    stream_puts(s, ">>");
962
173k
    if (pdev->PDFA != 0)
963
0
        stream_puts(s, "\n");
964
173k
    return 0;
965
173k
}
966
967
static int find_first_dict_entry(const cos_dict_t *d, const cos_dict_element_t **element)
968
17
{
969
17
    const cos_dict_element_t *pcde = d->elements, *First;
970
17
    int code, length, length1, length2, offset1 = 0, offset2 = 0, i;
971
972
17
    *element = 0L;
973
974
17
    First = pcde;
975
976
    /*
977
     * If the name has any 'unusual' characters, it is 'escaped' by starting with NULLs
978
     * I suspect we no longer need that, but here we remove the escaping NULLs
979
     */
980
17
    for (i = 0;First->key.data[i] == 0x00; i++)
981
0
        ;
982
983
17
    length1 = First->key.size - i;
984
17
    offset1 = i;
985
986
17
    if (First->key.data[offset1] == '/') {
987
0
        length1 -= 1;
988
0
        offset1 += 1;
989
17
    } else {
990
17
        if (First->key.data[offset1] == '(') {
991
17
            length1 = First->key.size - 2;
992
17
            offset1 = 1;
993
17
        } else {
994
0
            return_error(gs_error_typecheck);
995
0
        }
996
17
    }
997
998
17
    pcde = pcde->next;
999
17
    while (pcde){
1000
0
        for (i = 0;pcde->key.data[i] == 0x00; i++)
1001
0
            ;
1002
0
        length2 = pcde->key.size - i;
1003
0
        offset2 = i;
1004
1005
0
        if (pcde->key.data[offset2] == '/') {
1006
0
            length2 -= 1;
1007
0
            offset2 += 1;
1008
0
        } else {
1009
0
            if (pcde->key.data[offset2] == '(') {
1010
0
                length2 = pcde->key.size - 2;
1011
0
                offset2 = 1;
1012
0
            } else {
1013
0
                return_error(gs_error_typecheck);
1014
0
            }
1015
0
        }
1016
1017
0
        if (length2 < length1)
1018
0
            length = length2;
1019
0
        else
1020
0
            length = length1;
1021
0
        code = strncmp((const char *)&pcde->key.data[offset2], (const char *)&First->key.data[offset1], length);
1022
0
        if (code == 0) {
1023
0
            if (length2 < length1) {
1024
0
                First = pcde;
1025
0
                length1 = length2;
1026
0
                offset1 = offset2;
1027
0
            }
1028
0
        } else if (code < 0) {
1029
0
            First = pcde;
1030
0
            length1 = length2;
1031
0
            offset1 = offset2;
1032
0
        }
1033
0
        pcde = pcde->next;
1034
0
    }
1035
17
    *element = First;
1036
17
    return 0;
1037
17
}
1038
1039
static int find_next_dict_entry(const cos_dict_t *d, const cos_dict_element_t **element)
1040
34
{
1041
34
    const cos_dict_element_t *pcde = d->elements, *Current = *element, *Next = 0L;
1042
34
    int code, length, length1, length2, length3, offset1 = 0, offset2 = 0, offset3 = 0, i;
1043
1044
    /*
1045
     * If the name has any 'unusual' characters, it is 'escaped' by starting with NULLs
1046
     * I suspect we no longer need that, but here we remove the escaping NULLs
1047
     */
1048
34
    for (i = 0;Current->key.data[i] == 0x00; i++)
1049
0
        ;
1050
34
    length1 = Current->key.size - i;
1051
34
    offset1 = i;
1052
34
    if (Current->key.data[offset1] == '/') {
1053
0
        length1 -= 1;
1054
0
        offset1 += 1;
1055
34
    } else {
1056
34
        if (Current->key.data[0] == '(') {
1057
34
            length1 -= 2;
1058
34
            offset1 = 1;
1059
34
        } else {
1060
0
            return_error(gs_error_typecheck);
1061
0
        }
1062
34
    }
1063
1064
68
    while (pcde) {
1065
34
        for (i = 0;pcde->key.data[i] == 0x00; i++)
1066
0
            ;
1067
34
        length2 = pcde->key.size - i;
1068
34
        offset2 = i;
1069
34
        if (pcde->key.data[offset2] == '/') {
1070
0
            length2 -= 1;
1071
0
            offset2 += 1;
1072
34
        } else {
1073
34
            if (pcde->key.data[0] == '(') {
1074
34
                length2 -= 2;
1075
34
                offset2 = 1;
1076
34
            } else {
1077
0
                return_error(gs_error_typecheck);
1078
0
            }
1079
34
        }
1080
1081
34
        if (length2 < length1)
1082
0
            length = length2;
1083
34
        else
1084
34
            length = length1;
1085
34
        code = strncmp((const char *)&pcde->key.data[offset2], (const char *)&Current->key.data[offset1], length);
1086
34
        if (code > 0 || (code == 0 && length2 > length1)) {
1087
0
            if (Next) {
1088
0
                if (length3 < length2)
1089
0
                    length = length3;
1090
0
                else
1091
0
                    length = length2;
1092
0
                code = strncmp((const char *)&pcde->key.data[offset2], (const char *)&Next->key.data[offset3], length);
1093
0
                if (code < 0 || (code == 0 && length3 > length2)) {
1094
0
                    Next = pcde;
1095
0
                    length3 = length2;
1096
0
                    offset3 = offset2;
1097
0
                }
1098
0
            } else {
1099
0
                Next = pcde;
1100
0
                for (i = 0;Next->key.data[i] == 0x00; i++)
1101
0
                    ;
1102
0
                length3 = Next->key.size - i;
1103
0
                offset3 = i;
1104
0
                if (Next->key.data[offset3] == '/') {
1105
0
                    length3 -= 1;
1106
0
                    offset3 += 1;
1107
0
                } else {
1108
0
                    if (Next->key.data[0] == '(') {
1109
0
                        length3 -= 2;
1110
0
                        offset3 += 1;
1111
0
                    } else {
1112
0
                        return_error(gs_error_typecheck);
1113
0
                    }
1114
0
                }
1115
0
            }
1116
0
        }
1117
34
        pcde = pcde->next;
1118
34
    }
1119
34
    *element = Next;
1120
34
    return 0;
1121
34
}
1122
1123
static int find_last_dict_entry(const cos_dict_t *d, const cos_dict_element_t **element)
1124
17
{
1125
17
    const cos_dict_element_t *pcde = d->elements, *Last, *Next;
1126
1127
17
    *element = 0L;
1128
1129
17
    Next = Last = pcde;
1130
1131
17
    do {
1132
17
        Last = Next;
1133
17
        find_next_dict_entry(d, &Next);
1134
17
    } while (Next);
1135
1136
17
    *element = Last;
1137
1138
17
    return 0;
1139
17
}
1140
static int write_key_as_string_encrypted(const gx_device_pdf *pdev, const byte *str, uint size, gs_id object_id)
1141
0
{
1142
0
    stream sout;
1143
0
    stream_PSSD_state st;
1144
0
    stream_state so;
1145
0
    byte bufo[100], *buffer;
1146
0
    stream_arcfour_state sarc4;
1147
0
    int i, j = 0;
1148
1149
0
    buffer = gs_alloc_bytes(pdev->pdf_memory, (size_t)size * 2, "encryption buffer");
1150
0
    if (buffer == 0L)
1151
0
        return 0;
1152
1153
0
    if (pdf_encrypt_init(pdev, object_id, &sarc4) < 0) {
1154
0
        gs_free_object(pdev->pdf_memory, buffer, "Free encryption buffer");
1155
        /* The interface can't pass an error. */
1156
0
        stream_write(pdev->strm, str, size);
1157
0
        return size;
1158
0
    }
1159
0
    s_init_state((stream_state *)&st, &s_PSSD_template, NULL);
1160
0
    s_init(&sout, NULL);
1161
0
    s_init_state(&so, &s_PSSE_template, NULL);
1162
0
    s_init_filter(&sout, &so, bufo, sizeof(bufo), pdev->strm);
1163
0
    stream_putc(pdev->strm, '(');
1164
0
    for (i = 0;i < size;i++) {
1165
0
        if (str[i] == '(' || str[i] == ')' || str[i] == '\\')
1166
0
            buffer[j++] = '\\';
1167
0
        buffer[j++] = str[i];
1168
0
    }
1169
0
    s_arcfour_process_buffer(&sarc4, buffer, j);
1170
0
    stream_write(&sout, buffer, j);
1171
    /* Another case where we use sclose() and not s_close_filters(), because the
1172
     * buffer we supplied to s_init_filter is a heap based C object, so we
1173
     * must not free it.
1174
     */
1175
0
    sclose(&sout); /* Writes ')'. */
1176
0
    gs_free_object(pdev->pdf_memory, buffer, "Free encryption buffer");
1177
0
    return 0;
1178
0
}
1179
1180
static int write_key_as_string(const gx_device_pdf *pdev, stream *s, const cos_dict_element_t *element, gs_id object_id)
1181
51
{
1182
51
    int i, length, offset;
1183
1184
    /*
1185
     * If the name has any 'unusual' characters, it is 'escaped' by starting with NULLs
1186
     * I suspect we no longer need that, but here we remove the escaping NULLs
1187
     */
1188
51
    if (element->key.data[0] == 0x00) {
1189
0
        for (i = 0;element->key.data[i] == 0x00; i++)
1190
0
            ;
1191
0
        length = element->key.size - (i + 1);
1192
0
        offset = i;
1193
51
    } else {
1194
51
        offset = 0;
1195
51
        length = element->key.size;
1196
51
    }
1197
1198
51
    if (element->key.data[offset] == '/') {
1199
0
        offset++;
1200
0
        length--;
1201
0
        if (!pdev->KeyLength || object_id == (gs_id)-1) {
1202
0
            int i = 0;
1203
1204
0
            spputc(s, '(');
1205
            /* We need to escape some character codes */
1206
0
            for (i = 0;i < length;i++) {
1207
0
                if (element->key.data[offset + i] == '(' || element->key.data[offset + i] == ')' ||
1208
0
                    element->key.data[offset + i] == '\\')
1209
0
                    spputc(s, '\\');
1210
0
                spputc(s, element->key.data[offset + i]);
1211
0
            }
1212
0
            spputc(s, ')');
1213
0
        }
1214
0
        else
1215
0
            write_key_as_string_encrypted(pdev, &element->key.data[offset], length, object_id);
1216
51
    } else {
1217
51
        if (!pdev->KeyLength || object_id == (gs_id)-1)
1218
51
            stream_write(s, element->key.data, element->key.size);
1219
0
        else
1220
0
            write_key_as_string_encrypted(pdev, &element->key.data[1], element->key.size - 2, object_id);
1221
51
    }
1222
51
    return 0;
1223
51
}
1224
1225
int
1226
cos_write_dict_as_ordered_array(cos_object_t *pco, gx_device_pdf *pdev, pdf_resource_type_t type)
1227
17
{
1228
17
    stream *s;
1229
17
    int code;
1230
17
    const cos_dict_t *d;
1231
17
    const cos_dict_element_t *pcde, *First, *Last;
1232
1233
17
    if (cos_type(pco) != cos_type_dict)
1234
0
        return_error(gs_error_typecheck);
1235
1236
17
    d = (const cos_dict_t *)pco;
1237
1238
17
    if (pco->id == 0 || pco->written)
1239
0
        return_error(gs_error_Fatal);
1240
17
    pdf_open_separate(pdev, pco->id, type);
1241
1242
17
    s = pdev->strm;
1243
17
    pcde = d->elements;
1244
17
    if (!pcde){
1245
0
        stream_puts(s, "<<>>\n");
1246
0
        pdf_end_separate(pdev, type);
1247
0
        return 0;
1248
0
    }
1249
1250
17
    code = find_first_dict_entry(d, &First);
1251
17
    if (code < 0) {
1252
0
        pdf_end_separate(pdev, type);
1253
0
        return code;
1254
0
    }
1255
1256
17
    code = find_last_dict_entry(d, &Last);
1257
17
    if (code < 0) {
1258
0
        pdf_end_separate(pdev, type);
1259
0
        return code;
1260
0
    }
1261
1262
17
    stream_puts(s, "<<\n/Limits [\n");
1263
17
    write_key_as_string(pdev, s, First, pco->id);
1264
17
    stream_puts(s, "\n");
1265
17
    write_key_as_string(pdev, s, Last, pco->id);
1266
17
    stream_puts(s, "\n]\n");
1267
17
    stream_puts(s, "/Names [");
1268
17
    do {
1269
17
        stream_puts(s, "\n");
1270
17
        write_key_as_string(pdev, s, First, pco->id);
1271
17
        cos_value_write_spaced(&First->value, pdev, true, -1);
1272
17
        find_next_dict_entry(d, &First);
1273
17
    } while (First);
1274
17
    stream_puts(s, "]\n>>\n");
1275
1276
17
    pdf_end_separate(pdev, type);
1277
17
    pco->written = true;
1278
17
    return code;
1279
17
}
1280
1281
/* Write/delete definitions of named objects. */
1282
/* This is a special-purpose facility for pdf_close. */
1283
int
1284
cos_dict_objects_write(const cos_dict_t *pcd, gx_device_pdf *pdev)
1285
62.6k
{
1286
62.6k
    const cos_dict_element_t *pcde = pcd->elements;
1287
1288
128k
    for (; pcde; pcde = pcde->next)
1289
65.6k
        if (COS_VALUE_IS_OBJECT(&pcde->value) &&
1290
65.6k
            pcde->value.contents.object->id  &&
1291
34.3k
            !pcde->value.contents.object->written /* ForOPDFRead only. */)
1292
34.3k
            cos_write_object(pcde->value.contents.object, pdev, resourceOther);
1293
62.6k
    return 0;
1294
62.6k
}
1295
int
1296
cos_dict_objects_delete(cos_dict_t *pcd)
1297
62.6k
{
1298
62.6k
    cos_dict_element_t *pcde = pcd->elements;
1299
1300
    /*
1301
     * Delete duplicate references to prevent a dual object freeing.
1302
     * Delete the objects' IDs so that freeing the dictionary will
1303
     * free them.
1304
     */
1305
128k
    for (; pcde; pcde = pcde->next) {
1306
65.6k
        if (pcde->value.contents.object) {
1307
65.6k
            cos_dict_element_t *pcde1 = pcde->next;
1308
1309
159k
            for (; pcde1; pcde1 = pcde1->next)
1310
93.8k
                if (pcde->value.contents.object == pcde1->value.contents.object)
1311
0
                    pcde1->value.contents.object = NULL;
1312
65.6k
            pcde->value.contents.object->id = 0;
1313
65.6k
        }
1314
65.6k
    }
1315
62.6k
    return 0;
1316
62.6k
}
1317
1318
/* Put an element in a dictionary. */
1319
2.49M
#define DICT_COPY_KEY 1
1320
3.76M
#define DICT_COPY_VALUE 2
1321
2.49M
#define DICT_FREE_KEY 4
1322
134k
#define DICT_COPY_ALL (DICT_COPY_KEY | DICT_COPY_VALUE | DICT_FREE_KEY)
1323
static int
1324
cos_dict_put_copy(cos_dict_t *pcd, const byte *key_data, uint key_size,
1325
                  const cos_value_t *pvalue, int flags)
1326
2.05M
{
1327
2.05M
    gs_memory_t *mem = COS_OBJECT_MEMORY(pcd);
1328
2.05M
    cos_dict_element_t **ppcde = &pcd->elements;
1329
2.05M
    cos_dict_element_t *pcde;
1330
2.05M
    cos_dict_element_t *next;
1331
2.05M
    cos_value_t value;
1332
2.05M
    int code;
1333
1334
7.55M
    while ((next = *ppcde) != 0 &&
1335
5.52M
           bytes_compare(next->key.data, next->key.size, key_data, key_size)
1336
2.05M
           )
1337
5.49M
        ppcde = &next->next;
1338
2.05M
    if (next) {
1339
        /* We're replacing an existing element. */
1340
33.2k
        if ((pvalue->value_type == COS_VALUE_SCALAR ||
1341
0
             pvalue->value_type == COS_VALUE_CONST) &&
1342
33.2k
            pvalue->value_type == next->value.value_type &&
1343
27.7k
            !bytes_compare(pvalue->contents.chars.data, pvalue->contents.chars.size,
1344
27.7k
                next->value.contents.chars.data, next->value.contents.chars.size))
1345
20.9k
            return 0; /* Same as old value. */
1346
12.3k
        if ((pvalue->value_type == COS_VALUE_OBJECT ||
1347
12.3k
             pvalue->value_type == COS_VALUE_RESOURCE) &&
1348
0
            pvalue->value_type == next->value.value_type &&
1349
0
            pvalue->contents.object == next->value.contents.object)
1350
0
            return 0; /* Same as old value. */
1351
12.3k
        code = cos_copy_element_value(&value, mem, pvalue,
1352
12.3k
                                      (flags & DICT_COPY_VALUE) != 0);
1353
12.3k
        if (code < 0)
1354
0
            return code;
1355
12.3k
        cos_value_free(&next->value, mem,
1356
12.3k
                       "cos_dict_put(old value)");
1357
12.3k
        pcde = next;
1358
2.02M
    } else {
1359
        /* Create a new element. */
1360
2.02M
        byte *copied_key_data;
1361
1362
2.02M
        if (flags & DICT_COPY_KEY) {
1363
448k
            copied_key_data = gs_alloc_string(mem, key_size,
1364
448k
                                              "cos_dict_put(key)");
1365
448k
            if (copied_key_data == 0)
1366
0
                return_error(gs_error_VMerror);
1367
448k
            memcpy(copied_key_data, key_data, key_size);
1368
448k
        } else
1369
1.57M
            copied_key_data = (byte *)key_data; /* OK to break const */
1370
2.02M
        pcde = gs_alloc_struct(mem, cos_dict_element_t, &st_cos_dict_element,
1371
2.02M
                               "cos_dict_put(element)");
1372
2.02M
        code = cos_copy_element_value(&value, mem, pvalue,
1373
2.02M
                                      (flags & DICT_COPY_VALUE) != 0);
1374
2.02M
        if (pcde == 0 || code < 0) {
1375
0
            if (code >= 0)
1376
0
                cos_uncopy_element_value(&value, mem,
1377
0
                                         (flags & DICT_COPY_VALUE) != 0);
1378
0
            gs_free_object(mem, pcde, "cos_dict_put(element)");
1379
0
            if (flags & DICT_COPY_KEY)
1380
0
                gs_free_string(mem, copied_key_data, key_size,
1381
0
                               "cos_dict_put(key)");
1382
0
            return (code < 0 ? code : gs_note_error(gs_error_VMerror));
1383
0
        }
1384
2.02M
        pcde->key.data = copied_key_data;
1385
2.02M
        pcde->key.size = key_size;
1386
2.02M
        pcde->owns_key = (flags & DICT_FREE_KEY) != 0;
1387
2.02M
        pcde->next = next;
1388
2.02M
        *ppcde = pcde;
1389
2.02M
    }
1390
2.03M
    pcde->value = value;
1391
2.03M
    pcd->md5_valid = false;
1392
2.03M
    return 0;
1393
2.05M
}
1394
int
1395
cos_dict_put(cos_dict_t *pcd, const byte *key_data, uint key_size,
1396
             const cos_value_t *pvalue)
1397
134k
{
1398
134k
    return cos_dict_put_copy(pcd, key_data, key_size, pvalue, DICT_COPY_ALL);
1399
134k
}
1400
int
1401
cos_dict_put_no_copy(cos_dict_t *pcd, const byte *key_data, uint key_size,
1402
                     const cos_value_t *pvalue)
1403
334k
{
1404
334k
    return cos_dict_put_copy(pcd, key_data, key_size, pvalue,
1405
334k
                             DICT_COPY_KEY | DICT_FREE_KEY);
1406
334k
}
1407
int
1408
cos_dict_put_c_key(cos_dict_t *pcd, const char *key, const cos_value_t *pvalue)
1409
1.58M
{
1410
1.58M
    return cos_dict_put_copy(pcd, (const byte *)key, strlen(key), pvalue,
1411
1.58M
                             DICT_COPY_VALUE);
1412
1.58M
}
1413
int
1414
cos_dict_put_c_key_string(cos_dict_t *pcd, const char *key,
1415
                          const byte *data, uint size)
1416
824k
{
1417
824k
    cos_value_t value;
1418
1419
824k
    cos_string_value(&value, data, size);
1420
824k
    return cos_dict_put_c_key(pcd, key, &value);
1421
824k
}
1422
int
1423
cos_dict_put_c_key_int(cos_dict_t *pcd, const char *key, int value)
1424
555k
{
1425
555k
    char str[sizeof(int) * 8 / 3 + 3]; /* sign, rounding, 0 terminator */
1426
1427
555k
    gs_snprintf(str, sizeof(str), "%d", value);
1428
555k
    return cos_dict_put_c_key_string(pcd, key, (byte *)str, strlen(str));
1429
555k
}
1430
int
1431
cos_dict_put_c_key_bool(cos_dict_t *pcd, const char *key, bool value)
1432
19.4k
{
1433
19.4k
    return cos_dict_put_c_key_string(pcd, key,
1434
19.4k
                (const byte *)(value ? "true" : "false"),
1435
19.4k
                              (value ? 4 : 5));
1436
19.4k
}
1437
int
1438
cos_dict_put_c_key_real(cos_dict_t *pcd, const char *key, double value)
1439
11.6k
{
1440
11.6k
    byte str[50];   /****** ADHOC ******/
1441
11.6k
    stream s;
1442
1443
11.6k
    s_init(&s, NULL);
1444
11.6k
    swrite_string(&s, str, sizeof(str));
1445
11.6k
    pprintg1(&s, "%g", value);
1446
11.6k
    return cos_dict_put_c_key_string(pcd, key, str, stell(&s));
1447
11.6k
}
1448
int
1449
cos_dict_put_c_key_floats(gx_device_pdf *pdev, cos_dict_t *pcd, const char *key, const float *pf,
1450
                          uint size)
1451
22.4k
{
1452
22.4k
    cos_array_t *pca = cos_array_from_floats(pdev, pf, size,
1453
22.4k
                                             "cos_dict_put_c_key_floats");
1454
22.4k
    int code;
1455
1456
22.4k
    if (pca == 0)
1457
0
        return_error(gs_error_VMerror);
1458
22.4k
    code = cos_dict_put_c_key_object(pcd, key, COS_OBJECT(pca));
1459
22.4k
    if (code < 0)
1460
0
        COS_FREE(pca, "cos_dict_put_c_key_floats");
1461
22.4k
    return code;
1462
22.4k
}
1463
int
1464
cos_dict_put_c_key_object(cos_dict_t *pcd, const char *key, cos_object_t *pco)
1465
409k
{
1466
409k
    cos_value_t value;
1467
1468
409k
    return cos_dict_put_c_key(pcd, key, cos_object_value(&value, pco));
1469
409k
}
1470
1471
static int unescape_name(char *data, uint *size)
1472
459k
{
1473
459k
    int i, j, replace = 0;
1474
459k
    unsigned int byte = 0, shift = 4;
1475
459k
    unsigned char escapes[] = {0x00, 0x09, 0x0a, 0x0c, 0x0d, 0x20, 0x25, 0x29, 0x2f, 0x3c, 0x3e, 0x5b, 0x5d, 0x7b, 0x7d};
1476
1477
    /* We need only check up to the byte at size - 3, because we must have # and 2 following bytes */
1478
459k
    if (*size < 3)
1479
709
        return 0;
1480
1481
3.43M
    for (i=0;i< (*size) - 2;i++) {
1482
        /* Look for a potential escape */
1483
2.97M
        if (data[i] == '#') {
1484
352
            replace = 0;
1485
352
            byte = 0;
1486
352
            shift = 4;
1487
1488
            /* Check the two following bytes are valid Hex bytes */
1489
1.05k
            for (j = i + 1;j < i + 3;j++) {
1490
704
                if ((data[j] >= '0' && data[j] <= '9') ||
1491
0
                    (data[j] >= 'a' && data[j] <= 'f') ||
1492
704
                    (data[j] >= 'A' && data[j] <= 'F') ){
1493
                    /* Convert the two following bytes to a hex byte, as we go */
1494
704
                    if (data[j] <= '9')
1495
704
                        byte += (data[j] - 0x30) << shift;
1496
0
                    else
1497
0
                        if (data[j] <= 'F')
1498
0
                            byte += (data[j] - 0x37) << shift;
1499
0
                        else
1500
0
                            byte += (data[j] - 0x57) << shift;
1501
704
                    shift = 0;
1502
704
                } else
1503
0
                    break;
1504
704
            }
1505
352
            if (j < i + 2)
1506
0
                continue;
1507
1508
            /* Check the byte we got to see if it was a value needing to be escaped */
1509
2.11k
            for (j = 0;j < sizeof(escapes);j++) {
1510
2.11k
                if (byte == escapes[j]) {
1511
352
                    replace = 1;
1512
352
                    break;
1513
352
                }
1514
2.11k
            }
1515
            /* If it was, then replace it with the assembled byte */
1516
352
            if (replace) {
1517
352
                data[i] = byte;
1518
3.08k
                for (j = i + 1;j < *size - 2;j++) {
1519
2.72k
                    data[j] = data[j+2];
1520
2.72k
                }
1521
352
                *size-= 2;
1522
352
            }
1523
352
        }
1524
2.97M
    }
1525
459k
    return 0;
1526
459k
}
1527
1528
int
1529
cos_dict_put_string(cos_dict_t *pcd, const byte *key_data, uint key_size,
1530
                    const byte *value_data, uint value_size)
1531
68.8k
{
1532
68.8k
    cos_value_t cvalue;
1533
1534
68.8k
    if (value_size > 0 && value_data[0] == '/') {
1535
9.55k
        unescape_name((char *)value_data, &value_size);
1536
9.55k
    }
1537
68.8k
    return cos_dict_put(pcd, key_data, key_size,
1538
68.8k
                        cos_string_value(&cvalue, value_data, value_size));
1539
68.8k
}
1540
int
1541
cos_dict_put_string_copy(cos_dict_t *pcd, const char *key, const char *value)
1542
35.6k
{
1543
35.6k
    uint size = strlen(value) + 1;
1544
35.6k
    if (size > 1 && value[0] == '/') {
1545
21.0k
        unescape_name((char *)value, &size);
1546
21.0k
    }
1547
35.6k
    return cos_dict_put_c_key_string(pcd, key, (byte *)value, strlen(value));
1548
35.6k
}
1549
int
1550
cos_dict_put_c_strings(cos_dict_t *pcd, const char *key, const char *value)
1551
307k
{
1552
307k
    cos_value_t cvalue;
1553
1554
307k
    uint size = strlen(value) + 1;
1555
307k
    if (size > 1 && value[0] == '/') {
1556
159k
        unescape_name((char *)value, &size);
1557
159k
    }
1558
307k
    return cos_dict_put_c_key(pcd, key, cos_c_string_value(&cvalue, value));
1559
307k
}
1560
1561
/* Move all the elements from one dict to another. */
1562
int
1563
cos_dict_move_all(cos_dict_t *pcdto, cos_dict_t *pcdfrom)
1564
5
{
1565
5
    cos_dict_element_t *pcde = pcdfrom->elements;
1566
5
    cos_dict_element_t *head = pcdto->elements;
1567
1568
10
    while (pcde) {
1569
5
        cos_dict_element_t *next = pcde->next;
1570
1571
5
        if (cos_dict_find(pcdto, pcde->key.data, pcde->key.size)) {
1572
            /* Free the element, which has been superseded. */
1573
0
            cos_dict_element_free(pcdfrom, pcde, "cos_dict_move_all_from");
1574
5
        } else {
1575
            /* Move the element. */
1576
5
            pcde->next = head;
1577
5
            head = pcde;
1578
5
        }
1579
5
        pcde = next;
1580
5
    }
1581
5
    pcdto->elements = head;
1582
5
    pcdfrom->elements = 0;
1583
5
    pcdto->md5_valid = false;
1584
5
    return 0;
1585
5
}
1586
1587
/* Look up a key in a dictionary. */
1588
const cos_value_t *
1589
cos_dict_find(const cos_dict_t *pcd, const byte *key_data, uint key_size)
1590
200k
{
1591
200k
    cos_dict_element_t *pcde = pcd->elements;
1592
1593
537k
    for (; pcde; pcde = pcde->next)
1594
420k
        if (!bytes_compare(key_data, key_size, pcde->key.data, pcde->key.size))
1595
83.3k
            return &pcde->value;
1596
117k
    return 0;
1597
200k
}
1598
const cos_value_t *
1599
cos_dict_find_c_key(const cos_dict_t *pcd, const char *key)
1600
56.1k
{
1601
56.1k
    if (pcd == NULL)
1602
0
        return NULL;
1603
56.1k
    return cos_dict_find(pcd, (const byte *)key, strlen(key));
1604
56.1k
}
1605
1606
int cos_dict_hash(const cos_object_t *pco0, gs_md5_state_t *md5, gs_md5_byte_t *hash, gx_device_pdf *pdev)
1607
169k
{
1608
169k
    cos_dict_t *dict = (cos_dict_t *) pco0;
1609
169k
    cos_dict_element_t *pcde0 = dict->elements;
1610
1611
661k
    for (; pcde0; pcde0 = pcde0->next) {
1612
492k
        gs_md5_append(md5, pcde0->key.data, pcde0->key.size);
1613
492k
        cos_value_hash(&pcde0->value, md5, hash, pdev);
1614
492k
    }
1615
169k
    return 0;
1616
169k
}
1617
1618
/* Compare two dictionaries. */
1619
int
1620
cos_dict_equal(const cos_object_t *pco0, const cos_object_t *pco1, gx_device_pdf *pdev)
1621
1.15M
{
1622
1.15M
    int code = 0;
1623
1624
1.15M
    if (!pco0->md5_valid) {
1625
70.8k
        gs_md5_init((gs_md5_state_t *)&pco0->md5);
1626
70.8k
        code = cos_dict_hash(pco0, (gs_md5_state_t *)&pco0->md5, (gs_md5_byte_t *)pco0->hash, pdev);
1627
70.8k
        if (code < 0)
1628
0
            return code;
1629
70.8k
        gs_md5_finish((gs_md5_state_t *)&pco0->md5, (gs_md5_byte_t *)pco0->hash);
1630
70.8k
        ((cos_object_t *)pco0)->md5_valid = true;
1631
70.8k
    }
1632
1.15M
    if (!pco1->md5_valid) {
1633
64.0k
        gs_md5_init((gs_md5_state_t *)&pco1->md5);
1634
64.0k
        code = cos_dict_hash(pco1, (gs_md5_state_t *)&pco1->md5, (gs_md5_byte_t *)pco1->hash, pdev);
1635
64.0k
        if (code < 0)
1636
0
            return code;
1637
64.0k
        gs_md5_finish((gs_md5_state_t *)&pco1->md5, (gs_md5_byte_t *)pco1->hash);
1638
64.0k
        ((cos_object_t *)pco1)->md5_valid = true;
1639
64.0k
    }
1640
1.15M
    if (memcmp(&pco0->hash, &pco1->hash, 16) != 0)
1641
1.04M
        return false;
1642
1643
110k
    return true;
1644
1.15M
}
1645
1646
/* Process all entries in a dictionary. */
1647
int
1648
cos_dict_forall(const cos_dict_t *pcd, void *client_data,
1649
                int (*proc)(void *client_data, const byte *key_data, uint key_size, const cos_value_t *v))
1650
5.74k
{
1651
5.74k
    cos_dict_element_t *pcde = pcd->elements;
1652
1653
293k
    for (; pcde; pcde = pcde->next) {
1654
287k
        int code = proc(client_data, pcde->key.data, pcde->key.size, &pcde->value);
1655
1656
287k
        if (code != 0)
1657
0
            return code;
1658
287k
    }
1659
5.74k
    return 0;
1660
5.74k
}
1661
1662
/* Set up a parameter list that writes into a Cos dictionary. */
1663
1664
/* We'll implement the other printers later if we have to. */
1665
static param_proc_xmit_typed(cos_param_put_typed);
1666
static const gs_param_list_procs cos_param_list_writer_procs = {
1667
    cos_param_put_typed,
1668
    NULL /* begin_collection */ ,
1669
    NULL /* end_collection */ ,
1670
    NULL /* get_next_key */ ,
1671
    gs_param_request_default,
1672
    gs_param_requested_default
1673
};
1674
static int
1675
cos_param_put_typed(gs_param_list * plist, gs_param_name pkey,
1676
                    gs_param_typed_value * pvalue)
1677
334k
{
1678
334k
    cos_param_list_writer_t *const pclist =
1679
334k
        (cos_param_list_writer_t *)plist;
1680
334k
    gx_device_pdf *pdev = pclist->pdev;
1681
334k
    gs_memory_t *mem = pclist->memory;
1682
334k
    cos_value_t value;
1683
334k
    cos_array_t *pca;
1684
334k
    int key_len = strlen(pkey);
1685
334k
    byte key_chars[100];    /****** ADHOC ******/
1686
334k
    int code;
1687
1688
334k
    while(pdev->child)
1689
0
        pdev = (gx_device_pdf *)pdev->child;
1690
1691
334k
    if (key_len > sizeof(key_chars) - 1)
1692
0
        return_error(gs_error_limitcheck);
1693
334k
    switch (pvalue->type) {
1694
283k
    default: {
1695
283k
        param_printer_params_t ppp;
1696
283k
        printer_param_list_t pplist;
1697
283k
        stream s;
1698
283k
        int len, skip;
1699
283k
        byte *str;
1700
1701
283k
        s_init(&s, NULL);
1702
283k
        ppp = param_printer_params_default;
1703
283k
        ppp.prefix = ppp.suffix = ppp.item_prefix = ppp.item_suffix = 0;
1704
283k
        ppp.print_ok = pclist->print_ok;
1705
283k
        s_init_param_printer(&pplist, &ppp, &s);
1706
283k
        swrite_position_only(&s);
1707
283k
        param_write_typed((gs_param_list *)&pplist, "", pvalue);
1708
283k
        len = stell(&s);
1709
283k
        str = gs_alloc_string(mem, len, "cos_param_put(string)");
1710
283k
        if (str == 0)
1711
0
            return_error(gs_error_VMerror);
1712
283k
        swrite_string(&s, str, len);
1713
283k
        param_write_typed((gs_param_list *)&pplist, "", pvalue);
1714
        /*
1715
         * The string starts with an initial / or /<space>, which
1716
         * we need to remove.
1717
         */
1718
283k
        skip = (str[1] == ' ' ? 2 : 1);
1719
283k
        memmove(str, str + skip, len - skip);
1720
283k
        str = gs_resize_string(mem, str, len, len - skip,
1721
283k
                               "cos_param_put(string)");
1722
283k
        cos_string_value(&value, str, len - skip);
1723
283k
    }
1724
0
        break;
1725
11.4k
    case gs_param_type_int_array: {
1726
11.4k
        uint i;
1727
1728
11.4k
        pca = cos_array_alloc(pdev, "cos_param_put(array)");
1729
11.4k
        if (pca == 0)
1730
0
            return_error(gs_error_VMerror);
1731
23.5k
        for (i = 0; i < pvalue->value.ia.size; ++i)
1732
12.1k
            CHECK(cos_array_add_int(pca, pvalue->value.ia.data[i]));
1733
11.4k
    }
1734
50.4k
    av:
1735
50.4k
        cos_object_value(&value, COS_OBJECT(pca));
1736
50.4k
        break;
1737
39.0k
    case gs_param_type_float_array: {
1738
39.0k
        uint i;
1739
1740
39.0k
        pca = cos_array_alloc(pdev, "cos_param_put(array)");
1741
39.0k
        if (pca == 0)
1742
0
            return_error(gs_error_VMerror);
1743
133k
        for (i = 0; i < pvalue->value.ia.size; ++i)
1744
94.2k
            CHECK(cos_array_add_real(pca, pvalue->value.fa.data[i]));
1745
39.0k
    }
1746
39.0k
        goto av;
1747
39.0k
    case gs_param_type_string_array:
1748
0
    case gs_param_type_name_array:
1749
        /****** NYI ******/
1750
0
        return_error(gs_error_typecheck);
1751
334k
    }
1752
334k
    if (key_len > 99)
1753
0
        return_error(gs_error_rangecheck);
1754
334k
    memcpy(key_chars + 1, pkey, key_len);
1755
334k
    key_chars[0] = '/';
1756
334k
    return cos_dict_put_no_copy(pclist->pcd, key_chars, key_len + 1, &value);
1757
334k
}
1758
1759
int
1760
cos_param_list_writer_init(gx_device_pdf *pdev, cos_param_list_writer_t *pclist, cos_dict_t *pcd,
1761
                           int print_ok)
1762
103k
{
1763
103k
    gs_param_list_init((gs_param_list *)pclist, &cos_param_list_writer_procs,
1764
103k
                       COS_OBJECT_MEMORY(pcd));
1765
103k
    pclist->pcd = pcd;
1766
103k
    pclist->pdev = pdev;
1767
103k
    pclist->print_ok = print_ok;
1768
103k
    return 0;
1769
103k
}
1770
1771
/* ------ Streams ------ */
1772
1773
static cos_proc_release(cos_stream_release);
1774
cos_proc_write(cos_stream_write);
1775
static cos_proc_equal(cos_stream_equal);
1776
static cos_proc_hash(cos_stream_hash);
1777
const cos_object_procs_t cos_stream_procs = {
1778
    cos_stream_release, cos_stream_write, cos_stream_equal, cos_stream_hash
1779
};
1780
1781
cos_stream_t *
1782
cos_stream_alloc(gx_device_pdf *pdev, client_name_t cname)
1783
142k
{
1784
142k
    gs_memory_t *mem = pdev->pdf_memory;
1785
142k
    cos_stream_t *pcs =
1786
142k
        gs_alloc_struct(mem, cos_stream_t, &st_cos_object, cname);
1787
1788
142k
    cos_object_init((cos_object_t *)pcs, pdev, &cos_stream_procs);
1789
142k
    return pcs;
1790
142k
}
1791
1792
static void
1793
cos_stream_release(cos_object_t *pco, client_name_t cname)
1794
305k
{
1795
305k
    cos_stream_t *const pcs = (cos_stream_t *)pco;
1796
305k
    cos_stream_piece_t *cur;
1797
305k
    cos_stream_piece_t *next;
1798
1799
607k
    for (cur = pcs->pieces; cur; cur = next) {
1800
302k
        next = cur->next;
1801
302k
        gs_free_object(cos_object_memory(pco), cur, cname);
1802
302k
    }
1803
305k
    pcs->pieces = 0;
1804
305k
    cos_dict_release(pco, cname);
1805
305k
}
1806
1807
static int hash_cos_stream(const cos_object_t *pco0, gs_md5_state_t *md5, gs_md5_byte_t *hash, gx_device_pdf *pdev)
1808
4.31k
{
1809
4.31k
    const cos_stream_t *pcs = (const cos_stream_t *)pco0;
1810
4.31k
    cos_stream_piece_t *pcsp = pcs->pieces;
1811
4.31k
    gp_file *sfile = pdev->streams.file;
1812
4.31k
    byte *ptr;
1813
4.31k
    int64_t position_save;
1814
4.31k
    int result;
1815
1816
4.31k
    sflush(pdev->strm);
1817
4.31k
    sflush(pdev->streams.strm);
1818
4.31k
    position_save = gp_ftell(sfile);
1819
1820
4.31k
    if (!pcsp)
1821
4.06k
        return -1;
1822
1823
250
    gs_md5_init(md5);
1824
550
    while(pcsp) {
1825
300
        ptr = gs_malloc(pdev->memory, sizeof (byte), pcsp->size, "hash_cos_stream");
1826
300
        if (ptr == 0L) {
1827
0
            result = gs_note_error(gs_error_VMerror);
1828
0
            return result;
1829
0
        }
1830
300
        if (gp_fseek(sfile, pcsp->position, SEEK_SET) != 0)
1831
0
    return_error(gs_error_ioerror);
1832
1833
300
        if (gp_fread(ptr, 1, pcsp->size, sfile) != pcsp->size) {
1834
0
            gs_free(pdev->memory, ptr, sizeof (byte), pcsp->size, "hash_cos_stream");
1835
0
            result = gs_note_error(gs_error_ioerror);
1836
0
            return result;
1837
0
        }
1838
300
        gs_md5_append(md5, ptr, pcsp->size);
1839
300
        gs_free(pdev->memory, ptr, sizeof (byte), pcsp->size, "hash_cos_stream");
1840
300
        pcsp = pcsp->next;
1841
300
    }
1842
250
    gs_md5_finish(md5, (gs_md5_byte_t *)hash);
1843
250
    if (gp_fseek(sfile, position_save, SEEK_SET) != 0)
1844
0
      return_error(gs_error_ioerror);
1845
1846
250
    return 0;
1847
250
}
1848
1849
static int cos_stream_hash(const cos_object_t *pco0, gs_md5_state_t *md5, gs_md5_byte_t *hash, gx_device_pdf *pdev)
1850
26.0k
{
1851
26.0k
    cos_stream_t *pcs0 = (cos_stream_t *)pco0;
1852
26.0k
    int code=0;
1853
26.0k
    if (!pco0->stream_md5_valid) {
1854
4.31k
        code = hash_cos_stream(pco0, (gs_md5_state_t *)&pco0->md5, (gs_md5_byte_t *)&pco0->stream_hash, pdev);
1855
4.31k
        if (code < 0)
1856
4.06k
            return code;
1857
250
        pcs0->stream_md5_valid = 1;
1858
250
    }
1859
22.0k
    gs_md5_append(md5, (byte *)&pco0->stream_hash, sizeof(pco0->stream_hash));
1860
22.0k
    if (!pco0->md5_valid) {
1861
358
        gs_md5_init((gs_md5_state_t *)&pco0->md5);
1862
358
        code = cos_dict_hash(pco0, (gs_md5_state_t *)&pco0->md5, (gs_md5_byte_t *)pco0->hash, pdev);
1863
358
        if (code < 0)
1864
0
            return code;
1865
358
        gs_md5_finish((gs_md5_state_t *)&pco0->md5, (gs_md5_byte_t *)pco0->hash);
1866
358
        pcs0->md5_valid = 1;
1867
358
    }
1868
22.0k
    gs_md5_append(md5, (byte *)&pco0->hash, sizeof(pco0->hash));
1869
22.0k
    return code;
1870
22.0k
}
1871
1872
static int
1873
cos_stream_equal(const cos_object_t *pco0, const cos_object_t *pco1, gx_device_pdf *pdev)
1874
570k
{
1875
570k
    cos_stream_t *pcs0 = (cos_stream_t *)pco0;
1876
570k
    cos_stream_t *pcs1 = (cos_stream_t *)pco1;
1877
570k
    int code;
1878
570k
    gs_md5_state_t md5;
1879
570k
    gs_md5_byte_t hash[16];
1880
1881
570k
    gs_md5_init(&md5);
1882
1883
570k
    if (!pco0->stream_md5_valid) {
1884
28
        code = cos_stream_hash(pco0, &md5, (gs_md5_byte_t *)hash, pdev);
1885
28
        if (code < 0)
1886
6
            return false;
1887
28
    }
1888
570k
    if (!pco1->stream_md5_valid) {
1889
4.19k
        code = cos_stream_hash(pco1, &md5, (gs_md5_byte_t *)hash, pdev);
1890
4.19k
        if (code < 0)
1891
4.05k
            return false;
1892
4.19k
    }
1893
566k
    if (memcmp(&pcs0->stream_hash, &pcs1->stream_hash, 16) != 0)
1894
461k
        return false;
1895
105k
    code = cos_dict_equal(pco0, pco1, pdev);
1896
105k
    if (code < 0)
1897
0
        return false;
1898
105k
    if (!code)
1899
29.8k
        return false;
1900
75.1k
    return true;
1901
105k
}
1902
1903
/* Find the total length of a stream. */
1904
int64_t
1905
cos_stream_length(const cos_stream_t *pcs)
1906
784k
{
1907
784k
    return pcs->length;
1908
784k
}
1909
1910
/* Write the (dictionary) elements of a stream. */
1911
/* (This procedure is exported.) */
1912
int
1913
cos_stream_elements_write(const cos_stream_t *pcs, gx_device_pdf *pdev)
1914
130k
{
1915
130k
    return cos_elements_write(pdev->strm, pcs->elements, pdev, true, pcs->id);
1916
130k
}
1917
1918
/* Write the contents of a stream.  (This procedure is exported.) */
1919
int
1920
cos_stream_contents_write(const cos_stream_t *pcs, gx_device_pdf *pdev)
1921
220k
{
1922
220k
    stream *s = pdev->strm;
1923
220k
    cos_stream_piece_t *pcsp;
1924
220k
    cos_stream_piece_t *last;
1925
220k
    cos_stream_piece_t *next;
1926
220k
    gp_file *sfile = pdev->streams.file;
1927
220k
    int64_t end_pos;
1928
220k
    bool same_file = (pdev->sbstack_depth > 0);
1929
220k
    int code;
1930
220k
    stream_arcfour_state sarc4, *ss = NULL;
1931
1932
220k
    if (pdev->KeyLength) {
1933
0
        code = pdf_encrypt_init(pdev, pcs->id, &sarc4);
1934
0
        if (code < 0)
1935
0
            return code;
1936
0
        ss = &sarc4;
1937
0
    }
1938
220k
    sflush(s);
1939
220k
    sflush(pdev->streams.strm);
1940
1941
    /* Reverse the elements temporarily. */
1942
493k
    for (pcsp = pcs->pieces, last = NULL; pcsp; pcsp = next)
1943
273k
        next = pcsp->next, pcsp->next = last, last = pcsp;
1944
493k
    for (pcsp = last, code = 0; pcsp && code >= 0; pcsp = pcsp->next) {
1945
273k
        if (same_file) {
1946
126k
            code = pdf_copy_data_safe(s, sfile, pcsp->position, pcsp->size);
1947
126k
            if (code < 0)
1948
0
                return code;
1949
146k
        } else {
1950
146k
            end_pos = gp_ftell(sfile);
1951
146k
            if (gp_fseek(sfile, pcsp->position, SEEK_SET) != 0)
1952
0
        return_error(gs_error_ioerror);
1953
146k
            code = pdf_copy_data(s, sfile, pcsp->size, ss);
1954
146k
            if (code < 0)
1955
0
                return code;
1956
146k
            if (gp_fseek(sfile, end_pos, SEEK_SET) != 0)
1957
0
        return_error(gs_error_ioerror);
1958
146k
        }
1959
273k
    }
1960
    /* Reverse the elements back. */
1961
493k
    for (pcsp = last, last = NULL; pcsp; pcsp = next)
1962
273k
        next = pcsp->next, pcsp->next = last, last = pcsp;
1963
1964
220k
    return code;
1965
220k
}
1966
1967
int
1968
cos_stream_write(const cos_object_t *pco, gx_device_pdf *pdev, gs_id object_id)
1969
89.9k
{
1970
89.9k
    stream *s = pdev->strm;
1971
89.9k
    const cos_stream_t *const pcs = (const cos_stream_t *)pco;
1972
89.9k
    int code;
1973
1974
89.9k
    if (pcs->input_strm != NULL) {
1975
232
        stream *s = pco->input_strm;
1976
232
        int status = s_close_filters(&s, NULL);
1977
1978
232
        if (status < 0)
1979
0
            return_error(gs_error_ioerror);
1980
        /* We have to break const here to clear the input_strm. */
1981
232
        ((cos_object_t *)pco)->input_strm = 0;
1982
232
    }
1983
89.9k
    stream_puts(s, "<<");
1984
89.9k
    cos_elements_write(s, pcs->elements, pdev, false, object_id);
1985
89.9k
    pprinti64d1(s, "/Length %"PRId64">>stream\n", cos_stream_length(pcs));
1986
89.9k
    code = cos_stream_contents_write(pcs, pdev);
1987
89.9k
    stream_puts(s, "\nendstream\n");
1988
1989
89.9k
    return code;
1990
89.9k
}
1991
1992
/* Return a stream's dictionary (just a cast). */
1993
cos_dict_t *
1994
cos_stream_dict(cos_stream_t *pcs)
1995
198k
{
1996
198k
    return (cos_dict_t *)pcs;
1997
198k
}
1998
1999
/* Add a contents piece to a stream object: size bytes just written on */
2000
/* streams.strm. */
2001
int
2002
cos_stream_add(gx_device_pdf *pdev, cos_stream_t *pcs, uint size)
2003
3.13M
{
2004
3.13M
    stream *s;
2005
3.13M
    gs_offset_t position;
2006
3.13M
    cos_stream_piece_t *prev = pcs->pieces;
2007
2008
    /* Beware of subclassed devices. This is mad IMO, we should store
2009
     * 's' in the stream state somewhere.
2010
     */
2011
3.13M
    while (pdev->child)
2012
0
        pdev = (gx_device_pdf *)pdev->child;
2013
2014
3.13M
    s = pdev->streams.strm;
2015
3.13M
    position = stell(s);
2016
2017
    /* Check for consecutive writing -- just an optimization. */
2018
3.13M
    if (prev != 0 && prev->position + prev->size + size == position) {
2019
2.77M
        prev->size += size;
2020
2.77M
    } else {
2021
362k
        gs_memory_t *mem = pdev->pdf_memory;
2022
362k
        cos_stream_piece_t *pcsp =
2023
362k
            gs_alloc_struct(mem, cos_stream_piece_t, &st_cos_stream_piece,
2024
362k
                            "cos_stream_add");
2025
2026
362k
        if (pcsp == 0)
2027
0
            return_error(gs_error_VMerror);
2028
362k
        pcsp->position = position - size;
2029
362k
        pcsp->size = size;
2030
362k
        pcsp->next = pcs->pieces;
2031
362k
        pcs->pieces = pcsp;
2032
362k
    }
2033
3.13M
    pcs->length += size;
2034
3.13M
    return 0;
2035
3.13M
}
2036
2037
/* Add bytes to a stream object. */
2038
int
2039
cos_stream_add_bytes(gx_device_pdf *pdev, cos_stream_t *pcs, const byte *data, uint size)
2040
5.67k
{
2041
5.67k
    stream_write(pdev->streams.strm, data, size);
2042
5.67k
    return cos_stream_add(pdev, pcs, size);
2043
5.67k
}
2044
2045
/* Add the contents of a stream to a stream object. */
2046
int
2047
cos_stream_add_stream_contents(gx_device_pdf *pdev, cos_stream_t *pcs, stream *s)
2048
45
{
2049
45
    int code = 0;
2050
45
    byte sbuff[200];  /* arbitrary */
2051
45
    uint cnt;
2052
45
    int status = sseek(s, 0);
2053
2054
45
    if (status < 0)
2055
0
        return_error(gs_error_ioerror);
2056
4.04k
    do {
2057
4.04k
        status = sgets(s, sbuff, sizeof(sbuff), &cnt);
2058
2059
4.04k
        if (cnt == 0) {
2060
45
            if (status == EOFC)
2061
45
                break;
2062
45
            return_error(gs_error_ioerror);
2063
45
        }
2064
4.04k
    } while ((code = cos_stream_add_bytes(pdev, pcs, sbuff, cnt)) >= 0);
2065
45
    return code;
2066
45
}
2067
2068
/* Release the last contents piece of a stream object. */
2069
/* Warning : this function can't release pieces if another stream is written after them. */
2070
int
2071
cos_stream_release_pieces(gx_device_pdf *pdev, cos_stream_t *pcs)
2072
64.7k
{
2073
64.7k
    stream *s = pdev->streams.strm;
2074
64.7k
    gs_offset_t position = stell(s), position0 = position;
2075
2076
124k
    while (pcs->pieces != NULL &&
2077
64.8k
                position == pcs->pieces->position + pcs->pieces->size) {
2078
60.0k
        cos_stream_piece_t *p = pcs->pieces;
2079
2080
60.0k
        position -= p->size;
2081
60.0k
        pcs->pieces = p->next;
2082
60.0k
        gs_free_object(cos_object_memory((cos_object_t *)pcs), p, "cos_stream_release_pieces");
2083
60.0k
    }
2084
64.7k
    if (position0 != position)
2085
60.0k
        if (sseek(s, position) < 0)
2086
0
            return_error(gs_error_ioerror);
2087
64.7k
    return 0;
2088
64.7k
}
2089
2090
/* Create a stream that writes into a Cos stream. */
2091
/* Closing the stream will free it. */
2092
/* Note that this is not a filter. */
2093
typedef struct cos_write_stream_state_s {
2094
    stream_state_common;
2095
    cos_stream_t *pcs;
2096
    gx_device_pdf *pdev;
2097
    stream *s;      /* pointer back to stream */
2098
    stream *target;   /* use this instead of strm */
2099
} cos_write_stream_state_t;
2100
gs_private_st_suffix_add4(st_cos_write_stream_state, cos_write_stream_state_t,
2101
                          "cos_write_stream_state_t",
2102
                          cos_ws_state_enum_ptrs, cos_ws_state_reloc_ptrs,
2103
                          st_stream_state, pcs, pdev, s, target);
2104
2105
static int
2106
cos_write_stream_process(stream_state * st, stream_cursor_read * pr,
2107
                         stream_cursor_write * ignore_pw, bool last)
2108
3.13M
{
2109
3.13M
    uint count = pr->limit - pr->ptr;
2110
3.13M
    cos_write_stream_state_t *ss = (cos_write_stream_state_t *)st;
2111
3.13M
    stream *target = ss->target;
2112
3.13M
    gx_device_pdf *pdev = ss->pdev;
2113
3.13M
    gs_offset_t start_pos;
2114
3.13M
    int code;
2115
2116
3.13M
    while(pdev->child != NULL)
2117
0
        pdev = (gx_device_pdf *)pdev->child;
2118
2119
3.13M
    start_pos = stell(pdev->streams.strm);
2120
3.13M
    stream_write(target, pr->ptr + 1, count);
2121
3.13M
    gs_md5_append(&ss->pcs->md5, pr->ptr + 1, count);
2122
3.13M
    pr->ptr = pr->limit;
2123
3.13M
    sflush(target);
2124
3.13M
    code = cos_stream_add(pdev, ss->pcs, (uint)(stell(pdev->streams.strm) - start_pos));
2125
3.13M
    return (code < 0 ? ERRC : 0);
2126
3.13M
}
2127
static int
2128
cos_write_stream_close(stream *s)
2129
303k
{
2130
303k
    cos_write_stream_state_t *ss = (cos_write_stream_state_t *)s->state;
2131
303k
    int status;
2132
303k
    gx_device_pdf *target_dev = (gx_device_pdf *)ss->pdev;
2133
2134
303k
    while (target_dev->child != NULL)
2135
0
        target_dev = (gx_device_pdf *)target_dev->child;
2136
2137
303k
    sflush(s);
2138
303k
    status = s_close_filters(&ss->target, target_dev->streams.strm);
2139
303k
    gs_md5_finish(&ss->pcs->md5, (gs_md5_byte_t *)ss->pcs->stream_hash);
2140
303k
    ss->pcs->stream_md5_valid = 1;
2141
303k
    return (status < 0 ? status : s_std_close(s));
2142
303k
}
2143
2144
static const stream_procs cos_s_procs = {
2145
    s_std_noavailable, s_std_noseek, s_std_write_reset,
2146
    s_std_write_flush, cos_write_stream_close, cos_write_stream_process
2147
};
2148
static const stream_template cos_write_stream_template = {
2149
    &st_cos_write_stream_state, 0, cos_write_stream_process, 1, 1
2150
};
2151
stream *
2152
cos_write_stream_alloc(cos_stream_t *pcs, gx_device_pdf *pdev,
2153
                       client_name_t cname)
2154
303k
{
2155
303k
    gs_memory_t *mem = pdev->pdf_memory;
2156
303k
    stream *s = s_alloc(mem, cname);
2157
303k
    cos_write_stream_state_t *ss = (cos_write_stream_state_t *)
2158
303k
        s_alloc_state(mem, &st_cos_write_stream_state, cname);
2159
303k
#define CWS_BUF_SIZE 512  /* arbitrary */
2160
303k
    byte *buf = gs_alloc_bytes(mem, CWS_BUF_SIZE, cname);
2161
2162
303k
    if (s == 0 || ss == 0 || buf == 0)
2163
0
        goto fail;
2164
303k
    ss->templat = &cos_write_stream_template;
2165
303k
    ss->pcs = pcs;
2166
303k
    ss->pcs->stream_md5_valid = 0;
2167
303k
    gs_md5_init(&ss->pcs->md5);
2168
303k
    memset(&ss->pcs->hash, 0x00, 16);
2169
303k
    ss->pdev = pdev;
2170
303k
    while(ss->pdev->parent)
2171
0
        ss->pdev = (gx_device_pdf *)ss->pdev->parent;
2172
303k
    ss->s = s;
2173
303k
    ss->target = pdev->streams.strm; /* not s->strm */
2174
303k
    s_std_init(s, buf, CWS_BUF_SIZE, &cos_s_procs, s_mode_write);
2175
303k
    s->state = (stream_state *)ss;
2176
303k
    return s;
2177
0
#undef CWS_BUF_SIZE
2178
0
 fail:
2179
0
    gs_free_object(mem, buf, cname);
2180
0
    gs_free_object(mem, ss, cname);
2181
0
    gs_free_object(mem, s, cname);
2182
0
    return 0;
2183
303k
}
2184
2185
/* Get cos stream from pipeline. */
2186
cos_stream_t *
2187
cos_stream_from_pipeline(stream *s)
2188
990k
{
2189
990k
    cos_write_stream_state_t *ss;
2190
2191
2.91M
    while(s->procs.process != cos_s_procs.process) {
2192
1.92M
        s = s->strm;
2193
1.92M
        if (s == 0L)
2194
0
            return 0L;
2195
1.92M
    }
2196
2197
990k
    ss = (cos_write_stream_state_t *)s->state;
2198
990k
    return ss->pcs;
2199
990k
}