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