XKBRdBuf.c revision b4ee4795
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#define NEED_REPLIES
32#define NEED_EVENTS
33#include "Xlibint.h"
34#include "XKBlibint.h"
35#include <X11/extensions/XKBproto.h>
36
37/***====================================================================***/
38
39int
40_XkbInitReadBuffer(Display *dpy,XkbReadBufferPtr buf,int size)
41{
42    if ((dpy!=NULL) && (buf!=NULL) && (size>0)) {
43	buf->error=  0;
44	buf->size=   size;
45	buf->start= buf->data= _XkbAlloc(size);
46	if (buf->start) {
47	    _XRead(dpy, buf->start, size);
48	    return 1;
49	}
50    }
51    return 0;
52}
53
54#define	_XkbReadBufferDataLeft(b)	(((b)->size)-((b)->data-(b)->start))
55
56int
57_XkbSkipReadBufferData(XkbReadBufferPtr	from,int size)
58{
59    if (size==0)
60	return 1;
61    if ((from==NULL)||(from->error)||(size<1)||
62					(_XkbReadBufferDataLeft(from)<size))
63	return 0;
64    from->data+= size;
65    return 1;
66}
67
68int
69_XkbCopyFromReadBuffer(XkbReadBufferPtr	from,char *to,int size)
70{
71    if (size==0)
72	return 1;
73    if ((from==NULL)||(from->error)||(to==NULL)||(size<1)||
74					(_XkbReadBufferDataLeft(from)<size))
75	return 0;
76    memcpy(to,from->data,size);
77    from->data+= size;
78    return 1;
79}
80
81#ifdef XKB_FORCE_INT_KEYSYM
82int
83_XkbReadCopyKeySyms(int *wire,KeySym *to,int num_words)
84{
85    while (num_words-->0) {
86	*to++= *wire++;
87    }
88    return 1;
89}
90
91int
92_XkbReadBufferCopyKeySyms(XkbReadBufferPtr from,KeySym *to,int num_words)
93{
94    if ((unsigned)(num_words*4)>_XkbReadBufferDataLeft(from))
95        return 0;
96    _XkbReadCopyKeySyms((int *)from->data,to,num_words);
97    from->data+= (4*num_words);
98    return True;
99}
100
101int
102_XkbWriteCopyKeySyms (register KeySym *from,CARD32 *to,int len)
103{
104
105    while (len-->0) {
106        *to++= (CARD32)*from++;
107    }
108    return True;
109}
110#endif
111
112#ifdef LONG64
113int
114_XkbReadCopyData32(int *wire,long *to,int num_words)
115{
116    while (num_words-->0) {
117	*to++= *wire++;
118    }
119    return 1;
120}
121#endif
122#ifdef WORD64
123int
124_XkbReadCopyData32(int *from,long *lp,int num_words)
125{
126long *lpack;
127long mask32 = 0x00000000ffffffff;
128long maskw, i, bits;
129
130    lpack = (long *)from;
131    bits = 32;
132
133    for (i=0;i<num_words;i++) {
134	maskw = mask32 << bits;
135	*lp++ = (*lpack & maskw) >> bits;
136	bits = bits ^ 32;
137	if (bits)
138	    lpack++;
139    }
140    return 1;
141}
142#endif
143
144#if defined(LONG64) || defined(WORD64)
145int
146_XkbReadBufferCopy32(XkbReadBufferPtr from,long *to,int num_words)
147{
148    if ((unsigned)(num_words*4)>_XkbReadBufferDataLeft(from))
149	return 0;
150    _XkbReadCopyData32((int *)from->data,to,num_words);
151    from->data+= (4*num_words);
152    return True;
153}
154#endif
155
156#ifdef LONG64
157int
158_XkbWriteCopyData32 (register unsigned long *from,CARD32 *to,int len)
159{
160
161    while (len-->0) {
162	*to++= (CARD32)*from++;
163    }
164    return True;
165}
166#endif /* LONG64 */
167
168#ifdef WORD64
169_XkbWriteCopyData32 Not Implemented Yet for sizeof(int)==8
170#endif
171
172char *
173_XkbPeekAtReadBuffer(XkbReadBufferPtr from,int size)
174{
175    if ((from==NULL)||(from->error)||(size<1)||
176					(_XkbReadBufferDataLeft(from)<size))
177	return NULL;
178    return from->data;
179}
180
181char *
182_XkbGetReadBufferPtr(XkbReadBufferPtr from,int size)
183{
184char	*ptr;
185    if ((from==NULL)||(from->error)||(size<1)||
186					(_XkbReadBufferDataLeft(from)<size))
187	return NULL;
188    ptr= from->data;
189    from->data+= size;
190    return ptr;
191}
192
193
194int
195_XkbFreeReadBuffer(XkbReadBufferPtr buf)
196{
197    if ((buf!=NULL) && (buf->start!=NULL)) {
198	int left;
199	left= (int)_XkbReadBufferDataLeft(buf);
200	if (buf->start!=NULL)
201	    Xfree(buf->start);
202	buf->size= 0;
203	buf->start= buf->data= NULL;
204	return left;
205    }
206    return 0;
207}
208
209Bool
210_XkbGetReadBufferCountedString(XkbReadBufferPtr buf,char **rtrn)
211{
212CARD16	len,*pLen;
213int	left;
214char *	str = NULL;
215
216    if ((buf==NULL)||(buf->error)||((left=(int)_XkbReadBufferDataLeft(buf))<4))
217	return False;
218    pLen= (CARD16 *)buf->data;
219    len= *pLen;
220    if (len>0) {
221	if (XkbPaddedSize(len+2)>left)
222	    return False;
223	str= _XkbAlloc(len+1);
224	if (str) {
225	    memcpy(str,&buf->data[2],len);
226	    str[len]= '\0';
227	}
228    }
229    buf->data+= XkbPaddedSize(len+2);
230    *rtrn= str;
231    return True;
232}
233