GetProp.c revision eb411b4b
11ab64890Smrg/*
21ab64890Smrg
31ab64890SmrgCopyright 1986, 1998  The Open Group
41ab64890Smrg
51ab64890SmrgPermission to use, copy, modify, distribute, and sell this software and its
61ab64890Smrgdocumentation for any purpose is hereby granted without fee, provided that
71ab64890Smrgthe above copyright notice appear in all copies and that both that
81ab64890Smrgcopyright notice and this permission notice appear in supporting
91ab64890Smrgdocumentation.
101ab64890Smrg
111ab64890SmrgThe above copyright notice and this permission notice shall be included in
121ab64890Smrgall copies or substantial portions of the Software.
131ab64890Smrg
141ab64890SmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
151ab64890SmrgIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
161ab64890SmrgFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
171ab64890SmrgOPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
181ab64890SmrgAN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
191ab64890SmrgCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
201ab64890Smrg
211ab64890SmrgExcept as contained in this notice, the name of The Open Group shall not be
221ab64890Smrgused in advertising or otherwise to promote the sale, use or other dealings
231ab64890Smrgin this Software without prior written authorization from The Open Group.
241ab64890Smrg
251ab64890Smrg*/
261ab64890Smrg
271ab64890Smrg#ifdef HAVE_CONFIG_H
281ab64890Smrg#include <config.h>
291ab64890Smrg#endif
301ab64890Smrg#include "Xlibint.h"
31eb411b4bSmrg#include <limits.h>
321ab64890Smrg
331ab64890Smrgint
341ab64890SmrgXGetWindowProperty(
351ab64890Smrg    register Display *dpy,
361ab64890Smrg    Window w,
371ab64890Smrg    Atom property,
381ab64890Smrg    long offset,
391ab64890Smrg    long length,
401ab64890Smrg    Bool delete,
411ab64890Smrg    Atom req_type,
421ab64890Smrg    Atom *actual_type,		/* RETURN */
431ab64890Smrg    int *actual_format,  	/* RETURN  8, 16, or 32 */
441ab64890Smrg    unsigned long *nitems, 	/* RETURN  # of 8-, 16-, or 32-bit entities */
451ab64890Smrg    unsigned long *bytesafter,	/* RETURN */
461ab64890Smrg    unsigned char **prop)	/* RETURN */
471ab64890Smrg{
481ab64890Smrg    xGetPropertyReply reply;
491ab64890Smrg    register xGetPropertyReq *req;
5057f47464Smrg    xError error = {0};
511ab64890Smrg
52eb411b4bSmrg    /* Always initialize return values, in case callers fail to initialize
53eb411b4bSmrg       them and fail to check the return code for an error. */
54eb411b4bSmrg    *actual_type = None;
55eb411b4bSmrg    *actual_format = 0;
56eb411b4bSmrg    *nitems = *bytesafter = 0L;
57eb411b4bSmrg    *prop = (unsigned char *) NULL;
58eb411b4bSmrg
591ab64890Smrg    LockDisplay(dpy);
601ab64890Smrg    GetReq (GetProperty, req);
611ab64890Smrg    req->window = w;
621ab64890Smrg    req->property = property;
631ab64890Smrg    req->type = req_type;
641ab64890Smrg    req->delete = delete;
651ab64890Smrg    req->longOffset = offset;
661ab64890Smrg    req->longLength = length;
671ab64890Smrg    error.sequenceNumber = dpy->request;
6861b2299dSmrg
691ab64890Smrg    if (!_XReply (dpy, (xReply *) &reply, 0, xFalse)) {
701ab64890Smrg	UnlockDisplay(dpy);
711ab64890Smrg	SyncHandle();
721ab64890Smrg	return (1);	/* not Success */
7361b2299dSmrg	}
741ab64890Smrg
751ab64890Smrg    if (reply.propertyType != None) {
76eb411b4bSmrg	unsigned long nbytes, netbytes;
77eb411b4bSmrg	int format = reply.format;
78eb411b4bSmrg
79eb411b4bSmrg      /*
80eb411b4bSmrg       * Protect against both integer overflow and just plain oversized
81eb411b4bSmrg       * memory allocation - no server should ever return this many props.
82eb411b4bSmrg       */
83eb411b4bSmrg	if (reply.nItems >= (INT_MAX >> 4))
84eb411b4bSmrg	    format = -1;	/* fall through to default error case */
85eb411b4bSmrg
86eb411b4bSmrg	switch (format) {
8761b2299dSmrg      /*
881ab64890Smrg       * One extra byte is malloced than is needed to contain the property
8961b2299dSmrg       * data, but this last byte is null terminated and convenient for
9061b2299dSmrg       * returning string properties, so the client doesn't then have to
9161b2299dSmrg       * recopy the string to make it null terminated.
921ab64890Smrg       */
931ab64890Smrg	  case 8:
941ab64890Smrg	    nbytes = netbytes = reply.nItems;
95eb411b4bSmrg	    if (nbytes + 1 > 0 && (*prop = Xmalloc (nbytes + 1)))
961ab64890Smrg		_XReadPad (dpy, (char *) *prop, netbytes);
971ab64890Smrg	    break;
981ab64890Smrg
991ab64890Smrg	  case 16:
1001ab64890Smrg	    nbytes = reply.nItems * sizeof (short);
1011ab64890Smrg	    netbytes = reply.nItems << 1;
102eb411b4bSmrg	    if (nbytes + 1 > 0 && (*prop = Xmalloc (nbytes + 1)))
1031ab64890Smrg		_XRead16Pad (dpy, (short *) *prop, netbytes);
1041ab64890Smrg	    break;
1051ab64890Smrg
1061ab64890Smrg	  case 32:
1071ab64890Smrg	    nbytes = reply.nItems * sizeof (long);
1081ab64890Smrg	    netbytes = reply.nItems << 2;
109eb411b4bSmrg	    if (nbytes + 1 > 0 && (*prop = Xmalloc (nbytes + 1)))
1101ab64890Smrg		_XRead32 (dpy, (long *) *prop, netbytes);
1111ab64890Smrg	    break;
1121ab64890Smrg
1131ab64890Smrg	  default:
1141ab64890Smrg	    /*
1151ab64890Smrg	     * This part of the code should never be reached.  If it is,
1161ab64890Smrg	     * the server sent back a property with an invalid format.
11761b2299dSmrg	     * This is a BadImplementation error.
1181ab64890Smrg	     */
1191ab64890Smrg	    {
1201ab64890Smrg		/* sequence number stored above */
1211ab64890Smrg		error.type = X_Error;
1221ab64890Smrg		error.majorCode = X_GetProperty;
1231ab64890Smrg		error.minorCode = 0;
1241ab64890Smrg		error.errorCode = BadImplementation;
1251ab64890Smrg		_XError(dpy, &error);
1261ab64890Smrg	    }
1271ab64890Smrg	    nbytes = netbytes = 0L;
1281ab64890Smrg	    break;
1291ab64890Smrg	}
1301ab64890Smrg	if (! *prop) {
131eb411b4bSmrg	    _XEatDataWords(dpy, reply.length);
1321ab64890Smrg	    UnlockDisplay(dpy);
1331ab64890Smrg	    SyncHandle();
1341ab64890Smrg	    return(BadAlloc);	/* not Success */
1351ab64890Smrg	}
1361ab64890Smrg	(*prop)[nbytes] = '\0';
1371ab64890Smrg    }
1381ab64890Smrg    *actual_type = reply.propertyType;
1391ab64890Smrg    *actual_format = reply.format;
1401ab64890Smrg    *nitems = reply.nItems;
1411ab64890Smrg    *bytesafter = reply.bytesAfter;
1421ab64890Smrg    UnlockDisplay(dpy);
1431ab64890Smrg    SyncHandle();
1441ab64890Smrg    return(Success);
1451ab64890Smrg}
1461ab64890Smrg
147