Write.c revision 44dda7b2
1/*
2Copyright 1989, 1998  The Open Group
3
4Permission to use, copy, modify, distribute, and sell this software and its
5documentation for any purpose is hereby granted without fee, provided that
6the above copyright notice appear in all copies and that both that
7copyright notice and this permission notice appear in supporting
8documentation.
9
10The above copyright notice and this permission notice shall be included in
11all copies or substantial portions of the Software.
12
13THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
16OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
17AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
18CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19
20Except as contained in this notice, the name of The Open Group shall not be
21used in advertising or otherwise to promote the sale, use or other dealings
22in this Software without prior written authorization from The Open Group.
23 *
24 * Author:  Keith Packard, MIT X Consortium
25 */
26
27#ifdef HAVE_CONFIG_H
28#include <config.h>
29#endif
30#include <X11/Xos.h>
31#include <X11/X.h>
32#include <X11/Xmd.h>
33#include <X11/Xdmcp.h>
34#include <stdlib.h>
35
36int
37XdmcpWriteHeader (
38    XdmcpBufferPtr  buffer,
39    const XdmcpHeaderPtr  header)
40{
41    BYTE    *newData;
42
43    if ((int)buffer->size < 6 + (int)header->length)
44    {
45	newData = (BYTE *) malloc(XDM_MAX_MSGLEN * sizeof (BYTE));
46	if (!newData)
47	    return FALSE;
48	free((unsigned long *)(buffer->data));
49	buffer->data = newData;
50	buffer->size = XDM_MAX_MSGLEN;
51    }
52    buffer->pointer = 0;
53    if (!XdmcpWriteCARD16 (buffer, header->version))
54	return FALSE;
55    if (!XdmcpWriteCARD16 (buffer, header->opcode))
56	return FALSE;
57    if (!XdmcpWriteCARD16 (buffer, header->length))
58	return FALSE;
59    return TRUE;
60}
61
62int
63XdmcpWriteARRAY8 (XdmcpBufferPtr buffer, const ARRAY8Ptr array)
64{
65    int	i;
66
67    if (!XdmcpWriteCARD16 (buffer, array->length))
68	return FALSE;
69    for (i = 0; i < (int)array->length; i++)
70	if (!XdmcpWriteCARD8 (buffer, array->data[i]))
71	    return FALSE;
72    return TRUE;
73}
74
75int
76XdmcpWriteARRAY16 (XdmcpBufferPtr buffer, const ARRAY16Ptr array)
77{
78    int	i;
79
80    if (!XdmcpWriteCARD8 (buffer, array->length))
81	return FALSE;
82    for (i = 0; i < (int)array->length; i++)
83	if (!XdmcpWriteCARD16 (buffer, array->data[i]))
84	    return FALSE;
85    return TRUE;
86}
87
88int
89XdmcpWriteARRAY32 (XdmcpBufferPtr buffer, const ARRAY32Ptr array)
90{
91    int	i;
92
93    if (!XdmcpWriteCARD8 (buffer, array->length))
94	return FALSE;
95    for (i = 0; i < (int)array->length; i++)
96	if (!XdmcpWriteCARD32 (buffer, array->data[i]))
97	    return FALSE;
98    return TRUE;
99}
100
101int
102XdmcpWriteARRAYofARRAY8 (XdmcpBufferPtr buffer, ARRAYofARRAY8Ptr array)
103{
104    int	i;
105
106    if (!XdmcpWriteCARD8 (buffer, array->length))
107	return FALSE;
108    for (i = 0; i < (int)array->length; i++)
109	if (!XdmcpWriteARRAY8 (buffer, &array->data[i]))
110	    return FALSE;
111    return TRUE;
112}
113
114int
115XdmcpWriteCARD8 (
116    XdmcpBufferPtr  buffer,
117    unsigned	    value)
118{
119    if (buffer->pointer >= buffer->size)
120	return FALSE;
121    buffer->data[buffer->pointer++] = (BYTE) value;
122    return TRUE;
123}
124
125int
126XdmcpWriteCARD16 (
127    XdmcpBufferPtr  buffer,
128    unsigned	    value)
129{
130    if (!XdmcpWriteCARD8 (buffer, value >> 8))
131	return FALSE;
132    if (!XdmcpWriteCARD8 (buffer, value & 0xff))
133	return FALSE;
134    return TRUE;
135}
136
137int
138XdmcpWriteCARD32 (
139    XdmcpBufferPtr  buffer,
140    unsigned	    value)
141{
142    if (!XdmcpWriteCARD8 (buffer, value >> 24))
143	return FALSE;
144    if (!XdmcpWriteCARD8 (buffer, (value >> 16) & 0xff))
145	return FALSE;
146    if (!XdmcpWriteCARD8 (buffer, (value >> 8) & 0xff))
147	return FALSE;
148    if (!XdmcpWriteCARD8 (buffer, value & 0xff))
149	return FALSE;
150    return TRUE;
151}
152