We need to give room for the null termination when translating the
[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
8 #include <assert.h>
9 #include <stdlib.h>
10 #include <stdio.h>
11
12 #include "unicode.h"
13 #include "request.h"
14 #include "object.h"
15 #include "process.h"
16
17 #define HASH_SIZE     37
18 #define MIN_HASH_SIZE 4
19 #define MAX_HASH_SIZE 0x200
20
21 #define MAX_ATOM_LEN  255
22 #define MAX_ATOMS     0x4000
23
24 struct atom_entry
25 {
26     struct atom_entry *next;   /* hash table list */
27     struct atom_entry *prev;   /* hash table list */
28     int                atom;   /* atom handle */
29     int                count;  /* reference count */
30     int                hash;   /* string hash */
31     WCHAR              str[1]; /* atom string */
32 };
33
34 struct atom_table
35 {
36     struct object       obj;                 /* object header */
37     int                 count;               /* count of atom handles */
38     int                 last;                /* last handle in-use */
39     struct atom_entry **handles;             /* atom handles */
40     int                 entries_count;       /* humber of hash entries */
41     struct atom_entry **entries;             /* hash table entries */
42 };
43
44 static void atom_table_dump( struct object *obj, int verbose );
45 static void atom_table_destroy( struct object *obj );
46
47 static const struct object_ops atom_table_ops =
48 {
49     sizeof(struct atom_table),    /* size */
50     atom_table_dump,              /* dump */
51     no_add_queue,                 /* add_queue */
52     NULL,                         /* remove_queue */
53     NULL,                         /* signaled */
54     NULL,                         /* satified */
55     NULL,                         /* get_poll_events */
56     NULL,                         /* poll_event */
57     no_get_fd,                    /* get_fd */
58     no_flush,                     /* flush */
59     no_get_file_info,             /* get_file_info */
60     atom_table_destroy            /* destroy */
61 };
62
63 static struct atom_table *global_table;
64
65
66 /* copy an atom name to a temporary area */
67 static const WCHAR *copy_name( const WCHAR *str, size_t len )
68 {
69     static WCHAR buffer[MAX_ATOM_LEN+1];
70
71     if (len > MAX_ATOM_LEN*sizeof(WCHAR))
72     {
73         set_error( STATUS_INVALID_PARAMETER );
74         return NULL;
75     }
76     memcpy( buffer, str, len );
77     buffer[len / sizeof(WCHAR)] = 0;
78     return buffer;
79 }
80
81 /* create an atom table */
82 static struct atom_table *create_table(int entries_count)
83 {
84     struct atom_table *table;
85
86     if ((table = alloc_object( &atom_table_ops, -1 )))
87     {
88         if ((entries_count < MIN_HASH_SIZE) ||
89             (entries_count > MAX_HASH_SIZE)) entries_count = HASH_SIZE;
90         table->entries_count = entries_count;
91         if (!(table->entries = malloc( sizeof(*table->entries) * table->entries_count )))
92         {
93             set_error( STATUS_NO_MEMORY );
94             goto fail;
95         }
96         memset( table->entries, 0, sizeof(*table->entries) * table->entries_count );
97         table->count = 64;
98         table->last  = -1;
99         if ((table->handles = mem_alloc( sizeof(*table->handles) * table->count )))
100             return table;
101 fail:
102         release_object( table );
103         table = NULL;
104     }
105     return table;
106 }
107
108 /* retrieve an entry pointer from its atom */
109 static struct atom_entry *get_atom_entry( struct atom_table *table, int atom )
110 {
111     struct atom_entry *entry = NULL;
112     if (table && (atom >= 0) && (atom <= table->last)) entry = table->handles[atom];
113     if (!entry) set_error( STATUS_INVALID_HANDLE );
114     return entry;
115 }
116
117 /* add an atom entry in the table and return its handle */
118 static int add_atom_entry( struct atom_table *table, struct atom_entry *entry )
119 {
120     int i;
121     for (i = 0; i <= table->last; i++)
122         if (!table->handles[i]) goto found;
123     if (i == table->count)
124     {
125         struct atom_entry **new_table = NULL;
126         int new_size = table->count + table->count / 2;
127         if (new_size > MAX_ATOMS) new_size = MAX_ATOMS;
128         if (new_size > table->count)
129             new_table = realloc( table->handles, sizeof(*table->handles) * new_size );
130         if (!new_table)
131         {
132             set_error( STATUS_NO_MEMORY );
133             return -1;
134         }
135         table->count = new_size;
136         table->handles = new_table;
137     }
138     table->last = i;
139  found:
140     table->handles[i] = entry;
141     entry->atom = i;
142     return i;
143 }
144
145 /* compute the hash code for a string */
146 static int atom_hash( struct atom_table *table, const WCHAR *str )
147 {
148     int i;
149     WCHAR hash = 0;
150     for (i = 0; str[i]; i++) hash ^= toupperW(str[i]) + i;
151     return hash % table->entries_count;
152 }
153
154 /* dump an atom table */
155 static void atom_table_dump( struct object *obj, int verbose )
156 {
157     int i;
158     struct atom_table *table = (struct atom_table *)obj;
159     assert( obj->ops == &atom_table_ops );
160
161     fprintf( stderr, "Atom table size=%d entries=%d\n",
162              table->last + 1, table->entries_count );
163     if (!verbose) return;
164     for (i = 0; i <= table->last; i++)
165     {
166         struct atom_entry *entry = table->handles[i];
167         if (!entry) continue;
168         fprintf( stderr, "  %5d: ref=%d hash=%d \"", i, entry->count, entry->hash );
169         dump_strW( entry->str, strlenW(entry->str), stderr, "\"\"");
170         fprintf( stderr, "\"\n" );
171     }
172 }
173
174 /* destroy the atom table */
175 static void atom_table_destroy( struct object *obj )
176 {
177     int i;
178     struct atom_table *table = (struct atom_table *)obj;
179     assert( obj->ops == &atom_table_ops );
180     if (table->handles)
181     {
182         for (i = 0; i <= table->last; i++) free( table->handles[i] );
183         free( table->handles );
184     }
185     if (table->entries) free( table->entries );
186 }
187
188 /* find an atom entry in its hash list */
189 static struct atom_entry *find_atom_entry( struct atom_table *table, const WCHAR *str, int hash )
190 {
191     struct atom_entry *entry = table->entries[hash];
192     while (entry)
193     {
194         if (!strcmpiW( entry->str, str )) break;
195         entry = entry->next;
196     }
197     return entry;
198 }
199
200 /* close the atom table; used on server exit */
201 void close_atom_table(void)
202 {
203     if (global_table) release_object( global_table );
204 }
205
206 /* add an atom to the table */
207 static int add_atom( struct atom_table *table, const WCHAR *str )
208 {
209     struct atom_entry *entry;
210     int hash = atom_hash( table, str );
211     int atom = -1;
212
213     if (!*str)
214     {
215         set_error( STATUS_OBJECT_NAME_INVALID );
216         return -1;
217     }
218     if ((entry = find_atom_entry( table, str, hash )))  /* exists already */
219     {
220         entry->count++;
221         return entry->atom;
222     }
223
224     if ((entry = mem_alloc( sizeof(*entry) + strlenW(str) * sizeof(WCHAR) )))
225     {
226         if ((atom = add_atom_entry( table, entry )) != -1)
227         {
228             entry->prev  = NULL;
229             if ((entry->next = table->entries[hash])) entry->next->prev = entry;
230             table->entries[hash] = entry;
231             entry->count = 1;
232             entry->hash  = hash;
233             strcpyW( entry->str, str );
234         }
235         else free( entry );
236     }
237     else set_error( STATUS_NO_MEMORY );
238     return atom;
239 }
240
241 /* delete an atom from the table */
242 static void delete_atom( struct atom_table *table, int atom )
243 {
244     struct atom_entry *entry = get_atom_entry( table, atom );
245     if (entry && !--entry->count)
246     {
247         if (entry->next) entry->next->prev = entry->prev;
248         if (entry->prev) entry->prev->next = entry->next;
249         else table->entries[entry->hash] = entry->next;
250         table->handles[atom] = NULL;
251         free( entry );
252     }
253 }
254
255 /* find an atom in the table */
256 static int find_atom( struct atom_table *table, const WCHAR *str )
257 {
258     struct atom_entry *entry;
259
260     if (table && ((entry = find_atom_entry( table, str, atom_hash(table, str) )))) return entry->atom;
261     if (!*str) set_error( STATUS_OBJECT_NAME_INVALID );
262     else set_error( STATUS_OBJECT_NAME_NOT_FOUND );
263     return -1;
264 }
265
266 /* get an atom name and refcount*/
267 static size_t get_atom_name( struct atom_table *table, int atom,
268                              WCHAR *str, size_t maxsize, int *count )
269 {
270     size_t len = 0;
271     struct atom_entry *entry = get_atom_entry( table, atom );
272     *count = -1;
273     if (entry)
274     {
275         *count = entry->count;
276         len = strlenW( entry->str ) * sizeof(WCHAR);
277         if (len <= maxsize) memcpy( str, entry->str, len );
278         else
279         {
280             set_error( STATUS_BUFFER_OVERFLOW );
281             len = 0;
282         }
283     }
284     return len;
285 }
286
287 /* add a global atom */
288 DECL_HANDLER(add_atom)
289 {
290     struct atom_table **table_ptr = req->local ? &current->process->atom_table : &global_table;
291
292     if (!*table_ptr) *table_ptr = create_table(0);
293     if (*table_ptr)
294     {
295         const WCHAR *name = copy_name( get_req_data(req), get_req_data_size(req) );
296         if (name) req->atom = add_atom( *table_ptr, name );
297     }
298 }
299
300 /* delete a global atom */
301 DECL_HANDLER(delete_atom)
302 {
303     delete_atom( req->local ? current->process->atom_table : global_table, req->atom );
304 }
305
306 /* find a global atom */
307 DECL_HANDLER(find_atom)
308 {
309     const WCHAR *name = copy_name( get_req_data(req), get_req_data_size(req) );
310     if (name)
311         req->atom = find_atom( req->local ? current->process->atom_table : global_table, name );
312 }
313
314 /* get global atom name */
315 DECL_HANDLER(get_atom_name)
316 {
317     WCHAR *name = get_req_data(req);
318     size_t size = get_atom_name( req->local ? current->process->atom_table : global_table,
319                                  req->atom, name, get_req_data_size(req), &req->count );
320     set_req_data_size( req, size );
321 }
322
323 /* init the process atom table */
324 DECL_HANDLER(init_atom_table)
325 {
326     if (!current->process->atom_table)
327         current->process->atom_table = create_table( req->entries );
328 }