File: seexec.c

package info (click to toggle)
gs 3.33-7
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 7,436 kB
  • ctags: 15,511
  • sloc: ansic: 92,150; asm: 684; sh: 486; makefile: 91
file content (164 lines) | stat: -rw-r--r-- 4,555 bytes parent folder | download
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
/* Copyright (C) 1994 Aladdin Enterprises.  All rights reserved.
  
  This file is part of GNU Ghostscript.
  
  GNU Ghostscript is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility to
  anyone for the consequences of using it or for whether it serves any
  particular purpose or works at all, unless he says so in writing.  Refer
  to the GNU Ghostscript General Public License for full details.
  
*/

/* seexec.c */
/* eexec filters */
#include "stdio_.h"		/* includes std.h */
#include "strimpl.h"
#include "sfilter.h"
#include "gscrypt1.h"
#include "scanchar.h"

/* ------ eexecEncode ------ */

/* Encoding is much simpler than decoding, because we don't */
/* worry about initial characters or hex vs. binary (the client */
/* has to take care of these aspects). */

private_st_exE_state();

#define ss ((stream_exE_state *)st)

/* Process a buffer */
private int
s_exE_process(stream_state *st, stream_cursor_read *pr,
  stream_cursor_write *pw, bool last)
{	const byte *p = pr->ptr;
	byte *q = pw->ptr;
	uint rcount = pr->limit - p;
	uint wcount = pw->limit - q;
	uint count;
	int status;
	if ( rcount <= wcount )
		count = rcount, status = 0;
	else
		count = wcount, status = 1;
	gs_type1_encrypt(q + 1, p + 1, count, (crypt_state *)&ss->cstate);
	pr->ptr += count;
	pw->ptr += count;
	return status;
}

#undef ss

/* Stream template */
const stream_template s_exE_template =
{	&st_exE_state, NULL, s_exE_process, 1, 2
};

/* ------ eexecDecode ------ */

private_st_exD_state();

#define ss ((stream_exD_state *)st)

/* Initialize the state for reading and decrypting. */
/* Decrypting streams are not positionable. */
private int
s_exD_init(stream_state *st)
{	ss->odd = -1;
	ss->binary = -1;		/* unknown */
	ss->record_left = max_long;
	ss->skip = 4;
	return 0;
}

/* Process a buffer. */
private int
s_exD_process(stream_state *st, stream_cursor_read *pr,
  stream_cursor_write *pw, bool last)
{	register const byte *p = pr->ptr;
	register byte *q = pw->ptr;
	int skip = ss->skip;
	int rcount = pr->limit - p;
	int wcount = pw->limit - q;
	int status = 0;
	int count = (wcount < rcount ? (status = 1, wcount) : rcount);

	if ( ss->binary < 0 )
	{	/* This is the very first time we're filling the buffer. */
		/* Determine whether this is ASCII or hex encoding. */
		register const byte _ds *decoder = scan_char_decoder;
		int i;

		if ( rcount < 8 )
			return 0;
		/* Adobe's documentation doesn't actually specify the test */
		/* that eexec should use, but we believe the following */
		/* gives correct answers even on certain non-conforming */
		/* PostScript files encountered in practice: */
		ss->binary = 0;
		for ( i = 1; i <= 8; i++ )
		  if ( !(decoder[p[i]] <= 0xf ||
			 decoder[p[i]] == ctype_space)
		     )
		    {	ss->binary = 1;
			if ( ss->pfb_state != 0 )
			  {	/* Stop at the end of the .PFB binary data. */
				ss->record_left = ss->pfb_state->record_left;
			  }
			break;
		  }
	}
	if ( ss->binary )
	{	if ( count > ss->record_left )
		  {	count = ss->record_left;
			status = 0;
		  }
		/* We pause at the end of the .PFB binary data, */
		/* in an attempt to keep from reading beyond the end of */
		/* the encrypted data. */
		if ( (ss->record_left -= count) == 0 )
		  ss->record_left = max_long;
		pr->ptr = p + count;
	}
	else
	{	/* We only ignore leading whitespace, in an attempt to */
		/* keep from reading beyond the end of the encrypted data. */
		status = s_hex_process(pr, pw, &ss->odd,
				       hex_ignore_leading_whitespace);
		p = q;
		count = pw->ptr - q;
	}
	if ( skip >= count && skip != 0 )
	{	gs_type1_decrypt(q + 1, p + 1, count,
				 (crypt_state *)&ss->cstate);
		ss->skip -= count;
		count = 0;
		status = 0;
	}
	else
	{	gs_type1_decrypt(q + 1, p + 1, skip,
				 (crypt_state *)&ss->cstate);
		count -= skip;
		gs_type1_decrypt(q + 1, p + 1 + skip, count,
				 (crypt_state *)&ss->cstate);
		ss->skip = 0;
	}
	pw->ptr = q + count;
	return status;
}

#undef ss

/* Stream template */
/*
 * The specification of eexec decoding requires that it never read more than
 * 512 source bytes ahead.  The only reliable way to ensure this is to
 * limit the size of the output buffer to 256.  We set it a little smaller
 * so that it will stay under the limit even after adding min_in_size
 * for a subsequent filter in a pipeline.  Note that we have to specify
 * a size of at least 128 so that filter_read won't round it up.
 */
const stream_template s_exD_template =
{	&st_exD_state, s_exD_init, s_exD_process, 8, 200
};