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
_XkbInitReadBuffer(Display * dpy,XkbReadBufferPtr buf,int size)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
_XkbSkipReadBufferData(XkbReadBufferPtr from,int size)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
_XkbCopyFromReadBuffer(XkbReadBufferPtr from,char * to,int size)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_t)size);
75     from->data += size;
76     return 1;
77 }
78 
79 #ifdef XKB_FORCE_INT_KEYSYM
80 int
_XkbReadCopyKeySyms(int * wire,KeySym * to,int num_words)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
_XkbReadBufferCopyKeySyms(XkbReadBufferPtr from,KeySym * to,int num_words)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
_XkbWriteCopyKeySyms(register KeySym * from,CARD32 * to,int len)100 _XkbWriteCopyKeySyms(register KeySym * from, CARD32 *to, int len)
101 {
102     while (len-- > 0) {
103         *to++ = (CARD32) *from++;
104     }
105     return True;
106 }
107 #endif
108 
109 #ifdef LONG64
110 int
_XkbReadCopyData32(int * wire,long * to,int num_words)111 _XkbReadCopyData32(int *wire, long *to, int num_words)
112 {
113     while (num_words-- > 0) {
114         *to++ = *wire++;
115     }
116     return 1;
117 }
118 
119 int
_XkbReadBufferCopy32(XkbReadBufferPtr from,long * to,int num_words)120 _XkbReadBufferCopy32(XkbReadBufferPtr from, long *to, int num_words)
121 {
122     if ((unsigned) (num_words * 4) > _XkbReadBufferDataLeft(from))
123         return 0;
124     _XkbReadCopyData32((int *) from->data, to, num_words);
125     from->data += (4 * num_words);
126     return True;
127 }
128 
129 int
_XkbWriteCopyData32(register unsigned long * from,CARD32 * to,int len)130 _XkbWriteCopyData32(register unsigned long *from, CARD32 *to, int len)
131 {
132     while (len-- > 0) {
133         *to++ = (CARD32) *from++;
134     }
135     return True;
136 }
137 #endif                          /* LONG64 */
138 
139 
140 char *
_XkbPeekAtReadBuffer(XkbReadBufferPtr from,int size)141 _XkbPeekAtReadBuffer(XkbReadBufferPtr from, int size)
142 {
143     if ((from == NULL) || (from->error) || (size < 1) ||
144         (_XkbReadBufferDataLeft(from) < size))
145         return NULL;
146     return from->data;
147 }
148 
149 char *
_XkbGetReadBufferPtr(XkbReadBufferPtr from,int size)150 _XkbGetReadBufferPtr(XkbReadBufferPtr from, int size)
151 {
152     char *ptr;
153 
154     if ((from == NULL) || (from->error) || (size < 1) ||
155         (_XkbReadBufferDataLeft(from) < size))
156         return NULL;
157     ptr = from->data;
158     from->data += size;
159     return ptr;
160 }
161 
162 
163 int
_XkbFreeReadBuffer(XkbReadBufferPtr buf)164 _XkbFreeReadBuffer(XkbReadBufferPtr buf)
165 {
166     if ((buf != NULL) && (buf->start != NULL)) {
167         int left;
168 
169         left = (int) _XkbReadBufferDataLeft(buf);
170 
171         Xfree(buf->start);
172         buf->size = 0;
173         buf->start = buf->data = NULL;
174         return left;
175     }
176     return 0;
177 }
178 
179 Bool
_XkbGetReadBufferCountedString(XkbReadBufferPtr buf,char ** rtrn)180 _XkbGetReadBufferCountedString(XkbReadBufferPtr buf, char **rtrn)
181 {
182     CARD16 len, *pLen;
183     int left;
184     char *str = NULL;
185 
186     if ((buf == NULL) || (buf->error) ||
187         ((left = (int) _XkbReadBufferDataLeft(buf)) < 4))
188         return False;
189     pLen = (CARD16 *) buf->data;
190     len = *pLen;
191     if (len > 0) {
192         if (XkbPaddedSize(len + 2) > left)
193             return False;
194         str = _XkbAlloc(len + 1);
195         if (str) {
196             memcpy(str, &buf->data[2], len);
197             str[len] = '\0';
198         }
199     }
200     buf->data += XkbPaddedSize(len + 2);
201     *rtrn = str;
202     return True;
203 }
204