XKBRdBuf.c revision 9c019ec5
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_t)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    while (len-- > 0) {
103        *to++ = (CARD32) *from++;
104    }
105    return True;
106}
107#endif
108
109#ifdef LONG64
110int
111_XkbReadCopyData32(int *wire, long *to, int num_words)
112{
113    while (num_words-- > 0) {
114        *to++ = *wire++;
115    }
116    return 1;
117}
118
119int
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
129int
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
140char *
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
149char *
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
163int
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
179Bool
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