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

« back to all changes in this revision

Viewing changes to unix/xc/programs/Xserver/Xprint/pcl/PclSFonts.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: PclSFonts.c,v 1.3 2000/08/17 19:48:08 cpqbld Exp $ */
 
2
/*******************************************************************
 
3
**
 
4
**    *********************************************************
 
5
**    *
 
6
**    *  File:          PclSFonts.c
 
7
**    *
 
8
**    *  Contents:
 
9
**    *                 Send Soft Font Download data to the specified
 
10
**    *                 file pointer.
 
11
**    *
 
12
**    *  Created:       3/4/96
 
13
**    *
 
14
**    *********************************************************
 
15
**
 
16
********************************************************************/
 
17
/*
 
18
(c) Copyright 1996 Hewlett-Packard Company
 
19
(c) Copyright 1996 International Business Machines Corp.
 
20
(c) Copyright 1996 Sun Microsystems, Inc.
 
21
(c) Copyright 1996 Novell, Inc.
 
22
(c) Copyright 1996 Digital Equipment Corp.
 
23
(c) Copyright 1996 Fujitsu Limited
 
24
(c) Copyright 1996 Hitachi, Ltd.
 
25
 
 
26
Permission is hereby granted, free of charge, to any person obtaining a copy
 
27
of this software and associated documentation files (the "Software"), to deal
 
28
in the Software without restriction, including without limitation the rights
 
29
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 
30
copies of the Software, and to permit persons to whom the Software is
 
31
furnished to do so, subject to the following conditions:
 
32
 
 
33
The above copyright notice and this permission notice shall be included in
 
34
all copies or substantial portions of the Software.
 
35
 
 
36
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
37
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
38
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
 
39
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 
40
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 
41
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
42
 
 
43
Except as contained in this notice, the names of the copyright holders shall
 
44
not be used in advertising or otherwise to promote the sale, use or other
 
45
dealings in this Software without prior written authorization from said
 
46
copyright holders.
 
47
*/
 
48
/* $XFree86: xc/programs/Xserver/Xprint/pcl/PclSFonts.c,v 1.6 2001/10/28 03:32:55 tsi Exp $ */
 
49
 
 
50
 
 
51
#include <stdio.h>
 
52
#include "Pcl.h"
 
53
 
 
54
static char tmp1;
 
55
static short tmp2;
 
56
#define Put1byte(fp, x)         tmp1=x; fwrite((char *)&tmp1, 1, 1, fp)
 
57
#define Put2bytes(fp, x)        tmp2=x; fwrite((char *)&tmp2, 2, 1, fp)
 
58
 
 
59
#define ESC 0x1b
 
60
#define SYMBOL_SET 277
 
61
#define MAX_CINDEX 255
 
62
 
 
63
static unsigned int PclDownloadChar(FILE *,PclCharDataPtr,unsigned short,unsigned char);
 
64
static unsigned int PclDownloadHeader(FILE *, PclFontDescPtr, unsigned short);
 
65
 
 
66
#ifdef PCL_FONT_COMPRESS
 
67
static unsigned char *compress_bitmap_data(PclCharDataPtr, unsigned int *);
 
68
#endif /* PCL_FONT_COMPRESS */
 
69
 
 
70
/* -*- PclDownloadSoftFont8 -*-
 
71
 * Send the Character Definition Command for 8-bit font
 
72
 * **************************************************************************/
 
73
void
 
74
PclDownloadSoftFont8(
 
75
    FILE *fp,
 
76
    PclSoftFontInfoPtr pSoftFontInfo,
 
77
    PclFontHead8Ptr pfh,
 
78
    PclCharDataPtr pcd,
 
79
    unsigned char *code
 
80
)
 
81
{
 
82
    /*
 
83
     * Check whether the font header has already been downloaded.
 
84
     * If not, download it.
 
85
     */
 
86
 
 
87
    if ( !pfh->fid ) {
 
88
        pfh->fid = pSoftFontInfo->cur_max_fid++;
 
89
        PclDownloadHeader(fp, &(pfh->fd), pfh->fid);
 
90
    }
 
91
    pfh->index[*code] = *code;
 
92
    PclDownloadChar(fp, pcd, pfh->fid, pfh->index[*code]);
 
93
 
 
94
}
 
95
 
 
96
/* -*- PclDownloadSoftFont16 -*-
 
97
 * Send the Character Definition Command for 16 bit font
 
98
 * **************************************************************************/
 
99
void
 
100
PclDownloadSoftFont16(
 
101
    FILE *fp,
 
102
    PclSoftFontInfoPtr pSoftFontInfo,
 
103
    PclFontHead16Ptr pfh,
 
104
    PclCharDataPtr pcd,
 
105
    unsigned char row,
 
106
    unsigned char col
 
107
)
 
108
{
 
109
    /*
 
110
     * Check whether the font header is already downloaded.
 
111
     * If not, download it.
 
112
     */
 
113
 
 
114
    if ( !pfh->cur_cindex ) {
 
115
        pfh->cur_fid = pSoftFontInfo->cur_max_fid++;
 
116
        PclDownloadHeader(fp, &(pfh->fd), pfh->cur_fid);
 
117
    }
 
118
    pfh->index[row][col].fid = pfh->cur_fid;
 
119
    pfh->index[row][col].cindex = pfh->cur_cindex++;
 
120
    if ( pfh->cur_cindex > MAX_CINDEX )
 
121
        pfh->cur_cindex = 0;
 
122
 
 
123
    PclDownloadChar(fp, pcd, pfh->index[row][col].fid, pfh->index[row][col].cindex);
 
124
}
 
125
 
 
126
/* -*- PclCreateSoftFontInfo -*-
 
127
 * Create and Initialize the structure for storing the information
 
128
 * of the downloaded soft font.
 
129
 * **************************************************************************/
 
130
PclSoftFontInfoPtr
 
131
PclCreateSoftFontInfo(void)
 
132
{
 
133
PclSoftFontInfoPtr pSoftFontInfo;
 
134
 
 
135
    pSoftFontInfo = (PclSoftFontInfoPtr)xalloc(sizeof(PclSoftFontInfoRec));
 
136
    if ( pSoftFontInfo == (PclSoftFontInfoPtr) NULL)
 
137
        return (PclSoftFontInfoPtr) NULL;
 
138
    pSoftFontInfo->phead8 = (PclFontHead8Ptr)NULL;
 
139
    pSoftFontInfo->phead16 = (PclFontHead16Ptr)NULL;
 
140
    pSoftFontInfo->pinfont = (PclInternalFontPtr)NULL;
 
141
    pSoftFontInfo->cur_max_fid = 1;
 
142
    return pSoftFontInfo;
 
143
}
 
144
 
 
145
/* -*- PclDestroySoftFontInfo -*-
 
146
 * Destroy the soft font information structure
 
147
 * **************************************************************************/
 
148
void
 
149
PclDestroySoftFontInfo( PclSoftFontInfoPtr pSoftFontInfo )
 
150
{
 
151
PclFontHead8Ptr  pfh8,  pfh8_next;
 
152
PclFontHead16Ptr pfh16, pfh16_next;
 
153
PclInternalFontPtr pin, pin_next;
 
154
unsigned char nindex_row, nindex_col;
 
155
int i;
 
156
 
 
157
    if ( pSoftFontInfo == (PclSoftFontInfoPtr) NULL )
 
158
        return;
 
159
 
 
160
    pfh8  = pSoftFontInfo->phead8;
 
161
    while (pfh8 != (PclFontHead8Ptr) NULL) {
 
162
        xfree(pfh8->fontname);
 
163
        xfree(pfh8->index);
 
164
        pfh8_next = pfh8->next;
 
165
        xfree(pfh8);
 
166
        pfh8 = pfh8_next;
 
167
    }
 
168
 
 
169
    pfh16 = pSoftFontInfo->phead16;
 
170
    while (pfh16 != (PclFontHead16Ptr) NULL) {
 
171
        xfree(pfh16->fontname);
 
172
        nindex_col = pfh16->lastCol - pfh16->firstCol + 1;
 
173
        nindex_row = pfh16->lastRow - pfh16->firstRow + 1;
 
174
        for (i=0; i<nindex_row; i++)
 
175
            xfree(pfh16->index[i]);
 
176
        xfree(pfh16->index);
 
177
        pfh16_next = pfh16->next;
 
178
        xfree(pfh16);
 
179
        pfh16 = pfh16_next;
 
180
    }
 
181
 
 
182
    pin = pSoftFontInfo->pinfont;
 
183
    while (pin != (PclInternalFontPtr) NULL) {
 
184
        xfree(pin->fontname);
 
185
        pin_next = pin->next;
 
186
        xfree(pin);
 
187
        pin = pin_next;
 
188
    }
 
189
 
 
190
    xfree(pSoftFontInfo);
 
191
}
 
192
 
 
193
/* -*- PclDownloadHeader -*-
 
194
 * Send the Font Header Commnad. 
 
195
 *      Format 0  : Font Header for Pcl Bitmapped Fonts
 
196
 *      Format 20 : Font Header for Resolution Specified Bitmapped Fonts
 
197
 * **************************************************************************/
 
198
static unsigned int
 
199
PclDownloadHeader(
 
200
    FILE *fp,
 
201
    PclFontDescPtr fd,
 
202
    unsigned short fid
 
203
)
 
204
{
 
205
int nbytes;
 
206
 
 
207
#ifdef XP_PCL_LJ3
 
208
    nbytes = 64;
 
209
#else
 
210
    nbytes = 68;
 
211
#endif /* XP_PCL_LJ3 */
 
212
    /*
 
213
     * Font ID Command : Esc *c#D
 
214
     *          (Default = 0, Range = 0 - 32767)
 
215
     */
 
216
    fprintf(fp, "%c*c%dD", ESC, fid);
 
217
 
 
218
    /*
 
219
     * Font Header Commnad : Esc )s#W[font header data]
 
220
     *          (Default = 0, Range = 0 - 32767)
 
221
     */
 
222
    fprintf(fp, "%c)s%dW", ESC, nbytes);
 
223
 
 
224
    Put2bytes(fp, nbytes);                      /* Font Description Size */
 
225
#ifdef XP_PCL_LJ3
 
226
    Put1byte(fp, 0);                            /* Header Format */
 
227
#else
 
228
    Put1byte(fp, 20);                           /* Header Format */
 
229
#endif /* XP_PCL_LJ3 */
 
230
    Put1byte(fp, 2);                            /* Font Type */
 
231
    Put2bytes(fp, 0);                           /* Style MSB */
 
232
    Put2bytes(fp, fd->ascent);                  /* BaseLine Position */
 
233
    Put2bytes(fp, fd->cellwidth);               /* Cell Width */
 
234
    Put2bytes(fp, fd->cellheight);              /* Cell Height */
 
235
    Put1byte(fp, 0);                            /* Orienation */
 
236
    Put1byte(fp, fd->spacing);                  /* Spacing */
 
237
    Put2bytes(fp, SYMBOL_SET);                  /* Symbol Set */
 
238
    Put2bytes(fp, fd->pitch*4);                 /* font pitch */
 
239
    Put2bytes(fp, fd->cellheight * 4);          /* Height */
 
240
    Put2bytes(fp, 0);                           /* x-Height */
 
241
    Put1byte(fp, 0);                            /* width type (normal) */
 
242
    Put1byte(fp, 0);                            /* Style LSB */
 
243
    Put1byte(fp, 0);                            /* Stroke Weight */ 
 
244
    Put1byte(fp, 5);                            /* Typeface LSB */
 
245
    Put1byte(fp, 0);                            /* Typeface MSB */
 
246
    Put1byte(fp, 0);                            /* Serif Style */
 
247
    Put1byte(fp, 0);                            /* Quality */
 
248
    Put1byte(fp, 0);                            /* Placement */
 
249
    Put1byte(fp, 0);                            /* Underline Position */
 
250
    Put1byte(fp, 0);                            /* Underline Thickness */
 
251
    Put2bytes(fp, fd->cellheight*1.2);          /* Text Height */
 
252
    Put2bytes(fp, fd->cellwidth * 4);           /* Text Width */
 
253
    Put2bytes(fp, 0);                           /* First Code */
 
254
    Put2bytes(fp, 255);                         /* Last Code */
 
255
    Put1byte(fp, 0);                            /* Pitch Extend */
 
256
    Put1byte(fp, 0);                            /* Height Extend */
 
257
    Put2bytes(fp, 0);                           /* Cap Height */
 
258
    Put2bytes(fp, 0);                           /* Font Number 1 */
 
259
    Put2bytes(fp, 0);                           /* Font Number 2 */
 
260
    Put2bytes(fp, 0);                           /* Font Name */
 
261
    Put2bytes(fp, 0);                           /* Font Name */
 
262
    Put2bytes(fp, 0);                           /* Font Name */
 
263
    Put2bytes(fp, 0);                           /* Font Name */
 
264
    Put2bytes(fp, 0);                           /* Font Name */
 
265
    Put2bytes(fp, 0);                           /* Font Name */
 
266
    Put2bytes(fp, 0);                           /* Font Name */
 
267
    Put2bytes(fp, 0);                           /* Font Name */
 
268
 
 
269
#ifdef XP_PCL_LJ3
 
270
    return 64;
 
271
#else
 
272
    Put2bytes(fp, 300);                         /* X Resolution */
 
273
    Put2bytes(fp, 300);                         /* Y Resolution */
 
274
    return 68;
 
275
#endif /* XP_PCL_LJ3 */
 
276
 
 
277
}
 
278
 
 
279
/* -*- PclDownloadCharacter -*-
 
280
 * Send the Character Definition Command.
 
281
 * **************************************************************************/
 
282
static unsigned int
 
283
PclDownloadChar(
 
284
    FILE *fp,
 
285
    PclCharDataPtr cd,
 
286
    unsigned short fid,
 
287
    unsigned char code
 
288
)
 
289
{
 
290
unsigned int nbytes, n;
 
291
unsigned char *raster;
 
292
 
 
293
    /*
 
294
     * Font ID Command : Esc *c#D
 
295
     *          (Default = 0, Range = 0 - 32767)
 
296
     * Character Code Command : Esc *c#E
 
297
     *          (Default = 0, Range = 0 - 65535)
 
298
     */
 
299
    fprintf(fp, "%c*c%dd%dE", ESC, fid, code);
 
300
 
 
301
    /*
 
302
     * Character Definition Command : Esc (s#W[character descriptor and data]
 
303
     *          (Default = N/A, Range = 0 - 32767)
 
304
     */
 
305
 
 
306
    nbytes = n = cd->height * ((cd->width + 7) / 8);
 
307
#ifdef PCL_FONT_COMPRESS
 
308
    raster = compress_bitmap_data(cd, &nbytes);
 
309
#else
 
310
    raster = (unsigned char *)NULL;
 
311
#endif /* PCL_FONT_COMPRESS */
 
312
    fprintf(fp, "%c(s%dW", ESC, nbytes + 16);
 
313
 
 
314
    Put1byte(fp, 4);                            /* Format */
 
315
    Put1byte(fp, 0);                            /* Continuation */
 
316
    Put1byte(fp, 14);                           /* Descriptor Size */
 
317
    if (raster) {                               /* Class */
 
318
        Put1byte(fp, 2);
 
319
    } else {
 
320
        Put1byte(fp, 1);                        /* Class */
 
321
    }
 
322
    Put2bytes(fp, 0);                           /* Orientation */
 
323
    Put2bytes(fp, cd->h_offset);                /* left offset */
 
324
    Put2bytes(fp, cd->v_offset);                /* top offset */
 
325
    Put2bytes(fp, cd->width);                   /* character width */
 
326
    Put2bytes(fp, cd->height);                  /* character height */
 
327
    Put2bytes(fp, cd->font_pitch*4);            /* delta X */
 
328
 
 
329
    /*
 
330
     * Raster Character Data
 
331
     */
 
332
    if (raster) {
 
333
        fwrite(raster, nbytes, 1, fp);
 
334
        xfree(raster);
 
335
    } else
 
336
        fwrite(cd->raster_top, nbytes, 1, fp);
 
337
 
 
338
    return n + 16;
 
339
}
 
340
 
 
341
 
 
342
#ifdef PCL_FONT_COMPRESS
 
343
/* -*- compress_bitmap_data -*-
 
344
 * Compress Bitmap data
 
345
 * **************************************************************************/
 
346
static unsigned char *
 
347
compress_bitmap_data(
 
348
    PclCharDataPtr cd,
 
349
    unsigned int *nbytes
 
350
)
 
351
{
 
352
unsigned int  byte_width;
 
353
unsigned char *raster, *rptr_s, *rptr_e, *rptr_end;
 
354
unsigned char *tmp_s, *tmp_ptr;
 
355
unsigned char *p;
 
356
unsigned char cur, pixel;
 
357
unsigned int num;
 
358
 
 
359
int i, j, k, w;
 
360
 
 
361
    byte_width = (cd->width + 7) / 8;
 
362
    *nbytes = cd->height * byte_width;
 
363
 
 
364
    /* Create buffer for storing compress bitmap glyph  */
 
365
    raster = (unsigned char *)xalloc(*nbytes);
 
366
    rptr_s = raster;
 
367
    rptr_e = raster;
 
368
    rptr_end = raster + *nbytes;
 
369
 
 
370
    tmp_s = (unsigned char *)xalloc(cd->width * 8 + 2);
 
371
 
 
372
    p = cd->raster_top;
 
373
    for (i=0; i<cd->height; i++) {
 
374
        tmp_ptr = tmp_s;
 
375
        *tmp_ptr++ = 0;
 
376
        if ( (*p>>7)&0x1 == 1 ) {
 
377
            *tmp_ptr++ = 0;
 
378
            cur = 1;
 
379
        } else {
 
380
            cur = 0;
 
381
        }
 
382
        num = 0;
 
383
        for (j=0, w=0; j<byte_width; j++, p++) {
 
384
            for (k=0; k<8 && w<cd->width; k++, w++) {
 
385
                pixel = (*p>>(7-k))&0x1;
 
386
                if ( pixel == cur ) {
 
387
                    num++;
 
388
                } else {
 
389
                    cur = pixel;
 
390
                    while (num > 255) {
 
391
                        *tmp_ptr++ = 255;
 
392
                        *tmp_ptr++ = 0;
 
393
                        num -= 255;
 
394
                    }
 
395
                    *tmp_ptr++ = num;
 
396
                    num = 1;
 
397
                }
 
398
            }
 
399
        }
 
400
        if ( pixel == cur ) {
 
401
            while (num > 255) {
 
402
                *tmp_ptr++ = 255;
 
403
                *tmp_ptr++ = 0;
 
404
                num -= 255;
 
405
            }
 
406
            *tmp_ptr++ = num&0xff;
 
407
        } else
 
408
            *tmp_ptr++ = num;
 
409
 
 
410
        if ( ((rptr_e - rptr_s) == (tmp_ptr - tmp_s)) &&
 
411
                        !memcmp(rptr_s+1, tmp_s+1, (tmp_ptr - tmp_s) - 1) )
 
412
            *rptr_s += 1;
 
413
        else {
 
414
            if ( rptr_e + (tmp_ptr - tmp_s) > rptr_end ) {
 
415
                xfree(raster);
 
416
                xfree(tmp_s);
 
417
                return (unsigned char *)NULL;
 
418
            }
 
419
            memcpy (rptr_e, tmp_s, tmp_ptr - tmp_s);
 
420
            rptr_s = rptr_e;
 
421
            rptr_e = rptr_s + (tmp_ptr - tmp_s);
 
422
        }
 
423
    }
 
424
    xfree(tmp_s);
 
425
    *nbytes = rptr_e - raster;
 
426
 
 
427
    return raster;
 
428
}
 
429
#endif /* PCL_FONT_COMPRESS */