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

« back to all changes in this revision

Viewing changes to unix/xc/extras/Xpm/lib/data.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
/*
 
2
 * Copyright (C) 1989-95 GROUPE BULL
 
3
 *
 
4
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 
5
 * of this software and associated documentation files (the "Software"), to
 
6
 * deal in the Software without restriction, including without limitation the
 
7
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 
8
 * sell copies of the Software, and to permit persons to whom the Software is
 
9
 * furnished to do so, subject to the following conditions:
 
10
 *
 
11
 * The above copyright notice and this permission notice shall be included in
 
12
 * all copies or substantial portions of the Software.
 
13
 *
 
14
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
15
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
16
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 
17
 * GROUPE BULL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
 
18
 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 
19
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
20
 *
 
21
 * Except as contained in this notice, the name of GROUPE BULL shall not be
 
22
 * used in advertising or otherwise to promote the sale, use or other dealings
 
23
 * in this Software without prior written authorization from GROUPE BULL.
 
24
 */
 
25
 
 
26
/*****************************************************************************\
 
27
* data.c:                                                                     *
 
28
*                                                                             *
 
29
*  XPM library                                                                *
 
30
*  IO utilities                                                               *
 
31
*                                                                             *
 
32
*  Developed by Arnaud Le Hors                                                *
 
33
\*****************************************************************************/
 
34
/* $XFree86: xc/extras/Xpm/lib/data.c,v 1.4 2002/01/07 19:40:49 dawes Exp $ */
 
35
 
 
36
#ifndef CXPMPROG
 
37
#if 0
 
38
/* Official version number */
 
39
static char *RCS_Version = "$XpmVersion: 3.4k $";
 
40
 
 
41
/* Internal version number */
 
42
static char *RCS_Id = "Id: xpm.shar,v 3.71 1998/03/19 19:47:14 lehors Exp $";
 
43
#endif
 
44
#include "XpmI.h"
 
45
#endif
 
46
#include <ctype.h>
 
47
 
 
48
#ifndef CXPMPROG
 
49
#define Getc(data, file) getc(file)
 
50
#define Ungetc(data, c, file) ungetc(c, file)
 
51
#endif
 
52
 
 
53
static int
 
54
ParseComment(xpmData *data)
 
55
{
 
56
    if (data->type == XPMBUFFER) {
 
57
        register char c;
 
58
        register unsigned int n = 0;
 
59
        unsigned int notend;
 
60
        char *s, *s2;
 
61
 
 
62
        s = data->Comment;
 
63
        *s = data->Bcmt[0];
 
64
 
 
65
        /* skip the string beginning comment */
 
66
        s2 = data->Bcmt;
 
67
        do {
 
68
            c = *data->cptr++;
 
69
            *++s = c;
 
70
            n++;
 
71
            s2++;
 
72
        } while (c == *s2 && *s2 != '\0' && c);
 
73
 
 
74
        if (*s2 != '\0') {
 
75
            /* this wasn't the beginning of a comment */
 
76
            data->cptr -= n;
 
77
            return 0;
 
78
        }
 
79
        /* store comment */
 
80
        data->Comment[0] = *s;
 
81
        s = data->Comment;
 
82
        notend = 1;
 
83
        n = 0;
 
84
        while (notend) {
 
85
            s2 = data->Ecmt;
 
86
            while (*s != *s2 && c) {
 
87
                c = *data->cptr++;
 
88
                if (n == XPMMAXCMTLEN - 1)  { /* forget it */
 
89
                    s = data->Comment;
 
90
                    n = 0;
 
91
                }
 
92
                *++s = c;
 
93
                n++;
 
94
            }
 
95
            data->CommentLength = n;
 
96
            do {
 
97
                c = *data->cptr++;
 
98
                if (n == XPMMAXCMTLEN - 1)  { /* forget it */
 
99
                    s = data->Comment;
 
100
                    n = 0;
 
101
                }
 
102
                *++s = c;
 
103
                n++;
 
104
                s2++;
 
105
            } while (c == *s2 && *s2 != '\0' && c);
 
106
            if (*s2 == '\0') {
 
107
                /* this is the end of the comment */
 
108
                notend = 0;
 
109
                data->cptr--;
 
110
            }
 
111
        }
 
112
        return 0;
 
113
    } else {
 
114
        FILE *file = data->stream.file;
 
115
        register int c;
 
116
        register unsigned int n = 0, a;
 
117
        unsigned int notend;
 
118
        char *s, *s2;
 
119
 
 
120
        s = data->Comment;
 
121
        *s = data->Bcmt[0];
 
122
 
 
123
        /* skip the string beginning comment */
 
124
        s2 = data->Bcmt;
 
125
        do {
 
126
            c = Getc(data, file);
 
127
            *++s = c;
 
128
            n++;
 
129
            s2++;
 
130
        } while (c == *s2 && *s2 != '\0' && c != EOF);
 
131
 
 
132
        if (*s2 != '\0') {
 
133
            /* this wasn't the beginning of a comment */
 
134
            /* put characters back in the order that we got them */
 
135
            for (a = n; a > 0; a--, s--)
 
136
                Ungetc(data, *s, file);
 
137
            return 0;
 
138
        }
 
139
        /* store comment */
 
140
        data->Comment[0] = *s;
 
141
        s = data->Comment;
 
142
        notend = 1;
 
143
        n = 0;
 
144
        while (notend) {
 
145
            s2 = data->Ecmt;
 
146
            while (*s != *s2 && c != EOF) {
 
147
                c = Getc(data, file);
 
148
                if (n == XPMMAXCMTLEN - 1)  { /* forget it */
 
149
                    s = data->Comment;
 
150
                    n = 0;
 
151
                }
 
152
                *++s = c;
 
153
                n++;
 
154
            }
 
155
            data->CommentLength = n;
 
156
            do {
 
157
                c = Getc(data, file);
 
158
                if (n == XPMMAXCMTLEN - 1)  { /* forget it */
 
159
                    s = data->Comment;
 
160
                    n = 0;
 
161
                }
 
162
                *++s = c;
 
163
                n++;
 
164
                s2++;
 
165
            } while (c == *s2 && *s2 != '\0' && c != EOF);
 
166
            if (*s2 == '\0') {
 
167
                /* this is the end of the comment */
 
168
                notend = 0;
 
169
                Ungetc(data, *s, file);
 
170
            }
 
171
        }
 
172
        return 0;
 
173
    }
 
174
}
 
175
 
 
176
/*
 
177
 * skip to the end of the current string and the beginning of the next one
 
178
 */
 
179
int
 
180
xpmNextString(data)
 
181
    xpmData *data;
 
182
{
 
183
    if (!data->type)
 
184
        data->cptr = (data->stream.data)[++data->line];
 
185
    else if (data->type == XPMBUFFER) {
 
186
        register char c;
 
187
 
 
188
        /* get to the end of the current string */
 
189
        if (data->Eos)
 
190
            while ((c = *data->cptr++) && c != data->Eos);
 
191
 
 
192
        /*
 
193
         * then get to the beginning of the next string looking for possible
 
194
         * comment
 
195
         */
 
196
        if (data->Bos) {
 
197
            while ((c = *data->cptr++) && c != data->Bos)
 
198
                if (data->Bcmt && c == data->Bcmt[0])
 
199
                    ParseComment(data);
 
200
        } else if (data->Bcmt) {        /* XPM2 natural */
 
201
            while ((c = *data->cptr++) == data->Bcmt[0])
 
202
                ParseComment(data);
 
203
            data->cptr--;
 
204
        }
 
205
    } else {
 
206
        register int c;
 
207
        FILE *file = data->stream.file;
 
208
 
 
209
        /* get to the end of the current string */
 
210
        if (data->Eos)
 
211
            while ((c = Getc(data, file)) != data->Eos && c != EOF);
 
212
 
 
213
        /*
 
214
         * then get to the beginning of the next string looking for possible
 
215
         * comment
 
216
         */
 
217
        if (data->Bos) {
 
218
            while ((c = Getc(data, file)) != data->Bos && c != EOF)
 
219
                if (data->Bcmt && c == data->Bcmt[0])
 
220
                    ParseComment(data);
 
221
 
 
222
        } else if (data->Bcmt) {        /* XPM2 natural */
 
223
            while ((c = Getc(data, file)) == data->Bcmt[0])
 
224
                ParseComment(data);
 
225
            Ungetc(data, c, file);
 
226
        }
 
227
    }
 
228
    return 0;
 
229
}
 
230
 
 
231
 
 
232
/*
 
233
 * skip whitespace and return the following word
 
234
 */
 
235
unsigned int
 
236
xpmNextWord(data, buf, buflen)
 
237
    xpmData *data;
 
238
    char *buf;
 
239
    unsigned int buflen;
 
240
{
 
241
    register unsigned int n = 0;
 
242
    int c;
 
243
 
 
244
    if (!data->type || data->type == XPMBUFFER) {
 
245
        while (isspace(c = *data->cptr) && c != data->Eos)
 
246
            data->cptr++;
 
247
        do {
 
248
            c = *data->cptr++;
 
249
            *buf++ = c;
 
250
            n++;
 
251
        } while (!isspace(c) && c != data->Eos && n < buflen);
 
252
        n--;
 
253
        data->cptr--;
 
254
    } else {
 
255
        FILE *file = data->stream.file;
 
256
 
 
257
        while ((c = Getc(data, file)) != EOF && isspace(c) && c != data->Eos);
 
258
        while (!isspace(c) && c != data->Eos && c != EOF && n < buflen) {
 
259
            *buf++ = c;
 
260
            n++;
 
261
            c = Getc(data, file);
 
262
        }
 
263
        Ungetc(data, c, file);
 
264
    }
 
265
    return (n);
 
266
}
 
267
 
 
268
/*
 
269
 * skip whitespace and compute the following unsigned int,
 
270
 * returns 1 if one is found and 0 if not
 
271
 */
 
272
int
 
273
xpmNextUI(data, ui_return)
 
274
    xpmData *data;
 
275
    unsigned int *ui_return;
 
276
{
 
277
    char buf[BUFSIZ];
 
278
    int l;
 
279
 
 
280
    l = xpmNextWord(data, buf, BUFSIZ);
 
281
    return xpmatoui(buf, l, ui_return);
 
282
}
 
283
 
 
284
/*
 
285
 * return end of string - WARNING: malloc!
 
286
 */
 
287
int
 
288
xpmGetString(data, sptr, l)
 
289
    xpmData *data;
 
290
    char **sptr;
 
291
    unsigned int *l;
 
292
{
 
293
    unsigned int i, n = 0;
 
294
    int c;
 
295
    char *p = NULL, *q, buf[BUFSIZ];
 
296
 
 
297
    if (!data->type || data->type == XPMBUFFER) {
 
298
        if (data->cptr) {
 
299
            char *start = data->cptr;
 
300
            while ((c = *data->cptr) && c != data->Eos)
 
301
                data->cptr++;
 
302
            n = data->cptr - start + 1;
 
303
            p = (char *) XpmMalloc(n);
 
304
            if (!p)
 
305
                return (XpmNoMemory);
 
306
            strncpy(p, start, n);
 
307
            if (data->type)             /* XPMBUFFER */
 
308
                p[n - 1] = '\0';
 
309
        }
 
310
    } else {
 
311
        FILE *file = data->stream.file;
 
312
 
 
313
        if ((c = Getc(data, file)) == EOF)
 
314
            return (XpmFileInvalid);
 
315
 
 
316
        i = 0;
 
317
        q = buf;
 
318
        p = (char *) XpmMalloc(1);
 
319
        while (c != data->Eos && c != EOF) {
 
320
            if (i == BUFSIZ) {
 
321
                /* get to the end of the buffer */
 
322
                /* malloc needed memory */
 
323
                q = (char *) XpmRealloc(p, n + i);
 
324
                if (!q) {
 
325
                    XpmFree(p);
 
326
                    return (XpmNoMemory);
 
327
                }
 
328
                p = q;
 
329
                q += n;
 
330
                /* and copy what we already have */
 
331
                strncpy(q, buf, i);
 
332
                n += i;
 
333
                i = 0;
 
334
                q = buf;
 
335
            }
 
336
            *q++ = c;
 
337
            i++;
 
338
            c = Getc(data, file);
 
339
        }
 
340
        if (c == EOF) {
 
341
            XpmFree(p);
 
342
            return (XpmFileInvalid);
 
343
        }
 
344
        if (n + i != 0) {
 
345
            /* malloc needed memory */
 
346
            q = (char *) XpmRealloc(p, n + i + 1);
 
347
            if (!q) {
 
348
                XpmFree(p);
 
349
                return (XpmNoMemory);
 
350
            }
 
351
            p = q;
 
352
            q += n;
 
353
            /* and copy the buffer */
 
354
            strncpy(q, buf, i);
 
355
            n += i;
 
356
            p[n++] = '\0';
 
357
        } else {
 
358
            *p = '\0';
 
359
            n = 1;
 
360
        }
 
361
        Ungetc(data, c, file);
 
362
    }
 
363
    *sptr = p;
 
364
    *l = n;
 
365
    return (XpmSuccess);
 
366
}
 
367
 
 
368
/*
 
369
 * get the current comment line
 
370
 */
 
371
int
 
372
xpmGetCmt(data, cmt)
 
373
    xpmData *data;
 
374
    char **cmt;
 
375
{
 
376
    if (!data->type)
 
377
        *cmt = NULL;
 
378
    else if (data->CommentLength) {
 
379
        *cmt = (char *) XpmMalloc(data->CommentLength + 1);
 
380
        strncpy(*cmt, data->Comment, data->CommentLength);
 
381
        (*cmt)[data->CommentLength] = '\0';
 
382
        data->CommentLength = 0;
 
383
    } else
 
384
        *cmt = NULL;
 
385
    return 0;
 
386
}
 
387
 
 
388
xpmDataType xpmDataTypes[] =
 
389
{
 
390
    {"", "!", "\n", '\0', '\n', "", "", "", ""},        /* Natural type */
 
391
    {"C", "/*", "*/", '"', '"', ",\n", "static char *", "[] = {\n", "};\n"},
 
392
    {"Lisp", ";", "\n", '"', '"', "\n", "(setq ", " '(\n", "))\n"},
 
393
    {NULL, NULL, NULL, 0, 0, NULL, NULL, NULL, NULL}
 
394
};
 
395
 
 
396
/*
 
397
 * parse xpm header
 
398
 */
 
399
int
 
400
xpmParseHeader(data)
 
401
    xpmData *data;
 
402
{
 
403
    char buf[BUFSIZ];
 
404
    int l, n = 0;
 
405
 
 
406
    if (data->type) {
 
407
        data->Bos = '\0';
 
408
        data->Eos = '\n';
 
409
        data->Bcmt = data->Ecmt = NULL;
 
410
        l = xpmNextWord(data, buf, BUFSIZ);
 
411
        if (l == 7 && !strncmp("#define", buf, 7)) {
 
412
            /* this maybe an XPM 1 file */
 
413
            char *ptr;
 
414
 
 
415
            l = xpmNextWord(data, buf, BUFSIZ);
 
416
            if (!l)
 
417
                return (XpmFileInvalid);
 
418
            buf[l] = '\0';
 
419
            ptr = rindex(buf, '_');
 
420
            if (!ptr || strncmp("_format", ptr, l - (ptr - buf)))
 
421
                return XpmFileInvalid;
 
422
            /* this is definitely an XPM 1 file */
 
423
            data->format = 1;
 
424
            n = 1;                      /* handle XPM1 as mainly XPM2 C */
 
425
        } else {
 
426
 
 
427
            /*
 
428
             * skip the first word, get the second one, and see if this is
 
429
             * XPM 2 or 3
 
430
             */
 
431
            l = xpmNextWord(data, buf, BUFSIZ);
 
432
            if ((l == 3 && !strncmp("XPM", buf, 3)) ||
 
433
                (l == 4 && !strncmp("XPM2", buf, 4))) {
 
434
                if (l == 3)
 
435
                    n = 1;              /* handle XPM as XPM2 C */
 
436
                else {
 
437
                    /* get the type key word */
 
438
                    l = xpmNextWord(data, buf, BUFSIZ);
 
439
 
 
440
                    /*
 
441
                     * get infos about this type
 
442
                     */
 
443
                    while (xpmDataTypes[n].type
 
444
                           && strncmp(xpmDataTypes[n].type, buf, l))
 
445
                        n++;
 
446
                }
 
447
                data->format = 0;
 
448
            } else
 
449
                /* nope this is not an XPM file */
 
450
                return XpmFileInvalid;
 
451
        }
 
452
        if (xpmDataTypes[n].type) {
 
453
            if (n == 0) {               /* natural type */
 
454
                data->Bcmt = xpmDataTypes[n].Bcmt;
 
455
                data->Ecmt = xpmDataTypes[n].Ecmt;
 
456
                xpmNextString(data);    /* skip the end of the headerline */
 
457
                data->Bos = xpmDataTypes[n].Bos;
 
458
                data->Eos = xpmDataTypes[n].Eos;
 
459
            } else {
 
460
                data->Bcmt = xpmDataTypes[n].Bcmt;
 
461
                data->Ecmt = xpmDataTypes[n].Ecmt;
 
462
                if (!data->format) {    /* XPM 2 or 3 */
 
463
                    data->Bos = xpmDataTypes[n].Bos;
 
464
                    data->Eos = '\0';
 
465
                    /* get to the beginning of the first string */
 
466
                    xpmNextString(data);
 
467
                    data->Eos = xpmDataTypes[n].Eos;
 
468
                } else                  /* XPM 1 skip end of line */
 
469
                    xpmNextString(data);
 
470
            }
 
471
        } else
 
472
            /* we don't know about that type of XPM file... */
 
473
            return XpmFileInvalid;
 
474
    }
 
475
    return XpmSuccess;
 
476
}