105b261ecSmrg/***********************************************************
205b261ecSmrg
305b261ecSmrgCopyright 1987, 1998  The Open Group
405b261ecSmrg
505b261ecSmrgPermission to use, copy, modify, distribute, and sell this software and its
605b261ecSmrgdocumentation for any purpose is hereby granted without fee, provided that
705b261ecSmrgthe above copyright notice appear in all copies and that both that
805b261ecSmrgcopyright notice and this permission notice appear in supporting
905b261ecSmrgdocumentation.
1005b261ecSmrg
1105b261ecSmrgThe above copyright notice and this permission notice shall be included in
1205b261ecSmrgall copies or substantial portions of the Software.
1305b261ecSmrg
1405b261ecSmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1505b261ecSmrgIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1605b261ecSmrgFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
1705b261ecSmrgOPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
1805b261ecSmrgAN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
1905b261ecSmrgCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2005b261ecSmrg
2105b261ecSmrgExcept as contained in this notice, the name of The Open Group shall not be
2205b261ecSmrgused in advertising or otherwise to promote the sale, use or other dealings
2305b261ecSmrgin this Software without prior written authorization from The Open Group.
2405b261ecSmrg
2505b261ecSmrgCopyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts.
2605b261ecSmrg
2705b261ecSmrg                        All Rights Reserved
2805b261ecSmrg
2935c4bbdfSmrgPermission to use, copy, modify, and distribute this software and its
3035c4bbdfSmrgdocumentation for any purpose and without fee is hereby granted,
3105b261ecSmrgprovided that the above copyright notice appear in all copies and that
3235c4bbdfSmrgboth that copyright notice and this permission notice appear in
3305b261ecSmrgsupporting documentation, and that the name of Digital not be
3405b261ecSmrgused in advertising or publicity pertaining to distribution of the
3535c4bbdfSmrgsoftware without specific, written prior permission.
3605b261ecSmrg
3705b261ecSmrgDIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
3805b261ecSmrgALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
3905b261ecSmrgDIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
4005b261ecSmrgANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
4105b261ecSmrgWHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
4205b261ecSmrgARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
4305b261ecSmrgSOFTWARE.
4405b261ecSmrg
4505b261ecSmrg******************************************************************/
4605b261ecSmrg
4705b261ecSmrg#ifdef HAVE_DIX_CONFIG_H
4805b261ecSmrg#include <dix-config.h>
4905b261ecSmrg#endif
5005b261ecSmrg
5105b261ecSmrg#include <X11/X.h>
5205b261ecSmrg#include <X11/Xproto.h>
5305b261ecSmrg#include "misc.h"
5405b261ecSmrg#include "dixstruct.h"
5505b261ecSmrg#include "extnsionst.h"
5605b261ecSmrg#include "gcstruct.h"
5705b261ecSmrg#include "scrnintstr.h"
5805b261ecSmrg#include "dispatch.h"
594642e01fSmrg#include "privates.h"
604642e01fSmrg#include "registry.h"
6105b261ecSmrg#include "xace.h"
6205b261ecSmrg
6305b261ecSmrg#define LAST_ERROR 255
6405b261ecSmrg
6535c4bbdfSmrgstatic ExtensionEntry **extensions = (ExtensionEntry **) NULL;
6605b261ecSmrg
6705b261ecSmrgint lastEvent = EXTENSION_EVENT_BASE;
6805b261ecSmrgstatic int lastError = FirstExtensionError;
6905b261ecSmrgstatic unsigned int NumExtensions = 0;
7005b261ecSmrg
716747b715SmrgExtensionEntry *
7235c4bbdfSmrgAddExtension(const char *name, int NumEvents, int NumErrors,
7335c4bbdfSmrg             int (*MainProc) (ClientPtr c1),
7435c4bbdfSmrg             int (*SwappedMainProc) (ClientPtr c2),
7535c4bbdfSmrg             void (*CloseDownProc) (ExtensionEntry * e),
7635c4bbdfSmrg             unsigned short (*MinorOpcodeProc) (ClientPtr c3))
7705b261ecSmrg{
7805b261ecSmrg    int i;
7905b261ecSmrg    ExtensionEntry *ext, **newexts;
8005b261ecSmrg
814642e01fSmrg    if (!MainProc || !SwappedMainProc || !MinorOpcodeProc)
8235c4bbdfSmrg        return ((ExtensionEntry *) NULL);
8335c4bbdfSmrg    if ((lastEvent + NumEvents > MAXEVENTS) ||
8435c4bbdfSmrg        (unsigned) (lastError + NumErrors > LAST_ERROR)) {
856747b715Smrg        LogMessage(X_ERROR, "Not enabling extension %s: maximum number of "
866747b715Smrg                   "events or errors exceeded.\n", name);
8735c4bbdfSmrg        return ((ExtensionEntry *) NULL);
886747b715Smrg    }
8905b261ecSmrg
9035c4bbdfSmrg    ext = calloc(sizeof(ExtensionEntry), 1);
9105b261ecSmrg    if (!ext)
9235c4bbdfSmrg        return NULL;
936747b715Smrg    if (!dixAllocatePrivates(&ext->devPrivates, PRIVATE_EXTENSION)) {
9435c4bbdfSmrg        free(ext);
9535c4bbdfSmrg        return NULL;
966747b715Smrg    }
979ace9065Smrg    ext->name = strdup(name);
9835c4bbdfSmrg    if (!ext->name) {
9935c4bbdfSmrg        dixFreePrivates(ext->devPrivates, PRIVATE_EXTENSION);
10035c4bbdfSmrg        free(ext);
10135c4bbdfSmrg        return ((ExtensionEntry *) NULL);
10205b261ecSmrg    }
10305b261ecSmrg    i = NumExtensions;
10435c4bbdfSmrg    newexts = reallocarray(extensions, i + 1, sizeof(ExtensionEntry *));
10535c4bbdfSmrg    if (!newexts) {
10635c4bbdfSmrg        free((void *) ext->name);
10735c4bbdfSmrg        dixFreePrivates(ext->devPrivates, PRIVATE_EXTENSION);
10835c4bbdfSmrg        free(ext);
10935c4bbdfSmrg        return ((ExtensionEntry *) NULL);
11005b261ecSmrg    }
11105b261ecSmrg    NumExtensions++;
11205b261ecSmrg    extensions = newexts;
11305b261ecSmrg    extensions[i] = ext;
11405b261ecSmrg    ext->index = i;
11505b261ecSmrg    ext->base = i + EXTENSION_BASE;
11605b261ecSmrg    ext->CloseDown = CloseDownProc;
11705b261ecSmrg    ext->MinorOpcode = MinorOpcodeProc;
11805b261ecSmrg    ProcVector[i + EXTENSION_BASE] = MainProc;
11905b261ecSmrg    SwappedProcVector[i + EXTENSION_BASE] = SwappedMainProc;
12035c4bbdfSmrg    if (NumEvents) {
12105b261ecSmrg        ext->eventBase = lastEvent;
12235c4bbdfSmrg        ext->eventLast = lastEvent + NumEvents;
12335c4bbdfSmrg        lastEvent += NumEvents;
12405b261ecSmrg    }
12535c4bbdfSmrg    else {
12605b261ecSmrg        ext->eventBase = 0;
12705b261ecSmrg        ext->eventLast = 0;
12805b261ecSmrg    }
12935c4bbdfSmrg    if (NumErrors) {
13005b261ecSmrg        ext->errorBase = lastError;
13135c4bbdfSmrg        ext->errorLast = lastError + NumErrors;
13235c4bbdfSmrg        lastError += NumErrors;
13305b261ecSmrg    }
13435c4bbdfSmrg    else {
13505b261ecSmrg        ext->errorBase = 0;
13605b261ecSmrg        ext->errorLast = 0;
13705b261ecSmrg    }
13805b261ecSmrg
13935c4bbdfSmrg#ifdef X_REGISTRY_REQUEST
1404642e01fSmrg    RegisterExtensionNames(ext);
14135c4bbdfSmrg#endif
1426747b715Smrg    return ext;
14305b261ecSmrg}
14405b261ecSmrg
14505b261ecSmrgstatic int
14635c4bbdfSmrgFindExtension(const char *extname, int len)
14705b261ecSmrg{
1481b5d61b8Smrg    int i;
14905b261ecSmrg
15035c4bbdfSmrg    for (i = 0; i < NumExtensions; i++) {
15135c4bbdfSmrg        if ((strlen(extensions[i]->name) == len) &&
15235c4bbdfSmrg            !strncmp(extname, extensions[i]->name, len))
15335c4bbdfSmrg            break;
15405b261ecSmrg    }
15505b261ecSmrg    return ((i == NumExtensions) ? -1 : i);
15605b261ecSmrg}
15705b261ecSmrg
15805b261ecSmrg/*
15905b261ecSmrg * CheckExtension returns the extensions[] entry for the requested
16005b261ecSmrg * extension name.  Maybe this could just return a Bool instead?
16105b261ecSmrg */
1626747b715SmrgExtensionEntry *
16305b261ecSmrgCheckExtension(const char *extname)
16405b261ecSmrg{
16505b261ecSmrg    int n;
16605b261ecSmrg
16735c4bbdfSmrg    n = FindExtension(extname, strlen(extname));
16805b261ecSmrg    if (n != -1)
16935c4bbdfSmrg        return extensions[n];
17005b261ecSmrg    else
17135c4bbdfSmrg        return NULL;
17205b261ecSmrg}
17305b261ecSmrg
17405b261ecSmrg/*
17505b261ecSmrg * Added as part of Xace.
17605b261ecSmrg */
17705b261ecSmrgExtensionEntry *
17805b261ecSmrgGetExtensionEntry(int major)
17935c4bbdfSmrg{
18005b261ecSmrg    if (major < EXTENSION_BASE)
18135c4bbdfSmrg        return NULL;
18205b261ecSmrg    major -= EXTENSION_BASE;
18305b261ecSmrg    if (major >= NumExtensions)
18435c4bbdfSmrg        return NULL;
18505b261ecSmrg    return extensions[major];
18605b261ecSmrg}
18705b261ecSmrg
1886747b715Smrgunsigned short
18905b261ecSmrgStandardMinorOpcode(ClientPtr client)
19005b261ecSmrg{
19135c4bbdfSmrg    return ((xReq *) client->requestBuffer)->data;
19205b261ecSmrg}
19305b261ecSmrg
19405b261ecSmrgvoid
19505b261ecSmrgCloseDownExtensions(void)
19605b261ecSmrg{
1971b5d61b8Smrg    int i;
19835c4bbdfSmrg
19935c4bbdfSmrg    for (i = NumExtensions - 1; i >= 0; i--) {
20035c4bbdfSmrg        if (extensions[i]->CloseDown)
20135c4bbdfSmrg            extensions[i]->CloseDown(extensions[i]);
20235c4bbdfSmrg        NumExtensions = i;
20335c4bbdfSmrg        free((void *) extensions[i]->name);
20435c4bbdfSmrg        dixFreePrivates(extensions[i]->devPrivates, PRIVATE_EXTENSION);
20535c4bbdfSmrg        free(extensions[i]);
20605b261ecSmrg    }
2076747b715Smrg    free(extensions);
20835c4bbdfSmrg    extensions = (ExtensionEntry **) NULL;
20905b261ecSmrg    lastEvent = EXTENSION_EVENT_BASE;
21005b261ecSmrg    lastError = FirstExtensionError;
21105b261ecSmrg}
21205b261ecSmrg
2131b5d61b8Smrgstatic Bool
2141b5d61b8SmrgExtensionAvailable(ClientPtr client, ExtensionEntry *ext)
2151b5d61b8Smrg{
2161b5d61b8Smrg    if (XaceHook(XACE_EXT_ACCESS, client, ext) != Success)
2171b5d61b8Smrg        return FALSE;
2181b5d61b8Smrg    if (!ext->base)
2191b5d61b8Smrg        return FALSE;
2201b5d61b8Smrg    return TRUE;
2211b5d61b8Smrg}
2221b5d61b8Smrg
22305b261ecSmrgint
22405b261ecSmrgProcQueryExtension(ClientPtr client)
22505b261ecSmrg{
22605b261ecSmrg    xQueryExtensionReply reply;
22705b261ecSmrg    int i;
22835c4bbdfSmrg
22905b261ecSmrg    REQUEST(xQueryExtensionReq);
23005b261ecSmrg
23105b261ecSmrg    REQUEST_FIXED_SIZE(xQueryExtensionReq, stuff->nbytes);
2326747b715Smrg
23335c4bbdfSmrg    reply = (xQueryExtensionReply) {
23435c4bbdfSmrg        .type = X_Reply,
23535c4bbdfSmrg        .sequenceNumber = client->sequence,
23635c4bbdfSmrg        .length = 0,
23735c4bbdfSmrg        .major_opcode = 0
23835c4bbdfSmrg    };
23905b261ecSmrg
24035c4bbdfSmrg    if (!NumExtensions)
24105b261ecSmrg        reply.present = xFalse;
24235c4bbdfSmrg    else {
24335c4bbdfSmrg        i = FindExtension((char *) &stuff[1], stuff->nbytes);
2441b5d61b8Smrg        if (i < 0 || !ExtensionAvailable(client, extensions[i]))
24505b261ecSmrg            reply.present = xFalse;
24635c4bbdfSmrg        else {
24705b261ecSmrg            reply.present = xTrue;
24835c4bbdfSmrg            reply.major_opcode = extensions[i]->base;
24935c4bbdfSmrg            reply.first_event = extensions[i]->eventBase;
25035c4bbdfSmrg            reply.first_error = extensions[i]->errorBase;
25135c4bbdfSmrg        }
25205b261ecSmrg    }
25305b261ecSmrg    WriteReplyToClient(client, sizeof(xQueryExtensionReply), &reply);
2546747b715Smrg    return Success;
25505b261ecSmrg}
25605b261ecSmrg
25705b261ecSmrgint
25805b261ecSmrgProcListExtensions(ClientPtr client)
25905b261ecSmrg{
26005b261ecSmrg    xListExtensionsReply reply;
26105b261ecSmrg    char *bufptr, *buffer;
26205b261ecSmrg    int total_length = 0;
26305b261ecSmrg
26405b261ecSmrg    REQUEST_SIZE_MATCH(xReq);
26505b261ecSmrg
26635c4bbdfSmrg    reply = (xListExtensionsReply) {
26735c4bbdfSmrg        .type = X_Reply,
26835c4bbdfSmrg        .nExtensions = 0,
26935c4bbdfSmrg        .sequenceNumber = client->sequence,
27035c4bbdfSmrg        .length = 0
27135c4bbdfSmrg    };
27205b261ecSmrg    buffer = NULL;
27305b261ecSmrg
27435c4bbdfSmrg    if (NumExtensions) {
2751b5d61b8Smrg        int i;
27605b261ecSmrg
27735c4bbdfSmrg        for (i = 0; i < NumExtensions; i++) {
27835c4bbdfSmrg            /* call callbacks to find out whether to show extension */
2791b5d61b8Smrg            if (!ExtensionAvailable(client, extensions[i]))
28035c4bbdfSmrg                continue;
28135c4bbdfSmrg
28235c4bbdfSmrg            total_length += strlen(extensions[i]->name) + 1;
2831b5d61b8Smrg            reply.nExtensions += 1;
28435c4bbdfSmrg        }
2856747b715Smrg        reply.length = bytes_to_int32(total_length);
28635c4bbdfSmrg        buffer = bufptr = malloc(total_length);
28735c4bbdfSmrg        if (!buffer)
28835c4bbdfSmrg            return BadAlloc;
28935c4bbdfSmrg        for (i = 0; i < NumExtensions; i++) {
29035c4bbdfSmrg            int len;
29135c4bbdfSmrg
2921b5d61b8Smrg            if (!ExtensionAvailable(client, extensions[i]))
29335c4bbdfSmrg                continue;
29405b261ecSmrg
29505b261ecSmrg            *bufptr++ = len = strlen(extensions[i]->name);
29635c4bbdfSmrg            memmove(bufptr, extensions[i]->name, len);
29735c4bbdfSmrg            bufptr += len;
29835c4bbdfSmrg        }
29905b261ecSmrg    }
30005b261ecSmrg    WriteReplyToClient(client, sizeof(xListExtensionsReply), &reply);
30105b261ecSmrg    if (reply.length)
30205b261ecSmrg        WriteToClient(client, total_length, buffer);
3039ace9065Smrg
3049ace9065Smrg    free(buffer);
3056747b715Smrg    return Success;
30605b261ecSmrg}
307