⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rtl.c

📁 Wine-20031016
💻 C
📖 第 1 页 / 共 2 页
字号:
/* Unit test suite for Rtl* API functions * * Copyright 2003 Thomas Mertes * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA * * NOTES * We use function pointers here as there is no import library for NTDLL on * windows. */#include <stdarg.h>#include <stdlib.h>#include "ntstatus.h"#include "windef.h"#include "winbase.h"#include "wine/test.h"#include "winnt.h"#include "winnls.h"#include "winreg.h"#include "winternl.h"/* Function ptrs for ntdll calls */static HMODULE hntdll = 0;static SIZE_T    (WINAPI  *pRtlCompareMemory)(LPCVOID,LPCVOID,SIZE_T);static SIZE_T    (WINAPI  *pRtlCompareMemoryUlong)(PULONG, SIZE_T, ULONG);static VOID      (WINAPI  *pRtlMoveMemory)(LPVOID,LPCVOID,SIZE_T);static VOID      (WINAPI  *pRtlFillMemory)(LPVOID,SIZE_T,BYTE);static VOID      (WINAPI  *pRtlFillMemoryUlong)(LPVOID,SIZE_T,ULONG);static VOID      (WINAPI  *pRtlZeroMemory)(LPVOID,SIZE_T);static ULONGLONG (WINAPIV *pRtlUlonglongByteSwap)(ULONGLONG source);static ULONG     (WINAPI  *pRtlUniform)(PULONG);static ULONG     (WINAPI  *pRtlRandom)(PULONG);static BOOLEAN   (WINAPI  *pRtlAreAllAccessesGranted)(ACCESS_MASK, ACCESS_MASK);static BOOLEAN   (WINAPI  *pRtlAreAnyAccessesGranted)(ACCESS_MASK, ACCESS_MASK);static DWORD     (WINAPI  *pRtlComputeCrc32)(DWORD,const BYTE*,INT);#define LEN 16static const char* src_src = "This is a test!"; /* 16 bytes long, incl NUL */static ULONG src_aligned_block[4];static ULONG dest_aligned_block[32];static const char *src = (const char*)src_aligned_block;static char* dest = (char*)dest_aligned_block;static void InitFunctionPtrs(void){    hntdll = LoadLibraryA("ntdll.dll");    ok(hntdll != 0, "LoadLibrary failed");    if (hntdll) {	pRtlCompareMemory = (void *)GetProcAddress(hntdll, "RtlCompareMemory");	pRtlCompareMemoryUlong = (void *)GetProcAddress(hntdll, "RtlCompareMemoryUlong");	pRtlMoveMemory = (void *)GetProcAddress(hntdll, "RtlMoveMemory");	pRtlFillMemory = (void *)GetProcAddress(hntdll, "RtlFillMemory");	pRtlFillMemoryUlong = (void *)GetProcAddress(hntdll, "RtlFillMemoryUlong");	pRtlZeroMemory = (void *)GetProcAddress(hntdll, "RtlZeroMemory");	pRtlUlonglongByteSwap = (void *)GetProcAddress(hntdll, "RtlUlonglongByteSwap");	pRtlUniform = (void *)GetProcAddress(hntdll, "RtlUniform");	pRtlRandom = (void *)GetProcAddress(hntdll, "RtlRandom");	pRtlAreAllAccessesGranted = (void *)GetProcAddress(hntdll, "RtlAreAllAccessesGranted");	pRtlAreAnyAccessesGranted = (void *)GetProcAddress(hntdll, "RtlAreAnyAccessesGranted");	pRtlComputeCrc32 = (void *)GetProcAddress(hntdll, "RtlComputeCrc32");    }    strcpy((char*)src_aligned_block, src_src);    ok(strlen(src) == 15, "Source must be 16 bytes long!\n");}#define COMP(str1,str2,cmplen,len) size = pRtlCompareMemory(str1, str2, cmplen); \  ok(size == len, "Expected %ld, got %ld\n", size, (SIZE_T)len)static void test_RtlCompareMemory(void){  SIZE_T size;  if (!pRtlCompareMemory)    return;  strcpy(dest, src);  COMP(src,src,0,0);  COMP(src,src,LEN,LEN);  dest[0] = 'x';  COMP(src,dest,LEN,0);}static void test_RtlCompareMemoryUlong(void){    ULONG a[10];    ULONG result;    a[0]= 0x0123;    a[1]= 0x4567;    a[2]= 0x89ab;    a[3]= 0xcdef;    result = pRtlCompareMemoryUlong(a, 0, 0x0123);    ok(result == 0, "RtlCompareMemoryUlong(%p, 0, 0x0123) returns %lu, expected 0\n", a, result);    result = pRtlCompareMemoryUlong(a, 3, 0x0123);    ok(result == 0, "RtlCompareMemoryUlong(%p, 3, 0x0123) returns %lu, expected 0\n", a, result);    result = pRtlCompareMemoryUlong(a, 4, 0x0123);    ok(result == 4, "RtlCompareMemoryUlong(%p, 4, 0x0123) returns %lu, expected 4\n", a, result);    result = pRtlCompareMemoryUlong(a, 5, 0x0123);    ok(result == 4, "RtlCompareMemoryUlong(%p, 5, 0x0123) returns %lu, expected 4\n", a, result);    result = pRtlCompareMemoryUlong(a, 7, 0x0123);    ok(result == 4, "RtlCompareMemoryUlong(%p, 7, 0x0123) returns %lu, expected 4\n", a, result);    result = pRtlCompareMemoryUlong(a, 8, 0x0123);    ok(result == 4, "RtlCompareMemoryUlong(%p, 8, 0x0123) returns %lu, expected 4\n", a, result);    result = pRtlCompareMemoryUlong(a, 9, 0x0123);    ok(result == 4, "RtlCompareMemoryUlong(%p, 9, 0x0123) returns %lu, expected 4\n", a, result);    result = pRtlCompareMemoryUlong(a, 4, 0x0127);    ok(result == 0, "RtlCompareMemoryUlong(%p, 4, 0x0127) returns %lu, expected 0\n", a, result);    result = pRtlCompareMemoryUlong(a, 4, 0x7123);    ok(result == 0, "RtlCompareMemoryUlong(%p, 4, 0x7123) returns %lu, expected 0\n", a, result);    result = pRtlCompareMemoryUlong(a, 16, 0x4567);    ok(result == 0, "RtlCompareMemoryUlong(%p, 16, 0x4567) returns %lu, expected 0\n", a, result);    a[1]= 0x0123;    result = pRtlCompareMemoryUlong(a, 3, 0x0123);    ok(result == 0, "RtlCompareMemoryUlong(%p, 3, 0x0123) returns %lu, expected 0\n", a, result);    result = pRtlCompareMemoryUlong(a, 4, 0x0123);    ok(result == 4, "RtlCompareMemoryUlong(%p, 4, 0x0123) returns %lu, expected 4\n", a, result);    result = pRtlCompareMemoryUlong(a, 5, 0x0123);    ok(result == 4, "RtlCompareMemoryUlong(%p, 5, 0x0123) returns %lu, expected 4\n", a, result);    result = pRtlCompareMemoryUlong(a, 7, 0x0123);    ok(result == 4, "RtlCompareMemoryUlong(%p, 7, 0x0123) returns %lu, expected 4\n", a, result);    result = pRtlCompareMemoryUlong(a, 8, 0x0123);    ok(result == 8, "RtlCompareMemoryUlong(%p, 8, 0x0123) returns %lu, expected 8\n", a, result);    result = pRtlCompareMemoryUlong(a, 9, 0x0123);    ok(result == 8, "RtlCompareMemoryUlong(%p, 9, 0x0123) returns %lu, expected 8\n", a, result);}#define COPY(len) memset(dest,0,sizeof(dest_aligned_block)); pRtlMoveMemory(dest, src, len)#define CMP(str) ok(strcmp(dest,str) == 0, "Expected '%s', got '%s'\n", str, dest)static void test_RtlMoveMemory(void){  if (!pRtlMoveMemory)    return;  /* Length should be in bytes and not rounded. Use strcmp to ensure we   * didn't write past the end (it checks for the final NUL left by memset)   */  COPY(0); CMP("");  COPY(1); CMP("T");  COPY(2); CMP("Th");  COPY(3); CMP("Thi");  COPY(4); CMP("This");  COPY(5); CMP("This ");  COPY(6); CMP("This i");  COPY(7); CMP("This is");  COPY(8); CMP("This is ");  COPY(9); CMP("This is a");  /* Overlapping */  strcpy(dest, src); pRtlMoveMemory(dest, dest + 1, strlen(src) - 1);  CMP("his is a test!!");  strcpy(dest, src); pRtlMoveMemory(dest + 1, dest, strlen(src));  CMP("TThis is a test!");}#define FILL(len) memset(dest,0,sizeof(dest_aligned_block)); strcpy(dest, src); pRtlFillMemory(dest,len,'x')static void test_RtlFillMemory(void){  if (!pRtlFillMemory)    return;  /* Length should be in bytes and not rounded. Use strcmp to ensure we   * didn't write past the end (the remainder of the string should match)   */  FILL(0); CMP("This is a test!");  FILL(1); CMP("xhis is a test!");  FILL(2); CMP("xxis is a test!");  FILL(3); CMP("xxxs is a test!");  FILL(4); CMP("xxxx is a test!");  FILL(5); CMP("xxxxxis a test!");  FILL(6); CMP("xxxxxxs a test!");  FILL(7); CMP("xxxxxxx a test!");  FILL(8); CMP("xxxxxxxxa test!");  FILL(9); CMP("xxxxxxxxx test!");}#define LFILL(len) memset(dest,0,sizeof(dest_aligned_block)); strcpy(dest, src); pRtlFillMemoryUlong(dest,len,val)static void test_RtlFillMemoryUlong(void){  ULONG val = ('x' << 24) | ('x' << 16) | ('x' << 8) | 'x';  if (!pRtlFillMemoryUlong)    return;  /* Length should be in bytes and not rounded. Use strcmp to ensure we   * didn't write past the end (the remainder of the string should match)   */  LFILL(0); CMP("This is a test!");  LFILL(1); CMP("This is a test!");  LFILL(2); CMP("This is a test!");  LFILL(3); CMP("This is a test!");  LFILL(4); CMP("xxxx is a test!");  LFILL(5); CMP("xxxx is a test!");  LFILL(6); CMP("xxxx is a test!");  LFILL(7); CMP("xxxx is a test!");  LFILL(8); CMP("xxxxxxxxa test!");  LFILL(9); CMP("xxxxxxxxa test!");}#define ZERO(len) memset(dest,0,sizeof(dest_aligned_block)); strcpy(dest, src); pRtlZeroMemory(dest,len)#define MCMP(str) ok(memcmp(dest,str,LEN) == 0, "Memcmp failed\n")static void test_RtlZeroMemory(void){  if (!pRtlZeroMemory)    return;  /* Length should be in bytes and not rounded. */  ZERO(0); MCMP("This is a test!");  ZERO(1); MCMP("\0his is a test!");  ZERO(2); MCMP("\0\0is is a test!");  ZERO(3); MCMP("\0\0\0s is a test!");  ZERO(4); MCMP("\0\0\0\0 is a test!");  ZERO(5); MCMP("\0\0\0\0\0is a test!");  ZERO(6); MCMP("\0\0\0\0\0\0s a test!");  ZERO(7); MCMP("\0\0\0\0\0\0\0 a test!");  ZERO(8); MCMP("\0\0\0\0\0\0\0\0a test!");  ZERO(9); MCMP("\0\0\0\0\0\0\0\0\0 test!");}static void test_RtlUlonglongByteSwap(void){    ULONGLONG result;    result = pRtlUlonglongByteSwap( ((ULONGLONG)0x76543210 << 32) | 0x87654321 );    ok( (((ULONGLONG)0x21436587 << 32) | 0x10325476) == result,       "RtlUlonglongByteSwap(0x7654321087654321) returns 0x%llx, expected 0x2143658710325476",       result);}static void test_RtlUniform(void){    ULONGLONG num;    ULONG seed;    ULONG seed_bak;    ULONG expected;    ULONG result;/* * According to the documentation RtlUniform is using D.H. Lehmer's 1948 * algorithm. This algorithm is: * * seed = (seed * const_1 + const_2) % const_3; * * According to the documentation the random number is distributed over * [0..MAXLONG]. Therefore const_3 is MAXLONG + 1: * * seed = (seed * const_1 + const_2) % (MAXLONG + 1); * * Because MAXLONG is 0x7fffffff (and MAXLONG + 1 is 0x80000000) the * algorithm can be expressed without division as: * * seed = (seed * const_1 + const_2) & MAXLONG; * * To find out const_2 we just call RtlUniform with seed set to 0: */    seed = 0;    expected = 0x7fffffc3;    result = pRtlUniform(&seed);    ok(result == expected,        "RtlUniform(&seed (seed == 0)) returns %lx, expected %lx",        result, expected);/* * The algorithm is now: * * seed = (seed * const_1 + 0x7fffffc3) & MAXLONG; * * To find out const_1 we can use: * * const_1 = RtlUniform(1) - 0x7fffffc3; * * If that does not work a search loop can try all possible values of * const_1 and compare to the result to RtlUniform(1). * This way we find out that const_1 is 0xffffffed. * * For seed = 1 the const_2 is 0x7fffffc4: */    seed = 1;    expected = seed * 0xffffffed + 0x7fffffc3 + 1;    result = pRtlUniform(&seed);    ok(result == expected,        "RtlUniform(&seed (seed == 1)) returns %lx, expected %lx",        result, expected);/* * For seed = 2 the const_2 is 0x7fffffc3: */    seed = 2;    expected = seed * 0xffffffed + 0x7fffffc3;    result = pRtlUniform(&seed);    ok(result == expected,        "RtlUniform(&seed (seed == 2)) returns %lx, expected %lx",        result, expected);/* * More tests show that if seed is odd the result must be incremented by 1: */    seed = 3;    expected = seed * 0xffffffed + 0x7fffffc3 + (seed & 1);    result = pRtlUniform(&seed);    ok(result == expected,        "RtlUniform(&seed (seed == 2)) returns %lx, expected %lx",        result, expected);    seed = 0x6bca1aa;    expected = seed * 0xffffffed + 0x7fffffc3;    result = pRtlUniform(&seed);    ok(result == expected,        "RtlUniform(&seed (seed == 0x6bca1aa)) returns %lx, expected %lx",        result, expected);    seed = 0x6bca1ab;    expected = seed * 0xffffffed + 0x7fffffc3 + 1;    result = pRtlUniform(&seed);    ok(result == expected,        "RtlUniform(&seed (seed == 0x6bca1ab)) returns %lx, expected %lx",        result, expected);/* * When seed is 0x6bca1ac there is an exception: */    seed = 0x6bca1ac;    expected = seed * 0xffffffed + 0x7fffffc3 + 2;    result = pRtlUniform(&seed);    ok(result == expected,        "RtlUniform(&seed (seed == 0x6bca1ac)) returns %lx, expected %lx",        result, expected);/* * Note that up to here const_3 is not used * (the highest bit of the result is not set). * * Starting with 0x6bca1ad: If seed is even the result must be incremented by 1: */    seed = 0x6bca1ad;    expected = (seed * 0xffffffed + 0x7fffffc3) & MAXLONG;    result = pRtlUniform(&seed);    ok(result == expected,        "RtlUniform(&seed (seed == 0x6bca1ad)) returns %lx, expected %lx",        result, expected);    seed = 0x6bca1ae;    expected = (seed * 0xffffffed + 0x7fffffc3 + 1) & MAXLONG;    result = pRtlUniform(&seed);    ok(result == expected,        "RtlUniform(&seed (seed == 0x6bca1ae)) returns %lx, expected %lx",        result, expected);/* * There are several ranges where for odd or even seed the result must be * incremented by 1. You can see this ranges in the following test. * * For a full test use one of the following loop heads: * *  for (num = 0; num <= 0xffffffff; num++) { *      seed = num; *      ... * *  seed = 0; *  for (num = 0; num <= 0xffffffff; num++) { *      ... */    seed = 0;    for (num = 0; num <= 100000; num++) {	expected = seed * 0xffffffed + 0x7fffffc3;	if (seed < 0x6bca1ac) {	    expected = expected + (seed & 1);	} else if (seed == 0x6bca1ac) {	    expected = (expected + 2) & MAXLONG;	} else if (seed < 0xd79435c) {	    expected = (expected + (~seed & 1)) & MAXLONG;	} else if (seed < 0x1435e50b) {	    expected = expected + (seed & 1);	} else if (seed < 0x1af286ba) { 	    expected = (expected + (~seed & 1)) & MAXLONG;	} else if (seed < 0x21af2869) {	    expected = expected + (seed & 1);	} else if (seed < 0x286bca18) {	    expected = (expected + (~seed & 1)) & MAXLONG;	} else if (seed < 0x2f286bc7) {	    expected = expected + (seed & 1);	} else if (seed < 0x35e50d77) {	    expected = (expected + (~seed & 1)) & MAXLONG;	} else if (seed < 0x3ca1af26) {	    expected = expected + (seed & 1);	} else if (seed < 0x435e50d5) {	    expected = (expected + (~seed & 1)) & MAXLONG;	} else if (seed < 0x4a1af284) {	    expected = expected + (seed & 1);	} else if (seed < 0x50d79433) {	    expected = (expected + (~seed & 1)) & MAXLONG;	} else if (seed < 0x579435e2) {	    expected = expected + (seed & 1);	} else if (seed < 0x5e50d792) {	    expected = (expected + (~seed & 1)) & MAXLONG;	} else if (seed < 0x650d7941) {	    expected = expected + (seed & 1);	} else if (seed < 0x6bca1af0) {	    expected = (expected + (~seed & 1)) & MAXLONG;	} else if (seed < 0x7286bc9f) {	    expected = expected + (seed & 1);	} else if (seed < 0x79435e4e) {	    expected = (expected + (~seed & 1)) & MAXLONG;	} else if (seed < 0x7ffffffd) {	    expected = expected + (seed & 1);	} else if (seed < 0x86bca1ac) {	    expected = (expected + (~seed & 1)) & MAXLONG;	} else if (seed == 0x86bca1ac) {	    expected = (expected + 1) & MAXLONG;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -