summaryrefslogtreecommitdiff
path: root/dix/registry.c
blob: 5ab25ad70e5737795753d7510a79bc46d064fe12 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/************************************************************

Author: Eamon Walsh <ewalsh@tycho.nsa.gov>

Permission to use, copy, modify, distribute, and sell this software and its
documentation for any purpose is hereby granted without fee, provided that
this permission notice appear in supporting documentation.  This permission
notice shall be included in all copies or substantial portions of the
Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

********************************************************/

#ifdef HAVE_DIX_CONFIG_H
#include <dix-config.h>
#endif

#ifdef XREGISTRY

#include <stdlib.h>
#include <string.h>
#include <X11/X.h>
#include <X11/Xproto.h>
#include "resource.h"
#include "registry.h"

#define BASE_SIZE 16
#define CORE "X11"
#define FILENAME SERVER_MISC_CONFIG_PATH "/protocol.txt"

#define PROT_COMMENT '#'
#define PROT_REQUEST 'R'
#define PROT_EVENT 'V'
#define PROT_ERROR 'E'

static FILE *fh;

static char ***requests, **events, **errors;
static const char **resources;
static unsigned nmajor, *nminor, nevent, nerror, nresource;

/*
 * File parsing routines
 */
static int double_size(void *p, unsigned n, unsigned size)
{
    char **ptr = (char **)p;
    unsigned s, f;

    if (n) {
	s = n * size;
	n *= 2 * size;
	f = n;
    } else {
	s = 0;
	n = f = BASE_SIZE * size;
    }

    *ptr = realloc(*ptr, n);
    if (!*ptr) {
	dixResetRegistry();
	return FALSE;
    }
    memset(*ptr + s, 0, f - s);
    return TRUE;
}       

static void
RegisterRequestName(unsigned major, unsigned minor, char *name)
{
    while (major >= nmajor) {
	if (!double_size(&requests, nmajor, sizeof(char **)))
	    return;
	if (!double_size(&nminor, nmajor, sizeof(unsigned)))
	    return;
	nmajor = nmajor ? nmajor * 2 : BASE_SIZE;
    }
    while (minor >= nminor[major]) {
	if (!double_size(requests+major, nminor[major], sizeof(char *)))
	    return;
	nminor[major] = nminor[major] ? nminor[major] * 2 : BASE_SIZE;
    }

    free(requests[major][minor]);
    requests[major][minor] = name;
}

static void
RegisterEventName(unsigned event, char *name) {
    while (event >= nevent) {
	if (!double_size(&events, nevent, sizeof(char *)))
	    return;
	nevent = nevent ? nevent * 2 : BASE_SIZE;
    }

    free(events[event]);
    events[event] = name;
}

static void
RegisterErrorName(unsigned error, char *name) {
    while (error >= nerror) {
	if (!double_size(&errors, nerror, sizeof(char *)))
	    return;
	nerror = nerror ? nerror * 2 : BASE_SIZE;
    }

    free(errors[error]);
    errors[error] = name;
}

void
RegisterExtensionNames(ExtensionEntry *extEntry)
{
    char buf[256], *lineobj, *ptr;
    unsigned offset;

    if (fh == NULL)
	return;

    rewind(fh);

    while (fgets(buf, sizeof(buf), fh)) {
	lineobj = NULL;
	ptr = strchr(buf, '\n');
	if (ptr)
	    *ptr = 0;

	/* Check for comments or empty lines */
	switch (buf[0]) {
	case PROT_REQUEST:
	case PROT_EVENT:
	case PROT_ERROR:
	    break;
	case PROT_COMMENT:
	case '\0':
	    continue;
	default:
	    goto invalid;
	}

	/* Check for space character in the fifth position */
	ptr = strchr(buf, ' ');
	if (!ptr || ptr != buf + 4)
	    goto invalid;

	/* Duplicate the string after the space */
	lineobj = strdup(ptr + 1);
	if (!lineobj)
	    continue;

	/* Check for a colon somewhere on the line */
	ptr = strchr(buf, ':');
	if (!ptr)
	    goto invalid;

	/* Compare the part before colon with the target extension name */
	*ptr = 0;
	if (strcmp(buf + 5, extEntry->name))
	    goto skip;

	/* Get the opcode for the request, event, or error */
	offset = strtol(buf + 1, &ptr, 10);
	if (offset == 0 && ptr == buf + 1)
	    goto invalid;

	/* Save the strdup result in the registry */
	switch(buf[0]) {
	case PROT_REQUEST:
	    if (extEntry->base)
		RegisterRequestName(extEntry->base, offset, lineobj);
	    else
		RegisterRequestName(offset, 0, lineobj);
	    continue;
	case PROT_EVENT:
	    RegisterEventName(extEntry->eventBase + offset, lineobj);
	    continue;
	case PROT_ERROR:
	    RegisterErrorName(extEntry->errorBase + offset, lineobj);
	    continue;
	}

    invalid:
	LogMessage(X_WARNING, "Invalid line in " FILENAME ", skipping\n");
    skip:
	free(lineobj);
    }
}

/*
 * Registration functions
 */

void
RegisterResourceName(RESTYPE resource, const char *name)
{
    resource &= TypeMask;

    while (resource >= nresource) {
	if (!double_size(&resources, nresource, sizeof(char *)))
	    return;
	nresource = nresource ? nresource * 2 : BASE_SIZE;
    }

    resources[resource] = name;
}

/*
 * Lookup functions
 */

const char *
LookupRequestName(int major, int minor)
{
    if (major >= nmajor)
	return XREGISTRY_UNKNOWN;
    if (minor >= nminor[major])
	return XREGISTRY_UNKNOWN;

    return requests[major][minor] ? requests[major][minor] : XREGISTRY_UNKNOWN;
}

const char *
LookupMajorName(int major)
{
    if (major < 128) {
	const char *retval;

	if (major >= nmajor)
	    return XREGISTRY_UNKNOWN;
	if (0 >= nminor[major])
	    return XREGISTRY_UNKNOWN;

	retval = requests[major][0];
	return retval ? retval + sizeof(CORE) : XREGISTRY_UNKNOWN;
    } else {
	ExtensionEntry *extEntry = GetExtensionEntry(major);
	return extEntry ? extEntry->name : XREGISTRY_UNKNOWN;
    }
}

const char *
LookupEventName(int event)
{
    event &= 127;
    if (event >= nevent)
	return XREGISTRY_UNKNOWN;

    return events[event] ? events[event] : XREGISTRY_UNKNOWN;
}

const char *
LookupErrorName(int error)
{
    if (error >= nerror)
	return XREGISTRY_UNKNOWN;

    return errors[error] ? errors[error] : XREGISTRY_UNKNOWN;
}

const char *
LookupResourceName(RESTYPE resource)
{
    resource &= TypeMask;
    if (resource >= nresource)
	return XREGISTRY_UNKNOWN;

    return resources[resource] ? resources[resource] : XREGISTRY_UNKNOWN;
}

/*
 * Setup and teardown
 */
void
dixResetRegistry(void)
{
    ExtensionEntry extEntry;

    /* Free all memory */
    while (nmajor--) {
	while (nminor[nmajor])
	    free(requests[nmajor][--nminor[nmajor]]);
	free(requests[nmajor]);
    }
    free(requests);
    free(nminor);

    while (nevent--)
	free(events[nevent]);
    free(events);

    while (nerror--)
	free(errors[nerror]);
    free(errors);

    free(resources);

    requests = NULL;
    nminor = NULL;
    events = NULL;
    errors = NULL;
    resources = NULL;

    nmajor = nevent = nerror = nresource = 0;

    /* Open the protocol file */
    if (fh)
	fclose(fh);
    fh = fopen(FILENAME, "r");
    if (!fh)
	LogMessage(X_WARNING, "Failed to open protocol names file " FILENAME "\n");

    /* Add built-in resources */
    RegisterResourceName(RT_NONE, "NONE");
    RegisterResourceName(RT_WINDOW, "WINDOW");
    RegisterResourceName(RT_PIXMAP, "PIXMAP");
    RegisterResourceName(RT_GC, "GC");
    RegisterResourceName(RT_FONT, "FONT");
    RegisterResourceName(RT_CURSOR, "CURSOR");
    RegisterResourceName(RT_COLORMAP, "COLORMAP");
    RegisterResourceName(RT_CMAPENTRY, "COLORMAP ENTRY");
    RegisterResourceName(RT_OTHERCLIENT, "OTHER CLIENT");
    RegisterResourceName(RT_PASSIVEGRAB, "PASSIVE GRAB");

    /* Add the core protocol */
    memset(&extEntry, 0, sizeof(extEntry));
    extEntry.name = CORE;
    RegisterExtensionNames(&extEntry);
}

#endif /* XREGISTRY */