XKBRdBuf.c revision eb411b4b
1/************************************************************ 2Copyright (c) 1993 by Silicon Graphics Computer Systems, Inc. 3 4Permission to use, copy, modify, and distribute this 5software and its documentation for any purpose and without 6fee is hereby granted, provided that the above copyright 7notice appear in all copies and that both that copyright 8notice and this permission notice appear in supporting 9documentation, and that the name of Silicon Graphics not be 10used in advertising or publicity pertaining to distribution 11of the software without specific prior written permission. 12Silicon Graphics makes no representation about the suitability 13of this software for any purpose. It is provided "as is" 14without any express or implied warranty. 15 16SILICON GRAPHICS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS 17SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY 18AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON 19GRAPHICS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL 20DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, 21DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE 22OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH 23THE USE OR PERFORMANCE OF THIS SOFTWARE. 24 25********************************************************/ 26 27#ifdef HAVE_CONFIG_H 28#include <config.h> 29#endif 30#include <stdio.h> 31#include "Xlibint.h" 32#include "XKBlibint.h" 33#include <X11/extensions/XKBproto.h> 34 35/***====================================================================***/ 36 37int 38_XkbInitReadBuffer(Display *dpy,XkbReadBufferPtr buf,int size) 39{ 40 if ((dpy!=NULL) && (buf!=NULL) && (size>0)) { 41 buf->error= 0; 42 buf->size= size; 43 buf->start= buf->data= _XkbAlloc(size); 44 if (buf->start) { 45 _XRead(dpy, buf->start, size); 46 return 1; 47 } 48 } 49 return 0; 50} 51 52#define _XkbReadBufferDataLeft(b) (((b)->size)-((b)->data-(b)->start)) 53 54int 55_XkbSkipReadBufferData(XkbReadBufferPtr from,int size) 56{ 57 if (size==0) 58 return 1; 59 if ((from==NULL)||(from->error)||(size<1)|| 60 (_XkbReadBufferDataLeft(from)<size)) 61 return 0; 62 from->data+= size; 63 return 1; 64} 65 66int 67_XkbCopyFromReadBuffer(XkbReadBufferPtr from,char *to,int size) 68{ 69 if (size==0) 70 return 1; 71 if ((from==NULL)||(from->error)||(to==NULL)||(size<1)|| 72 (_XkbReadBufferDataLeft(from)<size)) 73 return 0; 74 memcpy(to,from->data,size); 75 from->data+= size; 76 return 1; 77} 78 79#ifdef XKB_FORCE_INT_KEYSYM 80int 81_XkbReadCopyKeySyms(int *wire,KeySym *to,int num_words) 82{ 83 while (num_words-->0) { 84 *to++= *wire++; 85 } 86 return 1; 87} 88 89int 90_XkbReadBufferCopyKeySyms(XkbReadBufferPtr from,KeySym *to,int num_words) 91{ 92 if ((unsigned)(num_words*4)>_XkbReadBufferDataLeft(from)) 93 return 0; 94 _XkbReadCopyKeySyms((int *)from->data,to,num_words); 95 from->data+= (4*num_words); 96 return True; 97} 98 99int 100_XkbWriteCopyKeySyms (register KeySym *from,CARD32 *to,int len) 101{ 102 103 while (len-->0) { 104 *to++= (CARD32)*from++; 105 } 106 return True; 107} 108#endif 109 110#ifdef LONG64 111int 112_XkbReadCopyData32(int *wire,long *to,int num_words) 113{ 114 while (num_words-->0) { 115 *to++= *wire++; 116 } 117 return 1; 118} 119 120int 121_XkbReadBufferCopy32(XkbReadBufferPtr from,long *to,int num_words) 122{ 123 if ((unsigned)(num_words*4)>_XkbReadBufferDataLeft(from)) 124 return 0; 125 _XkbReadCopyData32((int *)from->data,to,num_words); 126 from->data+= (4*num_words); 127 return True; 128} 129 130int 131_XkbWriteCopyData32 (register unsigned long *from,CARD32 *to,int len) 132{ 133 134 while (len-->0) { 135 *to++= (CARD32)*from++; 136 } 137 return True; 138} 139#endif /* LONG64 */ 140 141 142char * 143_XkbPeekAtReadBuffer(XkbReadBufferPtr from,int size) 144{ 145 if ((from==NULL)||(from->error)||(size<1)|| 146 (_XkbReadBufferDataLeft(from)<size)) 147 return NULL; 148 return from->data; 149} 150 151char * 152_XkbGetReadBufferPtr(XkbReadBufferPtr from,int size) 153{ 154char *ptr; 155 if ((from==NULL)||(from->error)||(size<1)|| 156 (_XkbReadBufferDataLeft(from)<size)) 157 return NULL; 158 ptr= from->data; 159 from->data+= size; 160 return ptr; 161} 162 163 164int 165_XkbFreeReadBuffer(XkbReadBufferPtr buf) 166{ 167 if ((buf!=NULL) && (buf->start!=NULL)) { 168 int left; 169 left= (int)_XkbReadBufferDataLeft(buf); 170 if (buf->start!=NULL) 171 Xfree(buf->start); 172 buf->size= 0; 173 buf->start= buf->data= NULL; 174 return left; 175 } 176 return 0; 177} 178 179Bool 180_XkbGetReadBufferCountedString(XkbReadBufferPtr buf,char **rtrn) 181{ 182CARD16 len,*pLen; 183int left; 184char * str = NULL; 185 186 if ((buf==NULL)||(buf->error)||((left=(int)_XkbReadBufferDataLeft(buf))<4)) 187 return False; 188 pLen= (CARD16 *)buf->data; 189 len= *pLen; 190 if (len>0) { 191 if (XkbPaddedSize(len+2)>left) 192 return False; 193 str= _XkbAlloc(len+1); 194 if (str) { 195 memcpy(str,&buf->data[2],len); 196 str[len]= '\0'; 197 } 198 } 199 buf->data+= XkbPaddedSize(len+2); 200 *rtrn= str; 201 return True; 202} 203