summaryrefslogtreecommitdiff
path: root/gs/src/zfilter.c
blob: f3c55fbf93297d6f65d76576107a52078f71fbac (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
/* Copyright (C) 1993, 2000 Aladdin Enterprises.  All rights reserved.
  
  This file is part of AFPL Ghostscript.
  
  AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  distributor accepts any responsibility for the consequences of using it, or
  for whether it serves any particular purpose or works at all, unless he or
  she says so in writing.  Refer to the Aladdin Free Public License (the
  "License") for full details.
  
  Every copy of AFPL Ghostscript must include a copy of the License, normally
  in a plain ASCII text file named PUBLIC.  The License grants you the right
  to copy, modify and redistribute AFPL Ghostscript, but only under certain
  conditions described in the License.  Among other things, the License
  requires that the copyright notice and this notice be preserved on all
  copies.
*/

/*$Id$ */
/* Filter creation */
#include "memory_.h"
#include "ghost.h"
#include "oper.h"
#include "gsstruct.h"
#include "ialloc.h"
#include "idict.h"
#include "idparam.h"
#include "ilevel.h"		/* SubFileDecode is different in LL3 */
#include "stream.h"
#include "strimpl.h"
#include "sfilter.h"
#include "srlx.h"
#include "sstring.h"
#include "ifilter.h"
#include "files.h"		/* for filter_open, file_d'_buffer_size */

/* <source> ASCIIHexEncode/filter <file> */
/* <source> <dict> ASCIIHexEncode/filter <file> */
private int
zAXE(i_ctx_t *i_ctx_p)
{
    return filter_write_simple(i_ctx_p, &s_AXE_template);
}

/* <target> ASCIIHexDecode/filter <file> */
/* <target> <dict> ASCIIHexDecode/filter <file> */
private int
zAXD(i_ctx_t *i_ctx_p)
{
    return filter_read_simple(i_ctx_p, &s_AXD_template);
}

/* <target> NullEncode/filter <file> */
/* <target> <dict_ignored> NullEncode/filter <file> */
private int
zNullE(i_ctx_t *i_ctx_p)
{
    return filter_write_simple(i_ctx_p, &s_NullE_template);
}

/* <source> <bool> PFBDecode/filter <file> */
/* <source> <dict> <bool> PFBDecode/filter <file> */
private int
zPFBD(i_ctx_t *i_ctx_p)
{
    os_ptr sop = osp;
    stream_PFBD_state state;

    check_type(*sop, t_boolean);
    state.binary_to_hex = sop->value.boolval;
    return filter_read(i_ctx_p, 1, &s_PFBD_template, (stream_state *)&state, 0);
}

/* <target> PSStringEncode/filter <file> */
/* <target> <dict> PSStringEncode/filter <file> */
private int
zPSSE(i_ctx_t *i_ctx_p)
{
    return filter_write_simple(i_ctx_p, &s_PSSE_template);
}

/* ------ RunLength filters ------ */

/* Common setup for RLE and RLD filters. */
private int
rl_setup(os_ptr dop, bool * eod)
{
    if (r_has_type(dop, t_dictionary)) {
	int code;

	check_dict_read(*dop);
	if ((code = dict_bool_param(dop, "EndOfData", true, eod)) < 0)
	    return code;
	return 1;
    } else {
	*eod = true;
	return 0;
    }
}

/* <target> <record_size> RunLengthEncode/filter <file> */
/* <target> <dict> <record_size> RunLengthEncode/filter <file> */
private int
zRLE(i_ctx_t *i_ctx_p)
{
    os_ptr op = osp;
    stream_RLE_state state;
    int code;

    check_op(2);
    code = rl_setup(op - 1, &state.EndOfData);
    if (code < 0)
	return code;
    check_int_leu(*op, max_uint);
    state.record_size = op->value.intval;
    return filter_write(i_ctx_p, 1, &s_RLE_template, (stream_state *) & state, 0);
}

/* <source> RunLengthDecode/filter <file> */
/* <source> <dict> RunLengthDecode/filter <file> */
private int
zRLD(i_ctx_t *i_ctx_p)
{
    os_ptr op = osp;
    stream_RLD_state state;
    int code = rl_setup(op, &state.EndOfData);

    if (code < 0)
	return code;
    return filter_read(i_ctx_p, 0, &s_RLD_template, (stream_state *) & state, 0);
}

/* <source> <EODcount> <EODstring> SubFileDecode/filter <file> */
/* <source> <dict> <EODcount> <EODstring> SubFileDecode/filter <file> */
/* <source> <dict> SubFileDecode/filter <file> *//* (LL3 only) */
private int
zSFD(i_ctx_t *i_ctx_p)
{
    os_ptr op = osp;
    stream_SFD_state state;
    ref *sop = op;
    int npop;

    if (s_SFD_template.set_defaults)
	s_SFD_template.set_defaults((stream_state *)&state);
    if (LL3_ENABLED && r_has_type(op, t_dictionary)) {
	int count;
	int code;

	check_dict_read(*op);
	if ((code = dict_int_param(op, "EODCount", 0, max_int, -1, &count)) < 0)
	    return code;
	if (dict_find_string(op, "EODString", &sop) <= 0)
	    return_error(e_rangecheck);
	state.count = count;
	npop = 0;
    } else {
	check_type(sop[-1], t_integer);
	if (sop[-1].value.intval < 0)
	    return_error(e_rangecheck);
	state.count = sop[-1].value.intval;
	npop = 2;
    }
    check_read_type(*sop, t_string);
    state.eod.data = sop->value.const_bytes;
    state.eod.size = r_size(sop);
    return filter_read(i_ctx_p, npop, &s_SFD_template,
		       (stream_state *)&state, r_space(sop));
}

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

/* Forward references */
private int filter_ensure_buf(P4(stream **, uint, gs_ref_memory_t *, bool));

/* Set up an input filter. */
int
filter_read(i_ctx_t *i_ctx_p, int npop, const stream_template * template,
	    stream_state * st, uint space)
{
    os_ptr op = osp;
    uint min_size = template->min_out_size + max_min_left;
    uint save_space = ialloc_space(idmemory);
    uint use_space = max(space, save_space);
    os_ptr sop = op - npop;
    stream *s;
    stream *sstrm;
    bool close = false;
    int code;

    /* Skip over an optional dictionary parameter. */
    if (r_has_type(sop, t_dictionary)) {
	check_dict_read(*sop);
	if ((code = dict_bool_param(sop, "CloseSource", false, &close)) < 0)
	    return code;
	--sop;
    }
    /*
     * Check to make sure that the underlying data
     * can function as a source for reading.
     */
    use_space = max(use_space, r_space(sop));
    switch (r_type(sop)) {
	case t_string:
	    check_read(*sop);
	    ialloc_set_space(idmemory, use_space);
	    sstrm = file_alloc_stream(imemory, "filter_read(string stream)");
	    if (sstrm == 0) {
		code = gs_note_error(e_VMerror);
		goto out;
	    }
	    sread_string(sstrm, sop->value.bytes, r_size(sop));
	    sstrm->is_temp = 1;
	    break;
	case t_file:
	    check_read_known_file(sstrm, sop, return);
	    ialloc_set_space(idmemory, use_space);
	    goto ens;
	default:
	    check_proc(*sop);
	    ialloc_set_space(idmemory, use_space);
	    code = sread_proc(sop, &sstrm, iimemory);
	    if (code < 0)
		goto out;
	    sstrm->is_temp = 2;
	  ens:
	    code = filter_ensure_buf(&sstrm,
				     template->min_in_size +
				     sstrm->state->template->min_out_size,
				     iimemory, false);
	    if (code < 0)
		goto out;
	    break;
    }
    if (min_size < 128)
	min_size = file_default_buffer_size;
    code = filter_open("r", min_size, (ref *) sop,
		       &s_filter_read_procs, template, st, imemory);
    if (code < 0)
	goto out;
    s = fptr(sop);
    s->strm = sstrm;
    s->close_strm = close;
    pop(op - sop);
out:
    ialloc_set_space(idmemory, save_space);
    return code;
}
int
filter_read_simple(i_ctx_t *i_ctx_p, const stream_template * template)
{
    return filter_read(i_ctx_p, 0, template, NULL, 0);
}

/* Set up an output filter. */
int
filter_write(i_ctx_t *i_ctx_p, int npop, const stream_template * template,
	     stream_state * st, uint space)
{
    os_ptr op = osp;
    uint min_size = template->min_in_size + max_min_left;
    uint save_space = ialloc_space(idmemory);
    uint use_space = max(space, save_space);
    register os_ptr sop = op - npop;
    stream *s;
    stream *sstrm;
    bool close = false;
    int code;

    /* Skip over an optional dictionary parameter. */
    if (r_has_type(sop, t_dictionary)) {
	check_dict_read(*sop);
	if ((code = dict_bool_param(sop, "CloseTarget", false, &close)) < 0)
	    return code;
	--sop;
    }
    /*
     * Check to make sure that the underlying data
     * can function as a sink for writing.
     */
    use_space = max(use_space, r_space(sop));
    switch (r_type(sop)) {
	case t_string:
	    check_write(*sop);
	    ialloc_set_space(idmemory, use_space);
	    sstrm = file_alloc_stream(imemory, "filter_write(string)");
	    if (sstrm == 0) {
		code = gs_note_error(e_VMerror);
		goto out;
	    }
	    swrite_string(sstrm, sop->value.bytes, r_size(sop));
	    sstrm->is_temp = 1;
	    break;
	case t_file:
	    check_write_known_file(sstrm, sop, return);
	    ialloc_set_space(idmemory, use_space);
	    goto ens;
	default:
	    check_proc(*sop);
	    ialloc_set_space(idmemory, use_space);
	    code = swrite_proc(sop, &sstrm, iimemory);
	    if (code < 0)
		goto out;
	    sstrm->is_temp = 2;
	  ens:
	    code = filter_ensure_buf(&sstrm,
				     template->min_out_size +
				     sstrm->state->template->min_in_size,
				     iimemory, true);
	    if (code < 0)
		goto out;
	    break;
    }
    if (min_size < 128)
	min_size = file_default_buffer_size;
    code = filter_open("w", min_size, (ref *) sop,
		       &s_filter_write_procs, template, st, imemory);
    if (code < 0)
	goto out;
    s = fptr(sop);
    s->strm = sstrm;
    s->close_strm = close;
    pop(op - sop);
out:
    ialloc_set_space(idmemory, save_space);
    return code;
}
int
filter_write_simple(i_ctx_t *i_ctx_p, const stream_template * template)
{
    return filter_write(i_ctx_p, 0, template, NULL, 0);
}

/* Define a byte-at-a-time NullDecode filter for intermediate buffers. */
/* (The standard NullDecode filter can read ahead too far.) */
private int
s_Null1D_process(stream_state * st, stream_cursor_read * pr,
		 stream_cursor_write * pw, bool last)
{
    if (pr->ptr >= pr->limit)
	return 0;
    if (pw->ptr >= pw->limit)
	return 1;
    *++(pw->ptr) = *++(pr->ptr);
    return 1;
}
private const stream_template s_Null1D_template = {
    &st_stream_state, NULL, s_Null1D_process, 1, 1
};

/* Ensure a minimum buffer size for a filter. */
/* This may require creating an intermediate stream. */
private int
filter_ensure_buf(stream ** ps, uint min_buf_size, gs_ref_memory_t *imem,
		  bool writing)
{
    stream *s = *ps;
    uint min_size = min_buf_size + max_min_left;
    stream *bs;
    ref bsop;
    int code;

    if (s->modes == 0 /* stream is closed */  || s->bsize >= min_size)
	return 0;
    /* Otherwise, allocate an intermediate stream. */
    if (s->cbuf == 0) {
	/* This is a newly created procedure stream. */
	/* Just allocate a buffer for it. */
	uint len = max(min_size, 128);
	byte *buf = gs_alloc_bytes((gs_memory_t *)imem, len,
				   "filter_ensure_buf");

	if (buf == 0)
	    return_error(e_VMerror);
	s->cbuf = buf;
	s->srptr = s->srlimit = s->swptr = buf - 1;
	s->swlimit = buf - 1 + len;
	s->bsize = s->cbsize = len;
	return 0;
    } else {
	/* Allocate an intermediate stream. */
	if (writing)
	    code = filter_open("w", min_size, &bsop, &s_filter_write_procs,
			       &s_NullE_template, NULL, (gs_memory_t *)imem);
	else
	    code = filter_open("r", min_size, &bsop, &s_filter_read_procs,
			       &s_Null1D_template, NULL, (gs_memory_t *)imem);
	if (code < 0)
	    return code;
	bs = fptr(&bsop);
	bs->strm = s;
	bs->is_temp = 2;
	*ps = bs;
	return code;
    }
}

/* Mark a (filter) stream as temporary. */
/* We define this here to avoid importing stream.h into zf*.c. */
void
filter_mark_temp(const ref * fop, int is_temp)
{
    fptr(fop)->is_temp = is_temp;
}

/* Mark the source or target of a filter as temporary, and propagate */
/* close_strm from the temporary stream to the filter. */
void
filter_mark_strm_temp(const ref * fop, int is_temp)
{
    stream *s = fptr(fop);
    stream *strm = s->strm;

    strm->is_temp = is_temp;
    s->close_strm = strm->close_strm;
}

/* ------ Initialization procedure ------ */

const op_def zfilter_op_defs[] = {
		/* We enter PSStringEncode and SubFileDecode (only) */
		/* as separate operators. */
    {"1.psstringencode", zPSSE},
    {"2.subfiledecode", zSFD},
    op_def_begin_filter(),
    {"1ASCIIHexEncode", zAXE},
    {"1ASCIIHexDecode", zAXD},
    {"1NullEncode", zNullE},
    {"2PFBDecode", zPFBD},
    {"1PSStringEncode", zPSSE},
    {"2RunLengthEncode", zRLE},
    {"1RunLengthDecode", zRLD},
    {"3SubFileDecode", zSFD},
    op_def_end(0)
};