Debian Codegraph @

texk/ps2pk/t1io.c

raw
1 /* $XConsortium: t1io.c,v 1.4 91/10/10 11:19:41 rws Exp $ */
2 /* Copyright International Business Machines,Corp. 1991
3 * All Rights Reserved
4 *
5 * License to use, copy, modify, and distribute this software
6 * and its documentation for any purpose and without fee is
7 * hereby granted, provided that the above copyright notice
8 * appear in all copies and that both that copyright notice and
9 * this permission notice appear in supporting documentation,
10 * and that the name of IBM not be used in advertising or
11 * publicity pertaining to distribution of the software without
12 * specific, written prior permission.
13 *
14 * IBM PROVIDES THIS SOFTWARE "AS IS", WITHOUT ANY WARRANTIES
15 * OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT
16 * LIMITED TO ANY IMPLIED WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, AND NONINFRINGEMENT OF
18 * THIRD PARTY RIGHTS. THE ENTIRE RISK AS TO THE QUALITY AND
19 * PERFORMANCE OF THE SOFTWARE, INCLUDING ANY DUTY TO SUPPORT
20 * OR MAINTAIN, BELONGS TO THE LICENSEE. SHOULD ANY PORTION OF
21 * THE SOFTWARE PROVE DEFECTIVE, THE LICENSEE (NOT IBM) ASSUMES
22 * THE ENTIRE COST OF ALL SERVICING, REPAIR AND CORRECTION. IN
23 * NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL, INDIRECT OR
24 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
25 * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
26 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
27 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
28 * SOFTWARE.
29 *
30 * Author: Carol H. Thompson IBM Almaden Research Center
31 */
32 /*******************************************************************
33 * I/O package for Type 1 font reading
34 ********************************************************************/
35
36 #ifndef STATIC
37 #define STATIC static
38 #endif
39
40 #ifdef HAVE_CONFIG_H
41 #include "c-auto.h"
42 #endif
43
44 #ifdef HAVE_FCNTL_H
45 #include <fcntl.h>
46 #endif
47
48 #include "t1stdio.h"
49 #include "t1hdigit.h"
50
51 /* Constants and variables used in the decryption */
52 #define c1 ((unsigned short)52845)
53 #define c2 ((unsigned short)22719)
54 static unsigned short r;
55 static int asc, Decrypt;
56 static int extrach;
57 static int haveextrach;
58
59 /* Our single FILE structure and buffer for this package */
60 STATIC F_FILE TheFile;
61 STATIC unsigned char TheBuffer[F_BUFSIZ];
62
63 /* Our routines */
64 STATIC int T1Decrypt(unsigned char *, int), T1Fill(F_FILE *);
65
66 /* -------------------------------------------------------------- */
67 /*ARGSUSED*/
68 F_FILE *T1Open(
69 char *fn, /* Pointer to filename */
70 char *mode) /* Pointer to open mode string */
71 {
72 F_FILE *of = &TheFile;
73
74 Decrypt = 0;
75
76 /* We know we are only reading */
77 #if defined(MSDOS) || defined(VMS) || defined(WIN32)
78 if ((of->fd=open(fn, O_RDONLY | O_BINARY)) < 0) return NULL;
79 #else
80 if ((of->fd=open(fn, O_RDONLY)) < 0) return NULL;
81 #endif
82
83 /* Initialize the buffer information of our file descriptor */
84 of->b_base = TheBuffer;
85 of->b_size = F_BUFSIZ;
86 of->b_ptr = NULL;
87 of->b_cnt = 0;
88 of->flags = 0;
89 of->error = 0;
90 haveextrach = 0;
91 return &TheFile;
92 } /* end Open */
93
94 /* -------------------------------------------------------------- */
95 int T1Getc( /* Read one character */
96 F_FILE *f) /* Stream descriptor */
97 {
98 if (f->b_base == NULL) return EOF; /* already closed */
99
100 if (f->flags & UNGOTTENC) { /* there is an ungotten c */
101 f->flags &= ~UNGOTTENC;
102 return (int) f->ungotc;
103 }
104
105 if (f->b_cnt == 0) /* Buffer needs to be (re)filled */
106 f->b_cnt = T1Fill(f);
107 if (f->b_cnt > 0) return (f->b_cnt--, (int) *(f->b_ptr++));
108 else {
109 f->flags |= FIOEOF;
110 return EOF;
111 }
112 } /* end Getc */
113
114 /* -------------------------------------------------------------- */
115 int T1Ungetc( /* Put back one character */
116 int c,
117 F_FILE *f) /* Stream descriptor */
118 {
119 if (c != EOF) {
120 f->ungotc = c;
121 f->flags |= UNGOTTENC; /* set flag */
122 f->flags &= ~FIOEOF; /* reset EOF */
123 }
124 return c;
125 } /* end Ungetc */
126
127 /* -------------------------------------------------------------- */
128 int T1Read( /* Read n items into caller's buffer */
129 char *buffP, /* Buffer to be filled */
130 int size, /* Size of each item */
131 int n, /* Number of items to read */
132 F_FILE *f) /* Stream descriptor */
133 {
134 int bytelen, cnt, i;
135 F_char *p = (F_char *)buffP;
136 int icnt; /* Number of characters to read */
137
138 if (f->b_base == NULL) return 0; /* closed */
139 icnt = (size!=1)?n*size:n; /* Number of bytes we want */
140
141 if (f->flags & UNGOTTENC) { /* there is an ungotten c */
142 f->flags &= ~UNGOTTENC;
143 *(p++) = f->ungotc;
144 icnt--; bytelen = 1;
145 }
146 else bytelen = 0;
147
148 while (icnt > 0) {
149 /* First use any bytes we have buffered in the stream buffer */
150 if ((cnt=f->b_cnt) > 0) {
151 if (cnt > icnt) cnt = icnt;
152 for (i=0; i<cnt; i++) *(p++) = *(f->b_ptr++);
153 f->b_cnt -= cnt;
154 icnt -= cnt;
155 bytelen += cnt;
156 }
157
158 if ((icnt == 0) || (f->flags & FIOEOF)) break;
159
160 f->b_cnt = T1Fill(f);
161 }
162 return ((size!=1)?bytelen/size:bytelen);
163 } /* end Read */
164
165 /* -------------------------------------------------------------- */
166 int T1Close( /* Close the file */
167 F_FILE *f) /* Stream descriptor */
168 {
169 if (f->b_base == NULL) return 0; /* already closed */
170 f->b_base = NULL; /* no valid stream */
171 return close(f->fd);
172 } /* end Close */
173
174 /* -------------------------------------------------------------- */
175 F_FILE *T1eexec( /* Initialization */
176 F_FILE *f) /* Stream descriptor */
177 {
178 int i, c;
179 int H;
180 unsigned char *p;
181 unsigned char randomP[8];
182
183 r = 55665; /* initial key */
184 asc = 1; /* indicate ASCII form */
185
186 /* Consume the 4 random bytes, determining if we are also to
187 ASCIIDecodeHex as we process our input. (See pages 63-64
188 of the Adobe Type 1 Font Format book.) */
189
190 /* Skip over any initial white space chars */
191 while (HighHexP[c=getc(f)] == HWHITE_SPACE) ;
192
193 /* If ASCII, the next 7 chars are guaranteed consecutive */
194 randomP[0] = c; /* store first non white space char */
195 fread((char *)randomP+1, 1, 3, f); /* read 3 more, for a total of 4 */
196 /* store first four chars */
197 for (i=0,p=randomP; i<4; i++) { /* Check 4 valid ASCIIEncode chars */
198 if (HighHexP[*p++] > LAST_HDIGIT) { /* non-ASCII byte */
199 asc = 0;
200 break;
201 }
202 }
203 if (asc) { /* ASCII form, convert first eight bytes to binary */
204 fread((char *)randomP+4, 1, 4, f); /* Need four more */
205 for (i=0,p=randomP; i<4; i++) { /* Convert */
206 H = HighHexP[*p++];
207 randomP[i] = H | LowHexP[*p++];
208 }
209 }
210
211 /* Adjust our key */
212 for (i=0,p=randomP; i<4; i++) {
213 r = (*p++ + r) * c1 + c2;
214 }
215
216 /* Decrypt the remaining buffered bytes */
217 f->b_cnt = T1Decrypt(f->b_ptr, f->b_cnt);
218 Decrypt = 1;
219 return (feof(f))?NULL:f;
220 } /* end eexec */
221
222 /* -------------------------------------------------------------- */
223 STATIC int T1Decrypt(unsigned char *p, int len)
224 {
225 int n;
226 int H = 0, L;
227 unsigned char *inp = p;
228 unsigned char *tblP;
229
230 if (asc) {
231 if (haveextrach) {
232 H = extrach;
233 tblP = LowHexP;
234 }
235 else tblP = HighHexP;
236 for (n=0; len>0; len--) {
237 L = tblP[*inp++];
238 if (L == HWHITE_SPACE) continue;
239 if (L > LAST_HDIGIT) break;
240 if (tblP == HighHexP) { /* Got first hexit value */
241 H = L;
242 tblP = LowHexP;
243 } else { /* Got second hexit value; compute value and store it */
244 n++;
245 tblP = HighHexP;
246 H |= L;
247 /* H is an int, 0 <= H <= 255, so all of this will work */
248 *p++ = H ^ (r >> 8);
249 r = (H + r) * c1 + c2;
250 }
251 }
252 if (tblP != HighHexP) { /* We had an odd number of hexits */
253 extrach = H;
254 haveextrach = 1;
255 } else haveextrach = 0;
256 return n;
257 } else {
258 for (n = len; n>0; n--) {
259 H = *inp++;
260 *p++ = H ^ (r >> 8);
261 r = (H + r) * c1 + c2;
262 }
263 return len;
264 }
265 } /* end Decrypt */
266
267 /* -------------------------------------------------------------- */
268 STATIC int T1Fill( /* Refill stream buffer */
269 F_FILE *f) /* Stream descriptor */
270 {
271 int rc;
272
273 rc = read(f->fd, f->b_base, F_BUFSIZ);
274 /* propagate any error or eof to current file */
275 if (rc <= 0) {
276 if (rc == 0) /* means EOF */
277 f->flags |= FIOEOF;
278 else {
279 f->error = (short)-rc;
280 f->flags |= FIOERROR;
281 rc = 0;
282 }
283 }
284 f->b_ptr = f->b_base;
285 if (Decrypt) rc = T1Decrypt(f->b_base, rc);
286 return rc;
287 } /* end Fill */