4 * Copyright 1993, 1994, 1995 Alexandre Julliard
5 * Copyright 2004,2005 Eric Pouech
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.
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.
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
23 #include "wine/port.h"
32 #define WIN32_NO_STATUS
35 #include "wine/server.h"
36 #include "wine/unicode.h"
38 #include "wine/debug.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(atom);
42 #define MAX_ATOM_LEN 255
43 #define IS_INTATOM(x) (((ULONG_PTR)(x) >> 16) == 0)
45 /******************************************************************
47 * Returns STATUS_SUCCESS if integral atom and 'pAtom' is filled
48 * STATUS_INVALID_PARAMETER if 'atomstr' is too long
49 * STATUS_MORE_ENTRIES otherwise
51 static NTSTATUS is_integral_atom( LPCWSTR atomstr, size_t len, RTL_ATOM* pAtom )
55 if (!IS_INTATOM( atomstr ))
57 const WCHAR* ptr = atomstr;
58 if (!len) return STATUS_OBJECT_NAME_INVALID;
63 while (ptr < atomstr + len && *ptr >= '0' && *ptr <= '9')
65 atom = atom * 10 + *ptr++ - '0';
67 if (ptr > atomstr + 1 && ptr == atomstr + len) goto done;
69 if (len > MAX_ATOM_LEN) return STATUS_INVALID_PARAMETER;
70 return STATUS_MORE_ENTRIES;
72 else atom = LOWORD( atomstr );
74 if (!atom || atom >= MAXINTATOM) return STATUS_INVALID_PARAMETER;
76 return STATUS_SUCCESS;
79 /******************************************************************
80 * RtlDeleteAtomFromAtomTable (NTDLL.@)
82 NTSTATUS WINAPI RtlDeleteAtomFromAtomTable( RTL_ATOM_TABLE table, RTL_ATOM atom )
86 TRACE( "%p %x\n", table, atom );
87 if (!table) status = STATUS_INVALID_PARAMETER;
90 SERVER_START_REQ( delete_atom )
93 req->table = wine_server_obj_handle( table );
94 status = wine_server_call( req );
101 /******************************************************************
102 * integral_atom_name (internal)
104 * Helper for fetching integral (local/global) atoms names.
106 static ULONG integral_atom_name(WCHAR* buffer, ULONG len, RTL_ATOM atom)
108 static const WCHAR fmt[] = {'#','%','u',0};
112 ret = sprintfW( tmp, fmt, atom );
113 if (!len) return ret * sizeof(WCHAR);
114 if (len <= ret) ret = len - 1;
115 memcpy( buffer, tmp, ret * sizeof(WCHAR) );
117 return ret * sizeof(WCHAR);
120 /******************************************************************
121 * RtlQueryAtomInAtomTable (NTDLL.@)
123 NTSTATUS WINAPI RtlQueryAtomInAtomTable( RTL_ATOM_TABLE table, RTL_ATOM atom, ULONG* ref,
124 ULONG* pin, WCHAR* name, ULONG* len )
126 NTSTATUS status = STATUS_SUCCESS;
129 if (!table) status = STATUS_INVALID_PARAMETER;
130 else if (atom < MAXINTATOM)
132 if (!atom) return STATUS_INVALID_PARAMETER;
133 if (len) wlen = integral_atom_name( name, *len, atom);
139 SERVER_START_REQ( get_atom_information )
142 req->table = wine_server_obj_handle( table );
143 if (len && *len && name)
144 wine_server_set_reply( req, name, *len );
145 status = wine_server_call( req );
146 if (status == STATUS_SUCCESS)
149 if (ref) *ref = reply->count;
150 if (pin) *pin = reply->pinned;
155 if (status == STATUS_SUCCESS && len)
159 wlen = min( *len - sizeof(WCHAR), wlen );
160 if (name) name[wlen / sizeof(WCHAR)] = 0;
162 else status = STATUS_BUFFER_TOO_SMALL;
166 TRACE( "%p %x -> %s (%x)\n",
167 table, atom, len ? debugstr_wn(name, wlen / sizeof(WCHAR)) : NULL, status );
171 /******************************************************************
172 * RtlCreateAtomTable (NTDLL.@)
174 NTSTATUS WINAPI RtlCreateAtomTable( ULONG size, RTL_ATOM_TABLE* table )
180 if (size) status = STATUS_INVALID_PARAMETER;
181 else status = STATUS_SUCCESS;
185 SERVER_START_REQ( init_atom_table )
188 status = wine_server_call( req );
189 *table = wine_server_ptr_handle( reply->table );
196 /******************************************************************
197 * RtlDestroyAtomTable (NTDLL.@)
199 NTSTATUS WINAPI RtlDestroyAtomTable( RTL_ATOM_TABLE table )
201 if (!table) return STATUS_INVALID_PARAMETER;
202 return NtClose( table );
205 /******************************************************************
206 * RtlAddAtomToAtomTable (NTDLL.@)
208 NTSTATUS WINAPI RtlAddAtomToAtomTable( RTL_ATOM_TABLE table, const WCHAR* name, RTL_ATOM* atom )
212 if (!table) status = STATUS_INVALID_PARAMETER;
215 size_t len = HIWORD(name) ? strlenW(name) : 0;
216 status = is_integral_atom( name, len, atom );
217 if (status == STATUS_MORE_ENTRIES)
219 SERVER_START_REQ( add_atom )
221 wine_server_add_data( req, name, len * sizeof(WCHAR) );
222 req->table = wine_server_obj_handle( table );
223 status = wine_server_call( req );
229 TRACE( "%p %s -> %x\n",
230 table, debugstr_w(name), status == STATUS_SUCCESS ? *atom : 0 );
235 /******************************************************************
236 * RtlLookupAtomInAtomTable (NTDLL.@)
238 NTSTATUS WINAPI RtlLookupAtomInAtomTable( RTL_ATOM_TABLE table, const WCHAR* name, RTL_ATOM* atom )
242 if (!table) status = STATUS_INVALID_PARAMETER;
245 size_t len = HIWORD(name) ? strlenW(name) : 0;
246 status = is_integral_atom( name, len, atom );
247 if (status == STATUS_MORE_ENTRIES)
249 SERVER_START_REQ( find_atom )
251 wine_server_add_data( req, name, len * sizeof(WCHAR) );
252 req->table = wine_server_obj_handle( table );
253 status = wine_server_call( req );
259 TRACE( "%p %s -> %x\n",
260 table, debugstr_w(name), status == STATUS_SUCCESS ? *atom : 0 );
264 /******************************************************************
265 * RtlEmptyAtomTable (NTDLL.@)
267 NTSTATUS WINAPI RtlEmptyAtomTable( RTL_ATOM_TABLE table, BOOLEAN delete_pinned )
271 if (!table) status = STATUS_INVALID_PARAMETER;
274 SERVER_START_REQ( empty_atom_table )
276 req->table = wine_server_obj_handle( table );
277 req->if_pinned = delete_pinned;
278 status = wine_server_call( req );
285 /******************************************************************
286 * RtlPinAtomInAtomTable (NTDLL.@)
288 NTSTATUS WINAPI RtlPinAtomInAtomTable( RTL_ATOM_TABLE table, RTL_ATOM atom )
292 if (!table) return STATUS_INVALID_PARAMETER;
293 if (atom < MAXINTATOM) return STATUS_SUCCESS;
295 SERVER_START_REQ( set_atom_information )
297 req->table = wine_server_obj_handle( table );
300 status = wine_server_call( req );
307 /*************************************************
308 * Global handle table management
309 *************************************************/
311 /******************************************************************
312 * NtAddAtom (NTDLL.@)
314 NTSTATUS WINAPI NtAddAtom( const WCHAR* name, ULONG length, RTL_ATOM* atom )
318 status = is_integral_atom( name, length / sizeof(WCHAR), atom );
319 if (status == STATUS_MORE_ENTRIES)
321 SERVER_START_REQ( add_atom )
323 wine_server_add_data( req, name, length );
325 status = wine_server_call( req );
331 debugstr_wn(name, length/sizeof(WCHAR)), status == STATUS_SUCCESS ? *atom : 0 );
335 /******************************************************************
336 * NtDeleteAtom (NTDLL.@)
338 NTSTATUS WINAPI NtDeleteAtom(RTL_ATOM atom)
342 SERVER_START_REQ( delete_atom )
346 status = wine_server_call( req );
352 /******************************************************************
353 * NtFindAtom (NTDLL.@)
355 NTSTATUS WINAPI NtFindAtom( const WCHAR* name, ULONG length, RTL_ATOM* atom )
359 status = is_integral_atom( name, length / sizeof(WCHAR), atom );
360 if (status == STATUS_MORE_ENTRIES)
362 SERVER_START_REQ( find_atom )
364 wine_server_add_data( req, name, length );
366 status = wine_server_call( req );
372 debugstr_wn(name, length/sizeof(WCHAR)), status == STATUS_SUCCESS ? *atom : 0 );
376 /******************************************************************
377 * NtQueryInformationAtom (NTDLL.@)
379 NTSTATUS WINAPI NtQueryInformationAtom( RTL_ATOM atom, ATOM_INFORMATION_CLASS class,
380 PVOID ptr, ULONG size, PULONG psize )
386 case AtomBasicInformation:
389 ATOM_BASIC_INFORMATION* abi = ptr;
391 if (size < sizeof(ATOM_BASIC_INFORMATION))
392 return STATUS_INVALID_PARAMETER;
393 name_len = size - sizeof(ATOM_BASIC_INFORMATION);
395 if (atom < MAXINTATOM)
399 abi->NameLength = integral_atom_name( abi->Name, name_len, atom );
400 status = (name_len) ? STATUS_SUCCESS : STATUS_BUFFER_TOO_SMALL;
401 abi->ReferenceCount = 1;
404 else status = STATUS_INVALID_PARAMETER;
408 SERVER_START_REQ( get_atom_information )
412 if (name_len) wine_server_set_reply( req, abi->Name, name_len );
413 status = wine_server_call( req );
414 if (status == STATUS_SUCCESS)
416 name_len = wine_server_reply_size( reply );
419 abi->NameLength = name_len;
420 abi->Name[name_len / sizeof(WCHAR)] = '\0';
424 name_len = reply->total;
425 abi->NameLength = name_len;
426 status = STATUS_BUFFER_TOO_SMALL;
428 abi->ReferenceCount = reply->count;
429 abi->Pinned = reply->pinned;
435 TRACE( "%x -> %s (%u)\n",
436 atom, debugstr_wn(abi->Name, abi->NameLength / sizeof(WCHAR)),
439 *psize = sizeof(ATOM_BASIC_INFORMATION) + name_len;
443 FIXME( "Unsupported class %u\n", class );
444 status = STATUS_INVALID_INFO_CLASS;