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 281ab64890Smrg#ifdef HAVE_CONFIG_H 291ab64890Smrg#include <config.h> 301ab64890Smrg#endif 311ab64890Smrg#include "Xlibint.h" 32f2d49d05Smrg#include "reallocarray.h" 33eb411b4bSmrg#include <limits.h> 341ab64890Smrg 351ab64890Smrgchar ** 361ab64890SmrgXListFonts( 371ab64890Smrgregister Display *dpy, 381ab64890Smrg_Xconst char *pattern, /* null-terminated */ 391ab64890Smrgint maxNames, 401ab64890Smrgint *actualCount) /* RETURN */ 4161b2299dSmrg{ 421ab64890Smrg register long nbytes; 431ab64890Smrg register unsigned i; 441ab64890Smrg register int length; 45eb411b4bSmrg char **flist = NULL; 46eb411b4bSmrg char *ch = NULL; 47c9e7fb8dSmrg char *chstart; 48eb411b4bSmrg char *chend; 49eb411b4bSmrg int count = 0; 501ab64890Smrg xListFontsReply rep; 511ab64890Smrg register xListFontsReq *req; 52dac667f7Smrg unsigned long rlen = 0; 531ab64890Smrg 54e4e01221Smrg if (pattern != NULL && strlen(pattern) >= USHRT_MAX) 555c4cfbd9Smrg return NULL; 565c4cfbd9Smrg 571ab64890Smrg LockDisplay(dpy); 581ab64890Smrg GetReq(ListFonts, req); 591ab64890Smrg req->maxNames = maxNames; 60e90f245cSmaya nbytes = req->nbytes = pattern ? (CARD16) strlen (pattern) : 0; 611ab64890Smrg req->length += (nbytes + 3) >> 2; 621ab64890Smrg _XSend (dpy, pattern, nbytes); 631ab64890Smrg /* use _XSend instead of Data, since following _XReply will flush buffer */ 641ab64890Smrg 651ab64890Smrg if (!_XReply (dpy, (xReply *)&rep, 0, xFalse)) { 661ab64890Smrg *actualCount = 0; 671ab64890Smrg UnlockDisplay(dpy); 681ab64890Smrg SyncHandle(); 691ab64890Smrg return (char **) NULL; 701ab64890Smrg } 711ab64890Smrg 721ab64890Smrg if (rep.nFonts) { 73f2d49d05Smrg flist = Xmallocarray (rep.nFonts, sizeof(char *)); 74dac667f7Smrg if (rep.length > 0 && rep.length < (INT_MAX >> 2)) { 75eb411b4bSmrg rlen = rep.length << 2; 76eb411b4bSmrg ch = Xmalloc(rlen + 1); 771ab64890Smrg /* +1 to leave room for last null-terminator */ 78eb411b4bSmrg } 791ab64890Smrg 801ab64890Smrg if ((! flist) || (! ch)) { 81cf2acddeSmrg Xfree(flist); 82cf2acddeSmrg Xfree(ch); 83eb411b4bSmrg _XEatDataWords(dpy, rep.length); 841ab64890Smrg *actualCount = 0; 851ab64890Smrg UnlockDisplay(dpy); 861ab64890Smrg SyncHandle(); 871ab64890Smrg return (char **) NULL; 881ab64890Smrg } 891ab64890Smrg 901ab64890Smrg _XReadPad (dpy, ch, rlen); 911ab64890Smrg /* 921ab64890Smrg * unpack into null terminated strings. 931ab64890Smrg */ 94c9e7fb8dSmrg chstart = ch; 95bd8e7fb0Smrg chend = ch + rlen; 961ab64890Smrg length = *(unsigned char *)ch; 971ab64890Smrg *ch = 1; /* make sure it is non-zero for XFreeFontNames */ 981ab64890Smrg for (i = 0; i < rep.nFonts; i++) { 99eb411b4bSmrg if (ch + length < chend) { 100eb411b4bSmrg flist[i] = ch + 1; /* skip over length */ 101eb411b4bSmrg ch += length + 1; /* find next length ... */ 102bd8e7fb0Smrg length = *(unsigned char *)ch; 103bd8e7fb0Smrg *ch = '\0'; /* and replace with null-termination */ 104bd8e7fb0Smrg count++; 105dac667f7Smrg } else { 106c9e7fb8dSmrg Xfree(chstart); 107dac667f7Smrg Xfree(flist); 108dac667f7Smrg flist = NULL; 109dac667f7Smrg count = 0; 110dac667f7Smrg break; 111dac667f7Smrg } 1121ab64890Smrg } 1131ab64890Smrg } 114eb411b4bSmrg *actualCount = count; 1151ab64890Smrg UnlockDisplay(dpy); 1161ab64890Smrg SyncHandle(); 1171ab64890Smrg return (flist); 1181ab64890Smrg} 1191ab64890Smrg 1201ab64890Smrgint 1211ab64890SmrgXFreeFontNames(char **list) 12261b2299dSmrg{ 1231ab64890Smrg if (list) { 1241ab64890Smrg if (!*(list[0]-1)) { /* from ListFontsWithInfo */ 1251ab64890Smrg register char **names; 1261ab64890Smrg for (names = list+1; *names; names++) 1271ab64890Smrg Xfree (*names); 1281ab64890Smrg } 1291ab64890Smrg Xfree (list[0]-1); 130c555af55Smrg Xfree (list); 1311ab64890Smrg } 1321ab64890Smrg return 1; 1331ab64890Smrg} 134