00001 /****************************** Module Header ******************************\ 00002 * Module Name: random.c 00003 * 00004 * Copyright (c) 1985 - 1999, Microsoft Corporation 00005 * 00006 * This module contains a random collection of support routines for the User 00007 * API functions. Many of these functions will be moved to more appropriate 00008 * files once we get our act together. 00009 * 00010 * History: 00011 * 10-17-90 DarrinM Created. 00012 * 02-06-91 IanJa HWND revalidation added (none required) 00013 \***************************************************************************/ 00014 00015 00016 /***************************************************************************\ 00017 * RtlGetExpWinVer 00018 * 00019 * Returns the expected windows version, in the same format as Win3.1's 00020 * GetExpWinVer(). This takes it out of the module header. As such, this 00021 * api cannot be called from the server context to get version info for 00022 * a client process - instead that information needs to be queried ahead 00023 * of time and passed with any client/server call. 00024 * 00025 * 03-14-92 ScottLu Created. 00026 \***************************************************************************/ 00027 00028 DWORD RtlGetExpWinVer( 00029 HANDLE hmod) 00030 { 00031 PIMAGE_NT_HEADERS pnthdr; 00032 DWORD dwMajor = 3; 00033 DWORD dwMinor = 0xA; 00034 00035 /* 00036 * If it doesn't look like a valid 32bit hmod, use the default 00037 * (i.e., assuming all 16bit hmods are 0x30a) 00038 */ 00039 if ((hmod != NULL) && (LOWORD(HandleToUlong(hmod)) == 0)) { 00040 try { 00041 pnthdr = RtlImageNtHeader((PVOID)hmod); 00042 dwMajor = pnthdr->OptionalHeader.MajorSubsystemVersion; 00043 /* 00044 * Still need this hack 'cuz the linker still puts 00045 * version 1.00 in the header of some things. 00046 */ 00047 if (dwMajor == 1) { 00048 dwMajor = 0x3; 00049 } else { 00050 dwMinor = pnthdr->OptionalHeader.MinorSubsystemVersion; 00051 } 00052 } except (W32ExceptionHandler(FALSE, RIP_WARNING)) { 00053 dwMajor = 3; // just to be safe 00054 dwMinor = 0xA; 00055 } 00056 } 00057 00058 00059 /* 00060 * Return this is a win3.1 compatible format: 00061 * 00062 * 0x030A == win3.1 00063 * 0x0300 == win3.0 00064 * 0x0200 == win2.0, etc. 00065 */ 00066 00067 return (DWORD)MAKELONG(MAKEWORD((BYTE)dwMinor, (BYTE)dwMajor), 0); 00068 } 00069 00070 /***************************************************************************\ 00071 * FindCharPosition 00072 * 00073 * Finds position of character ch in lpString. If not found, the length 00074 * of the string is returned. 00075 * 00076 * History: 00077 * 11-13-90 JimA Created. 00078 \***************************************************************************/ 00079 00080 DWORD FindCharPosition( 00081 LPWSTR lpString, 00082 WCHAR ch) 00083 { 00084 DWORD dwPos = 0L; 00085 00086 while (*lpString && *lpString != ch) { 00087 ++lpString; 00088 ++dwPos; 00089 } 00090 return dwPos; 00091 } 00092 00093 00094 /***************************************************************************\ 00095 * TextCopy 00096 * 00097 * Returns: number of characters copied not including the NULL 00098 * 00099 * History: 00100 * 10-25-90 MikeHar Wrote. 00101 * 11-09-90 DarrinM Rewrote with a radically new algorithm. 00102 * 01-25-91 MikeHar Fixed the radically new algorithm. 00103 * 02-01-91 DarrinM Bite me. 00104 * 11-26-91 DarrinM Ok, this time it's perfect (except NLS, probably). 00105 * 01-13-92 GregoryW Now it's okay for Unicode. 00106 \***************************************************************************/ 00107 00108 UINT TextCopy( 00109 PLARGE_UNICODE_STRING pstr, 00110 LPWSTR pszDst, 00111 UINT cchMax) 00112 { 00113 if (cchMax != 0) { 00114 cchMax = min(pstr->Length / sizeof(WCHAR), cchMax - 1); 00115 RtlCopyMemory(pszDst, (PVOID)pstr->Buffer, cchMax * sizeof(WCHAR)); 00116 pszDst[cchMax] = 0; 00117 } 00118 00119 return cchMax; 00120 } 00121 00122 /***************************************************************************\ 00123 * DWORD wcsncpycch(dest, source, count) - copy no more than n wide chars 00124 * 00125 * Purpose: 00126 * Copies no more than count characters from the source string to the 00127 * destination. If count is less than the length of source, 00128 * NO NULL CHARACTER is put onto the end of the copied string. 00129 * If count is greater than the length of sources, dest is NOT padded 00130 * with more than 1 null character. 00131 * 00132 * 00133 * Entry: 00134 * LPWSTR dest - pointer to destination 00135 * LPWSTR source - source string for copy 00136 * DWORD count - max number of characters to copy 00137 * 00138 * Exit: 00139 * returns number of characters copied into dest, including the null 00140 * terminator, if any. 00141 * 00142 * Exceptions: 00143 * 00144 ****************************************************************************/ 00145 00146 DWORD wcsncpycch ( 00147 LPWSTR dest, 00148 LPCWSTR source, 00149 DWORD count 00150 ) 00151 { 00152 LPWSTR start = dest; 00153 00154 while (count && (*dest++ = *source++)) /* copy string */ 00155 count--; 00156 00157 return (DWORD)(dest - start); 00158 } 00159 00160 /***************************************************************************\ 00161 * DWORD strncpycch(dest, source, count) - copy no more than n characters 00162 * 00163 * Purpose: 00164 * Copies no more than count characters from the source string to the 00165 * destination. If count is less than the length of source, 00166 * NO NULL CHARACTER is put onto the end of the copied string. 00167 * If count is greater than the length of sources, dest is NOT padded 00168 * with more than 1 null character. 00169 * 00170 * 00171 * Entry: 00172 * LPSTR dest - pointer to destination 00173 * LPSTR source - source string for copy 00174 * DWORD count - max number of characters to copy 00175 * 00176 * Exit: 00177 * returns number of characters copied into dest, including the null 00178 * terminator, if any. 00179 * 00180 * Exceptions: 00181 * 00182 *******************************************************************************/ 00183 00184 DWORD strncpycch ( 00185 LPSTR dest, 00186 LPCSTR source, 00187 DWORD count 00188 ) 00189 { 00190 LPSTR start = dest; 00191 00192 while (count && (*dest++ = *source++)) /* copy string */ 00193 count--; 00194 00195 return (DWORD)(dest - start); 00196 }