]> git.sesse.net Git - rdpsrv/blob - Xserver/lib/font/Speedo/spfile.c
acb45b0f2da36384c21a835fc0a26b9d9454a8a6
[rdpsrv] / Xserver / lib / font / Speedo / spfile.c
1 /* $TOG: spfile.c /main/13 1997/06/09 09:38:27 barstow $ */
2 /*
3  * Copyright 1990, 1991 Network Computing Devices;
4  * Portions Copyright 1987 by Digital Equipment Corporation
5  *
6  * Permission to use, copy, modify, distribute, and sell this software and
7  * its documentation for any purpose is hereby granted without fee, provided
8  * that the above copyright notice appear in all copies and that both that
9  * copyright notice and this permission notice appear in supporting
10  * documentation, and that the names of Network Computing Devices or Digital
11  * not be used in advertising or publicity pertaining to distribution of
12  * the software without specific, written prior permission.
13  *
14  * NETWORK COMPUTING DEVICES AND DIGITAL DISCLAIM ALL WARRANTIES WITH
15  * REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
16  * AND FITNESS, IN NO EVENT SHALL NETWORK COMPUTING DEVICES OR DIGITAL 
17  * LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
18  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
19  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21  *
22  * Author: Dave Lemke, Network Computing Devices Inc
23  */
24
25 /*
26
27 Copyright (c) 1987  X Consortium
28
29 Permission is hereby granted, free of charge, to any person obtaining
30 a copy of this software and associated documentation files (the
31 "Software"), to deal in the Software without restriction, including
32 without limitation the rights to use, copy, modify, merge, publish,
33 distribute, sublicense, and/or sell copies of the Software, and to
34 permit persons to whom the Software is furnished to do so, subject to
35 the following conditions:
36
37 The above copyright notice and this permission notice shall be included
38 in all copies or substantial portions of the Software.
39
40 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
41 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
42 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
43 IN NO EVENT SHALL THE X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR
44 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
45 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
46 OTHER DEALINGS IN THE SOFTWARE.
47
48 Except as contained in this notice, the name of the X Consortium shall
49 not be used in advertising or otherwise to promote the sale, use or
50 other dealings in this Software without prior written authorization
51 from the X Consortium.
52
53 */
54
55 #include <stdio.h>
56 #include "fntfilst.h"
57
58 #include "spint.h"
59
60 SpeedoFontPtr sp_fp_cur = (SpeedoFontPtr) 0;
61
62 #ifdef EXTRAFONTS
63 #include "ncdkeys.h"
64 #endif
65
66 #include "keys.h"
67
68 #ifdef EXTRAFONTS
69 static ufix8 skey[] =
70 {
71     SKEY0,
72     SKEY1,
73     SKEY2,
74     SKEY3,
75     SKEY4,
76     SKEY5,
77     SKEY6,
78     SKEY7,
79     SKEY8
80 };                              /* Sample Font decryption key */
81
82 static ufix8 rkey[] =
83 {
84     RKEY0,
85     RKEY1,
86     RKEY2,
87     RKEY3,
88     RKEY4,
89     RKEY5,
90     RKEY6,
91     RKEY7,
92     RKEY8
93 };                              /* Retail Font decryption key */
94
95 #endif                          /* EXTRAFONTS */
96
97 #ifdef XSAMPLEFONTS
98 static ufix8 xkey[] =
99 {
100     XKEY0,
101     XKEY1,
102     XKEY2,
103     XKEY3,
104     XKEY4,
105     XKEY5,
106     XKEY6,
107     XKEY7,
108     XKEY8
109 };                              /* Sample Font decryption key */
110 #endif
111
112 static ufix8 mkey[] =
113 {
114     KEY0,
115     KEY1,
116     KEY2,
117     KEY3,
118     KEY4,
119     KEY5,
120     KEY6,
121     KEY7,
122     KEY8
123 };                              /* Font decryption key */
124
125
126 static      fix15
127 read_2b(ptr)
128     ufix8      *ptr;
129 {
130     fix15       tmp;
131
132     tmp = *ptr++;
133     tmp = (tmp << 8) + *ptr;
134     return tmp;
135 }
136
137 static      fix31
138 read_4b(ptr)
139     ufix8      *ptr;
140 {
141     fix31       tmp;
142
143     tmp = *ptr++;
144     tmp = (tmp << 8) + *ptr++;
145     tmp = (tmp << 8) + *ptr++;
146     tmp = (tmp << 8) + *ptr;
147     return tmp;
148 }
149
150 /*
151  * loads the specified char's data
152  */
153 buff_t     *
154 sp_load_char_data(file_offset, num, cb_offset)
155     fix31       file_offset;
156     fix15       num;
157     fix15       cb_offset;
158 {
159     SpeedoMasterFontPtr master = sp_fp_cur->master;
160
161     if (fseek(master->fp, (long) file_offset, (int) 0)) {
162         SpeedoErr("can't seek to char\n");
163     }
164     if ((num + cb_offset) > master->mincharsize) {
165         SpeedoErr("char buf overflow\n");
166     }
167     if (fread((master->c_buffer + cb_offset), sizeof(ufix8), num,
168               master->fp) != num) {
169         SpeedoErr("can't get char data\n");
170     }
171     master->char_data.org = (ufix8 *) master->c_buffer + cb_offset;
172     master->char_data.no_bytes = num;
173
174     return &master->char_data;
175 }
176
177 int
178 sp_open_master(filename, master)
179     char       *filename;
180     SpeedoMasterFontPtr *master;
181 {
182     SpeedoMasterFontPtr spmf;
183     ufix8       tmp[16];
184     ufix16      cust_no;
185     FILE       *fp;
186     ufix32      minbufsize;
187     ufix16      mincharsize;
188     ufix8      *f_buffer;
189     ufix8      *c_buffer;
190     int         ret;
191     ufix8      *key;
192
193     spmf = (SpeedoMasterFontPtr) xalloc(sizeof(SpeedoMasterFontRec));
194     if (!spmf)
195         return AllocError;
196     bzero(spmf, sizeof(SpeedoMasterFontRec));
197     spmf->entry = NULL;
198     spmf->f_buffer = NULL;
199     spmf->c_buffer = NULL;
200
201     /* open font */
202     spmf->fname = (char *) xalloc(strlen(filename) + 1);
203     if (!spmf->fname)
204         return AllocError;
205     fp = fopen(filename, "r");
206     if (!fp) {
207         ret = BadFontName;
208         goto cleanup;
209     }
210     strcpy(spmf->fname, filename);
211     spmf->fp = fp;
212     spmf->state |= MasterFileOpen;
213
214     if (fread(tmp, sizeof(ufix8), 16, fp) != 16) {
215         ret = BadFontName;
216         goto cleanup;
217     }
218     minbufsize = (ufix32) read_4b(tmp + FH_FBFSZ);
219     f_buffer = (ufix8 *) xalloc(minbufsize);
220     if (!f_buffer) {
221         ret = AllocError;
222         goto cleanup;
223     }
224     spmf->f_buffer = f_buffer;
225
226     fseek(fp, (ufix32) 0, 0);
227
228     /* read in the font */
229     if (fread(f_buffer, sizeof(ufix8), (ufix16) minbufsize, fp) != minbufsize) {
230         ret = BadFontName;
231         goto cleanup;
232     }
233     spmf->copyright = (char *) (f_buffer + FH_CPYRT);
234     spmf->mincharsize = mincharsize = read_2b(f_buffer + FH_CBFSZ);
235
236     c_buffer = (ufix8 *) xalloc(mincharsize);
237     if (!c_buffer) {
238         ret = AllocError;
239         goto cleanup;
240     }
241     spmf->c_buffer = c_buffer;
242
243     spmf->font.org = spmf->f_buffer;
244     spmf->font.no_bytes = minbufsize;
245
246     cust_no = sp_get_cust_no(spmf->font);
247
248     /* XXX add custom encryption stuff here */
249
250 #ifdef EXTRAFONTS
251     if (cust_no == SCUS0) {
252         key = skey;
253     } else if (cust_no == RCUS0) {
254         key = rkey;
255     } else
256 #endif
257
258 #ifdef XSAMPLEFONTS
259     if (cust_no == XCUS0) {
260         key = xkey;
261     } else
262 #endif
263
264     if (cust_no == CUS0) {
265         key = mkey;
266     } else {
267         SpeedoErr("Non - standard encryption for \"%s\"\n", filename);
268         ret = BadFontName;
269         goto cleanup;
270     }
271     spmf->key = key;
272     sp_set_key(key);
273
274     spmf->first_char_id = read_2b(f_buffer + FH_FCHRF);
275     spmf->num_chars = read_2b(f_buffer + FH_NCHRL);
276
277
278     spmf->enc = sp_bics_map;
279     spmf->enc_size = sp_bics_map_size;
280
281 #ifdef EXTRAFONTS
282     {                           /* choose the proper encoding */
283         char       *f;
284
285         f = strrchr(filename, '/');
286         if (f) {
287             f++;
288             if (strncmp(f, "bx113", 5) == 0) {
289                 spmf->enc = adobe_map;
290                 spmf->enc_size = adobe_map_size;
291             }
292         }
293     }
294 #endif
295
296     /* XXX slam back to ISO Latin1 */
297     spmf->first_char_id = spmf->enc[0];
298     /* size of extents array */
299     spmf->max_id = spmf->enc[(spmf->enc_size - 1) * 2];
300     spmf->num_chars = spmf->enc_size;
301
302     *master = spmf;
303
304     return Successful;
305
306 cleanup:
307     *master = (SpeedoMasterFontPtr) 0;
308     sp_close_master_font(spmf);
309     return ret;
310 }
311
312 void
313 sp_close_master_font(spmf)
314     SpeedoMasterFontPtr spmf;
315 {
316     if (!spmf)
317         return;
318     if (spmf->state & MasterFileOpen)
319         fclose(spmf->fp);
320     if (spmf->entry)
321         spmf->entry->u.scalable.extra->private = NULL;
322     xfree(spmf->fname);
323     xfree(spmf->f_buffer);
324     xfree(spmf->c_buffer);
325     xfree(spmf);
326 }
327
328 void
329 sp_close_master_file(spmf)
330     SpeedoMasterFontPtr spmf;
331 {
332     (void) fclose(spmf->fp);
333     spmf->state &= ~MasterFileOpen;
334 }
335
336
337 /*
338  * reset the encryption key, and make sure the file is opened
339  */
340 void
341 sp_reset_master(spmf)
342     SpeedoMasterFontPtr spmf;
343 {
344     sp_set_key(spmf->key);
345     if (!(spmf->state & MasterFileOpen)) {
346         spmf->fp = fopen(spmf->fname, "r");
347         /* XXX -- what to do if we can't open the file? */
348         spmf->state |= MasterFileOpen;
349     }
350     fseek(spmf->fp, 0, 0);
351 }