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