XKBRdBuf.c revision e9fcaa8a
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#endif
120#ifdef WORD64
121int
122_XkbReadCopyData32(int *from,long *lp,int num_words)
123{
124long *lpack;
125long mask32 = 0x00000000ffffffff;
126long maskw, i, bits;
127
128    lpack = (long *)from;
129    bits = 32;
130
131    for (i=0;i<num_words;i++) {
132	maskw = mask32 << bits;
133	*lp++ = (*lpack & maskw) >> bits;
134	bits = bits ^ 32;
135	if (bits)
136	    lpack++;
137    }
138    return 1;
139}
140#endif
141
142#if defined(LONG64) || defined(WORD64)
143int
144_XkbReadBufferCopy32(XkbReadBufferPtr from,long *to,int num_words)
145{
146    if ((unsigned)(num_words*4)>_XkbReadBufferDataLeft(from))
147	return 0;
148    _XkbReadCopyData32((int *)from->data,to,num_words);
149    from->data+= (4*num_words);
150    return True;
151}
152#endif
153
154#ifdef LONG64
155int
156_XkbWriteCopyData32 (register unsigned long *from,CARD32 *to,int len)
157{
158
159    while (len-->0) {
160	*to++= (CARD32)*from++;
161    }
162    return True;
163}
164#endif /* LONG64 */
165
166#ifdef WORD64
167_XkbWriteCopyData32 Not Implemented Yet for sizeof(int)==8
168#endif
169
170char *
171_XkbPeekAtReadBuffer(XkbReadBufferPtr from,int size)
172{
173    if ((from==NULL)||(from->error)||(size<1)||
174					(_XkbReadBufferDataLeft(from)<size))
175	return NULL;
176    return from->data;
177}
178
179char *
180_XkbGetReadBufferPtr(XkbReadBufferPtr from,int size)
181{
182char	*ptr;
183    if ((from==NULL)||(from->error)||(size<1)||
184					(_XkbReadBufferDataLeft(from)<size))
185	return NULL;
186    ptr= from->data;
187    from->data+= size;
188    return ptr;
189}
190
191
192int
193_XkbFreeReadBuffer(XkbReadBufferPtr buf)
194{
195    if ((buf!=NULL) && (buf->start!=NULL)) {
196	int left;
197	left= (int)_XkbReadBufferDataLeft(buf);
198	if (buf->start!=NULL)
199	    Xfree(buf->start);
200	buf->size= 0;
201	buf->start= buf->data= NULL;
202	return left;
203    }
204    return 0;
205}
206
207Bool
208_XkbGetReadBufferCountedString(XkbReadBufferPtr buf,char **rtrn)
209{
210CARD16	len,*pLen;
211int	left;
212char *	str = NULL;
213
214    if ((buf==NULL)||(buf->error)||((left=(int)_XkbReadBufferDataLeft(buf))<4))
215	return False;
216    pLen= (CARD16 *)buf->data;
217    len= *pLen;
218    if (len>0) {
219	if (XkbPaddedSize(len+2)>left)
220	    return False;
221	str= _XkbAlloc(len+1);
222	if (str) {
223	    memcpy(str,&buf->data[2],len);
224	    str[len]= '\0';
225	}
226    }
227    buf->data+= XkbPaddedSize(len+2);
228    *rtrn= str;
229    return True;
230}
231