os: Add CryptoAPI as a choice of SHA1 implementation
[xserver/hramrach.git] / dix / property.c
blobb1b83124f5228cdff6c5f3c19500a3be2245829c
1 /***********************************************************
3 Copyright 1987, 1998 The Open Group
5 Permission to use, copy, modify, distribute, and sell this software and its
6 documentation for any purpose is hereby granted without fee, provided that
7 the above copyright notice appear in all copies and that both that
8 copyright notice and this permission notice appear in supporting
9 documentation.
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
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 THE
17 OPEN GROUP 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.
21 Except as contained in this notice, the name of The Open Group 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 The Open Group.
25 Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts.
27 All Rights Reserved
29 Permission to use, copy, modify, and distribute this software and its
30 documentation for any purpose and without fee is hereby granted,
31 provided that the above copyright notice appear in all copies and that
32 both that copyright notice and this permission notice appear in
33 supporting documentation, and that the name of Digital not be
34 used in advertising or publicity pertaining to distribution of the
35 software without specific, written prior permission.
37 DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
38 ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
39 DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
40 ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
41 WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
42 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
43 SOFTWARE.
45 ******************************************************************/
47 #ifdef HAVE_DIX_CONFIG_H
48 #include <dix-config.h>
49 #endif
51 #include <X11/X.h>
52 #include <X11/Xproto.h>
53 #include "windowstr.h"
54 #include "propertyst.h"
55 #include "dixstruct.h"
56 #include "dispatch.h"
57 #include "swaprep.h"
58 #include "xace.h"
60 /*****************************************************************
61 * Property Stuff
63 * dixLookupProperty, dixChangeProperty, DeleteProperty
65 * Properties belong to windows. The list of properties should not be
66 * traversed directly. Instead, use the three functions listed above.
68 *****************************************************************/
70 #ifdef notdef
71 static void
72 PrintPropertys(WindowPtr pWin)
74 PropertyPtr pProp;
75 int j;
77 pProp = pWin->userProps;
78 while (pProp) {
79 ErrorF("[dix] %x %x\n", pProp->propertyName, pProp->type);
80 ErrorF("[dix] property format: %d\n", pProp->format);
81 ErrorF("[dix] property data: \n");
82 for (j = 0; j < (pProp->format / 8) * pProp->size; j++)
83 ErrorF("[dix] %c\n", pProp->data[j]);
84 pProp = pProp->next;
87 #endif
89 int
90 dixLookupProperty(PropertyPtr *result, WindowPtr pWin, Atom propertyName,
91 ClientPtr client, Mask access_mode)
93 PropertyPtr pProp;
94 int rc = BadMatch;
96 client->errorValue = propertyName;
98 for (pProp = wUserProps(pWin); pProp; pProp = pProp->next)
99 if (pProp->propertyName == propertyName)
100 break;
102 if (pProp)
103 rc = XaceHookPropertyAccess(client, pWin, &pProp, access_mode);
104 *result = pProp;
105 return rc;
108 static void
109 deliverPropertyNotifyEvent(WindowPtr pWin, int state, Atom atom)
111 xEvent event;
113 memset(&event, 0, sizeof(xEvent));
114 event.u.u.type = PropertyNotify;
115 event.u.property.window = pWin->drawable.id;
116 event.u.property.state = state;
117 event.u.property.atom = atom;
118 event.u.property.time = currentTime.milliseconds;
119 DeliverEvents(pWin, &event, 1, (WindowPtr) NULL);
123 ProcRotateProperties(ClientPtr client)
125 int i, j, delta, rc;
127 REQUEST(xRotatePropertiesReq);
128 WindowPtr pWin;
129 Atom *atoms;
130 PropertyPtr *props; /* array of pointer */
131 PropertyPtr pProp, saved;
133 REQUEST_FIXED_SIZE(xRotatePropertiesReq, stuff->nAtoms << 2);
134 UpdateCurrentTime();
135 rc = dixLookupWindow(&pWin, stuff->window, client, DixSetPropAccess);
136 if (rc != Success || stuff->nAtoms <= 0)
137 return rc;
139 atoms = (Atom *) &stuff[1];
140 props = malloc(stuff->nAtoms * sizeof(PropertyPtr));
141 saved = malloc(stuff->nAtoms * sizeof(PropertyRec));
142 if (!props || !saved) {
143 rc = BadAlloc;
144 goto out;
147 for (i = 0; i < stuff->nAtoms; i++) {
148 if (!ValidAtom(atoms[i])) {
149 rc = BadAtom;
150 client->errorValue = atoms[i];
151 goto out;
153 for (j = i + 1; j < stuff->nAtoms; j++)
154 if (atoms[j] == atoms[i]) {
155 rc = BadMatch;
156 goto out;
159 rc = dixLookupProperty(&pProp, pWin, atoms[i], client,
160 DixReadAccess | DixWriteAccess);
161 if (rc != Success)
162 goto out;
164 props[i] = pProp;
165 saved[i] = *pProp;
167 delta = stuff->nPositions;
169 /* If the rotation is a complete 360 degrees, then moving the properties
170 around and generating PropertyNotify events should be skipped. */
172 if (abs(delta) % stuff->nAtoms) {
173 while (delta < 0) /* faster if abs value is small */
174 delta += stuff->nAtoms;
175 for (i = 0; i < stuff->nAtoms; i++) {
176 j = (i + delta) % stuff->nAtoms;
177 deliverPropertyNotifyEvent(pWin, PropertyNewValue, atoms[i]);
179 /* Preserve name and devPrivates */
180 props[j]->type = saved[i].type;
181 props[j]->format = saved[i].format;
182 props[j]->size = saved[i].size;
183 props[j]->data = saved[i].data;
186 out:
187 free(saved);
188 free(props);
189 return rc;
193 ProcChangeProperty(ClientPtr client)
195 WindowPtr pWin;
196 char format, mode;
197 unsigned long len;
198 int sizeInBytes, totalSize, err;
200 REQUEST(xChangePropertyReq);
202 REQUEST_AT_LEAST_SIZE(xChangePropertyReq);
203 UpdateCurrentTime();
204 format = stuff->format;
205 mode = stuff->mode;
206 if ((mode != PropModeReplace) && (mode != PropModeAppend) &&
207 (mode != PropModePrepend)) {
208 client->errorValue = mode;
209 return BadValue;
211 if ((format != 8) && (format != 16) && (format != 32)) {
212 client->errorValue = format;
213 return BadValue;
215 len = stuff->nUnits;
216 if (len > bytes_to_int32(0xffffffff - sizeof(xChangePropertyReq)))
217 return BadLength;
218 sizeInBytes = format >> 3;
219 totalSize = len * sizeInBytes;
220 REQUEST_FIXED_SIZE(xChangePropertyReq, totalSize);
222 err = dixLookupWindow(&pWin, stuff->window, client, DixSetPropAccess);
223 if (err != Success)
224 return err;
225 if (!ValidAtom(stuff->property)) {
226 client->errorValue = stuff->property;
227 return BadAtom;
229 if (!ValidAtom(stuff->type)) {
230 client->errorValue = stuff->type;
231 return BadAtom;
234 err = dixChangeWindowProperty(client, pWin, stuff->property, stuff->type,
235 (int) format, (int) mode, len, &stuff[1],
236 TRUE);
237 if (err != Success)
238 return err;
239 else
240 return Success;
244 dixChangeWindowProperty(ClientPtr pClient, WindowPtr pWin, Atom property,
245 Atom type, int format, int mode, unsigned long len,
246 pointer value, Bool sendevent)
248 PropertyPtr pProp;
249 PropertyRec savedProp;
250 int sizeInBytes, totalSize, rc;
251 unsigned char *data;
252 Mask access_mode;
254 sizeInBytes = format >> 3;
255 totalSize = len * sizeInBytes;
256 access_mode = (mode == PropModeReplace) ? DixWriteAccess : DixBlendAccess;
258 /* first see if property already exists */
259 rc = dixLookupProperty(&pProp, pWin, property, pClient, access_mode);
261 if (rc == BadMatch) { /* just add to list */
262 if (!pWin->optional && !MakeWindowOptional(pWin))
263 return BadAlloc;
264 pProp = dixAllocateObjectWithPrivates(PropertyRec, PRIVATE_PROPERTY);
265 if (!pProp)
266 return BadAlloc;
267 data = malloc(totalSize);
268 if (!data && len) {
269 dixFreeObjectWithPrivates(pProp, PRIVATE_PROPERTY);
270 return BadAlloc;
272 memcpy(data, value, totalSize);
273 pProp->propertyName = property;
274 pProp->type = type;
275 pProp->format = format;
276 pProp->data = data;
277 pProp->size = len;
278 rc = XaceHookPropertyAccess(pClient, pWin, &pProp,
279 DixCreateAccess | DixWriteAccess);
280 if (rc != Success) {
281 free(data);
282 dixFreeObjectWithPrivates(pProp, PRIVATE_PROPERTY);
283 pClient->errorValue = property;
284 return rc;
286 pProp->next = pWin->optional->userProps;
287 pWin->optional->userProps = pProp;
289 else if (rc == Success) {
290 /* To append or prepend to a property the request format and type
291 must match those of the already defined property. The
292 existing format and type are irrelevant when using the mode
293 "PropModeReplace" since they will be written over. */
295 if ((format != pProp->format) && (mode != PropModeReplace))
296 return BadMatch;
297 if ((pProp->type != type) && (mode != PropModeReplace))
298 return BadMatch;
300 /* save the old values for later */
301 savedProp = *pProp;
303 if (mode == PropModeReplace) {
304 data = malloc(totalSize);
305 if (!data && len)
306 return BadAlloc;
307 memcpy(data, value, totalSize);
308 pProp->data = data;
309 pProp->size = len;
310 pProp->type = type;
311 pProp->format = format;
313 else if (len == 0) {
314 /* do nothing */
316 else if (mode == PropModeAppend) {
317 data = malloc((pProp->size + len) * sizeInBytes);
318 if (!data)
319 return BadAlloc;
320 memcpy(data, pProp->data, pProp->size * sizeInBytes);
321 memcpy(data + pProp->size * sizeInBytes, value, totalSize);
322 pProp->data = data;
323 pProp->size += len;
325 else if (mode == PropModePrepend) {
326 data = malloc(sizeInBytes * (len + pProp->size));
327 if (!data)
328 return BadAlloc;
329 memcpy(data + totalSize, pProp->data, pProp->size * sizeInBytes);
330 memcpy(data, value, totalSize);
331 pProp->data = data;
332 pProp->size += len;
335 /* Allow security modules to check the new content */
336 access_mode |= DixPostAccess;
337 rc = XaceHookPropertyAccess(pClient, pWin, &pProp, access_mode);
338 if (rc == Success) {
339 if (savedProp.data != pProp->data)
340 free(savedProp.data);
342 else {
343 if (savedProp.data != pProp->data)
344 free(pProp->data);
345 *pProp = savedProp;
346 return rc;
349 else
350 return rc;
352 if (sendevent)
353 deliverPropertyNotifyEvent(pWin, PropertyNewValue, pProp->propertyName);
355 return Success;
359 ChangeWindowProperty(WindowPtr pWin, Atom property, Atom type, int format,
360 int mode, unsigned long len, pointer value, Bool sendevent)
362 return dixChangeWindowProperty(serverClient, pWin, property, type, format,
363 mode, len, value, sendevent);
367 DeleteProperty(ClientPtr client, WindowPtr pWin, Atom propName)
369 PropertyPtr pProp, prevProp;
370 int rc;
372 rc = dixLookupProperty(&pProp, pWin, propName, client, DixDestroyAccess);
373 if (rc == BadMatch)
374 return Success; /* Succeed if property does not exist */
376 if (rc == Success) {
377 if (pWin->optional->userProps == pProp) {
378 /* Takes care of head */
379 if (!(pWin->optional->userProps = pProp->next))
380 CheckWindowOptionalNeed(pWin);
382 else {
383 /* Need to traverse to find the previous element */
384 prevProp = pWin->optional->userProps;
385 while (prevProp->next != pProp)
386 prevProp = prevProp->next;
387 prevProp->next = pProp->next;
390 deliverPropertyNotifyEvent(pWin, PropertyDelete, pProp->propertyName);
391 free(pProp->data);
392 dixFreeObjectWithPrivates(pProp, PRIVATE_PROPERTY);
394 return rc;
397 void
398 DeleteAllWindowProperties(WindowPtr pWin)
400 PropertyPtr pProp, pNextProp;
402 pProp = wUserProps(pWin);
403 while (pProp) {
404 deliverPropertyNotifyEvent(pWin, PropertyDelete, pProp->propertyName);
405 pNextProp = pProp->next;
406 free(pProp->data);
407 dixFreeObjectWithPrivates(pProp, PRIVATE_PROPERTY);
408 pProp = pNextProp;
411 if (pWin->optional)
412 pWin->optional->userProps = NULL;
415 static int
416 NullPropertyReply(ClientPtr client,
417 ATOM propertyType, int format, xGetPropertyReply * reply)
419 reply->nItems = 0;
420 reply->length = 0;
421 reply->bytesAfter = 0;
422 reply->propertyType = propertyType;
423 reply->format = format;
424 WriteReplyToClient(client, sizeof(xGenericReply), reply);
425 return Success;
428 /*****************
429 * GetProperty
430 * If type Any is specified, returns the property from the specified
431 * window regardless of its type. If a type is specified, returns the
432 * property only if its type equals the specified type.
433 * If delete is True and a property is returned, the property is also
434 * deleted from the window and a PropertyNotify event is generated on the
435 * window.
436 *****************/
439 ProcGetProperty(ClientPtr client)
441 PropertyPtr pProp, prevProp;
442 unsigned long n, len, ind;
443 int rc;
444 WindowPtr pWin;
445 xGetPropertyReply reply;
446 Mask win_mode = DixGetPropAccess, prop_mode = DixReadAccess;
448 REQUEST(xGetPropertyReq);
450 REQUEST_SIZE_MATCH(xGetPropertyReq);
451 if (stuff->delete) {
452 UpdateCurrentTime();
453 win_mode |= DixSetPropAccess;
454 prop_mode |= DixDestroyAccess;
456 rc = dixLookupWindow(&pWin, stuff->window, client, win_mode);
457 if (rc != Success)
458 return rc;
460 if (!ValidAtom(stuff->property)) {
461 client->errorValue = stuff->property;
462 return BadAtom;
464 if ((stuff->delete != xTrue) && (stuff->delete != xFalse)) {
465 client->errorValue = stuff->delete;
466 return BadValue;
468 if ((stuff->type != AnyPropertyType) && !ValidAtom(stuff->type)) {
469 client->errorValue = stuff->type;
470 return BadAtom;
473 memset(&reply, 0, sizeof(xGetPropertyReply));
474 reply.type = X_Reply;
475 reply.sequenceNumber = client->sequence;
477 rc = dixLookupProperty(&pProp, pWin, stuff->property, client, prop_mode);
478 if (rc == BadMatch)
479 return NullPropertyReply(client, None, 0, &reply);
480 else if (rc != Success)
481 return rc;
483 /* If the request type and actual type don't match. Return the
484 property information, but not the data. */
486 if (((stuff->type != pProp->type) && (stuff->type != AnyPropertyType))
488 reply.bytesAfter = pProp->size;
489 reply.format = pProp->format;
490 reply.length = 0;
491 reply.nItems = 0;
492 reply.propertyType = pProp->type;
493 WriteReplyToClient(client, sizeof(xGenericReply), &reply);
494 return Success;
498 * Return type, format, value to client
500 n = (pProp->format / 8) * pProp->size; /* size (bytes) of prop */
501 ind = stuff->longOffset << 2;
503 /* If longOffset is invalid such that it causes "len" to
504 be negative, it's a value error. */
506 if (n < ind) {
507 client->errorValue = stuff->longOffset;
508 return BadValue;
511 len = min(n - ind, 4 * stuff->longLength);
513 reply.bytesAfter = n - (ind + len);
514 reply.format = pProp->format;
515 reply.length = bytes_to_int32(len);
516 reply.nItems = len / (pProp->format / 8);
517 reply.propertyType = pProp->type;
519 if (stuff->delete && (reply.bytesAfter == 0))
520 deliverPropertyNotifyEvent(pWin, PropertyDelete, pProp->propertyName);
522 WriteReplyToClient(client, sizeof(xGenericReply), &reply);
523 if (len) {
524 switch (reply.format) {
525 case 32:
526 client->pSwapReplyFunc = (ReplySwapPtr) CopySwap32Write;
527 break;
528 case 16:
529 client->pSwapReplyFunc = (ReplySwapPtr) CopySwap16Write;
530 break;
531 default:
532 client->pSwapReplyFunc = (ReplySwapPtr) WriteToClient;
533 break;
535 WriteSwappedDataToClient(client, len, (char *) pProp->data + ind);
538 if (stuff->delete && (reply.bytesAfter == 0)) {
539 /* Delete the Property */
540 if (pWin->optional->userProps == pProp) {
541 /* Takes care of head */
542 if (!(pWin->optional->userProps = pProp->next))
543 CheckWindowOptionalNeed(pWin);
545 else {
546 /* Need to traverse to find the previous element */
547 prevProp = pWin->optional->userProps;
548 while (prevProp->next != pProp)
549 prevProp = prevProp->next;
550 prevProp->next = pProp->next;
553 free(pProp->data);
554 dixFreeObjectWithPrivates(pProp, PRIVATE_PROPERTY);
556 return Success;
560 ProcListProperties(ClientPtr client)
562 Atom *pAtoms = NULL, *temppAtoms;
563 xListPropertiesReply xlpr;
564 int rc, numProps = 0;
565 WindowPtr pWin;
566 PropertyPtr pProp, realProp;
568 REQUEST(xResourceReq);
570 REQUEST_SIZE_MATCH(xResourceReq);
571 rc = dixLookupWindow(&pWin, stuff->id, client, DixListPropAccess);
572 if (rc != Success)
573 return rc;
575 for (pProp = wUserProps(pWin); pProp; pProp = pProp->next)
576 numProps++;
578 if (numProps && !(pAtoms = malloc(numProps * sizeof(Atom))))
579 return BadAlloc;
581 numProps = 0;
582 temppAtoms = pAtoms;
583 for (pProp = wUserProps(pWin); pProp; pProp = pProp->next) {
584 realProp = pProp;
585 rc = XaceHookPropertyAccess(client, pWin, &realProp, DixGetAttrAccess);
586 if (rc == Success && realProp == pProp) {
587 *temppAtoms++ = pProp->propertyName;
588 numProps++;
592 xlpr.type = X_Reply;
593 xlpr.nProperties = numProps;
594 xlpr.length = bytes_to_int32(numProps * sizeof(Atom));
595 xlpr.sequenceNumber = client->sequence;
596 WriteReplyToClient(client, sizeof(xGenericReply), &xlpr);
597 if (numProps) {
598 client->pSwapReplyFunc = (ReplySwapPtr) Swap32Write;
599 WriteSwappedDataToClient(client, numProps * sizeof(Atom), pAtoms);
601 free(pAtoms);
602 return Success;
606 ProcDeleteProperty(ClientPtr client)
608 WindowPtr pWin;
610 REQUEST(xDeletePropertyReq);
611 int result;
613 REQUEST_SIZE_MATCH(xDeletePropertyReq);
614 UpdateCurrentTime();
615 result = dixLookupWindow(&pWin, stuff->window, client, DixSetPropAccess);
616 if (result != Success)
617 return result;
618 if (!ValidAtom(stuff->property)) {
619 client->errorValue = stuff->property;
620 return BadAtom;
623 return DeleteProperty(client, pWin, stuff->property);