1/*
2 * Copyright © 2011 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23#ifdef HAVE_DIX_CONFIG_H
24#include <dix-config.h>
25#endif
26
27#include "glxserver.h"
28#include "indirect_dispatch.h"
29#include "glxbyteorder.h"
30#include "unpack.h"
31
32static int
33set_client_info(__GLXclientState * cl, xGLXSetClientInfoARBReq * req,
34                unsigned bytes_per_version)
35{
36    ClientPtr client = cl->client;
37    char *gl_extensions;
38    char *glx_extensions;
39    int size;
40
41    REQUEST_AT_LEAST_SIZE(xGLXSetClientInfoARBReq);
42
43    /* Verify that the size of the packet matches the size inferred from the
44     * sizes specified for the various fields.
45     */
46    size = sz_xGLXSetClientInfoARBReq;
47    size = safe_add(size, safe_mul(req->numVersions, bytes_per_version));
48    size = safe_add(size, safe_pad(req->numGLExtensionBytes));
49    size = safe_add(size, safe_pad(req->numGLXExtensionBytes));
50
51    if (size < 0 || req->length != (size / 4))
52        return BadLength;
53
54    /* Verify that the actual length of the GL extension string matches what's
55     * encoded in protocol packet.
56     */
57    gl_extensions = (char *) (req + 1) + (req->numVersions * bytes_per_version);
58    if (req->numGLExtensionBytes != 0
59        && memchr(gl_extensions, 0,
60                  __GLX_PAD(req->numGLExtensionBytes)) == NULL)
61        return BadLength;
62
63    /* Verify that the actual length of the GLX extension string matches
64     * what's encoded in protocol packet.
65     */
66    glx_extensions = gl_extensions + __GLX_PAD(req->numGLExtensionBytes);
67    if (req->numGLXExtensionBytes != 0
68        && memchr(glx_extensions, 0,
69                  __GLX_PAD(req->numGLXExtensionBytes)) == NULL)
70        return BadLength;
71
72    free(cl->GLClientextensions);
73    cl->GLClientextensions = strdup(gl_extensions);
74
75    return 0;
76}
77
78int
79__glXDisp_SetClientInfoARB(__GLXclientState * cl, GLbyte * pc)
80{
81    return set_client_info(cl, (xGLXSetClientInfoARBReq *) pc, 8);
82}
83
84int
85__glXDispSwap_SetClientInfoARB(__GLXclientState * cl, GLbyte * pc)
86{
87    ClientPtr client = cl->client;
88    xGLXSetClientInfoARBReq *req = (xGLXSetClientInfoARBReq *) pc;
89
90    REQUEST_AT_LEAST_SIZE(xGLXSetClientInfoARBReq);
91
92    req->length = bswap_16(req->length);
93    req->numVersions = bswap_32(req->numVersions);
94    req->numGLExtensionBytes = bswap_32(req->numGLExtensionBytes);
95    req->numGLXExtensionBytes = bswap_32(req->numGLXExtensionBytes);
96
97    return __glXDisp_SetClientInfoARB(cl, pc);
98}
99
100int
101__glXDisp_SetClientInfo2ARB(__GLXclientState * cl, GLbyte * pc)
102{
103    return set_client_info(cl, (xGLXSetClientInfoARBReq *) pc, 12);
104}
105
106int
107__glXDispSwap_SetClientInfo2ARB(__GLXclientState * cl, GLbyte * pc)
108{
109    ClientPtr client = cl->client;
110    xGLXSetClientInfoARBReq *req = (xGLXSetClientInfoARBReq *) pc;
111
112    REQUEST_AT_LEAST_SIZE(xGLXSetClientInfoARBReq);
113
114    req->length = bswap_16(req->length);
115    req->numVersions = bswap_32(req->numVersions);
116    req->numGLExtensionBytes = bswap_32(req->numGLExtensionBytes);
117    req->numGLXExtensionBytes = bswap_32(req->numGLXExtensionBytes);
118
119    return __glXDisp_SetClientInfo2ARB(cl, pc);
120}
121