2005-09-08 00:09:32 +00:00
|
|
|
/* COPYRIGHT: See COPYING in the top level directory
|
|
|
|
* PROJECT: ReactOS system libraries
|
2005-06-24 18:11:16 +00:00
|
|
|
* FILE: lib/rtl/atom.c
|
|
|
|
* PURPOSE: Atom managment
|
2005-09-08 00:09:32 +00:00
|
|
|
* PROGRAMMER: Thomas Weidenmueller
|
2005-06-24 18:11:16 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* INCLUDES *****************************************************************/
|
|
|
|
|
2005-07-26 08:39:07 +00:00
|
|
|
#include <rtl.h>
|
2005-06-24 18:11:16 +00:00
|
|
|
|
|
|
|
#define NDEBUG
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
/* PROTOTYPES ****************************************************************/
|
|
|
|
|
|
|
|
extern NTSTATUS RtlpInitAtomTableLock(PRTL_ATOM_TABLE AtomTable);
|
|
|
|
extern VOID RtlpDestroyAtomTableLock(PRTL_ATOM_TABLE AtomTable);
|
|
|
|
extern BOOLEAN RtlpLockAtomTable(PRTL_ATOM_TABLE AtomTable);
|
|
|
|
extern VOID RtlpUnlockAtomTable(PRTL_ATOM_TABLE AtomTable);
|
|
|
|
|
|
|
|
extern BOOLEAN RtlpCreateAtomHandleTable(PRTL_ATOM_TABLE AtomTable);
|
|
|
|
extern VOID RtlpDestroyAtomHandleTable(PRTL_ATOM_TABLE AtomTable);
|
|
|
|
|
|
|
|
extern PRTL_ATOM_TABLE RtlpAllocAtomTable(ULONG Size);
|
|
|
|
extern VOID RtlpFreeAtomTable(PRTL_ATOM_TABLE AtomTable);
|
|
|
|
extern PRTL_ATOM_TABLE_ENTRY RtlpAllocAtomTableEntry(ULONG Size);
|
|
|
|
extern VOID RtlpFreeAtomTableEntry(PRTL_ATOM_TABLE_ENTRY Entry);
|
|
|
|
|
|
|
|
extern BOOLEAN RtlpCreateAtomHandle(PRTL_ATOM_TABLE AtomTable, PRTL_ATOM_TABLE_ENTRY Entry);
|
|
|
|
extern VOID RtlpFreeAtomHandle(PRTL_ATOM_TABLE AtomTable, PRTL_ATOM_TABLE_ENTRY Entry);
|
|
|
|
extern PRTL_ATOM_TABLE_ENTRY RtlpGetAtomEntry(PRTL_ATOM_TABLE AtomTable, ULONG Index);
|
|
|
|
|
|
|
|
/* FUNCTIONS *****************************************************************/
|
|
|
|
|
2013-03-05 08:38:18 +00:00
|
|
|
static
|
|
|
|
PRTL_ATOM_TABLE_ENTRY
|
|
|
|
RtlpHashAtomName(
|
|
|
|
IN PRTL_ATOM_TABLE AtomTable,
|
|
|
|
IN PWSTR AtomName,
|
|
|
|
OUT PRTL_ATOM_TABLE_ENTRY **HashLink)
|
2005-06-24 18:11:16 +00:00
|
|
|
{
|
2013-03-05 08:38:18 +00:00
|
|
|
UNICODE_STRING Name;
|
|
|
|
ULONG Hash;
|
|
|
|
|
|
|
|
RtlInitUnicodeString(&Name, AtomName);
|
|
|
|
|
|
|
|
if (Name.Length != 0 &&
|
|
|
|
NT_SUCCESS(RtlHashUnicodeString(&Name,
|
|
|
|
TRUE,
|
|
|
|
HASH_STRING_ALGORITHM_X65599,
|
|
|
|
&Hash)))
|
|
|
|
{
|
2005-06-24 18:11:16 +00:00
|
|
|
PRTL_ATOM_TABLE_ENTRY Current;
|
|
|
|
PRTL_ATOM_TABLE_ENTRY *Link;
|
|
|
|
|
|
|
|
Link = &AtomTable->Buckets[Hash % AtomTable->NumberOfBuckets];
|
|
|
|
|
|
|
|
/* search for an existing entry */
|
|
|
|
Current = *Link;
|
|
|
|
while (Current != NULL)
|
2013-03-05 08:38:18 +00:00
|
|
|
{
|
|
|
|
if (Current->NameLength == Name.Length / sizeof(WCHAR) &&
|
|
|
|
!_wcsicmp(Current->Name, Name.Buffer))
|
|
|
|
{
|
|
|
|
*HashLink = Link;
|
|
|
|
return Current;
|
|
|
|
}
|
|
|
|
|
|
|
|
Link = &Current->HashLink;
|
|
|
|
Current = Current->HashLink;
|
|
|
|
}
|
2005-06-24 18:11:16 +00:00
|
|
|
|
|
|
|
/* no matching atom found, return the hash link */
|
|
|
|
*HashLink = Link;
|
2013-03-05 08:38:18 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
*HashLink = NULL;
|
2005-06-24 18:11:16 +00:00
|
|
|
|
2013-03-05 08:38:18 +00:00
|
|
|
return NULL;
|
2005-06-24 18:11:16 +00:00
|
|
|
}
|
|
|
|
|
2013-03-05 08:38:18 +00:00
|
|
|
static
|
|
|
|
BOOLEAN
|
|
|
|
RtlpCheckIntegerAtom(
|
|
|
|
PWSTR AtomName,
|
|
|
|
PUSHORT AtomValue)
|
2005-06-24 18:11:16 +00:00
|
|
|
{
|
2013-03-05 08:38:18 +00:00
|
|
|
UNICODE_STRING AtomString;
|
|
|
|
ULONG LongValue;
|
|
|
|
USHORT LoValue;
|
|
|
|
PWCHAR p;
|
2005-06-24 18:11:16 +00:00
|
|
|
|
2013-03-05 08:38:18 +00:00
|
|
|
DPRINT("RtlpCheckIntegerAtom(AtomName '%S' AtomValue %p)\n",
|
|
|
|
AtomName, AtomValue);
|
2005-06-24 18:11:16 +00:00
|
|
|
|
2013-03-05 08:38:18 +00:00
|
|
|
if (!((ULONG_PTR)AtomName & 0xFFFF0000))
|
|
|
|
{
|
2008-08-27 20:27:32 +00:00
|
|
|
LoValue = (USHORT)((ULONG_PTR)AtomName & 0xFFFF);
|
2005-06-24 18:11:16 +00:00
|
|
|
|
|
|
|
if (LoValue == 0)
|
2013-03-05 08:38:18 +00:00
|
|
|
LoValue = 0xC000;
|
2005-06-24 18:11:16 +00:00
|
|
|
|
|
|
|
if (AtomValue != NULL)
|
2013-03-05 08:38:18 +00:00
|
|
|
*AtomValue = LoValue;
|
2005-06-24 18:11:16 +00:00
|
|
|
|
|
|
|
return TRUE;
|
2013-03-05 08:38:18 +00:00
|
|
|
}
|
2005-06-24 18:11:16 +00:00
|
|
|
|
2013-03-05 08:38:18 +00:00
|
|
|
/*
|
|
|
|
* AtomName cannot be NULL because this
|
|
|
|
* case was caught by the previous test.
|
|
|
|
*/
|
|
|
|
ASSERT(AtomName != NULL);
|
[NTOSKRNL]
Coverity code defects fixes :
- Cache: CID 701441
- Config: CIDs 716570, 716669, 716760
- Dbgk: Kdbg: CIDs 716571, 515128/9, 500432
- Ex: CIDs 500156/7, 515122, 716200/67, 701301, 514669
- Fsrtl: Fstub: CIDs 701341/2, 701288, 716770, 701302, and CIDs 716576/7/8 + 514636 + 716805 thanks to Thomas Faber
- Io: CIDs 514576, 514643, 514672/3, 716203, 716269, 716581, 716591, 716713
- Ke: CIDs 515125, 716592
- Ps: CIDs 716603/4, 701422
- Ob: Po: CIDs 514671/680, 701419/420/421, 716763, 716601/2
All the details are given in the different bug reports.
CORE-6677 CORE-6679 CORE-6680 CORE-6683 CORE-6686 CORE-6692 CORE-6693 CORE-6694 CORE-6695 CORE-6696 #comment Committed in rev.57400 #resolve #close
svn path=/trunk/; revision=57400
2012-09-27 17:16:31 +00:00
|
|
|
|
2013-03-05 08:38:18 +00:00
|
|
|
if (*AtomName != L'#')
|
|
|
|
return FALSE;
|
2005-06-24 18:11:16 +00:00
|
|
|
|
2013-03-05 08:38:18 +00:00
|
|
|
p = AtomName;
|
|
|
|
p++;
|
|
|
|
while (*p)
|
|
|
|
{
|
2005-06-24 18:11:16 +00:00
|
|
|
if ((*p < L'0') || (*p > L'9'))
|
2013-03-05 08:38:18 +00:00
|
|
|
return FALSE;
|
2005-06-24 18:11:16 +00:00
|
|
|
p++;
|
2013-03-05 08:38:18 +00:00
|
|
|
}
|
2005-06-24 18:11:16 +00:00
|
|
|
|
2013-03-05 08:38:18 +00:00
|
|
|
p = AtomName;
|
|
|
|
p++;
|
|
|
|
RtlInitUnicodeString(&AtomString, p);
|
2005-06-24 18:11:16 +00:00
|
|
|
|
2013-03-05 08:38:18 +00:00
|
|
|
DPRINT("AtomString: %wZ\n", &AtomString);
|
2005-06-24 18:11:16 +00:00
|
|
|
|
2013-03-05 08:38:18 +00:00
|
|
|
RtlUnicodeStringToInteger(&AtomString, 10, &LongValue);
|
2005-06-24 18:11:16 +00:00
|
|
|
|
2013-03-05 08:38:18 +00:00
|
|
|
DPRINT("LongValue: %lu\n", LongValue);
|
2005-06-24 18:11:16 +00:00
|
|
|
|
2013-03-05 08:38:18 +00:00
|
|
|
*AtomValue = (USHORT)(LongValue & 0x0000FFFF);
|
2005-06-24 18:11:16 +00:00
|
|
|
|
2013-03-05 08:38:18 +00:00
|
|
|
return TRUE;
|
2005-06-24 18:11:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
2013-03-05 08:38:18 +00:00
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
|
|
|
RtlCreateAtomTable(
|
|
|
|
IN ULONG TableSize,
|
|
|
|
IN OUT PRTL_ATOM_TABLE *AtomTable)
|
2005-06-24 18:11:16 +00:00
|
|
|
{
|
2013-03-05 08:38:18 +00:00
|
|
|
PRTL_ATOM_TABLE Table;
|
|
|
|
NTSTATUS Status;
|
2005-06-24 18:11:16 +00:00
|
|
|
|
2013-03-05 08:38:18 +00:00
|
|
|
DPRINT("RtlCreateAtomTable(TableSize %lu AtomTable %p)\n",
|
|
|
|
TableSize, AtomTable);
|
2005-06-24 18:11:16 +00:00
|
|
|
|
2013-03-05 08:38:18 +00:00
|
|
|
if (*AtomTable != NULL)
|
|
|
|
{
|
2005-06-24 18:11:16 +00:00
|
|
|
return STATUS_SUCCESS;
|
2013-03-05 08:38:18 +00:00
|
|
|
}
|
2005-06-24 18:11:16 +00:00
|
|
|
|
2013-03-05 08:38:18 +00:00
|
|
|
/* Use default if size was incorrect */
|
|
|
|
if (TableSize <= 1) TableSize = 37;
|
2005-12-29 08:43:45 +00:00
|
|
|
|
2013-03-05 08:38:18 +00:00
|
|
|
/* allocate atom table */
|
|
|
|
Table = RtlpAllocAtomTable(((TableSize - 1) * sizeof(PRTL_ATOM_TABLE_ENTRY)) +
|
|
|
|
sizeof(RTL_ATOM_TABLE));
|
|
|
|
if (Table == NULL)
|
|
|
|
{
|
2005-06-24 18:11:16 +00:00
|
|
|
return STATUS_NO_MEMORY;
|
2013-03-05 08:38:18 +00:00
|
|
|
}
|
2005-06-24 18:11:16 +00:00
|
|
|
|
2013-03-05 08:38:18 +00:00
|
|
|
/* initialize atom table */
|
|
|
|
Table->NumberOfBuckets = TableSize;
|
2005-06-24 18:11:16 +00:00
|
|
|
|
2013-03-05 08:38:18 +00:00
|
|
|
Status = RtlpInitAtomTableLock(Table);
|
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
{
|
2005-06-24 18:11:16 +00:00
|
|
|
RtlpFreeAtomTable(Table);
|
|
|
|
return Status;
|
2013-03-05 08:38:18 +00:00
|
|
|
}
|
2005-06-24 18:11:16 +00:00
|
|
|
|
2013-03-05 08:38:18 +00:00
|
|
|
if (!RtlpCreateAtomHandleTable(Table))
|
|
|
|
{
|
2005-06-24 18:11:16 +00:00
|
|
|
RtlpDestroyAtomTableLock(Table);
|
|
|
|
RtlpFreeAtomTable(Table);
|
|
|
|
return STATUS_NO_MEMORY;
|
2013-03-05 08:38:18 +00:00
|
|
|
}
|
2005-06-24 18:11:16 +00:00
|
|
|
|
2013-03-05 08:38:18 +00:00
|
|
|
*AtomTable = Table;
|
|
|
|
return STATUS_SUCCESS;
|
2005-06-24 18:11:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
2013-03-05 08:38:18 +00:00
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
|
|
|
RtlDestroyAtomTable(
|
|
|
|
IN PRTL_ATOM_TABLE AtomTable)
|
2005-06-24 18:11:16 +00:00
|
|
|
{
|
2013-03-05 08:38:18 +00:00
|
|
|
PRTL_ATOM_TABLE_ENTRY *CurrentBucket, *LastBucket;
|
|
|
|
PRTL_ATOM_TABLE_ENTRY CurrentEntry, NextEntry;
|
2007-10-19 23:21:45 +00:00
|
|
|
|
2013-03-05 08:38:18 +00:00
|
|
|
DPRINT("RtlDestroyAtomTable (AtomTable %p)\n", AtomTable);
|
2005-06-24 18:11:16 +00:00
|
|
|
|
2013-03-05 08:38:18 +00:00
|
|
|
if (!RtlpLockAtomTable(AtomTable))
|
|
|
|
{
|
2005-06-24 18:11:16 +00:00
|
|
|
return (STATUS_INVALID_PARAMETER);
|
2013-03-05 08:38:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* delete all atoms */
|
|
|
|
LastBucket = AtomTable->Buckets + AtomTable->NumberOfBuckets;
|
|
|
|
for (CurrentBucket = AtomTable->Buckets;
|
|
|
|
CurrentBucket != LastBucket;
|
|
|
|
CurrentBucket++)
|
|
|
|
{
|
2005-06-24 18:11:16 +00:00
|
|
|
NextEntry = *CurrentBucket;
|
|
|
|
*CurrentBucket = NULL;
|
|
|
|
|
|
|
|
while (NextEntry != NULL)
|
2013-03-05 08:38:18 +00:00
|
|
|
{
|
|
|
|
CurrentEntry = NextEntry;
|
|
|
|
NextEntry = NextEntry->HashLink;
|
2005-06-24 18:11:16 +00:00
|
|
|
|
2013-03-05 08:38:18 +00:00
|
|
|
/* no need to delete the atom handle, the handles will all be freed
|
|
|
|
up when destroying the atom handle table! */
|
2005-06-24 18:11:16 +00:00
|
|
|
|
2013-03-05 08:38:18 +00:00
|
|
|
RtlpFreeAtomTableEntry(CurrentEntry);
|
|
|
|
}
|
|
|
|
}
|
2005-06-24 18:11:16 +00:00
|
|
|
|
2013-03-05 08:38:18 +00:00
|
|
|
RtlpDestroyAtomHandleTable(AtomTable);
|
2005-06-24 18:11:16 +00:00
|
|
|
|
2013-03-05 08:38:18 +00:00
|
|
|
RtlpUnlockAtomTable(AtomTable);
|
2005-06-24 18:11:16 +00:00
|
|
|
|
2013-03-05 08:38:18 +00:00
|
|
|
RtlpDestroyAtomTableLock(AtomTable);
|
2005-06-24 18:11:16 +00:00
|
|
|
|
2013-03-05 08:38:18 +00:00
|
|
|
RtlpFreeAtomTable(AtomTable);
|
2005-06-24 18:11:16 +00:00
|
|
|
|
2013-03-05 08:38:18 +00:00
|
|
|
return STATUS_SUCCESS;
|
2005-06-24 18:11:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
2013-03-05 08:38:18 +00:00
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
|
|
|
RtlEmptyAtomTable(
|
|
|
|
PRTL_ATOM_TABLE AtomTable,
|
|
|
|
BOOLEAN DeletePinned)
|
2005-06-24 18:11:16 +00:00
|
|
|
{
|
2013-03-05 08:38:18 +00:00
|
|
|
PRTL_ATOM_TABLE_ENTRY *CurrentBucket, *LastBucket;
|
|
|
|
PRTL_ATOM_TABLE_ENTRY CurrentEntry, NextEntry, *PtrEntry;
|
2005-06-24 18:11:16 +00:00
|
|
|
|
2013-03-05 08:38:18 +00:00
|
|
|
DPRINT("RtlEmptyAtomTable (AtomTable %p DeletePinned %x)\n",
|
|
|
|
AtomTable, DeletePinned);
|
2005-06-24 18:11:16 +00:00
|
|
|
|
2013-03-05 08:38:18 +00:00
|
|
|
if (RtlpLockAtomTable(AtomTable) == FALSE)
|
|
|
|
{
|
2005-06-24 18:11:16 +00:00
|
|
|
return (STATUS_INVALID_PARAMETER);
|
2013-03-05 08:38:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* delete all atoms */
|
|
|
|
LastBucket = AtomTable->Buckets + AtomTable->NumberOfBuckets;
|
|
|
|
for (CurrentBucket = AtomTable->Buckets;
|
|
|
|
CurrentBucket != LastBucket;
|
|
|
|
CurrentBucket++)
|
|
|
|
{
|
2005-06-24 18:11:16 +00:00
|
|
|
NextEntry = *CurrentBucket;
|
2005-08-03 14:26:56 +00:00
|
|
|
PtrEntry = CurrentBucket;
|
2005-06-24 18:11:16 +00:00
|
|
|
|
|
|
|
while (NextEntry != NULL)
|
2013-03-05 08:38:18 +00:00
|
|
|
{
|
|
|
|
CurrentEntry = NextEntry;
|
|
|
|
NextEntry = NextEntry->HashLink;
|
2005-06-24 18:11:16 +00:00
|
|
|
|
2013-03-05 08:38:18 +00:00
|
|
|
if (DeletePinned || !(CurrentEntry->Flags & RTL_ATOM_IS_PINNED))
|
|
|
|
{
|
|
|
|
*PtrEntry = NextEntry;
|
2005-06-24 18:11:16 +00:00
|
|
|
|
2013-03-05 08:38:18 +00:00
|
|
|
RtlpFreeAtomHandle(AtomTable, CurrentEntry);
|
2005-08-03 14:26:56 +00:00
|
|
|
|
2013-03-05 08:38:18 +00:00
|
|
|
RtlpFreeAtomTableEntry(CurrentEntry);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
PtrEntry = &CurrentEntry->HashLink;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2005-06-24 18:11:16 +00:00
|
|
|
|
2013-03-05 08:38:18 +00:00
|
|
|
RtlpUnlockAtomTable(AtomTable);
|
2005-06-24 18:11:16 +00:00
|
|
|
|
2013-03-05 08:38:18 +00:00
|
|
|
return STATUS_SUCCESS;
|
2005-06-24 18:11:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
2005-10-19 17:03:38 +00:00
|
|
|
NTSTATUS NTAPI
|
2005-06-24 18:11:16 +00:00
|
|
|
RtlAddAtomToAtomTable(IN PRTL_ATOM_TABLE AtomTable,
|
|
|
|
IN PWSTR AtomName,
|
|
|
|
OUT PRTL_ATOM Atom)
|
|
|
|
{
|
2013-03-05 08:38:18 +00:00
|
|
|
USHORT AtomValue;
|
|
|
|
PRTL_ATOM_TABLE_ENTRY *HashLink;
|
|
|
|
PRTL_ATOM_TABLE_ENTRY Entry = NULL;
|
|
|
|
NTSTATUS Status = STATUS_SUCCESS;
|
2005-06-24 18:11:16 +00:00
|
|
|
|
2013-03-05 08:38:18 +00:00
|
|
|
DPRINT("RtlAddAtomToAtomTable (AtomTable %p AtomName %S Atom %p)\n",
|
|
|
|
AtomTable, AtomName, Atom);
|
2005-06-24 18:11:16 +00:00
|
|
|
|
2013-03-05 08:38:18 +00:00
|
|
|
if (RtlpCheckIntegerAtom (AtomName, &AtomValue))
|
|
|
|
{
|
2005-06-24 18:11:16 +00:00
|
|
|
/* integer atom */
|
|
|
|
if (AtomValue >= 0xC000)
|
2013-03-05 08:38:18 +00:00
|
|
|
{
|
|
|
|
Status = STATUS_INVALID_PARAMETER;
|
|
|
|
}
|
2005-06-24 18:11:16 +00:00
|
|
|
else if (Atom != NULL)
|
2013-03-05 08:38:18 +00:00
|
|
|
{
|
|
|
|
*Atom = (RTL_ATOM)AtomValue;
|
|
|
|
}
|
2005-06-24 18:11:16 +00:00
|
|
|
|
|
|
|
return Status;
|
2013-03-05 08:38:18 +00:00
|
|
|
}
|
2005-06-24 18:11:16 +00:00
|
|
|
|
2013-03-05 08:38:18 +00:00
|
|
|
RtlpLockAtomTable(AtomTable);
|
2005-06-24 18:11:16 +00:00
|
|
|
|
2013-03-05 08:38:18 +00:00
|
|
|
/* string atom, hash it and try to find an existing atom with the same name */
|
|
|
|
Entry = RtlpHashAtomName(AtomTable, AtomName, &HashLink);
|
2005-06-24 18:11:16 +00:00
|
|
|
|
2013-03-05 08:38:18 +00:00
|
|
|
if (Entry != NULL)
|
|
|
|
{
|
2005-06-24 18:11:16 +00:00
|
|
|
/* found another atom, increment the reference counter unless it's pinned */
|
|
|
|
|
|
|
|
if (!(Entry->Flags & RTL_ATOM_IS_PINNED))
|
2013-03-05 08:38:18 +00:00
|
|
|
{
|
|
|
|
if (++Entry->ReferenceCount == 0)
|
|
|
|
{
|
|
|
|
/* FIXME - references overflowed, pin the atom? */
|
|
|
|
Entry->Flags |= RTL_ATOM_IS_PINNED;
|
|
|
|
}
|
|
|
|
}
|
2005-06-24 18:11:16 +00:00
|
|
|
|
|
|
|
if (Atom != NULL)
|
2013-03-05 08:38:18 +00:00
|
|
|
{
|
|
|
|
*Atom = (RTL_ATOM)Entry->Atom;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2005-06-24 18:11:16 +00:00
|
|
|
/* couldn't find an existing atom, HashLink now points to either the
|
|
|
|
HashLink pointer of the previous atom or to the bucket so we can
|
|
|
|
simply add it to the list */
|
|
|
|
if (HashLink != NULL)
|
2013-03-05 08:38:18 +00:00
|
|
|
{
|
|
|
|
ULONG AtomNameLen = (ULONG)wcslen(AtomName);
|
2007-10-19 23:21:45 +00:00
|
|
|
|
2013-03-05 08:38:18 +00:00
|
|
|
if (AtomNameLen > RTL_MAXIMUM_ATOM_LENGTH)
|
|
|
|
{
|
2005-09-24 02:50:02 +00:00
|
|
|
Status = STATUS_INVALID_PARAMETER;
|
|
|
|
goto end;
|
2013-03-05 08:38:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Entry = RtlpAllocAtomTableEntry(sizeof(RTL_ATOM_TABLE_ENTRY) -
|
|
|
|
sizeof(Entry->Name) +
|
|
|
|
(AtomNameLen + 1) * sizeof(WCHAR));
|
|
|
|
if (Entry != NULL)
|
|
|
|
{
|
|
|
|
Entry->HashLink = NULL;
|
|
|
|
Entry->ReferenceCount = 1;
|
|
|
|
Entry->Flags = 0x0;
|
|
|
|
|
|
|
|
Entry->NameLength = (UCHAR)AtomNameLen;
|
|
|
|
RtlCopyMemory(Entry->Name,
|
|
|
|
AtomName,
|
|
|
|
(AtomNameLen + 1) * sizeof(WCHAR));
|
|
|
|
|
|
|
|
if (RtlpCreateAtomHandle(AtomTable, Entry))
|
|
|
|
{
|
|
|
|
/* append the atom to the list */
|
|
|
|
*HashLink = Entry;
|
|
|
|
|
|
|
|
if (Atom != NULL)
|
2005-06-24 18:11:16 +00:00
|
|
|
{
|
2013-03-05 08:38:18 +00:00
|
|
|
*Atom = (RTL_ATOM)Entry->Atom;
|
2005-06-24 18:11:16 +00:00
|
|
|
}
|
2013-03-05 08:38:18 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
RtlpFreeAtomTableEntry(Entry);
|
|
|
|
Status = STATUS_NO_MEMORY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Status = STATUS_NO_MEMORY;
|
|
|
|
}
|
|
|
|
}
|
2005-06-24 18:11:16 +00:00
|
|
|
else
|
2013-03-05 08:38:18 +00:00
|
|
|
{
|
|
|
|
/* The caller supplied an empty atom name! */
|
|
|
|
Status = STATUS_OBJECT_NAME_INVALID;
|
|
|
|
}
|
|
|
|
}
|
2005-09-24 02:50:02 +00:00
|
|
|
end:
|
2013-03-05 08:38:18 +00:00
|
|
|
RtlpUnlockAtomTable(AtomTable);
|
2005-06-24 18:11:16 +00:00
|
|
|
|
2013-03-05 08:38:18 +00:00
|
|
|
return Status;
|
2005-06-24 18:11:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
2013-03-05 08:38:18 +00:00
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
|
|
|
RtlDeleteAtomFromAtomTable(
|
|
|
|
IN PRTL_ATOM_TABLE AtomTable,
|
|
|
|
IN RTL_ATOM Atom)
|
2005-06-24 18:11:16 +00:00
|
|
|
{
|
2013-03-05 08:38:18 +00:00
|
|
|
PRTL_ATOM_TABLE_ENTRY Entry;
|
|
|
|
NTSTATUS Status = STATUS_SUCCESS;
|
2005-06-24 18:11:16 +00:00
|
|
|
|
2013-03-05 08:38:18 +00:00
|
|
|
DPRINT("RtlDeleteAtomFromAtomTable (AtomTable %p Atom %x)\n",
|
|
|
|
AtomTable, Atom);
|
2005-06-24 18:11:16 +00:00
|
|
|
|
2013-03-05 08:38:18 +00:00
|
|
|
if (Atom >= 0xC000)
|
|
|
|
{
|
2005-06-24 18:11:16 +00:00
|
|
|
RtlpLockAtomTable(AtomTable);
|
|
|
|
|
2013-03-05 08:38:18 +00:00
|
|
|
Entry = RtlpGetAtomEntry(AtomTable, (ULONG)((USHORT)Atom - 0xC000));
|
2005-06-24 18:11:16 +00:00
|
|
|
|
|
|
|
if (Entry != NULL && Entry->Atom == (USHORT)Atom)
|
2013-03-05 08:38:18 +00:00
|
|
|
{
|
|
|
|
if (!(Entry->Flags & RTL_ATOM_IS_PINNED))
|
|
|
|
{
|
|
|
|
if (--Entry->ReferenceCount == 0)
|
|
|
|
{
|
|
|
|
PRTL_ATOM_TABLE_ENTRY *HashLink;
|
|
|
|
|
|
|
|
/* it's time to delete the atom. we need to unlink it from
|
|
|
|
the list. The easiest way is to take the atom name and
|
|
|
|
hash it again, this way we get the pointer to either
|
|
|
|
the hash bucket or the previous atom that links to the
|
|
|
|
one we want to delete. This way we can easily bypass
|
|
|
|
this item. */
|
|
|
|
if (RtlpHashAtomName(AtomTable, Entry->Name, &HashLink) != NULL)
|
|
|
|
{
|
|
|
|
/* bypass this atom */
|
|
|
|
*HashLink = Entry->HashLink;
|
|
|
|
|
|
|
|
RtlpFreeAtomHandle(AtomTable, Entry);
|
|
|
|
|
|
|
|
RtlpFreeAtomTableEntry(Entry);
|
|
|
|
}
|
|
|
|
else
|
2005-06-24 18:11:16 +00:00
|
|
|
{
|
2013-03-05 08:38:18 +00:00
|
|
|
/* WTF?! This should never happen!!! */
|
|
|
|
ASSERT(FALSE);
|
2005-06-24 18:11:16 +00:00
|
|
|
}
|
2013-03-05 08:38:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* tried to delete a pinned atom, do nothing and return
|
|
|
|
STATUS_WAS_LOCKED, which is NOT a failure code! */
|
|
|
|
Status = STATUS_WAS_LOCKED;
|
|
|
|
}
|
|
|
|
}
|
2005-06-24 18:11:16 +00:00
|
|
|
else
|
2013-03-05 08:38:18 +00:00
|
|
|
{
|
|
|
|
Status = STATUS_INVALID_HANDLE;
|
|
|
|
}
|
2005-06-24 18:11:16 +00:00
|
|
|
|
|
|
|
RtlpUnlockAtomTable(AtomTable);
|
2013-03-05 08:38:18 +00:00
|
|
|
}
|
2005-06-24 18:11:16 +00:00
|
|
|
|
2013-03-05 08:38:18 +00:00
|
|
|
return Status;
|
2005-06-24 18:11:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
2005-10-19 17:03:38 +00:00
|
|
|
NTSTATUS NTAPI
|
2005-06-24 18:11:16 +00:00
|
|
|
RtlLookupAtomInAtomTable(IN PRTL_ATOM_TABLE AtomTable,
|
|
|
|
IN PWSTR AtomName,
|
|
|
|
OUT PRTL_ATOM Atom)
|
|
|
|
{
|
2013-03-05 08:38:18 +00:00
|
|
|
PRTL_ATOM_TABLE_ENTRY Entry, *HashLink;
|
|
|
|
USHORT AtomValue;
|
|
|
|
RTL_ATOM FoundAtom = 0;
|
|
|
|
NTSTATUS Status = STATUS_SUCCESS;
|
2005-06-24 18:11:16 +00:00
|
|
|
|
2013-03-05 08:38:18 +00:00
|
|
|
DPRINT("RtlLookupAtomInAtomTable (AtomTable %p AtomName %S Atom %p)\n",
|
|
|
|
AtomTable, AtomName, Atom);
|
2005-06-24 18:11:16 +00:00
|
|
|
|
2013-03-05 08:38:18 +00:00
|
|
|
if (RtlpCheckIntegerAtom (AtomName, &AtomValue))
|
|
|
|
{
|
2005-06-24 18:11:16 +00:00
|
|
|
/* integer atom */
|
|
|
|
if (AtomValue >= 0xC000)
|
2013-03-05 08:38:18 +00:00
|
|
|
{
|
|
|
|
Status = STATUS_INVALID_PARAMETER;
|
|
|
|
}
|
2005-06-24 18:11:16 +00:00
|
|
|
else if (Atom != NULL)
|
2013-03-05 08:38:18 +00:00
|
|
|
{
|
|
|
|
*Atom = (RTL_ATOM)AtomValue;
|
|
|
|
}
|
2005-06-24 18:11:16 +00:00
|
|
|
|
|
|
|
return Status;
|
2013-03-05 08:38:18 +00:00
|
|
|
}
|
2005-06-24 18:11:16 +00:00
|
|
|
|
2013-03-05 08:38:18 +00:00
|
|
|
RtlpLockAtomTable(AtomTable);
|
|
|
|
Status = STATUS_OBJECT_NAME_NOT_FOUND;
|
2005-06-24 18:11:16 +00:00
|
|
|
|
2013-03-05 08:38:18 +00:00
|
|
|
/* string atom */
|
|
|
|
Entry = RtlpHashAtomName(AtomTable, AtomName, &HashLink);
|
|
|
|
if (Entry != NULL)
|
|
|
|
{
|
2005-06-24 18:11:16 +00:00
|
|
|
Status = STATUS_SUCCESS;
|
|
|
|
FoundAtom = (RTL_ATOM)Entry->Atom;
|
2013-03-05 08:38:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RtlpUnlockAtomTable(AtomTable);
|
|
|
|
if (NT_SUCCESS(Status) && Atom != NULL)
|
|
|
|
{
|
2005-06-24 18:11:16 +00:00
|
|
|
*Atom = FoundAtom;
|
2013-03-05 08:38:18 +00:00
|
|
|
}
|
|
|
|
return Status;
|
2005-06-24 18:11:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
2005-10-19 17:03:38 +00:00
|
|
|
NTSTATUS NTAPI
|
2005-06-24 18:11:16 +00:00
|
|
|
RtlPinAtomInAtomTable(IN PRTL_ATOM_TABLE AtomTable,
|
|
|
|
IN RTL_ATOM Atom)
|
|
|
|
{
|
2013-03-05 08:38:18 +00:00
|
|
|
NTSTATUS Status = STATUS_SUCCESS;
|
2005-06-24 18:11:16 +00:00
|
|
|
|
2013-03-05 08:38:18 +00:00
|
|
|
DPRINT("RtlPinAtomInAtomTable (AtomTable %p Atom %x)\n",
|
|
|
|
AtomTable, Atom);
|
2005-06-24 18:11:16 +00:00
|
|
|
|
2013-03-05 08:38:18 +00:00
|
|
|
if (Atom >= 0xC000)
|
|
|
|
{
|
2005-06-24 18:11:16 +00:00
|
|
|
PRTL_ATOM_TABLE_ENTRY Entry;
|
2007-10-19 23:21:45 +00:00
|
|
|
|
2005-06-24 18:11:16 +00:00
|
|
|
RtlpLockAtomTable(AtomTable);
|
|
|
|
|
2013-03-05 08:38:18 +00:00
|
|
|
Entry = RtlpGetAtomEntry(AtomTable, (ULONG)((USHORT)Atom - 0xC000));
|
2005-06-24 18:11:16 +00:00
|
|
|
|
|
|
|
if (Entry != NULL && Entry->Atom == (USHORT)Atom)
|
2013-03-05 08:38:18 +00:00
|
|
|
{
|
|
|
|
Entry->Flags |= RTL_ATOM_IS_PINNED;
|
|
|
|
}
|
2005-06-24 18:11:16 +00:00
|
|
|
else
|
2013-03-05 08:38:18 +00:00
|
|
|
{
|
|
|
|
Status = STATUS_INVALID_HANDLE;
|
|
|
|
}
|
2005-06-24 18:11:16 +00:00
|
|
|
|
|
|
|
RtlpUnlockAtomTable(AtomTable);
|
2013-03-05 08:38:18 +00:00
|
|
|
}
|
2005-06-24 18:11:16 +00:00
|
|
|
|
2013-03-05 08:38:18 +00:00
|
|
|
return Status;
|
2005-06-24 18:11:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @implemented
|
2005-09-23 21:08:57 +00:00
|
|
|
*
|
|
|
|
* This API is really messed up with regards to NameLength. If you pass in a
|
|
|
|
* valid buffer for AtomName, NameLength should be the size of the buffer
|
|
|
|
* (in bytes, not characters). So if you expect the string to be 6 char long,
|
|
|
|
* you need to allocate a buffer of 7 WCHARs and pass 14 for NameLength.
|
|
|
|
* The AtomName returned is always null terminated. If the NameLength you pass
|
|
|
|
* is smaller than 4 (4 would leave room for 1 character) the function will
|
|
|
|
* return with status STATUS_BUFFER_TOO_SMALL. If you pass more than 4, the
|
|
|
|
* return status will be STATUS_SUCCESS, even if the buffer is not large enough
|
|
|
|
* to hold the complete string. In that case, the string is silently truncated
|
|
|
|
* and made to fit in the provided buffer. On return NameLength is set to the
|
|
|
|
* number of bytes (but EXCLUDING the bytes for the null terminator) copied.
|
|
|
|
* So, if the string is 6 char long, you pass a buffer of 10 bytes, on return
|
|
|
|
* NameLength will be set to 8.
|
|
|
|
* If you pass in a NULL value for AtomName, the length of the string in bytes
|
|
|
|
* (again EXCLUDING the null terminator) is returned in NameLength, at least
|
|
|
|
* on Win2k, XP and ReactOS. NT4 will return 0 in that case.
|
2005-06-24 18:11:16 +00:00
|
|
|
*/
|
2013-03-05 08:38:18 +00:00
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
|
|
|
RtlQueryAtomInAtomTable(
|
|
|
|
PRTL_ATOM_TABLE AtomTable,
|
|
|
|
RTL_ATOM Atom,
|
|
|
|
PULONG RefCount,
|
|
|
|
PULONG PinCount,
|
|
|
|
PWSTR AtomName,
|
|
|
|
PULONG NameLength)
|
2005-06-24 18:11:16 +00:00
|
|
|
{
|
2013-03-05 08:38:18 +00:00
|
|
|
ULONG Length;
|
|
|
|
BOOL Unlock = FALSE;
|
|
|
|
|
|
|
|
union
|
|
|
|
{
|
|
|
|
/* A RTL_ATOM_TABLE_ENTRY has a "WCHAR Name[1]" entry at the end.
|
|
|
|
* Make sure we reserve enough room to facilitate a 12 character name */
|
|
|
|
RTL_ATOM_TABLE_ENTRY AtomTableEntry;
|
|
|
|
WCHAR StringBuffer[sizeof(RTL_ATOM_TABLE_ENTRY) / sizeof(WCHAR) + 12];
|
|
|
|
} NumberEntry;
|
|
|
|
PRTL_ATOM_TABLE_ENTRY Entry;
|
|
|
|
NTSTATUS Status = STATUS_SUCCESS;
|
|
|
|
|
|
|
|
if (Atom < 0xC000)
|
|
|
|
{
|
2005-09-23 21:08:57 +00:00
|
|
|
/* Synthesize an entry */
|
|
|
|
NumberEntry.AtomTableEntry.Atom = Atom;
|
|
|
|
NumberEntry.AtomTableEntry.NameLength = swprintf(NumberEntry.AtomTableEntry.Name,
|
2013-03-05 08:38:18 +00:00
|
|
|
L"#%lu",
|
|
|
|
(ULONG)Atom);
|
2005-09-23 21:08:57 +00:00
|
|
|
NumberEntry.AtomTableEntry.ReferenceCount = 1;
|
|
|
|
NumberEntry.AtomTableEntry.Flags = RTL_ATOM_IS_PINNED;
|
|
|
|
Entry = &NumberEntry.AtomTableEntry;
|
2013-03-05 08:38:18 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2005-09-23 21:08:57 +00:00
|
|
|
RtlpLockAtomTable(AtomTable);
|
2005-09-24 02:50:02 +00:00
|
|
|
Unlock = TRUE;
|
2005-06-24 18:11:16 +00:00
|
|
|
|
2013-03-05 08:38:18 +00:00
|
|
|
Entry = RtlpGetAtomEntry(AtomTable, (ULONG)((USHORT)Atom - 0xC000));
|
|
|
|
}
|
2005-06-24 18:11:16 +00:00
|
|
|
|
2013-03-05 08:38:18 +00:00
|
|
|
if (Entry != NULL && Entry->Atom == (USHORT)Atom)
|
|
|
|
{
|
2005-06-24 18:11:16 +00:00
|
|
|
DPRINT("Atom name: %wZ\n", &Entry->Name);
|
2007-10-19 23:21:45 +00:00
|
|
|
|
2005-06-24 18:11:16 +00:00
|
|
|
if (RefCount != NULL)
|
2013-03-05 08:38:18 +00:00
|
|
|
{
|
|
|
|
*RefCount = Entry->ReferenceCount;
|
|
|
|
}
|
2005-06-24 18:11:16 +00:00
|
|
|
|
|
|
|
if (PinCount != NULL)
|
2013-03-05 08:38:18 +00:00
|
|
|
{
|
|
|
|
*PinCount = ((Entry->Flags & RTL_ATOM_IS_PINNED) != 0);
|
|
|
|
}
|
2005-06-24 18:11:16 +00:00
|
|
|
|
2005-09-23 21:08:57 +00:00
|
|
|
if (NULL != NameLength)
|
2013-03-05 08:38:18 +00:00
|
|
|
{
|
|
|
|
Length = Entry->NameLength * sizeof(WCHAR);
|
|
|
|
if (NULL != AtomName)
|
|
|
|
{
|
|
|
|
if (*NameLength < Length + sizeof(WCHAR))
|
|
|
|
{
|
|
|
|
if (*NameLength < 4)
|
2005-09-23 21:08:57 +00:00
|
|
|
{
|
2013-03-05 08:38:18 +00:00
|
|
|
*NameLength = Length;
|
|
|
|
Status = STATUS_BUFFER_TOO_SMALL;
|
2005-09-23 21:08:57 +00:00
|
|
|
}
|
2013-03-05 08:38:18 +00:00
|
|
|
else
|
2005-09-23 21:08:57 +00:00
|
|
|
{
|
2013-03-05 08:38:18 +00:00
|
|
|
Length = *NameLength - sizeof(WCHAR);
|
2005-09-23 21:08:57 +00:00
|
|
|
}
|
2013-03-05 08:38:18 +00:00
|
|
|
}
|
|
|
|
if (NT_SUCCESS(Status))
|
|
|
|
{
|
|
|
|
RtlCopyMemory(AtomName, Entry->Name, Length);
|
|
|
|
AtomName[Length / sizeof(WCHAR)] = L'\0';
|
|
|
|
*NameLength = Length;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*NameLength = Length;
|
|
|
|
}
|
|
|
|
}
|
2005-09-23 21:08:57 +00:00
|
|
|
else if (NULL != AtomName)
|
2013-03-05 08:38:18 +00:00
|
|
|
{
|
|
|
|
Status = STATUS_INVALID_PARAMETER;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2005-06-24 18:11:16 +00:00
|
|
|
Status = STATUS_INVALID_HANDLE;
|
2013-03-05 08:38:18 +00:00
|
|
|
}
|
2005-06-24 18:11:16 +00:00
|
|
|
|
2013-03-05 08:38:18 +00:00
|
|
|
if (Unlock) RtlpUnlockAtomTable(AtomTable);
|
2005-06-24 18:11:16 +00:00
|
|
|
|
2013-03-05 08:38:18 +00:00
|
|
|
return Status;
|
2005-06-24 18:11:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @private - only used by NtQueryInformationAtom
|
|
|
|
*/
|
2013-03-05 08:38:18 +00:00
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
|
|
|
RtlQueryAtomListInAtomTable(
|
|
|
|
IN PRTL_ATOM_TABLE AtomTable,
|
|
|
|
IN ULONG MaxAtomCount,
|
|
|
|
OUT ULONG *AtomCount,
|
|
|
|
OUT RTL_ATOM *AtomList)
|
2005-06-24 18:11:16 +00:00
|
|
|
{
|
2013-03-05 08:38:18 +00:00
|
|
|
PRTL_ATOM_TABLE_ENTRY *CurrentBucket, *LastBucket;
|
|
|
|
PRTL_ATOM_TABLE_ENTRY CurrentEntry;
|
|
|
|
ULONG Atoms = 0;
|
|
|
|
NTSTATUS Status = STATUS_SUCCESS;
|
|
|
|
|
|
|
|
RtlpLockAtomTable(AtomTable);
|
|
|
|
|
|
|
|
LastBucket = AtomTable->Buckets + AtomTable->NumberOfBuckets;
|
|
|
|
for (CurrentBucket = AtomTable->Buckets;
|
|
|
|
CurrentBucket != LastBucket;
|
|
|
|
CurrentBucket++)
|
|
|
|
{
|
2005-06-24 18:11:16 +00:00
|
|
|
CurrentEntry = *CurrentBucket;
|
|
|
|
|
|
|
|
while (CurrentEntry != NULL)
|
2013-03-05 08:38:18 +00:00
|
|
|
{
|
|
|
|
if (MaxAtomCount > 0)
|
|
|
|
{
|
|
|
|
*(AtomList++) = (RTL_ATOM)CurrentEntry->Atom;
|
|
|
|
MaxAtomCount--;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* buffer too small, but don't bail. we need to determine the
|
|
|
|
total number of atoms in the table! */
|
|
|
|
Status = STATUS_INFO_LENGTH_MISMATCH;
|
|
|
|
}
|
|
|
|
|
|
|
|
Atoms++;
|
|
|
|
CurrentEntry = CurrentEntry->HashLink;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*AtomCount = Atoms;
|
|
|
|
|
|
|
|
RtlpUnlockAtomTable(AtomTable);
|
|
|
|
|
|
|
|
return Status;
|
2005-06-24 18:11:16 +00:00
|
|
|
}
|
|
|
|
|