144dda7b2Smrg/*
244dda7b2SmrgCopyright 1989, 1998  The Open Group
344dda7b2Smrg
444dda7b2SmrgPermission to use, copy, modify, distribute, and sell this software and its
544dda7b2Smrgdocumentation for any purpose is hereby granted without fee, provided that
644dda7b2Smrgthe above copyright notice appear in all copies and that both that
744dda7b2Smrgcopyright notice and this permission notice appear in supporting
844dda7b2Smrgdocumentation.
944dda7b2Smrg
1044dda7b2SmrgThe above copyright notice and this permission notice shall be included in
1144dda7b2Smrgall copies or substantial portions of the Software.
1244dda7b2Smrg
1344dda7b2SmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1444dda7b2SmrgIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1544dda7b2SmrgFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
1644dda7b2SmrgOPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
1744dda7b2SmrgAN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
1844dda7b2SmrgCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1944dda7b2Smrg
2044dda7b2SmrgExcept as contained in this notice, the name of The Open Group shall not be
2144dda7b2Smrgused in advertising or otherwise to promote the sale, use or other dealings
2244dda7b2Smrgin this Software without prior written authorization from The Open Group.
2344dda7b2Smrg *
2444dda7b2Smrg * Author:  Keith Packard, MIT X Consortium
2544dda7b2Smrg */
2644dda7b2Smrg
2744dda7b2Smrg#ifdef HAVE_CONFIG_H
2844dda7b2Smrg#include <config.h>
2944dda7b2Smrg#endif
3044dda7b2Smrg#include <X11/Xos.h>
3144dda7b2Smrg#include <X11/X.h>
3244dda7b2Smrg#include <X11/Xmd.h>
3344dda7b2Smrg#include <X11/Xdmcp.h>
3444dda7b2Smrg#include <stdlib.h>
3544dda7b2Smrg
3644dda7b2Smrgint
3744dda7b2SmrgXdmcpWriteHeader (
3844dda7b2Smrg    XdmcpBufferPtr  buffer,
3944dda7b2Smrg    const XdmcpHeaderPtr  header)
4044dda7b2Smrg{
4144dda7b2Smrg    BYTE    *newData;
4244dda7b2Smrg
4344dda7b2Smrg    if ((int)buffer->size < 6 + (int)header->length)
4444dda7b2Smrg    {
45e08b657dSmrg	newData = calloc(XDM_MAX_MSGLEN, sizeof (BYTE));
4644dda7b2Smrg	if (!newData)
4744dda7b2Smrg	    return FALSE;
48e08b657dSmrg	free(buffer->data);
4944dda7b2Smrg	buffer->data = newData;
5044dda7b2Smrg	buffer->size = XDM_MAX_MSGLEN;
5144dda7b2Smrg    }
5244dda7b2Smrg    buffer->pointer = 0;
5344dda7b2Smrg    if (!XdmcpWriteCARD16 (buffer, header->version))
5444dda7b2Smrg	return FALSE;
5544dda7b2Smrg    if (!XdmcpWriteCARD16 (buffer, header->opcode))
5644dda7b2Smrg	return FALSE;
5744dda7b2Smrg    if (!XdmcpWriteCARD16 (buffer, header->length))
5844dda7b2Smrg	return FALSE;
5944dda7b2Smrg    return TRUE;
6044dda7b2Smrg}
6144dda7b2Smrg
6244dda7b2Smrgint
6344dda7b2SmrgXdmcpWriteARRAY8 (XdmcpBufferPtr buffer, const ARRAY8Ptr array)
6444dda7b2Smrg{
6544dda7b2Smrg    int	i;
6644dda7b2Smrg
6744dda7b2Smrg    if (!XdmcpWriteCARD16 (buffer, array->length))
6844dda7b2Smrg	return FALSE;
6944dda7b2Smrg    for (i = 0; i < (int)array->length; i++)
7044dda7b2Smrg	if (!XdmcpWriteCARD8 (buffer, array->data[i]))
7144dda7b2Smrg	    return FALSE;
7244dda7b2Smrg    return TRUE;
7344dda7b2Smrg}
7444dda7b2Smrg
7544dda7b2Smrgint
7644dda7b2SmrgXdmcpWriteARRAY16 (XdmcpBufferPtr buffer, const ARRAY16Ptr array)
7744dda7b2Smrg{
7844dda7b2Smrg    int	i;
7944dda7b2Smrg
8044dda7b2Smrg    if (!XdmcpWriteCARD8 (buffer, array->length))
8144dda7b2Smrg	return FALSE;
8244dda7b2Smrg    for (i = 0; i < (int)array->length; i++)
8344dda7b2Smrg	if (!XdmcpWriteCARD16 (buffer, array->data[i]))
8444dda7b2Smrg	    return FALSE;
8544dda7b2Smrg    return TRUE;
8644dda7b2Smrg}
8744dda7b2Smrg
8844dda7b2Smrgint
8944dda7b2SmrgXdmcpWriteARRAY32 (XdmcpBufferPtr buffer, const ARRAY32Ptr array)
9044dda7b2Smrg{
9144dda7b2Smrg    int	i;
9244dda7b2Smrg
9344dda7b2Smrg    if (!XdmcpWriteCARD8 (buffer, array->length))
9444dda7b2Smrg	return FALSE;
9544dda7b2Smrg    for (i = 0; i < (int)array->length; i++)
9644dda7b2Smrg	if (!XdmcpWriteCARD32 (buffer, array->data[i]))
9744dda7b2Smrg	    return FALSE;
9844dda7b2Smrg    return TRUE;
9944dda7b2Smrg}
10044dda7b2Smrg
10144dda7b2Smrgint
10244dda7b2SmrgXdmcpWriteARRAYofARRAY8 (XdmcpBufferPtr buffer, ARRAYofARRAY8Ptr array)
10344dda7b2Smrg{
10444dda7b2Smrg    int	i;
10544dda7b2Smrg
10644dda7b2Smrg    if (!XdmcpWriteCARD8 (buffer, array->length))
10744dda7b2Smrg	return FALSE;
10844dda7b2Smrg    for (i = 0; i < (int)array->length; i++)
10944dda7b2Smrg	if (!XdmcpWriteARRAY8 (buffer, &array->data[i]))
11044dda7b2Smrg	    return FALSE;
11144dda7b2Smrg    return TRUE;
11244dda7b2Smrg}
11344dda7b2Smrg
11444dda7b2Smrgint
11544dda7b2SmrgXdmcpWriteCARD8 (
11644dda7b2Smrg    XdmcpBufferPtr  buffer,
11744dda7b2Smrg    unsigned	    value)
11844dda7b2Smrg{
11944dda7b2Smrg    if (buffer->pointer >= buffer->size)
12044dda7b2Smrg	return FALSE;
12144dda7b2Smrg    buffer->data[buffer->pointer++] = (BYTE) value;
12244dda7b2Smrg    return TRUE;
12344dda7b2Smrg}
12444dda7b2Smrg
12544dda7b2Smrgint
12644dda7b2SmrgXdmcpWriteCARD16 (
12744dda7b2Smrg    XdmcpBufferPtr  buffer,
12844dda7b2Smrg    unsigned	    value)
12944dda7b2Smrg{
13044dda7b2Smrg    if (!XdmcpWriteCARD8 (buffer, value >> 8))
13144dda7b2Smrg	return FALSE;
13244dda7b2Smrg    if (!XdmcpWriteCARD8 (buffer, value & 0xff))
13344dda7b2Smrg	return FALSE;
13444dda7b2Smrg    return TRUE;
13544dda7b2Smrg}
13644dda7b2Smrg
13744dda7b2Smrgint
13844dda7b2SmrgXdmcpWriteCARD32 (
13944dda7b2Smrg    XdmcpBufferPtr  buffer,
14044dda7b2Smrg    unsigned	    value)
14144dda7b2Smrg{
14244dda7b2Smrg    if (!XdmcpWriteCARD8 (buffer, value >> 24))
14344dda7b2Smrg	return FALSE;
14444dda7b2Smrg    if (!XdmcpWriteCARD8 (buffer, (value >> 16) & 0xff))
14544dda7b2Smrg	return FALSE;
14644dda7b2Smrg    if (!XdmcpWriteCARD8 (buffer, (value >> 8) & 0xff))
14744dda7b2Smrg	return FALSE;
14844dda7b2Smrg    if (!XdmcpWriteCARD8 (buffer, value & 0xff))
14944dda7b2Smrg	return FALSE;
15044dda7b2Smrg    return TRUE;
15144dda7b2Smrg}
152