Add name_lookup function in object_ops.
[wine] / server / atom.c
1 /*
2  * Server-side atom management
3  *
4  * Copyright (C) 1999, 2000 Alexandre Julliard
5  * Copyright (C) 2000 Turchanov Sergei
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 #include "config.h"
23 #include "wine/port.h"
24
25 #include <assert.h>
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
29
30 #include "unicode.h"
31 #include "request.h"
32 #include "object.h"
33 #include "process.h"
34 #include "handle.h"
35 #include "user.h"
36 #include "winuser.h"
37
38 #define HASH_SIZE     37
39 #define MIN_HASH_SIZE 4
40 #define MAX_HASH_SIZE 0x200
41
42 #define MAX_ATOM_LEN  255
43 #define MIN_STR_ATOM  0xc000
44 #define MAX_ATOMS     0x4000
45
46 struct atom_entry
47 {
48     struct atom_entry *next;   /* hash table list */
49     struct atom_entry *prev;   /* hash table list */
50     int                count;  /* reference count */
51     short              pinned; /* whether the atom is pinned or not */
52     atom_t             atom;   /* atom handle */
53     unsigned short     hash;   /* string hash */
54     unsigned short     len;    /* string len */
55     WCHAR              str[1]; /* atom string */
56 };
57
58 struct atom_table
59 {
60     struct object       obj;                 /* object header */
61     int                 count;               /* count of atom handles */
62     int                 last;                /* last handle in-use */
63     struct atom_entry **handles;             /* atom handles */
64     int                 entries_count;       /* humber of hash entries */
65     struct atom_entry **entries;             /* hash table entries */
66 };
67
68 static void atom_table_dump( struct object *obj, int verbose );
69 static void atom_table_destroy( struct object *obj );
70
71 static const struct object_ops atom_table_ops =
72 {
73     sizeof(struct atom_table),    /* size */
74     atom_table_dump,              /* dump */
75     no_add_queue,                 /* add_queue */
76     NULL,                         /* remove_queue */
77     NULL,                         /* signaled */
78     NULL,                         /* satisfied */
79     no_signal,                    /* signal */
80     no_get_fd,                    /* get_fd */
81     no_lookup_name,               /* lookup_name */
82     no_close_handle,              /* close_handle */
83     atom_table_destroy            /* destroy */
84 };
85
86
87 /* create an atom table */
88 static struct atom_table *create_table(int entries_count)
89 {
90     struct atom_table *table;
91
92     if ((table = alloc_object( &atom_table_ops )))
93     {
94         if ((entries_count < MIN_HASH_SIZE) ||
95             (entries_count > MAX_HASH_SIZE)) entries_count = HASH_SIZE;
96         table->entries_count = entries_count;
97         if (!(table->entries = malloc( sizeof(*table->entries) * table->entries_count )))
98         {
99             set_error( STATUS_NO_MEMORY );
100             goto fail;
101         }
102         memset( table->entries, 0, sizeof(*table->entries) * table->entries_count );
103         table->count = 64;
104         table->last  = -1;
105         if ((table->handles = mem_alloc( sizeof(*table->handles) * table->count )))
106             return table;
107 fail:
108         release_object( table );
109         table = NULL;
110     }
111     return table;
112 }
113
114 /* retrieve an entry pointer from its atom */
115 static struct atom_entry *get_atom_entry( struct atom_table *table, atom_t atom )
116 {
117     struct atom_entry *entry = NULL;
118     if (table && (atom >= MIN_STR_ATOM) && (atom <= MIN_STR_ATOM + table->last))
119         entry = table->handles[atom - MIN_STR_ATOM];
120     if (!entry) set_error( STATUS_INVALID_HANDLE );
121     return entry;
122 }
123
124 /* add an atom entry in the table and return its handle */
125 static atom_t add_atom_entry( struct atom_table *table, struct atom_entry *entry )
126 {
127     int i;
128     for (i = 0; i <= table->last; i++)
129         if (!table->handles[i]) goto found;
130     if (i == table->count)
131     {
132         struct atom_entry **new_table = NULL;
133         int new_size = table->count + table->count / 2;
134         if (new_size > MAX_ATOMS) new_size = MAX_ATOMS;
135         if (new_size > table->count)
136             new_table = realloc( table->handles, sizeof(*table->handles) * new_size );
137         if (!new_table)
138         {
139             set_error( STATUS_NO_MEMORY );
140             return 0;
141         }
142         table->count = new_size;
143         table->handles = new_table;
144     }
145     table->last = i;
146  found:
147     table->handles[i] = entry;
148     entry->atom = i + MIN_STR_ATOM;
149     return entry->atom;
150 }
151
152 /* compute the hash code for a string */
153 static unsigned short atom_hash( struct atom_table *table, const WCHAR *str, size_t len )
154 {
155     unsigned int i;
156     unsigned short hash = 0;
157     for (i = 0; i < len; i++) hash ^= toupperW(str[i]) + i;
158     return hash % table->entries_count;
159 }
160
161 /* dump an atom table */
162 static void atom_table_dump( struct object *obj, int verbose )
163 {
164     int i;
165     struct atom_table *table = (struct atom_table *)obj;
166     assert( obj->ops == &atom_table_ops );
167
168     fprintf( stderr, "Atom table size=%d entries=%d\n",
169              table->last + 1, table->entries_count );
170     if (!verbose) return;
171     for (i = 0; i <= table->last; i++)
172     {
173         struct atom_entry *entry = table->handles[i];
174         if (!entry) continue;
175         fprintf( stderr, "  %04x: ref=%d pinned=%c hash=%d \"",
176                  entry->atom, entry->count, entry->pinned ? 'Y' : 'N', entry->hash );
177         dump_strW( entry->str, entry->len, stderr, "\"\"");
178         fprintf( stderr, "\"\n" );
179     }
180 }
181
182 /* destroy the atom table */
183 static void atom_table_destroy( struct object *obj )
184 {
185     int i;
186     struct atom_table *table = (struct atom_table *)obj;
187     assert( obj->ops == &atom_table_ops );
188     if (table->handles)
189     {
190         for (i = 0; i <= table->last; i++) free( table->handles[i] );
191         free( table->handles );
192     }
193     if (table->entries) free( table->entries );
194 }
195
196 /* find an atom entry in its hash list */
197 static struct atom_entry *find_atom_entry( struct atom_table *table, const WCHAR *str,
198                                            size_t len, unsigned short hash )
199 {
200     struct atom_entry *entry = table->entries[hash];
201     while (entry)
202     {
203         if (entry->len == len && !memicmpW( entry->str, str, len )) break;
204         entry = entry->next;
205     }
206     return entry;
207 }
208
209 /* add an atom to the table */
210 static atom_t add_atom( struct atom_table *table, const WCHAR *str, size_t len )
211 {
212     struct atom_entry *entry;
213     unsigned short hash = atom_hash( table, str, len );
214     atom_t atom = 0;
215
216     if (!len)
217     {
218         set_error( STATUS_OBJECT_NAME_INVALID );
219         return 0;
220     }
221     if (len > MAX_ATOM_LEN)
222     {
223         set_error( STATUS_INVALID_PARAMETER );
224         return 0;
225     }
226     if ((entry = find_atom_entry( table, str, len, hash )))  /* exists already */
227     {
228         entry->count++;
229         return entry->atom;
230     }
231
232     if ((entry = mem_alloc( sizeof(*entry) + (len - 1) * sizeof(WCHAR) )))
233     {
234         if ((atom = add_atom_entry( table, entry )))
235         {
236             entry->prev  = NULL;
237             if ((entry->next = table->entries[hash])) entry->next->prev = entry;
238             table->entries[hash] = entry;
239             entry->count  = 1;
240             entry->pinned = 0;
241             entry->hash   = hash;
242             entry->len    = len;
243             memcpy( entry->str, str, len * sizeof(WCHAR) );
244         }
245         else free( entry );
246     }
247     else set_error( STATUS_NO_MEMORY );
248     return atom;
249 }
250
251 /* delete an atom from the table */
252 static void delete_atom( struct atom_table *table, atom_t atom, int if_pinned )
253 {
254     struct atom_entry *entry = get_atom_entry( table, atom );
255     if (!entry) return;
256     if (entry->pinned && !if_pinned) set_error( STATUS_WAS_LOCKED );
257     else if (!--entry->count)
258     {
259         if (entry->next) entry->next->prev = entry->prev;
260         if (entry->prev) entry->prev->next = entry->next;
261         else table->entries[entry->hash] = entry->next;
262         table->handles[atom - MIN_STR_ATOM] = NULL;
263         free( entry );
264     }
265 }
266
267 /* find an atom in the table */
268 static atom_t find_atom( struct atom_table *table, const WCHAR *str, size_t len )
269 {
270     struct atom_entry *entry;
271
272     if (!len)
273     {
274         set_error( STATUS_OBJECT_NAME_INVALID );
275         return 0;
276     }
277     if (len > MAX_ATOM_LEN)
278     {
279         set_error( STATUS_INVALID_PARAMETER );
280         return 0;
281     }
282     if (table && (entry = find_atom_entry( table, str, len, atom_hash(table, str, len) )))
283         return entry->atom;
284     set_error( STATUS_OBJECT_NAME_NOT_FOUND );
285     return 0;
286 }
287
288 static struct atom_table *get_global_table( struct winstation *winstation, int create )
289 {
290     if (!winstation->atom_table)
291     {
292         if (create) winstation->atom_table = create_table( HASH_SIZE );
293         else set_error( STATUS_OBJECT_NAME_NOT_FOUND );
294     }
295     return winstation->atom_table;
296 }
297
298 static struct atom_table *get_table( obj_handle_t h, int create )
299 {
300     struct atom_table *table = NULL;
301
302     if (h)
303     {
304         table = (struct atom_table *)get_handle_obj( current->process, h, 0, &atom_table_ops );
305     }
306     else
307     {
308         struct winstation *winstation = get_process_winstation( current->process,
309                                                                 WINSTA_ACCESSGLOBALATOMS );
310         if (winstation)
311         {
312             table = get_global_table( winstation, 1 );
313             if (table) grab_object( table );
314             release_object( winstation );
315         }
316     }
317     return table;
318 }
319
320 /* add an atom in the global table; used for window properties */
321 atom_t add_global_atom( struct winstation *winstation, const WCHAR *str, size_t len )
322 {
323     struct atom_table *global_table = get_global_table( winstation, 1 );
324     if (!global_table) return 0;
325     return add_atom( global_table, str, len );
326 }
327
328 /* find an atom in the global table; used for window properties */
329 atom_t find_global_atom( struct winstation *winstation, const WCHAR *str, size_t len )
330 {
331     struct atom_table *global_table = get_global_table( winstation, 0 );
332     struct atom_entry *entry;
333
334     if (!len || len > MAX_ATOM_LEN || !global_table) return 0;
335     if ((entry = find_atom_entry( global_table, str, len, atom_hash(global_table, str, len) )))
336         return entry->atom;
337     return 0;
338 }
339
340 /* increment the ref count of a global atom; used for window properties */
341 int grab_global_atom( struct winstation *winstation, atom_t atom )
342 {
343     if (atom >= MIN_STR_ATOM)
344     {
345         struct atom_table *global_table = get_global_table( winstation, 0 );
346         if (global_table)
347         {
348             struct atom_entry *entry = get_atom_entry( global_table, atom );
349             if (entry) entry->count++;
350             return (entry != NULL);
351         }
352         else return 0;
353     }
354     else return 1;
355 }
356
357 /* decrement the ref count of a global atom; used for window properties */
358 void release_global_atom( struct winstation *winstation, atom_t atom )
359 {
360     if (atom >= MIN_STR_ATOM)
361     {
362         struct atom_table *global_table = get_global_table( winstation, 0 );
363         if (global_table) delete_atom( global_table, atom, 1 );
364     }
365 }
366
367 /* add a global atom */
368 DECL_HANDLER(add_atom)
369 {
370     struct atom_table *table = get_table( req->table, 1 );
371     if (table)
372     {
373         reply->atom = add_atom( table, get_req_data(), get_req_data_size() / sizeof(WCHAR) );
374         release_object( table );
375     }
376 }
377
378 /* delete a global atom */
379 DECL_HANDLER(delete_atom)
380 {
381     struct atom_table *table = get_table( req->table, 0 );
382     if (table)
383     {
384         delete_atom( table, req->atom, 0 );
385         release_object( table );
386     }
387 }
388
389 /* find a global atom */
390 DECL_HANDLER(find_atom)
391 {
392     struct atom_table *table = get_table( req->table, 0 );
393     if (table)
394     {
395         reply->atom = find_atom( table, get_req_data(), get_req_data_size() / sizeof(WCHAR) );
396         release_object( table );
397     }
398 }
399
400 /* get global atom name */
401 DECL_HANDLER(get_atom_information)
402 {
403     struct atom_table *table = get_table( req->table, 0 );
404     if (table)
405     {
406         struct atom_entry *entry;
407
408         if ((entry = get_atom_entry( table, req->atom )))
409         {
410             size_t len = entry->len * sizeof(WCHAR);
411             if (len <= get_reply_max_size()) set_reply_data( entry->str, len );
412             else if (get_reply_max_size()) set_error( STATUS_BUFFER_OVERFLOW );
413             reply->count = entry->count;
414             reply->pinned = entry->pinned;
415         }
416         else reply->count = -1;
417         release_object( table );
418     }
419 }
420
421 /* set global atom name */
422 DECL_HANDLER(set_atom_information)
423 {
424     struct atom_table *table = get_table( req->table, 0 );
425     if (table)
426     {
427         struct atom_entry *entry;
428
429         if ((entry = get_atom_entry( table, req->atom )))
430         {
431             if (req->pinned) entry->pinned = 1;
432         }
433         release_object( table );
434     }
435 }
436
437 /* init a (local) atom table */
438 DECL_HANDLER(init_atom_table)
439 {
440     struct atom_table* table = create_table( req->entries );
441
442     if (table)
443     {
444         reply->table = alloc_handle( current->process, table, 0, FALSE);
445         release_object( table );
446     }
447 }
448
449 /* set global atom name */
450 DECL_HANDLER(empty_atom_table)
451 {
452     struct atom_table *table = get_table( req->table, 1 );
453     if (table)
454     {
455         int i;
456         struct atom_entry *entry;
457
458         for (i = 0; i <= table->last; i++)
459         {
460             entry = table->handles[i];
461             if (entry && (!entry->pinned || req->if_pinned))
462             {
463                 if (entry->next) entry->next->prev = entry->prev;
464                 if (entry->prev) entry->prev->next = entry->next;
465                 else table->entries[entry->hash] = entry->next;
466                 table->handles[i] = NULL;
467                 free( entry );
468             }
469         }
470         release_object( table );
471     }
472 }