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

« back to all changes in this revision

Viewing changes to unix/xc/lib/X11/RdBitF.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: RdBitF.c,v 1.5 2001/02/09 02:03:35 xorgcvs Exp $ */
 
2
/*
 
3
 
 
4
Copyright 1987, 1998  The Open Group
 
5
 
 
6
Permission to use, copy, modify, distribute, and sell this software and its
 
7
documentation for any purpose is hereby granted without fee, provided that
 
8
the above copyright notice appear in all copies and that both that
 
9
copyright notice and this permission notice appear in supporting
 
10
documentation.
 
11
 
 
12
The above copyright notice and this permission notice shall be included
 
13
in all copies or substantial portions of the Software.
 
14
 
 
15
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 
16
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 
17
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 
18
IN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
 
19
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 
20
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 
21
OTHER DEALINGS IN THE SOFTWARE.
 
22
 
 
23
Except as contained in this notice, the name of The Open Group shall
 
24
not be used in advertising or otherwise to promote the sale, use or
 
25
other dealings in this Software without prior written authorization
 
26
from The Open Group.
 
27
 
 
28
*/
 
29
/* $XFree86: xc/lib/X11/RdBitF.c,v 3.5 2002/05/31 18:45:41 dawes Exp $ */
 
30
 
 
31
/*
 
32
 *      Code to read bitmaps from disk files. Interprets 
 
33
 *      data from X10 and X11 bitmap files and creates
 
34
 *      Pixmap representations of files. Returns Pixmap
 
35
 *      ID and specifics about image.
 
36
 *
 
37
 *      Modified for speedup by Jim Becker, changed image
 
38
 *      data parsing logic (removed some fscanf()s). 
 
39
 *      Aug 5, 1988
 
40
 *
 
41
 * Note that this file and ../Xmu/RdBitF.c look very similar....  Keep them
 
42
 * that way (but don't use common source code so that people can have one 
 
43
 * without the other).
 
44
 */
 
45
 
 
46
#include "Xlibint.h"
 
47
#include <X11/Xos.h>
 
48
#include "Xutil.h"
 
49
#include <stdio.h>
 
50
#include <ctype.h>
 
51
 
 
52
 
 
53
#define MAX_SIZE 255
 
54
 
 
55
/* shared data for the image read/parse logic */
 
56
static short hexTable[256];             /* conversion value */
 
57
static Bool initialized = False;        /* easier to fill in at run time */
 
58
 
 
59
 
 
60
/*
 
61
 *      Table index for the hex values. Initialized once, first time.
 
62
 *      Used for translation value or delimiter significance lookup.
 
63
 */
 
64
static void initHexTable()
 
65
{
 
66
    /*
 
67
     * We build the table at run time for several reasons:
 
68
     *
 
69
     *     1.  portable to non-ASCII machines.
 
70
     *     2.  still reentrant since we set the init flag after setting table.
 
71
     *     3.  easier to extend.
 
72
     *     4.  less prone to bugs.
 
73
     */
 
74
    hexTable['0'] = 0;  hexTable['1'] = 1;
 
75
    hexTable['2'] = 2;  hexTable['3'] = 3;
 
76
    hexTable['4'] = 4;  hexTable['5'] = 5;
 
77
    hexTable['6'] = 6;  hexTable['7'] = 7;
 
78
    hexTable['8'] = 8;  hexTable['9'] = 9;
 
79
    hexTable['A'] = 10; hexTable['B'] = 11;
 
80
    hexTable['C'] = 12; hexTable['D'] = 13;
 
81
    hexTable['E'] = 14; hexTable['F'] = 15;
 
82
    hexTable['a'] = 10; hexTable['b'] = 11;
 
83
    hexTable['c'] = 12; hexTable['d'] = 13;
 
84
    hexTable['e'] = 14; hexTable['f'] = 15;
 
85
 
 
86
    /* delimiters of significance are flagged w/ negative value */
 
87
    hexTable[' '] = -1; hexTable[','] = -1;
 
88
    hexTable['}'] = -1; hexTable['\n'] = -1;
 
89
    hexTable['\t'] = -1;
 
90
        
 
91
    initialized = True;
 
92
}
 
93
 
 
94
/*
 
95
 *      read next hex value in the input stream, return -1 if EOF
 
96
 */
 
97
static int
 
98
NextInt (fstream)
 
99
    FILE *fstream;
 
100
{
 
101
    int ch;
 
102
    int value = 0;
 
103
    int gotone = 0;
 
104
    int done = 0;
 
105
    
 
106
    /* loop, accumulate hex value until find delimiter  */
 
107
    /* skip any initial delimiters found in read stream */
 
108
 
 
109
    while (!done) {
 
110
        ch = getc(fstream);
 
111
        if (ch == EOF) {
 
112
            value       = -1;
 
113
            done++;
 
114
        } else {
 
115
            /* trim high bits, check type and accumulate */
 
116
            ch &= 0xff;
 
117
            if (isascii(ch) && isxdigit(ch)) {
 
118
                value = (value << 4) + hexTable[ch];
 
119
                gotone++;
 
120
            } else if ((hexTable[ch]) < 0 && gotone)
 
121
              done++;
 
122
        }
 
123
    }
 
124
    return value;
 
125
}
 
126
 
 
127
#if NeedFunctionPrototypes
 
128
int XReadBitmapFileData (
 
129
    _Xconst char *filename,
 
130
    unsigned int *width,                /* RETURNED */
 
131
    unsigned int *height,               /* RETURNED */
 
132
    unsigned char **data,               /* RETURNED */
 
133
    int *x_hot,                         /* RETURNED */
 
134
    int *y_hot)                         /* RETURNED */
 
135
#else
 
136
int XReadBitmapFileData (filename, width, height, data, x_hot, y_hot)
 
137
    char *filename;
 
138
    unsigned int *width, *height;       /* RETURNED */
 
139
    unsigned char **data;               /* RETURNED */
 
140
    int *x_hot, *y_hot;                 /* RETURNED */
 
141
#endif
 
142
{
 
143
    FILE *fstream;                      /* handle on file  */
 
144
    unsigned char *bits = NULL;         /* working variable */
 
145
    char line[MAX_SIZE];                /* input line from file */
 
146
    int size;                           /* number of bytes of data */
 
147
    char name_and_type[MAX_SIZE];       /* an input line */
 
148
    char *type;                         /* for parsing */
 
149
    int value;                          /* from an input line */
 
150
    int version10p;                     /* boolean, old format */
 
151
    int padding;                        /* to handle alignment */
 
152
    int bytes_per_line;                 /* per scanline of data */
 
153
    unsigned int ww = 0;                /* width */
 
154
    unsigned int hh = 0;                /* height */
 
155
    int hx = -1;                        /* x hotspot */
 
156
    int hy = -1;                        /* y hotspot */
 
157
 
 
158
    /* first time initialization */
 
159
    if (initialized == False) initHexTable();
 
160
 
 
161
#ifdef __UNIXOS2__
 
162
    filename = __XOS2RedirRoot(filename);
 
163
#endif
 
164
    if (!(fstream = fopen(filename, "r")))
 
165
        return BitmapOpenFailed;
 
166
 
 
167
    /* error cleanup and return macro   */
 
168
#define RETURN(code) \
 
169
{ if (bits) Xfree ((char *)bits); fclose (fstream); return code; }
 
170
 
 
171
    while (fgets(line, MAX_SIZE, fstream)) {
 
172
        if (strlen(line) == MAX_SIZE-1)
 
173
            RETURN (BitmapFileInvalid);
 
174
        if (sscanf(line,"#define %s %d",name_and_type,&value) == 2) {
 
175
            if (!(type = strrchr(name_and_type, '_')))
 
176
              type = name_and_type;
 
177
            else
 
178
              type++;
 
179
 
 
180
            if (!strcmp("width", type))
 
181
              ww = (unsigned int) value;
 
182
            if (!strcmp("height", type))
 
183
              hh = (unsigned int) value;
 
184
            if (!strcmp("hot", type)) {
 
185
                if (type-- == name_and_type || type-- == name_and_type)
 
186
                  continue;
 
187
                if (!strcmp("x_hot", type))
 
188
                  hx = value;
 
189
                if (!strcmp("y_hot", type))
 
190
                  hy = value;
 
191
            }
 
192
            continue;
 
193
        }
 
194
    
 
195
        if (sscanf(line, "static short %s = {", name_and_type) == 1)
 
196
          version10p = 1;
 
197
        else if (sscanf(line,"static unsigned char %s = {",name_and_type) == 1)
 
198
          version10p = 0;
 
199
        else if (sscanf(line, "static char %s = {", name_and_type) == 1)
 
200
          version10p = 0;
 
201
        else
 
202
          continue;
 
203
 
 
204
        if (!(type = strrchr(name_and_type, '_')))
 
205
          type = name_and_type;
 
206
        else
 
207
          type++;
 
208
 
 
209
        if (strcmp("bits[]", type))
 
210
          continue;
 
211
    
 
212
        if (!ww || !hh)
 
213
          RETURN (BitmapFileInvalid);
 
214
 
 
215
        if ((ww % 16) && ((ww % 16) < 9) && version10p)
 
216
          padding = 1;
 
217
        else
 
218
          padding = 0;
 
219
 
 
220
        bytes_per_line = (ww+7)/8 + padding;
 
221
 
 
222
        size = bytes_per_line * hh;
 
223
        bits = (unsigned char *) Xmalloc ((unsigned int) size);
 
224
        if (!bits) 
 
225
          RETURN (BitmapNoMemory);
 
226
 
 
227
        if (version10p) {
 
228
            unsigned char *ptr;
 
229
            int bytes;
 
230
 
 
231
            for (bytes=0, ptr=bits; bytes<size; (bytes += 2)) {
 
232
                if ((value = NextInt(fstream)) < 0)
 
233
                  RETURN (BitmapFileInvalid);
 
234
                *(ptr++) = value;
 
235
                if (!padding || ((bytes+2) % bytes_per_line))
 
236
                  *(ptr++) = value >> 8;
 
237
            }
 
238
        } else {
 
239
            unsigned char *ptr;
 
240
            int bytes;
 
241
 
 
242
            for (bytes=0, ptr=bits; bytes<size; bytes++, ptr++) {
 
243
                if ((value = NextInt(fstream)) < 0) 
 
244
                  RETURN (BitmapFileInvalid);
 
245
                *ptr=value;
 
246
            }
 
247
        }
 
248
    }                                   /* end while */
 
249
 
 
250
    fclose(fstream);
 
251
    if (!bits)
 
252
        return (BitmapFileInvalid);
 
253
 
 
254
    *data = bits;
 
255
    *width = ww;
 
256
    *height = hh;
 
257
    if (x_hot) *x_hot = hx;
 
258
    if (y_hot) *y_hot = hy;
 
259
 
 
260
    return (BitmapSuccess);
 
261
}
 
262
 
 
263
#if NeedFunctionPrototypes
 
264
int XReadBitmapFile (
 
265
    Display *display,
 
266
    Drawable d,
 
267
    _Xconst char *filename,
 
268
    unsigned int *width,                /* RETURNED */
 
269
    unsigned int *height,               /* RETURNED */
 
270
    Pixmap *pixmap,                     /* RETURNED */
 
271
    int *x_hot,                         /* RETURNED */
 
272
    int *y_hot)                         /* RETURNED */
 
273
#else
 
274
int XReadBitmapFile (display, d, filename, width, height, pixmap, x_hot, y_hot)
 
275
    Display *display;
 
276
    Drawable d;
 
277
    char *filename;
 
278
    unsigned int *width, *height;       /* RETURNED */
 
279
    Pixmap *pixmap;                     /* RETURNED */
 
280
    int *x_hot, *y_hot;                 /* RETURNED */
 
281
#endif
 
282
{
 
283
    unsigned char *data;
 
284
    int res;
 
285
 
 
286
    res = XReadBitmapFileData(filename, width, height, &data, x_hot, y_hot);
 
287
    if (res != BitmapSuccess)
 
288
        return res;
 
289
    *pixmap = XCreateBitmapFromData(display, d, (char *)data, *width, *height);
 
290
    Xfree((char *)data);
 
291
    if (*pixmap == None)
 
292
        return (BitmapNoMemory);
 
293
    return (BitmapSuccess);
 
294
}