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(),
242 Status
= NtQueryInformationAtom(nAtom
,
243 AtomBasicInformation
,
247 if (!NT_SUCCESS(Status
))
249 RtlFreeHeap(RtlGetProcessHeap(),
255 RtlInitUnicodeString(&AtomNameU
,
257 AtomName
.Buffer
= lpBuffer
;
259 AtomName
.MaximumLength
= nSize
;
260 RtlUnicodeStringToAnsiString(&AtomName
,
264 ReturnLength
= AtomName
.Length
;
265 RtlFreeHeap(RtlGetProcessHeap(),
277 GlobalGetAtomNameW(ATOM nAtom
,
281 PATOM_BASIC_INFORMATION Buffer
;
286 BufferSize
= sizeof(ATOM_BASIC_INFORMATION
) + nSize
* sizeof(WCHAR
);
287 Buffer
= RtlAllocateHeap(RtlGetProcessHeap(),
291 Status
= NtQueryInformationAtom(nAtom
,
292 AtomBasicInformation
,
296 if (!NT_SUCCESS(Status
))
298 RtlFreeHeap(RtlGetProcessHeap(),
304 memcpy(lpBuffer
, Buffer
->Name
, Buffer
->NameLength
);
305 ReturnLength
= Buffer
->NameLength
/ sizeof(WCHAR
);
306 *(lpBuffer
+ ReturnLength
) = 0;
307 RtlFreeHeap(RtlGetProcessHeap(),
315 static PRTL_ATOM_TABLE
316 GetLocalAtomTable(VOID
)
318 if (LocalAtomTable
!= NULL
)
320 return LocalAtomTable
;
322 RtlCreateAtomTable(37,
324 return LocalAtomTable
;
332 InitAtomTable(DWORD nSize
)
336 /* nSize should be a prime number */
338 if ( nSize
< 4 || nSize
>= 512 )
343 if (LocalAtomTable
== NULL
)
345 Status
= RtlCreateAtomTable(nSize
,
347 if (!NT_SUCCESS(Status
))
349 SetLastErrorByStatus(Status
);
362 AddAtomA(LPCSTR lpString
)
364 PRTL_ATOM_TABLE AtomTable
;
365 UNICODE_STRING AtomName
;
369 if (HIWORD((ULONG
)lpString
) == 0)
371 if ((ULONG
)lpString
>= 0xC000)
373 SetLastErrorByStatus(STATUS_INVALID_PARAMETER
);
376 return (ATOM
)LOWORD((ULONG
)lpString
);
379 AtomTable
= GetLocalAtomTable();
381 RtlCreateUnicodeStringFromAsciiz(&AtomName
,
384 Status
= RtlAddAtomToAtomTable(AtomTable
,
387 RtlFreeUnicodeString(&AtomName
);
388 if (!NT_SUCCESS(Status
))
390 SetLastErrorByStatus(Status
);
402 AddAtomW(LPCWSTR lpString
)
404 PRTL_ATOM_TABLE AtomTable
;
408 if (HIWORD((ULONG
)lpString
) == 0)
410 if ((ULONG
)lpString
>= 0xC000)
412 SetLastErrorByStatus(STATUS_INVALID_PARAMETER
);
415 return (ATOM
)LOWORD((ULONG
)lpString
);
418 AtomTable
= GetLocalAtomTable();
420 Status
= RtlAddAtomToAtomTable(AtomTable
,
423 if (!NT_SUCCESS(Status
))
425 SetLastErrorByStatus(Status
);
437 DeleteAtom(ATOM nAtom
)
439 PRTL_ATOM_TABLE AtomTable
;
447 AtomTable
= GetLocalAtomTable();
449 Status
= RtlDeleteAtomFromAtomTable(AtomTable
,
451 if (!NT_SUCCESS(Status
))
453 SetLastErrorByStatus(Status
);
465 FindAtomA(LPCSTR lpString
)
467 PRTL_ATOM_TABLE AtomTable
;
468 UNICODE_STRING AtomName
;
472 if (HIWORD((ULONG
)lpString
) == 0)
474 if ((ULONG
)lpString
>= 0xC000)
476 SetLastErrorByStatus(STATUS_INVALID_PARAMETER
);
479 return (ATOM
)LOWORD((ULONG
)lpString
);
482 AtomTable
= GetLocalAtomTable();
483 RtlCreateUnicodeStringFromAsciiz(&AtomName
,
485 Status
= RtlLookupAtomInAtomTable(AtomTable
,
488 RtlFreeUnicodeString(&AtomName
);
489 if (!NT_SUCCESS(Status
))
491 SetLastErrorByStatus(Status
);
503 FindAtomW(LPCWSTR lpString
)
505 PRTL_ATOM_TABLE AtomTable
;
509 if (HIWORD((ULONG
)lpString
) == 0)
511 if ((ULONG
)lpString
>= 0xC000)
513 SetLastErrorByStatus(STATUS_INVALID_PARAMETER
);
516 return (ATOM
)LOWORD((ULONG
)lpString
);
519 AtomTable
= GetLocalAtomTable();
521 Status
= RtlLookupAtomInAtomTable(AtomTable
,
524 if (!NT_SUCCESS(Status
))
526 SetLastErrorByStatus(Status
);
538 GetAtomNameA(ATOM nAtom
,
542 PRTL_ATOM_TABLE AtomTable
;
544 UNICODE_STRING AtomNameU
;
545 ANSI_STRING AtomName
;
549 AtomTable
= GetLocalAtomTable();
551 NameLength
= nSize
* sizeof(WCHAR
);
552 Buffer
= RtlAllocateHeap(RtlGetProcessHeap(),
556 Status
= RtlQueryAtomInAtomTable(AtomTable
,
562 if (!NT_SUCCESS(Status
))
564 RtlFreeHeap(RtlGetProcessHeap(),
570 RtlInitUnicodeString(&AtomNameU
,
572 AtomName
.Buffer
= lpBuffer
;
574 AtomName
.MaximumLength
= nSize
;
575 RtlUnicodeStringToAnsiString(&AtomName
,
579 NameLength
= AtomName
.Length
;
580 RtlFreeHeap(RtlGetProcessHeap(),
592 GetAtomNameW(ATOM nAtom
,
596 PRTL_ATOM_TABLE AtomTable
;
600 AtomTable
= GetLocalAtomTable();
602 NameLength
= nSize
* sizeof(WCHAR
);
603 Status
= RtlQueryAtomInAtomTable(AtomTable
,
609 if (!NT_SUCCESS(Status
))
614 return(NameLength
/ sizeof(WCHAR
));