From 75a50813aaee3676d240cd4247d0517cec6a4be6 Mon Sep 17 00:00:00 2001 From: Gunnar Dalsnes Date: Sun, 20 Jun 2004 23:27:21 +0000 Subject: [PATCH] moved encode|image|splaytee into rtl svn path=/trunk/; revision=9781 --- reactos/lib/rtl/encode.c | 80 +++++++++++++++++++ reactos/lib/rtl/image.c | 148 ++++++++++++++++++++++++++++++++++++ reactos/lib/rtl/makefile | 5 +- reactos/lib/rtl/splaytree.c | 70 +++++++++++++++++ 4 files changed, 302 insertions(+), 1 deletion(-) create mode 100644 reactos/lib/rtl/encode.c create mode 100644 reactos/lib/rtl/image.c create mode 100644 reactos/lib/rtl/splaytree.c diff --git a/reactos/lib/rtl/encode.c b/reactos/lib/rtl/encode.c new file mode 100644 index 00000000000..82b82a30283 --- /dev/null +++ b/reactos/lib/rtl/encode.c @@ -0,0 +1,80 @@ +/* $Id: encode.c,v 1.1 2004/06/20 23:27:21 gdalsnes Exp $ + * + * COPYRIGHT: See COPYING in the top level directory + * PROJECT: ReactOS kernel + * PURPOSE: Security descriptor functions + * FILE: lib/rtl/encode.c + * PROGRAMMER: KJK::Hyperion + * REVISION HISTORY: + * 02/04/2003: created (code contributed by crazylord + * ) + */ + +/* INCLUDES *****************************************************************/ + +#include + +#include + +/* FUNCTIONS ***************************************************************/ + +VOID STDCALL +RtlRunDecodeUnicodeString (IN UCHAR Hash, + IN OUT PUNICODE_STRING String) +{ + PUCHAR ptr; + USHORT i; + + ptr = (PUCHAR)String->Buffer; + if (String->Length > 1) + { + for (i = String->Length; i > 1; i--) + { + ptr[i - 1] ^= ptr[i - 2] ^ Hash; + } + } + + if (String->Length >= 1) + { + ptr[0] ^= Hash | (UCHAR)0x43; + } +} + + +VOID STDCALL +RtlRunEncodeUnicodeString (IN OUT PUCHAR Hash, + IN OUT PUNICODE_STRING String) +{ + LARGE_INTEGER CurrentTime; + PUCHAR ptr; + USHORT i; + NTSTATUS Status; + + ptr = (PUCHAR) String->Buffer; + if (*Hash == 0) + { + Status = NtQuerySystemTime (&CurrentTime); + if (NT_SUCCESS(Status)) + { + for (i = 1; i < sizeof(LARGE_INTEGER) && (*Hash == 0); i++) + *Hash |= *(PUCHAR)(((PUCHAR)&CurrentTime) + i); + } + + if (*Hash == 0) + *Hash = 1; + } + + if (String->Length >= 1) + { + ptr[0] ^= (*Hash) | (UCHAR)0x43; + if (String->Length > 1) + { + for (i = 1; i < String->Length; i++) + { + ptr[i] ^= ptr[i - 1] ^ (*Hash); + } + } + } +} + +/* EOF */ diff --git a/reactos/lib/rtl/image.c b/reactos/lib/rtl/image.c new file mode 100644 index 00000000000..0b261f3599d --- /dev/null +++ b/reactos/lib/rtl/image.c @@ -0,0 +1,148 @@ +/* $Id: image.c,v 1.1 2004/06/20 23:27:21 gdalsnes Exp $ + * + * COPYRIGHT: See COPYING in the top level directory + * PROJECT: ReactOS kernel + * FILE: lib/ntdll/rtl/image.c + * PURPOSE: Image handling functions + * PROGRAMMER: Eric Kohl + * UPDATE HISTORY: + * 17/03/2000 Created + */ + +#include +#include + +#define NDEBUG +#include + +/* FUNCTIONS ****************************************************************/ + +/* + * @implemented + */ +PIMAGE_NT_HEADERS STDCALL +RtlImageNtHeader (IN PVOID BaseAddress) +{ + PIMAGE_NT_HEADERS NtHeader; + PIMAGE_DOS_HEADER DosHeader = (PIMAGE_DOS_HEADER)BaseAddress; + + if (DosHeader && DosHeader->e_magic != IMAGE_DOS_SIGNATURE) + { + DPRINT1("DosHeader->e_magic %x\n", DosHeader->e_magic); + DPRINT1("NtHeader %x\n", (BaseAddress + DosHeader->e_lfanew)); + } + + if (DosHeader && DosHeader->e_magic == IMAGE_DOS_SIGNATURE) + { + NtHeader = (PIMAGE_NT_HEADERS)(BaseAddress + DosHeader->e_lfanew); + if (NtHeader->Signature == IMAGE_NT_SIGNATURE) + return NtHeader; + } + + return NULL; +} + + +/* + * @implemented + */ +PVOID +STDCALL +RtlImageDirectoryEntryToData ( + PVOID BaseAddress, + BOOLEAN bFlag, + ULONG Directory, + PULONG Size + ) +{ + PIMAGE_NT_HEADERS NtHeader; + ULONG Va; + + NtHeader = RtlImageNtHeader (BaseAddress); + if (NtHeader == NULL) + return NULL; + + if (Directory >= NtHeader->OptionalHeader.NumberOfRvaAndSizes) + return NULL; + + Va = NtHeader->OptionalHeader.DataDirectory[Directory].VirtualAddress; + if (Va == 0) + return NULL; + + if (Size) + *Size = NtHeader->OptionalHeader.DataDirectory[Directory].Size; + + if (bFlag) + return (PVOID)(BaseAddress + Va); + + /* image mapped as ordinary file, we must find raw pointer */ + return (PVOID)RtlImageRvaToVa (NtHeader, BaseAddress, Va, NULL); +} + + +/* + * @implemented + */ +PIMAGE_SECTION_HEADER +STDCALL +RtlImageRvaToSection ( + PIMAGE_NT_HEADERS NtHeader, + PVOID BaseAddress, + ULONG Rva + ) +{ + PIMAGE_SECTION_HEADER Section; + ULONG Va; + ULONG Count; + + Count = NtHeader->FileHeader.NumberOfSections; + Section = (PIMAGE_SECTION_HEADER)((ULONG)&NtHeader->OptionalHeader + + NtHeader->FileHeader.SizeOfOptionalHeader); + while (Count) + { + Va = Section->VirtualAddress; + if ((Va <= Rva) && + (Rva < Va + Section->SizeOfRawData)) + return Section; + Section++; + } + return NULL; +} + + +/* + * @implemented + */ +ULONG +STDCALL +RtlImageRvaToVa ( + PIMAGE_NT_HEADERS NtHeader, + PVOID BaseAddress, + ULONG Rva, + PIMAGE_SECTION_HEADER *SectionHeader + ) +{ + PIMAGE_SECTION_HEADER Section = NULL; + + if (SectionHeader) + Section = *SectionHeader; + + if (Section == NULL || + Rva < Section->VirtualAddress || + Rva >= Section->VirtualAddress + Section->SizeOfRawData) + { + Section = RtlImageRvaToSection (NtHeader, BaseAddress, Rva); + if (Section == NULL) + return 0; + + if (SectionHeader) + *SectionHeader = Section; + } + + return (ULONG)(BaseAddress + + Rva + + Section->PointerToRawData - + Section->VirtualAddress); +} + +/* EOF */ diff --git a/reactos/lib/rtl/makefile b/reactos/lib/rtl/makefile index dc94c041136..e11603524f2 100644 --- a/reactos/lib/rtl/makefile +++ b/reactos/lib/rtl/makefile @@ -30,7 +30,10 @@ TARGET_OBJECTS = \ time.o \ timezone.o \ unicode.o \ - version.o + version.o \ + encode.o \ + image.o \ + splaytree.o # atom # registry diff --git a/reactos/lib/rtl/splaytree.c b/reactos/lib/rtl/splaytree.c new file mode 100644 index 00000000000..49d738b3af3 --- /dev/null +++ b/reactos/lib/rtl/splaytree.c @@ -0,0 +1,70 @@ +/* + * ReactOS kernel + * Copyright (C) 2003 ReactOS Team + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ +/* $Id: splaytree.c,v 1.1 2004/06/20 23:27:21 gdalsnes Exp $ + * + * COPYRIGHT: See COPYING in the top level directory + * PROJECT: ReactOS system libraries + * PURPOSE: Splay-Tree implementation + * FILE: lib/rtl/splaytree.c + */ + +#include + +/* FUNCTIONS *****************************************************************/ + +PRTL_SPLAY_LINKS STDCALL +RtlSubtreePredecessor (IN PRTL_SPLAY_LINKS Links) +{ + PRTL_SPLAY_LINKS Child; + + Child = Links->RightChild; + if (Child == NULL) + return NULL; + + if (Child->LeftChild == NULL) + return Child; + + /* Get left-most child */ + while (Child->LeftChild != NULL) + Child = Child->LeftChild; + + return Child; +} + + +PRTL_SPLAY_LINKS STDCALL +RtlSubtreeSuccessor (IN PRTL_SPLAY_LINKS Links) +{ + PRTL_SPLAY_LINKS Child; + + Child = Links->LeftChild; + if (Child == NULL) + return NULL; + + if (Child->RightChild == NULL) + return Child; + + /* Get right-most child */ + while (Child->RightChild != NULL) + Child = Child->RightChild; + + return Child; +} + +/* EOF */