Debian Codegraph @

contrib/fonttools/pfadecrypt.c

raw symbols SCIP
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <ctype.h>
5 #include <unistd.h>
6
7 static int leniv=4;
8 static int useshex;
9
10 static char *myfgets(char *str, int len, FILE *file) {
11 char *pt, *end;
12 int ch;
13
14 for ( pt = str, end = str+len-1; pt<end && (ch=getc(file))!=EOF && ch!='\r' && ch!='\n';
15 *pt++ = ch );
16 if ( ch=='\n' )
17 *pt++ = '\n';
18 else if ( ch=='\r' ) {
19 *pt++ = '\n';
20 if ((ch=getc(file))!='\n' )
21 ungetc(ch,file);
22 }
23 if ( pt==str )
24 return( NULL );
25 *pt = '\0';
26 return( str );
27 }
28
29 static int hex(int ch1, int ch2) {
30 if ( ch1>='0' && ch1<='9' )
31 ch1 -= '0';
32 else if ( ch1>='a' )
33 ch1 -= 'a'-10;
34 else
35 ch1 -= 'A'-10;
36 if ( ch2>='0' && ch2<='9' )
37 ch2 -= '0';
38 else if ( ch2>='a' )
39 ch2 -= 'a'-10;
40 else
41 ch2 -= 'A'-10;
42 return( (ch1<<4)|ch2 );
43 }
44
45 static unsigned short r;
46 #define c1 (unsigned short) 52845
47 #define c2 (unsigned short) 22719
48
49 static void initcode() {
50 r = 55665;
51 }
52
53 static int decode(unsigned char cypher) {
54 unsigned char plain = ( cypher ^ (r>>8));
55 r = (cypher + r) * c1 + c2;
56 return( plain );
57 }
58
59 static void dumpzeros(FILE *out, unsigned char *zeros, int zcnt) {
60 while ( --zcnt >= 0 )
61 fputc(*zeros++,out);
62 }
63
64 static void decodestr(unsigned char *str, int len) {
65 unsigned short rc = 4330;
66 unsigned char plain, cypher;
67
68 while ( len-->0 ) {
69 cypher = *str;
70 plain = ( cypher ^ (rc>>8));
71 rc = (cypher + rc) * c1 + c2;
72 *str++ = plain;
73 }
74 }
75
76 static void decodebytes(FILE *out,unsigned char *binpt, int binlen ) {
77 unsigned char *end = binpt+binlen;
78 static const char *commands[32] = { "?0", "hstem", "?2", "vstem", "vmoveto",
79 "rlineto", "hlineto", "vlineto", "rrcurveto", "closepath",
80 "callsubr", "return", "escape", "hsbw", "endchar", "?15", "?16",
81 "?17", "?18", "?19", "?20", "rmoveto", "hmoveto", "?23", "?24",
82 "?25", "?26", "?27", "?28", "?29", "vhcurveto", "hvcurveto" };
83
84 decodestr(binpt,binlen);
85 binpt += leniv;
86 while ( binpt<end ) {
87 int ch = *binpt++;
88 if ( ch>=32 && ch<=246 ) {
89 fprintf( out, " %d", ch-139 );
90 } else if ( ch>=247 && ch<=250 ) {
91 int ch2 = *binpt++;
92 fprintf( out, " %d", (ch-247)*256+ch2+108 );
93 } else if ( ch>=251 && ch<=254 ) {
94 int ch2 = *binpt++;
95 fprintf( out, " %d", -(ch-251)*256-ch2-108 );
96 } else if ( ch==255 ) {
97 int val;
98 val = *binpt++;
99 val = (val<<8) | *binpt++;
100 val = (val<<8) | *binpt++;
101 val = (val<<8) | *binpt++;
102 fprintf( out, " %d", val );
103 } else if ( ch!=12 ) {
104 fprintf( out, " %s", commands[ch]);
105 } else {
106 int ch2 = *binpt++;
107 if ( ch2==0 )
108 fprintf( out, " dotsection" );
109 else if ( ch2==1 )
110 fprintf( out, " vstem3" );
111 else if ( ch2==2 )
112 fprintf( out, " hstem3" );
113 else if ( ch2==6 )
114 fprintf( out, " seac" );
115 else if ( ch2==7 )
116 fprintf( out, " sbw" );
117 else if ( ch2==12 )
118 fprintf( out, " div" );
119 else if ( ch2==16 )
120 fprintf( out, " callothersubr" );
121 else if ( ch2==17 )
122 fprintf( out, " pop" );
123 else if ( ch2==33 )
124 fprintf( out, " setcurrentpoint" );
125 else
126 fprintf( out, " ?12-%d", ch2 );
127 }
128 }
129 }
130
131 /* In the book the token which starts a character description is always RD but*/
132 /* it's just the name of a subroutine which is defined in the private diction*/
133 /* and it could be anything. in one case it was "-|" (hyphen bar) so we can't*/
134 /* just look for RD we must be a bit smarter and figure out what the token is*/
135 /* It's defined as {string currentfile exch readstring pop} so look for that */
136 static int glorpline(FILE *temp, FILE *out,char *rdtok) {
137 char buffer[3000], *pt, *binstart;
138 int binlen;
139 int ch;
140 int innum, val, inbinary, inhex, cnt, inr, wasspace, nownum, nowr, nowspace, sptok;
141 const char *rdline = "{string currentfile exch readstring pop}", *rpt;
142 const char *rdline2 = "{string currentfile exch readhexstring pop}";
143 char *tokpt = NULL, *rdpt;
144 char temptok[255];
145 int intok, willbehex = 0;
146 int nibble=0, firstnibble=1;
147
148 ch = getc(temp);
149 if ( ch==EOF )
150 return( 0 );
151 ungetc(ch,temp);
152
153 innum = inr = 0; wasspace = 0; inbinary = inhex = 0; rpt = NULL; rdpt = NULL;
154 pt = buffer; binstart=NULL; binlen = 0; intok=0; sptok=0;
155 while ( (ch=getc(temp))!=EOF ) {
156 *pt++ = ch;
157 if ( pt>=buffer+sizeof(buffer)) {
158 fprintf(stderr,"Buffer overrun\n" );
159 exit(1);
160 }
161 nownum = nowspace = nowr = 0;
162 if ( rpt!=NULL && ch!=*rpt && ch=='h' && rpt-rdline>25 && rpt-rdline<30 &&
163 rdline2[rpt-rdline]=='h' ) {
164 rpt = rdline2 + (rpt-rdline);
165 willbehex = 1;
166 }
167 if ( inbinary ) {
168 if ( --cnt==0 )
169 inbinary = 0;
170 } else if ( inhex ) {
171 if ( isdigit(ch) || (ch>='a'&&ch<='f') || (ch>='A' && ch<='F')) {
172 int h;
173 if ( isdigit(ch)) h = ch-'0';
174 else if ( ch>='a' && ch<='f' ) h = ch-'a'+10;
175 else h = ch-'A'+10;
176 if ( firstnibble ) {
177 nibble = h;
178 --pt;
179 } else {
180 pt[-1] = (nibble<<4)|h;
181 if ( --cnt==0 )
182 inbinary = inhex = 0;
183 }
184 firstnibble = !firstnibble;
185 } else {
186 --pt;
187 /* skip everything not hex */
188 }
189 } else if ( ch=='/' ) {
190 intok = 1;
191 tokpt = temptok;
192 } else if ( intok && !isspace(ch) && ch!='{' && ch!='[' ) {
193 *tokpt++ = ch;
194 } else if ( (intok||sptok) && (ch=='{' || ch=='[')) {
195 *tokpt = '\0';
196 rpt = rdline+1;
197 intok = sptok = 0;
198 } else if ( intok ) {
199 *tokpt = '\0';
200 intok = 0;
201 sptok = 1;
202 } else if ( sptok && isspace(ch)) {
203 nowspace = 1;
204 if ( ch=='\n' || ch=='\r' )
205 break;
206 } else if ( sptok && !isdigit(ch) )
207 sptok = 0;
208 else if ( rpt!=NULL && ch==*rpt ) {
209 if ( *++rpt=='\0' ) {
210 /* it matched the character definition string so this is the */
211 /* token we want to search for */
212 strcpy(rdtok,temptok);
213 useshex = willbehex;
214 }
215 } else if ( isdigit(ch)) {
216 nownum = 1;
217 sptok = 0;
218 if ( innum )
219 val = 10*val + ch-'0';
220 else
221 val = ch-'0';
222 } else if ( isspace(ch)) {
223 nowspace = 1;
224 if ( ch=='\n' || ch=='\r' )
225 break;
226 } else if ( wasspace && ch==*rdtok ) {
227 nowr = 1;
228 rdpt = rdtok+1;
229 } else if ( inr && ch==*rdpt ) {
230 if ( *++rdpt =='\0' ) {
231 ch = getc(temp);
232 *pt++ = ch;
233 if ( isspace(ch) && val!=0 ) {
234 inhex = useshex;
235 inbinary = !useshex;
236 cnt = val;
237 binstart = pt;
238 binlen = val;
239 if ( binlen>sizeof(buffer)) {
240 fprintf(stderr, "Buffer overflow needs to be at least %d\ndying gracefully.\n", binlen);
241 exit(1);
242 }
243 }
244 } else
245 nowr = 1;
246 }
247 innum = nownum; wasspace = nowspace; inr = nowr;
248 }
249 if ( ch=='\r' ) {
250 ch=getc(temp);
251 if ( ch!='\n' )
252 ungetc(ch,temp);
253 pt[-1]='\n';
254 }
255 *pt = '\0';
256 if ( binstart==NULL ) {
257 if (( pt = strstr(buffer,"/lenIV"))!=NULL )
258 leniv = strtol(pt+6,NULL,0);
259 fputs(buffer,out);
260 } else {
261 for ( pt=buffer; pt<binstart; ++pt )
262 putc(*pt,out);
263 decodebytes(out,(unsigned char *) binstart,binlen);
264 for ( pt=binstart+binlen; *pt; ++pt )
265 putc(*pt,out);
266 }
267 return( 1 );
268 }
269
270 static int nrandombytes[4];
271 #define EODMARKLEN 16
272
273 #define bgetc(extra,in) (*(extra)=='\0' ? getc(in) : (unsigned char ) *(extra)++ )
274 static void decrypteexec(FILE *in,FILE *temp, int hassectionheads, char *extra) {
275 int ch1, ch2, ch3, ch4, binary;
276 int zcnt;
277 unsigned char zeros[EODMARKLEN+6+1];
278 int sect_len;
279
280 while ( (ch1=bgetc(extra,in))!=EOF && (ch1==' ' || ch1=='\t' || ch1=='\n' || ch1=='\r'));
281 /* Mac POST resources also have 6 bytes inserted here. They appear to be */
282 /* a four byte length followed by ^B ^@ */
283 if ( ch1==0200 && hassectionheads ) {
284 /* skip the 6 byte section header in pfb files that follows eexec */
285 ch1 = bgetc(extra,in);
286 sect_len = bgetc(extra,in);
287 sect_len |= bgetc(extra,in)<<8;
288 sect_len |= bgetc(extra,in)<<16;
289 sect_len |= bgetc(extra,in)<<24;
290 sect_len -= 3;
291 ch1 = bgetc(extra,in);
292 }
293 ch2 = bgetc(extra,in); ch3 = bgetc(extra,in); ch4 = bgetc(extra,in);
294 binary = 0;
295 if ( ch1<'0' || (ch1>'9' && ch1<'A') || ( ch1>'F' && ch1<'a') || (ch1>'f') ||
296 ch2<'0' || (ch2>'9' && ch2<'A') || (ch2>'F' && ch2<'a') || (ch2>'f') ||
297 ch3<'0' || (ch3>'9' && ch3<'A') || (ch3>'F' && ch3<'a') || (ch3>'f') ||
298 ch4<'0' || (ch4>'9' && ch4<'A') || (ch4>'F' && ch4<'a') || (ch4>'f') )
299 binary = 1;
300 if ( ch1==EOF || ch2==EOF || ch3==EOF || ch4==EOF ) {
301 return;
302 }
303
304 initcode();
305 if ( binary ) {
306 nrandombytes[0] = decode(ch1);
307 nrandombytes[1] = decode(ch2);
308 nrandombytes[2] = decode(ch3);
309 nrandombytes[3] = decode(ch4);
310 zcnt = 0;
311 while (( ch1=bgetc(extra,in))!=EOF ) {
312 --sect_len;
313 if ( hassectionheads ) {
314 if ( sect_len==0 && ch1==0200 ) {
315 ch1 = bgetc(extra,in);
316 sect_len = bgetc(extra,in);
317 sect_len |= bgetc(extra,in)<<8;
318 sect_len |= bgetc(extra,in)<<16;
319 sect_len |= bgetc(extra,in)<<24;
320 sect_len += 1;
321 if ( ch1=='\1' )
322 break;
323 } else {
324 zcnt = 0;
325 putc(decode(ch1),temp);
326 }
327 } else {
328 if ( ch1=='0' || isspace(ch1) ) ++zcnt; else {dumpzeros(temp,zeros,zcnt); zcnt = 0; }
329 if ( zcnt>EODMARKLEN )
330 break;
331 if ( zcnt==0 )
332 putc(decode(ch1),temp);
333 else
334 zeros[zcnt-1] = decode(ch1);
335 }
336 }
337 } else {
338 nrandombytes[0] = decode(hex(ch1,ch2));
339 nrandombytes[1] = decode(hex(ch3,ch4));
340 ch1 = bgetc(extra,in); ch2 = bgetc(extra,in); ch3 = bgetc(extra,in); ch4 = bgetc(extra,in);
341 nrandombytes[2] = decode(hex(ch1,ch2));
342 nrandombytes[3] = decode(hex(ch3,ch4));
343 zcnt = 0;
344 while (( ch1=bgetc(extra,in))!=EOF ) {
345 while ( ch1!=EOF && isspace(ch1)) ch1 = bgetc(extra,in);
346 while ( (ch2=bgetc(extra,in))!=EOF && isspace(ch2));
347 if ( ch1=='0' && ch2=='0' ) ++zcnt; else { dumpzeros(temp,zeros,zcnt); zcnt = 0;}
348 if ( zcnt>EODMARKLEN )
349 break;
350 if ( zcnt==0 )
351 putc(decode(hex(ch1,ch2)),temp);
352 else
353 zeros[zcnt-1] = decode(hex(ch1,ch2));
354 }
355 }
356 while (( ch1=bgetc(extra,in))=='0' || isspace(ch1) );
357 if ( ch1!=EOF ) ungetc(ch1,in);
358 }
359
360 static void decryptbinary(FILE *in,FILE *temp, char *line, long solpos) {
361 int i, cnt, ch;
362 char *pt;
363
364 fprintf( stderr, "This program does not handled cid-keyed fonts. Sorry\n" );
365 exit(1);
366 pt = strstr(line,"(Binary)");
367 pt += strlen("(Binary)");
368 cnt = strtol(pt,NULL,10);
369
370 pt = strstr(line,"StartData ");
371 pt += strlen("StartData ");
372
373 solpos += pt-line;
374 fseek(in,SEEK_SET,solpos);
375
376 initcode();
377 nrandombytes[0] = decode(getc(in));
378 nrandombytes[1] = decode(getc(in));
379 nrandombytes[2] = decode(getc(in));
380 nrandombytes[3] = decode(getc(in));
381 for ( i = 0; ( ch=getc(in))!=EOF && i<cnt; ++i )
382 putc(decode(ch),temp);
383 }
384
385 static void decryptagain(FILE *temp,FILE *out) {
386 char rdtok[255];
387 strcpy(rdtok,"RD");
388 while ( glorpline(temp,out,rdtok));
389 }
390
391 static void doubledecrypt(char *outputfile,char *fontname) {
392 FILE *in, *temp, *out;
393 char buffer[256]/*, *tempname*/;
394 char *pt;
395 int first, hassectionheads;
396 int mightbegsf = 1;
397 long oldpos;
398
399 leniv = 4;
400 useshex = 0;
401
402 in = fopen(fontname,"rb");
403 if ( in==NULL ) {
404 fprintf( stderr, "Cannot open %s\n", fontname );
405 return;
406 }
407 if ( outputfile==NULL ) {
408 pt = strrchr(fontname,'/'); if ( pt==NULL ) pt = fontname; else ++pt;
409 sprintf( buffer,"%s.decrypt", pt);
410 outputfile=buffer;
411 }
412 out = fopen(outputfile,"wb");
413 if ( out==NULL ) {
414 fprintf( stderr, "Cannot open %s for output\n", outputfile );
415 fclose(in);
416 return;
417 }
418
419 temp = tmpfile();
420 if ( temp==NULL ) {
421 fprintf( stderr, "Cannot open temporary file\n" );
422 fclose(in); fclose(out);
423 return;
424 }
425
426 first = 1; hassectionheads = 0;
427 oldpos = ftell(in);
428 while ( myfgets(buffer,sizeof(buffer),in)!=NULL ) {
429 /* Mac POST resources also have 6 bytes inserted here. They appear to be */
430 /* a four byte length followed by ^A ^@ */
431 if ( first && buffer[0]=='\200' ) {
432 hassectionheads = 1;
433 fputs(buffer+6,out);
434 } else
435 fputs(buffer,out);
436 first = 0;
437 if ( strstr(buffer,"currentfile")!=NULL && strstr(buffer, "eexec")!=NULL )
438 break;
439 if ( strstr(buffer,"(Binary)")!=NULL && strstr(buffer, "StartData")!=NULL )
440 break;
441 if ( strstr(buffer,"Blend")!=NULL )
442 mightbegsf = 0;
443 if ( mightbegsf ) {
444 if ( strstr(buffer,"/Private")!=NULL || strstr(buffer,"/Subrs")!=NULL ||
445 strstr(buffer,"/CharStrings")!=NULL )
446 break;
447 if ( strstr(buffer,"/CIDInit")!=NULL )
448 mightbegsf = 0;
449 }
450 oldpos = ftell(in);
451 }
452
453 if ( strstr(buffer,"currentfile")!=NULL && strstr(buffer, "eexec")!=NULL ) {
454 decrypteexec(in,temp,hassectionheads,strstr(buffer, "eexec")+5);
455 rewind(temp);
456 decryptagain(temp,out);
457 } else if ( strstr(buffer,"(Binary)")!=NULL && strstr(buffer, "StartData")!=NULL ) {
458 decryptbinary(in,temp,buffer,oldpos);
459 rewind(temp);
460 decryptagain(temp,out);
461 } else
462 decryptagain(in,out);
463 while ( myfgets(buffer,sizeof(buffer),in)!=NULL ) {
464 if ( buffer[0]!='\200' || !hassectionheads )
465 fputs(buffer,out);
466 }
467 fclose(in); fclose(out); fclose(temp);
468 }
469
470 int main( int argc, char **argv) {
471 int i;
472 char *outputfile=NULL;
473
474 for ( i=1; i<argc; ++i ) {
475 if ( strcmp("-o",argv[i])==0 )
476 outputfile = argv[++i];
477 else {
478 doubledecrypt(outputfile,argv[i]);
479 }
480 }
481 return( 0 );
482 }