├── .gitignore ├── COPYING ├── COPYING.ReactOS ├── COPYING.wine ├── Makefile ├── README.md ├── asmdefs.inc ├── autochk.c ├── common.mak ├── libs └── ntdll │ ├── Makefile │ ├── _aulldvrm.asm │ ├── _chkstk.asm │ ├── bitmap.c │ ├── bitscan.asm │ ├── crc32.c │ ├── crc64.c │ ├── crt.c │ ├── dllmain.c │ ├── format.c │ ├── ftable.h │ ├── heap.c │ ├── io.c │ ├── ioctl.c │ ├── large_int.c │ ├── lznt1.c │ ├── mm.c │ ├── ntdll.c │ ├── ntdll.dll.def │ ├── ntdll.h │ ├── random.c │ ├── registry.c │ ├── rtl.c │ ├── scancodes.c │ ├── sd.c │ ├── splay.c │ ├── str.c │ ├── sync.c │ ├── thread.c │ ├── time.c │ ├── timefields.c │ ├── version.c │ ├── wine_casemap.h │ └── winternl.h ├── loader.c ├── msvc ├── .gitignore ├── libs │ └── ntdll │ │ ├── ntdll.def │ │ ├── ntdll.vcxproj │ │ └── ntdll.vcxproj.filters ├── nloader.sln ├── nloader.vcxproj └── nloader.vcxproj.filters ├── nloader.c ├── nt_compat.h ├── nt_structs.h ├── stubs.asm ├── upcase.py └── volumeinfo.c /.gitignore: -------------------------------------------------------------------------------- 1 | *~ 2 | *.o 3 | *.a 4 | *.d 5 | *.so 6 | *.swp 7 | *.log 8 | *.exe 9 | *.lznt1 10 | *.dSYM 11 | nloader 12 | autochk 13 | lznt1 14 | disk.img 15 | .gdbinit 16 | -------------------------------------------------------------------------------- /COPYING: -------------------------------------------------------------------------------- 1 | Copyright (c) 2010, Sherpya , aCaB 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | * Redistributions of source code must retain the above copyright 7 | notice, this list of conditions and the following disclaimer. 8 | * Redistributions in binary form must reproduce the above copyright 9 | notice, this list of conditions and the following disclaimer in the 10 | documentation and/or other materials provided with the distribution. 11 | * Neither the name of the copyright holders nor the 12 | names of its contributors may be used to endorse or promote products 13 | derived from this software without specific prior written permission. 14 | 15 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 16 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY 19 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 22 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | top=. 2 | include common.mak 3 | include libs/ntdll/Makefile 4 | 5 | all:: $(LIBS) $(STATIC_LIBS) 6 | 7 | clean: 8 | rm -f *.o *.d *.dSYM nloader$(EXE) autochk lznt1$(EXE) volumeinfo.exe 9 | rm -f $(LIBS) $(STATIC_LIBS) $(CLEAN_TARGETS) 10 | 11 | disk.img: 12 | #dd if=/dev/zero of=disk.img bs=1M count=0 seek=20480 13 | #dd if=/dev/zero of=disk.img bs=1M count=0 seek=4096 14 | #dd if=/dev/zero of=disk.img bs=1M count=128 15 | dd if=/dev/zero of=disk.img bs=1 count=0 seek=2072738304 16 | $(MKNTFS) -Ff -p 63 -L Native -H 255 -s 512 -S 63 disk.img 17 | 18 | nloader$(EXE): nloader.o loader.o stubs.o $(LIBS) 19 | $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ 20 | 21 | autochk_blob.o: autochk.exe 22 | objcopy -B i386 -I binary -O elf32-i386 $^ $@ 23 | 24 | autochk: autochk.o loader.o stubs.o autochk_blob.o $(STATIC_LIBS) 25 | $(CC) $(CFLAGS) $(LDALONE) $(LDFLAGS) -o $@ $^ 26 | $(STRIP) $@ 27 | 28 | lznt1$(EXE): libs/ntdll/lznt1.c 29 | $(CC) -DMAIN $(CFLAGS) $(LDFLAGS) -o $@ $^ 30 | 31 | volumeinfo.exe: volumeinfo.o 32 | $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ -lntdll 33 | 34 | -include *.d 35 | .PHONY: all clean 36 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Native win32 executables Loader 2 | =============================== 3 | 4 | How it works 5 | ------------ 6 | 7 | The loader reads the target executable then it allocates and maps into memory 8 | each of its sections. 9 | 10 | It parses the import table and, for each required runtime library it 11 | attempts to dlopen a corresponding shared lib. Since native executables run 12 | inside a rather simple environment, the only runtime library they often rely upon 13 | is ntdll.dll (which gets dlopen'd as ntdll.dll.so). 14 | 15 | For each imported function a symbol lookup is performed and the 16 | corresponding import table entry is patched with the address of a small wrapper 17 | function. If the symbol is found the wrapper logs the call and jumps to the 18 | actual target. If the symbol is missing, then the wrapper complains about that 19 | and calls `abort()`. 20 | 21 | It fills in the `PEB` and `TEB` structures and make sure the latter is 22 | available via `fs:[]`. 23 | 24 | Next it allocates the stack and map `KUSER_SHARED_DATA` at `0x7ffe0000` where some 25 | static crt code expects it to be located. 26 | 27 | Finally, it jumps to the `EP` and lets the alien code do its thing. 28 | 29 | 30 | How to compile 31 | -------------- 32 | 33 | You need GNU make (sometimes called gmake) and yasm. 34 | Simply run make from the top source directory. 35 | 36 | If you are on Linux OS and provide an autochk.exe by placing it at the toplevel 37 | directory, the makefile will make for you a standalone executable that also contains 38 | the native Windows executable, suitable for a Live CD. 39 | 40 | You can find a suitable autochk.exe directly in windows 7 sp1 x86 (KB976932). 41 | Just download windows6.1-KB976932-X86.exe (unfortunately you can only pick the link 42 | after verifying that your windows is genuine - wow!). 43 | 44 | Then you can pick directly the needed executable with cabextract: 45 |
 46 | cabextract -p -F "*autochk.exe" windows6.1-KB976932-X86.exe > autochk.exe
 47 | 
48 | 49 | 50 | How to run 51 | ---------- 52 | 53 | First of all you need a native executable. If you want to run autochk.exe pick 54 | the one from W7. Threading is not properly implemented and, for some weird 55 | reason, all previous versions rely on it. 56 | 57 | Then simply execute: ./nloader autochk.exe -p disk.img 58 | 59 | NOTE: -p option will force the check even if the disk is not flagged as dirty 60 | 61 | You should get something like: 62 | 63 |
 64 | The type of the file system is NTFS.
 65 | Volume label is sheghey. (guarda che me ne so accorto)
 66 | 
 67 | One of your disks needs to be checked for consistency. You
 68 | may cancel the disk check, but it is strongly recommended
 69 | that you continue.
 70 | Windows will now check the disk.
 71 | 
 72 | CHKDSK is verifying files (stage 1 of 3)...
 73 | 
 74 | File verification completed.
 75 | 
 76 | CHKDSK is verifying indexes (stage 2 of 3)...
 77 | Deleting index entry System Volume Information in index $I30 of file 5.
 78 | 
 79 | Index verification completed.
 80 | 
 81 | CHKDSK is verifying security descriptors (stage 3 of 3)...
 82 | 
 83 | Security descriptor verification completed.
 84 | 
 85 | Windows has checked the file system and found no problems.
 86 | 
 87 |    2094079 KB total disk space.
 88 |     854716 KB in 4754 files.
 89 |       1808 KB in 302 indexes.
 90 |          0 KB in bad sectors.
 91 |      18379 KB in use by the system.
 92 |      12528 KB occupied by the log file.
 93 |    1219176 KB available on disk.
 94 | 
 95 |       4096 bytes in each allocation unit.
 96 |     523519 total allocation units on disk.
 97 |     304794 allocation units available on disk.
 98 | 
99 | 100 | 101 | If you want to trace all api calls, you can enable debug logging via the NLOG 102 | environment variable. 103 | Note that this can result in a lot of spam, expecially if you set NLOG=all. 104 | 105 | 106 | ### guessed autochk.exe cmdline arguments 107 | 108 | References: 109 | 110 | - [Chkdsk, ChkNTFS, and Autochk ](http://www.infocellar.com/winxp/chkdsk-and-autochk.htm) 111 | - [Using Chkntfs to Prevent Autochk from Running](http://windows-xp-dox.net/MS.Press-Microsoft.Windows.XP1/prkd_tro_rgwn.htm) 112 | - [Description of Enhanced Chkdsk, Autochk, and Chkntfs Tools in Windows 2000](http://support.microsoft.com/kb/218461) 113 | - [CHKNTFS.EXE: What You Can Use It For](http://support.microsoft.com/kb/160963/EN-US/) 114 | 115 |
116 | autochk.exe [switches] volume | * (* = all volumes, it queries global directory)
117 | 
118 |     -t           - unknown (w7)
119 |     -s           - silent execution (w7)
120 |     -p           - force check even if dirty bit is not set
121 |     -r           - locate and recover bad sectors, implies -p (untested)
122 |     -b           - re-evaluates bad clusters on the volume, implies -r (w7)
123 |     -x volume    - force dismount (untested), without arguments crashes
124 |     -lXX         - with some values like 10 prints info and log size of the volume (the value is mul by 1024)
125 |     -l:XX        - tries to set log size, always fails for me saying the size is too small or unable to adjust
126 |     -k:volume    - excludes volume from the check (does make sense when using *)
127 |     -m           - run chkdsk only if dirty bit is set (default ?)
128 |     -i           - Performs a less vigorous check of index entries
129 |     -i[:XX]      - XX = 0-50 - unknown
130 |     -c           - Skips the checking of cycles within the folder structure
131 | 
132 | 133 | 134 | Compatibility 135 | ------------ 136 | 137 | We've tested a few native executable and the results are quite variable. Some 138 | work perfectly, some aborts soon due to some unimplemented API, some totally 139 | misbehave, likely due some limitations in the emu layer. 140 | -------------------------------------------------------------------------------- /asmdefs.inc: -------------------------------------------------------------------------------- 1 | %ifidn __OUTPUT_FORMAT__,win32 2 | %define mangle(x) _ %+ x 3 | %define RODATA .rdata 4 | %elifidn __OUTPUT_FORMAT__,macho32 5 | %define mangle(x) _ %+ x 6 | %define RODATA .rodata 7 | %else 8 | %define mangle(x) x 9 | %define RODATA .rodata 10 | %endif 11 | 12 | %macro cextern 1 13 | %xdefine %1 mangle(%1) 14 | extern %1 15 | %endmacro 16 | 17 | %macro cfunction 1 18 | %xdefine %1 mangle(%1) 19 | global %1:function 20 | %1: 21 | %endmacro 22 | -------------------------------------------------------------------------------- /autochk.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010, Sherpya , aCaB 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of the copyright holders nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | #define _GNU_SOURCE 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | #include 35 | 36 | #include "nt_structs.h" 37 | 38 | MODULE(main) 39 | 40 | extern uint8_t autochk_data[] asm("_binary_autochk_exe_start"); 41 | extern uint8_t autochk_data_size[] asm("_binary_autochk_exe_size"); 42 | extern uint8_t autochk_data_end[] asm("_binary_autochk_exe_end"); 43 | 44 | static jmp_buf sigsegv_env; 45 | static void sigsegv_handler(int signum) 46 | { 47 | longjmp(sigsegv_env, 1); 48 | } 49 | 50 | static void usage(void) { 51 | printf( 52 | "\nautochk.exe cmdline arguments (guessed)\n" 53 | " refs:\n" 54 | " - http://www.infocellar.com/winxp/chkdsk-and-autochk.htm\n" 55 | " - http://support.microsoft.com/kb/218461\n" 56 | " - http://support.microsoft.com/kb/160963/EN-US/\n" 57 | " - http://technet.microsoft.com/en-us/library/cc771787%%28v=ws.10%%29.aspx\n" 58 | "\n" 59 | "autochk.exe [switches] volume | *\n" 60 | " * = all volumes, it queries global directory\n" 61 | "\n" 62 | " -t:timeout - timeout: 0 seconds up to 3 days (259,200) (w7)\n" 63 | " -v - verbose execution (w7?)\n" 64 | " -s - silent execution (w7)\n" 65 | " -p - force check even if dirty bit is not set\n" 66 | " -r - locate and recover bad sectors, implies -p (untested)\n" 67 | " -b - re-evaluates bad clusters on the volume, implies -r (w7)\n" 68 | " -x:volume - force dismount (untested), without arguments crashes\n" 69 | " -lXX - with some values like 10 prints info and log size of the volume (the value is mul by 1024)\n" 70 | " -l:XX - tries to set log size, always fails for me saying the size is too small or unable to adjust\n" 71 | " -k:volume - excludes volume from the check (does make sense when using *)\n" 72 | " -m - run chkdsk only if dirty bit is set (default ?)\n" 73 | " -i - Performs a less vigorous check of index entries\n" 74 | " -i[:XX] - XX = 0-50 - unknown\n" 75 | " -c - Skips the checking of cycles within the folder structure\n" 76 | "\n" 77 | "NOTE: I'm not responsable at all for any damage you can make on your filesystem\n" 78 | ". (a dot) means the filesystem is clean, if you want to force the check you should specify -p cmdline option\n" 79 | "\n" 80 | ); 81 | 82 | exit(1); 83 | } 84 | 85 | int main (int argc, char **argv) { 86 | 87 | unsigned int i; 88 | WCHAR commandline[1024] = L"autochk.exe *"; 89 | 90 | uint8_t *ptr; 91 | void *ep; 92 | RTL_USER_PROCESS_PARAMETERS *params; 93 | size_t autochk_size = (size_t)((void *) autochk_data_size); 94 | 95 | const char *executable = "autochk.exe"; 96 | 97 | ep = setup_nloader(autochk_data, autochk_size, ¶ms, 1); 98 | 99 | if (!ep) 100 | { 101 | fprintf(stderr, "setup_nloader failed\n"); 102 | return 1; 103 | } 104 | 105 | ptr = (uint8_t *)(params+1); 106 | params->ImagePathName.Buffer = (WCHAR *) ptr; 107 | 108 | for (i = 0; i < strlen(executable); i++) 109 | params->ImagePathName.Buffer[i] = executable[i]; 110 | 111 | params->ImagePathName.Length = i; 112 | params->ImagePathName.MaximumLength = i + 1; 113 | 114 | 115 | if (argc > 1) 116 | { 117 | int c = params->ImagePathName.Length; 118 | memcpy(commandline, params->ImagePathName.Buffer, params->ImagePathName.Length * sizeof(WCHAR)); 119 | 120 | for (i = 1; i < (unsigned) argc; i++) 121 | { 122 | char *arg = argv[i]; 123 | int dev = 0; 124 | commandline[c++] = L' '; 125 | if (!strncmp(arg, "/dev/", 4)) 126 | { 127 | size_t len = sizeof(VOLUME_PREFIX) - sizeof(WCHAR); 128 | dev = 1; 129 | memcpy(&commandline[c], VOLUME_PREFIX, len); 130 | len /= sizeof(WCHAR); 131 | c += len; 132 | } 133 | 134 | while (*arg && (c < sizeof(commandline) - 1)) 135 | commandline[c++] = *arg++; 136 | 137 | if (dev) commandline[c++] = L'}'; 138 | } 139 | commandline[c++] = 0; 140 | } 141 | else 142 | usage(); 143 | 144 | ptr += sizeof(WCHAR); 145 | memcpy(ptr, commandline, sizeof(commandline)); 146 | params->CommandLine.Length = sizeof(commandline) - sizeof(WCHAR); 147 | params->CommandLine.MaximumLength = sizeof(commandline); 148 | params->CommandLine.Buffer = (WCHAR *) ptr; 149 | 150 | /******************************************************************************************************* CALL ENTRYPOINT */ 151 | signal(SIGSEGV, sigsegv_handler); 152 | 153 | if (setjmp(sigsegv_env)) 154 | { 155 | /* clean up console */ 156 | fflush(stdout); 157 | fprintf(stderr, "Native program crashed\n"); 158 | exit(1); 159 | } 160 | else 161 | { 162 | BPX(); 163 | return to_ep(ep); 164 | } 165 | } 166 | -------------------------------------------------------------------------------- /common.mak: -------------------------------------------------------------------------------- 1 | # compiler detection 2 | COMPILER = "$(shell $(CC) --version | head -1)" 3 | 4 | CFLAGS = -I$(top) 5 | CFLAGS += -D_FILE_OFFSET_BITS=64 6 | CFLAGS += -m32 7 | CFLAGS += -O0 -g3 -Wall 8 | CFLAGS += -DLIBNLOADER=\"/usr/lib/nloader\" 9 | 10 | DEPFLAGS += -MMD -MF $(@:.o=.d) -MT $@ 11 | 12 | # glibc decided to crash in strcasestr using sse2 code 13 | ifneq (,$(findstring clang, $(COMPILER))) 14 | CFLAGS += -mstackrealign 15 | else ifneq (,$(findstring cc, $(COMPILER))) 16 | CFLAGS += -mincoming-stack-boundary=2 17 | endif 18 | 19 | ifdef RELEASE 20 | CFLAGS += -O2 21 | STRIP = strip --strip-unneeded 22 | else 23 | STRIP = : 24 | endif 25 | 26 | TARGETS = nloader lznt1 libs 27 | 28 | RANLIB = ranlib 29 | MKNTFS = PATH=$(PATH):/sbin:/usr/sbin mkntfs 30 | 31 | YFMT = elf32 32 | YDBG = dwarf2 33 | ifdef USE_CCACHE 34 | CC := $(shell which ccache 2>/dev/null) $(CC) 35 | endif 36 | OS = $(shell uname -s) 37 | 38 | ifneq (,$(findstring Linux, $(OS))) 39 | TARGETS += disk.img 40 | CFLAGS += -fshort-wchar 41 | CFLAGS += -Werror 42 | LDFLAGS += -ldl 43 | MKSO = $(CC) $(CFLAGS) -shared 44 | ifneq (,$(wildcard autochk.exe)) 45 | TARGETS += autochk 46 | LDALONE = -rdynamic -Wl,--whole-archive -L$(top)/libs/ntdll -lntdll -Wl,--no-whole-archive 47 | endif 48 | else ifneq (,$(findstring FreeBSD, $(OS))) 49 | TARGETS += disk.img 50 | CFLAGS += -fshort-wchar 51 | MKSO = $(CC) $(CFLAGS) -shared 52 | YFLAGS = -Dstderr=__stderrp 53 | else ifneq (,$(findstring Darwin, $(OS))) 54 | CFLAGS += -fshort-wchar -mstackrealign 55 | LDFLAGS += -ldl -Wl,-read_only_relocs,suppress,-undefined,dynamic_lookup -single-module 56 | YFMT = macho32 57 | YDBG = null 58 | MKSO = $(CC) $(CFLAGS) $(LDFLAGS) -dynamiclib 59 | else ifneq (,$(findstring MINGW32, $(OS))) 60 | TARGETS += volumeinfo 61 | MKSO = dllwrap --def $(basename $(@F)).def 62 | EXE = .exe 63 | YFMT = win32 64 | else 65 | $(error $(OS) is not supported) 66 | endif 67 | 68 | #CFLAGS += -DDEBUG_HEAP 69 | #CFLAGS += -DDEBUG_CRT 70 | 71 | #CFLAGS += -DTHREADED -pthread 72 | 73 | #CFLAGS += -DREDIR_IO 74 | #CFLAGS += -DREDIR_SYSCALL 75 | 76 | all:: $(TARGETS) 77 | 78 | %.o: %.c Makefile $(top)/common.mak 79 | $(CC) $(CFLAGS) $(DEPFLAGS) -c -o $@ $< 80 | 81 | %.o: %.asm Makefile $(top)/common.mak $(top)/asmdefs.inc 82 | yasm $(YFLAGS) -I$(top) -g $(YDBG) -a x86 -f $(YFMT) -o $@ $< 83 | 84 | # disable most of builtin rules 85 | .SUFFIXES: 86 | -------------------------------------------------------------------------------- /libs/ntdll/Makefile: -------------------------------------------------------------------------------- 1 | 2 | ntdll_sources = $(wildcard $(top)/libs/ntdll/*.c) 3 | ntdll_objects = $(ntdll_sources:.c=.o) 4 | ntdll_asm = $(wildcard $(top)/libs/ntdll/*.asm) 5 | ntdll_objects += $(ntdll_asm:.asm=.o) 6 | ntdll_deps = $(ntdll_sources:.c=.d) 7 | 8 | LIBS += $(top)/libs/ntdll.dll.so 9 | STATIC_LIBS += $(top)/libs/ntdll/libntdll.a 10 | CLEAN_TARGETS += $(ntdll_objects) $(ntdll_deps) 11 | 12 | $(top)/libs/ntdll/libntdll.a: $(ntdll_objects) 13 | $(AR) cru $@ $(ntdll_objects) 14 | $(RANLIB) $@ 15 | 16 | $(top)/libs/ntdll.dll.so: $(ntdll_objects) 17 | $(MKSO) -o $@ $(ntdll_objects) 18 | 19 | -include $(ntdll_deps) 20 | -------------------------------------------------------------------------------- /libs/ntdll/_aulldvrm.asm: -------------------------------------------------------------------------------- 1 | ; * COPYRIGHT: See COPYING in the top level directory 2 | ; * PROJECT: ReactOS kernel 3 | ; * PURPOSE: Run-Time Library 4 | ; * FILE: lib/rtl/i386/aulldvrm.S 5 | ; * PROGRAMER: Alex Ionescu (alex@relsoft.net) 6 | ; * 7 | ; * Copyright (C) 2002 Michael Ringgaard. 8 | ; * All rights reserved. 9 | ; * 10 | ; * Redistribution and use in source and binary forms, with or without 11 | ; * modification, are permitted provided that the following conditions 12 | ; * are met: 13 | ; * 14 | ; * 1. Redistributions of source code must retain the above copyright 15 | ; * notice, this list of conditions and the following disclaimer. 16 | ; * 2. Redistributions in binary form must reproduce the above copyright 17 | ; * notice, this list of conditions and the following disclaimer in the 18 | ; * documentation and/or other materials provided with the distribution. 19 | ; * 3. Neither the name of the project nor the names of its contributors 20 | ; * may be used to endorse or promote products derived from this software 21 | ; * without specific prior written permission. 22 | ; 23 | ; * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 24 | ; * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 | ; * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 | ; * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 27 | ; * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 | ; * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 | ; * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 | ; * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 | ; * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 | ; * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 | ; * SUCH DAMAGE. 34 | 35 | %include "asmdefs.inc" 36 | 37 | cfunction _aulldvrm 38 | 39 | ; ulldvrm - unsigned long divide and remainder 40 | ; 41 | ; Purpose: 42 | ; Does a unsigned long divide and remainder of the arguments. Arguments 43 | ; are not changed. 44 | ; 45 | ; Entry: 46 | ; Arguments are passed on the stack: 47 | ; 1st pushed: divisor (QWORD) 48 | ; 2nd pushed: dividend (QWORD) 49 | ; 50 | ; Exit: 51 | ; EDX:EAX contains the quotient (dividend/divisor) 52 | ; EBX:ECX contains the remainder (divided % divisor) 53 | ; NOTE: this routine removes the parameters from the stack. 54 | ; 55 | ; Uses: 56 | ; ECX 57 | 58 | push esi 59 | 60 | ; Set up the local stack and save the index registers. When this is done 61 | ; the stack frame will look as follows (assuming that the expression a/b will 62 | ; generate a call to aulldvrm(a, b)): 63 | ; 64 | ; ----------------- 65 | ; | | 66 | ; |---------------| 67 | ; | | 68 | ; |--divisor (b)--| 69 | ; | | 70 | ; |---------------| 71 | ; | | 72 | ; |--dividend (a)-| 73 | ; | | 74 | ; |---------------| 75 | ; | return addr** | 76 | ; |---------------| 77 | ; ESP---->| ESI | 78 | ; ----------------- 79 | 80 | 81 | %define DVNDLO [esp + 8] ; stack address of dividend (a) 82 | %define DVNDHI [esp + 12] ; stack address of dividend (a) 83 | %define DVSRLO [esp + 16] ; stack address of divisor (b) 84 | %define DVSRHI [esp + 20] ; stack address of divisor (b) 85 | 86 | 87 | ; Now do the divide. First look to see if the divisor is less than 4194304K. 88 | ; If so, then we can use a simple algorithm with word divides, otherwise 89 | ; things get a little more complex. 90 | 91 | mov eax,DVSRHI ; check to see if divisor < 4194304K 92 | or eax,eax 93 | jnz short .L1 ; nope, gotta do this the hard way 94 | mov ecx,DVSRLO ; load divisor 95 | mov eax,DVNDHI ; load high word of dividend 96 | xor edx,edx 97 | div ecx ; get high order bits of quotient 98 | mov ebx,eax ; save high bits of quotient 99 | mov eax,DVNDLO ; edx:eax <- remainder:lo word of dividend 100 | div ecx ; get low order bits of quotient 101 | mov esi,eax ; ebx:esi <- quotient 102 | 103 | ; Now we need to do a multiply so that we can compute the remainder. 104 | 105 | mov eax,ebx ; set up high word of quotient 106 | mul dword DVSRLO ; HIWORD(QUOT) * DVSR 107 | mov ecx,eax ; save the result in ecx 108 | mov eax,esi ; set up low word of quotient 109 | mul dword DVSRLO ; LOWORD(QUOT) * DVSR 110 | add edx,ecx ; EDX:EAX = QUOT * DVSR 111 | jmp short .L2 ; complete remainder calculation 112 | 113 | ; Here we do it the hard way. Remember, eax contains DVSRHI 114 | 115 | .L1: 116 | mov ecx,eax ; ecx:ebx <- divisor 117 | mov ebx,DVSRLO 118 | mov edx,DVNDHI ; edx:eax <- dividend 119 | mov eax,DVNDLO 120 | .L3: 121 | shr ecx,1 ; shift divisor right one bit; hi bit <- 0 122 | rcr ebx,1 123 | shr edx,1 ; shift dividend right one bit; hi bit <- 0 124 | rcr eax,1 125 | or ecx,ecx 126 | jnz short .L3 ; loop until divisor < 4194304K 127 | div ebx ; now divide, ignore remainder 128 | mov esi,eax ; save quotient 129 | 130 | ; We may be off by one, so to check, we will multiply the quotient 131 | ; by the divisor and check the result against the orignal dividend 132 | ; Note that we must also check for overflow, which can occur if the 133 | ; dividend is close to 2**64 and the quotient is off by 1. 134 | 135 | mul dword DVSRHI ; QUOT * DVSRHI 136 | mov ecx,eax 137 | mov eax,DVSRLO 138 | mul esi ; QUOT * DVSRLO 139 | add edx,ecx ; EDX:EAX = QUOT * DVSR 140 | jc short .L4 ; carry means Quotient is off by 1 141 | 142 | ; do long compare here between original dividend and the result of the 143 | ; multiply in edx:eax. If original is larger or equal, we are ok, otherwise 144 | ; subtract one (1) from the quotient. 145 | 146 | cmp edx,DVNDHI ; compare hi words of result and original 147 | ja short .L4 ; if result > original, do subtract 148 | jb short .L5 ; if result < original, we are ok 149 | cmp eax,DVNDLO ; hi words are equal, compare lo words 150 | jbe short .L5 ; if less or equal we are ok, else subtract 151 | .L4: 152 | dec esi ; subtract 1 from quotient 153 | sub eax,DVSRLO ; subtract divisor from result 154 | sbb edx,DVSRHI 155 | .L5: 156 | xor ebx,ebx ; ebx:esi <- quotient 157 | 158 | .L2: 159 | 160 | ; Calculate remainder by subtracting the result from the original dividend. 161 | ; Since the result is already in a register, we will do the subtract in the 162 | ; opposite direction and negate the result. 163 | 164 | sub eax,DVNDLO ; subtract dividend from result 165 | sbb edx,DVNDHI 166 | neg edx ; otherwise, negate the result 167 | neg eax 168 | sbb edx,0 169 | 170 | ; Now we need to get the quotient into edx:eax and the remainder into ebx:ecx. 171 | mov ecx,edx 172 | mov edx,ebx 173 | mov ebx,ecx 174 | mov ecx,eax 175 | mov eax,esi 176 | 177 | ; Just the cleanup left to do. edx:eax contains the quotient. 178 | ; Restore the saved registers and return. 179 | 180 | pop esi 181 | ret 16 182 | -------------------------------------------------------------------------------- /libs/ntdll/_chkstk.asm: -------------------------------------------------------------------------------- 1 | ; * Copyright (c) 2010, Sherpya , aCaB 2 | ; * All rights reserved. 3 | ; * 4 | ; * Redistribution and use in source and binary forms, with or without 5 | ; * modification, are permitted provided that the following conditions are met: 6 | ; * * Redistributions of source code must retain the above copyright 7 | ; * notice, this list of conditions and the following disclaimer. 8 | ; * * Redistributions in binary form must reproduce the above copyright 9 | ; * notice, this list of conditions and the following disclaimer in the 10 | ; * documentation and/or other materials provided with the distribution. 11 | ; * * Neither the name of the copyright holders nor the 12 | ; * names of its contributors may be used to endorse or promote products 13 | ; * derived from this software without specific prior written permission. 14 | ; * 15 | ; * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 16 | ; * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 | ; * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | ; * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY 19 | ; * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 | ; * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 | ; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 22 | ; * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | ; * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | ; * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | 26 | %include "asmdefs.inc" 27 | 28 | %ifidn __OUTPUT_FORMAT__,win32 29 | 30 | SECTION .text 31 | cfunction _chkstk 32 | push ecx 33 | mov ecx, esp 34 | add ecx, 8 35 | 36 | alloca_probe: 37 | cmp eax, 0x1000 38 | jb .done 39 | sub ecx, 0x1000 40 | or dword [ecx], 0 41 | sub eax, 0x1000 42 | jmp alloca_probe 43 | 44 | .done: 45 | sub ecx, eax 46 | or dword [ecx], 0 47 | mov eax, esp 48 | mov esp, ecx 49 | mov ecx, dword [eax] 50 | mov eax, dword [eax+4] 51 | jmp eax 52 | 53 | %else 54 | 55 | SECTION .rodata 56 | msg_stackover db 'Stack overflow, ', 0 57 | 58 | cextern stderr 59 | cextern fputs 60 | cextern abort 61 | 62 | SECTION .text 63 | cfunction _chkstk 64 | push ecx ; avoid clobbering ecx 65 | mov ecx, [fs:0x18] ; teb self 66 | neg eax 67 | mov ecx, [ecx+8] ; absolute stack top 68 | lea eax, [eax*1+esp+4] ; new stack 69 | cmp eax, ecx ; nuff stack space ? 70 | pop ecx ; restore ecx 71 | jb .stack_smash 72 | xchg eax, esp ; all good 73 | mov eax, [eax] 74 | jmp eax 75 | 76 | .stack_smash: ; no more stack 77 | push dword [stderr] 78 | push msg_stackover 79 | call fputs 80 | call abort 81 | 82 | %endif 83 | -------------------------------------------------------------------------------- /libs/ntdll/bitscan.asm: -------------------------------------------------------------------------------- 1 | ; * Copyright (c) 2010, Sherpya , aCaB 2 | ; * All rights reserved. 3 | ; * 4 | ; * Redistribution and use in source and binary forms, with or without 5 | ; * modification, are permitted provided that the following conditions are met: 6 | ; * * Redistributions of source code must retain the above copyright 7 | ; * notice, this list of conditions and the following disclaimer. 8 | ; * * Redistributions in binary form must reproduce the above copyright 9 | ; * notice, this list of conditions and the following disclaimer in the 10 | ; * documentation and/or other materials provided with the distribution. 11 | ; * * Neither the name of the copyright holders nor the 12 | ; * names of its contributors may be used to endorse or promote products 13 | ; * derived from this software without specific prior written permission. 14 | ; * 15 | ; * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 16 | ; * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 | ; * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | ; * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY 19 | ; * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 | ; * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 | ; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 22 | ; * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | ; * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | ; * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | 26 | %include "asmdefs.inc" 27 | 28 | SECTION .text 29 | 30 | cfunction BitScanForward 31 | mov eax, dword [esp+0x8] 32 | mov edx, dword [esp+0x4] 33 | bsf ecx, eax 34 | test eax, eax 35 | mov dword [edx], ecx 36 | setne al 37 | ret 38 | 39 | cfunction BitScanReverse 40 | mov eax, dword [esp+0x8] 41 | mov edx, dword [esp+0x4] 42 | bsr ecx, eax 43 | test eax, eax 44 | mov dword [edx], ecx 45 | setne al 46 | ret 47 | -------------------------------------------------------------------------------- /libs/ntdll/crc32.c: -------------------------------------------------------------------------------- 1 | /* 2 | * NT basis DLL 3 | * 4 | * This file contains the Rtl* API functions. These should be implementable. 5 | * 6 | * Copyright 1996-1998 Marcus Meissner 7 | * Copyright 1999 Alex Korobka 8 | * Copyright 2003 Thomas Mertes 9 | * Crc32 code Copyright 1986 Gary S. Brown (Public domain) 10 | * 11 | * This library is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | * 16 | * This library is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public 22 | * License along with this library; if not, write to the Free Software 23 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA 24 | */ 25 | 26 | #include "ntdll.h" 27 | 28 | /* CRC polynomial 0xedb88320 */ 29 | static const DWORD CRC_table[256] = 30 | { 31 | 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f, 32 | 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988, 33 | 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2, 34 | 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7, 35 | 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9, 36 | 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172, 37 | 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c, 38 | 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59, 39 | 0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, 40 | 0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924, 41 | 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106, 42 | 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433, 43 | 0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d, 44 | 0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e, 45 | 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950, 46 | 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65, 47 | 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7, 48 | 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0, 49 | 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa, 50 | 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f, 51 | 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81, 52 | 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a, 53 | 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84, 54 | 0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1, 55 | 0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb, 56 | 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc, 57 | 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e, 58 | 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b, 59 | 0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, 60 | 0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236, 61 | 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28, 62 | 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d, 63 | 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f, 64 | 0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38, 65 | 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242, 66 | 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777, 67 | 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69, 68 | 0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2, 69 | 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc, 70 | 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9, 71 | 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693, 72 | 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94, 73 | 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d 74 | }; 75 | 76 | DWORD NTAPI RtlComputeCrc32(DWORD dwInitial, const BYTE *pData, INT iLen) 77 | { 78 | DWORD crc = ~dwInitial; 79 | while (iLen > 0) 80 | { 81 | crc = CRC_table[(crc ^ *pData) & 0xff] ^ (crc >> 8); 82 | pData++; 83 | iLen--; 84 | } 85 | return ~crc; 86 | } 87 | 88 | // Windows 8 - RtlComputeCrc32() ? 89 | DWORD NTAPI RtlCrc32(const BYTE *pData, INT iLen, DWORD dwInitial) 90 | { 91 | return RtlComputeCrc32(dwInitial, pData, iLen); 92 | } 93 | -------------------------------------------------------------------------------- /libs/ntdll/crc64.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010-2013, Sherpya , aCaB 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of the copyright holders nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | #include "ntdll.h" 29 | 30 | static const ULONGLONG CRC_table[256] = 31 | { 32 | 0x0000000000000000LL, 0x42F0E1EBA9EA3693LL, 0x85E1C3D753D46D26LL, 0xC711223CFA3E5BB5LL, 33 | 0x493366450E42ECDFLL, 0x0BC387AEA7A8DA4CLL, 0xCCD2A5925D9681F9LL, 0x8E224479F47CB76ALL, 34 | 0x9266CC8A1C85D9BELL, 0xD0962D61B56FEF2DLL, 0x17870F5D4F51B498LL, 0x5577EEB6E6BB820BLL, 35 | 0xDB55AACF12C73561LL, 0x99A54B24BB2D03F2LL, 0x5EB4691841135847LL, 0x1C4488F3E8F96ED4LL, 36 | 0x663D78FF90E185EFLL, 0x24CD9914390BB37CLL, 0xE3DCBB28C335E8C9LL, 0xA12C5AC36ADFDE5ALL, 37 | 0x2F0E1EBA9EA36930LL, 0x6DFEFF5137495FA3LL, 0xAAEFDD6DCD770416LL, 0xE81F3C86649D3285LL, 38 | 0xF45BB4758C645C51LL, 0xB6AB559E258E6AC2LL, 0x71BA77A2DFB03177LL, 0x334A9649765A07E4LL, 39 | 0xBD68D2308226B08ELL, 0xFF9833DB2BCC861DLL, 0x388911E7D1F2DDA8LL, 0x7A79F00C7818EB3BLL, 40 | 0xCC7AF1FF21C30BDELL, 0x8E8A101488293D4DLL, 0x499B3228721766F8LL, 0x0B6BD3C3DBFD506BLL, 41 | 0x854997BA2F81E701LL, 0xC7B97651866BD192LL, 0x00A8546D7C558A27LL, 0x4258B586D5BFBCB4LL, 42 | 0x5E1C3D753D46D260LL, 0x1CECDC9E94ACE4F3LL, 0xDBFDFEA26E92BF46LL, 0x990D1F49C77889D5LL, 43 | 0x172F5B3033043EBFLL, 0x55DFBADB9AEE082CLL, 0x92CE98E760D05399LL, 0xD03E790CC93A650ALL, 44 | 0xAA478900B1228E31LL, 0xE8B768EB18C8B8A2LL, 0x2FA64AD7E2F6E317LL, 0x6D56AB3C4B1CD584LL, 45 | 0xE374EF45BF6062EELL, 0xA1840EAE168A547DLL, 0x66952C92ECB40FC8LL, 0x2465CD79455E395BLL, 46 | 0x3821458AADA7578FLL, 0x7AD1A461044D611CLL, 0xBDC0865DFE733AA9LL, 0xFF3067B657990C3ALL, 47 | 0x711223CFA3E5BB50LL, 0x33E2C2240A0F8DC3LL, 0xF4F3E018F031D676LL, 0xB60301F359DBE0E5LL, 48 | 0xDA050215EA6C212FLL, 0x98F5E3FE438617BCLL, 0x5FE4C1C2B9B84C09LL, 0x1D14202910527A9ALL, 49 | 0x93366450E42ECDF0LL, 0xD1C685BB4DC4FB63LL, 0x16D7A787B7FAA0D6LL, 0x5427466C1E109645LL, 50 | 0x4863CE9FF6E9F891LL, 0x0A932F745F03CE02LL, 0xCD820D48A53D95B7LL, 0x8F72ECA30CD7A324LL, 51 | 0x0150A8DAF8AB144ELL, 0x43A04931514122DDLL, 0x84B16B0DAB7F7968LL, 0xC6418AE602954FFBLL, 52 | 0xBC387AEA7A8DA4C0LL, 0xFEC89B01D3679253LL, 0x39D9B93D2959C9E6LL, 0x7B2958D680B3FF75LL, 53 | 0xF50B1CAF74CF481FLL, 0xB7FBFD44DD257E8CLL, 0x70EADF78271B2539LL, 0x321A3E938EF113AALL, 54 | 0x2E5EB66066087D7ELL, 0x6CAE578BCFE24BEDLL, 0xABBF75B735DC1058LL, 0xE94F945C9C3626CBLL, 55 | 0x676DD025684A91A1LL, 0x259D31CEC1A0A732LL, 0xE28C13F23B9EFC87LL, 0xA07CF2199274CA14LL, 56 | 0x167FF3EACBAF2AF1LL, 0x548F120162451C62LL, 0x939E303D987B47D7LL, 0xD16ED1D631917144LL, 57 | 0x5F4C95AFC5EDC62ELL, 0x1DBC74446C07F0BDLL, 0xDAAD56789639AB08LL, 0x985DB7933FD39D9BLL, 58 | 0x84193F60D72AF34FLL, 0xC6E9DE8B7EC0C5DCLL, 0x01F8FCB784FE9E69LL, 0x43081D5C2D14A8FALL, 59 | 0xCD2A5925D9681F90LL, 0x8FDAB8CE70822903LL, 0x48CB9AF28ABC72B6LL, 0x0A3B7B1923564425LL, 60 | 0x70428B155B4EAF1ELL, 0x32B26AFEF2A4998DLL, 0xF5A348C2089AC238LL, 0xB753A929A170F4ABLL, 61 | 0x3971ED50550C43C1LL, 0x7B810CBBFCE67552LL, 0xBC902E8706D82EE7LL, 0xFE60CF6CAF321874LL, 62 | 0xE224479F47CB76A0LL, 0xA0D4A674EE214033LL, 0x67C58448141F1B86LL, 0x253565A3BDF52D15LL, 63 | 0xAB1721DA49899A7FLL, 0xE9E7C031E063ACECLL, 0x2EF6E20D1A5DF759LL, 0x6C0603E6B3B7C1CALL, 64 | 0xF6FAE5C07D3274CDLL, 0xB40A042BD4D8425ELL, 0x731B26172EE619EBLL, 0x31EBC7FC870C2F78LL, 65 | 0xBFC9838573709812LL, 0xFD39626EDA9AAE81LL, 0x3A28405220A4F534LL, 0x78D8A1B9894EC3A7LL, 66 | 0x649C294A61B7AD73LL, 0x266CC8A1C85D9BE0LL, 0xE17DEA9D3263C055LL, 0xA38D0B769B89F6C6LL, 67 | 0x2DAF4F0F6FF541ACLL, 0x6F5FAEE4C61F773FLL, 0xA84E8CD83C212C8ALL, 0xEABE6D3395CB1A19LL, 68 | 0x90C79D3FEDD3F122LL, 0xD2377CD44439C7B1LL, 0x15265EE8BE079C04LL, 0x57D6BF0317EDAA97LL, 69 | 0xD9F4FB7AE3911DFDLL, 0x9B041A914A7B2B6ELL, 0x5C1538ADB04570DBLL, 0x1EE5D94619AF4648LL, 70 | 0x02A151B5F156289CLL, 0x4051B05E58BC1E0FLL, 0x87409262A28245BALL, 0xC5B073890B687329LL, 71 | 0x4B9237F0FF14C443LL, 0x0962D61B56FEF2D0LL, 0xCE73F427ACC0A965LL, 0x8C8315CC052A9FF6LL, 72 | 0x3A80143F5CF17F13LL, 0x7870F5D4F51B4980LL, 0xBF61D7E80F251235LL, 0xFD913603A6CF24A6LL, 73 | 0x73B3727A52B393CCLL, 0x31439391FB59A55FLL, 0xF652B1AD0167FEEALL, 0xB4A25046A88DC879LL, 74 | 0xA8E6D8B54074A6ADLL, 0xEA16395EE99E903ELL, 0x2D071B6213A0CB8BLL, 0x6FF7FA89BA4AFD18LL, 75 | 0xE1D5BEF04E364A72LL, 0xA3255F1BE7DC7CE1LL, 0x64347D271DE22754LL, 0x26C49CCCB40811C7LL, 76 | 0x5CBD6CC0CC10FAFCLL, 0x1E4D8D2B65FACC6FLL, 0xD95CAF179FC497DALL, 0x9BAC4EFC362EA149LL, 77 | 0x158E0A85C2521623LL, 0x577EEB6E6BB820B0LL, 0x906FC95291867B05LL, 0xD29F28B9386C4D96LL, 78 | 0xCEDBA04AD0952342LL, 0x8C2B41A1797F15D1LL, 0x4B3A639D83414E64LL, 0x09CA82762AAB78F7LL, 79 | 0x87E8C60FDED7CF9DLL, 0xC51827E4773DF90ELL, 0x020905D88D03A2BBLL, 0x40F9E43324E99428LL, 80 | 0x2CFFE7D5975E55E2LL, 0x6E0F063E3EB46371LL, 0xA91E2402C48A38C4LL, 0xEBEEC5E96D600E57LL, 81 | 0x65CC8190991CB93DLL, 0x273C607B30F68FAELL, 0xE02D4247CAC8D41BLL, 0xA2DDA3AC6322E288LL, 82 | 0xBE992B5F8BDB8C5CLL, 0xFC69CAB42231BACFLL, 0x3B78E888D80FE17ALL, 0x7988096371E5D7E9LL, 83 | 0xF7AA4D1A85996083LL, 0xB55AACF12C735610LL, 0x724B8ECDD64D0DA5LL, 0x30BB6F267FA73B36LL, 84 | 0x4AC29F2A07BFD00DLL, 0x08327EC1AE55E69ELL, 0xCF235CFD546BBD2BLL, 0x8DD3BD16FD818BB8LL, 85 | 0x03F1F96F09FD3CD2LL, 0x41011884A0170A41LL, 0x86103AB85A2951F4LL, 0xC4E0DB53F3C36767LL, 86 | 0xD8A453A01B3A09B3LL, 0x9A54B24BB2D03F20LL, 0x5D45907748EE6495LL, 0x1FB5719CE1045206LL, 87 | 0x919735E51578E56CLL, 0xD367D40EBC92D3FFLL, 0x1476F63246AC884ALL, 0x568617D9EF46BED9LL, 88 | 0xE085162AB69D5E3CLL, 0xA275F7C11F7768AFLL, 0x6564D5FDE549331ALL, 0x279434164CA30589LL, 89 | 0xA9B6706FB8DFB2E3LL, 0xEB46918411358470LL, 0x2C57B3B8EB0BDFC5LL, 0x6EA7525342E1E956LL, 90 | 0x72E3DAA0AA188782LL, 0x30133B4B03F2B111LL, 0xF7021977F9CCEAA4LL, 0xB5F2F89C5026DC37LL, 91 | 0x3BD0BCE5A45A6B5DLL, 0x79205D0E0DB05DCELL, 0xBE317F32F78E067BLL, 0xFCC19ED95E6430E8LL, 92 | 0x86B86ED5267CDBD3LL, 0xC4488F3E8F96ED40LL, 0x0359AD0275A8B6F5LL, 0x41A94CE9DC428066LL, 93 | 0xCF8B0890283E370CLL, 0x8D7BE97B81D4019FLL, 0x4A6ACB477BEA5A2ALL, 0x089A2AACD2006CB9LL, 94 | 0x14DEA25F3AF9026DLL, 0x562E43B4931334FELL, 0x913F6188692D6F4BLL, 0xD3CF8063C0C759D8LL, 95 | 0x5DEDC41A34BBEEB2LL, 0x1F1D25F19D51D821LL, 0xD80C07CD676F8394LL, 0x9AFCE626CE85B507LL 96 | }; 97 | 98 | // Windows 8 - FIXME: it's correct? 99 | ULONGLONG NTAPI RtlCrc64(const BYTE *pData, INT iLen, ULONGLONG InitialCrc) 100 | { 101 | ULONGLONG crc = ~InitialCrc; 102 | UINT idx; 103 | 104 | while (iLen > 0) 105 | { 106 | idx = ((UINT) (crc >> 56) ^ *pData) & 0xff; 107 | crc = CRC_table[idx] ^ (crc << 8); 108 | pData++; 109 | iLen--; 110 | } 111 | return ~crc; 112 | } 113 | -------------------------------------------------------------------------------- /libs/ntdll/crt.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010, Sherpya , aCaB 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of the copyright holders nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | #include "ntdll.h" 29 | #include 30 | 31 | MODULE(crt) 32 | 33 | FORWARD_FUNCTION(memcmp, rpl_memcmp); 34 | FORWARD_FUNCTION(memcpy, rpl_memcpy); 35 | FORWARD_FUNCTION(memset, rpl_memset); 36 | FORWARD_FUNCTION(memmove, rpl_memmove); 37 | FORWARD_FUNCTION(atoll, rpl__atoi64); 38 | FORWARD_FUNCTION(atoi, rpl_atoi); 39 | FORWARD_FUNCTION(qsort, rpl_qsort); 40 | FORWARD_FUNCTION(strcasecmp, rpl__stricmp); 41 | FORWARD_FUNCTION(strncasecmp, rpl__strnicmp); 42 | FORWARD_FUNCTION(strncpy, rpl_strncpy); 43 | FORWARD_FUNCTION(strpbrk, rpl_strpbrk); 44 | FORWARD_FUNCTION(strspn, rpl_strspn); 45 | 46 | // long is always 32bit on win32 47 | LONG CDECL rpl_atol(const char *nptr) 48 | { 49 | return atol(nptr); 50 | } 51 | 52 | SIZE_T CDECL rpl_wcslen(LPCWSTR str) 53 | { 54 | const WCHAR *s = str; 55 | 56 | if (!str) 57 | return 0; 58 | 59 | #ifdef DEBUG_CRT 60 | spam_wchar(str); 61 | #endif 62 | while (*s) 63 | s++; 64 | return (s - str); 65 | } 66 | 67 | LPWSTR CDECL rpl_wcscat(LPWSTR dest, LPCWSTR src) 68 | { 69 | DECLAREVARCONV(srcA); 70 | DECLAREVARCONV(destA); 71 | WCHAR *d; 72 | 73 | if (!src) 74 | return dest; 75 | 76 | WSTR2STR(dest); 77 | WSTR2STR(src); 78 | 79 | Log("ntdll.wcscat(\"%s\", \"%s\")\n", destA, srcA); 80 | 81 | d = dest; 82 | 83 | /* while (*d++); d--; */ // FIXME: why not? 84 | while ((*d++ = *src++)); 85 | 86 | return dest; 87 | } 88 | 89 | int CDECL rpl_wcscmp(LPCWSTR s1, LPCWSTR s2) 90 | { 91 | DECLAREVARCONV(s1A); 92 | DECLAREVARCONV(s2A); 93 | WSTR2STR(s1); 94 | WSTR2STR(s2); 95 | 96 | Log("ntdll._wcscmp(\"%s\", \"%s\")\n", s1A, s2A); 97 | 98 | while (*s1 == *s2) 99 | { 100 | if (!*s1++) return 0; 101 | s2++; 102 | } 103 | return (*s1 < *s2) ? -1 : 1; 104 | } 105 | 106 | int CDECL rpl_wcsncmp(LPCWSTR s1, LPCWSTR s2, SIZE_T n) 107 | { 108 | DECLAREVARCONV(s1A); 109 | DECLAREVARCONV(s2A); 110 | WSTR2STR(s1); 111 | WSTR2STR(s2); 112 | 113 | Log("ntdll.wcsncmp(\"%s\", \"%s\", %d)\n", s1A, s2A, n); 114 | 115 | do 116 | { 117 | if (!(n && *s1)) 118 | return 0; 119 | } 120 | while (n-- && (*s1++ == *s2++)); 121 | return (*s1 < *s2) ? -1 : 1; 122 | } 123 | 124 | int CDECL rpl__wcsicmp(LPCWSTR s1, LPCWSTR s2) 125 | { 126 | DECLAREVARCONV(s1A); 127 | DECLAREVARCONV(s2A); 128 | WSTR2STR(s1); 129 | WSTR2STR(s2); 130 | 131 | Log("ntdll._wcsicmp(\"%s\", \"%s\")\n", s1A, s2A); 132 | 133 | while (wcmp(*s1, *s2, TRUE)) 134 | { 135 | if (!*s1++) 136 | return 0; 137 | s2++; 138 | } 139 | return widetol(*s1) < widetol(*s2) ? -1 : 1; 140 | } 141 | 142 | int CDECL rpl__wcsnicmp(LPCWSTR s1, LPCWSTR s2, SIZE_T n) 143 | { 144 | DECLAREVARCONV(s1A); 145 | DECLAREVARCONV(s2A); 146 | WSTR2STR(s1); 147 | WSTR2STR(s2); 148 | 149 | Log("ntdll._wcsnicmp(\"%s\", \"%s\", %d)\n", s1A, s2A, n); 150 | 151 | while (wcmp(*s1, *s2, TRUE)) 152 | { 153 | n--; 154 | if (!(n && *s1++)) 155 | return 0; 156 | s2++; 157 | } 158 | return widetol(*s1) < widetol(*s2) ? -1 : 1; 159 | } 160 | 161 | LPWSTR CDECL rpl_wcsstr(LPCWSTR haystack, LPCWSTR needle) 162 | { 163 | char *res; 164 | DECLAREVARCONV(haystackA); 165 | DECLAREVARCONV(needleA); 166 | 167 | if (!rpl_wcslen(needle)) 168 | return (LPWSTR) haystack; 169 | 170 | WSTR2STR(haystack); 171 | WSTR2STR(needle); 172 | 173 | if (!(res = strstr(haystackA, needleA))) 174 | return NULL; 175 | 176 | return (LPWSTR) (haystack + (needleA - haystackA)); 177 | } 178 | 179 | LPWSTR CDECL rpl_wcscpy(LPWSTR dest, LPCWSTR src) 180 | { 181 | WCHAR *s = dest; 182 | while (*src) 183 | *s++ = *src++; 184 | *s = 0; 185 | return dest; 186 | } 187 | 188 | LPWSTR CDECL rpl_wcsrchr(LPCWSTR s, WCHAR c) 189 | { 190 | const WCHAR *p = NULL; 191 | DECLAREVARCONV(sA); 192 | WSTR2STR(s); 193 | 194 | Log("ntdll.wcsrchr(\"%s\", \"%c\")\n", sA, widetoa(c)); 195 | 196 | do 197 | { 198 | if (*s == c) 199 | p = s; 200 | } while (*s++); 201 | 202 | return (WCHAR *) p; 203 | } 204 | 205 | LPWSTR CDECL rpl_wcschr(LPCWSTR str, WCHAR c) 206 | { 207 | WCHAR *s = (WCHAR *) str; 208 | DECLAREVARCONV(strA); 209 | WSTR2STR(str); 210 | 211 | Log("ntdll.wcschr(\"%s\", \"%c\")\n", strA, widetoa(c)); 212 | 213 | while (*s) 214 | { 215 | if (*s == c) 216 | return s; 217 | s++; 218 | } 219 | return NULL; 220 | } 221 | 222 | LPWSTR CDECL rpl__wcsupr(LPWSTR str) 223 | { 224 | WCHAR *s = str; 225 | while (*s) 226 | { 227 | *s = widetou(*s); 228 | s++; 229 | } 230 | return str; 231 | } 232 | 233 | LPWSTR CDECL rpl__wcslwr(LPWSTR str) 234 | { 235 | WCHAR *s = str; 236 | while (*s) 237 | { 238 | *s = widetol(*s); 239 | s++; 240 | } 241 | return str; 242 | } 243 | 244 | SIZE_T rpl_wcsspn(LPCWSTR wcs, LPCWSTR accept) 245 | { 246 | SIZE_T res; 247 | DECLAREVARCONV(wcsA); 248 | DECLAREVARCONV(acceptA); 249 | WSTR2STR(wcs); 250 | WSTR2STR(accept); 251 | 252 | res = strspn(wcsA, acceptA); 253 | Log("ntdll._wcsspn(\"%s\", \"%s\") = %d\n", wcsA, acceptA, res); 254 | return res; 255 | } 256 | 257 | /* ctype */ 258 | int CDECL rpl_isspace(int c) 259 | { 260 | if (((unsigned) c <= 255) && strchr(" \f\n\r\t", c)) 261 | return 1; 262 | return 0; 263 | } 264 | 265 | int CDECL rpl_isprint(int c) 266 | { 267 | if (((unsigned) c <= 255) && (c >= ' ') && (c <= '~')) 268 | return 1; 269 | return 0; 270 | } 271 | 272 | ULONG CDECL rpl_wcstoul(const wchar_t *nptr, wchar_t **endptr, int base) 273 | { 274 | ULONG ret; 275 | DECLAREVARCONV(nptrA); 276 | WSTR2STR(nptr); 277 | char *endptrA; 278 | 279 | ret = strtoul(nptrA, &endptrA, base); 280 | 281 | if (endptr) 282 | *endptr = ((wchar_t *) nptr) + (endptrA - nptrA); 283 | 284 | return ret; 285 | } 286 | 287 | ULONGLONG CDECL rpl__wcstoui64(const wchar_t *nptr, wchar_t **endptr, int base) 288 | { 289 | ULONGLONG ret; 290 | DECLAREVARCONV(nptrA); 291 | WSTR2STR(nptr); 292 | char *endptrA; 293 | 294 | ret = strtoll(nptrA, &endptrA, base); 295 | 296 | if (endptr) 297 | *endptr = ((wchar_t *) nptr) + (endptrA - nptrA); 298 | return ret; 299 | } 300 | 301 | LONGLONG CDECL rpl__wtoi64(LPCWSTR str) 302 | { 303 | ULONGLONG value = 0; 304 | BOOLEAN neg = FALSE; 305 | 306 | while (rpl_isspace(widetoa(*str))) 307 | str++; 308 | 309 | if (*str == L'+') 310 | str++; 311 | else if (*str == L'-') 312 | { 313 | neg = TRUE; 314 | str++; 315 | } 316 | 317 | while (widetoa(*str) >= '0' && widetoa(*str) <= '9') 318 | { 319 | value = value * 10 + widetoa(*str) - '0'; 320 | str++; 321 | } 322 | return neg ? 0 - value : value; 323 | } 324 | 325 | LONG CDECL rpl__wtol(LPCWSTR str) 326 | { 327 | return (LONG) rpl__wtoi64(str); 328 | } 329 | -------------------------------------------------------------------------------- /libs/ntdll/dllmain.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010, Sherpya , aCaB 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of the copyright holders nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | #ifdef _WIN32 29 | 30 | #include "ntdll.h" 31 | #include "ftable.h" 32 | 33 | functable_t ftbl; 34 | 35 | #define IMPORT_FUNC(m, x) \ 36 | ftbl.m.x = ( imp_##x ) GetProcAddress(ftbl.m.hLib, Q(x)); 37 | 38 | #define IMPORT_FUNC_OR_FAIL(m, x) \ 39 | IMPORT_FUNC(m, x); \ 40 | if (!ftbl.m.x) ftbl.m.ok = FALSE; 41 | 42 | static BOOL InitFunctionTable(void) 43 | { 44 | ftbl.nt.hLib = LoadLibraryA("ntdll.dll"); 45 | ftbl.nt.ok = TRUE; 46 | IMPORT_FUNC_OR_FAIL(nt, NtOpenFile); 47 | IMPORT_FUNC_OR_FAIL(nt, NtCreateFile); 48 | IMPORT_FUNC_OR_FAIL(nt, NtReadFile); 49 | IMPORT_FUNC_OR_FAIL(nt, NtWriteFile); 50 | IMPORT_FUNC_OR_FAIL(nt, NtClose); 51 | IMPORT_FUNC_OR_FAIL(nt, NtDeviceIoControlFile); 52 | IMPORT_FUNC_OR_FAIL(nt, NtQueryInformationFile); 53 | IMPORT_FUNC_OR_FAIL(nt, NtFsControlFile); 54 | return TRUE; 55 | } 56 | 57 | BOOL APIENTRY DllMain(HINSTANCE hModule, DWORD reason, LPVOID lpReserved) 58 | { 59 | switch (reason) 60 | { 61 | case DLL_PROCESS_ATTACH: 62 | return InitFunctionTable(); 63 | case DLL_THREAD_ATTACH: 64 | case DLL_THREAD_DETACH: 65 | case DLL_PROCESS_DETACH: 66 | break; 67 | } 68 | return TRUE; 69 | } 70 | 71 | #endif 72 | -------------------------------------------------------------------------------- /libs/ntdll/format.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010, Sherpya , aCaB 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of the copyright holders nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | #include "ntdll.h" 29 | #include 30 | 31 | FORWARD_FUNCTION(sprintf, rpl_sprintf); 32 | FORWARD_FUNCTION(vsnprintf, rpl__vsnprintf); 33 | 34 | typedef struct _format_target_t 35 | { 36 | int type; 37 | void *data; 38 | int count; 39 | } __format_target_t; 40 | 41 | typedef enum 42 | { 43 | FORMAT_FILE = 0, 44 | FORMAT_BUFFER 45 | } __format_type_t; 46 | 47 | static int __emit_wchar(WCHAR c, __format_target_t *stream) 48 | { 49 | switch (stream->type) 50 | { 51 | case FORMAT_BUFFER: 52 | { 53 | ((WCHAR *)(stream->data))[stream->count] = c; 54 | break; 55 | } 56 | } 57 | stream->count++; 58 | return 1; 59 | } 60 | 61 | static int __emit_wstring(const WCHAR *str, __format_target_t *stream) 62 | { 63 | int chars = 0; 64 | 65 | if (!str) 66 | str = L"(nil)"; 67 | 68 | while (*str) 69 | { 70 | switch (stream->type) 71 | { 72 | case FORMAT_BUFFER: 73 | { 74 | ((WCHAR *)(stream->data))[stream->count++] = *str; 75 | break; 76 | } 77 | } 78 | str++; chars++; 79 | } 80 | return chars; 81 | } 82 | 83 | static int __emit_string(const char *str, __format_target_t *stream) 84 | { 85 | int chars = 0; 86 | 87 | if (!str) 88 | str = "(null)"; 89 | 90 | while (*str) 91 | { 92 | switch (stream->type) 93 | { 94 | case FORMAT_BUFFER: 95 | { 96 | ((WCHAR *)(stream->data))[stream->count++] = *str; 97 | break; 98 | } 99 | } 100 | str++; chars++; 101 | } 102 | return chars; 103 | } 104 | 105 | static int __emit_int32(int32_t i, const char *fmt, __format_target_t *stream) 106 | { 107 | char number[32]; 108 | int chars = snprintf(number, sizeof(number), fmt, i); 109 | __emit_string(number, stream); 110 | return chars; 111 | } 112 | 113 | static int __emit_int64(int64_t i, const char *fmt, __format_target_t *stream) 114 | { 115 | char number[64]; 116 | int chars = snprintf(number, sizeof(number), fmt, i); 117 | __emit_string(number, stream); 118 | return chars; 119 | } 120 | 121 | // FIXME: missing many types, no width, size has no checks 122 | // FIXME: check count/size 123 | static int __format(const WCHAR *format, __format_target_t *stream, size_t count, va_list argv) 124 | { 125 | size_t size = count; 126 | WCHAR c; 127 | const WCHAR *fmt = format; 128 | char nfmt[32]; 129 | int cf; 130 | 131 | format_next: while ((count > 0) && (c = *fmt++)) 132 | { 133 | if (c != L'%') 134 | { 135 | count -= __emit_wchar(c, stream); 136 | continue; 137 | } 138 | 139 | cf = 1; nfmt[0] = '%'; 140 | 141 | switch_next: switch ((c = *fmt++)) 142 | { 143 | case L'c': 144 | count -= __emit_wchar(va_arg(argv, int), stream); 145 | goto format_next; 146 | 147 | case L's': 148 | count -= __emit_wstring(va_arg(argv, WCHAR *), stream); 149 | goto format_next; 150 | 151 | case L'w': case L'W': 152 | if ((c = *fmt++) == L's') 153 | count -= __emit_wstring(va_arg(argv, WCHAR *), stream); 154 | goto format_next; 155 | 156 | case L'h': case L'H': 157 | if ((c = *fmt++) == L's') 158 | count -= __emit_string(va_arg(argv, char *), stream); 159 | goto format_next; 160 | 161 | case L'I': case L'i': 162 | if (*fmt++ != L'6') goto format_next; 163 | if (*fmt++ != L'4') goto format_next; 164 | nfmt[cf++] = 'l'; 165 | nfmt[cf++] = 'l'; 166 | switch (*fmt) 167 | { 168 | case L'd': case L'u': case L'x': 169 | nfmt[cf++] = (char) *fmt++; 170 | break; 171 | default: 172 | nfmt[cf++] = 'u'; 173 | } 174 | nfmt[cf] = 0; 175 | count -= __emit_int64(va_arg(argv, int64_t), nfmt, stream); 176 | goto format_next; 177 | case L'd': case L'u': case L'x': case L'X': 178 | nfmt[cf++] = (char) c; 179 | nfmt[cf] = 0; 180 | count -= __emit_int32(va_arg(argv, int32_t), nfmt, stream); 181 | goto format_next; 182 | 183 | /* width */ 184 | case L'0': case L'1': case L'2': case L'3': case L'4': 185 | case L'5': case L'6': case L'7': case L'8': case L'9': 186 | if (cf >= sizeof(nfmt) - sizeof("%llx ")) 187 | { 188 | fprintf(stderr, "__format: format prefix overflow\n"); 189 | abort(); 190 | } 191 | nfmt[cf++] = (char) c; 192 | goto switch_next; 193 | default: 194 | { 195 | DECLAREVARCONV(formatA); 196 | WSTR2STR(format); 197 | fprintf(stderr, "__format: unsupported type %c in format '%s'\n", c, formatA); 198 | abort(); 199 | } 200 | } 201 | } 202 | 203 | ((WCHAR *)(stream->data))[stream->count++] = 0; 204 | return size - count; 205 | } 206 | 207 | int CDECL rpl_swprintf(WCHAR *buffer, const WCHAR *format, ...) 208 | { 209 | int retval; 210 | va_list argptr; 211 | 212 | __format_target_t target; 213 | target.count = 0; 214 | target.type = FORMAT_BUFFER; 215 | target.data = (void *) buffer; 216 | va_start(argptr, format); 217 | 218 | retval = __format(format, &target, -1, argptr); 219 | 220 | va_end(argptr); 221 | return retval; 222 | } 223 | 224 | int CDECL rpl__vsnwprintf(WCHAR *buffer, size_t count, const WCHAR *format, va_list argptr) 225 | { 226 | __format_target_t target; 227 | target.count = 0; 228 | target.type = FORMAT_BUFFER; 229 | target.data = (void *) buffer; 230 | return __format(format, &target, count, argptr); 231 | } 232 | 233 | int CDECL rpl_swprintf_s(WCHAR *buffer, size_t sizeOfBuffer, const WCHAR *format, ...) 234 | { 235 | int retval; 236 | va_list argptr; 237 | 238 | __format_target_t target; 239 | target.count = 0; 240 | target.type = FORMAT_BUFFER; 241 | target.data = (void *) buffer; 242 | va_start(argptr, format); 243 | 244 | retval = __format(format, &target, sizeOfBuffer, argptr); 245 | 246 | va_end(argptr); 247 | return retval; 248 | } 249 | FORWARD_FUNCTION(rpl_swprintf_s, rpl__snwprintf); 250 | 251 | ULONG CDECL DbgPrint(char *format, ...) 252 | { 253 | va_list argptr; 254 | va_start(argptr, format); 255 | vprintf(format, argptr); 256 | va_end(argptr); 257 | return STATUS_SUCCESS; 258 | } 259 | 260 | ULONG CDECL DbgPrintEx(ULONG ComponentId, ULONG Level, const char *format, ...) 261 | { 262 | va_list argptr; 263 | va_start(argptr, format); 264 | vprintf(format, argptr); 265 | va_end(argptr); 266 | return STATUS_SUCCESS; 267 | } 268 | -------------------------------------------------------------------------------- /libs/ntdll/ftable.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010, Sherpya , aCaB 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of the copyright holders nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | #ifndef _FTABLE_H 29 | #define _FTABLE_H 30 | 31 | typedef NTSTATUS (NTAPI *imp_NtOpenFile)(PHANDLE,ACCESS_MASK,POBJECT_ATTRIBUTES,PIO_STATUS_BLOCK,ULONG,ULONG); 32 | typedef NTSTATUS (NTAPI *imp_NtCreateFile)(PHANDLE,ACCESS_MASK,POBJECT_ATTRIBUTES,PIO_STATUS_BLOCK,PLARGE_INTEGER,ULONG,ULONG,ULONG,ULONG,PVOID,ULONG); 33 | typedef NTSTATUS (NTAPI *imp_NtReadFile)(HANDLE,HANDLE,PIO_APC_ROUTINE,PVOID,PIO_STATUS_BLOCK,PVOID,ULONG,PLARGE_INTEGER,PULONG); 34 | typedef NTSTATUS (NTAPI *imp_NtWriteFile)(HANDLE,HANDLE,PIO_APC_ROUTINE,PVOID,PIO_STATUS_BLOCK,PVOID,ULONG,PLARGE_INTEGER,PULONG); 35 | typedef NTSTATUS (NTAPI *imp_NtClose)(HANDLE); 36 | typedef NTSTATUS (NTAPI *imp_NtDeviceIoControlFile)(HANDLE,HANDLE,PIO_APC_ROUTINE,PVOID,PIO_STATUS_BLOCK,ULONG,PVOID,ULONG,PVOID,ULONG); 37 | typedef NTSTATUS (NTAPI *imp_NtQueryInformationFile)(HANDLE,PIO_STATUS_BLOCK,PVOID,ULONG,FILE_INFORMATION_CLASS); 38 | typedef NTSTATUS (NTAPI *imp_NtFsControlFile)(HANDLE,HANDLE,PIO_APC_ROUTINE,PVOID,PIO_STATUS_BLOCK,ULONG,PVOID,ULONG,PVOID,ULONG); 39 | 40 | typedef struct _ntdll_t 41 | { 42 | BOOL ok; 43 | HINSTANCE hLib; 44 | imp_NtOpenFile NtOpenFile; 45 | imp_NtCreateFile NtCreateFile; 46 | imp_NtReadFile NtReadFile; 47 | imp_NtWriteFile NtWriteFile; 48 | imp_NtClose NtClose; 49 | imp_NtDeviceIoControlFile NtDeviceIoControlFile; 50 | imp_NtQueryInformationFile NtQueryInformationFile; 51 | imp_NtFsControlFile NtFsControlFile; 52 | } ntdll_t; 53 | 54 | typedef struct _functable_t 55 | { 56 | ntdll_t nt; 57 | } functable_t; 58 | 59 | extern functable_t ftbl; 60 | 61 | #endif /* _FTABLE_H */ 62 | -------------------------------------------------------------------------------- /libs/ntdll/heap.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010, Sherpya , aCaB 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of the copyright holders nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | #include "ntdll.h" 29 | #include 30 | 31 | MODULE(heap) 32 | 33 | #ifdef DEBUG_HEAP 34 | #define Debug(fmt, ...) fprintf(stderr, fmt, ##__VA_ARGS__) 35 | #else 36 | #define Debug(...) 37 | #endif 38 | 39 | #define MAX_HEAP_ALLOC 1073741824 40 | #define HEAPALLOC_MAGIC 0xFEFAF0F0 41 | #define HEAPFREE_MAGIC (~HEAPALLOC_MAGIC) 42 | 43 | #define HEAP_OFFMAGIC(x) ((uint32_t *) x)[-2] 44 | #define HEAP_OFFSIZE(x) ((uint32_t *) x)[-1] 45 | 46 | PVOID NTAPI RtlCreateHeap(ULONG Flags, PVOID HeapBase, SIZE_T ReserveSize, SIZE_T CommitSize, PVOID Lock, /*PRTL_HEAP_PARAMETERS */ LPVOID Parameters) 47 | { 48 | Log("ntdll.RtlCreateHeap(0x%08x, %p, %d, %d, %p, %p)\n", (int) Flags, HeapBase, ReserveSize, CommitSize, Lock, Parameters); 49 | return HANDLE_HEAP; 50 | } 51 | 52 | PVOID NTAPI RtlDestroyHeap(PVOID HeapHandle) 53 | { 54 | Log("ntdll.RtlDestroyHeap(%p)\n", HeapHandle); 55 | return NULL; 56 | } 57 | 58 | PVOID NTAPI RtlAllocateHeap(PVOID HeapHandle, ULONG Flags, SIZE_T Size) 59 | { 60 | PVOID MemoryPointer; 61 | 62 | assert(Size < MAX_HEAP_ALLOC); 63 | 64 | MemoryPointer = malloc(Size + (sizeof(uint32_t) * 2)); 65 | Debug("ntdll.RtlAllocateHeap(%p, 0x%08x, %d)=%p -> ", HeapHandle, (int) Flags, Size, MemoryPointer); 66 | 67 | assert(MemoryPointer); 68 | 69 | MemoryPointer = (PVOID)((uint32_t *) MemoryPointer + 2); 70 | HEAP_OFFMAGIC(MemoryPointer) = HEAPALLOC_MAGIC; 71 | HEAP_OFFSIZE(MemoryPointer) = Size; 72 | 73 | if (Flags & HEAP_ZERO_MEMORY) 74 | memset(MemoryPointer, 0, Size); 75 | 76 | Debug("%p\n", MemoryPointer); 77 | return MemoryPointer; 78 | } 79 | 80 | PVOID NTAPI RtlReAllocateHeap(PVOID HeapHandle, ULONG Flags, PVOID MemoryPointer, SIZE_T Size) 81 | { 82 | SIZE_T OldSize; 83 | 84 | assert(Size < MAX_HEAP_ALLOC); 85 | 86 | if (!MemoryPointer) 87 | return RtlAllocateHeap(HeapHandle, Flags, Size); 88 | 89 | OldSize = HEAP_OFFSIZE(MemoryPointer); 90 | MemoryPointer = realloc((PVOID) HEAP_OFFSIZE(MemoryPointer), Size + (sizeof(uint32_t) * 2)); 91 | 92 | assert(MemoryPointer); 93 | 94 | MemoryPointer = (PVOID)((uint32_t *) MemoryPointer + 2); 95 | HEAP_OFFMAGIC(MemoryPointer) = HEAPALLOC_MAGIC; 96 | HEAP_OFFSIZE(MemoryPointer) = Size; 97 | 98 | Log(" -> %p\n", MemoryPointer); 99 | 100 | if ((Size > OldSize) && (Flags & HEAP_ZERO_MEMORY)) 101 | memset((char *) MemoryPointer + OldSize, 0, Size - OldSize); 102 | 103 | return MemoryPointer; 104 | } 105 | 106 | SIZE_T NTAPI RtlSizeHeap(PVOID HeapHandle, ULONG Flags, LPCVOID MemoryPointer) 107 | { 108 | Debug("ntdll.RtlSizeHeap(%p, 0x%08x, %p) = %d\n", HeapHandle, (int) Flags, MemoryPointer, HEAP_OFFSIZE(MemoryPointer)); 109 | assert(HEAP_OFFMAGIC(MemoryPointer) == HEAPALLOC_MAGIC); 110 | return HEAP_OFFSIZE(MemoryPointer); 111 | } 112 | 113 | BOOLEAN NTAPI RtlFreeHeap(PVOID HeapHandle, ULONG Flags, PVOID MemoryPointer) 114 | { 115 | Debug("ntdll.RtlFreeHeap(%p, 0x%08x, %p)\n", HeapHandle, (int) Flags, MemoryPointer); 116 | assert(HEAP_OFFMAGIC(MemoryPointer) == HEAPALLOC_MAGIC); 117 | HEAP_OFFMAGIC(MemoryPointer) = HEAPFREE_MAGIC; 118 | MemoryPointer = (PVOID)((uint32_t *) MemoryPointer - 2); 119 | free(MemoryPointer); 120 | return TRUE; 121 | } 122 | -------------------------------------------------------------------------------- /libs/ntdll/large_int.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010, Sherpya , aCaB 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of the copyright holders nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | #include "ntdll.h" 29 | 30 | MODULE(large_int) 31 | 32 | LONGLONG NTAPI _alldiv(LONGLONG a, LONGLONG b) 33 | { 34 | Log("ntdll._alldiv(%lld, %lld) = %lld [ %lld / %lld ]\n", a, b, a / b, a, b); 35 | return a / b; 36 | } 37 | 38 | ULONGLONG NTAPI _aulldiv(ULONGLONG a, ULONGLONG b) 39 | { 40 | Log("ntdll._aulldiv(%llu, %llu) = %llu [ %llu / %llu ]\n", a, b, a / b, a, b); 41 | return a / b; 42 | } 43 | 44 | LONGLONG NTAPI _allrem(LONGLONG a, LONGLONG b) 45 | { 46 | Log("ntdll._allrem(%lld, %lld) = %lld [ %lld %% %lld ]\n", a, b, a % b, a, b); 47 | return a % b; 48 | } 49 | 50 | ULONGLONG NTAPI _aullrem(ULONGLONG a, ULONGLONG b) 51 | { 52 | Log("ntdll._aullrem(%llu, %llu) = %llu [ %llu %% %llu ]\n", a, b, a % b, a, b); 53 | return a % b; 54 | } 55 | 56 | LONGLONG NTAPI _allmul(LONGLONG a, LONGLONG b) 57 | { 58 | Log("ntdll._allmul(%lld, %lld) = %lld [ %lld * %lld ]\n", a, b, a * b, a, b); 59 | return a * b; 60 | } 61 | 62 | ULONGLONG NTAPI _aullshr(ULONGLONG a, LONG b) 63 | { 64 | Log("ntdll._aullshr(%llu, %d) = %llu [ %llu >> %d ]\n", a, b, a >> b, a, b); 65 | return a >> b; 66 | } 67 | 68 | LONGLONG NTAPI _allshl(LONGLONG a, LONG b) 69 | { 70 | Log("ntdll._allshr(%lld, %d) = %lld [ %llu >> %d ]\n", a, b, a << b, a, b); 71 | return a << b; 72 | } 73 | 74 | LONGLONG NTAPI RtlExtendedIntegerMultiply(LONGLONG a, INT b) 75 | { 76 | Log("ntdll.RtlExtendedIntegerMultiply(%lld, %d) = %lld [ %lld * %d ]\n", a, b, a * b, a, b); 77 | return a * b; 78 | } 79 | 80 | ULONGLONG NTAPI RtlLargeIntegerDivide(ULONGLONG a, ULONGLONG b, ULONGLONG *rem) 81 | { 82 | ULONGLONG ret = a / b; 83 | if (rem) 84 | *rem = a - ret * b; 85 | Log("ntdll.RtlLargeIntegerDivide(%llu, %llu) = %llu, %%llu [ %llu / %llu ]\n", a, b, ret, a - ret * b, a, b); 86 | return ret; 87 | } 88 | 89 | /* hmm why LARGE_INTEGER crashes? */ 90 | int64_t /*LARGE_INTEGER*/ NTAPI RtlConvertLongToLargeInteger(LONG SignedInteger) 91 | { 92 | Log("ntdll.RtlConvertLongToLargeInteger(%d)\n", SignedInteger); 93 | return SignedInteger; 94 | } 95 | -------------------------------------------------------------------------------- /libs/ntdll/lznt1.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010, Sherpya , aCaB 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of the copyright holders nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | #include "ntdll.h" 29 | 30 | static NTSTATUS RtlDecompressBufferLZNT1(PUCHAR UncompressedBuffer, ULONG UncompressedBufferSize, 31 | PUCHAR CompressedBuffer, ULONG CompressedBufferSize, PULONG FinalUncompressedSize) 32 | { 33 | UCHAR *c = CompressedBuffer, *d = UncompressedBuffer; 34 | 35 | if (!c || !d || !FinalUncompressedSize || !CompressedBufferSize) 36 | return STATUS_ACCESS_VIOLATION; 37 | 38 | *FinalUncompressedSize = UncompressedBufferSize; 39 | 40 | while (CompressedBufferSize) 41 | { 42 | uint16_t blocksz, blockpos = 0; 43 | unsigned int i, j; 44 | uint8_t mask; 45 | 46 | /* Block header */ 47 | if (CompressedBufferSize < 2) 48 | return STATUS_INVALID_PARAMETER; 49 | 50 | blocksz = ((uint16_t) c[1] << 8) + *c; 51 | c += 2; 52 | CompressedBufferSize -= 2; 53 | 54 | if (!(blocksz & 0x8000)) // uncompressed chunk 55 | { 56 | blocksz = 4096; 57 | 58 | if(blocksz > UncompressedBufferSize) 59 | return STATUS_BAD_COMPRESSION_BUFFER; 60 | 61 | if(blocksz > CompressedBufferSize) 62 | return STATUS_BAD_COMPRESSION_BUFFER; 63 | 64 | memcpy(d, c, blocksz); 65 | d += blocksz; 66 | c += blocksz; 67 | UncompressedBufferSize -= blocksz; 68 | CompressedBufferSize -= blocksz; 69 | continue; 70 | } 71 | 72 | blocksz &= 0xfff; 73 | blocksz++; 74 | 75 | while(blocksz) 76 | { 77 | /* Run mask */ 78 | if (!CompressedBufferSize) 79 | return STATUS_BAD_COMPRESSION_BUFFER; 80 | 81 | mask = *c; 82 | c++; 83 | CompressedBufferSize--; 84 | blocksz--; 85 | 86 | for (i = 0; i < 8 && blocksz; i++) 87 | { 88 | if (mask & 1) 89 | { 90 | uint16_t runlen, len = 0xfff, back = 12; 91 | if (CompressedBufferSize < 2 || blocksz < 2) 92 | return STATUS_BAD_COMPRESSION_BUFFER; 93 | 94 | runlen = ((uint16_t) c[1] << 8) + *c; 95 | c += 2; 96 | CompressedBufferSize -= 2; 97 | blocksz -= 2; 98 | 99 | if (!blockpos) 100 | return STATUS_BAD_COMPRESSION_BUFFER; 101 | 102 | for (j = blockpos - 1; j >= 0x10; j >>= 1) 103 | { 104 | len >>= 1; 105 | back--; 106 | } 107 | 108 | len = (runlen & len) + 3; 109 | back = (runlen >> back) + 1; 110 | 111 | if (UncompressedBufferSize < len) 112 | return STATUS_BAD_COMPRESSION_BUFFER; 113 | 114 | UncompressedBufferSize -= len; 115 | blockpos += len; 116 | 117 | while (len--) 118 | { 119 | *d = d[-back]; 120 | d++; 121 | } 122 | } 123 | else 124 | { 125 | if (!CompressedBufferSize || ! blocksz) 126 | return STATUS_BAD_COMPRESSION_BUFFER; 127 | 128 | if (!UncompressedBufferSize) 129 | return STATUS_BAD_COMPRESSION_BUFFER; 130 | 131 | *d++ = *c++; 132 | UncompressedBufferSize--; 133 | CompressedBufferSize--; 134 | blockpos++; 135 | blocksz--; 136 | } 137 | mask >>= 1; 138 | } 139 | } 140 | } 141 | 142 | *FinalUncompressedSize -= UncompressedBufferSize; 143 | return STATUS_SUCCESS; 144 | } 145 | 146 | #define COMPRESSION_FORMAT_LZNT1 2 147 | NTSTATUS NTAPI RtlDecompressBuffer(USHORT CompressionFormat, PUCHAR UncompressedBuffer, ULONG UncompressedBufferSize, 148 | PUCHAR CompressedBuffer, ULONG CompressedBufferSize, PULONG FinalUncompressedSize) 149 | { 150 | if (CompressionFormat != COMPRESSION_FORMAT_LZNT1) 151 | return STATUS_INVALID_PARAMETER; 152 | 153 | return RtlDecompressBufferLZNT1(UncompressedBuffer, UncompressedBufferSize, CompressedBuffer, CompressedBufferSize, FinalUncompressedSize); 154 | } 155 | 156 | #ifdef MAIN 157 | int main(int argc, char *argv[]) 158 | { 159 | NTSTATUS res; 160 | struct stat st; 161 | ULONG outsize, final, fsize; 162 | unsigned char *inbuff, *outbuff; 163 | FILE *fin, *fout; 164 | 165 | if (argc != 3) 166 | { 167 | printf("Usage: %s input output\n", argv[0]); 168 | exit(1); 169 | } 170 | 171 | if (stat(argv[1], &st) < 0) 172 | { 173 | perror("stat"); 174 | exit(1); 175 | } 176 | 177 | fsize = (ULONG) st.st_size; 178 | 179 | fin = fopen(argv[1], "rb"); 180 | inbuff = malloc(fsize); 181 | fread(inbuff, 1, fsize, fin); 182 | fclose(fin); 183 | 184 | printf("Insize: %d\n", fsize); 185 | outsize = fsize * 2; 186 | outbuff = malloc(outsize); 187 | 188 | while ((res = RtlDecompressBuffer(COMPRESSION_FORMAT_LZNT1, outbuff, outsize, inbuff, fsize, &final)) == STATUS_BAD_COMPRESSION_BUFFER) 189 | { 190 | outsize *= 2; 191 | printf("reallocating %d\n", outsize); 192 | outbuff = realloc(outbuff, outsize); 193 | } 194 | 195 | printf("0x%08x Done %d\n", res, final); 196 | 197 | fout = fopen(argv[2], "wb"); 198 | fwrite(outbuff, 1, final, fout); 199 | fclose(fout); 200 | 201 | free(inbuff); 202 | free(outbuff); 203 | return 0; 204 | } 205 | #endif 206 | -------------------------------------------------------------------------------- /libs/ntdll/mm.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010, Sherpya , aCaB 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of the copyright holders nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | #include "ntdll.h" 29 | 30 | MODULE(mm) 31 | 32 | // FIXME: untested 33 | NTSTATUS NTAPI NtAllocateVirtualMemory(HANDLE ProcessHandle, PVOID *BaseAddress, 34 | ULONG_PTR ZeroBits, PSIZE_T RegionSize, ULONG AllocationType, ULONG Protect) 35 | { 36 | int prot, flags = MAP_PRIVATE | MAP_ANON; 37 | SIZE_T size; 38 | PVOID address; 39 | 40 | if (!*RegionSize) 41 | return STATUS_INVALID_PARAMETER; 42 | 43 | switch (Protect) 44 | { 45 | case PAGE_READONLY: 46 | prot = PROT_READ; 47 | break; 48 | case PAGE_READWRITE: 49 | prot = PROT_READ | PROT_WRITE; 50 | break; 51 | case PAGE_EXECUTE: 52 | prot = PROT_EXEC; 53 | break; 54 | default: 55 | fprintf(stderr, "ntdll.NtAllocateVirtualMemory() unknown protection 0x%8x\n", Protect); 56 | abort(); 57 | } 58 | 59 | size = ROUND_TO_PAGES(*RegionSize); 60 | address = *BaseAddress ? PAGE_ALIGN(*BaseAddress) : NULL; 61 | 62 | if (address) flags |= MAP_FIXED; 63 | address = mmap(address, size, prot, flags, -1, 0); 64 | *RegionSize = size; 65 | 66 | Log("ntdll.NtAllocateVirtualMemory(%d, %p, 0x%08x, %d, 0x%08x, 0x%08x) = %p/%d\n", (int) ProcessHandle, 67 | *BaseAddress, ZeroBits, *RegionSize, AllocationType, Protect, address, size); 68 | 69 | if (address) 70 | { 71 | *BaseAddress = address; 72 | return STATUS_SUCCESS; 73 | } 74 | 75 | return STATUS_INSUFFICIENT_RESOURCES; 76 | } 77 | FORWARD_FUNCTION(NtAllocateVirtualMemory, ZwAllocateVirtualMemory); 78 | 79 | NTSTATUS NTAPI NtFreeVirtualMemory(HANDLE ProcessHandle, PVOID *BaseAddress, PSIZE_T RegionSize, ULONG FreeType) 80 | { 81 | Log("ntdll.NtFreeVirtualMemory(%d, %p, %d, 0x%08x)\n", (int) ProcessHandle, *BaseAddress, *RegionSize, FreeType); 82 | 83 | if (munmap(*BaseAddress, ROUND_TO_PAGES(*RegionSize)) < 0) 84 | return STATUS_UNSUCCESSFUL; 85 | else 86 | return STATUS_SUCCESS; 87 | } 88 | FORWARD_FUNCTION(NtFreeVirtualMemory, ZwFreeVirtualMemory); 89 | -------------------------------------------------------------------------------- /libs/ntdll/ntdll.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010, Sherpya , aCaB 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of the copyright holders nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | #include "ntdll.h" 29 | 30 | MODULE(nt) 31 | 32 | static void fill_sysinfo(SYSTEM_BASIC_INFORMATION *info) 33 | { 34 | info->unknown = 0; 35 | info->KeMaximumIncrement = 0; /* 0x2625a */ 36 | info->PageSize = getpagesize(); 37 | info->MmLowestPhysicalPage = 0x1; 38 | info->MmHighestPhysicalPage = 0x7fffffff / getpagesize(); 39 | info->MmNumberOfPhysicalPages = info->MmHighestPhysicalPage - info->MmLowestPhysicalPage; /* 0xefee5 */ 40 | info->AllocationGranularity = 0x10000; 41 | info->LowestUserAddress = (PVOID) 0x10000; 42 | info->HighestUserAddress = (PVOID) 0x7ffeffff; 43 | info->ActiveProcessorsAffinityMask = (1 << 1) - 1; 44 | info->NumberOfProcessors = 1; 45 | } 46 | 47 | static void fill_perfinfo(SYSTEM_PERFORMANCE_INFORMATION *spi) 48 | { 49 | spi->AvailablePages = 0x20000; 50 | 51 | /* 52 | spi->Reserved1 = 0x0; 53 | spi->Reserved2[0] = 0; 54 | spi->Reserved2[1] = 0; 55 | spi->Reserved3 = 0x7fffffff; 56 | spi->SystemCodePage = 200; 57 | */ 58 | } 59 | 60 | NTSTATUS NTAPI NtQuerySystemInformation(SYSTEM_INFORMATION_CLASS SystemInformationClass, PVOID SystemInformation, 61 | ULONG SystemInformationLength, PULONG ReturnLength) 62 | { 63 | NTSTATUS result = STATUS_BUFFER_TOO_SMALL; 64 | 65 | Log("ntdll.NtQuerySystemInformation(%s:0x%08x)", strsysinfo(SystemInformationClass), SystemInformationClass); 66 | 67 | switch (SystemInformationClass) 68 | { 69 | case SystemBasicInformation: 70 | { 71 | if (ReturnLength) 72 | *ReturnLength = sizeof(SYSTEM_BASIC_INFORMATION); 73 | 74 | if (SystemInformationLength < sizeof(SYSTEM_BASIC_INFORMATION)) 75 | break; 76 | 77 | fill_sysinfo(SystemInformation); 78 | result = STATUS_SUCCESS; 79 | break; 80 | } 81 | case SystemPerformanceInformation: 82 | { 83 | if (ReturnLength) 84 | *ReturnLength = sizeof(SYSTEM_PERFORMANCE_INFORMATION); 85 | 86 | if (SystemInformationLength < sizeof(SYSTEM_PERFORMANCE_INFORMATION)) 87 | break; 88 | 89 | fill_perfinfo(SystemInformation); 90 | result = STATUS_SUCCESS; 91 | break; 92 | } 93 | case SystemTimeOfDayInformation: 94 | { 95 | SYSTEM_TIMEOFDAY_INFORMATION *sti = SystemInformation; 96 | 97 | if (ReturnLength) 98 | *ReturnLength = sizeof(SYSTEM_TIMEOFDAY_INFORMATION); 99 | 100 | if (SystemInformationLength < sizeof(SYSTEM_TIMEOFDAY_INFORMATION)) 101 | break; 102 | 103 | memset(sti, 0, sizeof(SYSTEM_TIMEOFDAY_INFORMATION)); 104 | result = STATUS_SUCCESS; 105 | break; 106 | } 107 | case SystemModuleInformation: 108 | { 109 | SYSTEM_MODULE_INFORMATION *smi = SystemInformation; 110 | 111 | if (ReturnLength) 112 | *ReturnLength = sizeof(SYSTEM_MODULE_INFORMATION); 113 | 114 | if (SystemInformationLength < sizeof(SYSTEM_MODULE_INFORMATION)) 115 | break; 116 | 117 | memset(smi, 0, sizeof(SYSTEM_MODULE_INFORMATION)); 118 | result = STATUS_SUCCESS; 119 | break; 120 | } 121 | case SystemProcessInformation: 122 | { 123 | SYSTEM_PROCESS_INFORMATION *spi = SystemInformation; 124 | 125 | if (ReturnLength) 126 | *ReturnLength = sizeof(SYSTEM_PROCESS_INFORMATION); 127 | 128 | if (SystemInformationLength < sizeof(SYSTEM_PROCESS_INFORMATION)) 129 | break; 130 | 131 | memset(spi, 0, sizeof(SYSTEM_PROCESS_INFORMATION)); 132 | spi->UniqueProcessId = (HANDLE) 0x1337; 133 | spi->NumberOfThreads = 1; 134 | result = STATUS_SUCCESS; 135 | break; 136 | } 137 | default: 138 | Log(" !!UNIMPLEMENTED!!"); 139 | result = STATUS_NOT_IMPLEMENTED; 140 | } 141 | 142 | Log("\n"); 143 | 144 | return result; 145 | } 146 | 147 | NTSTATUS NTAPI NtSetInformationProcess(HANDLE ProcessHandle, PROCESS_INFORMATION_CLASS ProcessInformationClass, 148 | PVOID ProcessInformation, ULONG ProcessInformationLength) 149 | { 150 | return STATUS_SUCCESS; 151 | } 152 | FORWARD_FUNCTION(NtSetInformationProcess, ZwSetInformationProcess); 153 | 154 | NTSTATUS NTAPI NtTerminateProcess(HANDLE ProcessHandle, NTSTATUS ExitStatus) 155 | { 156 | Log("ntdll.NtTerminateProcess(%p, 0x%08x)\n", ProcessHandle, ExitStatus); 157 | exit(ExitStatus); 158 | } 159 | 160 | NTSTATUS NTAPI NtDisplayString(PUNICODE_STRING String) 161 | { 162 | DECLAREVARCONV(StringA); 163 | CHECK_POINTER(String); 164 | US2STR(String); 165 | 166 | Log("ntdll.DisplayString(String=%p, Buffer=%p) \"%s\"\n", String, String->Buffer, StringA); 167 | 168 | fputs(StringA, stdout); 169 | 170 | return STATUS_SUCCESS; 171 | } 172 | FORWARD_FUNCTION(NtDisplayString, NtDrawText); // Windows 8 173 | 174 | VOID NTAPI DbgBreakPoint(void) 175 | { 176 | fprintf(stderr, "ntdll.DbgBreakPoint()\n"); 177 | INT3(); 178 | } 179 | 180 | NTSTATUS NTAPI NtShutdownSystem(SHUTDOWN_ACTION Action) 181 | { 182 | fprintf(stderr, "ntdll.NtShutdownSystem(%d)\n", Action); 183 | exit(0); 184 | } 185 | FORWARD_FUNCTION(NtShutdownSystem, ZwShutdownSystem); 186 | 187 | NTSTATUS NTAPI LdrSetMUICacheType(ULONG Type) 188 | { 189 | Log("ntdll.LdrSetMUICacheType(0x%08x)\n", Type); 190 | return STATUS_SUCCESS; 191 | } 192 | 193 | NTSTATUS NTAPI NtSerializeBoot(VOID) 194 | { 195 | Log("ntdll.NtSerializeBoot()\n"); 196 | return STATUS_SUCCESS; 197 | } 198 | 199 | NTSTATUS NTAPI NtOpenSection(PHANDLE SectionHandle, ACCESS_MASK DesiredAccess, POBJECT_ATTRIBUTES ObjectAttributes) 200 | { 201 | DECLAREVARCONV(ObjectAttributesA); 202 | OA2STR(ObjectAttributes); 203 | Log("ntdll.NtOpenSection(\"%s\")\n", ObjectAttributesA); 204 | 205 | return STATUS_ACCESS_DENIED; 206 | } 207 | FORWARD_FUNCTION(NtOpenSection, ZwOpenSection); 208 | 209 | NTSTATUS NTAPI LdrGetDllHandle(PWORD pwPath, PVOID Unused, PUNICODE_STRING ModuleFileName, PHANDLE pHModule) 210 | { 211 | DECLAREVARCONV(ModuleFileNameA); 212 | CHECK_POINTER(ModuleFileName); 213 | 214 | US2STR(ModuleFileName); 215 | Log("ntdll.LdrGetDllHandle(\"%s\")\n", ModuleFileNameA); 216 | 217 | return STATUS_INVALID_HANDLE; 218 | } 219 | 220 | NTSTATUS NTAPI NtLoadDriver(IN PUNICODE_STRING DriverServiceName) 221 | { 222 | DECLAREVARCONV(DriverServiceNameA); 223 | CHECK_POINTER(DriverServiceName); 224 | 225 | US2STR(DriverServiceName); 226 | Log("ntdll.NtLoadDriver(\"%s\")\n", DriverServiceNameA); 227 | 228 | return STATUS_SUCCESS; 229 | } 230 | FORWARD_FUNCTION(NtLoadDriver, ZwLoadDriver); 231 | 232 | NTSTATUS NTAPI NtOpenProcessToken(HANDLE ProcessHandle, ACCESS_MASK DesiredAccess, PHANDLE TokenHandle) 233 | { 234 | Log("NtOpenProcessToken() - !!UNIMPLEMENTED!!\n"); 235 | return STATUS_NOT_IMPLEMENTED; 236 | } 237 | 238 | NTSTATUS NTAPI NtAdjustPrivilegesToken(HANDLE TokenHandle, BOOLEAN DisableAllPrivileges, 239 | PVOID /*PTOKEN_PRIVILEGES*/ TokenPrivileges, ULONG PreviousPrivilegesLength, 240 | PVOID /*PTOKEN_PRIVILEGES*/ PreviousPrivileges, PULONG RequiredLength) 241 | { 242 | Log("ntdll.NtAdjustPrivilegesToken()\n"); 243 | return STATUS_SUCCESS; 244 | } 245 | 246 | NTSTATUS NTAPI NtSetDefaultLocale(BOOLEAN UserProfile, LCID DefaultLocaleId) 247 | { 248 | Log("ntdll.NtSetDefaultLocale(%d, %d)\n", UserProfile, DefaultLocaleId); 249 | return STATUS_SUCCESS; 250 | } 251 | -------------------------------------------------------------------------------- /libs/ntdll/ntdll.dll.def: -------------------------------------------------------------------------------- 1 | LIBRARY ntdll 2 | EXPORTS 3 | 4 | ; _chkstk.asm 5 | _chkstk 6 | 7 | ; bitmap.c 8 | RtlAreBitsClear=RtlAreBitsClear@12 9 | RtlAreBitsSet=RtlAreBitsSet@12 10 | RtlClearAllBits=RtlClearAllBits@4 11 | RtlClearBits=RtlClearBits@12 12 | RtlFindClearBits=RtlFindClearBits@12 13 | RtlFindClearBitsAndSet=RtlFindClearBitsAndSet@12 14 | RtlFindClearRuns=RtlFindClearRuns@16 15 | RtlFindLeastSignificantBit=RtlFindLeastSignificantBit@8 16 | RtlFindLongestRunClear=RtlFindLongestRunClear@8 17 | RtlFindMostSignificantBit=RtlFindMostSignificantBit@8 18 | RtlFindNextForwardRunClear=RtlFindNextForwardRunClear@12 19 | RtlFindSetBits=RtlFindSetBits@12 20 | RtlFindSetBitsAndClear=RtlFindSetBitsAndClear@12 21 | RtlInitializeBitMap=RtlInitializeBitMap@12 22 | RtlNumberOfClearBits=RtlNumberOfClearBits@4 23 | RtlNumberOfSetBits=RtlNumberOfSetBits@4 24 | RtlSetAllBits=RtlSetAllBits@4 25 | RtlSetBits=RtlSetBits@12 26 | RtlTestBit=RtlTestBit@8 27 | 28 | ; crc32.c 29 | RtlComputeCrc32=RtlComputeCrc32@12 30 | RtlCrc32@12 31 | 32 | ; crc64.c 33 | RtlCrc64@16 34 | 35 | ; crt.c 36 | rpl__stricmp=stricmp 37 | rpl__strnicmp=strnicmp 38 | rpl__wcsicmp 39 | rpl__wcslwr 40 | rpl__wcsnicmp 41 | rpl__wcsupr 42 | rpl__wtoi64 43 | rpl__wtol 44 | rpl_atoi=atoi 45 | rpl_atol=atol 46 | rpl_atoll=_atoi64 47 | rpl_isprint 48 | rpl_isspace 49 | rpl_memcmp=memcmp 50 | rpl_memcpy=memcpy 51 | rpl_memmove=memmove 52 | rpl_memset=memset 53 | rpl_strncpy=strncpy 54 | rpl_strpbrk=strpbrk 55 | rpl_strspn=strspn 56 | rpl_qsort=qsort 57 | rpl_wcscat 58 | rpl_wcschr 59 | rpl_wcscmp 60 | rpl_wcscpy 61 | rpl_wcslen 62 | rpl_wcsncmp 63 | rpl_wcsrchr 64 | rpl_wcsspn 65 | rpl_wcsstr=wcsstr ; no memmem on win32 66 | rpl_wcstoul 67 | rpl__wcstoui64 68 | 69 | ; format.c 70 | DbgPrint 71 | DbgPrintEx 72 | rpl__snwprintf=rpl_swprintf_s 73 | rpl__vsnprintf=vsnprintf 74 | rpl__vsnwprintf 75 | rpl_sprintf=sprintf 76 | rpl_swprintf 77 | rpl_swprintf_s 78 | 79 | ; heap.c 80 | RtlAllocateHeap=RtlAllocateHeap@12 81 | RtlCreateHeap=RtlCreateHeap@24 82 | RtlDestroyHeap=RtlDestroyHeap@4 83 | RtlFreeHeap=RtlFreeHeap@12 84 | RtlReAllocateHeap=RtlReAllocateHeap@16 85 | RtlSizeHeap=RtlSizeHeap@12 86 | 87 | ; large_int.c 88 | RtlExtendedIntegerMultiply=RtlExtendedIntegerMultiply@12 89 | RtlLargeIntegerDivide=RtlLargeIntegerDivide@20 90 | RtlConvertLongToLargeInteger=RtlConvertLongToLargeInteger@4 91 | _alldiv=_alldiv@16 92 | _allmul=_allmul@16 93 | _allrem=_allrem@16 94 | _allshl=_allshl@12 95 | _aulldiv=_aulldiv@16 96 | _aullrem=_aullrem@16 97 | _aullshr=_aullshr@12 98 | 99 | ;lznt1.c 100 | RtlDecompressBuffer=RtlDecompressBuffer@24 101 | 102 | ; ntdll.c 103 | LdrGetDllHandle=LdrGetDllHandle@16 104 | LdrSetMUICacheType=LdrSetMUICacheType@4 105 | NtAdjustPrivilegesToken=NtAdjustPrivilegesToken@24 106 | NtSetDefaultLocale=NtSetDefaultLocale@8 107 | NtAllocateVirtualMemory=NtAllocateVirtualMemory@24 108 | NtClearEvent=NtClearEvent@4 109 | NtClose=NtClose@4 110 | NtCreateEvent=NtCreateEvent@20 111 | NtCreateFile=NtCreateFile@44 112 | NtDelayExecution=NtDelayExecution@8 113 | NtDeleteFile=NtDeleteFile@4 114 | NtDeviceIoControlFile=NtDeviceIoControlFile@40 115 | NtDisplayString=NtDisplayString@4 116 | NtDrawText=NtDrawText@4 117 | NtFlushBuffersFile=NtFlushBuffersFile@8 118 | NtCancelIoFile=NtCancelIoFile@8 119 | NtFreeVirtualMemory=NtFreeVirtualMemory@16 120 | NtFsControlFile=NtFsControlFile@40 121 | NtLoadDriver=NtLoadDriver@4 122 | NtOpenDirectoryObject=NtOpenDirectoryObject@12 123 | NtOpenEvent=NtOpenEvent@12 124 | NtOpenFile=NtOpenFile@24 125 | NtOpenProcessToken=NtOpenProcessToken@12 126 | NtOpenSection=NtOpenSection@12 127 | NtOpenSymbolicLinkObject=NtOpenSymbolicLinkObject@12 128 | NtQueryDirectoryObject=NtQueryDirectoryObject@28 129 | NtQueryInformationFile=NtQueryInformationFile@20 130 | NtQueryPerformanceCounter=NtQueryPerformanceCounter@8 131 | NtQuerySymbolicLinkObject=NtQuerySymbolicLinkObject@12 132 | NtQuerySystemInformation=NtQuerySystemInformation@16 133 | NtQuerySystemTime=NtQuerySystemTime@4 134 | NtQueryVolumeInformationFile=NtQueryVolumeInformationFile@20 135 | NtReadFile=NtReadFile@36 136 | NtResetEvent=NtResetEvent@8 137 | NtSerializeBoot=NtSerializeBoot@0 138 | NtSetEvent=NtSetEvent@8 139 | NtSetInformationFile=NtSetInformationFile@20 140 | NtSetInformationProcess=NtSetInformationProcess@16 141 | NtShutdownSystem=NtShutdownSystem@4 142 | ZwShutdownSystem=NtShutdownSystem@4 143 | NtTerminateProcess=NtTerminateProcess@8 144 | NtWaitForMultipleObjects=NtWaitForMultipleObjects@20 145 | NtWaitForSingleObject=NtWaitForSingleObject@12 146 | NtWriteFile=NtWriteFile@36 147 | RtlSystemTimeToLocalTime=RtlSystemTimeToLocalTime@8 148 | 149 | ; random.c 150 | RtlRandom=RtlRandom@4 151 | RtlRandomEx=RtlRandomEx@4 152 | RtlUniform=RtlUniform@4 153 | 154 | ; registry.c 155 | NtOpenKey=NtOpenKey@12 156 | NtQueryValueKey=NtQueryValueKey@24 157 | NtSetValueKey=NtSetValueKey@24 158 | RtlQueryRegistryValues=RtlQueryRegistryValues@20 159 | RtlQueryRegistryValuesEx=RtlQueryRegistryValuesEx@20 160 | RtlWriteRegistryValue=RtlWriteRegistryValue@24 161 | 162 | ; rtl.c 163 | RtlAdjustPrivilege=RtlAdjustPrivilege@16 164 | RtlAnsiStringToUnicodeString=RtlAnsiStringToUnicodeString@12 165 | RtlAnsiCharToUnicodeChar=RtlAnsiCharToUnicodeChar@4 166 | RtlDosPathNameToNtPathName_U=RtlDosPathNameToNtPathName_U@16 167 | RtlEqualUnicodeString=RtlEqualUnicodeString@12 168 | RtlExpandEnvironmentStrings_U=RtlExpandEnvironmentStrings_U@16 169 | RtlFillMemoryUlong=RtlFillMemoryUlong@12 170 | RtlGetCurrentDirectory_U=RtlGetCurrentDirectory_U@8 171 | RtlFindMessage=RtlFindMessage@20 172 | RtlFormatMessage=RtlFormatMessage@36 173 | RtlFreeAnsiString=RtlFreeAnsiString@4 174 | RtlFreeUnicodeString=RtlFreeUnicodeString@4 175 | RtlCreateUnicodeStringFromAsciiz=RtlCreateUnicodeStringFromAsciiz@8 176 | RtlInitAnsiString=RtlInitAnsiString@8 177 | RtlInitAnsiStringEx=RtlInitAnsiStringEx@8 178 | RtlInitUnicodeString=RtlInitUnicodeString@8 179 | RtlMultiByteToUnicodeN=RtlMultiByteToUnicodeN@20 180 | RtlNormalizeProcessParams=RtlNormalizeProcessParams@4 181 | RtlOemToUnicodeN=RtlMultiByteToUnicodeN@20 182 | RtlPrefixUnicodeString=RtlPrefixUnicodeString@12 183 | RtlSecondsSince1970ToTime=RtlSecondsSince1970ToTime@8 184 | RtlUnicodeStringToAnsiString=RtlUnicodeStringToAnsiString@12 185 | RtlUnicodeToMultiByteN=RtlUnicodeToOemN@20 186 | RtlUnicodeToOemN=RtlUnicodeToOemN@20 187 | RtlUpcaseUnicodeString=RtlUpcaseUnicodeString@12 188 | 189 | ; sd.c 190 | RtlAddAccessAllowedAce=RtlAddAccessAllowedAce@16 191 | RtlAddAce=RtlAddAce@20 192 | RtlCopySid=RtlCopySid@12 193 | RtlCreateAcl=RtlCreateAcl@12 194 | RtlCreateSecurityDescriptor=RtlCreateSecurityDescriptor@8 195 | RtlFirstFreeAce=RtlFirstFreeAce@8 196 | RtlInitializeSid=RtlInitializeSid@12 197 | RtlLengthRequiredSid=RtlLengthRequiredSid@4 198 | RtlLengthSecurityDescriptor=RtlLengthSecurityDescriptor@4 199 | RtlLengthSid=RtlLengthSid@4 200 | RtlQueryInformationAcl=RtlQueryInformationAcl@16 201 | RtlSetDaclSecurityDescriptor=RtlSetDaclSecurityDescriptor@16 202 | RtlSetGroupSecurityDescriptor=RtlSetGroupSecurityDescriptor@12 203 | RtlSubAuthoritySid=RtlSubAuthoritySid@8 204 | RtlValidAcl=RtlValidAcl@4 205 | RtlValidRelativeSecurityDescriptor=RtlValidRelativeSecurityDescriptor@12 206 | RtlValidSecurityDescriptor=RtlValidSecurityDescriptor@4 207 | RtlValidSid=RtlValidSid@4 208 | 209 | ; splay.c 210 | RtlDeleteElementGenericTable=RtlDeleteElementGenericTable@8 211 | RtlEnumerateGenericTableWithoutSplaying=RtlEnumerateGenericTableWithoutSplaying@8 212 | RtlInitializeGenericTable=RtlInitializeGenericTable@20 213 | RtlInsertElementGenericTable=RtlInsertElementGenericTable@16 214 | RtlLookupElementGenericTable=RtlLookupElementGenericTable@8 215 | 216 | ; thread.c 217 | NtQueryInformationThread=NtQueryInformationThread@20 218 | NtSetThreadExecutionState=NtSetThreadExecutionState@8 219 | NtTerminateThread=NtTerminateThread@8 220 | RtlCreateUserThread=RtlCreateUserThread@40 221 | RtlDeleteCriticalSection=RtlDeleteCriticalSection@4 222 | RtlEnterCriticalSection=RtlEnterCriticalSection@4 223 | RtlInitializeCriticalSection=RtlInitializeCriticalSection@4 224 | RtlLeaveCriticalSection=RtlLeaveCriticalSection@4 225 | ZwTerminateThread=NtTerminateThread@8 226 | 227 | ; time.c 228 | RtlTimeToTimeFields=RtlTimeToTimeFields@8 229 | 230 | ; version.c 231 | RtlVerifyVersionInfo=RtlVerifyVersionInfo@16 232 | VerSetConditionMask=VerSetConditionMask@16 233 | -------------------------------------------------------------------------------- /libs/ntdll/random.c: -------------------------------------------------------------------------------- 1 | /* 2 | * COPYRIGHT: See COPYING in the top level directory 3 | * PROJECT: ReactOS system libraries 4 | * PURPOSE: Random number generator functions 5 | * FILE: lib/rtl/random.c 6 | * PROGRAMMER: 7 | */ 8 | 9 | /* INCLUDES *****************************************************************/ 10 | 11 | #include "ntdll.h" 12 | 13 | static ULONG SavedValue[128] = 14 | { 15 | 0x4c8bc0aa, 0x4c022957, 0x2232827a, 0x2f1e7626, /* 0 */ 16 | 0x7f8bdafb, 0x5c37d02a, 0x0ab48f72, 0x2f0c4ffa, /* 4 */ 17 | 0x290e1954, 0x6b635f23, 0x5d3885c0, 0x74b49ff8, /* 8 */ 18 | 0x5155fa54, 0x6214ad3f, 0x111e9c29, 0x242a3a09, /* 12 */ 19 | 0x75932ae1, 0x40ac432e, 0x54f7ba7a, 0x585ccbd5, /* 16 */ 20 | 0x6df5c727, 0x0374dad1, 0x7112b3f1, 0x735fc311, /* 20 */ 21 | 0x404331a9, 0x74d97781, 0x64495118, 0x323e04be, /* 24 */ 22 | 0x5974b425, 0x4862e393, 0x62389c1d, 0x28a68b82, /* 28 */ 23 | 0x0f95da37, 0x7a50bbc6, 0x09b0091c, 0x22cdb7b4, /* 32 */ 24 | 0x4faaed26, 0x66417ccd, 0x189e4bfa, 0x1ce4e8dd, /* 36 */ 25 | 0x5274c742, 0x3bdcf4dc, 0x2d94e907, 0x32eac016, /* 40 */ 26 | 0x26d33ca3, 0x60415a8a, 0x31f57880, 0x68c8aa52, /* 44 */ 27 | 0x23eb16da, 0x6204f4a1, 0x373927c1, 0x0d24eb7c, /* 48 */ 28 | 0x06dd7379, 0x2b3be507, 0x0f9c55b1, 0x2c7925eb, /* 52 */ 29 | 0x36d67c9a, 0x42f831d9, 0x5e3961cb, 0x65d637a8, /* 56 */ 30 | 0x24bb3820, 0x4d08e33d, 0x2188754f, 0x147e409e, /* 60 */ 31 | 0x6a9620a0, 0x62e26657, 0x7bd8ce81, 0x11da0abb, /* 64 */ 32 | 0x5f9e7b50, 0x23e444b6, 0x25920c78, 0x5fc894f0, /* 68 */ 33 | 0x5e338cbb, 0x404237fd, 0x1d60f80f, 0x320a1743, /* 72 */ 34 | 0x76013d2b, 0x070294ee, 0x695e243b, 0x56b177fd, /* 76 */ 35 | 0x752492e1, 0x6decd52f, 0x125f5219, 0x139d2e78, /* 80 */ 36 | 0x1898d11e, 0x2f7ee785, 0x4db405d8, 0x1a028a35, /* 84 */ 37 | 0x63f6f323, 0x1f6d0078, 0x307cfd67, 0x3f32a78a, /* 88 */ 38 | 0x6980796c, 0x462b3d83, 0x34b639f2, 0x53fce379, /* 92 */ 39 | 0x74ba50f4, 0x1abc2c4b, 0x5eeaeb8d, 0x335a7a0d, /* 96 */ 40 | 0x3973dd20, 0x0462d66b, 0x159813ff, 0x1e4643fd, /* 100 */ 41 | 0x06bc5c62, 0x3115e3fc, 0x09101613, 0x47af2515, /* 104 */ 42 | 0x4f11ec54, 0x78b99911, 0x3db8dd44, 0x1ec10b9b, /* 108 */ 43 | 0x5b5506ca, 0x773ce092, 0x567be81a, 0x5475b975, /* 112 */ 44 | 0x7a2cde1a, 0x494536f5, 0x34737bb4, 0x76d9750b, /* 116 */ 45 | 0x2a1f6232, 0x2e49644d, 0x7dddcbe7, 0x500cebdb, /* 120 */ 46 | 0x619dab9e, 0x48c626fe, 0x1cda3193, 0x52dabe9d /* 124 */ 47 | }; 48 | 49 | 50 | /************************************************************************* 51 | * RtlRandom [NTDLL.@] 52 | * 53 | * Generates a random number 54 | * 55 | * PARAMS 56 | * Seed [O] The seed of the Random function 57 | * 58 | * RETURNS 59 | * It returns a random number distributed over [0..MAXLONG-1]. 60 | */ 61 | /* 62 | * @implemented 63 | */ 64 | ULONG NTAPI 65 | RtlRandom (IN OUT PULONG Seed) 66 | { 67 | ULONG Rand; 68 | int Pos; 69 | ULONG Result; 70 | 71 | Rand = (*Seed * 0x7fffffed + 0x7fffffc3) % 0x7fffffff; 72 | *Seed = (Rand * 0x7fffffed + 0x7fffffc3) % 0x7fffffff; 73 | Pos = *Seed & 0x7f; 74 | Result = SavedValue[Pos]; 75 | SavedValue[Pos] = Rand; 76 | 77 | return Result; 78 | } 79 | 80 | /* 81 | * @implemented 82 | */ 83 | ULONG 84 | NTAPI 85 | RtlRandomEx( IN OUT PULONG Seed 86 | ) 87 | { 88 | ULONG Rand; 89 | int Pos; 90 | ULONG Result; 91 | 92 | Rand = (*Seed * 0x7fffffed + 0x7fffffc3) % 0x7fffffff; 93 | *Seed = (Rand * 0x7fffffed + 0x7fffffc3) % 0x7fffffff; 94 | Pos = *Seed & 0x7f; 95 | Result = SavedValue[Pos]; 96 | SavedValue[Pos] = Rand; 97 | 98 | return Result; 99 | } 100 | 101 | 102 | 103 | /************************************************************************* 104 | * RtlUniform [NTDLL.@] 105 | * 106 | * Generates an uniform random number 107 | * 108 | * PARAMS 109 | * Seed [O] The seed of the Random function 110 | * 111 | * RETURNS 112 | * It returns a random number uniformly distributed over [0..MAXLONG-1]. 113 | * 114 | * NOTES 115 | * Generates an uniform random number using D.H. Lehmer's 1948 algorithm. 116 | * In our case the algorithm is: 117 | * 118 | * Result = (*Seed * 0x7fffffed + 0x7fffffc3) % MAXLONG; 119 | * 120 | * *Seed = Result; 121 | * 122 | * DIFFERENCES 123 | * The native documentation states that the random number is 124 | * uniformly distributed over [0..MAXLONG]. In reality the native 125 | * function and our function return a random number uniformly 126 | * distributed over [0..MAXLONG-1]. 127 | */ 128 | ULONG 129 | NTAPI 130 | RtlUniform(IN PULONG Seed) 131 | { 132 | ULONG Result; 133 | 134 | /* Generate the random number */ 135 | Result = (*Seed * 0x7fffffed + 0x7fffffc3) % MAXLONG; 136 | 137 | /* Return it */ 138 | *Seed = Result; 139 | return Result; 140 | } 141 | -------------------------------------------------------------------------------- /libs/ntdll/registry.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010, Sherpya , aCaB 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of the copyright holders nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | #include "ntdll.h" 29 | 30 | MODULE(registry) 31 | 32 | #define REG_PREFIX "\\Registry\\Machine\\" 33 | 34 | typedef struct _RegKey 35 | { 36 | const char *path; 37 | void *data; 38 | DWORD len; 39 | DWORD type; 40 | } RegKey; 41 | 42 | static RegKey RegKeys[] = 43 | { 44 | { "Software\\Microsoft\\Windows NT\\CurrentVersion\\SystemRoot", systemroot, sizeof(systemroot), REG_SZ }, 45 | 46 | { "Hardware\\Description\\System\\Identifier", L"AT/AT COMPATIBLE", sizeof(L"AT/AT COMPATIBLE"), REG_SZ }, 47 | 48 | { "System\\Setup\\SystemPartition", L"\\Device\\HarddiskVolume1", sizeof(L"\\Device\\HarddiskVolume1"), REG_SZ }, 49 | 50 | { "System\\ControlSet000\\Control\\SystemStartOptions", L"FASTDETECT", sizeof(L"FASTDETECT"), REG_SZ }, 51 | { "System\\CurrentControlSet\\Control\\SystemStartOptions", L"FASTDETECT", sizeof(L"FASTDETECT"), REG_SZ }, 52 | 53 | { "Session Manager\\BootExecute", L"autocheck autochk *\0\0", sizeof(L"autocheck autochk *\0\0"), REG_MULTI_SZ }, 54 | { "System\\ControlSet000\\Control\\Session Manager\\BootExecute", L"autocheck autochk *\0\0", 55 | sizeof(L"autocheck autochk *\0\0"), REG_MULTI_SZ }, 56 | { "System\\CurrentControlSet\\Control\\Session Manager\\BootExecute", L"autocheck autochk *\0\0", 57 | sizeof(L"autocheck autochk *\0\0"), REG_MULTI_SZ }, 58 | 59 | { "System\\ControlSet000\\Control\\Session Manager\\Memory Management\\PagingFiles", L"c:\\pagefile.sys\0\0", 60 | sizeof(L"c:\\pagefile.sys\0\0"), REG_MULTI_SZ }, 61 | 62 | { NULL, NULL, 0 } 63 | }; 64 | 65 | // FIXME: handle REG_DWORD 66 | static RegKey *RegistryLookup(const char *path) 67 | { 68 | int i = 0; 69 | 70 | if (!strncasecmp(path, REG_PREFIX, sizeof(REG_PREFIX) - 1)) 71 | path += sizeof(REG_PREFIX) - 1; 72 | 73 | while (RegKeys[i].path) 74 | { 75 | if (!strcasecmp(path, RegKeys[i].path)) 76 | return &RegKeys[i]; 77 | i++; 78 | } 79 | return NULL; 80 | } 81 | 82 | NTSTATUS NTAPI NtOpenKey(PHANDLE retkey, ACCESS_MASK access, POBJECT_ATTRIBUTES ObjectAttributes) 83 | { 84 | DECLAREVARCONV(ObjectAttributesA); 85 | CHECK_POINTER(retkey); 86 | CHECK_POINTER(ObjectAttributes); 87 | CHECK_POINTER(ObjectAttributes->ObjectName); 88 | 89 | OA2STR(ObjectAttributes); 90 | 91 | Log("ntdll.NtOpenKey(\"%s\")\n", ObjectAttributesA); 92 | 93 | __CreateHandle(*retkey, HANDLE_REG, ObjectAttributesA); 94 | 95 | return STATUS_SUCCESS; 96 | } 97 | FORWARD_FUNCTION(NtOpenKey, ZwOpenKey); 98 | 99 | NTSTATUS NTAPI NtQueryValueKey(HANDLE KeyHandle, PUNICODE_STRING ValueName, 100 | KEY_VALUE_INFORMATION_CLASS KeyValueInformationClass, PVOID KeyValueInformation, ULONG Length, PULONG ResultLength) 101 | { 102 | DECLAREVARCONV(ValueNameA); 103 | RegKey *value; 104 | char path[512]; 105 | size_t vlen; 106 | 107 | CHECK_HANDLE(KeyHandle, HANDLE_REG); 108 | CHECK_POINTER(ValueName); 109 | 110 | US2STR(ValueName); 111 | Log("ntdll.NtQueryValueKey(\"%s\", \"%s\", %d) - ", strhandle(KeyHandle), ValueNameA, KeyValueInformationClass); 112 | 113 | snprintf(path, sizeof(path), "%s\\%s", KeyHandle->name, ValueNameA); 114 | 115 | value = RegistryLookup(path); 116 | 117 | if (!value) 118 | { 119 | Log("Not Found\n"); 120 | return STATUS_OBJECT_NAME_NOT_FOUND; 121 | } 122 | 123 | Log("OK\n"); 124 | 125 | vlen = value->len; 126 | switch (KeyValueInformationClass) 127 | { 128 | case KeyValueFullInformation: 129 | { 130 | KEY_VALUE_FULL_INFORMATION *k = KeyValueInformation; 131 | 132 | vlen += sizeof(KEY_VALUE_FULL_INFORMATION); 133 | 134 | if (ResultLength) 135 | *ResultLength = vlen; 136 | 137 | if (Length < vlen) 138 | return STATUS_BUFFER_TOO_SMALL; 139 | 140 | k->TitleIndex = 0; 141 | k->Type = value->type; 142 | k->DataOffset = FIELD_OFFSET(KEY_VALUE_FULL_INFORMATION, Name); 143 | k->DataLength = k->NameLength = value->len; 144 | memcpy(k->Name, value->data, value->len); 145 | 146 | return STATUS_SUCCESS; 147 | } 148 | case KeyValuePartialInformation: 149 | { 150 | KEY_VALUE_PARTIAL_INFORMATION *k = KeyValueInformation; 151 | 152 | vlen += sizeof(KEY_VALUE_PARTIAL_INFORMATION); 153 | 154 | if (ResultLength) 155 | *ResultLength = vlen; 156 | 157 | if (Length < vlen) 158 | return STATUS_BUFFER_TOO_SMALL; 159 | 160 | k->TitleIndex = 0; 161 | k->Type = value->type; 162 | k->DataLength = value->len; 163 | memcpy(k->Data, value->data, value->len); 164 | 165 | return STATUS_SUCCESS; 166 | } 167 | default: 168 | return STATUS_NOT_IMPLEMENTED; 169 | } 170 | } 171 | FORWARD_FUNCTION(NtQueryValueKey, ZwQueryValueKey); 172 | 173 | NTSTATUS NTAPI NtSetValueKey(HANDLE KeyHandle, PUNICODE_STRING ValueName, ULONG TitleIndex, ULONG Type, PVOID Data, ULONG DataSize) 174 | { 175 | DECLAREVARCONV(ValueNameA); 176 | DECLAREVARCONV(DataA); 177 | 178 | CHECK_HANDLE(KeyHandle, HANDLE_REG); 179 | CHECK_POINTER(ValueName); 180 | 181 | US2STR(ValueName); 182 | RtlUnicodeToOemN(DataA, _CONVBUF, NULL, (LPCWSTR) Data, DataSize); 183 | 184 | if (Type == REG_MULTI_SZ) 185 | { 186 | unsigned int i; 187 | for (i = 0; i < DataSize / sizeof(WCHAR); i++) 188 | if (!ValueNameA[i]) ValueNameA[i] = '|'; 189 | } 190 | 191 | Log("ntdll.NtSetValueKey(\"%s\", \"%s\", \"%s\", %d)\n", strhandle(KeyHandle), ValueNameA, DataA, Type); 192 | 193 | return STATUS_SUCCESS; 194 | } 195 | FORWARD_FUNCTION(NtSetValueKey, ZwSetValueKey); 196 | 197 | NTSTATUS NTAPI RtlQueryRegistryValues(ULONG RelativeTo, PCWSTR Path, PRTL_QUERY_REGISTRY_TABLE QueryTable, PVOID Context, PVOID Environment) 198 | { 199 | NTSTATUS result = STATUS_OBJECT_NAME_NOT_FOUND; 200 | DECLAREVARCONV(PathA); 201 | DECLAREVARCONV(key); 202 | char path[512]; 203 | RegKey *value; 204 | 205 | CHECK_POINTER(Path); 206 | CHECK_POINTER(QueryTable); 207 | 208 | WSTR2STR(Path); 209 | Log("ntdll.RtlQueryRegistryValues(\"%s\"): keys -> ", PathA); 210 | 211 | for (; QueryTable->QueryRoutine != NULL || QueryTable->Name != NULL; ++QueryTable) 212 | { 213 | WSTR2STREX(QueryTable->Name, key); 214 | snprintf(path, sizeof(path), "%s\\%s", PathA, key); 215 | value = RegistryLookup(path); 216 | 217 | Log("%s (", path); 218 | 219 | if (!value) 220 | { 221 | Log("Not found)"); 222 | continue; 223 | } 224 | 225 | if (QueryTable->Flags & RTL_QUERY_REGISTRY_DIRECT) 226 | { 227 | PUNICODE_STRING str = QueryTable->EntryContext; 228 | if (str->MaximumLength < value->len) 229 | result = STATUS_BUFFER_TOO_SMALL; 230 | else 231 | { 232 | memcpy(str->Buffer, value->data, value->len); 233 | str->Length = value->len; 234 | result = STATUS_SUCCESS; 235 | } 236 | Log("OK)"); 237 | break; 238 | } 239 | Log(")"); 240 | } 241 | 242 | Log("\n"); 243 | 244 | return result; 245 | } 246 | 247 | /* Windows8 drivers use this new function as much as possible. 248 | If driver calls new function and the registy key is untrusted, 249 | it would cause BugCheck = KERNEL_SECURITY_CHECK_FAILURE */ 250 | FORWARD_FUNCTION(RtlQueryRegistryValues, RtlQueryRegistryValuesEx); 251 | 252 | NTSTATUS NTAPI RtlWriteRegistryValue(IN ULONG RelativeTo, IN PCWSTR Path, IN PCWSTR ValueName, 253 | IN ULONG ValueType, IN PVOID ValueData, IN ULONG ValueLength) 254 | { 255 | DECLAREVARCONV(PathA); 256 | DECLAREVARCONV(ValueNameA); 257 | WSTR2STR(Path); 258 | WSTR2STR(ValueName); 259 | Log("ntdll.RtlWriteRegistryValue(\"%s\\%s\")\n", PathA, ValueNameA); 260 | return STATUS_SUCCESS; 261 | } 262 | -------------------------------------------------------------------------------- /libs/ntdll/scancodes.c: -------------------------------------------------------------------------------- 1 | static const unsigned char AsciiToScan[] = { 2 | 0x00, // '\x00' 3 | 0x00, // '\x01' 4 | 0x00, // '\x02' 5 | 0x00, // '\x03' 6 | 0x00, // '\x04' 7 | 0x00, // '\x05' 8 | 0x00, // '\x06' 9 | 0x00, // '\x07' 10 | 0x0e, // '\x08' 11 | 0x00, // '\t' 12 | 0x1c, // '\n' 13 | 0x00, // '\x0b' 14 | 0x00, // '\x0c' 15 | 0x1c, // '\r' 16 | 0x00, // '\x0e' 17 | 0x00, // '\x0f' 18 | 0x00, // '\x10' 19 | 0x00, // '\x11' 20 | 0x00, // '\x12' 21 | 0x00, // '\x13' 22 | 0x00, // '\x14' 23 | 0x00, // '\x15' 24 | 0x00, // '\x16' 25 | 0x00, // '\x17' 26 | 0x00, // '\x18' 27 | 0x00, // '\x19' 28 | 0x00, // '\x1a' 29 | 0x00, // '\x1b' 30 | 0x00, // '\x1c' 31 | 0x00, // '\x1d' 32 | 0x00, // '\x1e' 33 | 0x00, // '\x1f' 34 | 0x39, // ' ' 35 | 0x02, // '!' 36 | 0x28, // '"' 37 | 0x04, // '#' 38 | 0x05, // '$' 39 | 0x06, // '%' 40 | 0x08, // '&' 41 | 0x29, // "'" 42 | 0x0a, // '(' 43 | 0x0b, // ')' 44 | 0x37, // '*' 45 | 0x4e, // '+' 46 | 0x33, // ',' 47 | 0x4a, // '-' 48 | 0x34, // '.' 49 | 0x35, // '/' 50 | 0x0b, // '0' 51 | 0x02, // '1' 52 | 0x03, // '2' 53 | 0x04, // '3' 54 | 0x05, // '4' 55 | 0x06, // '5' 56 | 0x07, // '6' 57 | 0x08, // '7' 58 | 0x09, // '8' 59 | 0x0a, // '9' 60 | 0x27, // ':' 61 | 0x27, // ';' 62 | 0x33, // '<' 63 | 0x0d, // '=' 64 | 0x34, // '>' 65 | 0x35, // '?' 66 | 0x03, // '@' 67 | 0x1e, // 'A' 68 | 0x30, // 'B' 69 | 0x2e, // 'C' 70 | 0x20, // 'D' 71 | 0x12, // 'E' 72 | 0x21, // 'F' 73 | 0x22, // 'G' 74 | 0x23, // 'H' 75 | 0x17, // 'I' 76 | 0x24, // 'J' 77 | 0x25, // 'K' 78 | 0x26, // 'L' 79 | 0x32, // 'M' 80 | 0x31, // 'N' 81 | 0x18, // 'O' 82 | 0x19, // 'P' 83 | 0x10, // 'Q' 84 | 0x13, // 'R' 85 | 0x1f, // 'S' 86 | 0x14, // 'T' 87 | 0x16, // 'U' 88 | 0x2f, // 'V' 89 | 0x11, // 'W' 90 | 0x2d, // 'X' 91 | 0x15, // 'Y' 92 | 0x2c, // 'Z' 93 | 0x1a, // '[' 94 | 0x2b, // '\\' 95 | 0x1b, // ']' 96 | 0x07, // '^' 97 | 0x0c, // '_' 98 | 0x00, // '`' 99 | 0x1e, // 'a' 100 | 0x30, // 'b' 101 | 0x2e, // 'c' 102 | 0x20, // 'd' 103 | 0x12, // 'e' 104 | 0x21, // 'f' 105 | 0x22, // 'g' 106 | 0x23, // 'h' 107 | 0x17, // 'i' 108 | 0x24, // 'j' 109 | 0x25, // 'k' 110 | 0x26, // 'l' 111 | 0x32, // 'm' 112 | 0x31, // 'n' 113 | 0x18, // 'o' 114 | 0x19, // 'p' 115 | 0x10, // 'q' 116 | 0x13, // 'r' 117 | 0x1f, // 's' 118 | 0x14, // 't' 119 | 0x16, // 'u' 120 | 0x2f, // 'v' 121 | 0x11, // 'w' 122 | 0x2d, // 'x' 123 | 0x15, // 'y' 124 | 0x2c, // 'z' 125 | 0x1a, // '{' 126 | 0x2b, // '|' 127 | 0x1b, // '}' 128 | 0x29, // '~' 129 | 0x0e, // '\x7f' 130 | 0x00, // '\x80' 131 | 0x00, // '\x81' 132 | 0x00, // '\x82' 133 | 0x00, // '\x83' 134 | 0x00, // '\x84' 135 | 0x00, // '\x85' 136 | 0x00, // '\x86' 137 | 0x00, // '\x87' 138 | 0x00, // '\x88' 139 | 0x00, // '\x89' 140 | 0x00, // '\x8a' 141 | 0x00, // '\x8b' 142 | 0x00, // '\x8c' 143 | 0x00, // '\x8d' 144 | 0x00, // '\x8e' 145 | 0x00, // '\x8f' 146 | 0x00, // '\x90' 147 | 0x00, // '\x91' 148 | 0x00, // '\x92' 149 | 0x00, // '\x93' 150 | 0x00, // '\x94' 151 | 0x00, // '\x95' 152 | 0x00, // '\x96' 153 | 0x00, // '\x97' 154 | 0x00, // '\x98' 155 | 0x00, // '\x99' 156 | 0x00, // '\x9a' 157 | 0x00, // '\x9b' 158 | 0x00, // '\x9c' 159 | 0x00, // '\x9d' 160 | 0x00, // '\x9e' 161 | 0x00, // '\x9f' 162 | 0x00, // '\xa0' 163 | 0x00, // '\xa1' 164 | 0x00, // '\xa2' 165 | 0x00, // '\xa3' 166 | 0x00, // '\xa4' 167 | 0x00, // '\xa5' 168 | 0x00, // '\xa6' 169 | 0x00, // '\xa7' 170 | 0x00, // '\xa8' 171 | 0x00, // '\xa9' 172 | 0x00, // '\xaa' 173 | 0x00, // '\xab' 174 | 0x00, // '\xac' 175 | 0x00, // '\xad' 176 | 0x00, // '\xae' 177 | 0x00, // '\xaf' 178 | 0x00, // '\xb0' 179 | 0x00, // '\xb1' 180 | 0x00, // '\xb2' 181 | 0x00, // '\xb3' 182 | 0x00, // '\xb4' 183 | 0x00, // '\xb5' 184 | 0x00, // '\xb6' 185 | 0x00, // '\xb7' 186 | 0x00, // '\xb8' 187 | 0x00, // '\xb9' 188 | 0x00, // '\xba' 189 | 0x00, // '\xbb' 190 | 0x00, // '\xbc' 191 | 0x00, // '\xbd' 192 | 0x00, // '\xbe' 193 | 0x00, // '\xbf' 194 | 0x00, // '\xc0' 195 | 0x00, // '\xc1' 196 | 0x00, // '\xc2' 197 | 0x00, // '\xc3' 198 | 0x00, // '\xc4' 199 | 0x00, // '\xc5' 200 | 0x00, // '\xc6' 201 | 0x00, // '\xc7' 202 | 0x00, // '\xc8' 203 | 0x00, // '\xc9' 204 | 0x00, // '\xca' 205 | 0x00, // '\xcb' 206 | 0x00, // '\xcc' 207 | 0x00, // '\xcd' 208 | 0x00, // '\xce' 209 | 0x00, // '\xcf' 210 | 0x00, // '\xd0' 211 | 0x00, // '\xd1' 212 | 0x00, // '\xd2' 213 | 0x00, // '\xd3' 214 | 0x00, // '\xd4' 215 | 0x00, // '\xd5' 216 | 0x00, // '\xd6' 217 | 0x00, // '\xd7' 218 | 0x00, // '\xd8' 219 | 0x00, // '\xd9' 220 | 0x00, // '\xda' 221 | 0x00, // '\xdb' 222 | 0x00, // '\xdc' 223 | 0x00, // '\xdd' 224 | 0x00, // '\xde' 225 | 0x00, // '\xdf' 226 | 0x00, // '\xe0' 227 | 0x00, // '\xe1' 228 | 0x00, // '\xe2' 229 | 0x00, // '\xe3' 230 | 0x00, // '\xe4' 231 | 0x00, // '\xe5' 232 | 0x00, // '\xe6' 233 | 0x00, // '\xe7' 234 | 0x00, // '\xe8' 235 | 0x00, // '\xe9' 236 | 0x00, // '\xea' 237 | 0x00, // '\xeb' 238 | 0x00, // '\xec' 239 | 0x00, // '\xed' 240 | 0x00, // '\xee' 241 | 0x00, // '\xef' 242 | 0x00, // '\xf0' 243 | 0x00, // '\xf1' 244 | 0x00, // '\xf2' 245 | 0x00, // '\xf3' 246 | 0x00, // '\xf4' 247 | 0x00, // '\xf5' 248 | 0x00, // '\xf6' 249 | 0x00, // '\xf7' 250 | 0x00, // '\xf8' 251 | 0x00, // '\xf9' 252 | 0x00, // '\xfa' 253 | 0x00, // '\xfb' 254 | 0x00, // '\xfc' 255 | 0x00, // '\xfd' 256 | 0x00 // '\xfe' 257 | }; 258 | -------------------------------------------------------------------------------- /libs/ntdll/str.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010, Sherpya , aCaB 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of the copyright holders nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | #include "ntdll.h" 29 | 30 | char *strhandle(HANDLE Handle) 31 | { 32 | if (!Handle) 33 | return "!! NULL HANDLE !!"; 34 | if (Handle->cookie == HANDLE_COOKIE) 35 | return Handle->desc; 36 | return "Not my own"; 37 | } 38 | 39 | const char *strctl(ULONG ControlCode) 40 | { 41 | switch (ControlCode) 42 | { 43 | case FSCTL_LOCK_VOLUME: 44 | return "FSCTL_LOCK_VOLUME"; 45 | case FSCTL_UNLOCK_VOLUME: 46 | return "FSCTL_UNLOCK_VOLUME"; 47 | case FSCTL_DISMOUNT_VOLUME: 48 | return "FSCTL_DISMOUNT_VOLUME"; 49 | case FSCTL_GET_RETRIEVAL_POINTERS: 50 | return "FSCTL_GET_RETRIEVAL_POINTERS"; 51 | case FSCTL_IS_VOLUME_DIRTY: 52 | return "FSCTL_IS_VOLUME_DIRTY"; 53 | case FSCTL_QUERY_DEPENDENT_VOLUME: 54 | return "FSCTL_QUERY_DEPENDENT_VOLUME"; 55 | case FSCTL_ALLOW_EXTENDED_DASD_IO: 56 | return "FSCTL_ALLOW_EXTENDED_DASD_IO"; 57 | case IOCTL_STORAGE_GET_HOTPLUG_INFO: 58 | return "IOCTL_STORAGE_GET_HOTPLUG_INFO"; 59 | case IOCTL_STORAGE_GET_DEVICE_NUMBER: 60 | return "IOCTL_STORAGE_GET_DEVICE_NUMBER"; 61 | case IOCTL_STORAGE_QUERY_PROPERTY: 62 | return "IOCTL_STORAGE_QUERY_PROPERTY"; 63 | case IOCTL_KEYBOARD_SET_INDICATORS: 64 | return "IOCTL_KEYBOARD_SET_INDICATORS"; 65 | case IOCTL_KEYBOARD_QUERY_INDICATORS: 66 | return "IOCTL_KEYBOARD_QUERY_INDICATORS"; 67 | case IOCTL_DISK_GET_PARTITION_INFO: 68 | return "IOCTL_DISK_GET_PARTITION_INFO"; 69 | case IOCTL_DISK_SET_PARTITION_INFO: 70 | return "IOCTL_DISK_SET_PARTITION_INFO"; 71 | case IOCTL_DISK_GET_DRIVE_LAYOUT: 72 | return "IOCTL_DISK_GET_DRIVE_LAYOUT"; 73 | case IOCTL_DISK_GET_DRIVE_GEOMETRY: 74 | return "IOCTL_DISK_GET_DRIVE_GEOMETRY"; 75 | case IOCTL_DISK_IS_WRITABLE: 76 | return "IOCTL_DISK_IS_WRITABLE"; 77 | case IOCTL_DISK_GET_PARTITION_INFO_EX: 78 | return "IOCTL_DISK_GET_PARTITION_INFO_EX"; 79 | case IOCTL_DISK_VERIFY: 80 | return "IOCTL_DISK_VERIFY"; 81 | case IOCTL_DISK_GET_DRIVE_LAYOUT_EX: 82 | return "IOCTL_DISK_GET_DRIVE_LAYOUT_EX"; 83 | case IOCTL_DISK_GET_LENGTH_INFO: 84 | return "IOCTL_DISK_GET_LENGTH_INFO"; 85 | case IOCTL_DISK_GET_MEDIA_TYPES: 86 | return "IOCTL_DISK_GET_MEDIA_TYPES"; 87 | case IOCTL_MOUNTDEV_QUERY_DEVICE_NAME: 88 | return "IOCTL_MOUNTDEV_QUERY_DEVICE_NAME"; 89 | default: 90 | return "CTL_UNKNOWN"; 91 | } 92 | } 93 | 94 | const char *strsysinfo(SYSTEM_INFORMATION_CLASS infoclass) 95 | { 96 | switch (infoclass) 97 | { 98 | case SystemBasicInformation: 99 | return "SystemBasicInformation"; 100 | case SystemPerformanceInformation: 101 | return "SystemPerformanceInformation"; 102 | case SystemTimeOfDayInformation: 103 | return "SystemTimeOfDayInformation"; 104 | default: 105 | return "Unknown_System_Info_Class"; 106 | } 107 | } 108 | 109 | const char *strfileinfo(FILE_INFORMATION_CLASS infoclass) 110 | { 111 | switch (infoclass) 112 | { 113 | case FileAlignmentInformation: 114 | return "FileAlignmentInformation"; 115 | case FileBasicInformation: 116 | return "FileBasicInformation"; 117 | case FileStandardInformation: 118 | return "FileStandardInformation"; 119 | default: 120 | return "Unknown_File_Info_Class"; 121 | } 122 | } 123 | 124 | const char *strfsinfo(FS_INFORMATION_CLASS infoclass) 125 | { 126 | switch (infoclass) 127 | { 128 | case FileFsDeviceInformation: 129 | return "FileFsDeviceInformation"; 130 | default: 131 | return "Unknown_Fs_Info_Class"; 132 | } 133 | } 134 | 135 | const char *strthinfo(THREADINFOCLASS infoclass) 136 | { 137 | switch (infoclass) 138 | { 139 | case ThreadBasicInformation: 140 | return "ThreadBasicInformation"; 141 | default: 142 | return "Unknown_Thread_Info_Class"; 143 | } 144 | } 145 | 146 | const char *strspqid(STORAGE_PROPERTY_ID PropertyId) 147 | { 148 | switch (PropertyId) 149 | { 150 | case StorageDeviceProperty: 151 | return "StorageDeviceProperty"; 152 | case StorageAdapterProperty: 153 | return "StorageAdapterProperty"; 154 | case StorageDeviceIdProperty: 155 | return "StorageDeviceIdProperty"; 156 | case StorageDeviceUniqueIdProperty: 157 | return "StorageDeviceUniqueIdProperty"; 158 | case StorageDeviceWriteCacheProperty: 159 | return "StorageDeviceWriteCacheProperty"; 160 | case StorageMiniportProperty: 161 | return "StorageMiniportProperty"; 162 | case StorageAccessAlignmentProperty: 163 | return "StorageAccessAlignmentProperty"; 164 | case StorageDeviceSeekPenaltyProperty: 165 | return "StorageDeviceSeekPenaltyProperty"; 166 | case StorageDeviceTrimProperty: 167 | return "StorageDeviceTrimProperty"; 168 | default: 169 | return "Unknown_Storage_PropertyId"; 170 | } 171 | } 172 | 173 | const char *strspqtype(STORAGE_QUERY_TYPE QueryType) 174 | { 175 | switch (QueryType) 176 | { 177 | case PropertyStandardQuery: 178 | return "PropertyStandardQuery"; 179 | case PropertyExistsQuery: 180 | return "PropertyExistsQuery"; 181 | case PropertyMaskQuery: 182 | return "PropertyMaskQuery"; 183 | case PropertyQueryMaxDefined: 184 | return "PropertyQueryMaxDefined"; 185 | default: 186 | return "Unknown_Storage_QueryType"; 187 | } 188 | } 189 | -------------------------------------------------------------------------------- /libs/ntdll/sync.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010, Sherpya , aCaB 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of the copyright holders nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | #include "ntdll.h" 29 | #include 30 | 31 | MODULE(sync) 32 | 33 | #ifdef THREADED 34 | #include 35 | #include 36 | 37 | static inline int interval_to_timespec(PLARGE_INTEGER DelayInterval, struct timespec *timeout, int relative) 38 | { 39 | LARGE_INTEGER when, delay; 40 | 41 | if (!DelayInterval) 42 | return 0; 43 | 44 | delay.QuadPart = DelayInterval->QuadPart; 45 | 46 | NtQuerySystemTime(&when); 47 | 48 | // Negative value means delay relative to current 49 | if (DelayInterval->QuadPart < 0) 50 | { 51 | if (relative) 52 | when.QuadPart = -DelayInterval->QuadPart; 53 | else 54 | when.QuadPart -= DelayInterval->QuadPart; 55 | } 56 | else 57 | { 58 | if (relative) 59 | when.QuadPart = DelayInterval->QuadPart - when.QuadPart; 60 | else 61 | when.QuadPart = DelayInterval->QuadPart; 62 | } 63 | 64 | timeout->tv_sec = when.QuadPart / 1000000; 65 | timeout->tv_nsec = when.QuadPart % 1000000; 66 | 67 | //Log("sync: %lld -> Rel: %s sec: %lu nsec: %lu\n", DelayInterval->QuadPart, relative ? "yes" : "no", timeout->tv_sec, timeout->tv_nsec); 68 | 69 | return 1; 70 | } 71 | #endif 72 | 73 | NTSTATUS NTAPI NtWaitForSingleObject(HANDLE Handle, BOOLEAN Alertable, PLARGE_INTEGER Timeout) 74 | { 75 | CHECK_POINTER(Handle); 76 | 77 | Log("ntdll.NtWaitForSingleObject(\"%s\", %llu)\n", strhandle(Handle), Timeout ? Timeout->QuadPart : 0); 78 | 79 | switch (Handle->type) 80 | { 81 | case HANDLE_FILE: 82 | return STATUS_SUCCESS; 83 | case HANDLE_EV: 84 | { 85 | NTSTATUS result = STATUS_SUCCESS; 86 | 87 | #ifdef THREADED 88 | struct timespec timeout; 89 | 90 | interval_to_timespec(Timeout, &timeout, 0); 91 | pthread_mutex_lock(&Handle->event.cond_mutex); 92 | 93 | while (1) 94 | { 95 | int ret; 96 | pthread_mutex_lock(&Handle->event.state_mutex); 97 | 98 | if(Handle->event.signaled) 99 | { 100 | result = STATUS_SUCCESS; 101 | break; 102 | } 103 | pthread_mutex_unlock(&Handle->event.state_mutex); 104 | 105 | if (Timeout && Timeout->QuadPart) 106 | { 107 | struct timeval before, after; 108 | gettimeofday(&before, NULL); 109 | ret = pthread_cond_timedwait(&Handle->event.cond, &Handle->event.cond_mutex, &timeout); 110 | gettimeofday(&after, NULL); 111 | timeout.tv_sec -= after.tv_sec - before.tv_sec; 112 | timeout.tv_nsec -= (after.tv_usec - before.tv_usec) * 1000; 113 | } 114 | else 115 | ret = pthread_cond_wait(&Handle->event.cond, &Handle->event.cond_mutex); 116 | 117 | switch (ret) 118 | { 119 | case 0: 120 | continue; 121 | case ETIMEDOUT: 122 | result = STATUS_TIMEOUT; 123 | break; 124 | default:/* EINVAL, EPERM, ENOTLOCKED, etc. */ 125 | result = STATUS_UNSUCCESSFUL; 126 | } 127 | break; 128 | } 129 | pthread_mutex_unlock(&Handle->event.cond_mutex); 130 | #else /* THREADED */ 131 | if(Handle->event.signaled) { /* I SHOULD BE DEALOCKING HERE */ } 132 | #endif /* THREADED */ 133 | if(result == STATUS_SUCCESS) 134 | { 135 | if(Handle->event.type == SynchronizationEvent) 136 | Handle->event.signaled = 0; 137 | #ifdef THREADED 138 | pthread_mutex_unlock(&Handle->event.state_mutex); 139 | #endif 140 | } 141 | return result; 142 | } 143 | case HANDLE_TH: 144 | { 145 | #ifdef THREADED 146 | pthread_join(Handle->thread.tid, NULL); 147 | #endif 148 | return STATUS_SUCCESS; 149 | } 150 | } 151 | 152 | return STATUS_NOT_IMPLEMENTED; 153 | } 154 | FORWARD_FUNCTION(NtWaitForSingleObject, ZwWaitForSingleObject); 155 | 156 | NTSTATUS NTAPI NtWaitForMultipleObjects(ULONG ObjectCount, PHANDLE ObjectsArray, 157 | OBJECT_WAIT_TYPE WaitType, BOOLEAN Alertable, PLARGE_INTEGER Timeout) 158 | { 159 | // FIXME: in threaded mode a call to: 160 | // NtWaitForSingleObject(ObjectsArray[0], Alertable, Timeout) 161 | // the programs creates an event and associates it to NtReadFile call 162 | // this event is triggered when something is ready 163 | // native programs use this method to e.g. read from keyboard 164 | 165 | if ((ObjectCount == 1) || (ObjectCount = 2)) // keyboards 166 | return STATUS_TIMEOUT; 167 | 168 | /* pagedefrag */ 169 | if (ObjectCount == 16) 170 | return STATUS_TIMEOUT; 171 | 172 | fprintf(stderr, "ntdll.NtWaitForMultipleObjects() is only implemented for the keyboard stuff\n"); 173 | abort(); 174 | 175 | return STATUS_UNSUCCESSFUL; /* vc please... abort is noreturn */ 176 | } 177 | FORWARD_FUNCTION(NtWaitForMultipleObjects, ZwWaitForMultipleObjects); 178 | 179 | NTSTATUS NTAPI NtCreateEvent(PHANDLE EventHandle, ACCESS_MASK DesiredAccess, 180 | POBJECT_ATTRIBUTES ObjectAttributes, EVENT_TYPE EventType, BOOLEAN InitialState) 181 | { 182 | char EventA[_CONVBUF] = "-Event-"; 183 | CHECK_POINTER(EventHandle); 184 | 185 | if (ObjectAttributes && ObjectAttributes->ObjectName) 186 | RtlUnicodeToOemN(EventA, sizeof(EventA), NULL, 187 | ObjectAttributes->ObjectName->Buffer, ObjectAttributes->ObjectName->Length); 188 | 189 | __CreateHandle(*EventHandle, HANDLE_EV, EventA); 190 | 191 | (*EventHandle)->event.type = EventType; 192 | (*EventHandle)->event.signaled = InitialState; 193 | 194 | #ifdef THREADED 195 | pthread_mutex_init(&(*EventHandle)->event.state_mutex, NULL); 196 | pthread_mutex_init(&(*EventHandle)->event.cond_mutex, NULL); 197 | pthread_cond_init(&(*EventHandle)->event.cond, NULL); 198 | #endif 199 | 200 | Log("ntdll.NtCreateEvent(\"%s\")\n", EventA); 201 | 202 | return STATUS_SUCCESS; 203 | } 204 | 205 | NTSTATUS NTAPI NtOpenEvent(PHANDLE EventHandle, ACCESS_MASK DesiredAccess, POBJECT_ATTRIBUTES ObjectAttributes) 206 | { 207 | DECLAREVARCONV(ObjectAttributesA); 208 | 209 | CHECK_POINTER(EventHandle); 210 | CHECK_POINTER(ObjectAttributes); 211 | 212 | OA2STR(ObjectAttributes); 213 | 214 | Log("ntdll.NtOpenEvent(\"%s\")\n", ObjectAttributesA); 215 | return STATUS_NOT_IMPLEMENTED; 216 | } 217 | FORWARD_FUNCTION(NtOpenEvent, ZwOpenEvent); 218 | 219 | NTSTATUS NTAPI NtSetEvent(HANDLE EventHandle, PLONG PreviousState) 220 | { 221 | CHECK_HANDLE(EventHandle, HANDLE_EV); 222 | 223 | #ifdef THREADED 224 | pthread_mutex_lock(&EventHandle->event.state_mutex); 225 | #endif 226 | if (PreviousState) 227 | *PreviousState = EventHandle->event.signaled; 228 | EventHandle->event.signaled = 1; 229 | #ifdef THREADED 230 | pthread_cond_signal(&EventHandle->event.cond); 231 | pthread_mutex_unlock(&EventHandle->event.state_mutex); 232 | #endif 233 | 234 | Log("ntdll.NtSetEvent(\"%s\")\n", strhandle(EventHandle)); 235 | 236 | return STATUS_SUCCESS; 237 | } 238 | 239 | NTSTATUS NTAPI NtResetEvent(HANDLE EventHandle, PLONG PreviousState) 240 | { 241 | CHECK_HANDLE(EventHandle, HANDLE_EV); 242 | 243 | Log("ntdll.NtReSetEvent(\"%s\")\n", strhandle(EventHandle)); 244 | 245 | #ifdef THREADED 246 | pthread_mutex_lock(&EventHandle->event.state_mutex); 247 | #endif 248 | if (PreviousState) 249 | *PreviousState = EventHandle->event.signaled; 250 | EventHandle->event.signaled = 0; 251 | #ifdef THREADED 252 | pthread_mutex_unlock(&EventHandle->event.state_mutex); 253 | #endif 254 | 255 | return STATUS_SUCCESS; 256 | } 257 | 258 | NTSTATUS NTAPI NtDelayExecution(BOOLEAN Alertable, PLARGE_INTEGER DelayInterval) 259 | { 260 | Log("ntdll.NtDelayExecution(%d, %llu)\n", Alertable, DelayInterval ? DelayInterval->QuadPart : 0); 261 | #if 0 262 | struct timespec timeout; 263 | if (!interval_to_timespec(DelayInterval, &timeout, 1)) 264 | return STATUS_SUCCESS; 265 | 266 | nanosleep(&timeout, NULL); 267 | #endif 268 | return STATUS_SUCCESS; 269 | } 270 | 271 | NTSTATUS NTAPI NtClearEvent(HANDLE EventHandle) 272 | { 273 | CHECK_HANDLE(EventHandle, HANDLE_EV); 274 | 275 | Log("ntdll.NtClearEvent(\"%s\")\n", strhandle(EventHandle)); 276 | return NtResetEvent(EventHandle, NULL); 277 | } 278 | FORWARD_FUNCTION(NtClearEvent, ZwClearEvent); 279 | -------------------------------------------------------------------------------- /libs/ntdll/thread.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010, Sherpya , aCaB 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of the copyright holders nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | #include "ntdll.h" 29 | 30 | MODULE(thread) 31 | 32 | #ifdef THREADED 33 | __thread 34 | #else 35 | #include 36 | static jmp_buf env; 37 | static 38 | #endif 39 | HANDLE CurrentThreadHandle = NULL; 40 | 41 | static void *thread_start(void *arg) 42 | { 43 | ThreadProc thread; 44 | CurrentThreadHandle = arg; 45 | thread = CurrentThreadHandle->thread.StartAddress; 46 | return (void *) (CurrentThreadHandle->thread.ExitStatus = thread(CurrentThreadHandle->thread.StartParameter)); 47 | } 48 | 49 | static HANDLE GetThreadHandle(HANDLE ThreadHandle) 50 | { 51 | if (ThreadHandle == NtCurrentThread()) 52 | return CurrentThreadHandle; 53 | else 54 | return ThreadHandle; 55 | } 56 | 57 | NTSTATUS NTAPI NtTerminateThread(HANDLE ThreadHandle, NTSTATUS ExitStatus) 58 | { 59 | ThreadHandle = GetThreadHandle(ThreadHandle); 60 | 61 | if (ExitStatus != STATUS_SUCCESS) 62 | { 63 | fprintf(stderr, "ntdll.NtTerminateThread(\"%s\", 0x%08x)\n", strhandle(ThreadHandle), ExitStatus); 64 | abort(); 65 | } 66 | 67 | Log("ntdll.NtTerminateThread(\"%s\", 0x%08x)\n", strhandle(ThreadHandle), ExitStatus); 68 | 69 | ThreadHandle->thread.ExitStatus = ExitStatus; 70 | #ifdef THREADED 71 | pthread_exit(&ThreadHandle->thread.ExitStatus); 72 | #else 73 | longjmp(env, 1); 74 | #endif 75 | 76 | return STATUS_SUCCESS; 77 | } 78 | FORWARD_FUNCTION(NtTerminateThread, ZwTerminateThread); 79 | 80 | NTSTATUS NTAPI RtlCreateUserThread(HANDLE ProcessHandle, /*PSECURITY_DESCRIPTOR*/ PVOID SecurityDescriptor, 81 | BOOLEAN CreateSuspended, ULONG StackZeroBits, PULONG StackReserved, PULONG StackCommit, 82 | PVOID StartAddress, PVOID StartParameter, PHANDLE ThreadHandle, PCLIENT_ID ClientID) 83 | { 84 | char desc[1024]; 85 | HANDLE th; 86 | 87 | CHECK_POINTER(ProcessHandle); 88 | CHECK_POINTER(StartAddress); 89 | CHECK_POINTER(ThreadHandle); 90 | 91 | snprintf(desc, sizeof(desc), "ThreadProc @%p - param @%p", StartAddress, StartParameter); 92 | 93 | __CreateHandle(th, HANDLE_TH, desc); 94 | th->thread.StartAddress = StartAddress; 95 | th->thread.StartParameter = StartParameter; 96 | th->thread.ExitStatus = -1; 97 | 98 | #ifdef THREADED 99 | if (pthread_create(&th->thread.tid, NULL, thread_start, (void *) th)) 100 | { 101 | RtlFreeHeap(GetProcessHeap(), 0, th); 102 | return STATUS_UNSUCCESSFUL; 103 | } 104 | #else 105 | if (!setjmp(env)) 106 | thread_start(th); 107 | #endif 108 | *ThreadHandle = th; 109 | 110 | return STATUS_SUCCESS; 111 | } 112 | 113 | NTSTATUS NTAPI NtQueryInformationThread(HANDLE ThreadHandle, THREADINFOCLASS ThreadInformationClass, 114 | PVOID ThreadInformation, ULONG ThreadInformationLength, PULONG ReturnLength) 115 | { 116 | NTSTATUS result = STATUS_BUFFER_TOO_SMALL; 117 | 118 | ThreadHandle = GetThreadHandle(ThreadHandle); 119 | 120 | Log("ntdll.NtQueryInformationThread(%s:0x%08x)", strthinfo(ThreadInformationClass), ThreadInformationClass); 121 | 122 | switch (ThreadInformationClass) 123 | { 124 | case ThreadBasicInformation: 125 | { 126 | THREAD_BASIC_INFORMATION *thinfo = ThreadInformation; 127 | 128 | if (ReturnLength) 129 | *ReturnLength = sizeof(THREAD_BASIC_INFORMATION); 130 | 131 | thinfo->ExitStatus = ThreadHandle ? ThreadHandle->thread.ExitStatus : 0; 132 | thinfo->TebBaseAddress = NtCurrentTeb(); 133 | result = STATUS_SUCCESS; 134 | break; 135 | } 136 | default: 137 | Log(" !!UNIMPLEMENTED!!"); 138 | result = STATUS_NOT_IMPLEMENTED; 139 | } 140 | 141 | Log("\n"); 142 | 143 | return result; 144 | } 145 | 146 | NTSTATUS NTAPI NtSetThreadExecutionState(EXECUTION_STATE esFlags, EXECUTION_STATE *PreviousFlags) 147 | { 148 | CHECK_POINTER(PreviousFlags); 149 | 150 | Log("ntdll.NtSetThreadExecutionState(0x%08x)\n", esFlags); 151 | 152 | *PreviousFlags = esFlags; 153 | return STATUS_SUCCESS; 154 | } 155 | 156 | /* critical sections */ 157 | 158 | NTSTATUS NTAPI RtlInitializeCriticalSection(PRTL_CRITICAL_SECTION CriticalSection) 159 | { 160 | CHECK_POINTER(CriticalSection); 161 | Log("ntdll.RtlInitializeCriticalSection(%p)\n", CriticalSection); 162 | memset(CriticalSection, 0, sizeof(RTL_CRITICAL_SECTION)); 163 | #ifdef THREADED 164 | CriticalSection->LockSemaphore = RtlAllocateHeap(GetProcessHeap(), 0, sizeof(pthread_mutex_t)); 165 | pthread_mutex_init((pthread_mutex_t *) CriticalSection->LockSemaphore, NULL); 166 | #endif 167 | return STATUS_SUCCESS; 168 | } 169 | 170 | NTSTATUS NTAPI RtlDeleteCriticalSection(PRTL_CRITICAL_SECTION CriticalSection) 171 | { 172 | CHECK_POINTER(CriticalSection); 173 | Log("ntdll.RtlDeleteCriticalSection(%p)\n", CriticalSection); 174 | #ifdef THREADED 175 | pthread_mutex_destroy((pthread_mutex_t *) CriticalSection->LockSemaphore); 176 | RtlFreeHeap(GetProcessHeap(), 0, CriticalSection->LockSemaphore); 177 | #endif 178 | return STATUS_SUCCESS; 179 | } 180 | 181 | NTSTATUS NTAPI RtlEnterCriticalSection(PRTL_CRITICAL_SECTION CriticalSection) 182 | { 183 | Log("ntdll.RtlEnterCriticalSection(%p)\n", CriticalSection); 184 | #ifdef THREADED 185 | pthread_mutex_lock((pthread_mutex_t *) CriticalSection->LockSemaphore); 186 | #endif 187 | return STATUS_SUCCESS; 188 | } 189 | 190 | NTSTATUS NTAPI RtlLeaveCriticalSection(PRTL_CRITICAL_SECTION CriticalSection) 191 | { 192 | Log("ntdll.RtlLeaveCriticalSection(%p)\n", CriticalSection); 193 | #ifdef THREADED 194 | pthread_mutex_unlock((pthread_mutex_t *) CriticalSection->LockSemaphore); 195 | #endif 196 | return STATUS_SUCCESS; 197 | } 198 | -------------------------------------------------------------------------------- /libs/ntdll/time.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010, Sherpya , aCaB 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of the copyright holders nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | #include "ntdll.h" 29 | #include 30 | 31 | #ifndef _WIN32 32 | #include 33 | #endif 34 | 35 | MODULE(time) 36 | 37 | NTSTATUS NTAPI NtQuerySystemTime(PLARGE_INTEGER Time) 38 | { 39 | struct timeval now; 40 | CHECK_POINTER(Time); 41 | 42 | gettimeofday(&now, NULL); 43 | Time->QuadPart = now.tv_sec * TICKSPERSEC + TICKS_1601_TO_1970; 44 | Time->QuadPart += now.tv_usec * 10; 45 | return STATUS_SUCCESS; 46 | } 47 | 48 | NTSTATUS NTAPI RtlSystemTimeToLocalTime(IN PLARGE_INTEGER SystemTime, OUT PLARGE_INTEGER LocalTime) 49 | { 50 | int64_t bias; 51 | time_t now = time(NULL); 52 | 53 | #ifdef _WIN32 54 | struct tm *tnow = gmtime(&now); 55 | time_t lnow = mktime(tnow); 56 | bias = now - lnow; 57 | #else 58 | struct tm *t = localtime(&now); 59 | bias = t ? t->tm_gmtoff : 0; 60 | #endif 61 | 62 | bias *= TICKSPERSEC; 63 | bias = -bias; 64 | 65 | LocalTime->QuadPart = SystemTime->QuadPart - bias; 66 | return STATUS_SUCCESS; 67 | } 68 | 69 | NTSTATUS NTAPI NtQueryPerformanceCounter(PLARGE_INTEGER PerformanceCounter, PLARGE_INTEGER PerformanceFrequency) 70 | { 71 | LARGE_INTEGER now; 72 | uint64_t server_start_time = 0; 73 | 74 | CHECK_POINTER(PerformanceCounter); 75 | 76 | if (!PerformanceCounter) 77 | return STATUS_ACCESS_VIOLATION; 78 | 79 | server_start_time = uptime() * TICKSPERSEC + TICKS_1601_TO_1970; 80 | 81 | /* convert a counter that increments at a rate of 10 MHz 82 | * to one of 1.193182 MHz, with some care for arithmetic 83 | * overflow and good accuracy (21/176 = 0.11931818) */ 84 | 85 | NtQuerySystemTime(&now); 86 | PerformanceCounter->QuadPart = ((now.QuadPart - server_start_time) * 21) / 176; 87 | 88 | if (PerformanceFrequency) 89 | PerformanceFrequency->QuadPart = 1193182; 90 | 91 | return STATUS_SUCCESS; 92 | } 93 | -------------------------------------------------------------------------------- /libs/ntdll/timefields.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Nt time functions. 3 | * 4 | * RtlTimeToTimeFields, RtlTimeFieldsToTime and defines are taken from ReactOS and 5 | * adapted to wine with special permissions of the author. This code is 6 | * Copyright 2002 Rex Jolliff (rex@lvcablemodem.com) 7 | * 8 | * Copyright 1999 Juergen Schmied 9 | * Copyright 2007 Dmitry Timoshkov 10 | * 11 | * This library is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 2.1 of the License, or (at your option) any later version. 15 | * 16 | * This library is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public 22 | * License along with this library; if not, write to the Free Software 23 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA 24 | */ 25 | 26 | #include "ntdll.h" 27 | 28 | typedef short CSHORT; 29 | 30 | typedef struct _TIME_FIELDS 31 | { 32 | CSHORT Year; 33 | CSHORT Month; 34 | CSHORT Day; 35 | CSHORT Hour; 36 | CSHORT Minute; 37 | CSHORT Second; 38 | CSHORT Milliseconds; 39 | CSHORT Weekday; 40 | } TIME_FIELDS, *PTIME_FIELDS; 41 | 42 | #define TICKSPERMSEC 10000 43 | #define SECSPERDAY 86400 44 | #define SECSPERHOUR 3600 45 | #define SECSPERMIN 60 46 | #define EPOCHWEEKDAY 1 /* Jan 1, 1601 was Monday */ 47 | #define DAYSPERWEEK 7 48 | #define DAYSPERQUADRICENTENNIUM (365 * 400 + 97) 49 | #define DAYSPERNORMALQUADRENNIUM (365 * 4 + 1) 50 | 51 | VOID NTAPI RtlTimeToTimeFields(const LARGE_INTEGER *liTime, PTIME_FIELDS TimeFields) 52 | { 53 | int SecondsInDay; 54 | LONG cleaps, years, yearday, months; 55 | LONG Days; 56 | LONGLONG Time; 57 | 58 | /* Extract millisecond from time and convert time into seconds */ 59 | TimeFields->Milliseconds = (CSHORT) (( liTime->QuadPart % TICKSPERSEC) / TICKSPERMSEC); 60 | Time = liTime->QuadPart / TICKSPERSEC; 61 | 62 | /* The native version of RtlTimeToTimeFields does not take leap seconds 63 | * into account */ 64 | 65 | /* Split the time into days and seconds within the day */ 66 | Days = (LONG) (Time / SECSPERDAY); 67 | SecondsInDay = Time % SECSPERDAY; 68 | 69 | /* compute time of day */ 70 | TimeFields->Hour = (CSHORT) (SecondsInDay / SECSPERHOUR); 71 | SecondsInDay = SecondsInDay % SECSPERHOUR; 72 | TimeFields->Minute = (CSHORT) (SecondsInDay / SECSPERMIN); 73 | TimeFields->Second = (CSHORT) (SecondsInDay % SECSPERMIN); 74 | 75 | /* compute day of week */ 76 | TimeFields->Weekday = (CSHORT) ((EPOCHWEEKDAY + Days) % DAYSPERWEEK); 77 | 78 | /* compute year, month and day of month. */ 79 | cleaps = (3 * ((4 * Days + 1227) / DAYSPERQUADRICENTENNIUM) + 3 ) / 4; 80 | Days += 28188 + cleaps; 81 | years = (20 * Days - 2442) / (5 * DAYSPERNORMALQUADRENNIUM); 82 | yearday = Days - (years * DAYSPERNORMALQUADRENNIUM)/4; 83 | months = (64 * yearday) / 1959; 84 | 85 | /* the result is based on a year starting on March. 86 | * To convert take 12 from Januari and Februari and 87 | * increase the year by one. */ 88 | if (months < 14) 89 | { 90 | TimeFields->Month = months - 1; 91 | TimeFields->Year = years + 1524; 92 | } 93 | else 94 | { 95 | TimeFields->Month = months - 13; 96 | TimeFields->Year = years + 1525; 97 | } 98 | 99 | /* calculation of day of month is based on the wonderful 100 | * sequence of INT( n * 30.6): it reproduces the 101 | * 31-30-31-30-31-31 month lengths exactly for small n's */ 102 | TimeFields->Day = yearday - (1959 * months) / 64 ; 103 | return; 104 | } 105 | -------------------------------------------------------------------------------- /libs/ntdll/version.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010, Sherpya , aCaB 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of the copyright holders nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | #include "ntdll.h" 29 | 30 | typedef struct _OSVERSIONINFOW { 31 | ULONG dwOSVersionInfoSize; 32 | ULONG dwMajorVersion; 33 | ULONG dwMinorVersion; 34 | ULONG dwBuildNumber; 35 | ULONG dwPlatformId; 36 | WCHAR szCSDVersion[128]; 37 | } OSVERSIONINFOW, *POSVERSIONINFOW, *LPOSVERSIONINFOW, RTL_OSVERSIONINFOW, *PRTL_OSVERSIONINFOW; 38 | 39 | typedef struct _OSVERSIONINFOEXW { 40 | ULONG dwOSVersionInfoSize; 41 | ULONG dwMajorVersion; 42 | ULONG dwMinorVersion; 43 | ULONG dwBuildNumber; 44 | ULONG dwPlatformId; 45 | WCHAR szCSDVersion[128]; 46 | USHORT wServicePackMajor; 47 | USHORT wServicePackMinor; 48 | USHORT wSuiteMask; 49 | UCHAR wProductType; 50 | UCHAR wReserved; 51 | } OSVERSIONINFOEXW, *POSVERSIONINFOEXW, *LPOSVERSIONINFOEXW, RTL_OSVERSIONINFOEXW, *PRTL_OSVERSIONINFOEXW; 52 | 53 | NTSTATUS NTAPI RtlGetVersion(PRTL_OSVERSIONINFOW lpVInfo) 54 | { 55 | PPEB Peb = NtCurrentPeb(); 56 | 57 | switch (lpVInfo->dwOSVersionInfoSize) 58 | { 59 | case sizeof(RTL_OSVERSIONINFOEXW): 60 | { 61 | RTL_OSVERSIONINFOEXW *InfoEx = (RTL_OSVERSIONINFOEXW *) lpVInfo; 62 | InfoEx->wServicePackMajor = Peb->OSPlatformId; 63 | InfoEx->wServicePackMinor = 0; 64 | InfoEx->wSuiteMask = 2; /* VER_SUITE_ENTERPRISE */ 65 | InfoEx->wProductType = NtProductServer; 66 | } 67 | case sizeof(RTL_OSVERSIONINFOW): 68 | { 69 | lpVInfo->dwMajorVersion = Peb->OSMajorVersion; 70 | lpVInfo->dwMinorVersion = Peb->OSMinorVersion; 71 | lpVInfo->dwBuildNumber = Peb->OSBuildNumber; 72 | lpVInfo->dwPlatformId = Peb->OSPlatformId; 73 | memcpy(lpVInfo->szCSDVersion, version_sp2, sizeof(version_sp2)); 74 | break; 75 | } 76 | default: 77 | return STATUS_INVALID_PARAMETER; 78 | } 79 | return STATUS_SUCCESS; 80 | } 81 | 82 | ULONGLONG NTAPI VerSetConditionMask(ULONGLONG dwlConditionMask, DWORD dwTypeBitMask, BYTE dwConditionMask) 83 | { 84 | return 0; 85 | } 86 | 87 | /* call from autochk.exe -> TypeMask = VER_PRODUCT_TYPE -> wProductType = VER_NT_SERVER */ 88 | NTSTATUS NTAPI RtlVerifyVersionInfo(PRTL_OSVERSIONINFOEXW VersionInfo, ULONG TypeMask, 89 | ULONGLONG ConditionMask) 90 | { 91 | return STATUS_SUCCESS; 92 | } 93 | -------------------------------------------------------------------------------- /msvc/.gitignore: -------------------------------------------------------------------------------- 1 | Debug 2 | Release 3 | ipch 4 | *.ncb 5 | *.suo 6 | *.sdf 7 | *.opensdf 8 | *.user 9 | -------------------------------------------------------------------------------- /msvc/libs/ntdll/ntdll.def: -------------------------------------------------------------------------------- 1 | LIBRARY ntdll 2 | EXPORTS 3 | 4 | ; _chkstk.asm 5 | _chkstk 6 | 7 | ; _aulldvrm.asm 8 | _aulldvrm 9 | 10 | ; bitmap.c 11 | RtlAreBitsClear 12 | RtlAreBitsSet 13 | RtlClearAllBits 14 | RtlClearBits 15 | RtlFindClearBits 16 | RtlFindClearBitsAndSet 17 | RtlFindClearRuns 18 | RtlFindLeastSignificantBit 19 | RtlFindLongestRunClear 20 | RtlFindMostSignificantBit 21 | RtlFindNextForwardRunClear 22 | RtlFindSetBits 23 | RtlFindSetBitsAndClear 24 | RtlInitializeBitMap 25 | RtlNumberOfClearBits 26 | RtlNumberOfSetBits 27 | RtlSetAllBits 28 | RtlSetBits 29 | RtlTestBit 30 | 31 | ; crc32.c 32 | RtlComputeCrc32 33 | 34 | ; crt.c 35 | rpl__stricmp=stricmp 36 | rpl__strnicmp=strnicmp 37 | rpl__wcsicmp 38 | rpl__wcslwr 39 | rpl__wcsnicmp 40 | rpl__wcsupr 41 | rpl__wtoi64 42 | rpl__wtol 43 | rpl_atoi=atoi 44 | rpl_atol=atol 45 | rpl_atoll=_atoi64 46 | rpl_isprint 47 | rpl_isspace 48 | rpl_memcmp=memcmp 49 | rpl_memcpy=memcpy 50 | rpl_memmove=memmove 51 | rpl_memset=memset 52 | rpl_strncpy=strncpy 53 | rpl_strpbrk=strpbrk 54 | rpl_strspn=strspn 55 | rpl_qsort=qsort 56 | rpl_wcscat 57 | rpl_wcschr 58 | rpl_wcscmp 59 | rpl_wcscpy 60 | rpl_wcslen 61 | rpl_wcsncmp 62 | rpl_wcsrchr 63 | rpl_wcsspn 64 | rpl_wcsstr=wcsstr ; no memmem on win32 65 | rpl_wcstoul 66 | rpl__wcstoui64 67 | 68 | ; format.c 69 | DbgPrint 70 | DbgPrintEx 71 | rpl__snwprintf=rpl_swprintf_s 72 | rpl__vsnprintf=vsnprintf 73 | rpl__vsnwprintf 74 | rpl_sprintf=sprintf 75 | rpl_swprintf 76 | rpl_swprintf_s 77 | 78 | ; heap.c 79 | RtlAllocateHeap 80 | RtlCreateHeap 81 | RtlDestroyHeap 82 | RtlFreeHeap 83 | RtlReAllocateHeap 84 | RtlSizeHeap 85 | 86 | ; large_int.c 87 | RtlExtendedIntegerMultiply 88 | RtlLargeIntegerDivide 89 | _alldiv=__alldiv@16 90 | _allmul=__allmul@16 91 | _allrem=__allrem@16 92 | _allshl=__allshl@12 93 | _aulldiv=__aulldiv@16 94 | _aullrem=__aullrem@16 95 | _aullshr=__aullshr@12 96 | 97 | ;lznt1.c 98 | RtlDecompressBuffer 99 | 100 | ; ntdll.c 101 | LdrGetDllHandle 102 | LdrSetMUICacheType 103 | NtAdjustPrivilegesToken 104 | NtSetDefaultLocale 105 | NtAllocateVirtualMemory 106 | NtClearEvent 107 | NtClose 108 | NtCreateEvent 109 | NtCreateFile 110 | NtDelayExecution 111 | NtDeleteFile 112 | NtDeviceIoControlFile 113 | NtDisplayString 114 | NtFlushBuffersFile 115 | NtFreeVirtualMemory 116 | NtFsControlFile 117 | NtLoadDriver 118 | NtOpenDirectoryObject 119 | NtOpenEvent 120 | NtOpenFile 121 | NtOpenProcessToken 122 | NtOpenSection 123 | NtOpenSymbolicLinkObject 124 | NtQueryDirectoryObject 125 | NtQueryInformationFile 126 | NtQueryPerformanceCounter 127 | NtQuerySymbolicLinkObject 128 | NtQuerySystemInformation 129 | NtQuerySystemTime 130 | NtQueryVolumeInformationFile 131 | NtReadFile 132 | NtResetEvent 133 | NtSerializeBoot 134 | NtSetEvent 135 | NtSetInformationFile 136 | NtSetInformationProcess 137 | NtShutdownSystem 138 | ZwShutdownSystem=NtShutdownSystem@4 139 | NtTerminateProcess 140 | NtWaitForMultipleObjects 141 | NtWaitForSingleObject 142 | NtWriteFile 143 | RtlSystemTimeToLocalTime 144 | 145 | ; random.c 146 | RtlRandom 147 | RtlRandomEx 148 | RtlUniform 149 | 150 | ; registry.c 151 | NtOpenKey 152 | NtQueryValueKey 153 | NtSetValueKey 154 | RtlQueryRegistryValues 155 | RtlWriteRegistryValue 156 | 157 | ; rtl.c 158 | RtlAdjustPrivilege 159 | RtlAnsiCharToUnicodeChar 160 | RtlAnsiStringToUnicodeString 161 | RtlDosPathNameToNtPathName_U 162 | RtlEqualUnicodeString 163 | RtlExpandEnvironmentStrings_U 164 | RtlFillMemoryUlong 165 | RtlGetCurrentDirectory_U 166 | RtlFindMessage 167 | RtlFormatMessage 168 | RtlFreeAnsiString 169 | RtlFreeUnicodeString 170 | RtlCreateUnicodeStringFromAsciiz 171 | RtlInitAnsiString 172 | RtlInitAnsiStringEx 173 | RtlInitUnicodeString 174 | RtlMultiByteToUnicodeN 175 | RtlNormalizeProcessParams 176 | RtlOemToUnicodeN=_RtlMultiByteToUnicodeN@20 177 | RtlPrefixUnicodeString 178 | RtlSecondsSince1970ToTime 179 | RtlUnicodeStringToAnsiString 180 | RtlUnicodeToMultiByteN=_RtlUnicodeToOemN@20 181 | RtlUnicodeToOemN=_RtlUnicodeToOemN@20 182 | RtlUpcaseUnicodeString 183 | 184 | ; sd.c 185 | RtlAddAccessAllowedAce 186 | RtlAddAce 187 | RtlCopySid 188 | RtlCreateAcl 189 | RtlCreateSecurityDescriptor 190 | RtlFirstFreeAce 191 | RtlInitializeSid 192 | RtlLengthRequiredSid 193 | RtlLengthSecurityDescriptor 194 | RtlLengthSid 195 | RtlQueryInformationAcl 196 | RtlSetDaclSecurityDescriptor 197 | RtlSetGroupSecurityDescriptor 198 | RtlSubAuthoritySid 199 | RtlValidAcl 200 | RtlValidRelativeSecurityDescriptor 201 | RtlValidSecurityDescriptor 202 | RtlValidSid 203 | 204 | ; splay.c 205 | RtlDeleteElementGenericTable 206 | RtlEnumerateGenericTableWithoutSplaying 207 | RtlInitializeGenericTable 208 | RtlInsertElementGenericTable 209 | RtlLookupElementGenericTable 210 | 211 | ; thread.c 212 | NtQueryInformationThread 213 | NtSetThreadExecutionState 214 | NtTerminateThread 215 | RtlCreateUserThread 216 | RtlDeleteCriticalSection 217 | RtlEnterCriticalSection 218 | RtlInitializeCriticalSection 219 | RtlLeaveCriticalSection 220 | ZwTerminateThread=_NtTerminateThread@8 221 | 222 | ; time.c 223 | RtlTimeToTimeFields 224 | 225 | ; version.c 226 | RtlVerifyVersionInfo 227 | VerSetConditionMask 228 | -------------------------------------------------------------------------------- /msvc/libs/ntdll/ntdll.vcxproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | 14 | 15 | Document 16 | ../../.. 17 | ../../.. 18 | 19 | 20 | Document 21 | ../../.. 22 | ../../.. 23 | 24 | 25 | Document 26 | ../../.. 27 | ../../.. 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | {00D8D5EE-42FD-4DF5-8EAD-F54982EF4AAC} 68 | Win32Proj 69 | ntdll 70 | 71 | 72 | 73 | DynamicLibrary 74 | true 75 | NotSet 76 | 77 | 78 | DynamicLibrary 79 | false 80 | true 81 | NotSet 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | true 96 | 97 | 98 | false 99 | 100 | 101 | 102 | NotUsing 103 | Level3 104 | Disabled 105 | WIN32;_DEBUG;_WINDOWS;_USRDLL;NTDLL_EXPORTS;%(PreprocessorDefinitions) 106 | ../../.. 107 | 4996 108 | 109 | 110 | Windows 111 | true 112 | ntdll.def 113 | 114 | 115 | 116 | 117 | Level3 118 | NotUsing 119 | MaxSpeed 120 | true 121 | true 122 | WIN32;NDEBUG;_WINDOWS;_USRDLL;NTDLL_EXPORTS;%(PreprocessorDefinitions) 123 | ../../.. 124 | 4996 125 | 126 | 127 | Windows 128 | true 129 | true 130 | true 131 | ntdll.def 132 | 133 | 134 | 135 | 136 | 137 | 138 | -------------------------------------------------------------------------------- /msvc/libs/ntdll/ntdll.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | Source Files 23 | 24 | 25 | Source Files 26 | 27 | 28 | Source Files 29 | 30 | 31 | Source Files 32 | 33 | 34 | Source Files 35 | 36 | 37 | Source Files 38 | 39 | 40 | Source Files 41 | 42 | 43 | Source Files 44 | 45 | 46 | Source Files 47 | 48 | 49 | Source Files 50 | 51 | 52 | Source Files 53 | 54 | 55 | Source Files 56 | 57 | 58 | Source Files 59 | 60 | 61 | Source Files 62 | 63 | 64 | Source Files 65 | 66 | 67 | Source Files 68 | 69 | 70 | Source Files 71 | 72 | 73 | Source Files 74 | 75 | 76 | Source Files 77 | 78 | 79 | Source Files 80 | 81 | 82 | Source Files 83 | 84 | 85 | Source Files 86 | 87 | 88 | 89 | 90 | Header Files 91 | 92 | 93 | Header Files 94 | 95 | 96 | Header Files 97 | 98 | 99 | Header Files 100 | 101 | 102 | Header Files 103 | 104 | 105 | Header Files 106 | 107 | 108 | 109 | 110 | Source Files 111 | 112 | 113 | Source Files 114 | 115 | 116 | Source Files 117 | 118 | 119 | 120 | 121 | Resource Files 122 | 123 | 124 | -------------------------------------------------------------------------------- /msvc/nloader.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 11.00 3 | # Visual Studio 2010 4 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "nloader", "nloader.vcxproj", "{683BD034-8B0C-40CF-9F93-22DB92CA3A89}" 5 | EndProject 6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ntdll", "libs\ntdll\ntdll.vcxproj", "{00D8D5EE-42FD-4DF5-8EAD-F54982EF4AAC}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|Win32 = Debug|Win32 11 | Release|Win32 = Release|Win32 12 | EndGlobalSection 13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 14 | {683BD034-8B0C-40CF-9F93-22DB92CA3A89}.Debug|Win32.ActiveCfg = Debug|Win32 15 | {683BD034-8B0C-40CF-9F93-22DB92CA3A89}.Debug|Win32.Build.0 = Debug|Win32 16 | {683BD034-8B0C-40CF-9F93-22DB92CA3A89}.Release|Win32.ActiveCfg = Release|Win32 17 | {683BD034-8B0C-40CF-9F93-22DB92CA3A89}.Release|Win32.Build.0 = Release|Win32 18 | {00D8D5EE-42FD-4DF5-8EAD-F54982EF4AAC}.Debug|Win32.ActiveCfg = Debug|Win32 19 | {00D8D5EE-42FD-4DF5-8EAD-F54982EF4AAC}.Debug|Win32.Build.0 = Debug|Win32 20 | {00D8D5EE-42FD-4DF5-8EAD-F54982EF4AAC}.Release|Win32.ActiveCfg = Release|Win32 21 | {00D8D5EE-42FD-4DF5-8EAD-F54982EF4AAC}.Release|Win32.Build.0 = Release|Win32 22 | EndGlobalSection 23 | GlobalSection(SolutionProperties) = preSolution 24 | HideSolutionNode = FALSE 25 | EndGlobalSection 26 | EndGlobal 27 | -------------------------------------------------------------------------------- /msvc/nloader.vcxproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | 14 | {683BD034-8B0C-40CF-9F93-22DB92CA3A89} 15 | Win32Proj 16 | nloader 17 | 18 | 19 | 20 | Application 21 | true 22 | NotSet 23 | 24 | 25 | Application 26 | false 27 | true 28 | NotSet 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | true 43 | 44 | 45 | false 46 | 47 | 48 | 49 | 50 | 51 | Level3 52 | Disabled 53 | WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) 54 | 4996 55 | ../../.. 56 | 57 | 58 | Console 59 | true 60 | 61 | 62 | 63 | 64 | Level3 65 | 66 | 67 | MaxSpeed 68 | true 69 | true 70 | WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 71 | 4996 72 | ../../.. 73 | 74 | 75 | Console 76 | true 77 | true 78 | true 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | Document 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | -------------------------------------------------------------------------------- /msvc/nloader.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | Source Files 23 | 24 | 25 | 26 | 27 | Source Files 28 | 29 | 30 | 31 | 32 | Header Files 33 | 34 | 35 | Header Files 36 | 37 | 38 | -------------------------------------------------------------------------------- /nloader.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010, Sherpya , aCaB 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of the copyright holders nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | #define _GNU_SOURCE 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | #include 35 | #include 36 | #include 37 | 38 | #include "nt_structs.h" 39 | 40 | MODULE(main) 41 | 42 | static jmp_buf sigsegv_env; 43 | static void sigsegv_handler(int signum) 44 | { 45 | longjmp(sigsegv_env, 1); 46 | } 47 | 48 | 49 | int main (int argc, char **argv) { 50 | 51 | int fd; 52 | unsigned int i; 53 | WCHAR commandline[1024] = L"autochk.exe *"; 54 | 55 | uint8_t *ptr, *exe; 56 | void *ep; 57 | struct stat st; 58 | RTL_USER_PROCESS_PARAMETERS *params; 59 | 60 | const char *executable = (argc > 1) ? argv[1] : "autochk.exe"; 61 | 62 | fd = open(executable, O_RDONLY | O_BINARY); 63 | if(fd<0) { 64 | perror("open"); 65 | return 1; 66 | } 67 | 68 | if(fstat(fd, &st)<0) { 69 | perror("fstat"); 70 | return 1; 71 | } 72 | 73 | exe = malloc(st.st_size); 74 | if(read(fd, exe, st.st_size) != st.st_size) { 75 | perror("read"); 76 | close(fd); 77 | free(exe); 78 | return 1; 79 | } 80 | 81 | close(fd); 82 | ep = setup_nloader(exe, st.st_size, ¶ms, 0); 83 | free(exe); 84 | 85 | if (!ep) { 86 | fprintf(stderr, "setup_nloader failed\n"); 87 | return 1; 88 | } 89 | 90 | ptr = (uint8_t *)(params+1); 91 | params->ImagePathName.Buffer = (WCHAR *) ptr; 92 | 93 | for (i = 0; i < strlen(executable); i++) 94 | params->ImagePathName.Buffer[i] = executable[i]; 95 | 96 | params->ImagePathName.Length = i; 97 | params->ImagePathName.MaximumLength = i + 1; 98 | 99 | // autochk.exe cmdline arguments (guess) 100 | // refs: 101 | // - http://www.infocellar.com/winxp/chkdsk-and-autochk.htm 102 | // - http://windows-xp-dox.net/MS.Press-Microsoft.Windows.XP1/prkd_tro_rgwn.htm 103 | // - http://support.microsoft.com/kb/218461 104 | // - http://support.microsoft.com/kb/160963/EN-US/ 105 | 106 | // autochk.exe [switches] volume | * 107 | // * = all volumes, it queries global directory 108 | // 109 | // -t - unknown (w7) 110 | // -s - silent execution (w7) 111 | // -p - force check even if dirty bit is not set 112 | // -r - locate and recover bad sectors, implies -p (untested) 113 | // -b - re-evaluates bad clusters on the volume, implies -r (w7) 114 | // -x volume - force dismount (untested), without arguments crashes 115 | // -lXX - with some values like 10 prints info and log size of the volume (the value is mul by 1024) 116 | // -l:XX - tries to set log size, always fails for me saying the size is too small or unable to adjust 117 | // -k:volume - excludes volume from the check (does make sense when using *) 118 | // -m - run chkdsk only if dirty bit is set (default ?) 119 | // -i - Performs a less vigorous check of index entries 120 | // -i[:XX] - XX = 0-50 - unknown 121 | // -c - Skips the checking of cycles within the folder structure 122 | 123 | if (argc > 1) 124 | { 125 | int c = params->ImagePathName.Length; 126 | memcpy(commandline, params->ImagePathName.Buffer, params->ImagePathName.Length * sizeof(WCHAR)); 127 | 128 | for (i = 2; i < (unsigned) argc; i++) 129 | { 130 | char *arg = argv[i]; 131 | int dev = 0; 132 | commandline[c++] = L' '; 133 | if (!strncmp(arg, "/dev/", 4)) 134 | { 135 | size_t len = sizeof(VOLUME_PREFIX) - sizeof(WCHAR); 136 | dev = 1; 137 | memcpy(&commandline[c], VOLUME_PREFIX, len); 138 | len /= sizeof(WCHAR); 139 | c += len; 140 | } 141 | 142 | while (*arg && (c < sizeof(commandline) - 1)) 143 | commandline[c++] = *arg++; 144 | 145 | if (dev) commandline[c++] = L'}'; 146 | } 147 | commandline[c++] = 0; 148 | } 149 | 150 | ptr += sizeof(WCHAR); 151 | memcpy(ptr, commandline, sizeof(commandline)); 152 | params->CommandLine.Length = sizeof(commandline) - sizeof(WCHAR); 153 | params->CommandLine.MaximumLength = sizeof(commandline); 154 | params->CommandLine.Buffer = (WCHAR *) ptr; 155 | 156 | /******************************************************************************************************* CALL ENTRYPOINT */ 157 | signal(SIGSEGV, sigsegv_handler); 158 | 159 | if (setjmp(sigsegv_env)) 160 | { 161 | /* clean up console */ 162 | fflush(stdout); 163 | fprintf(stderr, "Native program crashed\n"); 164 | exit(1); 165 | } 166 | else 167 | { 168 | BPX(); 169 | return to_ep(ep); 170 | } 171 | } 172 | -------------------------------------------------------------------------------- /nt_compat.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010, Sherpya , aCaB 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of the copyright holders nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | #ifndef __NT_COMPAT_H 29 | #define __NT_COMPAT_H 30 | 31 | #ifdef WIN32 32 | #include 33 | #include 34 | #include 35 | 36 | #define __func__ __FUNCTION__ 37 | 38 | #ifndef S_ISDIR 39 | #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR) 40 | #endif 41 | 42 | #ifndef S_IWUSR 43 | #define S_IWUSR S_IWRITE 44 | #endif 45 | 46 | typedef struct _SYSTEM_INFO 47 | { 48 | DWORD d1; 49 | DWORD dwPageSize; 50 | BYTE padding[(sizeof(PVOID) * 2) + (sizeof(DWORD) * 4) + (sizeof(WORD) * 2)]; 51 | } SYSTEM_INFO, *LPSYSTEM_INFO; 52 | 53 | DECLSPEC_IMPORT LPVOID WINAPI LoadLibraryA(LPCSTR); 54 | DECLSPEC_IMPORT HANDLE WINAPI GetModuleHandleA(LPCSTR); 55 | DECLSPEC_IMPORT FARPROC WINAPI GetProcAddress(LPVOID, LPCSTR); 56 | DECLSPEC_IMPORT DWORD WINAPI GetLastError(VOID); 57 | DECLSPEC_IMPORT VOID WINAPI SetLastError(DWORD); 58 | DECLSPEC_IMPORT PVOID WINAPI VirtualAlloc(PVOID, DWORD, DWORD, DWORD); 59 | DECLSPEC_IMPORT int WINAPI VirtualFree(PVOID, DWORD, DWORD); 60 | DECLSPEC_IMPORT int WINAPI VirtualProtect(PVOID, DWORD, DWORD, PDWORD); 61 | DECLSPEC_IMPORT VOID WINAPI GetSystemInfo(LPSYSTEM_INFO); 62 | DECLSPEC_IMPORT DWORD WINAPI GetTickCount(VOID); 63 | 64 | DECLSPEC_IMPORT HANDLE WINAPI CreateFileA(LPCSTR,DWORD,DWORD,LPVOID,DWORD,DWORD,HANDLE); 65 | DECLSPEC_IMPORT HANDLE WINAPI CreateFileW(LPWSTR,DWORD,DWORD,LPVOID,DWORD,DWORD,HANDLE); 66 | DECLSPEC_IMPORT BOOL WINAPI GetFileSizeEx(HANDLE,PLARGE_INTEGER); 67 | DECLSPEC_IMPORT DWORD WINAPI GetLastError(VOID); 68 | DECLSPEC_IMPORT BOOL WINAPI SetFilePointerEx(HANDLE,LARGE_INTEGER,PLARGE_INTEGER,DWORD); 69 | DECLSPEC_IMPORT BOOL WINAPI ReadFile(HANDLE,LPVOID,DWORD,PDWORD,LPVOID); 70 | DECLSPEC_IMPORT BOOL WINAPI WriteFile(HANDLE,LPCVOID,DWORD,PDWORD,LPVOID); 71 | DECLSPEC_IMPORT BOOL WINAPI CloseHandle(HANDLE); 72 | DECLSPEC_IMPORT BOOL WINAPI FlushFileBuffers(HANDLE); 73 | 74 | #define IsValidHandle(handle) (handle != (HANDLE) -1) 75 | #define FlushFile(handle) FlushFileBuffers(handle) 76 | 77 | struct timeval 78 | { 79 | long tv_sec; /* seconds */ 80 | long tv_usec; /* microseconds */ 81 | }; 82 | 83 | static inline int gettimeofday(struct timeval *tv, void *__restrict tzp) 84 | { 85 | if (tv) 86 | { 87 | struct timeb timebuffer; 88 | ftime(&timebuffer); 89 | tv->tv_sec = (long) timebuffer.time; 90 | tv->tv_usec = 1000 * timebuffer.millitm; 91 | } 92 | 93 | return 0; 94 | } 95 | 96 | static inline int getpagesize(void) 97 | { 98 | SYSTEM_INFO si; 99 | GetSystemInfo(&si); 100 | return si.dwPageSize; 101 | } 102 | 103 | static inline int uptime(void) 104 | { 105 | return GetTickCount() * 1000; 106 | } 107 | 108 | static inline char *strcasestr(const char *haystack, const char *needle) 109 | { 110 | char c, sc; 111 | size_t len; 112 | 113 | if ((c = *needle++) != 0) 114 | { 115 | c = tolower((unsigned char)c); 116 | len = strlen(needle); 117 | do 118 | { 119 | do 120 | { 121 | if (!(sc = *haystack++)) 122 | return NULL; 123 | } while ((char) tolower((unsigned char) sc) != c); 124 | } while (_strnicmp(haystack, needle, len)); 125 | haystack--; 126 | } 127 | return ((char *) haystack); 128 | } 129 | 130 | 131 | static inline void *load_library(const char *dllname) 132 | { 133 | char libpath[260]; 134 | 135 | if (!dllname) 136 | return GetModuleHandleA(NULL); 137 | 138 | #ifdef _MSC_VER 139 | _snprintf(libpath, sizeof(libpath) - 1, ".\\%s", dllname); 140 | return LoadLibraryA(libpath); 141 | #else 142 | snprintf(libpath, sizeof(libpath) - 1, "libs\\%s.so", dllname); 143 | return LoadLibraryA(libpath); 144 | #endif 145 | } 146 | 147 | 148 | #define dlerror() (GetLastError() == 127) 149 | #define dlsym(handle, sym) (SetLastError(0), (FARPROC) GetProcAddress(handle, sym)) 150 | 151 | #define perror(text) printf("Error - %s: %d\n", text, GetLastError()) 152 | #define strcasecmp stricmp 153 | #define strncasecmp strnicmp 154 | #define snprintf _snprintf 155 | 156 | #define S_IWGRP 0 157 | #define S_IWOTH 0 158 | 159 | #define PROT_READ 0x1 160 | #define PROT_WRITE 0x2 161 | #define PROT_EXEC 0x4 162 | #define PROT_NONE 0x0 163 | 164 | #define MAP_PRIVATE 0 165 | #define MAP_ANON 0 166 | #define MAP_FIXED 0 167 | 168 | #define MAP_FAILED NULL 169 | 170 | #define PAGE_READONLY 0x02 171 | #define PAGE_READWRITE 0x04 172 | #define PAGE_EXECUTE_READ 0x20 173 | #define PAGE_EXECUTE_READWRITE 0x40 174 | 175 | static inline DWORD memprot(int prot) 176 | { 177 | if (prot & PROT_EXEC) 178 | { 179 | if (prot & PROT_WRITE) 180 | return PAGE_EXECUTE_READWRITE; 181 | else 182 | return PAGE_EXECUTE_READ; 183 | } 184 | 185 | if (prot & PROT_WRITE) 186 | return PAGE_READWRITE; 187 | 188 | return PAGE_READONLY; 189 | } 190 | 191 | #define mmap(addr, length, prot, flags, fd, offset) VirtualAlloc(addr, length, 0x3000, memprot(prot)) 192 | #define munmap(addr, length) (VirtualFree(addr, length, 0x8000) ? 0 : -1) 193 | 194 | static inline int mprotect(void *addr, size_t len, int prot) 195 | { 196 | DWORD dummy; 197 | return VirtualProtect(addr, len, memprot(prot), &dummy) ? 0 : -1; 198 | } 199 | 200 | #else 201 | #include 202 | #include 203 | #include 204 | #include 205 | 206 | #if defined(__linux) 207 | #include 208 | #include 209 | #elif defined(__FreeBSD__) 210 | #include 211 | #include 212 | #elif defined(__APPLE__) 213 | #include 214 | #include 215 | #include 216 | #endif 217 | 218 | #ifndef MAP_ANONYMOUS 219 | #define MAP_ANONYMOUS MAP_ANON 220 | #endif 221 | 222 | #ifndef RTLD_DEEPBIND 223 | #define RTLD_DEEPBIND 0 224 | #endif 225 | 226 | #ifndef LIBNLOADER 227 | #define LIBNLOADER "/usr/lib/nloader" 228 | #endif 229 | 230 | static inline void *load_library(const char *dllname) 231 | { 232 | char libpath[512]; 233 | void *handle; 234 | 235 | if (!dllname) 236 | return dlopen(NULL, RTLD_NOW|RTLD_LOCAL|RTLD_DEEPBIND); 237 | 238 | snprintf(libpath, sizeof(libpath) - 1, LIBNLOADER"/%s.so", dllname); 239 | handle = dlopen(libpath, RTLD_NOW|RTLD_LOCAL|RTLD_DEEPBIND); 240 | 241 | if (handle) 242 | return handle; 243 | 244 | snprintf(libpath, sizeof(libpath) - 1, "libs/%s.so", dllname); 245 | return dlopen(libpath, RTLD_NOW|RTLD_LOCAL|RTLD_DEEPBIND); 246 | } 247 | 248 | struct modify_ldt_s 249 | { 250 | unsigned int entry_number; 251 | unsigned long base_addr; 252 | unsigned int limit; 253 | unsigned int seg_32bit:1; 254 | unsigned int contents:2; 255 | unsigned int read_exec_only:1; 256 | unsigned int limit_in_pages:1; 257 | unsigned int seg_not_present:1; 258 | unsigned int useable:1; 259 | }; 260 | 261 | #define LDT_SEL(idx) ((idx) << 3 | 1 << 2 | 3) 262 | 263 | #if defined(__linux) 264 | extern int modify_ldt(int func, struct modify_ldt_s *fs_ldt, unsigned long bytecount); 265 | #define TEB_SEL_IDX 17 266 | 267 | static inline int uptime(void) 268 | { 269 | struct sysinfo info; 270 | if (sysinfo(&info) != -1) 271 | return info.uptime; 272 | return 0; 273 | } 274 | 275 | #elif defined(__APPLE__) || defined(__FreeBSD__) 276 | // old wine loader in mplayer 277 | 278 | #define TEB_SEL_IDX LDT_AUTO_ALLOC 279 | 280 | static inline void LDT_EntryToBytes(unsigned long *buffer, const struct modify_ldt_s *content) 281 | { 282 | *buffer++ = ((content->base_addr & 0x0000ffff) << 16) | (content->limit & 0x0ffff); 283 | 284 | *buffer = (content->base_addr & 0xff000000) | 285 | ((content->base_addr & 0x00ff0000)>>16) | 286 | (content->limit & 0xf0000) | 287 | (content->contents << 10) | 288 | ((content->read_exec_only == 0) << 9) | 289 | ((content->seg_32bit != 0) << 22) | 290 | ((content->limit_in_pages != 0) << 23) | 291 | 0xf000; 292 | } 293 | 294 | static inline int modify_ldt(int func, struct modify_ldt_s *fs_ldt, unsigned long bytecount) 295 | { 296 | int ret; 297 | unsigned long entry[2]; 298 | LDT_EntryToBytes(entry, fs_ldt); 299 | ret = i386_set_ldt(fs_ldt->entry_number, (void *) entry, 1); 300 | return fs_ldt->entry_number = ret; 301 | } 302 | 303 | static inline int uptime(void) 304 | { 305 | 306 | #if defined(__FreeBSD__) 307 | struct timespec ts; 308 | if (clock_gettime(CLOCK_MONOTONIC, &ts) != -1) 309 | return ts.tv_sec; 310 | #elif defined( __APPLE__) 311 | struct timeval uptime; 312 | size_t len = sizeof(uptime); 313 | int mib[2] = { CTL_KERN, KERN_BOOTTIME }; 314 | if (sysctl(mib, 2, &uptime, &len, NULL, 0) == 0) 315 | return time(NULL) - uptime.tv_sec; 316 | #endif 317 | return 0; 318 | } 319 | 320 | #define MODIFY_LDT_CONTENTS_DATA 0 321 | #else 322 | # error Mboh? 323 | #endif 324 | 325 | #define O_BINARY 0 326 | #define IsValidHandle(handle) (handle != -1) 327 | #define FlushFile(handle) (fsync(handle) == 0) 328 | 329 | 330 | #endif 331 | 332 | #endif /* __NT_COMPAT_H */ 333 | -------------------------------------------------------------------------------- /stubs.asm: -------------------------------------------------------------------------------- 1 | ; * Copyright (c) 2010, Sherpya , aCaB 2 | ; * All rights reserved. 3 | ; * 4 | ; * Redistribution and use in source and binary forms, with or without 5 | ; * modification, are permitted provided that the following conditions are met: 6 | ; * * Redistributions of source code must retain the above copyright 7 | ; * notice, this list of conditions and the following disclaimer. 8 | ; * * Redistributions in binary form must reproduce the above copyright 9 | ; * notice, this list of conditions and the following disclaimer in the 10 | ; * documentation and/or other materials provided with the distribution. 11 | ; * * Neither the name of the copyright holders nor the 12 | ; * names of its contributors may be used to endorse or promote products 13 | ; * derived from this software without specific prior written permission. 14 | ; * 15 | ; * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 16 | ; * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 | ; * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | ; * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY 19 | ; * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 | ; * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 | ; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 22 | ; * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | ; * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | ; * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | 26 | %include "asmdefs.inc" 27 | 28 | SECTION RODATA 29 | 30 | msg_notimpl db 'Invoked %s which is not implemented.', 0xa,0 31 | msg_called db 'In %s (called from %p)...',0xa,0 32 | __module__ db 'loader',0 33 | 34 | cextern stubs 35 | cextern import_addrs 36 | cextern import_names 37 | 38 | cextern printf 39 | cextern abort 40 | cextern exit 41 | 42 | SECTION .text 43 | 44 | cfunction stub 45 | push edx 46 | push ecx 47 | push eax 48 | call .delta 49 | 50 | .delta: 51 | mov ecx, stub_end - stub 52 | pop eax 53 | sub eax, .delta - stub 54 | xor edx, edx 55 | sub eax, [stubs] 56 | div ecx 57 | shl eax, 2 58 | mov edx, [import_addrs] 59 | mov ecx, [import_names] 60 | add edx, eax 61 | add ecx, eax 62 | mov edx, dword [edx] 63 | or edx, edx 64 | je .stubbed 65 | 66 | push edx 67 | push dword [esp+0x10] 68 | push dword [ecx] 69 | push msg_called 70 | push __module__ 71 | mov ecx, [fs:0xb0] ; teb->LogModule 72 | call ecx 73 | nop 74 | add esp, 4*4 75 | pop edx 76 | 77 | pop eax 78 | pop ecx 79 | xchg [esp], edx 80 | 81 | ret 82 | 83 | .stubbed: 84 | push dword [ecx] 85 | push msg_notimpl 86 | mov ecx, printf 87 | call ecx 88 | mov ecx, abort 89 | call ecx 90 | stub_end: 91 | 92 | cfunction sizeof_stub 93 | mov eax, stub_end - stub 94 | ret 95 | 96 | cfunction to_ep 97 | mov edx, esp 98 | mov ecx, [fs:0x18] 99 | mov esp, [ecx+4] ; switch stack 100 | 101 | push edx ; save esp, ebp, ebx, esi, edi 102 | push ebp 103 | push ebx 104 | push esi 105 | push edi 106 | 107 | mov eax, [edx + 4] ; exe entrypoint 108 | mov ecx, [fs:0x30] ; pass the peb as an arg to the entrypoint 109 | push ecx 110 | call eax ; exec guest 111 | 112 | mov ecx, [fs:0x18] ; stack is possibly fucked up at this point 113 | mov ecx, [ecx+4] ; we roll back to the bottom 114 | lea esp, [ecx-20] ; minus the saved stuff 115 | 116 | pop edi ; restore clobbered regs 117 | pop esi 118 | pop ebx 119 | pop ebp 120 | pop esp ; revert the old stack 121 | 122 | ret 123 | -------------------------------------------------------------------------------- /upcase.py: -------------------------------------------------------------------------------- 1 | import ctypes 2 | import textwrap 3 | 4 | DWORD = ctypes.c_uint 5 | USHORT = ctypes.c_ushort 6 | PVOID = ctypes.c_void_p 7 | NTSTATUS = ctypes.c_long 8 | 9 | class UNICODE_STRING(ctypes.Structure): 10 | _fields_ = [ 11 | ("Length", USHORT), 12 | ("MaximumLength", USHORT), 13 | ("Buffer", PVOID), 14 | ] 15 | 16 | if __name__ == '__main__': 17 | length = 65535 18 | low = u''.join([ unichr(c) for c in xrange(1, length) ]) 19 | 20 | srcbuf = ctypes.create_unicode_buffer(low, length) 21 | src = UNICODE_STRING(ctypes.sizeof(srcbuf), ctypes.sizeof(srcbuf), ctypes.addressof(srcbuf)) 22 | 23 | dstbuf = ctypes.create_unicode_buffer(u'', length) 24 | dst = UNICODE_STRING(ctypes.sizeof(dstbuf), ctypes.sizeof(dstbuf), ctypes.addressof(dstbuf)) 25 | 26 | res = ctypes.windll.ntdll.RtlUpcaseUnicodeString(ctypes.addressof(dst), ctypes.addressof(src), DWORD(0)) 27 | 28 | if res: 29 | print 'Error', res 30 | raise SystemExit 31 | 32 | values = [ '0x0000' ] + [ '0x%04x' % ord(x) for x in dstbuf.value ] 33 | values = ', '.join(values) 34 | 35 | table = '// Unicode UpperCase Table, automatically generated\n\n' 36 | table += 'static const WCHAR UpcaseTable[65536] =\n{\n ' 37 | table += '\n '.join(textwrap.wrap(values, 72)) 38 | table += '\n};\n' 39 | open('libs/ntdll/upcasetable.h', 'wb').write(table) 40 | -------------------------------------------------------------------------------- /volumeinfo.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010, Sherpya , aCaB 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * * Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * * Neither the name of the copyright holders nor the 13 | * names of its contributors may be used to endorse or promote products 14 | * derived from this software without specific prior written permission. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | 34 | #define BPX() __asm__ volatile ("int 3") 35 | 36 | typedef struct _DRIVE_LAYOUT_INFORMATION_EX { 37 | DWORD PartitionStyle; 38 | DWORD PartitionCount; 39 | union 40 | { 41 | DRIVE_LAYOUT_INFORMATION_MBR Mbr; 42 | DRIVE_LAYOUT_INFORMATION_GPT Gpt; 43 | }; 44 | PARTITION_INFORMATION_EX PartitionEntry[1]; 45 | } DRIVE_LAYOUT_INFORMATION_EX, *PDRIVE_LAYOUT_INFORMATION_EX; 46 | 47 | typedef struct _MOUNTDEV_NAME { 48 | USHORT NameLength; 49 | WCHAR Name[1]; 50 | } MOUNTDEV_NAME, *PMOUNTDEV_NAME; 51 | #define IOCTL_MOUNTDEV_QUERY_DEVICE_NAME 0x004d0008 52 | 53 | typedef struct _STORAGE_HOTPLUG_INFO { 54 | UINT Size; 55 | UCHAR MediaRemovable; 56 | UCHAR MediaHotplug; 57 | UCHAR DeviceHotplug; 58 | UCHAR WriteCacheEnableOverride; 59 | } STORAGE_HOTPLUG_INFO, *PSTORAGE_HOTPLUG_INFO; 60 | #define IOCTL_STORAGE_GET_HOTPLUG_INFO 0x002d0c14 61 | 62 | #define FSCTL_QUERY_DEPENDENT_VOLUME 0x000901f0 63 | 64 | enum 65 | { 66 | _StorageDeviceProperty = 0, 67 | _StorageAdapterProperty, 68 | _StorageDeviceIdProperty, 69 | StorageDeviceUniqueIdProperty, 70 | StorageDeviceWriteCacheProperty, 71 | StorageMiniportProperty, 72 | StorageAccessAlignmentProperty, 73 | StorageDeviceSeekPenaltyProperty, 74 | StorageDeviceTrimProperty, 75 | StorageDeviceWriteAggregationProperty 76 | }; 77 | 78 | // http://msdn.microsoft.com/en-us/library/ff800832(v=VS.85).aspx 79 | static const char bus_types[][32] = { "Invalid", "Scsi", "Atapi", "Ata", "1394", "Ssa", "Fibre", "Usb", "RAID", "iScsi", "Sas", "Sata", "Sd", "Mmc" }; 80 | typedef struct __STORAGE_ADAPTER_DESCRIPTOR { 81 | DWORD Version; // 00 - sizeof(_STORAGE_ADAPTER_DESCRIPTOR) 82 | DWORD Size; // 04 - sizeof(_STORAGE_ADAPTER_DESCRIPTOR) 83 | DWORD MaximumTransferLength; // 08 - 0x20000 for c: 0x10000 for f: 84 | DWORD MaximumPhysicalPages; // 12 - 0x20 for c: - 0x11 for f: 85 | DWORD AlignmentMask; // 16 - 0x01 for c: - 0x00 for f: values: 0 (byte aligned), 1 (word aligned), 3 (DWORD aligned) and 7 (double DWORD aligned) 86 | BOOLEAN AdapterUsesPio; // 20 - 0x01 for c: - 0x00 for f: 87 | BOOLEAN AdapterScansDown; // 21 - 0x01 for c: - 0x00 for f: 88 | BOOLEAN CommandQueueing; // 22 - 0x00 for c: - 0x00 for f: 89 | BOOLEAN AcceleratedTransfer; // 23 - 0x00 for c: - 0x00 for f: 90 | BYTE BusType; // 24 - 0x03 for c: - 0x07 for f: (see table) STORAGE_BUS_TYPE 91 | WORD BusMajorVersion; // 25 - 0x01 for c: - 0x02 for f: 92 | WORD BusMinorVersion; // 27 - 0x00 for c: - 0x00 for f: 93 | } _STORAGE_ADAPTER_DESCRIPTOR, *_PSTORAGE_ADAPTER_DESCRIPTOR; 94 | 95 | typedef struct _STORAGE_ACCESS_ALIGNMENT_DESCRIPTOR { 96 | DWORD Version; 97 | DWORD Size; 98 | DWORD BytesPerCacheLine; 99 | DWORD BytesOffsetForCacheAlignment; 100 | DWORD BytesPerLogicalSector; 101 | DWORD BytesPerPhysicalSector; 102 | DWORD BytesOffsetForSectorAlignment; 103 | } STORAGE_ACCESS_ALIGNMENT_DESCRIPTOR, *PSTORAGE_ACCESS_ALIGNMENT_DESCRIPTOR; 104 | 105 | int main(int argc, char *argv[]) 106 | { 107 | NTSTATUS res; 108 | char *outbuf; 109 | 110 | WCHAR drive[] = L"\\??\\c:"; 111 | 112 | if (argc > 1) 113 | drive[4] = argv[1][0]; 114 | 115 | OBJECT_ATTRIBUTES ob; 116 | memset(&ob, 0, sizeof(OBJECT_ATTRIBUTES)); 117 | ob.Length = sizeof(OBJECT_ATTRIBUTES); 118 | 119 | UNICODE_STRING name; 120 | RtlInitUnicodeString(&name, drive); 121 | ob.ObjectName = &name; 122 | 123 | HANDLE h; 124 | IO_STATUS_BLOCK IoStatusBlock; 125 | 126 | //res = NtOpenFile(&h, 0x100080, &ob, &IoStatusBlock, 7, 17); 127 | res = NtOpenFile(&h, 0x00100003, &ob, &IoStatusBlock, 3, 32); 128 | 129 | if (res) 130 | { 131 | wprintf(L"Fail to open %s = 0x%08x\n", drive, (int) res); 132 | exit(0); 133 | } 134 | 135 | wprintf(L"Opened %s\n", drive); 136 | 137 | FILE_ALIGNMENT_INFORMATION al = { -1 }; 138 | res = NtQueryInformationFile(h, &IoStatusBlock, &al, sizeof(al), FileAlignmentInformation); 139 | printf("0x%08x: AlignmentRequirement = %lu\n", (int) res, al.AlignmentRequirement); 140 | 141 | FILE_FS_DEVICE_INFORMATION fsi; 142 | res = NtQueryVolumeInformationFile(h, &IoStatusBlock, &fsi, sizeof(fsi), FileFsDeviceInformation); 143 | printf("0x%08x: Volume: DeviceType = 0x%08x - Characteristics = 0x%08x\n", (int) res, 144 | (int) fsi.DeviceType, (int) fsi.Characteristics); 145 | 146 | DISK_GEOMETRY geo; 147 | res = NtDeviceIoControlFile(h, NULL, NULL, NULL, &IoStatusBlock, IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0, &geo, sizeof(geo)); 148 | printf("0x%08x: Geo (Type:%d): C = %I64lld - %d/%d/%d\n", (int) res, geo.MediaType, geo.Cylinders.QuadPart, 149 | (int) geo.TracksPerCylinder, (int) geo.SectorsPerTrack, (int) geo.BytesPerSector); 150 | 151 | GET_LENGTH_INFORMATION gli; 152 | res = NtDeviceIoControlFile(h, NULL, NULL, NULL, &IoStatusBlock, IOCTL_DISK_GET_LENGTH_INFO, NULL, 0, &gli, sizeof(gli)); 153 | printf("0x%08x: Volume Length %I64lld\n", (int) res, gli.Length.QuadPart); 154 | 155 | PARTITION_INFORMATION_EX piex; 156 | res = NtDeviceIoControlFile(h, NULL, NULL, NULL, &IoStatusBlock, IOCTL_DISK_GET_PARTITION_INFO_EX, NULL, 0, &piex, sizeof(piex)); 157 | printf("0x%08x: Partinfo: offset: %I64lld - len %I64lld - Hidden = %d\n", (int) res, piex.StartingOffset.QuadPart, piex.PartitionLength.QuadPart, 158 | (int) piex.Mbr.HiddenSectors); 159 | 160 | STORAGE_DEVICE_NUMBER sdn; 161 | res = NtDeviceIoControlFile(h, NULL, NULL, NULL, &IoStatusBlock, IOCTL_STORAGE_GET_DEVICE_NUMBER, NULL, 0, &sdn, sizeof(sdn)); 162 | printf("0x%08x: Type: 0x%08x - Device %d - Partition %d\n", (int) res, (int) sdn.DeviceType, (int) sdn.DeviceNumber, (int) sdn.PartitionNumber); 163 | 164 | #define DLE_SIZE 14016 165 | DRIVE_LAYOUT_INFORMATION_EX *dle = calloc(1, DLE_SIZE); 166 | res = NtDeviceIoControlFile(h, NULL, NULL, NULL, &IoStatusBlock, IOCTL_DISK_GET_DRIVE_LAYOUT_EX, NULL, 0, dle, DLE_SIZE); 167 | printf("0x%08x: Partition Count %d - Signature 0x%08x\n", (int) res, (int) dle->PartitionCount, (int) dle->Mbr.Signature); 168 | free(dle); 169 | 170 | #define MDEV_SIZE 524 171 | MOUNTDEV_NAME *mdev = calloc(1, MDEV_SIZE); 172 | res = NtDeviceIoControlFile(h, NULL, NULL, NULL, &IoStatusBlock, IOCTL_MOUNTDEV_QUERY_DEVICE_NAME, NULL, 0, mdev, MDEV_SIZE); 173 | wprintf(L"0x%08x: Device Name: %s\n", (int) res, mdev->Name); 174 | free(mdev); 175 | 176 | STORAGE_HOTPLUG_INFO hi; 177 | res = NtDeviceIoControlFile(h, NULL, NULL, NULL, &IoStatusBlock, IOCTL_STORAGE_GET_HOTPLUG_INFO, NULL, 0, &hi, sizeof(hi)); 178 | printf("0x%08x: Removable %d - Hotplug %d - DeviceHotplug %d - WCO %d\n", (int) res, (int) hi.MediaRemovable, 179 | (int) hi.MediaHotplug, (int) hi.DeviceHotplug, (int) hi.WriteCacheEnableOverride); 180 | 181 | #define DEPVOL_SIZE 268 182 | outbuf = calloc(1, 268); 183 | DWORD depvol[2] = { 1, 1 }; 184 | memset(outbuf, 0, DEPVOL_SIZE); 185 | res = NtFsControlFile(h, NULL, NULL, NULL, &IoStatusBlock, FSCTL_QUERY_DEPENDENT_VOLUME, depvol, sizeof(depvol), outbuf, DEPVOL_SIZE); 186 | printf("0x%08x: FSCTL_QUERY_DEPENDENT_VOLUME\n", (int) res); // STATUS_INVALID_DEVICE_REQUEST 187 | free(outbuf); 188 | 189 | STORAGE_PROPERTY_QUERY spq; 190 | 191 | STORAGE_ACCESS_ALIGNMENT_DESCRIPTOR saad; 192 | spq.PropertyId = StorageAccessAlignmentProperty; // vista+ 193 | spq.QueryType = PropertyStandardQuery; 194 | memset(&saad, 0, sizeof(saad)); 195 | res = NtDeviceIoControlFile(h, NULL, NULL, NULL, &IoStatusBlock, IOCTL_STORAGE_QUERY_PROPERTY, &spq, sizeof(spq), &saad, sizeof(saad)); 196 | printf("0x%08x: IOCTL_STORAGE_QUERY_PROPERTY Align:\n", (int) res); // STATUS_NOT_IMPLEMENTED 197 | 198 | spq.PropertyId = StorageAdapterProperty; 199 | spq.QueryType = PropertyStandardQuery; 200 | spq.AdditionalParameters[0] = 0x0; 201 | _STORAGE_ADAPTER_DESCRIPTOR sad; 202 | memset(&sad, 0, sizeof(sad)); 203 | res = NtDeviceIoControlFile(h, NULL, NULL, NULL, &IoStatusBlock, IOCTL_STORAGE_QUERY_PROPERTY, &spq, sizeof(spq), &sad, sizeof(sad)); 204 | if (sad.BusType > 12) sad.BusType = 0; 205 | printf("0x%08x: IOCTL_STORAGE_QUERY_PROPERTY Adapter: %s\n", (int) res, bus_types[sad.BusType]); 206 | 207 | //BPX(); 208 | 209 | NtClose(h); 210 | 211 | return 0; 212 | } 213 | --------------------------------------------------------------------------------