3023a32edfe00e4691eed025e5daede5a789e3cb
[platform/upstream/libX11.git] / src / xkb / XKBRdBuf.c
1 /************************************************************
2 Copyright (c) 1993 by Silicon Graphics Computer Systems, Inc.
3
4 Permission to use, copy, modify, and distribute this
5 software and its documentation for any purpose and without
6 fee is hereby granted, provided that the above copyright
7 notice appear in all copies and that both that copyright
8 notice and this permission notice appear in supporting
9 documentation, and that the name of Silicon Graphics not be
10 used in advertising or publicity pertaining to distribution
11 of the software without specific prior written permission.
12 Silicon Graphics makes no representation about the suitability
13 of this software for any purpose. It is provided "as is"
14 without any express or implied warranty.
15
16 SILICON GRAPHICS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
17 SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
18 AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
19 GRAPHICS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
20 DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
21 DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
22 OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION  WITH
23 THE 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
37 int
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
54 int
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
66 int
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
80 int
81 _XkbReadCopyKeySyms(int *wire,KeySym *to,int num_words)
82 {
83     while (num_words-->0) {
84         *to++= *wire++;
85     }
86     return 1;
87 }
88
89 int
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
99 int
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
111 int
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
121 int
122 _XkbReadCopyData32(int *from,long *lp,int num_words)
123 {
124 long *lpack;
125 long mask32 = 0x00000000ffffffff;
126 long 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)
143 int
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
155 int
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
170 char *
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
179 char *
180 _XkbGetReadBufferPtr(XkbReadBufferPtr from,int size)
181 {
182 char    *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
192 int
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
207 Bool
208 _XkbGetReadBufferCountedString(XkbReadBufferPtr buf,char **rtrn)
209 {
210 CARD16  len,*pLen;
211 int     left;
212 char *  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 }