Debian Codegraph @

texk/dvipsk/t1part.c

raw
1 /*
2 * T1part.c version 1.59 beta Copyright (C)1994, 1996
3 * by Sergey Lesenko
4 * [email protected]
5 *
6 * It is distributed with no warranty of any kind.
7 * You may modify and use this program. It can be included
8 * in any distribution, commercial or otherwise, so long as
9 * copyright notice be preserved on all copies.
10 */
11 #include "dvips.h"
12 #include "t1part.h"
13 #ifndef SEEK_SET
14 #define SEEK_SET (0)
15 #endif
16
17 #ifdef BORLANDC
18 void huge *UniGetMem(size)
19 ub4 size;
20 {
21 void huge *tmp;
22 if ((tmp =(void huge*) farcalloc(1L, size)) == NULL)
23 {
24 fprintf(stderr,"Error allocating far memory\n");
25 exit(1);
26 }
27 return tmp;
28 }
29 #else
30 #define UniGetMem getmem
31 #endif
32
33 #ifdef WIN32
34 /* CHAR is typedef'd by <windows.h> -- [email protected]. */
35 #define CHAR CHARACTER
36 #endif
37
38 typedef struct Char
39 {
40 unsigned char *name;
41 int length;
42 int num;
43 int choose;
44 struct Char *NextChar;
45 }
46 CHAR;
47
48 typedef struct String
49 {
50 unsigned char *name;
51 int num;
52 struct String *NextStr;
53 }
54 STRING;
55
56 static STRING *FirstStr;
57 static STRING *RevStr;
58
59 static CHAR *FirstChar;
60 static CHAR *FirstCharA;
61 CHAR *FirstCharB;
62 static CHAR *FirstCharW;
63
64 int CharCount;
65 int GridCount;
66 int ind_ref;
67
68 static unsigned char CDeCrypt(unsigned char, unsigned int *);
69 static void CorrectGrid(void);
70 static void OutHEX(FILE *);
71 static int Afm(void);
72 static int LoadVector(int, CHAR *);
73 static int ChooseVect(CHAR *);
74 static void ErrorOfScan(int err);
75 static void NameOfProgram(void);
76
77 typedef struct
78 {
79 int num[4];
80 int select;
81 }
82 def_ref;
83
84 def_ref refer[10];
85
86 #ifndef min
87 #define min(a, b) ((a) < (b) ? (a) : (b))
88 #endif
89
90 #ifndef max
91 #define max(a, b) ((a) > (b) ? (a) : (b))
92 #endif
93
94
95 #define PFA 1
96 #define PFB 2
97
98
99 #define NOTHING 0
100 #define FLG_ASCII 1
101 #define FLG_BINARY 2
102 #define FLG_EOF 3
103 #define FLG_ZERO_LINE 4
104
105 #define FLG_OUT_STR (-1)
106 #define CHAR_NOT_DEF (-1)
107 #define SEAC_NOT_END (-2)
108
109 #define FLG_LOAD_BASE (1)
110
111 #define STANDARD_ENC (1)
112 #define SPECIAL_ENC (2)
113 #define AFM_ENC (4)
114
115
116 #define FLG_REENCODE (4)
117
118
119 #define BYTES_PER_LINE 32
120
121 #ifndef KPATHSEA_TYPES_H
122 #define TRUE 1
123 #define FALSE 0
124 #endif /* not KPATHSEA_TYPES_H */
125
126 #define LENIV 0
127 #define SUBRS 1
128 #define CHARSTRINGS 2
129
130 #define ERR_FIRST_NUM (-1)
131 #define ERR_SECOND_NUM (-2)
132 #define ERR_FIRST_TOK (-3)
133 #define ERR_SECOND_TOK (-4)
134 #define ERR_STACK (-5)
135 #define ERR_NUM_CHAR (-6)
136 #define ERR_NAME_CHAR (-7)
137
138 #define SUBR_STR 1
139 #define CHAR_STR -1
140 #define CHAR_SEAC -2
141 #define SKIP_ON_DUP 3
142
143 #define C1 52845
144 #define C2 22719
145 #define EDR 55665
146 #define EER 55665
147 #define CDR 4330
148
149 #define MAX_ESCAPE 33
150
151 #define HSTEM 1
152 #define VSTEM 3
153 #define VMOVETO 4
154 #define CHARS_RLINETO 5
155 #define HLINETO 6
156 #define VLINETO 7
157 #define RRCURVETO 8
158 #define CHARS_CLOSEPATH 9
159 #define CALLSUBR 10
160 #define RETURN 11
161 #define ESCAPE 12
162 #define HSBW 13
163 #define ENDCHAR 14
164 #define CHARS_RMOVETO 21
165 #define HMOVETO 22
166 #define VHCURVETO 30
167 #define HVCURVETO 31
168
169 #define DOTSECTION 0
170 #define VSTEM3 1
171 #define HSTEM3 2
172 #define SEAC 6
173 #define SBW 7
174 #define CHARS_DIV 12
175 #define CALLOTHERSUBR 16
176 #define POP 17
177 #define SETCURRENTPOINT 33
178
179 typetemp BORLAND_HUGE *temp;
180 typetemp BORLAND_HUGE *begin_of_scan;
181 typetemp BORLAND_HUGE *end_of_scan;
182 unsigned char *line;
183 unsigned char *tmpline;
184 unsigned char token[64];
185 unsigned char notdef[]="/.notdef";
186 unsigned char grid[256];
187 unsigned char tmpgrid[256];
188 unsigned char psfontfile[500]; /* really should dynamically allocate */
189 unsigned char psvectfile[500];
190 unsigned char basevect[]="ad.enc";
191 unsigned char version[] = "v1.59 beta (c)1994, 1996";
192 unsigned char tmp_token[64];
193 /* static int j = 0; */
194 static int stack[128];
195
196 ub1 buf[BUFSIZ];
197
198 int loadbase = 0;
199 static int encode;
200 static int reencode;
201
202 int find_encod;
203 int lastpart=0;
204 int keep_flg=0;
205 int keep_num=0;
206
207 int number;
208 int offset;
209 long value;
210 int lenIV = 4;
211
212 static int grow=0;
213 static int level;
214 static int flg_seac=0;
215
216 typedef struct
217 {
218 const char *command;
219 int code;
220 }
221 tablecommand;
222
223 tablecommand TableCommand[] =
224 {
225 {"callsubr", CALLSUBR}, {"callothersubr", CALLOTHERSUBR},
226 {"pop", POP }, {"seac", SEAC}, {""}
227 };
228
229 typedef struct
230 {
231 const char *extension;
232 int num;
233 }
234 typefonts;
235
236
237 typefonts TypeFonts[] =
238 {
239 {".pfa", PFA}, {".pfb", PFB},
240 {".PFA", PFA}, {".PFB", PFB}, {""}
241 };
242
243 static char Dup[]="dup";
244
245 typedef struct
246 {
247 typetemp BORLAND_HUGE *begin;
248 int length;
249 int type;
250 int offset;
251 int oldnum;
252 int newnum;
253 }
254 def_key;
255
256 def_key keyword[6];
257
258 int FirstKey, current;
259
260 int subrs, char_str;
261
262
263 typedef struct
264 {
265 const char *name;
266 int num;
267 }
268 type_key;
269
270
271 type_key Key[] =
272 {
273 {"/LenIV", LENIV}, {"/Subrs", SUBRS}, {"/CharStrings", CHARSTRINGS}, {""}
274 };
275
276
277 struct def_label
278 {
279 typetemp BORLAND_HUGE *begin;
280 unsigned char skip;
281 int length;
282 short select;
283 int num;
284 };
285
286 struct def_label label[NUM_LABEL];
287
288
289
290 static int
291 DefTypeFont(unsigned char *name)
292 {
293 int i;
294
295 for(i=0;*TypeFonts[i].extension;i++)
296 {
297 if(strstr((char *)name,TypeFonts[i].extension)!=0)
298 return(TypeFonts[i].num);
299 }
300 return -1;
301 }
302
303 static int
304 GetZeroLine(unsigned char *str)
305 {
306 int token_type=0;
307 if(*str!='0')
308 {
309 return (token_type=0);
310 }
311 while(*str=='0')
312 {
313 str++;
314 }
315
316 if(*str=='\n' || *str=='\r')
317 token_type= -1;
318 else
319 token_type=0;
320 return(token_type);
321 }
322
323 /* We get token type and its content for ASCII code */
324
325 static int
326 GetWord(unsigned char *mem)
327 {
328 int token_type=0;
329 register unsigned char *tmp;
330 tmp=mem;
331 *tmp= *line;
332 while((*line!='\0')&&(*line!='%'))
333 {
334 if(*line=='-')
335 {
336 *tmp++= *line++;
337 }
338
339 if(isdigit(*line))
340 {
341 while(isdigit(*line))
342 {
343 *tmp++= *line++;
344 }
345 *tmp = '\0';
346 return 5;
347 }
348
349 if(*line=='/')
350 {
351 *tmp++= *line++; token_type=1;
352 }
353 if(*line=='.')
354 {
355 *tmp++= *line++;
356
357 if(token_type==1)
358 {
359 if(*line==' ')
360 {
361 *tmp = '\0';
362 return(token_type=token_type+2);
363 }
364 }
365 }
366 if(isalpha(*line))
367 {
368 *tmp++ = *line++;
369 while(!isspace(*line) && *line != '/')
370 *tmp++= *line++;
371 *tmp = '\0';
372 return(token_type=token_type+2);
373 }
374
375 token_type=0;
376 tmp=mem;
377 line++;
378 }
379 return(token_type= -1);
380 }
381
382 /* We get token type and its content for BINARY code */
383
384 static int
385 GetToken(void)
386 {
387 register unsigned char *tmp;
388 int token_type=0;
389
390 tmp=token;
391
392 *tmp= *temp;
393 while(temp<end_of_scan)
394 {
395 if(*temp=='/')
396 {
397 *tmp++= *temp++; token_type=1;
398 }
399
400 if(*temp=='.')
401 {
402 *tmp++= *temp++;
403 token_type++;
404 }
405
406 if(isalpha(*temp))
407 {
408 while(isalnum(*temp) || *temp == '.')
409 *tmp++= *temp++;
410
411 *tmp = '\0';
412
413 return(token_type=token_type+2);
414 }
415 token_type=0;
416 tmp=token;
417 temp++;
418 }
419 return(token_type= -1);
420 }
421
422 static int
423 GetNum(void)
424 {
425 unsigned char *tmp;
426 tmp=token;
427 *tmp= *temp;
428
429 while(temp<end_of_scan)
430 {
431 if(isdigit(*temp))
432 {
433 while(isdigit(*temp))
434 *tmp++= *temp++;
435 *tmp = '\0';
436 return(atoi((char *)token));
437 }
438 temp++;
439 }
440
441 return -1;
442 }
443
444 /* We pass token without definition its type, it's for speed */
445
446 static int
447 PassToken(void)
448 {
449 while(temp < end_of_scan)
450 {
451 if(!isspace(*temp))
452 {
453 while(!isspace(*temp))
454 temp++;
455 return 1;
456 }
457 temp++;
458 }
459 return -1;
460 }
461
462 /* */
463 /* Simple pass off without charstring decrypting */
464 /* */
465
466 static int
467 PassString(unsigned char flg)
468 {
469 int len_str;
470
471 len_str=GetNum();
472 if(len_str<0)
473 {
474 return ERR_SECOND_NUM;
475 }
476
477 if(PassToken()<0)
478 return ERR_FIRST_TOK;
479
480
481 if(flg==1)
482 {
483 label[number].length = len_str;
484 label[number].skip = temp - label[number].begin;
485 }
486
487 temp= temp+1+len_str;
488
489 if(PassToken()<0)
490 return ERR_SECOND_TOK;
491 return 1;
492 }
493
494 void *
495 getmem(unsigned size)
496 {
497 void *tmp;
498 if ((tmp = calloc(1, size)) == NULL)
499 {
500 fprintf(stderr,"Error allocating memory\n");
501 exit(1);
502 }
503 return tmp;
504 }
505
506 static CHAR *
507 AddChar(CHAR *TmpChar, unsigned char *CharName, int num)
508 {
509 int length;
510
511 CHAR *ThisChar = (CHAR*) getmem(sizeof(CHAR));
512 length = strlen((char *) CharName);
513 ThisChar->name = (unsigned char *) getmem(length+1);
514 strcpy((char *) ThisChar->name, (char *) CharName);
515 ThisChar->length= length;
516 ThisChar->num=num;
517 ThisChar->NextChar = TmpChar;
518 TmpChar = ThisChar;
519 return TmpChar;
520 }
521
522
523 static void
524 AddStr(unsigned char *name, int num)
525 {
526 int length;
527
528 STRING *ThisStr = (STRING *) getmem(sizeof(STRING));
529 length = strlen((char *) name);
530 ThisStr->name = (unsigned char *) getmem(length+1);
531 strcpy((char *) ThisStr->name, (char *) name);
532 ThisStr->num=num;
533
534 ThisStr->NextStr = FirstStr;
535 FirstStr = ThisStr;
536 }
537
538
539 /* We prepare own encoding vector for output */
540
541 static void
542 RevChar(CHAR *TmpChar)
543 {
544 int i;
545 CHAR *ThisChar = TmpChar;
546
547 while (ThisChar != NULL)
548 {
549 for(i=keyword[char_str].offset-1; i < number; i++)
550 {
551 if(ThisChar->num == label[i].num)
552 {
553 if (label[i].select==FLG_BINARY)
554 {
555 CHAR *Rev_Char = (CHAR *) getmem(sizeof(CHAR));
556 Rev_Char->name = ThisChar->name;
557 Rev_Char->num = ThisChar->num;
558
559 Rev_Char->NextChar = FirstCharW;
560 FirstCharW = Rev_Char;
561 break;
562 }
563 }
564 }
565 ThisChar = ThisChar->NextChar;
566 }
567 }
568
569 /* And here we produce own resulting encoding vector for partial font */
570
571 static void
572 OutChar(CHAR *TmpChar, FILE *fout)
573 {
574
575 CHAR *ThisChar = TmpChar;
576
577 while (ThisChar != NULL)
578 {
579 CHAR *tm = ThisChar;
580 if (ThisChar->num >= 0)
581 #ifdef SHIFTLOWCHARS
582 fprintf(fout, "dup %d %s put\n",T1Char(ThisChar->num),ThisChar->name);
583 #else
584 fprintf(fout, "dup %d %s put\n",ThisChar->num,ThisChar->name);
585 #endif
586 ThisChar = ThisChar->NextChar;
587 free(tm);
588 }
589
590 FirstCharW = NULL;
591 }
592
593
594
595
596 /* We prepare strings list for output */
597
598 static void
599 Reeverse(STRING *TmpStr)
600 {
601
602 int tmp;
603
604 if(encode==AFM_ENC)
605 tmp = -2;
606 else
607 tmp=0;
608
609
610 while (TmpStr != NULL)
611 {
612
613 if(TmpStr->num < tmp)
614 {
615 STRING *ThisStr = (STRING *) getmem(sizeof(STRING));
616 ThisStr->name = TmpStr->name;
617
618 ThisStr->NextStr = RevStr;
619 RevStr = ThisStr;
620 }
621 TmpStr = TmpStr->NextStr;
622 }
623 }
624
625
626 /* And here we post strings to out */
627
628 static void
629 OutStr(STRING *TmpStr, FILE *fout)
630 {
631 STRING *ThisStr = TmpStr;
632 if(encode==AFM_ENC)
633 fprintf(fout, "readonly def\n");
634 while (ThisStr != NULL)
635 {
636 STRING *tm = ThisStr;
637 fprintf(fout, "%s",ThisStr->name);
638 ThisStr = ThisStr->NextStr;
639 free(tm);
640 }
641 RevStr = NULL;
642 }
643
644
645
646
647
648
649 static void
650 PrintChar(CHAR *TmpChar)
651 {
652 CHAR *ThisChar = TmpChar;
653 while (ThisChar != NULL)
654 {
655 if(ThisChar->choose==1)
656 {
657 fprintf_str(stderr, " Debug: Char %d '%s'\n",
658 ThisChar->num,ThisChar->name);
659 }
660 ThisChar = ThisChar->NextChar;
661 }
662
663 }
664
665
666 static int
667 ClearB(void)
668 {
669 CHAR *ThisChar = FirstCharB;
670 while (ThisChar != NULL)
671 {
672 ThisChar->choose=0;
673 ThisChar = ThisChar->NextChar;
674 }
675 return 1;
676 }
677
678 /* We mark chars in encoding vector thanks same names from
679 reencoding vector */
680
681 static int
682 ChooseChar(unsigned char *name, CHAR *TmpChar)
683 {
684 int length;
685 CHAR *ThisChar = TmpChar;
686 length=strlen((char *) name);
687 while (ThisChar != NULL)
688 {
689 CHAR *NextChar = ThisChar->NextChar;
690 if(ThisChar->length==length)
691 {
692 if (strcmp((char *) name, (char *) ThisChar->name) == 0)
693 {
694 ThisChar->choose=1;
695 return 1;
696 }
697 }
698 /*
699 * If we couldn't find it, NextChar is NULL here. We fake up an entry
700 * (assuming it really does exist in the font). Fix is from Melissa
701 * O'Neill; bugs in application of fix due to me.
702 */
703 if (NextChar == 0) {
704 NextChar = (CHAR *) getmem(sizeof(CHAR));
705 NextChar->name = (unsigned char *) getmem(length + 1);
706 strcpy((char *) NextChar->name, (char *) name);
707 NextChar->length = length;
708 NextChar->num = -1;
709 NextChar->NextChar = 0;
710 NextChar->choose = 1;
711 ThisChar->NextChar = NextChar;
712 }
713 ThisChar = NextChar;
714 }
715 /*
716 * Can't ever get here.
717 */
718 return -1;
719 }
720
721 /* We find index in label array for char, wich is required
722 for compose char, if it uses SEAC command */
723
724 static int
725 FindSeac(int num)
726 {
727 int i;
728
729 for(i=keyword[char_str].offset-1; i < number; i++)
730 {
731 if(label[i].num==num)
732 {
733 return i;
734 }
735 }
736 return -1;
737 }
738
739
740 static void ClearCW(CHAR *);
741
742 static int
743 FindCharW(unsigned char *name, int length)
744 {
745 CHAR *ThisChar = FirstCharW;
746
747 int find = 0;
748 int keep_char = 0;
749 int tmp = 0;
750 int tmp_num = 0;
751
752 int ddd = 0;
753
754 #ifdef DEBUG
755 if(dd(D_VIEW_VECTOR))
756 {
757 ddd = 1;
758 }
759 #endif
760
761 while (ThisChar != NULL)
762 {
763 /* since ClearCW may free ThisChar, follow the pointer now. */
764 CHAR *NextChar = ThisChar->NextChar;
765
766 if(ThisChar->length==length)
767 {
768 if (strcmp((char *) name, (char *) ThisChar->name) == 0)
769 {
770 if(ThisChar->choose==1)
771 {
772 if(find !=2)
773 {
774 find = 2;
775 tmp=FLG_BINARY;
776 label[number].num=ThisChar->num;
777 }
778 }
779 else
780 {
781 keep_char = 0;
782 if(find != 2)
783 {
784 find = 1;
785 tmp_num = ThisChar->num;
786 }
787 }
788
789
790 if(keep_char == 0)
791 {
792 ClearCW(ThisChar);
793 }
794
795 if((find == 2)&&(ddd == 1))
796 keep_char = 1;
797 }
798 }
799 ThisChar = NextChar;
800 }
801
802 if(find == 1)
803 {
804 tmp=NOTHING;
805 label[number].num=tmp_num;
806 }
807 return tmp;
808 }
809
810
811 static void
812 ClearCW(CHAR *ThisChar)
813 {
814
815 if (ThisChar == FirstCharW)
816 FirstCharW = ThisChar->NextChar;
817 else
818 {
819 CHAR *tm = FirstCharW;
820 while (tm != NULL)
821 {
822 if (ThisChar == tm->NextChar)
823 {
824 tm->NextChar = ThisChar->NextChar;
825 break;
826 }
827 tm = tm->NextChar;
828 }
829 }
830 free(ThisChar);
831 }
832
833
834 /* We build temporary 'work' encoding vector only for searching
835 needed chars */
836
837 static int
838 WorkVect(CHAR *TmpChar)
839 {
840 while (TmpChar != NULL) {
841 {
842 CHAR *ThisChar = (CHAR *) getmem(sizeof(CHAR));
843 ThisChar->name = TmpChar->name;
844 ThisChar->length = TmpChar->length;
845 ThisChar->num = TmpChar->num;
846 ThisChar->choose = TmpChar->choose;
847
848 ThisChar->NextChar = FirstCharW;
849 FirstCharW = ThisChar;
850 }
851 TmpChar = TmpChar->NextChar;
852 }
853 return 0;
854 }
855
856
857 static void
858 UnDefineCharsW(void)
859 {
860 CHAR *ThisChar = FirstCharW;
861 while (ThisChar != NULL)
862 {
863 CHAR *tm = ThisChar;
864 ThisChar = ThisChar->NextChar;
865 free(tm);
866 }
867 FirstCharW = NULL;
868 CharCount = 0;
869 }
870
871 CHAR *
872 UnDefineChars(CHAR *TmpChar)
873 {
874 CHAR *ThisChar = TmpChar;
875 while (ThisChar != NULL)
876 {
877 CHAR *tm = ThisChar;
878 free(ThisChar->name);
879 ThisChar = ThisChar->NextChar;
880 free(tm);
881 }
882 TmpChar = NULL;
883 CharCount = 0;
884 return TmpChar;
885 }
886
887
888
889 static void
890 UnDefineStr(void)
891 {
892 STRING *ThisStr = FirstStr;
893 while (ThisStr != NULL)
894 {
895 STRING *tm = ThisStr;
896 free(ThisStr->name);
897 ThisStr = ThisStr->NextStr;
898 free(tm);
899 }
900 FirstStr = NULL;
901 }
902
903
904
905 /* */
906 /* We mark subroutines without charstring decrypting */
907 /* */
908
909 static void
910 ScanSubrs(int i)
911 {
912 int err_num;
913 int word_type = 0;
914 int len_dup;
915 int num_err=0;
916 int test=0;
917
918 len_dup = strlen(Dup);
919
920 for(; number < keyword[i].oldnum + keyword[i].offset;)
921 {
922 if((word_type=GetToken())>0)
923 {
924 if(word_type==2)
925 {
926 if(!strcmp((char *) token,Dup))
927 {
928 if(test==0)
929 test=1;
930 label[number].begin = temp-len_dup;
931
932 err_num=GetNum();
933 if(err_num<0)
934 ;
935 else
936 {
937 if(err_num<4)
938 {
939 label[number].select=FLG_BINARY;
940 keyword[i].newnum++;
941
942 }
943 }
944
945 label[number].num=err_num;
946
947 num_err=PassString(1);
948
949 if(num_err<0)
950 {
951 ErrorOfScan(num_err);
952 fprintf(stderr,"in %d Subr string", number - 1);
953 exit(1);
954 }
955 if(test>1)
956 PassToken();
957
958 number++;
959 }
960 else
961 {
962 if(test>=1)
963 test++;
964 }
965 }
966 }
967 else
968 {
969 ErrorOfScan(0);
970 fprintf(stderr,
971 "Token 'def' not found in %d Subr string ", number - 1);
972 exit(1);
973 }
974 }
975 }
976
977 static void
978 ViewReturnCall(int num_err, int top, int *pstack,
979 int j, int depth)
980 {
981 int k,m;
982
983 #ifdef DEBUG
984 if((dd(D_CALL_SUBR))&&(num_err>0))
985 {
986 if(grow==1)
987 {
988 grow=0;
989 fprintf(stderr, "\n Top: ");
990 }
991 else
992 fprintf(stderr, " Back: ");
993 }
994
995 #endif
996
997 if(num_err<0)
998 {
999 if(grow==1)
1000 {
1001 grow=0;
1002 fprintf(stderr, "\n ERROR: ");
1003 ErrorOfScan(num_err);
1004 }
1005 else
1006 fprintf(stderr, " Back: ");
1007 }
1008
1009 fprintf(stderr, " %d Subr \n", top);
1010
1011 fprintf(stderr," %dth level> STACK: ", level);
1012
1013 for(m=0; m < j; m++, pstack++)
1014 {
1015 if(depth>(j-(m+1)))
1016 {
1017 for(k=0;
1018 TableCommand[k].command;
1019 k++)
1020 {
1021 if(TableCommand[k].code==*pstack)
1022 {
1023 fprintf_str(stderr," %s",
1024 TableCommand[k].command);
1025 k=0;
1026 break;
1027 }
1028 }
1029 if(k!=0)
1030 fprintf(stderr," (%d)", *pstack);
1031 }
1032 else
1033 fprintf(stderr, " %d", *pstack);
1034 }
1035 fprintf(stderr, " \n");
1036 }
1037
1038 /* */
1039 /* We decrypt charstring with recursive descent */
1040 /* */
1041
1042 static int
1043 DeCodeStr(int num, int numseac)
1044 {
1045 unsigned int loccr;
1046 unsigned char byte;
1047 static int j;
1048 int i;
1049 unsigned char jj,k;
1050 int tmpnum;
1051 int depth = 0;
1052 int num_err = 0;
1053 int len_str;
1054 typetemp BORLAND_HUGE *loc;
1055 typetemp BORLAND_HUGE *end_str;
1056 int pstack[64];
1057 int last_subr;
1058
1059 if(num > CHAR_STR)
1060 {
1061 last_subr=keyword[subrs].offset+keyword[subrs].oldnum;
1062
1063 for(tmpnum=keyword[subrs].offset; tmpnum<last_subr; tmpnum++)
1064 {
1065 if(label[tmpnum].num==num)
1066 break;
1067 }
1068
1069 if(tmpnum==last_subr)
1070 {
1071 fprintf(stderr, " Error: %d Subr not found \n", num);
1072 exit(1);
1073 }
1074 if(label[tmpnum].select==FLG_BINARY)
1075 {
1076 if(j==0)
1077 return 1;
1078 }
1079 else
1080 {
1081 label[tmpnum].select=FLG_BINARY;
1082 if(num+1 > keyword[subrs].newnum ) /* max num of subr */
1083 keyword[subrs].newnum = num+1;
1084 }
1085 loc = label[tmpnum].begin + label[tmpnum].skip;
1086 len_str=label[tmpnum].length;
1087 }
1088 else
1089 {
1090 j=0;
1091
1092 if(num == CHAR_SEAC)
1093 {
1094 if(label[numseac].select!=FLG_BINARY)
1095 {
1096 label[numseac].select=FLG_BINARY;
1097 keyword[char_str].newnum++;
1098 temp = label[numseac].begin;
1099 }
1100 else return 1;
1101 }
1102 len_str=GetNum();
1103
1104 if(len_str < 0)
1105 {
1106 return ERR_SECOND_NUM;
1107 }
1108 num_err = PassToken();
1109 if(num_err < 0)
1110 {
1111 return ERR_FIRST_TOK;
1112 }
1113 loc=temp;
1114 }
1115 loc++;
1116
1117 end_str=loc+len_str;
1118
1119 loccr = CDR;
1120
1121 for (i = 0; i < lenIV; i++,loc++)
1122 {
1123 byte = CDeCrypt(*loc, &loccr);
1124 }
1125 for (; loc < end_str;)
1126 {
1127 byte = CDeCrypt(*loc++, &loccr);
1128 if (byte == RETURN)
1129 {
1130 j=0;
1131 break;
1132 }
1133 else if (byte == ESCAPE)
1134 {
1135 byte = CDeCrypt(*loc++, &loccr);
1136 if (byte > MAX_ESCAPE)
1137 fprintf_str(stderr,
1138 "Error: not_defined_e%d in %s", byte, psfontfile);
1139 else
1140 {
1141 switch(byte)
1142 {
1143 case DOTSECTION : j=0; break;
1144 case VSTEM3 : j=0; break;
1145 case HSTEM3 : j=0; break;
1146 case SEAC :
1147 stack[j++]=byte;
1148 grow=1;
1149 level++;
1150 jj=j;
1151 for(k=0;k<jj;k++)
1152 pstack[k]=stack[k];
1153 num_err=FindSeac(pstack[jj-3]);
1154 if(num_err<0)
1155 {
1156 flg_seac=1;
1157 CharCount++;
1158 keyword[char_str].newnum--;
1159 if(flg_seac > -3)
1160 label[number].select=SEAC_NOT_END;
1161 grow=0;
1162 level=0;
1163 j=0;
1164 break;
1165 }
1166
1167 num_err=DeCodeStr(CHAR_SEAC, num_err);
1168 level--;
1169
1170 #ifdef DEBUG
1171 if((num_err<0)||(dd(D_CALL_SUBR)))
1172 #else
1173 if(num_err<0)
1174 #endif
1175 ViewReturnCall
1176 (num_err, pstack[jj-3],pstack,jj,1);
1177 grow=1;
1178 level++;
1179 num_err=FindSeac(pstack[jj-2]);
1180 if(num_err<0)
1181 {
1182 flg_seac=1;
1183 CharCount++;
1184 keyword[char_str].newnum--;
1185 keyword[char_str].newnum--;
1186 if(flg_seac > -3)
1187 label[number].select=SEAC_NOT_END;
1188 grow=0;
1189 level=0;
1190 j=0;
1191 break;
1192 }
1193 num_err=DeCodeStr(CHAR_SEAC, num_err);
1194 level--;
1195 #ifdef DEBUG
1196 if((num_err<0)||(dd(D_CALL_SUBR)))
1197 #else
1198 if(num_err<0)
1199 #endif
1200
1201 ViewReturnCall
1202 (num_err, pstack[jj-2],pstack,jj,1);
1203
1204 if(num_err<0)
1205 return ERR_STACK;
1206 j=0; break;
1207 case SBW : j=0; break;
1208 case CHARS_DIV : j=0; break;
1209 case CALLOTHERSUBR : stack[j++]=byte;
1210 depth=depth+2;
1211 break;
1212 case POP : stack[j++]=byte;
1213 depth=depth+2;
1214 break;
1215 case SETCURRENTPOINT : j=0; break;
1216 }
1217 }
1218 continue;
1219 }
1220 else if (byte < 32)
1221 {
1222 switch(byte)
1223 {
1224 case HSTEM : j=0; break;
1225 case VSTEM : j=0; break;
1226 case VMOVETO : j=0; break;
1227 case CHARS_RLINETO : j=0; break;
1228 case HLINETO : j=0; break;
1229 case VLINETO : j=0; break;
1230 case RRCURVETO : j=0; break;
1231 case CHARS_CLOSEPATH : j=0; break;
1232 case CALLSUBR : stack[j++]=byte;
1233 depth=depth+2;
1234 level++;
1235 grow=1;
1236 jj=j;
1237 j=j-depth;
1238 for(k=0;k<jj;k++)
1239 pstack[k]=stack[k];
1240
1241 num_err = DeCodeStr(stack[j],0);
1242
1243 level--;
1244 #ifdef DEBUG
1245 if((num_err<0)||(dd(D_CALL_SUBR)))
1246 #else
1247 if(num_err<0)
1248 #endif
1249 ViewReturnCall
1250 (num_err, pstack[jj-depth], pstack,jj,depth/2);
1251
1252 if(num_err<0)
1253 return ERR_STACK;
1254 else
1255 {
1256 depth=0;
1257 break;
1258 }
1259 case RETURN : j=0; break;
1260 case ESCAPE : break;
1261 case HSBW : j=0; break;
1262 case ENDCHAR : j=0; break;
1263 case CHARS_RMOVETO : j=0; break;
1264 case HMOVETO : j=0; break;
1265 case VHCURVETO : j=0; break;
1266 case HVCURVETO : j=0; break;
1267 }
1268 }
1269 if (byte >= 32)
1270 {
1271 if (byte <= 246)
1272 {
1273 value= byte - 139;
1274 stack[j++]=value;
1275 }
1276 else if ((byte >= 247) && (byte <= 250))
1277 {
1278 value= (byte - 247) * 256 + CDeCrypt(*loc++, &loccr) + 108;
1279 stack[j++]=value;
1280 }
1281 else if ((byte >= 251) && (byte <= 254))
1282 {
1283 value= -(byte - 251) * 256 - CDeCrypt(*loc++, &loccr) - 108;
1284 stack[j++]=value;
1285 }
1286 else if (byte == 255)
1287 {
1288 value = CDeCrypt(*loc++, &loccr);
1289 value <<= 8;
1290 value += CDeCrypt(*loc++, &loccr);
1291 value <<= 8;
1292 value += CDeCrypt(*loc++, &loccr);
1293 value <<= 8;
1294 value += CDeCrypt(*loc++, &loccr);
1295 stack[j++]=value;
1296 }
1297 }
1298 }
1299 if(num == CHAR_STR)
1300 {
1301 temp=loc;
1302 num_err = PassToken();
1303 if(num_err<0)
1304 {
1305 return ERR_SECOND_TOK;
1306 }
1307 }
1308 return 1;
1309 }
1310
1311 /* */
1312 /* We mark only necessary charstring */
1313 /* */
1314
1315
1316 static void
1317 ScanChars(int i)
1318 {
1319
1320 int word_type=0;
1321 int found;
1322 int str_len;
1323 int max_num;
1324 int counter;
1325 int num_err = 0;
1326 typetemp BORLAND_HUGE *tmptemp;
1327
1328
1329 CharCount++;
1330 counter=number;
1331 max_num = keyword[i].offset+keyword[i].oldnum;
1332
1333 while( number < max_num )
1334 {
1335 if((word_type=GetToken())>0)
1336 {
1337 if(word_type>=3)
1338 {
1339 strcpy((char *) tmp_token, (char *) token);
1340 str_len = strlen((char *) token);
1341
1342
1343 if(CharCount!=0)
1344 {
1345
1346 num_err=FindCharW(token, str_len);
1347
1348 if(num_err==FLG_BINARY)
1349 {
1350 CharCount--;
1351 found=num_err;
1352 keyword[i].newnum++;
1353 }
1354 else
1355 {
1356 #ifdef DEBUG
1357
1358 if(dd(D_VIEW_VECTOR)&&(num_err==-1))
1359 {
1360 fprintf_str(stderr,
1361 " Debug: Char '%s' not used in WorkVector\n", token);
1362
1363 }
1364 #endif
1365 if(word_type>3)
1366 {
1367 if(strstr((char *) token, (char *) notdef)!=NULL)
1368 {
1369 CharCount--;
1370 label[number].num = -2;
1371 found=FLG_BINARY;
1372 keyword[i].newnum++;
1373 }
1374 else
1375 found=NOTHING;
1376 }
1377 else
1378 found=NOTHING;
1379 }
1380 }
1381 else
1382 {
1383 found=NOTHING;
1384 }
1385
1386 label[number].begin = temp-str_len;
1387 label[number].select = found;
1388
1389 switch(found)
1390 {
1391 case FLG_BINARY:
1392 tmptemp=temp;
1393 for(subrs=FirstKey; subrs<char_str; subrs++)
1394 {
1395 level=0;
1396 if(subrs!=FirstKey)
1397 {
1398 temp=tmptemp;
1399 }
1400
1401 num_err=DeCodeStr(CHAR_STR,0);
1402
1403 }
1404 #ifdef DEBUG
1405 if(dd(D_CALL_SUBR))
1406 {
1407 if(num_err>0)
1408 fprintf_str(stderr,
1409 " Debug for Char '%s'\n", tmp_token);
1410 }
1411 #endif
1412 break;
1413 case NOTHING:
1414
1415 num_err=PassString(0);
1416 break;
1417 }
1418
1419 if(num_err<0)
1420 {
1421 ErrorOfScan(num_err);
1422 fprintf_str(stderr,"in Char string of '%s'", tmp_token);
1423 exit(1);
1424 }
1425 number++;
1426 }
1427 }
1428 else
1429 {
1430 fprintf_str(stderr,
1431 "\n File <%s> ended before all chars have been found.", psfontfile);
1432
1433 fprintf(stderr,
1434 "\n We scan %d Chars from %d",
1435 counter - (2 + keyword[subrs].oldnum),
1436 keyword[i].oldnum);
1437
1438 if(tmp_token!=NULL)
1439 {
1440 fprintf_str(stderr, "\n Last seen token was '%s'\n", tmp_token);
1441 }
1442 exit(1);
1443 }
1444 }
1445
1446 if(flg_seac!=0)
1447 {
1448 tmptemp=temp;
1449 flg_seac--;
1450 for(; counter<max_num; counter++)
1451 {
1452 if((int) label[counter].select==SEAC_NOT_END)
1453 {
1454 for(subrs=FirstKey; subrs<char_str; subrs++)
1455 {
1456 level=0;
1457 temp=label[counter].begin;
1458 num_err=DeCodeStr(CHAR_STR,0);
1459 }
1460 if(num_err<0)
1461 {
1462 fprintf(stderr," Warning: %d char not choose during SEAC\n",
1463 label[counter].num);
1464 }
1465 else
1466 {
1467 CharCount--;
1468 label[counter].select=FLG_BINARY;
1469 if(CharCount==0)
1470 break;
1471 }
1472 }
1473 }
1474
1475 temp=tmptemp;
1476 }
1477
1478 if(CharCount!=0)
1479 {
1480 fprintf(stderr," WARNING: Not all chars found.");
1481 PrintChar(FirstCharW);
1482
1483 }
1484 }
1485
1486 static void
1487 LastLook(void)
1488 {
1489 label[number].begin = temp;
1490 label[number].select = FLG_BINARY;
1491 number++;
1492 }
1493
1494 static int
1495 FindKeyWord(int First_Key, int lastkey)
1496 {
1497 int word_type=0;
1498 int i;
1499 int tmp_num=0;
1500
1501 for(;;)
1502 {
1503 if((word_type=GetToken())>0)
1504 {
1505 if(word_type==3)
1506 {
1507 for(i=First_Key; i<=lastkey; i++)
1508 {
1509 if(!strcmp((char *) token, Key[i].name))
1510 {
1511 tmp_num = GetNum();
1512 if(tmp_num<0)
1513 {
1514 fprintf_str(stderr,
1515 "\n ERROR: Number not found for '%s' in <%s>",
1516 Key[i].name, psfontfile);
1517 exit(1);
1518 }
1519 keyword[current].oldnum = tmp_num;
1520 keyword[current].length=strlen((char *) token);
1521 keyword[current].begin=temp - keyword[current].length;
1522 return i;
1523 }
1524 }
1525 }
1526 }
1527 else
1528 {
1529 fprintf_str(stderr,
1530 "\n ERROR: In <%s> keyword not found:", psfontfile);
1531
1532 for(i=First_Key; i<=lastkey; i++)
1533 fprintf_str(stderr,"\n %dth > '%s' ",i,Key[i].name);
1534 exit(1);
1535 }
1536 }
1537 }
1538
1539 /* To increase scan speed we use dynamic range of keywords */
1540
1541 static int
1542 ScanBinary(void)
1543 {
1544 int i;
1545 int firstnum, lastnum;
1546 firstnum= LENIV;
1547 lastnum=SUBRS;
1548
1549 number=0;
1550 label[number].begin = begin_of_scan;
1551 temp = label[number].begin;
1552 label[number].select = FLG_BINARY;
1553 offset= ++number;
1554
1555 for (current=0, FirstKey=current;; current++)
1556 {
1557 i=FindKeyWord(firstnum,lastnum);
1558 switch(i)
1559 {
1560 case LENIV:
1561 FirstKey++;
1562 firstnum=SUBRS;
1563 lastnum=SUBRS;
1564 lenIV=keyword[current].oldnum;
1565 keyword[current].type=Key[0].num;
1566 break;
1567 case SUBRS:
1568 firstnum=SUBRS;
1569 lastnum= CHARSTRINGS;
1570 keyword[current].offset=number;
1571 keyword[current].newnum=0;
1572 keyword[current].type=Key[1].num;
1573 ScanSubrs(current);
1574 LastLook();
1575 break;
1576 case CHARSTRINGS:
1577 char_str=current;
1578 keyword[current].offset=number;
1579 keyword[current].newnum=0;
1580 keyword[current].type=Key[2].num;
1581 ScanChars(current);
1582 LastLook();
1583 #ifdef DEBUG
1584 if(dd(D_CALL_SUBR))
1585 {
1586 for(i=0;i<=2;i++)
1587 {
1588 if(keyword[i].oldnum!=0)
1589 fprintf_str(stderr, " Result for <%s>: %s %d (instead %d) \n",
1590 psfontfile, Key[keyword[i].type].name,keyword[i].newnum, keyword[i].oldnum);
1591 }
1592
1593 }
1594 #endif
1595 return 1;
1596 }
1597 }
1598 }
1599
1600 static unsigned char *
1601 itoasp(int n, unsigned char *s, int len)
1602 {
1603 static int i, j;
1604
1605 j++;
1606 if(n/10)
1607 itoasp(n/10,s,len);
1608 else
1609 i=0;
1610 s[i++]=abs(n)%10+'0';
1611 j--;
1612 if(j==0)
1613 {
1614 for(; i<len;)
1615 s[i++]=' ';
1616 s[i]='\0';
1617 return s;
1618 }
1619 return NULL;
1620 }
1621
1622 static void
1623 SubstNum(void)
1624 {
1625 int i, j;
1626
1627 for(i=FirstKey;i<=char_str;i++)
1628 {
1629 itoasp(keyword[i].newnum,token,keyword[i].length);
1630 temp=keyword[i].begin;
1631 for(j=0;token[j];++j,++temp)
1632 {
1633 *temp=token[j];
1634 }
1635 temp=keyword[i].begin;
1636 }
1637 }
1638
1639 static ub4
1640 little4(ub1 *buff)
1641 {
1642 return (ub4) buff[0] +
1643 ((ub4) buff[1] << 8) +
1644 ((ub4) buff[2] << 16) +
1645 ((ub4) buff[3] << 24);
1646 }
1647
1648 unsigned short int c1 = C1, c2 = C2;
1649 unsigned short int edr;
1650
1651
1652 static unsigned char
1653 CDeCrypt(unsigned char cipher, unsigned int *lcdr)
1654 {
1655 register unsigned char plain;
1656
1657 plain = (cipher ^ (*lcdr >> 8));
1658 *lcdr = (cipher + *lcdr) * c1 + c2;
1659 return plain;
1660 }
1661
1662 unsigned short int eer;
1663
1664 /* We find end of own vector with non StandardEncoding,
1665
1666
1667 */
1668
1669
1670 static int
1671 EndOfEncoding(int err_num)
1672 {
1673
1674 int j;
1675 int i = 0;
1676 int flg_get_word=0;
1677
1678
1679 static const char *RefKey[] =
1680 {
1681 "readonly",
1682 "getinterval",
1683 "exch",
1684 "def",
1685 ""
1686 };
1687
1688 for(;;)
1689 {
1690 if(flg_get_word==0)
1691 flg_get_word = 1;
1692 else
1693 {
1694 err_num=GetWord(token);
1695
1696 }
1697
1698 if(err_num <= 0)
1699 return -1;
1700
1701 if(err_num==5)
1702 refer[ind_ref].num[i++]=atoi((char *) token);
1703 else
1704 {
1705 for(j=0; *RefKey[j]; j++)
1706 {
1707 if(strcmp((char *) token, RefKey[j]) ==0)
1708 break;
1709 }
1710 switch(j)
1711 {
1712 case 0:
1713 case 3:
1714 find_encod=1;
1715 keep_num = -2;
1716 if(ind_ref!=0)
1717 {
1718 CorrectGrid();
1719 }
1720 return 1;
1721
1722 case 1:
1723 break;
1724
1725 case 2:
1726 if(i==1)
1727 {
1728 refer[ind_ref].num[1] = 1;
1729 refer[ind_ref].num[2] = refer[ind_ref].num[0];
1730 GetWord(token);
1731 refer[ind_ref].num[0]= atoi((char *) token);
1732 }
1733 i=0;
1734 refer[ind_ref].select=1;
1735 ind_ref++;
1736 break;
1737 default:
1738 break;
1739 }
1740 }
1741 }
1742
1743 }
1744
1745 /* We rebuild grid for case
1746 "dup dup 161 10 getinterval 0 exch putinterval
1747 dup dup 173 23 getinterval 10 exch putinterval
1748 dup dup 127 exch 196 get put readonly def"
1749 in non StandardEncoding */
1750
1751
1752 static void
1753 CorrectGrid(void)
1754 {
1755 int i, j, k, imax;
1756
1757
1758 for(j=0; j<=ind_ref; j++){
1759 if(refer[j].select==1){
1760 imax= refer[j].num[1] + refer[j].num[2];
1761
1762 for(k=0, i=refer[j].num[2]; i< imax; k++,i++){
1763 if(grid[i]==1){
1764 grid[i]=0;
1765 grid[k+refer[j].num[0]]=1;
1766 }
1767 }
1768 }
1769 }
1770 }
1771 /* We build vector for non StandardEncoding */
1772
1773 static int
1774 CharEncoding(void)
1775 {
1776 int err_token=0;
1777 int num=0;
1778 int seen = 0;
1779
1780 while (1) {
1781 err_token=GetWord(token);
1782
1783 if(err_token==2)
1784 {
1785 if(strcmp((char *) token, Dup) ==0)
1786 {
1787 err_token=GetWord(token);
1788 if(err_token<0)
1789 return ERR_NUM_CHAR;
1790
1791 if(err_token!=2) /* define "dup word" */
1792 {
1793 num=atoi((char *) token);
1794
1795 err_token=GetWord(token);
1796 if(err_token<0)
1797 {
1798 return ERR_NAME_CHAR;
1799 }
1800 FirstChar=AddChar(FirstChar,token, num);
1801 keep_num=num;
1802 keep_flg=1;
1803 seen++;
1804 err_token = GetWord(token);
1805 }
1806 } else {
1807
1808 if(keep_flg==1)
1809 {
1810 keep_num=FLG_OUT_STR;
1811
1812 if(EndOfEncoding(err_token)<0)
1813 {
1814 return -1;
1815 }
1816 }
1817 }
1818 } else
1819 return seen;
1820 }
1821 }
1822
1823
1824
1825 static void
1826 FindEncoding(void)
1827 {
1828 int num_err=0;
1829 int tmpnum;
1830
1831 line=tmpline;
1832
1833 if(encode==0)
1834 {
1835
1836 while((num_err=GetWord(token))>=0)
1837 {
1838 if(num_err==3)
1839 {
1840 if (strcmp((char *) token,"/Encoding") == 0)
1841 {
1842
1843 tmpnum=GetWord(token);
1844
1845 if(tmpnum==5)
1846 {
1847 encode=SPECIAL_ENC;
1848 }
1849
1850 else
1851 {
1852 find_encod=1;
1853 encode=STANDARD_ENC;
1854 }
1855 return;
1856 }
1857 }
1858 }
1859 }
1860
1861 else
1862 {
1863 num_err= CharEncoding();
1864 if(num_err<0)
1865 {
1866 ErrorOfScan(num_err);
1867 fprintf_str(stderr,
1868 "\n ERROR in encoding vector in <%s>", psfontfile);
1869 exit(1);
1870 }
1871 }
1872 }
1873
1874 /* Before parse in BINARY portion of font we should mark needed chars,
1875 reencode them if there is reencoding vector for this case and
1876 build work vector */
1877
1878 static void
1879 CheckChoosing(void)
1880 {
1881
1882 CHAR *TmpChar;
1883 int err_num, i;
1884
1885 if(encode==STANDARD_ENC)
1886 {
1887 TmpChar = FirstCharB;
1888 }
1889 else
1890 {
1891 if(encode==SPECIAL_ENC)
1892 {
1893 TmpChar = FirstChar;
1894 }
1895 else
1896 {
1897 fprintf_str(stderr,
1898 "WARNING: '/Encoding' not found in <%s>\n", psfontfile);
1899 exit(1);
1900 }
1901 }
1902
1903 if(reencode==FLG_REENCODE)
1904 err_num=LoadVector(reencode, TmpChar);
1905 else
1906 err_num=ChooseVect(TmpChar);
1907
1908 if(err_num<0)
1909 {
1910 Afm();
1911 encode=AFM_ENC;
1912
1913 TmpChar = FirstCharA;
1914
1915 for(i=0;i<=255;i++)
1916 grid[i]=tmpgrid[i];
1917
1918
1919 if(reencode==FLG_REENCODE)
1920 err_num=LoadVector(reencode, TmpChar);
1921 else
1922 err_num=ChooseVect(TmpChar);
1923
1924 if(err_num<0)
1925 {
1926 fprintf(stderr,
1927 "\n Warning: after loading AFM file \n");
1928
1929 fprintf_str(stderr,
1930 " only %d chars found instead %d for <%s>\n",
1931 CharCount, GridCount, psfontfile);
1932 }
1933
1934 }
1935 WorkVect(TmpChar);
1936
1937 #ifdef DEBUG
1938
1939 if(dd(D_VIEW_VECTOR))
1940 {
1941 fprintf(stderr, "\n");
1942 if(encode==1)
1943 fprintf(stderr, " Encoding: standard \n");
1944 else
1945 fprintf(stderr, " Encoding: not standard \n");
1946
1947 if(reencode==FLG_REENCODE)
1948 fprintf_str(stderr, " with reencode vector <%s>\n", psvectfile);
1949
1950 PrintChar(FirstCharW);
1951 }
1952 #endif
1953
1954 }
1955
1956 /*
1957 * As we write the output file, we search for /UniqueID, and if we find
1958 * such, we munge it. We need to do this because the font is only partial,
1959 * and if we send out a valid UniqueID, this well may mess up future jobs
1960 * that use this font, but characters that we do not include. We munge
1961 * the string from /UniqueID to /UniqueXX just to make it more clear
1962 * what we've done.
1963 */
1964
1965 static char *
1966 KillUnique(char *s)
1967 {
1968 char *r = strstr(s, "/UniqueID");
1969 if (r) {
1970 r[7] = 'X';
1971 r[8] = 'X';
1972 }
1973 return s;
1974 }
1975
1976 static void
1977 OutASCII(FILE *fout, ub1 *buff, ub4 len)
1978 {
1979 ub4 i;
1980
1981 for (i = 0; i < len; i++)
1982 {
1983 if ((*buff == 10)||(*buff == 13))
1984 {
1985 buff++;
1986 *line++='\n';
1987 *line='\0';
1988
1989 if((find_encod==0)&&(lastpart==0))
1990 {
1991 FindEncoding();
1992 }
1993
1994 line=(unsigned char *) KillUnique((char *) tmpline);
1995
1996 if(keep_flg==0)
1997 fprintf(fout,"%s", line);
1998 else
1999 {
2000 if(keep_num<0)
2001 {
2002 AddStr(line,keep_num);
2003 if(keep_num==-2)
2004 keep_num = -3;
2005 }
2006
2007 }
2008 }
2009 else
2010 {
2011 *line++ = *buff++;
2012 }
2013 }
2014 }
2015
2016 /* It's eexec decription for PFB format */
2017
2018 static void
2019 BinEDeCrypt(ub1 *buff, ub4 len)
2020 {
2021 ub4 i;
2022
2023 for (i = 0; i < len; i++, temp++, buff++)
2024 {
2025 *temp = (*buff ^ (edr >> 8));
2026 edr = (*buff + edr) * c1 + c2;
2027 }
2028 }
2029
2030 /* And it's eexec decription for PFA format */
2031
2032
2033 static void
2034 HexEDeCrypt(unsigned char *mem)
2035 {
2036 int ch1, ch2, cipher;
2037
2038
2039 for(;*mem!='\n' && *mem!='\r'; temp++)
2040 {
2041 ch1= *mem++;
2042 ch2= *mem++;
2043
2044 if ('A' <= ch1 && ch1 <= 'F')
2045 ch1 -= 'A' - 10;
2046 else if ('a' <= ch1 && ch1 <= 'f')
2047 ch1 -= 'a' - 10;
2048 else
2049 ch1 -= '0';
2050 ch1<<=4;
2051
2052 if ('A' <= ch2 && ch2 <= 'F')
2053 ch2 -= 'A' - 10;
2054 else if ('a' <= ch2 && ch2 <= 'f')
2055 ch2 -= 'a' - 10;
2056 else
2057 ch2 -= '0';
2058
2059 cipher = ch1 + ch2;
2060
2061 *temp = (cipher ^ (edr >> 8));
2062 edr = (cipher + edr) * c1 + c2;
2063
2064 }
2065 }
2066
2067 static int
2068 PartialPFA(FILE *fin, FILE *fout)
2069 {
2070 ub1 type;
2071 ub4 memory, addmemory, length, add_of_len;
2072 unsigned char *mem = tmpline;
2073 int check_vect=0;
2074
2075 tmpline=buf;
2076 edr = EDR;
2077 type = FLG_ASCII;
2078 memory = BASE_MEM;
2079 addmemory= ADD_MEM;
2080 length=0;
2081 temp=(typetemp *) UniGetMem(memory);
2082 begin_of_scan=temp;
2083
2084 for(;;)
2085 {
2086 if(fgets((char *)buf,BUFSIZ,fin)==NULL)
2087 break;
2088 switch (type)
2089 {
2090 case FLG_ASCII:
2091 if(strstr((char *)buf,"currentfile eexec") != NULL)
2092 {
2093 type=FLG_BINARY;
2094 }
2095
2096 if((find_encod==0)&&(lastpart==0))
2097 {
2098 FindEncoding();
2099 }
2100
2101 if(keep_flg==0)
2102 fprintf(fout,"%s", KillUnique((char *)buf));
2103 else
2104 {
2105 AddStr(buf,keep_num);
2106 }
2107 break;
2108
2109 case FLG_BINARY:
2110 if(check_vect==0)
2111 {
2112 tmpline=mem;
2113 CheckChoosing();
2114 check_vect=1;
2115 }
2116
2117 if(GetZeroLine(buf)<0)
2118 {
2119 type = FLG_ZERO_LINE;
2120 end_of_scan=temp;
2121 ScanBinary();
2122 SubstNum();
2123 if(keep_flg==1)
2124 {
2125 keep_flg=0;
2126 lastpart=1;
2127 if(encode!=1)
2128 {
2129 UnDefineCharsW();
2130 if(encode==4)
2131 RevChar(FirstCharA);
2132 else
2133 RevChar(FirstChar);
2134
2135 OutChar(FirstCharW, fout);
2136 }
2137 Reeverse(FirstStr);
2138 OutStr(RevStr, fout);
2139 }
2140
2141 OutHEX(fout);
2142 UniFree(begin_of_scan);
2143 fprintf(fout, "%s", KillUnique((char*) buf));
2144 break;
2145 }
2146
2147 add_of_len=strlen((char *) buf)/2;
2148 length=length + add_of_len;
2149
2150 if(length>memory)
2151 {
2152 memory = memory + addmemory;
2153 /* Using "memory = length;" retains minimum */
2154 /* of memory but it will be more slowly */
2155 begin_of_scan = (typetemp*) UniRealloc(begin_of_scan, memory);
2156 temp = begin_of_scan + length - add_of_len;
2157 }
2158 HexEDeCrypt(buf);
2159 break;
2160 case FLG_ZERO_LINE:
2161 fprintf(fout, "%s", buf);
2162 break;
2163 }
2164 }
2165 if(type == FLG_ZERO_LINE)
2166 return TRUE;
2167 else return FALSE;
2168 }
2169
2170 #define FIRST_ASCII 1
2171 #define FIRST_BINARY 2
2172 #define NEXT_BINARY 3
2173 #define NEXT_ASCII 4
2174
2175 static int
2176 PartialPFB(FILE *fin, FILE *fout)
2177 {
2178 ub1 type;
2179 ub4 t_length, length, nread;
2180 int nbytes, rc;
2181 int check_vect = 0;
2182 int sub_type = FIRST_ASCII;
2183
2184 line=tmpline;
2185 edr = EDR;
2186
2187 /* To find summary length of multiple binary parts we prescan font file */
2188
2189 t_length = 0L;
2190
2191 for (;;)
2192 {
2193 if ((rc = fread((char *) buf, 1, 6, fin)) < 2)
2194 {
2195 return FALSE;
2196 }
2197
2198 if (buf[0] != 128)
2199 return FALSE;
2200
2201 type = buf[1];
2202
2203 if (type == FLG_EOF)
2204 {
2205 break;
2206 }
2207
2208 if (rc != 6)
2209 return FALSE;
2210
2211 length = little4(&buf[2]);
2212
2213 if(type==FLG_BINARY)
2214 {
2215 t_length = (ub4)(length + t_length);
2216 }
2217 fseek(fin, ftell(fin) + length, SEEK_SET);
2218 }
2219
2220 /* Here we start real parsing */
2221
2222 sub_type = FIRST_BINARY;
2223
2224 fseek(fin, 0L, SEEK_SET);
2225
2226 for (;;)
2227 {
2228 if ((rc = fread((char *) buf, 1, 6, fin)) < 2)
2229 {
2230 return FALSE;
2231 }
2232 if (buf[0] != 128)
2233 return FALSE;
2234
2235 type = buf[1];
2236
2237 if (type == FLG_EOF)
2238 {
2239 return TRUE;
2240 }
2241
2242 if (rc != 6)
2243 return FALSE;
2244
2245 length = little4(&buf[2]);
2246
2247 if(type==FLG_BINARY)
2248 {
2249 if(sub_type == FIRST_BINARY)
2250 {
2251 sub_type = NEXT_BINARY;
2252 temp=(typetemp*) UniGetMem(t_length);
2253 begin_of_scan=temp;
2254 }
2255 }
2256 else
2257 {
2258 if( sub_type == NEXT_BINARY)
2259 {
2260 sub_type = NEXT_ASCII;
2261 end_of_scan=temp;
2262 ScanBinary();
2263 SubstNum();
2264 if(keep_flg==1)
2265 {
2266 keep_flg=0;
2267 lastpart=1;
2268 if(encode!=1)
2269 {
2270 UnDefineCharsW();
2271 if(encode==4)
2272 RevChar(FirstCharA);
2273 else
2274 RevChar(FirstChar);
2275
2276 OutChar(FirstCharW, fout);
2277 }
2278
2279 Reeverse(FirstStr);
2280 OutStr(RevStr, fout);
2281 }
2282 OutHEX(fout);
2283 UniFree(begin_of_scan);
2284 }
2285 }
2286
2287 for (nread = 0; nread < length; nread += rc)
2288 {
2289 nbytes = min(BUFSIZ, length - nread);
2290
2291 if ((rc = fread((char *) buf, 1, nbytes, fin)) == 0)
2292 {
2293 return FALSE;
2294 }
2295 switch (type)
2296 {
2297 case FLG_ASCII:
2298 OutASCII(fout, buf, (ub4) rc);
2299 break;
2300 case FLG_BINARY:
2301 if(check_vect==0)
2302 {
2303 CheckChoosing();
2304 check_vect=1;
2305 }
2306 BinEDeCrypt(buf, (ub4) rc);
2307 break;
2308 default:
2309 return FALSE;
2310 }
2311 }
2312 }
2313 }
2314
2315 static void
2316 OutHEX(FILE *fout)
2317 {
2318 int i=0;
2319 int num;
2320 static const char *hexstr = "0123456789abcdef";
2321 int bin;
2322
2323 line=tmpline;
2324 eer = EER;
2325 label[number].begin = end_of_scan;
2326 label[number].select = NOTHING;
2327 number++;
2328
2329 for(num=0; num < number; num++)
2330 {
2331 switch(label[num].select)
2332 {
2333 case NOTHING:
2334 break;
2335 case FLG_BINARY:
2336 label[num].select=NOTHING;
2337 for(temp=label[num].begin; temp<label[num+1].begin; temp++,i++)
2338 {
2339 bin = (*temp ^ (eer >> 8)); /* Eexec encryption */
2340 eer = ((bin + eer) * c1 + c2);
2341
2342 *line++= hexstr[(bin&0xf0)>>4];
2343 *line++= hexstr[bin&0xf];
2344
2345 if (!((i + 1) % BYTES_PER_LINE))
2346 {
2347 *line++='\0';
2348 line =tmpline;
2349 fprintf(fout, "%s\n",line);
2350 }
2351 }
2352 break;
2353 }
2354 }
2355 if (i % BYTES_PER_LINE)
2356 {
2357 *line++='\0';
2358 line =tmpline;
2359 fprintf(fout, "%s\n",line);
2360 }
2361 }
2362
2363 /* We parse AFM file only if we've received errors after
2364 parsing of own vector */
2365
2366 static int
2367 Afm(void)
2368 {
2369 unsigned char afmfile[100];
2370 FILE *fafm;
2371 int err_num=0;
2372 int i,j,k,num=0;
2373 unsigned char name[40];
2374
2375 static const char *AfmKey[] =
2376 {
2377 "StartCharMetrics",
2378 "EndCharMetrics",
2379 ""
2380 };
2381
2382 static const char *InfoKey[] =
2383 {
2384 "C",
2385 "N",
2386 ""
2387 };
2388
2389 for(i=0; psfontfile[i]; i++)
2390 {
2391 if(psfontfile[i] == '.')
2392 break;
2393 else
2394 afmfile[i]=psfontfile[i];
2395 }
2396
2397 afmfile[i]='\0';
2398 strcat((char *) afmfile,".afm");
2399 fprintf_str(stderr, "<%s>", afmfile);
2400
2401 if ((fafm = psfopen((char *) afmfile, "r")) == NULL)
2402 {
2403 NameOfProgram();
2404 perror((char *) afmfile);
2405 return -1;
2406 }
2407
2408 for(j=0;;)
2409 {
2410 line = tmpline;
2411
2412 if(fgets((char *) line,BUFSIZ,fafm)==NULL)
2413 break;
2414
2415 if(strstr((char *) line, AfmKey[j])!=NULL)
2416 {
2417 if(j==0)
2418 {
2419 j++;
2420 continue;
2421 }
2422 else
2423 {
2424 fclose(fafm);
2425 return 1;
2426 }
2427 }
2428
2429 if(j==1)
2430 {
2431 for(k=0; err_num>=0; )
2432 {
2433 err_num=GetWord(token);
2434 if(err_num==2)
2435 {
2436 if(strcmp((char *) token,InfoKey[k])==0)
2437 {
2438 if(k==0)
2439 {
2440 err_num=GetWord(token);
2441 num=atoi((char *) token);
2442 k=1;
2443 continue;
2444 }
2445 else
2446 {
2447 err_num=GetWord(token);
2448 name[0]='/';
2449 name[1]='\0';
2450 strcat((char *) name, (char *) token);
2451 if(num>=0)
2452 FirstCharA=AddChar(FirstCharA, name, num);
2453 break;
2454 }
2455 }
2456 }
2457
2458 }
2459 }
2460 }
2461 return -2;
2462 }
2463
2464 int
2465 FontPart(FILE *fout, unsigned char *fontfile,
2466 unsigned char *vectfile)
2467 {
2468 FILE *fin=0;
2469 int num;
2470 int rc;
2471 int i;
2472
2473 ind_ref=0;
2474 reencode=0;
2475 encode=0;
2476 lastpart=0;
2477 keep_flg=0;
2478 flg_seac=0;
2479 strcpy((char *) psfontfile, (char *) fontfile);
2480 find_encod=0;
2481 CharCount=0;
2482
2483 if(loadbase != 1)
2484 {
2485 for(i=offset; i < NUM_LABEL; i++)
2486 label[i].num=CHAR_NOT_DEF;
2487
2488
2489 strcpy((char *) psvectfile, (char *) basevect);
2490
2491 #ifdef DEBUG
2492 if(dd(D_VIEW_VECTOR))
2493 fprintf_str(stderr, " Base vector <%s>.", basevect);
2494 #endif
2495
2496 if(LoadVector(1, FirstCharB)==1)
2497 {
2498 loadbase = FLG_LOAD_BASE;
2499
2500 }
2501 else
2502 exit(1);
2503 }
2504
2505 if(vectfile)
2506 {
2507 reencode=FLG_REENCODE;
2508 strcpy((char *) psvectfile, (char *) vectfile);
2509 }
2510
2511 for(num=0;num<NUM_LABEL;num++)
2512 label[num].select = NOTHING;
2513
2514 switch(DefTypeFont(fontfile))
2515 {
2516 case PFA:
2517 if ((fin = psfopen((char *) fontfile, "r"))==NULL)
2518 {
2519 NameOfProgram();
2520 perror((char *) fontfile);
2521 return -1;
2522 }
2523 rc = PartialPFA(fin,fout);
2524 if (rc == FALSE)
2525 {
2526 NameOfProgram();
2527 (void) fprintf_str(stderr,
2528 "Error: %s is not a valid PFA file\n", fontfile);
2529 return -1;
2530 }
2531
2532 break;
2533 case PFB:
2534 if ((fin = psfopen((char *) fontfile, OPEN_READ_BINARY))==NULL)
2535 {
2536 NameOfProgram();
2537 perror((char *) fontfile);
2538 return -1;
2539 }
2540 rc = PartialPFB(fin,fout);
2541 if (rc==FALSE)
2542 {
2543 NameOfProgram();
2544 (void) fprintf_str(stderr,
2545 "Error: %s is not a valid PFB file\n", fontfile);
2546 return -1;
2547 }
2548 break;
2549 case -1:
2550 NameOfProgram();
2551 fprintf_str(stderr,
2552 "Error: %s has neither PFA nor PFB extension", fontfile);
2553 return -1;
2554 }
2555
2556 UnDefineCharsW();
2557
2558 if(encode==AFM_ENC)
2559 FirstCharA=UnDefineChars(FirstCharA);
2560
2561 if(encode!=1)
2562 {
2563 UnDefineStr();
2564 }
2565
2566 FirstChar=UnDefineChars(FirstChar);
2567
2568 fclose(fin);
2569
2570 for(i=0; i < number; i++)
2571 label[i].num=CHAR_NOT_DEF;
2572
2573 ClearB();
2574 return 1;
2575 }
2576
2577
2578
2579 static int
2580 LoadVector(int num, CHAR *TmpChar)
2581 {
2582
2583 FILE *fvect;
2584 int i = 0;
2585 int j = 0;
2586 int end_vect=0;
2587 int index_grid = 0;
2588
2589 CharCount = 0;
2590
2591 if ((fvect = psfopen((char *) psvectfile, "r")) == NULL)
2592 {
2593 NameOfProgram();
2594 perror((char *) psvectfile);
2595 return -1;
2596 }
2597
2598 for(;;)
2599 {
2600 line = tmpline;
2601
2602 if((fgets((char*)line,BUFSIZ,fvect)==NULL)||(end_vect!=0))
2603 break;
2604
2605 for(;;)
2606 {
2607 j=GetWord(token);
2608 if(j==3)
2609 {
2610 if(i==0)
2611 {
2612 i++;
2613 continue;
2614 }
2615
2616 if(num==4)
2617 {
2618 if(grid[index_grid]==1)
2619 {
2620 if(ChooseChar(token, TmpChar)> 0)
2621 CharCount++;
2622 else
2623 {
2624 fprintf_str(stderr,
2625 "Error: '%s' not found in reencoding vector <%s> for <%s>\n",
2626 token,psvectfile, psfontfile);
2627 }
2628 }
2629 index_grid++;
2630 }
2631 else
2632 {
2633 if(num==1) /* Build base vector */
2634 {
2635 FirstCharB=AddChar(FirstCharB,token, CharCount);
2636 CharCount++;
2637 }
2638 }
2639 continue;
2640 }
2641 if(j== -1)
2642 break;
2643 if(j==2)
2644 {
2645 i=0;
2646 end_vect = 1;
2647 break;
2648 }
2649 }
2650 }
2651
2652 if(j==2)
2653 {
2654 if((index_grid!=256)&&(CharCount!=256))
2655 {
2656 fclose(fvect);
2657 fprintf_str(stderr,"Error during Load Vector in <%s> \n",
2658 psvectfile);
2659 fprintf(stderr,
2660 "Found %d chars instead 256\n", max(index_grid,CharCount));
2661 return -3;
2662 }
2663
2664 if(CharCount>0)
2665 {
2666 fclose(fvect);
2667 return 1;
2668 }
2669 else
2670 {
2671 fclose(fvect);
2672 fprintf_str(stderr,
2673 "\n Warning: Vector from <%s> for <%s> doesn't load\n",
2674 psvectfile, psfontfile);
2675 return -1;
2676 }
2677 }
2678 else
2679 {
2680 fprintf_str(stderr,"\n Error: ending token 'def' not found in <%s> \n",
2681 psvectfile);
2682 return -2;
2683 }
2684 }
2685
2686 static int
2687 ChooseVect(CHAR *tmpChar)
2688 {
2689 CHAR *ThisChar = tmpChar;
2690
2691 CharCount=0;
2692 while (ThisChar != NULL)
2693 {
2694 ThisChar->choose= grid[ThisChar->num];
2695 if(grid[ThisChar->num]==1)
2696 {
2697 CharCount++;
2698 }
2699 ThisChar = ThisChar->NextChar;
2700 }
2701
2702 if(CharCount<GridCount)
2703 return -1;
2704 else
2705 return 1;
2706
2707 }
2708
2709 static void
2710 ErrorOfScan(int err)
2711 {
2712 switch(err)
2713 {
2714
2715 case 0: break;
2716
2717 case ERR_FIRST_NUM:
2718 fprintf(stderr, " First number not found ");
2719 break;
2720 case ERR_SECOND_NUM:
2721 fprintf(stderr, " Second number not found ");
2722 break;
2723
2724 case ERR_FIRST_TOK:
2725 fprintf(stderr, " First token not found ");
2726 break;
2727
2728 case ERR_SECOND_TOK:
2729 fprintf(stderr, " Second token not found ");
2730 break;
2731
2732 case ERR_STACK:
2733 fprintf(stderr, " End of stack ");
2734 break;
2735
2736 case ERR_NUM_CHAR:
2737 fprintf(stderr, " Number of char not found ");
2738 break;
2739
2740 case ERR_NAME_CHAR:
2741 fprintf(stderr, " Name of char not found ");
2742 break;
2743 }
2744 }
2745
2746 static void
2747 NameOfProgram(void)
2748 {
2749 #ifdef DVIPS
2750 fprintf(stderr,"This is DVIPS, t1part module \n");
2751 #else
2752 fprintf(stderr,"This is t1part, %s by Sergey Lesenko\n", version);
2753 #endif
2754 }
2755
2756