aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/postscript/misc/pscrypt.c
blob: 520daf331d3692724f0c9911fe2a0f90601213b3 (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
/*
 *
 * Adobe's encryption/decryption algorithm for eexec and show. Runs in
 * eexec mode unless told otherwise. Use,
 *
 *		pscrypt file.cypher > file.clear
 *
 * to decrypt eexec input. Assumes file.cypher is hex with the key as the
 * first four bytes, and writes file.clear as binary (omitting the key).
 * Use
 *
 *		pscrypt -e12ab34ef file.clear >file.cypher
 *
 * to encrypt file.clear (for eexec) using 12ab34ef as the key. Input is
 * binary and output is hex. The key must be given as a hex number. Use
 * -sshow to encrypt or decrypt a CharString or Subr,
 *
 *		pscrypt -sshow file.cypher > file.clear
 *
 * Use -b or -x to read binary or hex input, and -B or -X to output binary
 * or hex.
 *
 */

#include <stdio.h>
#include <ctype.h>

#define ENCRYPT		0
#define DECRYPT		1

#define NOTSET		-1
#define BINARY		0
#define HEX		1
#define LINELENGTH	40

#define CHARSTRING	4330
#define EEXEC		55665
#define MAGIC1		52845
#define MAGIC2		22719

int	argc;
char	**argv;

int	mode = DECRYPT;
int	input = NOTSET;
int	output = NOTSET;
int	outoffset = NOTSET;
int	inoffset = NOTSET;

int	cryptkey = 0;			/* encryption key set with -e */
int	linelength = LINELENGTH;	/* only for hex output */
int	lastchar = 0;

unsigned long	seed = EEXEC;
unsigned long	key;

FILE	*fp_in;

/*****************************************************************************/

main(agc, agv)

    int		agc;
    char	*agv[];

{

/*
 *
 * Implementation of the encryption/decryption used by eexec and show.
 *
 */

    argc = agc;
    argv = agv;

    fp_in = stdin;

    options();
    initialize();
    arguments();

    exit(0);

}   /* End of main */

/*****************************************************************************/

options()

{

    int		ch;
    char	*names = "bde:l:os:xBSX";

    extern char	*optarg;
    extern int	optind;

/*
 *
 * Command line options.
 *
 */

    while ( (ch = getopt(argc, argv, names)) != EOF )
	switch ( ch ) {
	    case 'b':			/* binary input */
		    input = BINARY;
		    break;

	    case 'd':			/* decrypt */
		    mode = DECRYPT;
		    break;

	    case 'e':			/* encrypt */
		    mode = ENCRYPT;
		    if ( *optarg == '0' && *optarg == 'x' )
			optarg += 2;
		    sscanf(optarg, "%8x", &cryptkey);
		    break;

	    case 'l':			/* line length hex output */
		    linelength = atoi(optarg);
		    break;

	    case 'o':			/* output all bytes - debugging */
		    outoffset = 0;
		    break;

	    case 's':			/* seed */
		    if ( *optarg == 'e' )
			seed = EEXEC;
		    else if ( *optarg == 's' )
			seed = CHARSTRING;
		    else if ( *optarg == '0' && *(optarg+1) == 'x' )
			sscanf(optarg+2, "%x", &seed);
		    else if ( *optarg == '0' )
			sscanf(optarg, "%o", &seed);
		    else sscanf(optarg, "%d", &seed);
		    break;

	    case 'x':			/* hex input */
		    input = HEX;
		    break;

	    case 'B':			/* binary output */
		    output = BINARY;
		    break;

	    case 'X':			/* hex output */
		    output = HEX;
		    break;

	    case '?':			/* don't understand the option */
		    fprintf(stderr, "bad option -%c\n", ch);
		    exit(1);
		    break;

	    default:			/* don't know what to do for ch */
		    fprintf(stderr, "missing case for option -%c\n", ch);
		    exit(1);
		    break;
	}   /* End switch */

    argc -= optind;			/* get ready for non-option args */
    argv += optind;

}   /* End of options */

/*****************************************************************************/

initialize()

{

/*
 *
 * Initialization that has to be done after the options.
 *
 */

    key = seed;

    if ( mode == DECRYPT ) {
	input = (input == NOTSET) ? HEX : input;
	output = (output == NOTSET) ? BINARY : output;
	inoffset = (inoffset == NOTSET) ? 0 : inoffset;
	outoffset = (outoffset == NOTSET) ? -4 : outoffset;
    } else {
	input = (input == NOTSET) ? BINARY : input;
	output = (output == NOTSET) ? HEX : output;
	inoffset = (inoffset == NOTSET) ? 4 : inoffset;
	outoffset = (outoffset == NOTSET) ? 0 : outoffset;
    }	/* End else */

    if ( linelength <= 0 )
	linelength = LINELENGTH;

}   /* End of initialize */

/*****************************************************************************/

arguments()

{

/*
 *
 * Everything left is an input file. No arguments or '-' means stdin.
 *
 */

    if ( argc < 1 )
	crypt();
    else
	while ( argc > 0 ) {
	    if ( strcmp(*argv, "-") == 0 )
		fp_in = stdin;
	    else if ( (fp_in = fopen(*argv, "r")) == NULL ) {
		fprintf(stderr, "can't open %s\n", *argv);
		exit(1);
	    }	/* End if */
	    crypt();
	    if ( fp_in != stdin )
		fclose(fp_in);
	    argc--;
	    argv++;
	}   /* End while */

}   /* End of arguments */

/*****************************************************************************/

crypt()

{

    unsigned int	cypher;
    unsigned int	clear;

/*
 *
 * Runs the encryption/decryption algorithm.
 *
 */

    while ( lastchar != EOF ) {
	cypher = nextbyte();
	clear = ((key >> 8) ^ cypher) & 0xFF;
	key = (key + (mode == DECRYPT ? cypher : clear)) * MAGIC1 + MAGIC2;
	if ( ++outoffset > 0 && lastchar != EOF ) {
	    if ( output == HEX ) {
		printf("%.2X", clear);
		if ( linelength > 0 && (outoffset % linelength) == 0 )
		    putchar('\n');
	    } else putchar(clear);
	}   /* End if */
    }	/* End while */

}   /* End of crypt */

/*****************************************************************************/

nextbyte()

{

    int		val = EOF;

/*
 *
 * Returns the next byte. Uses cryptkey (i.e. what followed -e) while inoffset is
 * positive, otherwise reads (hex or binary) from fp_in.
 *
 */

    if ( inoffset-- > 0 )
	val = (cryptkey >> (inoffset*8)) & 0xFF;
    else if ( input == HEX ) {
	if ( (val = nexthexchar()) != EOF )
	    val = (val << 4) | nexthexchar();
    } else if ( input == BINARY )
	val = Getc(fp_in);

    return(val);

}   /* End of nextbyte */

/*****************************************************************************/

nexthexchar()

{

    int		ch;

/*
 *
 * Reads the next hex character.
 *
 */

    while ( (ch = Getc(fp_in)) != EOF && ! isxdigit(ch) ) ;

    if ( isdigit(ch) )
	ch -= '0';
    else if ( isupper(ch) )
	ch -= 'A' - 10;
    else if ( islower(ch) )
	ch -= 'a' - 10;

    return(ch);

}   /* End of nexthexchar */

/*****************************************************************************/

Getc(fp)

    FILE	*fp;

{

/*
 *
 * Reads the next byte from *fp, sets lastchar, and returns the character.
 *
 */

    return(lastchar = getc(fp));

}   /* End of Getc */

/*****************************************************************************/