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