xref: /plan9/sys/src/cmd/gs/src/zmisc1.c (revision 593dc095aefb2a85c828727bbfa9da139a49bdf4)
1 /* Copyright (C) 1994, 1997, 1999 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: zmisc1.c,v 1.7 2002/06/16 03:43:51 lpd Exp $ */
18 /* Miscellaneous Type 1 font operators */
19 #include "memory_.h"
20 #include "ghost.h"
21 #include "oper.h"
22 #include "gscrypt1.h"
23 #include "stream.h"		/* for getting state of PFBD stream */
24 #include "strimpl.h"
25 #include "sfilter.h"
26 #include "idict.h"
27 #include "idparam.h"
28 #include "ifilter.h"
29 
30 /* <state> <from_string> <to_string> .type1encrypt <new_state> <substring> */
31 /* <state> <from_string> <to_string> .type1decrypt <new_state> <substring> */
32 private int type1crypt(i_ctx_t *,
33 		       int (*)(byte *, const byte *, uint, ushort *));
34 private int
ztype1encrypt(i_ctx_t * i_ctx_p)35 ztype1encrypt(i_ctx_t *i_ctx_p)
36 {
37     return type1crypt(i_ctx_p, gs_type1_encrypt);
38 }
39 private int
ztype1decrypt(i_ctx_t * i_ctx_p)40 ztype1decrypt(i_ctx_t *i_ctx_p)
41 {
42     return type1crypt(i_ctx_p, gs_type1_decrypt);
43 }
44 private int
type1crypt(i_ctx_t * i_ctx_p,int (* proc)(byte *,const byte *,uint,ushort *))45 type1crypt(i_ctx_t *i_ctx_p,
46 	   int (*proc)(byte *, const byte *, uint, ushort *))
47 {
48     os_ptr op = osp;
49     crypt_state state;
50     uint ssize;
51 
52     check_type(op[-2], t_integer);
53     state = op[-2].value.intval;
54     if (op[-2].value.intval != state)
55 	return_error(e_rangecheck);	/* state value was truncated */
56     check_read_type(op[-1], t_string);
57     check_write_type(*op, t_string);
58     ssize = r_size(op - 1);
59     if (r_size(op) < ssize)
60 	return_error(e_rangecheck);
61     discard((*proc)(op->value.bytes, op[-1].value.const_bytes, ssize,
62 		    &state));	/* can't fail */
63     op[-2].value.intval = state;
64     op[-1] = *op;
65     r_set_size(op - 1, ssize);
66     pop(1);
67     return 0;
68 }
69 
70 /* Get the seed parameter for eexecEncode/Decode. */
71 /* Return npop if OK. */
72 private int
eexec_param(os_ptr op,ushort * pcstate)73 eexec_param(os_ptr op, ushort * pcstate)
74 {
75     int npop = 1;
76 
77     if (r_has_type(op, t_dictionary))
78 	++npop, --op;
79     check_type(*op, t_integer);
80     *pcstate = op->value.intval;
81     if (op->value.intval != *pcstate)
82 	return_error(e_rangecheck);	/* state value was truncated */
83     return npop;
84 }
85 
86 /* <target> <seed> eexecEncode/filter <file> */
87 /* <target> <seed> <dict_ignored> eexecEncode/filter <file> */
88 private int
zexE(i_ctx_t * i_ctx_p)89 zexE(i_ctx_t *i_ctx_p)
90 {
91     os_ptr op = osp;
92     stream_exE_state state;
93     int code = eexec_param(op, &state.cstate);
94 
95     if (code < 0)
96 	return code;
97     return filter_write(i_ctx_p, code, &s_exE_template, (stream_state *)&state, 0);
98 }
99 
100 /* <source> <seed> eexecDecode/filter <file> */
101 /* <source> <dict> eexecDecode/filter <file> */
102 private int
zexD(i_ctx_t * i_ctx_p)103 zexD(i_ctx_t *i_ctx_p)
104 {
105     os_ptr op = osp;
106     stream_exD_state state;
107     int code;
108 
109     (*s_exD_template.set_defaults)((stream_state *)&state);
110     if (r_has_type(op, t_dictionary)) {
111 	uint cstate;
112         bool is_eexec;
113 
114 	check_dict_read(*op);
115 	if ((code = dict_uint_param(op, "seed", 0, 0xffff, 0x10000,
116 				    &cstate)) < 0 ||
117 	    (code = dict_int_param(op, "lenIV", 0, max_int, 4,
118 				   &state.lenIV)) < 0 ||
119 	    (code = dict_bool_param(op, "eexec", false,
120 				   &is_eexec)) < 0
121 	    )
122 	    return code;
123 	state.cstate = cstate;
124         state.binary = (is_eexec ? -1 : 1);
125 	code = 1;
126     } else {
127         state.binary = 1;
128 	code = eexec_param(op, &state.cstate);
129     }
130     if (code < 0)
131 	return code;
132     /*
133      * If we're reading a .PFB file, let the filter know about it,
134      * so it can read recklessly to the end of the binary section.
135      */
136     if (r_has_type(op - 1, t_file)) {
137 	stream *s = (op - 1)->value.pfile;
138 
139 	if (s->state != 0 && s->state->template == &s_PFBD_template) {
140 	    stream_PFBD_state *pss = (stream_PFBD_state *)s->state;
141 
142 	    state.pfb_state = pss;
143 	    /*
144 	     * If we're reading the binary section of a PFB stream,
145 	     * avoid the conversion from binary to hex and back again.
146 	     */
147 	    if (pss->record_type == 2) {
148 		/*
149 		 * The PFB decoder may have converted some data to hex
150 		 * already.  Convert it back if necessary.
151 		 */
152 		if (pss->binary_to_hex && sbufavailable(s) > 0) {
153 		    state.binary = 0;	/* start as hex */
154 		    state.hex_left = sbufavailable(s);
155 		} else {
156 		    state.binary = 1;
157 		}
158 		pss->binary_to_hex = 0;
159 	    }
160 	    state.record_left = pss->record_left;
161 	}
162     }
163     return filter_read(i_ctx_p, code, &s_exD_template, (stream_state *)&state, 0);
164 }
165 
166 /* ------ Initialization procedure ------ */
167 
168 const op_def zmisc1_op_defs[] =
169 {
170     {"3.type1encrypt", ztype1encrypt},
171     {"3.type1decrypt", ztype1decrypt},
172     op_def_begin_filter(),
173     {"2eexecEncode", zexE},
174     {"2eexecDecode", zexD},
175     op_def_end(0)
176 };
177