xref: /plan9/sys/src/cmd/gs/src/zfarc4.c (revision 593dc095aefb2a85c828727bbfa9da139a49bdf4)
1 /* Copyright (C) 2001 Artifex Software, Inc.  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: zfarc4.c,v 1.4 2002/02/21 22:24:54 giles Exp $ */
18 
19 /* this is the ps interpreter interface to the arcfour cipher filter
20    used in PDF encryption. We provide both Decode and Encode filters;
21    the cipher is symmetric, so the call to the underlying routines is
22    identical between the two filters */
23 
24 #include "memory_.h"
25 #include "ghost.h"
26 #include "oper.h"
27 #include "gsstruct.h"
28 #include "ialloc.h"
29 #include "idict.h"
30 #include "stream.h"
31 #include "strimpl.h"
32 #include "ifilter.h"
33 #include "sarc4.h"
34 
35 /* <source> <dict> arcfour/filter <file> */
36 
37 private int
z_arcfour_d(i_ctx_t * i_ctx_p)38 z_arcfour_d(i_ctx_t * i_ctx_p)
39 {
40     os_ptr op = osp;		/* i_ctx_p->op_stack.stack.p defined in osstack.h */
41     ref *sop = NULL;
42     stream_arcfour_state state;
43 
44     /* extract the key from the parameter dictionary */
45     check_type(*op, t_dictionary);
46     check_dict_read(*op);
47     if (dict_find_string(op, "Key", &sop) <= 0)
48 	return_error(e_rangecheck);
49 
50     s_arcfour_set_key(&state, sop->value.const_bytes, r_size(sop));
51 
52     /* we pass npop=0, since we've no arguments left to consume */
53     /* we pass 0 instead of the usual rspace(sop) will allocate storage for
54        filter state from the same memory pool as the stream it's coding. this
55        causes no trouble because we maintain no pointers */
56     return filter_read(i_ctx_p, 0, &s_arcfour_template,
57 		       (stream_state *) & state, 0);
58 }
59 
60 /* encode version of the filter */
61 private int
z_arcfour_e(i_ctx_t * i_ctx_p)62 z_arcfour_e(i_ctx_t * i_ctx_p)
63 {
64     os_ptr op = osp;		/* i_ctx_p->op_stack.stack.p defined in osstack.h */
65     ref *sop = NULL;
66     stream_arcfour_state state;
67 
68     /* extract the key from the parameter dictionary */
69     check_type(*op, t_dictionary);
70     check_dict_read(*op);
71     if (dict_find_string(op, "Key", &sop) <= 0)
72 	return_error(e_rangecheck);
73 
74     s_arcfour_set_key(&state, sop->value.const_bytes, r_size(sop));
75 
76     /* we pass npop=0, since we've no arguments left to consume */
77     /* we pass 0 instead of the usual rspace(sop) will allocate storage for
78        filter state from the same memory pool as the stream it's coding. this
79        causes no trouble because we maintain no pointers */
80     return filter_write(i_ctx_p, 0, &s_arcfour_template,
81 			(stream_state *) & state, 0);
82 }
83 
84 /* match the above routines to their postscript filter names
85    this is how our 'private' routines get called externally */
86 const op_def zfarc4_op_defs[] = {
87     op_def_begin_filter(),
88     {"2ArcfourDecode", z_arcfour_d},
89     {"2ArcfourEncode", z_arcfour_e},
90     op_def_end(0)
91 };
92