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 "reallocarray.h" 32#include <limits.h> 33 34char **XListExtensions( 35 register Display *dpy, 36 int *nextensions) /* RETURN */ 37{ 38 xListExtensionsReply rep; 39 char **list = NULL; 40 char *ch = NULL; 41 char *chend; 42 int count = 0; 43 register unsigned i; 44 register int length; 45 _X_UNUSED register xReq *req; 46 unsigned long rlen = 0; 47 48 LockDisplay(dpy); 49 GetEmptyReq (ListExtensions, req); 50 51 if (! _XReply (dpy, (xReply *) &rep, 0, xFalse)) { 52 UnlockDisplay(dpy); 53 SyncHandle(); 54 return (char **) NULL; 55 } 56 57 if (rep.nExtensions) { 58 list = Xmallocarray (rep.nExtensions, sizeof (char *)); 59 if (rep.length > 0 && rep.length < (INT_MAX >> 2)) { 60 rlen = rep.length << 2; 61 ch = Xmalloc (rlen + 1); 62 /* +1 to leave room for last null-terminator */ 63 } 64 65 if ((!list) || (!ch)) { 66 Xfree(list); 67 Xfree(ch); 68 _XEatDataWords(dpy, rep.length); 69 UnlockDisplay(dpy); 70 SyncHandle(); 71 return (char **) NULL; 72 } 73 74 _XReadPad (dpy, ch, rlen); 75 /* 76 * unpack into null terminated strings. 77 */ 78 chend = ch + rlen; 79 length = *(unsigned char *)ch; 80 for (i = 0; i < rep.nExtensions; i++) { 81 if (ch + length < chend) { 82 list[i] = ch+1; /* skip over length */ 83 ch += length + 1; /* find next length ... */ 84 length = *(unsigned char *)ch; 85 *ch = '\0'; /* and replace with null-termination */ 86 count++; 87 } else if (i == 0) { 88 Xfree(list); 89 Xfree(ch); 90 list = NULL; 91 break; 92 } else 93 list[i] = NULL; 94 } 95 } 96 97 *nextensions = count; 98 UnlockDisplay(dpy); 99 SyncHandle(); 100 return (list); 101} 102 103int 104XFreeExtensionList (char **list) 105{ 106 if (list != NULL) { 107 Xfree (list[0]-1); 108 Xfree (list); 109 } 110 return 1; 111} 112