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.

619 lines
14 KiB

  1. #include "precomp.h"
  2. #include <marsdev.h>
  3. //
  4. // Compare if two strings are equal.
  5. //
  6. BOOL StrEqlW(LPCWSTR psz1, LPCWSTR psz2)
  7. {
  8. while (*psz1 && *psz2 && *psz1 == *psz2)
  9. {
  10. psz1++;
  11. psz2++;
  12. }
  13. return (L'\0' == *psz1 && L'\0' == *psz2);
  14. }
  15. BOOL StrEqlA(LPCSTR psz1, LPCSTR psz2)
  16. {
  17. // bobn : I chose not to thunk to the wide version
  18. // for efficiency's sake.
  19. while (*psz1 && *psz2 && *psz1 == *psz2)
  20. {
  21. psz1++;
  22. psz2++;
  23. }
  24. return ('\0' == *psz1 && '\0' == *psz2);
  25. }
  26. int MapWCHARToInt(WCHAR c)
  27. {
  28. int nReturn;
  29. if ((L'0' <= c) && (L'9' >= c))
  30. nReturn = c - L'0';
  31. else if ((L'a' <= c) && (L'f' >= c))
  32. nReturn = c - L'a' + 10;
  33. else if ((L'A' <= c) && (L'F' >= c))
  34. nReturn = c - L'A' + 10;
  35. else
  36. {
  37. nReturn = 0;
  38. }
  39. return nReturn;
  40. }
  41. UINT64 HexStringToUINT64W(LPCWSTR lpwstr)
  42. {
  43. int start = 0;
  44. UINT64 iReturn = 0;
  45. // Read away leading 0's and x prefix
  46. while ((lpwstr[start]) &&
  47. ((lpwstr[start] == L'0') || (lpwstr[start] == L'x') || (lpwstr[start] == L'X')))
  48. {
  49. start++;
  50. }
  51. // Only proceed if we have something to work with
  52. while (lpwstr[start])
  53. {
  54. // Shift the current value
  55. iReturn <<= 4;
  56. // Place next digit
  57. iReturn |= MapWCHARToInt(lpwstr[start++]);
  58. }
  59. return iReturn;
  60. }
  61. /*
  62. * VARENUM usage key,
  63. *
  64. * * [V] - may appear in a VARIANT
  65. * * [T] - may appear in a TYPEDESC
  66. * * [P] - may appear in an OLE property set
  67. * * [S] - may appear in a Safe Array
  68. *
  69. *
  70. * VT_EMPTY [V] [P] nothing
  71. * VT_NULL [V] [P] SQL style Null
  72. * VT_I2 [V][T][P][S] 2 byte signed int
  73. * VT_I4 [V][T][P][S] 4 byte signed int
  74. * VT_R4 [V][T][P][S] 4 byte real
  75. * VT_R8 [V][T][P][S] 8 byte real
  76. * VT_CY [V][T][P][S] currency
  77. * VT_DATE [V][T][P][S] date
  78. * VT_BSTR [V][T][P][S] OLE Automation string
  79. * VT_DISPATCH [V][T] [S] IDispatch *
  80. * VT_ERROR [V][T][P][S] SCODE
  81. * VT_BOOL [V][T][P][S] True=-1, False=0
  82. * VT_VARIANT [V][T][P][S] VARIANT *
  83. * VT_UNKNOWN [V][T] [S] IUnknown *
  84. * VT_DECIMAL [V][T] [S] 16 byte fixed point
  85. * VT_RECORD [V] [P][S] user defined type
  86. * VT_I1 [V][T][P][s] signed char
  87. * VT_UI1 [V][T][P][S] unsigned char
  88. * VT_UI2 [V][T][P][S] unsigned short
  89. * VT_UI4 [V][T][P][S] unsigned short
  90. * VT_I8 [T][P] signed 64-bit int
  91. * VT_UI8 [T][P] unsigned 64-bit int
  92. * VT_INT [V][T][P][S] signed machine int
  93. * VT_UINT [V][T] [S] unsigned machine int
  94. * VT_VOID [T] C style void
  95. * VT_HRESULT [T] Standard return type
  96. * VT_PTR [T] pointer type
  97. * VT_SAFEARRAY [T] (use VT_ARRAY in VARIANT)
  98. * VT_CARRAY [T] C style array
  99. * VT_USERDEFINED [T] user defined type
  100. * VT_LPSTR [T][P] null terminated string
  101. * VT_LPWSTR [T][P] wide null terminated string
  102. * VT_FILETIME [P] FILETIME
  103. * VT_BLOB [P] Length prefixed bytes
  104. * VT_STREAM [P] Name of the stream follows
  105. * VT_STORAGE [P] Name of the storage follows
  106. * VT_STREAMED_OBJECT [P] Stream contains an object
  107. * VT_STORED_OBJECT [P] Storage contains an object
  108. * VT_VERSIONED_STREAM [P] Stream with a GUID version
  109. * VT_BLOB_OBJECT [P] Blob contains an object
  110. * VT_CF [P] Clipboard format
  111. * VT_CLSID [P] A Class ID
  112. * VT_VECTOR [P] simple counted array
  113. * VT_ARRAY [V] SAFEARRAY*
  114. * VT_BYREF [V] void* for local use
  115. * VT_BSTR_BLOB Reserved for system use
  116. */
  117. BOOL IsValidVariant(VARIANT var)
  118. {
  119. BOOL fRet;
  120. if(!(var.vt & VT_BYREF))
  121. {
  122. switch(var.vt)
  123. {
  124. //
  125. // Types that can have any value.
  126. //
  127. case VT_EMPTY:
  128. case VT_NULL:
  129. case VT_I2:
  130. case VT_I4:
  131. case VT_R4:
  132. case VT_R8:
  133. case VT_CY:
  134. case VT_DATE:
  135. case VT_ERROR:
  136. case VT_DECIMAL:
  137. case VT_RECORD:
  138. case VT_I1:
  139. case VT_UI1:
  140. case VT_UI2:
  141. case VT_UI4:
  142. case VT_INT:
  143. case VT_UINT:
  144. fRet = TRUE;
  145. break;
  146. case VT_BOOL:
  147. fRet = IsValidVariantBoolVal(var.boolVal);
  148. break;
  149. case VT_BSTR:
  150. fRet = (NULL == var.bstrVal) || IsValidBstr(var.bstrVal);
  151. break;
  152. case VT_DISPATCH:
  153. fRet = IsValidInterfacePtr(var.pdispVal);
  154. break;
  155. case VT_UNKNOWN:
  156. fRet = IsValidInterfacePtr(var.punkVal);
  157. break;
  158. case VT_ARRAY:
  159. fRet = IsValidReadPtr(var.parray);
  160. break;
  161. default:
  162. fRet = FALSE;
  163. break;
  164. }
  165. }
  166. else
  167. {
  168. // VT_BYREF
  169. switch(var.vt & ~VT_BYREF)
  170. {
  171. case 0: // void*
  172. fRet = var.byref != NULL;
  173. break;
  174. case VT_I2:
  175. fRet = IsValidReadPtr(var.piVal);
  176. break;
  177. case VT_I4:
  178. fRet = IsValidReadPtr(var.plVal);
  179. break;
  180. case VT_R4:
  181. fRet = IsValidReadPtr(var.pfltVal);
  182. break;
  183. case VT_R8:
  184. fRet = IsValidReadPtr(var.pdblVal);
  185. break;
  186. case VT_CY:
  187. fRet = IsValidReadPtr(var.pcyVal);
  188. break;
  189. case VT_DATE:
  190. fRet = IsValidReadPtr(var.pdate);
  191. break;
  192. case VT_ERROR:
  193. fRet = IsValidReadPtr(var.pscode);
  194. break;
  195. case VT_DECIMAL:
  196. fRet = IsValidReadPtr(var.pdecVal);
  197. break;
  198. case VT_I1:
  199. fRet = IsValidReadPtr(var.pbVal);
  200. break;
  201. case VT_UI1:
  202. fRet = IsValidReadPtr(var.pcVal);
  203. break;
  204. case VT_UI2:
  205. fRet = IsValidReadPtr(var.puiVal);
  206. break;
  207. case VT_UI4:
  208. fRet = IsValidReadPtr(var.pulVal);
  209. break;
  210. case VT_INT:
  211. fRet = IsValidReadPtr(var.pintVal);
  212. break;
  213. case VT_UINT:
  214. fRet = IsValidReadPtr(var.puintVal);
  215. break;
  216. case VT_BOOL:
  217. fRet = IsValidReadPtr(var.pboolVal);
  218. break;
  219. case VT_BSTR:
  220. fRet = IsValidReadPtr(var.pbstrVal);
  221. break;
  222. case VT_DISPATCH:
  223. fRet = IsValidReadPtr(var.ppdispVal);
  224. break;
  225. case VT_VARIANT:
  226. fRet = IsValidReadPtr(var.pvarVal);
  227. break;
  228. case VT_UNKNOWN:
  229. fRet = IsValidReadPtr(var.ppunkVal);
  230. break;
  231. case VT_ARRAY:
  232. fRet = IsValidReadPtr(var.pparray);
  233. break;
  234. default:
  235. fRet = FALSE;
  236. break;
  237. }
  238. }
  239. return fRet;
  240. }
  241. BOOL IsValidStringPtrBuffer(LPOLESTR* ppStr, UINT n)
  242. {
  243. BOOL fRet;
  244. fRet = IsValidReadBuffer(ppStr, n);
  245. if (fRet)
  246. {
  247. for (UINT i = 0; i < n && fRet; i++)
  248. {
  249. fRet = IsValidStringW(ppStr[i]);
  250. }
  251. }
  252. return fRet;
  253. }
  254. //
  255. // API parameter validation helpers. Use these on public APIs. If a parameter
  256. // is bad on debug build a RIP message will be generated.
  257. //
  258. #ifdef DEBUG
  259. #undef API_IsValidReadPtr
  260. BOOL API_IsValidReadPtr(void* ptr, UINT cbSize)
  261. {
  262. BOOL fRet;
  263. if (ptr)
  264. {
  265. fRet = TRUE;
  266. }
  267. else
  268. {
  269. fRet = FALSE;
  270. }
  271. return fRet;
  272. }
  273. #undef API_IsValidWritePtr
  274. BOOL API_IsValidWritePtr(void* ptr, UINT cbSize)
  275. {
  276. BOOL fRet;
  277. if (ptr)
  278. {
  279. fRet = TRUE;
  280. }
  281. else
  282. {
  283. //WCHAR szFunc[MAX_PATH];
  284. //GetFunctionName(1, szFunc, ARRAYSIZE(szFunc));
  285. //
  286. //RipMsg(L"(%s) Passed a bad write pointer 0x%08lx", szFunc, ptr);
  287. fRet = FALSE;
  288. }
  289. return fRet;
  290. }
  291. BOOL API_IsValidStringW(LPCWSTR psz)
  292. {
  293. BOOL fRet;
  294. if (IsValidString(psz))
  295. {
  296. fRet = TRUE;
  297. }
  298. else
  299. {
  300. //WCHAR szFunc[MAX_PATH];
  301. //GetFunctionName(1, szFunc, ARRAYSIZE(szFunc));
  302. //
  303. //RipMsg(L"(%s) Passed a bad string pointer 0x%08lx", szFunc, psz);
  304. fRet = FALSE;
  305. }
  306. return fRet;
  307. }
  308. #undef API_IsValidReadBuffer
  309. BOOL API_IsValidReadBuffer(void* ptr, UINT cbSize, UINT n)
  310. {
  311. BOOL fRet;
  312. if (ptr)
  313. {
  314. fRet = TRUE;
  315. }
  316. else
  317. {
  318. //WCHAR szFunc[MAX_PATH];
  319. //GetFunctionName(1, szFunc, ARRAYSIZE(szFunc));
  320. //
  321. //RipMsg(L"(%s) Passed a bad read buffer 0x%08lx size %d",
  322. // szFunc, ptr, n);
  323. fRet = FALSE;
  324. }
  325. return fRet;
  326. }
  327. #undef API_IsValidWriteBuffer
  328. BOOL API_IsValidWriteBuffer(void* ptr, UINT cbSize, UINT n)
  329. {
  330. BOOL fRet;
  331. if (ptr)
  332. {
  333. fRet = TRUE;
  334. }
  335. else
  336. {
  337. //WCHAR szFunc[MAX_PATH];
  338. //GetFunctionName(1, szFunc, ARRAYSIZE(szFunc));
  339. //
  340. //RipMsg(L"(%s) Passed a bad write buffer 0x%08lx size %d",
  341. // szFunc, ptr, n);
  342. fRet = FALSE;
  343. }
  344. return fRet;
  345. }
  346. BOOL API_IsValidInterfacePtr(IUnknown* punk)
  347. {
  348. BOOL fRet;
  349. if (IsValidInterfacePtr(punk))
  350. {
  351. fRet = TRUE;
  352. }
  353. else
  354. {
  355. //WCHAR szFunc[MAX_PATH];
  356. //GetFunctionName(1, szFunc, ARRAYSIZE(szFunc));
  357. //
  358. //RipMsg(L"(%s) Passed a bad interface pointer 0x%08lx", szFunc, punk);
  359. fRet = FALSE;
  360. }
  361. return fRet;
  362. }
  363. BOOL API_IsValidFunctionPtr(void *pfunc)
  364. {
  365. BOOL fRet;
  366. if (IsValidFunctionPtr(pfunc))
  367. {
  368. fRet = TRUE;
  369. }
  370. else
  371. {
  372. //WCHAR szFunc[MAX_PATH];
  373. //GetFunctionName(1, szFunc, ARRAYSIZE(szFunc));
  374. //
  375. //RipMsg(L"(%s) Passed a bad function pointer 0x%08lx", szFunc, pfunc);
  376. fRet = FALSE;
  377. }
  378. return fRet;
  379. }
  380. BOOL API_IsValidVariant(VARIANT var)
  381. {
  382. BOOL fRet;
  383. if (IsValidVariant(var))
  384. {
  385. fRet = TRUE;
  386. }
  387. else
  388. {
  389. //WCHAR szFunc[MAX_PATH];
  390. //GetFunctionName(1, szFunc, ARRAYSIZE(szFunc));
  391. //
  392. //RipMsg(L"(%s) Passed a bad variant ", szFunc);
  393. fRet = FALSE;
  394. }
  395. return fRet;
  396. }
  397. BOOL API_IsValidVariantI4(VARIANT var)
  398. {
  399. BOOL fRet;
  400. if (IsValidVariantI4(var))
  401. {
  402. fRet = TRUE;
  403. }
  404. else
  405. {
  406. //WCHAR szFunc[MAX_PATH];
  407. //GetFunctionName(1, szFunc, ARRAYSIZE(szFunc));
  408. //
  409. //RipMsg(L"(%s) Passed a bad variant (should be of type I4)", szFunc);
  410. fRet = FALSE;
  411. }
  412. return fRet;
  413. }
  414. BOOL API_IsValidVariantBstr(VARIANT var)
  415. {
  416. BOOL fRet;
  417. if (IsValidVariantBstr(var))
  418. {
  419. fRet = TRUE;
  420. }
  421. else
  422. {
  423. //WCHAR szFunc[MAX_PATH];
  424. //GetFunctionName(1, szFunc, ARRAYSIZE(szFunc));
  425. //
  426. //RipMsg(L"(%s) Passed a bad variant (should be of type VT_BSTR)", szFunc);
  427. fRet = FALSE;
  428. }
  429. return fRet;
  430. }
  431. BOOL API_IsValidBstr(BSTR bstr)
  432. {
  433. BOOL fRet;
  434. if (IsValidBstr(bstr))
  435. {
  436. fRet = TRUE;
  437. }
  438. else
  439. {
  440. //WCHAR szFunc[MAX_PATH];
  441. //GetFunctionName(1, szFunc, ARRAYSIZE(szFunc));
  442. //
  443. //RipMsg(L"(%s) Passed a bad BSTR", szFunc);
  444. fRet = FALSE;
  445. }
  446. return fRet;
  447. }
  448. BOOL API_IsValidOptionalBstr(BSTR bstr)
  449. {
  450. return (!bstr || API_IsValidBstr(bstr));
  451. }
  452. BOOL API_IsValidFlag(DWORD f, DWORD fAll)
  453. {
  454. BOOL fRet;
  455. if (IsValidFlag(f, fAll))
  456. {
  457. fRet = TRUE;
  458. }
  459. else
  460. {
  461. //WCHAR szFunc[MAX_PATH];
  462. //GetFunctionName(1, szFunc, ARRAYSIZE(szFunc));
  463. //
  464. //RipMsg(L"(%s) Passed bad flags %d (valid flags = %d)", szFunc, f, fAll);
  465. fRet = FALSE;
  466. }
  467. return fRet;
  468. }
  469. BOOL API_IsValidStringPtrBufferW(LPOLESTR* ppStr, UINT n)
  470. {
  471. BOOL fRet;
  472. if (IsValidStringPtrBuffer(ppStr, n))
  473. {
  474. fRet = TRUE;
  475. }
  476. else
  477. {
  478. //WCHAR szFunc[MAX_PATH];
  479. //GetFunctionName(1, szFunc, ARRAYSIZE(szFunc));
  480. //
  481. //RipMsg(L"(%s) Passed a bad array of string pointers", szFunc);
  482. fRet = FALSE;
  483. }
  484. return fRet;
  485. }
  486. #endif
  487. //------------------------------------------------------------------------------
  488. // SanitizeResult
  489. //
  490. // OM methods called from script get sanitized results (ie S_FALSE rather than
  491. // E_FAIL), which suppresses script errors while allowing C-code calling OM
  492. // methods to get "normal" (unsanitized) HRESULTs.
  493. //
  494. HRESULT SanitizeResult(HRESULT hr)
  495. {
  496. //
  497. // HACK:
  498. // Let DISP_E_MEMBERNOTFOUND go through -- this is because
  499. // behaviors use this IDispatchImpl and trident depends on this
  500. // HRESULT not being S_FALSE
  501. //
  502. if (FAILED(hr) && (hr != DISP_E_MEMBERNOTFOUND))
  503. {
  504. hr = S_FALSE;
  505. }
  506. return hr;
  507. }
  508. ////////////////////////////////////////////////////////////////////////////////
  509. //
  510. // Retail version of new and delete. New zero inits memory.
  511. //
  512. void* __cdecl operator new(size_t cbSize)
  513. {
  514. return (void*)LocalAlloc(LPTR, cbSize);
  515. }
  516. void __cdecl operator delete(void *pv)
  517. {
  518. if (pv)
  519. LocalFree((HLOCAL)pv);
  520. }