11ab64890Smrg/************************************************************
21ab64890SmrgCopyright (c) 1993 by Silicon Graphics Computer Systems, Inc.
31ab64890Smrg
41ab64890SmrgPermission to use, copy, modify, and distribute this
51ab64890Smrgsoftware and its documentation for any purpose and without
61ab64890Smrgfee is hereby granted, provided that the above copyright
71ab64890Smrgnotice appear in all copies and that both that copyright
81ab64890Smrgnotice and this permission notice appear in supporting
961b2299dSmrgdocumentation, and that the name of Silicon Graphics not be
1061b2299dSmrgused in advertising or publicity pertaining to distribution
111ab64890Smrgof the software without specific prior written permission.
1261b2299dSmrgSilicon Graphics makes no representation about the suitability
131ab64890Smrgof this software for any purpose. It is provided "as is"
141ab64890Smrgwithout any express or implied warranty.
151ab64890Smrg
1661b2299dSmrgSILICON GRAPHICS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
1761b2299dSmrgSOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
181ab64890SmrgAND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
1961b2299dSmrgGRAPHICS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
2061b2299dSmrgDAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
2161b2299dSmrgDATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
221ab64890SmrgOR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION  WITH
231ab64890SmrgTHE USE OR PERFORMANCE OF THIS SOFTWARE.
241ab64890Smrg
251ab64890Smrg********************************************************/
261ab64890Smrg
271ab64890Smrg#ifdef HAVE_CONFIG_H
281ab64890Smrg#include <config.h>
291ab64890Smrg#endif
301ab64890Smrg#include <stdio.h>
311ab64890Smrg#include "Xlibint.h"
321ab64890Smrg#include "XKBlibint.h"
331ab64890Smrg#include <X11/extensions/XKBproto.h>
341ab64890Smrg
351ab64890Smrg/***====================================================================***/
361ab64890Smrg
3761b2299dSmrgint
38818534a1Smrg_XkbInitReadBuffer(Display *dpy, XkbReadBufferPtr buf, int size)
391ab64890Smrg{
40818534a1Smrg    if ((dpy != NULL) && (buf != NULL) && (size > 0)) {
41818534a1Smrg        buf->error = 0;
42818534a1Smrg        buf->size = size;
43818534a1Smrg        buf->start = buf->data = _XkbAlloc(size);
44818534a1Smrg        if (buf->start) {
45818534a1Smrg            _XRead(dpy, buf->start, size);
46818534a1Smrg            return 1;
47818534a1Smrg        }
481ab64890Smrg    }
491ab64890Smrg    return 0;
501ab64890Smrg}
511ab64890Smrg
521ab64890Smrg#define	_XkbReadBufferDataLeft(b)	(((b)->size)-((b)->data-(b)->start))
531ab64890Smrg
541ab64890Smrgint
55818534a1Smrg_XkbSkipReadBufferData(XkbReadBufferPtr from, int size)
561ab64890Smrg{
57818534a1Smrg    if (size == 0)
58818534a1Smrg        return 1;
59818534a1Smrg    if ((from == NULL) || (from->error) || (size < 1) ||
60818534a1Smrg        (_XkbReadBufferDataLeft(from) < size))
61818534a1Smrg        return 0;
62818534a1Smrg    from->data += size;
631ab64890Smrg    return 1;
641ab64890Smrg}
651ab64890Smrg
661ab64890Smrgint
67818534a1Smrg_XkbCopyFromReadBuffer(XkbReadBufferPtr from, char *to, int size)
681ab64890Smrg{
69818534a1Smrg    if (size == 0)
70818534a1Smrg        return 1;
71818534a1Smrg    if ((from == NULL) || (from->error) || (to == NULL) || (size < 1) ||
72818534a1Smrg        (_XkbReadBufferDataLeft(from) < size))
73818534a1Smrg        return 0;
749c019ec5Smaya    memcpy(to, from->data, (size_t)size);
75818534a1Smrg    from->data += size;
761ab64890Smrg    return 1;
771ab64890Smrg}
781ab64890Smrg
791ab64890Smrg#ifdef XKB_FORCE_INT_KEYSYM
801ab64890Smrgint
81818534a1Smrg_XkbReadCopyKeySyms(int *wire, KeySym * to, int num_words)
821ab64890Smrg{
83818534a1Smrg    while (num_words-- > 0) {
84818534a1Smrg        *to++ = *wire++;
851ab64890Smrg    }
861ab64890Smrg    return 1;
871ab64890Smrg}
881ab64890Smrg
891ab64890Smrgint
90818534a1Smrg_XkbReadBufferCopyKeySyms(XkbReadBufferPtr from, KeySym * to, int num_words)
911ab64890Smrg{
92818534a1Smrg    if ((unsigned) (num_words * 4) > _XkbReadBufferDataLeft(from))
931ab64890Smrg        return 0;
94818534a1Smrg    _XkbReadCopyKeySyms((int *) from->data, to, num_words);
95818534a1Smrg    from->data += (4 * num_words);
961ab64890Smrg    return True;
971ab64890Smrg}
981ab64890Smrg
991ab64890Smrgint
100818534a1Smrg_XkbWriteCopyKeySyms(register KeySym * from, CARD32 *to, int len)
1011ab64890Smrg{
102818534a1Smrg    while (len-- > 0) {
103818534a1Smrg        *to++ = (CARD32) *from++;
1041ab64890Smrg    }
1051ab64890Smrg    return True;
1061ab64890Smrg}
1071ab64890Smrg#endif
1081ab64890Smrg
1091ab64890Smrg#ifdef LONG64
1101ab64890Smrgint
111818534a1Smrg_XkbReadCopyData32(int *wire, long *to, int num_words)
1121ab64890Smrg{
113818534a1Smrg    while (num_words-- > 0) {
114818534a1Smrg        *to++ = *wire++;
1151ab64890Smrg    }
1161ab64890Smrg    return 1;
1171ab64890Smrg}
1181ab64890Smrg
1191ab64890Smrgint
120818534a1Smrg_XkbReadBufferCopy32(XkbReadBufferPtr from, long *to, int num_words)
1211ab64890Smrg{
122818534a1Smrg    if ((unsigned) (num_words * 4) > _XkbReadBufferDataLeft(from))
123818534a1Smrg        return 0;
124818534a1Smrg    _XkbReadCopyData32((int *) from->data, to, num_words);
125818534a1Smrg    from->data += (4 * num_words);
1261ab64890Smrg    return True;
1271ab64890Smrg}
1281ab64890Smrg
1291ab64890Smrgint
130818534a1Smrg_XkbWriteCopyData32(register unsigned long *from, CARD32 *to, int len)
1311ab64890Smrg{
132818534a1Smrg    while (len-- > 0) {
133818534a1Smrg        *to++ = (CARD32) *from++;
1341ab64890Smrg    }
1351ab64890Smrg    return True;
1361ab64890Smrg}
137818534a1Smrg#endif                          /* LONG64 */
1381ab64890Smrg
1391ab64890Smrg
1401ab64890Smrgchar *
141818534a1Smrg_XkbPeekAtReadBuffer(XkbReadBufferPtr from, int size)
1421ab64890Smrg{
143818534a1Smrg    if ((from == NULL) || (from->error) || (size < 1) ||
144818534a1Smrg        (_XkbReadBufferDataLeft(from) < size))
145818534a1Smrg        return NULL;
1461ab64890Smrg    return from->data;
1471ab64890Smrg}
1481ab64890Smrg
1491ab64890Smrgchar *
150818534a1Smrg_XkbGetReadBufferPtr(XkbReadBufferPtr from, int size)
1511ab64890Smrg{
152818534a1Smrg    char *ptr;
153818534a1Smrg
154818534a1Smrg    if ((from == NULL) || (from->error) || (size < 1) ||
155818534a1Smrg        (_XkbReadBufferDataLeft(from) < size))
156818534a1Smrg        return NULL;
157818534a1Smrg    ptr = from->data;
158818534a1Smrg    from->data += size;
1591ab64890Smrg    return ptr;
1601ab64890Smrg}
1611ab64890Smrg
1621ab64890Smrg
1631ab64890Smrgint
1641ab64890Smrg_XkbFreeReadBuffer(XkbReadBufferPtr buf)
1651ab64890Smrg{
166818534a1Smrg    if ((buf != NULL) && (buf->start != NULL)) {
167818534a1Smrg        int left;
168818534a1Smrg
169818534a1Smrg        left = (int) _XkbReadBufferDataLeft(buf);
1700f8248bfSmrg
1710f8248bfSmrg        Xfree(buf->start);
172818534a1Smrg        buf->size = 0;
173818534a1Smrg        buf->start = buf->data = NULL;
174818534a1Smrg        return left;
1751ab64890Smrg    }
1761ab64890Smrg    return 0;
1771ab64890Smrg}
1781ab64890Smrg
1791ab64890SmrgBool
180818534a1Smrg_XkbGetReadBufferCountedString(XkbReadBufferPtr buf, char **rtrn)
1811ab64890Smrg{
182818534a1Smrg    CARD16 len, *pLen;
183818534a1Smrg    int left;
184818534a1Smrg    char *str = NULL;
185818534a1Smrg
186818534a1Smrg    if ((buf == NULL) || (buf->error) ||
187818534a1Smrg        ((left = (int) _XkbReadBufferDataLeft(buf)) < 4))
188818534a1Smrg        return False;
189818534a1Smrg    pLen = (CARD16 *) buf->data;
190818534a1Smrg    len = *pLen;
191818534a1Smrg    if (len > 0) {
192818534a1Smrg        if (XkbPaddedSize(len + 2) > left)
193818534a1Smrg            return False;
194818534a1Smrg        str = _XkbAlloc(len + 1);
195818534a1Smrg        if (str) {
196818534a1Smrg            memcpy(str, &buf->data[2], len);
197818534a1Smrg            str[len] = '\0';
198818534a1Smrg        }
1991ab64890Smrg    }
200818534a1Smrg    buf->data += XkbPaddedSize(len + 2);
201818534a1Smrg    *rtrn = str;
2021ab64890Smrg    return True;
2031ab64890Smrg}
204