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)
10 * Full rewrite 27/05/2001
16 #include "../include/debug.h"
19 /* GLOBALS *******************************************************************/
21 static PRTL_ATOM_TABLE LocalAtomTable
= NULL
;
23 static PRTL_ATOM_TABLE
GetLocalAtomTable(VOID
);
26 /* FUNCTIONS *****************************************************************/
32 GlobalAddAtomA(LPCSTR lpString
)
34 UNICODE_STRING AtomName
;
38 if (HIWORD((ULONG
)lpString
) == 0)
40 if ((ULONG
)lpString
>= 0xC000)
42 SetLastErrorByStatus(STATUS_INVALID_PARAMETER
);
45 return (ATOM
)LOWORD((ULONG
)lpString
);
48 if (lstrlenA(lpString
) > 255)
50 /* This limit does not exist with NtAddAtom so the limit is probably
51 * added for compability. -Gunnar
53 SetLastError(ERROR_INVALID_PARAMETER
);
57 RtlCreateUnicodeStringFromAsciiz(&AtomName
,
60 Status
= NtAddAtom(AtomName
.Buffer
,
63 RtlFreeUnicodeString(&AtomName
);
64 if (!NT_SUCCESS(Status
))
66 SetLastErrorByStatus(Status
);
78 GlobalAddAtomW(LPCWSTR lpString
)
83 if (HIWORD((ULONG
)lpString
) == 0)
85 if ((ULONG
)lpString
>= 0xC000)
87 SetLastErrorByStatus(STATUS_INVALID_PARAMETER
);
90 return (ATOM
)LOWORD((ULONG
)lpString
);
93 if (lstrlenW(lpString
) > 255)
95 /* This limit does not exist with NtAddAtom so the limit is probably
96 * added for compability. -Gunnar
98 SetLastError(ERROR_INVALID_PARAMETER
);
102 Status
= NtAddAtom((LPWSTR
)lpString
,
105 if (!NT_SUCCESS(Status
))
107 SetLastErrorByStatus(Status
);
119 GlobalDeleteAtom(ATOM nAtom
)
128 Status
= NtDeleteAtom(nAtom
);
129 if (!NT_SUCCESS(Status
))
131 SetLastErrorByStatus(Status
);
143 GlobalFindAtomA(LPCSTR lpString
)
145 UNICODE_STRING AtomName
;
149 if (HIWORD((ULONG
)lpString
) == 0)
151 if ((ULONG
)lpString
>= 0xC000)
153 SetLastErrorByStatus(STATUS_INVALID_PARAMETER
);
156 return (ATOM
)LOWORD((ULONG
)lpString
);
159 if (lstrlenA(lpString
) > 255)
161 /* This limit does not exist with NtAddAtom so the limit is probably
162 * added for compability. -Gunnar
164 SetLastError(ERROR_INVALID_PARAMETER
);
168 RtlCreateUnicodeStringFromAsciiz(&AtomName
,
170 Status
= NtFindAtom(AtomName
.Buffer
,
173 RtlFreeUnicodeString(&AtomName
);
174 if (!NT_SUCCESS(Status
))
176 SetLastErrorByStatus(Status
);
188 GlobalFindAtomW(LPCWSTR lpString
)
193 if (HIWORD((ULONG
)lpString
) == 0)
195 if ((ULONG
)lpString
>= 0xC000)
197 SetLastErrorByStatus(STATUS_INVALID_PARAMETER
);
200 return (ATOM
)LOWORD((ULONG
)lpString
);
203 if (lstrlenW(lpString
) > 255)
205 /* This limit does not exist with NtAddAtom so the limit is probably
206 * added for compability. -Gunnar
208 SetLastError(ERROR_INVALID_PARAMETER
);
212 Status
= NtFindAtom((LPWSTR
)lpString
,
215 if (!NT_SUCCESS(Status
))
217 SetLastErrorByStatus(Status
);
226 GlobalGetAtomNameA(ATOM nAtom
,
230 PATOM_BASIC_INFORMATION Buffer
;
231 UNICODE_STRING AtomNameU
;
232 ANSI_STRING AtomName
;
237 BufferSize
= sizeof(ATOM_BASIC_INFORMATION
) + nSize
* sizeof(WCHAR
);
238 Buffer
= RtlAllocateHeap(RtlGetProcessHeap(),
243 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
247 Status
= NtQueryInformationAtom(nAtom
,
248 AtomBasicInformation
,
252 if (!NT_SUCCESS(Status
))
254 RtlFreeHeap(RtlGetProcessHeap(),
257 SetLastErrorByStatus(Status
);
261 RtlInitUnicodeString(&AtomNameU
,
263 AtomName
.Buffer
= lpBuffer
;
265 AtomName
.MaximumLength
= nSize
;
266 RtlUnicodeStringToAnsiString(&AtomName
,
270 ReturnLength
= AtomName
.Length
;
271 RtlFreeHeap(RtlGetProcessHeap(),
283 GlobalGetAtomNameW(ATOM nAtom
,
287 PATOM_BASIC_INFORMATION Buffer
;
292 BufferSize
= sizeof(ATOM_BASIC_INFORMATION
) + nSize
* sizeof(WCHAR
);
293 Buffer
= RtlAllocateHeap(RtlGetProcessHeap(),
298 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
302 Status
= NtQueryInformationAtom(nAtom
,
303 AtomBasicInformation
,
307 if (!NT_SUCCESS(Status
))
309 RtlFreeHeap(RtlGetProcessHeap(),
312 SetLastErrorByStatus(Status
);
316 memcpy(lpBuffer
, Buffer
->Name
, Buffer
->NameLength
);
317 ReturnLength
= Buffer
->NameLength
/ sizeof(WCHAR
);
318 *(lpBuffer
+ ReturnLength
) = 0;
319 RtlFreeHeap(RtlGetProcessHeap(),
327 static PRTL_ATOM_TABLE
328 GetLocalAtomTable(VOID
)
330 if (LocalAtomTable
!= NULL
)
332 return LocalAtomTable
;
334 RtlCreateAtomTable(37,
336 return LocalAtomTable
;
344 InitAtomTable(DWORD nSize
)
348 /* nSize should be a prime number */
350 if ( nSize
< 4 || nSize
>= 512 )
355 if (LocalAtomTable
== NULL
)
357 Status
= RtlCreateAtomTable(nSize
,
359 if (!NT_SUCCESS(Status
))
361 SetLastErrorByStatus(Status
);
374 AddAtomA(LPCSTR lpString
)
376 PRTL_ATOM_TABLE AtomTable
;
377 UNICODE_STRING AtomName
;
381 if (HIWORD((ULONG
)lpString
) == 0)
383 if ((ULONG
)lpString
>= 0xC000)
385 SetLastErrorByStatus(STATUS_INVALID_PARAMETER
);
388 return (ATOM
)LOWORD((ULONG
)lpString
);
391 AtomTable
= GetLocalAtomTable();
393 RtlCreateUnicodeStringFromAsciiz(&AtomName
,
396 Status
= RtlAddAtomToAtomTable(AtomTable
,
399 RtlFreeUnicodeString(&AtomName
);
400 if (!NT_SUCCESS(Status
))
402 SetLastErrorByStatus(Status
);
414 AddAtomW(LPCWSTR lpString
)
416 PRTL_ATOM_TABLE AtomTable
;
420 if (HIWORD((ULONG
)lpString
) == 0)
422 if ((ULONG
)lpString
>= 0xC000)
424 SetLastErrorByStatus(STATUS_INVALID_PARAMETER
);
427 return (ATOM
)LOWORD((ULONG
)lpString
);
430 AtomTable
= GetLocalAtomTable();
432 Status
= RtlAddAtomToAtomTable(AtomTable
,
435 if (!NT_SUCCESS(Status
))
437 SetLastErrorByStatus(Status
);
449 DeleteAtom(ATOM nAtom
)
451 PRTL_ATOM_TABLE AtomTable
;
459 AtomTable
= GetLocalAtomTable();
461 Status
= RtlDeleteAtomFromAtomTable(AtomTable
,
463 if (!NT_SUCCESS(Status
))
465 SetLastErrorByStatus(Status
);
477 FindAtomA(LPCSTR lpString
)
479 PRTL_ATOM_TABLE AtomTable
;
480 UNICODE_STRING AtomName
;
484 if (HIWORD((ULONG
)lpString
) == 0)
486 if ((ULONG
)lpString
>= 0xC000)
488 SetLastErrorByStatus(STATUS_INVALID_PARAMETER
);
491 return (ATOM
)LOWORD((ULONG
)lpString
);
494 AtomTable
= GetLocalAtomTable();
495 RtlCreateUnicodeStringFromAsciiz(&AtomName
,
497 Status
= RtlLookupAtomInAtomTable(AtomTable
,
500 RtlFreeUnicodeString(&AtomName
);
501 if (!NT_SUCCESS(Status
))
503 SetLastErrorByStatus(Status
);
515 FindAtomW(LPCWSTR lpString
)
517 PRTL_ATOM_TABLE AtomTable
;
521 if (HIWORD((ULONG
)lpString
) == 0)
523 if ((ULONG
)lpString
>= 0xC000)
525 SetLastErrorByStatus(STATUS_INVALID_PARAMETER
);
528 return (ATOM
)LOWORD((ULONG
)lpString
);
531 AtomTable
= GetLocalAtomTable();
533 Status
= RtlLookupAtomInAtomTable(AtomTable
,
536 if (!NT_SUCCESS(Status
))
538 SetLastErrorByStatus(Status
);
550 GetAtomNameA(ATOM nAtom
,
554 PRTL_ATOM_TABLE AtomTable
;
556 UNICODE_STRING AtomNameU
;
557 ANSI_STRING AtomName
;
561 AtomTable
= GetLocalAtomTable();
563 NameLength
= nSize
* sizeof(WCHAR
);
564 Buffer
= RtlAllocateHeap(RtlGetProcessHeap(),
569 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
573 Status
= RtlQueryAtomInAtomTable(AtomTable
,
579 if (!NT_SUCCESS(Status
))
581 RtlFreeHeap(RtlGetProcessHeap(),
584 SetLastErrorByStatus(Status
);
588 RtlInitUnicodeString(&AtomNameU
,
590 AtomName
.Buffer
= lpBuffer
;
592 AtomName
.MaximumLength
= nSize
;
593 RtlUnicodeStringToAnsiString(&AtomName
,
597 NameLength
= AtomName
.Length
;
598 RtlFreeHeap(RtlGetProcessHeap(),
610 GetAtomNameW(ATOM nAtom
,
614 PRTL_ATOM_TABLE AtomTable
;
618 AtomTable
= GetLocalAtomTable();
620 NameLength
= nSize
* sizeof(WCHAR
);
621 Status
= RtlQueryAtomInAtomTable(AtomTable
,
627 if (!NT_SUCCESS(Status
))
632 return(NameLength
/ sizeof(WCHAR
));