386BSD 0.1 development
[unix-history] / usr / othersrc / public / ghostscript-2.4.1 / zfilter.c
CommitLineData
8a627367
WJ
1/* Copyright (C) 1991 Aladdin Enterprises. All rights reserved.
2 Distributed by Free Software Foundation, Inc.
3
4This file is part of Ghostscript.
5
6Ghostscript is distributed in the hope that it will be useful, but
7WITHOUT ANY WARRANTY. No author or distributor accepts responsibility
8to anyone for the consequences of using it or for whether it serves any
9particular purpose or works at all, unless he says so in writing. Refer
10to the Ghostscript General Public License for full details.
11
12Everyone is granted permission to copy, modify and redistribute
13Ghostscript, but only under the conditions described in the Ghostscript
14General Public License. A copy of this license is supposed to have been
15given to you along with Ghostscript so you can know your rights and
16responsibilities. It should be in a file named COPYING. Among other
17things, the copyright notice and this notice must be preserved on all
18copies. */
19
20/* zfilter.c */
21/* Filter creation for Ghostscript */
22#include "ghost.h"
23#include "errors.h"
24#include "oper.h"
25#include "alloc.h"
26#include "stream.h"
27
28/* Forward references */
29int filter_read(P3(os_ptr, stream_procs _ds *, stream **));
30int filter_write(P3(os_ptr, stream_procs _ds *, stream **));
31
32/* .filterASCIIHexEncode */
33extern stream_procs s_AXE_procs;
34int
35zAXE(os_ptr op)
36{ return filter_write(op, &s_AXE_procs, NULL);
37}
38
39/* .filterASCIIHexDecode */
40extern stream_procs s_AXD_procs;
41extern void s_AXD_init(P1(stream *));
42int
43zAXD(os_ptr op)
44{ stream *s;
45 int code = filter_read(op, &s_AXD_procs, &s);
46 if ( code < 0 ) return code;
47 s_AXD_init(s);
48 return code;
49}
50
51/* .filtereexecDecode */
52extern stream_procs s_exD_procs;
53extern void s_exD_init(P2(stream *, ushort));
54int
55zexD(register os_ptr op)
56{ stream *s;
57 ushort state;
58 int code;
59 check_type(*op, t_integer);
60 state = op->value.intval;
61 if ( op->value.intval != state )
62 return e_rangecheck; /* state value was truncated */
63 code = filter_read(op - 1, &s_exD_procs, &s);
64 if ( code < 0 ) return code;
65 s_exD_init(s, state);
66 pop(1);
67 return 0;
68}
69
70/* .filterPFBDecode */
71extern stream_procs s_PFBD_procs;
72extern void s_PFBD_init(P1(stream *));
73int
74zPFBD(os_ptr op)
75{ stream *s;
76 int code = filter_read(op, &s_PFBD_procs, &s);
77 if ( code < 0 ) return code;
78 s_PFBD_init(s);
79 return code;
80}
81
82/* ------ Utilities ------ */
83
84/* Free the underlying string stream if needed. */
85private void
86filter_free_null(stream *s)
87{
88}
89private void
90filter_free_stream(stream *s)
91{ alloc_free((char *)s->strm, 1, sizeof(stream),
92 "filter_free_stream(stream)");
93}
94
95/* Set up an input filter. */
96int
97filter_read(os_ptr op, stream_procs _ds *procs, stream **ps)
98{ stream *s;
99 stream *sstrm;
100 int is_temp;
101 int code;
102 /* Check to make sure that the underlying data */
103 /* can function as a source for reading. */
104 switch ( r_type(op) )
105 {
106 case t_string:
107 check_access(*op, a_read);
108 sstrm = (stream *)alloc(1, sizeof(stream),
109 "filter_read(string stream)");
110 if ( sstrm == 0 ) return e_VMerror;
111 sread_string(sstrm, op->value.bytes, r_size(op));
112 is_temp = 1;
113 break;
114 case t_file:
115 check_access(*op, a_read);
116 sstrm = op->value.pfile;
117 is_temp = 0;
118 break;
119 default:
120 return e_typecheck;
121 }
122 code = file_open((byte *)0, 0, "r", (ref *)op, &s);
123 if ( code < 0 ) return code;
124 s_std_init(s, s->cbuf, s->bsize, procs, s_mode_read);
125 s->end_status = 0;
126 s->position = -1; /* not positionable */
127 s->file = 0; /* not a file stream */
128 s->strm = sstrm;
129 s->strm_is_temp = is_temp;
130 if ( ps != NULL ) *ps = s;
131 return 0;
132}
133
134/* Set up an output filter. */
135int
136filter_write(os_ptr op, stream_procs _ds *procs, stream **ps)
137{ stream *s;
138 stream *sstrm;
139 int is_temp;
140 int code;
141 /* Check to make sure that the underlying data */
142 /* can function as a sink for writing. */
143 switch ( r_type(op) )
144 {
145 case t_string:
146 check_access(*op, a_write);
147 sstrm = (stream *)alloc(1, sizeof(stream),
148 "filter_write(string)");
149 if ( sstrm == 0 ) return e_VMerror;
150 swrite_string(sstrm, op->value.bytes, r_size(op));
151 is_temp = 1;
152 break;
153 case t_file:
154 check_access(*op, a_write);
155 sstrm = op->value.pfile;
156 is_temp = 0;
157 break;
158 default:
159 return e_typecheck;
160 }
161 code = file_open((byte *)0, 0, "w", (ref *)op, &s);
162 if ( code < 0 ) return code;
163 s_std_init(s, s->cbuf, s->bsize, procs, s_mode_write);
164 s->end_status = 0;
165 s->position = -1; /* not positionable */
166 s->file = 0; /* not a file stream */
167 s->strm = sstrm;
168 s->strm_is_temp = is_temp;
169 if ( ps != NULL ) *ps = s;
170 return 0;
171}
172
173/* ------ Initialization procedure ------ */
174
175op_def zfilter_op_defs[] = {
176 {"1.filterASCIIHexEncode", zAXE},
177 {"1.filterASCIIHexDecode", zAXD},
178 {"2.filtereexecDecode", zexD},
179 {"1.filterPFBDecode", zPFBD},
180 op_def_end(0)
181};