Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

350 lines
9.0 KiB

  1. /****************************************************************************
  2. Copyright (c) 1996-1999 Microsoft Corporation
  3. Module Name: tapireg.c
  4. ****************************************************************************/
  5. #ifndef UNICODE
  6. // These wrappers are only used when compiling for ANSI.
  7. #include <windows.h>
  8. #include <windowsx.h>
  9. #include <tapi.h>
  10. #include <tspi.h>
  11. #include "utils.h"
  12. #include "client.h"
  13. #include "private.h"
  14. #include "loc_comn.h"
  15. //***************************************************************************
  16. //***************************************************************************
  17. //***************************************************************************
  18. LONG TAPIRegQueryValueExW(
  19. HKEY hKey,
  20. const CHAR *SectionName,
  21. LPDWORD lpdwReserved,
  22. LPDWORD lpType,
  23. LPBYTE lpData,
  24. LPDWORD lpcbData
  25. )
  26. {
  27. WCHAR *szTempBuffer;
  28. LONG lResult;
  29. lResult = RegQueryValueEx(
  30. hKey,
  31. SectionName,
  32. lpdwReserved,
  33. lpType,
  34. lpData,
  35. lpcbData
  36. );
  37. //
  38. // Any problems?
  39. //
  40. if ( lResult )
  41. {
  42. //
  43. // Yup. Go away.
  44. //
  45. return lResult;
  46. }
  47. if (
  48. (REG_SZ == *lpType)
  49. &&
  50. (NULL != lpData)
  51. )
  52. {
  53. if ( NULL == (szTempBuffer = LocalAlloc( LPTR, *lpcbData * sizeof(WCHAR)) ) )
  54. {
  55. LOG((TL_ERROR, "Alloc failed - QUERYVALW - 0x%08lx", *lpcbData));
  56. return ERROR_NOT_ENOUGH_MEMORY;
  57. }
  58. MultiByteToWideChar(
  59. GetACP(),
  60. MB_PRECOMPOSED,
  61. lpData,
  62. -1,
  63. szTempBuffer,
  64. *lpcbData
  65. );
  66. wcscpy( (PWSTR) lpData, szTempBuffer );
  67. LocalFree( szTempBuffer );
  68. // *lpcbData = ( lstrlenW( (PWSTR)lpData ) + 1 ) * sizeof(WCHAR);
  69. }
  70. //
  71. // Need to adjust the size here because lpData might be NULL, but
  72. // the size needs to reflect WIDE CHAR size (cause we were using
  73. // the ANSI version of ReqQuery)
  74. //
  75. *lpcbData = (*lpcbData + 1) * sizeof(WCHAR);
  76. return 0;
  77. }
  78. //***************************************************************************
  79. //***************************************************************************
  80. //***************************************************************************
  81. LONG TAPIRegSetValueExW(
  82. HKEY hKey,
  83. const CHAR *SectionName,
  84. DWORD dwReserved,
  85. DWORD dwType,
  86. LPBYTE lpData,
  87. DWORD cbData
  88. )
  89. {
  90. CHAR *szTempBuffer;
  91. DWORD dwSize;
  92. LONG lResult;
  93. //
  94. // Only convert the data if this is a Unicode string
  95. //
  96. if ( REG_SZ == dwType )
  97. {
  98. dwSize = WideCharToMultiByte(
  99. GetACP(),
  100. 0,
  101. (PWSTR)lpData,
  102. cbData,
  103. NULL,
  104. 0,
  105. NULL,
  106. NULL
  107. );
  108. if ( NULL == (szTempBuffer = LocalAlloc( LPTR, dwSize )) )
  109. {
  110. LOG((TL_ERROR, "Alloc failed - SETVALW - 0x%08lx", dwSize));
  111. return ERROR_NOT_ENOUGH_MEMORY;
  112. }
  113. dwSize = WideCharToMultiByte(
  114. GetACP(),
  115. 0,
  116. (PWSTR)lpData,
  117. cbData,
  118. szTempBuffer,
  119. dwSize,
  120. NULL,
  121. NULL
  122. );
  123. }
  124. lResult = RegSetValueExA(
  125. hKey,
  126. SectionName,
  127. dwReserved,
  128. dwType,
  129. (REG_SZ == dwType) ?
  130. szTempBuffer :
  131. lpData,
  132. cbData
  133. );
  134. if (REG_SZ == dwType)
  135. {
  136. LocalFree( szTempBuffer );
  137. }
  138. return lResult;
  139. }
  140. //***************************************************************************
  141. //***************************************************************************
  142. //***************************************************************************
  143. int TAPILoadStringW(
  144. HINSTANCE hInst,
  145. UINT uID,
  146. PWSTR pBuffer,
  147. int nBufferMax
  148. )
  149. {
  150. int nResult;
  151. PSTR szTempString;
  152. if ( NULL == ( szTempString = LocalAlloc( LPTR, nBufferMax ) ) )
  153. {
  154. LOG((TL_ERROR, "Alloc failed myloadstr - (0x%lx)", nBufferMax ));
  155. return 0;
  156. }
  157. nResult = LoadStringA(
  158. hInst,
  159. uID,
  160. szTempString,
  161. nBufferMax
  162. );
  163. //
  164. // "... but more importantly: did we get a charge?"
  165. //
  166. if ( nResult )
  167. {
  168. MultiByteToWideChar(
  169. GetACP(),
  170. MB_PRECOMPOSED,
  171. szTempString,
  172. nResult + 1, //For null...
  173. pBuffer,
  174. nBufferMax
  175. );
  176. }
  177. return nResult;
  178. }
  179. //***************************************************************************
  180. //***************************************************************************
  181. //***************************************************************************
  182. HINSTANCE TAPILoadLibraryW(
  183. PWSTR pLibrary
  184. )
  185. {
  186. PSTR pszTempString;
  187. HINSTANCE hResult;
  188. DWORD dwSize;
  189. dwSize = WideCharToMultiByte(
  190. GetACP(),
  191. 0,
  192. pLibrary,
  193. -1,
  194. NULL,
  195. 0,
  196. NULL,
  197. NULL
  198. );
  199. if ( NULL == (pszTempString = LocalAlloc( LPTR, dwSize )) )
  200. {
  201. LOG((TL_ERROR, "Alloc failed - LoadLibW - 0x%08lx", dwSize));
  202. return NULL;
  203. }
  204. WideCharToMultiByte(
  205. GetACP(),
  206. 0,
  207. pLibrary,
  208. dwSize,
  209. pszTempString,
  210. dwSize,
  211. NULL,
  212. NULL
  213. );
  214. hResult = LoadLibraryA( pszTempString );
  215. LocalFree( pszTempString );
  216. return hResult;
  217. }
  218. //
  219. // Swiped this from NT - process.c
  220. //
  221. BOOL
  222. WINAPI
  223. TAPIIsBadStringPtrW(
  224. LPCWSTR lpsz,
  225. UINT cchMax
  226. )
  227. /*++
  228. Routine Description:
  229. This function verifies that the range of memory specified by the
  230. input parameters can be read by the calling process.
  231. The range is the smaller of the number of bytes covered by the
  232. specified NULL terminated UNICODE string, or the number of bytes
  233. specified by cchMax.
  234. If the entire range of memory is accessible, then a value of FALSE
  235. is returned; otherwise, a value of TRUE is returned.
  236. Note that since Win32 is a pre-emptive multi-tasking environment,
  237. the results of this test are only meaningful if the other threads in
  238. the process do not manipulate the range of memory being tested by
  239. this call. Even after a pointer validation, an application should
  240. use the structured exception handling capabilities present in the
  241. system to gaurd access through pointers that it does not control.
  242. Arguments:
  243. lpsz - Supplies the base address of the memory that is to be checked
  244. for read access.
  245. cchMax - Supplies the length in characters to be checked.
  246. Return Value:
  247. TRUE - Some portion of the specified range of memory is not accessible
  248. for read access.
  249. FALSE - All pages within the specified range have been successfully
  250. read.
  251. --*/
  252. {
  253. LPCWSTR EndAddress;
  254. LPCWSTR StartAddress;
  255. WCHAR c;
  256. //
  257. // If the structure has zero length, then do not probe the structure for
  258. // read accessibility.
  259. //
  260. if (cchMax != 0) {
  261. StartAddress = lpsz;
  262. //
  263. // Compute the ending address of the structure and probe for
  264. // read accessibility.
  265. //
  266. EndAddress = (LPCWSTR)((PSZ)StartAddress + (cchMax*2) - 2);
  267. try {
  268. c = *(volatile WCHAR *)StartAddress;
  269. while ( c && StartAddress != EndAddress ) {
  270. StartAddress++;
  271. c = *(volatile WCHAR *)StartAddress;
  272. }
  273. }
  274. except(EXCEPTION_EXECUTE_HANDLER) {
  275. return TRUE;
  276. }
  277. }
  278. return FALSE;
  279. }
  280. #endif // not UNICODE