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
|
/* Copyright (C) 1989, 1995 Aladdin Enterprises. All rights reserved.
This file is part of GNU Ghostscript.
GNU Ghostscript is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY. No author or distributor accepts responsibility to
anyone for the consequences of using it or for whether it serves any
particular purpose or works at all, unless he says so in writing. Refer
to the GNU Ghostscript General Public License for full details.
*/
/* stream.c */
/* 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 */
private int sreadbuf(P2(stream *, stream_cursor_write *));
private int swritebuf(P3(stream *, stream_cursor_read *, bool));
private void stream_compact(P1(stream *));
/* Structure types for allocating streams. */
private_st_stream();
public_st_stream_state(); /* default */
/* GC procedures */
#define st ((stream *)vptr)
private ENUM_PTRS_BEGIN(stream_enum_ptrs) return 0;
case 0:
if ( st->foreign )
*pep = NULL;
else if ( st->cbuf_string.data != 0 )
{ ENUM_RETURN_STRING_PTR(stream, cbuf_string);
}
else
*pep = st->cbuf;
break;
ENUM_PTR(1, stream, strm);
ENUM_PTR(2, stream, prev);
ENUM_PTR(3, stream, next);
ENUM_PTR(4, stream, state);
ENUM_PTRS_END
private RELOC_PTRS_BEGIN(stream_reloc_ptrs) {
byte *cbuf_old = st->cbuf;
if ( cbuf_old != 0 && !st->foreign )
{ long reloc;
if ( st->cbuf_string.data != 0 )
{ RELOC_STRING_PTR(stream, cbuf_string);
st->cbuf = st->cbuf_string.data;
}
else
RELOC_PTR(stream, cbuf);
reloc = cbuf_old - st->cbuf;
/* Relocate the other buffer pointers. */
st->srptr -= reloc;
st->srlimit -= reloc; /* same as swptr */
st->swlimit -= reloc;
}
RELOC_PTR(stream, strm);
RELOC_PTR(stream, prev);
RELOC_PTR(stream, next);
RELOC_PTR(stream, state);
} 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. */
private void
stream_finalize(void *vptr)
{ if_debug2('u', "[u]%s 0x%lx\n",
(!s_is_valid(st) ? "already closed:" :
st->is_temp ? "is_temp set:" :
st->file == 0 ? "not file:" :
"closing file:"), (ulong)st);
if ( s_is_valid(st) && !st->is_temp && st->file != 0 )
sclose(st); /* ignore errors */
}
#undef st
/* Dummy template for streams that don't have a separate state. */
private const stream_template s_no_template =
{ &st_stream_state, 0, 0, 1, 1, 0
};
/* ------ Generic procedures ------ */
/* Allocate a stream and initialize it minimally. */
stream *
s_alloc(gs_memory_t *mem, client_name_t cname)
{ stream *s = gs_alloc_struct(mem, stream, &st_stream, cname);
if_debug2('s', "[s]alloc(%s) = 0x%lx\n",
client_name_string(cname), (ulong)s);
if ( s == 0 )
return 0;
s->memory = mem;
s->report_error = s_no_report_error;
return s;
}
/* Allocate a stream state and initialize it minimally. */
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_debug3('s', "[s]alloc_state %s(%s) = 0x%lx\n",
client_name_string(cname),
client_name_string(stype->sname),
(ulong)st);
if ( st == 0 )
return 0;
st->memory = mem;
st->report_error = s_no_report_error;
return st;
}
/* Vacuous stream initialization */
int
s_no_init(stream_state *st)
{ return 0;
}
/* Standard stream initialization */
void
s_std_init(register stream *s, byte *ptr, uint len, const stream_procs *pp,
int modes)
{ s->template = &s_no_template;
s->cbuf = ptr;
s->srptr = s->srlimit = s->swptr = ptr - 1;
s->swlimit = 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 = 0;
s->file = 0;
if_debug4('s', "[s]init 0x%lx, buf=0x%lx, len=%u, modes=%d\n",
(ulong)s, (ulong)ptr, len, modes);
}
/* 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->srptr = s->srlimit = s->cbuf - 1;
}
/* Discard the contents of the buffer when writing. */
void
s_std_write_reset(stream *s)
{ s->swptr = s->cbuf- 1;
}
/* Flush data to end-of-file when reading. */
int
s_std_read_flush(stream *s)
{ while ( 1 )
{ s->srptr = s->srlimit = 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, long *pl)
{ *pl = -1;
return 0;
}
/* Indicate an error when asked to seek. */
int
s_std_noseek(stream *s, long 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;
s->cursor.r.ptr = s->cursor.r.limit = (const byte *)0 - 1;
s->cursor.w.limit = (byte *)0 - 1;
s->procs.close = s_std_null;
/* Clear pointers for GC */
s->strm = 0;
s->state = 0;
/****** SHOULD DO MORE THAN THIS ******/
if_debug1('s', "[s]disable 0x%lx\n", (ulong)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. */
int
s_filter_close(register stream *s)
{ if ( s_is_writing(s) )
{ int status = s_process_write_buf(s, true);
if ( status != 0 && status != EOFC )
return status;
}
return s_std_close(s);
}
/* Disregard a stream error message. */
int
s_no_report_error(stream_state *st, const char *str)
{ return 0;
}
/* ------ Implementation-independent procedures ------ */
/* Store the amount of available data in a(n input) stream. */
int
savailable(stream *s, long *pl)
{ return (*(s)->procs.available)(s, pl);
}
/* Return the current position of a stream. */
long
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->swptr : s->srptr);
return (ptr == 0 ? 0 : ptr + 1 - s->cbuf) + s->position;
}
/* Set the position of a stream. */
int
spseek(stream *s, long pos)
{ if_debug3('s', "[s]seek 0x%lx to %ld, position was %ld\n",
(ulong)s, pos, 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 code = (*s->procs.close)(s);
if ( code < 0 )
return code;
st = s->state;
if ( st != 0 )
{ stream_proc_release((*release)) = st->template->release;
if ( release != 0 )
(*release)(st);
if ( st != (stream_state *)s && st->memory != 0 )
gs_free_object(st->memory, st, "s_std_close");
s->state = 0;
}
s_disable(s);
return code;
}
/* Implement sgetc when the buffer may be empty. */
/* If the buffer really is empty, refill it and then read a byte. */
int
spgetc(register stream *s)
{ while ( sendrp(s) )
{ int status = s->end_status;
switch ( status )
{
case EOFC:
/* Do the equivalent of stream_compact, */
/* so stell will return the right result. */
s->position += s->cursor.r.ptr + 1 - s->cbuf;
s->cursor.r.ptr = s->cursor.r.limit = s->cbuf - 1;
status = sclose(s);
if ( status == 0 )
return EOFC;
s->end_status = status;
default: /* ERRC */
return status;
case 0:
;
}
s_process_read_buf(s);
}
return *++(s->srptr);
}
/* 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->swptr) = 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)
{ if ( !s_is_reading(s) || s->srptr < s->cbuf || *(s->srptr) != c )
return ERRC;
s->srptr--;
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;
cw.ptr = buf - 1;
cw.limit = cw.ptr + nmax;
while ( cw.ptr < cw.limit )
{ if ( !sendrp(s) )
stream_move(&s->cursor.r, &cw);
else
{ uint wanted = cw.limit - cw.ptr;
int c;
stream_state *st;
if ( wanted >= s->bsize >> 2 &&
(st = s->state) != 0 &&
wanted >= st->template->min_out_size
)
{ byte *wptr = cw.ptr;
status = sreadbuf(s, &cw);
/* We know the stream buffer is empty, */
/* so it's safe to update position. */
s->position += cw.ptr - wptr;
if ( status != 1 || 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->swlimit - s->swptr;
if ( count > 0 )
{ if ( count > len ) count = len;
memcpy(s->swptr + 1, str, count);
s->swptr += 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 a specified distance in a read stream. */
/* Return 0, EOFC, or ERRC. */
int
spskip(register stream *s, long n)
{ if ( n < 0 || !s_is_reading(s) ) return ERRC;
if ( s_can_seek(s) )
return sseek(s, stell(s) + n);
while ( sbufavailable(s) < n )
{ int code;
n -= sbufavailable(s) + 1;
s->srptr = s->srlimit;
if ( s->end_status )
return s->end_status;
code = sgetc(s);
if ( code < 0 )
return code;
}
s->srptr += n;
return 0;
}
/* ------ Utilities ------ */
/* Implement the old read_buf procedure in terms of the new */
/* process procedure. */
int
s_process_read_buf(stream *s)
{ int status;
stream_compact(s);
status = sreadbuf(s, &s->cursor.w);
s->end_status = (status >= 0 ? 0 : status);
return 0;
}
/* Implement the old write_buf procedure in terms of the new */
/* process procedure. */
int
s_process_write_buf(stream *s, bool last)
{ int status;
status = swritebuf(s, &s->cursor.r, last);
stream_compact(s);
if ( status >= 0 )
status = 0;
s->end_status = status;
return 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)\
{ stream *back = prev->strm;\
prev->strm = curr; curr = prev; prev = back;\
}
#define move_ahead(curr, prev)\
{ stream *ahead = curr->strm;\
curr->strm = prev; prev = curr; curr = ahead;\
}
/* Read from a pipeline. */
private int
sreadbuf(stream *s, stream_cursor_write *pbuf)
{ stream *prev = 0;
stream *curr = s;
int status;
for ( ; ; )
{ for ( ; ; )
{ /* Descend into the recursion. */
stream *strm = curr->strm;
stream_cursor_read cr;
stream_cursor_read *pr;
stream_cursor_write *pw;
bool eof;
if ( strm == 0 )
cr.ptr = 0, cr.limit = 0, pr = &cr,
eof = false;
else
pr = &strm->cursor.r,
eof = strm->end_status == EOFC;
pw = (prev == 0 ? pbuf : &curr->cursor.w);
if_debug4('s', "[s]read process 0x%lx, nr=%u, nw=%u, eof=%d\n",
(ulong)curr, (uint)(pr->limit - pr->ptr),
(uint)(pw->limit - pw->ptr), eof);
status = (*curr->procs.process)(curr->state, pr, pw, eof);
if_debug4('s', "[s]after read 0x%lx, nr=%u, nw=%u, status=%d\n",
(ulong)curr, (uint)(pr->limit - pr->ptr),
(uint)(pw->limit - pw->ptr), status);
if ( strm == 0 || status != 0 )
break;
status = strm->end_status;
if ( status < 0 )
break;
move_ahead(curr, prev);
stream_compact(curr);
}
/* Unwind from the recursion. */
curr->end_status = (status >= 0 ? 0 : status);
if ( prev == 0 )
return status;
move_back(curr, prev);
}
}
/* Write to a pipeline. */
private int
swritebuf(stream *s, stream_cursor_read *pbuf, bool last)
{ stream *prev = 0;
stream *curr = s;
int status;
for ( ; ; )
{ for ( ; ; )
{ /* Descend into the recursion. */
stream *strm = curr->strm;
stream_cursor_write cw;
stream_cursor_read *pr;
stream_cursor_write *pw;
bool end = last;
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_debug4('s', "[s]write process 0x%lx, nr=%u, nw=%u, end=%d\n",
(ulong)curr, (uint)(pr->limit - pr->ptr),
(uint)(pw->limit - pw->ptr), end);
status = (*curr->procs.process)(curr->state, pr, pw,
end);
if_debug4('s', "[s]after write 0x%lx, nr=%u, nw=%u, status=%d\n",
(ulong)curr, (uint)(pr->limit - pr->ptr),
(uint)(pw->limit - pw->ptr), status);
if ( strm == 0 || status < 0 )
break;
if ( status == 1 )
end = false;
else
{ /* Keep going if we are closing */
/* a filter with a sub-stream. */
/* We know status == 0. */
if ( !end || !strm->is_temp )
break;
}
status = strm->end_status;
if ( status < 0 )
break;
move_ahead(curr, prev);
stream_compact(curr);
}
/* Unwind from the recursion. */
curr->end_status = (status >= 0 ? 0 : status);
if ( prev == 0 )
return status;
move_back(curr, prev);
}
}
/* 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. */
private void
stream_compact(stream *s)
{ if ( s->cursor.r.ptr >= s->cbuf && 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 */
private int
s_string_available(P2(stream *, long *)),
s_string_read_seek(P2(stream *, long)),
s_string_write_seek(P2(stream *, long)),
s_string_read_process(P4(stream_state *, stream_cursor_read *,
stream_cursor_write *, bool)),
s_string_write_process(P4(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->srlimit = s->swlimit;
}
/* Return the number of available bytes when reading from a string. */
private int
s_string_available(stream *s, long *pl)
{ *pl = sbufavailable(s);
if ( *pl == 0 ) /* EOF */
*pl = -1;
return 0;
}
/* Seek in a string being read. Return 0 if OK, ERRC if not. */
private int
s_string_read_seek(register stream *s, long pos)
{ if ( pos < 0 || pos > s->bsize ) return ERRC;
s->srptr = s->cbuf + pos - 1;
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. */
private int
s_string_write_seek(register stream *s, long pos)
{ if ( pos < 0 || pos > s->bsize ) return ERRC;
s->swptr = 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. */
private 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). */
private int
s_string_write_process(stream_state *st, stream_cursor_read *pr,
stream_cursor_write *ignore_pw, bool last)
{ return (last ? EOFC : ERRC);
}
|