GetProp.c revision eb411b4b
1/* 2 3Copyright 1986, 1998 The Open Group 4 5Permission to use, copy, modify, distribute, and sell this software and its 6documentation for any purpose is hereby granted without fee, provided that 7the above copyright notice appear in all copies and that both that 8copyright notice and this permission notice appear in supporting 9documentation. 10 11The above copyright notice and this permission notice shall be included in 12all copies or substantial portions of the Software. 13 14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 18AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 19CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 20 21Except as contained in this notice, the name of The Open Group shall not be 22used in advertising or otherwise to promote the sale, use or other dealings 23in this Software without prior written authorization from The Open Group. 24 25*/ 26 27#ifdef HAVE_CONFIG_H 28#include <config.h> 29#endif 30#include "Xlibint.h" 31#include <limits.h> 32 33int 34XGetWindowProperty( 35 register Display *dpy, 36 Window w, 37 Atom property, 38 long offset, 39 long length, 40 Bool delete, 41 Atom req_type, 42 Atom *actual_type, /* RETURN */ 43 int *actual_format, /* RETURN 8, 16, or 32 */ 44 unsigned long *nitems, /* RETURN # of 8-, 16-, or 32-bit entities */ 45 unsigned long *bytesafter, /* RETURN */ 46 unsigned char **prop) /* RETURN */ 47{ 48 xGetPropertyReply reply; 49 register xGetPropertyReq *req; 50 xError error = {0}; 51 52 /* Always initialize return values, in case callers fail to initialize 53 them and fail to check the return code for an error. */ 54 *actual_type = None; 55 *actual_format = 0; 56 *nitems = *bytesafter = 0L; 57 *prop = (unsigned char *) NULL; 58 59 LockDisplay(dpy); 60 GetReq (GetProperty, req); 61 req->window = w; 62 req->property = property; 63 req->type = req_type; 64 req->delete = delete; 65 req->longOffset = offset; 66 req->longLength = length; 67 error.sequenceNumber = dpy->request; 68 69 if (!_XReply (dpy, (xReply *) &reply, 0, xFalse)) { 70 UnlockDisplay(dpy); 71 SyncHandle(); 72 return (1); /* not Success */ 73 } 74 75 if (reply.propertyType != None) { 76 unsigned long nbytes, netbytes; 77 int format = reply.format; 78 79 /* 80 * Protect against both integer overflow and just plain oversized 81 * memory allocation - no server should ever return this many props. 82 */ 83 if (reply.nItems >= (INT_MAX >> 4)) 84 format = -1; /* fall through to default error case */ 85 86 switch (format) { 87 /* 88 * One extra byte is malloced than is needed to contain the property 89 * data, but this last byte is null terminated and convenient for 90 * returning string properties, so the client doesn't then have to 91 * recopy the string to make it null terminated. 92 */ 93 case 8: 94 nbytes = netbytes = reply.nItems; 95 if (nbytes + 1 > 0 && (*prop = Xmalloc (nbytes + 1))) 96 _XReadPad (dpy, (char *) *prop, netbytes); 97 break; 98 99 case 16: 100 nbytes = reply.nItems * sizeof (short); 101 netbytes = reply.nItems << 1; 102 if (nbytes + 1 > 0 && (*prop = Xmalloc (nbytes + 1))) 103 _XRead16Pad (dpy, (short *) *prop, netbytes); 104 break; 105 106 case 32: 107 nbytes = reply.nItems * sizeof (long); 108 netbytes = reply.nItems << 2; 109 if (nbytes + 1 > 0 && (*prop = Xmalloc (nbytes + 1))) 110 _XRead32 (dpy, (long *) *prop, netbytes); 111 break; 112 113 default: 114 /* 115 * This part of the code should never be reached. If it is, 116 * the server sent back a property with an invalid format. 117 * This is a BadImplementation error. 118 */ 119 { 120 /* sequence number stored above */ 121 error.type = X_Error; 122 error.majorCode = X_GetProperty; 123 error.minorCode = 0; 124 error.errorCode = BadImplementation; 125 _XError(dpy, &error); 126 } 127 nbytes = netbytes = 0L; 128 break; 129 } 130 if (! *prop) { 131 _XEatDataWords(dpy, reply.length); 132 UnlockDisplay(dpy); 133 SyncHandle(); 134 return(BadAlloc); /* not Success */ 135 } 136 (*prop)[nbytes] = '\0'; 137 } 138 *actual_type = reply.propertyType; 139 *actual_format = reply.format; 140 *nitems = reply.nItems; 141 *bytesafter = reply.bytesAfter; 142 UnlockDisplay(dpy); 143 SyncHandle(); 144 return(Success); 145} 146 147