summaryrefslogtreecommitdiff
blob: 30648bd5d0a0e245d5dc889161c15520c97c3db9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
/* Copyright (C) 2001-2020 Artifex Software, Inc.
   All Rights Reserved.

   This software is provided AS-IS with no warranty, either express or
   implied.

   This software is distributed under license and may not be copied,
   modified or distributed except as expressly authorized under the terms
   of the license contained in the file LICENSE in this distribution.

   Refer to licensing information at http://www.artifex.com or contact
   Artifex Software, Inc.,  1305 Grant Avenue - Suite 200, Novato,
   CA 94945, U.S.A., +1(415)492-9861, for further information.
*/


/* Stream package for Ghostscript interpreter */
#include "stdio_.h"		/* includes std.h */
#include "memory_.h"
#include "gdebug.h"
#include "gpcheck.h"
#include "stream.h"
#include "strimpl.h"

/* Forward declarations */
int s_close_disable(stream *);
static int sreadbuf(stream *, stream_cursor_write *);
static int swritebuf(stream *, stream_cursor_read *, bool);
static void stream_compact(stream *, bool);

/* Structure types for allocating streams. */
public_st_stream();
public_st_stream_state();	/* default */
/* GC procedures */
static
ENUM_PTRS_WITH(stream_enum_ptrs, stream *st) return 0;
case 0:
if (st->foreign)
    ENUM_RETURN(NULL);
else if (st->cbuf_string.data != 0)
    ENUM_RETURN_STRING_PTR(stream, cbuf_string);
else
    ENUM_RETURN(st->cbuf);
ENUM_PTR3(1, stream, strm, prev, next);
ENUM_PTR(4, stream, state);
case 5: return ENUM_CONST_STRING(&st->file_name);
ENUM_PTRS_END
static RELOC_PTRS_WITH(stream_reloc_ptrs, stream *st)
{
    byte *cbuf_old = st->cbuf;

    if (cbuf_old != 0 && !st->foreign) {
        long reloc;

        if (st->cbuf_string.data != 0) {
            RELOC_STRING_VAR(st->cbuf_string);
            st->cbuf = st->cbuf_string.data;
        } else
            RELOC_VAR(st->cbuf);
        reloc = cbuf_old - st->cbuf;
        /* Relocate the other buffer pointers. */
        st->cursor.r.ptr -= reloc;
        st->cursor.r.limit -= reloc;	/* same as swptr */
        st->cursor.w.limit -= reloc;
    }
    RELOC_VAR(st->strm);
    RELOC_VAR(st->prev);
    RELOC_VAR(st->next);
    RELOC_VAR(st->state);
    RELOC_CONST_STRING_VAR(st->file_name);
}
RELOC_PTRS_END
/* Finalize a stream by closing it. */
/* We only do this for file streams, because other kinds of streams */
/* may attempt to free storage when closing. */
static void
stream_finalize(const gs_memory_t *cmem, void *vptr)
{
    stream *const st = vptr;
    (void)cmem; /* unused */

    if_debug2m('u', st->memory, "[u]%s "PRI_INTPTR"\n",
               (!s_is_valid(st) ? "already closed:" :
                st->is_temp ? "is_temp set:" :
                st->file == 0 ? "not file:" :
                "closing file:"), (intptr_t) st);
    if (s_is_valid(st) && !st->is_temp && st->file != 0) {
        /* Prevent any attempt to free the buffer. */
        st->cbuf = 0;
        st->cbuf_string.data = 0;
        sclose(st);		/* ignore errors */
    }
}

/* Dummy template for streams that don't have a separate state. */
static const stream_template s_no_template = {
    &st_stream_state, 0, 0, 1, 1, 0
};

/* ------ Generic procedures ------ */

/* Allocate a stream and initialize it minimally. */
void
s_init(stream *s, gs_memory_t * mem)
{
    s->memory = mem;
    s->report_error = s_no_report_error;
    s->min_left = 0;
    s->error_string[0] = 0;
    s->prev = s->next = 0;	/* clean for GC */
    s->file_name.data = 0;	/* ibid. */
    s->file_name.size = 0;
    s->close_strm = false;	/* default */
    s->close_at_eod = true;	/* default */
}
stream *
s_alloc(gs_memory_t * mem, client_name_t cname)
{
    stream *s = gs_alloc_struct(mem, stream, &st_stream, cname);

    if_debug2m('s', mem, "[s]alloc(%s) = "PRI_INTPTR"\n",
               client_name_string(cname), (intptr_t) s);
    if (s == 0)
        return 0;
    s_init(s, mem);
    return s;
}

/* Allocate a stream state and initialize it minimally. */
void
s_init_state(stream_state *st, const stream_template *templat,
             gs_memory_t *mem)
{
    st->templat = templat;
    st->memory = mem;
    st->report_error = s_no_report_error;
    st->min_left = 0;
    st->error_string[0] = 0;
}
stream_state *
s_alloc_state(gs_memory_t * mem, gs_memory_type_ptr_t stype,
              client_name_t cname)
{
    stream_state *st = gs_alloc_struct(mem, stream_state, stype, cname);

    if_debug3m('s', mem, "[s]alloc_state %s(%s) = "PRI_INTPTR"\n",
               client_name_string(cname),
               client_name_string(stype->sname),
               (intptr_t) st);
    if (st)
        s_init_state(st, NULL, mem);
    return st;
}

/* Standard stream initialization */
void
s_std_init(register stream * s, byte * ptr, uint len, const stream_procs * pp,
           int modes)
{
    s->templat = &s_no_template;
    s->cbuf = ptr;
    s->cursor.r.ptr = s->cursor.r.limit = s->cursor.w.ptr = ptr - 1;
    s->cursor.w.limit = ptr - 1 + len;
    s->end_status = 0;
    s->foreign = 0;
    s->modes = modes;
    s->cbuf_string.data = 0;
    s->position = 0;
    s->bsize = s->cbsize = len;
    s->strm = 0;		/* not a filter */
    s->is_temp = 0;
    s->procs = *pp;
    s->state = (stream_state *) s;	/* hack to avoid separate state */
    s->file = 0;
    s->file_name.data = 0;	/* in case stream is on stack */
    s->file_name.size = 0;
    if (s->memory) {
        if_debug4m('s', s->memory, "[s]init "PRI_INTPTR", buf="PRI_INTPTR", len=%u, modes=%d\n",
                   (intptr_t) s, (intptr_t) ptr, len, modes);
    }
}

/* Set the file name of a stream, copying the name. */
/* Return <0 if the copy could not be allocated. */
int
ssetfilename(stream *s, const byte *data, uint size)
{
    byte *str =
        (s->file_name.data == 0 ?
         gs_alloc_string(s->memory, size + 1, "ssetfilename") :
         gs_resize_string(s->memory,
                          (byte *)s->file_name.data,	/* break const */
                          s->file_name.size,
                          size + 1, "ssetfilename"));

    if (str == 0)
        return -1;
    memcpy(str, data, size);
    str[size] = 0;
    s->file_name.data = str;
    s->file_name.size = size + 1;
    return 0;
}

/* Return the file name of a stream, if any. */
/* There is a guaranteed 0 byte after the string. */
int
sfilename(stream *s, gs_const_string *pfname)
{
    pfname->data = s->file_name.data;
    if (pfname->data == 0) {
        pfname->size = 0;
        return -1;
    }
    pfname->size = s->file_name.size - 1; /* omit terminator */
    return 0;
}

/* Implement a stream procedure as a no-op. */
int
s_std_null(stream * s)
{
    return 0;
}

/* Discard the contents of the buffer when reading. */
void
s_std_read_reset(stream * s)
{
    s->cursor.r.ptr = s->cursor.r.limit = s->cbuf - 1;
}

/* Discard the contents of the buffer when writing. */
void
s_std_write_reset(stream * s)
{
    s->cursor.w.ptr = s->cbuf - 1;
}

/* Flush data to end-of-file when reading. */
int
s_std_read_flush(stream * s)
{
    while (1) {
        s->cursor.r.ptr = s->cursor.r.limit = s->cbuf - 1;
        if (s->end_status)
            break;
        s_process_read_buf(s);
    }
    return (s->end_status == EOFC ? 0 : s->end_status);
}

/* Flush buffered data when writing. */
int
s_std_write_flush(stream * s)
{
    return s_process_write_buf(s, false);
}

/* Indicate that the number of available input bytes is unknown. */
int
s_std_noavailable(stream * s, gs_offset_t *pl)
{
    *pl = -1;
    return 0;
}

/* Indicate an error when asked to seek. */
int
s_std_noseek(stream * s, gs_offset_t pos)
{
    return ERRC;
}

/* Standard stream closing. */
int
s_std_close(stream * s)
{
    return 0;
}

/* Standard stream mode switching. */
int
s_std_switch_mode(stream * s, bool writing)
{
    return ERRC;
}

/* Standard stream finalization.  Disable the stream. */
void
s_disable(register stream * s)
{
    s->cbuf = 0;
    s->bsize = 0;
    s->end_status = EOFC;
    s->modes = 0;
    s->cbuf_string.data = 0;
    /* The pointers in the next two statements should really be */
    /* initialized to ([const] byte *)0 - 1, but some very picky */
    /* compilers complain about this. */
    s->cursor.r.ptr = s->cursor.r.limit = 0;
    s->cursor.w.limit = 0;
    s->procs.close = s_std_null;
    /* Clear pointers for GC */
    s->strm = 0;
    s->state = (stream_state *) s;
    s->templat = &s_no_template;
    /* Free the file name. */
    if (s->file_name.data) {
        if (s->memory) {
            gs_free_const_string(s->memory, s->file_name.data, s->file_name.size,
                                 "s_disable(file_name)");
        }
        s->file_name.data = 0;
        s->file_name.size = 0;
    }
    /****** SHOULD DO MORE THAN THIS ******/
    if (s->memory) {
        if_debug1m('s', s->memory, "[s]disable "PRI_INTPTR"\n", (intptr_t) s);
    }
}

/* Implement flushing for encoding filters. */
int
s_filter_write_flush(register stream * s)
{
    int status = s_process_write_buf(s, false);

    if (status != 0)
        return status;
    return sflush(s->strm);
}

/* Close a filter.  If this is an encoding filter, flush it first. */
/* If CloseTarget was specified (close_strm), then propagate the sclose */
int
s_filter_close(register stream * s)
{
    int status;
    bool close = s->close_strm;
    stream *stemp = s->strm;

    if (s_is_writing(s)) {
        int status = s_process_write_buf(s, true);

        if (status != 0 && status != EOFC)
            return status;
        status = sflush(stemp);
        if (status != 0 && status != EOFC)
            return status;
    }
    status = s_std_close(s);
    if (status != 0 && status != EOFC)
        return status;
    if (close && stemp != 0)
        return sclose(stemp);
    return status;
}

/* Disregard a stream error message. */
int
s_no_report_error(stream_state * st, const char *str)
{
    return 0;
}

/* Generic procedure structures for filters. */

const stream_procs s_filter_read_procs = {
    s_std_noavailable, s_std_noseek, s_std_read_reset,
    s_std_read_flush, s_filter_close
};

const stream_procs s_filter_write_procs = {
    s_std_noavailable, s_std_noseek, s_std_write_reset,
    s_filter_write_flush, s_filter_close
};

/* ------ Implementation-independent procedures ------ */

/* Store the amount of available data in a(n input) stream. */
int
savailable(stream * s, gs_offset_t *pl)
{
    return (*(s)->procs.available) (s, pl);
}

/* Return the current position of a stream. */
gs_offset_t
stell(stream * s)
{
    /*
     * The stream might have been closed, but the position
     * is still meaningful in this case.
     */
    const byte *ptr = (s_is_writing(s) ? s->cursor.w.ptr : s->cursor.r.ptr);

    return (ptr == 0 ? 0 : ptr + 1 - s->cbuf) + s->position;
}

/* Set the position of a stream. */
int
spseek(stream * s, gs_offset_t pos)
{
    if_debug3m('s', s->memory, "[s]seek 0x%"PRIx64" to %"PRId64", position was %"PRId64"\n",
                (uint64_t)s, (int64_t)pos, (int64_t)stell(s));
    return (*(s)->procs.seek) (s, pos);
}

/* Switch a stream to read or write mode. */
/* Return 0 or ERRC. */
int
sswitch(register stream * s, bool writing)
{
    if (s->procs.switch_mode == 0)
        return ERRC;
    return (*s->procs.switch_mode) (s, writing);
}

/* Close a stream, disabling it if successful. */
/* (The stream may already be closed.) */
int
sclose(register stream * s)
{
    stream_state *st;
    int status = (*s->procs.close) (s);

    if (status < 0)
        return status;
    st = s->state;
    if (st != 0) {
        stream_proc_release((*release)) = st->templat->release;
        if (release != 0)
            (*release) (st);
        if (st != (stream_state *) s && st->memory != 0) {
            gs_memory_t *mem = st->memory;
            st->memory = NULL;
            gs_free_object(mem, st, "s_std_close");
        }
        s->state = (stream_state *) s;
    }
    s_disable(s);
    return status;
}

/*
 * Implement sgetc when the buffer may be empty.  If the buffer really is
 * empty, refill it and then read a byte.  Note that filters must read one
 * byte ahead, so that they can close immediately after the client reads the
 * last data byte if the next thing is an EOD.
 */
int
spgetcc(register stream * s, bool close_at_eod)
{
    int status, left;
    int min_left = sbuf_min_left(s);

    while (status = s->end_status,
           left = s->cursor.r.limit - s->cursor.r.ptr,
           left <= min_left && status >= 0
        )
        s_process_read_buf(s);
    if (left <= min_left &&
        (left <= 0 || (status != EOFC && status != ERRC))
        ) {
        /* Compact the stream so stell will return the right result. */
        if (left == 0)
            stream_compact(s, true);
        if (status == EOFC && close_at_eod && s->close_at_eod) {
            status = sclose(s);
            if (status == 0)
                status = EOFC;
            s->end_status = status;
        }
        return status;
    }
    return *++(s->cursor.r.ptr);
}

/* Implementing sputc when the buffer is full, */
/* by flushing the buffer and then writing the byte. */
int
spputc(register stream * s, byte b)
{
    for (;;) {
        if (s->end_status)
            return s->end_status;
        if (!sendwp(s)) {
            *++(s->cursor.w.ptr) = b;
            return b;
        }
        s_process_write_buf(s, false);
    }
}

/* Push back a character onto a (read) stream. */
/* The character must be the same as the last one read. */
/* Return 0 on success, ERRC on failure. */
int
sungetc(register stream * s, byte c)
{
    /* cbuf == NULL means this stream is stdin, and we shouldn't
       unread from stdin, ever.
     */
    if (s->cbuf == NULL || !s_is_reading(s) ||
        s->cursor.r.ptr < s->cbuf || *(s->cursor.r.ptr) != c)
        return ERRC;
    s->cursor.r.ptr--;
    return 0;
}

/* Get a string from a stream. */
/* Return 0 if the string was filled, or an exception status. */
int
sgets(stream * s, byte * buf, uint nmax, uint * pn)
{
    stream_cursor_write cw;
    int status = 0;
    gs_offset_t min_left = sbuf_min_left(s);

    cw.ptr = buf - 1;
    cw.limit = cw.ptr + nmax;
    while (cw.ptr < cw.limit) {
        int left;

        if ((left = s->cursor.r.limit - s->cursor.r.ptr) > min_left) {
            s->cursor.r.limit -= min_left;
            stream_move(&s->cursor.r, &cw);
            s->cursor.r.limit += min_left;
        } else {
            uint wanted = cw.limit - cw.ptr;
            int c;
            stream_state *st;

            if (wanted >= s->bsize >> 2 &&
                (st = s->state) != 0 &&
                wanted >= st->templat->min_out_size &&
                s->end_status == 0 &&
                left == 0
                ) {
                byte *wptr = cw.ptr;

                cw.limit -= min_left;
                status = sreadbuf(s, &cw);
                cw.limit += min_left;
                /* Compact the stream so stell will return the right result. */
                stream_compact(s, true);
                /*
                 * We know the stream buffer is empty, so it's safe to
                 * update position.  However, we need to reset the read
                 * cursor to indicate that there is no data in the buffer.
                 */
                s->cursor.r.ptr = s->cursor.r.limit = s->cbuf - 1;
                s->position += cw.ptr - wptr;
                if (status <= 0 || cw.ptr == cw.limit)
                    break;
            }
            c = spgetc(s);
            if (c < 0) {
                status = c;
                break;
            }
            *++(cw.ptr) = c;
        }
    }
    *pn = cw.ptr + 1 - buf;
    return (status >= 0 ? 0 : status);
}

/* Write a string on a stream. */
/* Return 0 if the entire string was written, or an exception status. */
int
sputs(register stream * s, const byte * str, uint wlen, uint * pn)
{
    uint len = wlen;
    int status = s->end_status;

    if (status >= 0)
        while (len > 0) {
            uint count = s->cursor.w.limit - s->cursor.w.ptr;

            if (count > 0) {
                if (count > len)
                    count = len;
                memcpy(s->cursor.w.ptr + 1, str, count);
                s->cursor.w.ptr += count;
                str += count;
                len -= count;
            } else {
                byte ch = *str++;

                status = sputc(s, ch);
                if (status < 0)
                    break;
                len--;
            }
        }
    *pn = wlen - len;
    return (status >= 0 ? 0 : status);
}

/* Skip ahead a specified distance in a read stream. */
/* Return 0 or an exception status. */
/* Store the number of bytes skipped in *pskipped. */
int
spskip(register stream * s, gs_offset_t nskip, gs_offset_t *pskipped)
{
    gs_offset_t n = nskip;
    gs_offset_t min_left;

    if (nskip < 0 || !s_is_reading(s)) {
        *pskipped = 0;
        return ERRC;
    }
    if (s_can_seek(s)) {
        gs_offset_t pos = stell(s);
        int status = sseek(s, pos + n);

        *pskipped = stell(s) - pos;
        return status;
    }
    min_left = sbuf_min_left(s);
    while (sbufavailable(s) < n + min_left) {
        int status;

        n -= sbufavailable(s);
        s->cursor.r.ptr = s->cursor.r.limit;
        if (s->end_status) {
            *pskipped = nskip - n;
            return s->end_status;
        }
        status = sgetc(s);
        if (status < 0) {
            *pskipped = nskip - n;
            return status;
        }
        --n;
    }
    /* Note that if min_left > 0, n < 0 is possible; this is harmless. */
    s->cursor.r.ptr += n;
    *pskipped = nskip;
    return 0;
}

/* Read a line from a stream.  See srdline.h for the specification. */
int
sreadline(stream *s_in, stream *s_out, void *readline_data,
          gs_const_string *prompt, gs_string * buf,
          gs_memory_t * bufmem, uint * pcount, bool *pin_eol,
          bool (*is_stdin)(const stream *))
{
    uint count = *pcount;

    /* Most systems define \n as 0xa and \r as 0xd; however, */
    /* OS-9 has \n == \r == 0xd and \l == 0xa.  The following */
    /* code works properly regardless of environment. */
#if '\n' == '\r'
#  define LF 0xa
#else
#  define LF '\n'
#endif

    if (count == 0 && s_out && prompt) {
        uint ignore_n;
        int ch = sputs(s_out, prompt->data, prompt->size, &ignore_n);

        if (ch < 0)
            return ch;
    }

top:
    if (*pin_eol) {
        /*
         * We're in the middle of checking for a two-character
         * end-of-line sequence.  If we get an EOF here, stop, but
         * don't signal EOF now; wait till the next read.
         */
        int ch = spgetcc(s_in, false);

        if (ch == EOFC) {
            *pin_eol = false;
            return 0;
        } else if (ch < 0)
            return ch;
        else if (ch != LF)
            sputback(s_in);
        *pin_eol = false;
        return 0;
    }
    for (;;) {
        int ch = sgetc(s_in);

        if (ch < 0) {		/* EOF or exception */
            *pcount = count;
            return ch;
        }
        switch (ch) {
            case '\r':
                {
#if '\n' == '\r'		/* OS-9 or similar */
                    if (!is_stdin(s_in))
#endif
                    {
                        *pcount = count;
                        *pin_eol = true;
                        goto top;
                    }
                }
                /* falls through */
            case LF:
#undef LF
                *pcount = count;
                return 0;
        }
        if (count >= buf->size) {	/* filled the string */
            if (!bufmem) {
                sputback(s_in);
                *pcount = count;
                return 1;
            }
            {
                uint nsize = count + max(count, 20);
                byte *ndata = gs_resize_string(bufmem, buf->data, buf->size,
                                               nsize, "sreadline(buffer)");

                if (ndata == 0)
                    return ERRC; /* no better choice */
                buf->data = ndata;
                buf->size = nsize;
            }
        }
        buf->data[count++] = ch;
    }
    /*return 0; *//* not reached */
}

/* ------ Utilities ------ */

/*
 * Attempt to refill the buffer of a read stream.  Only call this if the
 * end_status is not EOFC, and if the buffer is (nearly) empty.
 */
int
s_process_read_buf(stream * s)
{
    int status;

    stream_compact(s, false);
    status = sreadbuf(s, &s->cursor.w);
    s->end_status = (status >= 0 ? 0 : status);
    return 0;
}

/*
 * Attempt to empty the buffer of a write stream.  Only call this if the
 * end_status is not EOFC.
 */
int
s_process_write_buf(stream * s, bool last)
{
    int status = swritebuf(s, &s->cursor.r, last);

    stream_compact(s, false);
    return (status >= 0 ? 0 : status);
}

/* Move forward or backward in a pipeline.  We temporarily reverse */
/* the direction of the pointers while doing this. */
/* (Cf the Deutsch-Schorr-Waite graph marking algorithm.) */
#define MOVE_BACK(curr, prev)\
  BEGIN\
    stream *back = prev->strm;\
    prev->strm = curr; curr = prev; prev = back;\
  END
#define MOVE_AHEAD(curr, prev)\
  BEGIN\
    stream *ahead = curr->strm;\
    curr->strm = prev; prev = curr; curr = ahead;\
  END

/*
 * Read from a stream pipeline.  Update end_status for all streams that were
 * actually touched.  Return the status from the outermost stream: this is
 * normally the same as s->end_status, except that if s->procs.process
 * returned 1, sreadbuf sets s->end_status to 0, but returns 1.
 */
static int
sreadbuf(stream * s, stream_cursor_write * pbuf)
{
    stream *prev = 0;
    stream *curr = s;
    int status;

    for (;;) {
        stream *strm;
        stream_cursor_write *pw;
        byte *oldpos;

        for (;;) {		/* Descend into the recursion. */
            stream_cursor_read cr;
            stream_cursor_read *pr;
            int left;
            bool eof;

            strm = curr->strm;
            if (strm == 0) {
                cr.ptr = 0, cr.limit = 0;
                pr = &cr;
                left = 0;
                eof = false;
            } else {
                pr = &strm->cursor.r;
                left = sbuf_min_left(strm);
                left = min(left, pr->limit - pr->ptr);
                pr->limit -= left;
                eof = strm->end_status == EOFC;
            }
            pw = (prev == 0 ? pbuf : &curr->cursor.w);
            if_debug4m('s', s->memory, "[s]read process "PRI_INTPTR", nr=%u, nw=%u, eof=%d\n",
                       (intptr_t) curr, (uint) (pr->limit - pr->ptr),
                       (uint) (pw->limit - pw->ptr), eof);
            oldpos = pw->ptr;
            status = (*curr->procs.process) (curr->state, pr, pw, eof);
            pr->limit += left;
            if_debug5m('s', s->memory, "[s]after read "PRI_INTPTR", nr=%u, nw=%u, status=%d, position=%"PRId64"\n",
                       (intptr_t) curr, (uint) (pr->limit - pr->ptr),
                       (uint) (pw->limit - pw->ptr), status, s->position);
            if (strm == 0 || status != 0)
                break;
            if (strm->end_status < 0) {
                if (strm->end_status != EOFC || pw->ptr == oldpos)
                    status = strm->end_status;
                break;
            }
            MOVE_AHEAD(curr, prev);
            stream_compact(curr, false);
        }
        /* If curr reached EOD and is a filter or file stream, close it
         * if it is the last filter in the pipeline. Closing the last filter
         * seems to contradict PLRM3 but matches Adobe interpreters.
         */
        if ((strm != 0 || curr->file) && status == EOFC &&
            curr->cursor.r.ptr >= curr->cursor.r.limit &&
            curr->close_at_eod &&
            prev == 0
            ) {
            int cstat = sclose(curr);

            if (cstat != 0)
                status = cstat;
        }
        /* Unwind from the recursion. */
        curr->end_status = (status >= 0 ? 0 : status);
        if (prev == 0)
            return status;
        MOVE_BACK(curr, prev);
    }
}

/* Write to a pipeline. */
static int
swritebuf(stream * s, stream_cursor_read * pbuf, bool last)
{
    stream *prev = 0;
    stream *curr = s;
    int depth = 0;		/* # of non-temp streams before curr */
    int status;

    /*
     * The handling of EOFC is a little tricky.  There are two
     * invariants that keep it straight:
     *      - We only pass last = true to a stream if either it is
     * the first stream in the pipeline, or it is a temporary stream
     * below the first stream and the stream immediately above it has
     * end_status = EOFC.
     */
    for (;;) {
        for (;;) {
            /* Move ahead in the pipeline. */
            stream *strm = curr->strm;
            stream_cursor_write cw;
            stream_cursor_read *pr;
            stream_cursor_write *pw;

            /*
             * We only want to set the last/end flag for
             * the top-level stream and any temporary streams
             * immediately below it.
             */
            bool end = last &&
                (prev == 0 ||
                 (depth <= 1 && prev->end_status == EOFC));

            if (strm == 0)
                cw.ptr = 0, cw.limit = 0, pw = &cw;
            else
                pw = &strm->cursor.w;
            if (prev == 0)
                pr = pbuf;
            else
                pr = &curr->cursor.r;
            if_debug5m('s', s->memory,
                       "[s]write process "PRI_INTPTR"(%s), nr=%u, nw=%u, end=%d\n",
                       (intptr_t)curr,
                       gs_struct_type_name(curr->state->templat->stype),
                       (uint)(pr->limit - pr->ptr),
                       (uint)(pw->limit - pw->ptr), end);
            status = curr->end_status;
            if (status >= 0) {
                status = (*curr->procs.process)(curr->state, pr, pw, end);
                if_debug5m('s', s->memory,
                           "[s]after write "PRI_INTPTR", nr=%u, nw=%u, end=%d, status=%d\n",
                           (intptr_t) curr, (uint) (pr->limit - pr->ptr),
                           (uint) (pw->limit - pw->ptr), end, status);
                if (status == 0 && end)
                    status = EOFC;
                if (status == EOFC || status == ERRC)
                    curr->end_status = status;
            }
            if (strm == 0 || (status < 0 && status != EOFC))
                break;
            if (status != 1) {
                /*
                 * Keep going if we are closing a filter with a sub-stream.
                 * We know status == 0 or EOFC.
                 */
                if (!end || !strm->is_temp)
                    break;
            }
            status = strm->end_status;
            if (status < 0 && (status != EOFC || !end))
                break;
            if (!curr->is_temp)
                ++depth;
            if_debug1m('s', strm->memory, "[s]moving ahead, depth = %d\n", depth);
            MOVE_AHEAD(curr, prev);
            stream_compact(curr, false);
        }
        /* Move back in the pipeline. */
        curr->end_status = (status >= 0 ? 0 : status);
        if (status < 0 || prev == 0) {
            /*
             * All streams up to here were called with last = true
             * and returned 0 or EOFC (so their end_status is now EOFC):
             * finish unwinding and then return.  Change the status of
             * the prior streams to ERRC if the new status is ERRC,
             * otherwise leave it alone.
             */
            while (prev) {
                if_debug0m('s', s->memory, "[s]unwinding\n");
                MOVE_BACK(curr, prev);
                if (status >= 0)
                    curr->end_status = 0;
                else if (status == ERRC)
                    curr->end_status = ERRC;
            }
            return status;
        }
        MOVE_BACK(curr, prev);
        if (!curr->is_temp)
            --depth;
        if_debug1m('s', s->memory, "[s]moving back, depth = %d\n", depth);
    }
}

/* Move as much data as possible from one buffer to another. */
/* Return 0 if the input became empty, 1 if the output became full. */
int
stream_move(stream_cursor_read * pr, stream_cursor_write * pw)
{
    uint rcount = pr->limit - pr->ptr;
    uint wcount = pw->limit - pw->ptr;
    uint count;
    int status;

    if (rcount <= wcount)
        count = rcount, status = 0;
    else
        count = wcount, status = 1;
    memmove(pw->ptr + 1, pr->ptr + 1, count);
    pr->ptr += count;
    pw->ptr += count;
    return status;
}

/* If possible, compact the information in a stream buffer to the bottom. */
static void
stream_compact(stream * s, bool always)
{
    if (s->cursor.r.ptr >= s->cbuf && (always || s->end_status >= 0)) {
        uint dist = s->cursor.r.ptr + 1 - s->cbuf;

        memmove(s->cbuf, s->cursor.r.ptr + 1,
                (uint) (s->cursor.r.limit - s->cursor.r.ptr));
        s->cursor.r.ptr = s->cbuf - 1;
        s->cursor.r.limit -= dist;	/* same as w.ptr */
        s->position += dist;
    }
}

/* ------ String streams ------ */

/* String stream procedures */
static int
    s_string_available(stream *, gs_offset_t *),
    s_string_read_seek(stream *, gs_offset_t),
    s_string_write_seek(stream *, gs_offset_t),
    s_string_read_process(stream_state *, stream_cursor_read *,
                          stream_cursor_write *, bool),
    s_string_write_process(stream_state *, stream_cursor_read *,
                           stream_cursor_write *, bool);

/* Initialize a stream for reading a string. */
void
sread_string(register stream *s, const byte *ptr, uint len)
{
    static const stream_procs p = {
         s_string_available, s_string_read_seek, s_std_read_reset,
         s_std_read_flush, s_std_null, s_string_read_process
    };

    s_std_init(s, (byte *)ptr, len, &p, s_mode_read + s_mode_seek);
    s->cbuf_string.data = (byte *)ptr;
    s->cbuf_string.size = len;
    s->end_status = EOFC;
    s->cursor.r.limit = s->cursor.w.limit;
}
/* Initialize a reusable stream for reading a string. */
static void
s_string_reusable_reset(stream *s)
{
    s->cursor.r.ptr = s->cbuf - 1;	/* just reset to the beginning */
    s->cursor.r.limit = s->cursor.r.ptr + s->bsize;  /* might have gotten reset */
}
static int
s_string_reusable_flush(stream *s)
{
    s->cursor.r.ptr = s->cursor.r.limit = s->cbuf + s->bsize - 1;  /* just set to the end */
    return 0;
}
void
sread_string_reusable(stream *s, const byte *ptr, uint len)
{
    /*
     * Note that s->procs.close is s_close_disable, to parallel
     * file_close_disable.
     */
    static const stream_procs p = {
         s_string_available, s_string_read_seek, s_string_reusable_reset,
         s_string_reusable_flush, s_close_disable, s_string_read_process
    };

    sread_string(s, ptr, len);
    s->procs = p;
    s->close_at_eod = false;
}

/* Return the number of available bytes when reading from a string. */
static int
s_string_available(stream *s, gs_offset_t *pl)
{
    *pl = sbufavailable(s);
    if (*pl == 0 && s->close_at_eod)	/* EOF */
        *pl = -1;
    return 0;
}

/* Seek in a string being read.  Return 0 if OK, ERRC if not. */
static int
s_string_read_seek(register stream * s, gs_offset_t pos)
{
    if (pos < 0 || pos > s->bsize)
        return ERRC;
    s->cursor.r.ptr = s->cbuf + pos - 1;
    /* We might be seeking after a reusable string reached EOF. */
    s->cursor.r.limit = s->cbuf + s->bsize - 1;
    /*
     * When the file reaches EOF,
     * stream_compact sets s->position to its end.
     * Reset it now to allow stell to work properly
     * after calls to this function.
     * Note that if the riched EOF and this fuction
     * was not called, stell still returns a wrong value.
     */
    s->position = 0;
    return 0;
}

/* Initialize a stream for writing a string. */
void
swrite_string(register stream * s, byte * ptr, uint len)
{
    static const stream_procs p = {
        s_std_noavailable, s_string_write_seek, s_std_write_reset,
        s_std_null, s_std_null, s_string_write_process
    };

    s_std_init(s, ptr, len, &p, s_mode_write + s_mode_seek);
    s->cbuf_string.data = ptr;
    s->cbuf_string.size = len;
}

/* Seek in a string being written.  Return 0 if OK, ERRC if not. */
static int
s_string_write_seek(register stream * s, gs_offset_t pos)
{
    if (pos < 0 || pos > s->bsize)
        return ERRC;
    s->cursor.w.ptr = s->cbuf + pos - 1;
    return 0;
}

/* Since we initialize the input buffer of a string read stream */
/* to contain all of the data in the string, if we are ever asked */
/* to refill the buffer, we should signal EOF. */
static int
s_string_read_process(stream_state * st, stream_cursor_read * ignore_pr,
                      stream_cursor_write * pw, bool last)
{
    return EOFC;
}
/* Similarly, if we are ever asked to empty the buffer, it means that */
/* there has been an overrun (unless we are closing the stream). */
static int
s_string_write_process(stream_state * st, stream_cursor_read * pr,
                       stream_cursor_write * ignore_pw, bool last)
{
    return (last ? EOFC : ERRC);
}

/* ------ Position-tracking stream ------ */

static int
    s_write_position_process(stream_state *, stream_cursor_read *,
                             stream_cursor_write *, bool);

/* Set up a write stream that just keeps track of the position. */
void
swrite_position_only(stream *s)
{
    static byte discard_buf[50];	/* size is arbitrary */

    swrite_string(s, discard_buf, sizeof(discard_buf));
    s->procs.process = s_write_position_process;
}

static int
s_write_position_process(stream_state * st, stream_cursor_read * pr,
                         stream_cursor_write * ignore_pw, bool last)
{
    pr->ptr = pr->limit;	/* discard data */
    return 0;
}

/* ------ Filter pipelines ------ */

/*
 * Add a filter to an output pipeline.  The client must have allocated the
 * stream state, if any, using the given allocator.  For s_init_filter, the
 * client must have called s_init and s_init_state.
 */
int
s_init_filter(stream *fs, stream_state *fss, byte *buf, uint bsize,
              stream *target)
{
    const stream_template *templat = fss->templat;

    if (bsize < templat->min_in_size)
        return ERRC;
    s_std_init(fs, buf, bsize, &s_filter_write_procs, s_mode_write);
    fs->procs.process = templat->process;
    fs->state = fss;
    if (templat->init) {
        fs->end_status = (templat->init)(fss);
        if (fs->end_status < 0)
            return fs->end_status;
    }
    fs->strm = target;
    return 0;
}
stream *
s_add_filter(stream **ps, const stream_template *templat,
             stream_state *ss, gs_memory_t *mem)
{
    stream *es;
    stream_state *ess;
    uint bsize = max(templat->min_in_size, 256);	/* arbitrary */
    byte *buf;

    /*
     * Ensure enough buffering.  This may require adding an additional
     * stream.
     */
    if (bsize > (*ps)->bsize && templat->process != s_NullE_template.process) {
        stream_template null_template;

        null_template = s_NullE_template;
        null_template.min_in_size = bsize;
        if (s_add_filter(ps, &null_template, NULL, mem) == 0)
            return 0;
    }
    es = s_alloc(mem, "s_add_filter(stream)");
    buf = gs_alloc_bytes(mem, bsize, "s_add_filter(buf)");
    if (es == 0 || buf == 0) {
        gs_free_object(mem, buf, "s_add_filter(buf)");
        gs_free_object(mem, es, "s_add_filter(stream)");
        return 0;
    }
    ess = (ss == 0 ? (stream_state *)es : ss);
    ess->templat = templat;
    ess->memory = mem;
    es->memory = mem;
    if (s_init_filter(es, ess, buf, bsize, *ps) < 0)
        return 0;
    *ps = es;
    return es;
}

/*
 * Close the filters in a pipeline, up to a given target stream, freeing
 * their buffers and state structures.
 */
int
s_close_filters(stream **ps, stream *target)
{
    while (*ps != target) {
        stream *s = *ps;
        gs_memory_t *mem = s->state->memory;
        byte *sbuf = s->cbuf;
        stream *next = s->strm;
        int status = sclose(s);
        stream_state *ss = s->state; /* sclose may set this to s */

        if (status < 0)
            return status;
        if (mem) {
            gs_free_object(mem, sbuf, "s_close_filters(buf)");
            gs_free_object(mem, s, "s_close_filters(stream)");
            if (ss != (stream_state *)s)
                gs_free_object(mem, ss, "s_close_filters(state)");
        }
        *ps = next;
    }
    return 0;
}

/* ------ Stream closing ------ */

/*
 * Finish closing a file stream.  This used to check whether it was
 * currentfile, but we don't have to do this any longer.  This replaces the
 * close procedure for the std* streams, which cannot actually be closed.
 *
 * This is exported for ziodev.c.  */
int
file_close_finish(stream * s)
{
    return 0;
}

/*
 * Close a file stream, but don't deallocate the buffer.  This replaces the
 * close procedure for %lineedit and %statementedit.  (This is WRONG: these
 * streams should allocate a new buffer each time they are opened, but that
 * would overstress the allocator right now.)  This is exported for ziodev.c.
 * This also replaces the close procedure for the string-reading streams
 * created for gs_run_string and for reusable streams.
 */
int
s_close_disable(stream *s)
{
    /* Increment the IDs to prevent further access. */
    s->read_id = s->write_id = (s->read_id | s->write_id) + 1;
    return 0;
}
int
file_close_disable(stream * s)
{
    int code;

    if ((*s->save_close != NULL) && ((code = (*s->save_close)(s)) != 0))
        return code;
    s_close_disable(s);
    return file_close_finish(s);
}

/* ------ NullEncode/Decode ------ */

/* Process a buffer */
static int
s_Null_process(stream_state * st, stream_cursor_read * pr,
               stream_cursor_write * pw, bool last)
{
    return stream_move(pr, pw);
}

/* Stream template */
const stream_template s_NullE_template = {
    &st_stream_state, NULL, s_Null_process, 1, 1
};
const stream_template s_NullD_template = {
    &st_stream_state, NULL, s_Null_process, 1, 1
};