~ubuntu-branches/ubuntu/gutsy/vnc4/gutsy

« back to all changes in this revision

Viewing changes to unix/xc/lib/font/Speedo/spfile.c

  • Committer: Bazaar Package Importer
  • Author(s): Ola Lundqvist
  • Date: 2006-05-15 20:35:17 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20060515203517-l4lre1ku942mn26k
Tags: 4.1.1+X4.3.0-10
* Correction of critical security issue. Thanks to Martin Kogler
  <e9925248@student.tuwien.ac.at> that informed me about the issue,
  and provided the patch.
  This flaw was originally found by Steve Wiseman of intelliadmin.com.
* Applied patch from Javier Kohen <jkohen@users.sourceforge.net> that
  inform the user that only 8 first characters of the password will
  actually be used when typing more than 8 characters, closes:
  #355619.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $Xorg: spfile.c,v 1.4 2001/02/09 02:04:00 xorgcvs Exp $ */
 
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 1987, 1998  The Open Group
 
28
 
 
29
Permission to use, copy, modify, distribute, and sell this software and its
 
30
documentation for any purpose is hereby granted without fee, provided that
 
31
the above copyright notice appear in all copies and that both that
 
32
copyright notice and this permission notice appear in supporting
 
33
documentation.
 
34
 
 
35
The above copyright notice and this permission notice shall be included
 
36
in all copies or substantial portions of the Software.
 
37
 
 
38
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 
39
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 
40
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 
41
IN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
 
42
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 
43
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 
44
OTHER DEALINGS IN THE SOFTWARE.
 
45
 
 
46
Except as contained in this notice, the name of The Open Group shall
 
47
not be used in advertising or otherwise to promote the sale, use or
 
48
other dealings in this Software without prior written authorization
 
49
from The Open Group.
 
50
 
 
51
*/
 
52
/* $XFree86: xc/lib/font/Speedo/spfile.c,v 1.14 2001/12/14 19:56:41 dawes Exp $ */
 
53
 
 
54
#include "fntfilst.h"
 
55
#include "fontenc.h"
 
56
#ifndef FONTMODULE
 
57
#include <stdio.h>
 
58
#else
 
59
#include "xf86_ansic.h"
 
60
#endif
 
61
 
 
62
#include "spint.h"
 
63
#include "bics-unicode.h"
 
64
 
 
65
SpeedoFontPtr sp_fp_cur = (SpeedoFontPtr) 0;
 
66
 
 
67
#ifdef EXTRAFONTS
 
68
#include "ncdkeys.h"
 
69
#endif
 
70
 
 
71
#include "keys.h"
 
72
 
 
73
#ifdef EXTRAFONTS
 
74
static ufix8 skey[] =
 
75
{
 
76
    SKEY0,
 
77
    SKEY1,
 
78
    SKEY2,
 
79
    SKEY3,
 
80
    SKEY4,
 
81
    SKEY5,
 
82
    SKEY6,
 
83
    SKEY7,
 
84
    SKEY8
 
85
};                              /* Sample Font decryption key */
 
86
 
 
87
static ufix8 rkey[] =
 
88
{
 
89
    RKEY0,
 
90
    RKEY1,
 
91
    RKEY2,
 
92
    RKEY3,
 
93
    RKEY4,
 
94
    RKEY5,
 
95
    RKEY6,
 
96
    RKEY7,
 
97
    RKEY8
 
98
};                              /* Retail Font decryption key */
 
99
 
 
100
#endif                          /* EXTRAFONTS */
 
101
 
 
102
#ifdef XSAMPLEFONTS
 
103
static ufix8 xkey[] =
 
104
{
 
105
    XKEY0,
 
106
    XKEY1,
 
107
    XKEY2,
 
108
    XKEY3,
 
109
    XKEY4,
 
110
    XKEY5,
 
111
    XKEY6,
 
112
    XKEY7,
 
113
    XKEY8
 
114
};                              /* Sample Font decryption key */
 
115
#endif
 
116
 
 
117
static ufix8 mkey[] =
 
118
{
 
119
    KEY0,
 
120
    KEY1,
 
121
    KEY2,
 
122
    KEY3,
 
123
    KEY4,
 
124
    KEY5,
 
125
    KEY6,
 
126
    KEY7,
 
127
    KEY8
 
128
};                              /* Font decryption key */
 
129
 
 
130
 
 
131
static      fix15
 
132
read_2b(ufix8 *ptr)
 
133
{
 
134
    fix15       tmp;
 
135
 
 
136
    tmp = *ptr++;
 
137
    tmp = (tmp << 8) + *ptr;
 
138
    return tmp;
 
139
}
 
140
 
 
141
static      fix31
 
142
read_4b(ufix8 *ptr)
 
143
{
 
144
    fix31       tmp;
 
145
 
 
146
    tmp = *ptr++;
 
147
    tmp = (tmp << 8) + *ptr++;
 
148
    tmp = (tmp << 8) + *ptr++;
 
149
    tmp = (tmp << 8) + *ptr;
 
150
    return tmp;
 
151
}
 
152
 
 
153
/*
 
154
 * loads the specified char's data
 
155
 */
 
156
buff_t     *
 
157
sp_load_char_data(fix31 file_offset, fix15 num, 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
struct speedo_encoding {
 
178
    char *name;
 
179
    int *enc;
 
180
    int enc_size;
 
181
};
 
182
 
 
183
/* Takes care of caching encodings already referenced */
 
184
static int
 
185
find_encoding(const char *fontname, const char *filename,
 
186
                int **enc, int *enc_size)
 
187
{
 
188
    static struct speedo_encoding *known_encodings=0;
 
189
    static int number_known_encodings=0;
 
190
    static int known_encodings_size=0;
 
191
    
 
192
    char *encoding_name;
 
193
    int iso8859_1;
 
194
    FontMapPtr mapping;
 
195
    int i, j, k, size;
 
196
    struct speedo_encoding *temp;
 
197
    int *new_enc;
 
198
    char *new_name;
 
199
    
 
200
    iso8859_1 = 0;
 
201
    
 
202
    encoding_name = FontEncFromXLFD(fontname, strlen(fontname));
 
203
    if(!encoding_name) {
 
204
        encoding_name="iso8859-1";
 
205
        iso8859_1=1;
 
206
    }
 
207
    /* We don't go through the font library if asked for Latin-1 */
 
208
    iso8859_1 = iso8859_1 || !strcmp(encoding_name, "iso8859-1");
 
209
    
 
210
    for(i=0; i<number_known_encodings; i++) {
 
211
        if(!strcmp(encoding_name, known_encodings[i].name)) {
 
212
            *enc=known_encodings[i].enc;
 
213
            *enc_size=known_encodings[i].enc_size;
 
214
            return Successful;
 
215
        }
 
216
    }
 
217
    
 
218
    /* it hasn't been cached yet, need to compute it */
 
219
    
 
220
    /* ensure we've got enough storage first */
 
221
  
 
222
    if(known_encodings==0) {
 
223
        if((known_encodings=
 
224
            (struct speedo_encoding*)xalloc(2*sizeof(struct speedo_encoding)))
 
225
           ==0)
 
226
            return AllocError;
 
227
        number_known_encodings=0;
 
228
        known_encodings_size=2;
 
229
    }
 
230
  
 
231
    if(number_known_encodings >= known_encodings_size) {
 
232
        if((temp=
 
233
            (struct speedo_encoding*)xrealloc(known_encodings,
 
234
                                              2*sizeof(struct speedo_encoding)*
 
235
                                              known_encodings_size))==0)
 
236
            return AllocError;
 
237
        known_encodings=temp;
 
238
        known_encodings_size*=2;
 
239
    }
 
240
 
 
241
    mapping=0;
 
242
    if(!iso8859_1) {
 
243
        mapping = FontEncMapFind(encoding_name, 
 
244
                                 FONT_ENCODING_UNICODE, -1, -1,
 
245
                                 filename);
 
246
    }
 
247
#define SPEEDO_RECODE(c) \
 
248
  (mapping? \
 
249
   unicode_to_bics(FontEncRecode(c, mapping)): \
 
250
   unicode_to_bics(c))
 
251
        
 
252
    if((new_name = (char*)xalloc(strlen(encoding_name)))==0)
 
253
        return AllocError;
 
254
    strcpy(new_name, encoding_name);
 
255
    
 
256
    /* For now, we limit ourselves to 256 glyphs */
 
257
    size=0;
 
258
    for(i=0; i < (mapping?mapping->encoding->size:256) && i < 256; i++)
 
259
        if(SPEEDO_RECODE(i)>=0)
 
260
            size++;
 
261
    new_enc = (int*)xalloc(2*size*sizeof(int));
 
262
    if(!new_enc) {
 
263
        xfree(new_name);
 
264
        return AllocError;
 
265
    }
 
266
    for(i=j=0; i < (mapping?mapping->encoding->size:256) && i < 256; i++)
 
267
        if((k = SPEEDO_RECODE(i))>=0) {
 
268
            new_enc[2*j] = i;
 
269
            new_enc[2*j+1] = k;
 
270
            j++;
 
271
        }
 
272
    known_encodings[number_known_encodings].name = new_name;
 
273
    known_encodings[number_known_encodings].enc = new_enc;
 
274
    known_encodings[number_known_encodings].enc_size = size;
 
275
    number_known_encodings++;
 
276
    
 
277
    *enc = new_enc;
 
278
    *enc_size = size;
 
279
    return Successful;
 
280
#undef SPEEDO_RECODE
 
281
}
 
282
 
 
283
int
 
284
sp_open_master(const char *fontname, const char *filename,
 
285
                SpeedoMasterFontPtr *master)
 
286
{
 
287
    SpeedoMasterFontPtr spmf;
 
288
    ufix8       tmp[16];
 
289
    ufix16      cust_no;
 
290
    FILE       *fp;
 
291
    ufix32      minbufsize;
 
292
    ufix16      mincharsize;
 
293
    ufix8      *f_buffer;
 
294
    ufix8      *c_buffer;
 
295
    int         ret;
 
296
    ufix8      *key;
 
297
 
 
298
    spmf = (SpeedoMasterFontPtr) xalloc(sizeof(SpeedoMasterFontRec));
 
299
    if (!spmf)
 
300
        return AllocError;
 
301
    bzero(spmf, sizeof(SpeedoMasterFontRec));
 
302
    spmf->entry = NULL;
 
303
    spmf->f_buffer = NULL;
 
304
    spmf->c_buffer = NULL;
 
305
 
 
306
    /* open font */
 
307
    spmf->fname = (char *) xalloc(strlen(filename) + 1);
 
308
    if (!spmf->fname)
 
309
        return AllocError;
 
310
    fp = fopen(filename, "r");
 
311
    if (!fp) {
 
312
        ret = BadFontName;
 
313
        goto cleanup;
 
314
    }
 
315
    strcpy(spmf->fname, filename);
 
316
    spmf->fp = fp;
 
317
    spmf->state |= MasterFileOpen;
 
318
 
 
319
    if (fread(tmp, sizeof(ufix8), 16, fp) != 16) {
 
320
        ret = BadFontName;
 
321
        goto cleanup;
 
322
    }
 
323
    minbufsize = (ufix32) read_4b(tmp + FH_FBFSZ);
 
324
    f_buffer = (ufix8 *) xalloc(minbufsize);
 
325
    if (!f_buffer) {
 
326
        ret = AllocError;
 
327
        goto cleanup;
 
328
    }
 
329
    spmf->f_buffer = f_buffer;
 
330
 
 
331
    fseek(fp, (ufix32) 0, 0);
 
332
 
 
333
    /* read in the font */
 
334
    if (fread(f_buffer, sizeof(ufix8), (ufix16) minbufsize, fp) != minbufsize) {
 
335
        ret = BadFontName;
 
336
        goto cleanup;
 
337
    }
 
338
    spmf->copyright = (char *) (f_buffer + FH_CPYRT);
 
339
    spmf->mincharsize = mincharsize = read_2b(f_buffer + FH_CBFSZ);
 
340
 
 
341
    c_buffer = (ufix8 *) xalloc(mincharsize);
 
342
    if (!c_buffer) {
 
343
        ret = AllocError;
 
344
        goto cleanup;
 
345
    }
 
346
    spmf->c_buffer = c_buffer;
 
347
 
 
348
    spmf->font.org = spmf->f_buffer;
 
349
    spmf->font.no_bytes = minbufsize;
 
350
 
 
351
    cust_no = sp_get_cust_no(spmf->font);
 
352
 
 
353
    /* XXX add custom encryption stuff here */
 
354
 
 
355
#ifdef EXTRAFONTS
 
356
    if (cust_no == SCUS0) {
 
357
        key = skey;
 
358
    } else if (cust_no == RCUS0) {
 
359
        key = rkey;
 
360
    } else
 
361
#endif
 
362
 
 
363
#ifdef XSAMPLEFONTS
 
364
    if (cust_no == XCUS0) {
 
365
        key = xkey;
 
366
    } else
 
367
#endif
 
368
 
 
369
    if (cust_no == CUS0) {
 
370
        key = mkey;
 
371
    } else {
 
372
        SpeedoErr("Non - standard encryption for \"%s\"\n", filename);
 
373
        ret = BadFontName;
 
374
        goto cleanup;
 
375
    }
 
376
    spmf->key = key;
 
377
    sp_set_key(key);
 
378
 
 
379
    spmf->first_char_id = read_2b(f_buffer + FH_FCHRF);
 
380
    spmf->num_chars = read_2b(f_buffer + FH_NCHRL);
 
381
 
 
382
 
 
383
    spmf->enc = 0;
 
384
    spmf->enc_size = 0;
 
385
 
 
386
#ifdef EXTRAFONTS
 
387
    {                           /* choose the proper encoding */
 
388
        char       *f;
 
389
 
 
390
        f = strrchr(filename, '/');
 
391
        if (f) {
 
392
            f++;
 
393
            if (strncmp(f, "bx113", 5) == 0) {
 
394
                spmf->enc = adobe_map;
 
395
                spmf->enc_size = adobe_map_size;
 
396
            }
 
397
        }
 
398
    }
 
399
#endif
 
400
 
 
401
    if(!spmf->enc)
 
402
      if((ret=find_encoding(fontname, filename, &spmf->enc, &spmf->enc_size))
 
403
         !=Successful)
 
404
        goto cleanup;
 
405
 
 
406
    spmf->first_char_id = spmf->enc[0];
 
407
    /* size of extents array */
 
408
    spmf->max_id = spmf->enc[(spmf->enc_size - 1) * 2];
 
409
    spmf->num_chars = spmf->enc_size;
 
410
 
 
411
    *master = spmf;
 
412
 
 
413
    return Successful;
 
414
 
 
415
cleanup:
 
416
    *master = (SpeedoMasterFontPtr) 0;
 
417
    sp_close_master_font(spmf);
 
418
    return ret;
 
419
}
 
420
 
 
421
void
 
422
sp_close_master_font(SpeedoMasterFontPtr spmf)
 
423
{
 
424
    if (!spmf)
 
425
        return;
 
426
    if (spmf->state & MasterFileOpen)
 
427
        fclose(spmf->fp);
 
428
    if (spmf->entry)
 
429
        spmf->entry->u.scalable.extra->private = NULL;
 
430
    xfree(spmf->fname);
 
431
    xfree(spmf->f_buffer);
 
432
    xfree(spmf->c_buffer);
 
433
    xfree(spmf);
 
434
}
 
435
 
 
436
void
 
437
sp_close_master_file(SpeedoMasterFontPtr spmf)
 
438
{
 
439
    (void) fclose(spmf->fp);
 
440
    spmf->state &= ~MasterFileOpen;
 
441
}
 
442
 
 
443
 
 
444
/*
 
445
 * reset the encryption key, and make sure the file is opened
 
446
 */
 
447
void
 
448
sp_reset_master(SpeedoMasterFontPtr spmf)
 
449
{
 
450
    sp_set_key(spmf->key);
 
451
    if (!(spmf->state & MasterFileOpen)) {
 
452
        spmf->fp = fopen(spmf->fname, "r");
 
453
        /* XXX -- what to do if we can't open the file? */
 
454
        spmf->state |= MasterFileOpen;
 
455
    }
 
456
    fseek(spmf->fp, 0, 0);
 
457
}