Coverage Report

Created: 2026-11-12 07:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ghostpdl/base/gslibctx.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
        Some notes on the structure here:
18
19
        At the top level, we have 'instances' of Ghostscript (or GPL).
20
        Here, 'instance' is short for 'instance of the Ghostscript API'.
21
        Each instance is returned by 'gsapi_new_instance'. Every new
22
        instance gets a unique gs_lib_ctx_core_t. Each instance can be
23
        called from any number of threads, but only from one thread at
24
        a time!
25
26
        Each instance of Ghostscript owns one or more interpreters.
27
        Each interpreter gets a unique gs_lib_ctx_t, that shares the
28
        instance's gs_lib_ctx_core_t.
29
30
        Each interpreter (by which we include the graphics library
31
        called by that interpreter) can make multiple gs_memory_t's.
32
        Certainly, every simultaneous (rendering) thread created by by
33
        the interpreter will get a unique gs_memory_t. These
34
        gs_memory_t's share the same gs_lib_ctx_t (and hence the same
35
        gs_lib_ctx_core_t).
36
*/
37
38
39
/* library context functionality for ghostscript
40
 * api callers get a gs_main_instance
41
 */
42
43
/* Capture stdin/out/err before gs.h redefines them. */
44
#include "stdio_.h"
45
#include "string_.h" /* memset */
46
#include "gp.h"
47
#include "gpmisc.h"
48
#include "gsicc_manage.h"
49
#include "gserrors.h"
50
#include "gscdefs.h"            /* for gs_lib_device_list */
51
#include "gsstruct.h"           /* for gs_gc_root_t */
52
#ifdef WITH_CAL
53
#include "cal.h"
54
#endif
55
#include "gsargs.h"
56
#include "globals.h"
57
58
/* Include the extern for the device list. */
59
extern_gs_lib_device_list();
60
61
/* forward declaration */
62
static void gs_purge_permitted_devices(const gs_memory_t *mem);
63
64
static void
65
gs_lib_ctx_get_real_stdio(FILE **in, FILE **out, FILE **err)
66
166k
{
67
166k
    *in = stdin;
68
166k
    *out = stdout;
69
166k
    *err = stderr;
70
166k
}
71
72
#include "gslibctx.h"
73
#include "gsmemory.h"
74
75
/*  This sets the directory to prepend to the ICC profile names specified for
76
    defaultgray, defaultrgb, defaultcmyk, proofing, linking, named color and device */
77
int
78
gs_lib_ctx_set_icc_directory(const gs_memory_t *mem_gc, const char* pname,
79
                             int dir_namelen)
80
881k
{
81
881k
    char *result;
82
881k
    gs_lib_ctx_t *p_ctx = mem_gc->gs_lib_ctx;
83
881k
    gs_memory_t *p_ctx_mem = p_ctx->memory;
84
85
    /* If it is already set and the incoming is the default then don't set
86
       as we are coming from a VMreclaim which is trying to reset the user
87
       parameter */
88
881k
    if (p_ctx->profiledir != NULL && strcmp(pname,DEFAULT_DIR_ICC) == 0) {
89
695k
        return 0;
90
695k
    }
91
185k
    if (p_ctx->profiledir != NULL && p_ctx->profiledir_len > 0) {
92
0
        if (strncmp(pname, p_ctx->profiledir, p_ctx->profiledir_len) == 0) {
93
0
            return 0;
94
0
        }
95
0
        gs_free_object(p_ctx_mem, p_ctx->profiledir,
96
0
                       "gs_lib_ctx_set_icc_directory");
97
0
        p_ctx->profiledir = NULL;
98
0
        p_ctx->profiledir_len = 0;
99
0
    }
100
    /* User param string.  Must allocate in non-gc memory */
101
185k
    result = (char*) gs_alloc_bytes(p_ctx_mem, dir_namelen+1,
102
185k
                                     "gs_lib_ctx_set_icc_directory");
103
185k
    if (result == NULL) {
104
0
        return gs_error_VMerror;
105
0
    }
106
185k
    strcpy(result, pname);
107
185k
    p_ctx->profiledir = result;
108
185k
    p_ctx->profiledir_len = dir_namelen;
109
185k
    return 0;
110
185k
}
111
112
/* Sets/Gets the string containing the list of default devices we should try */
113
int
114
gs_lib_ctx_set_default_device_list(const gs_memory_t *mem, const char* dev_list_str,
115
                        int list_str_len)
116
185k
{
117
185k
    char *result;
118
185k
    gs_lib_ctx_t *p_ctx = mem->gs_lib_ctx;
119
185k
    gs_memory_t *ctx_mem = p_ctx->memory;
120
185k
    int code = 0;
121
122
185k
    result = (char *)gs_alloc_bytes(ctx_mem, list_str_len + 1,
123
185k
             "gs_lib_ctx_set_default_device_list");
124
125
185k
    if (result) {
126
185k
      gs_free_object(ctx_mem, p_ctx->default_device_list,
127
185k
                "gs_lib_ctx_set_default_device_list");
128
129
185k
      memcpy(result, dev_list_str, list_str_len);
130
185k
      result[list_str_len] = '\0';
131
185k
      p_ctx->default_device_list = result;
132
185k
    }
133
0
    else {
134
0
        code = gs_note_error(gs_error_VMerror);
135
0
    }
136
185k
    return code;
137
185k
}
138
139
int
140
gs_lib_ctx_get_default_device_list(const gs_memory_t *mem, char** dev_list_str,
141
                        int *list_str_len)
142
0
{
143
    /* In the case the lib ctx hasn't been initialised */
144
0
    if (mem && mem->gs_lib_ctx && mem->gs_lib_ctx->default_device_list) {
145
0
        *dev_list_str = mem->gs_lib_ctx->default_device_list;
146
0
    }
147
0
    else {
148
0
        *dev_list_str = (char *)gs_dev_defaults;
149
0
    }
150
151
0
    *list_str_len = strlen(*dev_list_str);
152
153
0
    return 0;
154
0
}
155
156
static int
157
gs_lib_ctx_alloc_root_structure(gs_memory_t *mem, gs_gc_root_ptr *rp)
158
555k
{
159
555k
  int code = 0;
160
161
555k
  *rp = gs_raw_alloc_struct_immovable(mem, &st_gc_root_t, "gs_lib_ctx_alloc_root_structure");
162
555k
  if (*rp == 0)
163
0
    code = gs_note_error(gs_error_VMerror);
164
165
555k
  return code;
166
555k
}
167
168
static int
169
fs_file_open_file(const gs_memory_t *mem,
170
                        void        *secret,
171
                  const char        *fname,
172
                  const char        *mode,
173
                        gp_file    **file)
174
48.7M
{
175
48.7M
    FILE *f;
176
177
48.7M
    *file = gp_file_FILE_alloc(mem);
178
48.7M
    if (*file == NULL)
179
0
        return 0;
180
48.7M
    f = gp_fopen_impl(mem->non_gc_memory, fname, mode);
181
48.7M
    if (gp_file_FILE_set(*file, f, fclose)) {
182
48.4M
        *file = NULL;
183
48.4M
        return gs_error_VMerror;
184
48.4M
    }
185
220k
    return 0;
186
48.7M
}
187
188
static int
189
fs_file_open_scratch(const gs_memory_t *mem, void *secret, const char *prefix, char *rfname, const char *mode, int rm, gp_file **file)
190
256k
{
191
256k
    *file = gp_file_FILE_alloc(mem);
192
256k
    if (*file == NULL)
193
0
        return gs_error_VMerror;
194
256k
    if (gp_file_FILE_set(*file,
195
256k
                         gp_open_scratch_file_impl(mem,
196
256k
                                                   prefix,
197
256k
                                                   rfname,
198
256k
                                                   mode,
199
256k
                                                   rm),
200
256k
                                                   NULL)) {
201
0
        *file = NULL;
202
0
        return gs_error_invalidfileaccess;
203
0
    }
204
205
256k
    return 0;
206
256k
}
207
208
static int
209
fs_file_open_printer(const gs_memory_t *mem, void *secret, const char *fname, int binary_mode, gp_file **file)
210
70.2k
{
211
70.2k
    FILE *f;
212
70.2k
    int (*close)(FILE *) = NULL;
213
214
70.2k
    *file = gp_file_FILE_alloc(mem);
215
70.2k
    if (*file == NULL)
216
0
        return gs_error_VMerror;
217
70.2k
    f = gp_open_printer_impl(mem->non_gc_memory, fname, &binary_mode, &close);
218
70.2k
    if (gp_file_FILE_set(*file, f, close)) {
219
0
        *file = NULL;
220
0
        return gs_error_invalidfileaccess;
221
0
    }
222
    /* The lgtm comment below is required because on some platforms that function
223
     * does nothing. */
224
70.2k
    gp_setmode_binary_impl(f, binary_mode); /* lgtm [cpp/useless-expression] */
225
226
70.2k
    return 0;
227
70.2k
}
228
229
#ifdef WITH_CAL
230
static void *
231
cal_do_malloc(void *opaque, size_t size)
232
{
233
    gs_memory_t *mem = (gs_memory_t *)opaque;
234
    return gs_alloc_bytes(mem, size, "cal_do_malloc");
235
}
236
237
static void *
238
cal_do_realloc(void *opaque, void *ptr, size_t newsize)
239
{
240
    gs_memory_t *mem = (gs_memory_t *)opaque;
241
    return gs_resize_object(mem, ptr, newsize, "cal_do_malloc");
242
}
243
244
static void
245
cal_do_free(void *opaque, void *ptr)
246
{
247
    gs_memory_t *mem = (gs_memory_t *)opaque;
248
    gs_free_object(mem, ptr, "cal_do_malloc");
249
}
250
251
static cal_allocators cal_allocs =
252
{
253
    cal_do_malloc,
254
    cal_do_realloc,
255
    cal_do_free
256
};
257
#endif
258
259
int gs_lib_ctx_init(gs_lib_ctx_t *ctx, gs_memory_t *mem)
260
185k
{
261
185k
    gs_lib_ctx_t *pio = NULL;
262
185k
    gs_globals *globals;
263
264
    /* Check the non gc allocator is being passed in */
265
185k
    if (mem == 0 || mem != mem->non_gc_memory)
266
0
        return_error(gs_error_Fatal);
267
268
    /* Get globals here, earlier than it seems we might need it
269
     * because a side effect of this is ensuring that the thread
270
     * local storage for the malloc pointer is set up. */
271
185k
    globals = gp_get_globals();
272
273
    /* Now it's safe to set this. */
274
185k
    gp_set_debug_mem_ptr(mem);
275
276
185k
    if (mem->gs_lib_ctx) /* one time initialization */
277
0
        return 0;
278
279
185k
    pio = (gs_lib_ctx_t*)gs_alloc_bytes_immovable(mem,
280
185k
                                                  sizeof(gs_lib_ctx_t),
281
185k
                                                  "gs_lib_ctx_init");
282
185k
    if(pio == NULL)
283
0
        return -1;
284
285
    /* Wholesale blanking is cheaper than retail, and scales better when new
286
     * fields are added. */
287
185k
    memset(pio, 0, sizeof(*pio));
288
289
185k
    if (ctx != NULL) {
290
18.4k
        pio->core = ctx->core;
291
18.4k
        gx_monitor_enter((gx_monitor_t *)(pio->core->monitor));
292
18.4k
        pio->core->refs++;
293
18.4k
        gx_monitor_leave((gx_monitor_t *)(pio->core->monitor));
294
166k
    } else {
295
166k
        pio->core = (gs_lib_ctx_core_t *)gs_alloc_bytes_immovable(mem,
296
166k
                                                                  sizeof(gs_lib_ctx_core_t),
297
166k
                                                                  "gs_lib_ctx_init(core)");
298
166k
        if (pio->core == NULL) {
299
0
            gs_free_object(mem, pio, "gs_lib_ctx_init");
300
0
            return -1;
301
0
        }
302
166k
        memset(pio->core, 0, sizeof(*pio->core));
303
166k
        pio->core->globals = globals;
304
166k
        pio->core->fs = (gs_fs_list_t *)gs_alloc_bytes_immovable(mem,
305
166k
                                                                 sizeof(gs_fs_list_t),
306
166k
                                                                 "gs_lib_ctx_init(gs_fs_list_t)");
307
166k
        if (pio->core->fs == NULL) {
308
0
            gs_free_object(mem, pio->core, "gs_lib_ctx_init");
309
0
            gs_free_object(mem, pio, "gs_lib_ctx_init");
310
0
            return -1;
311
0
        }
312
#ifdef WITH_CAL
313
        pio->core->cal_ctx = cal_init(&cal_allocs, mem);
314
        if (pio->core->cal_ctx == NULL) {
315
            gs_free_object(mem, pio->core->fs, "gs_lib_ctx_init");
316
            gs_free_object(mem, pio->core, "gs_lib_ctx_init");
317
            gs_free_object(mem, pio, "gs_lib_ctx_init");
318
            return -1;
319
        }
320
#endif
321
166k
        pio->core->fs->fs.open_file = fs_file_open_file;
322
        /* The iodev will fill this in later, if pipes are enabled */
323
166k
        pio->core->fs->fs.open_pipe = NULL;
324
166k
        pio->core->fs->fs.open_scratch = fs_file_open_scratch;
325
166k
        pio->core->fs->fs.open_printer = fs_file_open_printer;
326
166k
        pio->core->fs->secret = NULL;
327
166k
        pio->core->fs->memory = mem;
328
166k
        pio->core->fs->next   = NULL;
329
330
166k
        pio->core->monitor = gx_monitor_alloc(mem);
331
166k
        if (pio->core->monitor == NULL)
332
0
            goto core_create_failed;
333
166k
        pio->core->refs = 1;
334
166k
        pio->core->memory = mem;
335
336
        /* Set the non-zero "shared" things */
337
166k
        gs_lib_ctx_get_real_stdio(&pio->core->fstdin, &pio->core->fstdout, &pio->core->fstderr );
338
166k
        pio->core->stdin_is_interactive = true;
339
        /* id's 1 through 4 are reserved for Device color spaces; see gscspace.h */
340
166k
        pio->core->gs_next_id = 5; /* Cloned contexts share the state */
341
        /* Set scanconverter to 1 (default) */
342
166k
        pio->core->scanconverter = GS_SCANCONVERTER_DEFAULT;
343
        /* Initialise the underlying CMS. */
344
166k
        pio->core->cms_context = gscms_create(mem);
345
166k
        if (pio->core->cms_context == NULL) {
346
0
            gx_monitor_free((gx_monitor_t *)(pio->core->monitor));
347
0
core_create_failed:
348
#ifdef WITH_CAL
349
            cal_fin(pio->core->cal_ctx, mem);
350
#endif
351
0
            gs_free_object(mem, pio->core->fs, "gs_lib_ctx_init");
352
0
            gs_free_object(mem, pio->core, "gs_lib_ctx_init");
353
0
            gs_free_object(mem, pio, "gs_lib_ctx_init");
354
0
            return -1;
355
0
        }
356
166k
    }
357
358
    /* Now set the non zero/false/NULL things */
359
185k
    pio->memory               = mem;
360
361
    /* Need to set this before calling gs_lib_ctx_set_icc_directory. */
362
185k
    mem->gs_lib_ctx = pio;
363
    /* Initialize our default ICCProfilesDir */
364
185k
    pio->profiledir = NULL;
365
185k
    pio->profiledir_len = 0;
366
185k
    pio->icc_color_accuracy = MAX_COLOR_ACCURACY;
367
185k
    if (gs_lib_ctx_set_icc_directory(mem, DEFAULT_DIR_ICC, strlen(DEFAULT_DIR_ICC)) < 0)
368
0
      goto Failure;
369
370
185k
    if (gs_lib_ctx_set_default_device_list(mem, gs_dev_defaults,
371
185k
                                           strlen(gs_dev_defaults)) < 0)
372
0
        goto Failure;
373
374
    /* Initialise any lock required for the jpx codec */
375
185k
    if (sjpxd_create(mem))
376
0
        goto Failure;
377
378
185k
    gp_get_realtime(pio->real_time_0);
379
380
185k
    if (gs_lib_ctx_alloc_root_structure(mem, &pio->name_table_root))
381
0
        goto Failure;
382
383
185k
    if (gs_lib_ctx_alloc_root_structure(mem, &pio->io_device_table_root))
384
0
        goto Failure;
385
386
185k
    if (gs_lib_ctx_alloc_root_structure(mem, &pio->font_dir_root))
387
0
        goto Failure;
388
185k
    if (gs_add_control_path(mem, gs_permit_file_writing, gp_null_file_name) < 0)
389
0
        goto Failure;
390
391
185k
    return 0;
392
393
0
Failure:
394
0
    gs_lib_ctx_fin(mem);
395
0
    return -1;
396
185k
}
397
398
static void remove_ctx_pointers(gs_memory_t *mem)
399
185k
{
400
185k
    mem->gs_lib_ctx = NULL;
401
185k
    if (mem->stable_memory && mem->stable_memory != mem)
402
0
        remove_ctx_pointers(mem->stable_memory);
403
185k
    if (mem->non_gc_memory && mem->non_gc_memory != mem)
404
0
        remove_ctx_pointers(mem->non_gc_memory);
405
185k
    if (mem->thread_safe_memory && mem->thread_safe_memory != mem)
406
0
        remove_ctx_pointers(mem->thread_safe_memory);
407
185k
}
408
409
void gs_lib_ctx_fin(gs_memory_t *mem)
410
185k
{
411
185k
    gs_lib_ctx_t *ctx;
412
185k
    gs_memory_t *ctx_mem;
413
185k
    int refs, i;
414
185k
    gs_fs_list_t *fs;
415
185k
    gs_callout_list_t *entry;
416
417
185k
    if (!mem || !mem->gs_lib_ctx)
418
0
        return;
419
420
185k
    ctx = mem->gs_lib_ctx;
421
185k
    ctx_mem = ctx->memory;
422
423
185k
    sjpxd_destroy(mem);
424
185k
    gs_free_object(ctx_mem, ctx->profiledir,
425
185k
        "gs_lib_ctx_fin");
426
427
185k
    gs_free_object(ctx_mem, ctx->default_device_list,
428
185k
                "gs_lib_ctx_fin");
429
430
185k
    gs_free_object(ctx_mem, ctx->name_table_root, "gs_lib_ctx_fin");
431
185k
    gs_free_object(ctx_mem, ctx->io_device_table_root, "gs_lib_ctx_fin");
432
185k
    gs_free_object(ctx_mem, ctx->font_dir_root, "gs_lib_ctx_fin");
433
434
185k
    gx_monitor_enter((gx_monitor_t *)(ctx->core->monitor));
435
185k
    refs = --ctx->core->refs;
436
185k
    gx_monitor_leave((gx_monitor_t *)(ctx->core->monitor));
437
185k
    if (refs == 0) {
438
166k
        gscms_destroy(ctx->core->cms_context);
439
166k
        gx_monitor_free((gx_monitor_t *)(ctx->core->monitor));
440
#ifdef WITH_CAL
441
        cal_fin(ctx->core->cal_ctx, ctx->core->memory);
442
#endif
443
166k
        gs_purge_scratch_files(ctx->core->memory);
444
166k
        gs_purge_control_paths(ctx->core->memory, gs_permit_file_reading);
445
166k
        gs_purge_control_paths(ctx->core->memory, gs_permit_file_writing);
446
166k
        gs_purge_control_paths(ctx->core->memory, gs_permit_file_control);
447
448
166k
        gs_purge_permitted_devices(ctx->core->memory);
449
450
166k
        fs = ctx->core->fs;
451
333k
        while (fs) {
452
166k
            gs_fs_list_t *next = fs->next;
453
166k
            gs_free_object(fs->memory, fs, "gs_lib_ctx_fin");
454
166k
            fs = next;
455
166k
        }
456
457
166k
        entry = ctx->core->callouts;
458
166k
        while (entry) {
459
0
            gs_callout_list_t *next = entry->next;
460
0
            gs_free_object(mem->non_gc_memory, entry, "gs_callout_list_t");
461
0
            entry = next;
462
0
        }
463
464
3.01M
        for (i = 0; i < ctx->core->argc; i++)
465
2.85M
            gs_free_object(ctx->core->memory, ctx->core->argv[i], "gs_lib_ctx_arg");
466
166k
        gs_free_object(ctx->core->memory, ctx->core->argv, "gs_lib_ctx_args");
467
468
166k
        gs_free_object(ctx->core->memory, ctx->core, "gs_lib_ctx_fin");
469
166k
    }
470
185k
    remove_ctx_pointers(ctx_mem);
471
472
185k
    gs_free_object(ctx_mem, ctx, "gs_lib_ctx_init");
473
185k
}
474
475
gs_lib_ctx_t *gs_lib_ctx_get_interp_instance(const gs_memory_t *mem)
476
816M
{
477
816M
    if (mem == NULL)
478
0
        return NULL;
479
816M
    return mem->gs_lib_ctx;
480
816M
}
481
482
void *gs_lib_ctx_get_cms_context( const gs_memory_t *mem )
483
203M
{
484
203M
    if (mem == NULL)
485
0
        return NULL;
486
203M
    return mem->gs_lib_ctx->core->cms_context;
487
203M
}
488
489
int gs_lib_ctx_get_act_on_uel( const gs_memory_t *mem )
490
166k
{
491
166k
    if (mem == NULL)
492
0
        return 0;
493
166k
    return mem->gs_lib_ctx->core->act_on_uel;
494
166k
}
495
496
/* Provide a single point for all "C" stdout and stderr.
497
 */
498
499
int outwrite(const gs_memory_t *mem, const char *str, int len)
500
1.61M
{
501
1.61M
    int code;
502
1.61M
    gs_lib_ctx_t *pio = mem->gs_lib_ctx;
503
1.61M
    gs_lib_ctx_core_t *core = pio->core;
504
505
1.61M
    if (len == 0)
506
0
        return 0;
507
1.61M
    if (core->stdout_is_redirected) {
508
1.58M
        if (core->stdout_to_stderr)
509
0
            return errwrite(mem, str, len);
510
1.58M
        code = gp_fwrite(str, 1, len, core->fstdout2);
511
1.58M
        gp_fflush(core->fstdout2);
512
1.58M
    } else if (core->stdout_fn) {
513
21.2k
        return (*core->stdout_fn)(core->std_caller_handle, str, len);
514
21.2k
    } else {
515
0
        code = fwrite(str, 1, len, core->fstdout);
516
0
        fflush(core->fstdout);
517
0
    }
518
1.58M
    return code;
519
1.61M
}
520
521
int errwrite(const gs_memory_t *mem, const char *str, int len)
522
559k
{
523
559k
    int code;
524
559k
    gs_lib_ctx_t *ctx;
525
559k
    gs_lib_ctx_core_t *core;
526
559k
    if (len == 0)
527
0
        return 0;
528
559k
    if (mem == NULL) {
529
#ifdef DEBUG
530
        mem = gp_get_debug_mem_ptr();
531
        if (mem == NULL)
532
#endif
533
0
            return 0;
534
0
    }
535
559k
    ctx = mem->gs_lib_ctx;
536
559k
    if (ctx == NULL)
537
0
      return 0;
538
559k
    core = ctx->core;
539
559k
    if (core->stderr_fn)
540
559k
        return (*core->stderr_fn)(core->std_caller_handle, str, len);
541
542
0
    code = fwrite(str, 1, len, core->fstderr);
543
0
    fflush(core->fstderr);
544
0
    return code;
545
559k
}
546
547
void outflush(const gs_memory_t *mem)
548
0
{
549
0
    gs_lib_ctx_core_t *core = mem->gs_lib_ctx->core;
550
0
    if (core->stdout_is_redirected) {
551
0
        if (core->stdout_to_stderr) {
552
0
            if (!core->stderr_fn)
553
0
                fflush(core->fstderr);
554
0
        }
555
0
        else
556
0
            gp_fflush(core->fstdout2);
557
0
    }
558
0
    else if (!core->stdout_fn)
559
0
        fflush(core->fstdout);
560
0
}
561
562
void errflush(const gs_memory_t *mem)
563
0
{
564
0
    if (!mem->gs_lib_ctx->core->stderr_fn)
565
0
        fflush(mem->gs_lib_ctx->core->fstderr);
566
    /* else nothing to flush */
567
0
}
568
569
static void
570
rewrite_percent_specifiers(char *s)
571
200k
{
572
200k
    char *match_start;
573
574
200k
    while (*s)
575
200k
    {
576
200k
        int flags;
577
        /* Find a % */
578
2.84M
        while (*s && *s != '%')
579
2.64M
            s++;
580
200k
        if (*s == 0)
581
200k
            return;
582
242
        match_start = s;
583
242
        s++;
584
        /* Skip over flags (just one instance of any given flag, in any order) */
585
242
        flags = 0;
586
242
        while (*s) {
587
242
            if (*s == '-' && (flags & 1) == 0)
588
0
                flags |= 1;
589
242
            else if (*s == '+' && (flags & 2) == 0)
590
0
                flags |= 2;
591
242
            else if (*s == ' ' && (flags & 4) == 0)
592
0
                flags |= 4;
593
242
            else if (*s == '0' && (flags & 8) == 0)
594
0
                flags |= 8;
595
242
            else if (*s == '#' && (flags & 16) == 0)
596
0
                flags |= 16;
597
242
            else
598
242
                break;
599
0
            s++;
600
0
        }
601
        /* Skip over width */
602
242
        while (*s >= '0' && *s <= '9')
603
0
            s++;
604
        /* Skip over .precision */
605
242
        if (*s == '.' && s[1] >= '0' && s[1] <= '9') {
606
0
            s++;
607
0
            while (*s >= '0' && *s <= '9')
608
0
                s++;
609
0
        }
610
        /* Skip over 'l' */
611
242
        if (*s == 'l')
612
0
            s++;
613
242
        if (*s == 'd' ||
614
242
            *s == 'i' ||
615
242
            *s == 'u' ||
616
242
            *s == 'o' ||
617
242
            *s == 'x' ||
618
242
            *s == 'X') {
619
            /* Success! */
620
0
            memset(match_start, '*', s - match_start + 1);
621
0
        }
622
        /* If we have escaped percents ("%%") so the percent
623
           will survive a call to sprintf and co, then we need
624
           to drop the extra one here, because the validation
625
           code will see the string *after* it's been sprintf'ed.
626
         */
627
242
        else if (*s == '%') {
628
242
            char *s0 = s;
629
58.5k
            while (*s0) {
630
58.3k
                *s0 = *(s0 + 1);
631
58.3k
                s0++;
632
58.3k
            }
633
242
        }
634
242
    }
635
200k
}
636
637
/* For the OutputFile permission we have to deal with formattable strings
638
   i.e. ones that have "%d" or similar in them. For these we want to replace
639
   everything after the %d with a wildcard "*".
640
   Since we do this in two places (the -s and -o cases) split it into a
641
   function.
642
 */
643
#define IS_WHITESPACE(c) ((c == 0x20) || (c == 0x9) || (c == 0xD) || (c == 0xA))
644
int
645
gs_add_outputfile_control_path(gs_memory_t *mem, const char *fname)
646
183k
{
647
183k
    char f[gp_file_name_sizeof];
648
183k
    int code;
649
650
    /* Be sure the string copy will fit */
651
183k
    if (strlen(fname) >= gp_file_name_sizeof)
652
0
        return gs_error_rangecheck;
653
183k
    strcpy(f, fname);
654
    /* Try to rewrite any %d (or similar) in the string */
655
183k
    rewrite_percent_specifiers(f);
656
657
183k
    code = gs_add_control_path(mem, gs_permit_file_control, f);
658
183k
    if (code < 0)
659
0
        return code;
660
    /* Some file formats (eg TIFF) require the device to be able to read and write */
661
183k
    code = gs_add_control_path(mem, gs_permit_file_reading, f);
662
183k
    if (code < 0)
663
0
        return code;
664
183k
    return gs_add_control_path(mem, gs_permit_file_writing, f);
665
183k
}
666
667
int
668
gs_remove_outputfile_control_path(gs_memory_t *mem, const char *fname)
669
16.7k
{
670
16.7k
    char f[gp_file_name_sizeof];
671
16.7k
    int code;
672
673
    /* Be sure the string copy will fit */
674
16.7k
    if (strlen(fname) >= gp_file_name_sizeof)
675
0
        return gs_error_rangecheck;
676
16.7k
    strcpy(f, fname);
677
    /* Try to rewrite any %d (or similar) in the string */
678
16.7k
    rewrite_percent_specifiers(f);
679
680
16.7k
    code = gs_remove_control_path(mem, gs_permit_file_control, f);
681
16.7k
    if (code < 0)
682
0
        return code;
683
16.7k
    return gs_remove_control_path(mem, gs_permit_file_writing, f);
684
16.7k
}
685
686
int
687
gs_add_explicit_control_path(gs_memory_t *mem, const char *arg, gs_path_control_t control)
688
0
{
689
0
    char *p2, *p1 = (char *)arg;
690
0
    const char *lim;
691
0
    int code = 0;
692
693
0
    if (arg == NULL)
694
0
        return 0;
695
0
    lim = arg + strlen(arg);
696
0
    while (code >= 0 && p1 < lim && (p2 = strchr(p1, (int)gp_file_name_list_separator)) != NULL) {
697
0
        code = gs_add_control_path_len(mem, control, p1, (int)(p2 - p1));
698
0
        p1 = p2 + 1;
699
0
    }
700
0
    if (p1 < lim)
701
0
        code = gs_add_control_path_len(mem, control, p1, (int)(lim - p1));
702
0
    return code;
703
0
}
704
705
int
706
gs_add_control_path_len(const gs_memory_t *mem, gs_path_control_t type, const char *path, size_t len)
707
21.9M
{
708
21.9M
    return gs_add_control_path_len_flags(mem, type, path, len, 0);
709
21.9M
}
710
711
int
712
gs_add_control_path_len_flags(const gs_memory_t *mem, gs_path_control_t type, const char *path, size_t len, int flags)
713
23.3M
{
714
23.3M
    gs_path_control_set_t *control;
715
23.3M
    unsigned int n, i;
716
23.3M
    gs_lib_ctx_core_t *core;
717
23.3M
    char *buffer;
718
23.3M
    uint rlen;
719
720
23.3M
    if (path == NULL || len == 0)
721
0
        return 0;
722
723
23.3M
    if (mem == NULL || mem->gs_lib_ctx == NULL ||
724
23.3M
        (core = mem->gs_lib_ctx->core) == NULL)
725
0
        return gs_error_unknownerror;
726
727
23.3M
    switch(type) {
728
22.3M
        case gs_permit_file_reading:
729
22.3M
            control = &core->permit_reading;
730
22.3M
            break;
731
566k
        case gs_permit_file_writing:
732
566k
            control = &core->permit_writing;
733
566k
            break;
734
381k
        case gs_permit_file_control:
735
381k
            control = &core->permit_control;
736
381k
            break;
737
0
        default:
738
0
            return gs_error_rangecheck;
739
23.3M
    }
740
741
    /* "%pipe%" do not follow the normal rules for path definitions, so we
742
       don't "reduce" them to avoid unexpected results
743
     */
744
23.3M
    if (path[0] == '|' || (len > 5 && memcmp(path, "%pipe", 5) == 0)) {
745
0
        buffer = (char *)gs_alloc_bytes(core->memory, len + 1, "gs_add_control_path_len");
746
0
        if (buffer == NULL)
747
0
            return gs_error_VMerror;
748
0
        memcpy(buffer, path, len);
749
0
        buffer[len] = 0;
750
0
        rlen = len;
751
0
    }
752
23.3M
    else {
753
23.3M
        rlen = len + 1;
754
755
23.3M
        buffer = (char *)gs_alloc_bytes(core->memory, rlen, "gs_add_control_path_len");
756
23.3M
        if (buffer == NULL)
757
0
            return gs_error_VMerror;
758
759
23.3M
        if (gp_file_name_reduce(path, (uint)len, buffer, &rlen) != gp_combine_success)
760
0
            return gs_error_invalidfileaccess;
761
23.3M
        buffer[rlen] = 0;
762
23.3M
    }
763
764
23.3M
    n = control->num;
765
206M
    for (i = 0; i < n; i++)
766
200M
    {
767
200M
        if (strncmp(control->entry[i].path, buffer, rlen) == 0 &&
768
17.3M
            control->entry[i].path[rlen] == 0) {
769
17.3M
            gs_free_object(core->memory, buffer, "gs_add_control_path_len");
770
17.3M
            return 0; /* Already there! */
771
17.3M
        }
772
200M
    }
773
774
5.99M
    if (control->num == control->max) {
775
1.08M
        gs_path_control_entry_t *p;
776
777
1.08M
        n = control->max * 2;
778
1.08M
        if (n == 0) {
779
499k
            n = 4;
780
499k
            p = (gs_path_control_entry_t *)gs_alloc_bytes(core->memory, sizeof(*p)*n, "gs_lib_ctx(entries)");
781
499k
        } else
782
588k
            p = (gs_path_control_entry_t *)gs_resize_object(core->memory, control->entry, sizeof(*p)*n, "gs_lib_ctx(entries)");
783
1.08M
        if (p == NULL) {
784
0
            gs_free_object(core->memory, buffer, "gs_add_control_path_len");
785
0
            return gs_error_VMerror;
786
0
        }
787
1.08M
        control->entry = p;
788
1.08M
        control->max = n;
789
1.08M
    }
790
791
5.99M
    n = control->num;
792
5.99M
    control->entry[n].path = buffer;
793
5.99M
    control->entry[n].path[len] = 0;
794
5.99M
    control->entry[n].flags = flags;
795
5.99M
    control->num++;
796
797
5.99M
    return 0;
798
5.99M
}
799
800
int
801
gs_add_control_path(const gs_memory_t *mem, gs_path_control_t type, const char *path)
802
754k
{
803
754k
    return gs_add_control_path_len_flags(mem, type, path, path ? strlen(path) : 0, 0);
804
754k
}
805
806
int
807
gs_add_control_path_flags(const gs_memory_t *mem, gs_path_control_t type, const char *path, int flags)
808
594k
{
809
594k
    return gs_add_control_path_len_flags(mem, type, path, path ? strlen(path) : 0, flags);
810
594k
}
811
812
int
813
gs_remove_control_path_len(const gs_memory_t *mem, gs_path_control_t type, const char *path, size_t len)
814
0
{
815
0
    return gs_remove_control_path_len_flags(mem, type, path, len, 0);
816
0
}
817
818
int
819
gs_remove_control_path_len_flags(const gs_memory_t *mem, gs_path_control_t type, const char *path, size_t len, int flags)
820
646k
{
821
646k
    gs_path_control_set_t *control;
822
646k
    unsigned int n, i;
823
646k
    gs_lib_ctx_core_t *core;
824
646k
    char *buffer;
825
646k
    uint rlen;
826
827
646k
    if (path == NULL || len == 0)
828
0
        return 0;
829
830
646k
    if (mem == NULL || mem->gs_lib_ctx == NULL ||
831
646k
        (core = mem->gs_lib_ctx->core) == NULL)
832
0
        return gs_error_unknownerror;
833
834
646k
    switch(type) {
835
217k
        case gs_permit_file_reading:
836
217k
            control = &core->permit_reading;
837
217k
            break;
838
214k
        case gs_permit_file_writing:
839
214k
            control = &core->permit_writing;
840
214k
            break;
841
214k
        case gs_permit_file_control:
842
214k
            control = &core->permit_control;
843
214k
            break;
844
0
        default:
845
0
            return gs_error_rangecheck;
846
646k
    }
847
848
    /* "%pipe%" do not follow the normal rules for path definitions, so we
849
       don't "reduce" them to avoid unexpected results
850
     */
851
646k
    if (path[0] == '|' || (len > 5 && memcmp(path, "%pipe", 5) == 0)) {
852
0
        buffer = (char *)gs_alloc_bytes(core->memory, len + 1, "gs_remove_control_path_len");
853
0
        if (buffer == NULL)
854
0
            return gs_error_VMerror;
855
0
        memcpy(buffer, path, len);
856
0
        buffer[len] = 0;
857
0
        rlen = len;
858
0
    }
859
646k
    else {
860
646k
        rlen = len+1;
861
862
646k
        buffer = (char *)gs_alloc_bytes(core->memory, rlen, "gs_remove_control_path_len");
863
646k
        if (buffer == NULL)
864
0
            return gs_error_VMerror;
865
866
646k
        if (gp_file_name_reduce(path, (uint)len, buffer, &rlen) != gp_combine_success)
867
0
            return gs_error_invalidfileaccess;
868
646k
        buffer[rlen] = 0;
869
646k
    }
870
871
646k
    n = control->num;
872
6.84M
    for (i = 0; i < n; i++) {
873
6.84M
        if (control->entry[i].flags == flags &&
874
1.88M
            strncmp(control->entry[i].path, buffer, len) == 0 &&
875
645k
            control->entry[i].path[len] == 0)
876
645k
            break;
877
6.84M
    }
878
646k
    gs_free_object(core->memory, buffer, "gs_remove_control_path_len");
879
646k
    if (i == n)
880
891
        return 0;
881
882
645k
    gs_free_object(core->memory, control->entry[i].path, "gs_lib_ctx(path)");
883
2.13M
    for (;i < n-1; i++)
884
1.48M
        control->entry[i] = control->entry[i+1];
885
645k
    control->num = n-1;
886
887
645k
    return 0;
888
646k
}
889
890
int
891
gs_remove_control_path(const gs_memory_t *mem, gs_path_control_t type, const char *path)
892
52.9k
{
893
52.9k
    if (path == NULL)
894
0
        return 0;
895
896
52.9k
    return gs_remove_control_path_len_flags(mem, type, path, strlen(path), 0);
897
52.9k
}
898
899
int
900
gs_remove_control_path_flags(const gs_memory_t *mem, gs_path_control_t type, const char *path, int flags)
901
593k
{
902
593k
    if (path == NULL)
903
0
        return 0;
904
905
593k
    return gs_remove_control_path_len_flags(mem, type, path, strlen(path), flags);
906
593k
}
907
908
void
909
gs_purge_control_paths(const gs_memory_t *mem, gs_path_control_t type)
910
499k
{
911
499k
    gs_path_control_set_t *control;
912
499k
    unsigned int n, in, out;
913
499k
    gs_lib_ctx_core_t *core;
914
915
499k
    if (mem == NULL || mem->gs_lib_ctx == NULL ||
916
499k
        (core = mem->gs_lib_ctx->core) == NULL)
917
0
        return;
918
919
499k
    switch(type) {
920
166k
        case gs_permit_file_reading:
921
166k
            control = &core->permit_reading;
922
166k
            break;
923
166k
        case gs_permit_file_writing:
924
166k
            control = &core->permit_writing;
925
166k
            break;
926
166k
        case gs_permit_file_control:
927
166k
            control = &core->permit_control;
928
166k
            break;
929
0
        default:
930
0
            return;
931
499k
    }
932
933
499k
    n = control->num;
934
5.85M
    for (in = out = 0; in < n; in++) {
935
5.35M
        if (control->entry[in].flags & gs_path_control_flag_is_scratch_file) {
936
            /* Don't purge scratch files! */
937
0
            control->entry[out++] = control->entry[in];
938
0
        } else
939
5.35M
            gs_free_object(core->memory, control->entry[in].path, "gs_lib_ctx(path)");
940
5.35M
    }
941
499k
    control->num = out;
942
499k
    if (out == 0) {
943
499k
        gs_free_object(core->memory, control->entry, "gs_lib_ctx(paths)");
944
499k
        control->entry = NULL;
945
499k
        control->max = 0;
946
499k
    }
947
499k
}
948
949
void
950
gs_purge_scratch_files(const gs_memory_t *mem)
951
166k
{
952
166k
    gs_path_control_set_t *control;
953
166k
    gs_path_control_t type;
954
166k
    int n, in, out;
955
166k
    gs_lib_ctx_core_t *core;
956
957
166k
    if (mem == NULL || mem->gs_lib_ctx == NULL ||
958
166k
        (core = mem->gs_lib_ctx->core) == NULL)
959
0
        return;
960
961
666k
    for (type = gs_permit_file_reading; type <= gs_permit_file_control; type++)
962
499k
    {
963
499k
        switch(type) {
964
0
            default:
965
166k
            case gs_permit_file_reading:
966
166k
                control = &core->permit_reading;
967
166k
                break;
968
166k
            case gs_permit_file_writing:
969
166k
                control = &core->permit_writing;
970
166k
                break;
971
166k
            case gs_permit_file_control:
972
166k
                control = &core->permit_control;
973
166k
                break;
974
499k
        }
975
976
499k
        n = control->num;
977
5.85M
        for (in = out = 0; in < n; in++) {
978
5.35M
            if ((control->entry[in].flags & gs_path_control_flag_is_scratch_file) == 0) {
979
                /* Only purge scratch files! */
980
5.35M
                control->entry[out++] = control->entry[in];
981
5.35M
            } else {
982
1.05k
                if (type == gs_permit_file_reading) {
983
                    /* Call gp_unlink_impl, as we don't want gp_unlink
984
                     * to go looking in the lists we are currently
985
                     * manipulating! */
986
353
                    gp_unlink_impl(core->memory, control->entry[in].path);
987
353
                }
988
1.05k
                gs_free_object(core->memory, control->entry[in].path, "gs_lib_ctx(path)");
989
1.05k
            }
990
5.35M
        }
991
499k
        control->num = out;
992
499k
        if (out == 0) {
993
0
            gs_free_object(core->memory, control->entry, "gs_lib_ctx(paths)");
994
0
            control->entry = NULL;
995
0
            control->max = 0;
996
0
        }
997
499k
    }
998
166k
}
999
1000
void
1001
gs_activate_path_control(gs_memory_t *mem, int enable)
1002
166k
{
1003
166k
    gs_lib_ctx_core_t *core;
1004
1005
166k
    if (mem == NULL || mem->gs_lib_ctx == NULL ||
1006
166k
        (core = mem->gs_lib_ctx->core) == NULL)
1007
0
        return;
1008
1009
166k
    core->path_control_active = enable;
1010
166k
}
1011
1012
int
1013
gs_is_path_control_active(const gs_memory_t *mem)
1014
4.68M
{
1015
4.68M
    gs_lib_ctx_core_t *core;
1016
1017
4.68M
    if (mem == NULL || mem->gs_lib_ctx == NULL ||
1018
4.68M
        (core = mem->gs_lib_ctx->core) == NULL)
1019
0
        return 0;
1020
1021
4.68M
    return core->path_control_active;
1022
4.68M
}
1023
1024
int
1025
gs_add_fs(const gs_memory_t *mem,
1026
                gs_fs_t     *fs,
1027
                void        *secret)
1028
0
{
1029
0
    gs_fs_list_t *fsl;
1030
0
    gs_lib_ctx_core_t *core;
1031
1032
0
    if (mem == NULL || mem->gs_lib_ctx == NULL ||
1033
0
        (core = mem->gs_lib_ctx->core) == NULL)
1034
0
        return gs_error_unknownerror;
1035
1036
0
    fsl = (gs_fs_list_t *)gs_alloc_bytes_immovable(mem->non_gc_memory,
1037
0
                                                   sizeof(gs_fs_list_t),
1038
0
                                                   "gs_fs_list_t");
1039
0
    if (fsl == NULL)
1040
0
        return gs_error_VMerror;
1041
0
    fsl->fs      = *fs;
1042
0
    fsl->secret  = secret;
1043
0
    fsl->memory  = mem->non_gc_memory;
1044
0
    fsl->next    = core->fs;
1045
0
    core->fs     = fsl;
1046
1047
0
    return 0;
1048
0
}
1049
1050
void
1051
gs_remove_fs(const gs_memory_t *mem,
1052
                   gs_fs_t     *rfs,
1053
                   void        *secret)
1054
0
{
1055
0
    gs_fs_list_t **pfs;
1056
0
    gs_lib_ctx_core_t *core;
1057
1058
0
    if (mem == NULL || mem->gs_lib_ctx == NULL ||
1059
0
        (core = mem->gs_lib_ctx->core) == NULL)
1060
0
        return;
1061
1062
0
    pfs = &core->fs;
1063
0
    while (*pfs)
1064
0
    {
1065
0
        gs_fs_list_t *fs = *pfs;
1066
0
        if (fs->fs.open_file == rfs->open_file &&
1067
0
            fs->fs.open_pipe == rfs->open_pipe &&
1068
0
            fs->fs.open_scratch == rfs->open_scratch &&
1069
0
            fs->fs.open_printer == rfs->open_printer &&
1070
0
            fs->secret == secret) {
1071
0
            *pfs = fs->next;
1072
0
            gs_free_object(fs->memory, fs, "gs_fs_t");
1073
0
        } else
1074
0
            pfs = &(*pfs)->next;
1075
0
    }
1076
0
}
1077
1078
static int gs_add_device_control(gs_memory_t *mem, char *device, int len)
1079
0
{
1080
0
    gs_lib_ctx_core_t *core;
1081
0
    char *buffer;
1082
0
    uint rlen, n, i;
1083
1084
0
    if (mem == NULL || mem->gs_lib_ctx == NULL ||
1085
0
        (core = mem->gs_lib_ctx->core) == NULL)
1086
0
        return gs_error_unknownerror;
1087
1088
0
    rlen = len + 1;
1089
1090
0
    buffer = (char *)gs_alloc_bytes(core->memory, rlen, "gs_add_device");
1091
0
    if (buffer == NULL)
1092
0
        return gs_error_VMerror;
1093
1094
0
    memcpy(buffer, device, len);
1095
0
    buffer[len] = 0;
1096
1097
0
    n = core->permitted_devices.num;
1098
0
    for (i = 0; i < n; i++)
1099
0
    {
1100
0
        if (strlen(core->permitted_devices.devices[i]) == len && strncmp(core->permitted_devices.devices[i], buffer, len) == 0) {
1101
0
            gs_free_object(core->memory, buffer, "gs_add_device");
1102
0
            return 0; /* Already there! */
1103
0
        }
1104
0
    }
1105
1106
0
    if (core->permitted_devices.num == core->permitted_devices.max) {
1107
0
        char **table;
1108
1109
0
        n = core->permitted_devices.max * 2;
1110
0
        if (n == 0) {
1111
0
            n = 2;
1112
0
            table = (char **)gs_alloc_bytes(core->memory, sizeof(char *) * n, "gs_add_device_control");
1113
0
            if (table == NULL) {
1114
0
                gs_free_object(core->memory, buffer, "gs_add_device");
1115
0
                return_error(gs_error_VMerror);
1116
0
            }
1117
0
            memset(table, 0x00, sizeof(char *) * n);
1118
0
        } else {
1119
0
            table = (char **)gs_resize_object(core->memory, core->permitted_devices.devices,sizeof(char *) * n, "gs_add_device_control");
1120
0
            if (table == NULL) {
1121
0
                gs_free_object(core->memory, buffer, "gs_add_device");
1122
0
                return_error(gs_error_VMerror);
1123
0
            }
1124
0
            memset(table + core->permitted_devices.max, 0x00, core->permitted_devices.max * sizeof(char *));
1125
0
        }
1126
0
        core->permitted_devices.devices = table;
1127
0
        core->permitted_devices.max = n;
1128
0
    }
1129
0
    n = core->permitted_devices.num;
1130
0
    core->permitted_devices.devices[n] = buffer;
1131
0
    core->permitted_devices.num++;
1132
1133
0
    return 0;
1134
0
}
1135
1136
int
1137
gs_check_device_permission (gs_memory_t *mem, const char *dname, const int len)
1138
0
{
1139
0
    if (mem->gs_lib_ctx->core->permitted_devices.num != 0) {
1140
0
        int i;
1141
0
        char *permit_name = NULL;
1142
1143
0
        for (i=0;i < mem->gs_lib_ctx->core->permitted_devices.num; i++) {
1144
0
            permit_name = mem->gs_lib_ctx->core->permitted_devices.devices[i];
1145
1146
            /* Allow * as a wildcard */
1147
0
            if (permit_name != NULL && strlen(permit_name) == 1 && permit_name[0] == '*')
1148
0
                break;
1149
1150
0
            if (permit_name != NULL && strlen(permit_name) == len &&
1151
0
                strncmp(permit_name, dname, len) == 0)
1152
0
                break;
1153
0
        }
1154
0
        if (i >= mem->gs_lib_ctx->core->permitted_devices.num)
1155
0
            return 0;
1156
0
        return 1;
1157
0
    }
1158
0
    return 0;
1159
0
}
1160
1161
int
1162
gs_add_explicit_permitted_device(gs_memory_t *mem, const char *arg)
1163
0
{
1164
0
    char *p2, *p1 = (char *)arg;
1165
0
    const char *lim;
1166
0
    int code = 0;
1167
1168
0
    if (arg == NULL)
1169
0
        return 0;
1170
0
    lim = arg + strlen(arg);
1171
0
    while (code >= 0 && p1 < lim && (p2 = strchr(p1, gp_file_name_list_separator)) != NULL) {
1172
0
        code = gs_add_device_control(mem, p1, (int)(p2 - p1));
1173
0
        p1 = p2 + 1;
1174
0
    }
1175
0
    if (p1 < lim)
1176
0
        code = gs_add_device_control(mem, p1, (int)(lim - p1));
1177
0
    return code;
1178
0
}
1179
1180
static void
1181
gs_purge_permitted_devices(const gs_memory_t *mem)
1182
166k
{
1183
166k
    gs_lib_ctx_core_t *core = mem->gs_lib_ctx->core;
1184
166k
    uint i;
1185
1186
166k
    if (core == NULL)
1187
0
        return;
1188
1189
166k
    for (i=0;i < core->permitted_devices.max;i++) {
1190
0
        if (core->permitted_devices.devices[i] != NULL)
1191
0
            gs_free_object(core->memory, core->permitted_devices.devices[i], "gs_purge_permitted_devices");
1192
0
    }
1193
166k
    if (core->permitted_devices.devices != NULL)
1194
0
        gs_free_object(core->memory, core->permitted_devices.devices, "gs_purge_permitted_devices");
1195
1196
166k
    core->permitted_devices.max = core->permitted_devices.num = 0;
1197
166k
    core->permitted_devices.devices = NULL;
1198
166k
}
1199
1200
int
1201
gs_lib_ctx_stash_sanitized_arg(gs_lib_ctx_t *ctx, const char *arg)
1202
2.66M
{
1203
2.66M
    gs_lib_ctx_core_t *core;
1204
2.66M
    size_t len;
1205
2.66M
    const char *p;
1206
2.66M
    int elide = 0;
1207
1208
2.66M
    if (ctx == NULL || ctx->core == NULL || arg == NULL)
1209
0
        return 0;
1210
1211
    /* Sanitize arg */
1212
2.66M
    switch(*arg)
1213
2.66M
    {
1214
2.66M
    case '-':
1215
2.66M
        switch (arg[1])
1216
2.66M
        {
1217
0
        case 0:   /* We can let - through unchanged */
1218
0
        case '-': /* Need to check for permitted file lists */
1219
            /* By default, we want to keep the key, but lose the value */
1220
0
            p = arg+2;
1221
0
            while (*p && *p != '=' && *p != '#')
1222
0
                p++;
1223
0
            if (*p == '=' || *p == '#')
1224
0
                p++;
1225
0
            if (*p == 0)
1226
0
                break; /* No value to elide */
1227
            /* Check for our blocked values here */
1228
0
#define ARG_MATCHES(STR, ARG, LEN) \
1229
0
    (strlen(STR) == LEN && !memcmp(STR, ARG, LEN))
1230
0
            if (ARG_MATCHES("permit-file-read", arg+2, p-arg-3))
1231
0
                elide=1;
1232
0
            if (ARG_MATCHES("permit-file-write", arg+2, p-arg-3))
1233
0
                elide=1;
1234
0
            if (ARG_MATCHES("permit-file-control", arg+2, p-arg-3))
1235
0
                elide=1;
1236
0
            if (ARG_MATCHES("permit-file-all", arg+2, p-arg-3))
1237
0
                elide=1;
1238
0
#undef ARG_MATCHES
1239
            /* Didn't match a blocked value, so allow it. */
1240
0
            break;
1241
1.55M
        case 'd': /* We can let -dFoo=<whatever> through unchanged */
1242
1.55M
        case 'D': /* We can let -DFoo=<whatever> through unchanged */
1243
1.72M
        case 'r': /* We can let -r through unchanged */
1244
1.72M
        case 'Z': /* We can let -Z through unchanged */
1245
1.72M
        case 'g': /* We can let -g through unchanged */
1246
1.72M
        case 'P': /* We can let -P through unchanged */
1247
1.72M
        case '+': /* We can let -+ through unchanged */
1248
1.87M
        case '_': /* We can let -_ through unchanged */
1249
1.87M
        case 'u': /* We can let -u through unchanged */
1250
1.87M
        case 'q': /* We can let -q through unchanged */
1251
1.87M
            break;
1252
0
        case 'I': /* Let through the I, but hide anything else */
1253
0
        case 'f': /* Let through the I, but hide anything else */
1254
0
            if (arg[2] == 0)
1255
0
                break;
1256
0
            p = arg+2;
1257
0
            while (*p == 32)
1258
0
                p++;
1259
0
            elide = 1;
1260
0
            break;
1261
645k
        case 's':
1262
645k
        case 'S':
1263
            /* By default, we want to keep the key, but lose the value */
1264
645k
            p = arg+2;
1265
6.52M
            while (*p && *p != '=' && *p != '#')
1266
5.87M
                p++;
1267
645k
            if (*p == '=' || *p == '#')
1268
645k
                p++;
1269
645k
            if (*p == 0)
1270
0
                break; /* No value to elide */
1271
            /* Check for our whitelisted values here */
1272
645k
#define ARG_MATCHES(STR, ARG, LEN) \
1273
4.05M
    (strlen(STR) == LEN && !memcmp(STR, ARG, LEN))
1274
645k
            if (ARG_MATCHES("DEFAULTPAPERSIZE", arg+2, p-arg-3))
1275
0
                break;
1276
645k
            if (ARG_MATCHES("DEVICE", arg+2, p-arg-3))
1277
185k
                break;
1278
460k
            if (ARG_MATCHES("PAPERSIZE", arg+2, p-arg-3))
1279
0
                break;
1280
460k
            if (ARG_MATCHES("SUBSTFONT", arg+2, p-arg-3))
1281
0
                break;
1282
460k
            if (ARG_MATCHES("ColorConversionStrategy", arg+2, p-arg-3))
1283
0
                break;
1284
460k
            if (ARG_MATCHES("NupControl", arg+2, p-arg-3))
1285
0
                break;
1286
460k
            if (ARG_MATCHES("PageList", arg+2, p-arg-3))
1287
0
                break;
1288
460k
            if (ARG_MATCHES("ProcessColorModel", arg+2, p-arg-3))
1289
0
                break;
1290
460k
#undef ARG_MATCHES
1291
            /* Didn't match a whitelisted value, so elide it. */
1292
460k
            elide = 1;
1293
460k
            break;
1294
148k
        default:
1295
            /* Shouldn't happen, but elide it just in case */
1296
148k
            arg = "?";
1297
148k
            break;
1298
2.66M
        }
1299
2.66M
        break;
1300
2.66M
    case '@':
1301
        /* Shouldn't happen */
1302
0
    default:
1303
        /* Anything else should be elided */
1304
0
        arg = "?";
1305
0
        break;
1306
2.66M
    }
1307
1308
2.66M
    core = ctx->core;
1309
2.66M
    if (elide)
1310
460k
        len = p-arg;
1311
2.20M
    else
1312
2.20M
        len = strlen(arg);
1313
1314
2.66M
    if (core->arg_max == core->argc) {
1315
478k
        char **argv;
1316
478k
        int newlen = core->arg_max * 2;
1317
478k
        if (newlen == 0)
1318
0
            newlen = 4;
1319
478k
        argv = (char **)gs_alloc_bytes(ctx->core->memory, sizeof(char *) * newlen,
1320
478k
                                       "gs_lib_ctx_args");
1321
478k
        if (argv == NULL)
1322
0
            return gs_error_VMerror;
1323
478k
        if (core->argc > 0) {
1324
478k
            memcpy(argv, core->argv, sizeof(char *) * core->argc);
1325
478k
            gs_free_object(ctx->memory, core->argv, "gs_lib_ctx_args");
1326
478k
        }
1327
478k
        core->argv = argv;
1328
478k
        core->arg_max = newlen;
1329
478k
    }
1330
1331
2.66M
    core->argv[core->argc] = (char *)gs_alloc_bytes(ctx->core->memory, len+1+elide,
1332
2.66M
                                                    "gs_lib_ctx_arg");
1333
2.66M
    if (core->argv[core->argc] == NULL)
1334
0
        return gs_error_VMerror;
1335
2.66M
    memcpy(core->argv[core->argc], arg, len);
1336
2.66M
    if (elide) {
1337
460k
        core->argv[core->argc][len] = '?';
1338
460k
    }
1339
2.66M
    core->argv[core->argc][len+elide] = 0;
1340
2.66M
    core->argc++;
1341
1342
2.66M
    return 0;
1343
2.66M
}
1344
1345
int
1346
gs_lib_ctx_stash_exe(gs_lib_ctx_t *ctx, const char *arg)
1347
185k
{
1348
185k
    gs_lib_ctx_core_t *core;
1349
185k
    size_t len;
1350
185k
    const char *p, *word;
1351
185k
    const char *sep = gp_file_name_directory_separator();
1352
185k
    size_t seplen = strlen(sep);
1353
1354
185k
    if (ctx == NULL || ctx->core == NULL || arg == NULL)
1355
0
        return 0;
1356
1357
    /* Sanitize arg */
1358
185k
    p = arg;
1359
185k
    word = NULL;
1360
629k
    for (p = arg; *p; p++) {
1361
443k
        if (memcmp(sep, p, seplen) == 0) {
1362
0
            word = p+seplen;
1363
0
            p += seplen-1;
1364
0
        }
1365
#if defined(__WIN32__) || defined(__OS2__) || defined(METRO)
1366
        if (*p == '\\')
1367
            word = p+1;
1368
#endif
1369
443k
    }
1370
185k
    len = p - (word ? word : arg) + 1;
1371
185k
    if (word)
1372
0
        len += 5;
1373
1374
185k
    core = ctx->core;
1375
185k
    if (core->arg_max == core->argc) {
1376
166k
        char **argv;
1377
166k
        int newlen = core->arg_max * 2;
1378
166k
        if (newlen == 0)
1379
166k
            newlen = 4;
1380
166k
        argv = (char **)gs_alloc_bytes(ctx->core->memory, sizeof(char *) * newlen,
1381
166k
                                       "gs_lib_ctx_args");
1382
166k
        if (argv == NULL)
1383
0
            return gs_error_VMerror;
1384
166k
        if (core->argc > 0) {
1385
0
            memcpy(argv, core->argv, sizeof(char *) * core->argc);
1386
0
            gs_free_object(ctx->memory, core->argv, "gs_lib_ctx_args");
1387
0
        }
1388
166k
        core->argv = argv;
1389
166k
        core->arg_max = newlen;
1390
166k
    }
1391
1392
185k
    core->argv[core->argc] = (char *)gs_alloc_bytes(ctx->core->memory, len,
1393
185k
                                                    "gs_lib_ctx_arg");
1394
185k
    if (core->argv[core->argc] == NULL)
1395
0
        return gs_error_VMerror;
1396
185k
    if (word)
1397
0
        strcpy(core->argv[core->argc], "path/");
1398
185k
    else
1399
185k
        core->argv[core->argc][0] = 0;
1400
185k
    strcat(core->argv[core->argc], word ? word : arg);
1401
185k
    core->argc++;
1402
1403
185k
    return 0;
1404
185k
}
1405
1406
int gs_lib_ctx_get_args(gs_lib_ctx_t *ctx, const char * const **argv)
1407
31.3k
{
1408
31.3k
    gs_lib_ctx_core_t *core;
1409
1410
31.3k
    if (ctx == NULL || ctx->core == NULL || argv == NULL)
1411
0
        return 0;
1412
1413
31.3k
    core = ctx->core;
1414
31.3k
    *argv = (const char * const *)core->argv;
1415
31.3k
    return core->argc;
1416
31.3k
}
1417
1418
int gs_lib_ctx_register_callout(gs_memory_t *mem, gs_callout_fn fn, void *arg)
1419
0
{
1420
0
    gs_lib_ctx_core_t *core;
1421
0
    gs_callout_list_t *entry;
1422
1423
0
    if (mem == NULL || mem->gs_lib_ctx == NULL ||
1424
0
        mem->gs_lib_ctx->core == NULL || fn == NULL)
1425
0
        return 0;
1426
1427
0
   core = mem->gs_lib_ctx->core;
1428
0
   entry = (gs_callout_list_t *)gs_alloc_bytes(core->memory,
1429
0
                                               sizeof(*entry),
1430
0
                                               "gs_callout_list_t");
1431
0
   if (entry == NULL)
1432
0
       return_error(gs_error_VMerror);
1433
0
   entry->next = core->callouts;
1434
0
   entry->callout = fn;
1435
0
   entry->handle = arg;
1436
0
   core->callouts = entry;
1437
1438
0
   return 0;
1439
0
}
1440
1441
void gs_lib_ctx_deregister_callout(gs_memory_t *mem, gs_callout_fn fn, void *arg)
1442
0
{
1443
0
    gs_lib_ctx_core_t *core;
1444
0
    gs_callout_list_t **entry;
1445
1446
0
    if (mem == NULL || mem->gs_lib_ctx == NULL ||
1447
0
        mem->gs_lib_ctx->core == NULL || fn == NULL)
1448
0
        return;
1449
1450
0
   core = mem->gs_lib_ctx->core;
1451
0
   entry = &core->callouts;
1452
0
   while (*entry) {
1453
0
        if ((*entry)->callout == fn && (*entry)->handle == arg) {
1454
0
            gs_callout_list_t *next = (*entry)->next;
1455
0
            gs_free_object(core->memory, *entry, "gs_callout_list_t");
1456
0
            *entry = next;
1457
0
        } else {
1458
0
            entry = &(*entry)->next;
1459
0
        }
1460
0
   }
1461
0
}
1462
1463
int gs_lib_ctx_callout(gs_memory_t *mem, const char *dev_name,
1464
                       int id, int size, void *data)
1465
0
{
1466
0
    gs_lib_ctx_core_t *core;
1467
0
    gs_callout_list_t *entry;
1468
1469
0
    if (mem == NULL || mem->gs_lib_ctx == NULL || mem->gs_lib_ctx->core == NULL)
1470
0
        return -1;
1471
1472
0
    core = mem->gs_lib_ctx->core;
1473
0
    entry = core->callouts;
1474
0
    while (entry) {
1475
0
        int code = entry->callout(mem->gs_lib_ctx->top_of_system,
1476
0
                                  entry->handle, dev_name, id, size, data);
1477
0
        if (code >= 0)
1478
0
            return code;
1479
0
        if (code != gs_error_unknownerror)
1480
0
            return code;
1481
0
        entry = entry->next;
1482
0
    }
1483
0
    return -1;
1484
0
}
1485
1486
int gs_lib_ctx_nts_adjust(gs_memory_t *mem, int adjust)
1487
0
{
1488
0
    gs_lib_ctx_core_t *core;
1489
0
    int ret = 0;
1490
0
    gs_globals *globals;
1491
1492
0
    if (adjust == 0)
1493
0
        return 0;
1494
1495
0
    if (mem == NULL || mem->gs_lib_ctx == NULL || mem->gs_lib_ctx->core == NULL)
1496
0
        return_error(gs_error_unknownerror);
1497
1498
0
    core = mem->gs_lib_ctx->core;
1499
0
    globals = core->globals;
1500
0
    if (globals == NULL)
1501
0
        return 0; /* No globals means just once instance. Adjustment is pointless. */
1502
1503
0
    gp_global_lock(globals);
1504
0
    if (adjust > 0 && globals->non_threadsafe_count != 0)
1505
0
        ret = gs_error_unknownerror; /* We already have one non threadsafe device running. */
1506
0
    else if (adjust < 0 && globals->non_threadsafe_count == 0)
1507
0
        ret = gs_error_unknownerror; /* This indicates something has gone very wrong! */
1508
0
    else
1509
0
        globals->non_threadsafe_count += adjust;
1510
0
    gp_global_unlock(globals);
1511
1512
0
    if (ret)
1513
0
        ret = gs_note_error(ret);
1514
0
    return ret;
1515
0
}
1516
1517
void gs_globals_init(gs_globals *globals)
1518
22
{
1519
22
    memset(globals, 0, sizeof(*globals));
1520
22
}