First import
[xorg_rtime.git] / xorg-server-1.4 / dix / extension.c
blob186574d76a2d5f404bcc7aa2e61d7252fd302934
1 /***********************************************************
3 Copyright 1987, 1998 The Open Group
5 Permission to use, copy, modify, distribute, and sell this software and its
6 documentation for any purpose is hereby granted without fee, provided that
7 the above copyright notice appear in all copies and that both that
8 copyright notice and this permission notice appear in supporting
9 documentation.
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 Except as contained in this notice, the name of The Open Group shall not be
22 used in advertising or otherwise to promote the sale, use or other dealings
23 in this Software without prior written authorization from The Open Group.
26 Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts.
28 All Rights Reserved
30 Permission to use, copy, modify, and distribute this software and its
31 documentation for any purpose and without fee is hereby granted,
32 provided that the above copyright notice appear in all copies and that
33 both that copyright notice and this permission notice appear in
34 supporting documentation, and that the name of Digital not be
35 used in advertising or publicity pertaining to distribution of the
36 software without specific, written prior permission.
38 DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
39 ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
40 DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
41 ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
42 WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
43 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
44 SOFTWARE.
46 ******************************************************************/
48 #ifdef HAVE_DIX_CONFIG_H
49 #include <dix-config.h>
50 #endif
52 #include <X11/X.h>
53 #define NEED_EVENTS
54 #define NEED_REPLIES
55 #include <X11/Xproto.h>
56 #include "misc.h"
57 #include "dixstruct.h"
58 #include "extnsionst.h"
59 #include "gcstruct.h"
60 #include "scrnintstr.h"
61 #include "dispatch.h"
62 #include "xace.h"
64 #define EXTENSION_BASE 128
65 #define EXTENSION_EVENT_BASE 64
66 #define LAST_EVENT 128
67 #define LAST_ERROR 255
69 static ExtensionEntry **extensions = (ExtensionEntry **)NULL;
71 int lastEvent = EXTENSION_EVENT_BASE;
72 static int lastError = FirstExtensionError;
73 static unsigned int NumExtensions = 0;
75 extern int extensionPrivateLen;
76 extern unsigned *extensionPrivateSizes;
77 extern unsigned totalExtensionSize;
79 static void
80 InitExtensionPrivates(ExtensionEntry *ext)
82 char *ptr;
83 DevUnion *ppriv;
84 unsigned *sizes;
85 unsigned size;
86 int i;
88 if (totalExtensionSize == sizeof(ExtensionEntry))
89 ppriv = (DevUnion *)NULL;
90 else
91 ppriv = (DevUnion *)(ext + 1);
93 ext->devPrivates = ppriv;
94 sizes = extensionPrivateSizes;
95 ptr = (char *)(ppriv + extensionPrivateLen);
96 for (i = extensionPrivateLen; --i >= 0; ppriv++, sizes++)
98 if ( (size = *sizes) )
100 ppriv->ptr = (pointer)ptr;
101 ptr += size;
103 else
104 ppriv->ptr = (pointer)NULL;
108 _X_EXPORT ExtensionEntry *
109 AddExtension(char *name, int NumEvents, int NumErrors,
110 int (*MainProc)(ClientPtr c1),
111 int (*SwappedMainProc)(ClientPtr c2),
112 void (*CloseDownProc)(ExtensionEntry *e),
113 unsigned short (*MinorOpcodeProc)(ClientPtr c3))
115 int i;
116 ExtensionEntry *ext, **newexts;
118 if (!MainProc || !SwappedMainProc || !CloseDownProc || !MinorOpcodeProc)
119 return((ExtensionEntry *) NULL);
120 if ((lastEvent + NumEvents > LAST_EVENT) ||
121 (unsigned)(lastError + NumErrors > LAST_ERROR))
122 return((ExtensionEntry *) NULL);
124 ext = (ExtensionEntry *) xalloc(totalExtensionSize);
125 if (!ext)
126 return((ExtensionEntry *) NULL);
127 bzero(ext, totalExtensionSize);
128 InitExtensionPrivates(ext);
129 ext->name = (char *)xalloc(strlen(name) + 1);
130 ext->num_aliases = 0;
131 ext->aliases = (char **)NULL;
132 if (!ext->name)
134 xfree(ext);
135 return((ExtensionEntry *) NULL);
137 strcpy(ext->name, name);
138 i = NumExtensions;
139 newexts = (ExtensionEntry **) xrealloc(extensions,
140 (i + 1) * sizeof(ExtensionEntry *));
141 if (!newexts)
143 xfree(ext->name);
144 xfree(ext);
145 return((ExtensionEntry *) NULL);
147 NumExtensions++;
148 extensions = newexts;
149 extensions[i] = ext;
150 ext->index = i;
151 ext->base = i + EXTENSION_BASE;
152 ext->CloseDown = CloseDownProc;
153 ext->MinorOpcode = MinorOpcodeProc;
154 ProcVector[i + EXTENSION_BASE] = MainProc;
155 SwappedProcVector[i + EXTENSION_BASE] = SwappedMainProc;
156 if (NumEvents)
158 ext->eventBase = lastEvent;
159 ext->eventLast = lastEvent + NumEvents;
160 lastEvent += NumEvents;
162 else
164 ext->eventBase = 0;
165 ext->eventLast = 0;
167 if (NumErrors)
169 ext->errorBase = lastError;
170 ext->errorLast = lastError + NumErrors;
171 lastError += NumErrors;
173 else
175 ext->errorBase = 0;
176 ext->errorLast = 0;
179 return(ext);
182 _X_EXPORT Bool AddExtensionAlias(char *alias, ExtensionEntry *ext)
184 char *name;
185 char **aliases;
187 aliases = (char **)xrealloc(ext->aliases,
188 (ext->num_aliases + 1) * sizeof(char *));
189 if (!aliases)
190 return FALSE;
191 ext->aliases = aliases;
192 name = (char *)xalloc(strlen(alias) + 1);
193 if (!name)
194 return FALSE;
195 strcpy(name, alias);
196 ext->aliases[ext->num_aliases] = name;
197 ext->num_aliases++;
198 return TRUE;
201 static int
202 FindExtension(char *extname, int len)
204 int i, j;
206 for (i=0; i<NumExtensions; i++)
208 if ((strlen(extensions[i]->name) == len) &&
209 !strncmp(extname, extensions[i]->name, len))
210 break;
211 for (j = extensions[i]->num_aliases; --j >= 0;)
213 if ((strlen(extensions[i]->aliases[j]) == len) &&
214 !strncmp(extname, extensions[i]->aliases[j], len))
215 break;
217 if (j >= 0) break;
219 return ((i == NumExtensions) ? -1 : i);
223 * CheckExtension returns the extensions[] entry for the requested
224 * extension name. Maybe this could just return a Bool instead?
226 _X_EXPORT ExtensionEntry *
227 CheckExtension(const char *extname)
229 int n;
231 n = FindExtension((char*)extname, strlen(extname));
232 if (n != -1)
233 return extensions[n];
234 else
235 return NULL;
239 * Added as part of Xace.
241 ExtensionEntry *
242 GetExtensionEntry(int major)
244 if (major < EXTENSION_BASE)
245 return NULL;
246 major -= EXTENSION_BASE;
247 if (major >= NumExtensions)
248 return NULL;
249 return extensions[major];
252 _X_EXPORT void
253 DeclareExtensionSecurity(char *extname, Bool secure)
255 int i = FindExtension(extname, strlen(extname));
256 if (i >= 0)
257 XaceHook(XACE_DECLARE_EXT_SECURE, extensions[i], secure);
260 _X_EXPORT unsigned short
261 StandardMinorOpcode(ClientPtr client)
263 return ((xReq *)client->requestBuffer)->data;
266 _X_EXPORT unsigned short
267 MinorOpcodeOfRequest(ClientPtr client)
269 unsigned char major;
271 major = ((xReq *)client->requestBuffer)->reqType;
272 if (major < EXTENSION_BASE)
273 return 0;
274 major -= EXTENSION_BASE;
275 if (major >= NumExtensions)
276 return 0;
277 return (*extensions[major]->MinorOpcode)(client);
280 void
281 CloseDownExtensions(void)
283 int i,j;
285 for (i = NumExtensions - 1; i >= 0; i--)
287 (* extensions[i]->CloseDown)(extensions[i]);
288 NumExtensions = i;
289 xfree(extensions[i]->name);
290 for (j = extensions[i]->num_aliases; --j >= 0;)
291 xfree(extensions[i]->aliases[j]);
292 xfree(extensions[i]->aliases);
293 xfree(extensions[i]);
295 xfree(extensions);
296 extensions = (ExtensionEntry **)NULL;
297 lastEvent = EXTENSION_EVENT_BASE;
298 lastError = FirstExtensionError;
302 ProcQueryExtension(ClientPtr client)
304 xQueryExtensionReply reply;
305 int i;
306 REQUEST(xQueryExtensionReq);
308 REQUEST_FIXED_SIZE(xQueryExtensionReq, stuff->nbytes);
310 reply.type = X_Reply;
311 reply.length = 0;
312 reply.major_opcode = 0;
313 reply.sequenceNumber = client->sequence;
315 if ( ! NumExtensions )
316 reply.present = xFalse;
317 else
319 i = FindExtension((char *)&stuff[1], stuff->nbytes);
320 if (i < 0 || !XaceHook(XACE_EXT_ACCESS, client, extensions[i]))
321 reply.present = xFalse;
322 else
324 reply.present = xTrue;
325 reply.major_opcode = extensions[i]->base;
326 reply.first_event = extensions[i]->eventBase;
327 reply.first_error = extensions[i]->errorBase;
330 WriteReplyToClient(client, sizeof(xQueryExtensionReply), &reply);
331 return(client->noClientException);
335 ProcListExtensions(ClientPtr client)
337 xListExtensionsReply reply;
338 char *bufptr, *buffer;
339 int total_length = 0;
341 REQUEST_SIZE_MATCH(xReq);
343 reply.type = X_Reply;
344 reply.nExtensions = 0;
345 reply.length = 0;
346 reply.sequenceNumber = client->sequence;
347 buffer = NULL;
349 if ( NumExtensions )
351 int i, j;
353 for (i=0; i<NumExtensions; i++)
355 /* call callbacks to find out whether to show extension */
356 if (!XaceHook(XACE_EXT_ACCESS, client, extensions[i]))
357 continue;
359 total_length += strlen(extensions[i]->name) + 1;
360 reply.nExtensions += 1 + extensions[i]->num_aliases;
361 for (j = extensions[i]->num_aliases; --j >= 0;)
362 total_length += strlen(extensions[i]->aliases[j]) + 1;
364 reply.length = (total_length + 3) >> 2;
365 buffer = bufptr = (char *)ALLOCATE_LOCAL(total_length);
366 if (!buffer)
367 return(BadAlloc);
368 for (i=0; i<NumExtensions; i++)
370 int len;
371 if (!XaceHook(XACE_EXT_ACCESS, client, extensions[i]))
372 continue;
374 *bufptr++ = len = strlen(extensions[i]->name);
375 memmove(bufptr, extensions[i]->name, len);
376 bufptr += len;
377 for (j = extensions[i]->num_aliases; --j >= 0;)
379 *bufptr++ = len = strlen(extensions[i]->aliases[j]);
380 memmove(bufptr, extensions[i]->aliases[j], len);
381 bufptr += len;
385 WriteReplyToClient(client, sizeof(xListExtensionsReply), &reply);
386 if (reply.length)
388 WriteToClient(client, total_length, buffer);
389 DEALLOCATE_LOCAL(buffer);
391 return(client->noClientException);
394 #ifdef XSERVER_DTRACE
395 void LoadExtensionNames(char **RequestNames) {
396 int i;
398 for (i=0; i<NumExtensions; i++) {
399 int r = extensions[i]->base;
401 if (RequestNames[r] == NULL) {
402 RequestNames[r] = strdup(extensions[i]->name);
406 #endif