Coverage Report

Created: 2022-10-31 07:00

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