xref: /plan9/sys/src/cmd/gs/src/zfzlib.c (revision 593dc095aefb2a85c828727bbfa9da139a49bdf4)
1 /* Copyright (C) 1995, 2000 Aladdin Enterprises.  All rights reserved.
2 
3   This software is provided AS-IS with no warranty, either express or
4   implied.
5 
6   This software is distributed under license and may not be copied,
7   modified or distributed except as expressly authorized under the terms
8   of the license contained in the file LICENSE in this distribution.
9 
10   For more information about licensing, please refer to
11   http://www.ghostscript.com/licensing/. For information on
12   commercial licensing, go to http://www.artifex.com/licensing/ or
13   contact Artifex Software, Inc., 101 Lucas Valley Road #110,
14   San Rafael, CA  94903, U.S.A., +1(415)492-9861.
15 */
16 
17 /* $Id: zfzlib.c,v 1.5 2002/02/21 22:24:54 giles Exp $ */
18 /* zlib and Flate filter creation */
19 #include "ghost.h"
20 #include "oper.h"
21 #include "idict.h"
22 #include "strimpl.h"
23 #include "spdiffx.h"
24 #include "spngpx.h"
25 #include "szlibx.h"
26 #include "idparam.h"
27 #include "ifilter.h"
28 #include "ifrpred.h"
29 #include "ifwpred.h"
30 
31 /* Common setup for zlib (Flate) filter */
32 private int
filter_zlib(i_ctx_t * i_ctx_p,stream_zlib_state * pzls)33 filter_zlib(i_ctx_t *i_ctx_p, stream_zlib_state *pzls)
34 {
35     os_ptr op = osp;
36     int code = 0;
37 
38     (*s_zlibE_template.set_defaults)((stream_state *)pzls);
39     if (r_has_type(op, t_dictionary))
40 	code = dict_int_param(op, "Effort", -1, 9, -1, &pzls->level);
41     return code;
42 }
43 
44 /* <source> zlibEncode/filter <file> */
45 /* <source> <dict> zlibEncode/filter <file> */
46 private int
zzlibE(i_ctx_t * i_ctx_p)47 zzlibE(i_ctx_t *i_ctx_p)
48 {
49     stream_zlib_state zls;
50     int code = filter_zlib(i_ctx_p, &zls);
51 
52     if (code < 0)
53 	return code;
54     return filter_write(i_ctx_p, 0, &s_zlibE_template, (stream_state *)&zls, 0);
55 }
56 
57 /* <target> zlibDecode/filter <file> */
58 /* <target> <dict> zlibDecode/filter <file> */
59 private int
zzlibD(i_ctx_t * i_ctx_p)60 zzlibD(i_ctx_t *i_ctx_p)
61 {
62     stream_zlib_state zls;
63 
64     (*s_zlibD_template.set_defaults)((stream_state *)&zls);
65     return filter_read(i_ctx_p, 0, &s_zlibD_template, (stream_state *)&zls, 0);
66 }
67 
68 /* <source> FlateEncode/filter <file> */
69 /* <source> <dict> FlateEncode/filter <file> */
70 private int
zFlateE(i_ctx_t * i_ctx_p)71 zFlateE(i_ctx_t *i_ctx_p)
72 {
73     stream_zlib_state zls;
74     int code = filter_zlib(i_ctx_p, &zls);
75 
76     if (code < 0)
77 	return code;
78     return filter_write_predictor(i_ctx_p, 0, &s_zlibE_template,
79 				  (stream_state *)&zls);
80 }
81 
82 /* <target> FlateDecode/filter <file> */
83 /* <target> <dict> FlateDecode/filter <file> */
84 private int
zFlateD(i_ctx_t * i_ctx_p)85 zFlateD(i_ctx_t *i_ctx_p)
86 {
87     stream_zlib_state zls;
88 
89     (*s_zlibD_template.set_defaults)((stream_state *)&zls);
90     return filter_read_predictor(i_ctx_p, 0, &s_zlibD_template,
91 				 (stream_state *)&zls);
92 }
93 
94 /* ------ Initialization procedure ------ */
95 
96 const op_def zfzlib_op_defs[] =
97 {
98     op_def_begin_filter(),
99     {"1zlibEncode", zzlibE},
100     {"1zlibDecode", zzlibD},
101     {"1FlateEncode", zFlateE},
102     {"1FlateDecode", zFlateD},
103     op_def_end(0)
104 };
105