Coverage Report

Created: 2026-11-12 07:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ghostpdl/psi/zfile.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
/* Non-I/O file operators */
18
#include "memory_.h"
19
#include "string_.h"
20
#include "unistd_.h"
21
#include "stat_.h" /* get system header early to avoid name clash on Cygwin */
22
#include "ghost.h"
23
#include "gscdefs.h"            /* for gx_io_device_table */
24
#include "gsutil.h"             /* for bytes_compare */
25
#include "gp.h"
26
#include "gpmisc.h"
27
#include "gsfname.h"
28
#include "gsstruct.h"           /* for registering root */
29
#include "gxalloc.h"            /* for streams */
30
#include "oper.h"
31
#include "dstack.h"             /* for systemdict */
32
#include "estack.h"             /* for filenameforall, .execfile */
33
#include "ialloc.h"
34
#include "ilevel.h"             /* %names only work in Level 2 */
35
#include "iname.h"
36
#include "isave.h"              /* for restore */
37
#include "idict.h"
38
#include "iddict.h"
39
#include "iutil.h"
40
#include "stream.h"
41
#include "strimpl.h"
42
#include "sfilter.h"
43
#include "gxiodev.h"            /* must come after stream.h */
44
                                /* and before files.h */
45
#include "files.h"
46
#include "main.h"               /* for gs_lib_paths */
47
#include "store.h"
48
#include "zfile.h"
49
50
/* Import the IODevice table. */
51
extern_gx_io_device_table();
52
53
/* Import the dtype of the stdio IODevices. */
54
extern const char iodev_dtype_stdio[];
55
56
/* Forward references: file name parsing. */
57
static int parse_file_name(const ref * op, gs_parsed_file_name_t * pfn,
58
                           bool safemode, gs_memory_t *memory);
59
static int parse_real_file_name(const ref * op,
60
                                 gs_parsed_file_name_t * pfn,
61
                                 gs_memory_t *mem, client_name_t cname);
62
static int parse_file_access_string(const ref *op, char file_access[4]);
63
64
/* Forward references: other. */
65
static int execfile_finish(i_ctx_t *);
66
static int execfile_cleanup(i_ctx_t *);
67
static iodev_proc_open_file(iodev_os_open_file);
68
stream_proc_report_error(filter_report_error);
69
70
/*
71
 * Since there can be many file objects referring to the same file/stream,
72
 * we can't simply free a stream when we close it.  On the other hand,
73
 * we don't want freed streams to clutter up memory needlessly.
74
 * Our solution is to retain the freed streams, and reuse them.
75
 * To prevent an old file object from being able to access a reused stream,
76
 * we keep a serial number in each stream, and check it against a serial
77
 * number stored in the file object (as the "size"); when we close a file,
78
 * we increment its serial number.  If the serial number ever overflows,
79
 * we leave it at zero, and do not reuse the stream.
80
 * (This will never happen.)
81
 *
82
 * Storage management for this scheme is a little tricky.  We maintain an
83
 * invariant that says that a stream opened at a given save level always
84
 * uses a stream structure allocated at that level.  By doing this, we don't
85
 * need to keep track separately of streams open at a level vs. streams
86
 * allocated at a level.  To make this interact properly with save and
87
 * restore, we maintain a list of all streams allocated at this level, both
88
 * open and closed.  We store this list in the allocator: this is a hack,
89
 * but it simplifies bookkeeping (in particular, it guarantees the list is
90
 * restored properly by a restore).
91
 *
92
 * We want to close streams freed by restore and by garbage collection.  We
93
 * use the finalization procedure for this.  For restore, we don't have to
94
 * do anything special to make this happen.  For garbage collection, we do
95
 * something more drastic: we simply clear the list of known streams (at all
96
 * save levels).  Any streams open at the time of garbage collection will no
97
 * longer participate in the list of known streams, but this does no harm;
98
 * it simply means that they won't get reused, and can only be reclaimed by
99
 * a future garbage collection or restore.
100
 */
101
102
/*
103
 * Define the default stream buffer sizes.  For file streams,
104
 * this is arbitrary, since the C library or operating system
105
 * does its own buffering in addition.
106
 * However, a buffer size of at least 2K bytes is necessary to prevent
107
 * JPEG decompression from running very slow. When less than 2K, an
108
 * intermediate filter is installed that transfers 1 byte at a time
109
 * causing many aborted roundtrips through the JPEG filter code.
110
 */
111
#define DEFAULT_BUFFER_SIZE 2048
112
extern const uint file_default_buffer_size;
113
114
/* Make an invalid file object. */
115
void
116
make_invalid_file(i_ctx_t *i_ctx_p, ref * fp)
117
1
{
118
1
    make_file(fp, avm_invalid_file_entry, ~0, i_ctx_p->invalid_file_stream);
119
1
}
120
121
/* <name_string> <access_string> file <file> */
122
int                             /* exported for zsysvm.c */
123
zfile(i_ctx_t *i_ctx_p)
124
17.1M
{
125
17.1M
    os_ptr op = osp;
126
17.1M
    char file_access[4];
127
17.1M
    gs_parsed_file_name_t pname;
128
17.1M
    int code = parse_file_access_string(op, file_access);
129
17.1M
    stream *s;
130
131
17.1M
    if (code < 0)
132
34
        return code;
133
17.1M
    code = parse_file_name(op-1, &pname, i_ctx_p->LockFilePermissions, imemory);
134
17.1M
    if (code < 0)
135
6
        return code;
136
        /*
137
         * HACK: temporarily patch the current context pointer into the
138
         * state pointer for stdio-related devices.  See ziodev.c for
139
         * more information.
140
         */
141
17.1M
    if (pname.iodev && pname.iodev->dtype == iodev_dtype_stdio) {
142
13.3M
        bool statement = (strcmp(pname.iodev->dname, "%statementedit%") == 0);
143
13.3M
        bool lineedit = (strcmp(pname.iodev->dname, "%lineedit%") == 0);
144
13.3M
        if (pname.fname)
145
0
            return_error(gs_error_invalidfileaccess);
146
13.3M
        if (statement || lineedit) {
147
            /* These need special code to support callouts */
148
0
            gx_io_device *indev = gs_findiodevice(imemory,
149
0
                                                  (const byte *)"%stdin", 6);
150
0
            stream *ins;
151
0
            if (strcmp(file_access, "r"))
152
0
                return_error(gs_error_invalidfileaccess);
153
0
            indev->state = i_ctx_p;
154
0
            code = (indev->procs.open_device)(indev, file_access, &ins, imemory);
155
0
            indev->state = 0;
156
0
            if (code < 0)
157
0
                return code;
158
0
            check_ostack(2);
159
0
            push(2);
160
0
            make_stream_file(op - 3, ins, file_access);
161
0
            make_bool(op-2, statement);
162
0
            make_int(op-1, 0);
163
0
            make_string(op, icurrent_space, 0, NULL);
164
0
            return zfilelineedit(i_ctx_p);
165
0
        }
166
13.3M
        pname.iodev->state = i_ctx_p;
167
13.3M
        code = (*pname.iodev->procs.open_device)(pname.iodev,
168
13.3M
                                                 file_access, &s, imemory);
169
13.3M
        pname.iodev->state = NULL;
170
13.3M
    } else {
171
3.82M
        if (pname.iodev == NULL)
172
1.84M
            pname.iodev = iodev_default(imemory);
173
3.82M
        code = zopen_file(i_ctx_p, &pname, file_access, &s, imemory);
174
3.82M
    }
175
17.1M
    if (code < 0)
176
2.91M
        return code;
177
14.2M
    if (s == NULL)
178
0
        return_error(gs_error_undefinedfilename);
179
14.2M
    code = ssetfilename(s, op[-1].value.const_bytes, r_size(op - 1));
180
14.2M
    if (code < 0) {
181
0
        sclose(s);
182
0
        return_error(gs_error_VMerror);
183
0
    }
184
14.2M
    make_stream_file(op - 1, s, file_access);
185
14.2M
    pop(1);
186
14.2M
    return code;
187
14.2M
}
188
189
/* ------ Level 2 extensions ------ */
190
191
/* <string> deletefile - */
192
static int
193
zdeletefile(i_ctx_t *i_ctx_p)
194
85.6k
{
195
85.6k
    os_ptr op = osp;
196
85.6k
    gs_parsed_file_name_t pname;
197
85.6k
    int code;
198
199
85.6k
    check_op(1);
200
85.5k
    code = parse_real_file_name(op, &pname, imemory, "deletefile");
201
85.5k
    if (code < 0)
202
4
        return code;
203
85.5k
    code = (*pname.iodev->procs.delete_file)(pname.iodev, pname.fname);
204
205
85.5k
    gs_free_file_name(&pname, "deletefile");
206
85.5k
    if (code < 0)
207
9
        return code;
208
85.5k
    pop(1);
209
85.5k
    return 0;
210
85.5k
}
211
212
/* <template> <proc> <scratch> filenameforall - */
213
static int file_continue(i_ctx_t *);
214
static int file_cleanup(i_ctx_t *);
215
static int
216
zfilenameforall(i_ctx_t *i_ctx_p)
217
2.33M
{
218
2.33M
    os_ptr op = osp;
219
2.33M
    file_enum *pfen;
220
2.33M
    gx_io_device *iodev = NULL;
221
2.33M
    gs_parsed_file_name_t pname;
222
2.33M
    int code = 0;
223
224
2.33M
    check_op(3);
225
2.33M
    check_write_type(*op, t_string);
226
2.33M
    check_proc(op[-1]);
227
2.33M
    check_read_type(op[-2], t_string);
228
    /* Push a mark, the iodev, devicenamelen, the scratch string, the enumerator, */
229
    /* and the procedure, and invoke the continuation. */
230
2.33M
    check_estack(7);
231
    /* Get the iodevice */
232
2.33M
    code = parse_file_name(op-2, &pname, i_ctx_p->LockFilePermissions, imemory);
233
2.33M
    if (code < 0)
234
0
        return code;
235
2.33M
    iodev = (pname.iodev == NULL) ? iodev_default(imemory) : pname.iodev;
236
237
    /* Check for several conditions that just cause us to return success */
238
2.33M
    if (pname.len == 0 || iodev->procs.enumerate_files == iodev_no_enumerate_files) {
239
0
        pop(3);
240
0
        return 0;       /* no pattern, or device not found -- just return */
241
0
    }
242
2.33M
    pfen = iodev->procs.enumerate_files(imemory, iodev, (const char *)pname.fname,
243
2.33M
                pname.len);
244
2.33M
    if (pfen == 0)
245
0
        return_error(gs_error_VMerror);
246
2.33M
    push_mark_estack(es_for, file_cleanup);
247
2.33M
    ++esp;
248
2.33M
    make_istruct(esp, 0, iodev);
249
2.33M
    ++esp;
250
2.33M
    make_int(esp, r_size(op-2) - pname.len);
251
2.33M
    *++esp = *op;
252
2.33M
    ++esp;
253
2.33M
    make_istruct(esp, 0, pfen);
254
2.33M
    *++esp = op[-1];
255
2.33M
    ref_stack_pop(&o_stack, 3);
256
2.33M
    code = file_continue(i_ctx_p);
257
2.33M
    return code;
258
2.33M
}
259
/* Continuation operator for enumerating files */
260
static int
261
file_continue(i_ctx_t *i_ctx_p)
262
2.66M
{
263
2.66M
    os_ptr op = osp;
264
2.66M
    es_ptr pscratch = esp - 2;
265
2.66M
    file_enum *pfen = r_ptr(esp - 1, file_enum);
266
2.66M
    int devlen = esp[-3].value.intval;
267
2.66M
    gx_io_device *iodev = r_ptr(esp - 4, gx_io_device);
268
2.66M
    uint len = r_size(pscratch);
269
2.66M
    uint code;
270
271
2.66M
    if (len < devlen) {
272
0
        esp -= 6;               /* pop proc, pfen, scratch, devlen, iodev , mark */
273
0
        return_error(gs_error_rangecheck);     /* not even room for device len */
274
0
    }
275
276
2.66M
    do {
277
2.66M
        memcpy((char *)pscratch->value.bytes, iodev->dname, devlen);
278
2.66M
        code = iodev->procs.enumerate_next(imemory, pfen, (char *)pscratch->value.bytes + devlen,
279
2.66M
                    len - devlen);
280
2.66M
        if (code == ~(uint) 0) {    /* all done */
281
2.33M
            esp -= 6;               /* pop proc, pfen, scratch, devlen, iodev , mark */
282
2.33M
            return o_pop_estack;
283
2.33M
        } else if (code > len - devlen) {      /* overran string */
284
0
            return_error(gs_error_rangecheck);
285
0
        }
286
333k
        else if (iodev != iodev_default(imemory)) {
287
333k
            push(1);
288
333k
            ref_assign(op, pscratch);
289
333k
            r_set_size(op, code + devlen);
290
333k
            push_op_estack(file_continue);  /* come again */
291
333k
            *++esp = pscratch[2];   /* proc */
292
333k
            return o_push_estack;
293
333k
        }
294
2.66M
    } while(1);
295
2.66M
}
296
/* Cleanup procedure for enumerating files */
297
static int
298
file_cleanup(i_ctx_t *i_ctx_p)
299
0
{
300
0
    gx_io_device *iodev = r_ptr(esp + 2, gx_io_device);
301
302
0
    iodev->procs.enumerate_close(imemory, r_ptr(esp + 5, file_enum));
303
    /* See bug #707007, gp_enumerate_file_close() explicitly frees the file enumerator */
304
0
    make_null(esp + 5);
305
0
    return 0;
306
0
}
307
308
/* <string1> <string2> renamefile - */
309
static int
310
zrenamefile(i_ctx_t *i_ctx_p)
311
10
{
312
10
    int code;
313
10
    os_ptr op = osp;
314
10
    gs_parsed_file_name_t pname1, pname2;
315
316
10
    check_op(2);
317
2
    code = parse_real_file_name(op, &pname2, imemory, "renamefile(to)");
318
2
    if (code < 0)
319
2
        return code;
320
321
0
    pname1.fname = 0;
322
0
    code = parse_real_file_name(op - 1, &pname1, imemory, "renamefile(from)");
323
0
    if (code >= 0) {
324
0
        gx_io_device *iodev_dflt = iodev_default(imemory);
325
0
        if (pname1.iodev != pname2.iodev ) {
326
0
            if (pname1.iodev == iodev_dflt)
327
0
                pname1.iodev = pname2.iodev;
328
0
            if (pname2.iodev == iodev_dflt)
329
0
                pname2.iodev = pname1.iodev;
330
0
        }
331
0
        code = (*pname1.iodev->procs.rename_file)(pname1.iodev, pname1.fname, pname2.fname);
332
0
    }
333
0
    gs_free_file_name(&pname2, "renamefile(to)");
334
0
    gs_free_file_name(&pname1, "renamefile(from)");
335
0
    if (code < 0)
336
0
        return code;
337
0
    pop(2);
338
0
    return 0;
339
0
}
340
341
/* <file> status <open_bool> */
342
/* <string> status <pages> <bytes> <ref_time> <creation_time> true */
343
/* <string> status false */
344
static int
345
zstatus(i_ctx_t *i_ctx_p)
346
2.27M
{
347
2.27M
    os_ptr op = osp;
348
349
2.27M
    check_op(1);
350
2.27M
    switch (r_type(op)) {
351
226k
        case t_file:
352
226k
            {
353
226k
                stream *s;
354
355
226k
                make_bool(op, (file_is_valid(s, op) ? 1 : 0));
356
226k
            }
357
226k
            return 0;
358
2.04M
        case t_string:
359
2.04M
            {
360
2.04M
                gs_parsed_file_name_t pname;
361
2.04M
                struct stat fstat;
362
2.04M
                int code = parse_file_name(op, &pname,
363
2.04M
                                           i_ctx_p->LockFilePermissions, imemory);
364
2.04M
                if (code < 0) {
365
2
                    if (code == gs_error_undefinedfilename) {
366
2
                        make_bool(op, 0);
367
2
                        code = 0;
368
2
                    }
369
2
                    return code;
370
2
                }
371
2.04M
                code = gs_terminate_file_name(&pname, imemory, "status");
372
2.04M
                if (code < 0)
373
0
                    return code;
374
2.04M
                code = (*pname.iodev->procs.file_status)(pname.iodev, pname.fname, &fstat);
375
2.04M
                switch (code) {
376
1.21M
                    case 0:
377
1.21M
                        check_ostack(4);
378
                        /*
379
                         * Check to make sure that the file size fits into
380
                         * a PostScript integer.  (On some systems, long is
381
                         * 32 bits, but file sizes are 64 bits.)
382
                         */
383
1.21M
                        push(4);
384
1.21M
                        make_int(op - 4, stat_blocks(&fstat));
385
1.21M
                        make_int(op - 3, fstat.st_size);
386
                        /*
387
                         * We can't check the value simply by using ==,
388
                         * because signed/unsigned == does the wrong thing.
389
                         * Instead, since integer assignment only keeps the
390
                         * bottom bits, we convert the values to double
391
                         * and then test for equality.  This handles all
392
                         * cases of signed/unsigned or width mismatch.
393
                         */
394
1.21M
                        if ((double)op[-4].value.intval !=
395
1.21M
                              (double)stat_blocks(&fstat) ||
396
1.21M
                            (double)op[-3].value.intval !=
397
1.21M
                              (double)fstat.st_size
398
1.21M
                            )
399
0
                            return_error(gs_error_limitcheck);
400
1.21M
                        make_int(op - 2, fstat.st_mtime);
401
1.21M
                        make_int(op - 1, fstat.st_ctime);
402
1.21M
                        make_bool(op, 1);
403
1.21M
                        break;
404
829k
                    case gs_error_undefinedfilename:
405
829k
                        make_bool(op, 0);
406
829k
                        code = 0;
407
2.04M
                }
408
2.04M
                gs_free_file_name(&pname, "status");
409
2.04M
                return code;
410
2.04M
            }
411
7
        default:
412
7
            return_op_typecheck(op);
413
2.27M
    }
414
2.27M
}
415
416
/* ------ Non-standard extensions ------ */
417
418
/* <executable_file> .execfile - */
419
static int
420
zexecfile(i_ctx_t *i_ctx_p)
421
0
{
422
0
    os_ptr op = osp;
423
424
0
    check_op(1);
425
0
    check_type_access(*op, t_file, a_executable | a_read | a_execute);
426
0
    check_estack(4);            /* cleanup, file, finish, file */
427
0
    push_mark_estack(es_other, execfile_cleanup);
428
0
    *++esp = *op;
429
0
    push_op_estack(execfile_finish);
430
0
    return zexec(i_ctx_p);
431
0
}
432
/* Finish normally. */
433
static int
434
execfile_finish(i_ctx_t *i_ctx_p)
435
0
{
436
0
    check_ostack(1);
437
0
    esp -= 2;
438
0
    execfile_cleanup(i_ctx_p);
439
0
    return o_pop_estack;
440
0
}
441
/* Clean up by closing the file. */
442
static int
443
execfile_cleanup(i_ctx_t *i_ctx_p)
444
0
{
445
0
    check_ostack(1);
446
0
    *++osp = esp[2];
447
0
    return zclosefile(i_ctx_p);
448
0
}
449
450
/* - .filenamelistseparator <string> */
451
static int
452
zfilenamelistseparator(i_ctx_t *i_ctx_p)
453
0
{
454
0
    os_ptr op = osp;
455
456
0
    push(1);
457
0
    make_const_string(op, avm_foreign | a_readonly, 1,
458
0
                      (const byte *)&gp_file_name_list_separator);
459
0
    return 0;
460
0
}
461
462
/* <name> .filenamesplit <dir> <base> <extension> */
463
static int
464
zfilenamesplit(i_ctx_t *i_ctx_p)
465
0
{
466
0
    os_ptr op = osp;
467
468
0
    check_op(1);
469
0
    check_read_type(*op, t_string);
470
/****** NOT IMPLEMENTED YET ******/
471
0
    return_error(gs_error_undefined);
472
0
}
473
474
/* <string> .libfile <file> true */
475
/* <string> .libfile <string> false */
476
int                             /* exported for zsysvm.c */
477
zlibfile(i_ctx_t *i_ctx_p)
478
3.99M
{
479
3.99M
    os_ptr op = osp;
480
3.99M
    int code;
481
3.99M
    byte cname[DEFAULT_BUFFER_SIZE];
482
3.99M
    uint clen;
483
3.99M
    gs_parsed_file_name_t pname;
484
3.99M
    stream *s;
485
3.99M
    gx_io_device *iodev_dflt;
486
487
3.99M
    check_op(1); /* Enough arguments */
488
3.99M
    check_ostack(2); /* Enough space for our results */
489
3.99M
    code = parse_file_name(op, &pname, i_ctx_p->LockFilePermissions, imemory);
490
3.99M
    if (code < 0)
491
694
        return code;
492
3.99M
    iodev_dflt = iodev_default(imemory);
493
3.99M
    if (pname.iodev == NULL)
494
3.02M
        pname.iodev = iodev_dflt;
495
3.99M
    if (pname.iodev != iodev_dflt) { /* Non-OS devices don't have search paths (yet). */
496
970k
        code = zopen_file(i_ctx_p, &pname, "r", &s, imemory);
497
970k
        if (s == NULL) code = gs_note_error(gs_error_undefinedfilename);
498
970k
        if (code >= 0) {
499
166k
            code = ssetfilename(s, op->value.const_bytes, r_size(op));
500
166k
            if (code < 0) {
501
0
                sclose(s);
502
0
                return_error(gs_error_VMerror);
503
0
            }
504
166k
        }
505
970k
        if (code < 0) {
506
803k
            push(1);
507
803k
            make_false(op);
508
803k
            return 0;
509
803k
        }
510
166k
        make_stream_file(op, s, "r");
511
3.02M
    } else {
512
3.02M
        ref fref;
513
514
3.02M
        code = lib_file_open(i_ctx_p->lib_path, imemory, i_ctx_p, pname.fname, pname.len,
515
3.02M
                             (char *)cname, sizeof(cname), &clen, &fref);
516
3.02M
        if (code >= 0) {
517
667k
            s = fptr(&fref);
518
667k
            code = ssetfilename(s, cname, clen);
519
667k
            if (code < 0) {
520
0
                sclose(s);
521
0
                return_error(gs_error_VMerror);
522
0
            }
523
667k
        }
524
3.02M
        if (code < 0) {
525
2.35M
            if (code == gs_error_VMerror || code == gs_error_invalidfileaccess)
526
0
                return code;
527
2.35M
            push(1);
528
2.35M
            make_false(op);
529
2.35M
            return 0;
530
2.35M
        }
531
667k
        ref_assign(op, &fref);
532
667k
    }
533
3.99M
    push(1);
534
834k
    make_true(op);
535
834k
    return 0;
536
834k
}
537
538
/* A "simple" prefix is defined as a (possibly empty) string of
539
   alphanumeric, underscore, and hyphen characters. */
540
static bool
541
prefix_is_simple(const char *pstr)
542
85.9k
{
543
85.9k
    int i;
544
85.9k
    char c;
545
546
343k
    for (i = 0; (c = pstr[i]) != 0; i++) {
547
257k
        if (!(c == '-' || c == '_' || (c >= '0' && c <= '9') ||
548
171k
              (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')))
549
0
            return false;
550
257k
    }
551
85.9k
    return true;
552
85.9k
}
553
554
/* <prefix|null> <access_string> .tempfile <name_string> <file> */
555
static int
556
ztempfile(i_ctx_t *i_ctx_p)
557
85.9k
{
558
85.9k
    os_ptr op = osp;
559
85.9k
    const char *pstr;
560
85.9k
    char fmode[4];
561
85.9k
    char fmode_temp[4];
562
85.9k
    int code;
563
85.9k
    char *prefix = NULL;
564
85.9k
    char *fname= NULL;
565
85.9k
    uint fnlen;
566
85.9k
    gp_file *sfile;
567
85.9k
    stream *s;
568
85.9k
    byte *buf, *sbody;
569
570
85.9k
    check_op(2);
571
85.9k
    code = parse_file_access_string(op, fmode_temp);
572
85.9k
    if (code < 0)
573
0
        return code;
574
85.9k
    prefix = (char *)gs_alloc_bytes(imemory, gp_file_name_sizeof, "ztempfile(prefix)");
575
85.9k
    fname = (char *)gs_alloc_bytes(imemory, gp_file_name_sizeof, "ztempfile(fname)");
576
85.9k
    if (!prefix || !fname) {
577
0
        code = gs_note_error(gs_error_VMerror);
578
0
        goto done;
579
0
    }
580
581
85.9k
    snprintf(fmode, sizeof(fmode), "%s%s", fmode_temp, gp_fmode_binary_suffix);
582
85.9k
    if (r_has_type(op - 1, t_null))
583
85.9k
        pstr = gp_scratch_file_name_prefix;
584
0
    else {
585
0
        uint psize;
586
587
0
        check_read_type(op[-1], t_string);
588
0
        psize = r_size(op - 1);
589
0
        if (psize >= gp_file_name_sizeof) {
590
0
            code = gs_note_error(gs_error_rangecheck);
591
0
            goto done;
592
0
        }
593
0
        memcpy(prefix, op[-1].value.const_bytes, psize);
594
0
        prefix[psize] = 0;
595
0
        pstr = prefix;
596
0
    }
597
598
85.9k
    if (gp_file_name_is_absolute(pstr, strlen(pstr))) {
599
0
        int plen = strlen(pstr);
600
0
        const char *sep = gp_file_name_separator();
601
0
        int seplen = strlen(sep);
602
603
        /* This should not be possible if gp_file_name_is_absolute is true I think
604
         * But let's avoid the problem.
605
         */
606
0
        if (plen < seplen)
607
0
            return_error(gs_error_Fatal);
608
609
0
        plen -= seplen;
610
        /* strip off the file name prefix, leave just the directory name
611
         * so we can check if we are allowed to write to it
612
         */
613
0
        for ( ; plen >=0; plen--) {
614
0
            if ( gs_file_name_check_separator(&pstr[plen], seplen, &pstr[plen]))
615
0
                break;
616
0
        }
617
0
        if (plen < 0)
618
0
            return_error(gs_error_Fatal);
619
620
0
        memcpy(fname, pstr, plen);
621
0
        fname[plen] = '\0';
622
85.9k
    } else if (!prefix_is_simple(pstr)) {
623
0
        code = gs_note_error(gs_error_invalidfileaccess);
624
0
        goto done;
625
0
    }
626
627
85.9k
    s = file_alloc_stream(imemory, "ztempfile(stream)");
628
85.9k
    if (s == 0) {
629
0
        code = gs_note_error(gs_error_VMerror);
630
0
        goto done;
631
0
    }
632
85.9k
    buf = gs_alloc_bytes(imemory, file_default_buffer_size,
633
85.9k
                         "ztempfile(buffer)");
634
85.9k
    if (buf == 0) {
635
0
        code = gs_note_error(gs_error_VMerror);
636
0
        goto done;
637
0
    }
638
85.9k
    sfile = gp_open_scratch_file(imemory, pstr, fname, fmode);
639
85.9k
    if (sfile == 0) {
640
0
        gs_free_object(imemory, buf, "ztempfile(buffer)");
641
0
        code = gs_note_error(gs_error_invalidfileaccess);
642
0
        goto done;
643
0
    }
644
85.9k
    fnlen = strlen(fname);
645
85.9k
    sbody = ialloc_string(fnlen, ".tempfile(fname)");
646
85.9k
    if (sbody == 0) {
647
0
        gs_free_object(imemory, buf, "ztempfile(buffer)");
648
0
        code = gs_note_error(gs_error_VMerror);
649
0
        goto done;
650
0
    }
651
85.9k
    memcpy(sbody, fname, fnlen);
652
85.9k
    file_init_stream(s, sfile, fmode, buf, file_default_buffer_size);
653
85.9k
    code = ssetfilename(s, (const unsigned char*) fname, fnlen);
654
85.9k
    if (code < 0) {
655
0
        gx_io_device *iodev_dflt = iodev_default(imemory);
656
0
        sclose(s);
657
0
        iodev_dflt->procs.delete_file(iodev_dflt, fname);
658
0
        ifree_string(sbody, fnlen, ".tempfile(fname)");
659
0
        code = gs_note_error(gs_error_VMerror);
660
0
        goto done;
661
0
    }
662
85.9k
    make_string(op - 1, a_readonly | icurrent_space, fnlen, sbody);
663
85.9k
    make_stream_file(op, s, fmode);
664
665
85.9k
done:
666
85.9k
    if (prefix)
667
85.9k
        gs_free_object(imemory, prefix, "ztempfile(prefix)");
668
85.9k
    if (fname)
669
85.9k
        gs_free_object(imemory, fname, "ztempfile(fname)");
670
85.9k
    return code;
671
85.9k
}
672
673
/* Return the filename used to open a file object
674
 * this is currently only used by the PDF interpreter to
675
 * get a filename corresponding to the PDF file being
676
 * executed. Since we always execute PDF files from disk
677
 * this will always be OK.
678
 */
679
static int zgetfilename(i_ctx_t *i_ctx_p)
680
0
{
681
0
    os_ptr op = osp;
682
0
    uint fnlen;
683
0
    gs_const_string pfname;
684
0
    stream *s;
685
0
    byte *sbody;
686
0
    int code;
687
688
0
    check_op(1);
689
0
    check_ostack(1);
690
0
    check_read_type(*op, t_file);
691
692
0
    s = (op)->value.pfile;
693
694
0
    code = sfilename(s, &pfname);
695
0
    if (code < 0) {
696
0
        pfname.size = 0;
697
0
    }
698
699
0
    fnlen = pfname.size;
700
0
    sbody = ialloc_string(fnlen, ".getfilename");
701
0
    if (sbody == 0) {
702
0
        code = gs_note_error(gs_error_VMerror);
703
0
        return code;
704
0
    }
705
0
    memcpy(sbody, pfname.data, fnlen);
706
0
    make_string(op, a_readonly | icurrent_space, fnlen, sbody);
707
708
0
    return 0;
709
0
}
710
711
static int zaddcontrolpath(i_ctx_t *i_ctx_p)
712
2.66M
{
713
2.66M
    int code;
714
2.66M
    os_ptr op = osp;
715
2.66M
    ref nsref;
716
2.66M
    unsigned int n = -1;
717
718
2.66M
    check_op(2);
719
2.66M
    check_read_type(*op, t_string);
720
2.66M
    check_type(op[-1], t_name);
721
722
2.66M
    name_string_ref(imemory, op-1, &nsref);
723
2.66M
    if (r_size(&nsref) == 17 &&
724
2.66M
        strncmp((const char *)nsref.value.const_bytes,
725
2.66M
                "PermitFileReading", 17) == 0) {
726
2.66M
        n = gs_permit_file_reading;
727
2.66M
    } else if (r_size(&nsref) == 17 &&
728
0
               strncmp((const char *)nsref.value.const_bytes,
729
0
                       "PermitFileWriting", 17) == 0) {
730
0
        n = gs_permit_file_writing;
731
0
    } else if (r_size(&nsref) == 17 &&
732
0
               strncmp((const char *)nsref.value.const_bytes,
733
0
                       "PermitFileControl", 17) == 0) {
734
0
        n = gs_permit_file_control;
735
0
    }
736
737
2.66M
    if (n == -1)
738
0
        code = gs_note_error(gs_error_rangecheck);
739
2.66M
    else if (gs_is_path_control_active(imemory))
740
0
        code = gs_note_error(gs_error_Fatal);
741
2.66M
    else
742
2.66M
        code = gs_add_control_path_len(imemory, n,
743
2.66M
                                       (const char *)op[0].value.const_bytes,
744
2.66M
                                       (size_t)r_size(&op[0]));
745
2.66M
    pop(2);
746
2.66M
    return code;
747
2.66M
}
748
749
static int zactivatepathcontrol(i_ctx_t *i_ctx_p)
750
166k
{
751
166k
    ref *safety, *safe;
752
166k
    int code = 0;
753
754
166k
    gs_activate_path_control(imemory, 1);
755
756
    /* Set the PostScript level 'safe' parameter. This now correctly prevents
757
     * /tests_private/ps/ps3cet/27-09.ps from changing FontDir, GenericResourceDir
758
     * and GenericResourcePathSep by using setsystemparams (this is implemented
759
     * in PostScript in gs_ll2.ps and relies on systemdict->SAFETY->safe being true).
760
     */
761
166k
    if (dict_find_string(systemdict, "SAFETY", &safety) > 0 &&
762
166k
        dict_find_string(safety, "safe", &safe) > 0) {
763
166k
        make_bool(safe, true);
764
166k
        code = idict_put_string(systemdict, "safe", safe);
765
166k
    }
766
166k
    return code;
767
166k
}
768
static int zcurrentpathcontrolstate(i_ctx_t *i_ctx_p)
769
166k
{
770
166k
    os_ptr op = osp;
771
166k
    push(1);
772
166k
    if (gs_is_path_control_active(imemory)) {
773
7
        make_true(op);
774
7
    }
775
166k
    else {
776
166k
        make_false(op);
777
166k
    }
778
166k
    return 0;
779
166k
}
780
781
/* ------ Initialization procedure ------ */
782
783
const op_def zfile_op_defs[] =
784
{
785
    {"1deletefile", zdeletefile},
786
    {"1.execfile", zexecfile},
787
    {"2file", zfile},
788
    {"3filenameforall", zfilenameforall},
789
    {"0.filenamelistseparator", zfilenamelistseparator},
790
    {"1.filenamesplit", zfilenamesplit},
791
    {"1.libfile", zlibfile},
792
    {"2renamefile", zrenamefile},
793
    {"1status", zstatus},
794
    {"2.tempfile", ztempfile},
795
                /* Internal operators */
796
    {"0%file_continue", file_continue},
797
    {"0%execfile_finish", execfile_finish},
798
    {"1.getfilename", zgetfilename},
799
    /* Control path operators */
800
    {"2.addcontrolpath", zaddcontrolpath},
801
    {"0.activatepathcontrol", zactivatepathcontrol},
802
    {"0.currentpathcontrolstate", zcurrentpathcontrolstate},
803
    op_def_end(0)
804
};
805
806
/* ------ File name parsing ------ */
807
808
/* Parse a file name into device and individual name. */
809
/* See gsfname.c for details. */
810
static int
811
parse_file_name(const ref * op, gs_parsed_file_name_t * pfn, bool safemode,
812
                gs_memory_t *memory)
813
25.5M
{
814
25.5M
    int code;
815
816
25.5M
    check_read_type(*op, t_string);
817
25.5M
    code = gs_parse_file_name(pfn, (const char *)op->value.const_bytes,
818
25.5M
                              r_size(op), memory);
819
25.5M
    if (code < 0)
820
697
        return code;
821
    /*
822
     * Check here for the %pipe device which is illegal when
823
     * LockFilePermissions is true. In the future we might want to allow
824
     * the %pipe device to be included on the PermitFile... paths, but
825
     * for now it is simply disallowed.
826
     */
827
25.5M
    if (pfn->iodev && safemode && strcmp(pfn->iodev->dname, "%pipe%") == 0)
828
0
        return gs_error_invalidfileaccess;
829
25.5M
    return code;
830
25.5M
}
831
832
/* Parse a real (non-device) file name and convert to a C string. */
833
/* See gsfname.c for details. */
834
static int
835
parse_real_file_name(const ref *op, gs_parsed_file_name_t *pfn,
836
                     gs_memory_t *mem, client_name_t cname)
837
85.5k
{
838
85.5k
    check_read_type(*op, t_string);
839
85.5k
    return gs_parse_real_file_name(pfn, (const char *)op->value.const_bytes,
840
85.5k
                                   r_size(op), mem, cname);
841
85.5k
}
842
843
/* Parse the access string for opening a file. */
844
/* [4] is for r/w, +, b, \0. */
845
static int
846
parse_file_access_string(const ref *op, char file_access[4])
847
17.2M
{
848
17.2M
    const byte *astr;
849
850
17.2M
    check_read_type(*op, t_string);
851
17.2M
    astr = op->value.const_bytes;
852
17.2M
    switch (r_size(op)) {
853
85.9k
        case 2:
854
85.9k
            if (astr[1] != '+')
855
1
                return_error(gs_error_invalidfileaccess);
856
85.9k
            file_access[1] = '+';
857
85.9k
            file_access[2] = 0;
858
85.9k
            break;
859
17.1M
        case 1:
860
17.1M
            file_access[1] = 0;
861
17.1M
            break;
862
7
        default:
863
7
            return_error(gs_error_invalidfileaccess);
864
17.2M
    }
865
17.2M
    switch (astr[0]) {
866
4.22M
        case 'r':
867
17.2M
        case 'w':
868
17.2M
        case 'a':
869
17.2M
            break;
870
4
        default:
871
4
            return_error(gs_error_invalidfileaccess);
872
17.2M
    }
873
17.2M
    file_access[0] = astr[0];
874
17.2M
    return 0;
875
17.2M
}
876
877
/* ------ Stream opening ------ */
878
879
/*
880
 * Open a file specified by a parsed file name (which may be only a
881
 * device).
882
 */
883
int
884
zopen_file(i_ctx_t *i_ctx_p, const gs_parsed_file_name_t *pfn,
885
           const char *file_access, stream **ps, gs_memory_t *mem)
886
4.79M
{
887
4.79M
    gx_io_device *const iodev = pfn->iodev;
888
4.79M
    int code = 0;
889
890
4.79M
    if (pfn->fname == NULL) {     /* just a device */
891
0
        iodev->state = i_ctx_p;
892
0
        code = iodev->procs.open_device(iodev, file_access, ps, mem);
893
0
        iodev->state = NULL;
894
0
        return code;
895
0
    }
896
4.79M
    else {                      /* file */
897
4.79M
        iodev_proc_open_file((*open_file)) = iodev->procs.open_file;
898
899
4.79M
        if (open_file == 0)
900
1.84M
            open_file = iodev_os_open_file;
901
4.79M
        return open_file(iodev, pfn->fname, pfn->len, file_access, ps, mem);
902
4.79M
    }
903
4.79M
}
904
905
/*
906
 * Define the file_open procedure for the %os% IODevice (also used, as the
907
 * default, for %pipe% and possibly others).
908
 */
909
static int
910
iodev_os_open_file(gx_io_device * iodev, const char *fname, uint len,
911
                   const char *file_access, stream ** ps, gs_memory_t * mem)
912
30.1M
{
913
30.1M
    return file_open_stream(fname, len, file_access,
914
30.1M
                            file_default_buffer_size, ps,
915
30.1M
                            iodev, iodev->procs.gp_fopen, mem);
916
30.1M
}
917
918
/* Make a t_file reference to a stream. */
919
void
920
make_stream_file(ref * pfile, stream * s, const char *access)
921
17.1M
{
922
17.1M
    uint attrs =
923
17.1M
        (access[1] == '+' ? a_write + a_read + a_execute : 0) |
924
17.1M
        imemory_space((gs_ref_memory_t *) s->memory);
925
926
17.1M
    if (access[0] == 'r') {
927
3.63M
        make_file(pfile, attrs | (a_read | a_execute), s->read_id, s);
928
3.63M
        s->write_id = 0;
929
13.5M
    } else {
930
13.5M
        make_file(pfile, attrs | a_write, s->write_id, s);
931
13.5M
        s->read_id = 0;
932
13.5M
    }
933
17.1M
}
934
935
/* return zero for success, -ve for error, +1 for continue */
936
static int
937
lib_file_open_search_with_no_combine(gs_file_path_ptr  lib_path, const gs_memory_t *mem, i_ctx_t *i_ctx_p,
938
                                     const char *fname, uint flen, char *buffer, int blen, uint *pclen, ref *pfile,
939
                                     gx_io_device *iodev, char *fmode)
940
2.35M
{
941
2.35M
    stream *s;
942
2.35M
    uint blen1 = blen;
943
2.35M
    struct stat fstat;
944
2.35M
    int code = 1;
945
946
2.35M
    if (gp_file_name_reduce(fname, flen, buffer, &blen1) != gp_combine_success)
947
0
      goto skip;
948
949
2.35M
    if (iodev_os_open_file(iodev, (const char *)buffer, blen1,
950
2.35M
                   (const char *)fmode, &s, (gs_memory_t *)mem) == 0) {
951
895
       if (s == NULL)
952
0
           return 1;
953
895
        *pclen = blen1;
954
895
        make_stream_file(pfile, s, "r");
955
895
        code = 0;
956
895
    }
957
2.35M
 skip:
958
2.35M
    return code;
959
2.35M
}
960
961
/* return zero for success, -ve for error, +1 for continue */
962
static int
963
lib_file_open_search_with_combine(gs_file_path_ptr  lib_path, const gs_memory_t *mem, i_ctx_t *i_ctx_p,
964
                                  const char *fname, uint flen, char *buffer, int blen, uint *pclen, ref *pfile,
965
                                  gx_io_device *iodev, char *fmode)
966
3.19M
{
967
3.19M
    stream *s;
968
3.19M
    const gs_file_path *pfpath = lib_path;
969
3.19M
    uint pi;
970
3.19M
    int code = 1;
971
972
34.6M
    for (pi = 0; pi < r_size(&pfpath->list) && code == 1; ++pi) {
973
31.4M
        const ref *prdir = pfpath->list.value.refs + pi;
974
31.4M
        const char *pstr = (const char *)prdir->value.const_bytes;
975
31.4M
        uint plen = r_size(prdir), blen1 = blen;
976
31.4M
        gs_parsed_file_name_t pname;
977
31.4M
        gp_file_name_combine_result r;
978
979
        /* We need to concatenate and parse the file name here
980
         * if this path has a %device% prefix.              */
981
31.4M
        if (pstr[0] == '%') {
982
            /* We concatenate directly since gp_file_name_combine_*
983
             * rules are not correct for other devices such as %rom% */
984
5.54M
            code = gs_parse_file_name(&pname, pstr, plen, mem);
985
5.54M
            if (code < 0) {
986
0
                code = 1;
987
0
                continue;
988
0
            }
989
5.54M
            if (blen < max(pname.len, plen) + flen)
990
0
              return_error(gs_error_limitcheck);
991
5.54M
            memcpy(buffer, pname.fname, pname.len);
992
5.54M
            memcpy(buffer+pname.len, fname, flen);
993
5.54M
            if (pname.iodev->procs.open_file == NULL) {
994
0
                code = 1;
995
0
                continue;
996
0
            }
997
5.54M
            code = pname.iodev->procs.open_file(pname.iodev, buffer, pname.len + flen, fmode,
998
5.54M
                                          &s, (gs_memory_t *)mem);
999
5.54M
            if (code < 0) {
1000
4.71M
                code = 1;
1001
4.71M
                continue;
1002
4.71M
            }
1003
833k
            make_stream_file(pfile, s, "r");
1004
            /* fill in the buffer with the device concatenated */
1005
833k
            memcpy(buffer, pstr, plen);
1006
833k
            memcpy(buffer+plen, fname, flen);
1007
833k
            *pclen = plen + flen;
1008
833k
            code = 0;
1009
25.9M
        } else {
1010
25.9M
            r = gp_file_name_combine(pstr, plen,
1011
25.9M
                    fname, flen, false, buffer, &blen1);
1012
25.9M
            if (r != gp_combine_success)
1013
0
                continue;
1014
1015
25.9M
            if (iodev_os_open_file(iodev, (const char *)buffer, blen1,
1016
25.9M
                        (const char *)fmode, &s, (gs_memory_t *)mem) == 0) {
1017
0
               if (s == NULL)
1018
0
                   return 1;
1019
0
                *pclen = blen1;
1020
0
                make_stream_file(pfile, s, "r");
1021
0
                code = 0;
1022
0
            }
1023
25.9M
        }
1024
31.4M
    }
1025
3.19M
    return code;
1026
3.19M
}
1027
1028
/* Return a file object of of the file searched for using the search paths. */
1029
/* The fname cannot contain a device part (%...%) but the lib paths might. */
1030
/* The startup code calls this to open the initialization file gs_init.ps. */
1031
/* The startup code also calls this to open @-files. */
1032
int
1033
lib_file_open(gs_file_path_ptr  lib_path, const gs_memory_t *mem, i_ctx_t *i_ctx_p,
1034
                       const char *fname, uint flen, char *buffer, int blen, uint *pclen, ref *pfile)
1035
3.19M
{   /* i_ctx_p is NULL running arg (@) files.
1036
     * lib_path and mem are never NULL
1037
     */
1038
3.19M
    bool search_with_no_combine = false;
1039
3.19M
    bool search_with_combine = false;
1040
3.19M
    char fmode[2] = { 'r', 0};
1041
3.19M
    gx_io_device *iodev = iodev_default(mem);
1042
3.19M
    gs_main_instance *minst = get_minst_from_memory(mem);
1043
3.19M
    int code;
1044
1045
    /* when starting arg files (@ files) iodev_default is not yet set */
1046
3.19M
    if (iodev == 0)
1047
0
        iodev = (gx_io_device *)gx_io_device_table[0];
1048
1049
3.19M
    if (gp_file_name_is_absolute(fname, flen)) {
1050
899
       search_with_no_combine = true;
1051
899
       search_with_combine = false;
1052
3.19M
    } else {
1053
3.19M
       search_with_no_combine = true;
1054
3.19M
       search_with_combine = true;
1055
3.19M
    }
1056
3.19M
    if (minst->search_here_first) {
1057
0
      if (search_with_no_combine) {
1058
0
        code = lib_file_open_search_with_no_combine(lib_path, mem, i_ctx_p,
1059
0
                                                    fname, flen, buffer, blen, pclen, pfile,
1060
0
                                                    iodev, fmode);
1061
0
        if (code <= 0) /* +ve means continue continue */
1062
0
          return code;
1063
0
      }
1064
0
      if (search_with_combine) {
1065
0
        code = lib_file_open_search_with_combine(lib_path, mem, i_ctx_p,
1066
0
                                                 fname, flen, buffer, blen, pclen, pfile,
1067
0
                                                 iodev, fmode);
1068
0
        if (code <= 0) /* +ve means continue searching */
1069
0
          return code;
1070
0
      }
1071
3.19M
    } else {
1072
3.19M
      if (search_with_combine) {
1073
3.19M
        code = lib_file_open_search_with_combine(lib_path, mem, i_ctx_p,
1074
3.19M
                                                 fname, flen, buffer, blen, pclen, pfile,
1075
3.19M
                                                 iodev, fmode);
1076
3.19M
        if (code <= 0) /* +ve means continue searching */
1077
833k
          return code;
1078
3.19M
      }
1079
2.35M
      if (search_with_no_combine) {
1080
2.35M
        code = lib_file_open_search_with_no_combine(lib_path, mem, i_ctx_p,
1081
2.35M
                                                    fname, flen, buffer, blen, pclen, pfile,
1082
2.35M
                                                    iodev, fmode);
1083
2.35M
        if (code <= 0) /* +ve means continue searching */
1084
895
          return code;
1085
2.35M
      }
1086
2.35M
    }
1087
3.19M
    return_error(gs_error_undefinedfilename);
1088
3.19M
}
1089
1090
/* The startup code calls this to open @-files. */
1091
stream *
1092
lib_sopen(const gs_file_path_ptr pfpath, const gs_memory_t *mem, const char *fname)
1093
0
{
1094
    /* We need a buffer to hold the expanded file name. */
1095
0
    char filename_found[DEFAULT_BUFFER_SIZE];
1096
0
    uint fnamelen;
1097
0
    ref obj;
1098
0
    int code;
1099
1100
    /* open the usual 'stream', then if successful, return the file */
1101
0
    code = lib_file_open(pfpath, mem, NULL, fname, strlen(fname),
1102
0
                            filename_found, sizeof(filename_found), &fnamelen, &obj);
1103
1104
0
    if (code < 0)
1105
0
        return NULL;
1106
1107
0
    return((stream *)(obj.value.pfile));
1108
0
}
1109
1110
/* Open a file stream that reads a string. */
1111
/* (This is currently used only by the ccinit feature.) */
1112
/* The string must be allocated in non-garbage-collectable (foreign) space. */
1113
int
1114
file_read_string(const byte *str, uint len, ref *pfile, gs_ref_memory_t *imem)
1115
0
{
1116
0
    stream *s = file_alloc_stream((gs_memory_t *)imem, "file_read_string");
1117
1118
0
    if (s == 0)
1119
0
        return_error(gs_error_VMerror);
1120
0
    sread_string(s, str, len);
1121
0
    s->foreign = 1;
1122
0
    s->write_id = 0;
1123
0
    make_file(pfile, a_readonly | imemory_space(imem), s->read_id, s);
1124
0
    s->save_close = s->procs.close;
1125
0
    s->procs.close = file_close_disable;
1126
0
    return 0;
1127
0
}
1128
1129
/* Report an error by storing it in the stream's error_string. */
1130
int
1131
filter_report_error(stream_state * st, const char *str)
1132
0
{
1133
0
    if_debug1m('s', st->memory, "[s]stream error: %s\n", str);
1134
0
    strncpy(st->error_string, str, STREAM_MAX_ERROR_STRING);
1135
    /* Ensure null termination. */
1136
0
    st->error_string[STREAM_MAX_ERROR_STRING] = 0;
1137
0
    return 0;
1138
0
}
1139
1140
/* Open a file stream for a filter. */
1141
int
1142
filter_open(const char *file_access, uint buffer_size, ref * pfile,
1143
            const stream_procs * procs, const stream_template * templat,
1144
            const stream_state * st, gs_memory_t *mem)
1145
1.40M
{
1146
1.40M
    stream *s;
1147
1.40M
    uint ssize = gs_struct_type_size(templat->stype);
1148
1.40M
    stream_state *sst = 0;
1149
1.40M
    int code;
1150
1151
1.40M
    if (templat->stype != &st_stream_state) {
1152
1.33M
        sst = s_alloc_state(mem, templat->stype, "filter_open(stream_state)");
1153
1.33M
        if (sst == 0)
1154
0
            return_error(gs_error_VMerror);
1155
1.33M
    }
1156
1.40M
    code = file_open_stream((char *)0, 0, file_access, buffer_size, &s,
1157
1.40M
                                (gx_io_device *)0, (iodev_proc_fopen_t)0, mem);
1158
1.40M
    if (code < 0) {
1159
0
        gs_free_object(mem, sst, "filter_open(stream_state)");
1160
0
        return code;
1161
0
    }
1162
1.40M
    s_std_init(s, s->cbuf, s->bsize, procs,
1163
1.40M
               (*file_access == 'r' ? s_mode_read : s_mode_write));
1164
1.40M
    s->procs.process = templat->process;
1165
1.40M
    s->save_close = s->procs.close;
1166
1.40M
    s->procs.close = file_close_file;
1167
1.40M
    if (sst == 0) {
1168
        /* This stream doesn't have any state of its own. */
1169
        /* Hack: use the stream itself as the state. */
1170
73.7k
        sst = (stream_state *) s;
1171
1.33M
    } else if (st != 0)         /* might not have client parameters */
1172
1.33M
        memcpy(sst, st, ssize);
1173
1.40M
    s->state = sst;
1174
1.40M
    s_init_state(sst, templat, mem);
1175
1.40M
    sst->report_error = filter_report_error;
1176
1177
1.40M
    if (templat->init != 0) {
1178
1.33M
        code = (*templat->init)(sst);
1179
1.33M
        if (code < 0) {
1180
0
            gs_free_object(mem, sst, "filter_open(stream_state)");
1181
0
            gs_free_object(mem, s->cbuf, "filter_open(buffer)");
1182
0
            return code;
1183
0
        }
1184
1.33M
    }
1185
1.40M
    make_stream_file(pfile, s, file_access);
1186
1.40M
    return 0;
1187
1.40M
}
1188
1189
/* Close a file object. */
1190
/* This is exported only for gsmain.c. */
1191
int
1192
file_close(ref * pfile)
1193
0
{
1194
0
    stream *s;
1195
1196
0
    if (file_is_valid(s, pfile)) {      /* closing a closed file is a no-op */
1197
0
        if (sclose(s))
1198
0
            return_error(gs_error_ioerror);
1199
0
    }
1200
0
    return 0;
1201
0
}