2 Copyright 1989, 1998 The Open Group
4 Permission to use, copy, modify, distribute, and sell this software and its
5 documentation for any purpose is hereby granted without fee, provided that
6 the above copyright notice appear in all copies and that both that
7 copyright notice and this permission notice appear in supporting
10 The above copyright notice and this permission notice shall be included in
11 all copies or substantial portions of the Software.
13 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
17 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
18 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 Except as contained in this notice, the name of The Open Group shall not be
21 used in advertising or otherwise to promote the sale, use or other dealings
22 in this Software without prior written authorization from The Open Group.
24 * Author: Keith Packard, MIT X Consortium
33 #include <X11/Xdmcp.h>
37 XdmcpReadHeader (XdmcpBufferPtr buffer, XdmcpHeaderPtr header)
39 if (XdmcpReadCARD16 (buffer, &header->version) &&
40 XdmcpReadCARD16 (buffer, &header->opcode) &&
41 XdmcpReadCARD16 (buffer, &header->length))
47 XdmcpReadRemaining (const XdmcpBufferPtr buffer)
49 return buffer->count - buffer->pointer;
53 XdmcpReadARRAY8 (XdmcpBufferPtr buffer, ARRAY8Ptr array)
58 * When returning FALSE, guarantee that array->data = 0.
59 * This allows the user to safely call XdmcpDisposeARRAY8(array)
60 * regardless of the return value below.
61 * Note that XdmcpDisposeARRAY*(array) will call free(array->data),
62 * so we must guarantee that array->data is NULL or a malloced pointer.
64 if (!XdmcpReadCARD16 (buffer, &array->length)) {
73 array->data = (CARD8 *) malloc(array->length * sizeof (CARD8));
76 for (i = 0; i < (int)array->length; i++)
78 if (!XdmcpReadCARD8 (buffer, &array->data[i]))
90 XdmcpReadARRAY16 (XdmcpBufferPtr buffer, ARRAY16Ptr array)
95 * When returning FALSE, guarantee that array->data = 0.
96 * This allows the user to safely call XdmcpDisposeARRAY16(array)
97 * regardless of the return value below.
98 * Note that XdmcpDisposeARRAY*(array) will call free(array->data),
99 * so we must guarantee that array->data is NULL or a malloced pointer.
101 if (!XdmcpReadCARD8 (buffer, &array->length)) {
110 array->data = (CARD16 *) malloc(array->length * sizeof (CARD16));
113 for (i = 0; i < (int)array->length; i++)
115 if (!XdmcpReadCARD16 (buffer, &array->data[i]))
127 XdmcpReadARRAY32 (XdmcpBufferPtr buffer, ARRAY32Ptr array)
132 * When returning FALSE, guarantee that array->data = 0.
133 * This allows the user to safely call XdmcpDisposeARRAY32(array)
134 * regardless of the return value below.
135 * Note that XdmcpDisposeARRAY*(array) will call free(array->data),
136 * so we must guarantee that array->data is NULL or a malloced pointer.
138 if (!XdmcpReadCARD8 (buffer, &array->length)) {
147 array->data = (CARD32 *) malloc(array->length * sizeof (CARD32));
150 for (i = 0; i < (int)array->length; i++)
152 if (!XdmcpReadCARD32 (buffer, &array->data[i]))
164 XdmcpReadARRAYofARRAY8 (XdmcpBufferPtr buffer, ARRAYofARRAY8Ptr array)
169 * When returning FALSE, guarantee that array->data = 0.
170 * This allows the user to safely call XdmcpDisposeARRAYofARRAY8(array)
171 * regardless of the return value below.
172 * Note that XdmcpDisposeARRAY*(array) will call free(array->data),
173 * so we must guarantee that array->data is NULL or a malloced pointer.
175 if (!XdmcpReadCARD8 (buffer, &array->length)) {
184 array->data = (ARRAY8 *) malloc(array->length * sizeof (ARRAY8));
187 for (i = 0; i < array->length; i++)
189 if (!XdmcpReadARRAY8 (buffer, &array->data[i]))
192 * We must free all of the arrays allocated thus far in the loop
193 * and free array->data and finally set array->data = 0;
194 * The easiest way to do this is to reset the length and call
195 * XdmcpDisposeARRAYofARRAY8(array).
198 XdmcpDisposeARRAYofARRAY8(array);
206 XdmcpReadCARD8 (XdmcpBufferPtr buffer, CARD8Ptr valuep)
208 if (buffer->pointer >= buffer->count)
210 *valuep = (CARD8) buffer->data[buffer->pointer++];
215 XdmcpReadCARD16 (XdmcpBufferPtr buffer, CARD16Ptr valuep)
219 if (XdmcpReadCARD8 (buffer, &high) &&
220 XdmcpReadCARD8 (buffer, &low))
222 *valuep = (((CARD16) high) << 8) | ((CARD16) low);
229 XdmcpReadCARD32 (XdmcpBufferPtr buffer, CARD32Ptr valuep)
231 CARD8 byte0, byte1, byte2, byte3;
232 if (XdmcpReadCARD8 (buffer, &byte0) &&
233 XdmcpReadCARD8 (buffer, &byte1) &&
234 XdmcpReadCARD8 (buffer, &byte2) &&
235 XdmcpReadCARD8 (buffer, &byte3))
237 *valuep = (((CARD32) byte0) << 24) |
238 (((CARD32) byte1) << 16) |
239 (((CARD32) byte2) << 8) |