Reverted latest changes.
[reactos.git] / reactos / lib / kernel32 / misc / atom.c
1 /* $Id: atom.c,v 1.15 2002/09/08 10:22:43 chorns Exp $
2 *
3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS system libraries
5 * FILE: lib/kernel32/misc/atom.c
6 * PURPOSE: Atom functions
7 * PROGRAMMER: Eric Kohl ( ariadne@xs4all.nl)
8 * UPDATE HISTORY:
9 * Created 01/11/98
10 * Full rewrite 27/05/2001
11 */
12
13 #include <ddk/ntddk.h>
14 #include <windows.h>
15 #include <kernel32/error.h>
16
17 #define NDEBUG
18 #include <kernel32/kernel32.h>
19
20
21 /* GLOBALS *******************************************************************/
22
23 static PRTL_ATOM_TABLE LocalAtomTable = NULL;
24
25 static PRTL_ATOM_TABLE GetLocalAtomTable(VOID);
26
27
28 /* FUNCTIONS *****************************************************************/
29
30 ATOM STDCALL
31 GlobalAddAtomA(LPCSTR lpString)
32 {
33 UNICODE_STRING AtomName;
34 NTSTATUS Status;
35 ATOM Atom;
36
37 if (HIWORD((ULONG)lpString) == 0)
38 {
39 if ((ULONG)lpString >= 0xC000)
40 {
41 SetLastErrorByStatus(STATUS_INVALID_PARAMETER);
42 return (ATOM)0;
43 }
44 return (ATOM)LOWORD((ULONG)lpString);
45 }
46
47 RtlCreateUnicodeStringFromAsciiz(&AtomName,
48 (LPSTR)lpString);
49
50 Status = NtAddAtom(AtomName.Buffer,
51 &Atom);
52 RtlFreeUnicodeString(&AtomName);
53 if (!NT_SUCCESS(Status))
54 {
55 SetLastErrorByStatus(Status);
56 return (ATOM)0;
57 }
58
59 return Atom;
60 }
61
62
63 ATOM STDCALL
64 GlobalAddAtomW(LPCWSTR lpString)
65 {
66 ATOM Atom;
67 NTSTATUS Status;
68
69 if (HIWORD((ULONG)lpString) == 0)
70 {
71 if ((ULONG)lpString >= 0xC000)
72 {
73 SetLastErrorByStatus(STATUS_INVALID_PARAMETER);
74 return (ATOM)0;
75 }
76 return (ATOM)LOWORD((ULONG)lpString);
77 }
78
79 Status = NtAddAtom((LPWSTR)lpString,
80 &Atom);
81 if (!NT_SUCCESS(Status))
82 {
83 SetLastErrorByStatus(Status);
84 return (ATOM)0;
85 }
86
87 return Atom;
88 }
89
90
91 ATOM STDCALL
92 GlobalDeleteAtom(ATOM nAtom)
93 {
94 NTSTATUS Status;
95
96 if (nAtom < 0xC000)
97 {
98 return 0;
99 }
100
101 Status = NtDeleteAtom(nAtom);
102 if (!NT_SUCCESS(Status))
103 {
104 SetLastErrorByStatus(Status);
105 return nAtom;
106 }
107
108 return 0;
109 }
110
111
112 ATOM STDCALL
113 GlobalFindAtomA(LPCSTR lpString)
114 {
115 UNICODE_STRING AtomName;
116 NTSTATUS Status;
117 ATOM Atom;
118
119 if (HIWORD((ULONG)lpString) == 0)
120 {
121 if ((ULONG)lpString >= 0xC000)
122 {
123 SetLastErrorByStatus(STATUS_INVALID_PARAMETER);
124 return (ATOM)0;
125 }
126 return (ATOM)LOWORD((ULONG)lpString);
127 }
128
129 RtlCreateUnicodeStringFromAsciiz(&AtomName,
130 (LPSTR)lpString);
131 Status = NtFindAtom(AtomName.Buffer,
132 &Atom);
133 RtlFreeUnicodeString(&AtomName);
134 if (!NT_SUCCESS(Status))
135 {
136 SetLastErrorByStatus(Status);
137 return (ATOM)0;
138 }
139
140 return Atom;
141 }
142
143
144 ATOM STDCALL
145 GlobalFindAtomW(LPCWSTR lpString)
146 {
147 ATOM Atom;
148 NTSTATUS Status;
149
150 if (HIWORD((ULONG)lpString) == 0)
151 {
152 if ((ULONG)lpString >= 0xC000)
153 {
154 SetLastErrorByStatus(STATUS_INVALID_PARAMETER);
155 return (ATOM)0;
156 }
157 return (ATOM)LOWORD((ULONG)lpString);
158 }
159
160 Status = NtFindAtom((LPWSTR)lpString,
161 &Atom);
162 if (!NT_SUCCESS(Status))
163 {
164 SetLastErrorByStatus(Status);
165 return (ATOM)0;
166 }
167
168 return Atom;
169 }
170
171
172 UINT STDCALL
173 GlobalGetAtomNameA(ATOM nAtom,
174 LPSTR lpBuffer,
175 int nSize)
176 {
177 PATOM_BASIC_INFORMATION Buffer;
178 UNICODE_STRING AtomNameU;
179 ANSI_STRING AtomName;
180 ULONG BufferSize;
181 ULONG ReturnLength;
182 NTSTATUS Status;
183
184 BufferSize = sizeof(ATOM_BASIC_INFORMATION) + nSize * sizeof(WCHAR);
185 Buffer = RtlAllocateHeap(RtlGetProcessHeap(),
186 HEAP_ZERO_MEMORY,
187 BufferSize);
188
189 Status = NtQueryInformationAtom(nAtom,
190 AtomBasicInformation,
191 (PVOID)&Buffer,
192 BufferSize,
193 &ReturnLength);
194 if (!NT_SUCCESS(Status))
195 {
196 RtlFreeHeap(RtlGetProcessHeap(),
197 0,
198 Buffer);
199 return 0;
200 }
201
202 RtlInitUnicodeString(&AtomNameU,
203 Buffer->Name);
204 AtomName.Buffer = lpBuffer;
205 AtomName.Length = 0;
206 AtomName.MaximumLength = nSize;
207 RtlUnicodeStringToAnsiString(&AtomName,
208 &AtomNameU,
209 FALSE);
210
211 ReturnLength = AtomName.Length;
212 RtlFreeHeap(RtlGetProcessHeap(),
213 0,
214 Buffer);
215
216 return ReturnLength;
217 }
218
219
220 UINT STDCALL
221 GlobalGetAtomNameW(ATOM nAtom,
222 LPWSTR lpBuffer,
223 int nSize)
224 {
225 PATOM_BASIC_INFORMATION Buffer;
226 ULONG BufferSize;
227 ULONG ReturnLength;
228 NTSTATUS Status;
229
230 BufferSize = sizeof(ATOM_BASIC_INFORMATION) + nSize * sizeof(WCHAR);
231 Buffer = RtlAllocateHeap(RtlGetProcessHeap(),
232 HEAP_ZERO_MEMORY,
233 BufferSize);
234
235 Status = NtQueryInformationAtom(nAtom,
236 AtomBasicInformation,
237 (PVOID)&Buffer,
238 BufferSize,
239 &ReturnLength);
240 if (!NT_SUCCESS(Status))
241 {
242 RtlFreeHeap(RtlGetProcessHeap(),
243 0,
244 Buffer);
245 return 0;
246 }
247
248 wcscpy(lpBuffer, Buffer->Name);
249 ReturnLength = Buffer->NameLength / sizeof(WCHAR);
250 RtlFreeHeap(RtlGetProcessHeap(),
251 0,
252 Buffer);
253
254 return ReturnLength;
255 }
256
257
258 static PRTL_ATOM_TABLE
259 GetLocalAtomTable(VOID)
260 {
261 if (LocalAtomTable != NULL)
262 {
263 return LocalAtomTable;
264 }
265 RtlCreateAtomTable(37,
266 &LocalAtomTable);
267 return LocalAtomTable;
268 }
269
270
271 BOOL STDCALL
272 InitAtomTable(DWORD nSize)
273 {
274 NTSTATUS Status;
275
276 /* nSize should be a prime number */
277
278 if ( nSize < 4 || nSize >= 512 )
279 {
280 nSize = 37;
281 }
282
283 if (LocalAtomTable == NULL)
284 {
285 Status = RtlCreateAtomTable(nSize,
286 &LocalAtomTable);
287 if (!NT_SUCCESS(Status))
288 {
289 SetLastErrorByStatus(Status);
290 return FALSE;
291 }
292 }
293
294 return TRUE;
295 }
296
297
298 ATOM STDCALL
299 AddAtomA(LPCSTR lpString)
300 {
301 PRTL_ATOM_TABLE AtomTable;
302 UNICODE_STRING AtomName;
303 NTSTATUS Status;
304 ATOM Atom;
305
306 if (HIWORD((ULONG)lpString) == 0)
307 {
308 if ((ULONG)lpString >= 0xC000)
309 {
310 SetLastErrorByStatus(STATUS_INVALID_PARAMETER);
311 return (ATOM)0;
312 }
313 return (ATOM)LOWORD((ULONG)lpString);
314 }
315
316 AtomTable = GetLocalAtomTable();
317
318 RtlCreateUnicodeStringFromAsciiz(&AtomName,
319 (LPSTR)lpString);
320
321 Status = RtlAddAtomToAtomTable(AtomTable,
322 AtomName.Buffer,
323 &Atom);
324 RtlFreeUnicodeString(&AtomName);
325 if (!NT_SUCCESS(Status))
326 {
327 SetLastErrorByStatus(Status);
328 return (ATOM)0;
329 }
330
331 return Atom;
332 }
333
334
335 ATOM STDCALL
336 AddAtomW(LPCWSTR lpString)
337 {
338 PRTL_ATOM_TABLE AtomTable;
339 ATOM Atom;
340 NTSTATUS Status;
341
342 if (HIWORD((ULONG)lpString) == 0)
343 {
344 if ((ULONG)lpString >= 0xC000)
345 {
346 SetLastErrorByStatus(STATUS_INVALID_PARAMETER);
347 return (ATOM)0;
348 }
349 return (ATOM)LOWORD((ULONG)lpString);
350 }
351
352 AtomTable = GetLocalAtomTable();
353
354 Status = RtlAddAtomToAtomTable(AtomTable,
355 (LPWSTR)lpString,
356 &Atom);
357 if (!NT_SUCCESS(Status))
358 {
359 SetLastErrorByStatus(Status);
360 return (ATOM)0;
361 }
362
363 return Atom;
364 }
365
366
367 ATOM STDCALL
368 DeleteAtom(ATOM nAtom)
369 {
370 PRTL_ATOM_TABLE AtomTable;
371 NTSTATUS Status;
372
373 if (nAtom < 0xC000)
374 {
375 return 0;
376 }
377
378 AtomTable = GetLocalAtomTable();
379
380 Status = RtlDeleteAtomFromAtomTable(AtomTable,
381 nAtom);
382 if (!NT_SUCCESS(Status))
383 {
384 SetLastErrorByStatus(Status);
385 return nAtom;
386 }
387
388 return 0;
389 }
390
391
392 ATOM STDCALL
393 FindAtomA(LPCSTR lpString)
394 {
395 PRTL_ATOM_TABLE AtomTable;
396 UNICODE_STRING AtomName;
397 NTSTATUS Status;
398 ATOM Atom;
399
400 if (HIWORD((ULONG)lpString) == 0)
401 {
402 if ((ULONG)lpString >= 0xC000)
403 {
404 SetLastErrorByStatus(STATUS_INVALID_PARAMETER);
405 return (ATOM)0;
406 }
407 return (ATOM)LOWORD((ULONG)lpString);
408 }
409
410 AtomTable = GetLocalAtomTable();
411 RtlCreateUnicodeStringFromAsciiz(&AtomName,
412 (LPSTR)lpString);
413 Status = RtlLookupAtomInAtomTable(AtomTable,
414 AtomName.Buffer,
415 &Atom);
416 RtlFreeUnicodeString(&AtomName);
417 if (!NT_SUCCESS(Status))
418 {
419 SetLastErrorByStatus(Status);
420 return (ATOM)0;
421 }
422
423 return Atom;
424 }
425
426
427 ATOM STDCALL
428 FindAtomW(LPCWSTR lpString)
429 {
430 PRTL_ATOM_TABLE AtomTable;
431 ATOM Atom;
432 NTSTATUS Status;
433
434 if (HIWORD((ULONG)lpString) == 0)
435 {
436 if ((ULONG)lpString >= 0xC000)
437 {
438 SetLastErrorByStatus(STATUS_INVALID_PARAMETER);
439 return (ATOM)0;
440 }
441 return (ATOM)LOWORD((ULONG)lpString);
442 }
443
444 AtomTable = GetLocalAtomTable();
445
446 Status = RtlLookupAtomInAtomTable(AtomTable,
447 (LPWSTR)lpString,
448 &Atom);
449 if (!NT_SUCCESS(Status))
450 {
451 SetLastErrorByStatus(Status);
452 return (ATOM)0;
453 }
454
455 return Atom;
456 }
457
458
459 UINT STDCALL
460 GetAtomNameA(ATOM nAtom,
461 LPSTR lpBuffer,
462 int nSize)
463 {
464 PRTL_ATOM_TABLE AtomTable;
465 PWCHAR Buffer;
466 UNICODE_STRING AtomNameU;
467 ANSI_STRING AtomName;
468 ULONG NameLength;
469 NTSTATUS Status;
470
471 AtomTable = GetLocalAtomTable();
472
473 NameLength = nSize * sizeof(WCHAR);
474 Buffer = RtlAllocateHeap(RtlGetProcessHeap(),
475 HEAP_ZERO_MEMORY,
476 NameLength);
477
478 Status = RtlQueryAtomInAtomTable(AtomTable,
479 nAtom,
480 NULL,
481 NULL,
482 Buffer,
483 &NameLength);
484 if (!NT_SUCCESS(Status))
485 {
486 RtlFreeHeap(RtlGetProcessHeap(),
487 0,
488 Buffer);
489 return 0;
490 }
491
492 RtlInitUnicodeString(&AtomNameU,
493 Buffer);
494 AtomName.Buffer = lpBuffer;
495 AtomName.Length = 0;
496 AtomName.MaximumLength = nSize;
497 RtlUnicodeStringToAnsiString(&AtomName,
498 &AtomNameU,
499 FALSE);
500
501 NameLength = AtomName.Length;
502 RtlFreeHeap(RtlGetProcessHeap(),
503 0,
504 Buffer);
505
506 return NameLength;
507 }
508
509
510 UINT STDCALL
511 GetAtomNameW(ATOM nAtom,
512 LPWSTR lpBuffer,
513 int nSize)
514 {
515 PRTL_ATOM_TABLE AtomTable;
516 ULONG NameLength;
517 NTSTATUS Status;
518
519 AtomTable = GetLocalAtomTable();
520
521 NameLength = nSize * sizeof(WCHAR);
522 Status = RtlQueryAtomInAtomTable(AtomTable,
523 nAtom,
524 NULL,
525 NULL,
526 lpBuffer,
527 &NameLength);
528 if (!NT_SUCCESS(Status))
529 {
530 return 0;
531 }
532
533 return(NameLength / sizeof(WCHAR));
534 }
535
536 /* EOF */