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

« back to all changes in this revision

Viewing changes to unix/xc/lib/X11/cmsCmap.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: cmsCmap.c,v 1.3 2000/08/17 19:45:09 cpqbld Exp $ */
 
2
 
 
3
/*
 
4
 * Code and supporting documentation (c) Copyright 1990 1991 Tektronix, Inc.
 
5
 *      All Rights Reserved
 
6
 * 
 
7
 * This file is a component of an X Window System-specific implementation
 
8
 * of Xcms based on the TekColor Color Management System.  Permission is
 
9
 * hereby granted to use, copy, modify, sell, and otherwise distribute this
 
10
 * software and its documentation for any purpose and without fee, provided
 
11
 * that this copyright, permission, and disclaimer notice is reproduced in
 
12
 * all copies of this software and in supporting documentation.  TekColor
 
13
 * is a trademark of Tektronix, Inc.
 
14
 * 
 
15
 * Tektronix makes no representation about the suitability of this software
 
16
 * for any purpose.  It is provided "as is" and with all faults.
 
17
 * 
 
18
 * TEKTRONIX DISCLAIMS ALL WARRANTIES APPLICABLE TO THIS SOFTWARE,
 
19
 * INCLUDING THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 
20
 * PARTICULAR PURPOSE.  IN NO EVENT SHALL TEKTRONIX BE LIABLE FOR ANY
 
21
 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
 
22
 * RESULTING FROM LOSS OF USE, DATA, OR PROFITS, WHETHER IN AN ACTION OF
 
23
 * CONTRACT, NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
 
24
 * CONNECTION WITH THE USE OR THE PERFORMANCE OF THIS SOFTWARE.
 
25
 *
 
26
 *
 
27
 *      NAME
 
28
 *              XcmsCmap.c - Client Colormap Management Routines
 
29
 *
 
30
 *      DESCRIPTION
 
31
 *              Routines that store additional information about
 
32
 *              colormaps being used by the X Client.
 
33
 *
 
34
 *
 
35
 */
 
36
 
 
37
#define NEED_EVENTS
 
38
#define NEED_REPLIES
 
39
#include "Xlibint.h"
 
40
#include "Xcmsint.h"
 
41
#include "Xutil.h"
 
42
 
 
43
/*
 
44
 *      FORWARD DECLARATIONS
 
45
 */
 
46
XcmsCmapRec *_XcmsAddCmapRec();
 
47
static void _XcmsFreeClientCmaps();
 
48
 
 
49
 
 
50
/************************************************************************
 
51
 *                                                                      *
 
52
 *                      PRIVATE INTERFACES                              *
 
53
 *                                                                      *
 
54
 ************************************************************************/
 
55
 
 
56
/*
 
57
 *      NAME
 
58
 *              CmapRecForColormap
 
59
 *
 
60
 *      SYNOPSIS
 
61
 */
 
62
static XcmsCmapRec *
 
63
CmapRecForColormap(dpy, cmap)
 
64
    Display *dpy;
 
65
    Colormap cmap;
 
66
/*
 
67
 *      DESCRIPTION
 
68
 *              Find the corresponding XcmsCmapRec for cmap.  In not found
 
69
 *              this routines attempts to create one.
 
70
 *
 
71
 *      RETURNS
 
72
 *              Returns NULL if failed; otherwise the address to
 
73
 *              the corresponding XcmsCmapRec.
 
74
 *
 
75
 */
 
76
{
 
77
    XcmsCmapRec *pRec;
 
78
    int nScrn;
 
79
    int i, j;
 
80
    XVisualInfo visualTemplate; /* Template of the visual we want */
 
81
    XVisualInfo *visualList;    /* List for visuals that match */
 
82
    int nVisualsMatched;        /* Number of visuals that match */
 
83
    Window tmpWindow;
 
84
    Visual *vp;
 
85
    unsigned long border = 0;
 
86
    _XAsyncHandler async;
 
87
    _XAsyncErrorState async_state;
 
88
 
 
89
    for (pRec = (XcmsCmapRec *)dpy->cms.clientCmaps; pRec != NULL;
 
90
            pRec = pRec->pNext) {
 
91
        if (pRec->cmapID == cmap) {
 
92
            return(pRec);
 
93
        }
 
94
    }
 
95
 
 
96
    /*
 
97
     * Can't find an XcmsCmapRec associated with cmap in our records.
 
98
     * Let's try to see if its a default colormap
 
99
     */
 
100
    nScrn = ScreenCount(dpy);
 
101
    for (i = 0; i < nScrn; i++) {
 
102
        if (cmap == DefaultColormap(dpy, i)) {
 
103
            /* It is ... lets go ahead and store that info */
 
104
            if ((pRec = _XcmsAddCmapRec(dpy, cmap, RootWindow(dpy, i),
 
105
                    DefaultVisual(dpy, i))) == NULL) {
 
106
                return((XcmsCmapRec *)NULL);
 
107
            }
 
108
            pRec->ccc = XcmsCreateCCC(
 
109
                    dpy,
 
110
                    i,                  /* screenNumber */
 
111
                    DefaultVisual(dpy, i),
 
112
                    (XcmsColor *)NULL,  /* clientWhitePt */
 
113
                    (XcmsCompressionProc)NULL,  /* gamutCompProc */
 
114
                    (XPointer)NULL,     /* gamutCompClientData */
 
115
                    (XcmsWhiteAdjustProc)NULL,  /* whitePtAdjProc */
 
116
                    (XPointer)NULL      /* whitePtAdjClientData */
 
117
                    );
 
118
            return(pRec);
 
119
        }
 
120
    }
 
121
 
 
122
    /*
 
123
     * Nope, its not a default colormap, so it's probably a foreign color map
 
124
     * of which we have no specific details.  Let's go through the
 
125
     * rigorous process of finding this colormap:
 
126
     *        for each screen
 
127
     *            for each screen's visual types
 
128
     *                create a window with cmap specified as the colormap
 
129
     *                if successful
 
130
     *                    Add a CmapRec
 
131
     *                    Create an XcmsCCC
 
132
     *                    return the CmapRec
 
133
     *                else
 
134
     *                    continue
 
135
     */
 
136
 
 
137
    async_state.error_code = 0; /* don't care */
 
138
    async_state.major_opcode = X_CreateWindow;
 
139
    async_state.minor_opcode = 0;
 
140
    for (i = 0; i < nScrn; i++) {
 
141
        visualTemplate.screen = i;
 
142
        visualList = XGetVisualInfo(dpy, VisualScreenMask, &visualTemplate,
 
143
            &nVisualsMatched);
 
144
        if (nVisualsMatched == 0) {
 
145
            continue;
 
146
        }
 
147
 
 
148
        /*
 
149
         * Attempt to create a window with cmap
 
150
         */
 
151
        j = 0;
 
152
        do {
 
153
            vp = (visualList+j)->visual;
 
154
            LockDisplay(dpy);
 
155
            {
 
156
                register xCreateWindowReq *req;
 
157
 
 
158
                GetReq(CreateWindow, req);
 
159
                async_state.min_sequence_number = dpy->request;
 
160
                async_state.max_sequence_number = dpy->request;
 
161
                async_state.error_count = 0;
 
162
                async.next = dpy->async_handlers;
 
163
                async.handler = _XAsyncErrorHandler;
 
164
                async.data = (XPointer)&async_state;
 
165
                dpy->async_handlers = &async;
 
166
                req->parent = RootWindow(dpy, i);
 
167
                req->x = 0;
 
168
                req->y = 0;
 
169
                req->width = 1;
 
170
                req->height = 1;
 
171
                req->borderWidth = 0;
 
172
                req->depth = (visualList+j)->depth;
 
173
                req->class = CopyFromParent;
 
174
                req->visual = vp->visualid;
 
175
                tmpWindow = req->wid = XAllocID(dpy);
 
176
                req->mask = CWBorderPixel | CWColormap;
 
177
                req->length += 2;
 
178
                Data32 (dpy, (long *) &border, 4);
 
179
                Data32 (dpy, (long *) &cmap, 4);
 
180
            }
 
181
            {
 
182
                xGetInputFocusReply rep;
 
183
                register xReq *req;
 
184
 
 
185
                GetEmptyReq(GetInputFocus, req);
 
186
                (void) _XReply (dpy, (xReply *)&rep, 0, xTrue);
 
187
            }
 
188
            DeqAsyncHandler(dpy, &async);
 
189
            UnlockDisplay(dpy);
 
190
            SyncHandle();
 
191
        } while (async_state.error_count > 0 && ++j < nVisualsMatched);
 
192
 
 
193
        Xfree((char *)visualList);
 
194
 
 
195
        /*
 
196
         * if successful
 
197
         */
 
198
        if (j < nVisualsMatched) {
 
199
            if ((pRec = _XcmsAddCmapRec(dpy, cmap, tmpWindow, vp)) == NULL)
 
200
                return((XcmsCmapRec *)NULL);
 
201
            pRec->ccc = XcmsCreateCCC(
 
202
                    dpy,
 
203
                    i,                  /* screenNumber */
 
204
                    vp,
 
205
                    (XcmsColor *)NULL,  /* clientWhitePt */
 
206
                    (XcmsCompressionProc)NULL,  /* gamutCompProc */
 
207
                    (XPointer)NULL,     /* gamutCompClientData */
 
208
                    (XcmsWhiteAdjustProc)NULL,  /* whitePtAdjProc */
 
209
                    (XPointer)NULL      /* whitePtAdjClientData */
 
210
                    );
 
211
            XDestroyWindow(dpy, tmpWindow);
 
212
            return(pRec);
 
213
        }
 
214
    }
 
215
 
 
216
    return(NULL);
 
217
}
 
218
 
 
219
 
 
220
 
 
221
/************************************************************************
 
222
 *                                                                      *
 
223
 *                      API PRIVATE INTERFACES                          *
 
224
 *                                                                      *
 
225
 ************************************************************************/
 
226
 
 
227
/*
 
228
 *      NAME
 
229
 *              _XcmsAddCmapRec
 
230
 *
 
231
 *      SYNOPSIS
 
232
 */
 
233
XcmsCmapRec *
 
234
_XcmsAddCmapRec(dpy, cmap, windowID, visual)
 
235
    Display *dpy;
 
236
    Colormap cmap;
 
237
    Window windowID;
 
238
    Visual *visual;
 
239
/*
 
240
 *      DESCRIPTION
 
241
 *              Create an XcmsCmapRec for the specified cmap, windowID,
 
242
 *              and visual, then adds it to its list of CmapRec's.
 
243
 *
 
244
 *      RETURNS
 
245
 *              Returns NULL if failed; otherwise the address to
 
246
 *              the added XcmsCmapRec.
 
247
 *
 
248
 */
 
249
{
 
250
    XcmsCmapRec *pNew;
 
251
 
 
252
    if ((pNew = (XcmsCmapRec *) Xcalloc(1, (unsigned) sizeof(XcmsCmapRec)))
 
253
            == NULL) {
 
254
        return((XcmsCmapRec *)NULL);
 
255
    }
 
256
 
 
257
    pNew->cmapID = cmap;
 
258
    pNew->dpy = dpy;
 
259
    pNew->windowID = windowID;
 
260
    pNew->visual = visual;
 
261
    pNew->pNext = (XcmsCmapRec *)dpy->cms.clientCmaps;
 
262
    dpy->cms.clientCmaps = (XPointer)pNew;
 
263
    dpy->free_funcs->clientCmaps = _XcmsFreeClientCmaps;
 
264
 
 
265
    /*
 
266
     * Note, we don't create the XcmsCCC for pNew->ccc here because
 
267
     * it may require the use of XGetWindowAttributes (a round trip request)
 
268
     * to determine the screen.
 
269
     */
 
270
    return(pNew);
 
271
}
 
272
 
 
273
 
 
274
/*
 
275
 *      NAME
 
276
 *              _XcmsCopyCmapRecAndFree
 
277
 *
 
278
 *      SYNOPSIS
 
279
 */
 
280
XcmsCmapRec *
 
281
_XcmsCopyCmapRecAndFree(dpy, src_cmap, copy_cmap)
 
282
    Display *dpy;
 
283
    Colormap src_cmap;
 
284
    Colormap copy_cmap;
 
285
/*
 
286
 *      DESCRIPTION
 
287
 *              Augments Xlib's XCopyColormapAndFree() to copy
 
288
 *              XcmsCmapRecs.
 
289
 *
 
290
 *      RETURNS
 
291
 *              Returns NULL if failed; otherwise the address to
 
292
 *              the copy XcmsCmapRec.
 
293
 *
 
294
 */
 
295
{
 
296
    XcmsCmapRec *pRec_src;
 
297
    XcmsCmapRec *pRec_copy;
 
298
 
 
299
    if ((pRec_src = CmapRecForColormap(dpy, src_cmap)) != NULL) {
 
300
        pRec_copy =_XcmsAddCmapRec(dpy, copy_cmap, pRec_src->windowID,
 
301
                pRec_src->visual);
 
302
        if (pRec_copy != NULL && pRec_src->ccc) {
 
303
            pRec_copy->ccc = (XcmsCCC)Xcalloc(1, (unsigned) sizeof(XcmsCCCRec));
 
304
            memcpy((char *)pRec_copy->ccc, (char *)pRec_src->ccc,
 
305
                   sizeof(XcmsCCCRec));
 
306
        }
 
307
        return(pRec_copy);
 
308
    }
 
309
    return((XcmsCmapRec *)NULL);
 
310
}
 
311
 
 
312
 
 
313
/*
 
314
 *      NAME
 
315
 *              _XcmsDeleteCmapRec
 
316
 *
 
317
 *      SYNOPSIS
 
318
 */
 
319
void
 
320
_XcmsDeleteCmapRec(dpy, cmap)
 
321
    Display *dpy;
 
322
    Colormap cmap;
 
323
/*
 
324
 *      DESCRIPTION
 
325
 *              Removes and frees the specified XcmsCmapRec structure
 
326
 *              from the linked list of structures.
 
327
 *
 
328
 *      RETURNS
 
329
 *              void
 
330
 *
 
331
 */
 
332
{
 
333
    XcmsCmapRec **pPrevPtr;
 
334
    XcmsCmapRec *pRec;
 
335
    int scr;
 
336
 
 
337
    /* If it is the default cmap for a screen, do not delete it,
 
338
     * because the server will not actually free it */
 
339
    for (scr = ScreenCount(dpy); --scr >= 0; ) {
 
340
        if (cmap == DefaultColormap(dpy, scr))
 
341
            return;
 
342
    }
 
343
 
 
344
    /* search for it in the list */
 
345
    pPrevPtr = (XcmsCmapRec **)&dpy->cms.clientCmaps;
 
346
    while ((pRec = *pPrevPtr) && (pRec->cmapID != cmap)) {
 
347
        pPrevPtr = &pRec->pNext;
 
348
    }
 
349
 
 
350
    if (pRec) {
 
351
        if (pRec->ccc) {
 
352
            XcmsFreeCCC(pRec->ccc);
 
353
        }
 
354
        *pPrevPtr = pRec->pNext;
 
355
        Xfree((char *)pRec);
 
356
    }
 
357
}
 
358
 
 
359
 
 
360
/*
 
361
 *      NAME
 
362
 *              _XcmsFreeClientCmaps
 
363
 *
 
364
 *      SYNOPSIS
 
365
 */
 
366
static void
 
367
_XcmsFreeClientCmaps(dpy)
 
368
    Display *dpy;
 
369
/*
 
370
 *      DESCRIPTION
 
371
 *              Frees all XcmsCmapRec structures in the linked list
 
372
 *              and sets dpy->cms.clientCmaps to NULL.
 
373
 *
 
374
 *      RETURNS
 
375
 *              void
 
376
 *
 
377
 */
 
378
{
 
379
    XcmsCmapRec *pRecNext, *pRecFree;
 
380
 
 
381
    pRecNext = (XcmsCmapRec *)dpy->cms.clientCmaps;
 
382
    while (pRecNext != NULL) {
 
383
        pRecFree = pRecNext;
 
384
        pRecNext = pRecNext->pNext;
 
385
        if (pRecFree->ccc) {
 
386
            /* Free the XcmsCCC structure */
 
387
            XcmsFreeCCC(pRecFree->ccc);
 
388
        }
 
389
        /* Now free the XcmsCmapRec structure */
 
390
        Xfree((char *)pRecFree);
 
391
    }
 
392
    dpy->cms.clientCmaps = (XPointer)NULL;
 
393
}
 
394
 
 
395
 
 
396
 
 
397
/************************************************************************
 
398
 *                                                                      *
 
399
 *                      PUBLIC INTERFACES                               *
 
400
 *                                                                      *
 
401
 ************************************************************************/
 
402
 
 
403
/*
 
404
 *      NAME
 
405
 *              XcmsCCCOfColormap
 
406
 *
 
407
 *      SYNOPSIS
 
408
 */
 
409
XcmsCCC 
 
410
XcmsCCCOfColormap(dpy, cmap)
 
411
    Display *dpy;
 
412
    Colormap cmap;
 
413
/*
 
414
 *      DESCRIPTION
 
415
 *              Finds the XcmsCCC associated with the specified colormap.
 
416
 *
 
417
 *      RETURNS
 
418
 *              Returns NULL if failed; otherwise the address to
 
419
 *              the associated XcmsCCC structure.
 
420
 *
 
421
 */
 
422
{
 
423
    XWindowAttributes windowAttr;
 
424
    XcmsCmapRec *pRec;
 
425
    int nScrn = ScreenCount(dpy);
 
426
    int i;
 
427
 
 
428
    if ((pRec = CmapRecForColormap(dpy, cmap)) != NULL) {
 
429
        if (pRec->ccc) {
 
430
            /* XcmsCmapRec already has a XcmsCCC */
 
431
            return(pRec->ccc);
 
432
        }
 
433
 
 
434
        /*
 
435
         * The XcmsCmapRec does not have a XcmsCCC yet, so let's create
 
436
         * one.  But first, we need to know the screen associated with
 
437
         * cmap, so use XGetWindowAttributes() to extract that
 
438
         * information.  Unless, of course there is only one screen!!
 
439
         */
 
440
        if (nScrn == 1) {
 
441
            /* Assume screenNumber == 0 */
 
442
            return(pRec->ccc = XcmsCreateCCC(
 
443
                    dpy,
 
444
                    0,                  /* screenNumber */
 
445
                    pRec->visual,
 
446
                    (XcmsColor *)NULL,  /* clientWhitePt */
 
447
                    (XcmsCompressionProc)NULL,  /* gamutCompProc */
 
448
                    (XPointer)NULL,     /* gamutCompClientData */
 
449
                    (XcmsWhiteAdjustProc)NULL,  /* whitePtAdjProc */
 
450
                    (XPointer)NULL      /* whitePtAdjClientData */
 
451
                    ));
 
452
        } else {
 
453
            if (XGetWindowAttributes(dpy, pRec->windowID, &windowAttr)) {
 
454
                for (i = 0; i < nScrn; i++) {
 
455
                    if (ScreenOfDisplay(dpy, i) == windowAttr.screen) {
 
456
                        return(pRec->ccc = XcmsCreateCCC(
 
457
                                dpy,
 
458
                                i,                 /* screenNumber */
 
459
                                pRec->visual,
 
460
                                (XcmsColor *)NULL, /* clientWhitePt */
 
461
                                (XcmsCompressionProc)NULL, /* gamutCompProc */
 
462
                                (XPointer)NULL,    /* gamutCompClientData */
 
463
                                (XcmsWhiteAdjustProc)NULL, /* whitePtAdjProc */
 
464
                                (XPointer)NULL     /* whitePtAdjClientData */
 
465
                                ));
 
466
                    }
 
467
                }
 
468
            }
 
469
        }
 
470
    }
 
471
 
 
472
    /*
 
473
     * No such cmap
 
474
     */
 
475
    return(NULL);
 
476
}
 
477
 
 
478
XcmsCCC XcmsSetCCCOfColormap(dpy, cmap, ccc)
 
479
    Display *dpy;
 
480
    Colormap cmap;
 
481
    XcmsCCC ccc;
 
482
{
 
483
    XcmsCCC prev_ccc = NULL;
 
484
    XcmsCmapRec *pRec;
 
485
 
 
486
    pRec = CmapRecForColormap(dpy, cmap);
 
487
    if (pRec) {
 
488
        prev_ccc = pRec->ccc;
 
489
        pRec->ccc = ccc;
 
490
    }
 
491
    return prev_ccc;
 
492
}