Coverage Report

Created: 2026-04-09 07:06

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ghostpdl/base/stream.c
Line
Count
Source
1
/* Copyright (C) 2001-2024 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
/* Stream package for Ghostscript interpreter */
18
#include "stdio_.h"   /* includes std.h */
19
#include "memory_.h"
20
#include "gdebug.h"
21
#include "gpcheck.h"
22
#include "stream.h"
23
#include "strimpl.h"
24
25
/* Forward declarations */
26
int s_close_disable(stream *);
27
static int sreadbuf(stream *, stream_cursor_write *);
28
static int swritebuf(stream *, stream_cursor_read *, bool);
29
static void stream_compact(stream *, bool);
30
31
/* Structure types for allocating streams. */
32
public_st_stream();
33
public_st_stream_state(); /* default */
34
/* GC procedures */
35
static
36
15.1M
ENUM_PTRS_WITH(stream_enum_ptrs, stream *st) return 0;
37
2.15M
case 0:
38
2.15M
if (st->foreign)
39
0
    ENUM_RETURN(NULL);
40
2.15M
else if (st->cbuf_string.data != 0)
41
0
    ENUM_RETURN_STRING_PTR(stream, cbuf_string);
42
2.15M
else
43
2.15M
    ENUM_RETURN(st->cbuf);
44
2.15M
ENUM_PTR3(1, stream, strm, prev, next);
45
2.15M
ENUM_PTR(4, stream, state);
46
2.15M
case 5: return ENUM_CONST_STRING(&st->file_name);
47
15.1M
ENUM_PTRS_END
48
2.15M
static RELOC_PTRS_WITH(stream_reloc_ptrs, stream *st)
49
2.15M
{
50
2.15M
    byte *cbuf_old = st->cbuf;
51
52
2.15M
    if (cbuf_old != 0 && !st->foreign) {
53
1.32M
        long reloc;
54
55
1.32M
        if (st->cbuf_string.data != 0) {
56
0
            RELOC_STRING_VAR(st->cbuf_string);
57
0
            st->cbuf = st->cbuf_string.data;
58
0
        } else
59
1.32M
            RELOC_VAR(st->cbuf);
60
1.32M
        reloc = cbuf_old - st->cbuf;
61
        /* Relocate the other buffer pointers. */
62
1.32M
        st->cursor.r.ptr -= reloc;
63
1.32M
        st->cursor.r.limit -= reloc;  /* same as swptr */
64
1.32M
        st->cursor.w.limit -= reloc;
65
1.32M
    }
66
2.15M
    RELOC_VAR(st->strm);
67
2.15M
    RELOC_VAR(st->prev);
68
2.15M
    RELOC_VAR(st->next);
69
2.15M
    RELOC_VAR(st->state);
70
2.15M
    RELOC_CONST_STRING_VAR(st->file_name);
71
2.15M
}
72
2.15M
RELOC_PTRS_END
73
/* Finalize a stream by closing it. */
74
/* We only do this for file streams, because other kinds of streams */
75
/* may attempt to free storage when closing. */
76
static void
77
stream_finalize(const gs_memory_t *cmem, void *vptr)
78
70.8M
{
79
70.8M
    stream *const st = vptr;
80
70.8M
    (void)cmem; /* unused */
81
82
70.8M
    if_debug2m('u', st->memory, "[u]%s "PRI_INTPTR"\n",
83
70.8M
               (!s_is_valid(st) ? "already closed:" :
84
70.8M
                st->is_temp ? "is_temp set:" :
85
70.8M
                st->file == 0 ? "not file:" :
86
70.8M
                "closing file:"), (intptr_t) st);
87
70.8M
    if (s_is_valid(st) && !st->is_temp && st->file != 0) {
88
        /* Prevent any attempt to free the buffer. */
89
481
        st->cbuf = 0;
90
481
        st->cbuf_string.data = 0;
91
481
        sclose(st);   /* ignore errors */
92
481
    }
93
70.8M
}
94
95
/* Dummy template for streams that don't have a separate state. */
96
static const stream_template s_no_template = {
97
    &st_stream_state, 0, 0, 1, 1, 0
98
};
99
100
/* ------ Generic procedures ------ */
101
102
/* Allocate a stream and initialize it minimally. */
103
void
104
s_init(stream *s, gs_memory_t * mem)
105
100M
{
106
100M
    s->memory = mem;
107
100M
    s->report_error = s_no_report_error;
108
100M
    s->min_left = 0;
109
100M
    s->error_string[0] = 0;
110
100M
    s->prev = s->next = 0;  /* clean for GC */
111
100M
    s->file_name.data = 0;  /* ibid. */
112
100M
    s->file_name.size = 0;
113
100M
    s->end_status = 0;
114
100M
    s->modes = 0;
115
100M
    s->close_strm = false; /* default */
116
100M
    s->close_at_eod = true; /* default */
117
100M
    s->cbuf_string_memory = NULL;
118
100M
}
119
stream *
120
s_alloc(gs_memory_t * mem, client_name_t cname)
121
70.5M
{
122
70.5M
    stream *s = gs_alloc_struct(mem, stream, &st_stream, cname);
123
124
70.5M
    if_debug2m('s', mem, "[s]alloc(%s) = "PRI_INTPTR"\n",
125
70.5M
               client_name_string(cname), (intptr_t) s);
126
70.5M
    if (s == 0)
127
0
        return 0;
128
70.5M
    s_init(s, mem);
129
70.5M
    return s;
130
70.5M
}
131
stream *
132
s_alloc_immovable(gs_memory_t * mem, client_name_t cname)
133
85.5k
{
134
85.5k
    stream *s = gs_alloc_struct_immovable(mem, stream, &st_stream, cname);
135
136
85.5k
    if_debug2m('s', mem, "[s]alloc(%s) = "PRI_INTPTR"\n",
137
85.5k
               client_name_string(cname), (intptr_t) s);
138
85.5k
    if (s == 0)
139
0
        return 0;
140
85.5k
    s_init(s, mem);
141
85.5k
    return s;
142
85.5k
}
143
144
/* Allocate a stream state and initialize it minimally. */
145
void
146
s_init_state(stream_state *st, const stream_template *templat,
147
             gs_memory_t *mem)
148
21.6M
{
149
21.6M
    st->templat = templat;
150
21.6M
    st->memory = mem;
151
21.6M
    st->report_error = s_no_report_error;
152
21.6M
    st->min_left = 0;
153
21.6M
    st->error_string[0] = 0;
154
21.6M
}
155
stream_state *
156
s_alloc_state(gs_memory_t * mem, gs_memory_type_ptr_t stype,
157
              client_name_t cname)
158
8.83M
{
159
8.83M
    stream_state *st = gs_alloc_struct(mem, stream_state, stype, cname);
160
161
8.83M
    if_debug3m('s', mem, "[s]alloc_state %s(%s) = "PRI_INTPTR"\n",
162
8.83M
               client_name_string(cname),
163
8.83M
               client_name_string(stype->sname),
164
8.83M
               (intptr_t) st);
165
8.83M
    if (st)
166
8.83M
        s_init_state(st, NULL, mem);
167
8.83M
    return st;
168
8.83M
}
169
170
/* Standard stream initialization */
171
void
172
s_std_init(register stream * s, byte * ptr, uint len, const stream_procs * pp,
173
           int modes)
174
56.7M
{
175
56.7M
    s->templat = &s_no_template;
176
56.7M
    s->cbuf = ptr;
177
178
    /* IMPORTANT: "read" MUST come before "write" - see comment in scommon.h about
179
     * the layout of read/write cursor structures.
180
     */
181
56.7M
    stream_cursor_read_init(&s->cursor.r, ptr, 0);
182
56.7M
    stream_cursor_write_init(&s->cursor.w, ptr, len);
183
184
56.7M
    s->end_status = 0;
185
56.7M
    s->foreign = 0;
186
56.7M
    s->modes = modes;
187
56.7M
    s->cbuf_string.data = 0;
188
56.7M
    s->position = 0;
189
56.7M
    s->bsize = s->cbsize = len;
190
56.7M
    s->strm = 0;    /* not a filter */
191
56.7M
    s->is_temp = 0;
192
56.7M
    s->procs = *pp;
193
56.7M
    s->state = (stream_state *) s;  /* hack to avoid separate state */
194
56.7M
    s->file = 0;
195
56.7M
    s->file_name.data = 0;  /* in case stream is on stack */
196
56.7M
    s->file_name.size = 0;
197
56.7M
    s->cbuf_string_memory = NULL;
198
56.7M
    if (s->memory) {
199
32.9M
        if_debug4m('s', s->memory, "[s]init "PRI_INTPTR", buf="PRI_INTPTR", len=%u, modes=%d\n",
200
32.9M
                   (intptr_t) s, (intptr_t) ptr, len, modes);
201
32.9M
    }
202
56.7M
}
203
204
/* Set the file name of a stream, copying the name. */
205
/* Return <0 if the copy could not be allocated. */
206
int
207
ssetfilename(stream *s, const byte *data, uint size)
208
26.6M
{
209
26.6M
    byte *str =
210
26.6M
        (s->file_name.data == 0 ?
211
14.7M
         gs_alloc_string(s->memory, size + 1, "ssetfilename") :
212
26.6M
         gs_resize_string(s->memory,
213
26.6M
                          (byte *)s->file_name.data,  /* break const */
214
26.6M
                          s->file_name.size,
215
26.6M
                          size + 1, "ssetfilename"));
216
217
26.6M
    if (str == 0)
218
0
        return -1;
219
26.6M
    memcpy(str, data, size);
220
26.6M
    str[size] = 0;
221
26.6M
    s->file_name.data = str;
222
26.6M
    s->file_name.size = size + 1;
223
26.6M
    return 0;
224
26.6M
}
225
226
/* Return the file name of a stream, if any. */
227
/* There is a guaranteed 0 byte after the string. */
228
int
229
sfilename(stream *s, gs_const_string *pfname)
230
1.65M
{
231
1.65M
    pfname->data = s->file_name.data;
232
1.65M
    if (pfname->data == 0) {
233
90.4k
        pfname->size = 0;
234
90.4k
        return -1;
235
90.4k
    }
236
1.56M
    pfname->size = s->file_name.size - 1; /* omit terminator */
237
1.56M
    return 0;
238
1.65M
}
239
240
/* Implement a stream procedure as a no-op. */
241
int
242
s_std_null(stream * s)
243
6.28M
{
244
6.28M
    return 0;
245
6.28M
}
246
247
/* Discard the contents of the buffer when reading. */
248
void
249
s_std_read_reset(stream * s)
250
0
{
251
0
    s->cursor.r.ptr = s->cursor.r.limit = s->cbuf - 1;
252
0
}
253
254
/* Discard the contents of the buffer when writing. */
255
void
256
s_std_write_reset(stream * s)
257
0
{
258
0
    s->cursor.w.ptr = s->cbuf - 1;
259
0
}
260
261
/* Flush data to end-of-file when reading. */
262
int
263
s_std_read_flush(stream * s)
264
453k
{
265
1.05M
    while (1) {
266
1.05M
        s->cursor.r.ptr = s->cursor.r.limit = s->cbuf - 1;
267
1.05M
        if (s->end_status)
268
453k
            break;
269
605k
        s_process_read_buf(s);
270
605k
    }
271
453k
    return (s->end_status == EOFC ? 0 : s->end_status);
272
453k
}
273
274
/* Flush buffered data when writing. */
275
int
276
s_std_write_flush(stream * s)
277
1.83M
{
278
1.83M
    return s_process_write_buf(s, false);
279
1.83M
}
280
281
/* Indicate that the number of available input bytes is unknown. */
282
int
283
s_std_noavailable(stream * s, gs_offset_t *pl)
284
22
{
285
22
    *pl = -1;
286
22
    return 0;
287
22
}
288
289
/* Indicate an error when asked to seek. */
290
int
291
s_std_noseek(stream * s, gs_offset_t pos)
292
10
{
293
10
    return ERRC;
294
10
}
295
296
/* Standard stream closing. */
297
int
298
s_std_close(stream * s)
299
8.38M
{
300
8.38M
    return 0;
301
8.38M
}
302
303
/* Standard stream mode switching. */
304
int
305
s_std_switch_mode(stream * s, bool writing)
306
0
{
307
0
    return ERRC;
308
0
}
309
310
/* Standard stream finalization.  Disable the stream. */
311
void
312
s_disable(register stream * s)
313
100M
{
314
100M
    s->cbuf = 0;
315
100M
    s->bsize = 0;
316
100M
    s->end_status = EOFC;
317
100M
    s->modes = 0;
318
100M
    s->cbuf_string.data = 0;
319
    /* The pointers in the next two statements should really be */
320
    /* initialized to ([const] byte *)0 - 1, but some very picky */
321
    /* compilers complain about this. */
322
100M
    s->cursor.r.ptr = s->cursor.r.limit = 0;
323
100M
    s->cursor.w.limit = 0;
324
100M
    s->procs.close = s_std_null;
325
    /* Clear pointers for GC */
326
100M
    s->strm = 0;
327
100M
    s->state = (stream_state *) s;
328
100M
    s->templat = &s_no_template;
329
    /* Free the file name. */
330
100M
    if (s->file_name.data) {
331
14.5M
        if (s->memory) {
332
14.5M
            gs_free_const_string(s->memory, s->file_name.data, s->file_name.size,
333
14.5M
                                 "s_disable(file_name)");
334
14.5M
        }
335
14.5M
        s->file_name.data = 0;
336
14.5M
        s->file_name.size = 0;
337
14.5M
    }
338
    /****** SHOULD DO MORE THAN THIS ******/
339
100M
    if (s->memory) {
340
98.2M
        if_debug1m('s', s->memory, "[s]disable "PRI_INTPTR"\n", (intptr_t) s);
341
98.2M
    }
342
100M
}
343
344
/* Implement flushing for encoding filters. */
345
int
346
s_filter_write_flush(register stream * s)
347
101k
{
348
101k
    int status = s_process_write_buf(s, false);
349
350
101k
    if (status != 0)
351
0
        return status;
352
101k
    return sflush(s->strm);
353
101k
}
354
355
/* Close a filter.  If this is an encoding filter, flush it first. */
356
/* If CloseTarget was specified (close_strm), then propagate the sclose */
357
int
358
s_filter_close(register stream * s)
359
7.53M
{
360
7.53M
    int status;
361
7.53M
    bool close = s->close_strm;
362
7.53M
    stream *stemp = s->strm;
363
364
7.53M
    if (s_is_writing(s)) {
365
937k
        int status = s_process_write_buf(s, true);
366
367
937k
        if (status != 0 && status != EOFC)
368
200k
            return status;
369
737k
        if (status != EOFC)
370
0
            status = sflush(stemp);
371
737k
        if (status != 0 && status != EOFC)
372
0
            return status;
373
737k
    }
374
7.32M
    status = s_std_close(s);
375
7.32M
    if (status != 0 && status != EOFC)
376
0
        return status;
377
7.32M
    if (close && stemp != 0)
378
32.9k
        return sclose(stemp);
379
7.29M
    return status;
380
7.32M
}
381
382
/* Disregard a stream error message. */
383
int
384
s_no_report_error(stream_state * st, const char *str)
385
23
{
386
23
    return 0;
387
23
}
388
389
/* Generic procedure structures for filters. */
390
391
const stream_procs s_filter_read_procs = {
392
    s_std_noavailable, s_std_noseek, s_std_read_reset,
393
    s_std_read_flush, s_filter_close
394
};
395
396
const stream_procs s_filter_write_procs = {
397
    s_std_noavailable, s_std_noseek, s_std_write_reset,
398
    s_filter_write_flush, s_filter_close
399
};
400
401
/* ------ Implementation-independent procedures ------ */
402
403
/* Store the amount of available data in a(n input) stream. */
404
int
405
savailable(stream * s, gs_offset_t *pl)
406
14.0M
{
407
14.0M
    return (*(s)->procs.available) (s, pl);
408
14.0M
}
409
410
/* Return the current position of a stream. */
411
gs_offset_t
412
stell(stream * s)
413
196M
{
414
    /*
415
     * The stream might have been closed, but the position
416
     * is still meaningful in this case.
417
     */
418
196M
    const byte *ptr = (s_is_writing(s) ? s->cursor.w.ptr : s->cursor.r.ptr);
419
420
196M
    return (ptr == 0 ? 0 : ptr + 1 - s->cbuf) + s->position;
421
196M
}
422
423
/* Set the position of a stream. */
424
int
425
spseek(stream * s, gs_offset_t pos)
426
149M
{
427
149M
    if_debug3m('s', s->memory, "[s]seek 0x%"PRIx64" to %"PRId64", position was %"PRId64"\n",
428
149M
                (uint64_t)s, (int64_t)pos, (int64_t)stell(s));
429
149M
    return (*(s)->procs.seek) (s, pos);
430
149M
}
431
432
/* Switch a stream to read or write mode. */
433
/* Return 0 or ERRC. */
434
int
435
sswitch(register stream * s, bool writing)
436
85.5k
{
437
85.5k
    if (s->procs.switch_mode == 0)
438
0
        return ERRC;
439
85.5k
    return (*s->procs.switch_mode) (s, writing);
440
85.5k
}
441
442
/* Close a stream, disabling it if successful. */
443
/* (The stream may already be closed.) */
444
int
445
sclose(register stream * s)
446
30.8M
{
447
30.8M
    stream_state *st;
448
30.8M
    int status = (*s->procs.close) (s);
449
450
30.8M
    if (status < 0)
451
200k
        return status;
452
30.6M
    st = s->state;
453
30.6M
    if (st != 0) {
454
30.6M
        stream_proc_release((*release)) = st->templat->release;
455
30.6M
        if (release != 0)
456
2.70M
            (*release) (st);
457
30.6M
        if (st != (stream_state *) s && st->memory != 0) {
458
7.39M
            gs_memory_t *mem = st->memory;
459
7.39M
            st->memory = NULL;
460
7.39M
            gs_free_object(mem, st, "s_std_close");
461
7.39M
        }
462
30.6M
        s->state = (stream_state *) s;
463
30.6M
    }
464
30.6M
    s_disable(s);
465
30.6M
    return status;
466
30.8M
}
467
468
/*
469
 * Implement sgetc when the buffer may be empty.  If the buffer really is
470
 * empty, refill it and then read a byte.  Note that filters must read one
471
 * byte ahead, so that they can close immediately after the client reads the
472
 * last data byte if the next thing is an EOD.
473
 */
474
int
475
spgetcc(register stream * s, bool close_at_eod)
476
13.1G
{
477
13.1G
    int status, left;
478
13.1G
    int min_left = sbuf_min_left(s);
479
480
13.2G
    while (status = s->end_status,
481
13.2G
           left = s->cursor.r.limit - s->cursor.r.ptr,
482
13.2G
           left <= min_left && status >= 0
483
13.1G
        )
484
154M
        s_process_read_buf(s);
485
13.1G
    if (left <= min_left &&
486
9.15M
        (left <= 0 || (status != EOFC && status != ERRC))
487
13.1G
        ) {
488
        /* Compact the stream so stell will return the right result. */
489
9.04M
        if (left == 0)
490
9.04M
            stream_compact(s, true);
491
9.04M
        if (status == EOFC && close_at_eod && s->close_at_eod) {
492
6.62M
            status = sclose(s);
493
6.62M
            if (status == 0)
494
6.62M
                status = EOFC;
495
6.62M
            s->end_status = status;
496
6.62M
        }
497
9.04M
        return status;
498
9.04M
    }
499
13.0G
    return *++(s->cursor.r.ptr);
500
13.1G
}
501
502
/* Implementing sputc when the buffer is full, */
503
/* by flushing the buffer and then writing the byte. */
504
int
505
spputc(register stream * s, byte b)
506
420M
{
507
515M
    for (;;) {
508
515M
        if (s->end_status)
509
149
            return s->end_status;
510
515M
        if (!sendwp(s)) {
511
420M
            *++(s->cursor.w.ptr) = b;
512
420M
            return b;
513
420M
        }
514
95.1M
        s_process_write_buf(s, false);
515
95.1M
    }
516
420M
}
517
518
/* Push back a character onto a (read) stream. */
519
/* The character must be the same as the last one read. */
520
/* Return 0 on success, ERRC on failure. */
521
int
522
sungetc(register stream * s, byte c)
523
21.6k
{
524
    /* cbuf == NULL means this stream is stdin, and we shouldn't
525
       unread from stdin, ever.
526
     */
527
21.6k
    if (s->cbuf == NULL || !s_is_reading(s) ||
528
21.6k
        s->cursor.r.ptr < s->cbuf || *(s->cursor.r.ptr) != c)
529
0
        return ERRC;
530
21.6k
    s->cursor.r.ptr--;
531
21.6k
    return 0;
532
21.6k
}
533
534
/* Get a string from a stream. */
535
/* Return 0 if the string was filled, or an exception status. */
536
int
537
sgets(stream * s, byte * buf, uint nmax, uint * pn)
538
166M
{
539
166M
    stream_cursor_write cw;
540
166M
    int status = 0;
541
166M
    gs_offset_t min_left = sbuf_min_left(s);
542
543
166M
    cw.ptr = buf - 1;
544
166M
    cw.limit = cw.ptr + nmax;
545
449M
    while (cw.ptr < cw.limit) {
546
292M
        int left;
547
548
292M
        if ((left = s->cursor.r.limit - s->cursor.r.ptr) > min_left) {
549
221M
            s->cursor.r.limit -= min_left;
550
221M
            stream_move(&s->cursor.r, &cw);
551
221M
            s->cursor.r.limit += min_left;
552
221M
        } else {
553
70.5M
            uint wanted = cw.limit - cw.ptr;
554
70.5M
            int c;
555
70.5M
            stream_state *st;
556
557
70.5M
            if (wanted >= s->bsize >> 2 &&
558
58.9M
                (st = s->state) != 0 &&
559
58.9M
                wanted >= st->templat->min_out_size &&
560
30.4M
                s->end_status == 0 &&
561
30.4M
                left == 0
562
70.5M
                ) {
563
30.4M
                byte *wptr = cw.ptr;
564
565
30.4M
                cw.limit -= min_left;
566
30.4M
                status = sreadbuf(s, &cw);
567
30.4M
                cw.limit += min_left;
568
                /* Compact the stream so stell will return the right result. */
569
30.4M
                stream_compact(s, true);
570
                /*
571
                 * We know the stream buffer is empty, so it's safe to
572
                 * update position.  However, we need to reset the read
573
                 * cursor to indicate that there is no data in the buffer.
574
                 */
575
30.4M
                s->cursor.r.ptr = s->cursor.r.limit = s->cbuf - 1;
576
30.4M
                s->position += cw.ptr - wptr;
577
30.4M
                if (status <= 0 || cw.ptr == cw.limit)
578
8.49M
                    break;
579
30.4M
            }
580
62.0M
            c = spgetc(s);
581
62.0M
            if (c < 0) {
582
853k
                status = c;
583
853k
                break;
584
853k
            }
585
61.2M
            *++(cw.ptr) = c;
586
61.2M
        }
587
292M
    }
588
166M
    *pn = cw.ptr + 1 - buf;
589
166M
    return (status >= 0 ? 0 : status);
590
166M
}
591
592
/* Write a string on a stream. */
593
/* Return 0 if the entire string was written, or an exception status. */
594
int
595
sputs(register stream * s, const byte * str, uint wlen, uint * pn)
596
845M
{
597
845M
    uint len = wlen;
598
845M
    int status = s->end_status;
599
600
845M
    if (status >= 0)
601
1.86G
        while (len > 0) {
602
1.01G
            uint count = s->cursor.w.limit - s->cursor.w.ptr;
603
604
1.01G
            if (count > 0) {
605
926M
                if (count > len)
606
827M
                    count = len;
607
926M
                memcpy(s->cursor.w.ptr + 1, str, count);
608
926M
                s->cursor.w.ptr += count;
609
926M
                str += count;
610
926M
                len -= count;
611
926M
            } else {
612
91.0M
                byte ch = *str++;
613
614
91.0M
                status = sputc(s, ch);
615
91.0M
                if (status < 0)
616
0
                    break;
617
91.0M
                len--;
618
91.0M
            }
619
1.01G
        }
620
845M
    *pn = wlen - len;
621
845M
    return (status >= 0 ? 0 : status);
622
845M
}
623
624
/* Skip ahead a specified distance in a read stream. */
625
/* Return 0 or an exception status. */
626
/* Store the number of bytes skipped in *pskipped. */
627
int
628
spskip(register stream * s, gs_offset_t nskip, gs_offset_t *pskipped)
629
0
{
630
0
    gs_offset_t n = nskip;
631
0
    gs_offset_t min_left;
632
633
0
    if (nskip < 0 || !s_is_reading(s)) {
634
0
        *pskipped = 0;
635
0
        return ERRC;
636
0
    }
637
0
    if (s_can_seek(s)) {
638
0
        gs_offset_t pos = stell(s);
639
0
        int status = sseek(s, pos + n);
640
641
0
        *pskipped = stell(s) - pos;
642
0
        return status;
643
0
    }
644
0
    min_left = sbuf_min_left(s);
645
0
    while (sbufavailable(s) < n + min_left) {
646
0
        int status;
647
648
0
        n -= sbufavailable(s);
649
0
        s->cursor.r.ptr = s->cursor.r.limit;
650
0
        if (s->end_status) {
651
0
            *pskipped = nskip - n;
652
0
            return s->end_status;
653
0
        }
654
0
        status = sgetc(s);
655
0
        if (status < 0) {
656
0
            *pskipped = nskip - n;
657
0
            return status;
658
0
        }
659
0
        --n;
660
0
    }
661
    /* Note that if min_left > 0, n < 0 is possible; this is harmless. */
662
0
    s->cursor.r.ptr += n;
663
0
    *pskipped = nskip;
664
0
    return 0;
665
0
}
666
667
/* Read a line from a stream.  See srdline.h for the specification. */
668
int
669
sreadline(stream *s_in, stream *s_out, void *readline_data,
670
          gs_const_string *prompt, gs_string * buf,
671
          gs_memory_t * bufmem, uint * pcount, bool *pin_eol,
672
          bool (*is_stdin)(const stream *))
673
41
{
674
41
    uint count = *pcount;
675
676
    /* Most systems define \n as 0xa and \r as 0xd; however, */
677
    /* OS-9 has \n == \r == 0xd and \l == 0xa.  The following */
678
    /* code works properly regardless of environment. */
679
#if '\n' == '\r'
680
#  define LF 0xa
681
#else
682
41
#  define LF '\n'
683
41
#endif
684
685
41
    if (count == 0 && s_out && prompt) {
686
0
        uint ignore_n;
687
0
        int ch = sputs(s_out, prompt->data, prompt->size, &ignore_n);
688
689
0
        if (ch < 0)
690
0
            return ch;
691
0
    }
692
693
82
top:
694
82
    if (*pin_eol) {
695
        /*
696
         * We're in the middle of checking for a two-character
697
         * end-of-line sequence.  If we get an EOF here, stop, but
698
         * don't signal EOF now; wait till the next read.
699
         */
700
41
        int ch = spgetcc(s_in, false);
701
702
41
        if (ch == EOFC) {
703
0
            *pin_eol = false;
704
0
            return 0;
705
41
        } else if (ch < 0)
706
0
            return ch;
707
41
        else if (ch != LF)
708
0
            sputback(s_in);
709
41
        *pin_eol = false;
710
41
        return 0;
711
41
    }
712
1.02k
    for (;;) {
713
1.02k
        int ch = sgetc(s_in);
714
715
1.02k
        if (ch < 0) {   /* EOF or exception */
716
0
            *pcount = count;
717
0
            return ch;
718
0
        }
719
1.02k
        switch (ch) {
720
41
            case '\r':
721
41
                {
722
#if '\n' == '\r'    /* OS-9 or similar */
723
                    if (!is_stdin(s_in))
724
#endif
725
41
                    {
726
41
                        *pcount = count;
727
41
                        *pin_eol = true;
728
41
                        goto top;
729
0
                    }
730
0
                }
731
                /* falls through */
732
0
            case LF:
733
0
#undef LF
734
0
                *pcount = count;
735
0
                return 0;
736
1.02k
        }
737
984
        if (count >= buf->size) { /* filled the string */
738
0
            if (!bufmem) {
739
0
                sputback(s_in);
740
0
                *pcount = count;
741
0
                return 1;
742
0
            }
743
0
            {
744
0
                uint nsize = count + max(count, 20);
745
0
                byte *ndata = gs_resize_string(bufmem, buf->data, buf->size,
746
0
                                               nsize, "sreadline(buffer)");
747
748
0
                if (ndata == 0)
749
0
                    return ERRC; /* no better choice */
750
0
                buf->data = ndata;
751
0
                buf->size = nsize;
752
0
            }
753
0
        }
754
984
        buf->data[count++] = ch;
755
984
    }
756
    /*return 0; *//* not reached */
757
41
}
758
759
/* ------ Utilities ------ */
760
761
/*
762
 * Attempt to refill the buffer of a read stream.  Only call this if the
763
 * end_status is not EOFC, and if the buffer is (nearly) empty.
764
 */
765
int
766
s_process_read_buf(stream * s)
767
165M
{
768
165M
    int status;
769
770
165M
    stream_compact(s, false);
771
165M
    status = sreadbuf(s, &s->cursor.w);
772
165M
    s->end_status = (status >= 0 ? 0 : status);
773
165M
    return 0;
774
165M
}
775
776
/*
777
 * Attempt to empty the buffer of a write stream.  Only call this if the
778
 * end_status is not EOFC.
779
 */
780
int
781
s_process_write_buf(stream * s, bool last)
782
102M
{
783
102M
    int status = swritebuf(s, &s->cursor.r, last);
784
785
102M
    stream_compact(s, false);
786
102M
    return (status >= 0 ? 0 : status);
787
102M
}
788
789
/* Move forward or backward in a pipeline.  We temporarily reverse */
790
/* the direction of the pointers while doing this. */
791
/* (Cf the Deutsch-Schorr-Waite graph marking algorithm.) */
792
#define MOVE_BACK(curr, prev)\
793
83.8M
  BEGIN\
794
83.8M
    stream *back = prev->strm;\
795
83.8M
    prev->strm = curr; curr = prev; prev = back;\
796
83.8M
  END
797
#define MOVE_AHEAD(curr, prev)\
798
53.7M
  BEGIN\
799
53.7M
    stream *ahead = curr->strm;\
800
53.7M
    curr->strm = prev; prev = curr; curr = ahead;\
801
53.7M
  END
802
803
/*
804
 * Read from a stream pipeline.  Update end_status for all streams that were
805
 * actually touched.  Return the status from the outermost stream: this is
806
 * normally the same as s->end_status, except that if s->procs.process
807
 * returned 1, sreadbuf sets s->end_status to 0, but returns 1.
808
 */
809
static int
810
sreadbuf(stream * s, stream_cursor_write * pbuf)
811
195M
{
812
195M
    stream *prev = 0;
813
195M
    stream *curr = s;
814
195M
    int status;
815
816
207M
    for (;;) {
817
207M
        stream *strm;
818
207M
        stream_cursor_write *pw;
819
207M
        byte *oldpos;
820
821
219M
        for (;;) {   /* Descend into the recursion. */
822
219M
            stream_cursor_read cr;
823
219M
            stream_cursor_read *pr;
824
219M
            int left;
825
219M
            bool eof;
826
827
219M
            strm = curr->strm;
828
219M
            if (strm == 0) {
829
104M
                cr.ptr = 0, cr.limit = 0;
830
104M
                pr = &cr;
831
104M
                left = 0;
832
104M
                eof = false;
833
114M
            } else {
834
114M
                pr = &strm->cursor.r;
835
114M
                left = sbuf_min_left(strm);
836
114M
                left = min(left, pr->limit - pr->ptr);
837
114M
                pr->limit -= left;
838
114M
                eof = strm->end_status == EOFC;
839
114M
            }
840
219M
            pw = (prev == 0 ? pbuf : &curr->cursor.w);
841
219M
            if_debug4m('s', s->memory, "[s]read process "PRI_INTPTR", nr=%u, nw=%u, eof=%d\n",
842
219M
                       (intptr_t) curr, (uint) (pr->limit - pr->ptr),
843
219M
                       (uint) (pw->limit - pw->ptr), eof);
844
219M
            oldpos = pw->ptr;
845
219M
            status = (*curr->procs.process) (curr->state, pr, pw, eof);
846
219M
            if (pr->limit != NULL)
847
114M
                pr->limit += left;
848
219M
            if_debug5m('s', s->memory, "[s]after read "PRI_INTPTR", nr=%u, nw=%u, status=%d, position=%"PRId64"\n",
849
219M
                       (intptr_t) curr, (uint) (pr->limit - pr->ptr),
850
219M
                       (uint) (pw->limit - pw->ptr), status, s->position);
851
219M
            if (strm == 0 || status != 0)
852
203M
                break;
853
16.0M
            if (strm->end_status < 0) {
854
4.36M
                if (strm->end_status != EOFC || pw->ptr == oldpos)
855
4.26M
                    status = strm->end_status;
856
4.36M
                break;
857
4.36M
            }
858
16.0M
            MOVE_AHEAD(curr, prev);
859
11.6M
            stream_compact(curr, false);
860
11.6M
        }
861
        /* If curr reached EOD and is a filter or file stream, close it
862
         * if it is the last filter in the pipeline. Closing the last filter
863
         * seems to contradict PLRM3 but matches Adobe interpreters.
864
         */
865
207M
        if ((strm != 0 || curr->file) && status == EOFC &&
866
6.43M
            curr->cursor.r.ptr >= curr->cursor.r.limit &&
867
2.08M
            curr->close_at_eod &&
868
1.98M
            prev == 0
869
207M
            ) {
870
1.98M
            int cstat = sclose(curr);
871
872
1.98M
            if (cstat != 0)
873
0
                status = cstat;
874
1.98M
        }
875
        /* Unwind from the recursion. */
876
207M
        curr->end_status = (status >= 0 ? 0 : status);
877
207M
        if (prev == 0)
878
195M
            return status;
879
207M
        MOVE_BACK(curr, prev);
880
11.6M
    }
881
195M
}
882
883
/* Write to a pipeline. */
884
static int
885
swritebuf(stream * s, stream_cursor_read * pbuf, bool last)
886
102M
{
887
102M
    stream *prev = 0;
888
102M
    stream *curr = s;
889
102M
    int depth = 0;    /* # of non-temp streams before curr */
890
102M
    int status;
891
892
    /*
893
     * The handling of EOFC is a little tricky.  There are two
894
     * invariants that keep it straight:
895
     *      - We only pass last = true to a stream if either it is
896
     * the first stream in the pipeline, or it is a temporary stream
897
     * below the first stream and the stream immediately above it has
898
     * end_status = EOFC.
899
     */
900
132M
    for (;;) {
901
162M
        for (;;) {
902
            /* Move ahead in the pipeline. */
903
162M
            stream *strm = curr->strm;
904
162M
            stream_cursor_write cw;
905
162M
            stream_cursor_read *pr;
906
162M
            stream_cursor_write *pw;
907
908
            /*
909
             * We only want to set the last/end flag for
910
             * the top-level stream and any temporary streams
911
             * immediately below it.
912
             */
913
162M
            bool end = last &&
914
2.14M
                (prev == 0 ||
915
944k
                 (depth <= 1 && prev->end_status == EOFC));
916
917
162M
            if (strm == 0)
918
49.6M
                cw.ptr = 0, cw.limit = 0, pw = &cw;
919
113M
            else
920
113M
                pw = &strm->cursor.w;
921
162M
            if (prev == 0)
922
124M
                pr = pbuf;
923
38.3M
            else
924
38.3M
                pr = &curr->cursor.r;
925
162M
            if_debug5m('s', s->memory,
926
162M
                       "[s]write process "PRI_INTPTR"(%s), nr=%u, nw=%u, end=%d\n",
927
162M
                       (intptr_t)curr,
928
162M
                       gs_struct_type_name(curr->state->templat->stype),
929
162M
                       (uint)(pr->limit - pr->ptr),
930
162M
                       (uint)(pw->limit - pw->ptr), end);
931
162M
            status = curr->end_status;
932
162M
            if (status >= 0) {
933
162M
                status = (*curr->procs.process)(curr->state, pr, pw, end);
934
162M
                if_debug5m('s', s->memory,
935
162M
                           "[s]after write "PRI_INTPTR", nr=%u, nw=%u, end=%d, status=%d\n",
936
162M
                           (intptr_t) curr, (uint) (pr->limit - pr->ptr),
937
162M
                           (uint) (pw->limit - pw->ptr), end, status);
938
162M
                if (status == 0 && end)
939
702k
                    status = EOFC;
940
162M
                if (status == EOFC || status == ERRC)
941
812k
                    curr->end_status = status;
942
162M
            }
943
162M
            if (strm == 0 || (status < 0 && status != EOFC))
944
49.6M
                break;
945
113M
            if (status != 1) {
946
                /*
947
                 * Keep going if we are closing a filter with a sub-stream.
948
                 * We know status == 0 or EOFC.
949
                 */
950
83.0M
                if (!end || !strm->is_temp)
951
82.7M
                    break;
952
83.0M
            }
953
30.3M
            status = strm->end_status;
954
30.3M
            if (status < 0 && (status != EOFC || !end))
955
0
                break;
956
30.3M
            if (!curr->is_temp)
957
30.3M
                ++depth;
958
30.3M
            if_debug1m('s', strm->memory, "[s]moving ahead, depth = %d\n", depth);
959
30.3M
            MOVE_AHEAD(curr, prev);
960
30.3M
            stream_compact(curr, false);
961
30.3M
        }
962
        /* Move back in the pipeline. */
963
132M
        curr->end_status = (status >= 0 ? 0 : status);
964
132M
        if (status < 0 || prev == 0) {
965
            /*
966
             * All streams up to here were called with last = true
967
             * and returned 0 or EOFC (so their end_status is now EOFC):
968
             * finish unwinding and then return.  Change the status of
969
             * the prior streams to ERRC if the new status is ERRC,
970
             * otherwise leave it alone.
971
             */
972
102M
            while (prev) {
973
273k
                if_debug0m('s', s->memory, "[s]unwinding\n");
974
273k
                MOVE_BACK(curr, prev);
975
273k
                if (status >= 0)
976
0
                    curr->end_status = 0;
977
273k
                else if (status == ERRC)
978
26
                    curr->end_status = ERRC;
979
273k
            }
980
102M
            return status;
981
102M
        }
982
132M
        MOVE_BACK(curr, prev);
983
30.1M
        if (!curr->is_temp)
984
30.1M
            --depth;
985
30.1M
        if_debug1m('s', s->memory, "[s]moving back, depth = %d\n", depth);
986
30.1M
    }
987
102M
}
988
989
/* Move as much data as possible from one buffer to another. */
990
/* Return 0 if the input became empty, 1 if the output became full. */
991
int
992
stream_move(stream_cursor_read * pr, stream_cursor_write * pw)
993
272M
{
994
272M
    uint rcount = pr->limit - pr->ptr;
995
272M
    uint wcount = pw->limit - pw->ptr;
996
272M
    uint count;
997
272M
    int status;
998
999
272M
    if (rcount <= wcount)
1000
121M
        count = rcount, status = 0;
1001
151M
    else
1002
151M
        count = wcount, status = 1;
1003
272M
    memmove(pw->ptr + 1, pr->ptr + 1, count);
1004
272M
    pr->ptr += count;
1005
272M
    pw->ptr += count;
1006
272M
    return status;
1007
272M
}
1008
1009
/* If possible, compact the information in a stream buffer to the bottom. */
1010
static void
1011
stream_compact(stream * s, bool always)
1012
349M
{
1013
349M
    if (s->cbuf != NULL && s->cursor.r.ptr >= s->cbuf
1014
281M
        && (always || s->end_status >= 0)) {
1015
280M
        uint dist = s->cursor.r.ptr + 1 - s->cbuf;
1016
1017
280M
        memmove(s->cbuf, s->cursor.r.ptr + 1,
1018
280M
                (uint) (s->cursor.r.limit - s->cursor.r.ptr));
1019
280M
        s->cursor.r.ptr = s->cbuf - 1;
1020
280M
        s->cursor.r.limit -= dist;  /* same as w.ptr */
1021
280M
        s->position += dist;
1022
280M
    }
1023
349M
}
1024
1025
/* ------ String streams ------ */
1026
1027
/* String stream procedures */
1028
static int
1029
    s_string_available(stream *, gs_offset_t *),
1030
    s_string_read_seek(stream *, gs_offset_t),
1031
    s_string_write_seek(stream *, gs_offset_t),
1032
    s_string_read_process(stream_state *, stream_cursor_read *,
1033
                          stream_cursor_write *, bool),
1034
    s_string_write_process(stream_state *, stream_cursor_read *,
1035
                           stream_cursor_write *, bool);
1036
1037
/* Initialize a stream for reading a string. */
1038
/* String ownership retained by the caller, for example
1039
   Postscript string objects owned by the Postscript
1040
   interpreter
1041
 */
1042
void
1043
sread_string(register stream *s, const byte *ptr, uint len)
1044
16.0M
{
1045
16.0M
    static const stream_procs p = {
1046
16.0M
         s_string_available, s_string_read_seek, s_std_read_reset,
1047
16.0M
         s_std_read_flush, s_std_null, s_string_read_process
1048
16.0M
    };
1049
1050
16.0M
    s_std_init(s, (byte *)ptr, len, &p, s_mode_read + s_mode_seek);
1051
16.0M
    s->cbuf_string.data = (byte *)ptr;
1052
16.0M
    s->cbuf_string.size = len;
1053
16.0M
    s->cbuf_string_memory = NULL;
1054
16.0M
    s->end_status = EOFC;
1055
16.0M
    s->cursor.r.limit = s->cursor.w.limit;
1056
16.0M
}
1057
1058
/* The string ownership is transferred from caller to stream.
1059
   string_mem pointer must be allocator used to allocate the
1060
   "string" buffer.
1061
 */
1062
void
1063
sread_transient_string(register stream *s, gs_memory_t *string_mem, const byte *ptr, uint len)
1064
31.2k
{
1065
31.2k
    static const stream_procs p = {
1066
31.2k
         s_string_available, s_string_read_seek, s_std_read_reset,
1067
31.2k
         s_std_read_flush, s_std_null, s_string_read_process
1068
31.2k
    };
1069
1070
31.2k
    s_std_init(s, (byte *)ptr, len, &p, s_mode_read + s_mode_seek);
1071
31.2k
    s->cbuf_string.data = (byte *)ptr;
1072
31.2k
    s->cbuf_string.size = len;
1073
31.2k
    s->cbuf_string_memory = string_mem;
1074
31.2k
    s->end_status = EOFC;
1075
31.2k
    s->cursor.r.limit = s->cursor.w.limit;
1076
31.2k
}
1077
1078
/* Initialize a reusable stream for reading a string. */
1079
static void
1080
s_string_reusable_reset(stream *s)
1081
0
{
1082
0
    s->cursor.r.ptr = s->cbuf - 1;  /* just reset to the beginning */
1083
0
    s->cursor.r.limit = s->cursor.r.ptr + s->bsize;  /* might have gotten reset */
1084
0
}
1085
static int
1086
s_string_reusable_flush(stream *s)
1087
0
{
1088
0
    s->cursor.r.ptr = s->cursor.r.limit = s->cbuf + s->bsize - 1;  /* just set to the end */
1089
0
    return 0;
1090
0
}
1091
1092
/* String ownership retained by the caller, for example
1093
   Postscript string objects owned by the Postscript
1094
   interpreter
1095
 */
1096
void
1097
sread_string_reusable(stream *s, const byte *ptr, uint len)
1098
55
{
1099
    /*
1100
     * Note that s->procs.close is s_close_disable, to parallel
1101
     * file_close_disable.
1102
     */
1103
55
    static const stream_procs p = {
1104
55
         s_string_available, s_string_read_seek, s_string_reusable_reset,
1105
55
         s_string_reusable_flush, s_close_disable, s_string_read_process
1106
55
    };
1107
1108
55
    sread_string(s, ptr, len);
1109
55
    s->procs = p;
1110
55
    s->close_at_eod = false;
1111
55
}
1112
1113
/* The string ownership is transferred from caller to stream.
1114
   string_mem pointer must be allocator used to allocate the
1115
   "string" buffer.
1116
 */
1117
void
1118
sread_transient_string_reusable(stream *s, gs_memory_t *string_mem, const byte *ptr, uint len)
1119
0
{
1120
    /*
1121
     * Note that s->procs.close is s_close_disable, to parallel
1122
     * file_close_disable.
1123
     */
1124
0
    static const stream_procs p = {
1125
0
         s_string_available, s_string_read_seek, s_string_reusable_reset,
1126
0
         s_string_reusable_flush, s_close_disable, s_string_read_process
1127
0
    };
1128
1129
0
    sread_transient_string(s, string_mem, ptr, len);
1130
0
    s->procs = p;
1131
0
    s->close_at_eod = false;
1132
0
}
1133
1134
/* Return the number of available bytes when reading from a string. */
1135
static int
1136
s_string_available(stream *s, gs_offset_t *pl)
1137
22.6k
{
1138
22.6k
    *pl = sbufavailable(s);
1139
22.6k
    if (*pl == 0 && s->close_at_eod)  /* EOF */
1140
0
        *pl = -1;
1141
22.6k
    return 0;
1142
22.6k
}
1143
1144
/* Seek in a string being read.  Return 0 if OK, ERRC if not. */
1145
static int
1146
s_string_read_seek(register stream * s, gs_offset_t pos)
1147
120k
{
1148
120k
    if (pos < 0 || pos > s->bsize)
1149
55
        return ERRC;
1150
120k
    s->cursor.r.ptr = s->cbuf + pos - 1;
1151
    /* We might be seeking after a reusable string reached EOF. */
1152
120k
    s->cursor.r.limit = s->cbuf + s->bsize - 1;
1153
    /*
1154
     * When the file reaches EOF,
1155
     * stream_compact sets s->position to its end.
1156
     * Reset it now to allow stell to work properly
1157
     * after calls to this function.
1158
     * Note that if the riched EOF and this fuction
1159
     * was not called, stell still returns a wrong value.
1160
     */
1161
120k
    s->position = 0;
1162
120k
    return 0;
1163
120k
}
1164
1165
/* Initialize a stream for writing a string. */
1166
void
1167
swrite_string(register stream * s, byte * ptr, uint len)
1168
12.9M
{
1169
12.9M
    static const stream_procs p = {
1170
12.9M
        s_std_noavailable, s_string_write_seek, s_std_write_reset,
1171
12.9M
        s_std_null, s_std_null, s_string_write_process
1172
12.9M
    };
1173
1174
12.9M
    s_std_init(s, ptr, len, &p, s_mode_write + s_mode_seek);
1175
12.9M
    s->cbuf_string.data = ptr;
1176
12.9M
    s->cbuf_string.size = len;
1177
12.9M
}
1178
1179
/* Seek in a string being written.  Return 0 if OK, ERRC if not. */
1180
static int
1181
s_string_write_seek(register stream * s, gs_offset_t pos)
1182
17
{
1183
17
    if (pos < 0 || pos > s->bsize)
1184
0
        return ERRC;
1185
17
    s->cursor.w.ptr = s->cbuf + pos - 1;
1186
17
    return 0;
1187
17
}
1188
1189
/* Since we initialize the input buffer of a string read stream */
1190
/* to contain all of the data in the string, if we are ever asked */
1191
/* to refill the buffer, we should signal EOF. */
1192
static int
1193
s_string_read_process(stream_state * st, stream_cursor_read * ignore_pr,
1194
                      stream_cursor_write * pw, bool last)
1195
0
{
1196
0
    return EOFC;
1197
0
}
1198
/* Similarly, if we are ever asked to empty the buffer, it means that */
1199
/* there has been an overrun (unless we are closing the stream). */
1200
static int
1201
s_string_write_process(stream_state * st, stream_cursor_read * pr,
1202
                       stream_cursor_write * ignore_pw, bool last)
1203
26
{
1204
26
    return (last ? EOFC : ERRC);
1205
26
}
1206
1207
/* ------ Position-tracking stream ------ */
1208
1209
static int
1210
    s_write_position_process(stream_state *, stream_cursor_read *,
1211
                             stream_cursor_write *, bool);
1212
1213
/* Set up a write stream that just keeps track of the position. */
1214
void
1215
swrite_position_only(stream *s)
1216
6.41M
{
1217
6.41M
    static byte discard_buf[50];  /* size is arbitrary */
1218
1219
6.41M
    swrite_string(s, discard_buf, sizeof(discard_buf));
1220
6.41M
    s->procs.process = s_write_position_process;
1221
6.41M
}
1222
1223
static int
1224
s_write_position_process(stream_state * st, stream_cursor_read * pr,
1225
                         stream_cursor_write * ignore_pw, bool last)
1226
10.2M
{
1227
10.2M
    pr->ptr = pr->limit;  /* discard data */
1228
10.2M
    return 0;
1229
10.2M
}
1230
1231
/* ------ Filter pipelines ------ */
1232
1233
/*
1234
 * Add a filter to an output pipeline.  The client must have allocated the
1235
 * stream state, if any, using the given allocator.  For s_init_filter, the
1236
 * client must have called s_init and s_init_state.
1237
 */
1238
int
1239
s_init_filter(stream *fs, stream_state *fss, byte *buf, uint bsize,
1240
              stream *target)
1241
651k
{
1242
651k
    const stream_template *templat = fss->templat;
1243
1244
651k
    if (bsize < templat->min_in_size)
1245
0
        return ERRC;
1246
651k
    s_std_init(fs, buf, bsize, &s_filter_write_procs, s_mode_write);
1247
651k
    fs->procs.process = templat->process;
1248
651k
    fs->state = fss;
1249
651k
    if (templat->init) {
1250
496k
        fs->end_status = (templat->init)(fss);
1251
496k
        if (fs->end_status < 0)
1252
0
            return fs->end_status;
1253
496k
    }
1254
651k
    fs->strm = target;
1255
651k
    return 0;
1256
651k
}
1257
stream *
1258
s_add_filter(stream **ps, const stream_template *templat,
1259
             stream_state *ss, gs_memory_t *mem)
1260
391k
{
1261
391k
    stream *es;
1262
391k
    stream_state *ess;
1263
391k
    uint bsize = max(templat->min_in_size, 256);  /* arbitrary */
1264
391k
    byte *buf;
1265
1266
    /*
1267
     * Ensure enough buffering.  This may require adding an additional
1268
     * stream.
1269
     */
1270
391k
    if (bsize > (*ps)->bsize && templat->process != s_NullE_template.process) {
1271
146k
        stream_template null_template;
1272
1273
146k
        null_template = s_NullE_template;
1274
146k
        null_template.min_in_size = bsize;
1275
146k
        if (s_add_filter(ps, &null_template, NULL, mem) == 0)
1276
0
            return 0;
1277
146k
    }
1278
391k
    es = s_alloc(mem, "s_add_filter(stream)");
1279
391k
    buf = gs_alloc_bytes(mem, bsize, "s_add_filter(buf)");
1280
391k
    if (es == 0 || buf == 0) {
1281
0
        gs_free_object(mem, buf, "s_add_filter(buf)");
1282
0
        gs_free_object(mem, es, "s_add_filter(stream)");
1283
0
        return 0;
1284
0
    }
1285
391k
    ess = (ss == 0 ? (stream_state *)es : ss);
1286
391k
    ess->templat = templat;
1287
391k
    ess->memory = mem;
1288
391k
    es->memory = mem;
1289
391k
    if (s_init_filter(es, ess, buf, bsize, *ps) < 0) {
1290
0
        gs_free_object(mem, buf, "s_add_filter(buf)");
1291
0
        gs_free_object(mem, es, "s_add_filter(stream)");
1292
0
        return 0;
1293
0
    }
1294
391k
    *ps = es;
1295
391k
    return es;
1296
391k
}
1297
1298
/*
1299
 * Close the filters in a pipeline, up to a given target stream, freeing
1300
 * their buffers and state structures.
1301
 */
1302
int
1303
s_close_filters(stream **ps, stream *target)
1304
726k
{
1305
726k
    int code = 0;
1306
1.67M
    while (*ps != target) {
1307
952k
        stream *s = *ps;
1308
952k
        gs_memory_t *mem = s->state->memory;
1309
952k
        gs_memory_t *cbuf_string_memory = s->cbuf_string_memory;
1310
952k
        byte *sbuf = s->cbuf;
1311
952k
        byte *cbuf = s->cbuf_string.data;
1312
952k
        stream *next = s->strm;
1313
952k
        int status = sclose(s);
1314
952k
        stream_state *ss = s->state; /* sclose may set this to s */
1315
1316
952k
        if (code == 0)
1317
950k
            code = status;
1318
1319
952k
        if (s->cbuf_string_memory != NULL) { /* stream owns string buffer, so free it */
1320
19.1k
            gs_free_object(cbuf_string_memory, cbuf, "s_close_filters(cbuf)");
1321
19.1k
        }
1322
1323
952k
        if (mem) {
1324
935k
            if (sbuf != cbuf)
1325
916k
                gs_free_object(mem, sbuf, "s_close_filters(buf)");
1326
935k
            gs_free_object(mem, s, "s_close_filters(stream)");
1327
935k
            if (ss != (stream_state *)s)
1328
1.43k
                gs_free_object(mem, ss, "s_close_filters(state)");
1329
935k
        }
1330
952k
        *ps = next;
1331
952k
    }
1332
726k
    return code;
1333
726k
}
1334
1335
/* ------ Stream closing ------ */
1336
1337
/*
1338
 * Finish closing a file stream.  This used to check whether it was
1339
 * currentfile, but we don't have to do this any longer.  This replaces the
1340
 * close procedure for the std* streams, which cannot actually be closed.
1341
 *
1342
 * This is exported for ziodev.c.  */
1343
int
1344
file_close_finish(stream * s)
1345
7.04M
{
1346
7.04M
    return 0;
1347
7.04M
}
1348
1349
/*
1350
 * Close a file stream, but don't deallocate the buffer.  This replaces the
1351
 * close procedure for %lineedit and %statementedit.  (This is WRONG: these
1352
 * streams should allocate a new buffer each time they are opened, but that
1353
 * would overstress the allocator right now.)  This is exported for ziodev.c.
1354
 * This also replaces the close procedure for the string-reading streams
1355
 * created for gs_run_string and for reusable streams.
1356
 */
1357
int
1358
s_close_disable(stream *s)
1359
7.04M
{
1360
    /* Increment the IDs to prevent further access. */
1361
7.04M
    s->read_id = s->write_id = (s->read_id | s->write_id) + 1;
1362
7.04M
    return 0;
1363
7.04M
}
1364
int
1365
file_close_disable(stream * s)
1366
7.24M
{
1367
7.24M
    int code;
1368
1369
7.24M
    if ((*s->save_close != NULL) && ((code = (*s->save_close)(s)) != 0))
1370
199k
        return code;
1371
7.04M
    s_close_disable(s);
1372
7.04M
    return file_close_finish(s);
1373
7.24M
}
1374
1375
/* ------ NullEncode/Decode ------ */
1376
1377
/* Process a buffer */
1378
static int
1379
s_Null_process(stream_state * st, stream_cursor_read * pr,
1380
               stream_cursor_write * pw, bool last)
1381
8.31M
{
1382
8.31M
    return stream_move(pr, pw);
1383
8.31M
}
1384
1385
/* Stream template */
1386
const stream_template s_NullE_template = {
1387
    &st_stream_state, NULL, s_Null_process, 1, 1
1388
};
1389
const stream_template s_NullD_template = {
1390
    &st_stream_state, NULL, s_Null_process, 1, 1
1391
};